• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright (c) 2014 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 firmware 2common.c
6  */
7 
8 #include "2sysincludes.h"
9 #include "vb2_common.h"
10 #include "vboot_struct.h"  /* For old struct sizes */
11 #include "test_common.h"
12 
13 /*
14  * Test struct packing for vboot_struct.h structs which are passed between
15  * firmware and OS, or passed between different phases of firmware.
16  */
test_struct_packing(void)17 static void test_struct_packing(void)
18 {
19 	/* Test vboot2 versions of vboot1 structs */
20 	TEST_EQ(EXPECTED_VB2_PACKED_KEY_SIZE,
21 		sizeof(struct vb2_packed_key),
22 		"sizeof(vb2_packed_key)");
23 	TEST_EQ(EXPECTED_VB2_SIGNATURE_SIZE,
24 		sizeof(struct vb2_signature),
25 		"sizeof(vb2_signature)");
26 	TEST_EQ(EXPECTED_VB2_KEYBLOCK_SIZE,
27 		sizeof(struct vb2_keyblock),
28 		"sizeof(vb2_keyblock)");
29 	TEST_EQ(EXPECTED_VB2_FW_PREAMBLE_SIZE,
30 		sizeof(struct vb2_fw_preamble),
31 		"sizeof(vb2_fw_preamble)");
32 	TEST_EQ(EXPECTED_VB2_GBB_HEADER_SIZE,
33 		sizeof(struct vb2_gbb_header),
34 		"sizeof(vb2_gbb_header)");
35 
36 	/* And make sure they're the same as their vboot1 equivalents */
37 	TEST_EQ(EXPECTED_VB2_PACKED_KEY_SIZE,
38 		EXPECTED_VBPUBLICKEY_SIZE,
39 		"vboot1->2 packed key sizes same");
40 	TEST_EQ(EXPECTED_VB2_SIGNATURE_SIZE,
41 		EXPECTED_VBSIGNATURE_SIZE,
42 		"vboot1->2 signature sizes same");
43 	TEST_EQ(EXPECTED_VB2_KEYBLOCK_SIZE,
44 		EXPECTED_VBKEYBLOCKHEADER_SIZE,
45 		"vboot1->2 keyblock sizes same");
46 	TEST_EQ(EXPECTED_VB2_FW_PREAMBLE_SIZE,
47 		EXPECTED_VBFIRMWAREPREAMBLEHEADER2_1_SIZE,
48 		"vboot1->2 firmware preamble sizes same");
49 }
50 
51 /**
52  * Helper functions not dependent on specific key sizes
53  */
test_helper_functions(void)54 static void test_helper_functions(void)
55 {
56 	{
57 		uint8_t *p = (uint8_t *)test_helper_functions;
58 		TEST_EQ((int)vb2_offset_of(p, p), 0, "vb2_offset_of() equal");
59 		TEST_EQ((int)vb2_offset_of(p, p+10), 10,
60 			"vb2_offset_of() positive");
61 	}
62 
63 	{
64 		struct vb2_packed_key k = {.key_offset = sizeof(k)};
65 		TEST_EQ((int)vb2_offset_of(&k, vb2_packed_key_data(&k)),
66 			sizeof(k), "vb2_packed_key_data() adjacent");
67 	}
68 
69 	{
70 		struct vb2_packed_key k = {.key_offset = 123};
71 		TEST_EQ((int)vb2_offset_of(&k, vb2_packed_key_data(&k)), 123,
72 			"vb2_packed_key_data() spaced");
73 	}
74 
75 	{
76 		struct vb2_signature s = {.sig_offset = sizeof(s)};
77 		TEST_EQ((int)vb2_offset_of(&s, vb2_signature_data(&s)),
78 			sizeof(s), "vb2_signature_data() adjacent");
79 	}
80 
81 	{
82 		struct vb2_signature s = {.sig_offset = 123};
83 		TEST_EQ((int)vb2_offset_of(&s, vb2_signature_data(&s)), 123,
84 			"vb2_signature_data() spaced");
85 	}
86 
87 	{
88 		uint8_t *p = (uint8_t *)test_helper_functions;
89 		TEST_SUCC(vb2_verify_member_inside(p, 20, p, 6, 11, 3),
90 			  "MemberInside ok 1");
91 		TEST_SUCC(vb2_verify_member_inside(p, 20, p+4, 4, 8, 4),
92 			  "MemberInside ok 2");
93 		TEST_EQ(vb2_verify_member_inside(p, 20, p-4, 4, 8, 4),
94 			VB2_ERROR_INSIDE_MEMBER_OUTSIDE,
95 			"MemberInside member before parent");
96 		TEST_EQ(vb2_verify_member_inside(p, 20, p+20, 4, 8, 4),
97 			VB2_ERROR_INSIDE_MEMBER_OUTSIDE,
98 			"MemberInside member after parent");
99 		TEST_EQ(vb2_verify_member_inside(p, 20, p, 21, 0, 0),
100 			VB2_ERROR_INSIDE_MEMBER_OUTSIDE,
101 			"MemberInside member too big");
102 		TEST_EQ(vb2_verify_member_inside(p, 20, p, 4, 21, 0),
103 			VB2_ERROR_INSIDE_DATA_OUTSIDE,
104 			"MemberInside data after parent");
105 		TEST_EQ(vb2_verify_member_inside(p, 20, p, 4, SIZE_MAX, 0),
106 			VB2_ERROR_INSIDE_DATA_OUTSIDE,
107 			"MemberInside data before parent");
108 		TEST_EQ(vb2_verify_member_inside(p, 20, p, 4, 4, 17),
109 			VB2_ERROR_INSIDE_DATA_OUTSIDE,
110 			"MemberInside data too big");
111 		TEST_EQ(vb2_verify_member_inside(p, 20, p, 8, 4, 8),
112 			VB2_ERROR_INSIDE_DATA_OVERLAP,
113 			"MemberInside data overlaps member");
114 		TEST_EQ(vb2_verify_member_inside(p, -8, p, 12, 0, 0),
115 			VB2_ERROR_INSIDE_PARENT_WRAPS,
116 			"MemberInside wraparound 1");
117 		TEST_EQ(vb2_verify_member_inside(p, 20, p, -8, 0, 0),
118 			VB2_ERROR_INSIDE_MEMBER_WRAPS,
119 			"MemberInside wraparound 2");
120 		TEST_EQ(vb2_verify_member_inside(p, 20, p, 4, 4, -12),
121 			VB2_ERROR_INSIDE_DATA_WRAPS,
122 			"MemberInside wraparound 3");
123 	}
124 
125 	{
126 		struct vb2_packed_key k = {.key_offset = sizeof(k),
127 					   .key_size = 128};
128 		TEST_SUCC(vb2_verify_packed_key_inside(&k, sizeof(k)+128, &k),
129 			  "PublicKeyInside ok 1");
130 		TEST_SUCC(vb2_verify_packed_key_inside(&k - 1,
131 						       2*sizeof(k)+128, &k),
132 			  "PublicKeyInside ok 2");
133 		TEST_EQ(vb2_verify_packed_key_inside(&k, 128, &k),
134 			VB2_ERROR_INSIDE_DATA_OUTSIDE,
135 			"PublicKeyInside key too big");
136 	}
137 
138 	{
139 		struct vb2_packed_key k = {.key_offset = 100,
140 					   .key_size = 4};
141 		TEST_EQ(vb2_verify_packed_key_inside(&k, 99, &k),
142 			VB2_ERROR_INSIDE_DATA_OUTSIDE,
143 			"PublicKeyInside offset too big");
144 	}
145 
146 	{
147 		struct vb2_signature s = {.sig_offset = sizeof(s),
148 					  .sig_size = 128};
149 		TEST_SUCC(vb2_verify_signature_inside(&s, sizeof(s)+128, &s),
150 			"SignatureInside ok 1");
151 		TEST_SUCC(vb2_verify_signature_inside(&s - 1,
152 						      2*sizeof(s)+128, &s),
153 			  "SignatureInside ok 2");
154 		TEST_EQ(vb2_verify_signature_inside(&s, 128, &s),
155 			VB2_ERROR_INSIDE_DATA_OUTSIDE,
156 			"SignatureInside sig too big");
157 	}
158 
159 	{
160 		struct vb2_signature s = {.sig_offset = 100,
161 					  .sig_size = 4};
162 		TEST_EQ(vb2_verify_signature_inside(&s, 99, &s),
163 			VB2_ERROR_INSIDE_DATA_OUTSIDE,
164 			"SignatureInside offset too big");
165 	}
166 }
167 
main(int argc,char * argv[])168 int main(int argc, char* argv[])
169 {
170 	test_struct_packing();
171 	test_helper_functions();
172 
173 	return gTestSuccess ? 0 : 255;
174 }
175