Skip to content

Commit 598d882

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 d045415 commit 598d882

21 files changed

Lines changed: 1281 additions & 37 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"
52+
- "custom-enc-crypto,custom-enc-crypto overwrite-only,custom-enc-crypto validate-primary-slot,custom-enc-crypto 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: 13 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,10 @@
2730

2831
#if defined(MCUBOOT_USE_PSA_CRYPTO)
2932
#include "bootutil/crypto/aes_ctr_psa.h"
30-
#endif
33+
#endif /* MCUBOOT_USE_PSA_CRYPTO */
34+
35+
#if defined(MCUBOOT_USE_CUSTOM_CRYPTO)
36+
#include "mcuboot_custom_crypto.h"
37+
#endif /* MCUBOOT_USE_CUSTOM_CRYPTO */
3138

3239
#endif /* __BOOTUTIL_CRYPTO_AES_CTR_H_ */

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

Lines changed: 14 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)
@@ -29,6 +30,10 @@
2930
#define BOOTUTIL_CRYPTO_ECDH_P256_HASH_SIZE (4 * 8)
3031
#endif /* MCUBOOT_USE_TINYCRYPT */
3132

33+
#if defined(MCUBOOT_USE_CUSTOM_CRYPTO)
34+
#include "mcuboot_custom_crypto.h"
35+
#endif /* MCUBOOT_USE_CUSTOM_CRYPTO */
36+
3237
#ifdef __cplusplus
3338
extern "C" {
3439
#endif
@@ -149,6 +154,10 @@ static inline int bootutil_ecdh_p256_shared_secret(bootutil_ecdh_p256_context *c
149154
}
150155
#endif /* MCUBOOT_USE_MBED_TLS */
151156

157+
/* When MCUBOOT_USE_CUSTOM_CRYPTO is defined, bootutil_ecdh_p256_context and
158+
* all bootutil_ecdh_p256_* functions are provided by <mcuboot_custom_crypto.h> which
159+
* is included above via the platform-specific include path. */
160+
152161
#ifdef __cplusplus
153162
}
154163
#endif

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

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,22 @@
22
* SPDX-License-Identifier: Apache-2.0
33
*
44
* Copyright (c) 2023-2025 Arm Limited
5+
* Copyright (c) 2026 Infineon Technologies AG, or an affiliate of Infineon
6+
* Technologies AG
57
*/
68

79
/*
810
* This module provides a thin abstraction over some of the crypto
911
* primitives to make it easier to swap out the used crypto library.
1012
*
1113
* 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.
14+
* MCUBOOT_USE_MBED_TLS, MCUBOOT_USE_PSA_CRYPTO, MCUBOOT_USE_CUSTOM_CRYPTO.
15+
* Note that support for MCUBOOT_USE_PSA_CRYPTO is still experimental and it
16+
* might not support all the crypto abstractions that MCUBOOT_USE_MBED_TLS
17+
* supports. For this reason, it's allowed to have both of them defined, and
18+
* for crypto modules that support both abstractions, the MCUBOOT_USE_PSA_CRYPTO
19+
* will take precedence. MCUBOOT_USE_CUSTOM_CRYPTO delegates all operations to
20+
* a platform-supplied <mcuboot_custom_crypto.h> resolved via the include path.
1821
*/
1922

2023
#ifndef __BOOTUTIL_CRYPTO_ECDSA_H_
@@ -27,15 +30,20 @@
2730
#define MCUBOOT_USE_PSA_OR_MBED_TLS
2831
#endif /* MCUBOOT_USE_PSA_CRYPTO || MCUBOOT_USE_MBED_TLS */
2932

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+
3037
#if defined(MCUBOOT_SIGN_EC384) && \
3138
!defined(MCUBOOT_USE_PSA_CRYPTO)
3239
#error "P384 requires PSA_CRYPTO to be defined"
3340
#endif
3441

3542
#if (defined(MCUBOOT_USE_TINYCRYPT) + \
3643
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"
44+
defined(MCUBOOT_USE_PSA_OR_MBED_TLS) + \
45+
defined(MCUBOOT_USE_CUSTOM_CRYPTO)) != 1
46+
#error "One crypto backend must be defined: either CC310/TINYCRYPT/MBED_TLS/PSA_CRYPTO/CUSTOM_CRYPTO"
3947
#endif
4048

4149
#if defined(MCUBOOT_USE_TINYCRYPT)
@@ -47,6 +55,10 @@
4755
#include <cc310_glue.h>
4856
#endif /* MCUBOOT_USE_CC310 */
4957

58+
#if defined(MCUBOOT_USE_CUSTOM_CRYPTO)
59+
#include "mcuboot_custom_crypto.h"
60+
#endif /* MCUBOOT_USE_CUSTOM_CRYPTO */
61+
5062
#if defined(MCUBOOT_USE_PSA_CRYPTO)
5163
#include <psa/crypto.h>
5264
#include <string.h>
@@ -66,7 +78,7 @@
6678
#define BOOTUTIL_CRYPTO_ECDSA_P256_HASH_SIZE (32)
6779

6880
#include "bootutil/sign_key.h"
69-
#if !defined(MCUBOOT_USE_PSA_CRYPTO)
81+
#if !defined(MCUBOOT_USE_PSA_CRYPTO) && !defined(MCUBOOT_USE_CUSTOM_CRYPTO)
7082
#include "bootutil/crypto/common.h"
7183
#include "mbedtls/asn1.h"
7284
#include "mbedtls/oid.h"
@@ -615,6 +627,10 @@ static inline int bootutil_ecdsa_parse_public_key(bootutil_ecdsa_context *ctx,
615627

616628
#endif /* MCUBOOT_USE_MBED_TLS */
617629

630+
/* When MCUBOOT_USE_CUSTOM_CRYPTO is defined, bootutil_ecdsa_context and all
631+
* bootutil_ecdsa_* functions are provided by <mcuboot_custom_crypto.h> which is
632+
* included above via the platform-specific include path. */
633+
618634
#ifdef __cplusplus
619635
}
620636
#endif

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

Lines changed: 14 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_HMAC_SHA256_H_
@@ -13,10 +13,15 @@
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

21+
#if defined(MCUBOOT_USE_CUSTOM_CRYPTO)
22+
#include "mcuboot_custom_crypto.h"
23+
#endif /* MCUBOOT_USE_CUSTOM_CRYPTO */
24+
2025
#if defined(MCUBOOT_USE_MBED_TLS)
2126
#include <stdint.h>
2227
#include <stddef.h>
@@ -37,6 +42,10 @@
3742
extern "C" {
3843
#endif
3944

45+
/* When MCUBOOT_USE_CUSTOM_CRYPTO is defined, bootutil_hmac_sha256_context and
46+
* all bootutil_hmac_sha256_* functions are provided by <mcuboot_custom_crypto.h>
47+
* which is resolved via the include path. */
48+
4049
#if defined(MCUBOOT_USE_TINYCRYPT)
4150
typedef struct tc_hmac_state_struct bootutil_hmac_sha256_context;
4251
static inline void bootutil_hmac_sha256_init(bootutil_hmac_sha256_context *ctx)

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

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,22 @@
44
* Copyright (c) 2017-2019 Linaro LTD
55
* Copyright (c) 2017-2019 JUUL Labs
66
* Copyright (c) 2021-2023 Arm Limited
7+
* Copyright (c) 2026 Infineon Technologies AG, or an affiliate of Infineon
8+
* Technologies AG
79
*/
810

911
/*
1012
* This module provides a thin abstraction over some of the crypto
1113
* primitives to make it easier to swap out the used crypto library.
1214
*
1315
* 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.
16+
* MCUBOOT_USE_PSA_CRYPTO, MCUBOOT_USE_CC310, MCUBOOT_USE_CUSTOM_CRYPTO. Note
17+
* that support for MCUBOOT_USE_PSA_CRYPTO is still experimental and it might
18+
* not support all the crypto abstractions that MCUBOOT_USE_MBED_TLS supports.
19+
* For this reason, it's allowed to have both of them defined, and for crypto
20+
* modules that support both abstractions, the MCUBOOT_USE_PSA_CRYPTO will take
21+
* precedence. MCUBOOT_USE_CUSTOM_CRYPTO delegates all operations to a
22+
* platform-supplied <mcuboot_custom_crypto.h> resolved via the include path.
1923
*/
2024

2125
#ifndef __BOOTUTIL_CRYPTO_SHA_H_
@@ -28,10 +32,15 @@
2832
#define MCUBOOT_USE_PSA_OR_MBED_TLS
2933
#endif /* MCUBOOT_USE_PSA_CRYPTO || MCUBOOT_USE_MBED_TLS */
3034

35+
#if defined(MCUBOOT_USE_CUSTOM_CRYPTO) && defined(MCUBOOT_USE_PSA_OR_MBED_TLS)
36+
#error "MCUBOOT_USE_CUSTOM_CRYPTO is mutually exclusive with MCUBOOT_USE_PSA_CRYPTO and MCUBOOT_USE_MBED_TLS"
37+
#endif
38+
3139
#if (defined(MCUBOOT_USE_PSA_OR_MBED_TLS) + \
3240
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"
41+
defined(MCUBOOT_USE_CC310) + \
42+
defined(MCUBOOT_USE_CUSTOM_CRYPTO)) != 1
43+
#error "One crypto backend must be defined: either CC310/MBED_TLS/TINYCRYPT/PSA_CRYPTO/CUSTOM_CRYPTO"
3544
#endif
3645

3746
#if defined(MCUBOOT_SHA512)
@@ -78,6 +87,10 @@
7887
#include <cc310_glue.h>
7988
#endif /* MCUBOOT_USE_CC310 */
8089

90+
#if defined(MCUBOOT_USE_CUSTOM_CRYPTO)
91+
#include "mcuboot_custom_crypto.h"
92+
#endif /* MCUBOOT_USE_CUSTOM_CRYPTO */
93+
8194
#include <stdint.h>
8295

8396
#ifdef __cplusplus
@@ -267,6 +280,10 @@ static inline int bootutil_sha_finish(bootutil_sha_context *ctx,
267280
}
268281
#endif /* MCUBOOT_USE_CC310 */
269282

283+
/* When MCUBOOT_USE_CUSTOM_CRYPTO is defined, bootutil_sha_context and all
284+
* bootutil_sha_* functions are provided by <mcuboot_custom_crypto.h> which is
285+
* included above via the platform-specific include path. */
286+
270287
#ifdef __cplusplus
271288
}
272289
#endif

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: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
/*
22
* Copyright (c) 2025 Nordic Semiconductor ASA
3+
* Copyright (c) 2026 Infineon Technologies AG, or an affiliate of Infineon
4+
* Technologies AG
35
*
46
* SPDX-License-Identifier: Apache-2.0
57
*/
68

79
#include "mcuboot_config/mcuboot_config.h"
810

11+
#if defined(MCUBOOT_USE_PSA_CRYPTO)
12+
913
#include <stddef.h>
1014
#include <inttypes.h>
1115
#include <string.h>
@@ -536,3 +540,5 @@ int bootutil_aes_ctr_decrypt(bootutil_aes_ctr_context *ctx, uint8_t *counter,
536540
return ret;
537541
}
538542
#endif /* defined(MCUBOOT_ENC_IMAGES) */
543+
544+
#endif /* defined(MCUBOOT_USE_PSA_CRYPTO) */

0 commit comments

Comments
 (0)