Skip to content

Commit e94cd20

Browse files
committed
bootutil: Allow bypassing ASN.1 encoding for ED25519 key import
The commit adds MCUBOOT_KEY_IMPORT_BYPASS_ASN configuration option that allows bypassing ASN.1 decoding of ED25519 public key, compiled into MCUboot. When the option is enabled the key will be accessed directly and ASN.1 processing is not compiled in, resulting in smaller footprint of MCUboot, at a cost of reduced detection of invalid key, i.e. public key designated for different method than compiled in. Signed-off-by: Dominik Ermel <[email protected]>
1 parent 6cd7edc commit e94cd20

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

boot/bootutil/src/image_ed25519.c

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,18 @@
2626

2727
#define EDDSA_SIGNATURE_LENGTH 64
2828

29-
static const uint8_t ed25519_pubkey_oid[] = MBEDTLS_OID_ISO_IDENTIFIED_ORG "\x65\x70";
3029
#define NUM_ED25519_BYTES 32
3130

3231
extern int ED25519_verify(const uint8_t *message, size_t message_len,
3332
const uint8_t signature[EDDSA_SIGNATURE_LENGTH],
3433
const uint8_t public_key[NUM_ED25519_BYTES]);
3534

35+
#if !defined(MCUBOOT_KEY_IMPORT_BYPASS_ASN)
3636
/*
3737
* Parse the public key used for signing.
3838
*/
39+
static const uint8_t ed25519_pubkey_oid[] = MBEDTLS_OID_ISO_IDENTIFIED_ORG "\x65\x70";
40+
3941
static int
4042
bootutil_import_key(uint8_t **cp, uint8_t *end)
4143
{
@@ -71,6 +73,7 @@ bootutil_import_key(uint8_t **cp, uint8_t *end)
7173

7274
return 0;
7375
}
76+
#endif /* !defined(MCUBOOT_KEY_IMPORT_BYPASS_ASN) */
7477

7578
fih_ret
7679
bootutil_verify_sig(uint8_t *hash, uint32_t hlen, uint8_t *sig, size_t slen,
@@ -89,11 +92,25 @@ bootutil_verify_sig(uint8_t *hash, uint32_t hlen, uint8_t *sig, size_t slen,
8992
pubkey = (uint8_t *)bootutil_keys[key_id].key;
9093
end = pubkey + *bootutil_keys[key_id].len;
9194

95+
#if !defined(MCUBOOT_KEY_IMPORT_BYPASS_ASN)
9296
rc = bootutil_import_key(&pubkey, end);
9397
if (rc) {
9498
FIH_SET(fih_rc, FIH_FAILURE);
9599
goto out;
96100
}
101+
#else
102+
/* Directly use the key contents from the ASN stream,
103+
* these are the last NUM_ED25519_BYTES.
104+
* There is no check whether this is the correct key,
105+
* here, by the algorithm selected.
106+
*/
107+
if (*bootutil_keys[key_id].len < NUM_ED25519_BYTES) {
108+
FIH_SET(fih_rc, FIH_FAILURE);
109+
goto out;
110+
}
111+
112+
pubkey = end - NUM_ED25519_BYTES;
113+
#endif
97114

98115
rc = ED25519_verify(hash, IMAGE_HASH_SIZE, sig, pubkey);
99116

boot/zephyr/include/mcuboot_config/mcuboot_config.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@
3434
# error "One crypto library implementation allowed at a time."
3535
#endif
3636

37+
#if defined(CONFIG_BOOT_KEY_IMPORT_BYPASS_ASN)
38+
#define MCUBOOT_KEY_IMPORT_BYPASS_ASN
39+
#endif
40+
3741
#ifdef CONFIG_BOOT_USE_MBEDTLS
3842
#define MCUBOOT_USE_MBED_TLS
3943
#elif defined(CONFIG_BOOT_USE_TINYCRYPT)

0 commit comments

Comments
 (0)