Skip to content

Commit 059657a

Browse files
committed
[nrf fromlist] zephyr: Add support for AES256
This commit adds the parts in the tooling allowing AES256 to work with MCUBoot. Currently only in combination PSA + ED25519 Upstream PR #: 2406 Signed-off-by: Artur Hadasz <[email protected]>
1 parent 7810a8c commit 059657a

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

boot/zephyr/Kconfig

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,22 @@ config BOOT_ENCRYPT_X25519
717717
help
718718
Hidden option selecting x25519 encryption.
719719

720+
if BOOT_ENCRYPT_IMAGE
721+
722+
choice BOOT_ENCRYPT_ALG
723+
prompt "Algorithm used for image encryption"
724+
default BOOT_ENCRYPT_ALG_AES_128
725+
726+
config BOOT_ENCRYPT_ALG_AES_128
727+
bool "Use AES-128 for image encryption"
728+
729+
config BOOT_ENCRYPT_ALG_AES_256
730+
bool "Use AES-256 for image encryption"
731+
732+
endchoice # BOOT_ENCRYPT_ALG
733+
734+
endif # BOOT_ENCRYPT_IMAGE
735+
720736
if BOOT_ENCRYPT_X25519 && BOOT_USE_PSA_CRYPTO
721737

722738
choice BOOT_HMAC_SHA

boot/zephyr/include/mcuboot_config/mcuboot_config.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,14 @@
159159
#define MCUBOOT_ENCRYPT_X25519
160160
#endif
161161

162+
#ifdef CONFIG_BOOT_ENCRYPT_ALG_AES_128
163+
#define MCUBOOT_AES_128
164+
#endif
165+
166+
#ifdef CONFIG_BOOT_ENCRYPT_ALG_AES_256
167+
#define MCUBOOT_AES_256
168+
#endif
169+
162170
/* Support for HMAC/HKDF using SHA512; this is used in key exchange where
163171
* HKDF is used for key expansion and HMAC is used for key verification.
164172
*/

scripts/imgtool/image.py

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

0 commit comments

Comments
 (0)