Skip to content

Commit 9de171d

Browse files
committed
[nrf noup] bootutil: Provide support for embedded AES keys
Commit provides support for MCUBOOT_EMBEDDED_ENC_KEY config option, that allows to compile code with embedded key. When this option is enabled, compilation requires definition of boot_take_enc_key function to be provided by user; prototype for the function is provided. The boot_take_enc_key function is supposed to provide encryption AES key to be used for image encryption and decryption. Signed-off-by: Dominik Ermel <[email protected]>
1 parent 58a01bf commit 9de171d

File tree

9 files changed

+42
-4
lines changed

9 files changed

+42
-4
lines changed

boot/boot_serial/src/boot_serial_encryption.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ boot_image_validate_encrypted(struct boot_loader_state *state,
3131
int rc;
3232

3333
if (MUST_DECRYPT(fa_p, BOOT_CURR_IMG(state), hdr)) {
34+
#ifdef MCUBOOT_EMBEDDED_ENC_KEY
35+
rc = boot_en_take_key(bs->enckey[BOOT_SLOT_SECONDARY], BOOT_CUR_IMG(state), BOOT_SLOT_SECONDARY);
36+
#else
3437
rc = boot_enc_load(state, BOOT_SLOT_SECONDARY, hdr, fa_p, bs);
38+
#endif
3539
if (rc < 0) {
3640
FIH_RET(fih_rc);
3741
}
@@ -235,7 +239,11 @@ decrypt_image_inplace(const struct flash_area *fa_p,
235239
#endif
236240
memset(&boot_data, 0, sizeof(struct boot_loader_state));
237241
/* Load the encryption keys into cache */
242+
#ifdef MCUBOOT_EMBEDDED_ENC_KEY
243+
rc = boot_take_enc_key(bs->enckey[BOOT_SLOT_PRIMARY], BOOT_CURR_IMG(state), BOOT_SLOT_PRIMARY);
244+
#else
238245
rc = boot_enc_load(state, BOOT_SLOT_PRIMARY, hdr, fa_p, bs);
246+
#endif
239247
if (rc < 0) {
240248
FIH_RET(fih_rc);
241249
}

boot/bootutil/include/bootutil/enc_key.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ void boot_enc_decrypt(struct enc_key_data *enc_state,
7575
/* Note that boot_enc_zeorize takes BOOT_CURR_ENC, not BOOT_CURR_ENC_SLOT */
7676
void boot_enc_zeroize(struct enc_key_data *enc_state);
7777

78+
/* Retrieve key for a slot */
79+
int boot_take_enc_key(uint8_t *key, int image, int slot);
80+
7881
#ifdef __cplusplus
7982
}
8083
#endif

boot/bootutil/include/bootutil/enc_key_public.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ extern "C" {
7070
# define BOOT_ENC_KEY_SIZE 16
7171
#endif
7272

73+
#if !defined(CONFIG_BOOT_ENCRYPT_IMAGE_WITH_EMBEDDED_KEY)
7374
#ifdef MCUBOOT_HMAC_SHA512
7475
# define BOOT_HMAC_SIZE 64
7576
#else
@@ -100,6 +101,7 @@ extern "C" {
100101
# define BOOT_ENC_TLV_SIZE (BOOT_ENC_KEY_SIZE + 8)
101102
# define BOOT_ENC_TLV IMAGE_TLV_ENC_KW
102103
#endif
104+
#endif
103105

104106
/* Common ECIES definitions */
105107
#if defined(EC_PUBK_LEN)

boot/bootutil/src/bootutil_loader.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,11 @@ boot_check_image(struct boot_loader_state *state, struct boot_status *bs, int sl
197197
*/
198198
#if defined(MCUBOOT_ENC_IMAGES) && !defined(MCUBOOT_RAM_LOAD)
199199
if (MUST_DECRYPT(fap, BOOT_CURR_IMG(state), hdr)) {
200+
#ifdef MCUBOOT_EMBEDDED_ENC_KEY
201+
rc = boot_take_enc_key(bs->enckey[BOOT_SLOT_SECONDARY], BOOT_CURR_IMG(state), BOOT_SLOT_SECONDARY);
202+
#else
200203
rc = boot_enc_load(state, BOOT_SLOT_SECONDARY, hdr, fap, bs);
204+
#endif
201205
if (rc < 0) {
202206
FIH_RET(fih_rc);
203207
}

boot/bootutil/src/bootutil_misc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ boot_read_unprotected_tlv_sizes(const struct flash_area *fap, uint16_t *tlv_size
240240
}
241241
#endif
242242

243-
#ifdef MCUBOOT_ENC_IMAGES
243+
#if defined(MCUBOOT_ENC_IMAGES) && !defined(MCUBOOT_EMBEDDED_ENC_KEY)
244244
int
245245
boot_read_enc_key(const struct flash_area *fap, uint8_t slot, struct boot_status *bs)
246246
{

boot/bootutil/src/encrypted.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,7 @@ static int fake_rng(void *p_rng, unsigned char *output, size_t len)
370370
#endif /* (MCUBOOT_ENCRYPT_RSA && MCUBOOT_USE_MBED_TLS && !MCUBOOT_USE_PSA_CRYPTO) ||
371371
(MCUBOOT_ENCRYPT_EC256 && MCUBOOT_USE_MBED_TLS) */
372372

373+
#if !defined(MCUBOOT_EMBEDDED_ENC_KEY)
373374
/*
374375
* Decrypt an encryption key TLV.
375376
*
@@ -564,7 +565,9 @@ boot_decrypt_key(const uint8_t *buf, uint8_t *enckey)
564565
return rc;
565566
}
566567
#endif /* CONFIG_BOOT_ED25519_PSA && CONFIG_BOOT_ECDSA_PSA */
568+
#endif /* defined(MCUBOOT_EMBEDDED_ENC_KEY) */
567569

570+
#if !defined(MCUBOOT_EMBEDDED_ENC_KEY)
568571
/*
569572
* Load encryption key.
570573
*/
@@ -625,6 +628,7 @@ boot_enc_load(struct boot_loader_state *state, int slot,
625628

626629
return boot_decrypt_key(buf, bs->enckey[slot]);
627630
}
631+
#endif /* defined(MCUBOOT_EMBEDDED_ENC_KEY */
628632

629633
int
630634
boot_enc_init(struct enc_key_data *enc_state)

boot/bootutil/src/encrypted_psa.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ int bootutil_aes_ctr_set_key(bootutil_aes_ctr_context *ctx, const uint8_t *k)
231231
return 0;
232232
}
233233

234-
#if defined(MCUBOOT_ENC_IMAGES)
234+
#if defined(MCUBOOT_ENC_IMAGES) && !defined(CONFIG_BOOT_ENCRYPT_IMAGE_WITH_EMBEDDED_KEY)
235235
extern const struct bootutil_key bootutil_enc_key;
236236
/*
237237
* Decrypt an encryption key TLV.
@@ -421,6 +421,7 @@ boot_decrypt_key(const uint8_t *buf, uint8_t *enckey)
421421

422422
return 0;
423423
}
424+
#endif /* defined(MCUBOOT_ENC_IMAGES) && !defined(CONFIG_BOOT_ENCRYPT_IMAGE_WITH_EMBEDDED_KEY) */
424425

425426
int bootutil_aes_ctr_encrypt(bootutil_aes_ctr_context *ctx, uint8_t *counter,
426427
const uint8_t *m, uint32_t mlen, size_t blk_off, uint8_t *c)
@@ -535,4 +536,3 @@ int bootutil_aes_ctr_decrypt(bootutil_aes_ctr_context *ctx, uint8_t *counter,
535536
gone:
536537
return ret;
537538
}
538-
#endif /* defined(MCUBOOT_ENC_IMAGES) */

boot/bootutil/src/loader.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1421,9 +1421,13 @@ boot_copy_image(struct boot_loader_state *state, struct boot_status *bs)
14211421

14221422
#ifdef MCUBOOT_ENC_IMAGES
14231423
if (IS_ENCRYPTED(boot_img_hdr(state, BOOT_SLOT_SECONDARY))) {
1424+
#ifdef MCUBOOT_EMBEDDED_ENC_KEY
1425+
rc = boot_take_enc_key(bs->enckey[BOOT_SLOT_SECONDARY], BOOT_CURR_IMG(state), BOOT_SLOT_SECONDARY);
1426+
#else
14241427
rc = boot_enc_load(state, BOOT_SLOT_SECONDARY,
14251428
boot_img_hdr(state, BOOT_SLOT_SECONDARY),
14261429
fap_secondary_slot, bs);
1430+
#endif /* MCUBOOT_EMBEDDED_ENC_KEY */
14271431

14281432
if (rc < 0) {
14291433
return BOOT_EBADIMAGE;
@@ -1545,7 +1549,11 @@ boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
15451549
#ifdef MCUBOOT_ENC_IMAGES
15461550
if (IS_ENCRYPTED(hdr)) {
15471551
fap = BOOT_IMG_AREA(state, BOOT_SLOT_PRIMARY);
1552+
#ifdef MCUBOOT_EMBEDDED_ENC_KEY
1553+
rc = boot_take_enc_key(bs->enckey[BOOT_SLOT_PRIMARY], BOOT_CURR_IMG(state), BOOT_SLOT_PRIMARY);
1554+
#else
15481555
rc = boot_enc_load(state, BOOT_SLOT_PRIMARY, hdr, fap, bs);
1556+
#endif /* MCUBOOT_EMBEDDED_ENC_KEY */
15491557
assert(rc >= 0);
15501558

15511559
if (rc == 0) {
@@ -1569,7 +1577,11 @@ boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
15691577
hdr = boot_img_hdr(state, BOOT_SLOT_SECONDARY);
15701578
if (IS_ENCRYPTED(hdr)) {
15711579
fap = BOOT_IMG_AREA(state, BOOT_SLOT_SECONDARY);
1580+
#ifdef MCUBOOT_EMBEDDED_ENC_KEY
1581+
rc = boot_take_enc_key(bs->enckey[BOOT_SLOT_SECONDARY], BOOT_CURR_IMG(state), BOOT_SLOT_SECONDARY);
1582+
#else
15721583
rc = boot_enc_load(state, BOOT_SLOT_SECONDARY, hdr, fap, bs);
1584+
#endif /* MCUBOOT_EMBEDDED_ENC_KEY */
15731585
assert(rc >= 0);
15741586

15751587
if (rc == 0) {
@@ -1606,15 +1618,19 @@ boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
16061618

16071619
boot_enc_init(BOOT_CURR_ENC_SLOT(state, slot));
16081620

1621+
#ifdef MCUBOOT_EMBEDDED_ENC_KEY
1622+
rc = boot_take_enc_key(bs->enckey[slot], image_index, slot);
1623+
#else
16091624
rc = boot_read_enc_key(fap, slot, bs);
1625+
#endif /* MCUBOOT_EMBEDDED_ENC_KEY */
16101626
if (rc) {
16111627
BOOT_LOG_DBG("boot_swap_image: Failed loading key (%d, %d)",
16121628
image_index, slot);
16131629
} else {
16141630
boot_enc_set_key(BOOT_CURR_ENC_SLOT(state, slot), bs->enckey[slot]);
16151631
}
16161632
}
1617-
#endif
1633+
#endif /* MCUBOOT_ENC_IMAGES */
16181634
flash_area_close(fap);
16191635
}
16201636

boot/mynewt/src/single_loader.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ boot_image_validate(const struct flash_area *fa_p,
4949
* was performed. We will try to validate the image, and if still
5050
* encrypted the validation will fail, and go in panic mode
5151
*/
52+
BOOT_LOG_DBG("boot_image_validate: clearing encryption flags");
5253
hdr->ih_flags &= ~(ENCRYPTIONFLAGS);
5354
}
5455
FIH_CALL(bootutil_img_validate, fih_rc, NULL, hdr, fa_p, tmpbuf,

0 commit comments

Comments
 (0)