Skip to content

Commit 611edce

Browse files
committed
[nrf fromlist] boot: Replace boot_encrypt by boot_enc_encrypt and boot_enc_decrypt
To be able to implement encryption with API that requires different calls for encryption and encryption, the boot_encrypt needs to be replaced with encryption/decryption specific functions. Upstream PR: mcu-tools/mcuboot#2017 Signed-off-by: Dominik Ermel <[email protected]>
1 parent ccda2d5 commit 611edce

File tree

5 files changed

+44
-16
lines changed

5 files changed

+44
-16
lines changed

boot/boot_serial/src/boot_serial_encryption.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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), slot,
174+
boot_enc_decrypt(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: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ int boot_enc_load(struct enc_key_data *enc_state, int slot,
5959
const struct image_header *hdr, const struct flash_area *fap,
6060
struct boot_status *bs);
6161
bool boot_enc_valid(struct enc_key_data *enc_state, int slot);
62-
void boot_encrypt(struct enc_key_data *enc_state, int slot,
62+
void boot_enc_encrypt(struct enc_key_data *enc_state, int slot,
63+
uint32_t off, uint32_t sz, uint32_t blk_off, uint8_t *buf);
64+
void boot_enc_decrypt(struct enc_key_data *enc_state, int slot,
6365
uint32_t off, uint32_t sz, uint32_t blk_off, uint8_t *buf);
6466
void boot_enc_zeroize(struct enc_key_data *enc_state);
6567

boot/bootutil/src/encrypted.c

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -663,14 +663,13 @@ boot_enc_valid(struct enc_key_data *enc_state, int slot)
663663
}
664664

665665
void
666-
boot_encrypt(struct enc_key_data *enc_state, int slot, uint32_t off,
666+
boot_enc_encrypt(struct enc_key_data *enc_state, int slot, uint32_t off,
667667
uint32_t sz, uint32_t blk_off, uint8_t *buf)
668668
{
669-
struct enc_key_data *enc;
669+
struct enc_key_data *enc = &enc_state[slot];
670670
uint8_t nonce[16];
671671

672-
/* boot_copy_region will call boot_encrypt with sz = 0 when skipping over
673-
the TLVs. */
672+
/* Nothing to do with size == 0 */
674673
if (sz == 0) {
675674
return;
676675
}
@@ -682,11 +681,33 @@ boot_encrypt(struct enc_key_data *enc_state, int slot, uint32_t off,
682681
nonce[14] = (uint8_t)(off >> 8);
683682
nonce[15] = (uint8_t)off;
684683

685-
enc = &enc_state[slot];
686684
assert(enc->valid == 1);
687685
bootutil_aes_ctr_encrypt(&enc->aes_ctr, nonce, buf, sz, blk_off, buf);
688686
}
689687

688+
void
689+
boot_enc_decrypt(struct enc_key_data *enc_state, int slot, uint32_t off,
690+
uint32_t sz, uint32_t blk_off, uint8_t *buf)
691+
{
692+
struct enc_key_data *enc = &enc_state[slot];
693+
uint8_t nonce[16];
694+
695+
/* Nothing to do with size == 0 */
696+
if (sz == 0) {
697+
return;
698+
}
699+
700+
memset(nonce, 0, 12);
701+
off >>= 4;
702+
nonce[12] = (uint8_t)(off >> 24);
703+
nonce[13] = (uint8_t)(off >> 16);
704+
nonce[14] = (uint8_t)(off >> 8);
705+
nonce[15] = (uint8_t)off;
706+
707+
assert(enc->valid == 1);
708+
bootutil_aes_ctr_decrypt(&enc->aes_ctr, nonce, buf, sz, blk_off, buf);
709+
}
710+
690711
/**
691712
* Clears encrypted state after use.
692713
*/

boot/bootutil/src/image_validate.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,8 @@ bootutil_img_hash(struct enc_key_data *enc_state, int image_index,
162162

163163
if (off >= hdr_size && off < tlv_off) {
164164
blk_off = (off - hdr_size) & 0xf;
165-
boot_encrypt(enc_state, slot, off - hdr_size,
166-
blk_sz, blk_off, tmp_buf);
165+
boot_enc_decrypt(enc_state, slot, off - hdr_size,
166+
blk_sz, blk_off, tmp_buf);
167167
}
168168
}
169169
#endif

boot/bootutil/src/loader.c

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1676,9 +1676,15 @@ boot_copy_region(struct boot_loader_state *state,
16761676
blk_sz = tlv_off - abs_off;
16771677
}
16781678
}
1679-
boot_encrypt(BOOT_CURR_ENC(state), source_slot,
1680-
(abs_off + idx) - hdr->ih_hdr_size, blk_sz,
1681-
blk_off, &buf[idx]);
1679+
if (source_slot == 0) {
1680+
boot_enc_encrypt(BOOT_CURR_ENC(state), source_slot,
1681+
(abs_off + idx) - hdr->ih_hdr_size, blk_sz,
1682+
blk_off, &buf[idx]);
1683+
} else {
1684+
boot_enc_decrypt(BOOT_CURR_ENC(state), source_slot,
1685+
(abs_off + idx) - hdr->ih_hdr_size, blk_sz,
1686+
blk_off, &buf[idx]);
1687+
}
16821688
}
16831689
}
16841690
#endif
@@ -3184,10 +3190,9 @@ boot_decrypt_and_copy_image_to_sram(struct boot_loader_state *state,
31843190
* Part of the chunk is encrypted payload */
31853191
blk_sz = tlv_off - (bytes_copied);
31863192
}
3187-
boot_encrypt(BOOT_CURR_ENC(state), slot,
3188-
(bytes_copied + idx) - hdr->ih_hdr_size, blk_sz,
3189-
blk_off, cur_dst);
3190-
3193+
boot_enc_decrypt(BOOT_CURR_ENC(state), slot,
3194+
(bytes_copied + idx) - hdr->ih_hdr_size, blk_sz,
3195+
blk_off, cur_dst);
31913196
bytes_copied += chunk_sz;
31923197
}
31933198
rc = 0;

0 commit comments

Comments
 (0)