Skip to content

Commit 3443eae

Browse files
committed
[nrf noup] zephyr: Fix issue with unpadded encrypted updates
nrf-squash! zephyr: Add support for compressed image updates Fixes an issue whereby compressed encrypted update images were not padded and the final part of decryption would fail due to not being a length of the block size Signed-off-by: Jamie McCrae <[email protected]>
1 parent 81e6fc3 commit 3443eae

File tree

1 file changed

+44
-6
lines changed

1 file changed

+44
-6
lines changed

boot/zephyr/decompression.c

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@
3737
#endif
3838
#define DECOMP_BUF_ALLOC_SIZE (DECOMP_BUF_SIZE + DECOMP_BUF_EXTRA_SIZE)
3939

40+
#define DECRYPTION_BLOCK_SIZE_AES128 16
41+
#define DECRYPTION_BLOCK_SIZE_AES256 32
42+
4043
/* Number of times that consumed data by decompression system can be 0 in a row before aborting */
4144
#define OFFSET_ZERO_CHECK_TIMES 3
4245

@@ -187,6 +190,7 @@ int bootutil_img_hash_decompress(struct boot_loader_state *state, struct image_h
187190
struct enc_key_data *enc_state;
188191
int image_index;
189192
uint32_t comp_size = 0;
193+
uint8_t decryption_block_size = 0;
190194

191195
rc = bootutil_get_img_decrypted_comp_size(hdr, fap, &comp_size);
192196

@@ -209,6 +213,18 @@ int bootutil_img_hash_decompress(struct boot_loader_state *state, struct image_h
209213
!boot_enc_valid(enc_state, 1)) {
210214
return -1;
211215
}
216+
217+
if (MUST_DECRYPT(fap, image_index, hdr)) {
218+
if (hdr->ih_flags & IMAGE_F_ENCRYPTED_AES128) {
219+
decryption_block_size = DECRYPTION_BLOCK_SIZE_AES128;
220+
} else if (hdr->ih_flags & IMAGE_F_ENCRYPTED_AES256) {
221+
decryption_block_size = DECRYPTION_BLOCK_SIZE_AES256;
222+
} else {
223+
LOG_ERR("Unknown decryption block size");
224+
rc = BOOT_EBADIMAGE;
225+
goto finish_end;
226+
}
227+
}
212228
#endif
213229

214230
bootutil_sha_init(&sha_ctx);
@@ -319,11 +335,17 @@ int bootutil_img_hash_decompress(struct boot_loader_state *state, struct image_h
319335
}
320336

321337
#ifdef MCUBOOT_ENC_IMAGES
322-
if (MUST_DECRYPT(fap, image_index, hdr)) {
323-
boot_enc_decrypt(enc_state, 1, read_pos,
324-
copy_size, (read_pos & 0xf),
325-
tmp_buf);
326-
}
338+
if (MUST_DECRYPT(fap, image_index, hdr)) {
339+
uint8_t dummy_bytes = 0;
340+
341+
if ((copy_size % decryption_block_size)) {
342+
dummy_bytes = decryption_block_size - (copy_size % decryption_block_size);
343+
memset(&tmp_buf[copy_size], 0x00, dummy_bytes);
344+
}
345+
346+
boot_enc_decrypt(enc_state, 1, read_pos, (copy_size + dummy_bytes), (read_pos & 0xf),
347+
tmp_buf);
348+
}
327349
#endif
328350

329351
/* Decompress data in chunks, writing it back with a larger write offset of the primary
@@ -990,6 +1012,7 @@ int boot_copy_region_decompress(struct boot_loader_state *state, const struct fl
9901012

9911013
#ifdef MCUBOOT_ENC_IMAGES
9921014
uint32_t comp_size = 0;
1015+
uint8_t decryption_block_size = 0;
9931016
#endif
9941017

9951018
hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
@@ -1002,6 +1025,14 @@ int boot_copy_region_decompress(struct boot_loader_state *state, const struct fl
10021025
rc = BOOT_EBADIMAGE;
10031026
goto finish;
10041027
}
1028+
1029+
if (IS_ENCRYPTED(hdr)) {
1030+
if (hdr->ih_flags & IMAGE_F_ENCRYPTED_AES128) {
1031+
decryption_block_size = DECRYPTION_BLOCK_SIZE_AES128;
1032+
} else if (hdr->ih_flags & IMAGE_F_ENCRYPTED_AES256) {
1033+
decryption_block_size = DECRYPTION_BLOCK_SIZE_AES256;
1034+
}
1035+
}
10051036
#endif
10061037

10071038
/* Setup decompression system */
@@ -1107,7 +1138,14 @@ int boot_copy_region_decompress(struct boot_loader_state *state, const struct fl
11071138

11081139
#ifdef MCUBOOT_ENC_IMAGES
11091140
if (IS_ENCRYPTED(hdr)) {
1110-
boot_enc_decrypt(BOOT_CURR_ENC(state), 1, pos, copy_size, (pos & 0xf), buf);
1141+
uint8_t dummy_bytes = 0;
1142+
1143+
if ((copy_size % decryption_block_size)) {
1144+
dummy_bytes = decryption_block_size - (copy_size % decryption_block_size);
1145+
memset(&buf[copy_size], 0x00, dummy_bytes);
1146+
}
1147+
1148+
boot_enc_decrypt(BOOT_CURR_ENC(state), 1, pos, (copy_size + dummy_bytes), (pos & 0xf), buf);
11111149
}
11121150
#endif
11131151

0 commit comments

Comments
 (0)