1 /* Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
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 sanity 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 #include "utility.h"
20 
21 /* Runs [op] and ensures it returns success and doesn't run longer than
22  * [time_limit] in milliseconds.
23  */
24 #define TTPM_CHECK(op, time_limit) do {                                 \
25     struct timeval before, after;                                       \
26     int time;                                                           \
27     uint32_t __result;                                                  \
28     gettimeofday(&before, NULL);                                        \
29     __result = op;                                                      \
30     if (__result != TPM_SUCCESS) {                                      \
31       printf(#op ": error 0x%x\n", __result);                           \
32       errors++;                                                          \
33     }                                                                   \
34     gettimeofday(&after, NULL);                                         \
35     time = (int) ((after.tv_sec - before.tv_sec) * 1000 +               \
36                   (after.tv_usec - before.tv_usec) / 1000);             \
37     printf(#op ": %d ms\n", time);                                      \
38     if (time > time_limit) {                                            \
39       printf(#op " exceeded " #time_limit " ms\n");                     \
40       time_limit_exceeded = 1;                                          \
41     }                                                                   \
42   } while (0)
43 
main(int argc,char ** argv)44 int main(int argc, char** argv) {
45   uint32_t x;
46   uint8_t in[20], out[20];
47   int time_limit_exceeded = 0;
48   int errors = 0;
49 
50   TlclLibInit();
51   TTPM_CHECK(0, 50);
52   TTPM_CHECK(TlclStartupIfNeeded(), 50);
53   TTPM_CHECK(TlclContinueSelfTest(), 100);
54   TTPM_CHECK(TlclSelfTestFull(), 1000);
55   TTPM_CHECK(TlclAssertPhysicalPresence(), 100);
56   TTPM_CHECK(TlclWrite(INDEX0, (uint8_t*) &x, sizeof(x)), 100);
57   TTPM_CHECK(TlclRead(INDEX0, (uint8_t*) &x, sizeof(x)), 100);
58   TTPM_CHECK(TlclExtend(0, in, out), 200);
59   TTPM_CHECK(TlclSetGlobalLock(), 50);
60   TTPM_CHECK(TlclLockPhysicalPresence(), 100);
61   if (time_limit_exceeded || errors > 0) {
62     printf("TEST FAILED\n");
63     exit(1);
64   } else {
65     printf("TEST SUCCEEDED\n");
66     return 0;
67   }
68 }
69