| 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 "timer_utils.h" | 15 #include "timer_utils.h" |
| 16 #include "utility.h" | 16 #include "utility.h" |
| 17 | 17 |
| 18 #define FILE_NAME_SIZE 128 | 18 #define FILE_NAME_SIZE 128 |
| 19 #define NUM_OPERATIONS 100 /* Number of verify operations to time. */ | 19 #define NUM_OPERATIONS 100 /* Number of verify operations to time. */ |
| 20 |
| 20 #define FIRMWARE_SIZE_SMALL 512000 | 21 #define FIRMWARE_SIZE_SMALL 512000 |
| 21 #define FIRMWARE_SIZE_MEDIUM 1024000 | 22 #define FIRMWARE_SIZE_MEDIUM 1024000 |
| 22 #define FIRMWARE_SIZE_LARGE 4096000 | 23 #define FIRMWARE_SIZE_LARGE 4096000 |
| 24 const uint64_t g_firmware_sizes_to_test[] = { |
| 25 FIRMWARE_SIZE_SMALL, |
| 26 FIRMWARE_SIZE_MEDIUM, |
| 27 FIRMWARE_SIZE_LARGE |
| 28 }; |
| 29 const char* g_firmware_size_labels[] = { |
| 30 "small", |
| 31 "medium", |
| 32 "large" |
| 33 }; |
| 34 #define NUM_SIZES_TO_TEST (sizeof(g_firmware_sizes_to_test) / \ |
| 35 sizeof(g_firmware_sizes_to_test[0])) |
| 23 | 36 |
| 24 uint8_t* GenerateTestFirmwareBlob(int algorithm, | 37 uint8_t* GenerateTestFirmwareBlob(int algorithm, |
| 25 int firmware_len, | 38 int firmware_len, |
| 26 const uint8_t* firmware_sign_key, | 39 const uint8_t* firmware_sign_key, |
| 27 const char* root_key_file, | 40 const char* root_key_file, |
| 28 const char* firmware_sign_key_file) { | 41 const char* firmware_sign_key_file) { |
| 29 FirmwareImage* image = FirmwareImageNew(); | 42 FirmwareImage* image = FirmwareImageNew(); |
| 30 uint8_t* firmware_blob = NULL; | 43 uint8_t* firmware_blob = NULL; |
| 31 uint64_t firmware_blob_len = 0; | 44 uint64_t firmware_blob_len = 0; |
| 32 | 45 |
| (...skipping 29 matching lines...) Expand all Loading... |
| 62 fprintf(stderr, "Couldn't create firmware and preamble signature.\n"); | 75 fprintf(stderr, "Couldn't create firmware and preamble signature.\n"); |
| 63 FirmwareImageFree(image); | 76 FirmwareImageFree(image); |
| 64 return NULL; | 77 return NULL; |
| 65 } | 78 } |
| 66 firmware_blob = GetFirmwareBlob(image, &firmware_blob_len); | 79 firmware_blob = GetFirmwareBlob(image, &firmware_blob_len); |
| 67 FirmwareImageFree(image); | 80 FirmwareImageFree(image); |
| 68 return firmware_blob; | 81 return firmware_blob; |
| 69 } | 82 } |
| 70 | 83 |
| 71 int SpeedTestAlgorithm(int algorithm) { | 84 int SpeedTestAlgorithm(int algorithm) { |
| 72 int i, key_size, error_code = 0; | 85 int i, j, key_size, error_code = 0; |
| 73 ClockTimerState ct; | 86 ClockTimerState ct; |
| 74 double msecs; | 87 double msecs; |
| 75 uint64_t len; | 88 uint64_t len; |
| 76 uint8_t* firmware_sign_key = NULL; | 89 uint8_t* firmware_sign_key = NULL; |
| 77 uint8_t* root_key_blob = NULL; | 90 uint8_t* root_key_blob = NULL; |
| 78 char firmware_sign_key_file[FILE_NAME_SIZE]; | 91 char firmware_sign_key_file[FILE_NAME_SIZE]; |
| 79 char file_name[FILE_NAME_SIZE]; | 92 char file_name[FILE_NAME_SIZE]; |
| 80 char* sha_strings[] = { /* Maps algorithm->SHA algorithm. */ | 93 char* sha_strings[] = { /* Maps algorithm->SHA algorithm. */ |
| 81 "sha1", "sha256", "sha512", /* RSA-1024 */ | 94 "sha1", "sha256", "sha512", /* RSA-1024 */ |
| 82 "sha1", "sha256", "sha512", /* RSA-2048 */ | 95 "sha1", "sha256", "sha512", /* RSA-2048 */ |
| 83 "sha1", "sha256", "sha512", /* RSA-4096 */ | 96 "sha1", "sha256", "sha512", /* RSA-4096 */ |
| 84 "sha1", "sha256", "sha512", /* RSA-8192 */ | 97 "sha1", "sha256", "sha512", /* RSA-8192 */ |
| 85 }; | 98 }; |
| 86 /* Test for three different firmware sizes. */ | 99 uint8_t* firmware_blobs[NUM_SIZES_TO_TEST]; |
| 87 uint8_t* firmware_blob_small = NULL; | 100 for (i = 0; i < NUM_SIZES_TO_TEST; ++i) |
| 88 uint8_t* firmware_blob_medium = NULL; | 101 firmware_blobs[i] = NULL; |
| 89 uint8_t* firmware_blob_large = NULL; | |
| 90 | 102 |
| 91 key_size = siglen_map[algorithm] * 8; /* in bits. */ | 103 key_size = siglen_map[algorithm] * 8; /* in bits. */ |
| 92 snprintf(firmware_sign_key_file, FILE_NAME_SIZE, "testkeys/key_rsa%d.pem", | 104 snprintf(firmware_sign_key_file, FILE_NAME_SIZE, "testkeys/key_rsa%d.pem", |
| 93 key_size); | 105 key_size); |
| 94 | 106 |
| 95 snprintf(file_name, FILE_NAME_SIZE, "testkeys/key_rsa%d.keyb", key_size); | 107 snprintf(file_name, FILE_NAME_SIZE, "testkeys/key_rsa%d.keyb", key_size); |
| 96 firmware_sign_key = BufferFromFile(file_name, &len); | 108 firmware_sign_key = BufferFromFile(file_name, &len); |
| 97 if (!firmware_sign_key) { | 109 if (!firmware_sign_key) { |
| 98 fprintf(stderr, "Couldn't read pre-processed firmware signing key.\n"); | 110 fprintf(stderr, "Couldn't read pre-processed firmware signing key.\n"); |
| 99 error_code = 1; | 111 error_code = 1; |
| 100 goto cleanup; | 112 goto cleanup; |
| 101 } | 113 } |
| 102 | 114 |
| 103 /* Generate test images. */ | 115 /* Generate test images. */ |
| 104 firmware_blob_small = GenerateTestFirmwareBlob(algorithm, | 116 for (i = 0; i < NUM_SIZES_TO_TEST; ++i) { |
| 105 FIRMWARE_SIZE_SMALL, | 117 firmware_blobs[i] = GenerateTestFirmwareBlob(algorithm, |
| 118 g_firmware_sizes_to_test[i], |
| 106 firmware_sign_key, | 119 firmware_sign_key, |
| 107 "testkeys/key_rsa8192.pem", | 120 "testkeys/key_rsa8192.pem", |
| 108 firmware_sign_key_file); | 121 firmware_sign_key_file); |
| 109 firmware_blob_medium = GenerateTestFirmwareBlob(algorithm, | 122 if (!firmware_blobs[i]) { |
| 110 FIRMWARE_SIZE_MEDIUM, | 123 fprintf(stderr, "Couldn't generate test firmware images.\n"); |
| 111 firmware_sign_key, | 124 error_code = 1; |
| 112 "testkeys/key_rsa8192.pem", | 125 goto cleanup; |
| 113 firmware_sign_key_file); | 126 } |
| 114 firmware_blob_large = GenerateTestFirmwareBlob(algorithm, | |
| 115 FIRMWARE_SIZE_LARGE, | |
| 116 firmware_sign_key, | |
| 117 "testkeys/key_rsa8192.pem", | |
| 118 firmware_sign_key_file); | |
| 119 if (!firmware_blob_small || !firmware_blob_medium || !firmware_blob_large) { | |
| 120 fprintf(stderr, "Couldn't generate test firmware images.\n"); | |
| 121 error_code = 1; | |
| 122 goto cleanup; | |
| 123 } | 127 } |
| 124 | 128 |
| 129 /* Get pre-processed key used for verification. */ |
| 125 root_key_blob = BufferFromFile("testkeys/key_rsa8192.keyb", &len); | 130 root_key_blob = BufferFromFile("testkeys/key_rsa8192.keyb", &len); |
| 126 if (!root_key_blob) { | 131 if (!root_key_blob) { |
| 127 fprintf(stderr, "Couldn't read pre-processed rootkey.\n"); | 132 fprintf(stderr, "Couldn't read pre-processed rootkey.\n"); |
| 128 error_code = 1; | 133 error_code = 1; |
| 129 goto cleanup; | 134 goto cleanup; |
| 130 } | 135 } |
| 131 | 136 |
| 132 /* Small.*/ | 137 /* Now run the timing tests. */ |
| 133 StartTimer(&ct); | 138 for (i = 0; i < NUM_SIZES_TO_TEST; ++i) { |
| 134 for (i = 0; i < NUM_OPERATIONS; ++i) { | 139 StartTimer(&ct); |
| 135 if (VERIFY_FIRMWARE_SUCCESS != | 140 for (j = 0; j < NUM_OPERATIONS; ++j) { |
| 136 VerifyFirmware(root_key_blob, firmware_blob_small, 0)) | 141 if (VERIFY_FIRMWARE_SUCCESS != |
| 137 fprintf(stderr, "Warning: Firmware Verification Failed.\n"); | 142 VerifyFirmware(root_key_blob, firmware_blobs[i], 0)) |
| 143 fprintf(stderr, "Warning: Firmware Verification Failed.\n"); |
| 144 } |
| 145 StopTimer(&ct); |
| 146 msecs = (float) GetDurationMsecs(&ct) / NUM_OPERATIONS; |
| 147 fprintf(stderr, |
| 148 "# Firmware (%s, Algo = %s):" |
| 149 "\t%.02f ms/verification\n", |
| 150 g_firmware_size_labels[i], |
| 151 algo_strings[algorithm], |
| 152 msecs); |
| 153 fprintf(stdout, "ms_firmware_%s_rsa%d_%s:%.02f\n", |
| 154 g_firmware_size_labels[i], |
| 155 key_size, |
| 156 sha_strings[algorithm], |
| 157 msecs); |
| 138 } | 158 } |
| 139 StopTimer(&ct); | |
| 140 msecs = (float) GetDurationMsecs(&ct) / NUM_OPERATIONS; | |
| 141 fprintf(stderr, | |
| 142 "# Firmware (Small, Algo = %s):" | |
| 143 "\t%.02f ms/verification\n", | |
| 144 algo_strings[algorithm], msecs); | |
| 145 fprintf(stdout, "ms_firmware_sm_rsa%d_%s:%.02f\n", | |
| 146 key_size, | |
| 147 sha_strings[algorithm], | |
| 148 msecs); | |
| 149 | |
| 150 /* Medium. */ | |
| 151 StartTimer(&ct); | |
| 152 for (i = 0; i < NUM_OPERATIONS; ++i) { | |
| 153 if (VERIFY_FIRMWARE_SUCCESS != | |
| 154 VerifyFirmware(root_key_blob, firmware_blob_medium, 0)) | |
| 155 fprintf(stderr, "Warning: Firmware Verification Failed.\n"); | |
| 156 } | |
| 157 StopTimer(&ct); | |
| 158 msecs = (float) GetDurationMsecs(&ct) / NUM_OPERATIONS; | |
| 159 fprintf(stderr, | |
| 160 "# Firmware (Medium, Algo = %s):" | |
| 161 "\t%.02f ms/verification\n", | |
| 162 algo_strings[algorithm], msecs); | |
| 163 fprintf(stdout, "ms_firmware_med_rsa%d_%s:%.02f\n", | |
| 164 key_size, | |
| 165 sha_strings[algorithm], | |
| 166 msecs); | |
| 167 | |
| 168 | |
| 169 /* Large */ | |
| 170 StartTimer(&ct); | |
| 171 for (i = 0; i < NUM_OPERATIONS; ++i) { | |
| 172 if (VERIFY_FIRMWARE_SUCCESS != | |
| 173 VerifyFirmware(root_key_blob, firmware_blob_large, 0)) | |
| 174 fprintf(stderr, "Warning: Firmware Verification Failed.\n"); | |
| 175 } | |
| 176 StopTimer(&ct); | |
| 177 msecs = (float) GetDurationMsecs(&ct) / NUM_OPERATIONS; | |
| 178 fprintf(stderr, | |
| 179 "# Firmware (Large, Algorithm = %s):" | |
| 180 "\t%.02f ms/verification\n", | |
| 181 algo_strings[algorithm], msecs); | |
| 182 fprintf(stdout, "ms_firmware_large_rsa%d_%s:%.02f\n", | |
| 183 key_size, | |
| 184 sha_strings[algorithm], | |
| 185 msecs); | |
| 186 | 159 |
| 187 cleanup: | 160 cleanup: |
| 161 for (i = 0; i < NUM_SIZES_TO_TEST; i++) |
| 162 Free(firmware_blobs[i]); |
| 188 Free(root_key_blob); | 163 Free(root_key_blob); |
| 189 Free(firmware_blob_large); | |
| 190 Free(firmware_blob_medium); | |
| 191 Free(firmware_blob_small); | |
| 192 return error_code; | 164 return error_code; |
| 193 } | 165 } |
| 194 | 166 |
| 195 | 167 |
| 196 int main(int argc, char* argv[]) { | 168 int main(int argc, char* argv[]) { |
| 197 int i, error_code = 0; | 169 int i, error_code = 0; |
| 198 for (i = 0; i < kNumAlgorithms; ++i) { | 170 for (i = 0; i < kNumAlgorithms; ++i) { |
| 199 if (0 != (error_code = SpeedTestAlgorithm(i))) | 171 if (0 != (error_code = SpeedTestAlgorithm(i))) |
| 200 return error_code; | 172 return error_code; |
| 201 } | 173 } |
| 202 return 0; | 174 return 0; |
| 203 } | 175 } |
| OLD | NEW |