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 <dominik.ermel@nordicsemi.no>
diff --git a/boot/bootutil/include/bootutil/enc_key.h b/boot/bootutil/include/bootutil/enc_key.h
index 297381f..57ac244 100644
--- a/boot/bootutil/include/bootutil/enc_key.h
+++ b/boot/bootutil/include/bootutil/enc_key.h
@@ -71,9 +71,8 @@
struct boot_status *bs);
bool boot_enc_valid(struct enc_key_data *enc_state, int image_index,
const struct flash_area *fap);
-void boot_encrypt(struct enc_key_data *enc_state, int image_index,
- int fa_id, uint32_t off, uint32_t sz,
- uint32_t blk_off, uint8_t *buf);
+void boot_encrypt(struct enc_key_data *enc_state, int slot,
+ uint32_t off, uint32_t sz, uint32_t blk_off, uint8_t *buf);
void boot_enc_zeroize(struct enc_key_data *enc_state);
#ifdef __cplusplus
diff --git a/boot/bootutil/src/encrypted.c b/boot/bootutil/src/encrypted.c
index 08df0fe..760deef 100644
--- a/boot/bootutil/src/encrypted.c
+++ b/boot/bootutil/src/encrypted.c
@@ -698,13 +698,11 @@
}
void
-boot_encrypt(struct enc_key_data *enc_state, int image_index,
- int fa_id, uint32_t off, uint32_t sz,
- uint32_t blk_off, uint8_t *buf)
+boot_encrypt(struct enc_key_data *enc_state, int slot, uint32_t off,
+ uint32_t sz, uint32_t blk_off, uint8_t *buf)
{
struct enc_key_data *enc;
uint8_t nonce[16];
- int rc;
/* boot_copy_region will call boot_encrypt with sz = 0 when skipping over
the TLVs. */
@@ -719,13 +717,7 @@
nonce[14] = (uint8_t)(off >> 8);
nonce[15] = (uint8_t)off;
- rc = flash_area_id_to_multi_image_slot(image_index, fa_id);
- if (rc < 0) {
- assert(0);
- return;
- }
-
- enc = &enc_state[rc];
+ enc = &enc_state[slot];
assert(enc->valid == 1);
bootutil_aes_ctr_encrypt(&enc->aes_ctr, nonce, buf, sz, blk_off, buf);
}
diff --git a/boot/bootutil/src/image_validate.c b/boot/bootutil/src/image_validate.c
index b4e8b79..239f369 100644
--- a/boot/bootutil/src/image_validate.c
+++ b/boot/bootutil/src/image_validate.c
@@ -148,10 +148,13 @@
#ifdef MCUBOOT_ENC_IMAGES
if (MUST_DECRYPT(fap, image_index, hdr)) {
/* Only payload is encrypted (area between header and TLVs) */
+ int slot = flash_area_id_to_multi_image_slot(image_index,
+ flash_area_get_id(fap));
+
if (off >= hdr_size && off < tlv_off) {
blk_off = (off - hdr_size) & 0xf;
- boot_encrypt(enc_state, image_index, flash_area_get_id(fap), off - hdr_size,
- blk_sz, blk_off, tmp_buf);
+ boot_encrypt(enc_state, slot, off - hdr_size,
+ blk_sz, blk_off, tmp_buf);
}
}
#endif
diff --git a/boot/bootutil/src/loader.c b/boot/bootutil/src/loader.c
index e30a83f..6a2323e 100644
--- a/boot/bootutil/src/loader.c
+++ b/boot/bootutil/src/loader.c
@@ -1216,13 +1216,14 @@
uint32_t off;
uint32_t tlv_off;
size_t blk_off;
- int enc_area_id;
struct image_header *hdr;
uint16_t idx;
uint32_t blk_sz;
uint8_t image_index;
bool encrypted_src;
bool encrypted_dst;
+ /* Assuming the secondary slot is source and needs decryption */
+ int source_slot = 1;
#endif
TARGET_STATIC uint8_t buf[BUF_SZ] __attribute__((aligned(4)));
@@ -1255,11 +1256,11 @@
if (encrypted_dst) {
/* Need encryption, metadata from the primary slot */
hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
- enc_area_id = FLASH_AREA_IMAGE_PRIMARY(image_index);
+ source_slot = 0;
} else {
/* Need decryption, metadata from the secondary slot */
hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
- enc_area_id = FLASH_AREA_IMAGE_SECONDARY(image_index);
+ source_slot = 1;
}
if (IS_ENCRYPTED(hdr)) {
@@ -1291,7 +1292,7 @@
blk_sz = tlv_off - abs_off;
}
}
- boot_encrypt(BOOT_CURR_ENC(state), image_index, enc_area_id,
+ boot_encrypt(BOOT_CURR_ENC(state), source_slot,
(abs_off + idx) - hdr->ih_hdr_size, blk_sz,
blk_off, &buf[idx]);
}
@@ -2726,13 +2727,11 @@
uint32_t chunk_sz;
uint32_t max_sz = 1024;
uint16_t idx;
- uint8_t image_index;
uint8_t * cur_dst;
int area_id;
int rc;
uint8_t * ram_dst = (void *)(IMAGE_RAM_BASE + img_dst);
- image_index = BOOT_CURR_IMG(state);
area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
rc = flash_area_open(area_id, &fap_src);
if (rc != 0){
@@ -2774,7 +2773,7 @@
* Part of the chunk is encrypted payload */
blk_sz = tlv_off - (bytes_copied);
}
- boot_encrypt(BOOT_CURR_ENC(state), image_index, area_id,
+ boot_encrypt(BOOT_CURR_ENC(state), slot,
(bytes_copied + idx) - hdr->ih_hdr_size, blk_sz,
blk_off, cur_dst);