| OLD | NEW |
| 1 /* Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 1 /* Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
| 2 * Use of this source code is governed by a BSD-style license that can be | 2 * Use of this source code is governed by a BSD-style license that can be |
| 3 * found in the LICENSE file. | 3 * found in the LICENSE file. |
| 4 * | 4 * |
| 5 * Tests for firmware image library. | 5 * Tests for firmware image library. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include <stdio.h> | 8 #include <stdio.h> |
| 9 #include <stdlib.h> | 9 #include <stdlib.h> |
| 10 | 10 |
| 11 #include "cryptolib.h" | 11 #include "cryptolib.h" |
| 12 #include "file_keys.h" | 12 #include "file_keys.h" |
| 13 #include "firmware_image.h" | 13 #include "firmware_image.h" |
| 14 #include "host_common.h" | 14 #include "host_common.h" |
| 15 #include "test_common.h" | 15 #include "test_common.h" |
| 16 #include "utility.h" | 16 #include "utility.h" |
| 17 #include "vboot_common.h" | 17 #include "vboot_common.h" |
| 18 | 18 |
| 19 | 19 |
| 20 static void ReChecksumKeyBlock(VbKeyBlockHeader *h) { | 20 static void ReChecksumKeyBlock(VbKeyBlockHeader *h) { |
| 21 uint8_t* newchk = DigestBuf((const uint8_t*)h, | 21 uint8_t* newchk = DigestBuf((const uint8_t*)h, |
| 22 h->key_block_checksum.data_size, | 22 h->key_block_checksum.data_size, |
| 23 SHA512_DIGEST_ALGORITHM); | 23 SHA512_DIGEST_ALGORITHM); |
| 24 Memcpy(GetSignatureData(&h->key_block_checksum), newchk, SHA512_DIGEST_SIZE); | 24 Memcpy(GetSignatureData(&h->key_block_checksum), newchk, SHA512_DIGEST_SIZE); |
| 25 Free(newchk); | 25 Free(newchk); |
| 26 } | 26 } |
| 27 | 27 |
| 28 | 28 |
| 29 static void VerifyKeyBlockTest(const VbPublicKey* public_key, | 29 static void KeyBlockVerifyTest(const VbPublicKey* public_key, |
| 30 const VbPrivateKey* private_key, | 30 const VbPrivateKey* private_key, |
| 31 const VbPublicKey* data_key) { | 31 const VbPublicKey* data_key) { |
| 32 | 32 |
| 33 VbKeyBlockHeader *hdr; | 33 VbKeyBlockHeader *hdr; |
| 34 VbKeyBlockHeader *h; | 34 VbKeyBlockHeader *h; |
| 35 uint64_t hsize; | 35 uint64_t hsize; |
| 36 | 36 |
| 37 hdr = CreateKeyBlock(data_key, private_key, 0x1234); | 37 hdr = KeyBlockCreate(data_key, private_key, 0x1234); |
| 38 TEST_NEQ((size_t)hdr, 0, "VerifyKeyBlock() prerequisites"); | 38 TEST_NEQ((size_t)hdr, 0, "KeyBlockVerify() prerequisites"); |
| 39 if (!hdr) | 39 if (!hdr) |
| 40 return; | 40 return; |
| 41 hsize = hdr->key_block_size; | 41 hsize = hdr->key_block_size; |
| 42 h = (VbKeyBlockHeader*)Malloc(hsize + 1024); | 42 h = (VbKeyBlockHeader*)Malloc(hsize + 1024); |
| 43 | 43 |
| 44 TEST_EQ(VerifyKeyBlock(hdr, hsize, NULL), 0, | 44 TEST_EQ(KeyBlockVerify(hdr, hsize, NULL), 0, |
| 45 "VerifyKeyBlock() ok using checksum"); | 45 "KeyBlockVerify() ok using checksum"); |
| 46 TEST_EQ(VerifyKeyBlock(hdr, hsize, public_key), 0, | 46 TEST_EQ(KeyBlockVerify(hdr, hsize, public_key), 0, |
| 47 "VerifyKeyBlock() ok using key"); | 47 "KeyBlockVerify() ok using key"); |
| 48 | 48 |
| 49 TEST_NEQ(VerifyKeyBlock(hdr, hsize - 1, NULL), 0, "VerifyKeyBlock() size--"); | 49 TEST_NEQ(KeyBlockVerify(hdr, hsize - 1, NULL), 0, "KeyBlockVerify() size--"); |
| 50 TEST_EQ(VerifyKeyBlock(hdr, hsize + 1, NULL), 0, "VerifyKeyBlock() size++"); | 50 TEST_EQ(KeyBlockVerify(hdr, hsize + 1, NULL), 0, "KeyBlockVerify() size++"); |
| 51 | 51 |
| 52 Memcpy(h, hdr, hsize); | 52 Memcpy(h, hdr, hsize); |
| 53 h->magic[0] &= 0x12; | 53 h->magic[0] &= 0x12; |
| 54 TEST_NEQ(VerifyKeyBlock(h, hsize, NULL), 0, "VerifyKeyBlock() magic"); | 54 TEST_NEQ(KeyBlockVerify(h, hsize, NULL), 0, "KeyBlockVerify() magic"); |
| 55 | 55 |
| 56 /* Care about major version but not minor */ | 56 /* Care about major version but not minor */ |
| 57 Memcpy(h, hdr, hsize); | 57 Memcpy(h, hdr, hsize); |
| 58 h->header_version_major++; | 58 h->header_version_major++; |
| 59 ReChecksumKeyBlock(h); | 59 ReChecksumKeyBlock(h); |
| 60 TEST_NEQ(VerifyKeyBlock(h, hsize, NULL), 0, "VerifyKeyBlock() major++"); | 60 TEST_NEQ(KeyBlockVerify(h, hsize, NULL), 0, "KeyBlockVerify() major++"); |
| 61 | 61 |
| 62 Memcpy(h, hdr, hsize); | 62 Memcpy(h, hdr, hsize); |
| 63 h->header_version_major--; | 63 h->header_version_major--; |
| 64 ReChecksumKeyBlock(h); | 64 ReChecksumKeyBlock(h); |
| 65 TEST_NEQ(VerifyKeyBlock(h, hsize, NULL), 0, "VerifyKeyBlock() major--"); | 65 TEST_NEQ(KeyBlockVerify(h, hsize, NULL), 0, "KeyBlockVerify() major--"); |
| 66 | 66 |
| 67 Memcpy(h, hdr, hsize); | 67 Memcpy(h, hdr, hsize); |
| 68 h->header_version_minor++; | 68 h->header_version_minor++; |
| 69 ReChecksumKeyBlock(h); | 69 ReChecksumKeyBlock(h); |
| 70 TEST_EQ(VerifyKeyBlock(h, hsize, NULL), 0, "VerifyKeyBlock() minor++"); | 70 TEST_EQ(KeyBlockVerify(h, hsize, NULL), 0, "KeyBlockVerify() minor++"); |
| 71 | 71 |
| 72 Memcpy(h, hdr, hsize); | 72 Memcpy(h, hdr, hsize); |
| 73 h->header_version_minor--; | 73 h->header_version_minor--; |
| 74 ReChecksumKeyBlock(h); | 74 ReChecksumKeyBlock(h); |
| 75 TEST_EQ(VerifyKeyBlock(h, hsize, NULL), 0, "VerifyKeyBlock() minor--"); | 75 TEST_EQ(KeyBlockVerify(h, hsize, NULL), 0, "KeyBlockVerify() minor--"); |
| 76 | 76 |
| 77 /* Check hash */ | 77 /* Check hash */ |
| 78 Memcpy(h, hdr, hsize); | 78 Memcpy(h, hdr, hsize); |
| 79 h->key_block_checksum.sig_offset = hsize; | 79 h->key_block_checksum.sig_offset = hsize; |
| 80 ReChecksumKeyBlock(h); | 80 ReChecksumKeyBlock(h); |
| 81 TEST_NEQ(VerifyKeyBlock(h, hsize, NULL), 0, | 81 TEST_NEQ(KeyBlockVerify(h, hsize, NULL), 0, |
| 82 "VerifyKeyBlock() checksum off end"); | 82 "KeyBlockVerify() checksum off end"); |
| 83 | 83 |
| 84 Memcpy(h, hdr, hsize); | 84 Memcpy(h, hdr, hsize); |
| 85 h->key_block_checksum.sig_size /= 2; | 85 h->key_block_checksum.sig_size /= 2; |
| 86 ReChecksumKeyBlock(h); | 86 ReChecksumKeyBlock(h); |
| 87 TEST_NEQ(VerifyKeyBlock(h, hsize, NULL), 0, | 87 TEST_NEQ(KeyBlockVerify(h, hsize, NULL), 0, |
| 88 "VerifyKeyBlock() checksum too small"); | 88 "KeyBlockVerify() checksum too small"); |
| 89 | 89 |
| 90 Memcpy(h, hdr, hsize); | 90 Memcpy(h, hdr, hsize); |
| 91 GetPublicKeyData(&h->data_key)[0] ^= 0x34; | 91 GetPublicKeyData(&h->data_key)[0] ^= 0x34; |
| 92 TEST_NEQ(VerifyKeyBlock(h, hsize, NULL), 0, | 92 TEST_NEQ(KeyBlockVerify(h, hsize, NULL), 0, |
| 93 "VerifyKeyBlock() checksum mismatch"); | 93 "KeyBlockVerify() checksum mismatch"); |
| 94 | 94 |
| 95 /* Check signature */ | 95 /* Check signature */ |
| 96 Memcpy(h, hdr, hsize); | 96 Memcpy(h, hdr, hsize); |
| 97 h->key_block_signature.sig_offset = hsize; | 97 h->key_block_signature.sig_offset = hsize; |
| 98 ReChecksumKeyBlock(h); | 98 ReChecksumKeyBlock(h); |
| 99 TEST_NEQ(VerifyKeyBlock(h, hsize, public_key), 0, | 99 TEST_NEQ(KeyBlockVerify(h, hsize, public_key), 0, |
| 100 "VerifyKeyBlock() sig off end"); | 100 "KeyBlockVerify() sig off end"); |
| 101 | 101 |
| 102 Memcpy(h, hdr, hsize); | 102 Memcpy(h, hdr, hsize); |
| 103 h->key_block_signature.sig_size--; | 103 h->key_block_signature.sig_size--; |
| 104 ReChecksumKeyBlock(h); | 104 ReChecksumKeyBlock(h); |
| 105 TEST_NEQ(VerifyKeyBlock(h, hsize, public_key), 0, | 105 TEST_NEQ(KeyBlockVerify(h, hsize, public_key), 0, |
| 106 "VerifyKeyBlock() sig too small"); | 106 "KeyBlockVerify() sig too small"); |
| 107 | 107 |
| 108 Memcpy(h, hdr, hsize); | 108 Memcpy(h, hdr, hsize); |
| 109 GetPublicKeyData(&h->data_key)[0] ^= 0x34; | 109 GetPublicKeyData(&h->data_key)[0] ^= 0x34; |
| 110 TEST_NEQ(VerifyKeyBlock(h, hsize, public_key), 0, | 110 TEST_NEQ(KeyBlockVerify(h, hsize, public_key), 0, |
| 111 "VerifyKeyBlock() sig mismatch"); | 111 "KeyBlockVerify() sig mismatch"); |
| 112 | 112 |
| 113 /* Check that we signed header and data key */ | 113 /* Check that we signed header and data key */ |
| 114 Memcpy(h, hdr, hsize); | 114 Memcpy(h, hdr, hsize); |
| 115 h->key_block_checksum.data_size = 4; | 115 h->key_block_checksum.data_size = 4; |
| 116 h->data_key.key_offset = 0; | 116 h->data_key.key_offset = 0; |
| 117 h->data_key.key_size = 0; | 117 h->data_key.key_size = 0; |
| 118 ReChecksumKeyBlock(h); | 118 ReChecksumKeyBlock(h); |
| 119 TEST_NEQ(VerifyKeyBlock(h, hsize, NULL), 0, | 119 TEST_NEQ(KeyBlockVerify(h, hsize, NULL), 0, |
| 120 "VerifyKeyBlock() didn't sign header"); | 120 "KeyBlockVerify() didn't sign header"); |
| 121 | 121 |
| 122 Memcpy(h, hdr, hsize); | 122 Memcpy(h, hdr, hsize); |
| 123 h->data_key.key_offset = hsize; | 123 h->data_key.key_offset = hsize; |
| 124 ReChecksumKeyBlock(h); | 124 ReChecksumKeyBlock(h); |
| 125 TEST_NEQ(VerifyKeyBlock(h, hsize, NULL), 0, | 125 TEST_NEQ(KeyBlockVerify(h, hsize, NULL), 0, |
| 126 "VerifyKeyBlock() data key off end"); | 126 "KeyBlockVerify() data key off end"); |
| 127 | 127 |
| 128 /* TODO: verify parser can support a bigger header (i.e., one where | 128 /* TODO: verify parser can support a bigger header (i.e., one where |
| 129 * data_key.key_offset is bigger than expected). */ | 129 * data_key.key_offset is bigger than expected). */ |
| 130 | 130 |
| 131 Free(h); | 131 Free(h); |
| 132 Free(hdr); | 132 Free(hdr); |
| 133 } | 133 } |
| 134 | 134 |
| 135 | 135 |
| 136 static void ReSignFirmwarePreamble(VbFirmwarePreambleHeader *h, | 136 static void ReSignFirmwarePreamble(VbFirmwarePreambleHeader *h, |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 276 fprintf(stderr, "Error reading signing_public_key"); | 276 fprintf(stderr, "Error reading signing_public_key"); |
| 277 return 1; | 277 return 1; |
| 278 } | 278 } |
| 279 | 279 |
| 280 data_public_key = PublicKeyReadKeyb(argv[6], data_key_algorithm, 1); | 280 data_public_key = PublicKeyReadKeyb(argv[6], data_key_algorithm, 1); |
| 281 if (!data_public_key) { | 281 if (!data_public_key) { |
| 282 fprintf(stderr, "Error reading data_public_key"); | 282 fprintf(stderr, "Error reading data_public_key"); |
| 283 return 1; | 283 return 1; |
| 284 } | 284 } |
| 285 | 285 |
| 286 VerifyKeyBlockTest(signing_public_key, signing_private_key, data_public_key); | 286 KeyBlockVerifyTest(signing_public_key, signing_private_key, data_public_key); |
| 287 VerifyFirmwarePreambleTest(signing_public_key, signing_private_key, | 287 VerifyFirmwarePreambleTest(signing_public_key, signing_private_key, |
| 288 data_public_key); | 288 data_public_key); |
| 289 | 289 |
| 290 if (signing_public_key) | 290 if (signing_public_key) |
| 291 Free(signing_public_key); | 291 Free(signing_public_key); |
| 292 if (signing_private_key) | 292 if (signing_private_key) |
| 293 Free(signing_private_key); | 293 Free(signing_private_key); |
| 294 if (data_public_key) | 294 if (data_public_key) |
| 295 Free(data_public_key); | 295 Free(data_public_key); |
| 296 | 296 |
| 297 return error_code; | 297 return error_code; |
| 298 } | 298 } |
| OLD | NEW |