Skip to content

Commit a5c48f3

Browse files
ahasztagde-nordic
authored andcommitted
imgtool: Allow for AES256 encryption
Modified code to correctly generate the TLV for AES256 Signed-off-by: Artur Hadasz <[email protected]>
1 parent fc8477e commit a5c48f3

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

scripts/imgtool/image.py

100644100755
Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -444,14 +444,23 @@ def ecies_hkdf(self, enckey, plainkey, hmac_sha_alg):
444444
else:
445445
newpk = X25519PrivateKey.generate()
446446
shared = newpk.exchange(enckey._get_public())
447+
448+
# Detect AES key length from plainkey size
449+
key_len = len(plainkey) # 16 for AES-128, 32 for AES-256
450+
451+
# Generate derived key with appropriate length (key_len + 32 bytes for HMAC)
447452
derived_key = HKDF(
448-
algorithm=hmac_sha_alg, length=16 + hmac_sha_alg.digest_size, salt=None,
453+
algorithm=hmac_sha_alg, length=key_len + hmac_sha_alg.digest_size, salt=None,
449454
info=b'MCUBoot_ECIES_v1', backend=default_backend()).derive(shared)
450-
encryptor = Cipher(algorithms.AES(derived_key[:16]),
455+
456+
# Use appropriate key length for AES encryption
457+
encryptor = Cipher(algorithms.AES(derived_key[:key_len]),
451458
modes.CTR(bytes([0] * 16)),
452459
backend=default_backend()).encryptor()
453460
cipherkey = encryptor.update(plainkey) + encryptor.finalize()
454-
mac = hmac.HMAC(derived_key[16:], hmac_sha_alg,
461+
462+
# Use remaining bytes for HMAC (after the AES key)
463+
mac = hmac.HMAC(derived_key[key_len:], hmac_sha_alg,
455464
backend=default_backend())
456465
mac.update(cipherkey)
457466
ciphermac = mac.finalize()

0 commit comments

Comments
 (0)