| 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 "file_keys.h" | 11 #include "file_keys.h" |
| 12 #include "firmware_image.h" | 12 #include "firmware_image.h" |
| 13 #include "rsa_utility.h" | 13 #include "rsa_utility.h" |
| 14 #include "sha_utility.h" | |
| 15 #include "utility.h" | 14 #include "utility.h" |
| 16 | 15 |
| 17 /* ANSI Color coding sequences. */ | 16 /* ANSI Color coding sequences. */ |
| 18 #define COL_GREEN "\e[1;32m" | 17 #define COL_GREEN "\e[1;32m" |
| 19 #define COL_RED "\e[0;31m]" | 18 #define COL_RED "\e[0;31m]" |
| 20 #define COL_STOP "\e[m" | 19 #define COL_STOP "\e[m" |
| 21 | 20 |
| 22 int TEST_EQ(int result, int expected_result, char* testname) { | 21 int TEST_EQ(int result, int expected_result, char* testname) { |
| 23 if (result == expected_result) { | 22 if (result == expected_result) { |
| 24 fprintf(stderr, "%s Test " COL_GREEN " PASSED\n" COL_STOP, testname); | 23 fprintf(stderr, "%s Test " COL_GREEN " PASSED\n" COL_STOP, testname); |
| 25 return 1; | 24 return 1; |
| 26 } | 25 } |
| 27 else { | 26 else { |
| 28 fprintf(stderr, "%s Test " COL_RED " FAILED\n" COL_STOP, testname); | 27 fprintf(stderr, "%s Test " COL_RED " FAILED\n" COL_STOP, testname); |
| 29 return 0; | 28 return 0; |
| 30 } | 29 } |
| 31 } | 30 } |
| 32 | 31 |
| 33 FirmwareImage* GenerateTestFirmwareImage(int algorithm, | 32 FirmwareImage* GenerateTestFirmwareImage(int algorithm, |
| 34 uint8_t* firmware_sign_key, | 33 uint8_t* firmware_sign_key, |
| 35 int firmware_key_version, | 34 int firmware_key_version, |
| 36 int firmware_version, | 35 int firmware_version, |
| 37 int firmware_len) { | 36 int firmware_len) { |
| 38 FirmwareImage* image = FirmwareImageNew(); | 37 FirmwareImage* image = FirmwareImageNew(); |
| 39 uint8_t* header_checksum; | |
| 40 DigestContext ctx; | |
| 41 | 38 |
| 42 Memcpy(image->magic, FIRMWARE_MAGIC, FIRMWARE_MAGIC_SIZE); | 39 Memcpy(image->magic, FIRMWARE_MAGIC, FIRMWARE_MAGIC_SIZE); |
| 43 image->firmware_sign_algorithm = algorithm; | 40 image->firmware_sign_algorithm = algorithm; |
| 44 image->firmware_sign_key = (uint8_t*) Malloc( | 41 image->firmware_sign_key = (uint8_t*) Malloc( |
| 45 RSAProcessedKeySize(image->firmware_sign_algorithm)); | 42 RSAProcessedKeySize(image->firmware_sign_algorithm)); |
| 46 Memcpy(image->firmware_sign_key, firmware_sign_key, | 43 Memcpy(image->firmware_sign_key, firmware_sign_key, |
| 47 RSAProcessedKeySize(image->firmware_sign_algorithm)); | 44 RSAProcessedKeySize(image->firmware_sign_algorithm)); |
| 48 image->firmware_key_version = firmware_key_version; | 45 image->firmware_key_version = firmware_key_version; |
| 49 | 46 |
| 50 /* Update correct header length. */ | 47 /* Update correct header length. */ |
| 51 image->header_len = GetFirmwareHeaderLen(image); | 48 image->header_len = GetFirmwareHeaderLen(image); |
| 52 | 49 |
| 53 /* Calculate SHA-512 digest on header and populate header_checksum. */ | 50 /* Calculate SHA-512 digest on header and populate header_checksum. */ |
| 54 DigestInit(&ctx, ROOT_SIGNATURE_ALGORITHM); | 51 CalculateFirmwareHeaderChecksum(image, image->header_checksum); |
| 55 DigestUpdate(&ctx, (uint8_t*) &image->header_len, | |
| 56 sizeof(image->header_len)); | |
| 57 DigestUpdate(&ctx, (uint8_t*) &image->firmware_sign_algorithm, | |
| 58 sizeof(image->firmware_sign_algorithm)); | |
| 59 DigestUpdate(&ctx, image->firmware_sign_key, | |
| 60 RSAProcessedKeySize(image->firmware_sign_algorithm)); | |
| 61 DigestUpdate(&ctx, (uint8_t*) &image->firmware_key_version, | |
| 62 sizeof(image->firmware_key_version)); | |
| 63 header_checksum = DigestFinal(&ctx); | |
| 64 Memcpy(image->header_checksum, header_checksum, SHA512_DIGEST_SIZE); | |
| 65 Free(header_checksum); | |
| 66 | |
| 67 | 52 |
| 68 /* Populate firmware and preamble with dummy data. */ | 53 /* Populate firmware and preamble with dummy data. */ |
| 69 image->firmware_version = firmware_version; | 54 image->firmware_version = firmware_version; |
| 70 image->firmware_len = firmware_len; | 55 image->firmware_len = firmware_len; |
| 71 image->preamble_signature = image->firmware_signature = NULL; | 56 image->preamble_signature = image->firmware_signature = NULL; |
| 72 Memset(image->preamble, 'P', FIRMWARE_PREAMBLE_SIZE); | 57 Memset(image->preamble, 'P', FIRMWARE_PREAMBLE_SIZE); |
| 73 image->firmware_data = Malloc(image->firmware_len); | 58 image->firmware_data = Malloc(image->firmware_len); |
| 74 Memset(image->firmware_data, 'F', image->firmware_len); | 59 Memset(image->firmware_data, 'F', image->firmware_len); |
| 75 | 60 |
| 76 return image; | 61 return image; |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 210 | 195 |
| 211 failure: | 196 failure: |
| 212 Free(firmware_blob); | 197 Free(firmware_blob); |
| 213 FirmwareImageFree(image); | 198 FirmwareImageFree(image); |
| 214 Free(firmware_sign_key_buf); | 199 Free(firmware_sign_key_buf); |
| 215 Free(root_key_blob); | 200 Free(root_key_blob); |
| 216 RSAPublicKeyFree(root_key); | 201 RSAPublicKeyFree(root_key); |
| 217 | 202 |
| 218 return error_code; | 203 return error_code; |
| 219 } | 204 } |
| OLD | NEW |