Commit e52c08ed authored by Fabio Utzig's avatar Fabio Utzig Committed by Fabio Utzig
Browse files

bootutil: update to new protected TLV format

Implements the validation system where hashing is performed over header
+ payload + protected TLVs.

Signed-off-by: default avatarFabio Utzig <utzig@apache.org>
parent fd140ece
Loading
Loading
Loading
Loading
+2 −1
Changes for boot/bootutil/include/bootutil/image.h: 2 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -41,6 +41,7 @@ struct flash_area;
#define IMAGE_MAGIC_V1              0x96f3b83c
#define IMAGE_MAGIC_NONE            0xffffffff
#define IMAGE_TLV_INFO_MAGIC        0x6907
#define IMAGE_TLV_PROT_INFO_MAGIC   0x6908

#define IMAGE_HEADER_SIZE           32

@@ -150,7 +151,7 @@ struct image_tlv_iter {
    const struct flash_area *fap;
    uint8_t type;
    bool prot;
    uint16_t prot_len;
    uint16_t prot_end;
    uint32_t tlv_off;
    uint32_t tlv_end;
};
+16 −27
Changes for boot/bootutil/src/image_validate.c: 16 added lines, 27 removed lines.
Original line number Diff line number Diff line
@@ -61,15 +61,15 @@ bootutil_img_hash(struct enc_key_data *enc_state, int image_index,
    uint16_t hdr_size;
    uint32_t off;
    int rc;
#ifdef MCUBOOT_ENC_IMAGES
    uint32_t protected_off;
    uint32_t blk_off;
#endif
    uint32_t tlv_off;

#if (BOOT_IMAGE_NUMBER == 1) || !defined(MCUBOOT_ENC_IMAGES)
    (void)enc_state;
    (void)image_index;
    (void)hdr_size;
    (void)blk_off;
    (void)tlv_off;
#endif

#ifdef MCUBOOT_ENC_IMAGES
@@ -89,25 +89,18 @@ bootutil_img_hash(struct enc_key_data *enc_state, int image_index,
    }

    /* Hash is computed over image header and image itself. */
    hdr_size = hdr->ih_hdr_size;
    size = BOOT_TLV_OFF(hdr);

#ifdef MCUBOOT_ENC_IMAGES
    protected_off = size;
#endif
    size = hdr_size = hdr->ih_hdr_size;
    size += hdr->ih_img_size;
    tlv_off = size;

#if (MCUBOOT_IMAGE_NUMBER > 1)
    /* If dependency TLVs are present then the TLV info header and the
     * dependency TLVs are also protected and have to be included in the hash
     * calculation.
     */
    if (hdr->ih_protect_tlv_size != 0) {
    /* If protected TLVs are present they are also hashed. */
    size += hdr->ih_protect_tlv_size;
    }
#endif

    for (off = 0; off < size; off += blk_sz) {
        blk_sz = size - off;
        if (blk_sz > tmp_buf_sz) {
            blk_sz = tmp_buf_sz;
        }
#ifdef MCUBOOT_ENC_IMAGES
        /* The only data that is encrypted in an image is the payload;
         * both header and TLVs (when protected) are not.
@@ -115,24 +108,20 @@ bootutil_img_hash(struct enc_key_data *enc_state, int image_index,
        if ((off < hdr_size) && ((off + blk_sz) > hdr_size)) {
            /* read only the header */
            blk_sz = hdr_size - off;
        } else if (off >= protected_off) {
            /* read protected TLVs */
            blk_sz = size - off;
        } else if ((off + blk_sz) > protected_off) {
            /* do not copy beyond image payload */
            blk_sz = protected_off - off;
        }
#endif
        if (blk_sz > tmp_buf_sz) {
            blk_sz = tmp_buf_sz;
        if ((off < tlv_off) && ((off + blk_sz) > tlv_off)) {
            /* read only up to the end of the image payload */
            blk_sz = tlv_off - off;
        }
#endif
        rc = flash_area_read(fap, off, tmp_buf, blk_sz);
        if (rc) {
            return rc;
        }
#ifdef MCUBOOT_ENC_IMAGES
        if (MUST_DECRYPT(fap, image_index, hdr)) {
            if (off >= hdr_size && off < protected_off) {
            /* 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,
                        blk_sz, blk_off, tmp_buf);
+18 −1
Changes for boot/bootutil/src/loader.c: 18 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -253,6 +253,7 @@ boot_read_image_size(struct boot_loader_state *state, int slot, uint32_t *size)
    const struct flash_area *fap;
    struct image_tlv_info info;
    uint32_t off;
    uint32_t protect_tlv_size;
    int area_id;
    int rc;

@@ -274,12 +275,28 @@ boot_read_image_size(struct boot_loader_state *state, int slot, uint32_t *size)
        goto done;
    }

    protect_tlv_size = boot_img_hdr(state, slot)->ih_protect_tlv_size;
    if (info.it_magic == IMAGE_TLV_PROT_INFO_MAGIC) {
        if (protect_tlv_size != info.it_tlv_tot) {
            rc = BOOT_EBADIMAGE;
            goto done;
        }

        if (flash_area_read(fap, off + info.it_tlv_tot, &info, sizeof(info))) {
            rc = BOOT_EFLASH;
            goto done;
        }
    } else if (protect_tlv_size != 0) {
        rc = BOOT_EBADIMAGE;
        goto done;
    }

    if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
        rc = BOOT_EBADIMAGE;
        goto done;
    }

    *size = off + info.it_tlv_tot;
    *size = off + protect_tlv_size + info.it_tlv_tot;
    rc = 0;

done:
+21 −5
Changes for boot/bootutil/src/tlv.c: 21 added lines, 5 removed lines.
Original line number Diff line number Diff line
@@ -48,6 +48,18 @@ bootutil_tlv_iter_begin(struct image_tlv_iter *it, const struct image_header *hd
        return -1;
    }

    if (info.it_magic == IMAGE_TLV_PROT_INFO_MAGIC) {
        if (hdr->ih_protect_tlv_size != info.it_tlv_tot) {
            return -1;
        }

        if (flash_area_read(fap, off_ + info.it_tlv_tot, &info, sizeof(info))) {
            return -1;
        }
    } else if (hdr->ih_protect_tlv_size != 0) {
        return -1;
    }

    if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
        return -1;
    }
@@ -56,10 +68,10 @@ bootutil_tlv_iter_begin(struct image_tlv_iter *it, const struct image_header *hd
    it->fap = fap;
    it->type = type;
    it->prot = prot;
    off_ += sizeof(info);
    it->tlv_off = off_;
    it->prot_len = off_ + it->hdr->ih_protect_tlv_size;
    it->tlv_end = off_ + info.it_tlv_tot;
    it->prot_end = off_ + it->hdr->ih_protect_tlv_size;
    it->tlv_end = off_ + it->hdr->ih_protect_tlv_size + info.it_tlv_tot;
    // position on first TLV
    it->tlv_off = off_ + sizeof(info);
    return 0;
}

@@ -87,13 +99,17 @@ bootutil_tlv_iter_next(struct image_tlv_iter *it, uint32_t *off, uint16_t *len,
    }

    while (it->tlv_off < it->tlv_end) {
        if (it->hdr->ih_protect_tlv_size > 0 && it->tlv_off == it->prot_end) {
            it->tlv_off += sizeof(struct image_tlv_info);
        }

        rc = flash_area_read(it->fap, it->tlv_off, &tlv, sizeof tlv);
        if (rc) {
            return -1;
        }

        /* No more TLVs in the protected area */
        if (it->prot && it->tlv_off >= it->prot_len) {
        if (it->prot && it->tlv_off >= it->prot_end) {
            return 1;
        }