Skip to content

Commit ae1e9df

Browse files
committed
Force-zero secrets in update_disk.c
F/97
1 parent 0a8e95f commit ae1e9df

2 files changed

Lines changed: 28 additions & 1 deletion

File tree

src/update_disk.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,12 @@ static int decrypt_header(const uint8_t *src, uint8_t *dst)
208208
return 0;
209209
}
210210

211+
static void disk_crypto_clear(void)
212+
{
213+
ForceZero(disk_encrypt_key, sizeof(disk_encrypt_key));
214+
ForceZero(disk_encrypt_nonce, sizeof(disk_encrypt_nonce));
215+
}
216+
211217
#endif /* DISK_ENCRYPT */
212218

213219
extern int wolfBoot_get_dts_size(void *dts_addr);
@@ -254,11 +260,13 @@ void RAMFUNCTION wolfBoot_start(void)
254260
#ifdef DISK_ENCRYPT
255261
/* Initialize encryption - this sets up the cipher with key from storage */
256262
if (wolfBoot_initialize_encryption() != 0) {
263+
disk_crypto_clear();
257264
wolfBoot_printf("Error initializing encryption\r\n");
258265
wolfBoot_panic();
259266
}
260267
/* Retrieve encryption key and nonce for disk decryption */
261268
if (wolfBoot_get_encrypt_key(disk_encrypt_key, disk_encrypt_nonce) != 0) {
269+
disk_crypto_clear();
262270
wolfBoot_printf("Error getting encryption key\r\n");
263271
wolfBoot_panic();
264272
}
@@ -267,10 +275,16 @@ void RAMFUNCTION wolfBoot_start(void)
267275

268276
ret = disk_init(BOOT_DISK);
269277
if (ret != 0) {
278+
#ifdef DISK_ENCRYPT
279+
disk_crypto_clear();
280+
#endif
270281
wolfBoot_panic();
271282
}
272283

273284
if (disk_open(BOOT_DISK) < 0) {
285+
#ifdef DISK_ENCRYPT
286+
disk_crypto_clear();
287+
#endif
274288
wolfBoot_printf("Error opening disk %d\r\n", BOOT_DISK);
275289
wolfBoot_panic();
276290
}
@@ -306,6 +320,9 @@ void RAMFUNCTION wolfBoot_start(void)
306320
}
307321

308322
if ((pB_ver == 0) && (pA_ver == 0)) {
323+
#ifdef DISK_ENCRYPT
324+
disk_crypto_clear();
325+
#endif
309326
wolfBoot_printf("No valid OS image found in either partition %d or %d\r\n",
310327
BOOT_PART_A, BOOT_PART_B);
311328
wolfBoot_panic();
@@ -409,6 +426,7 @@ void RAMFUNCTION wolfBoot_start(void)
409426
wolfBoot_printf("Decrypting image...");
410427
BENCHMARK_START();
411428
if ((IMAGE_HEADER_SIZE % ENCRYPT_BLOCK_SIZE) != 0) {
429+
disk_crypto_clear();
412430
wolfBoot_printf("Encrypted disk images require aligned header size\r\n");
413431
wolfBoot_panic();
414432
}
@@ -456,6 +474,9 @@ void RAMFUNCTION wolfBoot_start(void)
456474
} while (failures < MAX_FAILURES);
457475

458476
if (failures) {
477+
#ifdef DISK_ENCRYPT
478+
disk_crypto_clear();
479+
#endif
459480
wolfBoot_printf("Unable to find a valid partition!\r\n");
460481
wolfBoot_panic();
461482
}
@@ -512,6 +533,9 @@ void RAMFUNCTION wolfBoot_start(void)
512533

513534
#ifdef WOLFBOOT_HOOK_BOOT
514535
wolfBoot_hook_boot(&os_image);
536+
#endif
537+
#ifdef DISK_ENCRYPT
538+
disk_crypto_clear();
515539
#endif
516540
do_boot((uint32_t*)load_address
517541
#ifdef MMU

tools/unit-tests/Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ TESTS:=unit-parser unit-extflash unit-string unit-spi-flash unit-aes128 \
4747
unit-image unit-image-rsa unit-nvm unit-nvm-flagshome unit-enc-nvm \
4848
unit-enc-nvm-flagshome unit-delta unit-update-flash \
4949
unit-update-flash-enc unit-update-ram unit-pkcs11_store unit-psa_store unit-disk \
50-
unit-multiboot unit-boot-x86-fsp unit-qspi-flash unit-tpm-rsa-exp \
50+
unit-update-disk unit-multiboot unit-boot-x86-fsp unit-qspi-flash unit-tpm-rsa-exp \
5151
unit-image-nopart unit-image-sha384 unit-image-sha3-384 \
5252
unit-tpm-blob
5353

@@ -214,6 +214,9 @@ unit-update-flash-enc: ../../include/target.h unit-update-flash.c
214214
unit-update-ram: ../../include/target.h unit-update-ram.c
215215
gcc -o $@ unit-update-ram.c ../../src/image.c $(WOLFBOOT_LIB_WOLFSSL)/wolfcrypt/src/sha256.c $(CFLAGS) $(LDFLAGS)
216216

217+
unit-update-disk: ../../include/target.h unit-update-disk.c
218+
gcc -o $@ unit-update-disk.c $(CFLAGS) $(LDFLAGS)
219+
217220
unit-pkcs11_store: ../../include/target.h unit-pkcs11_store.c
218221
gcc -o $@ $(WOLFCRYPT_SRC) unit-pkcs11_store.c $(CFLAGS) $(WOLFCRYPT_CFLAGS) $(LDFLAGS)
219222

0 commit comments

Comments
 (0)