blob: 64171b56ab7321caf5afbbd0e006268a92bdb794 [file] [log] [blame]
Randall Spangleraeb86322011-08-24 18:35:19 -07001/* Copyright (c) 2011 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 * Tests for tpm_bootmode functions
6 */
7
Bill Richardson0c3ba242013-03-29 11:09:30 -07008#include <stdint.h>
Randall Spangleraeb86322011-08-24 18:35:19 -07009#include <stdio.h>
10#include <stdlib.h>
11#include <string.h>
12
13#define _STUB_IMPLEMENTATION_ /* So we can use memset() ourselves */
14
15#include "test_common.h"
16#include "utility.h"
17#include "tpm_bootmode.h"
18
19extern const char* kBootStateSHA1Digests[];
20
Bill Richardsonf4f395e2014-10-22 17:42:20 -070021/* Last in_digest passed to TlclExtend() for each PCR */
22static const uint8_t *last_in[20];
Randall Spangleraeb86322011-08-24 18:35:19 -070023
24/* Return value to pass for TlclExtend() */
Bill Richardsonf4f395e2014-10-22 17:42:20 -070025static uint32_t extend_returns;
26
27/* How many calls to TlclExtend() should one SetTPMBootModeState() make? */
28static int expected_extend_count;
29/* How many did we get? */
30static int actual_extend_count;
31
32static GoogleBinaryBlockHeader gbb_v1 = {
33 .major_version = GBB_MAJOR_VER,
34 .minor_version = 1,
35};
36
37static GoogleBinaryBlockHeader gbb_v2 = {
38 .major_version = GBB_MAJOR_VER,
39 .minor_version = 2,
40 .hwid_digest = {1, 2, 3, 4,},
41};
Randall Spangleraeb86322011-08-24 18:35:19 -070042
43/* Mocked TlclExtend() function for testing */
Bill Richardsonf4f395e2014-10-22 17:42:20 -070044uint32_t TlclExtend(int pcr_num, const uint8_t *in_digest,
45 uint8_t *out_digest)
46{
47 /* Should be using correct pcr */
48 TEST_EQ(pcr_num, actual_extend_count, "TlclExtend pcr_num");
Randall Spangleraeb86322011-08-24 18:35:19 -070049
Bill Richardsonf4f395e2014-10-22 17:42:20 -070050 last_in[actual_extend_count] = in_digest;
Randall Spangleraeb86322011-08-24 18:35:19 -070051
Bill Richardsonf4f395e2014-10-22 17:42:20 -070052 actual_extend_count++;
53 return extend_returns;
Randall Spangleraeb86322011-08-24 18:35:19 -070054}
55
56
57/* Test setting TPM boot mode state */
Bill Richardsonf4f395e2014-10-22 17:42:20 -070058static void BootStateTest(void)
59{
60 int recdev;
61 int flags;
62 int index;
63 char what[128];
Randall Spangleraeb86322011-08-24 18:35:19 -070064
Bill Richardsonf4f395e2014-10-22 17:42:20 -070065 /* Test all permutations of developer and recovery mode */
66 for (recdev = 0; recdev < 4; recdev++) {
67 /* Exhaustively test all permutations of key block flags
68 * currently defined in vboot_struct.h (KEY_BLOCK_FLAG_*) */
69 for (flags = 0; flags < 16; flags++) {
70 index = recdev * 3;
71 if (6 == flags)
72 index += 2;
73 else if (7 == flags)
74 index += 1;
Randall Spangleraeb86322011-08-24 18:35:19 -070075
Bill Richardsonf4f395e2014-10-22 17:42:20 -070076 /* Passing a null pointer for GBB */
77 memset(last_in, 0, sizeof(last_in));
78 actual_extend_count = 0;
79 expected_extend_count = 1;
80 TEST_EQ(SetTPMBootModeState(recdev & 2, recdev & 1,
81 flags, 0), 0,
82 "SetTPMBootModeState return (gbb0)");
83 snprintf(what, sizeof(what),
84 "SetTPMBootModeState %d, 0x%x (gbb0)",
85 recdev, flags);
86 TEST_PTR_EQ(last_in[0],
87 kBootStateSHA1Digests[index], what);
88 TEST_EQ(expected_extend_count, actual_extend_count,
89 "Expected TlclExtend call count (gbb0)");
90 snprintf(what, sizeof(what),
91 "SetTPMBootModeState %d, 0x%x (gbb0) PCR1",
92 recdev, flags);
93 TEST_PTR_EQ(last_in[1], NULL, what);
Randall Spangleraeb86322011-08-24 18:35:19 -070094
Bill Richardsonf4f395e2014-10-22 17:42:20 -070095 /* GBB v1.1 - should be exactly the same */
96 memset(last_in, 0, sizeof(last_in));
97 actual_extend_count = 0;
98 expected_extend_count = 1;
99 TEST_EQ(SetTPMBootModeState(recdev & 2, recdev & 1,
100 flags, &gbb_v1), 0,
101 "SetTPMBootModeState return (gbb1)");
102 snprintf(what, sizeof(what),
103 "SetTPMBootModeState %d, 0x%x (gbb1)",
104 recdev, flags);
105 TEST_PTR_EQ(last_in[0],
106 kBootStateSHA1Digests[index], what);
107 TEST_EQ(expected_extend_count, actual_extend_count,
108 "Expected TlclExtend call count (gbb1)");
109 snprintf(what, sizeof(what),
110 "SetTPMBootModeState %d, 0x%x (gbb1) PCR1",
111 recdev, flags);
112 TEST_PTR_EQ(last_in[1], NULL, what);
113
114 /* GBB v1.2 - should extend PCR1 with HWID digest */
115 memset(last_in, 0, sizeof(last_in));
116 actual_extend_count = 0;
117 expected_extend_count = 2;
118 TEST_EQ(SetTPMBootModeState(recdev & 2, recdev & 1,
119 flags, &gbb_v2), 0,
120 "SetTPMBootModeState return (gbb2)");
121 snprintf(what, sizeof(what),
122 "SetTPMBootModeState %d, 0x%x (gbb2)",
123 recdev, flags);
124 TEST_PTR_EQ(last_in[0],
125 kBootStateSHA1Digests[index], what);
126 TEST_EQ(expected_extend_count, actual_extend_count,
127 "Expected TlclExtend call count (gbb2)");
128 snprintf(what, sizeof(what),
129 "SetTPMBootModeState %d, 0x%x (gbb2) PCR1",
130 recdev, flags);
131 TEST_PTR_EQ(last_in[1], gbb_v2.hwid_digest, what);
132 }
133 }
134
135 extend_returns = 1;
136 actual_extend_count = 0;
137 expected_extend_count = 1;
138 TEST_EQ(SetTPMBootModeState(0, 0, 0, 0), 1,
139 "SetTPMBootModeState error");
Randall Spangleraeb86322011-08-24 18:35:19 -0700140}
141
Bill Richardsonf4f395e2014-10-22 17:42:20 -0700142int main(int argc, char *argv[])
143{
144 int error_code = 0;
Randall Spangleraeb86322011-08-24 18:35:19 -0700145
Bill Richardsonf4f395e2014-10-22 17:42:20 -0700146 BootStateTest();
Randall Spangleraeb86322011-08-24 18:35:19 -0700147
Bill Richardsonf4f395e2014-10-22 17:42:20 -0700148 if (!gTestSuccess)
149 error_code = 255;
Randall Spangleraeb86322011-08-24 18:35:19 -0700150
Bill Richardsonf4f395e2014-10-22 17:42:20 -0700151 return error_code;
Randall Spangleraeb86322011-08-24 18:35:19 -0700152}