| 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 * Timing benchmark for verifying a firmware image. | 5 * Timing benchmark for verifying a firmware image. |
| 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 "padding.h" | 13 #include "padding.h" |
| 14 #include "rsa_utility.h" | 14 #include "rsa_utility.h" |
| 15 #include "test_common.h" |
| 15 #include "timer_utils.h" | 16 #include "timer_utils.h" |
| 16 #include "utility.h" | 17 #include "utility.h" |
| 17 | 18 |
| 18 #define FILE_NAME_SIZE 128 | 19 #define FILE_NAME_SIZE 128 |
| 19 #define NUM_OPERATIONS 100 /* Number of verify operations to time. */ | 20 #define NUM_OPERATIONS 100 /* Number of verify operations to time. */ |
| 20 | 21 |
| 21 #define FIRMWARE_SIZE_SMALL 512000 | 22 #define FIRMWARE_SIZE_SMALL 512000 |
| 22 #define FIRMWARE_SIZE_MEDIUM 1024000 | 23 #define FIRMWARE_SIZE_MEDIUM 1024000 |
| 23 #define FIRMWARE_SIZE_LARGE 4096000 | 24 #define FIRMWARE_SIZE_LARGE 4096000 |
| 24 const uint64_t g_firmware_sizes_to_test[] = { | 25 const uint64_t g_firmware_sizes_to_test[] = { |
| 25 FIRMWARE_SIZE_SMALL, | 26 FIRMWARE_SIZE_SMALL, |
| 26 FIRMWARE_SIZE_MEDIUM, | 27 FIRMWARE_SIZE_MEDIUM, |
| 27 FIRMWARE_SIZE_LARGE | 28 FIRMWARE_SIZE_LARGE |
| 28 }; | 29 }; |
| 29 const char* g_firmware_size_labels[] = { | 30 const char* g_firmware_size_labels[] = { |
| 30 "small", | 31 "small", |
| 31 "medium", | 32 "medium", |
| 32 "large" | 33 "large" |
| 33 }; | 34 }; |
| 34 #define NUM_SIZES_TO_TEST (sizeof(g_firmware_sizes_to_test) / \ | 35 #define NUM_SIZES_TO_TEST (sizeof(g_firmware_sizes_to_test) / \ |
| 35 sizeof(g_firmware_sizes_to_test[0])) | 36 sizeof(g_firmware_sizes_to_test[0])) |
| 36 | 37 |
| 37 uint8_t* GenerateTestFirmwareBlob(int algorithm, | |
| 38 int firmware_len, | |
| 39 const uint8_t* firmware_sign_key, | |
| 40 const char* root_key_file, | |
| 41 const char* firmware_sign_key_file) { | |
| 42 FirmwareImage* image = FirmwareImageNew(); | |
| 43 uint8_t* firmware_blob = NULL; | |
| 44 uint64_t firmware_blob_len = 0; | |
| 45 | |
| 46 Memcpy(image->magic, FIRMWARE_MAGIC, FIRMWARE_MAGIC_SIZE); | |
| 47 image->firmware_sign_algorithm = algorithm; | |
| 48 image->firmware_sign_key = (uint8_t*) Malloc( | |
| 49 RSAProcessedKeySize(image->firmware_sign_algorithm)); | |
| 50 Memcpy(image->firmware_sign_key, firmware_sign_key, | |
| 51 RSAProcessedKeySize(image->firmware_sign_algorithm)); | |
| 52 image->firmware_key_version = 1; | |
| 53 | |
| 54 /* Update correct header length. */ | |
| 55 image->header_len = GetFirmwareHeaderLen(image); | |
| 56 | |
| 57 /* Calculate SHA-512 digest on header and populate header_checksum. */ | |
| 58 CalculateFirmwareHeaderChecksum(image, image->header_checksum); | |
| 59 | |
| 60 /* Populate firmware and preamble with dummy data. */ | |
| 61 image->firmware_version = 1; | |
| 62 image->firmware_len = firmware_len; | |
| 63 image->preamble_signature = image->firmware_signature = NULL; | |
| 64 Memset(image->preamble, 'P', FIRMWARE_PREAMBLE_SIZE); | |
| 65 image->firmware_data = Malloc(image->firmware_len); | |
| 66 Memset(image->firmware_data, 'F', image->firmware_len); | |
| 67 | |
| 68 if (!AddFirmwareKeySignature(image, root_key_file)) { | |
| 69 fprintf(stderr, "Couldn't create key signature.\n"); | |
| 70 FirmwareImageFree(image); | |
| 71 return NULL; | |
| 72 } | |
| 73 | |
| 74 if (!AddFirmwareSignature(image, firmware_sign_key_file)) { | |
| 75 fprintf(stderr, "Couldn't create firmware and preamble signature.\n"); | |
| 76 FirmwareImageFree(image); | |
| 77 return NULL; | |
| 78 } | |
| 79 firmware_blob = GetFirmwareBlob(image, &firmware_blob_len); | |
| 80 FirmwareImageFree(image); | |
| 81 return firmware_blob; | |
| 82 } | |
| 83 | |
| 84 int SpeedTestAlgorithm(int algorithm) { | 38 int SpeedTestAlgorithm(int algorithm) { |
| 85 int i, j, key_size, error_code = 0; | 39 int i, j, key_size, error_code = 0; |
| 86 ClockTimerState ct; | 40 ClockTimerState ct; |
| 87 double msecs; | 41 double msecs; |
| 88 uint64_t len; | 42 uint64_t len; |
| 89 uint8_t* firmware_sign_key = NULL; | 43 uint8_t* firmware_sign_key = NULL; |
| 90 uint8_t* root_key_blob = NULL; | 44 uint8_t* root_key_blob = NULL; |
| 91 char firmware_sign_key_file[FILE_NAME_SIZE]; | 45 char firmware_sign_key_file[FILE_NAME_SIZE]; |
| 92 char file_name[FILE_NAME_SIZE]; | 46 char file_name[FILE_NAME_SIZE]; |
| 93 char* sha_strings[] = { /* Maps algorithm->SHA algorithm. */ | 47 char* sha_strings[] = { /* Maps algorithm->SHA algorithm. */ |
| (...skipping 14 matching lines...) Expand all Loading... |
| 108 firmware_sign_key = BufferFromFile(file_name, &len); | 62 firmware_sign_key = BufferFromFile(file_name, &len); |
| 109 if (!firmware_sign_key) { | 63 if (!firmware_sign_key) { |
| 110 fprintf(stderr, "Couldn't read pre-processed firmware signing key.\n"); | 64 fprintf(stderr, "Couldn't read pre-processed firmware signing key.\n"); |
| 111 error_code = 1; | 65 error_code = 1; |
| 112 goto cleanup; | 66 goto cleanup; |
| 113 } | 67 } |
| 114 | 68 |
| 115 /* Generate test images. */ | 69 /* Generate test images. */ |
| 116 for (i = 0; i < NUM_SIZES_TO_TEST; ++i) { | 70 for (i = 0; i < NUM_SIZES_TO_TEST; ++i) { |
| 117 firmware_blobs[i] = GenerateTestFirmwareBlob(algorithm, | 71 firmware_blobs[i] = GenerateTestFirmwareBlob(algorithm, |
| 72 firmware_sign_key, |
| 73 1, /* firmware key version. */ |
| 74 1, /* firmware version. */ |
| 118 g_firmware_sizes_to_test[i], | 75 g_firmware_sizes_to_test[i], |
| 119 firmware_sign_key, | |
| 120 "testkeys/key_rsa8192.pem", | 76 "testkeys/key_rsa8192.pem", |
| 121 firmware_sign_key_file); | 77 firmware_sign_key_file); |
| 122 if (!firmware_blobs[i]) { | 78 if (!firmware_blobs[i]) { |
| 123 fprintf(stderr, "Couldn't generate test firmware images.\n"); | 79 fprintf(stderr, "Couldn't generate test firmware images.\n"); |
| 124 error_code = 1; | 80 error_code = 1; |
| 125 goto cleanup; | 81 goto cleanup; |
| 126 } | 82 } |
| 127 } | 83 } |
| 128 | 84 |
| 129 /* Get pre-processed key used for verification. */ | 85 /* Get pre-processed key used for verification. */ |
| 130 root_key_blob = BufferFromFile("testkeys/key_rsa8192.keyb", &len); | 86 root_key_blob = BufferFromFile("testkeys/key_rsa8192.keyb", &len); |
| 131 if (!root_key_blob) { | 87 if (!root_key_blob) { |
| 132 fprintf(stderr, "Couldn't read pre-processed rootkey.\n"); | 88 fprintf(stderr, "Couldn't read pre-processed rootkey.\n"); |
| 133 error_code = 1; | 89 error_code = 1; |
| 134 goto cleanup; | 90 goto cleanup; |
| 135 } | 91 } |
| 136 | 92 |
| 137 /* Now run the timing tests. */ | 93 /* Now run the timing tests. */ |
| 138 for (i = 0; i < NUM_SIZES_TO_TEST; ++i) { | 94 for (i = 0; i < NUM_SIZES_TO_TEST; ++i) { |
| 139 StartTimer(&ct); | 95 StartTimer(&ct); |
| 140 for (j = 0; j < NUM_OPERATIONS; ++j) { | 96 for (j = 0; j < NUM_OPERATIONS; ++j) { |
| 141 if (VERIFY_FIRMWARE_SUCCESS != | 97 if (VERIFY_FIRMWARE_SUCCESS != |
| 142 VerifyFirmware(root_key_blob, firmware_blobs[i], 0)) | 98 VerifyFirmware(root_key_blob, firmware_blobs[i])) |
| 143 fprintf(stderr, "Warning: Firmware Verification Failed.\n"); | 99 fprintf(stderr, "Warning: Firmware Verification Failed.\n"); |
| 144 } | 100 } |
| 145 StopTimer(&ct); | 101 StopTimer(&ct); |
| 146 msecs = (float) GetDurationMsecs(&ct) / NUM_OPERATIONS; | 102 msecs = (float) GetDurationMsecs(&ct) / NUM_OPERATIONS; |
| 147 fprintf(stderr, | 103 fprintf(stderr, |
| 148 "# Firmware (%s, Algo = %s):" | 104 "# Firmware (%s, Algo = %s):" |
| 149 "\t%.02f ms/verification\n", | 105 "\t%.02f ms/verification\n", |
| 150 g_firmware_size_labels[i], | 106 g_firmware_size_labels[i], |
| 151 algo_strings[algorithm], | 107 algo_strings[algorithm], |
| 152 msecs); | 108 msecs); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 166 | 122 |
| 167 | 123 |
| 168 int main(int argc, char* argv[]) { | 124 int main(int argc, char* argv[]) { |
| 169 int i, error_code = 0; | 125 int i, error_code = 0; |
| 170 for (i = 0; i < kNumAlgorithms; ++i) { | 126 for (i = 0; i < kNumAlgorithms; ++i) { |
| 171 if (0 != (error_code = SpeedTestAlgorithm(i))) | 127 if (0 != (error_code = SpeedTestAlgorithm(i))) |
| 172 return error_code; | 128 return error_code; |
| 173 } | 129 } |
| 174 return 0; | 130 return 0; |
| 175 } | 131 } |
| OLD | NEW |