Skip to content

Commit 1d83177

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 e542295 commit 1d83177

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
@@ -113,6 +113,9 @@ extern "C" {
113113
#define IMAGE_TLV_ENC_KW 0x31 /* Key encrypted with AES-KW 128 or 256*/
114114
#define IMAGE_TLV_ENC_EC256 0x32 /* Key encrypted with ECIES-EC256 */
115115
#define IMAGE_TLV_ENC_X25519 0x33 /* Key encrypted with ECIES-X25519 */
116+
#define IMAGE_TLV_ENC_X25519_SHA512 0x34 /* Key exchange using ECIES-X25519 and SHA512 for MAC
117+
* tag and HKDF in key derivation process
118+
*/
116119
#define IMAGE_TLV_DEPENDENCY 0x40 /* Image depends on other image */
117120
#define IMAGE_TLV_SEC_CNT 0x50 /* security counter */
118121
#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;
@@ -162,7 +168,7 @@ boot_decrypt_key(const uint8_t *buf, uint8_t *enckey)
162168
return -1;
163169
}
164170

165-
key_do_alg = PSA_ALG_KEY_AGREEMENT(PSA_ALG_ECDH, PSA_ALG_HKDF(PSA_ALG_SHA_256));
171+
key_do_alg = PSA_ALG_KEY_AGREEMENT(PSA_ALG_ECDH, PSA_ALG_HKDF(PSA_HMAC_HKDF_SHA));
166172

167173
psa_ret = psa_key_derivation_setup(&key_do, key_do_alg);
168174
if (psa_ret != PSA_SUCCESS) {
@@ -225,7 +231,7 @@ boot_decrypt_key(const uint8_t *buf, uint8_t *enckey)
225231
*/
226232
psa_set_key_type(&kattr, PSA_KEY_TYPE_HMAC);
227233
psa_set_key_usage_flags(&kattr, PSA_KEY_USAGE_VERIFY_MESSAGE);
228-
psa_set_key_algorithm(&kattr, PSA_ALG_HMAC(PSA_ALG_SHA_256));
234+
psa_set_key_algorithm(&kattr, PSA_ALG_HMAC(PSA_HMAC_HKDF_SHA));
229235

230236
/* Import the MAC tag key part of derived key */
231237
psa_ret = psa_import_key(&kattr,
@@ -239,7 +245,7 @@ boot_decrypt_key(const uint8_t *buf, uint8_t *enckey)
239245
}
240246

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

boot/bootutil/src/image_validate.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,11 @@ static const uint16_t allowed_unprot_tlvs[] = {
474474
IMAGE_TLV_ENC_RSA2048,
475475
IMAGE_TLV_ENC_KW,
476476
IMAGE_TLV_ENC_EC256,
477+
#if !defined(MCUBOOT_HMAC_SHA512)
477478
IMAGE_TLV_ENC_X25519,
479+
#else
480+
IMAGE_TLV_ENC_X25519_SHA512,
481+
#endif
478482
/* Mark end with ANY. */
479483
IMAGE_TLV_ANY,
480484
};

0 commit comments

Comments
 (0)