Skip to content

Commit 5054696

Browse files
committed
bootutil: PureEdDSA using ED25519
The commit adds support for PureEdDSA, which validates signature of image rather than hash. This is most secure, available, ED25519 usage in MCUboot, but due to requirement of PureEdDSA to be able to calculate signature at whole message at once, here image, it only works on setups where entire image can be mapped to device address space, so that PSA functions calculating the signature can see the whole image at once. This option is enabled with Kconfig option: CONFIG_BOOT_SIGNATURE_TYPE_PURE when the ED25519 signature type is already selected. Note that the option will enable SHA512 for calculating public key hash. Signed-off-by: Dominik Ermel <[email protected]>
1 parent fe5bbcd commit 5054696

File tree

5 files changed

+156
-8
lines changed

5 files changed

+156
-8
lines changed

boot/bootutil/src/bootutil_priv.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,9 @@ struct boot_loader_state {
268268
fih_ret bootutil_verify_sig(uint8_t *hash, uint32_t hlen, uint8_t *sig,
269269
size_t slen, uint8_t key_id);
270270

271+
fih_ret bootutil_verify_img(const uint8_t *img, uint32_t size,
272+
uint8_t *sig, size_t slen, uint8_t key_id);
273+
271274
fih_ret boot_fih_memequal(const void *s1, const void *s2, size_t n);
272275

273276
int boot_find_status(int image_index, const struct flash_area **fap);

boot/bootutil/src/image_ed25519.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,41 @@ bootutil_verify_sig(uint8_t *hash, uint32_t hlen, uint8_t *sig, size_t slen,
105105
FIH_RET(fih_rc);
106106
}
107107

108+
fih_ret
109+
bootutil_verify_img(const uint8_t *img, uint32_t size,
110+
uint8_t *sig, size_t slen, uint8_t key_id)
111+
{
112+
int rc;
113+
FIH_DECLARE(fih_rc, FIH_FAILURE);
114+
uint8_t *pubkey;
115+
uint8_t *end;
116+
117+
if (slen != EDDSA_SIGNATURE_LENGTH) {
118+
FIH_SET(fih_rc, FIH_FAILURE);
119+
goto out;
120+
}
121+
122+
pubkey = (uint8_t *)bootutil_keys[key_id].key;
123+
end = pubkey + *bootutil_keys[key_id].len;
124+
125+
rc = bootutil_import_key(&pubkey, end);
126+
if (rc) {
127+
FIH_SET(fih_rc, FIH_FAILURE);
128+
goto out;
129+
}
130+
131+
rc = ED25519_verify(img, size, sig, pubkey);
132+
133+
if (rc == 0) {
134+
/* if verify returns 0, there was an error. */
135+
FIH_SET(fih_rc, FIH_FAILURE);
136+
goto out;
137+
}
138+
139+
FIH_SET(fih_rc, FIH_SUCCESS);
140+
out:
141+
142+
FIH_RET(fih_rc);
143+
}
144+
108145
#endif /* MCUBOOT_SIGN_ED25519 */

boot/bootutil/src/image_validate.c

Lines changed: 81 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656

5757
#include "bootutil_priv.h"
5858

59+
#ifndef MCUBOOT_SIGN_PURE
5960
/*
6061
* Compute SHA hash over the image.
6162
* (SHA384 if ECDSA-P384 is being used,
@@ -175,6 +176,7 @@ bootutil_img_hash(struct enc_key_data *enc_state, int image_index,
175176

176177
return 0;
177178
}
179+
#endif
178180

179181
/*
180182
* Currently, we only support being able to verify one type of
@@ -361,6 +363,35 @@ bootutil_get_img_security_cnt(struct image_header *hdr,
361363
return 0;
362364
}
363365

366+
#if defined(MCUBOOT_SIGN_PURE)
367+
/* Returns:
368+
* 0 -- found
369+
* 1 -- not found
370+
* -1 -- failed for some reason
371+
*
372+
* Value of TLV does not matter, presence decides.
373+
*/
374+
static int bootutil_check_for_pure(const struct image_header *hdr,
375+
const struct flash_area *fap)
376+
{
377+
struct image_tlv_iter it;
378+
uint32_t off;
379+
uint16_t len;
380+
int32_t rc;
381+
382+
rc = bootutil_tlv_iter_begin(&it, hdr, fap, IMAGE_TLV_SIG_PURE, false);
383+
if (rc) {
384+
return rc;
385+
}
386+
387+
/* Search for the TLV */
388+
rc = bootutil_tlv_iter_next(&it, &off, &len, NULL);
389+
390+
return rc;
391+
}
392+
#endif
393+
394+
364395
#ifndef ALLOW_ROGUE_TLVS
365396
/*
366397
* The following list of TLVs are the only entries allowed in the unprotected
@@ -377,6 +408,9 @@ static const uint16_t allowed_unprot_tlvs[] = {
377408
IMAGE_TLV_ECDSA_SIG,
378409
IMAGE_TLV_RSA3072_PSS,
379410
IMAGE_TLV_ED25519,
411+
#if defined(MCUBOOT_SIGN_PURE)
412+
IMAGE_TLV_SIG_PURE,
413+
#endif
380414
IMAGE_TLV_ENC_RSA2048,
381415
IMAGE_TLV_ENC_KW,
382416
IMAGE_TLV_ENC_EC256,
@@ -399,7 +433,6 @@ bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
399433
uint32_t off;
400434
uint16_t len;
401435
uint16_t type;
402-
int image_hash_valid = 0;
403436
#ifdef EXPECTED_SIG_TLV
404437
FIH_DECLARE(valid_signature, FIH_FAILURE);
405438
#ifndef MCUBOOT_BUILTIN_KEY
@@ -416,7 +449,10 @@ bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
416449
#endif /* EXPECTED_SIG_TLV */
417450
struct image_tlv_iter it;
418451
uint8_t buf[SIG_BUF_SIZE];
452+
#if defined(EXPECTED_HASH_TLV) && !defined(MCUBOOT_SIGN_PURE)
453+
int image_hash_valid = 0;
419454
uint8_t hash[IMAGE_HASH_SIZE];
455+
#endif
420456
int rc = 0;
421457
FIH_DECLARE(fih_rc, FIH_FAILURE);
422458
#ifdef MCUBOOT_HW_ROLLBACK_PROT
@@ -425,6 +461,7 @@ bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
425461
FIH_DECLARE(security_counter_valid, FIH_FAILURE);
426462
#endif
427463

464+
#if defined(EXPECTED_HASH_TLV) && !defined(MCUBOOT_SIGN_PURE)
428465
rc = bootutil_img_hash(enc_state, image_index, hdr, fap, tmp_buf,
429466
tmp_buf_sz, hash, seed, seed_len);
430467
if (rc) {
@@ -434,6 +471,15 @@ bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
434471
if (out_hash) {
435472
memcpy(out_hash, hash, IMAGE_HASH_SIZE);
436473
}
474+
#endif
475+
476+
#if defined(MCUBOOT_SIGN_PURE)
477+
/* If Pure type signature is expected then it has to be there */
478+
rc = bootutil_check_for_pure(hdr, fap);
479+
if (rc != 0) {
480+
goto out;
481+
}
482+
#endif
437483

438484
rc = bootutil_tlv_iter_begin(&it, hdr, fap, IMAGE_TLV_ANY, false);
439485
if (rc) {
@@ -477,8 +523,10 @@ bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
477523
}
478524
}
479525
#endif
480-
481-
if (type == EXPECTED_HASH_TLV) {
526+
switch(type) {
527+
#if defined(EXPECTED_HASH_TLV) && !defined(MCUBOOT_SIGN_PURE)
528+
case EXPECTED_HASH_TLV:
529+
{
482530
/* Verify the image hash. This must always be present. */
483531
if (len != sizeof(hash)) {
484532
rc = -1;
@@ -496,8 +544,12 @@ bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
496544
}
497545

498546
image_hash_valid = 1;
547+
break;
548+
}
549+
#endif /* defined(EXPECTED_HASH_TLV) && !defined(MCUBOOT_SIGN_PURE) */
499550
#ifdef EXPECTED_KEY_TLV
500-
} else if (type == EXPECTED_KEY_TLV) {
551+
case EXPECTED_KEY_TLV:
552+
{
501553
/*
502554
* Determine which key we should be checking.
503555
*/
@@ -522,9 +574,12 @@ bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
522574
* The key may not be found, which is acceptable. There
523575
* can be multiple signatures, each preceded by a key.
524576
*/
577+
break;
578+
}
525579
#endif /* EXPECTED_KEY_TLV */
526580
#ifdef EXPECTED_SIG_TLV
527-
} else if (type == EXPECTED_SIG_TLV) {
581+
case EXPECTED_SIG_TLV:
582+
{
528583
/* Ignore this signature if it is out of bounds. */
529584
if (key_id < 0 || key_id >= bootutil_key_cnt) {
530585
key_id = -1;
@@ -538,12 +593,25 @@ bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
538593
if (rc) {
539594
goto out;
540595
}
596+
#ifndef MCUBOOT_SIGN_PURE
541597
FIH_CALL(bootutil_verify_sig, valid_signature, hash, sizeof(hash),
542598
buf, len, key_id);
599+
#else
600+
/* Directly check signature on the image, by using the mapping of
601+
* a device to memory. The pointer is beginning of image in flash,
602+
* so offset of area, the range is header + image + protected tlvs.
603+
*/
604+
FIH_CALL(bootutil_verify_img, valid_signature, (void *)flash_area_get_off(fap),
605+
hdr->ih_hdr_size + hdr->ih_img_size + hdr->ih_protect_tlv_size,
606+
buf, len, key_id);
607+
#endif
543608
key_id = -1;
609+
break;
610+
}
544611
#endif /* EXPECTED_SIG_TLV */
545612
#ifdef MCUBOOT_HW_ROLLBACK_PROT
546-
} else if (type == IMAGE_TLV_SEC_CNT) {
613+
case IMAGE_TLV_SEC_CNT:
614+
{
547615
/*
548616
* Verify the image's security counter.
549617
* This must always be present.
@@ -578,14 +646,21 @@ bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
578646

579647
/* The image's security counter has been successfully verified. */
580648
security_counter_valid = fih_rc;
649+
break;
650+
}
581651
#endif /* MCUBOOT_HW_ROLLBACK_PROT */
582652
}
583653
}
584654

655+
#if defined(EXPECTED_HASH_TLV) && !defined(MCUBOOT_SIGN_PURE)
585656
rc = !image_hash_valid;
586657
if (rc) {
587658
goto out;
588659
}
660+
#elif defined(MCUBOOT_SIGN_PURE)
661+
/* This returns true on EQ, rc is err on non-0 */
662+
rc = !FIH_EQ(valid_signature, FIH_SUCCESS);
663+
#endif
589664
#ifdef EXPECTED_SIG_TLV
590665
FIH_SET(fih_rc, valid_signature);
591666
#endif

boot/zephyr/Kconfig

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,14 @@ config BOOT_IMG_HASH_ALG_SHA512
134134

135135
endchoice # BOOT_IMG_HASH_ALG
136136

137+
config BOOT_SIGNATURE_TYPE_PURE_ALLOW
138+
bool
139+
help
140+
Hidden option set by configurations that allow Pure variant,
141+
for example ed25519. The pure variant means that image
142+
signature is calculated over entire image instead of hash
143+
of an image.
144+
137145
choice BOOT_SIGNATURE_TYPE
138146
prompt "Signature type"
139147
default BOOT_SIGNATURE_TYPE_RSA
@@ -183,10 +191,31 @@ endif
183191

184192
config BOOT_SIGNATURE_TYPE_ED25519
185193
bool "Edwards curve digital signatures using ed25519"
186-
select BOOT_ENCRYPTION_SUPPORT
187-
select BOOT_IMG_HASH_ALG_SHA256_ALLOW
194+
select BOOT_ENCRYPTION_SUPPORT if !BOOT_SIGNATURE_TYPE_PURE
195+
select BOOT_IMG_HASH_ALG_SHA256_ALLOW if !BOOT_SIGNATURE_TYPE_PURE
196+
# The SHA is used only for key hashing, not for images.
197+
select BOOT_SIGNATURE_TYPE_PURE_ALLOW
198+
help
199+
This is ed25519 signature calculated over SHA512 of SHA256 of application
200+
image; that is not completely correct approach as the SHA512 should be
201+
rather directly calculated over an image.
202+
Select BOOT_SIGNATURE_TYPE_PURE to have a PureEdDSA calculating image
203+
signature directly on image, rather than hash of the image.
188204

189205
if BOOT_SIGNATURE_TYPE_ED25519
206+
207+
config BOOT_SIGNATURE_TYPE_PURE
208+
bool "Use Pure signature of image"
209+
depends on BOOT_SIGNATURE_TYPE_PURE_ALLOW
210+
help
211+
The Pure signature is calculated directly over image rather than
212+
hash of an image.
213+
This is more secure signature, specifically if hardware can do the
214+
verification without need to share key.
215+
Note that this requires that all slots for which signature is to be
216+
verified need to be accessible through memory address space that
217+
cryptography can access.
218+
190219
choice BOOT_ED25519_IMPLEMENTATION
191220
prompt "Ecdsa implementation"
192221
default BOOT_ED25519_TINYCRYPT

boot/zephyr/include/mcuboot_config/mcuboot_config.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,10 @@
148148
#define MCUBOOT_HASH_STORAGE_DIRECTLY
149149
#endif
150150

151+
#ifdef CONFIG_BOOT_SIGNATURE_TYPE_PURE
152+
#define MCUBOOT_SIGN_PURE
153+
#endif
154+
151155
#ifdef CONFIG_BOOT_BOOTSTRAP
152156
#define MCUBOOT_BOOTSTRAP 1
153157
#endif

0 commit comments

Comments
 (0)