From d502ae1f0268581803311dacbf5f67e1233691f4 Mon Sep 17 00:00:00 2001 From: Oliver Mueller Date: Thu, 30 Nov 2023 13:46:13 +0100 Subject: [PATCH] add the option to use custom definitions for all crypto inline functions instead of the prepared ones as previously discussed in #1478 Signed-off-by: Oliver Mueller --- boot/bootutil/include/bootutil/crypto/aes_ctr.h | 9 +++++++-- boot/bootutil/include/bootutil/crypto/aes_kw.h | 9 +++++++-- boot/bootutil/include/bootutil/crypto/ecdh_p256.h | 8 ++++++-- boot/bootutil/include/bootutil/crypto/ecdh_x25519.h | 8 ++++++-- boot/bootutil/include/bootutil/crypto/ecdsa.h | 11 ++++++++--- boot/bootutil/include/bootutil/crypto/hmac_sha256.h | 9 +++++++-- boot/bootutil/include/bootutil/crypto/rsa.h | 5 ++++- boot/bootutil/include/bootutil/crypto/sha.h | 7 ++++++- 8 files changed, 51 insertions(+), 15 deletions(-) diff --git a/boot/bootutil/include/bootutil/crypto/aes_ctr.h b/boot/bootutil/include/bootutil/crypto/aes_ctr.h index e69b0372f..d0bffa117 100644 --- a/boot/bootutil/include/bootutil/crypto/aes_ctr.h +++ b/boot/bootutil/include/bootutil/crypto/aes_ctr.h @@ -15,8 +15,9 @@ #include "mcuboot_config/mcuboot_config.h" #if (defined(MCUBOOT_USE_MBED_TLS) + \ - defined(MCUBOOT_USE_TINYCRYPT)) != 1 - #error "One crypto backend must be defined: either MBED_TLS or TINYCRYPT" + defined(MCUBOOT_USE_TINYCRYPT) + \ + defined (MCUBOOT_USE_CUSTOM_CRYPT)) != 1 + #error "One crypto backend must be defined: either MBED_TLS, TINYCRYPT or CUSTOM_CRYPT" #endif #if defined(MCUBOOT_USE_MBED_TLS) @@ -38,6 +39,10 @@ #define BOOTUTIL_CRYPTO_AES_CTR_BLOCK_SIZE TC_AES_BLOCK_SIZE #endif /* MCUBOOT_USE_TINYCRYPT */ +#if defined (MCUBOOT_USE_CUSTOM_CRYPT) + #include "aes_ctr_custom.h" +#endif /* MCUBOOT_USE_CUSTOM_CRYPT */ + #include #ifdef __cplusplus diff --git a/boot/bootutil/include/bootutil/crypto/aes_kw.h b/boot/bootutil/include/bootutil/crypto/aes_kw.h index cf3194f9c..2eb7df6f8 100644 --- a/boot/bootutil/include/bootutil/crypto/aes_kw.h +++ b/boot/bootutil/include/bootutil/crypto/aes_kw.h @@ -13,8 +13,9 @@ #include "mcuboot_config/mcuboot_config.h" #if (defined(MCUBOOT_USE_MBED_TLS) + \ - defined(MCUBOOT_USE_TINYCRYPT)) != 1 - #error "One crypto backend must be defined: either MBED_TLS or TINYCRYPT" + defined(MCUBOOT_USE_TINYCRYPT) + \ + defined (MCUBOOT_USE_CUSTOM_CRYPT)) != 1 + #error "One crypto backend must be defined: either MBED_TLS, TINYCRYPT or CUSTOM_CRYPT" #endif #if defined(MCUBOOT_USE_MBED_TLS) @@ -30,6 +31,10 @@ #include #endif /* MCUBOOT_USE_TINYCRYPT */ +#if defined (MCUBOOT_USE_CUSTOM_CRYPT) + #include "aes_kw_custom.h" +#endif /* MCUBOOT_USE_CUSTOM_CRYPT */ + #include #ifdef __cplusplus diff --git a/boot/bootutil/include/bootutil/crypto/ecdh_p256.h b/boot/bootutil/include/bootutil/crypto/ecdh_p256.h index 962535cbc..d7d3e9047 100644 --- a/boot/bootutil/include/bootutil/crypto/ecdh_p256.h +++ b/boot/bootutil/include/bootutil/crypto/ecdh_p256.h @@ -13,8 +13,9 @@ #include "mcuboot_config/mcuboot_config.h" #if (defined(MCUBOOT_USE_MBED_TLS) + \ - defined(MCUBOOT_USE_TINYCRYPT)) != 1 - #error "One crypto backend must be defined: either MBED_TLS or TINYCRYPT" + defined(MCUBOOT_USE_TINYCRYPT) + \ + defined (MCUBOOT_USE_CUSTOM_CRYPT)) != 1 + #error "One crypto backend must be defined: either MBED_TLS, TINYCRYPT or CUSTOM_CRYPT" #endif #if defined(MCUBOOT_USE_MBED_TLS) @@ -29,6 +30,9 @@ #define BOOTUTIL_CRYPTO_ECDH_P256_HASH_SIZE (4 * 8) #endif /* MCUBOOT_USE_TINYCRYPT */ +#if defined (MCUBOOT_USE_CUSTOM_CRYPT) + #include "ecdh_p256_custom.h" +#endif /* MCUBOOT_USE_CUSTOM_CRYPT */ #ifdef __cplusplus extern "C" { #endif diff --git a/boot/bootutil/include/bootutil/crypto/ecdh_x25519.h b/boot/bootutil/include/bootutil/crypto/ecdh_x25519.h index 1d11b6473..c9d106e5f 100644 --- a/boot/bootutil/include/bootutil/crypto/ecdh_x25519.h +++ b/boot/bootutil/include/bootutil/crypto/ecdh_x25519.h @@ -13,10 +13,14 @@ #include "mcuboot_config/mcuboot_config.h" #if (defined(MCUBOOT_USE_MBED_TLS) + \ - defined(MCUBOOT_USE_TINYCRYPT)) != 1 - #error "One crypto backend must be defined: either MBED_TLS or TINYCRYPT" + defined(MCUBOOT_USE_TINYCRYPT) + \ + defined (MCUBOOT_USE_CUSTOM_CRYPT)) != 1 + #error "One crypto backend must be defined: either MBED_TLS, TINYCRYPT or CUSTOM_CRYPT" #endif +#if defined(MCUBOOT_USE_CUSTOM_CRYPT) + #include "ecdh_x25519_custom.h" +#endif /* MCUBOOT_USE_CUSTOM_CRYPT */ #ifdef __cplusplus extern "C" { #endif diff --git a/boot/bootutil/include/bootutil/crypto/ecdsa.h b/boot/bootutil/include/bootutil/crypto/ecdsa.h index a73388622..a5179d1ba 100644 --- a/boot/bootutil/include/bootutil/crypto/ecdsa.h +++ b/boot/bootutil/include/bootutil/crypto/ecdsa.h @@ -34,8 +34,9 @@ #if (defined(MCUBOOT_USE_TINYCRYPT) + \ defined(MCUBOOT_USE_CC310) + \ - defined(MCUBOOT_USE_PSA_OR_MBED_TLS)) != 1 - #error "One crypto backend must be defined: either CC310/TINYCRYPT/MBED_TLS/PSA_CRYPTO" + defined(MCUBOOT_USE_PSA_OR_MBED_TLS) +\ + defined (MCUBOOT_USE_CUSTOM_CRYPT)) != 1 + #error "One crypto backend must be defined: either CC310/TINYCRYPT/MBED_TLS/PSA_CRYPTO/CUSTOM_CRYPT" #endif #if defined(MCUBOOT_USE_TINYCRYPT) @@ -62,6 +63,10 @@ #define NUM_ECC_BYTES (256 / 8) #endif +#if defined (MCUBOOT_USE_CUSTOM_CRYPT) + #include "ecdsa_custom.h" +#endif /* MCUBOOT_USE_CUSTOM_CRYPT */ + /* Universal defines */ #define BOOTUTIL_CRYPTO_ECDSA_P256_HASH_SIZE (32) @@ -74,7 +79,7 @@ extern "C" { #endif -#if (defined(MCUBOOT_USE_TINYCRYPT) || defined(MCUBOOT_USE_MBED_TLS) || \ +#if (defined(MCUBOOT_USE_TINYCRYPT) || defined(MCUBOOT_USE_MBED_TLS) ||\ defined(MCUBOOT_USE_CC310)) && !defined(MCUBOOT_USE_PSA_CRYPTO) /* * Declaring these like this adds NULL termination. diff --git a/boot/bootutil/include/bootutil/crypto/hmac_sha256.h b/boot/bootutil/include/bootutil/crypto/hmac_sha256.h index e6840182c..26e73b977 100644 --- a/boot/bootutil/include/bootutil/crypto/hmac_sha256.h +++ b/boot/bootutil/include/bootutil/crypto/hmac_sha256.h @@ -13,8 +13,9 @@ #include "mcuboot_config/mcuboot_config.h" #if (defined(MCUBOOT_USE_MBED_TLS) + \ - defined(MCUBOOT_USE_TINYCRYPT)) != 1 - #error "One crypto backend must be defined: either MBED_TLS or TINYCRYPT" + defined(MCUBOOT_USE_TINYCRYPT) + \ + defined (MCUBOOT_USE_CUSTOM_CRYPT)) != 1 + #error "One crypto backend must be defined: either MBED_TLS, TINYCRYPT or CUSTOM_CRYPT" #endif #if defined(MCUBOOT_USE_MBED_TLS) @@ -31,6 +32,10 @@ #include #endif /* MCUBOOT_USE_TINYCRYPT */ +#if defined (MCUBOOT_USE_CUSTOM_CRYPT) + #include "hmac_sha256_custom.h" +#endif /* MCUBOOT_USE_CUSTOM_CRYPT */ + #include #ifdef __cplusplus diff --git a/boot/bootutil/include/bootutil/crypto/rsa.h b/boot/bootutil/include/bootutil/crypto/rsa.h index 581e4ec9b..b528ab4ef 100644 --- a/boot/bootutil/include/bootutil/crypto/rsa.h +++ b/boot/bootutil/include/bootutil/crypto/rsa.h @@ -28,7 +28,7 @@ #include "mcuboot_config/mcuboot_config.h" -#if defined(MCUBOOT_USE_PSA_CRYPTO) || defined(MCUBOOT_USE_MBED_TLS) +#if defined(MCUBOOT_USE_PSA_CRYPTO) || defined(MCUBOOT_USE_MBED_TLS) || defined(MCUBOOT_USE_CUSTOM_CRYPT) #define MCUBOOT_USE_PSA_OR_MBED_TLS #endif /* MCUBOOT_USE_PSA_CRYPTO || MCUBOOT_USE_MBED_TLS */ @@ -57,6 +57,9 @@ #endif /* MCUBOOT_USE_MBED_TLS */ +#if defined(MCUBOOT_USE_CUSTOM_CRYPT) + #include "rsa_custom.h" +#endif /* MCUBOOT_USE_CUSTOM_CRYPT */ #include #ifdef __cplusplus diff --git a/boot/bootutil/include/bootutil/crypto/sha.h b/boot/bootutil/include/bootutil/crypto/sha.h index 9ce54bee5..12e91602d 100644 --- a/boot/bootutil/include/bootutil/crypto/sha.h +++ b/boot/bootutil/include/bootutil/crypto/sha.h @@ -30,7 +30,8 @@ #if (defined(MCUBOOT_USE_PSA_OR_MBED_TLS) + \ defined(MCUBOOT_USE_TINYCRYPT) + \ - defined(MCUBOOT_USE_CC310)) != 1 + defined(MCUBOOT_USE_CC310) +\ + defined (MCUBOOT_USE_CUSTOM_CRYPT)) != 1 #error "One crypto backend must be defined: either CC310/MBED_TLS/TINYCRYPT/PSA_CRYPTO" #endif @@ -69,6 +70,10 @@ #include #endif /* MCUBOOT_USE_CC310 */ +#if defined (MCUBOOT_USE_CUSTOM_CRYPT) + #include "sha_custom.h" +#endif /* MCUBOOT_USE_CUSTOM_CRYPT */ + #include #ifdef __cplusplus