diff options
Diffstat (limited to 'thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/crypto/backend/cc310_bl')
10 files changed, 1430 insertions, 0 deletions
diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/crypto/backend/cc310_bl/cc310_bl_backend_ecc.c b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/crypto/backend/cc310_bl/cc310_bl_backend_ecc.c new file mode 100644 index 0000000..cec2ec7 --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/crypto/backend/cc310_bl/cc310_bl_backend_ecc.c @@ -0,0 +1,165 @@ +/** + * Copyright (c) 2018 - 2018, Nordic Semiconductor ASA + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form, except as embedded into a Nordic + * Semiconductor ASA integrated circuit in a product or a software update for + * such product, must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * 4. This software, with or without modification, must only be used with a + * Nordic Semiconductor ASA integrated circuit. + * + * 5. Any software provided in binary form under this license must not be reverse + * engineered, decompiled, modified and/or disassembled. + * + * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include "sdk_config.h" +#include "nordic_common.h" + +#if NRF_MODULE_ENABLED(NRF_CRYPTO) && NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_BL) + +#include <string.h> + +#include "nrf_crypto_mem.h" +#include "nrf_crypto_ecc.h" +#include "nrf_crypto_shared.h" +#include "cc310_bl_backend_ecc.h" + + +#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_BL_ECC_LITTLE_ENDIAN) +#define ENDIAN_MEM_COPY nrf_crypto_internal_swap_endian +#else +#define ENDIAN_MEM_COPY memcpy +#endif + + +#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_BL_ECC_SECP224R1) + + +ret_code_t nrf_crypto_backend_secp224r1_public_key_from_raw( + void * p_public_key, + uint8_t const * p_raw_data) +{ + nrf_crypto_backend_secp224r1_public_key_t * p_pub = + (nrf_crypto_backend_secp224r1_public_key_t *)p_public_key; + + ENDIAN_MEM_COPY(&p_pub->public_key.x[0], + &p_raw_data[0], + NRF_CRYPTO_ECC_SECP224R1_RAW_PRIVATE_KEY_SIZE); + ENDIAN_MEM_COPY(&p_pub->public_key.y[0], + &p_raw_data[NRF_CRYPTO_ECC_SECP224R1_RAW_PRIVATE_KEY_SIZE], + NRF_CRYPTO_ECC_SECP224R1_RAW_PRIVATE_KEY_SIZE); + + return NRF_SUCCESS; +} + + +ret_code_t nrf_crypto_backend_secp224r1_public_key_to_raw( + void const * p_public_key, + uint8_t * p_raw_data) +{ + nrf_crypto_backend_secp224r1_public_key_t const * p_pub = + (nrf_crypto_backend_secp224r1_public_key_t const *)p_public_key; + + ENDIAN_MEM_COPY(&p_raw_data[0], + &p_pub->public_key.x[0], + NRF_CRYPTO_ECC_SECP224R1_RAW_PRIVATE_KEY_SIZE); + ENDIAN_MEM_COPY(&p_raw_data[NRF_CRYPTO_ECC_SECP224R1_RAW_PRIVATE_KEY_SIZE], + &p_pub->public_key.y[0], + NRF_CRYPTO_ECC_SECP224R1_RAW_PRIVATE_KEY_SIZE); + + return NRF_SUCCESS; +} + + +nrf_crypto_ecc_curve_info_t const g_nrf_crypto_ecc_secp224r1_curve_info = +{ + .public_key_size = sizeof(nrf_crypto_backend_secp224r1_public_key_t), + .private_key_size = 0, + .curve_type = NRF_CRYPTO_ECC_SECP224R1_CURVE_TYPE, + .raw_private_key_size = NRF_CRYPTO_ECC_SECP224R1_RAW_PRIVATE_KEY_SIZE, + .raw_public_key_size = NRF_CRYPTO_ECC_SECP224R1_RAW_PUBLIC_KEY_SIZE, +}; + + +#endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_BL_ECC_SECP224R1) + + +#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_BL_ECC_SECP256R1) + + +ret_code_t nrf_crypto_backend_secp256r1_public_key_from_raw( + void * p_public_key, + uint8_t const * p_raw_data) +{ + nrf_crypto_backend_secp256r1_public_key_t * p_pub = + (nrf_crypto_backend_secp256r1_public_key_t *)p_public_key; + + ENDIAN_MEM_COPY(&p_pub->public_key.x[0], + &p_raw_data[0], + NRF_CRYPTO_ECC_SECP256R1_RAW_PRIVATE_KEY_SIZE); + ENDIAN_MEM_COPY(&p_pub->public_key.y[0], + &p_raw_data[NRF_CRYPTO_ECC_SECP256R1_RAW_PRIVATE_KEY_SIZE], + NRF_CRYPTO_ECC_SECP256R1_RAW_PRIVATE_KEY_SIZE); + + return NRF_SUCCESS; +} + + +ret_code_t nrf_crypto_backend_secp256r1_public_key_to_raw( + void const * p_public_key, + uint8_t * p_raw_data) +{ + nrf_crypto_backend_secp256r1_public_key_t const * p_pub = + (nrf_crypto_backend_secp256r1_public_key_t const *)p_public_key; + + ENDIAN_MEM_COPY(&p_raw_data[0], + &p_pub->public_key.x[0], + NRF_CRYPTO_ECC_SECP256R1_RAW_PRIVATE_KEY_SIZE); + ENDIAN_MEM_COPY(&p_raw_data[NRF_CRYPTO_ECC_SECP256R1_RAW_PRIVATE_KEY_SIZE], + &p_pub->public_key.y[0], + NRF_CRYPTO_ECC_SECP256R1_RAW_PRIVATE_KEY_SIZE); + + return NRF_SUCCESS; +} + + +nrf_crypto_ecc_curve_info_t const g_nrf_crypto_ecc_secp256r1_curve_info = +{ + .public_key_size = sizeof(nrf_crypto_backend_secp256r1_public_key_t), + .private_key_size = 0, + .curve_type = NRF_CRYPTO_ECC_SECP256R1_CURVE_TYPE, + .raw_private_key_size = NRF_CRYPTO_ECC_SECP256R1_RAW_PRIVATE_KEY_SIZE, + .raw_public_key_size = NRF_CRYPTO_ECC_SECP256R1_RAW_PUBLIC_KEY_SIZE, +}; + + +#endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_BL_ECC_SECP256R1) + + +#endif // NRF_MODULE_ENABLED(NRF_CRYPTO) && NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_BL) diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/crypto/backend/cc310_bl/cc310_bl_backend_ecc.h b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/crypto/backend/cc310_bl/cc310_bl_backend_ecc.h new file mode 100644 index 0000000..381c642 --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/crypto/backend/cc310_bl/cc310_bl_backend_ecc.h @@ -0,0 +1,155 @@ +/** + * Copyright (c) 2018 - 2018, Nordic Semiconductor ASA + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form, except as embedded into a Nordic + * Semiconductor ASA integrated circuit in a product or a software update for + * such product, must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * 4. This software, with or without modification, must only be used with a + * Nordic Semiconductor ASA integrated circuit. + * + * 5. Any software provided in binary form under this license must not be reverse + * engineered, decompiled, modified and/or disassembled. + * + * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef CC310_BL_BACKEND_ECC_H__ +#define CC310_BL_BACKEND_ECC_H__ + +#include "sdk_config.h" +#include "nordic_common.h" + +#if NRF_MODULE_ENABLED(NRF_CRYPTO) && NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_BL) + +#include "nrf_crypto_ecc_shared.h" +#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_BL_ECC_SECP224R1) +#include "nrf_cc310_bl_ecdsa_verify_secp224r1.h" +#endif +#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_BL_ECC_SECP256R1) +#include "nrf_cc310_bl_ecdsa_verify_secp256r1.h" +#endif + + +#ifdef __cplusplus +extern "C" { +#endif + + +#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_BL_ECC_SECP224R1) + +#if NRF_MODULE_ENABLED(NRF_CRYPTO_ECC_SECP224R1) +#error "More than one backend enabled for secp224r1 (NIST 224-bit)."); +#endif +#define NRF_CRYPTO_ECC_SECP224R1_ENABLED 1 + +typedef struct +{ + nrf_crypto_internal_ecc_key_header_t header; /**< @internal @brief Common ECC key header */ + nrf_cc310_bl_ecc_public_key_secp224r1_t public_key; /**< @internal @brief CC310_BL specific key representation */ +} nrf_crypto_backend_secp224r1_public_key_t; + +/** @internal See @ref nrf_crypto_backend_ecc_public_key_from_raw_fn_t. +*/ +ret_code_t nrf_crypto_backend_secp224r1_public_key_from_raw( + void * p_public_key, + uint8_t const * p_raw_data); + +/** @internal See @ref nrf_crypto_backend_ecc_public_key_to_raw_fn_t. +*/ +ret_code_t nrf_crypto_backend_secp224r1_public_key_to_raw( + void const * p_public_key, + uint8_t * p_raw_data); + +// Dummy and empty definitions for unused symbols +#define nrf_crypto_backend_secp224r1_key_pair_generate NULL +#define nrf_crypto_backend_secp224r1_public_key_calculate NULL +#define nrf_crypto_backend_secp224r1_private_key_from_raw NULL +#define nrf_crypto_backend_secp224r1_private_key_to_raw NULL +#define nrf_crypto_backend_secp224r1_private_key_free NULL +#define nrf_crypto_backend_secp224r1_public_key_free NULL + +#define NRF_CRYPTO_BACKEND_SECP224R1_KEY_PAIR_GENERATE_CONTEXT_SIZE 0 +#define NRF_CRYPTO_BACKEND_SECP224R1_PUBLIC_KEY_CALCULATE_CONTEXT_SIZE 0 + +typedef uint32_t nrf_crypto_backend_secp224r1_private_key_t; +typedef uint32_t nrf_crypto_backend_secp224r1_key_pair_generate_context_t; +typedef uint32_t nrf_crypto_backend_secp224r1_public_key_calculate_context_t; + +#endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_BL_ECC_SECP224R1) + + +#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_BL_ECC_SECP256R1) + +#if NRF_MODULE_ENABLED(NRF_CRYPTO_ECC_SECP256R1) +#error "More than one backend enabled for secp256r1 (NIST 256-bit)."); +#endif +#define NRF_CRYPTO_ECC_SECP256R1_ENABLED 1 + +typedef struct +{ + nrf_crypto_internal_ecc_key_header_t header; /**< @internal @brief Common ECC key header */ + nrf_cc310_bl_ecc_public_key_secp256r1_t public_key; /**< @internal @brief CC310_BL specific key representation */ +} nrf_crypto_backend_secp256r1_public_key_t; + +/** @internal See @ref nrf_crypto_backend_ecc_public_key_from_raw_fn_t. +*/ +ret_code_t nrf_crypto_backend_secp256r1_public_key_from_raw( + void * p_public_key, + uint8_t const * p_raw_data); + +/** @internal See @ref nrf_crypto_backend_ecc_public_key_to_raw_fn_t. +*/ +ret_code_t nrf_crypto_backend_secp256r1_public_key_to_raw( + void const * p_public_key, + uint8_t * p_raw_data); + +// Dummy and empty definitions for unused symbols +#define nrf_crypto_backend_secp256r1_key_pair_generate NULL +#define nrf_crypto_backend_secp256r1_public_key_calculate NULL +#define nrf_crypto_backend_secp256r1_private_key_from_raw NULL +#define nrf_crypto_backend_secp256r1_private_key_to_raw NULL +#define nrf_crypto_backend_secp256r1_private_key_free NULL +#define nrf_crypto_backend_secp256r1_public_key_free NULL + +#define NRF_CRYPTO_BACKEND_SECP256R1_KEY_PAIR_GENERATE_CONTEXT_SIZE 0 +#define NRF_CRYPTO_BACKEND_SECP256R1_PUBLIC_KEY_CALCULATE_CONTEXT_SIZE 0 + +typedef uint32_t nrf_crypto_backend_secp256r1_private_key_t; +typedef uint32_t nrf_crypto_backend_secp256r1_key_pair_generate_context_t; +typedef uint32_t nrf_crypto_backend_secp256r1_public_key_calculate_context_t; + +#endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_BL_ECC_SECP256R1) + + +#ifdef __cplusplus +} +#endif + +#endif // NRF_MODULE_ENABLED(NRF_CRYPTO) && NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_BL) + +#endif // CC310_BL_BACKEND_ECC_H__ diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/crypto/backend/cc310_bl/cc310_bl_backend_ecdh.h b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/crypto/backend/cc310_bl/cc310_bl_backend_ecdh.h new file mode 100644 index 0000000..ce52194 --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/crypto/backend/cc310_bl/cc310_bl_backend_ecdh.h @@ -0,0 +1,75 @@ +/** + * Copyright (c) 2018 - 2018, Nordic Semiconductor ASA + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form, except as embedded into a Nordic + * Semiconductor ASA integrated circuit in a product or a software update for + * such product, must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * 4. This software, with or without modification, must only be used with a + * Nordic Semiconductor ASA integrated circuit. + * + * 5. Any software provided in binary form under this license must not be reverse + * engineered, decompiled, modified and/or disassembled. + * + * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef CC310_BL_BACKEND_ECDH_H__ +#define CC310_BL_BACKEND_ECDH_H__ + +#include "sdk_config.h" +#include "nordic_common.h" + +#if NRF_MODULE_ENABLED(NRF_CRYPTO) && NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_BL) + +#include "nrf_crypto_ecc.h" +#include "nrf_crypto_ecdh_shared.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_BL_ECC_SECP224R1) +#define nrf_crypto_backend_secp224r1_ecdh_compute NULL +typedef uint32_t nrf_crypto_backend_secp224r1_ecdh_context_t; +#define NRF_CRYPTO_BACKEND_SECP224R1_ECDH_CONTEXT_SIZE 0 +#endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_BL_ECC_SECP224R1) + + +#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_BL_ECC_SECP256R1) +#define nrf_crypto_backend_secp256r1_ecdh_compute NULL +typedef uint32_t nrf_crypto_backend_secp256r1_ecdh_context_t; +#define NRF_CRYPTO_BACKEND_SECP256R1_ECDH_CONTEXT_SIZE 0 +#endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_BL_ECC_SECP256R1) + +#ifdef __cplusplus +} +#endif + +#endif // NRF_MODULE_ENABLED(NRF_CRYPTO) && NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_BL) + +#endif // CC310_BL_BACKEND_ECDH_H__ diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/crypto/backend/cc310_bl/cc310_bl_backend_ecdsa.c b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/crypto/backend/cc310_bl/cc310_bl_backend_ecdsa.c new file mode 100644 index 0000000..5b99a83 --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/crypto/backend/cc310_bl/cc310_bl_backend_ecdsa.c @@ -0,0 +1,267 @@ +/** + * Copyright (c) 2018 - 2018, Nordic Semiconductor ASA + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form, except as embedded into a Nordic + * Semiconductor ASA integrated circuit in a product or a software update for + * such product, must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * 4. This software, with or without modification, must only be used with a + * Nordic Semiconductor ASA integrated circuit. + * + * 5. Any software provided in binary form under this license must not be reverse + * engineered, decompiled, modified and/or disassembled. + * + * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include "sdk_config.h" +#include "nordic_common.h" + +#if NRF_MODULE_ENABLED(NRF_CRYPTO) && NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_BL) + +#include <string.h> +#include "app_util.h" +#include "nrf_crypto_error.h" +#include "nrf_crypto_mem.h" +#include "nrf_crypto_shared.h" +#include "cc310_bl_backend_ecdsa.h" +#include "cc310_bl_backend_shared.h" +#include "cc310_backend_mutex.h" +#include "crys_ecpki_error.h" + + +#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_BL_ECC_SECP224R1) || \ + NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_BL_ECC_SECP256R1) + +static ret_code_t crys_error_to_ret_code(CRYSError_t crys_error) +{ + switch (crys_error) + { + case CRYS_OK: + return NRF_SUCCESS; + + case CRYS_ECDSA_VERIFY_INCONSISTENT_VERIFY_ERROR: + return NRF_ERROR_CRYPTO_ECDSA_INVALID_SIGNATURE; + + default: + break; + } + + return NRF_ERROR_CRYPTO_INTERNAL; +} + +#endif + + +#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_BL_ECC_SECP224R1) + +STATIC_ASSERT(offsetof(nrf_cc310_bl_ecc_signature_secp224r1_t, r) == 0, + "Offset of r in nrf_cc310_bl_ecc_signature_secp224r1_t is unexpected"); +STATIC_ASSERT(offsetof(nrf_cc310_bl_ecc_signature_secp224r1_t, s) == + NRF_CRYPTO_ECC_SECP224R1_RAW_PRIVATE_KEY_SIZE, + "Offset of s in nrf_cc310_bl_ecc_signature_secp224r1_t is unexpected"); + + +ret_code_t nrf_crypto_backend_secp224r1_verify( + void * p_context, + void const * p_public_key, + uint8_t const * p_data, + size_t data_size, + uint8_t const * p_signature) +{ + ret_code_t result; + CRYSError_t crys_error; + bool mutex_locked; + + nrf_crypto_backend_secp224r1_verify_context_t * p_ctx = + (nrf_crypto_backend_secp224r1_verify_context_t *)p_context; + + nrf_crypto_backend_secp224r1_public_key_t * p_pub = + (nrf_crypto_backend_secp224r1_public_key_t *)p_public_key; + + p_ctx->user_context.init_val = NRF_CC310_BL_ECDSA_CONTEXT_INITIALIZED; + +#if defined(NRF_CRYPTO_BACKEND_CC310_BL_ECC_LITTLE_ENDIAN_ENABLED) && (NRF_CRYPTO_BACKEND_CC310_BL_ECC_LITTLE_ENDIAN_ENABLED == 1) + + size_t hash_size = MIN(data_size, NRF_CRYPTO_ECC_SECP224R1_RAW_PRIVATE_KEY_SIZE); + uint8_t * p_hash_and_sig_le = + NRF_CRYPTO_ALLOC(hash_size + 2 * NRF_CRYPTO_ECC_SECP224R1_RAW_PRIVATE_KEY_SIZE); + + if (p_hash_and_sig_le == NULL) + { + return NRF_ERROR_CRYPTO_ALLOC_FAILED; + } + + nrf_crypto_internal_swap_endian(p_hash_and_sig_le, p_data, hash_size); + + nrf_crypto_internal_double_swap_endian(&p_hash_and_sig_le[hash_size], + p_signature, + NRF_CRYPTO_ECC_SECP224R1_RAW_PRIVATE_KEY_SIZE); + + mutex_locked = cc310_backend_mutex_trylock(); + VERIFY_TRUE(mutex_locked, NRF_ERROR_CRYPTO_BUSY); + + cc310_bl_backend_enable(); + + crys_error = nrf_cc310_bl_ecdsa_verify_secp224r1( + &p_ctx->user_context, + &p_pub->public_key, + (nrf_cc310_bl_ecc_signature_secp224r1_t const *)&p_hash_and_sig_le[hash_size], + p_hash_and_sig_le, + hash_size); + + cc310_bl_backend_disable(); + + cc310_backend_mutex_unlock(); + + NRF_CRYPTO_FREE(p_hash_and_sig_le); + +#elif defined(NRF_CRYPTO_BACKEND_CC310_BL_ECC_LITTLE_ENDIAN_ENABLED) && (NRF_CRYPTO_BACKEND_CC310_BL_ECC_LITTLE_ENDIAN_ENABLED == 0) + + mutex_locked = cc310_backend_mutex_trylock(); + VERIFY_TRUE(mutex_locked, NRF_ERROR_CRYPTO_BUSY); + + cc310_bl_backend_enable(); + + crys_error = nrf_cc310_bl_ecdsa_verify_secp224r1( + &p_ctx->user_context, + &p_pub->public_key, + (nrf_cc310_bl_ecc_signature_secp224r1_t const *)p_signature, + p_data, + data_size); + + cc310_bl_backend_disable(); + + cc310_backend_mutex_unlock(); + +#else + + #error NRF_CRYPTO_BACKEND_CC310_BL_ECC_LITTLE_ENDIAN_ENABLED define not found in sdk_config.h Inalid sdk_config.h! + +#endif + + result = crys_error_to_ret_code(crys_error); + + return result; +} + + +#endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_BL_ECC_SECP224R1) + + +#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_BL_ECC_SECP256R1) + + +STATIC_ASSERT(offsetof(nrf_cc310_bl_ecc_signature_secp256r1_t, r) == 0, + "Offset of r in nrf_cc310_bl_ecc_signature_secp256r1_t is unexpected"); + +STATIC_ASSERT(offsetof(nrf_cc310_bl_ecc_signature_secp256r1_t, s) == + NRF_CRYPTO_ECC_SECP256R1_RAW_PRIVATE_KEY_SIZE, + "Offset of s in nrf_cc310_bl_ecc_signature_secp256r1_t is unexpected"); + + +ret_code_t nrf_crypto_backend_secp256r1_verify( + void * p_context, + void const * p_public_key, + uint8_t const * p_data, + size_t data_size, + uint8_t const * p_signature) +{ + ret_code_t result; + CRYSError_t crys_error; + bool mutex_locked; + + + nrf_crypto_backend_secp256r1_verify_context_t * p_ctx = + (nrf_crypto_backend_secp256r1_verify_context_t *)p_context; + + nrf_crypto_backend_secp256r1_public_key_t * p_pub = + (nrf_crypto_backend_secp256r1_public_key_t *)p_public_key; + + p_ctx->user_context.init_val = NRF_CC310_BL_ECDSA_CONTEXT_INITIALIZED; + +#if defined(NRF_CRYPTO_BACKEND_CC310_BL_ECC_LITTLE_ENDIAN_ENABLED) && (NRF_CRYPTO_BACKEND_CC310_BL_ECC_LITTLE_ENDIAN_ENABLED == 1) + + uint8_t hash_le[NRF_CRYPTO_HASH_SIZE_SHA256]; + uint8_t signature_le[NRF_CRYPTO_ECC_SECP256R1_RAW_PRIVATE_KEY_SIZE * 2]; + size_t hash_size = MIN(data_size, NRF_CRYPTO_HASH_SIZE_SHA256); + + nrf_crypto_internal_swap_endian(hash_le, p_data, hash_size); + + nrf_crypto_internal_double_swap_endian(signature_le, + p_signature, + NRF_CRYPTO_ECC_SECP256R1_RAW_PRIVATE_KEY_SIZE); + + mutex_locked = cc310_backend_mutex_trylock(); + VERIFY_TRUE(mutex_locked, NRF_ERROR_CRYPTO_BUSY); + + cc310_bl_backend_enable(); + + crys_error = nrf_cc310_bl_ecdsa_verify_secp256r1( + &p_ctx->user_context, + &p_pub->public_key, + (nrf_cc310_bl_ecc_signature_secp256r1_t const *)signature_le, + hash_le, + hash_size); + + cc310_bl_backend_disable(); + + cc310_backend_mutex_unlock(); + +#elif defined(NRF_CRYPTO_BACKEND_CC310_BL_ECC_LITTLE_ENDIAN_ENABLED) && (NRF_CRYPTO_BACKEND_CC310_BL_ECC_LITTLE_ENDIAN_ENABLED == 0) + + mutex_locked = cc310_backend_mutex_trylock(); + VERIFY_TRUE(mutex_locked, NRF_ERROR_CRYPTO_BUSY); + + cc310_bl_backend_enable(); + + crys_error = nrf_cc310_bl_ecdsa_verify_secp256r1( + &p_ctx->user_context, + &p_pub->public_key, + (nrf_cc310_bl_ecc_signature_secp256r1_t const *)p_signature, + p_data, + data_size); + + cc310_bl_backend_disable(); + + cc310_backend_mutex_unlock(); + +#else + + #error NRF_CRYPTO_BACKEND_CC310_BL_ECC_LITTLE_ENDIAN_ENABLED define not found in sdk_config.h. Invalid sdk_config.file! + +#endif + + result = crys_error_to_ret_code(crys_error); + + return result; +} + + +#endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_BL_ECC_SECP256R1) + +#endif // NRF_MODULE_ENABLED(NRF_CRYPTO) && NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_BL) diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/crypto/backend/cc310_bl/cc310_bl_backend_ecdsa.h b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/crypto/backend/cc310_bl/cc310_bl_backend_ecdsa.h new file mode 100644 index 0000000..f75a035 --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/crypto/backend/cc310_bl/cc310_bl_backend_ecdsa.h @@ -0,0 +1,124 @@ +/** + * Copyright (c) 2018 - 2018, Nordic Semiconductor ASA + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form, except as embedded into a Nordic + * Semiconductor ASA integrated circuit in a product or a software update for + * such product, must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * 4. This software, with or without modification, must only be used with a + * Nordic Semiconductor ASA integrated circuit. + * + * 5. Any software provided in binary form under this license must not be reverse + * engineered, decompiled, modified and/or disassembled. + * + * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef CC310_BL_BACKEND_ECDSA_H__ +#define CC310_BL_BACKEND_ECDSA_H__ + +#include "sdk_config.h" +#include "nordic_common.h" + +#if NRF_MODULE_ENABLED(NRF_CRYPTO) && NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_BL) + +#include "nrf_crypto_ecc_shared.h" +#include "nrf_crypto_ecdsa_shared.h" +#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_BL_ECC_SECP224R1) +#include "nrf_cc310_bl_ecdsa_verify_secp224r1.h" +#endif +#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_BL_ECC_SECP256R1) +#include "nrf_cc310_bl_ecdsa_verify_secp256r1.h" +#endif + + +#ifdef __cplusplus +extern "C" { +#endif + + +#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_BL_ECC_SECP224R1) + +/** @internal @brief Common structure holding context for ECDSA verify. + */ +typedef struct +{ + nrf_cc310_bl_ecdsa_verify_context_secp224r1_t user_context; /**< @internal @brief Temporary buffer for CC310_BL internal storage */ +} nrf_crypto_backend_secp224r1_verify_context_t; + +#define NRF_CRYPTO_BACKEND_SECP224R1_VERIFY_CONTEXT_SIZE \ + sizeof(nrf_crypto_backend_secp224r1_verify_context_t) + +ret_code_t nrf_crypto_backend_secp224r1_verify( + void * p_context, + void const * p_public_key, + uint8_t const * p_data, + size_t data_size, + uint8_t const * p_signature); + +// Dummy and empty definitions for unused symbols +#define NRF_CRYPTO_BACKEND_SECP224R1_SIGN_CONTEXT_SIZE 0 +typedef uint32_t nrf_crypto_backend_secp224r1_sign_context_t; +#define nrf_crypto_backend_secp224r1_sign NULL + +#endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_BL_ECC_SECP224R1) + + +#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_BL_ECC_SECP256R1) + +/** @internal @brief Common structure holding context for ECDSA verify. + */ +typedef struct +{ + nrf_cc310_bl_ecdsa_verify_context_secp256r1_t user_context; /**< @internal @brief Temporary buffer for CC310_BL internal storage */ +} nrf_crypto_backend_secp256r1_verify_context_t; + +#define NRF_CRYPTO_BACKEND_SECP256R1_VERIFY_CONTEXT_SIZE \ + sizeof(nrf_crypto_backend_secp256r1_verify_context_t) + +ret_code_t nrf_crypto_backend_secp256r1_verify( + void * p_context, + void const * p_public_key, + uint8_t const * p_data, + size_t data_size, + uint8_t const * p_signature); + +// Dummy and empty definitions for unused symbols +#define NRF_CRYPTO_BACKEND_SECP256R1_SIGN_CONTEXT_SIZE 0 +typedef uint32_t nrf_crypto_backend_secp256r1_sign_context_t; +#define nrf_crypto_backend_secp256r1_sign NULL + +#endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_BL_ECC_SECP256R1) + + +#ifdef __cplusplus +} +#endif + +#endif // NRF_MODULE_ENABLED(NRF_CRYPTO) && NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_BL) + +#endif // CC310_BL_BACKEND_ECDSA_H__ diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/crypto/backend/cc310_bl/cc310_bl_backend_hash.c b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/crypto/backend/cc310_bl/cc310_bl_backend_hash.c new file mode 100644 index 0000000..59010f5 --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/crypto/backend/cc310_bl/cc310_bl_backend_hash.c @@ -0,0 +1,281 @@ +/** + * Copyright (c) 2018 - 2018, Nordic Semiconductor ASA + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form, except as embedded into a Nordic + * Semiconductor ASA integrated circuit in a product or a software update for + * such product, must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * 4. This software, with or without modification, must only be used with a + * Nordic Semiconductor ASA integrated circuit. + * + * 5. Any software provided in binary form under this license must not be reverse + * engineered, decompiled, modified and/or disassembled. + * + * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ +#include "sdk_common.h" + +#if NRF_MODULE_ENABLED(NRF_CRYPTO) && NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_BL) + +#include "nrf.h" +#include "cc310_bl_backend_hash.h" +#include "cc310_bl_backend_shared.h" +#include "cc310_backend_mutex.h" +#include "cc310_backend_shared.h" +#include "nrf_cc310_bl_hash_sha256.h" +#include "crys_hash_error.h" +#include "nrf_crypto_init.h" +#include "nrf_crypto_types.h" +#include "nrf_crypto_error.h" +#include "nrf_crypto_shared.h" +#include "nrf_crypto_hash_shared.h" +#include "sdk_macros.h" +#include "nrf_log.h" +#include "nrf_assert.h" +#include <drivers/nrfx_common.h> + + +#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_BL_HASH_SHA256) + +#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_BL_HASH_AUTOMATIC_RAM_BUFFER) + +__ALIGN(4) static uint8_t m_hash_buffer[NRF_CRYPTO_BACKEND_CC310_BL_HASH_AUTOMATIC_RAM_BUFFER_SIZE]; + +#endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_BL_HASH_AUTOMATIC_RAM_BUFFER) + + +static ret_code_t hash_result_get(CRYSError_t error) +{ + ret_code_t ret_val; + + switch (error) + { + case CRYS_OK: + ret_val = NRF_SUCCESS; + break; + + case CRYS_HASH_INVALID_USER_CONTEXT_POINTER_ERROR: + ret_val = NRF_ERROR_CRYPTO_CONTEXT_NULL; + break; + + case CRYS_HASH_ILLEGAL_OPERATION_MODE_ERROR: + ret_val = NRF_ERROR_CRYPTO_FEATURE_UNAVAILABLE; + break; + + case CRYS_HASH_USER_CONTEXT_CORRUPTED_ERROR: + ret_val = NRF_ERROR_CRYPTO_CONTEXT_NOT_INITIALIZED; + break; + + // May be added to specialized errors for hash. + case CRYS_HASH_LAST_BLOCK_ALREADY_PROCESSED_ERROR: + ret_val = NRF_ERROR_CRYPTO_INTERNAL; + break; + + case CRYS_HASH_IS_NOT_SUPPORTED: + ret_val = NRF_ERROR_CRYPTO_FEATURE_UNAVAILABLE; + break; + + default: + ret_val = NRF_ERROR_CRYPTO_INTERNAL; + break; + } + + return ret_val; +} + + +static ret_code_t cc310_bl_backend_hash_sha256_init(void * const p_context) +{ + uint32_t ret_val; + CRYSError_t crys_error; + + // Limited parameter testing on this level. + // This has been done on upper level. + + nrf_cc310_bl_hash_context_sha256_t * const p_backend_context + = &(((nrf_crypto_backend_hash_sha256_context_t *)p_context)->context); + + crys_error = nrf_cc310_bl_hash_sha256_init(p_backend_context); + + ret_val = hash_result_get(crys_error); + + return ret_val; +} + + +static uint32_t cc310_bl_backend_hash_sha256_update(void * const p_context, + uint8_t const * p_data, + size_t size) +{ + ret_code_t ret_val; + CRYSError_t crys_error; + uint32_t cur_size; + uint32_t size_left; + uint8_t * p_cur; + bool mutex_locked; + + // Limited parameter testing on this level. + // This has been done on upper level. + + nrf_cc310_bl_hash_context_sha256_t * const p_backend_context + = &(((nrf_crypto_backend_hash_sha256_context_t *)p_context)->context); + + p_cur = (uint8_t *)p_data; + size_left = size; + + mutex_locked = cc310_backend_mutex_trylock(); + VERIFY_TRUE(mutex_locked, NRF_ERROR_CRYPTO_BUSY); + + cc310_bl_backend_enable(); + +#if defined (NRF_CRYPTO_BACKEND_CC310_BL_HASH_AUTOMATIC_RAM_BUFFER_ENABLED) && (NRF_CRYPTO_BACKEND_CC310_BL_HASH_AUTOMATIC_RAM_BUFFER_ENABLED == 1) + + do + { + // Copy a block from FLASH to RAM for use in CC310 + cur_size = (size_left > NRF_CRYPTO_BACKEND_CC310_BL_HASH_AUTOMATIC_RAM_BUFFER_SIZE) ? + NRF_CRYPTO_BACKEND_CC310_BL_HASH_AUTOMATIC_RAM_BUFFER_SIZE : size_left; + + // Copy from FLASH to ram + memcpy(m_hash_buffer, p_cur, cur_size); + + // Update the hash with current input. + crys_error = nrf_cc310_bl_hash_sha256_update(p_backend_context, m_hash_buffer, cur_size); + + size_left -= cur_size; + p_cur += cur_size; + + } while(crys_error == SASI_OK && size_left > 0); + +#elif defined(NRF_CRYPTO_BACKEND_CC310_BL_HASH_AUTOMATIC_RAM_BUFFER_ENABLED) && (NRF_CRYPTO_BACKEND_CC310_BL_HASH_AUTOMATIC_RAM_BUFFER_ENABLED == 0) + + // Verify that the data is in RAM (required for CC310 hashing) + VERIFY_TRUE(nrfx_is_in_ram(p_data), NRF_ERROR_CRYPTO_INPUT_LOCATION); + + do + { + // Get the largest block that can sent to the CC310 through DMA + cur_size = (size_left > CC310_MAX_LENGTH_DMA_OPERATIONS) ? + CC310_MAX_LENGTH_DMA_OPERATIONS : size_left; + + crys_error = nrf_cc310_bl_hash_sha256_update(p_backend_context, p_cur, cur_size); + + size_left -= cur_size; + p_cur += cur_size; + } while(crys_error == SASI_OK && size_left > 0); + +#else + + UNUSED_PARAMETER(p_backend_context); + UNUSED_PARAMETER(cur_size); + UNUSED_PARAMETER(size_left); + UNUSED_PARAMETER(p_cur); + + #warning NRF_CRYPTO_BACKEND_CC310_BL_HASH_AUTOMATIC_RAM_BUFFER_ENABLED define not found in sdk_config.h (Is the sdk_config.h valid?). + +#endif + + cc310_bl_backend_disable(); + + cc310_backend_mutex_unlock(); + + ret_val = hash_result_get(crys_error); + + return ret_val; +} + + +static uint32_t cc310_bl_backend_hash_sha256_finalize(void * const p_context, + uint8_t * p_digest, + size_t * const p_digest_size) +{ + ret_code_t ret_val; + CRYSError_t crys_error; + bool mutex_locked; + + // Limited parameter testing on this level. + // This has been done on upper level. + + nrf_cc310_bl_hash_context_sha256_t * const p_backend_context + = &(((nrf_crypto_backend_hash_sha256_context_t * )p_context)->context); + + nrf_cc310_bl_hash_digest_sha256_t * p_int_digest + = (nrf_cc310_bl_hash_digest_sha256_t *)p_digest; + + if (NRF_CRYPTO_HASH_SIZE_SHA256 > *p_digest_size) + { + return NRF_ERROR_CRYPTO_OUTPUT_LENGTH; + } + + mutex_locked = cc310_backend_mutex_trylock(); + VERIFY_TRUE(mutex_locked, NRF_ERROR_CRYPTO_BUSY); + + cc310_bl_backend_enable(); + + // Do the hash finalize calculation + crys_error = nrf_cc310_bl_hash_sha256_finalize(p_backend_context, p_int_digest); + + cc310_bl_backend_disable(); + + cc310_backend_mutex_unlock(); + + ret_val = hash_result_get(crys_error); + + if (ret_val == NRF_SUCCESS) + { + *p_digest_size = NRF_CRYPTO_HASH_SIZE_SHA256; + } + +#if defined(NRF_CRYPTO_BACKEND_CC310_BL_HASH_LITTLE_ENDIAN_DIGEST_ENABLED) && (NRF_CRYPTO_BACKEND_CC310_BL_HASH_LITTLE_ENDIAN_DIGEST_ENABLED == 1) + + nrf_crypto_internal_swap_endian_in_place(p_digest, NRF_CRYPTO_HASH_SIZE_SHA256); + +#elif defined(NRF_CRYPTO_BACKEND_CC310_BL_HASH_LITTLE_ENDIAN_DIGEST_ENABLED) && (NRF_CRYPTO_BACKEND_CC310_BL_HASH_LITTLE_ENDIAN_DIGEST_ENABLED == 0) + + // Do nothing + +#else + + #warning NRF_CRYPTO_BACKEND_CC310_BL_HASH_LITTLE_ENDIAN_DIGEST_ENABLED define not found in sdk_config.h (Is the sdk_config.h valid?). + +#endif + + return ret_val; +} + +const nrf_crypto_hash_info_t g_nrf_crypto_hash_sha256_info = +{ + .init_fn = cc310_bl_backend_hash_sha256_init, + .update_fn = cc310_bl_backend_hash_sha256_update, + .finalize_fn = cc310_bl_backend_hash_sha256_finalize, + .digest_size = NRF_CRYPTO_HASH_SIZE_SHA256, + .hash_mode = NRF_CRYPTO_HASH_MODE_SHA256 +}; + +#endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_BL_HASH_SHA256) + +#endif // NRF_MODULE_ENABLED(NRF_CRYPTO) && #if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_BL) diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/crypto/backend/cc310_bl/cc310_bl_backend_hash.h b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/crypto/backend/cc310_bl/cc310_bl_backend_hash.h new file mode 100644 index 0000000..6882c04 --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/crypto/backend/cc310_bl/cc310_bl_backend_hash.h @@ -0,0 +1,97 @@ +/** + * Copyright (c) 2018 - 2018, Nordic Semiconductor ASA + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form, except as embedded into a Nordic + * Semiconductor ASA integrated circuit in a product or a software update for + * such product, must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * 4. This software, with or without modification, must only be used with a + * Nordic Semiconductor ASA integrated circuit. + * + * 5. Any software provided in binary form under this license must not be reverse + * engineered, decompiled, modified and/or disassembled. + * + * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef CC310_BL_BACKEND_HASH_H__ +#define CC310_BL_BACKEND_HASH_H__ + +/** @file + * + * @defgroup nrf_crypto_cc310_bl_backend_hash nrf_crypto CC310_BL backend hash + * @{ + * @ingroup nrf_crypto_cc310_bl_backend + * + * @brief Hash functionality provided by the nrf_crypto CC310_BL backend. + */ + +#include "sdk_common.h" + +#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_BL) + +#include "sdk_errors.h" +#include "nrf_crypto_hash_shared.h" +#include "nrf_cc310_bl_hash_sha256.h" + +#ifdef __cplusplus +extern "C" { +#endif + + +#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_BL_HASH_SHA256) + +// Flag that nrf_crypto_hash frontend can be compiled +#undef NRF_CRYPTO_HASH_ENABLED +#define NRF_CRYPTO_HASH_ENABLED 1 + +// Flag that SHA-256 is enabled in backend +#if NRF_MODULE_ENABLED(NRF_CRYPTO_HASH_SHA256) +#error "Duplicate definition of SHA-256. More than one backend enabled"); +#endif +#define NRF_CRYPTO_HASH_SHA256_ENABLED 1 + + +/**@internal @brief nrf_crypto_hash context for SHA-256 in nrf_crypto CC310_BL backend. */ +typedef struct +{ + nrf_crypto_hash_internal_context_t header; /**< Common header for context. */ + nrf_cc310_bl_hash_context_sha256_t context; /**< Hash context internal to CC310_BL. */ +} nrf_crypto_backend_hash_sha256_context_t; + + +#endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_BL_HASH_SHA256) + +#ifdef __cplusplus +} +#endif + +#endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_BL) + +/**@} */ + +#endif // CC310_BL_BACKEND_HASH_H__ diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/crypto/backend/cc310_bl/cc310_bl_backend_init.c b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/crypto/backend/cc310_bl/cc310_bl_backend_init.c new file mode 100644 index 0000000..38f440c --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/crypto/backend/cc310_bl/cc310_bl_backend_init.c @@ -0,0 +1,115 @@ +/** + * Copyright (c) 2018 - 2018, Nordic Semiconductor ASA + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form, except as embedded into a Nordic + * Semiconductor ASA integrated circuit in a product or a software update for + * such product, must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * 4. This software, with or without modification, must only be used with a + * Nordic Semiconductor ASA integrated circuit. + * + * 5. Any software provided in binary form under this license must not be reverse + * engineered, decompiled, modified and/or disassembled. + * + * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include "sdk_common.h" +#if NRF_MODULE_ENABLED(NRF_CRYPTO) && NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_BL) + +#include "nrf.h" +#include "nrf_crypto_init.h" +#include "nrf_crypto_error.h" + +#include "cc310_bl_backend_shared.h" +#include "cc310_backend_mutex.h" +#include "sns_silib.h" +#include "nrf_cc310_bl_init.h" + +/**@brief Mutex to ensure single access to nrf_cc310_bl resources */ +nrf_mtx_t g_cc310_mutex; + +static uint32_t init_result_get(uint32_t crys_error) +{ + uint32_t ret_val = NRF_ERROR_INTERNAL; + switch (crys_error) + { + case SA_SILIB_RET_OK: + ret_val = NRF_SUCCESS; + break; + + case SA_SILIB_RET_EINVAL_HW_VERSION: + ret_val = NRF_ERROR_CRYPTO_FEATURE_UNAVAILABLE; + break; + + default: + ret_val = NRF_ERROR_INTERNAL; + break; + } + + return ret_val; +} + + +static ret_code_t cc310_bl_backend_init(void) +{ + uint32_t ret_val; + CRYSError_t crys_error; + + cc310_backend_mutex_init(); + + // Enable the CC310 HW. + NRF_CRYPTOCELL->ENABLE = 1; + + // Initialize the CC310_BL run-time library + crys_error = nrf_cc310_bl_init(); + + // Disable the CC310 HW after initialization. + NRF_CRYPTOCELL->ENABLE = 0; + + ret_val = init_result_get(crys_error); + + return ret_val; +} + + +static ret_code_t cc310_bl_backend_uninit(void) +{ + // Disable the CC310 HW. + NRF_CRYPTOCELL->ENABLE = 0; + + return NRF_SUCCESS; +} + + +CRYPTO_BACKEND_REGISTER(nrf_crypto_backend_info_t const cc310_bl_backend) = +{ + .init_fn = cc310_bl_backend_init, + .uninit_fn = cc310_bl_backend_uninit +}; + +#endif // NRF_MODULE_ENABLED(NRF_CRYPTO) && NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_BL) diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/crypto/backend/cc310_bl/cc310_bl_backend_shared.c b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/crypto/backend/cc310_bl/cc310_bl_backend_shared.c new file mode 100644 index 0000000..520a116 --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/crypto/backend/cc310_bl/cc310_bl_backend_shared.c @@ -0,0 +1,68 @@ +/** + * Copyright (c) 2018 - 2018, Nordic Semiconductor ASA + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form, except as embedded into a Nordic + * Semiconductor ASA integrated circuit in a product or a software update for + * such product, must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * 4. This software, with or without modification, must only be used with a + * Nordic Semiconductor ASA integrated circuit. + * + * 5. Any software provided in binary form under this license must not be reverse + * engineered, decompiled, modified and/or disassembled. + * + * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ +#include "sdk_common.h" +#if NRF_MODULE_ENABLED(NRF_CRYPTO) && NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_BL) + +#include "nrf.h" +#include "cc310_bl_backend_shared.h" +#include "nrf_crypto_error.h" + + +void cc310_bl_backend_enable(void) +{ + // Enable the cryptocell hardware + NRF_CRYPTOCELL->ENABLE = 1; + + // Enable the CryptoCell IRQ + NVIC_EnableIRQ(CRYPTOCELL_IRQn); +} + + +void cc310_bl_backend_disable(void) +{ + // Enable the cryptocell hardware + NRF_CRYPTOCELL->ENABLE = 0; + + // Disable the CryptoCell IRQ + NVIC_DisableIRQ(CRYPTOCELL_IRQn); +} + + +#endif // NRF_MODULE_ENABLED(NRF_CRYPTO) && NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_BL) diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/crypto/backend/cc310_bl/cc310_bl_backend_shared.h b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/crypto/backend/cc310_bl/cc310_bl_backend_shared.h new file mode 100644 index 0000000..db7c933 --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/crypto/backend/cc310_bl/cc310_bl_backend_shared.h @@ -0,0 +1,83 @@ +/** + * Copyright (c) 2018 - 2018, Nordic Semiconductor ASA + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form, except as embedded into a Nordic + * Semiconductor ASA integrated circuit in a product or a software update for + * such product, must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * 4. This software, with or without modification, must only be used with a + * Nordic Semiconductor ASA integrated circuit. + * + * 5. Any software provided in binary form under this license must not be reverse + * engineered, decompiled, modified and/or disassembled. + * + * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef CC310_BL_BACKEND_SHARED_H__ +#define CC310_BL_BACKEND_SHARED_H__ + +/** @file + * + * @defgroup nrf_crypto_cc310_bl_backend_shared nrf_crypto CC310_BL backend shared + * @{ + * @ingroup nrf_crypto_cc310_bl_backend + * + * @brief Shared functionality for the nrf_crypto CC310_BL backend. + */ + +#include "sdk_common.h" + +#if NRF_MODULE_ENABLED(NRF_CRYPTO) && NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_BL) + +#include "sdk_errors.h" +#include "nrf_crypto_hash_shared.h" + +#ifdef __cplusplus +extern "C" { +#endif + + +/**@internal @brief Function to enable CC310 (in HW) + */ +void cc310_bl_backend_enable(void); + + +/**@internal @brief Function to disable CC310 (in HW) + */ +void cc310_bl_backend_disable(void); + + +#ifdef __cplusplus +} +#endif + +#endif // NRF_MODULE_ENABLED(NRF_CRYPTO) && NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_BL) + +/**@} */ + +#endif // CC310_BL_BACKEND_SHARED_H__ |