6de4b8a0b0d1463ac69d6b9b6ec5ed8b037c857d
[vboot.git] / tests / vb20_common3_tests.c
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 image library.
6  */
7
8 #include <stdio.h>
9
10 #include "2sysincludes.h"
11 #include "2rsa.h"
12
13 #include "file_keys.h"
14 #include "host_common.h"
15 #include "host_key.h"
16 #include "host_keyblock.h"
17 #include "host_signature.h"
18 #include "vb2_common.h"
19 #include "vboot_common.h"
20 #include "test_common.h"
21
22 static void resign_keyblock(struct vb2_keyblock *h, const VbPrivateKey *key)
23 {
24         VbSignature *sig =
25                 CalculateSignature((const uint8_t *)h,
26                                    h->keyblock_signature.data_size, key);
27
28         SignatureCopy((VbSignature *)&h->keyblock_signature, sig);
29         free(sig);
30 }
31
32 static void test_check_keyblock(const VbPublicKey *public_key,
33                                  const VbPrivateKey *private_key,
34                                  const VbPublicKey *data_key)
35 {
36         struct vb2_public_key key;
37         struct vb2_keyblock *hdr;
38         struct vb2_keyblock *h;
39         struct vb2_signature *sig;
40         uint32_t hsize;
41
42         /* Unpack public key */
43         TEST_SUCC(vb2_unpack_key(&key, (uint8_t *)public_key,
44                                  public_key->key_offset + public_key->key_size),
45                   "vb2_verify_keyblock public key");
46
47         hdr = (struct vb2_keyblock *)
48                 KeyBlockCreate(data_key, private_key, 0x1234);
49         TEST_NEQ((size_t)hdr, 0, "vb2_verify_keyblock() prerequisites");
50         if (!hdr)
51                 return;
52         hsize = hdr->keyblock_size;
53         h = (struct vb2_keyblock *)malloc(hsize + 2048);
54         sig = &h->keyblock_signature;
55
56         Memcpy(h, hdr, hsize);
57         TEST_SUCC(vb2_check_keyblock(h, hsize, sig),
58                   "vb2_check_keyblock() ok");
59
60         Memcpy(h, hdr, hsize);
61         TEST_EQ(vb2_check_keyblock(h, hsize - 1, sig),
62                 VB2_ERROR_KEYBLOCK_SIZE, "vb2_check_keyblock() size--");
63
64         /* Buffer is allowed to be bigger than keyblock */
65         Memcpy(h, hdr, hsize);
66         TEST_SUCC(vb2_check_keyblock(h, hsize + 1, sig),
67                   "vb2_check_keyblock() size++");
68
69         Memcpy(h, hdr, hsize);
70         h->magic[0] &= 0x12;
71         TEST_EQ(vb2_check_keyblock(h, hsize, sig),
72                 VB2_ERROR_KEYBLOCK_MAGIC, "vb2_check_keyblock() magic");
73
74         /* Care about major version but not minor */
75         Memcpy(h, hdr, hsize);
76         h->header_version_major++;
77         resign_keyblock(h, private_key);
78         TEST_EQ(vb2_check_keyblock(h, hsize, sig),
79                 VB2_ERROR_KEYBLOCK_HEADER_VERSION,
80                 "vb2_check_keyblock() major++");
81
82         Memcpy(h, hdr, hsize);
83         h->header_version_major--;
84         resign_keyblock(h, private_key);
85         TEST_EQ(vb2_check_keyblock(h, hsize, sig),
86                 VB2_ERROR_KEYBLOCK_HEADER_VERSION,
87                 "vb2_check_keyblock() major--");
88
89         Memcpy(h, hdr, hsize);
90         h->header_version_minor++;
91         resign_keyblock(h, private_key);
92         TEST_SUCC(vb2_check_keyblock(h, hsize, sig),
93                   "vb2_check_keyblock() minor++");
94
95         Memcpy(h, hdr, hsize);
96         h->header_version_minor--;
97         resign_keyblock(h, private_key);
98         TEST_SUCC(vb2_check_keyblock(h, hsize, sig),
99                   "vb2_check_keyblock() minor--");
100
101         /* Check signature */
102         Memcpy(h, hdr, hsize);
103         h->keyblock_signature.sig_offset = hsize;
104         resign_keyblock(h, private_key);
105         TEST_EQ(vb2_check_keyblock(h, hsize, sig),
106                 VB2_ERROR_KEYBLOCK_SIG_OUTSIDE,
107                 "vb2_check_keyblock() sig off end");
108
109         Memcpy(h, hdr, hsize);
110         h->keyblock_signature.data_size = h->keyblock_size + 1;
111         TEST_EQ(vb2_check_keyblock(h, hsize, sig),
112                 VB2_ERROR_KEYBLOCK_SIGNED_TOO_MUCH,
113                 "vb2_check_keyblock() sig data past end of block");
114
115         /* Check that we signed header and data key */
116         Memcpy(h, hdr, hsize);
117         h->keyblock_signature.data_size = 4;
118         h->data_key.key_offset = 0;
119         h->data_key.key_size = 0;
120         resign_keyblock(h, private_key);
121         TEST_EQ(vb2_check_keyblock(h, hsize, sig),
122                 VB2_ERROR_KEYBLOCK_SIGNED_TOO_LITTLE,
123                 "vb2_check_keyblock() didn't sign header");
124
125         Memcpy(h, hdr, hsize);
126         h->data_key.key_offset = hsize;
127         resign_keyblock(h, private_key);
128         TEST_EQ(vb2_check_keyblock(h, hsize, sig),
129                 VB2_ERROR_KEYBLOCK_DATA_KEY_OUTSIDE,
130                 "vb2_check_keyblock() data key off end");
131
132         /* Corner cases for error checking */
133         TEST_EQ(vb2_check_keyblock(NULL, 4, sig),
134                 VB2_ERROR_KEYBLOCK_TOO_SMALL_FOR_HEADER,
135                 "vb2_check_keyblock size too small");
136
137         /*
138          * TODO: verify parser can support a bigger header (i.e., one where
139          * data_key.key_offset is bigger than expected).
140          */
141
142         free(h);
143         free(hdr);
144 }
145
146 static void test_verify_keyblock(const VbPublicKey *public_key,
147                                  const VbPrivateKey *private_key,
148                                  const VbPublicKey *data_key)
149 {
150         uint8_t workbuf[VB2_KEY_BLOCK_VERIFY_WORKBUF_BYTES]
151                 __attribute__ ((aligned (VB2_WORKBUF_ALIGN)));
152         struct vb2_workbuf wb;
153         struct vb2_public_key key;
154         struct vb2_keyblock *hdr;
155         struct vb2_keyblock *h;
156         uint32_t hsize;
157
158         vb2_workbuf_init(&wb, workbuf, sizeof(workbuf));
159
160         /* Unpack public key */
161         TEST_SUCC(vb2_unpack_key(&key, (uint8_t *)public_key,
162                                  public_key->key_offset + public_key->key_size),
163                   "vb2_verify_keyblock public key");
164
165         hdr = (struct vb2_keyblock *)
166                 KeyBlockCreate(data_key, private_key, 0x1234);
167         TEST_NEQ((size_t)hdr, 0, "vb2_verify_keyblock() prerequisites");
168         if (!hdr)
169                 return;
170         hsize = hdr->keyblock_size;
171         h = (struct vb2_keyblock *)malloc(hsize + 2048);
172
173         Memcpy(h, hdr, hsize);
174         TEST_SUCC(vb2_verify_keyblock(h, hsize, &key, &wb),
175                   "vb2_verify_keyblock() ok using key");
176
177         /* Failures in keyblock check also cause verify to fail */
178         Memcpy(h, hdr, hsize);
179         TEST_EQ(vb2_verify_keyblock(h, hsize - 1, &key, &wb),
180                 VB2_ERROR_KEYBLOCK_SIZE, "vb2_verify_keyblock() check");
181
182         /* Check signature */
183         Memcpy(h, hdr, hsize);
184         h->keyblock_signature.sig_size--;
185         resign_keyblock(h, private_key);
186         TEST_EQ(vb2_verify_keyblock(h, hsize, &key, &wb),
187                 VB2_ERROR_KEYBLOCK_SIG_INVALID,
188                 "vb2_verify_keyblock() sig too small");
189
190         Memcpy(h, hdr, hsize);
191         ((uint8_t *)vb2_packed_key_data(&h->data_key))[0] ^= 0x34;
192         TEST_EQ(vb2_verify_keyblock(h, hsize, &key, &wb),
193                 VB2_ERROR_KEYBLOCK_SIG_INVALID,
194                 "vb2_verify_keyblock() sig mismatch");
195
196         /*
197          * TODO: verify parser can support a bigger header (i.e., one where
198          * data_key.key_offset is bigger than expected).
199          */
200
201         free(h);
202         free(hdr);
203 }
204
205 static void resign_fw_preamble(struct vb2_fw_preamble *h,
206                                struct vb2_private_key *key)
207 {
208         struct vb2_signature *sig = vb2_calculate_signature(
209                 (const uint8_t *)h, h->preamble_signature.data_size, key);
210
211         vb2_copy_signature(&h->preamble_signature, sig);
212         free(sig);
213 }
214
215 static void test_verify_fw_preamble(const VbPublicKey *public_key,
216                                     struct vb2_private_key *private_key,
217                                     struct vb2_packed_key *kernel_subkey)
218 {
219         struct vb2_fw_preamble *hdr;
220         struct vb2_fw_preamble *h;
221         struct vb2_public_key rsa;
222         uint8_t workbuf[VB2_VERIFY_FIRMWARE_PREAMBLE_WORKBUF_BYTES]
223                  __attribute__ ((aligned (VB2_WORKBUF_ALIGN)));
224         struct vb2_workbuf wb;
225         uint32_t hsize;
226
227         vb2_workbuf_init(&wb, workbuf, sizeof(workbuf));
228
229         /* Create a dummy signature */
230         struct vb2_signature *body_sig = vb2_alloc_signature(56, 78);
231
232         TEST_SUCC(vb2_unpack_key(&rsa, (uint8_t *)public_key,
233                                  public_key->key_offset + public_key->key_size),
234                   "vb2_verify_fw_preamble() prereq key");
235
236         hdr = vb2_create_fw_preamble(0x1234, kernel_subkey, body_sig,
237                                      private_key, 0x5678);
238         TEST_PTR_NEQ(hdr, NULL,
239                      "vb2_verify_fw_preamble() prereq test preamble");
240         if (!hdr)
241                 return;
242         hsize = (uint32_t) hdr->preamble_size;
243         h = (struct vb2_fw_preamble *)malloc(hsize + 16384);
244
245         Memcpy(h, hdr, hsize);
246         TEST_SUCC(vb2_verify_fw_preamble(h, hsize, &rsa, &wb),
247                   "vb2_verify_fw_preamble() ok using key");
248
249         Memcpy(h, hdr, hsize);
250         TEST_EQ(vb2_verify_fw_preamble(h, 4, &rsa, &wb),
251                 VB2_ERROR_PREAMBLE_TOO_SMALL_FOR_HEADER,
252                 "vb2_verify_fw_preamble() size tiny");
253
254         Memcpy(h, hdr, hsize);
255         TEST_EQ(vb2_verify_fw_preamble(h, hsize - 1, &rsa, &wb),
256                 VB2_ERROR_PREAMBLE_SIZE,
257                 "vb2_verify_fw_preamble() size--");
258
259         /* Buffer is allowed to be bigger than preamble */
260         Memcpy(h, hdr, hsize);
261         TEST_SUCC(vb2_verify_fw_preamble(h, hsize + 1, &rsa, &wb),
262                   "vb2_verify_fw_preamble() size++");
263
264         /* Care about major version but not minor */
265         Memcpy(h, hdr, hsize);
266         h->header_version_major++;
267         resign_fw_preamble(h, private_key);
268         TEST_EQ(vb2_verify_fw_preamble(h, hsize, &rsa, &wb),
269                 VB2_ERROR_PREAMBLE_HEADER_VERSION
270                 , "vb2_verify_fw_preamble() major++");
271
272         Memcpy(h, hdr, hsize);
273         h->header_version_major--;
274         resign_fw_preamble(h, private_key);
275         TEST_EQ(vb2_verify_fw_preamble(h, hsize, &rsa, &wb),
276                 VB2_ERROR_PREAMBLE_HEADER_VERSION,
277                 "vb2_verify_fw_preamble() major--");
278
279         Memcpy(h, hdr, hsize);
280         h->header_version_minor++;
281         resign_fw_preamble(h, private_key);
282         TEST_SUCC(vb2_verify_fw_preamble(h, hsize, &rsa, &wb),
283                   "vb2_verify_fw_preamble() minor++");
284
285         Memcpy(h, hdr, hsize);
286         h->header_version_minor--;
287         resign_fw_preamble(h, private_key);
288         TEST_EQ(vb2_verify_fw_preamble(h, hsize, &rsa, &wb),
289                 VB2_ERROR_PREAMBLE_HEADER_OLD,
290                 "vb2_verify_fw_preamble() 2.0 not supported");
291
292         /* Check signature */
293         Memcpy(h, hdr, hsize);
294         h->preamble_signature.sig_offset = hsize;
295         resign_fw_preamble(h, private_key);
296         TEST_EQ(vb2_verify_fw_preamble(h, hsize, &rsa, &wb),
297                 VB2_ERROR_PREAMBLE_SIG_OUTSIDE,
298                 "vb2_verify_fw_preamble() sig off end");
299
300         Memcpy(h, hdr, hsize);
301         h->preamble_signature.sig_size--;
302         resign_fw_preamble(h, private_key);
303         TEST_EQ(vb2_verify_fw_preamble(h, hsize, &rsa, &wb),
304                 VB2_ERROR_PREAMBLE_SIG_INVALID,
305                 "vb2_verify_fw_preamble() sig too small");
306
307         Memcpy(h, hdr, hsize);
308         ((uint8_t *)vb2_packed_key_data(&h->kernel_subkey))[0] ^= 0x34;
309         TEST_EQ(vb2_verify_fw_preamble(h, hsize, &rsa, &wb),
310                 VB2_ERROR_PREAMBLE_SIG_INVALID,
311                 "vb2_verify_fw_preamble() sig mismatch");
312
313         /* Check that we signed header, kernel subkey, and body sig */
314         Memcpy(h, hdr, hsize);
315         h->preamble_signature.data_size = 4;
316         h->kernel_subkey.key_offset = 0;
317         h->kernel_subkey.key_size = 0;
318         h->body_signature.sig_offset = 0;
319         h->body_signature.sig_size = 0;
320         resign_fw_preamble(h, private_key);
321         TEST_EQ(vb2_verify_fw_preamble(h, hsize, &rsa, &wb),
322                 VB2_ERROR_PREAMBLE_SIGNED_TOO_LITTLE,
323                 "vb2_verify_fw_preamble() didn't sign header");
324
325         Memcpy(h, hdr, hsize);
326         h->kernel_subkey.key_offset = hsize;
327         resign_fw_preamble(h, private_key);
328         TEST_EQ(vb2_verify_fw_preamble(h, hsize, &rsa, &wb),
329                 VB2_ERROR_PREAMBLE_KERNEL_SUBKEY_OUTSIDE,
330                 "vb2_verify_fw_preamble() kernel subkey off end");
331
332         Memcpy(h, hdr, hsize);
333         h->body_signature.sig_offset = hsize;
334         resign_fw_preamble(h, private_key);
335         TEST_EQ(vb2_verify_fw_preamble(h, hsize, &rsa, &wb),
336                 VB2_ERROR_PREAMBLE_BODY_SIG_OUTSIDE,
337                 "vb2_verify_fw_preamble() body sig off end");
338
339         /* TODO: verify with extra padding at end of header. */
340
341         free(h);
342         free(hdr);
343 }
344
345 static void resign_kernel_preamble(struct vb2_kernel_preamble *h,
346                                    const VbPrivateKey *key)
347 {
348         VbSignature *sig = CalculateSignature(
349                 (const uint8_t *)h, h->preamble_signature.data_size, key);
350
351         SignatureCopy((VbSignature *)&h->preamble_signature, sig);
352         free(sig);
353 }
354
355 static void test_verify_kernel_preamble(const VbPublicKey *public_key,
356                                         const VbPrivateKey *private_key)
357 {
358         struct vb2_kernel_preamble *hdr;
359         struct vb2_kernel_preamble *h;
360         struct vb2_public_key rsa;
361         // TODO: how many workbuf bytes?
362         uint8_t workbuf[VB2_VERIFY_FIRMWARE_PREAMBLE_WORKBUF_BYTES]
363                  __attribute__ ((aligned (VB2_WORKBUF_ALIGN)));
364         struct vb2_workbuf wb;
365         uint32_t hsize;
366
367         vb2_workbuf_init(&wb, workbuf, sizeof(workbuf));
368
369         /* Create a dummy signature */
370         struct vb2_signature *body_sig = vb2_alloc_signature(56, 0x214000);
371
372         TEST_SUCC(vb2_unpack_key(&rsa, (uint8_t *)public_key,
373                                  public_key->key_offset + public_key->key_size),
374                   "vb2_verify_kernel_preamble() prereq key");
375
376         hdr = (struct vb2_kernel_preamble *)
377                 CreateKernelPreamble(0x1234, 0x100000, 0x300000, 0x4000,
378                                      (VbSignature *)body_sig,
379                                      0x304000, 0x10000, 0, 0,
380                                      private_key);
381         TEST_PTR_NEQ(hdr, NULL,
382                      "vb2_verify_kernel_preamble() prereq test preamble");
383         if (!hdr)
384                 return;
385         hsize = (uint32_t) hdr->preamble_size;
386         h = (struct vb2_kernel_preamble *)malloc(hsize + 16384);
387
388         Memcpy(h, hdr, hsize);
389         TEST_SUCC(vb2_verify_kernel_preamble(h, hsize, &rsa, &wb),
390                   "vb2_verify_kernel_preamble() ok using key");
391
392         Memcpy(h, hdr, hsize);
393         TEST_EQ(vb2_verify_kernel_preamble(h, 4, &rsa, &wb),
394                 VB2_ERROR_PREAMBLE_TOO_SMALL_FOR_HEADER,
395                 "vb2_verify_kernel_preamble() size tiny");
396
397         Memcpy(h, hdr, hsize);
398         TEST_EQ(vb2_verify_kernel_preamble(h, hsize - 1, &rsa, &wb),
399                 VB2_ERROR_PREAMBLE_SIZE,
400                 "vb2_verify_kernel_preamble() size--");
401
402         /* Buffer is allowed to be bigger than preamble */
403         Memcpy(h, hdr, hsize);
404         TEST_SUCC(vb2_verify_kernel_preamble(h, hsize + 1, &rsa, &wb),
405                   "vb2_verify_kernel_preamble() size++");
406
407         /* Care about major version but not minor */
408         Memcpy(h, hdr, hsize);
409         h->header_version_major++;
410         resign_kernel_preamble(h, private_key);
411         TEST_EQ(vb2_verify_kernel_preamble(h, hsize, &rsa, &wb),
412                 VB2_ERROR_PREAMBLE_HEADER_VERSION
413                 , "vb2_verify_kernel_preamble() major++");
414
415         Memcpy(h, hdr, hsize);
416         h->header_version_major--;
417         resign_kernel_preamble(h, private_key);
418         TEST_EQ(vb2_verify_kernel_preamble(h, hsize, &rsa, &wb),
419                 VB2_ERROR_PREAMBLE_HEADER_VERSION,
420                 "vb2_verify_kernel_preamble() major--");
421
422         Memcpy(h, hdr, hsize);
423         h->header_version_minor++;
424         resign_kernel_preamble(h, private_key);
425         TEST_SUCC(vb2_verify_kernel_preamble(h, hsize, &rsa, &wb),
426                   "vb2_verify_kernel_preamble() minor++");
427
428         /* Check signature */
429         Memcpy(h, hdr, hsize);
430         h->preamble_signature.sig_offset = hsize;
431         resign_kernel_preamble(h, private_key);
432         TEST_EQ(vb2_verify_kernel_preamble(h, hsize, &rsa, &wb),
433                 VB2_ERROR_PREAMBLE_SIG_OUTSIDE,
434                 "vb2_verify_kernel_preamble() sig off end");
435
436         Memcpy(h, hdr, hsize);
437         h->preamble_signature.sig_size--;
438         resign_kernel_preamble(h, private_key);
439         TEST_EQ(vb2_verify_kernel_preamble(h, hsize, &rsa, &wb),
440                 VB2_ERROR_PREAMBLE_SIG_INVALID,
441                 "vb2_verify_kernel_preamble() sig too small");
442
443         Memcpy(h, hdr, hsize);
444         h->flags++;
445         TEST_EQ(vb2_verify_kernel_preamble(h, hsize, &rsa, &wb),
446                 VB2_ERROR_PREAMBLE_SIG_INVALID,
447                 "vb2_verify_kernel_preamble() sig mismatch");
448
449         /* Check that we signed header and body sig */
450         Memcpy(h, hdr, hsize);
451         h->preamble_signature.data_size = 4;
452         h->body_signature.sig_offset = 0;
453         h->body_signature.sig_size = 0;
454         resign_kernel_preamble(h, private_key);
455         TEST_EQ(vb2_verify_kernel_preamble(h, hsize, &rsa, &wb),
456                 VB2_ERROR_PREAMBLE_SIGNED_TOO_LITTLE,
457                 "vb2_verify_kernel_preamble() didn't sign header");
458
459         Memcpy(h, hdr, hsize);
460         h->body_signature.sig_offset = hsize;
461         resign_kernel_preamble(h, private_key);
462         TEST_EQ(vb2_verify_kernel_preamble(h, hsize, &rsa, &wb),
463                 VB2_ERROR_PREAMBLE_BODY_SIG_OUTSIDE,
464                 "vb2_verify_kernel_preamble() body sig off end");
465
466         /* Check bootloader inside signed body */
467         Memcpy(h, hdr, hsize);
468         h->bootloader_address = h->body_load_address - 1;
469         resign_kernel_preamble(h, private_key);
470         TEST_EQ(vb2_verify_kernel_preamble(h, hsize, &rsa, &wb),
471                 VB2_ERROR_PREAMBLE_BOOTLOADER_OUTSIDE,
472                 "vb2_verify_kernel_preamble() bootloader before body");
473
474         Memcpy(h, hdr, hsize);
475         h->bootloader_address = h->body_load_address +
476                 h->body_signature.data_size + 1;
477         resign_kernel_preamble(h, private_key);
478         TEST_EQ(vb2_verify_kernel_preamble(h, hsize, &rsa, &wb),
479                 VB2_ERROR_PREAMBLE_BOOTLOADER_OUTSIDE,
480                 "vb2_verify_kernel_preamble() bootloader off end of body");
481
482         Memcpy(h, hdr, hsize);
483         h->bootloader_address = h->body_load_address +
484                 h->body_signature.data_size + 1;
485         h->bootloader_size = 0;
486         resign_kernel_preamble(h, private_key);
487         TEST_SUCC(vb2_verify_kernel_preamble(h, hsize, &rsa, &wb),
488                   "vb2_verify_kernel_preamble() no bootloader");
489
490         /* Check vmlinuz inside signed body */
491         Memcpy(h, hdr, hsize);
492         h->vmlinuz_header_address = h->body_load_address - 1;
493         resign_kernel_preamble(h, private_key);
494         TEST_EQ(vb2_verify_kernel_preamble(h, hsize, &rsa, &wb),
495                 VB2_ERROR_PREAMBLE_VMLINUZ_HEADER_OUTSIDE,
496                 "vb2_verify_kernel_preamble() vmlinuz_header before body");
497
498         Memcpy(h, hdr, hsize);
499         h->vmlinuz_header_address = h->body_load_address +
500                 h->body_signature.data_size + 1;
501         resign_kernel_preamble(h, private_key);
502         TEST_EQ(vb2_verify_kernel_preamble(h, hsize, &rsa, &wb),
503                 VB2_ERROR_PREAMBLE_VMLINUZ_HEADER_OUTSIDE,
504                 "vb2_verify_kernel_preamble() vmlinuz_header off end of body");
505
506         Memcpy(h, hdr, hsize);
507         h->vmlinuz_header_address = h->body_load_address +
508                 h->body_signature.data_size + 1;
509         h->vmlinuz_header_size = 0;
510         resign_kernel_preamble(h, private_key);
511         TEST_SUCC(vb2_verify_kernel_preamble(h, hsize, &rsa, &wb),
512                   "vb2_verify_kernel_preamble() no vmlinuz_header");
513
514         /* TODO: verify with extra padding at end of header. */
515
516         free(h);
517         free(hdr);
518 }
519
520 int test_permutation(int signing_key_algorithm, int data_key_algorithm,
521                      const char *keys_dir)
522 {
523         char filename[1024];
524         int signing_rsa_len = siglen_map[signing_key_algorithm] * 8;
525         int data_rsa_len = siglen_map[data_key_algorithm] * 8;
526
527         VbPrivateKey *signing_private_key = NULL;
528         VbPublicKey *signing_public_key = NULL;
529         VbPublicKey *data_public_key = NULL;
530
531         printf("***Testing signing algorithm: %s\n",
532                algo_strings[signing_key_algorithm]);
533         printf("***With data key algorithm: %s\n",
534                algo_strings[data_key_algorithm]);
535
536         sprintf(filename, "%s/key_rsa%d.pem", keys_dir, signing_rsa_len);
537         signing_private_key = PrivateKeyReadPem(filename,
538                                                 signing_key_algorithm);
539         if (!signing_private_key) {
540                 fprintf(stderr, "Error reading signing_private_key: %s\n",
541                         filename);
542                 return 1;
543         }
544
545         struct vb2_private_key *signing_private_key2 =
546                 vb2_read_private_key_pem(filename, signing_key_algorithm);
547         if (!signing_private_key2) {
548                 fprintf(stderr, "Error reading signing_private_key: %s\n",
549                         filename);
550                 return 1;
551         }
552
553         sprintf(filename, "%s/key_rsa%d.keyb", keys_dir, signing_rsa_len);
554         signing_public_key = PublicKeyReadKeyb(filename,
555                                                signing_key_algorithm, 1);
556         if (!signing_public_key) {
557                 fprintf(stderr, "Error reading signing_public_key: %s\n",
558                         filename);
559                 return 1;
560         }
561
562         sprintf(filename, "%s/key_rsa%d.keyb", keys_dir, data_rsa_len);
563         data_public_key = PublicKeyReadKeyb(filename,
564                                             data_key_algorithm, 1);
565         if (!data_public_key) {
566                 fprintf(stderr, "Error reading data_public_key: %s\n",
567                         filename);
568                 return 1;
569         }
570
571         test_check_keyblock(signing_public_key, signing_private_key,
572                             data_public_key);
573         test_verify_keyblock(signing_public_key, signing_private_key,
574                              data_public_key);
575         test_verify_fw_preamble(signing_public_key, signing_private_key2,
576                                 (struct vb2_packed_key *)data_public_key);
577         test_verify_kernel_preamble(signing_public_key, signing_private_key);
578
579         if (signing_public_key)
580                 free(signing_public_key);
581         if (signing_private_key)
582                 free(signing_private_key);
583         if (signing_private_key2)
584                 free(signing_private_key2);
585         if (data_public_key)
586                 free(data_public_key);
587
588         return 0;
589 }
590
591 struct test_perm
592 {
593         int signing_algorithm;
594         int data_key_algorithm;
595 };
596
597 /* Permutations of signing and data key algorithms in active use */
598 const struct test_perm test_perms[] = {
599         {VB2_ALG_RSA4096_SHA256, VB2_ALG_RSA2048_SHA256},
600         {VB2_ALG_RSA8192_SHA512, VB2_ALG_RSA2048_SHA256},
601         {VB2_ALG_RSA8192_SHA512, VB2_ALG_RSA4096_SHA256},
602 };
603
604 int main(int argc, char *argv[])
605 {
606         if (argc == 2) {
607                 /* Test only the algorithms we use */
608                 int i;
609
610                 for (i = 0; i < ARRAY_SIZE(test_perms); i++) {
611                         if (test_permutation(test_perms[i].signing_algorithm,
612                                              test_perms[i].data_key_algorithm,
613                                              argv[1]))
614                                 return 1;
615                 }
616
617         } else if (argc == 3 && !strcasecmp(argv[2], "--all")) {
618                 /* Test all the algorithms */
619                 int sign_alg, data_alg;
620
621                 for (sign_alg = 0; sign_alg < VB2_ALG_COUNT; sign_alg++) {
622                         for (data_alg = 0; data_alg < VB2_ALG_COUNT;
623                              data_alg++) {
624                                 if (test_permutation(sign_alg, data_alg,
625                                                      argv[1]))
626                                         return 1;
627                         }
628                 }
629         } else {
630                 fprintf(stderr, "Usage: %s <keys_dir> [--all]", argv[0]);
631                 return -1;
632         }
633
634         return gTestSuccess ? 0 : 255;
635 }