Skip to content

Commit 206c01a

Browse files
committed
bootutil: Provide support for embedded AES keys
Commit provides support for MCUBOOT_BUILTIN_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 <dominik.ermel@nordicsemi.no>
1 parent 86f606b commit 206c01a

File tree

6 files changed

+47
-8
lines changed

6 files changed

+47
-8
lines changed

boot/boot_serial/src/boot_serial_encryption.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,11 @@ decrypt_image_inplace(const struct flash_area *fa_p,
235235
#endif
236236
memset(&boot_data, 0, sizeof(struct boot_loader_state));
237237
/* Load the encryption keys into cache */
238+
#if !defined(MCUBOOT_BUILTIN_ENC_KEY)
238239
rc = boot_enc_load(state, BOOT_SLOT_PRIMARY, hdr, fa_p, bs);
240+
#else
241+
rc = boot_take_enc_key(bs->enckey[BOOT_SLOT_PRIMARY], BOOT_CURR_IMG(state), BOOT_SLOT_PRIMARY);
242+
#endif
239243
if (rc < 0) {
240244
FIH_RET(fih_rc);
241245
}

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/src/bootutil_misc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ boot_read_unprotected_tlv_sizes(const struct flash_area *fap, uint16_t *tlv_size
386386
}
387387
#endif
388388

389-
#ifdef MCUBOOT_ENC_IMAGES
389+
#if defined(MCUBOOT_ENC_IMAGES) && !defined(MCUBOOT_BUILTIN_ENC_KEY)
390390
int
391391
boot_read_enc_key(const struct flash_area *fap, uint8_t slot, struct boot_status *bs)
392392
{

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_BUILTIN_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_BUILTIN_ENC_KEY) */
567569

570+
#if !defined(MCUBOOT_BUILTIN_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_BUILTIN_ENC_KEY */
628632

629633
int
630634
boot_enc_init(struct enc_key_data *enc_state)

boot/bootutil/src/loader.c

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,7 @@ boot_image_check(struct boot_loader_state *state, struct image_header *hdr,
629629
int rc;
630630
FIH_DECLARE(fih_rc, FIH_FAILURE);
631631

632+
BOOT_LOG_DBG("boot_image_check");
632633
#if (BOOT_IMAGE_NUMBER == 1)
633634
(void)state;
634635
#endif
@@ -641,7 +642,11 @@ boot_image_check(struct boot_loader_state *state, struct image_header *hdr,
641642
*/
642643
#if defined(MCUBOOT_ENC_IMAGES) && !defined(MCUBOOT_RAM_LOAD)
643644
if (MUST_DECRYPT(fap, BOOT_CURR_IMG(state), hdr)) {
645+
#if !defined(MCUBOOT_BUILTIN_ENC_KEY)
644646
rc = boot_enc_load(state, BOOT_SLOT_SECONDARY, hdr, fap, bs);
647+
#else
648+
rc = boot_take_enc_key(bs->enckey[BOOT_SLOT_SECONDARY], BOOT_CURR_IMG(state), BOOT_SLOT_SECONDARY);
649+
#endif
645650
if (rc < 0) {
646651
FIH_RET(fih_rc);
647652
}
@@ -932,6 +937,7 @@ boot_validate_slot(struct boot_loader_state *state, int slot,
932937
}
933938
#endif
934939
if (!boot_is_header_valid(hdr, fap, state)) {
940+
BOOT_LOG_DBG("boot_validate_slot: header validation failed %d", slot);
935941
fih_rc = FIH_FAILURE;
936942
} else {
937943
BOOT_HOOK_CALL_FIH(boot_image_check_hook, FIH_BOOT_HOOK_REGULAR,
@@ -944,16 +950,16 @@ boot_validate_slot(struct boot_loader_state *state, int slot,
944950
check_validity:
945951
#endif
946952
if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
953+
#if !defined(__BOOTSIM__)
954+
BOOT_LOG_ERR("Image in the %s slot is not valid!",
955+
(slot == BOOT_SLOT_PRIMARY) ? "primary" : "secondary");
956+
#endif
947957
if ((slot != BOOT_SLOT_PRIMARY) || ARE_SLOTS_EQUIVALENT()) {
948958
boot_scramble_slot(fap, slot);
949959
/* Image is invalid, erase it to prevent further unnecessary
950960
* attempts to validate and boot it.
951961
*/
952962
}
953-
#if !defined(__BOOTSIM__)
954-
BOOT_LOG_ERR("Image in the %s slot is not valid!",
955-
(slot == BOOT_SLOT_PRIMARY) ? "primary" : "secondary");
956-
#endif
957963
fih_rc = FIH_NO_BOOTABLE_IMAGE;
958964
goto out;
959965
}
@@ -1490,8 +1496,12 @@ boot_copy_image(struct boot_loader_state *state, struct boot_status *bs)
14901496
#ifdef MCUBOOT_ENC_IMAGES
14911497
if (IS_ENCRYPTED(boot_img_hdr(state, BOOT_SLOT_SECONDARY))) {
14921498
rc = boot_enc_load(state, BOOT_SLOT_SECONDARY,
1499+
#if !defined(MCUBOOT_BUILTIN_ENC_KEY)
14931500
boot_img_hdr(state, BOOT_SLOT_SECONDARY),
14941501
fap_secondary_slot, bs);
1502+
#else
1503+
rc = boot_take_enc_key(bs->enckey[BOOT_SLOT_SECONDARY], BOOT_CURR_IMG(state), BOOT_SLOT_SECONDARY);
1504+
#endif
14951505

14961506
if (rc < 0) {
14971507
return BOOT_EBADIMAGE;
@@ -1587,8 +1597,10 @@ boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
15871597
struct image_header *hdr;
15881598
const struct flash_area *fap;
15891599
#ifdef MCUBOOT_ENC_IMAGES
1600+
#ifndef MCUBOOT_BUILTIN_ENC_KEY
1601+
int i;
1602+
#endif
15901603
uint8_t slot;
1591-
uint8_t i;
15921604
#endif
15931605
uint32_t size;
15941606
uint32_t copy_size;
@@ -1614,7 +1626,11 @@ boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
16141626
#ifdef MCUBOOT_ENC_IMAGES
16151627
if (IS_ENCRYPTED(hdr)) {
16161628
fap = BOOT_IMG_AREA(state, BOOT_SLOT_PRIMARY);
1629+
#if !defined(MCUBOOT_BUILTIN_ENC_KEY)
16171630
rc = boot_enc_load(state, BOOT_SLOT_PRIMARY, hdr, fap, bs);
1631+
#else
1632+
rc = boot_take_enc_key(bs->enckey[BOOT_SLOT_PRIMARY], BOOT_CURR_IMG(state), BOOT_SLOT_PRIMARY);
1633+
#endif
16181634
assert(rc >= 0);
16191635

16201636
if (rc == 0) {
@@ -1638,7 +1654,11 @@ boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
16381654
hdr = boot_img_hdr(state, BOOT_SLOT_SECONDARY);
16391655
if (IS_ENCRYPTED(hdr)) {
16401656
fap = BOOT_IMG_AREA(state, BOOT_SLOT_SECONDARY);
1657+
#if !defined(MCUBOOT_BUILTIN_ENC_KEY)
16411658
rc = boot_enc_load(state, BOOT_SLOT_SECONDARY, hdr, fap, bs);
1659+
#else
1660+
rc = boot_take_enc_key(bs->enckey[BOOT_SLOT_SECONDARY], BOOT_CURR_IMG(state), BOOT_SLOT_SECONDARY);
1661+
#endif
16421662
assert(rc >= 0);
16431663

16441664
if (rc == 0) {
@@ -1675,6 +1695,7 @@ boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
16751695

16761696
boot_enc_init(BOOT_CURR_ENC_SLOT(state, slot));
16771697

1698+
#ifndef MCUBOOT_BUILTIN_ENC_KEY
16781699
rc = boot_read_enc_key(fap, slot, bs);
16791700
assert(rc == 0);
16801701

@@ -1684,9 +1705,15 @@ boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
16841705
}
16851706
}
16861707

1687-
if (i != BOOT_ENC_KEY_SIZE) {
1688-
boot_enc_set_key(BOOT_CURR_ENC_SLOT(state, slot), bs->enckey[slot]);
1708+
if (i == BOOT_ENC_KEY_SIZE) {
1709+
/* Invalid key */
1710+
continue;
16891711
}
1712+
#else
1713+
rc = boot_take_enc_key(bs->enckey[slot], image_index, slot);
1714+
assert(rc == 0);
1715+
#endif
1716+
boot_enc_set_key(BOOT_CURR_ENC_SLOT(state, slot), bs->enckey[slot]);
16901717
}
16911718
#endif
16921719
flash_area_close(fap);

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)