Skip to content

Commit 2fc1bd8

Browse files
committed
bootutil: Drop slot number and boot_state from most boot_enc functions
Step forward in reducing entanglement between internal APIs. Signed-off-by: Dominik Ermel <[email protected]>
1 parent 1dd8ae6 commit 2fc1bd8

File tree

7 files changed

+41
-45
lines changed

7 files changed

+41
-45
lines changed

boot/boot_serial/src/boot_serial_encryption.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ boot_image_validate_encrypted(struct boot_loader_state *state,
3535
if (rc < 0) {
3636
FIH_RET(fih_rc);
3737
}
38-
rc = boot_enc_set_key(BOOT_CURR_ENC(state), BOOT_SLOT_SECONDARY, bs);
38+
rc = boot_enc_set_key(BOOT_CURR_ENC_SLOT(state, BOOT_SLOT_SECONDARY), bs->enckey[BOOT_SLOT_SECONDARY]);
3939
if (rc < 0) {
4040
FIH_RET(fih_rc);
4141
}
@@ -169,7 +169,7 @@ decrypt_region_inplace(struct boot_loader_state *state,
169169
blk_sz = tlv_off - (off + bytes_copied);
170170
}
171171
}
172-
boot_enc_decrypt(BOOT_CURR_ENC(state), slot,
172+
boot_enc_decrypt(BOOT_CURR_ENC_SLOT(state, slot),
173173
(off + bytes_copied + idx) - hdr->ih_hdr_size, blk_sz,
174174
blk_off, &buf[idx]);
175175
}
@@ -239,7 +239,7 @@ decrypt_image_inplace(const struct flash_area *fa_p,
239239
if (rc < 0) {
240240
FIH_RET(fih_rc);
241241
}
242-
if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC(state), BOOT_SLOT_PRIMARY, bs)) {
242+
if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC_SLOT(state, BOOT_SLOT_PRIMARY), bs->enckey[BOOT_SLOT_PRIMARY])) {
243243
FIH_RET(fih_rc);
244244
}
245245
}

boot/bootutil/include/bootutil/enc_key.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,18 +61,18 @@ struct boot_loader_state;
6161
/* Decrypt random, symmetric encryption key */
6262
int boot_decrypt_key(const uint8_t *buf, uint8_t *enckey);
6363

64-
int boot_enc_init(struct enc_key_data *enc_state, uint8_t slot);
65-
int boot_enc_drop(struct enc_key_data *enc_state, uint8_t slot);
66-
int boot_enc_set_key(struct enc_key_data *enc_state, uint8_t slot,
67-
const struct boot_status *bs);
64+
int boot_enc_init(struct enc_key_data *enc_state);
65+
int boot_enc_drop(struct enc_key_data *enc_state);
66+
int boot_enc_set_key(struct enc_key_data *enc_state, const uint8_t *key);
6867
int boot_enc_load(struct boot_loader_state *state, int slot,
6968
const struct image_header *hdr, const struct flash_area *fap,
7069
struct boot_status *bs);
71-
bool boot_enc_valid(struct enc_key_data *enc_state, int slot);
72-
void boot_enc_encrypt(struct enc_key_data *enc_state, int slot,
70+
bool boot_enc_valid(const struct enc_key_data *enc_state);
71+
void boot_enc_encrypt(struct enc_key_data *enc_state,
7372
uint32_t off, uint32_t sz, uint32_t blk_off, uint8_t *buf);
74-
void boot_enc_decrypt(struct enc_key_data *enc_state, int slot,
73+
void boot_enc_decrypt(struct enc_key_data *enc_state,
7574
uint32_t off, uint32_t sz, uint32_t blk_off, uint8_t *buf);
75+
/* 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

7878
#ifdef __cplusplus

boot/bootutil/src/bootutil_img_hash.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ bootutil_img_hash(struct boot_loader_state *state,
6565
int fa_ret;
6666
#endif
6767
#if defined(MCUBOOT_ENC_IMAGES)
68-
struct enc_key_data *enc_state;
6968
int image_index;
7069
#endif
7170
#if defined(MCUBOOT_SWAP_USING_OFFSET)
@@ -91,16 +90,14 @@ bootutil_img_hash(struct boot_loader_state *state,
9190

9291
#ifdef MCUBOOT_ENC_IMAGES
9392
if (state == NULL) {
94-
enc_state = NULL;
9593
image_index = 0;
9694
} else {
97-
enc_state = BOOT_CURR_ENC(state);
9895
image_index = BOOT_CURR_IMG(state);
9996
}
10097

10198
/* Encrypted images only exist in the secondary slot */
10299
if (MUST_DECRYPT(fap, image_index, hdr) &&
103-
!boot_enc_valid(enc_state, 1)) {
100+
!boot_enc_valid(BOOT_CURR_ENC_SLOT(state, BOOT_SLOT_SECONDARY))) {
104101
BOOT_LOG_DBG("bootutil_img_hash: error encrypted image found in primary slot");
105102
return -1;
106103
}
@@ -182,7 +179,7 @@ bootutil_img_hash(struct boot_loader_state *state,
182179

183180
if (off >= hdr_size && off < tlv_off) {
184181
blk_off = (off - hdr_size) & 0xf;
185-
boot_enc_decrypt(enc_state, slot, off - hdr_size,
182+
boot_enc_decrypt(BOOT_CURR_ENC_SLOT(state, slot), off - hdr_size,
186183
blk_sz, blk_off, tmp_buf);
187184
}
188185
}

boot/bootutil/src/bootutil_priv.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,8 +483,10 @@ static inline bool boot_u16_safe_add(uint16_t *dest, uint16_t a, uint16_t b)
483483
#endif
484484
#ifdef MCUBOOT_ENC_IMAGES
485485
#define BOOT_CURR_ENC(state) ((state)->enc[BOOT_CURR_IMG(state)])
486+
#define BOOT_CURR_ENC_SLOT(state, slot) (&((state)->enc[BOOT_CURR_IMG(state)][slot]))
486487
#else
487488
#define BOOT_CURR_ENC(state) NULL
489+
#define BOOT_CURR_ENC_SLOT(state, slot) NULL
488490
#endif
489491
#define BOOT_IMG(state, slot) ((state)->imgs[BOOT_CURR_IMG(state)][(slot)])
490492
#define BOOT_IMG_AREA(state, slot) (BOOT_IMG(state, slot).area)

boot/bootutil/src/encrypted.c

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,7 @@ boot_enc_load(struct boot_loader_state *state, int slot,
573573
const struct image_header *hdr, const struct flash_area *fap,
574574
struct boot_status *bs)
575575
{
576-
struct enc_key_data *enc_state = BOOT_CURR_ENC(state);
576+
struct enc_key_data *enc_state = BOOT_CURR_ENC_SLOT(state, slot);
577577
uint32_t off;
578578
uint16_t len;
579579
struct image_tlv_iter it;
@@ -587,13 +587,13 @@ boot_enc_load(struct boot_loader_state *state, int slot,
587587
BOOT_LOG_DBG("boot_enc_load: slot %d", slot);
588588

589589
/* Already loaded... */
590-
if (enc_state[slot].valid) {
590+
if (boot_enc_valid(enc_state)) {
591591
BOOT_LOG_DBG("boot_enc_load: already loaded");
592592
return 1;
593593
}
594594

595595
/* Initialize the AES context */
596-
boot_enc_init(enc_state, slot);
596+
boot_enc_init(enc_state);
597597

598598
#if defined(MCUBOOT_SWAP_USING_OFFSET)
599599
it.start_off = boot_get_state_secondary_offset(state, fap);
@@ -627,48 +627,46 @@ boot_enc_load(struct boot_loader_state *state, int slot,
627627
}
628628

629629
int
630-
boot_enc_init(struct enc_key_data *enc_state, uint8_t slot)
630+
boot_enc_init(struct enc_key_data *enc_state)
631631
{
632-
bootutil_aes_ctr_init(&enc_state[slot].aes_ctr);
632+
bootutil_aes_ctr_init(&enc_state->aes_ctr);
633633
return 0;
634634
}
635635

636636
int
637-
boot_enc_drop(struct enc_key_data *enc_state, uint8_t slot)
637+
boot_enc_drop(struct enc_key_data *enc_state)
638638
{
639-
bootutil_aes_ctr_drop(&enc_state[slot].aes_ctr);
640-
enc_state[slot].valid = 0;
639+
bootutil_aes_ctr_drop(&enc_state->aes_ctr);
640+
enc_state->valid = 0;
641641
return 0;
642642
}
643643

644644
int
645-
boot_enc_set_key(struct enc_key_data *enc_state, uint8_t slot,
646-
const struct boot_status *bs)
645+
boot_enc_set_key(struct enc_key_data *enc_state, const uint8_t *key)
647646
{
648647
int rc;
649648

650-
rc = bootutil_aes_ctr_set_key(&enc_state[slot].aes_ctr, bs->enckey[slot]);
649+
rc = bootutil_aes_ctr_set_key(&enc_state->aes_ctr, key);
651650
if (rc != 0) {
652-
boot_enc_drop(enc_state, slot);
651+
boot_enc_drop(enc_state);
653652
return -1;
654653
}
655654

656-
enc_state[slot].valid = 1;
655+
enc_state->valid = 1;
657656

658657
return 0;
659658
}
660659

661660
bool
662-
boot_enc_valid(struct enc_key_data *enc_state, int slot)
661+
boot_enc_valid(const struct enc_key_data *enc_state)
663662
{
664-
return enc_state[slot].valid;
663+
return enc_state->valid;
665664
}
666665

667666
void
668-
boot_enc_encrypt(struct enc_key_data *enc_state, int slot, uint32_t off,
667+
boot_enc_encrypt(struct enc_key_data *enc, uint32_t off,
669668
uint32_t sz, uint32_t blk_off, uint8_t *buf)
670669
{
671-
struct enc_key_data *enc = &enc_state[slot];
672670
uint8_t nonce[16];
673671

674672
/* Nothing to do with size == 0 */
@@ -688,10 +686,9 @@ boot_enc_encrypt(struct enc_key_data *enc_state, int slot, uint32_t off,
688686
}
689687

690688
void
691-
boot_enc_decrypt(struct enc_key_data *enc_state, int slot, uint32_t off,
689+
boot_enc_decrypt(struct enc_key_data *enc, uint32_t off,
692690
uint32_t sz, uint32_t blk_off, uint8_t *buf)
693691
{
694-
struct enc_key_data *enc = &enc_state[slot];
695692
uint8_t nonce[16];
696693

697694
/* Nothing to do with size == 0 */
@@ -718,7 +715,7 @@ boot_enc_zeroize(struct enc_key_data *enc_state)
718715
{
719716
uint8_t slot;
720717
for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
721-
(void)boot_enc_drop(enc_state, slot);
718+
(void)boot_enc_drop(&enc_state[slot]);
722719
}
723720
memset(enc_state, 0, sizeof(struct enc_key_data) * BOOT_NUM_SLOTS);
724721
}

boot/bootutil/src/loader.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,7 @@ boot_image_check(struct boot_loader_state *state, struct image_header *hdr,
645645
if (rc < 0) {
646646
FIH_RET(fih_rc);
647647
}
648-
if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC(state), BOOT_SLOT_SECONDARY, bs)) {
648+
if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC_SLOT(state, BOOT_SLOT_SECONDARY), bs->enckey[BOOT_SLOT_SECONDARY])) {
649649
FIH_RET(fih_rc);
650650
}
651651
}
@@ -1378,11 +1378,11 @@ boot_copy_region(struct boot_loader_state *state,
13781378
}
13791379
}
13801380
if (source_slot == 0) {
1381-
boot_enc_encrypt(BOOT_CURR_ENC(state), source_slot,
1381+
boot_enc_encrypt(BOOT_CURR_ENC_SLOT(state, source_slot),
13821382
(abs_off + idx) - hdr->ih_hdr_size, blk_sz,
13831383
blk_off, &buf[idx]);
13841384
} else {
1385-
boot_enc_decrypt(BOOT_CURR_ENC(state), source_slot,
1385+
boot_enc_decrypt(BOOT_CURR_ENC_SLOT(state, source_slot),
13861386
(abs_off + idx) - hdr->ih_hdr_size, blk_sz,
13871387
blk_off, &buf[idx]);
13881388
}
@@ -1496,7 +1496,7 @@ boot_copy_image(struct boot_loader_state *state, struct boot_status *bs)
14961496
if (rc < 0) {
14971497
return BOOT_EBADIMAGE;
14981498
}
1499-
if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC(state), BOOT_SLOT_SECONDARY, bs)) {
1499+
if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC_SLOT(state, BOOT_SLOT_SECONDARY), bs->enckey[BOOT_SLOT_SECONDARY])) {
15001500
return BOOT_EBADIMAGE;
15011501
}
15021502
}
@@ -1618,7 +1618,7 @@ boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
16181618
assert(rc >= 0);
16191619

16201620
if (rc == 0) {
1621-
rc = boot_enc_set_key(BOOT_CURR_ENC(state), BOOT_SLOT_PRIMARY, bs);
1621+
rc = boot_enc_set_key(BOOT_CURR_ENC_SLOT(state, BOOT_SLOT_PRIMARY), bs->enckey[BOOT_SLOT_PRIMARY]);
16221622
assert(rc == 0);
16231623
} else {
16241624
rc = 0;
@@ -1642,7 +1642,7 @@ boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
16421642
assert(rc >= 0);
16431643

16441644
if (rc == 0) {
1645-
rc = boot_enc_set_key(BOOT_CURR_ENC(state), BOOT_SLOT_SECONDARY, bs);
1645+
rc = boot_enc_set_key(BOOT_CURR_ENC_SLOT(state, BOOT_SLOT_SECONDARY), bs->enckey[BOOT_SLOT_SECONDARY]);
16461646
assert(rc == 0);
16471647
} else {
16481648
rc = 0;
@@ -1673,7 +1673,7 @@ boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
16731673
#ifdef MCUBOOT_ENC_IMAGES
16741674
for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
16751675

1676-
boot_enc_init(BOOT_CURR_ENC(state), slot);
1676+
boot_enc_init(BOOT_CURR_ENC_SLOT(state, slot));
16771677

16781678
rc = boot_read_enc_key(fap, slot, bs);
16791679
assert(rc == 0);
@@ -1685,7 +1685,7 @@ boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
16851685
}
16861686

16871687
if (i != BOOT_ENC_KEY_SIZE) {
1688-
boot_enc_set_key(BOOT_CURR_ENC(state), slot, bs);
1688+
boot_enc_set_key(BOOT_CURR_ENC_SLOT(state, slot), bs->enckey[slot]);
16891689
}
16901690
}
16911691
#endif

boot/bootutil/src/ram_load.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ boot_decrypt_and_copy_image_to_sram(struct boot_loader_state *state,
155155
}
156156

157157
/* if rc > 0 then the key has already been loaded */
158-
if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC(state), slot, &bs)) {
158+
if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC_SLOT(state, slot), bs.enckey[slot])) {
159159
goto done;
160160
}
161161

@@ -176,7 +176,7 @@ boot_decrypt_and_copy_image_to_sram(struct boot_loader_state *state,
176176
* Part of the chunk is encrypted payload */
177177
blk_sz = tlv_off - (bytes_copied);
178178
}
179-
boot_enc_decrypt(BOOT_CURR_ENC(state), slot,
179+
boot_enc_decrypt(BOOT_CURR_ENC_SLOT(state, slot),
180180
(bytes_copied + idx) - hdr->ih_hdr_size, blk_sz,
181181
blk_off, cur_dst);
182182
bytes_copied += chunk_sz;

0 commit comments

Comments
 (0)