Skip to content

Commit 1dcfbda

Browse files
de-nordicdavidvincze
authored andcommitted
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 f2b6def commit 1dcfbda

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

boot/bootutil/src/image_ed25519.c

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,30 @@
1313
#ifdef MCUBOOT_SIGN_ED25519
1414
#include "bootutil/sign_key.h"
1515

16+
#if !defined(MCUBOOT_KEY_IMPORT_BYPASS_ASN)
1617
/* We are not really using the MBEDTLS but need the ASN.1 parsing functions */
1718
#define MBEDTLS_ASN1_PARSE_C
1819
#include "mbedtls/oid.h"
1920
#include "mbedtls/asn1.h"
21+
#endif
2022

2123
#include "bootutil_priv.h"
2224
#include "bootutil/crypto/common.h"
2325
#include "bootutil/crypto/sha.h"
2426

2527
#define EDDSA_SIGNATURE_LENGTH 64
26-
27-
static const uint8_t ed25519_pubkey_oid[] = MBEDTLS_OID_ISO_IDENTIFIED_ORG "\x65\x70";
2828
#define NUM_ED25519_BYTES 32
2929

3030
extern int ED25519_verify(const uint8_t *message, size_t message_len,
3131
const uint8_t signature[EDDSA_SIGNATURE_LENGTH],
3232
const uint8_t public_key[NUM_ED25519_BYTES]);
3333

34+
#if !defined(MCUBOOT_KEY_IMPORT_BYPASS_ASN)
3435
/*
3536
* Parse the public key used for signing.
3637
*/
38+
static const uint8_t ed25519_pubkey_oid[] = MBEDTLS_OID_ISO_IDENTIFIED_ORG "\x65\x70";
39+
3740
static int
3841
bootutil_import_key(uint8_t **cp, uint8_t *end)
3942
{
@@ -69,6 +72,7 @@ bootutil_import_key(uint8_t **cp, uint8_t *end)
6972

7073
return 0;
7174
}
75+
#endif /* !defined(MCUBOOT_KEY_IMPORT_BYPASS_ASN) */
7276

7377
/* Signature verification base function.
7478
* The function takes buffer of specified length and tries to verify
@@ -94,11 +98,25 @@ bootutil_verify(uint8_t *buf, uint32_t blen,
9498
pubkey = (uint8_t *)bootutil_keys[key_id].key;
9599
end = pubkey + *bootutil_keys[key_id].len;
96100

101+
#if !defined(MCUBOOT_KEY_IMPORT_BYPASS_ASN)
97102
rc = bootutil_import_key(&pubkey, end);
98103
if (rc) {
99104
FIH_SET(fih_rc, FIH_FAILURE);
100105
goto out;
101106
}
107+
#else
108+
/* Directly use the key contents from the ASN stream,
109+
* these are the last NUM_ED25519_BYTES.
110+
* There is no check whether this is the correct key,
111+
* here, by the algorithm selected.
112+
*/
113+
if (*bootutil_keys[key_id].len < NUM_ED25519_BYTES) {
114+
FIH_SET(fih_rc, FIH_FAILURE);
115+
goto out;
116+
}
117+
118+
pubkey = end - NUM_ED25519_BYTES;
119+
#endif
102120

103121
rc = ED25519_verify(buf, blen, sig, pubkey);
104122

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)