|
| 1 | +#define WOLFBOOT_UPDATE_DISK |
| 2 | +#define WOLFBOOT_SKIP_BOOT_VERIFY |
| 3 | +#define EXT_ENCRYPTED |
| 4 | +#define ENCRYPT_WITH_CHACHA |
| 5 | +#define HAVE_CHACHA |
| 6 | +#define IMAGE_HEADER_SIZE 256 |
| 7 | +#define BOOT_PART_A 0 |
| 8 | +#define BOOT_PART_B 1 |
| 9 | + |
| 10 | +#include <stdio.h> |
| 11 | +#include <stdint.h> |
| 12 | +#include <string.h> |
| 13 | +#include <check.h> |
| 14 | + |
| 15 | +#include "target.h" |
| 16 | +#include "wolfboot/wolfboot.h" |
| 17 | +#include "image.h" |
| 18 | +#include "loader.h" |
| 19 | +#include <wolfssl/wolfcrypt/chacha.h> |
| 20 | + |
| 21 | +#define TEST_PAYLOAD_SIZE 64 |
| 22 | + |
| 23 | +static uint8_t load_buffer[TEST_PAYLOAD_SIZE]; |
| 24 | +#define WOLFBOOT_LOAD_ADDRESS ((uintptr_t)load_buffer) |
| 25 | + |
| 26 | +static uint8_t part_a_image[IMAGE_HEADER_SIZE + TEST_PAYLOAD_SIZE]; |
| 27 | +static uint8_t part_b_image[IMAGE_HEADER_SIZE + TEST_PAYLOAD_SIZE]; |
| 28 | +static int mock_disk_init_ret; |
| 29 | +static int mock_disk_close_called; |
| 30 | +static int mock_do_boot_called; |
| 31 | +static const uint32_t *mock_boot_address; |
| 32 | + |
| 33 | +ChaCha chacha; |
| 34 | + |
| 35 | +static void set_u16_le(uint8_t *dst, uint16_t value) |
| 36 | +{ |
| 37 | + dst[0] = (uint8_t)(value & 0xFF); |
| 38 | + dst[1] = (uint8_t)(value >> 8); |
| 39 | +} |
| 40 | + |
| 41 | +static void set_u32_le(uint8_t *dst, uint32_t value) |
| 42 | +{ |
| 43 | + dst[0] = (uint8_t)(value & 0xFF); |
| 44 | + dst[1] = (uint8_t)((value >> 8) & 0xFF); |
| 45 | + dst[2] = (uint8_t)((value >> 16) & 0xFF); |
| 46 | + dst[3] = (uint8_t)(value >> 24); |
| 47 | +} |
| 48 | + |
| 49 | +static void build_image(uint8_t *image, uint32_t version, uint8_t fill) |
| 50 | +{ |
| 51 | + memset(image, 0, IMAGE_HEADER_SIZE + TEST_PAYLOAD_SIZE); |
| 52 | + set_u32_le(image, WOLFBOOT_MAGIC); |
| 53 | + set_u32_le(image + sizeof(uint32_t), TEST_PAYLOAD_SIZE); |
| 54 | + set_u16_le(image + IMAGE_HEADER_OFFSET, HDR_VERSION); |
| 55 | + set_u16_le(image + IMAGE_HEADER_OFFSET + sizeof(uint16_t), 4); |
| 56 | + set_u32_le(image + IMAGE_HEADER_OFFSET + 2 * sizeof(uint16_t), version); |
| 57 | + memset(image + IMAGE_HEADER_SIZE, fill, TEST_PAYLOAD_SIZE); |
| 58 | +} |
| 59 | + |
| 60 | +static void reset_mocks(void) |
| 61 | +{ |
| 62 | + memset(load_buffer, 0, sizeof(load_buffer)); |
| 63 | + memset(part_a_image, 0, sizeof(part_a_image)); |
| 64 | + memset(part_b_image, 0, sizeof(part_b_image)); |
| 65 | + build_image(part_a_image, 1, 0xA1); |
| 66 | + build_image(part_b_image, 2, 0xB2); |
| 67 | + mock_disk_init_ret = 0; |
| 68 | + mock_disk_close_called = 0; |
| 69 | + mock_do_boot_called = 0; |
| 70 | + mock_boot_address = NULL; |
| 71 | + wolfBoot_panicked = 0; |
| 72 | +} |
| 73 | + |
| 74 | +int chacha_init(void) |
| 75 | +{ |
| 76 | + return 0; |
| 77 | +} |
| 78 | + |
| 79 | +int wc_Chacha_SetIV(ChaCha* ctx, const byte* inIv, word32 counter) |
| 80 | +{ |
| 81 | + (void)ctx; |
| 82 | + (void)inIv; |
| 83 | + (void)counter; |
| 84 | + return 0; |
| 85 | +} |
| 86 | + |
| 87 | +int wc_Chacha_Process(ChaCha* ctx, byte* output, const byte* input, word32 msglen) |
| 88 | +{ |
| 89 | + (void)ctx; |
| 90 | + memmove(output, input, msglen); |
| 91 | + return 0; |
| 92 | +} |
| 93 | + |
| 94 | +void ForceZero(void* mem, size_t len) |
| 95 | +{ |
| 96 | + volatile uint8_t *p = (volatile uint8_t *)mem; |
| 97 | + while (len-- > 0) { |
| 98 | + *p++ = 0; |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +int wolfBoot_initialize_encryption(void) |
| 103 | +{ |
| 104 | + return 0; |
| 105 | +} |
| 106 | + |
| 107 | +int wolfBoot_get_encrypt_key(uint8_t *key, uint8_t *nonce) |
| 108 | +{ |
| 109 | + memset(key, 0x5A, ENCRYPT_KEY_SIZE); |
| 110 | + memset(nonce, 0xC3, ENCRYPT_NONCE_SIZE); |
| 111 | + return 0; |
| 112 | +} |
| 113 | + |
| 114 | +int disk_init(int drv) |
| 115 | +{ |
| 116 | + (void)drv; |
| 117 | + return mock_disk_init_ret; |
| 118 | +} |
| 119 | + |
| 120 | +int disk_open(int drv) |
| 121 | +{ |
| 122 | + (void)drv; |
| 123 | + return 0; |
| 124 | +} |
| 125 | + |
| 126 | +void disk_close(int drv) |
| 127 | +{ |
| 128 | + (void)drv; |
| 129 | + mock_disk_close_called++; |
| 130 | +} |
| 131 | + |
| 132 | +int disk_part_read(int drv, int part, uint64_t off, uint64_t sz, uint8_t *buf) |
| 133 | +{ |
| 134 | + uint8_t *image; |
| 135 | + |
| 136 | + (void)drv; |
| 137 | + image = (part == BOOT_PART_B) ? part_b_image : part_a_image; |
| 138 | + if (off + sz > IMAGE_HEADER_SIZE + TEST_PAYLOAD_SIZE) |
| 139 | + return -1; |
| 140 | + memcpy(buf, image + off, (size_t)sz); |
| 141 | + return (int)sz; |
| 142 | +} |
| 143 | + |
| 144 | +int wolfBoot_open_image_address(struct wolfBoot_image* img, uint8_t* image) |
| 145 | +{ |
| 146 | + uint32_t magic = *(uint32_t *)image; |
| 147 | + |
| 148 | + if (magic != WOLFBOOT_MAGIC) |
| 149 | + return -1; |
| 150 | + memset(img, 0, sizeof(*img)); |
| 151 | + img->hdr = image; |
| 152 | + img->fw_size = *(uint32_t *)(image + sizeof(uint32_t)); |
| 153 | + img->fw_base = image + IMAGE_HEADER_SIZE; |
| 154 | + img->hdr_ok = 1; |
| 155 | + return 0; |
| 156 | +} |
| 157 | + |
| 158 | +int wolfBoot_verify_integrity(struct wolfBoot_image* img) |
| 159 | +{ |
| 160 | + (void)img; |
| 161 | + return 0; |
| 162 | +} |
| 163 | + |
| 164 | +int wolfBoot_verify_authenticity(struct wolfBoot_image* img) |
| 165 | +{ |
| 166 | + (void)img; |
| 167 | + return 0; |
| 168 | +} |
| 169 | + |
| 170 | +int wolfBoot_get_dts_size(void *dts_addr) |
| 171 | +{ |
| 172 | + (void)dts_addr; |
| 173 | + return -1; |
| 174 | +} |
| 175 | + |
| 176 | +void hal_prepare_boot(void) |
| 177 | +{ |
| 178 | +} |
| 179 | + |
| 180 | +void do_boot(const uint32_t *address) |
| 181 | +{ |
| 182 | + mock_do_boot_called++; |
| 183 | + mock_boot_address = address; |
| 184 | +} |
| 185 | + |
| 186 | +#include "update_disk.c" |
| 187 | + |
| 188 | +START_TEST(test_update_disk_zeroizes_key_material_on_panic) |
| 189 | +{ |
| 190 | + size_t i; |
| 191 | + |
| 192 | + reset_mocks(); |
| 193 | + mock_disk_init_ret = -1; |
| 194 | + |
| 195 | + wolfBoot_start(); |
| 196 | + |
| 197 | + ck_assert_int_eq(wolfBoot_panicked, 1); |
| 198 | + for (i = 0; i < ENCRYPT_KEY_SIZE; i++) { |
| 199 | + ck_assert_uint_eq(disk_encrypt_key[i], 0); |
| 200 | + } |
| 201 | + for (i = 0; i < ENCRYPT_NONCE_SIZE; i++) { |
| 202 | + ck_assert_uint_eq(disk_encrypt_nonce[i], 0); |
| 203 | + } |
| 204 | +} |
| 205 | +END_TEST |
| 206 | + |
| 207 | +START_TEST(test_update_disk_zeroizes_key_material_before_boot) |
| 208 | +{ |
| 209 | + size_t i; |
| 210 | + |
| 211 | + reset_mocks(); |
| 212 | + |
| 213 | + wolfBoot_start(); |
| 214 | + |
| 215 | + ck_assert_int_eq(wolfBoot_panicked, 0); |
| 216 | + ck_assert_int_eq(mock_disk_close_called, 1); |
| 217 | + ck_assert_int_eq(mock_do_boot_called, 1); |
| 218 | + ck_assert_ptr_eq(mock_boot_address, (const uint32_t *)WOLFBOOT_LOAD_ADDRESS); |
| 219 | + for (i = 0; i < ENCRYPT_KEY_SIZE; i++) { |
| 220 | + ck_assert_uint_eq(disk_encrypt_key[i], 0); |
| 221 | + } |
| 222 | + for (i = 0; i < ENCRYPT_NONCE_SIZE; i++) { |
| 223 | + ck_assert_uint_eq(disk_encrypt_nonce[i], 0); |
| 224 | + } |
| 225 | +} |
| 226 | +END_TEST |
| 227 | + |
| 228 | +START_TEST(test_get_decrypted_blob_version_rejects_truncated_version_tlv) |
| 229 | +{ |
| 230 | + uint8_t hdr[IMAGE_HEADER_SIZE + 2]; |
| 231 | + uint8_t *p; |
| 232 | + |
| 233 | + memset(hdr, 0, sizeof(hdr)); |
| 234 | + set_u32_le(hdr, WOLFBOOT_MAGIC); |
| 235 | + |
| 236 | + p = hdr + IMAGE_HEADER_SIZE - 6; |
| 237 | + { |
| 238 | + uint8_t *q; |
| 239 | + |
| 240 | + for (q = hdr + IMAGE_HEADER_OFFSET; q < p; q += 2) { |
| 241 | + q[0] = 0xFF; |
| 242 | + if (q + 1 < p) |
| 243 | + q[1] = 0x00; |
| 244 | + } |
| 245 | + } |
| 246 | + set_u16_le(p, HDR_VERSION); |
| 247 | + set_u16_le(p + sizeof(uint16_t), 4); |
| 248 | + p[4] = 0x11; |
| 249 | + p[5] = 0x22; |
| 250 | + p[6] = 0x33; |
| 251 | + p[7] = 0x44; |
| 252 | + |
| 253 | + ck_assert_uint_eq(get_decrypted_blob_version(hdr), 0); |
| 254 | +} |
| 255 | +END_TEST |
| 256 | + |
| 257 | +Suite *wolfboot_suite(void) |
| 258 | +{ |
| 259 | + Suite *s = suite_create("wolfBoot"); |
| 260 | + TCase *tc = tcase_create("update-disk"); |
| 261 | + |
| 262 | + tcase_add_test(tc, test_update_disk_zeroizes_key_material_on_panic); |
| 263 | + tcase_add_test(tc, test_update_disk_zeroizes_key_material_before_boot); |
| 264 | + tcase_add_test(tc, test_get_decrypted_blob_version_rejects_truncated_version_tlv); |
| 265 | + suite_add_tcase(s, tc); |
| 266 | + |
| 267 | + return s; |
| 268 | +} |
| 269 | + |
| 270 | +int main(void) |
| 271 | +{ |
| 272 | + int fails; |
| 273 | + Suite *s = wolfboot_suite(); |
| 274 | + SRunner *sr = srunner_create(s); |
| 275 | + |
| 276 | + srunner_run_all(sr, CK_NORMAL); |
| 277 | + fails = srunner_ntests_failed(sr); |
| 278 | + srunner_free(sr); |
| 279 | + return fails; |
| 280 | +} |
0 commit comments