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 host library vboot2 keyblock functions
6  */
7 
8 #include <stdio.h>
9 #include <unistd.h>
10 
11 #include "2sysincludes.h"
12 #include "2common.h"
13 #include "2rsa.h"
14 #include "vb2_common.h"
15 #include "host_common.h"
16 #include "host_key2.h"
17 #include "host_keyblock2.h"
18 
19 #include "test_common.h"
20 
keyblock_tests(const char * keys_dir)21 static void keyblock_tests(const char *keys_dir)
22 {
23 	struct vb2_public_key *pubk2048, *pubk4096, *pubk8192, pubkhash;
24 	struct vb2_private_key *prik4096, *prik8192;
25 	struct vb2_packed_key *pak, *pakgood;
26 	struct vb2_keyblock *kb;
27 	const struct vb2_private_key *prikhash;
28 	const struct vb2_private_key *prik[2];
29 	char fname[1024];
30 	const char test_desc[] = "Test keyblock";
31 
32 	uint8_t workbuf[VB2_KEY_BLOCK_VERIFY_WORKBUF_BYTES]
33 		 __attribute__ ((aligned (VB2_WORKBUF_ALIGN)));
34 	struct vb2_workbuf wb;
35 
36 	vb2_workbuf_init(&wb, workbuf, sizeof(workbuf));
37 
38 	/* Read keys */
39 	sprintf(fname, "%s/key_rsa2048.keyb", keys_dir);
40 	TEST_SUCC(vb2_public_key_read_keyb(&pubk2048, fname),
41 					   "Read public key 2");
42 	vb2_public_key_set_desc(pubk2048, "Test RSA2048 public key");
43 	pubk2048->hash_alg = VB2_HASH_SHA256;
44 
45 	sprintf(fname, "%s/key_rsa4096.keyb", keys_dir);
46 	TEST_SUCC(vb2_public_key_read_keyb(&pubk4096, fname),
47 					   "Read public key 1");
48 	vb2_public_key_set_desc(pubk4096, "Test RSA4096 public key");
49 	pubk4096->hash_alg = VB2_HASH_SHA256;
50 
51 	sprintf(fname, "%s/key_rsa8192.keyb", keys_dir);
52 	TEST_SUCC(vb2_public_key_read_keyb(&pubk8192, fname),
53 					   "Read public key 2");
54 	vb2_public_key_set_desc(pubk8192, "Test RSA8192 public key");
55 	pubk8192->hash_alg = VB2_HASH_SHA512;
56 
57 	sprintf(fname, "%s/key_rsa4096.pem", keys_dir);
58 	TEST_SUCC(vb2_private_key_read_pem(&prik4096, fname),
59 		  "Read private key 2");
60 	vb2_private_key_set_desc(prik4096, "Test RSA4096 private key");
61 	prik4096->sig_alg = VB2_SIG_RSA4096;
62 	prik4096->hash_alg = VB2_HASH_SHA256;
63 
64 	sprintf(fname, "%s/key_rsa8192.pem", keys_dir);
65 	TEST_SUCC(vb2_private_key_read_pem(&prik8192, fname),
66 		  "Read private key 1");
67 	vb2_private_key_set_desc(prik8192, "Test RSA8192 private key");
68 	prik8192->sig_alg = VB2_SIG_RSA8192;
69 	prik8192->hash_alg = VB2_HASH_SHA512;
70 
71 	TEST_SUCC(vb2_private_key_hash(&prikhash, VB2_HASH_SHA512),
72 		  "Create private hash key");
73 
74 	TEST_SUCC(vb2_public_key_hash(&pubkhash, VB2_HASH_SHA512),
75 		  "Create public hash key");
76 
77 	TEST_SUCC(vb2_public_key_pack(&pakgood, pubk2048), "Test packed key");
78 
79 	/* Sign a keyblock with one key */
80 	prik[0] = prik4096;
81 	TEST_SUCC(vb2_keyblock_create(&kb, pubk2048, prik, 1, 0x1234, NULL),
82 		  "Keyblock single");
83 	TEST_PTR_NEQ(kb, NULL, "  kb_ptr");
84 	TEST_SUCC(vb2_verify_keyblock(kb, kb->c.total_size, pubk4096, &wb),
85 		  "  verify");
86 	TEST_EQ(strcmp(vb2_common_desc(kb), pubk2048->desc), 0,	"  desc");
87 	TEST_EQ(kb->flags, 0x1234, "  flags");
88 
89 	pak = (struct vb2_packed_key *)((uint8_t *)kb + kb->key_offset);
90 	TEST_EQ(0, memcmp(pak, pakgood, pakgood->c.total_size), "  data key");
91 	free(kb);
92 
93 	/* Sign a keyblock with two keys */
94 	prik[0] = prik8192;
95 	prik[1] = prikhash;
96 	TEST_SUCC(vb2_keyblock_create(&kb, pubk4096, prik, 2, 0, test_desc),
97 		  "Keyblock multiple");
98 	TEST_SUCC(vb2_verify_keyblock(kb, kb->c.total_size, pubk8192, &wb),
99 		  "  verify 1");
100 	TEST_SUCC(vb2_verify_keyblock(kb, kb->c.total_size, &pubkhash, &wb),
101 		  "  verify 2");
102 	TEST_EQ(strcmp(vb2_common_desc(kb), test_desc), 0,	"  desc");
103 	TEST_EQ(kb->flags, 0, "  flags");
104 	free(kb);
105 
106 	/* Test errors */
107 	prik[0] = prik8192;
108 	prik8192->hash_alg = VB2_HASH_INVALID;
109 	TEST_EQ(vb2_keyblock_create(&kb, pubk4096, prik, 1, 0, NULL),
110 		VB2_KEYBLOCK_CREATE_SIG_SIZE, "Keyblock bad sig size");
111 	TEST_PTR_EQ(kb, NULL, "  kb_ptr");
112 
113 	prik[0] = prik4096;
114 	pubk4096->sig_alg = VB2_SIG_INVALID;
115 	TEST_EQ(vb2_keyblock_create(&kb, pubk4096, prik, 1, 0, NULL),
116 		VB2_KEYBLOCK_CREATE_DATA_KEY, "Keyblock bad data key");
117 
118 	/* Free keys */
119 	free(pakgood);
120 	vb2_public_key_free(pubk2048);
121 	vb2_public_key_free(pubk4096);
122 	vb2_public_key_free(pubk8192);
123 	vb2_private_key_free(prik4096);
124 	vb2_private_key_free(prik8192);
125 }
126 
main(int argc,char * argv[])127 int main(int argc, char *argv[]) {
128 
129 	if (argc == 2) {
130 		keyblock_tests(argv[1]);
131 	} else {
132 		fprintf(stderr, "Usage: %s <keys_dir>", argv[0]);
133 		return -1;
134 	}
135 
136 	return gTestSuccess ? 0 : 255;
137 }
138