Skip to content

Commit 8f0460a

Browse files
committed
[nrf mergeup] Merge upstream up to commit 018dc01
Regular upmerge. Signed-off-by: Robert Lubos <[email protected]>
2 parents f67a11a + 018dc01 commit 8f0460a

File tree

99 files changed

+830
-366
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

99 files changed

+830
-366
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
[travis]: https://travis-ci.org/mcu-tools/mcuboot
1919
[license]: https://github.com/mcu-tools/mcuboot/blob/main/LICENSE
2020

21-
This is MCUboot version 2.1.0-dev
21+
This is MCUboot version 2.1.0
2222

2323
MCUboot is a secure bootloader for 32-bits microcontrollers. It defines a
2424
common infrastructure for the bootloader and the system flash layout on

boot/boot_serial/src/boot_serial.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1169,10 +1169,6 @@ boot_serial_in_dec(char *in, int inlen, char *out, int *out_off, int maxout)
11691169
return 0;
11701170
}
11711171

1172-
if (len > *out_off - sizeof(uint16_t)) {
1173-
len = *out_off - sizeof(uint16_t);
1174-
}
1175-
11761172
out += sizeof(uint16_t);
11771173
#ifdef __ZEPHYR__
11781174
crc = crc16_itu_t(CRC16_INITIAL_CRC, out, len);

boot/bootutil/include/bootutil/crypto/ecdsa.h

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* SPDX-License-Identifier: Apache-2.0
33
*
4-
* Copyright (c) 2023 Arm Limited
4+
* Copyright (c) 2023-2024 Arm Limited
55
*/
66

77
/*
@@ -349,7 +349,7 @@ static void parse_signature_from_rfc5480_encoding(const uint8_t *sig,
349349
{
350350
const uint8_t *sig_ptr = NULL;
351351

352-
/* r or s can be greater than the expected size by one, due to the way
352+
/* r or s can be greater than the expected size by one, due to the way
353353
* ASN.1 encodes signed integers. If either r or s starts with a bit 1,
354354
* a zero byte will be added in front of the encoding
355355
*/
@@ -395,9 +395,26 @@ static const uint8_t Secp384r1[] = {0x06, 0x05, 0x2b, 0x81, 0x04, 0x00, 0x22};
395395

396396
static inline void bootutil_ecdsa_init(bootutil_ecdsa_context *ctx)
397397
{
398+
#if !defined(MCUBOOT_BUILTIN_KEY)
398399
ctx->key_id = PSA_KEY_ID_NULL;
399400
ctx->curve_byte_count = 0;
400401
ctx->required_algorithm = 0;
402+
403+
#else /* !MCUBOOT_BUILTIN_KEY */
404+
/* The incoming key ID is equal to the image index. The key ID value must be
405+
* shifted (by one in this case) because zero is reserved (PSA_KEY_ID_NULL)
406+
* and considered invalid.
407+
*/
408+
ctx->key_id++; /* Make sure it is not equal to 0. */
409+
#if defined(MCUBOOT_SIGN_EC256)
410+
ctx->curve_byte_count = 32;
411+
ctx->required_algorithm = PSA_ALG_SHA_256;
412+
#endif /* MCUBOOT_SIGN_EC256 */
413+
#if defined(MCUBOOT_SIGN_EC384)
414+
ctx->curve_byte_count = 48;
415+
ctx->required_algorithm = PSA_ALG_SHA_384;
416+
#endif /* MCUBOOT_SIGN_EC384 */
417+
#endif /* !MCUBOOT_BUILTIN_KEY */
401418
}
402419

403420
static inline void bootutil_ecdsa_drop(bootutil_ecdsa_context *ctx)
@@ -407,6 +424,7 @@ static inline void bootutil_ecdsa_drop(bootutil_ecdsa_context *ctx)
407424
}
408425
}
409426

427+
#if !defined(MCUBOOT_BUILTIN_KEY)
410428
/*
411429
* Parse a ECDSA public key with format specified in RFC5280 et al.
412430
*
@@ -451,6 +469,7 @@ static int bootutil_ecdsa_parse_public_key(bootutil_ecdsa_context *ctx,
451469

452470
return (int)psa_import_key(&key_attributes, *cp, key_size, &ctx->key_id);
453471
}
472+
#endif /* !MCUBOOT_BUILTIN_KEY */
454473

455474
/* Verify the signature against the provided hash. The signature gets parsed from
456475
* the encoding first, then PSA Crypto has a dedicated API for ECDSA verification

boot/bootutil/include/bootutil/image.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ int bootutil_tlv_iter_begin(struct image_tlv_iter *it,
186186
bool prot);
187187
int bootutil_tlv_iter_next(struct image_tlv_iter *it, uint32_t *off,
188188
uint16_t *len, uint16_t *type);
189+
int bootutil_tlv_iter_is_prot(struct image_tlv_iter *it, uint32_t off);
189190

190191
int32_t bootutil_get_img_security_cnt(struct image_header *hdr,
191192
const struct flash_area *fap,

boot/bootutil/pkg.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ pkg.deps:
4141
- "@apache-mynewt-core/sys/defs"
4242
- "@mcuboot/boot/mynewt/flash_map_backend"
4343

44+
pkg.ign_files.BOOTUTIL_SINGLE_APPLICATION_SLOT:
45+
- "loader.c"
46+
4447
pkg.deps.BOOTUTIL_USE_MBED_TLS:
4548
- "@apache-mynewt-core/crypto/mbedtls"
4649

boot/bootutil/src/encrypted.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,8 @@ hkdf(uint8_t *ikm, uint16_t ikm_len, uint8_t *info, uint16_t info_len,
276276
goto error;
277277
}
278278

279+
bootutil_hmac_sha256_drop(&hmac);
280+
279281
/*
280282
* Expand
281283
*/
@@ -315,6 +317,8 @@ hkdf(uint8_t *ikm, uint16_t ikm_len, uint8_t *info, uint16_t info_len,
315317
goto error;
316318
}
317319

320+
bootutil_hmac_sha256_drop(&hmac);
321+
318322
if (len > BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE) {
319323
memcpy(&okm[off], T, BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE);
320324
len -= BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE;
@@ -324,7 +328,6 @@ hkdf(uint8_t *ikm, uint16_t ikm_len, uint8_t *info, uint16_t info_len,
324328
}
325329
}
326330

327-
bootutil_hmac_sha256_drop(&hmac);
328331
return 0;
329332

330333
error:

boot/bootutil/src/fault_injection_hardening_delay_rng_mbedtls.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
#ifdef FIH_ENABLE_DELAY
1010

11-
#include "mcuboot-mbedtls-cfg.h"
1211
#include "mbedtls/ctr_drbg.h"
1312
#include "mbedtls/entropy.h"
1413

boot/bootutil/src/image_ecdsa.c

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright (c) 2016-2019 JUUL Labs
55
* Copyright (c) 2017 Linaro LTD
6-
* Copyright (C) 2021-2023 Arm Limited
6+
* Copyright (C) 2021-2024 Arm Limited
77
*
88
* Original license:
99
*
@@ -35,6 +35,7 @@
3535
#include "bootutil/fault_injection_hardening.h"
3636
#include "bootutil/crypto/ecdsa.h"
3737

38+
#if !defined(MCUBOOT_BUILTIN_KEY)
3839
fih_ret
3940
bootutil_verify_sig(uint8_t *hash, uint32_t hlen, uint8_t *sig, size_t slen,
4041
uint8_t key_id)
@@ -65,5 +66,30 @@ bootutil_verify_sig(uint8_t *hash, uint32_t hlen, uint8_t *sig, size_t slen,
6566

6667
FIH_RET(fih_rc);
6768
}
69+
#else /* !MCUBOOT_BUILTIN_KEY */
70+
fih_ret
71+
bootutil_verify_sig(uint8_t *hash, uint32_t hlen, uint8_t *sig, size_t slen,
72+
uint8_t key_id)
73+
{
74+
int rc;
75+
bootutil_ecdsa_context ctx;
76+
FIH_DECLARE(fih_rc, FIH_FAILURE);
77+
78+
/* Use builtin key for image verification, no key parsing is required. */
79+
ctx.key_id = key_id;
80+
bootutil_ecdsa_init(&ctx);
81+
82+
/* The public key pointer and key size can be omitted. */
83+
rc = bootutil_ecdsa_verify(&ctx, NULL, 0, hash, hlen, sig, slen);
84+
fih_rc = fih_ret_encode_zero_equality(rc);
85+
if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
86+
FIH_SET(fih_rc, FIH_FAILURE);
87+
}
88+
89+
bootutil_ecdsa_drop(&ctx);
90+
91+
FIH_RET(fih_rc);
92+
}
93+
#endif /* MCUBOOT_BUILTIN_KEY */
6894

6995
#endif /* MCUBOOT_SIGN_EC256 || MCUBOOT_SIGN_EC384 */

boot/bootutil/src/image_validate.c

Lines changed: 84 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright (c) 2017-2019 Linaro LTD
55
* Copyright (c) 2016-2019 JUUL Labs
6-
* Copyright (c) 2019-2023 Arm Limited
6+
* Copyright (c) 2019-2024 Arm Limited
77
*
88
* Original license:
99
*
@@ -201,7 +201,27 @@ bootutil_img_hash(struct enc_key_data *enc_state, int image_index,
201201
# define SIG_BUF_SIZE 32 /* no signing, sha256 digest only */
202202
#endif
203203

204+
#if (defined(MCUBOOT_HW_KEY) + \
205+
defined(MCUBOOT_BUILTIN_KEY)) > 1
206+
#error "Please use either MCUBOOT_HW_KEY or the MCUBOOT_BUILTIN_KEY feature."
207+
#endif
208+
204209
#ifdef EXPECTED_SIG_TLV
210+
211+
#if !defined(MCUBOOT_BUILTIN_KEY)
212+
#if !defined(MCUBOOT_HW_KEY)
213+
/* The key TLV contains the hash of the public key. */
214+
# define EXPECTED_KEY_TLV IMAGE_TLV_KEYHASH
215+
# define KEY_BUF_SIZE IMAGE_HASH_SIZE
216+
#else
217+
/* The key TLV contains the whole public key.
218+
* Add a few extra bytes to the key buffer size for encoding and
219+
* for public exponent.
220+
*/
221+
# define EXPECTED_KEY_TLV IMAGE_TLV_PUBKEY
222+
# define KEY_BUF_SIZE (SIG_BUF_SIZE + 24)
223+
#endif /* !MCUBOOT_HW_KEY */
224+
205225
#if !defined(MCUBOOT_HW_KEY)
206226
static int
207227
bootutil_find_key(uint8_t *keyhash, uint8_t keyhash_len)
@@ -228,7 +248,7 @@ bootutil_find_key(uint8_t *keyhash, uint8_t keyhash_len)
228248
bootutil_sha_drop(&sha_ctx);
229249
return -1;
230250
}
231-
#else
251+
#else /* !MCUBOOT_HW_KEY */
232252
extern unsigned int pub_key_len;
233253
static int
234254
bootutil_find_key(uint8_t image_index, uint8_t *key, uint16_t key_len)
@@ -266,7 +286,8 @@ bootutil_find_key(uint8_t image_index, uint8_t *key, uint16_t key_len)
266286
return -1;
267287
}
268288
#endif /* !MCUBOOT_HW_KEY */
269-
#endif
289+
#endif /* !MCUBOOT_BUILTIN_KEY */
290+
#endif /* EXPECTED_SIG_TLV */
270291

271292
/**
272293
* Reads the value of an image's security counter.
@@ -328,6 +349,30 @@ bootutil_get_img_security_cnt(struct image_header *hdr,
328349
return 0;
329350
}
330351

352+
#ifndef ALLOW_ROGUE_TLVS
353+
/*
354+
* The following list of TLVs are the only entries allowed in the unprotected
355+
* TLV section. All other TLV entries must be in the protected section.
356+
*/
357+
static const uint16_t allowed_unprot_tlvs[] = {
358+
IMAGE_TLV_KEYHASH,
359+
IMAGE_TLV_PUBKEY,
360+
IMAGE_TLV_SHA256,
361+
IMAGE_TLV_SHA384,
362+
IMAGE_TLV_RSA2048_PSS,
363+
IMAGE_TLV_ECDSA224,
364+
IMAGE_TLV_ECDSA_SIG,
365+
IMAGE_TLV_RSA3072_PSS,
366+
IMAGE_TLV_ED25519,
367+
IMAGE_TLV_ENC_RSA2048,
368+
IMAGE_TLV_ENC_KW,
369+
IMAGE_TLV_ENC_EC256,
370+
IMAGE_TLV_ENC_X25519,
371+
/* Mark end with ANY. */
372+
IMAGE_TLV_ANY,
373+
};
374+
#endif
375+
331376
/*
332377
* Verify the integrity of the image.
333378
* Return non-zero if image could not be validated/does not validate.
@@ -344,10 +389,16 @@ bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
344389
int image_hash_valid = 0;
345390
#ifdef EXPECTED_SIG_TLV
346391
FIH_DECLARE(valid_signature, FIH_FAILURE);
392+
#ifndef MCUBOOT_BUILTIN_KEY
347393
int key_id = -1;
394+
#else
395+
/* Pass a key ID equal to the image index, the underlying crypto library
396+
* is responsible for mapping the image index to a builtin key ID.
397+
*/
398+
int key_id = image_index;
399+
#endif /* !MCUBOOT_BUILTIN_KEY */
348400
#ifdef MCUBOOT_HW_KEY
349-
/* Few extra bytes for encoding and for public exponent. */
350-
uint8_t key_buf[SIG_BUF_SIZE + 24];
401+
uint8_t key_buf[KEY_BUF_SIZE];
351402
#endif
352403
#endif /* EXPECTED_SIG_TLV */
353404
struct image_tlv_iter it;
@@ -393,6 +444,27 @@ bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
393444
break;
394445
}
395446

447+
#ifndef ALLOW_ROGUE_TLVS
448+
/*
449+
* Ensure that the non-protected TLV only has entries necessary to hold
450+
* the signature. We also allow encryption related keys to be in the
451+
* unprotected area.
452+
*/
453+
if (!bootutil_tlv_iter_is_prot(&it, off)) {
454+
bool found = false;
455+
for (const uint16_t *p = allowed_unprot_tlvs; *p != IMAGE_TLV_ANY; p++) {
456+
if (type == *p) {
457+
found = true;
458+
break;
459+
}
460+
}
461+
if (!found) {
462+
FIH_SET(fih_rc, FIH_FAILURE);
463+
goto out;
464+
}
465+
}
466+
#endif
467+
396468
if (type == EXPECTED_HASH_TLV) {
397469
/* Verify the image hash. This must always be present. */
398470
if (len != sizeof(hash)) {
@@ -411,44 +483,34 @@ bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
411483
}
412484

413485
image_hash_valid = 1;
414-
#ifdef EXPECTED_SIG_TLV
415-
#ifndef MCUBOOT_HW_KEY
416-
} else if (type == IMAGE_TLV_KEYHASH) {
486+
#ifdef EXPECTED_KEY_TLV
487+
} else if (type == EXPECTED_KEY_TLV) {
417488
/*
418489
* Determine which key we should be checking.
419490
*/
420-
if (len > IMAGE_HASH_SIZE) {
491+
if (len > KEY_BUF_SIZE) {
421492
rc = -1;
422493
goto out;
423494
}
495+
#ifndef MCUBOOT_HW_KEY
424496
rc = LOAD_IMAGE_DATA(hdr, fap, off, buf, len);
425497
if (rc) {
426498
goto out;
427499
}
428500
key_id = bootutil_find_key(buf, len);
429-
/*
430-
* The key may not be found, which is acceptable. There
431-
* can be multiple signatures, each preceded by a key.
432-
*/
433501
#else
434-
} else if (type == IMAGE_TLV_PUBKEY) {
435-
/*
436-
* Determine which key we should be checking.
437-
*/
438-
if (len > sizeof(key_buf)) {
439-
rc = -1;
440-
goto out;
441-
}
442502
rc = LOAD_IMAGE_DATA(hdr, fap, off, key_buf, len);
443503
if (rc) {
444504
goto out;
445505
}
446506
key_id = bootutil_find_key(image_index, key_buf, len);
507+
#endif /* !MCUBOOT_HW_KEY */
447508
/*
448509
* The key may not be found, which is acceptable. There
449510
* can be multiple signatures, each preceded by a key.
450511
*/
451-
#endif /* !MCUBOOT_HW_KEY */
512+
#endif /* EXPECTED_KEY_TLV */
513+
#ifdef EXPECTED_SIG_TLV
452514
} else if (type == EXPECTED_SIG_TLV) {
453515
/* Ignore this signature if it is out of bounds. */
454516
if (key_id < 0 || key_id >= bootutil_key_cnt) {

boot/bootutil/src/loader.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1703,6 +1703,7 @@ boot_copy_image(struct boot_loader_state *state, struct boot_status *bs)
17031703
}
17041704
#endif /* MCUBOOT_HW_ROLLBACK_PROT */
17051705

1706+
#ifndef MCUBOOT_OVERWRITE_ONLY_KEEP_BACKUP
17061707
/*
17071708
* Erases header and trailer. The trailer is erased because when a new
17081709
* image is written without a trailer as is the case when using newt, the
@@ -1713,6 +1714,8 @@ boot_copy_image(struct boot_loader_state *state, struct boot_status *bs)
17131714
boot_img_sector_off(state, BOOT_SECONDARY_SLOT, 0),
17141715
boot_img_sector_size(state, BOOT_SECONDARY_SLOT, 0));
17151716
assert(rc == 0);
1717+
#endif
1718+
17161719
last_sector = boot_img_num_sectors(state, BOOT_SECONDARY_SLOT) - 1;
17171720
BOOT_LOG_DBG("erasing secondary trailer");
17181721
rc = boot_erase_region(fap_secondary_slot,

0 commit comments

Comments
 (0)