• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* 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 
8 #include <stdint.h>
9 #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 
19 extern const char* kBootStateSHA1Digests[];
20 
21 /* Last in_digest passed to TlclExtend() for each PCR */
22 static const uint8_t *last_in[20];
23 
24 /* Return value to pass for TlclExtend() */
25 static uint32_t extend_returns;
26 
27 /* How many calls to TlclExtend() should one SetTPMBootModeState() make? */
28 static int expected_extend_count;
29 /* How many did we get? */
30 static int actual_extend_count;
31 
32 static GoogleBinaryBlockHeader gbb_v1 = {
33 	.major_version = GBB_MAJOR_VER,
34 	.minor_version = 1,
35 };
36 
37 static GoogleBinaryBlockHeader gbb_v2 = {
38 	.major_version = GBB_MAJOR_VER,
39 	.minor_version = 2,
40 	.hwid_digest = {1, 2, 3, 4,},
41 };
42 
43 /* Mocked TlclExtend() function for testing */
TlclExtend(int pcr_num,const uint8_t * in_digest,uint8_t * out_digest)44 uint32_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");
49 
50 	last_in[actual_extend_count] = in_digest;
51 
52 	actual_extend_count++;
53 	return extend_returns;
54 }
55 
56 
57 /* Test setting TPM boot mode state */
BootStateTest(void)58 static void BootStateTest(void)
59 {
60 	int recdev;
61 	int flags;
62 	int index;
63 	char what[128];
64 
65 	/* 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;
75 
76 			/* 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);
94 
95 			/* 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");
140 }
141 
main(int argc,char * argv[])142 int main(int argc, char *argv[])
143 {
144 	int error_code = 0;
145 
146 	BootStateTest();
147 
148 	if (!gTestSuccess)
149 		error_code = 255;
150 
151 	return error_code;
152 }
153