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 preamble 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
15 #include "vb2_common.h"
16
17 #include "host_common.h"
18 #include "host_fw_preamble2.h"
19 #include "host_key2.h"
20 #include "host_signature2.h"
21
22 #include "test_common.h"
23
24 const uint8_t test_data1[] = "Some test data";
25 const uint8_t test_data2[] = "Some more test data";
26 const uint8_t test_data3[] = "Even more test data";
27
preamble_tests(const char * keys_dir)28 static void preamble_tests(const char *keys_dir)
29 {
30 struct vb2_private_key *prik4096;
31 struct vb2_public_key *pubk4096;
32 struct vb2_fw_preamble *fp;
33 const struct vb2_private_key *prikhash;
34 struct vb2_signature *hashes[3];
35 char fname[1024];
36 const char test_desc[] = "Test fw preamble";
37 const uint32_t test_version = 2061;
38 const uint32_t test_flags = 0x11223344;
39 uint32_t hash_next;
40 int i;
41
42 uint8_t workbuf[VB2_VERIFY_FIRMWARE_PREAMBLE_WORKBUF_BYTES]
43 __attribute__ ((aligned (VB2_WORKBUF_ALIGN)));
44 struct vb2_workbuf wb;
45
46 vb2_workbuf_init(&wb, workbuf, sizeof(workbuf));
47
48 /* Read keys */
49 sprintf(fname, "%s/key_rsa4096.keyb", keys_dir);
50 TEST_SUCC(vb2_public_key_read_keyb(&pubk4096, fname),
51 "Read public key 1");
52 vb2_public_key_set_desc(pubk4096, "Test RSA4096 public key");
53 pubk4096->hash_alg = VB2_HASH_SHA256;
54
55 sprintf(fname, "%s/key_rsa4096.pem", keys_dir);
56 TEST_SUCC(vb2_private_key_read_pem(&prik4096, fname),
57 "Read private key 2");
58 vb2_private_key_set_desc(prik4096, "Test RSA4096 private key");
59 prik4096->sig_alg = VB2_SIG_RSA4096;
60 prik4096->hash_alg = VB2_HASH_SHA256;
61
62 TEST_SUCC(vb2_private_key_hash(&prikhash, VB2_HASH_SHA256),
63 "Create private hash key");
64
65 /* Create some signatures */
66 TEST_SUCC(vb2_sign_data(hashes + 0, test_data1, sizeof(test_data1),
67 prikhash, "Hash 1"),
68 "Hash 1");
69 TEST_SUCC(vb2_sign_data(hashes + 1, test_data2, sizeof(test_data2),
70 prikhash, "Hash 2"),
71 "Hash 2");
72 TEST_SUCC(vb2_sign_data(hashes + 2, test_data3, sizeof(test_data3),
73 prikhash, "Hash 3"),
74 "Hash 3");
75
76 /* Test good preamble */
77 TEST_SUCC(vb2_fw_preamble_create(&fp, prik4096,
78 (const struct vb2_signature **)hashes,
79 3, test_version, test_flags,
80 test_desc),
81 "Create preamble good");
82 TEST_PTR_NEQ(fp, NULL, " fp_ptr");
83 TEST_SUCC(vb2_verify_fw_preamble(fp, fp->c.total_size, pubk4096, &wb),
84 "Verify preamble good");
85 TEST_EQ(strcmp(vb2_common_desc(fp), test_desc), 0, " desc");
86 TEST_EQ(fp->fw_version, test_version, " fw_version");
87 TEST_EQ(fp->flags, test_flags, " flags");
88 TEST_EQ(fp->hash_count, 3, " hash_count");
89
90 hash_next = fp->hash_offset;
91 for (i = 0; i < 3; i++) {
92 TEST_EQ(0, memcmp((uint8_t *)fp + hash_next, hashes[i],
93 hashes[i]->c.total_size), " hash[i]");
94 hash_next += hashes[i]->c.total_size;
95 }
96
97 free(fp);
98
99 /* Test errors */
100 prik4096->hash_alg = VB2_HASH_INVALID;
101 TEST_EQ(vb2_fw_preamble_create(&fp, prik4096,
102 (const struct vb2_signature **)hashes,
103 3, test_version, test_flags,
104 test_desc),
105 VB2_FW_PREAMBLE_CREATE_SIG_SIZE,
106 "Create preamble bad sig");
107 TEST_PTR_EQ(fp, NULL, " fp_ptr");
108
109 /* Free keys */
110 vb2_public_key_free(pubk4096);
111 vb2_private_key_free(prik4096);
112 }
113
main(int argc,char * argv[])114 int main(int argc, char *argv[]) {
115
116 if (argc == 2) {
117 preamble_tests(argv[1]);
118 } else {
119 fprintf(stderr, "Usage: %s <keys_dir>", argv[0]);
120 return -1;
121 }
122
123 return gTestSuccess ? 0 : 255;
124 }
125