|
3 | 3 | *
|
4 | 4 | * Copyright (c) 2018-2019 JUUL Labs
|
5 | 5 | * Copyright (c) 2019-2024 Arm Limited
|
| 6 | + * Copyright (c) 2025 Nordic Semiconductor ASA |
6 | 7 | */
|
7 | 8 |
|
8 | 9 | #include "mcuboot_config/mcuboot_config.h"
|
|
25 | 26 | #include "bootutil/crypto/ecdh_p256.h"
|
26 | 27 | #endif
|
27 | 28 |
|
| 29 | +#if !defined(MCUBOOT_USE_PSA_CRYPTO) |
28 | 30 | #if defined(MCUBOOT_ENCRYPT_X25519)
|
29 | 31 | #include "bootutil/crypto/ecdh_x25519.h"
|
30 | 32 | #endif
|
|
35 | 37 | #include "mbedtls/oid.h"
|
36 | 38 | #include "mbedtls/asn1.h"
|
37 | 39 | #endif
|
| 40 | +#endif |
38 | 41 |
|
39 | 42 | #include "bootutil/image.h"
|
40 | 43 | #include "bootutil/enc_key.h"
|
|
43 | 46 |
|
44 | 47 | #include "bootutil_priv.h"
|
45 | 48 |
|
| 49 | +#define EXPECTED_ENC_LEN BOOT_ENC_TLV_SIZE |
| 50 | + |
| 51 | +#if defined(MCUBOOT_ENCRYPT_RSA) |
| 52 | +# define EXPECTED_ENC_TLV IMAGE_TLV_ENC_RSA2048 |
| 53 | +#elif defined(MCUBOOT_ENCRYPT_KW) |
| 54 | +# define EXPECTED_ENC_TLV IMAGE_TLV_ENC_KW |
| 55 | +#elif defined(MCUBOOT_ENCRYPT_EC256) |
| 56 | +# define EXPECTED_ENC_TLV IMAGE_TLV_ENC_EC256 |
| 57 | +# define EC_PUBK_INDEX (0) |
| 58 | +# define EC_TAG_INDEX (65) |
| 59 | +# define EC_CIPHERKEY_INDEX (65 + 32) |
| 60 | +_Static_assert(EC_CIPHERKEY_INDEX + BOOT_ENC_KEY_SIZE == EXPECTED_ENC_LEN, |
| 61 | + "Please fix ECIES-P256 component indexes"); |
| 62 | +#elif defined(MCUBOOT_ENCRYPT_X25519) |
| 63 | +# define EXPECTED_ENC_TLV IMAGE_TLV_ENC_X25519 |
| 64 | +# define EC_PUBK_INDEX (0) |
| 65 | +# define EC_TAG_INDEX (32) |
| 66 | +# define EC_CIPHERKEY_INDEX (32 + 32) |
| 67 | +_Static_assert(EC_CIPHERKEY_INDEX + BOOT_ENC_KEY_SIZE == EXPECTED_ENC_LEN, |
| 68 | + "Please fix ECIES-X25519 component indexes"); |
| 69 | +#endif |
| 70 | + |
| 71 | +/* NOUP Fixme: */ |
| 72 | +#if !defined(CONFIG_BOOT_ED25519_PSA) |
46 | 73 | #if defined(MCUBOOT_ENCRYPT_EC256) || defined(MCUBOOT_ENCRYPT_X25519)
|
47 | 74 | #if defined(_compare)
|
48 | 75 | static inline int bootutil_constant_time_compare(const uint8_t *a, const uint8_t *b, size_t size)
|
@@ -351,60 +378,6 @@ int boot_enc_retrieve_private_key(struct bootutil_key **private_key)
|
351 | 378 | }
|
352 | 379 | #endif /* !MCUBOOT_ENC_BUILTIN_KEY */
|
353 | 380 |
|
354 |
| -int |
355 |
| -boot_enc_init(struct enc_key_data *enc_state, uint8_t slot) |
356 |
| -{ |
357 |
| - bootutil_aes_ctr_init(&enc_state[slot].aes_ctr); |
358 |
| - return 0; |
359 |
| -} |
360 |
| - |
361 |
| -int |
362 |
| -boot_enc_drop(struct enc_key_data *enc_state, uint8_t slot) |
363 |
| -{ |
364 |
| - bootutil_aes_ctr_drop(&enc_state[slot].aes_ctr); |
365 |
| - enc_state[slot].valid = 0; |
366 |
| - return 0; |
367 |
| -} |
368 |
| - |
369 |
| -int |
370 |
| -boot_enc_set_key(struct enc_key_data *enc_state, uint8_t slot, |
371 |
| - const struct boot_status *bs) |
372 |
| -{ |
373 |
| - int rc; |
374 |
| - |
375 |
| - rc = bootutil_aes_ctr_set_key(&enc_state[slot].aes_ctr, bs->enckey[slot]); |
376 |
| - if (rc != 0) { |
377 |
| - boot_enc_drop(enc_state, slot); |
378 |
| - return -1; |
379 |
| - } |
380 |
| - |
381 |
| - enc_state[slot].valid = 1; |
382 |
| - |
383 |
| - return 0; |
384 |
| -} |
385 |
| - |
386 |
| -#define EXPECTED_ENC_LEN BOOT_ENC_TLV_SIZE |
387 |
| - |
388 |
| -#if defined(MCUBOOT_ENCRYPT_RSA) |
389 |
| -# define EXPECTED_ENC_TLV IMAGE_TLV_ENC_RSA2048 |
390 |
| -#elif defined(MCUBOOT_ENCRYPT_KW) |
391 |
| -# define EXPECTED_ENC_TLV IMAGE_TLV_ENC_KW |
392 |
| -#elif defined(MCUBOOT_ENCRYPT_EC256) |
393 |
| -# define EXPECTED_ENC_TLV IMAGE_TLV_ENC_EC256 |
394 |
| -# define EC_PUBK_INDEX (0) |
395 |
| -# define EC_TAG_INDEX (65) |
396 |
| -# define EC_CIPHERKEY_INDEX (65 + 32) |
397 |
| -_Static_assert(EC_CIPHERKEY_INDEX + BOOT_ENC_KEY_SIZE == EXPECTED_ENC_LEN, |
398 |
| - "Please fix ECIES-P256 component indexes"); |
399 |
| -#elif defined(MCUBOOT_ENCRYPT_X25519) |
400 |
| -# define EXPECTED_ENC_TLV IMAGE_TLV_ENC_X25519 |
401 |
| -# define EC_PUBK_INDEX (0) |
402 |
| -# define EC_TAG_INDEX (32) |
403 |
| -# define EC_CIPHERKEY_INDEX (32 + 32) |
404 |
| -_Static_assert(EC_CIPHERKEY_INDEX + BOOT_ENC_KEY_SIZE == EXPECTED_ENC_LEN, |
405 |
| - "Please fix ECIES-X25519 component indexes"); |
406 |
| -#endif |
407 |
| - |
408 | 381 | #if ( (defined(MCUBOOT_ENCRYPT_RSA) && defined(MCUBOOT_USE_MBED_TLS) && !defined(MCUBOOT_USE_PSA_CRYPTO)) || \
|
409 | 382 | (defined(MCUBOOT_ENCRYPT_EC256) && defined(MCUBOOT_USE_MBED_TLS)) )
|
410 | 383 | #if MBEDTLS_VERSION_NUMBER >= 0x03000000
|
@@ -627,6 +600,7 @@ boot_decrypt_key(const uint8_t *buf, uint8_t *enckey)
|
627 | 600 |
|
628 | 601 | return rc;
|
629 | 602 | }
|
| 603 | +#endif /* CONFIG_BOOT_ED25519_PSA */ |
630 | 604 |
|
631 | 605 | /*
|
632 | 606 | * Load encryption key.
|
@@ -694,6 +668,38 @@ boot_enc_load(struct boot_loader_state *state, int slot,
|
694 | 668 | return boot_decrypt_key(buf, bs->enckey[slot]);
|
695 | 669 | }
|
696 | 670 |
|
| 671 | +int |
| 672 | +boot_enc_init(struct enc_key_data *enc_state, uint8_t slot) |
| 673 | +{ |
| 674 | + bootutil_aes_ctr_init(&enc_state[slot].aes_ctr); |
| 675 | + return 0; |
| 676 | +} |
| 677 | + |
| 678 | +int |
| 679 | +boot_enc_drop(struct enc_key_data *enc_state, uint8_t slot) |
| 680 | +{ |
| 681 | + bootutil_aes_ctr_drop(&enc_state[slot].aes_ctr); |
| 682 | + enc_state[slot].valid = 0; |
| 683 | + return 0; |
| 684 | +} |
| 685 | + |
| 686 | +int |
| 687 | +boot_enc_set_key(struct enc_key_data *enc_state, uint8_t slot, |
| 688 | + const struct boot_status *bs) |
| 689 | +{ |
| 690 | + int rc; |
| 691 | + |
| 692 | + rc = bootutil_aes_ctr_set_key(&enc_state[slot].aes_ctr, bs->enckey[slot]); |
| 693 | + if (rc != 0) { |
| 694 | + boot_enc_drop(enc_state, slot); |
| 695 | + return -1; |
| 696 | + } |
| 697 | + |
| 698 | + enc_state[slot].valid = 1; |
| 699 | + |
| 700 | + return 0; |
| 701 | +} |
| 702 | + |
697 | 703 | bool
|
698 | 704 | boot_enc_valid(struct enc_key_data *enc_state, int slot)
|
699 | 705 | {
|
|
0 commit comments