summaryrefslogtreecommitdiff
path: root/tests/tpm_lite/timing.c
blob: 9300d0ccaace68b1dc9a6f1d6d7a5a2a1e9600d7 (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
/* 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"
#include "utility.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 0x%x\n", __result);                           \
      exit(1);                                                          \
    }                                                                   \
    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");                     \
      exit(1);                                                          \
    }                                                                   \
  } while (0)

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

  TlclLibInit();
  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);
  printf("TEST SUCCEEDED\n");
  return 0;
}