@@ -97,9 +97,6 @@ key_unwrap(const uint8_t *wrapped, uint8_t *enckey, struct bootutil_key *bootuti
9797static const uint8_t ec_pubkey_oid [] = MBEDTLS_OID_EC_ALG_UNRESTRICTED ;
9898static const uint8_t ec_secp256r1_oid [] = MBEDTLS_OID_EC_GRP_SECP256R1 ;
9999
100- #define SHARED_KEY_LEN NUM_ECC_BYTES
101- #define PRIV_KEY_LEN NUM_ECC_BYTES
102-
103100/*
104101 * Parses the output of `imgtool keygen`, which produces a PKCS#8 elliptic
105102 * curve keypair. See RFC5208 and RFC5915.
@@ -179,9 +176,6 @@ parse_ec256_enckey(uint8_t **p, uint8_t *end, uint8_t *private_key)
179176static const uint8_t ec_pubkey_oid [] = MBEDTLS_OID_ISO_IDENTIFIED_ORG \
180177 MBEDTLS_OID_ORG_GOV X25519_OID ;
181178
182- #define SHARED_KEY_LEN 32
183- #define PRIV_KEY_LEN 32
184-
185179static int
186180parse_x25519_enckey (uint8_t * * p , uint8_t * end , uint8_t * private_key )
187181{
@@ -221,11 +215,11 @@ parse_x25519_enckey(uint8_t **p, uint8_t *end, uint8_t *private_key)
221215 return -7 ;
222216 }
223217
224- if (len != PRIV_KEY_LEN ) {
218+ if (len != EC_PRIVK_LEN ) {
225219 return -8 ;
226220 }
227221
228- memcpy (private_key , * p , PRIV_KEY_LEN );
222+ memcpy (private_key , * p , EC_PRIVK_LEN );
229223 return 0 ;
230224}
231225#endif /* defined(MCUBOOT_ENCRYPT_X25519) */
@@ -399,11 +393,11 @@ boot_decrypt_key(const uint8_t *buf, uint8_t *enckey)
399393 bootutil_hmac_sha256_context hmac ;
400394 bootutil_aes_ctr_context aes_ctr ;
401395 uint8_t tag [BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE ];
402- uint8_t shared [SHARED_KEY_LEN ];
396+ uint8_t shared [EC_SHARED_LEN ];
403397 uint8_t derived_key [BOOT_ENC_KEY_SIZE + BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE ];
404398 uint8_t * cp ;
405399 uint8_t * cpend ;
406- uint8_t private_key [PRIV_KEY_LEN ];
400+ uint8_t private_key [EC_PRIVK_LEN ];
407401 uint8_t counter [BOOT_ENC_BLOCK_SIZE ];
408402 uint16_t len ;
409403#endif
@@ -509,7 +503,7 @@ boot_decrypt_key(const uint8_t *buf, uint8_t *enckey)
509503 */
510504
511505 len = BOOT_ENC_KEY_SIZE + BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE ;
512- rc = hkdf (shared , SHARED_KEY_LEN , (uint8_t * )"MCUBoot_ECIES_v1" , 16 ,
506+ rc = hkdf (shared , EC_SHARED_LEN , (uint8_t * )"MCUBoot_ECIES_v1" , 16 ,
513507 derived_key , & len );
514508 if (rc != 0 || len != (BOOT_ENC_KEY_SIZE + BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE )) {
515509 return -1 ;
@@ -521,6 +515,9 @@ boot_decrypt_key(const uint8_t *buf, uint8_t *enckey)
521515
522516 bootutil_hmac_sha256_init (& hmac );
523517
518+ /* First BOOT_ENC_KEY_SIZE are used for decryption, remaining 32 bytes are used
519+ * for MAC tag key
520+ */
524521 rc = bootutil_hmac_sha256_set_key (& hmac , & derived_key [BOOT_ENC_KEY_SIZE ], 32 );
525522 if (rc != 0 ) {
526523 (void )bootutil_hmac_sha256_drop (& hmac );
@@ -540,7 +537,7 @@ boot_decrypt_key(const uint8_t *buf, uint8_t *enckey)
540537 return -1 ;
541538 }
542539
543- if (bootutil_constant_time_compare (tag , & buf [EC_TAG_INDEX ], 32 ) != 0 ) {
540+ if (bootutil_constant_time_compare (tag , & buf [EC_TAG_INDEX ], EC_TAG_LEN ) != 0 ) {
544541 return -1 ;
545542 }
546543
0 commit comments