| 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 * Common functions used by tests. | 5 * Common functions used by tests. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "test_common.h" | 8 #include "test_common.h" |
| 9 | 9 |
| 10 #include <stdio.h> | 10 #include <stdio.h> |
| (...skipping 27 matching lines...) Expand all Loading... |
| 38 fprintf(stderr, "%s Test " COL_GREEN "PASSED\n" COL_STOP, testname); | 38 fprintf(stderr, "%s Test " COL_GREEN "PASSED\n" COL_STOP, testname); |
| 39 return 1; | 39 return 1; |
| 40 } | 40 } |
| 41 else { | 41 else { |
| 42 fprintf(stderr, "%s Test " COL_RED "FAILED\n" COL_STOP, testname); | 42 fprintf(stderr, "%s Test " COL_RED "FAILED\n" COL_STOP, testname); |
| 43 gTestSuccess = 0; | 43 gTestSuccess = 0; |
| 44 return 0; | 44 return 0; |
| 45 } | 45 } |
| 46 } | 46 } |
| 47 | 47 |
| 48 FirmwareImage* GenerateTestFirmwareImage(int algorithm, | |
| 49 const uint8_t* firmware_sign_key, | |
| 50 int firmware_key_version, | |
| 51 int firmware_version, | |
| 52 uint64_t firmware_len, | |
| 53 const char* root_key_file, | |
| 54 const char* firmware_key_file, | |
| 55 uint8_t firmware_data_fill_char) { | |
| 56 FirmwareImage* image = FirmwareImageNew(); | |
| 57 | |
| 58 Memcpy(image->magic, FIRMWARE_MAGIC, FIRMWARE_MAGIC_SIZE); | |
| 59 image->firmware_sign_algorithm = algorithm; | |
| 60 image->firmware_sign_key = (uint8_t*) Malloc( | |
| 61 RSAProcessedKeySize(image->firmware_sign_algorithm)); | |
| 62 Memcpy(image->firmware_sign_key, firmware_sign_key, | |
| 63 RSAProcessedKeySize(image->firmware_sign_algorithm)); | |
| 64 image->firmware_key_version = firmware_key_version; | |
| 65 | |
| 66 /* Update correct header length. */ | |
| 67 image->header_len = GetFirmwareHeaderLen(image); | |
| 68 | |
| 69 /* Calculate SHA-512 digest on header and populate header_checksum. */ | |
| 70 CalculateFirmwareHeaderChecksum(image, image->header_checksum); | |
| 71 | |
| 72 /* Populate firmware and preamble with dummy data. */ | |
| 73 image->firmware_version = firmware_version; | |
| 74 image->firmware_len = firmware_len; | |
| 75 /* Make the kernel subkey the same as the firmware signing key. */ | |
| 76 image->kernel_subkey_sign_algorithm = algorithm; | |
| 77 image->kernel_subkey_sign_key = (uint8_t*) Malloc( | |
| 78 RSAProcessedKeySize(image->kernel_subkey_sign_algorithm)); | |
| 79 Memcpy(image->kernel_subkey_sign_key, firmware_sign_key, | |
| 80 RSAProcessedKeySize(image->kernel_subkey_sign_algorithm)); | |
| 81 image->preamble_signature = image->firmware_signature = NULL; | |
| 82 Memset(image->preamble, 'P', FIRMWARE_PREAMBLE_SIZE); | |
| 83 image->firmware_data = Malloc(image->firmware_len); | |
| 84 Memset(image->firmware_data, firmware_data_fill_char, image->firmware_len); | |
| 85 | |
| 86 /* Generate and populate signatures. */ | |
| 87 if (!AddFirmwareKeySignature(image, root_key_file)) { | |
| 88 debug("Couldn't create key signature.\n"); | |
| 89 FirmwareImageFree(image); | |
| 90 return NULL; | |
| 91 } | |
| 92 | |
| 93 if (!AddFirmwareSignature(image, firmware_key_file)) { | |
| 94 debug("Couldn't create firmware and preamble signature.\n"); | |
| 95 FirmwareImageFree(image); | |
| 96 return NULL; | |
| 97 } | |
| 98 return image; | |
| 99 } | |
| 100 | |
| 101 uint8_t* GenerateTestVerificationBlob(int algorithm, | |
| 102 const uint8_t* firmware_sign_key, | |
| 103 int firmware_key_version, | |
| 104 int firmware_version, | |
| 105 uint64_t firmware_len, | |
| 106 const char* root_key_file, | |
| 107 const char* firmware_key_file) { | |
| 108 FirmwareImage* image = NULL; | |
| 109 uint8_t* firmware_blob = NULL; | |
| 110 uint64_t firmware_blob_len = 0; | |
| 111 | |
| 112 image = GenerateTestFirmwareImage(algorithm, | |
| 113 firmware_sign_key, | |
| 114 firmware_key_version, | |
| 115 firmware_version, | |
| 116 firmware_len, | |
| 117 root_key_file, | |
| 118 firmware_key_file, | |
| 119 'F'); | |
| 120 firmware_blob = GetFirmwareBlob(image, &firmware_blob_len); | |
| 121 FirmwareImageFree(image); | |
| 122 return firmware_blob; | |
| 123 } | |
| 124 | |
| 125 uint8_t* GenerateRollbackTestVerificationBlob(int firmware_key_version, | |
| 126 int firmware_version) { | |
| 127 FirmwareImage* image = NULL; | |
| 128 uint64_t len; | |
| 129 uint8_t* verification_blob = NULL; | |
| 130 uint8_t* firmware_sign_key = NULL; | |
| 131 | |
| 132 firmware_sign_key = BufferFromFile("testkeys/key_rsa1024.keyb", | |
| 133 &len); | |
| 134 if (!firmware_sign_key) | |
| 135 return NULL; | |
| 136 image = GenerateTestFirmwareImage(0, /* RSA1024/SHA1 */ | |
| 137 firmware_sign_key, | |
| 138 firmware_key_version, | |
| 139 firmware_version, | |
| 140 1, /* Firmware length. */ | |
| 141 "testkeys/key_rsa8192.pem", | |
| 142 "testkeys/key_rsa1024.pem", | |
| 143 'F'); | |
| 144 if (!image) | |
| 145 return NULL; | |
| 146 verification_blob = GetFirmwareBlob(image, &len); | |
| 147 FirmwareImageFree(image); | |
| 148 return verification_blob; | |
| 149 } | |
| 150 | |
| 151 | |
| 152 KernelImage* GenerateTestKernelImage(int firmware_sign_algorithm, | 48 KernelImage* GenerateTestKernelImage(int firmware_sign_algorithm, |
| 153 int kernel_sign_algorithm, | 49 int kernel_sign_algorithm, |
| 154 const uint8_t* kernel_sign_key, | 50 const uint8_t* kernel_sign_key, |
| 155 int kernel_key_version, | 51 int kernel_key_version, |
| 156 int kernel_version, | 52 int kernel_version, |
| 157 uint64_t kernel_len, | 53 uint64_t kernel_len, |
| 158 const char* firmware_key_file, | 54 const char* firmware_key_file, |
| 159 const char* kernel_key_file, | 55 const char* kernel_key_file, |
| 160 uint8_t kernel_data_fill_char) { | 56 uint8_t kernel_data_fill_char) { |
| 161 KernelImage* image = KernelImageNew(); | 57 KernelImage* image = KernelImageNew(); |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 252 if (!image) | 148 if (!image) |
| 253 return NULL; | 149 return NULL; |
| 254 if (is_corrupt) { | 150 if (is_corrupt) { |
| 255 /* Invalidate image. */ | 151 /* Invalidate image. */ |
| 256 Memset(image->kernel_data, 'X', image->kernel_len); | 152 Memset(image->kernel_data, 'X', image->kernel_len); |
| 257 } | 153 } |
| 258 kernel_blob = GetKernelBlob(image, &len); | 154 kernel_blob = GetKernelBlob(image, &len); |
| 259 KernelImageFree(image); | 155 KernelImageFree(image); |
| 260 return kernel_blob; | 156 return kernel_blob; |
| 261 } | 157 } |
| OLD | NEW |