xref: /aosp_15_r20/external/vboot_reference/tests/vb21_host_common_tests.c (revision 8617a60d3594060b7ecbd21bc622a7c14f3cf2bc)
1 /* Copyright 2014 The ChromiumOS Authors
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 "2common.h"
9 #include "2rsa.h"
10 #include "2sysincludes.h"
11 #include "common/tests.h"
12 #include "host_common21.h"
13 #include "host_key21.h"
14 #include "host_signature21.h"
15 
16 static const uint8_t test_data[] = "This is some test data to sign.";
17 
18 /*
19  * Test struct packing for vboot_struct.h structs which are passed between
20  * firmware and OS, or passed between different phases of firmware.
21  */
test_struct_packing(void)22 static void test_struct_packing(void)
23 {
24 	/* Test new struct sizes */
25 	TEST_EQ(EXPECTED_ID_SIZE,
26 		sizeof(struct vb2_id),
27 		"sizeof(vb2_id)");
28 	TEST_EQ(EXPECTED_VB21_STRUCT_COMMON_SIZE,
29 		sizeof(struct vb21_struct_common),
30 		"sizeof(vb21_struct_common)");
31 	TEST_EQ(EXPECTED_VB21_PACKED_KEY_SIZE,
32 		sizeof(struct vb21_packed_key),
33 		"sizeof(vb21_packed_key)");
34 	TEST_EQ(EXPECTED_VB21_SIGNATURE_SIZE,
35 		sizeof(struct vb21_signature),
36 		"sizeof(vb21_signature)");
37 }
38 
39 /**
40  * Common header functions
41  */
test_common_header_functions(void)42 static void test_common_header_functions(void)
43 {
44 	uint8_t cbuf[sizeof(struct vb21_struct_common) + 128];
45 	uint8_t cbufgood[sizeof(cbuf)];
46 	struct vb21_struct_common *c = (struct vb21_struct_common *)cbuf;
47 	struct vb21_struct_common *c2;
48 	const char test_desc[32] = "test desc";
49 	uint32_t desc_end, m;
50 
51 	c->total_size = sizeof(cbuf);
52 	c->fixed_size = sizeof(*c);
53 	c->desc_size = sizeof(test_desc);
54 	memcpy(cbuf + c->fixed_size, test_desc, sizeof(test_desc));
55 	desc_end = c->fixed_size + c->desc_size;
56 
57 	c2 = (struct vb21_struct_common *)(cbuf + desc_end);
58 	c2->total_size = c->total_size - desc_end;
59 	c2->fixed_size = sizeof(*c2);
60 	c2->desc_size = 0;
61 
62 	/* Description helper */
63 	TEST_EQ(0, strcmp(vb21_common_desc(c), test_desc), "vb21_common_desc()");
64 	TEST_EQ(0, strcmp(vb21_common_desc(c2), ""), "vb21_common_desc() empty");
65 
66 	TEST_SUCC(vb21_verify_common_header(cbuf, sizeof(cbuf)),
67 		  "vb21_verify_common_header() good");
68 	memcpy(cbufgood, cbuf, sizeof(cbufgood));
69 
70 	memcpy(cbuf, cbufgood, sizeof(cbuf));
71 	c->total_size += 4;
72 	TEST_EQ(vb21_verify_common_header(cbuf, sizeof(cbuf)),
73 		VB2_ERROR_COMMON_TOTAL_SIZE,
74 		"vb21_verify_common_header() total size");
75 
76 	memcpy(cbuf, cbufgood, sizeof(cbuf));
77 	c->fixed_size = c->total_size + 4;
78 	TEST_EQ(vb21_verify_common_header(cbuf, sizeof(cbuf)),
79 		VB2_ERROR_COMMON_FIXED_SIZE,
80 		"vb21_verify_common_header() fixed size");
81 
82 	memcpy(cbuf, cbufgood, sizeof(cbuf));
83 	c->desc_size = c->total_size - c->fixed_size + 4;
84 	TEST_EQ(vb21_verify_common_header(cbuf, sizeof(cbuf)),
85 		VB2_ERROR_COMMON_DESC_SIZE,
86 		"vb21_verify_common_header() desc size");
87 
88 	memcpy(cbuf, cbufgood, sizeof(cbuf));
89 	c->total_size--;
90 	TEST_EQ(vb21_verify_common_header(cbuf, sizeof(cbuf)),
91 		VB2_ERROR_COMMON_TOTAL_UNALIGNED,
92 		"vb21_verify_common_header() total unaligned");
93 
94 	memcpy(cbuf, cbufgood, sizeof(cbuf));
95 	c->fixed_size++;
96 	TEST_EQ(vb21_verify_common_header(cbuf, sizeof(cbuf)),
97 		VB2_ERROR_COMMON_FIXED_UNALIGNED,
98 		"vb21_verify_common_header() fixed unaligned");
99 
100 	memcpy(cbuf, cbufgood, sizeof(cbuf));
101 	c->desc_size--;
102 	TEST_EQ(vb21_verify_common_header(cbuf, sizeof(cbuf)),
103 		VB2_ERROR_COMMON_DESC_UNALIGNED,
104 		"vb21_verify_common_header() desc unaligned");
105 
106 	memcpy(cbuf, cbufgood, sizeof(cbuf));
107 	c->desc_size = -4;
108 	TEST_EQ(vb21_verify_common_header(cbuf, sizeof(cbuf)),
109 		VB2_ERROR_COMMON_DESC_WRAPS,
110 		"vb21_verify_common_header() desc wraps");
111 
112 	memcpy(cbuf, cbufgood, sizeof(cbuf));
113 	cbuf[desc_end - 1] = 1;
114 	TEST_EQ(vb21_verify_common_header(cbuf, sizeof(cbuf)),
115 		VB2_ERROR_COMMON_DESC_TERMINATOR,
116 		"vb21_verify_common_header() desc not terminated");
117 
118 	/* Member checking function */
119 	memcpy(cbuf, cbufgood, sizeof(cbuf));
120 	m = 0;
121 	TEST_SUCC(vb21_verify_common_member(cbuf, &m, c->total_size - 8, 4),
122 		  "vb21_verify_common_member()");
123 	TEST_EQ(m, c->total_size - 4, "  new minimum");
124 
125 	m = desc_end;
126 	TEST_SUCC(vb21_verify_common_member(cbuf, &m, desc_end, 4),
127 		  "vb21_verify_common_member() good offset");
128 	TEST_EQ(m, desc_end + 4, "  new minimum");
129 
130 	m = 0;
131 	TEST_EQ(vb21_verify_common_member(cbuf, &m, c->total_size - 8, -4),
132 		VB2_ERROR_COMMON_MEMBER_WRAPS,
133 		"vb21_verify_common_member() wraps");
134 
135 	m = 0;
136 	TEST_EQ(vb21_verify_common_member(cbuf, &m, c->total_size - 7, 4),
137 		VB2_ERROR_COMMON_MEMBER_UNALIGNED,
138 		"vb21_verify_common_member() offset unaligned");
139 
140 	m = 0;
141 	TEST_EQ(vb21_verify_common_member(cbuf, &m, c->total_size - 8, 5),
142 		VB2_ERROR_COMMON_MEMBER_UNALIGNED,
143 		"vb21_verify_common_member() size unaligned");
144 
145 	m = 0;
146 	TEST_EQ(vb21_verify_common_member(cbuf, &m, desc_end - 4, 4),
147 		VB2_ERROR_COMMON_MEMBER_OVERLAP,
148 		"vb21_verify_common_member() overlap");
149 
150 	m = desc_end + 4;
151 	TEST_EQ(vb21_verify_common_member(cbuf, &m, desc_end, 4),
152 		VB2_ERROR_COMMON_MEMBER_OVERLAP,
153 		"vb21_verify_common_member() overlap 2");
154 
155 	m = 0;
156 	TEST_EQ(vb21_verify_common_member(cbuf, &m, c->total_size - 4, 8),
157 		VB2_ERROR_COMMON_MEMBER_SIZE,
158 		"vb21_verify_common_member() size");
159 
160 	/* Subobject checking */
161 	m = 0;
162 	TEST_SUCC(vb21_verify_common_subobject(cbuf, &m, desc_end),
163 		  "vb21_verify_common_subobject() good offset");
164 	TEST_EQ(m, sizeof(cbuf), "  new minimum");
165 
166 	m = desc_end + 4;
167 	TEST_EQ(vb21_verify_common_subobject(cbuf, &m, desc_end),
168 		VB2_ERROR_COMMON_MEMBER_OVERLAP,
169 		"vb21_verify_common_subobject() overlap");
170 
171 	m = 0;
172 	c2->total_size += 4;
173 	TEST_EQ(vb21_verify_common_subobject(cbuf, &m, desc_end),
174 		VB2_ERROR_COMMON_TOTAL_SIZE,
175 		"vb21_verify_common_subobject() size");
176 }
177 
178 /**
179  * Signature size
180  */
test_sig_size(void)181 static void test_sig_size(void)
182 {
183 	TEST_EQ(vb2_sig_size(VB2_SIG_INVALID, VB2_HASH_SHA256), 0,
184 		"vb2_sig_size() sig invalid");
185 
186 	TEST_EQ(vb2_sig_size(VB2_SIG_RSA2048, VB2_HASH_INVALID), 0,
187 		"vb2_sig_size() hash invalid");
188 
189 	TEST_EQ(vb2_sig_size(VB2_SIG_RSA2048, VB2_HASH_SHA256), 2048 / 8,
190 		"vb2_sig_size() RSA2048");
191 	TEST_EQ(vb2_sig_size(VB2_SIG_RSA4096, VB2_HASH_SHA256), 4096 / 8,
192 		"vb2_sig_size() RSA4096");
193 	TEST_EQ(vb2_sig_size(VB2_SIG_RSA8192, VB2_HASH_SHA512), 8192 / 8,
194 		"vb2_sig_size() RSA8192");
195 
196 	TEST_EQ(vb2_sig_size(VB2_SIG_NONE, VB2_HASH_SHA1),
197 		VB2_SHA1_DIGEST_SIZE, "vb2_sig_size() SHA1");
198 	TEST_EQ(vb2_sig_size(VB2_SIG_NONE, VB2_HASH_SHA256),
199 		VB2_SHA256_DIGEST_SIZE, "vb2_sig_size() SHA256");
200 	TEST_EQ(vb2_sig_size(VB2_SIG_NONE, VB2_HASH_SHA512),
201 		VB2_SHA512_DIGEST_SIZE, "vb2_sig_size() SHA512");
202 }
203 
204 /**
205  * Verify data on bare hash
206  */
test_verify_hash(void)207 static void test_verify_hash(void)
208 {
209 	struct vb21_signature *sig;
210 	const struct vb2_private_key *prik;
211 	struct vb2_public_key pubk;
212 	uint8_t workbuf[VB2_VERIFY_DATA_WORKBUF_BYTES]
213 		 __attribute__((aligned(VB2_WORKBUF_ALIGN)));
214 	struct vb2_workbuf wb;
215 
216 	vb2_workbuf_init(&wb, workbuf, sizeof(workbuf));
217 
218 	TEST_SUCC(vb2_private_key_hash(&prik, VB2_HASH_SHA256),
219 		  "create private hash key");
220 	TEST_SUCC(vb2_public_key_hash(&pubk, VB2_HASH_SHA256),
221 		  "create hash key");
222 
223 	/* Create the signature */
224 	TEST_SUCC(vb21_sign_data(&sig, test_data, sizeof(test_data),
225 				 prik, NULL),
226 		  "create hash sig");
227 
228 	TEST_SUCC(vb21_verify_data(test_data, sizeof(test_data),
229 				   sig, &pubk, &wb),
230 		  "vb21_verify_data() hash ok");
231 
232 	*((uint8_t *)sig + sig->sig_offset) ^= 0xab;
233 	TEST_EQ(vb21_verify_data(test_data, sizeof(test_data), sig, &pubk, &wb),
234 		VB2_ERROR_VDATA_VERIFY_DIGEST, "vb21_verify_data() hash bad");
235 
236 	free(sig);
237 }
238 
main(int argc,char * argv[])239 int main(int argc, char* argv[])
240 {
241 	test_struct_packing();
242 	test_common_header_functions();
243 	test_sig_size();
244 	test_verify_hash();
245 
246 	return gTestSuccess ? 0 : 255;
247 }
248