diff --git a/boot/bootutil/include/bootutil/sha256.h b/boot/bootutil/include/bootutil/sha256.h index 91bbe22c5..740ad5eed 100644 --- a/boot/bootutil/include/bootutil/sha256.h +++ b/boot/bootutil/include/bootutil/sha256.h @@ -49,6 +49,10 @@ #include #endif /* MCUBOOT_USE_CC310 */ +#ifdef MCUBOOT_USE_NRF_EXTERNAL_CRYPTO + #include +#endif /* MCUBOOT_USE_NRF_EXTERNAL_CRYPTO */ + #include #ifdef __cplusplus @@ -119,6 +123,29 @@ static inline void bootutil_sha256_finish(bootutil_sha256_context *ctx, } #endif /* MCUBOOT_USE_CC310 */ +#ifdef MCUBOOT_USE_NRF_EXTERNAL_CRYPTO +typedef bl_sha256_ctx_t bootutil_sha256_context; + +static inline void bootutil_sha256_init(bootutil_sha256_context *ctx) +{ + bl_sha256_init(ctx); +} + +static inline void bootutil_sha256_update(bootutil_sha256_context *ctx, + const void * data, + uint32_t data_len) +{ + bl_sha256_update(ctx, data, data_len); + +} + +static inline void bootutil_sha256_finish(bootutil_sha256_context *ctx, + uint8_t * output) +{ + bl_sha256_finalize(ctx, output); +} +#endif /* MCUBOOT_USE_NRF_EXTERNAL_CRYPTO */ + #ifdef __cplusplus } #endif diff --git a/boot/bootutil/src/image_ec256.c b/boot/bootutil/src/image_ec256.c index a121e6da6..f1310c021 100644 --- a/boot/bootutil/src/image_ec256.c +++ b/boot/bootutil/src/image_ec256.c @@ -34,6 +34,11 @@ #include "cc310_glue.h" #define NUM_ECC_BYTES (4*8) #endif +#ifdef MCUBOOT_USE_NRF_EXTERNAL_CRYPTO +#include "bl_crypto.h" +#define NUM_ECC_BYTES (4*8) +#endif + #include "bootutil_priv.h" /* @@ -183,6 +188,7 @@ bootutil_verify_sig(uint8_t *hash, uint32_t hlen, uint8_t *sig, size_t slen, } } #endif /* MCUBOOT_USE_TINYCRYPT */ + #ifdef MCUBOOT_USE_CC310 int bootutil_verify_sig(uint8_t *hash, @@ -227,4 +233,50 @@ bootutil_verify_sig(uint8_t *hash, return rc; } #endif /* MCUBOOT_USE_CC310 */ + +#ifdef MCUBOOT_USE_NRF_EXTERNAL_CRYPTO +int +bootutil_verify_sig(uint8_t *hash, + uint32_t hlen, + uint8_t *sig, + size_t slen, + uint8_t key_id) +{ + int rc; + uint8_t *pubkey; + uint8_t *end; + uint8_t signature[2 * NUM_ECC_BYTES]; + + pubkey = (uint8_t *)bootutil_keys[key_id].key; + end = pubkey + *bootutil_keys[key_id].len; + + rc = bootutil_import_key(&pubkey, end); + if (rc) { + return -1; + } + + /* Decode signature */ + rc = bootutil_decode_sig(signature, sig, sig + slen); + if (rc) { + return -1; + } + + /* + * This is simplified, as the hash length is also 32 bytes. + */ + if (hlen != NUM_ECC_BYTES) { + return -1; + } + + /* Initialize and verify in one go */ + rc = bl_secp256r1_validate(hash, hlen, pubkey, signature); + + if(rc != 0 /*CRYS_OK*/){ + return -2; + } + + return rc; +} +#endif /* MCUBOOT_USE_NRF_EXTERNAL_CRYPTO */ + #endif /* MCUBOOT_SIGN_EC256 */ diff --git a/boot/zephyr/CMakeLists.txt b/boot/zephyr/CMakeLists.txt index b33967423..647ba13a1 100644 --- a/boot/zephyr/CMakeLists.txt +++ b/boot/zephyr/CMakeLists.txt @@ -74,6 +74,8 @@ if(CONFIG_BOOT_USE_NRF_CC310_BL) set(NRFXLIB_DIR ${MCUBOOT_DIR}/../nrfxlib) assert_exists(NRFXLIB_DIR) endif() +set(NRF_EXTERNAL_CRYPTO_DIR "${MCUBOOT_DIR}/../nrf/subsys/bootloader/bl_crypto") +assert_exists(NRF_EXTERNAL_CRYPTO_DIR) zephyr_library_include_directories( include @@ -137,6 +139,8 @@ if(CONFIG_BOOT_SIGNATURE_TYPE_ECDSA_P256) zephyr_library_sources(${NRF_DIR}/cc310_glue.c) zephyr_library_include_directories(${NRF_DIR}) zephyr_link_libraries(nrfxlib_crypto) + elseif(CONFIG_BOOT_USE_NRF_EXTERNAL_CRYPTO) + zephyr_include_directories(${BL_CRYPTO_DIR}/../include) endif() # Since here we are not using Zephyr's mbedTLS but rather our own, we need diff --git a/boot/zephyr/Kconfig b/boot/zephyr/Kconfig index 7634aeecc..52072df52 100644 --- a/boot/zephyr/Kconfig +++ b/boot/zephyr/Kconfig @@ -47,6 +47,17 @@ config BOOT_USE_NRF_CC310_BL bool default n +config BOOT_USE_NRF_EXTERNAL_CRYPTO + bool + # Hidden option + default n + # When building for ECDSA, we use our own copy of mbedTLS, so the + # Zephyr one must not be enabled or the MBEDTLS_CONFIG_FILE macros + # will collide. + depends on ! MBEDTLS + help + Use Shared crypto for crypto primitives. + menu "MCUBoot settings" choice @@ -76,19 +87,28 @@ config BOOT_SIGNATURE_TYPE_ED25519 if BOOT_SIGNATURE_TYPE_ECDSA_P256 choice prompt "Ecdsa implementation" + default BOOT_NRF_EXTERNAL_CRYPTO if SECURE_BOOT default BOOT_CC310 if HAS_HW_NRF_CC310 default BOOT_TINYCRYPT + config BOOT_TINYCRYPT bool "Use tinycrypt" select BOOT_USE_TINYCRYPT + config BOOT_CC310 bool "Use CC310" select BOOT_USE_NRF_CC310_BL if HAS_HW_NRF_CC310 - select NRF_CC310_BL if HAS_HW_NRF_CC310 select NRFXLIB_CRYPTO if SOC_FAMILY_NRF select BOOT_USE_CC310 + +config BOOT_NRF_EXTERNAL_CRYPTO + bool "Use Shared Crypto from bootloader" + select BOOT_USE_NRF_EXTERNAL_CRYPTO + depends on SECURE_BOOT + endchoice -endif +endif #BOOT_SIGNATURE_TYPE_ECDSA_P256 + endchoice config BOOT_SIGNATURE_KEY_FILE diff --git a/boot/zephyr/include/mcuboot_config/mcuboot_config.h b/boot/zephyr/include/mcuboot_config/mcuboot_config.h index 925591c37..a5673f7e7 100644 --- a/boot/zephyr/include/mcuboot_config/mcuboot_config.h +++ b/boot/zephyr/include/mcuboot_config/mcuboot_config.h @@ -39,9 +39,8 @@ #define MCUBOOT_USE_TINYCRYPT #elif defined(CONFIG_BOOT_USE_CC310) #define MCUBOOT_USE_CC310 -#ifdef CONFIG_BOOT_USE_NRF_CC310_BL -#define MCUBOOT_USE_NRF_CC310_BL -#endif +#elif defined(CONFIG_BOOT_USE_NRF_EXTERNAL_CRYPTO) +#define MCUBOOT_USE_NRF_EXTERNAL_CRYPTO #endif #ifdef CONFIG_BOOT_VALIDATE_SLOT0