Skip to content

Commit 830869c

Browse files
committed
Use fixed-length erased-key check
F/2252
1 parent 56c46be commit 830869c

1 file changed

Lines changed: 12 additions & 1 deletion

File tree

src/libwolfboot.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,17 @@ static int encrypt_key_is_valid(const uint8_t *key, uint32_t len)
8484
return (has_one != 0) && (has_zero != 0);
8585
}
8686

87+
static int encrypt_key_is_erased(const uint8_t *key, uint32_t len)
88+
{
89+
uint8_t diff = 0;
90+
uint32_t i;
91+
92+
for (i = 0; i < len; i++)
93+
diff |= key[i] ^ FLASH_BYTE_ERASED;
94+
95+
return diff == 0;
96+
}
97+
8798
#define FALLBACK_IV_OFFSET 0x00100000U
8899
#if !defined(XMEMSET)
89100
#include <string.h>
@@ -1682,7 +1693,7 @@ int RAMFUNCTION wolfBoot_erase_encrypt_key(void)
16821693
mem -= (sel_sec * WOLFBOOT_SECTOR_SIZE);
16831694
#endif
16841695
XMEMSET(ff, FLASH_BYTE_ERASED, ENCRYPT_KEY_SIZE + ENCRYPT_NONCE_SIZE);
1685-
if (XMEMCMP(mem, ff, ENCRYPT_KEY_SIZE + ENCRYPT_NONCE_SIZE) != 0)
1696+
if (!encrypt_key_is_erased(mem, ENCRYPT_KEY_SIZE + ENCRYPT_NONCE_SIZE))
16861697
ret = hal_set_key(ff, ff + ENCRYPT_KEY_SIZE);
16871698
return ret;
16881699
#endif

0 commit comments

Comments
 (0)