Skip to content

Commit 18781c5

Browse files
de-nordicbjarki-andreasen
authored andcommitted
[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 3a28585 commit 18781c5

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
@@ -70,7 +70,9 @@ int boot_enc_load(struct enc_key_data *enc_state, int slot,
7070
const struct image_header *hdr, const struct flash_area *fap,
7171
struct boot_status *bs);
7272
bool boot_enc_valid(struct enc_key_data *enc_state, int slot);
73-
void boot_encrypt(struct enc_key_data *enc_state, int slot,
73+
void boot_enc_encrypt(struct enc_key_data *enc_state, int slot,
74+
uint32_t off, uint32_t sz, uint32_t blk_off, uint8_t *buf);
75+
void boot_enc_decrypt(struct enc_key_data *enc_state, int slot,
7476
uint32_t off, uint32_t sz, uint32_t blk_off, uint8_t *buf);
7577
void boot_enc_zeroize(struct enc_key_data *enc_state);
7678

boot/bootutil/src/encrypted.c

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

690690
void
691-
boot_encrypt(struct enc_key_data *enc_state, int slot, uint32_t off,
691+
boot_enc_encrypt(struct enc_key_data *enc_state, int slot, uint32_t off,
692692
uint32_t sz, uint32_t blk_off, uint8_t *buf)
693693
{
694-
struct enc_key_data *enc;
694+
struct enc_key_data *enc = &enc_state[slot];
695695
uint8_t nonce[16];
696696

697-
/* boot_copy_region will call boot_encrypt with sz = 0 when skipping over
698-
the TLVs. */
697+
/* Nothing to do with size == 0 */
699698
if (sz == 0) {
700699
return;
701700
}
@@ -707,11 +706,33 @@ boot_encrypt(struct enc_key_data *enc_state, int slot, uint32_t off,
707706
nonce[14] = (uint8_t)(off >> 8);
708707
nonce[15] = (uint8_t)off;
709708

710-
enc = &enc_state[slot];
711709
assert(enc->valid == 1);
712710
bootutil_aes_ctr_encrypt(&enc->aes_ctr, nonce, buf, sz, blk_off, buf);
713711
}
714712

713+
void
714+
boot_enc_decrypt(struct enc_key_data *enc_state, int slot, uint32_t off,
715+
uint32_t sz, uint32_t blk_off, uint8_t *buf)
716+
{
717+
struct enc_key_data *enc = &enc_state[slot];
718+
uint8_t nonce[16];
719+
720+
/* Nothing to do with size == 0 */
721+
if (sz == 0) {
722+
return;
723+
}
724+
725+
memset(nonce, 0, 12);
726+
off >>= 4;
727+
nonce[12] = (uint8_t)(off >> 24);
728+
nonce[13] = (uint8_t)(off >> 16);
729+
nonce[14] = (uint8_t)(off >> 8);
730+
nonce[15] = (uint8_t)off;
731+
732+
assert(enc->valid == 1);
733+
bootutil_aes_ctr_decrypt(&enc->aes_ctr, nonce, buf, sz, blk_off, buf);
734+
}
735+
715736
/**
716737
* Clears encrypted state after use.
717738
*/

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
@@ -1688,9 +1688,15 @@ boot_copy_region(struct boot_loader_state *state,
16881688
blk_sz = tlv_off - abs_off;
16891689
}
16901690
}
1691-
boot_encrypt(BOOT_CURR_ENC(state), source_slot,
1692-
(abs_off + idx) - hdr->ih_hdr_size, blk_sz,
1693-
blk_off, &buf[idx]);
1691+
if (source_slot == 0) {
1692+
boot_enc_encrypt(BOOT_CURR_ENC(state), source_slot,
1693+
(abs_off + idx) - hdr->ih_hdr_size, blk_sz,
1694+
blk_off, &buf[idx]);
1695+
} else {
1696+
boot_enc_decrypt(BOOT_CURR_ENC(state), source_slot,
1697+
(abs_off + idx) - hdr->ih_hdr_size, blk_sz,
1698+
blk_off, &buf[idx]);
1699+
}
16941700
}
16951701
}
16961702
#endif
@@ -3253,10 +3259,9 @@ boot_decrypt_and_copy_image_to_sram(struct boot_loader_state *state,
32533259
* Part of the chunk is encrypted payload */
32543260
blk_sz = tlv_off - (bytes_copied);
32553261
}
3256-
boot_encrypt(BOOT_CURR_ENC(state), slot,
3257-
(bytes_copied + idx) - hdr->ih_hdr_size, blk_sz,
3258-
blk_off, cur_dst);
3259-
3262+
boot_enc_decrypt(BOOT_CURR_ENC(state), slot,
3263+
(bytes_copied + idx) - hdr->ih_hdr_size, blk_sz,
3264+
blk_off, cur_dst);
32603265
bytes_copied += chunk_sz;
32613266
}
32623267
rc = 0;

0 commit comments

Comments
 (0)