Skip to content

Commit 207f850

Browse files
committed
bootutil: Add support for HAMC-SHA512 with ECIES-X25519
Add support for HKDF/HMAC based on SHA512 for ECIES-X25519 key exchange. The commit adds MCUBOOT_HMAC_SHA512 that enables new TLV IMAGE_TLV_ENC_X25519_SHA512. Encryption code has been altered to support the MCUBOOT_HMAC_SHA512. Signed-off-by: Dominik Ermel <[email protected]>
1 parent 6d6e061 commit 207f850

File tree

4 files changed

+31
-5
lines changed

4 files changed

+31
-5
lines changed

boot/bootutil/include/bootutil/enc_key_public.h

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,19 @@ extern "C" {
7070
# define BOOT_ENC_KEY_SIZE 16
7171
#endif
7272

73+
#ifdef MCUBOOT_HMAC_SHA512
74+
# define BOOT_HMAC_SIZE 64
75+
#else
76+
# define BOOT_HMAC_SIZE 32
77+
#endif
78+
7379
#if defined(MCUBOOT_ENCRYPT_RSA)
7480
# define BOOT_ENC_TLV_SIZE (256)
7581
# define BOOT_ENC_TLV IMAGE_TLV_ENC_RSA2048
7682
#elif defined(MCUBOOT_ENCRYPT_EC256)
83+
# if defined(MCUBOOT_HMAC_SHA512)
84+
# error "ECIES-P256 does not support HMAC-SHA512"
85+
# endif
7786
# define EC_PUBK_LEN (65)
7887
# define EC_PRIVK_LEN (32)
7988
# define EC_SHARED_LEN (32)
@@ -82,7 +91,11 @@ extern "C" {
8291
# define EC_PUBK_LEN (32)
8392
# define EC_PRIVK_LEN (32)
8493
# define EC_SHARED_LEN (32)
85-
# define BOOT_ENC_TLV IMAGE_TLV_ENC_X25519
94+
# if !defined(MCUBOOT_HMAC_SHA512)
95+
# define BOOT_ENC_TLV IMAGE_TLV_ENC_X25519
96+
# else
97+
# define BOOT_ENC_TLV IMAGE_TLV_ENC_X25519_SHA512
98+
# endif
8699
#elif defined(MCUBOOT_ENCRYPT_KW)
87100
# define BOOT_ENC_TLV_SIZE (BOOT_ENC_KEY_SIZE + 8)
88101
# define BOOT_ENC_TLV IMAGE_TLV_ENC_KW
@@ -91,7 +104,7 @@ extern "C" {
91104
/* Common ECIES definitions */
92105
#if defined(EC_PUBK_LEN)
93106
# define EC_PUBK_INDEX (0)
94-
# define EC_TAG_LEN (32)
107+
# define EC_TAG_LEN (BOOT_HMAC_SIZE)
95108
# define EC_TAG_INDEX (EC_PUBK_INDEX + EC_PUBK_LEN)
96109
# define EC_CIPHERKEY_INDEX (EC_TAG_INDEX + EC_TAG_LEN)
97110
# define EC_CIPHERKEY_LEN BOOT_ENC_KEY_SIZE

boot/bootutil/include/bootutil/image.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ struct flash_area;
115115
#define IMAGE_TLV_ENC_KW 0x31 /* Key encrypted with AES-KW 128 or 256*/
116116
#define IMAGE_TLV_ENC_EC256 0x32 /* Key encrypted with ECIES-EC256 */
117117
#define IMAGE_TLV_ENC_X25519 0x33 /* Key encrypted with ECIES-X25519 */
118+
#define IMAGE_TLV_ENC_X25519_SHA512 0x34 /* Key exchange using ECIES-X25519 and SHA512 for MAC
119+
* tag and HKDF in key derivation process
120+
*/
118121
#define IMAGE_TLV_DEPENDENCY 0x40 /* Image depends on other image */
119122
#define IMAGE_TLV_SEC_CNT 0x50 /* security counter */
120123
#define IMAGE_TLV_BOOT_RECORD 0x60 /* measured boot record */

boot/bootutil/src/encrypted_psa.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@
2727

2828
BOOT_LOG_MODULE_DECLARE(mcuboot_psa_enc);
2929

30+
#if defined(MCUBOOT_HMAC_SHA512)
31+
#define PSA_HMAC_HKDF_SHA PSA_ALG_SHA_512
32+
#else
33+
#define PSA_HMAC_HKDF_SHA PSA_ALG_SHA_256
34+
#endif
35+
3036
#define X25519_OID "\x6e"
3137
static const uint8_t ec_pubkey_oid[] = MBEDTLS_OID_ISO_IDENTIFIED_ORG \
3238
MBEDTLS_OID_ORG_GOV X25519_OID;
@@ -160,7 +166,7 @@ boot_decrypt_key(const uint8_t *buf, uint8_t *enckey)
160166
return -1;
161167
}
162168

163-
key_do_alg = PSA_ALG_KEY_AGREEMENT(PSA_ALG_ECDH, PSA_ALG_HKDF(PSA_ALG_SHA_256));
169+
key_do_alg = PSA_ALG_KEY_AGREEMENT(PSA_ALG_ECDH, PSA_ALG_HKDF(PSA_HMAC_HKDF_SHA));
164170

165171
psa_ret = psa_key_derivation_setup(&key_do, key_do_alg);
166172
if (psa_ret != PSA_SUCCESS) {
@@ -223,7 +229,7 @@ boot_decrypt_key(const uint8_t *buf, uint8_t *enckey)
223229
*/
224230
psa_set_key_type(&kattr, PSA_KEY_TYPE_HMAC);
225231
psa_set_key_usage_flags(&kattr, PSA_KEY_USAGE_VERIFY_MESSAGE);
226-
psa_set_key_algorithm(&kattr, PSA_ALG_HMAC(PSA_ALG_SHA_256));
232+
psa_set_key_algorithm(&kattr, PSA_ALG_HMAC(PSA_HMAC_HKDF_SHA));
227233

228234
/* Import the MAC tag key part of derived key */
229235
psa_ret = psa_import_key(&kattr,
@@ -237,7 +243,7 @@ boot_decrypt_key(const uint8_t *buf, uint8_t *enckey)
237243
}
238244

239245
/* Verify the MAC tag of the random encryption key */
240-
psa_ret = psa_mac_verify(kid, PSA_ALG_HMAC(PSA_ALG_SHA_256),
246+
psa_ret = psa_mac_verify(kid, PSA_ALG_HMAC(PSA_HMAC_HKDF_SHA),
241247
&buf[EC_CIPHERKEY_INDEX], EC_CIPHERKEY_LEN,
242248
&buf[EC_TAG_INDEX],
243249
EC_TAG_LEN);

boot/bootutil/src/image_validate.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,11 @@ static const uint16_t allowed_unprot_tlvs[] = {
463463
IMAGE_TLV_ENC_RSA2048,
464464
IMAGE_TLV_ENC_KW,
465465
IMAGE_TLV_ENC_EC256,
466+
#if !defined(MCUBOOT_HMAC_SHA512)
466467
IMAGE_TLV_ENC_X25519,
468+
#else
469+
IMAGE_TLV_ENC_X25519_SHA512,
470+
#endif
467471
/* Mark end with ANY. */
468472
IMAGE_TLV_ANY,
469473
};

0 commit comments

Comments
 (0)