Skip to content

Commit a0fb0b2

Browse files
committed
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 d5e0e89 commit a0fb0b2

File tree

5 files changed

+17
-25
lines changed

5 files changed

+17
-25
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
@@ -60,9 +60,8 @@ int boot_enc_load(struct enc_key_data *enc_state, int slot,
6060
struct boot_status *bs);
6161
bool boot_enc_valid(struct enc_key_data *enc_state, int image_index,
6262
const struct flash_area *fap);
63-
void boot_encrypt(struct enc_key_data *enc_state, int image_index,
64-
int fa_id, uint32_t off, uint32_t sz,
65-
uint32_t blk_off, uint8_t *buf);
63+
void boot_encrypt(struct enc_key_data *enc_state, int slot,
64+
uint32_t off, uint32_t sz, uint32_t blk_off, uint8_t *buf);
6665
void boot_enc_zeroize(struct enc_key_data *enc_state);
6766

6867
#ifdef __cplusplus

boot/bootutil/src/encrypted.c

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

675675
void
676-
boot_encrypt(struct enc_key_data *enc_state, int image_index,
677-
int fa_id, uint32_t off, uint32_t sz,
678-
uint32_t blk_off, uint8_t *buf)
676+
boot_encrypt(struct enc_key_data *enc_state, int slot, uint32_t off,
677+
uint32_t sz, uint32_t blk_off, uint8_t *buf)
679678
{
680679
struct enc_key_data *enc;
681680
uint8_t nonce[16];
682-
int rc;
683681

684682
/* boot_copy_region will call boot_encrypt with sz = 0 when skipping over
685683
the TLVs. */
@@ -694,13 +692,7 @@ boot_encrypt(struct enc_key_data *enc_state, int image_index,
694692
nonce[14] = (uint8_t)(off >> 8);
695693
nonce[15] = (uint8_t)off;
696694

697-
rc = flash_area_id_to_multi_image_slot(image_index, fa_id);
698-
if (rc < 0) {
699-
assert(0);
700-
return;
701-
}
702-
703-
enc = &enc_state[rc];
695+
enc = &enc_state[slot];
704696
assert(enc->valid == 1);
705697
bootutil_aes_ctr_encrypt(&enc->aes_ctr, nonce, buf, sz, blk_off, buf);
706698
}

boot/bootutil/src/image_validate.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,8 @@ bootutil_img_hash(struct enc_key_data *enc_state, int image_index,
150150
/* Only payload is encrypted (area between header and TLVs) */
151151
if (off >= hdr_size && off < tlv_off) {
152152
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);
153+
boot_encrypt(enc_state, 1, off - hdr_size,
154+
blk_sz, blk_off, tmp_buf);
155155
}
156156
}
157157
#endif

boot/bootutil/src/loader.c

Lines changed: 6 additions & 5 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
}
@@ -2774,7 +2775,7 @@ boot_decrypt_and_copy_image_to_sram(struct boot_loader_state *state,
27742775
* Part of the chunk is encrypted payload */
27752776
blk_sz = tlv_off - (bytes_copied);
27762777
}
2777-
boot_encrypt(BOOT_CURR_ENC(state), image_index, area_id,
2778+
boot_encrypt(BOOT_CURR_ENC(state), slot, area_id,
27782779
(bytes_copied + idx) - hdr->ih_hdr_size, blk_sz,
27792780
blk_off, cur_dst);
27802781

0 commit comments

Comments
 (0)