Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .github/workflows/sim.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ jobs:
# BOOT_ENC_KEY_SIZE. PSA_KEY_TYPE_AES covers all AES key
# sizes so no config delta.
- "sig-ecdsa-psa enc-aes256-ec256 mbedtls-v4,sig-ecdsa-psa enc-aes256-ec256 swap-offset validate-primary-slot max-align-16 mbedtls-v4"
# ECIES-P384, paired with P-384 signing. One job each, as
# P-384 is slow enough that together they near the 6h limit.
- "sig-ecdsa-psa sig-p384 enc-ec384 mbedtls-v4"
- "sig-ecdsa-psa sig-p384 enc-aes256-ec384 mbedtls-v4"
- "sig-ecdsa-psa sig-p384 enc-aes256-ec384 swap-offset validate-primary-slot max-align-16 mbedtls-v4"
- "ram-load enc-aes256-kw multiimage"
- "ram-load enc-aes256-kw sig-ecdsa-mbedtls multiimage"
- "custom-crypto,custom-crypto overwrite-only,custom-crypto validate-primary-slot,custom-crypto swap-offset"
Expand Down
1 change: 1 addition & 0 deletions boot/bootutil/include/bootutil/caps.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ uint32_t bootutil_get_caps(void);
#define BOOTUTIL_CAP_HW_ROLLBACK_PROT (1<<18)
#define BOOTUTIL_CAP_ECDSA_P384 (1<<19)
#define BOOTUTIL_CAP_SWAP_USING_OFFSET (1<<20)
#define BOOTUTIL_CAP_ENC_EC384 (1<<21)

/*
* Query the number of images this bootloader is configured for. This
Expand Down
7 changes: 7 additions & 0 deletions boot/bootutil/include/bootutil/crypto/ecdsa.h
Original file line number Diff line number Diff line change
Expand Up @@ -443,8 +443,15 @@ static int bootutil_ecdsa_parse_public_key(bootutil_ecdsa_context *ctx,
{
psa_key_attributes_t key_attributes = psa_key_attributes_init();
size_t key_size;
psa_status_t status;
(void)end;

/* Key import needs an initialized PSA core; repeated calls are harmless. */
status = psa_crypto_init();
if (status != PSA_SUCCESS) {
return (int)status;
}

/* public key oid is valid */
if (memcmp(PUB_KEY_OID_OFFSET(cp), IdEcPublicKey, sizeof(IdEcPublicKey))) {
return (int)PSA_ERROR_INVALID_ARGUMENT;
Expand Down
10 changes: 10 additions & 0 deletions boot/bootutil/include/bootutil/enc_key_public.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ extern "C" {

#ifdef MCUBOOT_HMAC_SHA512
# define BOOT_HMAC_SIZE 64
#elif defined(MCUBOOT_ENCRYPT_EC384)
# define BOOT_HMAC_SIZE 48
#else
# define BOOT_HMAC_SIZE 32
#endif
Expand All @@ -87,6 +89,14 @@ extern "C" {
# define EC_PRIVK_LEN (32)
# define EC_SHARED_LEN (32)
# define BOOT_ENC_TLV IMAGE_TLV_ENC_EC256
#elif defined(MCUBOOT_ENCRYPT_EC384)
# if defined(MCUBOOT_HMAC_SHA512)
# error "ECIES-P384 uses HMAC-SHA384 only"
# endif
# define EC_PUBK_LEN (97)
# define EC_PRIVK_LEN (48)
# define EC_SHARED_LEN (48)
# define BOOT_ENC_TLV IMAGE_TLV_ENC_EC384
#elif defined(MCUBOOT_ENCRYPT_X25519)
# define EC_PUBK_LEN (32)
# define EC_PRIVK_LEN (32)
Expand Down
1 change: 1 addition & 0 deletions boot/bootutil/include/bootutil/image.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ extern "C" {
#define IMAGE_TLV_ENC_X25519_SHA512 0x34 /* Key exchange using ECIES-X25519 and SHA512 for MAC
* tag and HKDF in key derivation process
*/
#define IMAGE_TLV_ENC_EC384 0x35 /* Key encrypted with ECIES-P384 */
#define IMAGE_TLV_DEPENDENCY 0x40 /* Image depends on other image */
#define IMAGE_TLV_SEC_CNT 0x50 /* security counter */
#define IMAGE_TLV_BOOT_RECORD 0x60 /* measured boot record */
Expand Down
3 changes: 3 additions & 0 deletions boot/bootutil/src/caps.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ uint32_t bootutil_get_caps(void)
#if defined(MCUBOOT_ENCRYPT_EC256)
res |= BOOTUTIL_CAP_ENC_EC256;
#endif
#if defined(MCUBOOT_ENCRYPT_EC384)
res |= BOOTUTIL_CAP_ENC_EC384;
#endif
#if defined(MCUBOOT_ENCRYPT_X25519)
res |= BOOTUTIL_CAP_ENC_X25519;
#endif
Expand Down
5 changes: 3 additions & 2 deletions boot/bootutil/src/encrypted.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ BOOT_LOG_MODULE_DECLARE(mcuboot);
#include "bootutil_priv.h"

/* NOUP Fixme: */
#if !defined(CONFIG_BOOT_ED25519_PSA) && !defined(CONFIG_BOOT_ECDSA_PSA)
#if !defined(CONFIG_BOOT_ED25519_PSA) && !defined(CONFIG_BOOT_ECDSA_PSA) && \
!defined(MCUBOOT_ENCRYPT_EC384)
#if defined(MCUBOOT_ENCRYPT_EC256) || defined(MCUBOOT_ENCRYPT_X25519)
#if defined(_compare)
static inline int bootutil_constant_time_compare(const uint8_t *a, const uint8_t *b, size_t size)
Expand Down Expand Up @@ -570,7 +571,7 @@ boot_decrypt_key(const uint8_t *buf, uint8_t *enckey)

return rc;
}
#endif /* CONFIG_BOOT_ED25519_PSA && CONFIG_BOOT_ECDSA_PSA */
#endif /* !CONFIG_BOOT_{ED25519,ECDSA}_PSA && !MCUBOOT_ENCRYPT_EC384 */

/*
* Load encryption key.
Expand Down
51 changes: 31 additions & 20 deletions boot/bootutil/src/encrypted_psa.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
#include <inttypes.h>
#include <string.h>

#if defined(MCUBOOT_ENCRYPT_EC256) || defined(MCUBOOT_ENCRYPT_X25519)
#if defined(MCUBOOT_ENCRYPT_EC256) || defined(MCUBOOT_ENCRYPT_EC384) || \
defined(MCUBOOT_ENCRYPT_X25519)
/* We are not really using the MBEDTLS but need the ASN.1 parsing functions */
#define MBEDTLS_ASN1_PARSE_C

Expand All @@ -38,7 +39,7 @@
#if !defined(MBEDTLS_OID_EC_GRP_SECP256R1)
#define MBEDTLS_OID_EC_GRP_SECP256R1 "\x2a\x86\x48\xce\x3d\x03\x01\x07"
#endif
#endif /* defined(MCUBOOT_ENCRYPT_EC256) || defined(MCUBOOT_ENCRYPT_X25519) */
#endif /* MCUBOOT_ENCRYPT_{EC256,EC384,X25519} */

#include "bootutil/image.h"
#include "bootutil/enc_key.h"
Expand All @@ -51,16 +52,25 @@ BOOT_LOG_MODULE_REGISTER(mcuboot_psa_enc);

#if defined(MCUBOOT_HMAC_SHA512)
#define PSA_HMAC_HKDF_SHA PSA_ALG_SHA_512
#elif defined(MCUBOOT_ENCRYPT_EC384)
#define PSA_HMAC_HKDF_SHA PSA_ALG_SHA_384
#else
#define PSA_HMAC_HKDF_SHA PSA_ALG_SHA_256
#endif

#if defined(MCUBOOT_ENC_IMAGES) && defined(MCUBOOT_ENCRYPT_EC256)
#define NUM_ECC_BYTES (256 / 8)
static const uint8_t ec_pubkey_oid[] = MBEDTLS_OID_EC_ALG_UNRESTRICTED;
static const uint8_t ec_secp256r1_oid[] = MBEDTLS_OID_EC_GRP_SECP256R1;
static const uint8_t ec_curve_oid[] = MBEDTLS_OID_EC_GRP_SECP256R1;
#define ECC_FAMILY PSA_ECC_FAMILY_SECP_R1
#endif /* defined(MCUBOOT_ENC_IMAGES) && defined(MCUBOOT_ENCRYPT_EC256) */
#if defined(MCUBOOT_ENC_IMAGES) && defined(MCUBOOT_ENCRYPT_EC384)
#define NUM_ECC_BYTES (384 / 8)
static const uint8_t ec_pubkey_oid[] = MBEDTLS_OID_EC_ALG_UNRESTRICTED;
/* secp384r1 (1.3.132.0.34) */
static const uint8_t ec_curve_oid[] = "\x2b\x81\x04\x00\x22";
#define ECC_FAMILY PSA_ECC_FAMILY_SECP_R1
#endif /* defined(MCUBOOT_ENC_IMAGES) && defined(MCUBOOT_ENCRYPT_EC384) */
#if defined(MCUBOOT_ENC_IMAGES) && defined(MCUBOOT_ENCRYPT_X25519)
#define X25519_OID "\x6e"
static const uint8_t ec_pubkey_oid[] = MBEDTLS_OID_ISO_IDENTIFIED_ORG \
Expand All @@ -74,15 +84,12 @@ static const uint8_t ec_pubkey_oid[] = MBEDTLS_OID_ISO_IDENTIFIED_ORG \
#define HKDF_AES_KEY_SIZE (BOOT_ENC_KEY_SIZE)
/* MAC feed */
#define HKDF_MAC_FEED_INDEX (HKDF_AES_KEY_INDEX + HKDF_AES_KEY_SIZE)
#if !defined(MCUBOOT_HMAC_SHA512)
#define HKDF_MAC_FEED_SIZE (32)
#else
#define HKDF_MAC_FEED_SIZE (64)
#endif
#define HKDF_MAC_FEED_SIZE (BOOT_HMAC_SIZE)
/* Total size */
#define HKDF_SIZE (HKDF_AES_KEY_SIZE + HKDF_MAC_FEED_SIZE)

#if defined(MCUBOOT_ENC_IMAGES) && defined(MCUBOOT_ENCRYPT_EC256)
#if defined(MCUBOOT_ENC_IMAGES) && \
(defined(MCUBOOT_ENCRYPT_EC256) || defined(MCUBOOT_ENCRYPT_EC384))
/* Fixme: This duplicates code from encrypted.c and depends on mbedtls */

/*
Expand Down Expand Up @@ -119,8 +126,9 @@ parse_priv_enckey(uint8_t **p, uint8_t *end, uint8_t *private_key)
memcmp(alg.ASN1_CONTEXT_MEMBER(p), ec_pubkey_oid, sizeof(ec_pubkey_oid) - 1)) {
return -1;
}
if (param.ASN1_CONTEXT_MEMBER(len) != sizeof(ec_secp256r1_oid) - 1 ||
memcmp(param.ASN1_CONTEXT_MEMBER(p), ec_secp256r1_oid, sizeof(ec_secp256r1_oid) - 1)) {
if (param.ASN1_CONTEXT_MEMBER(len) != sizeof(ec_curve_oid) - 1 ||
memcmp(param.ASN1_CONTEXT_MEMBER(p), ec_curve_oid,
sizeof(ec_curve_oid) - 1) != 0) {
return -1;
}

Expand Down Expand Up @@ -156,7 +164,7 @@ parse_priv_enckey(uint8_t **p, uint8_t *end, uint8_t *private_key)

return 0;
}
#endif /* defined(MCUBOOT_ENC_IMAGES) && defined(MCUBOOT_ENCRYPT_EC256) */
#endif /* MCUBOOT_ENC_IMAGES && MCUBOOT_ENCRYPT_{EC256,EC384} */

#if defined(MCUBOOT_ENC_IMAGES) && defined(MCUBOOT_ENCRYPT_X25519)
/* Fixme: This duplicates code from encrypted.c and depends on mbedtls */
Expand Down Expand Up @@ -254,7 +262,8 @@ int bootutil_aes_ctr_set_key(bootutil_aes_ctr_context *ctx, const uint8_t *k)
}

#if defined(MCUBOOT_ENC_IMAGES)
#if defined(MCUBOOT_ENCRYPT_EC256) || defined(MCUBOOT_ENCRYPT_X25519)
#if defined(MCUBOOT_ENCRYPT_EC256) || defined(MCUBOOT_ENCRYPT_EC384) || \
defined(MCUBOOT_ENCRYPT_X25519)
extern const struct bootutil_key bootutil_enc_key;
/*
* Decrypt an encryption key TLV.
Expand Down Expand Up @@ -300,6 +309,7 @@ boot_decrypt_key(const uint8_t *buf, uint8_t *enckey)
*/
rc = parse_priv_enckey(&cp, cpend, private_key);
if (rc) {
bootutil_wipe_memory(private_key, sizeof(private_key));
BOOT_LOG_ERR("Failed to parse ASN1 private key");
return rc;
}
Expand All @@ -309,7 +319,7 @@ boot_decrypt_key(const uint8_t *buf, uint8_t *enckey)
psa_set_key_algorithm(&kattr, PSA_ALG_ECDH);

psa_ret = psa_import_key(&kattr, private_key, sizeof(private_key), &kid);
memset(private_key, 0, sizeof(private_key));
bootutil_wipe_memory(private_key, sizeof(private_key));
psa_reset_key_attributes(&kattr);
if (psa_ret != PSA_SUCCESS) {
BOOT_LOG_ERR("Built-in key import failed %d", psa_ret);
Expand Down Expand Up @@ -369,6 +379,7 @@ boot_decrypt_key(const uint8_t *buf, uint8_t *enckey)
BOOT_LOG_WRN("Key derivation cleanup failed %d", psa_ret);
}
if (psa_ret != PSA_SUCCESS) {
bootutil_wipe_memory(derived_key, sizeof(derived_key));
BOOT_LOG_ERR("Key derivation failed %d", psa_ret);
return -1;
}
Expand All @@ -387,7 +398,7 @@ boot_decrypt_key(const uint8_t *buf, uint8_t *enckey)
HKDF_MAC_FEED_SIZE, &kid);
psa_reset_key_attributes(&kattr);
if (psa_ret != PSA_SUCCESS) {
memset(derived_key, 0, sizeof(derived_key));
bootutil_wipe_memory(derived_key, sizeof(derived_key));
BOOT_LOG_ERR("MAC key import failed %d", psa_ret);
return -1;
}
Expand All @@ -402,7 +413,7 @@ boot_decrypt_key(const uint8_t *buf, uint8_t *enckey)
BOOT_LOG_WRN("MAC key destruction failed %d", psa_cleanup_ret);
}
if (psa_ret != PSA_SUCCESS) {
memset(derived_key, 0, sizeof(derived_key));
bootutil_wipe_memory(derived_key, sizeof(derived_key));
BOOT_LOG_ERR("MAC verification failed %d", psa_ret);
return -1;
}
Expand All @@ -415,7 +426,7 @@ boot_decrypt_key(const uint8_t *buf, uint8_t *enckey)
/* Import the AES partition of derived key, the first 16 bytes */
psa_ret = psa_import_key(&kattr, &derived_key[HKDF_AES_KEY_INDEX],
HKDF_AES_KEY_SIZE, &kid);
memset(derived_key, 0, sizeof(derived_key));
bootutil_wipe_memory(derived_key, sizeof(derived_key));
if (psa_ret != PSA_SUCCESS) {
BOOT_LOG_ERR("AES key import failed %d", psa_ret);
return -1;
Expand All @@ -431,20 +442,20 @@ boot_decrypt_key(const uint8_t *buf, uint8_t *enckey)
len = 0;
psa_ret = psa_cipher_decrypt(kid, PSA_ALG_CTR, iv_and_key, sizeof(iv_and_key),
enckey, BOOT_ENC_KEY_SIZE, &len);
memset(iv_and_key, 0, sizeof(iv_and_key));
bootutil_wipe_memory(iv_and_key, sizeof(iv_and_key));
psa_cleanup_ret = psa_destroy_key(kid);
if (psa_cleanup_ret != PSA_SUCCESS) {
BOOT_LOG_WRN("AES key destruction failed %d", psa_cleanup_ret);
}
if (psa_ret != PSA_SUCCESS || len != BOOT_ENC_KEY_SIZE) {
memset(enckey, 0, BOOT_ENC_KEY_SIZE);
bootutil_wipe_memory(enckey, BOOT_ENC_KEY_SIZE);
BOOT_LOG_ERR("Random key decryption failed %d", psa_ret);
return -1;
}

return 0;
}
#endif /* defined(MCUBOOT_ENCRYPT_EC256) || defined(MCUBOOT_ENCRYPT_X25519) */
#endif /* MCUBOOT_ENCRYPT_{EC256,EC384,X25519} */

int bootutil_aes_ctr_encrypt(bootutil_aes_ctr_context *ctx, uint8_t *counter,
const uint8_t *m, uint32_t mlen, size_t blk_off, uint8_t *c)
Expand Down
1 change: 1 addition & 0 deletions boot/bootutil/src/image_validate.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ static const uint16_t allowed_unprot_tlvs[] = {
IMAGE_TLV_ENC_RSA2048,
IMAGE_TLV_ENC_KW,
IMAGE_TLV_ENC_EC256,
IMAGE_TLV_ENC_EC384,
#if !defined(MCUBOOT_HMAC_SHA512)
IMAGE_TLV_ENC_X25519,
#else
Expand Down
12 changes: 10 additions & 2 deletions boot/zephyr/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ zephyr_sources(
)

if((CONFIG_BOOT_ENCRYPT_X25519 AND CONFIG_BOOT_ED25519_PSA)
OR (CONFIG_BOOT_ENCRYPT_EC256 AND CONFIG_BOOT_ECDSA_PSA))
OR (CONFIG_BOOT_ENCRYPT_EC256 AND CONFIG_BOOT_ECDSA_PSA)
OR CONFIG_BOOT_ENCRYPT_EC384)
zephyr_sources(${BOOT_DIR}/bootutil/src/encrypted_psa.c)
endif()

Expand Down Expand Up @@ -430,6 +431,8 @@ if(CONFIG_BOOT_ENCRYPTION_KEY_FILE AND NOT CONFIG_BOOT_ENCRYPTION_KEY_FILE STREQ
set(mcuboot_default_encryption_files
${MCUBOOT_DIR}/enc-ec256-priv.pem
${MCUBOOT_DIR}/enc-ec256-pub.pem
${MCUBOOT_DIR}/enc-ec384-priv.pem
${MCUBOOT_DIR}/enc-ec384-pub.pem
${MCUBOOT_DIR}/enc-rsa2048-priv.pem
${MCUBOOT_DIR}/enc-rsa2048-pub.pem
${MCUBOOT_DIR}/enc-x25519-priv.pem
Expand Down Expand Up @@ -726,13 +729,16 @@ if(SYSBUILD)
endif()
elseif(CONFIG_BOOT_SIGNATURE_TYPE_ECDSA_P256)
set(boot_tlv_estimate 150)
elseif(CONFIG_BOOT_SIGNATURE_TYPE_ECDSA_P384)
set(boot_tlv_estimate 216)
elseif(CONFIG_BOOT_SIGNATURE_TYPE_ED25519)
set(boot_tlv_estimate 144)
else()
set(boot_tlv_estimate 40)
endif()

if(CONFIG_BOOT_ENCRYPT_RSA OR CONFIG_BOOT_ENCRYPT_EC256 OR CONFIG_BOOT_ENCRYPT_X25519)
if(CONFIG_BOOT_ENCRYPT_RSA OR CONFIG_BOOT_ENCRYPT_EC256 OR CONFIG_BOOT_ENCRYPT_EC384 OR
CONFIG_BOOT_ENCRYPT_X25519)
# 128-bit AES key size
set(boot_enc_key_size 16)

Expand All @@ -741,6 +747,8 @@ if(SYSBUILD)
set(key_size 256)
elseif(CONFIG_BOOT_ENCRYPT_EC256)
math(EXPR key_size "65 + 32 + ${boot_enc_key_size}")
elseif(CONFIG_BOOT_ENCRYPT_EC384)
math(EXPR key_size "97 + 48 + ${boot_enc_key_size}")
elseif(CONFIG_BOOT_ENCRYPT_X25519)
math(EXPR key_size "32 + 32 + ${boot_enc_key_size}")
endif()
Expand Down
Loading
Loading