xref: /aosp_15_r20/external/vboot_reference/tests/tpm_lite/tpmtest_timing.c (revision 8617a60d3594060b7ecbd21bc622a7c14f3cf2bc)
1 /* Copyright 2010 The ChromiumOS Authors
2  * Use of this source code is governed by a BSD-style license that can be
3  * found in the LICENSE file.
4  */
5 
6 /* Timing test for various TPM operations.  This is mostly a validity check to
7  * make sure the part doesn't have ridicolously bad timing on simple
8  * operations.
9  */
10 
11 #include <stdio.h>
12 #include <stdint.h>
13 #include <stdlib.h>
14 #include <sys/time.h>
15 #include <time.h>
16 
17 #include "tlcl.h"
18 #include "tlcl_tests.h"
19 
20 /* Runs [op] and ensures it returns success and doesn't run longer than
21  * [time_limit] in milliseconds.
22  */
23 #define TTPM_CHECK(op, time_limit) do {                          \
24 	struct timeval before, after;                            \
25 	int time;                                                \
26 	uint32_t __result;                                       \
27 	gettimeofday(&before, NULL);                             \
28 	__result = op;                                           \
29 	if (__result != TPM_SUCCESS) {                           \
30 		printf(#op ": error %#x\n", __result);          \
31 		errors++;                                        \
32 	}                                                        \
33 	gettimeofday(&after, NULL);                              \
34 	time = (int) ((after.tv_sec - before.tv_sec) * 1000 +    \
35 		      (after.tv_usec - before.tv_usec) / 1000);  \
36 	printf(#op ": %d ms\n", time);                           \
37 	if (time > time_limit) {                                 \
38 		printf(#op " exceeded " #time_limit " ms\n");    \
39 		time_limit_exceeded = 1;                         \
40 	}                                                        \
41 } while (0)
42 
main(int argc,char ** argv)43 int main(int argc, char** argv) {
44 	uint32_t x;
45 	uint8_t in[20], out[20];
46 	int time_limit_exceeded = 0;
47 	int errors = 0;
48 
49 	TlclLibInit();
50 	TTPM_CHECK(0, 50);
51 	TTPM_CHECK(TlclStartupIfNeeded(), 50);
52 	TTPM_CHECK(TlclContinueSelfTest(), 100);
53 	TTPM_CHECK(TlclSelfTestFull(), 1000);
54 	TTPM_CHECK(TlclAssertPhysicalPresence(), 100);
55 	TTPM_CHECK(TlclWrite(INDEX0, (uint8_t*) &x, sizeof(x)), 100);
56 	TTPM_CHECK(TlclRead(INDEX0, (uint8_t*) &x, sizeof(x)), 100);
57 	TTPM_CHECK(TlclExtend(0, in, out), 200);
58 	TTPM_CHECK(TlclSetGlobalLock(), 50);
59 	TTPM_CHECK(TlclLockPhysicalPresence(), 100);
60 	if (time_limit_exceeded || errors > 0) {
61 		printf("TEST FAILED\n");
62 		exit(1);
63 	} else {
64 		printf("TEST SUCCEEDED\n");
65 		return 0;
66 	}
67 }
68