Skip to content

Commit ec6a9c2

Browse files
boot:crypto Add custom crypto support
The `MCUBOOT_USE_CUSTOM_CRYPTO` option allows to implement a custom backend that lets users plug in any crypto library, hardware accelerator, proprietary SDK, or another software implementation without modifying MCUboot's own source. Signed-off-by: Oleksandr Shkurchenko <Oleksandr.Shkurchenko@infineon.com>
1 parent a26c3e3 commit ec6a9c2

22 files changed

Lines changed: 1284 additions & 41 deletions

.github/workflows/sim.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ jobs:
4848
- "sig-ecdsa-psa enc-ec256 max-align-16, sig-ecdsa-psa enc-ec256 swap-offset validate-primary-slot max-align-16"
4949
- "ram-load enc-aes256-kw multiimage"
5050
- "ram-load enc-aes256-kw sig-ecdsa-mbedtls multiimage"
51+
- "custom-crypto,custom-crypto overwrite-only,custom-crypto validate-primary-slot,custom-crypto swap-offset"
52+
- "custom-enc-crypto,custom-enc-crypto validate-primary-slot,custom-enc-crypto swap-offset validate-primary-slot max-align-32"
5153
runs-on: ubuntu-latest
5254
env:
5355
MULTI_FEATURES: ${{ matrix.features }}

boot/bootutil/include/bootutil/crypto/aes_ctr.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
* This module provides a thin abstraction over some of the crypto
33
* primitives to make it easier to swap out the used crypto library.
44
*
5-
* At this point, there are two choices: MCUBOOT_USE_MBED_TLS, or
6-
* MCUBOOT_USE_TINYCRYPT. It is a compile error there is not exactly
7-
* one of these defined.
5+
* At this point, there are four choices: MCUBOOT_USE_MBED_TLS,
6+
* MCUBOOT_USE_TINYCRYPT, MCUBOOT_USE_PSA_CRYPTO, or
7+
* MCUBOOT_USE_CUSTOM_CRYPTO. It is a compile error if there is not
8+
* exactly one of these defined.
89
*/
910

1011
#ifndef __BOOTUTIL_CRYPTO_AES_CTR_H_
@@ -13,8 +14,10 @@
1314
#include "mcuboot_config/mcuboot_config.h"
1415

1516
#if (defined(MCUBOOT_USE_MBED_TLS) + \
16-
defined(MCUBOOT_USE_TINYCRYPT) + defined(MCUBOOT_USE_PSA_CRYPTO)) != 1
17-
#error "One crypto backend must be defined: either MBED_TLS or TINYCRYPT or PSA"
17+
defined(MCUBOOT_USE_TINYCRYPT) + \
18+
defined(MCUBOOT_USE_PSA_CRYPTO) + \
19+
defined(MCUBOOT_USE_CUSTOM_CRYPTO)) != 1
20+
#error "One crypto backend must be defined: either MBED_TLS or TINYCRYPT or PSA or CUSTOM_CRYPTO"
1821
#endif
1922

2023
#if defined(MCUBOOT_USE_MBED_TLS)
@@ -27,6 +30,6 @@
2730

2831
#if defined(MCUBOOT_USE_PSA_CRYPTO)
2932
#include "bootutil/crypto/aes_ctr_psa.h"
30-
#endif
33+
#endif /* MCUBOOT_USE_PSA_CRYPTO */
3134

3235
#endif /* __BOOTUTIL_CRYPTO_AES_CTR_H_ */

boot/bootutil/include/bootutil/crypto/ecdh_p256.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
* This module provides a thin abstraction over some of the crypto
33
* primitives to make it easier to swap out the used crypto library.
44
*
5-
* At this point, there are two choices: MCUBOOT_USE_MBED_TLS, or
6-
* MCUBOOT_USE_TINYCRYPT. It is a compile error there is not exactly
7-
* one of these defined.
5+
* At this point, there are three choices: MCUBOOT_USE_MBED_TLS,
6+
* MCUBOOT_USE_TINYCRYPT, or MCUBOOT_USE_CUSTOM_CRYPTO. It is a compile
7+
* error if there is not exactly one of these defined.
88
*/
99

1010
#ifndef __BOOTUTIL_CRYPTO_ECDH_P256_H_
@@ -13,8 +13,9 @@
1313
#include "mcuboot_config/mcuboot_config.h"
1414

1515
#if (defined(MCUBOOT_USE_MBED_TLS) + \
16-
defined(MCUBOOT_USE_TINYCRYPT)) != 1
17-
#error "One crypto backend must be defined: either MBED_TLS or TINYCRYPT"
16+
defined(MCUBOOT_USE_TINYCRYPT) + \
17+
defined(MCUBOOT_USE_CUSTOM_CRYPTO)) != 1
18+
#error "One crypto backend must be defined: either MBED_TLS or TINYCRYPT or CUSTOM_CRYPTO"
1819
#endif
1920

2021
#if defined(MCUBOOT_USE_MBED_TLS)

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

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@
99
* primitives to make it easier to swap out the used crypto library.
1010
*
1111
* At this point, the choices are: MCUBOOT_USE_TINYCRYPT, MCUBOOT_USE_CC310,
12-
* MCUBOOT_USE_MBED_TLS, MCUBOOT_USE_PSA_CRYPTO. Note that support for
13-
* MCUBOOT_USE_PSA_CRYPTO is still experimental and it might not support all
14-
* the crypto abstractions that MCUBOOT_USE_MBED_TLS supports. For this
15-
* reason, it's allowed to have both of them defined, and for crypto modules
16-
* that support both abstractions, the MCUBOOT_USE_PSA_CRYPTO will take
17-
* precedence.
12+
* MCUBOOT_USE_MBED_TLS, MCUBOOT_USE_PSA_CRYPTO, MCUBOOT_USE_CUSTOM_CRYPTO.
13+
* Note that support for MCUBOOT_USE_PSA_CRYPTO is still experimental and it
14+
* might not support all the crypto abstractions that MCUBOOT_USE_MBED_TLS
15+
* supports. For this reason, it's allowed to have both of them defined, and
16+
* for crypto modules that support both abstractions, the MCUBOOT_USE_PSA_CRYPTO
17+
* will take precedence. MCUBOOT_USE_CUSTOM_CRYPTO delegates all operations to
18+
* a platform-supplied <mcuboot_custom_crypto.h> resolved via the include path.
1819
*/
1920

2021
#ifndef __BOOTUTIL_CRYPTO_ECDSA_H_
@@ -27,15 +28,21 @@
2728
#define MCUBOOT_USE_PSA_OR_MBED_TLS
2829
#endif /* MCUBOOT_USE_PSA_CRYPTO || MCUBOOT_USE_MBED_TLS */
2930

31+
#if defined(MCUBOOT_USE_CUSTOM_CRYPTO) && defined(MCUBOOT_USE_PSA_OR_MBED_TLS)
32+
#error "MCUBOOT_USE_CUSTOM_CRYPTO is mutually exclusive with MCUBOOT_USE_PSA_CRYPTO and MCUBOOT_USE_MBED_TLS"
33+
#endif
34+
3035
#if defined(MCUBOOT_SIGN_EC384) && \
31-
!defined(MCUBOOT_USE_PSA_CRYPTO)
32-
#error "P384 requires PSA_CRYPTO to be defined"
36+
!defined(MCUBOOT_USE_PSA_CRYPTO) && \
37+
!defined(MCUBOOT_USE_CUSTOM_CRYPTO)
38+
#error "P384 requires PSA_CRYPTO or CUSTOM_CRYPTO to be defined"
3339
#endif
3440

3541
#if (defined(MCUBOOT_USE_TINYCRYPT) + \
3642
defined(MCUBOOT_USE_CC310) + \
37-
defined(MCUBOOT_USE_PSA_OR_MBED_TLS)) != 1
38-
#error "One crypto backend must be defined: either CC310/TINYCRYPT/MBED_TLS/PSA_CRYPTO"
43+
defined(MCUBOOT_USE_PSA_OR_MBED_TLS) + \
44+
defined(MCUBOOT_USE_CUSTOM_CRYPTO)) != 1
45+
#error "One crypto backend must be defined: either CC310/TINYCRYPT/MBED_TLS/PSA_CRYPTO/CUSTOM_CRYPTO"
3946
#endif
4047

4148
#if defined(MCUBOOT_USE_TINYCRYPT)
@@ -66,7 +73,7 @@
6673
#define BOOTUTIL_CRYPTO_ECDSA_P256_HASH_SIZE (32)
6774

6875
#include "bootutil/sign_key.h"
69-
#if !defined(MCUBOOT_USE_PSA_CRYPTO)
76+
#if !defined(MCUBOOT_USE_PSA_CRYPTO) && !defined(MCUBOOT_USE_CUSTOM_CRYPTO)
7077
#include "bootutil/crypto/common.h"
7178
#include "mbedtls/asn1.h"
7279
#include "mbedtls/oid.h"

boot/bootutil/include/bootutil/crypto/hmac_sha256.h

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
* This module provides a thin abstraction over some of the crypto
33
* primitives to make it easier to swap out the used crypto library.
44
*
5-
* At this point, there are two choices: MCUBOOT_USE_MBED_TLS, or
6-
* MCUBOOT_USE_TINYCRYPT. It is a compile error there is not exactly
7-
* one of these defined.
5+
* At this point, there are three choices: MCUBOOT_USE_MBED_TLS,
6+
* MCUBOOT_USE_TINYCRYPT, or MCUBOOT_USE_CUSTOM_CRYPTO. It is a compile
7+
* error if there is not exactly one of these defined.
88
*/
99

1010
#ifndef __BOOTUTIL_CRYPTO_HMAC_SHA256_H_
@@ -13,8 +13,9 @@
1313
#include "mcuboot_config/mcuboot_config.h"
1414

1515
#if (defined(MCUBOOT_USE_MBED_TLS) + \
16-
defined(MCUBOOT_USE_TINYCRYPT)) != 1
17-
#error "One crypto backend must be defined: either MBED_TLS or TINYCRYPT"
16+
defined(MCUBOOT_USE_TINYCRYPT) + \
17+
defined(MCUBOOT_USE_CUSTOM_CRYPTO)) != 1
18+
#error "One crypto backend must be defined: MBED_TLS, TINYCRYPT, or CUSTOM_CRYPTO"
1819
#endif
1920

2021
#if defined(MCUBOOT_USE_MBED_TLS)
@@ -119,10 +120,13 @@ static inline int bootutil_hmac_sha256_update(bootutil_hmac_sha256_context *ctx,
119120

120121
static inline int bootutil_hmac_sha256_finish(bootutil_hmac_sha256_context *ctx, uint8_t *tag, unsigned int taglen)
121122
{
122-
(void)taglen;
123123
/*
124-
* HMAC the key and check that our received MAC matches the generated tag
124+
* Finalize the HMAC computation and output the tag.
125+
* mbedtls_md_hmac_finish() always outputs the full 32-byte SHA-256 digest
126+
* and does not support truncation. taglen is ignored; caller must provide
127+
* a 32-byte buffer.
125128
*/
129+
(void)taglen;
126130
return mbedtls_md_hmac_finish(ctx, tag);
127131
}
128132
#endif /* MCUBOOT_USE_MBED_TLS */

boot/bootutil/include/bootutil/crypto/sha.h

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111
* primitives to make it easier to swap out the used crypto library.
1212
*
1313
* At this point, the choices are: MCUBOOT_USE_MBED_TLS, MCUBOOT_USE_TINYCRYPT,
14-
* MCUBOOT_USE_PSA_CRYPTO, MCUBOOT_USE_CC310. Note that support for MCUBOOT_USE_PSA_CRYPTO
15-
* is still experimental and it might not support all the crypto abstractions
16-
* that MCUBOOT_USE_MBED_TLS supports. For this reason, it's allowed to have
17-
* both of them defined, and for crypto modules that support both abstractions,
18-
* the MCUBOOT_USE_PSA_CRYPTO will take precedence.
14+
* MCUBOOT_USE_PSA_CRYPTO, MCUBOOT_USE_CC310, MCUBOOT_USE_CUSTOM_CRYPTO. Note
15+
* that support for MCUBOOT_USE_PSA_CRYPTO is still experimental and it might
16+
* not support all the crypto abstractions that MCUBOOT_USE_MBED_TLS supports.
17+
* For this reason, it's allowed to have both of them defined, and for crypto
18+
* modules that support both abstractions, the MCUBOOT_USE_PSA_CRYPTO will take
19+
* precedence. MCUBOOT_USE_CUSTOM_CRYPTO delegates all operations to a
20+
* platform-supplied <mcuboot_custom_crypto.h> resolved via the include path.
1921
*/
2022

2123
#ifndef __BOOTUTIL_CRYPTO_SHA_H_
@@ -28,10 +30,15 @@
2830
#define MCUBOOT_USE_PSA_OR_MBED_TLS
2931
#endif /* MCUBOOT_USE_PSA_CRYPTO || MCUBOOT_USE_MBED_TLS */
3032

33+
#if defined(MCUBOOT_USE_CUSTOM_CRYPTO) && defined(MCUBOOT_USE_PSA_OR_MBED_TLS)
34+
#error "MCUBOOT_USE_CUSTOM_CRYPTO is mutually exclusive with MCUBOOT_USE_PSA_CRYPTO and MCUBOOT_USE_MBED_TLS"
35+
#endif
36+
3137
#if (defined(MCUBOOT_USE_PSA_OR_MBED_TLS) + \
3238
defined(MCUBOOT_USE_TINYCRYPT) + \
33-
defined(MCUBOOT_USE_CC310)) != 1
34-
#error "One crypto backend must be defined: either CC310/MBED_TLS/TINYCRYPT/PSA_CRYPTO"
39+
defined(MCUBOOT_USE_CC310) + \
40+
defined(MCUBOOT_USE_CUSTOM_CRYPTO)) != 1
41+
#error "One crypto backend must be defined: either CC310/MBED_TLS/TINYCRYPT/PSA_CRYPTO/CUSTOM_CRYPTO"
3542
#endif
3643

3744
#if defined(MCUBOOT_SHA512)

boot/bootutil/src/encrypted.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@
99
#include "mcuboot_config/mcuboot_config.h"
1010

1111
#if defined(MCUBOOT_ENC_IMAGES)
12+
13+
/* When MCUBOOT_USE_CUSTOM_CRYPTO is active the custom crypto translation unit
14+
* provides all boot_enc_* and boot_decrypt_key symbols and handles HMAC/KDF
15+
* internally without depending on MBED_TLS or TINYCRYPT. Suppress this
16+
* entire file to avoid duplicate symbol link errors. */
17+
#if !defined(MCUBOOT_USE_CUSTOM_CRYPTO)
18+
1219
#include <stddef.h>
1320
#include <inttypes.h>
1421
#include <string.h>
@@ -720,4 +727,6 @@ boot_enc_zeroize(struct enc_key_data *enc_state)
720727
memset(enc_state, 0, sizeof(struct enc_key_data) * BOOT_NUM_SLOTS);
721728
}
722729

730+
#endif /* !MCUBOOT_USE_CUSTOM_CRYPTO */
731+
723732
#endif /* MCUBOOT_ENC_IMAGES */

boot/bootutil/src/encrypted_psa.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
#include "mcuboot_config/mcuboot_config.h"
88

9+
#if defined(MCUBOOT_USE_PSA_CRYPTO)
10+
911
#include <stddef.h>
1012
#include <inttypes.h>
1113
#include <string.h>
@@ -536,3 +538,5 @@ int bootutil_aes_ctr_decrypt(bootutil_aes_ctr_context *ctx, uint8_t *counter,
536538
return ret;
537539
}
538540
#endif /* defined(MCUBOOT_ENC_IMAGES) */
541+
542+
#endif /* defined(MCUBOOT_USE_PSA_CRYPTO) */

0 commit comments

Comments
 (0)