blob: 9d4cc50b5321206ea8f7313492588b0b82488652 [file] [log] [blame]
Randall Spangler7141d732014-05-15 15:34:54 -07001/* 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
Randall Spangler7141d732014-05-15 15:34:54 -07008#include <stdio.h>
Randall Spangler224f5ac2014-06-06 09:42:30 -07009
10#include "2sysincludes.h"
Randall Spangler224f5ac2014-06-06 09:42:30 -070011#include "2rsa.h"
Randall Spangler7141d732014-05-15 15:34:54 -070012
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"
Randall Spangler6f1b82a2014-12-03 12:29:37 -080018#include "vb2_common.h"
Randall Spangler7141d732014-05-15 15:34:54 -070019#include "vboot_common.h"
20#include "test_common.h"
21
Randall Spangler7141d732014-05-15 15:34:54 -070022static 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
32static void test_verify_keyblock(const VbPublicKey *public_key,
33 const VbPrivateKey *private_key,
34 const VbPublicKey *data_key)
35{
Bill Richardson73e5eb32015-01-26 12:18:25 -080036 uint8_t workbuf[VB2_KEY_BLOCK_VERIFY_WORKBUF_BYTES]
37 __attribute__ ((aligned (VB2_WORKBUF_ALIGN)));
Randall Spangler7141d732014-05-15 15:34:54 -070038 struct vb2_workbuf wb;
39 struct vb2_public_key key;
40 struct vb2_keyblock *hdr;
41 struct vb2_keyblock *h;
42 uint32_t hsize;
43
44 vb2_workbuf_init(&wb, workbuf, sizeof(workbuf));
45
46 /* Unpack public key */
Randall Spangler224f5ac2014-06-06 09:42:30 -070047 TEST_SUCC(vb2_unpack_key(&key, (uint8_t *)public_key,
48 public_key->key_offset + public_key->key_size),
49 "vb2_verify_keyblock public key");
Randall Spangler7141d732014-05-15 15:34:54 -070050
51 hdr = (struct vb2_keyblock *)
52 KeyBlockCreate(data_key, private_key, 0x1234);
53 TEST_NEQ((size_t)hdr, 0, "vb2_verify_keyblock() prerequisites");
54 if (!hdr)
55 return;
56 hsize = hdr->keyblock_size;
57 h = (struct vb2_keyblock *)malloc(hsize + 2048);
58
59 Memcpy(h, hdr, hsize);
Randall Spangler224f5ac2014-06-06 09:42:30 -070060 TEST_SUCC(vb2_verify_keyblock(h, hsize, &key, &wb),
61 "vb2_verify_keyblock() ok using key");
Randall Spangler7141d732014-05-15 15:34:54 -070062
63 Memcpy(h, hdr, hsize);
Randall Spangler224f5ac2014-06-06 09:42:30 -070064 TEST_EQ(vb2_verify_keyblock(h, hsize - 1, &key, &wb),
65 VB2_ERROR_KEYBLOCK_SIZE, "vb2_verify_keyblock() size--");
Randall Spangler7141d732014-05-15 15:34:54 -070066
Randall Spangler224f5ac2014-06-06 09:42:30 -070067 /* Buffer is allowed to be bigger than keyblock */
Randall Spangler7141d732014-05-15 15:34:54 -070068 Memcpy(h, hdr, hsize);
Randall Spangler224f5ac2014-06-06 09:42:30 -070069 TEST_SUCC(vb2_verify_keyblock(h, hsize + 1, &key, &wb),
70 "vb2_verify_keyblock() size++");
Randall Spangler7141d732014-05-15 15:34:54 -070071
72 Memcpy(h, hdr, hsize);
73 h->magic[0] &= 0x12;
Randall Spangler224f5ac2014-06-06 09:42:30 -070074 TEST_EQ(vb2_verify_keyblock(h, hsize, &key, &wb),
75 VB2_ERROR_KEYBLOCK_MAGIC, "vb2_verify_keyblock() magic");
Randall Spangler7141d732014-05-15 15:34:54 -070076
77 /* Care about major version but not minor */
78 Memcpy(h, hdr, hsize);
79 h->header_version_major++;
80 resign_keyblock(h, private_key);
Randall Spangler224f5ac2014-06-06 09:42:30 -070081 TEST_EQ(vb2_verify_keyblock(h, hsize, &key, &wb),
82 VB2_ERROR_KEYBLOCK_HEADER_VERSION,
83 "vb2_verify_keyblock() major++");
Randall Spangler7141d732014-05-15 15:34:54 -070084
85 Memcpy(h, hdr, hsize);
86 h->header_version_major--;
87 resign_keyblock(h, private_key);
Randall Spangler224f5ac2014-06-06 09:42:30 -070088 TEST_EQ(vb2_verify_keyblock(h, hsize, &key, &wb),
89 VB2_ERROR_KEYBLOCK_HEADER_VERSION,
90 "vb2_verify_keyblock() major--");
Randall Spangler7141d732014-05-15 15:34:54 -070091
92 Memcpy(h, hdr, hsize);
93 h->header_version_minor++;
94 resign_keyblock(h, private_key);
Randall Spangler224f5ac2014-06-06 09:42:30 -070095 TEST_SUCC(vb2_verify_keyblock(h, hsize, &key, &wb),
96 "vb2_verify_keyblock() minor++");
Randall Spangler7141d732014-05-15 15:34:54 -070097
98 Memcpy(h, hdr, hsize);
99 h->header_version_minor--;
100 resign_keyblock(h, private_key);
Randall Spangler224f5ac2014-06-06 09:42:30 -0700101 TEST_SUCC(vb2_verify_keyblock(h, hsize, &key, &wb),
102 "vb2_verify_keyblock() minor--");
Randall Spangler7141d732014-05-15 15:34:54 -0700103
104 /* Check signature */
105 Memcpy(h, hdr, hsize);
106 h->keyblock_signature.sig_offset = hsize;
107 resign_keyblock(h, private_key);
Randall Spangler224f5ac2014-06-06 09:42:30 -0700108 TEST_EQ(vb2_verify_keyblock(h, hsize, &key, &wb),
109 VB2_ERROR_KEYBLOCK_SIG_OUTSIDE,
110 "vb2_verify_keyblock() sig off end");
Randall Spangler7141d732014-05-15 15:34:54 -0700111
112 Memcpy(h, hdr, hsize);
113 h->keyblock_signature.sig_size--;
114 resign_keyblock(h, private_key);
Randall Spangler224f5ac2014-06-06 09:42:30 -0700115 TEST_EQ(vb2_verify_keyblock(h, hsize, &key, &wb),
116 VB2_ERROR_KEYBLOCK_SIG_INVALID,
117 "vb2_verify_keyblock() sig too small");
Randall Spangler7141d732014-05-15 15:34:54 -0700118
119 Memcpy(h, hdr, hsize);
120 ((uint8_t *)vb2_packed_key_data(&h->data_key))[0] ^= 0x34;
Randall Spangler224f5ac2014-06-06 09:42:30 -0700121 TEST_EQ(vb2_verify_keyblock(h, hsize, &key, &wb),
122 VB2_ERROR_KEYBLOCK_SIG_INVALID,
123 "vb2_verify_keyblock() sig mismatch");
Randall Spangler7141d732014-05-15 15:34:54 -0700124
125 Memcpy(h, hdr, hsize);
126 h->keyblock_signature.data_size = h->keyblock_size + 1;
Randall Spangler224f5ac2014-06-06 09:42:30 -0700127 TEST_EQ(vb2_verify_keyblock(h, hsize, &key, &wb),
128 VB2_ERROR_KEYBLOCK_SIGNED_TOO_MUCH,
129 "vb2_verify_keyblock() sig data past end of block");
Randall Spangler7141d732014-05-15 15:34:54 -0700130
131 /* Check that we signed header and data key */
132 Memcpy(h, hdr, hsize);
133 h->keyblock_signature.data_size = 4;
134 h->data_key.key_offset = 0;
135 h->data_key.key_size = 0;
136 resign_keyblock(h, private_key);
Randall Spangler224f5ac2014-06-06 09:42:30 -0700137 TEST_EQ(vb2_verify_keyblock(h, hsize, &key, &wb),
138 VB2_ERROR_KEYBLOCK_SIGNED_TOO_LITTLE,
139 "vb2_verify_keyblock() didn't sign header");
Randall Spangler7141d732014-05-15 15:34:54 -0700140
141 Memcpy(h, hdr, hsize);
142 h->data_key.key_offset = hsize;
143 resign_keyblock(h, private_key);
Randall Spangler224f5ac2014-06-06 09:42:30 -0700144 TEST_EQ(vb2_verify_keyblock(h, hsize, &key, &wb),
145 VB2_ERROR_KEYBLOCK_DATA_KEY_OUTSIDE,
146 "vb2_verify_keyblock() data key off end");
Randall Spangler7141d732014-05-15 15:34:54 -0700147
148 /* Corner cases for error checking */
Randall Spangler224f5ac2014-06-06 09:42:30 -0700149 TEST_EQ(vb2_verify_keyblock(NULL, 4, &key, &wb),
150 VB2_ERROR_KEYBLOCK_TOO_SMALL_FOR_HEADER,
151 "vb2_verify_keyblock size too small");
Randall Spangler7141d732014-05-15 15:34:54 -0700152
153 /*
154 * TODO: verify parser can support a bigger header (i.e., one where
155 * data_key.key_offset is bigger than expected).
156 */
157
158 free(h);
159 free(hdr);
160}
161
162static void resign_fw_preamble(struct vb2_fw_preamble *h,
163 const VbPrivateKey *key)
164{
165 VbSignature *sig = CalculateSignature(
166 (const uint8_t *)h, h->preamble_signature.data_size, key);
167
168 SignatureCopy((VbSignature *)&h->preamble_signature, sig);
169 free(sig);
170}
171
172static void test_verify_fw_preamble(const VbPublicKey *public_key,
173 const VbPrivateKey *private_key,
174 const VbPublicKey *kernel_subkey)
175{
176 struct vb2_fw_preamble *hdr;
177 struct vb2_fw_preamble *h;
178 struct vb2_public_key rsa;
Bill Richardson73e5eb32015-01-26 12:18:25 -0800179 uint8_t workbuf[VB2_VERIFY_FIRMWARE_PREAMBLE_WORKBUF_BYTES]
180 __attribute__ ((aligned (VB2_WORKBUF_ALIGN)));
Randall Spangler7141d732014-05-15 15:34:54 -0700181 struct vb2_workbuf wb;
182 uint32_t hsize;
183
184 vb2_workbuf_init(&wb, workbuf, sizeof(workbuf));
185
186 /* Create a dummy signature */
187 VbSignature *body_sig = SignatureAlloc(56, 78);
188
Randall Spangler224f5ac2014-06-06 09:42:30 -0700189 TEST_SUCC(vb2_unpack_key(&rsa, (uint8_t *)public_key,
190 public_key->key_offset + public_key->key_size),
191 "vb2_verify_fw_preamble() prereq key");
Randall Spangler7141d732014-05-15 15:34:54 -0700192
193 hdr = (struct vb2_fw_preamble *)
194 CreateFirmwarePreamble(0x1234, kernel_subkey, body_sig,
195 private_key, 0x5678);
196 TEST_PTR_NEQ(hdr, NULL,
197 "VerifyFirmwarePreamble() prereq test preamble");
198 if (!hdr)
199 return;
200 hsize = (uint32_t) hdr->preamble_size;
201 h = (struct vb2_fw_preamble *)malloc(hsize + 16384);
202
203 Memcpy(h, hdr, hsize);
Randall Spangler224f5ac2014-06-06 09:42:30 -0700204 TEST_SUCC(vb2_verify_fw_preamble(h, hsize, &rsa, &wb),
205 "vb2_verify_fw_preamble() ok using key");
Randall Spangler7141d732014-05-15 15:34:54 -0700206
207 Memcpy(h, hdr, hsize);
Randall Spangler224f5ac2014-06-06 09:42:30 -0700208 TEST_EQ(vb2_verify_fw_preamble(h, 4, &rsa, &wb),
209 VB2_ERROR_PREAMBLE_TOO_SMALL_FOR_HEADER,
210 "vb2_verify_fw_preamble() size tiny");
Randall Spangler7141d732014-05-15 15:34:54 -0700211
212 Memcpy(h, hdr, hsize);
Randall Spangler224f5ac2014-06-06 09:42:30 -0700213 TEST_EQ(vb2_verify_fw_preamble(h, hsize - 1, &rsa, &wb),
214 VB2_ERROR_PREAMBLE_SIZE,
215 "vb2_verify_fw_preamble() size--");
Randall Spangler7141d732014-05-15 15:34:54 -0700216
Randall Spangler224f5ac2014-06-06 09:42:30 -0700217 /* Buffer is allowed to be bigger than preamble */
Randall Spangler7141d732014-05-15 15:34:54 -0700218 Memcpy(h, hdr, hsize);
Randall Spangler224f5ac2014-06-06 09:42:30 -0700219 TEST_SUCC(vb2_verify_fw_preamble(h, hsize + 1, &rsa, &wb),
220 "vb2_verify_fw_preamble() size++");
Randall Spangler7141d732014-05-15 15:34:54 -0700221
222 /* Care about major version but not minor */
223 Memcpy(h, hdr, hsize);
224 h->header_version_major++;
225 resign_fw_preamble(h, private_key);
Randall Spangler224f5ac2014-06-06 09:42:30 -0700226 TEST_EQ(vb2_verify_fw_preamble(h, hsize, &rsa, &wb),
227 VB2_ERROR_PREAMBLE_HEADER_VERSION
228 , "vb2_verify_fw_preamble() major++");
Randall Spangler7141d732014-05-15 15:34:54 -0700229
230 Memcpy(h, hdr, hsize);
231 h->header_version_major--;
232 resign_fw_preamble(h, private_key);
Randall Spangler224f5ac2014-06-06 09:42:30 -0700233 TEST_EQ(vb2_verify_fw_preamble(h, hsize, &rsa, &wb),
234 VB2_ERROR_PREAMBLE_HEADER_VERSION,
235 "vb2_verify_fw_preamble() major--");
Randall Spangler7141d732014-05-15 15:34:54 -0700236
237 Memcpy(h, hdr, hsize);
238 h->header_version_minor++;
239 resign_fw_preamble(h, private_key);
Randall Spangler224f5ac2014-06-06 09:42:30 -0700240 TEST_SUCC(vb2_verify_fw_preamble(h, hsize, &rsa, &wb),
241 "vb2_verify_fw_preamble() minor++");
Randall Spangler7141d732014-05-15 15:34:54 -0700242
243 Memcpy(h, hdr, hsize);
244 h->header_version_minor--;
245 resign_fw_preamble(h, private_key);
Randall Spangler224f5ac2014-06-06 09:42:30 -0700246 TEST_EQ(vb2_verify_fw_preamble(h, hsize, &rsa, &wb),
247 VB2_ERROR_PREAMBLE_HEADER_OLD,
248 "vb2_verify_fw_preamble() 2.0 not supported");
Randall Spangler7141d732014-05-15 15:34:54 -0700249
250 /* Check signature */
251 Memcpy(h, hdr, hsize);
252 h->preamble_signature.sig_offset = hsize;
253 resign_fw_preamble(h, private_key);
Randall Spangler224f5ac2014-06-06 09:42:30 -0700254 TEST_EQ(vb2_verify_fw_preamble(h, hsize, &rsa, &wb),
255 VB2_ERROR_PREAMBLE_SIG_OUTSIDE,
256 "vb2_verify_fw_preamble() sig off end");
Randall Spangler7141d732014-05-15 15:34:54 -0700257
258 Memcpy(h, hdr, hsize);
259 h->preamble_signature.sig_size--;
260 resign_fw_preamble(h, private_key);
Randall Spangler224f5ac2014-06-06 09:42:30 -0700261 TEST_EQ(vb2_verify_fw_preamble(h, hsize, &rsa, &wb),
262 VB2_ERROR_PREAMBLE_SIG_INVALID,
263 "vb2_verify_fw_preamble() sig too small");
Randall Spangler7141d732014-05-15 15:34:54 -0700264
265 Memcpy(h, hdr, hsize);
266 ((uint8_t *)vb2_packed_key_data(&h->kernel_subkey))[0] ^= 0x34;
Randall Spangler224f5ac2014-06-06 09:42:30 -0700267 TEST_EQ(vb2_verify_fw_preamble(h, hsize, &rsa, &wb),
268 VB2_ERROR_PREAMBLE_SIG_INVALID,
269 "vb2_verify_fw_preamble() sig mismatch");
Randall Spangler7141d732014-05-15 15:34:54 -0700270
271 /* Check that we signed header, kernel subkey, and body sig */
272 Memcpy(h, hdr, hsize);
273 h->preamble_signature.data_size = 4;
274 h->kernel_subkey.key_offset = 0;
275 h->kernel_subkey.key_size = 0;
276 h->body_signature.sig_offset = 0;
277 h->body_signature.sig_size = 0;
278 resign_fw_preamble(h, private_key);
Randall Spangler224f5ac2014-06-06 09:42:30 -0700279 TEST_EQ(vb2_verify_fw_preamble(h, hsize, &rsa, &wb),
280 VB2_ERROR_PREAMBLE_SIGNED_TOO_LITTLE,
281 "vb2_verify_fw_preamble() didn't sign header");
Randall Spangler7141d732014-05-15 15:34:54 -0700282
283 Memcpy(h, hdr, hsize);
284 h->kernel_subkey.key_offset = hsize;
285 resign_fw_preamble(h, private_key);
Randall Spangler224f5ac2014-06-06 09:42:30 -0700286 TEST_EQ(vb2_verify_fw_preamble(h, hsize, &rsa, &wb),
287 VB2_ERROR_PREAMBLE_KERNEL_SUBKEY_OUTSIDE,
288 "vb2_verify_fw_preamble() kernel subkey off end");
Randall Spangler7141d732014-05-15 15:34:54 -0700289
290 Memcpy(h, hdr, hsize);
291 h->body_signature.sig_offset = hsize;
292 resign_fw_preamble(h, private_key);
Randall Spangler224f5ac2014-06-06 09:42:30 -0700293 TEST_EQ(vb2_verify_fw_preamble(h, hsize, &rsa, &wb),
294 VB2_ERROR_PREAMBLE_BODY_SIG_OUTSIDE,
295 "vb2_verify_fw_preamble() body sig off end");
Randall Spangler7141d732014-05-15 15:34:54 -0700296
297 /* TODO: verify with extra padding at end of header. */
298
299 free(h);
300 free(hdr);
301}
302
303int test_permutation(int signing_key_algorithm, int data_key_algorithm,
304 const char *keys_dir)
305{
306 char filename[1024];
307 int signing_rsa_len = siglen_map[signing_key_algorithm] * 8;
308 int data_rsa_len = siglen_map[data_key_algorithm] * 8;
309
310 VbPrivateKey *signing_private_key = NULL;
311 VbPublicKey *signing_public_key = NULL;
312 VbPublicKey *data_public_key = NULL;
313
314 printf("***Testing signing algorithm: %s\n",
315 algo_strings[signing_key_algorithm]);
316 printf("***With data key algorithm: %s\n",
317 algo_strings[data_key_algorithm]);
318
319 sprintf(filename, "%s/key_rsa%d.pem", keys_dir, signing_rsa_len);
320 signing_private_key = PrivateKeyReadPem(filename,
321 signing_key_algorithm);
322 if (!signing_private_key) {
323 fprintf(stderr, "Error reading signing_private_key: %s\n",
324 filename);
325 return 1;
326 }
327
328 sprintf(filename, "%s/key_rsa%d.keyb", keys_dir, signing_rsa_len);
329 signing_public_key = PublicKeyReadKeyb(filename,
330 signing_key_algorithm, 1);
331 if (!signing_public_key) {
332 fprintf(stderr, "Error reading signing_public_key: %s\n",
333 filename);
334 return 1;
335 }
336
337 sprintf(filename, "%s/key_rsa%d.keyb", keys_dir, data_rsa_len);
338 data_public_key = PublicKeyReadKeyb(filename,
339 data_key_algorithm, 1);
340 if (!data_public_key) {
341 fprintf(stderr, "Error reading data_public_key: %s\n",
342 filename);
343 return 1;
344 }
345
346 test_verify_keyblock(signing_public_key, signing_private_key,
347 data_public_key);
348 test_verify_fw_preamble(signing_public_key, signing_private_key,
349 data_public_key);
350
351 if (signing_public_key)
352 free(signing_public_key);
353 if (signing_private_key)
354 free(signing_private_key);
355 if (data_public_key)
356 free(data_public_key);
357
358 return 0;
359}
360
361struct test_perm
362{
363 int signing_algorithm;
364 int data_key_algorithm;
365};
366
367/* Permutations of signing and data key algorithms in active use */
368const struct test_perm test_perms[] = {
369 {VB2_ALG_RSA4096_SHA256, VB2_ALG_RSA2048_SHA256},
370 {VB2_ALG_RSA8192_SHA512, VB2_ALG_RSA2048_SHA256},
371 {VB2_ALG_RSA8192_SHA512, VB2_ALG_RSA4096_SHA256},
372};
373
374int main(int argc, char *argv[])
375{
376 if (argc == 2) {
377 /* Test only the algorithms we use */
378 int i;
379
380 for (i = 0; i < ARRAY_SIZE(test_perms); i++) {
381 if (test_permutation(test_perms[i].signing_algorithm,
382 test_perms[i].data_key_algorithm,
383 argv[1]))
384 return 1;
385 }
386
387 } else if (argc == 3 && !strcasecmp(argv[2], "--all")) {
388 /* Test all the algorithms */
389 int sign_alg, data_alg;
390
391 for (sign_alg = 0; sign_alg < VB2_ALG_COUNT; sign_alg++) {
392 for (data_alg = 0; data_alg < VB2_ALG_COUNT;
393 data_alg++) {
394 if (test_permutation(sign_alg, data_alg,
395 argv[1]))
396 return 1;
397 }
398 }
399 } else {
400 fprintf(stderr, "Usage: %s <keys_dir> [--all]", argv[0]);
401 return -1;
402 }
403
404 return gTestSuccess ? 0 : 255;
405}