| 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 26 matching lines...) Expand all Loading... |
| 37 if (result != not_expected_result) { | 37 if (result != not_expected_result) { |
| 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 | |
| 48 KernelImage* GenerateTestKernelImage(int firmware_sign_algorithm, | |
| 49 int kernel_sign_algorithm, | |
| 50 const uint8_t* kernel_sign_key, | |
| 51 int kernel_key_version, | |
| 52 int kernel_version, | |
| 53 uint64_t kernel_len, | |
| 54 const char* firmware_key_file, | |
| 55 const char* kernel_key_file, | |
| 56 uint8_t kernel_data_fill_char) { | |
| 57 KernelImage* image = KernelImageNew(); | |
| 58 | |
| 59 Memcpy(image->magic, KERNEL_MAGIC, KERNEL_MAGIC_SIZE); | |
| 60 image->header_version = 1; | |
| 61 image->firmware_sign_algorithm = firmware_sign_algorithm; | |
| 62 image->kernel_sign_algorithm = kernel_sign_algorithm; | |
| 63 image->kernel_key_version = kernel_key_version; | |
| 64 image->kernel_sign_key = (uint8_t*) Malloc( | |
| 65 RSAProcessedKeySize(image->kernel_sign_algorithm)); | |
| 66 Memcpy(image->kernel_sign_key, kernel_sign_key, | |
| 67 RSAProcessedKeySize(image->kernel_sign_algorithm)); | |
| 68 | |
| 69 /* Update correct header length. */ | |
| 70 image->header_len = GetKernelHeaderLen(image); | |
| 71 | |
| 72 /* Calculate SHA-512 digest on header and populate header_checksum. */ | |
| 73 CalculateKernelHeaderChecksum(image, image->header_checksum); | |
| 74 | |
| 75 /* Populate kernel options and data with dummy data. */ | |
| 76 image->kernel_version = kernel_version; | |
| 77 image->bootloader_offset = 0; | |
| 78 image->bootloader_size = 512; | |
| 79 image->padded_header_size = 100; | |
| 80 image->kernel_len = kernel_len; | |
| 81 image->kernel_key_signature = image->kernel_signature = NULL; | |
| 82 image->kernel_data = Malloc(kernel_len); | |
| 83 Memset(image->kernel_data, kernel_data_fill_char, kernel_len); | |
| 84 | |
| 85 /* Generate and populate signatures. */ | |
| 86 if (!AddKernelKeySignature(image, firmware_key_file)) { | |
| 87 debug("Couldn't create key signature.\n"); | |
| 88 KernelImageFree(image); | |
| 89 return NULL; | |
| 90 } | |
| 91 | |
| 92 if (!AddKernelSignature(image, kernel_key_file)) { | |
| 93 debug("Couldn't create kernel option and kernel signature.\n"); | |
| 94 KernelImageFree(image); | |
| 95 return NULL; | |
| 96 } | |
| 97 | |
| 98 return image; | |
| 99 } | |
| 100 | |
| 101 uint8_t* GenerateTestKernelBlob(int firmware_sign_algorithm, | |
| 102 int kernel_sign_algorithm, | |
| 103 const uint8_t* kernel_sign_key, | |
| 104 int kernel_key_version, | |
| 105 int kernel_version, | |
| 106 uint64_t kernel_len, | |
| 107 const char* firmware_key_file, | |
| 108 const char* kernel_key_file) { | |
| 109 KernelImage* image = NULL; | |
| 110 uint8_t* kernel_blob = NULL; | |
| 111 uint64_t kernel_blob_len = 0; | |
| 112 | |
| 113 image = GenerateTestKernelImage(firmware_sign_algorithm, | |
| 114 kernel_sign_algorithm, | |
| 115 kernel_sign_key, | |
| 116 kernel_key_version, | |
| 117 kernel_version, | |
| 118 kernel_len, | |
| 119 firmware_key_file, | |
| 120 kernel_key_file, | |
| 121 'K'); | |
| 122 | |
| 123 kernel_blob = GetKernelBlob(image, &kernel_blob_len); | |
| 124 KernelImageFree(image); | |
| 125 return kernel_blob; | |
| 126 } | |
| 127 | |
| 128 uint8_t* GenerateRollbackTestKernelBlob(int kernel_key_version, | |
| 129 int kernel_version, | |
| 130 int is_corrupt) { | |
| 131 KernelImage* image = NULL; | |
| 132 uint64_t len; | |
| 133 uint8_t* kernel_blob = NULL; | |
| 134 uint8_t* kernel_sign_key = NULL; | |
| 135 kernel_sign_key = BufferFromFile("testkeys/key_rsa1024.keyb", | |
| 136 &len); | |
| 137 if (!kernel_sign_key) | |
| 138 return NULL; | |
| 139 image = GenerateTestKernelImage(0, /* Firmware algo: RSA1024/SHA1 */ | |
| 140 0, /* Kernel algo: RSA1024/SHA1 */ | |
| 141 kernel_sign_key, | |
| 142 kernel_key_version, | |
| 143 kernel_version, | |
| 144 1, /* kernel length. */ | |
| 145 "testkeys/key_rsa1024.pem", | |
| 146 "testkeys/key_rsa1024.pem", | |
| 147 'K'); | |
| 148 if (!image) | |
| 149 return NULL; | |
| 150 if (is_corrupt) { | |
| 151 /* Invalidate image. */ | |
| 152 Memset(image->kernel_data, 'X', image->kernel_len); | |
| 153 } | |
| 154 kernel_blob = GetKernelBlob(image, &len); | |
| 155 KernelImageFree(image); | |
| 156 return kernel_blob; | |
| 157 } | |
| OLD | NEW |