| 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 * Data structure and API definitions for a verified boot firmware image. | 5 * API definitions for a verified boot firmware image. |
| 6 * (Userland Portion) |
| 6 */ | 7 */ |
| 7 | 8 |
| 8 #ifndef VBOOT_REFERENCE_FIRMWARE_IMAGE_H_ | 9 #ifndef VBOOT_REFERENCE_FIRMWARE_IMAGE_H_ |
| 9 #define VBOOT_REFERENCE_FIRMWARE_IMAGE_H_ | 10 #define VBOOT_REFERENCE_FIRMWARE_IMAGE_H_ |
| 10 | 11 |
| 11 #include <inttypes.h> | 12 #include "firmware_image_fw.h" |
| 12 | |
| 13 #include "rsa.h" | |
| 14 #include "sha.h" | |
| 15 | |
| 16 #define FIRMWARE_MAGIC "CHROMEOS" | |
| 17 #define FIRMWARE_MAGIC_SIZE 8 | |
| 18 #define FIRMWARE_PREAMBLE_SIZE 8 | |
| 19 | |
| 20 /* RSA 8192 and SHA-512. */ | |
| 21 #define ROOT_SIGNATURE_ALGORITHM 11 | |
| 22 #define ROOT_SIGNATURE_ALGORITHM_STRING "11" | |
| 23 | |
| 24 typedef struct FirmwareImage { | |
| 25 uint8_t magic[FIRMWARE_MAGIC_SIZE]; | |
| 26 /* Key Header */ | |
| 27 uint16_t header_len; /* Length of the header. */ | |
| 28 uint16_t firmware_sign_algorithm; /* Signature algorithm used by the signing | |
| 29 * key. */ | |
| 30 uint16_t firmware_key_version; /* Key Version# for preventing rollbacks. */ | |
| 31 uint8_t* firmware_sign_key; /* Pre-processed public half of signing key. */ | |
| 32 uint8_t header_checksum[SHA512_DIGEST_SIZE]; /* SHA-512 hash of the header.*/ | |
| 33 | |
| 34 uint8_t firmware_key_signature[RSA8192NUMBYTES]; /* Signature of the header | |
| 35 * above. */ | |
| 36 | |
| 37 /* Firmware Preamble. */ | |
| 38 uint16_t firmware_version; /* Firmware Version# for preventing rollbacks.*/ | |
| 39 uint64_t firmware_len; /* Length of the rest of the R/W firmware data. */ | |
| 40 uint8_t preamble[FIRMWARE_PREAMBLE_SIZE]; /* Remaining preamble data.*/ | |
| 41 | |
| 42 uint8_t* preamble_signature; /* Signature over the preamble. */ | |
| 43 | |
| 44 /* The firmware signature comes first as it may allow us to parallelize | |
| 45 * the firmware data fetch and RSA public operation. | |
| 46 */ | |
| 47 uint8_t* firmware_signature; /* Signature on the Preamble + | |
| 48 [firmware_data]. */ | |
| 49 uint8_t* firmware_data; /* Rest of firmware data */ | |
| 50 | |
| 51 } FirmwareImage; | |
| 52 | 13 |
| 53 /* Allocate and return a new FirmwareImage structure. */ | 14 /* Allocate and return a new FirmwareImage structure. */ |
| 54 FirmwareImage* FirmwareImageNew(void); | 15 FirmwareImage* FirmwareImageNew(void); |
| 55 | 16 |
| 56 /* Deep free the contents of [fw]. */ | 17 /* Deep free the contents of [fw]. */ |
| 57 void FirmwareImageFree(FirmwareImage* fw); | 18 void FirmwareImageFree(FirmwareImage* fw); |
| 58 | 19 |
| 59 /* Read firmware data from file named [input_file]. | 20 /* Read firmware data from file named [input_file]. |
| 60 * | 21 * |
| 61 * Returns a filled up FirmwareImage structure on success, NULL on error. | 22 * Returns a filled up FirmwareImage structure on success, NULL on error. |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 93 */ | 54 */ |
| 94 uint8_t* GetFirmwareBlob(const FirmwareImage* image, uint64_t* blob_len); | 55 uint8_t* GetFirmwareBlob(const FirmwareImage* image, uint64_t* blob_len); |
| 95 | 56 |
| 96 /* Write firmware data from [image] into a file named [input_file]. | 57 /* Write firmware data from [image] into a file named [input_file]. |
| 97 * | 58 * |
| 98 * Return 1 on success, 0 on failure. | 59 * Return 1 on success, 0 on failure. |
| 99 */ | 60 */ |
| 100 int WriteFirmwareImage(const char* input_file, | 61 int WriteFirmwareImage(const char* input_file, |
| 101 const FirmwareImage* image); | 62 const FirmwareImage* image); |
| 102 | 63 |
| 103 | |
| 104 /* Pretty print the contents of [image]. Only headers and metadata information | 64 /* Pretty print the contents of [image]. Only headers and metadata information |
| 105 * is printed. | 65 * is printed. |
| 106 */ | 66 */ |
| 107 void PrintFirmwareImage(const FirmwareImage* image); | 67 void PrintFirmwareImage(const FirmwareImage* image); |
| 108 | 68 |
| 109 /* Error Codes for VerifyFirmware* family of functions. */ | |
| 110 #define VERIFY_FIRMWARE_SUCCESS 0 | |
| 111 #define VERIFY_FIRMWARE_INVALID_IMAGE 1 | |
| 112 #define VERIFY_FIRMWARE_ROOT_SIGNATURE_FAILED 2 | |
| 113 #define VERIFY_FIRMWARE_INVALID_ALGORITHM 3 | |
| 114 #define VERIFY_FIRMWARE_PREAMBLE_SIGNATURE_FAILED 4 | |
| 115 #define VERIFY_FIRMWARE_SIGNATURE_FAILED 5 | |
| 116 #define VERIFY_FIRMWARE_WRONG_MAGIC 6 | |
| 117 #define VERIFY_FIRMWARE_WRONG_HEADER_CHECKSUM 7 | |
| 118 #define VERIFY_FIRMWARE_KEY_ROLLBACK 8 | |
| 119 #define VERIFY_FIRMWARE_VERSION_ROLLBACK 9 | |
| 120 #define VERIFY_FIRMWARE_MAX 10 /* Total number of error codes. */ | |
| 121 | |
| 122 extern char* kVerifyFirmwareErrors[VERIFY_FIRMWARE_MAX]; | |
| 123 | |
| 124 /* Checks for the sanity of the firmware header pointed by [header_blob]. | |
| 125 * | |
| 126 * On success, put signature algorithm in [algorithm], header length | |
| 127 * in [header_len], and return 0. | |
| 128 * Else, return error code on failure. | |
| 129 */ | |
| 130 int VerifyFirmwareHeader(const uint8_t* root_key_blob, | |
| 131 const uint8_t* header_blob, | |
| 132 int* algorithm, | |
| 133 int* header_len); | |
| 134 | |
| 135 /* Checks the preamble signature on firmware preamble pointed by | |
| 136 * [preamble_blob] using the signing key [sign_key]. | |
| 137 * | |
| 138 * On success, put firmware length into [firmware_len], and return 0. | |
| 139 * Else, return error code on failure. | |
| 140 */ | |
| 141 int VerifyFirmwarePreamble(RSAPublicKey* sign_key, | |
| 142 const uint8_t* preamble_blob, | |
| 143 int algorithm, | |
| 144 uint64_t* firmware_len); | |
| 145 | |
| 146 /* Checks the signature on the preamble + firmware data at | |
| 147 * [preamble_start] and [firmware_data_start]. | |
| 148 * The length of the actual firmware data is firmware_len and it is assumed to | |
| 149 * be prepended with the signature whose size depends on the signature_algorithm | |
| 150 * [algorithm]. This signature also covers the preamble data (but not the | |
| 151 * preamble signature itself). | |
| 152 * | |
| 153 * Return 0 on success, error code on failure. | |
| 154 */ | |
| 155 int VerifyFirmwareData(RSAPublicKey* sign_key, | |
| 156 const uint8_t* preamble_start, | |
| 157 const uint8_t* firmware_data_start, | |
| 158 uint64_t firmware_len, | |
| 159 int algorithm); | |
| 160 | |
| 161 /* Performs a chained verify of the firmware blob [firmware_blob]. | |
| 162 * | |
| 163 * Returns 0 on success, error code on failure. | |
| 164 * | |
| 165 * NOTE: The length of the firmware blob is derived from reading the fields | |
| 166 * in the first few bytes of the buffer. This might look risky but in firmware | |
| 167 * land, the start address of the firmware_blob will always be fixed depending | |
| 168 * on the memory map on the particular platform. In addition, the signature on | |
| 169 * length itself is checked early in the verification process for extra safety. | |
| 170 */ | |
| 171 int VerifyFirmware(const uint8_t* root_key_blob, | |
| 172 const uint8_t* firmware_blob); | |
| 173 | |
| 174 /* Performs a chained verify of the firmware [image]. | 69 /* Performs a chained verify of the firmware [image]. |
| 175 * | 70 * |
| 176 * Returns 0 on success, error code on failure. | 71 * Returns 0 on success, error code on failure. |
| 177 */ | 72 */ |
| 178 int VerifyFirmwareImage(const RSAPublicKey* root_key, | 73 int VerifyFirmwareImage(const RSAPublicKey* root_key, |
| 179 const FirmwareImage* image); | 74 const FirmwareImage* image); |
| 180 | 75 |
| 181 /* Maps error codes from VerifyFirmware() to error description. */ | 76 /* Maps error codes from VerifyFirmware() to error description. */ |
| 182 const char* VerifyFirmwareErrorString(int error); | 77 const char* VerifyFirmwareErrorString(int error); |
| 183 | 78 |
| 184 /* Add a root key signature to the key header to a firmware image [image] | 79 /* Add a root key signature to the key header to a firmware image [image] |
| 185 * using the private root key in file [root_key_file]. | 80 * using the private root key in file [root_key_file]. |
| 186 * | 81 * |
| 187 * Return 1 on success, 0 on failure. | 82 * Return 1 on success, 0 on failure. |
| 188 */ | 83 */ |
| 189 int AddFirmwareKeySignature(FirmwareImage* image, const char* root_key_file); | 84 int AddFirmwareKeySignature(FirmwareImage* image, const char* root_key_file); |
| 190 | 85 |
| 191 /* Add firmware and preamble signature to a firmware image [image] | 86 /* Add firmware and preamble signature to a firmware image [image] |
| 192 * using the private signing key in file [signing_key_file]. | 87 * using the private signing key in file [signing_key_file]. |
| 193 * | 88 * |
| 194 * Return 1 on success, 0 on failure. | 89 * Return 1 on success, 0 on failure. |
| 195 */ | 90 */ |
| 196 int AddFirmwareSignature(FirmwareImage* image, const char* signing_key_file); | 91 int AddFirmwareSignature(FirmwareImage* image, const char* signing_key_file); |
| 197 | 92 |
| 198 /* Returns the logical version of a firmware blob which is calculated as | |
| 199 * (firmware_key_version << 16 | firmware_version). */ | |
| 200 uint32_t GetLogicalFirmwareVersion(uint8_t* firmware_blob); | |
| 201 | |
| 202 #define BOOT_FIRMWARE_A_CONTINUE 1 | |
| 203 #define BOOT_FIRMWARE_B_CONTINUE 2 | |
| 204 #define BOOT_FIRMWARE_RECOVERY_CONTINUE 3 | |
| 205 | |
| 206 /* This function is the driver used by the RO firmware to | |
| 207 * determine which copy of the firmware to boot from. It performs | |
| 208 * the requisite rollback index checking, including updating them, | |
| 209 * if required. | |
| 210 * | |
| 211 * Returns the code path to follow. It is one of: | |
| 212 * BOOT_FIRMWARE_A_CONTINUE Boot from Firmware A | |
| 213 * BOOT_FIRMWARE_B_CONTINUE Boot from Firmware B | |
| 214 * BOOT_FIRMWARE_RECOVERY_CONTINUE Jump to recovery mode | |
| 215 */ | |
| 216 int VerifyFirmwareDriver_f(uint8_t* root_key_blob, | |
| 217 uint8_t* firmwareA, | |
| 218 uint8_t* firmwareB); | |
| 219 | |
| 220 #endif /* VBOOT_REFERENCE_FIRMWARE_IMAGE_H_ */ | 93 #endif /* VBOOT_REFERENCE_FIRMWARE_IMAGE_H_ */ |
| OLD | NEW |