Commit 08d2d94c authored by Thomas Altenbach's avatar Thomas Altenbach Committed by Fabio Utzig
Browse files

bootutil: Keep image encrypted in scratch area

Currently, when swap using scratch is used with encrypted images,
MCUboot is decrypting the images during the copy from the secondary slot
to the scratch area. This means the scratch area contains plaintext
image data and therefore that the scratch area must be placed in the
MCU's internal flash memory. This commit makes the necessary changes to
perform the decryption when copying from the scratch area to the primary
slot instead, making possible to place the scratch area in an external
flash memory since the scratch area is now encrypted.

Note that BOOT_SWAP_SAVE_ENCTLV must be enabled if the scratch area is
placed in external flash memory.

Signed-off-by: default avatarThomas Altenbach <thomas.altenbach@legrand.com>
parent f515bb1c
Loading
Loading
Loading
Loading
+1 −1
Changes for boot/boot_serial/src/boot_serial_encryption.c: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -175,7 +175,7 @@ decrypt_region_inplace(struct boot_loader_state *state,
                    blk_sz = tlv_off - (off + bytes_copied);
                }
            }
            boot_encrypt(BOOT_CURR_ENC(state), image_index, fap,
            boot_encrypt(BOOT_CURR_ENC(state), image_index, flash_area_get_id(fap),
                    (off + bytes_copied + idx) - hdr->ih_hdr_size, blk_sz,
                    blk_off, &buf[idx]);
        }
+1 −1
Changes for boot/bootutil/include/bootutil/enc_key.h: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -59,7 +59,7 @@ int boot_enc_decrypt(const uint8_t *buf, uint8_t *enckey);
bool boot_enc_valid(struct enc_key_data *enc_state, int image_index,
        const struct flash_area *fap);
void boot_encrypt(struct enc_key_data *enc_state, int image_index,
        const struct flash_area *fap, uint32_t off, uint32_t sz,
        int fa_id, uint32_t off, uint32_t sz,
        uint32_t blk_off, uint8_t *buf);
void boot_enc_zeroize(struct enc_key_data *enc_state);

+2 −2
Changes for boot/bootutil/src/encrypted.c: 2 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -681,7 +681,7 @@ boot_enc_valid(struct enc_key_data *enc_state, int image_index,

void
boot_encrypt(struct enc_key_data *enc_state, int image_index,
        const struct flash_area *fap, uint32_t off, uint32_t sz,
        int fa_id, uint32_t off, uint32_t sz,
        uint32_t blk_off, uint8_t *buf)
{
    struct enc_key_data *enc;
@@ -701,7 +701,7 @@ boot_encrypt(struct enc_key_data *enc_state, int image_index,
    nonce[14] = (uint8_t)(off >> 8);
    nonce[15] = (uint8_t)off;

    rc = flash_area_id_to_multi_image_slot(image_index, flash_area_get_id(fap));
    rc = flash_area_id_to_multi_image_slot(image_index, fa_id);
    if (rc < 0) {
        assert(0);
        return;
+1 −1
Changes for boot/bootutil/src/image_validate.c: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -150,7 +150,7 @@ bootutil_img_hash(struct enc_key_data *enc_state, int image_index,
            /* Only payload is encrypted (area between header and TLVs) */
            if (off >= hdr_size && off < tlv_off) {
                blk_off = (off - hdr_size) & 0xf;
                boot_encrypt(enc_state, image_index, fap, off - hdr_size,
                boot_encrypt(enc_state, image_index, flash_area_get_id(fap), off - hdr_size,
                        blk_sz, blk_off, tmp_buf);
            }
        }
+19 −19
Changes for boot/bootutil/src/loader.c: 19 added lines, 19 removed lines.
Original line number Diff line number Diff line
@@ -1218,10 +1218,13 @@ boot_copy_region(struct boot_loader_state *state,
    uint32_t off;
    uint32_t tlv_off;
    size_t blk_off;
    int enc_area_id;
    struct image_header *hdr;
    uint16_t idx;
    uint32_t blk_sz;
    uint8_t image_index;
    bool encrypted_src;
    bool encrypted_dst;
#endif

    TARGET_STATIC uint8_t buf[BUF_SZ] __attribute__((aligned(4)));
@@ -1245,25 +1248,22 @@ boot_copy_region(struct boot_loader_state *state,

#ifdef MCUBOOT_ENC_IMAGES
        image_index = BOOT_CURR_IMG(state);
        if ((flash_area_get_id(fap_src) == FLASH_AREA_IMAGE_SECONDARY(image_index) ||
             flash_area_get_id(fap_dst) == FLASH_AREA_IMAGE_SECONDARY(image_index)) &&
            !(flash_area_get_id(fap_src) == FLASH_AREA_IMAGE_SECONDARY(image_index) &&
              flash_area_get_id(fap_dst) == FLASH_AREA_IMAGE_SECONDARY(image_index))) {
            /* assume the secondary slot as src, needs decryption */
            hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
#if !defined(MCUBOOT_SWAP_USING_MOVE)
            off = off_src;
            if (flash_area_get_id(fap_dst) == FLASH_AREA_IMAGE_SECONDARY(image_index)) {
                /* might need encryption (metadata from the primary slot) */
                hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
                off = off_dst;
            }
#else
        encrypted_src = (flash_area_get_id(fap_src) != FLASH_AREA_IMAGE_PRIMARY(image_index));
        encrypted_dst = (flash_area_get_id(fap_dst) != FLASH_AREA_IMAGE_PRIMARY(image_index));

        if (encrypted_src != encrypted_dst) {
            off = off_dst;
            if (flash_area_get_id(fap_dst) == FLASH_AREA_IMAGE_SECONDARY(image_index)) {

            if (encrypted_dst) {
                /* Need encryption, metadata from the primary slot */
                hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
                enc_area_id = FLASH_AREA_IMAGE_PRIMARY(image_index);
            } else {
                /* Need decryption, metadata from the secondary slot */
                hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
                enc_area_id = FLASH_AREA_IMAGE_SECONDARY(image_index);
            }
#endif

            if (IS_ENCRYPTED(hdr)) {
                uint32_t abs_off = off + bytes_copied;
                if (abs_off < hdr->ih_hdr_size) {
@@ -1294,7 +1294,7 @@ boot_copy_region(struct boot_loader_state *state,
                            blk_sz = tlv_off - abs_off;
                        }
                    }
                    boot_encrypt(BOOT_CURR_ENC(state), image_index, fap_src,
                    boot_encrypt(BOOT_CURR_ENC(state), image_index, enc_area_id,
                            (abs_off + idx) - hdr->ih_hdr_size, blk_sz,
                            blk_off, &buf[idx]);
                }
@@ -2774,13 +2774,13 @@ boot_decrypt_and_copy_image_to_sram(struct boot_loader_state *state,
             * Part of the chunk is encrypted payload */
            blk_off = ((bytes_copied) - hdr->ih_hdr_size) & 0xf;
            blk_sz = tlv_off - (bytes_copied);
            boot_encrypt(BOOT_CURR_ENC(state), image_index, fap_src,
            boot_encrypt(BOOT_CURR_ENC(state), image_index, area_id,
                (bytes_copied + idx) - hdr->ih_hdr_size, blk_sz,
                blk_off, cur_dst);
        } else {
            /* Image encrypted payload section */
            blk_off = ((bytes_copied) - hdr->ih_hdr_size) & 0xf;
            boot_encrypt(BOOT_CURR_ENC(state), image_index, fap_src,
            boot_encrypt(BOOT_CURR_ENC(state), image_index, area_id,
                    (bytes_copied + idx) - hdr->ih_hdr_size, blk_sz,
                    blk_off, cur_dst);
        }