Skip to content

Commit cf08819

Browse files
dgarskedanielinux
authored andcommitted
Support for CUSTOM_ENCRYPT_KEY that allows customer to supply their own implementation
1 parent 5a48625 commit cf08819

File tree

7 files changed

+229
-56
lines changed

7 files changed

+229
-56
lines changed

docs/Targets.md

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,23 +1047,20 @@ int wolfBoot_get_encrypt_key(uint8_t *key, uint8_t *nonce);
10471047
int wolfBoot_erase_encrypt_key(void); /* called automatically by wolfBoot_success() */
10481048
```
10491049
1050+
To use your own implementation for getting the encryption key use `CUSTOM_ENCRYPT_KEY` and `OBJS_EXTRA=src/my_custom_encrypt_key.o`. Then provide your own implementation of `int wolfBoot_get_encrypt_key(uint8_t *key, uint8_t *nonce);`
1051+
10501052
To sign and encrypt an image, create a key file with the concatenated key and nonce, then use the sign tool:
10511053
10521054
```sh
1053-
# Create key file (32-byte key + 16-byte IV for AES-256)
1054-
echo -n "0123456789abcdef0123456789abcdef0123456789abcdef" > enc_key.der
1055+
# Create key file (32-byte key + 16-byte nonce for AES-256)
1056+
printf "0123456789abcdef0123456789abcdef0123456789abcdef" > /tmp/enc_key.der
10551057
10561058
# Sign and encrypt
1057-
./tools/keytools/sign --ecc384 --sha384 --aes256 --encrypt enc_key.der \
1059+
./tools/keytools/sign --ecc384 --sha384 --aes256 --encrypt /tmp/enc_key.der \
10581060
fitImage wolfboot_signing_private_key.der 1
10591061
```
10601062

1061-
In your application, set the encryption key before triggering an update:
1062-
1063-
```c
1064-
wolfBoot_set_encrypt_key(enc_key, enc_iv);
1065-
wolfBoot_update_trigger();
1066-
```
1063+
The result is `fitImage_v1_signed_and_encrypted.bin`, which gets placed into your OFP_A or OFP_B partitions.
10671064

10681065
During boot, wolfBoot decrypts the image headers from disk to select the best candidate, loads and decrypts the full image to RAM, then verifies integrity and authenticity before booting. On successful boot, `wolfBoot_success()` clears the key from RAM.
10691066

docs/encrypted_partitions.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,55 @@ wolfBoot upon next boot.
3636
Aside from setting the temporary key, the update mechanism remains the same for distributing, uploading and
3737
installing firmware updates through wolfBoot.
3838

39+
### Custom encryption key storage
40+
41+
You can use the `CUSTOM_ENCRYPT_KEY` option to implement your own functions for:
42+
`wolfBoot_get_encrypt_key`, `wolfBoot_set_encrypt_key` and
43+
`wolfBoot_erase_encrypt_key`.
44+
45+
To enable:
46+
47+
1) Add `CUSTOM_ENCRYPT_KEY=1` to your `.config`
48+
2) Add your own .c file using `OBJS_EXTRA`. For example, for your own
49+
`src/custom_encrypt_key.c` add this to your `.config`:
50+
`OBJS_EXTRA=src/custom_encrypt_key.o`
51+
52+
Your custom implementation must provide these functions:
53+
54+
```c
55+
int wolfBoot_set_encrypt_key(const uint8_t *key, const uint8_t *nonce);
56+
int wolfBoot_get_encrypt_key(uint8_t *key, uint8_t *nonce);
57+
int wolfBoot_erase_encrypt_key(void);
58+
```
59+
60+
Example custom function for testing:
61+
62+
```c
63+
#include "wolfboot/wolfboot.h"
64+
#include "image.h"
65+
66+
int RAMFUNCTION wolfBoot_get_encrypt_key(uint8_t *key, uint8_t *nonce)
67+
{
68+
int i;
69+
/* Test key: "0123456789abcdef0123456789abcdef" (32 bytes for AES-256) */
70+
const char test_key[] = "0123456789abcdef0123456789abcdef";
71+
/* Test nonce: "0123456789abcdef" (16 bytes) */
72+
const char test_nonce[] = "0123456789abcdef";
73+
74+
for (i = 0; i < ENCRYPT_KEY_SIZE && i < (int)sizeof(test_key); i++) {
75+
key[i] = (uint8_t)test_key[i];
76+
}
77+
for (i = 0; i < ENCRYPT_NONCE_SIZE && i < (int)sizeof(test_nonce); i++) {
78+
nonce[i] = (uint8_t)test_nonce[i];
79+
}
80+
return 0;
81+
}
82+
```
83+
84+
Note: On platforms that use the src/update_disk.c loader it only reads from a
85+
GPT partition and with ENCRYPT=1 it only needs `wolfBoot_get_encrypt_key` implemented.
86+
87+
3988
### Libwolfboot API
4089

4190
The API to communicate with the bootloader from the application is expanded when this feature is enabled,

hal/mpfs250.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1605,7 +1605,7 @@ int disk_read(int drv, uint64_t start, uint32_t count, uint8_t *buf)
16051605
uint32_t start_offset = (start % EMMC_SD_BLOCK_SIZE);
16061606
(void)drv; /* only one drive supported */
16071607

1608-
#if 1 //def DEBUG_MMC
1608+
#ifdef DEBUG_MMC
16091609
wolfBoot_printf("disk_read: drv:%d, start:%llu, count:%d, dst:%p\n",
16101610
drv, start, count, buf);
16111611
#endif
@@ -1657,7 +1657,7 @@ int disk_write(int drv, uint64_t start, uint32_t count, const uint8_t *buf)
16571657
uint32_t start_offset = (start % EMMC_SD_BLOCK_SIZE);
16581658
(void)drv; /* only one drive supported */
16591659

1660-
#if 1 //def DEBUG_MMC
1660+
#ifdef DEBUG_MMC
16611661
wolfBoot_printf("disk_write: drv:%d, start:%llu, count:%d, src:%p\n",
16621662
drv, start, count, buf);
16631663
#endif

options.mk

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,9 @@ ifeq ($(ENCRYPT),1)
563563
endif
564564
endif
565565
endif
566+
ifeq ($(CUSTOM_ENCRYPT_KEY),1)
567+
CFLAGS+=-D"CUSTOM_ENCRYPT_KEY"
568+
endif
566569
endif
567570

568571
ifeq ($(EXT_FLASH),1)

src/libwolfboot.c

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1491,6 +1491,7 @@ static int RAMFUNCTION hal_set_key(const uint8_t *k, const uint8_t *nonce)
14911491
return ret;
14921492
#endif
14931493
}
1494+
#ifndef CUSTOM_ENCRYPT_KEY
14941495
/**
14951496
* @brief Set the encryption key.
14961497
*
@@ -1545,7 +1546,8 @@ int RAMFUNCTION wolfBoot_get_encrypt_key(uint8_t *k, uint8_t *nonce)
15451546
#endif
15461547
return 0;
15471548
}
1548-
#endif
1549+
#endif /* UNIT_TEST */
1550+
15491551
/**
15501552
* @brief Erase the encryption key.
15511553
*
@@ -1575,6 +1577,7 @@ int RAMFUNCTION wolfBoot_erase_encrypt_key(void)
15751577
#endif
15761578
return 0;
15771579
}
1580+
#endif /* !CUSTOM_ENCRYPT_KEY */
15781581

15791582
#if defined(__WOLFBOOT) || defined(UNIT_TEST)
15801583

@@ -1585,20 +1588,31 @@ ChaCha chacha;
15851588

15861589
int RAMFUNCTION chacha_init(void)
15871590
{
1588-
#if defined(MMU) || defined(UNIT_TEST)
1589-
const uint8_t *key = ENCRYPT_KEY;
1591+
#ifdef CUSTOM_ENCRYPT_KEY
1592+
uint8_t stored_nonce[ENCRYPT_NONCE_SIZE];
1593+
uint8_t key[ENCRYPT_KEY_SIZE];
15901594
#else
1591-
const uint8_t *key = (uint8_t *)(WOLFBOOT_PARTITION_BOOT_ADDRESS +
1592-
ENCRYPT_TMP_SECRET_OFFSET);
1593-
#endif
1594-
uint8_t ff[ENCRYPT_KEY_SIZE];
15951595
const uint8_t* stored_nonce;
1596-
1597-
#ifdef NVM_FLASH_WRITEONCE
1598-
key -= WOLFBOOT_SECTOR_SIZE * nvm_select_fresh_sector(PART_BOOT);
1596+
uint8_t *key;
15991597
#endif
1598+
uint8_t ff[ENCRYPT_KEY_SIZE];
16001599

1600+
#ifdef CUSTOM_ENCRYPT_KEY
1601+
int ret = wolfBoot_get_encrypt_key(key, stored_nonce);
1602+
if (ret != 0)
1603+
return ret;
1604+
#else
1605+
#if defined(MMU) || defined(UNIT_TEST)
1606+
key = ENCRYPT_KEY;
1607+
#else
1608+
key = (uint8_t *)(WOLFBOOT_PARTITION_BOOT_ADDRESS +
1609+
ENCRYPT_TMP_SECRET_OFFSET);
1610+
#endif
1611+
#ifdef NVM_FLASH_WRITEONCE
1612+
key -= WOLFBOOT_SECTOR_SIZE * nvm_select_fresh_sector(PART_BOOT);
1613+
#endif
16011614
stored_nonce = key + ENCRYPT_KEY_SIZE;
1615+
#endif
16021616

16031617
XMEMSET(&chacha, 0, sizeof(chacha));
16041618

@@ -1632,9 +1646,14 @@ Aes aes_dec, aes_enc;
16321646
*/
16331647
int aes_init(void)
16341648
{
1635-
int devId;
1649+
int devId = INVALID_DEVID;
1650+
#if defined(CUSTOM_ENCRYPT_KEY) && !defined(WOLFBOOT_RENESAS_TSIP)
1651+
uint8_t stored_nonce[ENCRYPT_NONCE_SIZE];
1652+
uint8_t key[ENCRYPT_KEY_SIZE];
1653+
#else
16361654
uint8_t *stored_nonce;
16371655
uint8_t *key;
1656+
#endif
16381657
uint8_t ff[ENCRYPT_KEY_SIZE];
16391658

16401659
#ifdef WOLFBOOT_RENESAS_TSIP
@@ -1645,19 +1664,20 @@ int aes_init(void)
16451664
key = enc_key->encrypted_user_key;
16461665
stored_nonce = enc_key->initial_vector;
16471666
wolfCrypt_Init(); /* required to setup the crypto callback defaults */
1648-
#else /* non TSIP */
1649-
devId = INVALID_DEVID;
1650-
#if defined(MMU) || defined(UNIT_TEST)
1651-
key = ENCRYPT_KEY;
1667+
#elif defined(CUSTOM_ENCRYPT_KEY)
1668+
wolfBoot_get_encrypt_key(key, stored_nonce);
16521669
#else
1653-
key = (uint8_t*)(WOLFBOOT_PARTITION_BOOT_ADDRESS +
1654-
ENCRYPT_TMP_SECRET_OFFSET);
1655-
#endif
1656-
#ifdef NVM_FLASH_WRITEONCE
1657-
key -= WOLFBOOT_SECTOR_SIZE * nvm_select_fresh_sector(PART_BOOT);
1658-
#endif
1659-
stored_nonce = key + ENCRYPT_KEY_SIZE;
1660-
#endif /* WOLFBOOT_RENESAS_TSIP */
1670+
#if defined(MMU) || defined(UNIT_TEST)
1671+
key = ENCRYPT_KEY;
1672+
#else
1673+
key = (uint8_t*)(WOLFBOOT_PARTITION_BOOT_ADDRESS +
1674+
ENCRYPT_TMP_SECRET_OFFSET);
1675+
#endif
1676+
#ifdef NVM_FLASH_WRITEONCE
1677+
key -= WOLFBOOT_SECTOR_SIZE * nvm_select_fresh_sector(PART_BOOT);
1678+
#endif
1679+
stored_nonce = key + ENCRYPT_KEY_SIZE;
1680+
#endif /* non TSIP */
16611681

16621682
XMEMSET(&aes_enc, 0, sizeof(aes_enc));
16631683
XMEMSET(&aes_dec, 0, sizeof(aes_dec));

0 commit comments

Comments
 (0)