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

Add bootutil support for encrypted images

This allows storing encrypted images in slot1, that are automatically
decrypted when copying to slot0 and re-encrypted when copying from slot0
to slot1.

The encryption works by applying AES-CTR-128 on the image blocks
(excluding the header and TLVs) using a random key. This random key
is itself encrypted using either RSA-OAEP-2048 or AES-KW-128 (AES keywrap
as defined by RFC3394), and appended to the image as newly defined TLVs.

AES-CTR-128 was chosen primarily for having stream cipher proporties,
which basically means that any block being encrypted/decrypted does not
depend on any other previous blocks results.

The TLV adds about 256 bytes to the image in RSA-OAEP-2048 mode, and 24
bytes in AES-KW-128 mode. Resulting sizes for a Mynewt generated mcuboot
(frdm-k64f):

- swap mode and no signing: 12KB
- adding encryption with RSA-OAEP-2048: 28KB
- adding encryption with AES-KW-128: 20KB

Some extra comments:

- AES-KW-128 requires a fairly new mbedtls with nist_kw support.
- An alternative methods which could be added later are ECIES.
- Key-wrapping seems easy enough to implement using just standard
  AES-ECB mode that it should be straight-forward to also add support to
  tinycrypt.

Signed-off-by: default avatarFabio Utzig <utzig@apache.org>
parent 9d306d20
Loading
Loading
Loading
Loading
+53
Changes for boot/bootutil/include/bootutil/enc_key.h: 53 added lines, 0 removed lines.
Original line number Diff line number Diff line
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

#ifndef BOOTUTIL_ENC_KEY_H
#define BOOTUTIL_ENC_KEY_H

#include <flash_map_backend/flash_map_backend.h>
#include "bootutil/image.h"
#include "mbedtls/aes.h"

#ifdef __cplusplus
extern "C" {
#endif

#define BOOT_ENC_KEY_SIZE       16
#define BOOT_ENC_KEY_SIZE_BITS  (BOOT_ENC_KEY_SIZE * 8)

struct enc_key_data {
    uint8_t valid;
    mbedtls_aes_context aes;
};

extern const struct bootutil_key bootutil_enc_key;

int boot_enc_set_key(uint8_t slot, uint8_t *enckey);
int boot_enc_load(const struct image_header *hdr, const struct flash_area *fap,
        uint8_t *enckey);
int boot_enc_valid(const struct flash_area *fap);
void boot_encrypt(const struct flash_area *fap, uint32_t off, uint32_t sz,
        uint32_t blk_off, uint8_t *buf);
void boot_enc_zeroize(void);

#ifdef __cplusplus
}
#endif

#endif /* BOOTUTIL_ENC_KEY_H */
+3
Changes for boot/bootutil/include/bootutil/image.h: 3 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -40,6 +40,7 @@ struct flash_area;
 */
#define IMAGE_F_PIC                      0x00000001 /* Not supported. */
#define IMAGE_F_NON_BOOTABLE             0x00000010 /* Split image app. */
#define IMAGE_F_ENCRYPTED                0x00000004 /* Encrypted. */
/*
 * Indicates that this image should be loaded into RAM instead of run
 * directly from flash.  The address to load should be in the
@@ -67,6 +68,8 @@ struct flash_area;
#define IMAGE_TLV_RSA2048_PSS       0x20   /* RSA2048 of hash output */
#define IMAGE_TLV_ECDSA224          0x21   /* ECDSA of hash output */
#define IMAGE_TLV_ECDSA256          0x22   /* ECDSA of hash output */
#define IMAGE_TLV_ENC_RSA2048       0x30   /* Key encrypted with RSA-OAEP-2048 */
#define IMAGE_TLV_ENC_KW128         0x31   /* Key encrypted with AES-KW-128 */

struct image_version {
    uint8_t iv_major;
+102 −3
Changes for boot/bootutil/src/bootutil_misc.c: 102 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -33,6 +33,9 @@
#include "bootutil/bootutil.h"
#include "bootutil_priv.h"
#include "bootutil/bootutil_log.h"
#ifdef MCUBOOT_ENC_IMAGES
#include "bootutil/enc_key.h"
#endif

int boot_current_slot;

@@ -122,15 +125,26 @@ boot_slots_trailer_sz(uint8_t min_write_sz)
{
    return /* state for all sectors */
           BOOT_STATUS_MAX_ENTRIES * BOOT_STATUS_STATE_COUNT * min_write_sz +
           BOOT_MAX_ALIGN * 3 /* copy_done + image_ok + swap_size */        +
#ifdef MCUBOOT_ENC_IMAGES
           /* encryption keys */
           BOOT_ENC_KEY_SIZE * 2                  +
#endif
           /* copy_done + image_ok + swap_size */
           BOOT_MAX_ALIGN * 3                     +
           BOOT_MAGIC_SZ;
}

static uint32_t
boot_scratch_trailer_sz(uint8_t min_write_sz)
{
    return BOOT_STATUS_STATE_COUNT * min_write_sz +  /* state for one sector */
           BOOT_MAX_ALIGN * 2                     +  /* image_ok + swap_size */
           /* state for one sector */
    return BOOT_STATUS_STATE_COUNT * min_write_sz +
#ifdef MCUBOOT_ENC_IMAGES
           /* encryption keys */
           BOOT_ENC_KEY_SIZE * 2                  +
#endif
           /* image_ok + swap_size */
           BOOT_MAX_ALIGN * 2                     +
           BOOT_MAGIC_SZ;
}

@@ -202,6 +216,20 @@ boot_swap_size_off(const struct flash_area *fap)
    return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN * 3;
}

#ifdef MCUBOOT_ENC_IMAGES
static uint32_t
boot_enc_key_off(const struct flash_area *fap, uint8_t slot)
{
    if (fap->fa_id == FLASH_AREA_IMAGE_SCRATCH) {
        return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN * 2 -
                              ((slot + 1) * BOOT_ENC_KEY_SIZE);
    }

    return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN * 3 -
                          ((slot + 1) * BOOT_ENC_KEY_SIZE);
}
#endif

int
boot_read_swap_state(const struct flash_area *fap,
                     struct boot_swap_state *state)
@@ -337,6 +365,60 @@ out:
    return rc;
}

#ifdef MCUBOOT_ENC_IMAGES
int
boot_read_enc_key(uint8_t slot, uint8_t *enckey)
{
    uint32_t magic[BOOT_MAGIC_SZ];
    uint32_t off;
    const struct flash_area *fap;
    int rc;

    rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
    if (rc != 0) {
        return BOOT_EFLASH;
    }

    off = boot_magic_off(fap);
    rc = flash_area_read(fap, off, magic, BOOT_MAGIC_SZ);
    if (rc != 0) {
        rc = BOOT_EFLASH;
        goto out;
    }

    if (memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) != 0) {
        /*
         * If Slot 0 's magic is not valid, try scratch...
         */

        flash_area_close(fap);

        rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap);
        if (rc != 0) {
            return BOOT_EFLASH;
        }

        off = boot_magic_off(fap);
        rc = flash_area_read(fap, off, magic, BOOT_MAGIC_SZ);
        if (rc != 0) {
            rc = BOOT_EFLASH;
            goto out;
        }

        assert(memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) == 0);
    }

    off = boot_enc_key_off(fap, slot);
    rc = flash_area_read(fap, off, enckey, BOOT_ENC_KEY_SIZE);
    if (rc != 0) {
        rc = BOOT_EFLASH;
    }

out:
    flash_area_close(fap);
    return rc;
}
#endif

int
boot_write_magic(const struct flash_area *fap)
@@ -427,6 +509,23 @@ boot_write_swap_size(const struct flash_area *fap, uint32_t swap_size)
    return 0;
}

#ifdef MCUBOOT_ENC_IMAGES
int
boot_write_enc_key(const struct flash_area *fap, uint8_t slot, const uint8_t *enckey)
{
    uint32_t off;
    int rc;

    off = boot_enc_key_off(fap, slot);
    rc = flash_area_write(fap, off, enckey, BOOT_ENC_KEY_SIZE);
    if (rc != 0) {
        return BOOT_EFLASH;
    }

    return 0;
}
#endif

int
boot_swap_type(void)
{
+12
Changes for boot/bootutil/src/bootutil_priv.h: 12 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -27,6 +27,10 @@
#include "bootutil/image.h"
#include "mcuboot_config/mcuboot_config.h"

#ifdef MCUBOOT_ENC_IMAGES
#include "bootutil/enc_key.h"
#endif

#ifdef __cplusplus
extern "C" {
#endif
@@ -57,6 +61,9 @@ struct boot_status {
    uint8_t state;        /* Which part of the swapping process are we at */
    uint8_t use_scratch;  /* Are status bytes ever written to scratch? */
    uint32_t swap_size;   /* Total size of swapped image */
#ifdef MCUBOOT_ENC_IMAGES
    uint8_t enckey[2][BOOT_ENC_KEY_SIZE];
#endif
};

#define BOOT_MAGIC_GOOD     1
@@ -179,6 +186,11 @@ int boot_write_copy_done(const struct flash_area *fap);
int boot_write_image_ok(const struct flash_area *fap);
int boot_write_swap_size(const struct flash_area *fap, uint32_t swap_size);
int boot_read_swap_size(uint32_t *swap_size);
#ifdef MCUBOOT_ENC_IMAGES
int boot_write_enc_key(const struct flash_area *fap, uint8_t slot,
                       const uint8_t *enckey);
int boot_read_enc_key(uint8_t slot, uint8_t *enckey);
#endif

/*
 * Accessors for the contents of struct boot_loader_state.
+281
Changes for boot/bootutil/src/encrypted.c: 281 added lines, 0 removed lines.
Original line number Diff line number Diff line
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

#include "mcuboot_config/mcuboot_config.h"

#if defined(MCUBOOT_ENC_IMAGES)
#include <assert.h>
#include <stddef.h>
#include <inttypes.h>
#include <string.h>

#include "hal/hal_flash.h"

#if defined(MCUBOOT_ENCRYPT_RSA)
#include "mbedtls/rsa.h"
#include "mbedtls/asn1.h"
#endif

#if defined(MCUBOOT_ENCRYPT_KW)
#include "mbedtls/nist_kw.h"
#endif

#include "mbedtls/aes.h"

#include "bootutil/image.h"
#include "bootutil/enc_key.h"
#include "bootutil/sign_key.h"

#include "bootutil_priv.h"

static struct enc_key_data enc_state[BOOT_NUM_SLOTS];

#define TLV_ENC_RSA_SZ  256
#define TLV_ENC_KW_SZ   24

#if defined(MCUBOOT_ENCRYPT_RSA)
static int
parse_enckey(mbedtls_rsa_context *ctx, uint8_t **p, uint8_t *end)
{
    int rc;
    size_t len;

    if ((rc = mbedtls_asn1_get_tag(p, end, &len,
                    MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
        return -1;
    }

    if (*p + len != end) {
        return -2;
    }

    if ( /* version */
        mbedtls_asn1_get_int(p, end, &ctx->ver) != 0 ||
         /* public modulus */
        mbedtls_asn1_get_mpi(p, end, &ctx->N) != 0 ||
         /* public exponent */
        mbedtls_asn1_get_mpi(p, end, &ctx->E) != 0 ||
         /* private exponent */
        mbedtls_asn1_get_mpi(p, end, &ctx->D) != 0 ||
         /* primes */
        mbedtls_asn1_get_mpi(p, end, &ctx->P) != 0 ||
        mbedtls_asn1_get_mpi(p, end, &ctx->Q) != 0 ||
         /* d mod (p-1) and d mod (q-1) */
        mbedtls_asn1_get_mpi(p, end, &ctx->DP) != 0 ||
        mbedtls_asn1_get_mpi(p, end, &ctx->DQ) != 0 ||
         /* q ^ (-1) mod p */
        mbedtls_asn1_get_mpi(p, end, &ctx->QP) != 0) {
        return -3;
    }

    ctx->len = mbedtls_mpi_size(&ctx->N);

    if (*p != end) {
        return -4;
    }

    if (mbedtls_rsa_check_pubkey(ctx) != 0 ||
        mbedtls_rsa_check_privkey(ctx) != 0) {
        return -5;
    }

    return 0;
}
#endif

int
boot_enc_set_key(uint8_t slot, uint8_t *enckey)
{
    int rc;

    mbedtls_aes_init(&enc_state[slot].aes);
    rc = mbedtls_aes_setkey_enc(&enc_state[slot].aes, enckey, BOOT_ENC_KEY_SIZE_BITS);
    if (rc) {
        mbedtls_aes_free(&enc_state[slot].aes);
        return -1;
    }

    enc_state[slot].valid = 1;

    return 0;
}

#if defined(MCUBOOT_ENCRYPT_RSA)
#    define EXPECTED_ENC_TLV    IMAGE_TLV_ENC_RSA2048
#    define EXPECTED_ENC_LEN    TLV_ENC_RSA_SZ
#elif defined(MCUBOOT_ENCRYPT_KW)
#    define EXPECTED_ENC_TLV    IMAGE_TLV_ENC_KW128
#    define EXPECTED_ENC_LEN    TLV_ENC_KW_SZ
#endif

/*
 * Load encryption key.
 */
int
boot_enc_load(const struct image_header *hdr, const struct flash_area *fap,
              uint8_t *enckey)
{
#if defined(MCUBOOT_ENCRYPT_RSA)
    mbedtls_rsa_context rsa;
    uint8_t *cp;
    uint8_t *cpend;
#endif
#if defined(MCUBOOT_ENCRYPT_KW)
    mbedtls_nist_kw_context kw;
#endif
    size_t olen;
    uint32_t off;
    uint32_t end;
    struct image_tlv_info info;
    struct image_tlv tlv;
    uint8_t buf[TLV_ENC_RSA_SZ];
    uint8_t slot;
    uint8_t enckey_type;
    int rc;

    slot = fap->fa_id - FLASH_AREA_IMAGE_0;

    /* Already loaded... */
    if (enc_state[slot].valid) {
        return 1;
    }

    off = hdr->ih_img_size + hdr->ih_hdr_size;

    rc = flash_area_read(fap, off, &info, sizeof(info));
    if (rc) {
        return rc;
    }
    if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
        return -1;
    }
    end = off + info.it_tlv_tot;
    off += sizeof(info);

    enckey_type = 0;
    for (; off < end; off += sizeof(tlv) + tlv.it_len) {
        rc = flash_area_read(fap, off, &tlv, sizeof tlv);
        if (rc) {
            return rc;
        }

        if (tlv.it_type == EXPECTED_ENC_TLV) {
            if (tlv.it_len != EXPECTED_ENC_LEN) {
                return -1;
            }
            rc = flash_area_read(fap, off + sizeof(tlv), buf, EXPECTED_ENC_LEN);
            if (rc) {
                return rc;
            }
            enckey_type = EXPECTED_ENC_TLV;
            break;
        }
    }

    if (enckey_type == 0) {
        return -1;
    }

    if (enckey_type == EXPECTED_ENC_TLV) {
#if defined(MCUBOOT_ENCRYPT_RSA)
        mbedtls_rsa_init(&rsa, MBEDTLS_RSA_PKCS_V21, MBEDTLS_MD_SHA256);

        cp = (uint8_t *)bootutil_enc_key.key;
        cpend = cp + *bootutil_enc_key.len;

        rc = parse_enckey(&rsa, &cp, cpend);
        if (rc) {
            mbedtls_rsa_free(&rsa);
            goto done;
        }

        rc = mbedtls_rsa_rsaes_oaep_decrypt(&rsa, NULL, NULL, MBEDTLS_RSA_PRIVATE,
                NULL, 0, &olen, buf, enckey, BOOT_ENC_KEY_SIZE);
        mbedtls_rsa_free(&rsa);

#elif defined(MCUBOOT_ENCRYPT_KW)
        mbedtls_nist_kw_init(&kw);

        assert(*bootutil_enc_key.len == 16);
        rc = mbedtls_nist_kw_setkey(&kw, MBEDTLS_CIPHER_ID_AES,
                bootutil_enc_key.key, *bootutil_enc_key.len * 8, 0);
        if (rc) {
            mbedtls_nist_kw_free(&kw);
            goto done;
        }

        rc = mbedtls_nist_kw_unwrap(&kw, MBEDTLS_KW_MODE_KW, buf, TLV_ENC_KW_SZ,
                enckey, &olen, BOOT_ENC_KEY_SIZE);

        mbedtls_nist_kw_free(&kw);
#endif
    }

done:
    return rc;
}

int
boot_enc_valid(const struct flash_area *fap)
{
    return enc_state[fap->fa_id - FLASH_AREA_IMAGE_0].valid;
}

void
boot_encrypt(const struct flash_area *fap, uint32_t off, uint32_t sz,
        uint32_t blk_off, uint8_t *buf)
{
    struct enc_key_data *enc;
    uint32_t i, j;
    uint8_t u8;
    uint8_t nonce[16];
    uint8_t blk[16];

    memset(nonce, 0, 12);
    off >>= 4;
    nonce[12] = (uint8_t)(off >> 24);
    nonce[13] = (uint8_t)(off >> 16);
    nonce[14] = (uint8_t)(off >> 8);
    nonce[15] = (uint8_t)off;

    enc = &enc_state[fap->fa_id - FLASH_AREA_IMAGE_0];
    assert(enc->valid == 1);
    for (i = 0; i < sz; i++) {
        if (i == 0 || blk_off == 0) {
            mbedtls_aes_crypt_ecb(&enc->aes, MBEDTLS_AES_ENCRYPT, nonce, blk);

            for (j = 16; j > 0; --j) {
                if (++nonce[j - 1] != 0) {
                    break;
                }
            }
        }

        u8 = *buf;
        *buf++ = u8 ^ blk[blk_off];
        blk_off = (blk_off + 1) & 0x0f;
    }
}

void boot_enc_zeroize(void)
{
    memset(&enc_state, 0, sizeof(enc_state));
}

#endif /* MCUBOOT_ENC_IMAGES */
Loading