blob: d2ac0edf906094a0040445df0b482dfae456fc38 [file] [log] [blame]
Randall Spangler108d9912014-12-02 15:55:56 -08001/* 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 "2common.h"
10#include "2rsa.h"
11#include "vb2_common.h"
12#include "host_fw_preamble2.h"
13#include "host_key2.h"
14#include "host_keyblock2.h"
15#include "host_signature2.h"
16
17#include "test_common.h"
18
19static const uint8_t test_data[] = "This is some test data to sign.";
20static const uint8_t test_data2[] = "Some more test data";
21static const uint8_t test_data3[] = "Even more test data";
22
23/*
24 * Test struct packing for vboot_struct.h structs which are passed between
25 * firmware and OS, or passed between different phases of firmware.
26 */
27static void test_struct_packing(void)
28{
29 /* Test new struct sizes */
30 TEST_EQ(EXPECTED_GUID_SIZE,
31 sizeof(struct vb2_guid),
32 "sizeof(vb2_guid)");
33 TEST_EQ(EXPECTED_VB2_STRUCT_COMMON_SIZE,
34 sizeof(struct vb2_struct_common),
35 "sizeof(vb2_struct_common)");
Randall Spangler308d2542014-12-04 09:54:37 -080036 TEST_EQ(EXPECTED_VB2_PACKED_KEY_SIZE,
37 sizeof(struct vb2_packed_key),
38 "sizeof(vb2_packed_key)");
39 TEST_EQ(EXPECTED_VB2_SIGNATURE_SIZE,
40 sizeof(struct vb2_signature),
41 "sizeof(vb2_signature)");
42 TEST_EQ(EXPECTED_VB2_KEYBLOCK_SIZE,
43 sizeof(struct vb2_keyblock),
44 "sizeof(vb2_keyblock)");
45 TEST_EQ(EXPECTED_VB2_FW_PREAMBLE_SIZE,
46 sizeof(struct vb2_fw_preamble),
47 "sizeof(vb2_fw_preamble)");
Randall Spangler108d9912014-12-02 15:55:56 -080048}
49
50/**
51 * Common header functions
52 */
53static void test_common_header_functions(void)
54{
55 uint8_t cbuf[sizeof(struct vb2_struct_common) + 128];
56 uint8_t cbufgood[sizeof(cbuf)];
57 struct vb2_struct_common *c = (struct vb2_struct_common *)cbuf;
58 struct vb2_struct_common *c2;
59 const char test_desc[32] = "test desc";
60 uint32_t desc_end, m;
61
62 c->total_size = sizeof(cbuf);
63 c->fixed_size = sizeof(*c);
64 c->desc_size = sizeof(test_desc);
65 memcpy(cbuf + c->fixed_size, test_desc, sizeof(test_desc));
66 desc_end = c->fixed_size + c->desc_size;
67
68 c2 = (struct vb2_struct_common *)(cbuf + desc_end);
69 c2->total_size = c->total_size - desc_end;
70 c2->fixed_size = sizeof(*c2);
71 c2->desc_size = 0;
72
73 /* Description helper */
74 TEST_EQ(0, strcmp(vb2_common_desc(c), test_desc), "vb2_common_desc()");
75 TEST_EQ(0, strcmp(vb2_common_desc(c2), ""), "vb2_common_desc() empty");
76
77 TEST_SUCC(vb2_verify_common_header(cbuf, sizeof(cbuf)),
78 "vb2_verify_common_header() good");
79 memcpy(cbufgood, cbuf, sizeof(cbufgood));
80
81 memcpy(cbuf, cbufgood, sizeof(cbuf));
82 c->total_size += 4;
83 TEST_EQ(vb2_verify_common_header(cbuf, sizeof(cbuf)),
84 VB2_ERROR_COMMON_TOTAL_SIZE,
85 "vb2_verify_common_header() total size");
86
87 memcpy(cbuf, cbufgood, sizeof(cbuf));
88 c->fixed_size = c->total_size + 4;
89 TEST_EQ(vb2_verify_common_header(cbuf, sizeof(cbuf)),
90 VB2_ERROR_COMMON_FIXED_SIZE,
91 "vb2_verify_common_header() fixed size");
92
93 memcpy(cbuf, cbufgood, sizeof(cbuf));
94 c->desc_size = c->total_size - c->fixed_size + 4;
95 TEST_EQ(vb2_verify_common_header(cbuf, sizeof(cbuf)),
96 VB2_ERROR_COMMON_DESC_SIZE,
97 "vb2_verify_common_header() desc size");
98
99 memcpy(cbuf, cbufgood, sizeof(cbuf));
100 c->total_size--;
101 TEST_EQ(vb2_verify_common_header(cbuf, sizeof(cbuf)),
102 VB2_ERROR_COMMON_TOTAL_UNALIGNED,
103 "vb2_verify_common_header() total unaligned");
104
105 memcpy(cbuf, cbufgood, sizeof(cbuf));
106 c->fixed_size++;
107 TEST_EQ(vb2_verify_common_header(cbuf, sizeof(cbuf)),
108 VB2_ERROR_COMMON_FIXED_UNALIGNED,
109 "vb2_verify_common_header() fixed unaligned");
110
111 memcpy(cbuf, cbufgood, sizeof(cbuf));
112 c->desc_size--;
113 TEST_EQ(vb2_verify_common_header(cbuf, sizeof(cbuf)),
114 VB2_ERROR_COMMON_DESC_UNALIGNED,
115 "vb2_verify_common_header() desc unaligned");
116
117 memcpy(cbuf, cbufgood, sizeof(cbuf));
118 c->desc_size = -4;
119 TEST_EQ(vb2_verify_common_header(cbuf, sizeof(cbuf)),
120 VB2_ERROR_COMMON_DESC_WRAPS,
121 "vb2_verify_common_header() desc wraps");
122
123 memcpy(cbuf, cbufgood, sizeof(cbuf));
124 cbuf[desc_end - 1] = 1;
125 TEST_EQ(vb2_verify_common_header(cbuf, sizeof(cbuf)),
126 VB2_ERROR_COMMON_DESC_TERMINATOR,
127 "vb2_verify_common_header() desc not terminated");
128
129 /* Member checking function */
130 memcpy(cbuf, cbufgood, sizeof(cbuf));
131 m = 0;
132 TEST_SUCC(vb2_verify_common_member(cbuf, &m, c->total_size - 8, 4),
133 "vb2_verify_common_member()");
134 TEST_EQ(m, c->total_size - 4, " new minimum");
135
136 m = desc_end;
137 TEST_SUCC(vb2_verify_common_member(cbuf, &m, desc_end, 4),
138 "vb2_verify_common_member() good offset");
139 TEST_EQ(m, desc_end + 4, " new minimum");
140
141 m = 0;
142 TEST_EQ(vb2_verify_common_member(cbuf, &m, c->total_size - 8, -4),
143 VB2_ERROR_COMMON_MEMBER_WRAPS,
144 "vb2_verify_common_member() wraps");
145
146 m = 0;
147 TEST_EQ(vb2_verify_common_member(cbuf, &m, c->total_size - 7, 4),
148 VB2_ERROR_COMMON_MEMBER_UNALIGNED,
149 "vb2_verify_common_member() offset unaligned");
150
151 m = 0;
152 TEST_EQ(vb2_verify_common_member(cbuf, &m, c->total_size - 8, 5),
153 VB2_ERROR_COMMON_MEMBER_UNALIGNED,
154 "vb2_verify_common_member() size unaligned");
155
156 m = 0;
157 TEST_EQ(vb2_verify_common_member(cbuf, &m, desc_end - 4, 4),
158 VB2_ERROR_COMMON_MEMBER_OVERLAP,
159 "vb2_verify_common_member() overlap");
160
161 m = desc_end + 4;
162 TEST_EQ(vb2_verify_common_member(cbuf, &m, desc_end, 4),
163 VB2_ERROR_COMMON_MEMBER_OVERLAP,
164 "vb2_verify_common_member() overlap 2");
165
166 m = 0;
167 TEST_EQ(vb2_verify_common_member(cbuf, &m, c->total_size - 4, 8),
168 VB2_ERROR_COMMON_MEMBER_SIZE,
169 "vb2_verify_common_member() size");
170
171 /* Subobject checking */
172 m = 0;
173 TEST_SUCC(vb2_verify_common_subobject(cbuf, &m, desc_end),
174 "vb2_verify_common_subobject() good offset");
175 TEST_EQ(m, sizeof(cbuf), " new minimum");
176
177 m = desc_end + 4;
178 TEST_EQ(vb2_verify_common_subobject(cbuf, &m, desc_end),
179 VB2_ERROR_COMMON_MEMBER_OVERLAP,
180 "vb2_verify_common_subobject() overlap");
181
182 m = 0;
183 c2->total_size += 4;
184 TEST_EQ(vb2_verify_common_subobject(cbuf, &m, desc_end),
185 VB2_ERROR_COMMON_TOTAL_SIZE,
186 "vb2_verify_common_subobject() size");
187}
188
189/**
190 * Signature size
191 */
192static void test_sig_size(void)
193{
194 TEST_EQ(vb2_sig_size(VB2_SIG_INVALID, VB2_HASH_SHA256), 0,
195 "vb2_sig_size() sig invalid");
196
197 TEST_EQ(vb2_sig_size(VB2_SIG_RSA2048, VB2_HASH_INVALID), 0,
198 "vb2_sig_size() hash invalid");
199
200 TEST_EQ(vb2_sig_size(VB2_SIG_RSA2048, VB2_HASH_SHA256), 2048 / 8,
201 "vb2_sig_size() RSA2048");
202 TEST_EQ(vb2_sig_size(VB2_SIG_RSA4096, VB2_HASH_SHA256), 4096 / 8,
203 "vb2_sig_size() RSA4096");
204 TEST_EQ(vb2_sig_size(VB2_SIG_RSA8192, VB2_HASH_SHA512), 8192 / 8,
205 "vb2_sig_size() RSA8192");
206
207 TEST_EQ(vb2_sig_size(VB2_SIG_NONE, VB2_HASH_SHA1),
208 VB2_SHA1_DIGEST_SIZE, "vb2_sig_size() SHA1");
209 TEST_EQ(vb2_sig_size(VB2_SIG_NONE, VB2_HASH_SHA256),
210 VB2_SHA256_DIGEST_SIZE, "vb2_sig_size() SHA256");
211 TEST_EQ(vb2_sig_size(VB2_SIG_NONE, VB2_HASH_SHA512),
212 VB2_SHA512_DIGEST_SIZE, "vb2_sig_size() SHA512");
213}
214
215/**
216 * Verify data on bare hash
217 */
218static void test_verify_hash(void)
219{
Randall Spangler308d2542014-12-04 09:54:37 -0800220 struct vb2_signature *sig;
Randall Spangler108d9912014-12-02 15:55:56 -0800221 const struct vb2_private_key *prik;
222 struct vb2_public_key pubk;
Bill Richardson73e5eb32015-01-26 12:18:25 -0800223 uint8_t workbuf[VB2_VERIFY_DATA_WORKBUF_BYTES]
224 __attribute__ ((aligned (VB2_WORKBUF_ALIGN)));
Randall Spangler108d9912014-12-02 15:55:56 -0800225 struct vb2_workbuf wb;
226
227 vb2_workbuf_init(&wb, workbuf, sizeof(workbuf));
228
229 TEST_SUCC(vb2_private_key_hash(&prik, VB2_HASH_SHA256),
230 "create private hash key");
231 TEST_SUCC(vb2_public_key_hash(&pubk, VB2_HASH_SHA256),
232 "create hash key");
233
234 /* Create the signature */
Randall Spangler308d2542014-12-04 09:54:37 -0800235 TEST_SUCC(vb2_sign_data(&sig, test_data, sizeof(test_data), prik, NULL),
Randall Spangler108d9912014-12-02 15:55:56 -0800236 "create hash sig");
237
Randall Spangler308d2542014-12-04 09:54:37 -0800238 TEST_SUCC(vb2_verify_data(test_data, sizeof(test_data),
239 sig, &pubk, &wb),
240 "vb2_verify_data() hash ok");
Randall Spangler108d9912014-12-02 15:55:56 -0800241
242 *((uint8_t *)sig + sig->sig_offset) ^= 0xab;
Randall Spangler308d2542014-12-04 09:54:37 -0800243 TEST_EQ(vb2_verify_data(test_data, sizeof(test_data), sig, &pubk, &wb),
244 VB2_ERROR_VDATA_VERIFY_DIGEST, "vb2_verify_data() hash bad");
Randall Spangler108d9912014-12-02 15:55:56 -0800245
246 free(sig);
247}
248
249/**
250 * Verify keyblock
251 */
252static void test_verify_keyblock(void)
253{
254 const char desc[16] = "test keyblock";
255 const struct vb2_private_key *prik[2];
256 struct vb2_public_key pubk, pubk2, pubk3;
Randall Spangler308d2542014-12-04 09:54:37 -0800257 struct vb2_signature *sig;
258 struct vb2_keyblock *kbuf;
Randall Spangler108d9912014-12-02 15:55:56 -0800259 uint32_t buf_size;
260 uint8_t *buf, *buf2;
261
Bill Richardson73e5eb32015-01-26 12:18:25 -0800262 uint8_t workbuf[VB2_KEY_BLOCK_VERIFY_WORKBUF_BYTES]
263 __attribute__ ((aligned (VB2_WORKBUF_ALIGN)));
Randall Spangler108d9912014-12-02 15:55:56 -0800264 struct vb2_workbuf wb;
265
266 TEST_SUCC(vb2_public_key_hash(&pubk, VB2_HASH_SHA256),
267 "create hash key 1");
268 TEST_SUCC(vb2_public_key_hash(&pubk2, VB2_HASH_SHA512),
269 "create hash key 2");
270 TEST_SUCC(vb2_public_key_hash(&pubk3, VB2_HASH_SHA1),
271 "create hash key 3");
272
273 TEST_SUCC(vb2_private_key_hash(prik + 0, VB2_HASH_SHA256),
274 "create private key 1");
275 TEST_SUCC(vb2_private_key_hash(prik + 1, VB2_HASH_SHA512),
276 "create private key 2");
277
278 /* Create the test keyblock */
279 TEST_SUCC(vb2_keyblock_create(&kbuf, &pubk3, prik, 2, 0x4321, desc),
280 "create keyblock");
281
282 buf = (uint8_t *)kbuf;
283 buf_size = kbuf->c.total_size;
284
285 /* Make a copy of the buffer, so we can mangle it for tests */
286 buf2 = malloc(buf_size);
287 memcpy(buf2, buf, buf_size);
288
289 vb2_workbuf_init(&wb, workbuf, sizeof(workbuf));
Randall Spangler308d2542014-12-04 09:54:37 -0800290 kbuf = (struct vb2_keyblock *)buf;
Randall Spangler108d9912014-12-02 15:55:56 -0800291
Randall Spangler308d2542014-12-04 09:54:37 -0800292 TEST_SUCC(vb2_verify_keyblock(kbuf, buf_size, &pubk, &wb),
293 "vb2_verify_keyblock()");
Randall Spangler108d9912014-12-02 15:55:56 -0800294
295 memcpy(buf, buf2, buf_size);
Randall Spangler308d2542014-12-04 09:54:37 -0800296 TEST_SUCC(vb2_verify_keyblock(kbuf, buf_size, &pubk2, &wb),
297 "vb2_verify_keyblock() key 2");
Randall Spangler108d9912014-12-02 15:55:56 -0800298
299 memcpy(buf, buf2, buf_size);
Randall Spangler308d2542014-12-04 09:54:37 -0800300 TEST_EQ(vb2_verify_keyblock(kbuf, buf_size, &pubk3, &wb),
Randall Spangler108d9912014-12-02 15:55:56 -0800301 VB2_ERROR_KEYBLOCK_SIG_GUID,
Randall Spangler308d2542014-12-04 09:54:37 -0800302 "vb2_verify_keyblock() key not present");
Randall Spangler108d9912014-12-02 15:55:56 -0800303
304 memcpy(buf, buf2, buf_size);
Randall Spangler308d2542014-12-04 09:54:37 -0800305 kbuf->c.magic = VB2_MAGIC_PACKED_KEY;
306 TEST_EQ(vb2_verify_keyblock(kbuf, buf_size, &pubk, &wb),
Randall Spangler108d9912014-12-02 15:55:56 -0800307 VB2_ERROR_KEYBLOCK_MAGIC,
Randall Spangler308d2542014-12-04 09:54:37 -0800308 "vb2_verify_keyblock() magic");
Randall Spangler108d9912014-12-02 15:55:56 -0800309
310 memcpy(buf, buf2, buf_size);
311 kbuf->c.fixed_size++;
Randall Spangler308d2542014-12-04 09:54:37 -0800312 TEST_EQ(vb2_verify_keyblock(kbuf, buf_size, &pubk, &wb),
Randall Spangler108d9912014-12-02 15:55:56 -0800313 VB2_ERROR_COMMON_FIXED_UNALIGNED,
Randall Spangler308d2542014-12-04 09:54:37 -0800314 "vb2_verify_keyblock() header");
Randall Spangler108d9912014-12-02 15:55:56 -0800315
316 memcpy(buf, buf2, buf_size);
317 kbuf->c.struct_version_major++;
Randall Spangler308d2542014-12-04 09:54:37 -0800318 TEST_EQ(vb2_verify_keyblock(kbuf, buf_size, &pubk, &wb),
Randall Spangler108d9912014-12-02 15:55:56 -0800319 VB2_ERROR_KEYBLOCK_HEADER_VERSION,
Randall Spangler308d2542014-12-04 09:54:37 -0800320 "vb2_verify_keyblock() major version");
Randall Spangler108d9912014-12-02 15:55:56 -0800321
322 memcpy(buf, buf2, buf_size);
323 kbuf->c.struct_version_minor++;
324 /* That changes the signature, so resign the keyblock */
325 vb2_sign_data(&sig, buf, kbuf->sig_offset, prik[0], NULL);
326 memcpy(buf + kbuf->sig_offset, sig, sig->c.total_size);
327 free(sig);
Randall Spangler308d2542014-12-04 09:54:37 -0800328 TEST_SUCC(vb2_verify_keyblock(kbuf, buf_size, &pubk, &wb),
329 "vb2_verify_keyblock() minor version");
Randall Spangler108d9912014-12-02 15:55:56 -0800330
331 memcpy(buf, buf2, buf_size);
332 kbuf->c.fixed_size -= 4;
333 kbuf->c.desc_size += 4;
Randall Spangler308d2542014-12-04 09:54:37 -0800334 TEST_EQ(vb2_verify_keyblock(kbuf, buf_size, &pubk, &wb),
Randall Spangler108d9912014-12-02 15:55:56 -0800335 VB2_ERROR_KEYBLOCK_SIZE,
Randall Spangler308d2542014-12-04 09:54:37 -0800336 "vb2_verify_keyblock() header size");
Randall Spangler108d9912014-12-02 15:55:56 -0800337
338 memcpy(buf, buf2, buf_size);
339 kbuf->key_offset = kbuf->c.total_size - 4;
Randall Spangler308d2542014-12-04 09:54:37 -0800340 TEST_EQ(vb2_verify_keyblock(kbuf, buf_size, &pubk, &wb),
Randall Spangler108d9912014-12-02 15:55:56 -0800341 VB2_ERROR_COMMON_MEMBER_SIZE,
Randall Spangler308d2542014-12-04 09:54:37 -0800342 "vb2_verify_keyblock() data key outside");
Randall Spangler108d9912014-12-02 15:55:56 -0800343
344 memcpy(buf, buf2, buf_size);
Randall Spangler308d2542014-12-04 09:54:37 -0800345 sig = (struct vb2_signature *)(buf + kbuf->sig_offset);
Randall Spangler108d9912014-12-02 15:55:56 -0800346 sig->data_size--;
Randall Spangler308d2542014-12-04 09:54:37 -0800347 TEST_EQ(vb2_verify_keyblock(kbuf, buf_size, &pubk, &wb),
Randall Spangler108d9912014-12-02 15:55:56 -0800348 VB2_ERROR_KEYBLOCK_SIGNED_SIZE,
Randall Spangler308d2542014-12-04 09:54:37 -0800349 "vb2_verify_keyblock() signed wrong size");
Randall Spangler108d9912014-12-02 15:55:56 -0800350
351 memcpy(buf, buf2, buf_size);
Randall Spangler308d2542014-12-04 09:54:37 -0800352 sig = (struct vb2_signature *)(buf + kbuf->sig_offset);
Randall Spangler108d9912014-12-02 15:55:56 -0800353 sig->c.total_size = kbuf->c.total_size - 4;
Randall Spangler308d2542014-12-04 09:54:37 -0800354 TEST_EQ(vb2_verify_keyblock(kbuf, buf_size, &pubk, &wb),
Randall Spangler108d9912014-12-02 15:55:56 -0800355 VB2_ERROR_COMMON_TOTAL_SIZE,
Randall Spangler308d2542014-12-04 09:54:37 -0800356 "vb2_verify_keyblock() key outside keyblock");
Randall Spangler108d9912014-12-02 15:55:56 -0800357
358 memcpy(buf, buf2, buf_size);
Randall Spangler308d2542014-12-04 09:54:37 -0800359 sig = (struct vb2_signature *)(buf + kbuf->sig_offset);
Randall Spangler108d9912014-12-02 15:55:56 -0800360 sig->c.struct_version_major++;
Randall Spangler308d2542014-12-04 09:54:37 -0800361 TEST_EQ(vb2_verify_keyblock(kbuf, buf_size, &pubk, &wb),
Randall Spangler108d9912014-12-02 15:55:56 -0800362 VB2_ERROR_SIG_VERSION,
Randall Spangler308d2542014-12-04 09:54:37 -0800363 "vb2_verify_keyblock() corrupt key");
Randall Spangler108d9912014-12-02 15:55:56 -0800364
365 memcpy(buf, buf2, buf_size);
366 kbuf->c.struct_version_minor++;
Randall Spangler308d2542014-12-04 09:54:37 -0800367 TEST_EQ(vb2_verify_keyblock(kbuf, buf_size, &pubk, &wb),
Randall Spangler108d9912014-12-02 15:55:56 -0800368 VB2_ERROR_VDATA_VERIFY_DIGEST,
Randall Spangler308d2542014-12-04 09:54:37 -0800369 "vb2_verify_keyblock() corrupt");
Randall Spangler108d9912014-12-02 15:55:56 -0800370
371 free(buf);
372 free(buf2);
373}
374
375/**
376 * Verify firmware preamble
377 */
378static void test_verify_fw_preamble(void)
379{
380 const char desc[16] = "test preamble";
381 const struct vb2_private_key *prikhash;
Randall Spangler308d2542014-12-04 09:54:37 -0800382 struct vb2_signature *hashes[3];
Randall Spangler108d9912014-12-02 15:55:56 -0800383 struct vb2_public_key pubk;
Randall Spangler308d2542014-12-04 09:54:37 -0800384 struct vb2_signature *sig;
385 struct vb2_fw_preamble *pre;
Randall Spangler108d9912014-12-02 15:55:56 -0800386 uint32_t buf_size;
387 uint8_t *buf, *buf2;
388
Bill Richardson73e5eb32015-01-26 12:18:25 -0800389 uint8_t workbuf[VB2_VERIFY_FIRMWARE_PREAMBLE_WORKBUF_BYTES]
390 __attribute__ ((aligned (VB2_WORKBUF_ALIGN)));
Randall Spangler108d9912014-12-02 15:55:56 -0800391 struct vb2_workbuf wb;
392
393 /*
394 * Preambles will usually be signed with a real key not a bare hash,
Randall Spangler308d2542014-12-04 09:54:37 -0800395 * but the call to vb2_verify_data() inside the preamble check is the
Randall Spangler108d9912014-12-02 15:55:56 -0800396 * same (and its functionality is verified separately), and using a
397 * bare hash here saves us from needing to have a private key to do
398 * this test.
399 */
400 TEST_SUCC(vb2_public_key_hash(&pubk, VB2_HASH_SHA256),
401 "create hash key");
402 TEST_SUCC(vb2_private_key_hash(&prikhash, VB2_HASH_SHA256),
403 "Create private hash key");
404
405 /* Create some signatures */
406 TEST_SUCC(vb2_sign_data(hashes + 0, test_data, sizeof(test_data),
407 prikhash, "Hash 1"),
408 "Hash 1");
409 TEST_SUCC(vb2_sign_data(hashes + 1, test_data2, sizeof(test_data2),
410 prikhash, "Hash 2"),
411 "Hash 2");
412 TEST_SUCC(vb2_sign_data(hashes + 2, test_data3, sizeof(test_data3),
413 prikhash, "Hash 3"),
414 "Hash 3");
415
416 /* Test good preamble */
417 TEST_SUCC(vb2_fw_preamble_create(&pre, prikhash,
Randall Spangler308d2542014-12-04 09:54:37 -0800418 (const struct vb2_signature **)hashes,
Randall Spangler108d9912014-12-02 15:55:56 -0800419 3, 0x1234, 0x5678, desc),
420 "Create preamble good");
421
422 buf = (uint8_t *)pre;
423 buf_size = pre->c.total_size;
424
425 /* Make a copy of the buffer, so we can mangle it for tests */
426 buf2 = malloc(buf_size);
427 memcpy(buf2, buf, buf_size);
428
429 vb2_workbuf_init(&wb, workbuf, sizeof(workbuf));
Randall Spangler308d2542014-12-04 09:54:37 -0800430 pre = (struct vb2_fw_preamble *)buf;
Randall Spangler108d9912014-12-02 15:55:56 -0800431
Randall Spangler308d2542014-12-04 09:54:37 -0800432 TEST_SUCC(vb2_verify_fw_preamble(pre, buf_size, &pubk, &wb),
433 "vb2_verify_fw_preamble()");
Randall Spangler108d9912014-12-02 15:55:56 -0800434
435 memcpy(buf, buf2, buf_size);
Randall Spangler308d2542014-12-04 09:54:37 -0800436 pre->c.magic = VB2_MAGIC_PACKED_KEY;
437 TEST_EQ(vb2_verify_fw_preamble(pre, buf_size, &pubk, &wb),
Randall Spangler108d9912014-12-02 15:55:56 -0800438 VB2_ERROR_PREAMBLE_MAGIC,
Randall Spangler308d2542014-12-04 09:54:37 -0800439 "vb2_verify_fw_preamble() magic");
Randall Spangler108d9912014-12-02 15:55:56 -0800440
441 memcpy(buf, buf2, buf_size);
442 pre->c.fixed_size++;
Randall Spangler308d2542014-12-04 09:54:37 -0800443 TEST_EQ(vb2_verify_fw_preamble(pre, buf_size, &pubk, &wb),
Randall Spangler108d9912014-12-02 15:55:56 -0800444 VB2_ERROR_COMMON_FIXED_UNALIGNED,
Randall Spangler308d2542014-12-04 09:54:37 -0800445 "vb2_verify_fw_preamble() header");
Randall Spangler108d9912014-12-02 15:55:56 -0800446
447 memcpy(buf, buf2, buf_size);
448 pre->c.struct_version_major++;
Randall Spangler308d2542014-12-04 09:54:37 -0800449 TEST_EQ(vb2_verify_fw_preamble(pre, buf_size, &pubk, &wb),
Randall Spangler108d9912014-12-02 15:55:56 -0800450 VB2_ERROR_PREAMBLE_HEADER_VERSION,
Randall Spangler308d2542014-12-04 09:54:37 -0800451 "vb2_verify_fw_preamble() major version");
Randall Spangler108d9912014-12-02 15:55:56 -0800452
453 memcpy(buf, buf2, buf_size);
454 pre->c.struct_version_minor++;
455 /* That changes the signature, so resign the fw_preamble */
456 vb2_sign_data(&sig, buf, pre->sig_offset, prikhash, NULL);
457 memcpy(buf + pre->sig_offset, sig, sig->c.total_size);
458 free(sig);
Randall Spangler308d2542014-12-04 09:54:37 -0800459 TEST_SUCC(vb2_verify_fw_preamble(pre, buf_size, &pubk, &wb),
460 "vb2_verify_fw_preamble() minor version");
Randall Spangler108d9912014-12-02 15:55:56 -0800461
462 memcpy(buf, buf2, buf_size);
463 pre->c.fixed_size -= 4;
464 pre->c.desc_size += 4;
Randall Spangler308d2542014-12-04 09:54:37 -0800465 TEST_EQ(vb2_verify_fw_preamble(pre, buf_size, &pubk, &wb),
Randall Spangler108d9912014-12-02 15:55:56 -0800466 VB2_ERROR_PREAMBLE_SIZE,
Randall Spangler308d2542014-12-04 09:54:37 -0800467 "vb2_verify_fw_preamble() header size");
Randall Spangler108d9912014-12-02 15:55:56 -0800468
469 memcpy(buf, buf2, buf_size);
Randall Spangler308d2542014-12-04 09:54:37 -0800470 sig = (struct vb2_signature *)(buf + pre->hash_offset);
Randall Spangler108d9912014-12-02 15:55:56 -0800471 sig->c.total_size += pre->c.total_size;
Randall Spangler308d2542014-12-04 09:54:37 -0800472 TEST_EQ(vb2_verify_fw_preamble(pre, buf_size, &pubk, &wb),
Randall Spangler108d9912014-12-02 15:55:56 -0800473 VB2_ERROR_COMMON_TOTAL_SIZE,
Randall Spangler308d2542014-12-04 09:54:37 -0800474 "vb2_verify_fw_preamble() hash size");
Randall Spangler108d9912014-12-02 15:55:56 -0800475
476 memcpy(buf, buf2, buf_size);
Randall Spangler308d2542014-12-04 09:54:37 -0800477 sig = (struct vb2_signature *)(buf + pre->hash_offset);
Randall Spangler108d9912014-12-02 15:55:56 -0800478 sig->sig_size /= 2;
Randall Spangler308d2542014-12-04 09:54:37 -0800479 TEST_EQ(vb2_verify_fw_preamble(pre, buf_size, &pubk, &wb),
Randall Spangler108d9912014-12-02 15:55:56 -0800480 VB2_ERROR_SIG_SIZE,
Randall Spangler308d2542014-12-04 09:54:37 -0800481 "vb2_verify_fw_preamble() hash integrity");
Randall Spangler108d9912014-12-02 15:55:56 -0800482
483 memcpy(buf, buf2, buf_size);
484 pre->hash_count++;
Randall Spangler308d2542014-12-04 09:54:37 -0800485 TEST_EQ(vb2_verify_fw_preamble(pre, buf_size, &pubk, &wb),
Randall Spangler108d9912014-12-02 15:55:56 -0800486 VB2_ERROR_COMMON_MEMBER_OVERLAP,
Randall Spangler308d2542014-12-04 09:54:37 -0800487 "vb2_verify_fw_preamble() hash count");
Randall Spangler108d9912014-12-02 15:55:56 -0800488
489 memcpy(buf, buf2, buf_size);
Randall Spangler308d2542014-12-04 09:54:37 -0800490 sig = (struct vb2_signature *)(buf + pre->sig_offset);
Randall Spangler108d9912014-12-02 15:55:56 -0800491 sig->c.total_size += 4;
Randall Spangler308d2542014-12-04 09:54:37 -0800492 TEST_EQ(vb2_verify_fw_preamble(pre, buf_size, &pubk, &wb),
Randall Spangler108d9912014-12-02 15:55:56 -0800493 VB2_ERROR_COMMON_TOTAL_SIZE,
Randall Spangler308d2542014-12-04 09:54:37 -0800494 "vb2_verify_fw_preamble() sig inside");
Randall Spangler108d9912014-12-02 15:55:56 -0800495
496 memcpy(buf, buf2, buf_size);
Randall Spangler308d2542014-12-04 09:54:37 -0800497 sig = (struct vb2_signature *)(buf + pre->sig_offset);
Randall Spangler108d9912014-12-02 15:55:56 -0800498 buf[pre->sig_offset + sig->sig_offset]++;
Randall Spangler308d2542014-12-04 09:54:37 -0800499 TEST_EQ(vb2_verify_fw_preamble(pre, buf_size, &pubk, &wb),
Randall Spangler108d9912014-12-02 15:55:56 -0800500 VB2_ERROR_VDATA_VERIFY_DIGEST,
Randall Spangler308d2542014-12-04 09:54:37 -0800501 "vb2_verify_fw_preamble() sig corrupt");
Randall Spangler108d9912014-12-02 15:55:56 -0800502
503 memcpy(buf, buf2, buf_size);
504 pre->flags++;
Randall Spangler308d2542014-12-04 09:54:37 -0800505 TEST_EQ(vb2_verify_fw_preamble(pre, buf_size, &pubk, &wb),
Randall Spangler108d9912014-12-02 15:55:56 -0800506 VB2_ERROR_VDATA_VERIFY_DIGEST,
Randall Spangler308d2542014-12-04 09:54:37 -0800507 "vb2_verify_fw_preamble() preamble corrupt");
Randall Spangler108d9912014-12-02 15:55:56 -0800508
509 free(buf);
510 free(buf2);
511}
512
513int main(int argc, char* argv[])
514{
515 test_struct_packing();
516 test_common_header_functions();
517 test_sig_size();
518 test_verify_hash();
519 test_verify_keyblock();
520 test_verify_fw_preamble();
521
522 return gTestSuccess ? 0 : 255;
523}