summaryrefslogtreecommitdiff
path: root/tests/tpm_lite/tpmtest_timing.c
blob: eaba52f4532e7208f771f13629fecc462fbe115c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/* Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

/* Timing test for various TPM operations.  This is mostly a sanity check to
 * make sure the part doesn't have ridicolously bad timing on simple
 * operations.
 */

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <sys/time.h>
#include <time.h>

#include "tlcl.h"
#include "tlcl_tests.h"

/* Runs [op] and ensures it returns success and doesn't run longer than
 * [time_limit] in milliseconds.
 */
#define TTPM_CHECK(op, time_limit) do {                          \
	struct timeval before, after;                            \
	int time;                                                \
	uint32_t __result;                                       \
	gettimeofday(&before, NULL);                             \
	__result = op;                                           \
	if (__result != TPM_SUCCESS) {                           \
		printf(#op ": error %#x\n", __result);          \
		errors++;                                        \
	}                                                        \
	gettimeofday(&after, NULL);                              \
	time = (int) ((after.tv_sec - before.tv_sec) * 1000 +    \
		      (after.tv_usec - before.tv_usec) / 1000);  \
	printf(#op ": %d ms\n", time);                           \
	if (time > time_limit) {                                 \
		printf(#op " exceeded " #time_limit " ms\n");    \
		time_limit_exceeded = 1;                         \
	}                                                        \
} while (0)

int main(int argc, char** argv) {
	uint32_t x;
	uint8_t in[20], out[20];
	int time_limit_exceeded = 0;
	int errors = 0;

	TlclLibInit();
	TTPM_CHECK(0, 50);
	TTPM_CHECK(TlclStartupIfNeeded(), 50);
	TTPM_CHECK(TlclContinueSelfTest(), 100);
	TTPM_CHECK(TlclSelfTestFull(), 1000);
	TTPM_CHECK(TlclAssertPhysicalPresence(), 100);
	TTPM_CHECK(TlclWrite(INDEX0, (uint8_t*) &x, sizeof(x)), 100);
	TTPM_CHECK(TlclRead(INDEX0, (uint8_t*) &x, sizeof(x)), 100);
	TTPM_CHECK(TlclExtend(0, in, out), 200);
	TTPM_CHECK(TlclSetGlobalLock(), 50);
	TTPM_CHECK(TlclLockPhysicalPresence(), 100);
	if (time_limit_exceeded || errors > 0) {
		printf("TEST FAILED\n");
		exit(1);
	} else {
		printf("TEST SUCCEEDED\n");
		return 0;
	}
}