|
62 | 62 |
|
63 | 63 | #include <stddef.h> /* for size_t */ |
64 | 64 |
|
65 | | -#if defined(EXT_ENCRYPTED) |
| 65 | +#if defined(EXT_ENCRYPTED) && (defined(__WOLFBOOT) || defined(UNIT_TEST)) |
| 66 | +#include "encrypt.h" |
66 | 67 | static int encrypt_initialized = 0; |
67 | 68 |
|
68 | 69 | static uint8_t encrypt_iv_nonce[ENCRYPT_NONCE_SIZE] XALIGNED(4); |
69 | | - #if defined(__WOLFBOOT) |
70 | | - #include "encrypt.h" |
71 | | - #elif !defined(XMEMSET) |
| 70 | +static uint32_t encrypt_iv_offset = 0; |
| 71 | + |
| 72 | +#define FALLBACK_IV_OFFSET 0x00100000U |
| 73 | + #if !defined(XMEMSET) |
72 | 74 | #include <string.h> |
73 | 75 | #define XMEMSET memset |
74 | 76 | #define XMEMCPY memcpy |
75 | 77 | #define XMEMCMP memcmp |
76 | 78 | #endif |
| 79 | +#if defined(ENCRYPT_WITH_AES128) || defined(ENCRYPT_WITH_AES256) |
| 80 | +extern void aes_set_iv(uint8_t *nonce, uint32_t address); |
| 81 | +#endif |
77 | 82 |
|
78 | 83 | #if defined (__WOLFBOOT) || defined (UNIT_TEST) |
79 | 84 | int wolfBoot_initialize_encryption(void) |
@@ -1027,7 +1032,7 @@ static int decrypt_header(uint8_t *src) |
1027 | 1032 | uint32_t magic; |
1028 | 1033 | uint32_t len; |
1029 | 1034 | for (i = 0; i < IMAGE_HEADER_SIZE; i+=ENCRYPT_BLOCK_SIZE) { |
1030 | | - crypto_set_iv(encrypt_iv_nonce, i / ENCRYPT_BLOCK_SIZE); |
| 1035 | + wolfBoot_crypto_set_iv(encrypt_iv_nonce, i / ENCRYPT_BLOCK_SIZE); |
1031 | 1036 | crypto_decrypt(dec_hdr + i, src + i, ENCRYPT_BLOCK_SIZE); |
1032 | 1037 | } |
1033 | 1038 | magic = *((uint32_t*)(dec_hdr)); |
@@ -1359,6 +1364,7 @@ int wolfBoot_fallback_is_possible(void) |
1359 | 1364 |
|
1360 | 1365 | #ifdef EXT_ENCRYPTED |
1361 | 1366 | #include "encrypt.h" |
| 1367 | +#include "string.h" |
1362 | 1368 |
|
1363 | 1369 | #if defined(WOLFBOOT_RENESAS_TSIP) |
1364 | 1370 | #include "wolfssl/wolfcrypt/port/Renesas/renesas-tsip-crypt.h" |
@@ -1388,6 +1394,39 @@ int wolfBoot_fallback_is_possible(void) |
1388 | 1394 | static uint8_t ENCRYPT_KEY[ENCRYPT_KEY_SIZE + ENCRYPT_NONCE_SIZE]; |
1389 | 1395 | #endif |
1390 | 1396 |
|
| 1397 | +#if defined(EXT_ENCRYPTED) && (defined(__WOLFBOOT) || defined(UNIT_TEST)) |
| 1398 | +int RAMFUNCTION wolfBoot_enable_fallback_iv(int enable) |
| 1399 | +{ |
| 1400 | + int prev = 0; |
| 1401 | + if (encrypt_iv_offset != 0) |
| 1402 | + prev = 1; |
| 1403 | + |
| 1404 | + if (enable) |
| 1405 | + encrypt_iv_offset = FALLBACK_IV_OFFSET; |
| 1406 | + else |
| 1407 | + encrypt_iv_offset = 0; |
| 1408 | + |
| 1409 | + return prev; |
| 1410 | +} |
| 1411 | + |
| 1412 | +void RAMFUNCTION wolfBoot_crypto_set_iv(const uint8_t *nonce, uint32_t iv_counter) |
| 1413 | +{ |
| 1414 | +#if defined(ENCRYPT_WITH_CHACHA) |
| 1415 | + crypto_set_iv((uint8_t *)nonce, iv_counter + encrypt_iv_offset); |
| 1416 | +#elif defined(ENCRYPT_WITH_AES128) || defined(ENCRYPT_WITH_AES256) |
| 1417 | + uint8_t local_nonce[ENCRYPT_NONCE_SIZE]; |
| 1418 | + XMEMCPY(local_nonce, nonce, ENCRYPT_NONCE_SIZE); |
| 1419 | + crypto_set_iv(local_nonce, iv_counter + encrypt_iv_offset); |
| 1420 | +#else |
| 1421 | + (void)nonce; |
| 1422 | + (void)iv_counter; |
| 1423 | +#endif |
| 1424 | + |
| 1425 | + /* Fallback IV offset is single-use; clear it once applied. */ |
| 1426 | + encrypt_iv_offset = 0; |
| 1427 | +} |
| 1428 | +#endif /* EXT_ENCRYPTED && (__WOLFBOOT || UNIT_TEST) */ |
| 1429 | + |
1391 | 1430 | static int RAMFUNCTION hal_set_key(const uint8_t *k, const uint8_t *nonce) |
1392 | 1431 | { |
1393 | 1432 | #ifdef WOLFBOOT_RENESAS_TSIP |
@@ -1808,8 +1847,7 @@ int RAMFUNCTION ext_flash_encrypt_write(uintptr_t address, const uint8_t *data, |
1808 | 1847 | } |
1809 | 1848 | if (wolfBoot_initialize_encryption() < 0) |
1810 | 1849 | return -1; |
1811 | | - |
1812 | | - crypto_set_iv(encrypt_iv_nonce, iv_counter); |
| 1850 | + wolfBoot_crypto_set_iv(encrypt_iv_nonce, iv_counter); |
1813 | 1851 | break; |
1814 | 1852 | case PART_SWAP: |
1815 | 1853 | /* data is coming from update and is already encrypted */ |
@@ -1892,7 +1930,7 @@ int RAMFUNCTION ext_flash_decrypt_read(uintptr_t address, uint8_t *data, int len |
1892 | 1930 | return -1; |
1893 | 1931 | } |
1894 | 1932 | } |
1895 | | - crypto_set_iv(encrypt_iv_nonce, iv_counter); |
| 1933 | + wolfBoot_crypto_set_iv(encrypt_iv_nonce, iv_counter); |
1896 | 1934 | break; |
1897 | 1935 | case PART_SWAP: |
1898 | 1936 | break; |
@@ -1988,7 +2026,7 @@ int wolfBoot_ram_decrypt(uint8_t *src, uint8_t *dst) |
1988 | 2026 |
|
1989 | 2027 | /* decrypt content */ |
1990 | 2028 | while (dst_offset < (len + IMAGE_HEADER_SIZE)) { |
1991 | | - crypto_set_iv(encrypt_iv_nonce, iv_counter); |
| 2029 | + wolfBoot_crypto_set_iv(encrypt_iv_nonce, iv_counter); |
1992 | 2030 | crypto_decrypt(dec_block, row_address, ENCRYPT_BLOCK_SIZE); |
1993 | 2031 | XMEMCPY(dst + dst_offset, dec_block, ENCRYPT_BLOCK_SIZE); |
1994 | 2032 | row_address += ENCRYPT_BLOCK_SIZE; |
@@ -2035,4 +2073,3 @@ int wolfBoot_nsc_write_update(uint32_t address, const uint8_t *buf, uint32_t len |
2035 | 2073 | } |
2036 | 2074 |
|
2037 | 2075 | #endif |
2038 | | - |
|
0 commit comments