Add macro to check if image must be decrypted
An image must be decrypted when it is loaded on the secondary slot and
its header flag indicates it is encrypted. Instead of checking both
things every time the image is read, add a new macro, MUST_DECRYPT, that
does both checks.
Also `BOOT_CURR_ENC` was simplified to be used directly on
`bootutil_img_validate` calls, returning NULL for no encrypted images.
Signed-off-by: Fabio Utzig <utzig@apache.org>
diff --git a/boot/bootutil/src/loader.c b/boot/bootutil/src/loader.c
index 0685cbb..43a6955 100644
--- a/boot/bootutil/src/loader.c
+++ b/boot/bootutil/src/loader.c
@@ -771,18 +771,13 @@
(void)state;
#endif
- image_index = BOOT_CURR_IMG(state);
-
-#ifndef MCUBOOT_ENC_IMAGES
(void)bs;
(void)rc;
- if (bootutil_img_validate(NULL, image_index, hdr, fap, tmpbuf,
- BOOT_TMPBUF_SZ, NULL, 0, NULL)) {
- return BOOT_EBADIMAGE;
- }
-#else
- if ((fap->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index))
- && IS_ENCRYPTED(hdr)) {
+
+ image_index = BOOT_CURR_IMG(state);
+
+#ifdef MCUBOOT_ENC_IMAGES
+ if (MUST_DECRYPT(fap, image_index, hdr)) {
rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs->enckey[1]);
if (rc < 0) {
return BOOT_EBADIMAGE;
@@ -791,11 +786,12 @@
return BOOT_EBADIMAGE;
}
}
+#endif
+
if (bootutil_img_validate(BOOT_CURR_ENC(state), image_index, hdr, fap, tmpbuf,
BOOT_TMPBUF_SZ, NULL, 0, NULL)) {
return BOOT_EBADIMAGE;
}
-#endif
return 0;
}