Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 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 2common.c |
| 6 | */ |
| 7 | |
| 8 | #include "2sysincludes.h" |
| 9 | #include "2common.h" |
| 10 | #include "2rsa.h" |
| 11 | #include "vb2_common.h" |
| 12 | #include "host_fw_preamble2.h" |
| 13 | #include "host_key2.h" |
| 14 | #include "host_keyblock2.h" |
| 15 | #include "host_signature2.h" |
| 16 | |
| 17 | #include "test_common.h" |
| 18 | |
| 19 | static const uint8_t test_data[] = "This is some test data to sign."; |
| 20 | static const uint8_t test_data2[] = "Some more test data"; |
| 21 | static const uint8_t test_data3[] = "Even more test data"; |
| 22 | |
| 23 | /* |
| 24 | * Test struct packing for vboot_struct.h structs which are passed between |
| 25 | * firmware and OS, or passed between different phases of firmware. |
| 26 | */ |
| 27 | static void test_struct_packing(void) |
| 28 | { |
| 29 | /* Test new struct sizes */ |
| 30 | TEST_EQ(EXPECTED_GUID_SIZE, |
| 31 | sizeof(struct vb2_guid), |
| 32 | "sizeof(vb2_guid)"); |
| 33 | TEST_EQ(EXPECTED_VB2_STRUCT_COMMON_SIZE, |
| 34 | sizeof(struct vb2_struct_common), |
| 35 | "sizeof(vb2_struct_common)"); |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame] | 36 | TEST_EQ(EXPECTED_VB2_PACKED_KEY_SIZE, |
| 37 | sizeof(struct vb2_packed_key), |
| 38 | "sizeof(vb2_packed_key)"); |
| 39 | TEST_EQ(EXPECTED_VB2_SIGNATURE_SIZE, |
| 40 | sizeof(struct vb2_signature), |
| 41 | "sizeof(vb2_signature)"); |
| 42 | TEST_EQ(EXPECTED_VB2_KEYBLOCK_SIZE, |
| 43 | sizeof(struct vb2_keyblock), |
| 44 | "sizeof(vb2_keyblock)"); |
| 45 | TEST_EQ(EXPECTED_VB2_FW_PREAMBLE_SIZE, |
| 46 | sizeof(struct vb2_fw_preamble), |
| 47 | "sizeof(vb2_fw_preamble)"); |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Common header functions |
| 52 | */ |
| 53 | static void test_common_header_functions(void) |
| 54 | { |
| 55 | uint8_t cbuf[sizeof(struct vb2_struct_common) + 128]; |
| 56 | uint8_t cbufgood[sizeof(cbuf)]; |
| 57 | struct vb2_struct_common *c = (struct vb2_struct_common *)cbuf; |
| 58 | struct vb2_struct_common *c2; |
| 59 | const char test_desc[32] = "test desc"; |
| 60 | uint32_t desc_end, m; |
| 61 | |
| 62 | c->total_size = sizeof(cbuf); |
| 63 | c->fixed_size = sizeof(*c); |
| 64 | c->desc_size = sizeof(test_desc); |
| 65 | memcpy(cbuf + c->fixed_size, test_desc, sizeof(test_desc)); |
| 66 | desc_end = c->fixed_size + c->desc_size; |
| 67 | |
| 68 | c2 = (struct vb2_struct_common *)(cbuf + desc_end); |
| 69 | c2->total_size = c->total_size - desc_end; |
| 70 | c2->fixed_size = sizeof(*c2); |
| 71 | c2->desc_size = 0; |
| 72 | |
| 73 | /* Description helper */ |
| 74 | TEST_EQ(0, strcmp(vb2_common_desc(c), test_desc), "vb2_common_desc()"); |
| 75 | TEST_EQ(0, strcmp(vb2_common_desc(c2), ""), "vb2_common_desc() empty"); |
| 76 | |
| 77 | TEST_SUCC(vb2_verify_common_header(cbuf, sizeof(cbuf)), |
| 78 | "vb2_verify_common_header() good"); |
| 79 | memcpy(cbufgood, cbuf, sizeof(cbufgood)); |
| 80 | |
| 81 | memcpy(cbuf, cbufgood, sizeof(cbuf)); |
| 82 | c->total_size += 4; |
| 83 | TEST_EQ(vb2_verify_common_header(cbuf, sizeof(cbuf)), |
| 84 | VB2_ERROR_COMMON_TOTAL_SIZE, |
| 85 | "vb2_verify_common_header() total size"); |
| 86 | |
| 87 | memcpy(cbuf, cbufgood, sizeof(cbuf)); |
| 88 | c->fixed_size = c->total_size + 4; |
| 89 | TEST_EQ(vb2_verify_common_header(cbuf, sizeof(cbuf)), |
| 90 | VB2_ERROR_COMMON_FIXED_SIZE, |
| 91 | "vb2_verify_common_header() fixed size"); |
| 92 | |
| 93 | memcpy(cbuf, cbufgood, sizeof(cbuf)); |
| 94 | c->desc_size = c->total_size - c->fixed_size + 4; |
| 95 | TEST_EQ(vb2_verify_common_header(cbuf, sizeof(cbuf)), |
| 96 | VB2_ERROR_COMMON_DESC_SIZE, |
| 97 | "vb2_verify_common_header() desc size"); |
| 98 | |
| 99 | memcpy(cbuf, cbufgood, sizeof(cbuf)); |
| 100 | c->total_size--; |
| 101 | TEST_EQ(vb2_verify_common_header(cbuf, sizeof(cbuf)), |
| 102 | VB2_ERROR_COMMON_TOTAL_UNALIGNED, |
| 103 | "vb2_verify_common_header() total unaligned"); |
| 104 | |
| 105 | memcpy(cbuf, cbufgood, sizeof(cbuf)); |
| 106 | c->fixed_size++; |
| 107 | TEST_EQ(vb2_verify_common_header(cbuf, sizeof(cbuf)), |
| 108 | VB2_ERROR_COMMON_FIXED_UNALIGNED, |
| 109 | "vb2_verify_common_header() fixed unaligned"); |
| 110 | |
| 111 | memcpy(cbuf, cbufgood, sizeof(cbuf)); |
| 112 | c->desc_size--; |
| 113 | TEST_EQ(vb2_verify_common_header(cbuf, sizeof(cbuf)), |
| 114 | VB2_ERROR_COMMON_DESC_UNALIGNED, |
| 115 | "vb2_verify_common_header() desc unaligned"); |
| 116 | |
| 117 | memcpy(cbuf, cbufgood, sizeof(cbuf)); |
| 118 | c->desc_size = -4; |
| 119 | TEST_EQ(vb2_verify_common_header(cbuf, sizeof(cbuf)), |
| 120 | VB2_ERROR_COMMON_DESC_WRAPS, |
| 121 | "vb2_verify_common_header() desc wraps"); |
| 122 | |
| 123 | memcpy(cbuf, cbufgood, sizeof(cbuf)); |
| 124 | cbuf[desc_end - 1] = 1; |
| 125 | TEST_EQ(vb2_verify_common_header(cbuf, sizeof(cbuf)), |
| 126 | VB2_ERROR_COMMON_DESC_TERMINATOR, |
| 127 | "vb2_verify_common_header() desc not terminated"); |
| 128 | |
| 129 | /* Member checking function */ |
| 130 | memcpy(cbuf, cbufgood, sizeof(cbuf)); |
| 131 | m = 0; |
| 132 | TEST_SUCC(vb2_verify_common_member(cbuf, &m, c->total_size - 8, 4), |
| 133 | "vb2_verify_common_member()"); |
| 134 | TEST_EQ(m, c->total_size - 4, " new minimum"); |
| 135 | |
| 136 | m = desc_end; |
| 137 | TEST_SUCC(vb2_verify_common_member(cbuf, &m, desc_end, 4), |
| 138 | "vb2_verify_common_member() good offset"); |
| 139 | TEST_EQ(m, desc_end + 4, " new minimum"); |
| 140 | |
| 141 | m = 0; |
| 142 | TEST_EQ(vb2_verify_common_member(cbuf, &m, c->total_size - 8, -4), |
| 143 | VB2_ERROR_COMMON_MEMBER_WRAPS, |
| 144 | "vb2_verify_common_member() wraps"); |
| 145 | |
| 146 | m = 0; |
| 147 | TEST_EQ(vb2_verify_common_member(cbuf, &m, c->total_size - 7, 4), |
| 148 | VB2_ERROR_COMMON_MEMBER_UNALIGNED, |
| 149 | "vb2_verify_common_member() offset unaligned"); |
| 150 | |
| 151 | m = 0; |
| 152 | TEST_EQ(vb2_verify_common_member(cbuf, &m, c->total_size - 8, 5), |
| 153 | VB2_ERROR_COMMON_MEMBER_UNALIGNED, |
| 154 | "vb2_verify_common_member() size unaligned"); |
| 155 | |
| 156 | m = 0; |
| 157 | TEST_EQ(vb2_verify_common_member(cbuf, &m, desc_end - 4, 4), |
| 158 | VB2_ERROR_COMMON_MEMBER_OVERLAP, |
| 159 | "vb2_verify_common_member() overlap"); |
| 160 | |
| 161 | m = desc_end + 4; |
| 162 | TEST_EQ(vb2_verify_common_member(cbuf, &m, desc_end, 4), |
| 163 | VB2_ERROR_COMMON_MEMBER_OVERLAP, |
| 164 | "vb2_verify_common_member() overlap 2"); |
| 165 | |
| 166 | m = 0; |
| 167 | TEST_EQ(vb2_verify_common_member(cbuf, &m, c->total_size - 4, 8), |
| 168 | VB2_ERROR_COMMON_MEMBER_SIZE, |
| 169 | "vb2_verify_common_member() size"); |
| 170 | |
| 171 | /* Subobject checking */ |
| 172 | m = 0; |
| 173 | TEST_SUCC(vb2_verify_common_subobject(cbuf, &m, desc_end), |
| 174 | "vb2_verify_common_subobject() good offset"); |
| 175 | TEST_EQ(m, sizeof(cbuf), " new minimum"); |
| 176 | |
| 177 | m = desc_end + 4; |
| 178 | TEST_EQ(vb2_verify_common_subobject(cbuf, &m, desc_end), |
| 179 | VB2_ERROR_COMMON_MEMBER_OVERLAP, |
| 180 | "vb2_verify_common_subobject() overlap"); |
| 181 | |
| 182 | m = 0; |
| 183 | c2->total_size += 4; |
| 184 | TEST_EQ(vb2_verify_common_subobject(cbuf, &m, desc_end), |
| 185 | VB2_ERROR_COMMON_TOTAL_SIZE, |
| 186 | "vb2_verify_common_subobject() size"); |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Signature size |
| 191 | */ |
| 192 | static void test_sig_size(void) |
| 193 | { |
| 194 | TEST_EQ(vb2_sig_size(VB2_SIG_INVALID, VB2_HASH_SHA256), 0, |
| 195 | "vb2_sig_size() sig invalid"); |
| 196 | |
| 197 | TEST_EQ(vb2_sig_size(VB2_SIG_RSA2048, VB2_HASH_INVALID), 0, |
| 198 | "vb2_sig_size() hash invalid"); |
| 199 | |
| 200 | TEST_EQ(vb2_sig_size(VB2_SIG_RSA2048, VB2_HASH_SHA256), 2048 / 8, |
| 201 | "vb2_sig_size() RSA2048"); |
| 202 | TEST_EQ(vb2_sig_size(VB2_SIG_RSA4096, VB2_HASH_SHA256), 4096 / 8, |
| 203 | "vb2_sig_size() RSA4096"); |
| 204 | TEST_EQ(vb2_sig_size(VB2_SIG_RSA8192, VB2_HASH_SHA512), 8192 / 8, |
| 205 | "vb2_sig_size() RSA8192"); |
| 206 | |
| 207 | TEST_EQ(vb2_sig_size(VB2_SIG_NONE, VB2_HASH_SHA1), |
| 208 | VB2_SHA1_DIGEST_SIZE, "vb2_sig_size() SHA1"); |
| 209 | TEST_EQ(vb2_sig_size(VB2_SIG_NONE, VB2_HASH_SHA256), |
| 210 | VB2_SHA256_DIGEST_SIZE, "vb2_sig_size() SHA256"); |
| 211 | TEST_EQ(vb2_sig_size(VB2_SIG_NONE, VB2_HASH_SHA512), |
| 212 | VB2_SHA512_DIGEST_SIZE, "vb2_sig_size() SHA512"); |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * Verify data on bare hash |
| 217 | */ |
| 218 | static void test_verify_hash(void) |
| 219 | { |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame] | 220 | struct vb2_signature *sig; |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 221 | const struct vb2_private_key *prik; |
| 222 | struct vb2_public_key pubk; |
Bill Richardson | 73e5eb3 | 2015-01-26 12:18:25 -0800 | [diff] [blame] | 223 | uint8_t workbuf[VB2_VERIFY_DATA_WORKBUF_BYTES] |
| 224 | __attribute__ ((aligned (VB2_WORKBUF_ALIGN))); |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 225 | struct vb2_workbuf wb; |
| 226 | |
| 227 | vb2_workbuf_init(&wb, workbuf, sizeof(workbuf)); |
| 228 | |
| 229 | TEST_SUCC(vb2_private_key_hash(&prik, VB2_HASH_SHA256), |
| 230 | "create private hash key"); |
| 231 | TEST_SUCC(vb2_public_key_hash(&pubk, VB2_HASH_SHA256), |
| 232 | "create hash key"); |
| 233 | |
| 234 | /* Create the signature */ |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame] | 235 | TEST_SUCC(vb2_sign_data(&sig, test_data, sizeof(test_data), prik, NULL), |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 236 | "create hash sig"); |
| 237 | |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame] | 238 | TEST_SUCC(vb2_verify_data(test_data, sizeof(test_data), |
| 239 | sig, &pubk, &wb), |
| 240 | "vb2_verify_data() hash ok"); |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 241 | |
| 242 | *((uint8_t *)sig + sig->sig_offset) ^= 0xab; |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame] | 243 | TEST_EQ(vb2_verify_data(test_data, sizeof(test_data), sig, &pubk, &wb), |
| 244 | VB2_ERROR_VDATA_VERIFY_DIGEST, "vb2_verify_data() hash bad"); |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 245 | |
| 246 | free(sig); |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Verify keyblock |
| 251 | */ |
| 252 | static void test_verify_keyblock(void) |
| 253 | { |
| 254 | const char desc[16] = "test keyblock"; |
| 255 | const struct vb2_private_key *prik[2]; |
| 256 | struct vb2_public_key pubk, pubk2, pubk3; |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame] | 257 | struct vb2_signature *sig; |
| 258 | struct vb2_keyblock *kbuf; |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 259 | uint32_t buf_size; |
| 260 | uint8_t *buf, *buf2; |
| 261 | |
Bill Richardson | 73e5eb3 | 2015-01-26 12:18:25 -0800 | [diff] [blame] | 262 | uint8_t workbuf[VB2_KEY_BLOCK_VERIFY_WORKBUF_BYTES] |
| 263 | __attribute__ ((aligned (VB2_WORKBUF_ALIGN))); |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 264 | struct vb2_workbuf wb; |
| 265 | |
| 266 | TEST_SUCC(vb2_public_key_hash(&pubk, VB2_HASH_SHA256), |
| 267 | "create hash key 1"); |
| 268 | TEST_SUCC(vb2_public_key_hash(&pubk2, VB2_HASH_SHA512), |
| 269 | "create hash key 2"); |
| 270 | TEST_SUCC(vb2_public_key_hash(&pubk3, VB2_HASH_SHA1), |
| 271 | "create hash key 3"); |
| 272 | |
| 273 | TEST_SUCC(vb2_private_key_hash(prik + 0, VB2_HASH_SHA256), |
| 274 | "create private key 1"); |
| 275 | TEST_SUCC(vb2_private_key_hash(prik + 1, VB2_HASH_SHA512), |
| 276 | "create private key 2"); |
| 277 | |
| 278 | /* Create the test keyblock */ |
| 279 | TEST_SUCC(vb2_keyblock_create(&kbuf, &pubk3, prik, 2, 0x4321, desc), |
| 280 | "create keyblock"); |
| 281 | |
| 282 | buf = (uint8_t *)kbuf; |
| 283 | buf_size = kbuf->c.total_size; |
| 284 | |
| 285 | /* Make a copy of the buffer, so we can mangle it for tests */ |
| 286 | buf2 = malloc(buf_size); |
| 287 | memcpy(buf2, buf, buf_size); |
| 288 | |
| 289 | vb2_workbuf_init(&wb, workbuf, sizeof(workbuf)); |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame] | 290 | kbuf = (struct vb2_keyblock *)buf; |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 291 | |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame] | 292 | TEST_SUCC(vb2_verify_keyblock(kbuf, buf_size, &pubk, &wb), |
| 293 | "vb2_verify_keyblock()"); |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 294 | |
| 295 | memcpy(buf, buf2, buf_size); |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame] | 296 | TEST_SUCC(vb2_verify_keyblock(kbuf, buf_size, &pubk2, &wb), |
| 297 | "vb2_verify_keyblock() key 2"); |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 298 | |
| 299 | memcpy(buf, buf2, buf_size); |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame] | 300 | TEST_EQ(vb2_verify_keyblock(kbuf, buf_size, &pubk3, &wb), |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 301 | VB2_ERROR_KEYBLOCK_SIG_GUID, |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame] | 302 | "vb2_verify_keyblock() key not present"); |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 303 | |
| 304 | memcpy(buf, buf2, buf_size); |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame] | 305 | kbuf->c.magic = VB2_MAGIC_PACKED_KEY; |
| 306 | TEST_EQ(vb2_verify_keyblock(kbuf, buf_size, &pubk, &wb), |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 307 | VB2_ERROR_KEYBLOCK_MAGIC, |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame] | 308 | "vb2_verify_keyblock() magic"); |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 309 | |
| 310 | memcpy(buf, buf2, buf_size); |
| 311 | kbuf->c.fixed_size++; |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame] | 312 | TEST_EQ(vb2_verify_keyblock(kbuf, buf_size, &pubk, &wb), |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 313 | VB2_ERROR_COMMON_FIXED_UNALIGNED, |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame] | 314 | "vb2_verify_keyblock() header"); |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 315 | |
| 316 | memcpy(buf, buf2, buf_size); |
| 317 | kbuf->c.struct_version_major++; |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame] | 318 | TEST_EQ(vb2_verify_keyblock(kbuf, buf_size, &pubk, &wb), |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 319 | VB2_ERROR_KEYBLOCK_HEADER_VERSION, |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame] | 320 | "vb2_verify_keyblock() major version"); |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 321 | |
| 322 | memcpy(buf, buf2, buf_size); |
| 323 | kbuf->c.struct_version_minor++; |
| 324 | /* That changes the signature, so resign the keyblock */ |
| 325 | vb2_sign_data(&sig, buf, kbuf->sig_offset, prik[0], NULL); |
| 326 | memcpy(buf + kbuf->sig_offset, sig, sig->c.total_size); |
| 327 | free(sig); |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame] | 328 | TEST_SUCC(vb2_verify_keyblock(kbuf, buf_size, &pubk, &wb), |
| 329 | "vb2_verify_keyblock() minor version"); |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 330 | |
| 331 | memcpy(buf, buf2, buf_size); |
| 332 | kbuf->c.fixed_size -= 4; |
| 333 | kbuf->c.desc_size += 4; |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame] | 334 | TEST_EQ(vb2_verify_keyblock(kbuf, buf_size, &pubk, &wb), |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 335 | VB2_ERROR_KEYBLOCK_SIZE, |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame] | 336 | "vb2_verify_keyblock() header size"); |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 337 | |
| 338 | memcpy(buf, buf2, buf_size); |
| 339 | kbuf->key_offset = kbuf->c.total_size - 4; |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame] | 340 | TEST_EQ(vb2_verify_keyblock(kbuf, buf_size, &pubk, &wb), |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 341 | VB2_ERROR_COMMON_MEMBER_SIZE, |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame] | 342 | "vb2_verify_keyblock() data key outside"); |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 343 | |
| 344 | memcpy(buf, buf2, buf_size); |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame] | 345 | sig = (struct vb2_signature *)(buf + kbuf->sig_offset); |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 346 | sig->data_size--; |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame] | 347 | TEST_EQ(vb2_verify_keyblock(kbuf, buf_size, &pubk, &wb), |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 348 | VB2_ERROR_KEYBLOCK_SIGNED_SIZE, |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame] | 349 | "vb2_verify_keyblock() signed wrong size"); |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 350 | |
| 351 | memcpy(buf, buf2, buf_size); |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame] | 352 | sig = (struct vb2_signature *)(buf + kbuf->sig_offset); |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 353 | sig->c.total_size = kbuf->c.total_size - 4; |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame] | 354 | TEST_EQ(vb2_verify_keyblock(kbuf, buf_size, &pubk, &wb), |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 355 | VB2_ERROR_COMMON_TOTAL_SIZE, |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame] | 356 | "vb2_verify_keyblock() key outside keyblock"); |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 357 | |
| 358 | memcpy(buf, buf2, buf_size); |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame] | 359 | sig = (struct vb2_signature *)(buf + kbuf->sig_offset); |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 360 | sig->c.struct_version_major++; |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame] | 361 | TEST_EQ(vb2_verify_keyblock(kbuf, buf_size, &pubk, &wb), |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 362 | VB2_ERROR_SIG_VERSION, |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame] | 363 | "vb2_verify_keyblock() corrupt key"); |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 364 | |
| 365 | memcpy(buf, buf2, buf_size); |
| 366 | kbuf->c.struct_version_minor++; |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame] | 367 | TEST_EQ(vb2_verify_keyblock(kbuf, buf_size, &pubk, &wb), |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 368 | VB2_ERROR_VDATA_VERIFY_DIGEST, |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame] | 369 | "vb2_verify_keyblock() corrupt"); |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 370 | |
| 371 | free(buf); |
| 372 | free(buf2); |
| 373 | } |
| 374 | |
| 375 | /** |
| 376 | * Verify firmware preamble |
| 377 | */ |
| 378 | static void test_verify_fw_preamble(void) |
| 379 | { |
| 380 | const char desc[16] = "test preamble"; |
| 381 | const struct vb2_private_key *prikhash; |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame] | 382 | struct vb2_signature *hashes[3]; |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 383 | struct vb2_public_key pubk; |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame] | 384 | struct vb2_signature *sig; |
| 385 | struct vb2_fw_preamble *pre; |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 386 | uint32_t buf_size; |
| 387 | uint8_t *buf, *buf2; |
| 388 | |
Bill Richardson | 73e5eb3 | 2015-01-26 12:18:25 -0800 | [diff] [blame] | 389 | uint8_t workbuf[VB2_VERIFY_FIRMWARE_PREAMBLE_WORKBUF_BYTES] |
| 390 | __attribute__ ((aligned (VB2_WORKBUF_ALIGN))); |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 391 | struct vb2_workbuf wb; |
| 392 | |
| 393 | /* |
| 394 | * Preambles will usually be signed with a real key not a bare hash, |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame] | 395 | * but the call to vb2_verify_data() inside the preamble check is the |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 396 | * same (and its functionality is verified separately), and using a |
| 397 | * bare hash here saves us from needing to have a private key to do |
| 398 | * this test. |
| 399 | */ |
| 400 | TEST_SUCC(vb2_public_key_hash(&pubk, VB2_HASH_SHA256), |
| 401 | "create hash key"); |
| 402 | TEST_SUCC(vb2_private_key_hash(&prikhash, VB2_HASH_SHA256), |
| 403 | "Create private hash key"); |
| 404 | |
| 405 | /* Create some signatures */ |
| 406 | TEST_SUCC(vb2_sign_data(hashes + 0, test_data, sizeof(test_data), |
| 407 | prikhash, "Hash 1"), |
| 408 | "Hash 1"); |
| 409 | TEST_SUCC(vb2_sign_data(hashes + 1, test_data2, sizeof(test_data2), |
| 410 | prikhash, "Hash 2"), |
| 411 | "Hash 2"); |
| 412 | TEST_SUCC(vb2_sign_data(hashes + 2, test_data3, sizeof(test_data3), |
| 413 | prikhash, "Hash 3"), |
| 414 | "Hash 3"); |
| 415 | |
| 416 | /* Test good preamble */ |
| 417 | TEST_SUCC(vb2_fw_preamble_create(&pre, prikhash, |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame] | 418 | (const struct vb2_signature **)hashes, |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 419 | 3, 0x1234, 0x5678, desc), |
| 420 | "Create preamble good"); |
| 421 | |
| 422 | buf = (uint8_t *)pre; |
| 423 | buf_size = pre->c.total_size; |
| 424 | |
| 425 | /* Make a copy of the buffer, so we can mangle it for tests */ |
| 426 | buf2 = malloc(buf_size); |
| 427 | memcpy(buf2, buf, buf_size); |
| 428 | |
| 429 | vb2_workbuf_init(&wb, workbuf, sizeof(workbuf)); |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame] | 430 | pre = (struct vb2_fw_preamble *)buf; |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 431 | |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame] | 432 | TEST_SUCC(vb2_verify_fw_preamble(pre, buf_size, &pubk, &wb), |
| 433 | "vb2_verify_fw_preamble()"); |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 434 | |
| 435 | memcpy(buf, buf2, buf_size); |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame] | 436 | pre->c.magic = VB2_MAGIC_PACKED_KEY; |
| 437 | TEST_EQ(vb2_verify_fw_preamble(pre, buf_size, &pubk, &wb), |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 438 | VB2_ERROR_PREAMBLE_MAGIC, |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame] | 439 | "vb2_verify_fw_preamble() magic"); |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 440 | |
| 441 | memcpy(buf, buf2, buf_size); |
| 442 | pre->c.fixed_size++; |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame] | 443 | TEST_EQ(vb2_verify_fw_preamble(pre, buf_size, &pubk, &wb), |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 444 | VB2_ERROR_COMMON_FIXED_UNALIGNED, |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame] | 445 | "vb2_verify_fw_preamble() header"); |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 446 | |
| 447 | memcpy(buf, buf2, buf_size); |
| 448 | pre->c.struct_version_major++; |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame] | 449 | TEST_EQ(vb2_verify_fw_preamble(pre, buf_size, &pubk, &wb), |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 450 | VB2_ERROR_PREAMBLE_HEADER_VERSION, |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame] | 451 | "vb2_verify_fw_preamble() major version"); |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 452 | |
| 453 | memcpy(buf, buf2, buf_size); |
| 454 | pre->c.struct_version_minor++; |
| 455 | /* That changes the signature, so resign the fw_preamble */ |
| 456 | vb2_sign_data(&sig, buf, pre->sig_offset, prikhash, NULL); |
| 457 | memcpy(buf + pre->sig_offset, sig, sig->c.total_size); |
| 458 | free(sig); |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame] | 459 | TEST_SUCC(vb2_verify_fw_preamble(pre, buf_size, &pubk, &wb), |
| 460 | "vb2_verify_fw_preamble() minor version"); |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 461 | |
| 462 | memcpy(buf, buf2, buf_size); |
| 463 | pre->c.fixed_size -= 4; |
| 464 | pre->c.desc_size += 4; |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame] | 465 | TEST_EQ(vb2_verify_fw_preamble(pre, buf_size, &pubk, &wb), |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 466 | VB2_ERROR_PREAMBLE_SIZE, |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame] | 467 | "vb2_verify_fw_preamble() header size"); |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 468 | |
| 469 | memcpy(buf, buf2, buf_size); |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame] | 470 | sig = (struct vb2_signature *)(buf + pre->hash_offset); |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 471 | sig->c.total_size += pre->c.total_size; |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame] | 472 | TEST_EQ(vb2_verify_fw_preamble(pre, buf_size, &pubk, &wb), |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 473 | VB2_ERROR_COMMON_TOTAL_SIZE, |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame] | 474 | "vb2_verify_fw_preamble() hash size"); |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 475 | |
| 476 | memcpy(buf, buf2, buf_size); |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame] | 477 | sig = (struct vb2_signature *)(buf + pre->hash_offset); |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 478 | sig->sig_size /= 2; |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame] | 479 | TEST_EQ(vb2_verify_fw_preamble(pre, buf_size, &pubk, &wb), |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 480 | VB2_ERROR_SIG_SIZE, |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame] | 481 | "vb2_verify_fw_preamble() hash integrity"); |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 482 | |
| 483 | memcpy(buf, buf2, buf_size); |
| 484 | pre->hash_count++; |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame] | 485 | TEST_EQ(vb2_verify_fw_preamble(pre, buf_size, &pubk, &wb), |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 486 | VB2_ERROR_COMMON_MEMBER_OVERLAP, |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame] | 487 | "vb2_verify_fw_preamble() hash count"); |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 488 | |
| 489 | memcpy(buf, buf2, buf_size); |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame] | 490 | sig = (struct vb2_signature *)(buf + pre->sig_offset); |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 491 | sig->c.total_size += 4; |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame] | 492 | TEST_EQ(vb2_verify_fw_preamble(pre, buf_size, &pubk, &wb), |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 493 | VB2_ERROR_COMMON_TOTAL_SIZE, |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame] | 494 | "vb2_verify_fw_preamble() sig inside"); |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 495 | |
| 496 | memcpy(buf, buf2, buf_size); |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame] | 497 | sig = (struct vb2_signature *)(buf + pre->sig_offset); |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 498 | buf[pre->sig_offset + sig->sig_offset]++; |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame] | 499 | TEST_EQ(vb2_verify_fw_preamble(pre, buf_size, &pubk, &wb), |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 500 | VB2_ERROR_VDATA_VERIFY_DIGEST, |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame] | 501 | "vb2_verify_fw_preamble() sig corrupt"); |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 502 | |
| 503 | memcpy(buf, buf2, buf_size); |
| 504 | pre->flags++; |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame] | 505 | TEST_EQ(vb2_verify_fw_preamble(pre, buf_size, &pubk, &wb), |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 506 | VB2_ERROR_VDATA_VERIFY_DIGEST, |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame] | 507 | "vb2_verify_fw_preamble() preamble corrupt"); |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 508 | |
| 509 | free(buf); |
| 510 | free(buf2); |
| 511 | } |
| 512 | |
| 513 | int main(int argc, char* argv[]) |
| 514 | { |
| 515 | test_struct_packing(); |
| 516 | test_common_header_functions(); |
| 517 | test_sig_size(); |
| 518 | test_verify_hash(); |
| 519 | test_verify_keyblock(); |
| 520 | test_verify_fw_preamble(); |
| 521 | |
| 522 | return gTestSuccess ? 0 : 255; |
| 523 | } |