|
25 | 25 | #include "bootutil/crypto/ecdh_p256.h"
|
26 | 26 | #endif
|
27 | 27 |
|
| 28 | +#if !defined(MCUBOOT_USE_PSA_CRYPTO) |
28 | 29 | #if defined(MCUBOOT_ENCRYPT_X25519)
|
29 | 30 | #include "bootutil/crypto/ecdh_x25519.h"
|
30 | 31 | #endif
|
|
35 | 36 | #include "mbedtls/oid.h"
|
36 | 37 | #include "mbedtls/asn1.h"
|
37 | 38 | #endif
|
| 39 | +#endif |
38 | 40 |
|
39 | 41 | #include "bootutil/image.h"
|
40 | 42 | #include "bootutil/enc_key.h"
|
|
43 | 45 |
|
44 | 46 | #include "bootutil_priv.h"
|
45 | 47 |
|
| 48 | +#define EXPECTED_ENC_LEN BOOT_ENC_TLV_SIZE |
| 49 | + |
| 50 | +#if defined(MCUBOOT_ENCRYPT_RSA) |
| 51 | +# define EXPECTED_ENC_TLV IMAGE_TLV_ENC_RSA2048 |
| 52 | +#elif defined(MCUBOOT_ENCRYPT_KW) |
| 53 | +# define EXPECTED_ENC_TLV IMAGE_TLV_ENC_KW |
| 54 | +#elif defined(MCUBOOT_ENCRYPT_EC256) |
| 55 | +# define EXPECTED_ENC_TLV IMAGE_TLV_ENC_EC256 |
| 56 | +# define EC_PUBK_INDEX (0) |
| 57 | +# define EC_TAG_INDEX (65) |
| 58 | +# define EC_CIPHERKEY_INDEX (65 + 32) |
| 59 | +_Static_assert(EC_CIPHERKEY_INDEX + BOOT_ENC_KEY_SIZE == EXPECTED_ENC_LEN, |
| 60 | + "Please fix ECIES-P256 component indexes"); |
| 61 | +#elif defined(MCUBOOT_ENCRYPT_X25519) |
| 62 | +# define EXPECTED_ENC_TLV IMAGE_TLV_ENC_X25519 |
| 63 | +# define EC_PUBK_INDEX (0) |
| 64 | +# define EC_TAG_INDEX (32) |
| 65 | +# define EC_CIPHERKEY_INDEX (32 + 32) |
| 66 | +_Static_assert(EC_CIPHERKEY_INDEX + BOOT_ENC_KEY_SIZE == EXPECTED_ENC_LEN, |
| 67 | + "Please fix ECIES-X25519 component indexes"); |
| 68 | +#endif |
| 69 | + |
| 70 | +/* NOUP Fixme: */ |
| 71 | +#if !defined(CONFIG_BOOT_ED25519_PSA) |
46 | 72 | #if defined(MCUBOOT_ENCRYPT_EC256) || defined(MCUBOOT_ENCRYPT_X25519)
|
47 | 73 | #if defined(_compare)
|
48 | 74 | static inline int bootutil_constant_time_compare(const uint8_t *a, const uint8_t *b, size_t size)
|
@@ -351,60 +377,6 @@ int boot_enc_retrieve_private_key(struct bootutil_key **private_key)
|
351 | 377 | }
|
352 | 378 | #endif /* !MCUBOOT_ENC_BUILTIN_KEY */
|
353 | 379 |
|
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 | 380 | #if ( (defined(MCUBOOT_ENCRYPT_RSA) && defined(MCUBOOT_USE_MBED_TLS) && !defined(MCUBOOT_USE_PSA_CRYPTO)) || \
|
409 | 381 | (defined(MCUBOOT_ENCRYPT_EC256) && defined(MCUBOOT_USE_MBED_TLS)) )
|
410 | 382 | #if MBEDTLS_VERSION_NUMBER >= 0x03000000
|
@@ -627,6 +599,7 @@ boot_decrypt_key(const uint8_t *buf, uint8_t *enckey)
|
627 | 599 |
|
628 | 600 | return rc;
|
629 | 601 | }
|
| 602 | +#endif /* CONFIG_BOOT_ED25519_PSA */ |
630 | 603 |
|
631 | 604 | /*
|
632 | 605 | * Load encryption key.
|
@@ -681,6 +654,39 @@ boot_enc_load(struct enc_key_data *enc_state, int slot,
|
681 | 654 | return boot_decrypt_key(buf, bs->enckey[slot]);
|
682 | 655 | }
|
683 | 656 |
|
| 657 | +int |
| 658 | +boot_enc_init(struct enc_key_data *enc_state, uint8_t slot) |
| 659 | +{ |
| 660 | + bootutil_aes_ctr_init(&enc_state[slot].aes_ctr); |
| 661 | + return 0; |
| 662 | +} |
| 663 | + |
| 664 | +int |
| 665 | +boot_enc_drop(struct enc_key_data *enc_state, uint8_t slot) |
| 666 | +{ |
| 667 | + bootutil_aes_ctr_drop(&enc_state[slot].aes_ctr); |
| 668 | + enc_state[slot].valid = 0; |
| 669 | + return 0; |
| 670 | +} |
| 671 | + |
| 672 | +int |
| 673 | +boot_enc_set_key(struct enc_key_data *enc_state, uint8_t slot, |
| 674 | + const struct boot_status *bs) |
| 675 | +{ |
| 676 | + int rc; |
| 677 | + |
| 678 | + rc = bootutil_aes_ctr_set_key(&enc_state[slot].aes_ctr, bs->enckey[slot]); |
| 679 | + if (rc != 0) { |
| 680 | + boot_enc_drop(enc_state, slot); |
| 681 | + return -1; |
| 682 | + } |
| 683 | + |
| 684 | + enc_state[slot].valid = 1; |
| 685 | + |
| 686 | + return 0; |
| 687 | +} |
| 688 | + |
| 689 | + |
684 | 690 | bool
|
685 | 691 | boot_enc_valid(struct enc_key_data *enc_state, int slot)
|
686 | 692 | {
|
|
0 commit comments