blob: 744402546ae5db9a702936d37f4cf40f946dacc7 [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;
223 uint8_t workbuf[VB2_VERIFY_DATA_WORKBUF_BYTES];
224 struct vb2_workbuf wb;
225
226 vb2_workbuf_init(&wb, workbuf, sizeof(workbuf));
227
228 TEST_SUCC(vb2_private_key_hash(&prik, VB2_HASH_SHA256),
229 "create private hash key");
230 TEST_SUCC(vb2_public_key_hash(&pubk, VB2_HASH_SHA256),
231 "create hash key");
232
233 /* Create the signature */
Randall Spangler308d2542014-12-04 09:54:37 -0800234 TEST_SUCC(vb2_sign_data(&sig, test_data, sizeof(test_data), prik, NULL),
Randall Spangler108d9912014-12-02 15:55:56 -0800235 "create hash sig");
236
Randall Spangler308d2542014-12-04 09:54:37 -0800237 TEST_SUCC(vb2_verify_data(test_data, sizeof(test_data),
238 sig, &pubk, &wb),
239 "vb2_verify_data() hash ok");
Randall Spangler108d9912014-12-02 15:55:56 -0800240
241 *((uint8_t *)sig + sig->sig_offset) ^= 0xab;
Randall Spangler308d2542014-12-04 09:54:37 -0800242 TEST_EQ(vb2_verify_data(test_data, sizeof(test_data), sig, &pubk, &wb),
243 VB2_ERROR_VDATA_VERIFY_DIGEST, "vb2_verify_data() hash bad");
Randall Spangler108d9912014-12-02 15:55:56 -0800244
245 free(sig);
246}
247
248/**
249 * Verify keyblock
250 */
251static void test_verify_keyblock(void)
252{
253 const char desc[16] = "test keyblock";
254 const struct vb2_private_key *prik[2];
255 struct vb2_public_key pubk, pubk2, pubk3;
Randall Spangler308d2542014-12-04 09:54:37 -0800256 struct vb2_signature *sig;
257 struct vb2_keyblock *kbuf;
Randall Spangler108d9912014-12-02 15:55:56 -0800258 uint32_t buf_size;
259 uint8_t *buf, *buf2;
260
261 uint8_t workbuf[VB2_KEY_BLOCK_VERIFY_WORKBUF_BYTES];
262 struct vb2_workbuf wb;
263
264 TEST_SUCC(vb2_public_key_hash(&pubk, VB2_HASH_SHA256),
265 "create hash key 1");
266 TEST_SUCC(vb2_public_key_hash(&pubk2, VB2_HASH_SHA512),
267 "create hash key 2");
268 TEST_SUCC(vb2_public_key_hash(&pubk3, VB2_HASH_SHA1),
269 "create hash key 3");
270
271 TEST_SUCC(vb2_private_key_hash(prik + 0, VB2_HASH_SHA256),
272 "create private key 1");
273 TEST_SUCC(vb2_private_key_hash(prik + 1, VB2_HASH_SHA512),
274 "create private key 2");
275
276 /* Create the test keyblock */
277 TEST_SUCC(vb2_keyblock_create(&kbuf, &pubk3, prik, 2, 0x4321, desc),
278 "create keyblock");
279
280 buf = (uint8_t *)kbuf;
281 buf_size = kbuf->c.total_size;
282
283 /* Make a copy of the buffer, so we can mangle it for tests */
284 buf2 = malloc(buf_size);
285 memcpy(buf2, buf, buf_size);
286
287 vb2_workbuf_init(&wb, workbuf, sizeof(workbuf));
Randall Spangler308d2542014-12-04 09:54:37 -0800288 kbuf = (struct vb2_keyblock *)buf;
Randall Spangler108d9912014-12-02 15:55:56 -0800289
Randall Spangler308d2542014-12-04 09:54:37 -0800290 TEST_SUCC(vb2_verify_keyblock(kbuf, buf_size, &pubk, &wb),
291 "vb2_verify_keyblock()");
Randall Spangler108d9912014-12-02 15:55:56 -0800292
293 memcpy(buf, buf2, buf_size);
Randall Spangler308d2542014-12-04 09:54:37 -0800294 TEST_SUCC(vb2_verify_keyblock(kbuf, buf_size, &pubk2, &wb),
295 "vb2_verify_keyblock() key 2");
Randall Spangler108d9912014-12-02 15:55:56 -0800296
297 memcpy(buf, buf2, buf_size);
Randall Spangler308d2542014-12-04 09:54:37 -0800298 TEST_EQ(vb2_verify_keyblock(kbuf, buf_size, &pubk3, &wb),
Randall Spangler108d9912014-12-02 15:55:56 -0800299 VB2_ERROR_KEYBLOCK_SIG_GUID,
Randall Spangler308d2542014-12-04 09:54:37 -0800300 "vb2_verify_keyblock() key not present");
Randall Spangler108d9912014-12-02 15:55:56 -0800301
302 memcpy(buf, buf2, buf_size);
Randall Spangler308d2542014-12-04 09:54:37 -0800303 kbuf->c.magic = VB2_MAGIC_PACKED_KEY;
304 TEST_EQ(vb2_verify_keyblock(kbuf, buf_size, &pubk, &wb),
Randall Spangler108d9912014-12-02 15:55:56 -0800305 VB2_ERROR_KEYBLOCK_MAGIC,
Randall Spangler308d2542014-12-04 09:54:37 -0800306 "vb2_verify_keyblock() magic");
Randall Spangler108d9912014-12-02 15:55:56 -0800307
308 memcpy(buf, buf2, buf_size);
309 kbuf->c.fixed_size++;
Randall Spangler308d2542014-12-04 09:54:37 -0800310 TEST_EQ(vb2_verify_keyblock(kbuf, buf_size, &pubk, &wb),
Randall Spangler108d9912014-12-02 15:55:56 -0800311 VB2_ERROR_COMMON_FIXED_UNALIGNED,
Randall Spangler308d2542014-12-04 09:54:37 -0800312 "vb2_verify_keyblock() header");
Randall Spangler108d9912014-12-02 15:55:56 -0800313
314 memcpy(buf, buf2, buf_size);
315 kbuf->c.struct_version_major++;
Randall Spangler308d2542014-12-04 09:54:37 -0800316 TEST_EQ(vb2_verify_keyblock(kbuf, buf_size, &pubk, &wb),
Randall Spangler108d9912014-12-02 15:55:56 -0800317 VB2_ERROR_KEYBLOCK_HEADER_VERSION,
Randall Spangler308d2542014-12-04 09:54:37 -0800318 "vb2_verify_keyblock() major version");
Randall Spangler108d9912014-12-02 15:55:56 -0800319
320 memcpy(buf, buf2, buf_size);
321 kbuf->c.struct_version_minor++;
322 /* That changes the signature, so resign the keyblock */
323 vb2_sign_data(&sig, buf, kbuf->sig_offset, prik[0], NULL);
324 memcpy(buf + kbuf->sig_offset, sig, sig->c.total_size);
325 free(sig);
Randall Spangler308d2542014-12-04 09:54:37 -0800326 TEST_SUCC(vb2_verify_keyblock(kbuf, buf_size, &pubk, &wb),
327 "vb2_verify_keyblock() minor version");
Randall Spangler108d9912014-12-02 15:55:56 -0800328
329 memcpy(buf, buf2, buf_size);
330 kbuf->c.fixed_size -= 4;
331 kbuf->c.desc_size += 4;
Randall Spangler308d2542014-12-04 09:54:37 -0800332 TEST_EQ(vb2_verify_keyblock(kbuf, buf_size, &pubk, &wb),
Randall Spangler108d9912014-12-02 15:55:56 -0800333 VB2_ERROR_KEYBLOCK_SIZE,
Randall Spangler308d2542014-12-04 09:54:37 -0800334 "vb2_verify_keyblock() header size");
Randall Spangler108d9912014-12-02 15:55:56 -0800335
336 memcpy(buf, buf2, buf_size);
337 kbuf->key_offset = kbuf->c.total_size - 4;
Randall Spangler308d2542014-12-04 09:54:37 -0800338 TEST_EQ(vb2_verify_keyblock(kbuf, buf_size, &pubk, &wb),
Randall Spangler108d9912014-12-02 15:55:56 -0800339 VB2_ERROR_COMMON_MEMBER_SIZE,
Randall Spangler308d2542014-12-04 09:54:37 -0800340 "vb2_verify_keyblock() data key outside");
Randall Spangler108d9912014-12-02 15:55:56 -0800341
342 memcpy(buf, buf2, buf_size);
Randall Spangler308d2542014-12-04 09:54:37 -0800343 sig = (struct vb2_signature *)(buf + kbuf->sig_offset);
Randall Spangler108d9912014-12-02 15:55:56 -0800344 sig->data_size--;
Randall Spangler308d2542014-12-04 09:54:37 -0800345 TEST_EQ(vb2_verify_keyblock(kbuf, buf_size, &pubk, &wb),
Randall Spangler108d9912014-12-02 15:55:56 -0800346 VB2_ERROR_KEYBLOCK_SIGNED_SIZE,
Randall Spangler308d2542014-12-04 09:54:37 -0800347 "vb2_verify_keyblock() signed wrong size");
Randall Spangler108d9912014-12-02 15:55:56 -0800348
349 memcpy(buf, buf2, buf_size);
Randall Spangler308d2542014-12-04 09:54:37 -0800350 sig = (struct vb2_signature *)(buf + kbuf->sig_offset);
Randall Spangler108d9912014-12-02 15:55:56 -0800351 sig->c.total_size = kbuf->c.total_size - 4;
Randall Spangler308d2542014-12-04 09:54:37 -0800352 TEST_EQ(vb2_verify_keyblock(kbuf, buf_size, &pubk, &wb),
Randall Spangler108d9912014-12-02 15:55:56 -0800353 VB2_ERROR_COMMON_TOTAL_SIZE,
Randall Spangler308d2542014-12-04 09:54:37 -0800354 "vb2_verify_keyblock() key outside keyblock");
Randall Spangler108d9912014-12-02 15:55:56 -0800355
356 memcpy(buf, buf2, buf_size);
Randall Spangler308d2542014-12-04 09:54:37 -0800357 sig = (struct vb2_signature *)(buf + kbuf->sig_offset);
Randall Spangler108d9912014-12-02 15:55:56 -0800358 sig->c.struct_version_major++;
Randall Spangler308d2542014-12-04 09:54:37 -0800359 TEST_EQ(vb2_verify_keyblock(kbuf, buf_size, &pubk, &wb),
Randall Spangler108d9912014-12-02 15:55:56 -0800360 VB2_ERROR_SIG_VERSION,
Randall Spangler308d2542014-12-04 09:54:37 -0800361 "vb2_verify_keyblock() corrupt key");
Randall Spangler108d9912014-12-02 15:55:56 -0800362
363 memcpy(buf, buf2, buf_size);
364 kbuf->c.struct_version_minor++;
Randall Spangler308d2542014-12-04 09:54:37 -0800365 TEST_EQ(vb2_verify_keyblock(kbuf, buf_size, &pubk, &wb),
Randall Spangler108d9912014-12-02 15:55:56 -0800366 VB2_ERROR_VDATA_VERIFY_DIGEST,
Randall Spangler308d2542014-12-04 09:54:37 -0800367 "vb2_verify_keyblock() corrupt");
Randall Spangler108d9912014-12-02 15:55:56 -0800368
369 free(buf);
370 free(buf2);
371}
372
373/**
374 * Verify firmware preamble
375 */
376static void test_verify_fw_preamble(void)
377{
378 const char desc[16] = "test preamble";
379 const struct vb2_private_key *prikhash;
Randall Spangler308d2542014-12-04 09:54:37 -0800380 struct vb2_signature *hashes[3];
Randall Spangler108d9912014-12-02 15:55:56 -0800381 struct vb2_public_key pubk;
Randall Spangler308d2542014-12-04 09:54:37 -0800382 struct vb2_signature *sig;
383 struct vb2_fw_preamble *pre;
Randall Spangler108d9912014-12-02 15:55:56 -0800384 uint32_t buf_size;
385 uint8_t *buf, *buf2;
386
387 uint8_t workbuf[VB2_VERIFY_FIRMWARE_PREAMBLE_WORKBUF_BYTES];
388 struct vb2_workbuf wb;
389
390 /*
391 * Preambles will usually be signed with a real key not a bare hash,
Randall Spangler308d2542014-12-04 09:54:37 -0800392 * but the call to vb2_verify_data() inside the preamble check is the
Randall Spangler108d9912014-12-02 15:55:56 -0800393 * same (and its functionality is verified separately), and using a
394 * bare hash here saves us from needing to have a private key to do
395 * this test.
396 */
397 TEST_SUCC(vb2_public_key_hash(&pubk, VB2_HASH_SHA256),
398 "create hash key");
399 TEST_SUCC(vb2_private_key_hash(&prikhash, VB2_HASH_SHA256),
400 "Create private hash key");
401
402 /* Create some signatures */
403 TEST_SUCC(vb2_sign_data(hashes + 0, test_data, sizeof(test_data),
404 prikhash, "Hash 1"),
405 "Hash 1");
406 TEST_SUCC(vb2_sign_data(hashes + 1, test_data2, sizeof(test_data2),
407 prikhash, "Hash 2"),
408 "Hash 2");
409 TEST_SUCC(vb2_sign_data(hashes + 2, test_data3, sizeof(test_data3),
410 prikhash, "Hash 3"),
411 "Hash 3");
412
413 /* Test good preamble */
414 TEST_SUCC(vb2_fw_preamble_create(&pre, prikhash,
Randall Spangler308d2542014-12-04 09:54:37 -0800415 (const struct vb2_signature **)hashes,
Randall Spangler108d9912014-12-02 15:55:56 -0800416 3, 0x1234, 0x5678, desc),
417 "Create preamble good");
418
419 buf = (uint8_t *)pre;
420 buf_size = pre->c.total_size;
421
422 /* Make a copy of the buffer, so we can mangle it for tests */
423 buf2 = malloc(buf_size);
424 memcpy(buf2, buf, buf_size);
425
426 vb2_workbuf_init(&wb, workbuf, sizeof(workbuf));
Randall Spangler308d2542014-12-04 09:54:37 -0800427 pre = (struct vb2_fw_preamble *)buf;
Randall Spangler108d9912014-12-02 15:55:56 -0800428
Randall Spangler308d2542014-12-04 09:54:37 -0800429 TEST_SUCC(vb2_verify_fw_preamble(pre, buf_size, &pubk, &wb),
430 "vb2_verify_fw_preamble()");
Randall Spangler108d9912014-12-02 15:55:56 -0800431
432 memcpy(buf, buf2, buf_size);
Randall Spangler308d2542014-12-04 09:54:37 -0800433 pre->c.magic = VB2_MAGIC_PACKED_KEY;
434 TEST_EQ(vb2_verify_fw_preamble(pre, buf_size, &pubk, &wb),
Randall Spangler108d9912014-12-02 15:55:56 -0800435 VB2_ERROR_PREAMBLE_MAGIC,
Randall Spangler308d2542014-12-04 09:54:37 -0800436 "vb2_verify_fw_preamble() magic");
Randall Spangler108d9912014-12-02 15:55:56 -0800437
438 memcpy(buf, buf2, buf_size);
439 pre->c.fixed_size++;
Randall Spangler308d2542014-12-04 09:54:37 -0800440 TEST_EQ(vb2_verify_fw_preamble(pre, buf_size, &pubk, &wb),
Randall Spangler108d9912014-12-02 15:55:56 -0800441 VB2_ERROR_COMMON_FIXED_UNALIGNED,
Randall Spangler308d2542014-12-04 09:54:37 -0800442 "vb2_verify_fw_preamble() header");
Randall Spangler108d9912014-12-02 15:55:56 -0800443
444 memcpy(buf, buf2, buf_size);
445 pre->c.struct_version_major++;
Randall Spangler308d2542014-12-04 09:54:37 -0800446 TEST_EQ(vb2_verify_fw_preamble(pre, buf_size, &pubk, &wb),
Randall Spangler108d9912014-12-02 15:55:56 -0800447 VB2_ERROR_PREAMBLE_HEADER_VERSION,
Randall Spangler308d2542014-12-04 09:54:37 -0800448 "vb2_verify_fw_preamble() major version");
Randall Spangler108d9912014-12-02 15:55:56 -0800449
450 memcpy(buf, buf2, buf_size);
451 pre->c.struct_version_minor++;
452 /* That changes the signature, so resign the fw_preamble */
453 vb2_sign_data(&sig, buf, pre->sig_offset, prikhash, NULL);
454 memcpy(buf + pre->sig_offset, sig, sig->c.total_size);
455 free(sig);
Randall Spangler308d2542014-12-04 09:54:37 -0800456 TEST_SUCC(vb2_verify_fw_preamble(pre, buf_size, &pubk, &wb),
457 "vb2_verify_fw_preamble() minor version");
Randall Spangler108d9912014-12-02 15:55:56 -0800458
459 memcpy(buf, buf2, buf_size);
460 pre->c.fixed_size -= 4;
461 pre->c.desc_size += 4;
Randall Spangler308d2542014-12-04 09:54:37 -0800462 TEST_EQ(vb2_verify_fw_preamble(pre, buf_size, &pubk, &wb),
Randall Spangler108d9912014-12-02 15:55:56 -0800463 VB2_ERROR_PREAMBLE_SIZE,
Randall Spangler308d2542014-12-04 09:54:37 -0800464 "vb2_verify_fw_preamble() header size");
Randall Spangler108d9912014-12-02 15:55:56 -0800465
466 memcpy(buf, buf2, buf_size);
Randall Spangler308d2542014-12-04 09:54:37 -0800467 sig = (struct vb2_signature *)(buf + pre->hash_offset);
Randall Spangler108d9912014-12-02 15:55:56 -0800468 sig->c.total_size += pre->c.total_size;
Randall Spangler308d2542014-12-04 09:54:37 -0800469 TEST_EQ(vb2_verify_fw_preamble(pre, buf_size, &pubk, &wb),
Randall Spangler108d9912014-12-02 15:55:56 -0800470 VB2_ERROR_COMMON_TOTAL_SIZE,
Randall Spangler308d2542014-12-04 09:54:37 -0800471 "vb2_verify_fw_preamble() hash size");
Randall Spangler108d9912014-12-02 15:55:56 -0800472
473 memcpy(buf, buf2, buf_size);
Randall Spangler308d2542014-12-04 09:54:37 -0800474 sig = (struct vb2_signature *)(buf + pre->hash_offset);
Randall Spangler108d9912014-12-02 15:55:56 -0800475 sig->sig_size /= 2;
Randall Spangler308d2542014-12-04 09:54:37 -0800476 TEST_EQ(vb2_verify_fw_preamble(pre, buf_size, &pubk, &wb),
Randall Spangler108d9912014-12-02 15:55:56 -0800477 VB2_ERROR_SIG_SIZE,
Randall Spangler308d2542014-12-04 09:54:37 -0800478 "vb2_verify_fw_preamble() hash integrity");
Randall Spangler108d9912014-12-02 15:55:56 -0800479
480 memcpy(buf, buf2, buf_size);
481 pre->hash_count++;
Randall Spangler308d2542014-12-04 09:54:37 -0800482 TEST_EQ(vb2_verify_fw_preamble(pre, buf_size, &pubk, &wb),
Randall Spangler108d9912014-12-02 15:55:56 -0800483 VB2_ERROR_COMMON_MEMBER_OVERLAP,
Randall Spangler308d2542014-12-04 09:54:37 -0800484 "vb2_verify_fw_preamble() hash count");
Randall Spangler108d9912014-12-02 15:55:56 -0800485
486 memcpy(buf, buf2, buf_size);
Randall Spangler308d2542014-12-04 09:54:37 -0800487 sig = (struct vb2_signature *)(buf + pre->sig_offset);
Randall Spangler108d9912014-12-02 15:55:56 -0800488 sig->c.total_size += 4;
Randall Spangler308d2542014-12-04 09:54:37 -0800489 TEST_EQ(vb2_verify_fw_preamble(pre, buf_size, &pubk, &wb),
Randall Spangler108d9912014-12-02 15:55:56 -0800490 VB2_ERROR_COMMON_TOTAL_SIZE,
Randall Spangler308d2542014-12-04 09:54:37 -0800491 "vb2_verify_fw_preamble() sig inside");
Randall Spangler108d9912014-12-02 15:55:56 -0800492
493 memcpy(buf, buf2, buf_size);
Randall Spangler308d2542014-12-04 09:54:37 -0800494 sig = (struct vb2_signature *)(buf + pre->sig_offset);
Randall Spangler108d9912014-12-02 15:55:56 -0800495 buf[pre->sig_offset + sig->sig_offset]++;
Randall Spangler308d2542014-12-04 09:54:37 -0800496 TEST_EQ(vb2_verify_fw_preamble(pre, buf_size, &pubk, &wb),
Randall Spangler108d9912014-12-02 15:55:56 -0800497 VB2_ERROR_VDATA_VERIFY_DIGEST,
Randall Spangler308d2542014-12-04 09:54:37 -0800498 "vb2_verify_fw_preamble() sig corrupt");
Randall Spangler108d9912014-12-02 15:55:56 -0800499
500 memcpy(buf, buf2, buf_size);
501 pre->flags++;
Randall Spangler308d2542014-12-04 09:54:37 -0800502 TEST_EQ(vb2_verify_fw_preamble(pre, buf_size, &pubk, &wb),
Randall Spangler108d9912014-12-02 15:55:56 -0800503 VB2_ERROR_VDATA_VERIFY_DIGEST,
Randall Spangler308d2542014-12-04 09:54:37 -0800504 "vb2_verify_fw_preamble() preamble corrupt");
Randall Spangler108d9912014-12-02 15:55:56 -0800505
506 free(buf);
507 free(buf2);
508}
509
510int main(int argc, char* argv[])
511{
512 test_struct_packing();
513 test_common_header_functions();
514 test_sig_size();
515 test_verify_hash();
516 test_verify_keyblock();
517 test_verify_fw_preamble();
518
519 return gTestSuccess ? 0 : 255;
520}