17141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler/* Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
27141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler * Use of this source code is governed by a BSD-style license that can be
37141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler * found in the LICENSE file.
47141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler *
57141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler * Tests for firmware image library.
67141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler */
77141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler
87141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler#include <stdio.h>
9224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler
10224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler#include "2sysincludes.h"
11224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler#include "2rsa.h"
127141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler
137141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler#include "file_keys.h"
147141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler#include "host_common.h"
157141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler#include "host_key.h"
167141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler#include "host_keyblock.h"
177141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler#include "host_signature.h"
186f1b82ac14f341d9733d6e95d518b3ee352002efRandall Spangler#include "vb2_common.h"
197141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler#include "vboot_common.h"
207141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler#include "test_common.h"
217141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler
227141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spanglerstatic void resign_keyblock(struct vb2_keyblock *h, const VbPrivateKey *key)
237141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler{
247141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	VbSignature *sig =
257141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler		CalculateSignature((const uint8_t *)h,
267141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler				   h->keyblock_signature.data_size, key);
277141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler
287141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	SignatureCopy((VbSignature *)&h->keyblock_signature, sig);
297141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	free(sig);
307141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler}
317141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler
327141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spanglerstatic void test_verify_keyblock(const VbPublicKey *public_key,
337141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler				 const VbPrivateKey *private_key,
347141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler				 const VbPublicKey *data_key)
357141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler{
3673e5eb38821d693244f841ce4f0a14546e5b6361Bill Richardson	uint8_t workbuf[VB2_KEY_BLOCK_VERIFY_WORKBUF_BYTES]
3773e5eb38821d693244f841ce4f0a14546e5b6361Bill Richardson		__attribute__ ((aligned (VB2_WORKBUF_ALIGN)));
387141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	struct vb2_workbuf wb;
397141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	struct vb2_public_key key;
407141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	struct vb2_keyblock *hdr;
417141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	struct vb2_keyblock *h;
427141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	uint32_t hsize;
437141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler
447141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	vb2_workbuf_init(&wb, workbuf, sizeof(workbuf));
457141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler
467141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	/* Unpack public key */
47224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler	TEST_SUCC(vb2_unpack_key(&key, (uint8_t *)public_key,
48224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler				 public_key->key_offset + public_key->key_size),
49224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler		  "vb2_verify_keyblock public key");
507141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler
517141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	hdr = (struct vb2_keyblock *)
527141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler		KeyBlockCreate(data_key, private_key, 0x1234);
537141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	TEST_NEQ((size_t)hdr, 0, "vb2_verify_keyblock() prerequisites");
547141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	if (!hdr)
557141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler		return;
567141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	hsize = hdr->keyblock_size;
577141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	h = (struct vb2_keyblock *)malloc(hsize + 2048);
587141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler
597141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	Memcpy(h, hdr, hsize);
60224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler	TEST_SUCC(vb2_verify_keyblock(h, hsize, &key, &wb),
61224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler		  "vb2_verify_keyblock() ok using key");
627141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler
637141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	Memcpy(h, hdr, hsize);
64224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler	TEST_EQ(vb2_verify_keyblock(h, hsize - 1, &key, &wb),
65224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler		VB2_ERROR_KEYBLOCK_SIZE, "vb2_verify_keyblock() size--");
667141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler
67224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler	/* Buffer is allowed to be bigger than keyblock */
687141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	Memcpy(h, hdr, hsize);
69224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler	TEST_SUCC(vb2_verify_keyblock(h, hsize + 1, &key, &wb),
70224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler		  "vb2_verify_keyblock() size++");
717141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler
727141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	Memcpy(h, hdr, hsize);
737141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	h->magic[0] &= 0x12;
74224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler	TEST_EQ(vb2_verify_keyblock(h, hsize, &key, &wb),
75224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler		VB2_ERROR_KEYBLOCK_MAGIC, "vb2_verify_keyblock() magic");
767141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler
777141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	/* Care about major version but not minor */
787141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	Memcpy(h, hdr, hsize);
797141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	h->header_version_major++;
807141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	resign_keyblock(h, private_key);
81224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler	TEST_EQ(vb2_verify_keyblock(h, hsize, &key, &wb),
82224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler		VB2_ERROR_KEYBLOCK_HEADER_VERSION,
83224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler		"vb2_verify_keyblock() major++");
847141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler
857141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	Memcpy(h, hdr, hsize);
867141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	h->header_version_major--;
877141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	resign_keyblock(h, private_key);
88224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler	TEST_EQ(vb2_verify_keyblock(h, hsize, &key, &wb),
89224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler		VB2_ERROR_KEYBLOCK_HEADER_VERSION,
90224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler		"vb2_verify_keyblock() major--");
917141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler
927141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	Memcpy(h, hdr, hsize);
937141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	h->header_version_minor++;
947141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	resign_keyblock(h, private_key);
95224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler	TEST_SUCC(vb2_verify_keyblock(h, hsize, &key, &wb),
96224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler		  "vb2_verify_keyblock() minor++");
977141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler
987141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	Memcpy(h, hdr, hsize);
997141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	h->header_version_minor--;
1007141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	resign_keyblock(h, private_key);
101224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler	TEST_SUCC(vb2_verify_keyblock(h, hsize, &key, &wb),
102224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler		  "vb2_verify_keyblock() minor--");
1037141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler
1047141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	/* Check signature */
1057141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	Memcpy(h, hdr, hsize);
1067141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	h->keyblock_signature.sig_offset = hsize;
1077141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	resign_keyblock(h, private_key);
108224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler	TEST_EQ(vb2_verify_keyblock(h, hsize, &key, &wb),
109224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler		VB2_ERROR_KEYBLOCK_SIG_OUTSIDE,
110224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler		"vb2_verify_keyblock() sig off end");
1117141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler
1127141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	Memcpy(h, hdr, hsize);
1137141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	h->keyblock_signature.sig_size--;
1147141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	resign_keyblock(h, private_key);
115224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler	TEST_EQ(vb2_verify_keyblock(h, hsize, &key, &wb),
116224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler		VB2_ERROR_KEYBLOCK_SIG_INVALID,
117224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler		"vb2_verify_keyblock() sig too small");
1187141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler
1197141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	Memcpy(h, hdr, hsize);
1207141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	((uint8_t *)vb2_packed_key_data(&h->data_key))[0] ^= 0x34;
121224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler	TEST_EQ(vb2_verify_keyblock(h, hsize, &key, &wb),
122224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler		VB2_ERROR_KEYBLOCK_SIG_INVALID,
123224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler		"vb2_verify_keyblock() sig mismatch");
1247141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler
1257141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	Memcpy(h, hdr, hsize);
1267141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	h->keyblock_signature.data_size = h->keyblock_size + 1;
127224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler	TEST_EQ(vb2_verify_keyblock(h, hsize, &key, &wb),
128224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler		VB2_ERROR_KEYBLOCK_SIGNED_TOO_MUCH,
129224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler		"vb2_verify_keyblock() sig data past end of block");
1307141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler
1317141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	/* Check that we signed header and data key */
1327141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	Memcpy(h, hdr, hsize);
1337141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	h->keyblock_signature.data_size = 4;
1347141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	h->data_key.key_offset = 0;
1357141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	h->data_key.key_size = 0;
1367141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	resign_keyblock(h, private_key);
137224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler	TEST_EQ(vb2_verify_keyblock(h, hsize, &key, &wb),
138224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler		VB2_ERROR_KEYBLOCK_SIGNED_TOO_LITTLE,
139224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler		"vb2_verify_keyblock() didn't sign header");
1407141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler
1417141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	Memcpy(h, hdr, hsize);
1427141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	h->data_key.key_offset = hsize;
1437141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	resign_keyblock(h, private_key);
144224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler	TEST_EQ(vb2_verify_keyblock(h, hsize, &key, &wb),
145224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler		VB2_ERROR_KEYBLOCK_DATA_KEY_OUTSIDE,
146224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler		"vb2_verify_keyblock() data key off end");
1477141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler
1487141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	/* Corner cases for error checking */
149224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler	TEST_EQ(vb2_verify_keyblock(NULL, 4, &key, &wb),
150224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler		VB2_ERROR_KEYBLOCK_TOO_SMALL_FOR_HEADER,
151224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler		"vb2_verify_keyblock size too small");
1527141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler
1537141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	/*
1547141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	 * TODO: verify parser can support a bigger header (i.e., one where
1557141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	 * data_key.key_offset is bigger than expected).
1567141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	 */
1577141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler
1587141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	free(h);
1597141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	free(hdr);
1607141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler}
1617141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler
1627141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spanglerstatic void resign_fw_preamble(struct vb2_fw_preamble *h,
1637141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler			       const VbPrivateKey *key)
1647141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler{
1657141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	VbSignature *sig = CalculateSignature(
1667141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler		(const uint8_t *)h, h->preamble_signature.data_size, key);
1677141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler
1687141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	SignatureCopy((VbSignature *)&h->preamble_signature, sig);
1697141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	free(sig);
1707141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler}
1717141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler
1727141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spanglerstatic void test_verify_fw_preamble(const VbPublicKey *public_key,
1737141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler				    const VbPrivateKey *private_key,
1747141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler				    const VbPublicKey *kernel_subkey)
1757141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler{
1767141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	struct vb2_fw_preamble *hdr;
1777141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	struct vb2_fw_preamble *h;
1787141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	struct vb2_public_key rsa;
17973e5eb38821d693244f841ce4f0a14546e5b6361Bill Richardson	uint8_t workbuf[VB2_VERIFY_FIRMWARE_PREAMBLE_WORKBUF_BYTES]
18073e5eb38821d693244f841ce4f0a14546e5b6361Bill Richardson		 __attribute__ ((aligned (VB2_WORKBUF_ALIGN)));
1817141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	struct vb2_workbuf wb;
1827141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	uint32_t hsize;
1837141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler
1847141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	vb2_workbuf_init(&wb, workbuf, sizeof(workbuf));
1857141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler
1867141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	/* Create a dummy signature */
1877141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	VbSignature *body_sig = SignatureAlloc(56, 78);
1887141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler
189224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler	TEST_SUCC(vb2_unpack_key(&rsa, (uint8_t *)public_key,
190224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler				 public_key->key_offset + public_key->key_size),
191224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler		  "vb2_verify_fw_preamble() prereq key");
1927141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler
1937141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	hdr = (struct vb2_fw_preamble *)
1947141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler		CreateFirmwarePreamble(0x1234, kernel_subkey, body_sig,
1957141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler				       private_key, 0x5678);
1967141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	TEST_PTR_NEQ(hdr, NULL,
1977141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler		     "VerifyFirmwarePreamble() prereq test preamble");
1987141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	if (!hdr)
1997141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler		return;
2007141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	hsize = (uint32_t) hdr->preamble_size;
2017141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	h = (struct vb2_fw_preamble *)malloc(hsize + 16384);
2027141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler
2037141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	Memcpy(h, hdr, hsize);
204224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler	TEST_SUCC(vb2_verify_fw_preamble(h, hsize, &rsa, &wb),
205224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler		  "vb2_verify_fw_preamble() ok using key");
2067141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler
2077141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	Memcpy(h, hdr, hsize);
208224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler	TEST_EQ(vb2_verify_fw_preamble(h, 4, &rsa, &wb),
209224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler		VB2_ERROR_PREAMBLE_TOO_SMALL_FOR_HEADER,
210224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler		"vb2_verify_fw_preamble() size tiny");
2117141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler
2127141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	Memcpy(h, hdr, hsize);
213224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler	TEST_EQ(vb2_verify_fw_preamble(h, hsize - 1, &rsa, &wb),
214224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler		VB2_ERROR_PREAMBLE_SIZE,
215224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler		"vb2_verify_fw_preamble() size--");
2167141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler
217224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler	/* Buffer is allowed to be bigger than preamble */
2187141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	Memcpy(h, hdr, hsize);
219224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler	TEST_SUCC(vb2_verify_fw_preamble(h, hsize + 1, &rsa, &wb),
220224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler		  "vb2_verify_fw_preamble() size++");
2217141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler
2227141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	/* Care about major version but not minor */
2237141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	Memcpy(h, hdr, hsize);
2247141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	h->header_version_major++;
2257141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	resign_fw_preamble(h, private_key);
226224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler	TEST_EQ(vb2_verify_fw_preamble(h, hsize, &rsa, &wb),
227224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler		VB2_ERROR_PREAMBLE_HEADER_VERSION
228224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler		, "vb2_verify_fw_preamble() major++");
2297141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler
2307141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	Memcpy(h, hdr, hsize);
2317141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	h->header_version_major--;
2327141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	resign_fw_preamble(h, private_key);
233224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler	TEST_EQ(vb2_verify_fw_preamble(h, hsize, &rsa, &wb),
234224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler		VB2_ERROR_PREAMBLE_HEADER_VERSION,
235224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler		"vb2_verify_fw_preamble() major--");
2367141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler
2377141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	Memcpy(h, hdr, hsize);
2387141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	h->header_version_minor++;
2397141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	resign_fw_preamble(h, private_key);
240224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler	TEST_SUCC(vb2_verify_fw_preamble(h, hsize, &rsa, &wb),
241224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler		  "vb2_verify_fw_preamble() minor++");
2427141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler
2437141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	Memcpy(h, hdr, hsize);
2447141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	h->header_version_minor--;
2457141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	resign_fw_preamble(h, private_key);
246224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler	TEST_EQ(vb2_verify_fw_preamble(h, hsize, &rsa, &wb),
247224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler		VB2_ERROR_PREAMBLE_HEADER_OLD,
248224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler		"vb2_verify_fw_preamble() 2.0 not supported");
2497141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler
2507141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	/* Check signature */
2517141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	Memcpy(h, hdr, hsize);
2527141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	h->preamble_signature.sig_offset = hsize;
2537141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	resign_fw_preamble(h, private_key);
254224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler	TEST_EQ(vb2_verify_fw_preamble(h, hsize, &rsa, &wb),
255224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler		VB2_ERROR_PREAMBLE_SIG_OUTSIDE,
256224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler		"vb2_verify_fw_preamble() sig off end");
2577141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler
2587141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	Memcpy(h, hdr, hsize);
2597141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	h->preamble_signature.sig_size--;
2607141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	resign_fw_preamble(h, private_key);
261224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler	TEST_EQ(vb2_verify_fw_preamble(h, hsize, &rsa, &wb),
262224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler		VB2_ERROR_PREAMBLE_SIG_INVALID,
263224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler		"vb2_verify_fw_preamble() sig too small");
2647141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler
2657141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	Memcpy(h, hdr, hsize);
2667141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	((uint8_t *)vb2_packed_key_data(&h->kernel_subkey))[0] ^= 0x34;
267224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler	TEST_EQ(vb2_verify_fw_preamble(h, hsize, &rsa, &wb),
268224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler		VB2_ERROR_PREAMBLE_SIG_INVALID,
269224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler		"vb2_verify_fw_preamble() sig mismatch");
2707141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler
2717141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	/* Check that we signed header, kernel subkey, and body sig */
2727141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	Memcpy(h, hdr, hsize);
2737141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	h->preamble_signature.data_size = 4;
2747141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	h->kernel_subkey.key_offset = 0;
2757141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	h->kernel_subkey.key_size = 0;
2767141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	h->body_signature.sig_offset = 0;
2777141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	h->body_signature.sig_size = 0;
2787141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	resign_fw_preamble(h, private_key);
279224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler	TEST_EQ(vb2_verify_fw_preamble(h, hsize, &rsa, &wb),
280224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler		VB2_ERROR_PREAMBLE_SIGNED_TOO_LITTLE,
281224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler		"vb2_verify_fw_preamble() didn't sign header");
2827141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler
2837141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	Memcpy(h, hdr, hsize);
2847141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	h->kernel_subkey.key_offset = hsize;
2857141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	resign_fw_preamble(h, private_key);
286224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler	TEST_EQ(vb2_verify_fw_preamble(h, hsize, &rsa, &wb),
287224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler		VB2_ERROR_PREAMBLE_KERNEL_SUBKEY_OUTSIDE,
288224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler		"vb2_verify_fw_preamble() kernel subkey off end");
2897141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler
2907141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	Memcpy(h, hdr, hsize);
2917141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	h->body_signature.sig_offset = hsize;
2927141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	resign_fw_preamble(h, private_key);
293224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler	TEST_EQ(vb2_verify_fw_preamble(h, hsize, &rsa, &wb),
294224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler		VB2_ERROR_PREAMBLE_BODY_SIG_OUTSIDE,
295224f5ac761852cd9ffe56438f6807732bd9ee445Randall Spangler		"vb2_verify_fw_preamble() body sig off end");
2967141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler
2977141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	/* TODO: verify with extra padding at end of header. */
2987141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler
2997141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	free(h);
3007141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	free(hdr);
3017141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler}
3027141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler
3037141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spanglerint test_permutation(int signing_key_algorithm, int data_key_algorithm,
3047141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler		     const char *keys_dir)
3057141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler{
3067141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	char filename[1024];
3077141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	int signing_rsa_len = siglen_map[signing_key_algorithm] * 8;
3087141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	int data_rsa_len = siglen_map[data_key_algorithm] * 8;
3097141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler
3107141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	VbPrivateKey *signing_private_key = NULL;
3117141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	VbPublicKey *signing_public_key = NULL;
3127141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	VbPublicKey *data_public_key = NULL;
3137141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler
3147141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	printf("***Testing signing algorithm: %s\n",
3157141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	       algo_strings[signing_key_algorithm]);
3167141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	printf("***With data key algorithm: %s\n",
3177141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	       algo_strings[data_key_algorithm]);
3187141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler
3197141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	sprintf(filename, "%s/key_rsa%d.pem", keys_dir, signing_rsa_len);
3207141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	signing_private_key = PrivateKeyReadPem(filename,
3217141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler						signing_key_algorithm);
3227141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	if (!signing_private_key) {
3237141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler		fprintf(stderr, "Error reading signing_private_key: %s\n",
3247141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler			filename);
3257141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler		return 1;
3267141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	}
3277141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler
3287141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	sprintf(filename, "%s/key_rsa%d.keyb", keys_dir, signing_rsa_len);
3297141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	signing_public_key = PublicKeyReadKeyb(filename,
3307141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler					       signing_key_algorithm, 1);
3317141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	if (!signing_public_key) {
3327141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler		fprintf(stderr, "Error reading signing_public_key: %s\n",
3337141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler			filename);
3347141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler		return 1;
3357141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	}
3367141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler
3377141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	sprintf(filename, "%s/key_rsa%d.keyb", keys_dir, data_rsa_len);
3387141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	data_public_key = PublicKeyReadKeyb(filename,
3397141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler					    data_key_algorithm, 1);
3407141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	if (!data_public_key) {
3417141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler		fprintf(stderr, "Error reading data_public_key: %s\n",
3427141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler			filename);
3437141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler		return 1;
3447141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	}
3457141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler
3467141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	test_verify_keyblock(signing_public_key, signing_private_key,
3477141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler			     data_public_key);
3487141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	test_verify_fw_preamble(signing_public_key, signing_private_key,
3497141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler				data_public_key);
3507141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler
3517141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	if (signing_public_key)
3527141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler		free(signing_public_key);
3537141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	if (signing_private_key)
3547141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler		free(signing_private_key);
3557141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	if (data_public_key)
3567141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler		free(data_public_key);
3577141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler
3587141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	return 0;
3597141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler}
3607141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler
3617141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spanglerstruct test_perm
3627141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler{
3637141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	int signing_algorithm;
3647141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	int data_key_algorithm;
3657141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler};
3667141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler
3677141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler/* Permutations of signing and data key algorithms in active use */
3687141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spanglerconst struct test_perm test_perms[] = {
3697141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	{VB2_ALG_RSA4096_SHA256, VB2_ALG_RSA2048_SHA256},
3707141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	{VB2_ALG_RSA8192_SHA512, VB2_ALG_RSA2048_SHA256},
3717141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	{VB2_ALG_RSA8192_SHA512, VB2_ALG_RSA4096_SHA256},
3727141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler};
3737141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler
3747141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spanglerint main(int argc, char *argv[])
3757141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler{
3767141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	if (argc == 2) {
3777141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler		/* Test only the algorithms we use */
3787141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler		int i;
3797141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler
3807141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler		for (i = 0; i < ARRAY_SIZE(test_perms); i++) {
3817141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler			if (test_permutation(test_perms[i].signing_algorithm,
3827141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler					     test_perms[i].data_key_algorithm,
3837141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler					     argv[1]))
3847141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler				return 1;
3857141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler		}
3867141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler
3877141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	} else if (argc == 3 && !strcasecmp(argv[2], "--all")) {
3887141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler		/* Test all the algorithms */
3897141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler		int sign_alg, data_alg;
3907141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler
3917141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler		for (sign_alg = 0; sign_alg < VB2_ALG_COUNT; sign_alg++) {
3927141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler			for (data_alg = 0; data_alg < VB2_ALG_COUNT;
3937141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler			     data_alg++) {
3947141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler				if (test_permutation(sign_alg, data_alg,
3957141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler						     argv[1]))
3967141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler					return 1;
3977141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler			}
3987141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler		}
3997141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	} else {
4007141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler		fprintf(stderr,	"Usage: %s <keys_dir> [--all]",	argv[0]);
4017141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler		return -1;
4027141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	}
4037141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler
4047141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler	return gTestSuccess ? 0 : 255;
4057141d73c149ee203a192f0cb45ca9bfa10c0ff8cRandall Spangler}
406