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; |
| 223 | uint8_t workbuf[VB2_VERIFY_DATA_WORKBUF_BYTES]; |
| 224 | struct vb2_workbuf wb; |
| 225 | |
| 226 | vb2_workbuf_init(&wb, workbuf, sizeof(workbuf)); |
| 227 | |
| 228 | TEST_SUCC(vb2_private_key_hash(&prik, VB2_HASH_SHA256), |
| 229 | "create private hash key"); |
| 230 | TEST_SUCC(vb2_public_key_hash(&pubk, VB2_HASH_SHA256), |
| 231 | "create hash key"); |
| 232 | |
| 233 | /* Create the signature */ |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame^] | 234 | 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] | 235 | "create hash sig"); |
| 236 | |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame^] | 237 | TEST_SUCC(vb2_verify_data(test_data, sizeof(test_data), |
| 238 | sig, &pubk, &wb), |
| 239 | "vb2_verify_data() hash ok"); |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 240 | |
| 241 | *((uint8_t *)sig + sig->sig_offset) ^= 0xab; |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame^] | 242 | TEST_EQ(vb2_verify_data(test_data, sizeof(test_data), sig, &pubk, &wb), |
| 243 | VB2_ERROR_VDATA_VERIFY_DIGEST, "vb2_verify_data() hash bad"); |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 244 | |
| 245 | free(sig); |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * Verify keyblock |
| 250 | */ |
| 251 | static void test_verify_keyblock(void) |
| 252 | { |
| 253 | const char desc[16] = "test keyblock"; |
| 254 | const struct vb2_private_key *prik[2]; |
| 255 | struct vb2_public_key pubk, pubk2, pubk3; |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame^] | 256 | struct vb2_signature *sig; |
| 257 | struct vb2_keyblock *kbuf; |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 258 | uint32_t buf_size; |
| 259 | uint8_t *buf, *buf2; |
| 260 | |
| 261 | uint8_t workbuf[VB2_KEY_BLOCK_VERIFY_WORKBUF_BYTES]; |
| 262 | struct vb2_workbuf wb; |
| 263 | |
| 264 | TEST_SUCC(vb2_public_key_hash(&pubk, VB2_HASH_SHA256), |
| 265 | "create hash key 1"); |
| 266 | TEST_SUCC(vb2_public_key_hash(&pubk2, VB2_HASH_SHA512), |
| 267 | "create hash key 2"); |
| 268 | TEST_SUCC(vb2_public_key_hash(&pubk3, VB2_HASH_SHA1), |
| 269 | "create hash key 3"); |
| 270 | |
| 271 | TEST_SUCC(vb2_private_key_hash(prik + 0, VB2_HASH_SHA256), |
| 272 | "create private key 1"); |
| 273 | TEST_SUCC(vb2_private_key_hash(prik + 1, VB2_HASH_SHA512), |
| 274 | "create private key 2"); |
| 275 | |
| 276 | /* Create the test keyblock */ |
| 277 | TEST_SUCC(vb2_keyblock_create(&kbuf, &pubk3, prik, 2, 0x4321, desc), |
| 278 | "create keyblock"); |
| 279 | |
| 280 | buf = (uint8_t *)kbuf; |
| 281 | buf_size = kbuf->c.total_size; |
| 282 | |
| 283 | /* Make a copy of the buffer, so we can mangle it for tests */ |
| 284 | buf2 = malloc(buf_size); |
| 285 | memcpy(buf2, buf, buf_size); |
| 286 | |
| 287 | vb2_workbuf_init(&wb, workbuf, sizeof(workbuf)); |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame^] | 288 | kbuf = (struct vb2_keyblock *)buf; |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 289 | |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame^] | 290 | TEST_SUCC(vb2_verify_keyblock(kbuf, buf_size, &pubk, &wb), |
| 291 | "vb2_verify_keyblock()"); |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 292 | |
| 293 | memcpy(buf, buf2, buf_size); |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame^] | 294 | TEST_SUCC(vb2_verify_keyblock(kbuf, buf_size, &pubk2, &wb), |
| 295 | "vb2_verify_keyblock() key 2"); |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 296 | |
| 297 | memcpy(buf, buf2, buf_size); |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame^] | 298 | TEST_EQ(vb2_verify_keyblock(kbuf, buf_size, &pubk3, &wb), |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 299 | VB2_ERROR_KEYBLOCK_SIG_GUID, |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame^] | 300 | "vb2_verify_keyblock() key not present"); |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 301 | |
| 302 | memcpy(buf, buf2, buf_size); |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame^] | 303 | kbuf->c.magic = VB2_MAGIC_PACKED_KEY; |
| 304 | TEST_EQ(vb2_verify_keyblock(kbuf, buf_size, &pubk, &wb), |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 305 | VB2_ERROR_KEYBLOCK_MAGIC, |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame^] | 306 | "vb2_verify_keyblock() magic"); |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 307 | |
| 308 | memcpy(buf, buf2, buf_size); |
| 309 | kbuf->c.fixed_size++; |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame^] | 310 | TEST_EQ(vb2_verify_keyblock(kbuf, buf_size, &pubk, &wb), |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 311 | VB2_ERROR_COMMON_FIXED_UNALIGNED, |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame^] | 312 | "vb2_verify_keyblock() header"); |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 313 | |
| 314 | memcpy(buf, buf2, buf_size); |
| 315 | kbuf->c.struct_version_major++; |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame^] | 316 | TEST_EQ(vb2_verify_keyblock(kbuf, buf_size, &pubk, &wb), |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 317 | VB2_ERROR_KEYBLOCK_HEADER_VERSION, |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame^] | 318 | "vb2_verify_keyblock() major version"); |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 319 | |
| 320 | memcpy(buf, buf2, buf_size); |
| 321 | kbuf->c.struct_version_minor++; |
| 322 | /* That changes the signature, so resign the keyblock */ |
| 323 | vb2_sign_data(&sig, buf, kbuf->sig_offset, prik[0], NULL); |
| 324 | memcpy(buf + kbuf->sig_offset, sig, sig->c.total_size); |
| 325 | free(sig); |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame^] | 326 | TEST_SUCC(vb2_verify_keyblock(kbuf, buf_size, &pubk, &wb), |
| 327 | "vb2_verify_keyblock() minor version"); |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 328 | |
| 329 | memcpy(buf, buf2, buf_size); |
| 330 | kbuf->c.fixed_size -= 4; |
| 331 | kbuf->c.desc_size += 4; |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame^] | 332 | TEST_EQ(vb2_verify_keyblock(kbuf, buf_size, &pubk, &wb), |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 333 | VB2_ERROR_KEYBLOCK_SIZE, |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame^] | 334 | "vb2_verify_keyblock() header size"); |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 335 | |
| 336 | memcpy(buf, buf2, buf_size); |
| 337 | kbuf->key_offset = kbuf->c.total_size - 4; |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame^] | 338 | TEST_EQ(vb2_verify_keyblock(kbuf, buf_size, &pubk, &wb), |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 339 | VB2_ERROR_COMMON_MEMBER_SIZE, |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame^] | 340 | "vb2_verify_keyblock() data key outside"); |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 341 | |
| 342 | memcpy(buf, buf2, buf_size); |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame^] | 343 | sig = (struct vb2_signature *)(buf + kbuf->sig_offset); |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 344 | sig->data_size--; |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame^] | 345 | TEST_EQ(vb2_verify_keyblock(kbuf, buf_size, &pubk, &wb), |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 346 | VB2_ERROR_KEYBLOCK_SIGNED_SIZE, |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame^] | 347 | "vb2_verify_keyblock() signed wrong size"); |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 348 | |
| 349 | memcpy(buf, buf2, buf_size); |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame^] | 350 | sig = (struct vb2_signature *)(buf + kbuf->sig_offset); |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 351 | sig->c.total_size = kbuf->c.total_size - 4; |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame^] | 352 | TEST_EQ(vb2_verify_keyblock(kbuf, buf_size, &pubk, &wb), |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 353 | VB2_ERROR_COMMON_TOTAL_SIZE, |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame^] | 354 | "vb2_verify_keyblock() key outside keyblock"); |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 355 | |
| 356 | memcpy(buf, buf2, buf_size); |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame^] | 357 | sig = (struct vb2_signature *)(buf + kbuf->sig_offset); |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 358 | sig->c.struct_version_major++; |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame^] | 359 | TEST_EQ(vb2_verify_keyblock(kbuf, buf_size, &pubk, &wb), |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 360 | VB2_ERROR_SIG_VERSION, |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame^] | 361 | "vb2_verify_keyblock() corrupt key"); |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 362 | |
| 363 | memcpy(buf, buf2, buf_size); |
| 364 | kbuf->c.struct_version_minor++; |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame^] | 365 | TEST_EQ(vb2_verify_keyblock(kbuf, buf_size, &pubk, &wb), |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 366 | VB2_ERROR_VDATA_VERIFY_DIGEST, |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame^] | 367 | "vb2_verify_keyblock() corrupt"); |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 368 | |
| 369 | free(buf); |
| 370 | free(buf2); |
| 371 | } |
| 372 | |
| 373 | /** |
| 374 | * Verify firmware preamble |
| 375 | */ |
| 376 | static void test_verify_fw_preamble(void) |
| 377 | { |
| 378 | const char desc[16] = "test preamble"; |
| 379 | const struct vb2_private_key *prikhash; |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame^] | 380 | struct vb2_signature *hashes[3]; |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 381 | struct vb2_public_key pubk; |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame^] | 382 | struct vb2_signature *sig; |
| 383 | struct vb2_fw_preamble *pre; |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 384 | uint32_t buf_size; |
| 385 | uint8_t *buf, *buf2; |
| 386 | |
| 387 | uint8_t workbuf[VB2_VERIFY_FIRMWARE_PREAMBLE_WORKBUF_BYTES]; |
| 388 | struct vb2_workbuf wb; |
| 389 | |
| 390 | /* |
| 391 | * 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^] | 392 | * 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] | 393 | * same (and its functionality is verified separately), and using a |
| 394 | * bare hash here saves us from needing to have a private key to do |
| 395 | * this test. |
| 396 | */ |
| 397 | TEST_SUCC(vb2_public_key_hash(&pubk, VB2_HASH_SHA256), |
| 398 | "create hash key"); |
| 399 | TEST_SUCC(vb2_private_key_hash(&prikhash, VB2_HASH_SHA256), |
| 400 | "Create private hash key"); |
| 401 | |
| 402 | /* Create some signatures */ |
| 403 | TEST_SUCC(vb2_sign_data(hashes + 0, test_data, sizeof(test_data), |
| 404 | prikhash, "Hash 1"), |
| 405 | "Hash 1"); |
| 406 | TEST_SUCC(vb2_sign_data(hashes + 1, test_data2, sizeof(test_data2), |
| 407 | prikhash, "Hash 2"), |
| 408 | "Hash 2"); |
| 409 | TEST_SUCC(vb2_sign_data(hashes + 2, test_data3, sizeof(test_data3), |
| 410 | prikhash, "Hash 3"), |
| 411 | "Hash 3"); |
| 412 | |
| 413 | /* Test good preamble */ |
| 414 | TEST_SUCC(vb2_fw_preamble_create(&pre, prikhash, |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame^] | 415 | (const struct vb2_signature **)hashes, |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 416 | 3, 0x1234, 0x5678, desc), |
| 417 | "Create preamble good"); |
| 418 | |
| 419 | buf = (uint8_t *)pre; |
| 420 | buf_size = pre->c.total_size; |
| 421 | |
| 422 | /* Make a copy of the buffer, so we can mangle it for tests */ |
| 423 | buf2 = malloc(buf_size); |
| 424 | memcpy(buf2, buf, buf_size); |
| 425 | |
| 426 | vb2_workbuf_init(&wb, workbuf, sizeof(workbuf)); |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame^] | 427 | pre = (struct vb2_fw_preamble *)buf; |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 428 | |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame^] | 429 | TEST_SUCC(vb2_verify_fw_preamble(pre, buf_size, &pubk, &wb), |
| 430 | "vb2_verify_fw_preamble()"); |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 431 | |
| 432 | memcpy(buf, buf2, buf_size); |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame^] | 433 | pre->c.magic = VB2_MAGIC_PACKED_KEY; |
| 434 | TEST_EQ(vb2_verify_fw_preamble(pre, buf_size, &pubk, &wb), |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 435 | VB2_ERROR_PREAMBLE_MAGIC, |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame^] | 436 | "vb2_verify_fw_preamble() magic"); |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 437 | |
| 438 | memcpy(buf, buf2, buf_size); |
| 439 | pre->c.fixed_size++; |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame^] | 440 | TEST_EQ(vb2_verify_fw_preamble(pre, buf_size, &pubk, &wb), |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 441 | VB2_ERROR_COMMON_FIXED_UNALIGNED, |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame^] | 442 | "vb2_verify_fw_preamble() header"); |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 443 | |
| 444 | memcpy(buf, buf2, buf_size); |
| 445 | pre->c.struct_version_major++; |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame^] | 446 | TEST_EQ(vb2_verify_fw_preamble(pre, buf_size, &pubk, &wb), |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 447 | VB2_ERROR_PREAMBLE_HEADER_VERSION, |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame^] | 448 | "vb2_verify_fw_preamble() major version"); |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 449 | |
| 450 | memcpy(buf, buf2, buf_size); |
| 451 | pre->c.struct_version_minor++; |
| 452 | /* That changes the signature, so resign the fw_preamble */ |
| 453 | vb2_sign_data(&sig, buf, pre->sig_offset, prikhash, NULL); |
| 454 | memcpy(buf + pre->sig_offset, sig, sig->c.total_size); |
| 455 | free(sig); |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame^] | 456 | TEST_SUCC(vb2_verify_fw_preamble(pre, buf_size, &pubk, &wb), |
| 457 | "vb2_verify_fw_preamble() minor version"); |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 458 | |
| 459 | memcpy(buf, buf2, buf_size); |
| 460 | pre->c.fixed_size -= 4; |
| 461 | pre->c.desc_size += 4; |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame^] | 462 | TEST_EQ(vb2_verify_fw_preamble(pre, buf_size, &pubk, &wb), |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 463 | VB2_ERROR_PREAMBLE_SIZE, |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame^] | 464 | "vb2_verify_fw_preamble() header size"); |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 465 | |
| 466 | memcpy(buf, buf2, buf_size); |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame^] | 467 | sig = (struct vb2_signature *)(buf + pre->hash_offset); |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 468 | sig->c.total_size += pre->c.total_size; |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame^] | 469 | TEST_EQ(vb2_verify_fw_preamble(pre, buf_size, &pubk, &wb), |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 470 | VB2_ERROR_COMMON_TOTAL_SIZE, |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame^] | 471 | "vb2_verify_fw_preamble() hash size"); |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 472 | |
| 473 | memcpy(buf, buf2, buf_size); |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame^] | 474 | sig = (struct vb2_signature *)(buf + pre->hash_offset); |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 475 | sig->sig_size /= 2; |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame^] | 476 | TEST_EQ(vb2_verify_fw_preamble(pre, buf_size, &pubk, &wb), |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 477 | VB2_ERROR_SIG_SIZE, |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame^] | 478 | "vb2_verify_fw_preamble() hash integrity"); |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 479 | |
| 480 | memcpy(buf, buf2, buf_size); |
| 481 | pre->hash_count++; |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame^] | 482 | TEST_EQ(vb2_verify_fw_preamble(pre, buf_size, &pubk, &wb), |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 483 | VB2_ERROR_COMMON_MEMBER_OVERLAP, |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame^] | 484 | "vb2_verify_fw_preamble() hash count"); |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 485 | |
| 486 | memcpy(buf, buf2, buf_size); |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame^] | 487 | sig = (struct vb2_signature *)(buf + pre->sig_offset); |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 488 | sig->c.total_size += 4; |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame^] | 489 | TEST_EQ(vb2_verify_fw_preamble(pre, buf_size, &pubk, &wb), |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 490 | VB2_ERROR_COMMON_TOTAL_SIZE, |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame^] | 491 | "vb2_verify_fw_preamble() sig inside"); |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 492 | |
| 493 | memcpy(buf, buf2, buf_size); |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame^] | 494 | sig = (struct vb2_signature *)(buf + pre->sig_offset); |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 495 | buf[pre->sig_offset + sig->sig_offset]++; |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame^] | 496 | TEST_EQ(vb2_verify_fw_preamble(pre, buf_size, &pubk, &wb), |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 497 | VB2_ERROR_VDATA_VERIFY_DIGEST, |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame^] | 498 | "vb2_verify_fw_preamble() sig corrupt"); |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 499 | |
| 500 | memcpy(buf, buf2, buf_size); |
| 501 | pre->flags++; |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame^] | 502 | TEST_EQ(vb2_verify_fw_preamble(pre, buf_size, &pubk, &wb), |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 503 | VB2_ERROR_VDATA_VERIFY_DIGEST, |
Randall Spangler | 308d254 | 2014-12-04 09:54:37 -0800 | [diff] [blame^] | 504 | "vb2_verify_fw_preamble() preamble corrupt"); |
Randall Spangler | 108d991 | 2014-12-02 15:55:56 -0800 | [diff] [blame] | 505 | |
| 506 | free(buf); |
| 507 | free(buf2); |
| 508 | } |
| 509 | |
| 510 | int main(int argc, char* argv[]) |
| 511 | { |
| 512 | test_struct_packing(); |
| 513 | test_common_header_functions(); |
| 514 | test_sig_size(); |
| 515 | test_verify_hash(); |
| 516 | test_verify_keyblock(); |
| 517 | test_verify_fw_preamble(); |
| 518 | |
| 519 | return gTestSuccess ? 0 : 255; |
| 520 | } |