Skip to content

Commit 3f11286

Browse files
de-nordicdavidvincze
authored andcommitted
boot: Remove image_index from boot_encrypt
boot_encrypt required the image_index paired with flash area pointer to be able to figure out which slot it will operate on. Since in most calls the slot is known in advance it can be just passed to the function directly. The commit replaces both parameters with slot number. Signed-off-by: Dominik Ermel <[email protected]>
1 parent 2a7565b commit 3f11286

File tree

5 files changed

+20
-27
lines changed

5 files changed

+20
-27
lines changed

boot/boot_serial/src/boot_serial_encryption.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,11 @@ decrypt_region_inplace(struct boot_loader_state *state,
125125
size_t blk_off;
126126
uint16_t idx;
127127
uint32_t blk_sz;
128-
uint8_t image_index;
129-
128+
int slot = flash_area_id_to_multi_image_slot(BOOT_CURR_IMG(state),
129+
flash_area_get_id(fap));
130130
uint8_t buf[sz] __attribute__((aligned));
131131
assert(sz <= sizeof buf);
132+
assert(slot >= 0);
132133

133134
bytes_copied = 0;
134135
while (bytes_copied < sz) {
@@ -143,7 +144,6 @@ decrypt_region_inplace(struct boot_loader_state *state,
143144
return BOOT_EFLASH;
144145
}
145146

146-
image_index = BOOT_CURR_IMG(state);
147147
if (IS_ENCRYPTED(hdr)) {
148148
blk_sz = chunk_sz;
149149
idx = 0;
@@ -171,7 +171,7 @@ decrypt_region_inplace(struct boot_loader_state *state,
171171
blk_sz = tlv_off - (off + bytes_copied);
172172
}
173173
}
174-
boot_encrypt(BOOT_CURR_ENC(state), image_index, flash_area_get_id(fap),
174+
boot_encrypt(BOOT_CURR_ENC(state), slot,
175175
(off + bytes_copied + idx) - hdr->ih_hdr_size, blk_sz,
176176
blk_off, &buf[idx]);
177177
}

boot/bootutil/include/bootutil/enc_key.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,8 @@ int boot_enc_load(struct enc_key_data *enc_state, int slot,
7171
struct boot_status *bs);
7272
bool boot_enc_valid(struct enc_key_data *enc_state, int image_index,
7373
const struct flash_area *fap);
74-
void boot_encrypt(struct enc_key_data *enc_state, int image_index,
75-
int fa_id, uint32_t off, uint32_t sz,
76-
uint32_t blk_off, uint8_t *buf);
74+
void boot_encrypt(struct enc_key_data *enc_state, int slot,
75+
uint32_t off, uint32_t sz, uint32_t blk_off, uint8_t *buf);
7776
void boot_enc_zeroize(struct enc_key_data *enc_state);
7877

7978
#ifdef __cplusplus

boot/bootutil/src/encrypted.c

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -698,13 +698,11 @@ boot_enc_valid(struct enc_key_data *enc_state, int image_index,
698698
}
699699

700700
void
701-
boot_encrypt(struct enc_key_data *enc_state, int image_index,
702-
int fa_id, uint32_t off, uint32_t sz,
703-
uint32_t blk_off, uint8_t *buf)
701+
boot_encrypt(struct enc_key_data *enc_state, int slot, uint32_t off,
702+
uint32_t sz, uint32_t blk_off, uint8_t *buf)
704703
{
705704
struct enc_key_data *enc;
706705
uint8_t nonce[16];
707-
int rc;
708706

709707
/* boot_copy_region will call boot_encrypt with sz = 0 when skipping over
710708
the TLVs. */
@@ -719,13 +717,7 @@ boot_encrypt(struct enc_key_data *enc_state, int image_index,
719717
nonce[14] = (uint8_t)(off >> 8);
720718
nonce[15] = (uint8_t)off;
721719

722-
rc = flash_area_id_to_multi_image_slot(image_index, fa_id);
723-
if (rc < 0) {
724-
assert(0);
725-
return;
726-
}
727-
728-
enc = &enc_state[rc];
720+
enc = &enc_state[slot];
729721
assert(enc->valid == 1);
730722
bootutil_aes_ctr_encrypt(&enc->aes_ctr, nonce, buf, sz, blk_off, buf);
731723
}

boot/bootutil/src/image_validate.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,13 @@ bootutil_img_hash(struct enc_key_data *enc_state, int image_index,
148148
#ifdef MCUBOOT_ENC_IMAGES
149149
if (MUST_DECRYPT(fap, image_index, hdr)) {
150150
/* Only payload is encrypted (area between header and TLVs) */
151+
int slot = flash_area_id_to_multi_image_slot(image_index,
152+
flash_area_get_id(fap));
153+
151154
if (off >= hdr_size && off < tlv_off) {
152155
blk_off = (off - hdr_size) & 0xf;
153-
boot_encrypt(enc_state, image_index, flash_area_get_id(fap), off - hdr_size,
154-
blk_sz, blk_off, tmp_buf);
156+
boot_encrypt(enc_state, slot, off - hdr_size,
157+
blk_sz, blk_off, tmp_buf);
155158
}
156159
}
157160
#endif

boot/bootutil/src/loader.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,13 +1216,14 @@ boot_copy_region(struct boot_loader_state *state,
12161216
uint32_t off;
12171217
uint32_t tlv_off;
12181218
size_t blk_off;
1219-
int enc_area_id;
12201219
struct image_header *hdr;
12211220
uint16_t idx;
12221221
uint32_t blk_sz;
12231222
uint8_t image_index;
12241223
bool encrypted_src;
12251224
bool encrypted_dst;
1225+
/* Assuming the secondary slot is source and needs decryption */
1226+
int source_slot = 1;
12261227
#endif
12271228

12281229
TARGET_STATIC uint8_t buf[BUF_SZ] __attribute__((aligned(4)));
@@ -1255,11 +1256,11 @@ boot_copy_region(struct boot_loader_state *state,
12551256
if (encrypted_dst) {
12561257
/* Need encryption, metadata from the primary slot */
12571258
hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
1258-
enc_area_id = FLASH_AREA_IMAGE_PRIMARY(image_index);
1259+
source_slot = 0;
12591260
} else {
12601261
/* Need decryption, metadata from the secondary slot */
12611262
hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
1262-
enc_area_id = FLASH_AREA_IMAGE_SECONDARY(image_index);
1263+
source_slot = 1;
12631264
}
12641265

12651266
if (IS_ENCRYPTED(hdr)) {
@@ -1291,7 +1292,7 @@ boot_copy_region(struct boot_loader_state *state,
12911292
blk_sz = tlv_off - abs_off;
12921293
}
12931294
}
1294-
boot_encrypt(BOOT_CURR_ENC(state), image_index, enc_area_id,
1295+
boot_encrypt(BOOT_CURR_ENC(state), source_slot,
12951296
(abs_off + idx) - hdr->ih_hdr_size, blk_sz,
12961297
blk_off, &buf[idx]);
12971298
}
@@ -2726,13 +2727,11 @@ boot_decrypt_and_copy_image_to_sram(struct boot_loader_state *state,
27262727
uint32_t chunk_sz;
27272728
uint32_t max_sz = 1024;
27282729
uint16_t idx;
2729-
uint8_t image_index;
27302730
uint8_t * cur_dst;
27312731
int area_id;
27322732
int rc;
27332733
uint8_t * ram_dst = (void *)(IMAGE_RAM_BASE + img_dst);
27342734

2735-
image_index = BOOT_CURR_IMG(state);
27362735
area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
27372736
rc = flash_area_open(area_id, &fap_src);
27382737
if (rc != 0){
@@ -2774,7 +2773,7 @@ boot_decrypt_and_copy_image_to_sram(struct boot_loader_state *state,
27742773
* Part of the chunk is encrypted payload */
27752774
blk_sz = tlv_off - (bytes_copied);
27762775
}
2777-
boot_encrypt(BOOT_CURR_ENC(state), image_index, area_id,
2776+
boot_encrypt(BOOT_CURR_ENC(state), slot,
27782777
(bytes_copied + idx) - hdr->ih_hdr_size, blk_sz,
27792778
blk_off, cur_dst);
27802779

0 commit comments

Comments
 (0)