初始版本

This commit is contained in:
xiaozhengsheng
2025-08-19 09:49:41 +08:00
parent 10f1ddf1c1
commit 6df0f7d96e
2974 changed files with 1712873 additions and 54 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,227 @@
/**
* Copyright (c) 2018 - 2020, 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 MBEDTLS_BACKEND_AES_H__
#define MBEDTLS_BACKEND_AES_H__
/** @file
*
* @defgroup nrf_crypto_mbedtls_backend_aes nrf_crypto mbed TLS backend AES
* @{
* @ingroup nrf_crypto_mbedtls_backend
*
* @brief AES functionality provided by the nrf_crypto mbed TLS backend.
*/
#include "sdk_config.h"
#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS)
/*lint -save -e????*/
#include "mbedtls/aes.h"
#include "mbedtls/cmac.h"
#include "mbedtls/platform.h"
/*lint -restore*/
#include "nrf_crypto_error.h"
#include "nrf_crypto_types.h"
#include "nrf_crypto_aes_shared.h"
#ifdef __cplusplus
extern "C" {
#endif
/* AES CBC */
#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_AES_CBC)
#if NRF_MODULE_ENABLED(NRF_CRYPTO_AES_CBC)
#error "Duplicate definition of AES CBC mode. More than one backend enabled");
#endif
/* Flag that AES CBC is enabled in backend */
#define NRF_CRYPTO_AES_CBC_ENABLED 1
#undef NRF_CRYPTO_AES_ENABLED
#define NRF_CRYPTO_AES_ENABLED 1 // Flag that nrf_crypto_aes frontend can be compiled
#undef NRF_CRYPTO_MBEDTLS_AES_ENABLED
#define NRF_CRYPTO_MBEDTLS_AES_ENABLED 1
/* defines for test purposes */
#define NRF_CRYPTO_AES_CBC_128_ENABLED 1
#define NRF_CRYPTO_AES_CBC_192_ENABLED 1
#define NRF_CRYPTO_AES_CBC_256_ENABLED 1
typedef struct
{
nrf_crypto_aes_internal_context_t header; /**< Common header for context. */
nrf_crypto_backend_aes_ctx_t backend; /**< Backend-specific internal context. */
mbedtls_aes_context context; /**< AES context internal to mbed TLS. */
} nrf_crypto_backend_aes_cbc_context_t;
#endif
/* AES CTR */
#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_AES_CTR)
#if NRF_MODULE_ENABLED(NRF_CRYPTO_AES_CTR)
#error "Duplicate definition of AES CTR mode. More than one backend enabled");
#endif
#define NRF_CRYPTO_AES_CTR_ENABLED 1
#undef NRF_CRYPTO_AES_ENABLED
#define NRF_CRYPTO_AES_ENABLED 1 // Flag that nrf_crypto_aes frontend can be compiled
#undef NRF_CRYPTO_MBEDTLS_AES_ENABLED
#define NRF_CRYPTO_MBEDTLS_AES_ENABLED 1
/* defines for test purposes */
#define NRF_CRYPTO_AES_CTR_128_ENABLED 1
#define NRF_CRYPTO_AES_CTR_192_ENABLED 1
#define NRF_CRYPTO_AES_CTR_256_ENABLED 1
typedef struct
{
nrf_crypto_aes_internal_context_t header; /**< Common header for context. */
nrf_crypto_backend_aes_ctx_t backend; /**< Backend-specific internal context. */
mbedtls_aes_context context; /**< AES context internal to mbed TLS. */
} nrf_crypto_backend_aes_ctr_context_t;
#endif
/* AES CFB */
#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_AES_CFB)
#if NRF_MODULE_ENABLED(NRF_CRYPTO_AES_CFB)
#error "Duplicate definition of AES CFB mode. More than one backend enabled");
#endif
#define NRF_CRYPTO_AES_CFB_ENABLED 1
#undef NRF_CRYPTO_AES_ENABLED
#define NRF_CRYPTO_AES_ENABLED 1 // Flag that nrf_crypto_aes frontend can be compiled
#undef NRF_CRYPTO_MBEDTLS_AES_ENABLED
#define NRF_CRYPTO_MBEDTLS_AES_ENABLED 1
/* defines for test purposes */
#define NRF_CRYPTO_AES_CFB_128_ENABLED 1
#define NRF_CRYPTO_AES_CFB_192_ENABLED 1
#define NRF_CRYPTO_AES_CFB_256_ENABLED 1
typedef struct
{
nrf_crypto_aes_internal_context_t header; /**< Common header for context. */
nrf_crypto_backend_aes_ctx_t backend; /**< Backend-specific internal context. */
mbedtls_aes_context context; /**< AES context internal to mbed TLS. */
} nrf_crypto_backend_aes_cfb_context_t;
#endif
/* AES ECB */
#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_AES_ECB)
#if NRF_MODULE_ENABLED(NRF_CRYPTO_AES_ECB)
#error "Duplicate definition of AES ECB mode. More than one backend enabled");
#endif
#define NRF_CRYPTO_AES_ECB_ENABLED 1
#undef NRF_CRYPTO_AES_ENABLED
#define NRF_CRYPTO_AES_ENABLED 1
#undef NRF_CRYPTO_MBEDTLS_AES_ENABLED
#define NRF_CRYPTO_MBEDTLS_AES_ENABLED 1
/* defines for test purposes */
#define NRF_CRYPTO_AES_ECB_128_ENABLED 1
#define NRF_CRYPTO_AES_ECB_192_ENABLED 1
#define NRF_CRYPTO_AES_ECB_256_ENABLED 1
typedef struct
{
nrf_crypto_aes_internal_context_t header; /**< Common header for context. */
nrf_crypto_backend_no_iv_aes_ctx_t backend; /**< Backend-specific internal context. */
mbedtls_aes_context context; /**< AES context internal to mbed TLS. */
} nrf_crypto_backend_aes_ecb_context_t;
#endif
/* AES CBC MAC */
#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_AES_CBC_MAC)
#if NRF_MODULE_ENABLED(NRF_CRYPTO_AES_CBC_MAC)
#error "Duplicate definition of AES CBC MAC mode. More than one backend enabled");
#endif
/* Flag that AES CBC MAC is enabled in backend */
#define NRF_CRYPTO_AES_CBC_MAC_ENABLED 1
#undef NRF_CRYPTO_AES_ENABLED
#define NRF_CRYPTO_AES_ENABLED 1 // Flag that nrf_crypto_aes frontend can be compiled
#undef NRF_CRYPTO_MBEDTLS_AES_ENABLED
#define NRF_CRYPTO_MBEDTLS_AES_ENABLED 1
/* defines for test purposes */
#define NRF_CRYPTO_AES_CBC_MAC_128_ENABLED 1
#define NRF_CRYPTO_AES_CBC_MAC_192_ENABLED 1
#define NRF_CRYPTO_AES_CBC_MAC_256_ENABLED 1
typedef struct
{
nrf_crypto_aes_internal_context_t header; /**< Common header for context. */
nrf_crypto_backend_aes_ctx_t backend; /**< Backend-specific internal context. */
mbedtls_aes_context context; /**< AES context internal to mbed TLS. */
} nrf_crypto_backend_aes_cbc_mac_context_t;
#endif
/* AES CMAC */
#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_AES_CMAC)
#if NRF_MODULE_ENABLED(NRF_CRYPTO_AES_CMAC)
#error "Duplicate definition of AES CMAC mode. More than one backend enabled");
#endif
#define NRF_CRYPTO_AES_CMAC_ENABLED 1
#undef NRF_CRYPTO_AES_ENABLED
#define NRF_CRYPTO_AES_ENABLED 1 // Flag that nrf_crypto_aes frontend can be compiled
#undef NRF_CRYPTO_MBEDTLS_AES_ENABLED
#define NRF_CRYPTO_MBEDTLS_AES_ENABLED 1
/* defines for test purposes */
#define NRF_CRYPTO_AES_CMAC_128_ENABLED 1
#define NRF_CRYPTO_AES_CMAC_192_ENABLED 1
#define NRF_CRYPTO_AES_CMAC_256_ENABLED 1
typedef struct
{
nrf_crypto_aes_internal_context_t header; /**< Common header for context. */
nrf_crypto_backend_no_iv_aes_ctx_t backend; /**< Backend-specific internal context. */
mbedtls_cipher_context_t context; /**< AES context internal to mbedtls. */
} nrf_crypto_backend_aes_cmac_context_t;
#endif
#ifdef __cplusplus
}
#endif
#endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS)
/** @} */
#endif // MBEDTLS_BACKEND_AES_H__

View File

@@ -0,0 +1,384 @@
/**
* Copyright (c) 2018 - 2020, 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"
#include <drivers/nrfx_common.h>
#if NRF_MODULE_ENABLED(NRF_CRYPTO)
#include "nrf_crypto_error.h"
#include "mbedtls_backend_aes_aead.h"
#if NRF_MODULE_ENABLED(NRF_CRYPTO_MBEDTLS_AES_AEAD)
/**@internal @brief Type declaration of a template suiting all possible context sizes
* for this backend.
*/
typedef union
{
nrf_crypto_aead_internal_context_t header; /**< Common header for context. */
#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_AES_CCM)
nrf_crypto_backend_aes_ccm_context_t ccm;
#endif
#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_AES_GCM)
nrf_crypto_backend_aes_gcm_context_t gcm;
#endif
} nrf_crypto_backend_mbedtls_aes_aead_context_t;
static ret_code_t result_get(int error)
{
ret_code_t ret_val;
switch (error)
{
case 0:
ret_val = NRF_SUCCESS;
break;
case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
ret_val = NRF_ERROR_CRYPTO_INPUT_LENGTH;
break;
case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
ret_val = NRF_ERROR_CRYPTO_ALLOC_FAILED;
break;
case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
ret_val = NRF_ERROR_CRYPTO_FEATURE_UNAVAILABLE;
break;
#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_AES_CCM)
case MBEDTLS_ERR_CCM_BAD_INPUT:
ret_val = NRF_ERROR_CRYPTO_INVALID_PARAM;
break;
case MBEDTLS_ERR_CCM_AUTH_FAILED:
ret_val = NRF_ERROR_CRYPTO_AEAD_INVALID_MAC;
break;
#endif
#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_AES_GCM)
case MBEDTLS_ERR_GCM_BAD_INPUT:
ret_val = NRF_ERROR_CRYPTO_INVALID_PARAM;
break;
case MBEDTLS_ERR_GCM_AUTH_FAILED:
ret_val = NRF_ERROR_CRYPTO_AEAD_INVALID_MAC;
break;
#endif
default:
ret_val = NRF_ERROR_CRYPTO_INTERNAL;
break;
}
return ret_val;
}
static ret_code_t backend_mbedtls_init(void * const p_context, uint8_t * p_key)
{
int result;
ret_code_t ret_val;
nrf_crypto_backend_mbedtls_aes_aead_context_t * p_ctx =
(nrf_crypto_backend_mbedtls_aes_aead_context_t *)p_context;
if ((p_ctx->header.p_info->key_size != NRF_CRYPTO_KEY_SIZE_128) &&
(p_ctx->header.p_info->key_size != NRF_CRYPTO_KEY_SIZE_192) &&
(p_ctx->header.p_info->key_size != NRF_CRYPTO_KEY_SIZE_256))
{
return NRF_ERROR_CRYPTO_KEY_SIZE;
}
switch (p_ctx->header.p_info->mode)
{
#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_AES_CCM)
case NRF_CRYPTO_AEAD_MODE_AES_CCM:
mbedtls_ccm_init(&p_ctx->ccm.context);
result = mbedtls_ccm_setkey(&p_ctx->ccm.context,
MBEDTLS_CIPHER_ID_AES,
p_key,
p_ctx->header.p_info->key_size);
break;
#endif
#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_AES_GCM)
case NRF_CRYPTO_AEAD_MODE_AES_GCM:
mbedtls_gcm_init(&p_ctx->gcm.context);
result = mbedtls_gcm_setkey(&p_ctx->gcm.context,
MBEDTLS_CIPHER_ID_AES,
p_key,
p_ctx->header.p_info->key_size);
break;
#endif
default:
return NRF_ERROR_CRYPTO_FEATURE_UNAVAILABLE;
}
if (result != 0)
{
ret_val = result_get(result);
return ret_val;
}
return NRF_SUCCESS;
}
static ret_code_t backend_mbedtls_uninit(void * const p_context)
{
nrf_crypto_backend_mbedtls_aes_aead_context_t * p_ctx =
(nrf_crypto_backend_mbedtls_aes_aead_context_t *)p_context;
if (p_ctx->header.p_info->mode == NRF_CRYPTO_AEAD_MODE_AES_CCM)
{
#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_AES_CCM)
mbedtls_ccm_free(&p_ctx->ccm.context);
#endif
}
else
{
#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_AES_GCM)
mbedtls_gcm_free(&p_ctx->gcm.context);
#endif
}
return NRF_SUCCESS;
}
#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_AES_CCM)
static ret_code_t backend_mbedtls_ccm_crypt(void * const p_context,
nrf_crypto_operation_t operation,
uint8_t * p_nonce,
uint8_t nonce_size,
uint8_t * p_adata,
size_t adata_size,
uint8_t * p_data_in,
size_t data_in_size,
uint8_t * p_data_out,
uint8_t * p_mac,
uint8_t mac_size)
{
int result;
ret_code_t ret_val;
nrf_crypto_backend_mbedtls_aes_aead_context_t * p_ctx =
(nrf_crypto_backend_mbedtls_aes_aead_context_t *)p_context;
/* CCM mode allows following MAC sizes: [4, 6, 8, 10, 12, 14, 16] */
if ((mac_size < NRF_CRYPTO_AES_CCM_MAC_MIN) || (mac_size > NRF_CRYPTO_AES_CCM_MAC_MAX) ||
((mac_size & 0x01) != 0))
{
return NRF_ERROR_CRYPTO_AEAD_MAC_SIZE;
}
if ((nonce_size < NRF_CRYPTO_AES_CCM_NONCE_SIZE_MIN) ||
(nonce_size > NRF_CRYPTO_AES_CCM_NONCE_SIZE_MAX))
{
return NRF_ERROR_CRYPTO_AEAD_NONCE_SIZE;
}
if (operation == NRF_CRYPTO_ENCRYPT)
{
result = mbedtls_ccm_encrypt_and_tag(&p_ctx->ccm.context,
data_in_size,
p_nonce,
nonce_size,
p_adata,
adata_size,
p_data_in,
p_data_out,
p_mac,
(size_t)mac_size);
}
else if (operation == NRF_CRYPTO_DECRYPT)
{
result = mbedtls_ccm_auth_decrypt(&p_ctx->ccm.context,
data_in_size,
p_nonce,
nonce_size,
p_adata,
adata_size,
p_data_in,
p_data_out,
p_mac,
(size_t)mac_size);
}
else
{
return NRF_ERROR_CRYPTO_INVALID_PARAM;
}
ret_val = result_get(result);
return ret_val;
}
#endif
#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_AES_GCM)
static ret_code_t backend_mbedtls_gcm_crypt(void * const p_context,
nrf_crypto_operation_t operation,
uint8_t * p_nonce,
uint8_t nonce_size,
uint8_t * p_adata,
size_t adata_size,
uint8_t * p_data_in,
size_t data_in_size,
uint8_t * p_data_out,
uint8_t * p_mac,
uint8_t mac_size)
{
int result;
ret_code_t ret_val;
nrf_crypto_backend_mbedtls_aes_aead_context_t * p_ctx =
(nrf_crypto_backend_mbedtls_aes_aead_context_t *)p_context;
/* GCM allows following MAC size: [4 ... 16] */
if ((mac_size < NRF_CRYPTO_AES_GCM_MAC_MIN) || (mac_size > NRF_CRYPTO_AES_GCM_MAC_MAX))
{
return NRF_ERROR_CRYPTO_AEAD_MAC_SIZE;
}
if (operation == NRF_CRYPTO_ENCRYPT)
{
result = mbedtls_gcm_crypt_and_tag(&p_ctx->gcm.context,
MBEDTLS_GCM_ENCRYPT,
data_in_size,
p_nonce,
nonce_size,
p_adata,
adata_size,
p_data_in,
p_data_out,
(size_t)mac_size,
p_mac);
ret_val = result_get(result);
}
else if (operation == NRF_CRYPTO_DECRYPT)
{
result = mbedtls_gcm_auth_decrypt(&p_ctx->gcm.context,
data_in_size,
p_nonce,
nonce_size,
p_adata,
adata_size,
p_mac,
(size_t)mac_size,
p_data_in,
p_data_out);
ret_val = result_get(result);
}
else
{
return NRF_ERROR_CRYPTO_INVALID_PARAM;
}
return ret_val;
}
#endif
#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_AES_CCM)
nrf_crypto_aead_info_t const g_nrf_crypto_aes_ccm_128_info =
{
.key_size = NRF_CRYPTO_KEY_SIZE_128,
.mode = NRF_CRYPTO_AEAD_MODE_AES_CCM,
.init_fn = backend_mbedtls_init,
.uninit_fn = backend_mbedtls_uninit,
.crypt_fn = backend_mbedtls_ccm_crypt
};
nrf_crypto_aead_info_t const g_nrf_crypto_aes_ccm_192_info =
{
.key_size = NRF_CRYPTO_KEY_SIZE_192,
.mode = NRF_CRYPTO_AEAD_MODE_AES_CCM,
.init_fn = backend_mbedtls_init,
.uninit_fn = backend_mbedtls_uninit,
.crypt_fn = backend_mbedtls_ccm_crypt
};
nrf_crypto_aead_info_t const g_nrf_crypto_aes_ccm_256_info =
{
.key_size = NRF_CRYPTO_KEY_SIZE_256,
.mode = NRF_CRYPTO_AEAD_MODE_AES_CCM,
.init_fn = backend_mbedtls_init,
.uninit_fn = backend_mbedtls_uninit,
.crypt_fn = backend_mbedtls_ccm_crypt
};
#endif
#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_AES_GCM)
nrf_crypto_aead_info_t const g_nrf_crypto_aes_gcm_128_info =
{
.key_size = NRF_CRYPTO_KEY_SIZE_128,
.mode = NRF_CRYPTO_AEAD_MODE_AES_GCM,
.init_fn = backend_mbedtls_init,
.uninit_fn = backend_mbedtls_uninit,
.crypt_fn = backend_mbedtls_gcm_crypt
};
nrf_crypto_aead_info_t const g_nrf_crypto_aes_gcm_192_info =
{
.key_size = NRF_CRYPTO_KEY_SIZE_192,
.mode = NRF_CRYPTO_AEAD_MODE_AES_GCM,
.init_fn = backend_mbedtls_init,
.uninit_fn = backend_mbedtls_uninit,
.crypt_fn = backend_mbedtls_gcm_crypt
};
nrf_crypto_aead_info_t const g_nrf_crypto_aes_gcm_256_info =
{
.key_size = NRF_CRYPTO_KEY_SIZE_256,
.mode = NRF_CRYPTO_AEAD_MODE_AES_GCM,
.init_fn = backend_mbedtls_init,
.uninit_fn = backend_mbedtls_uninit,
.crypt_fn = backend_mbedtls_gcm_crypt
};
#endif
#endif // MODULE_ENABLED(NRF_CRYPTO_MBEDTLS_AES_AEAD)
#endif // MODULE_ENABLED(NRF_CRYPTO)

View File

@@ -0,0 +1,123 @@
/**
* Copyright (c) 2018 - 2020, 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 MBEDTLS_BACKEND_AES_AEAD_H__
#define MBEDTLS_BACKEND_AES_AEAD_H__
/** @file
*
* @defgroup nrf_crypto_mbedtls_backend_aes_aead nrf_crypto mbed TLS backend AES AEAD
* @{
* @ingroup nrf_crypto_mbedtls_backend
*
* @brief AES AEAD functionality provided by the nrf_crypto mbed TLS backend.
*/
#include "sdk_config.h"
#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS)
/*lint -save -e????*/
#include "mbedtls/ccm.h"
#include "mbedtls/gcm.h"
#include "mbedtls/platform.h"
/*lint -restore*/
#include "nrf_crypto_error.h"
#include "nrf_crypto_aead_shared.h"
#ifdef __cplusplus
extern "C" {
#endif
/* AES CCM */
#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_AES_CCM)
#if NRF_MODULE_ENABLED(NRF_CRYPTO_AES_CCM)
#error "Duplicate definition of AES CCM mode. More than one backend enabled");
#endif
#define NRF_CRYPTO_AES_CCM_ENABLED 1
#undef NRF_CRYPTO_AEAD_ENABLED
#define NRF_CRYPTO_AEAD_ENABLED 1
#undef NRF_CRYPTO_MBEDTLS_AES_AEAD_ENABLED
#define NRF_CRYPTO_MBEDTLS_AES_AEAD_ENABLED 1
/* defines for test purposes */
#define NRF_CRYPTO_AES_CCM_128_ENABLED 1
#define NRF_CRYPTO_AES_CCM_192_ENABLED 1
#define NRF_CRYPTO_AES_CCM_256_ENABLED 1
typedef struct
{
nrf_crypto_aead_internal_context_t header; /**< Common header for context. */
mbedtls_ccm_context context; /**< AES CCM context internal to mbed TLS. */
} nrf_crypto_backend_aes_ccm_context_t;
#endif
/* AES GCM */
#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_AES_GCM)
#if NRF_MODULE_ENABLED(NRF_CRYPTO_AES_GCM)
#error "Duplicate definition of AES GCM mode. More than one backend enabled");
#endif
#define NRF_CRYPTO_AES_GCM_ENABLED 1
#undef NRF_CRYPTO_AEAD_ENABLED
#define NRF_CRYPTO_AEAD_ENABLED 1
#undef NRF_CRYPTO_MBEDTLS_AES_AEAD_ENABLED
#define NRF_CRYPTO_MBEDTLS_AES_AEAD_ENABLED 1
/* defines for test purposes */
#define NRF_CRYPTO_AES_GCM_128_ENABLED 1
#define NRF_CRYPTO_AES_GCM_192_ENABLED 1
#define NRF_CRYPTO_AES_GCM_256_ENABLED 1
typedef struct
{
nrf_crypto_aead_internal_context_t header; /**< Common header for context. */
mbedtls_gcm_context context; /**< AES GCM context internal to mbed TLS. */
} nrf_crypto_backend_aes_gcm_context_t;
#endif
#ifdef __cplusplus
}
#endif
#endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS)
/** @} */
#endif // MBEDTLS_BACKEND_AES_AEAD_H__

View File

@@ -0,0 +1,585 @@
/**
* Copyright (c) 2018 - 2020, 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_MBEDTLS)
#include <stdbool.h>
#include <string.h>
#include "nrf_crypto_ecc.h"
#include "nrf_crypto_ecdh.h"
#include "nrf_crypto_mem.h"
#include "nrf_crypto_rng.h"
#include "nrf_crypto_shared.h"
#include "nrf_assert.h"
#include "mbedtls_backend_ecc.h"
/*lint -save -e????*/
#if !defined(MBEDTLS_CONFIG_FILE)
#include "mbedtls/config.h"
#else
#include MBEDTLS_CONFIG_FILE
#endif
#include "mbedtls/ecp.h"
#include "mbedtls/bignum.h"
/*lint -restore*/
bool nrf_crypto_backend_mbedtls_ecc_group_load(
mbedtls_ecp_group * p_group,
struct nrf_crypto_ecc_curve_info_s const * p_info)
{
int result;
mbedtls_ecp_group_init(p_group);
result = mbedtls_ecp_group_load(p_group,
(mbedtls_ecp_group_id)(intptr_t)p_info->p_backend_data);
if (result != 0)
{
return false;
}
return true;
}
int nrf_crypto_backend_mbedtls_ecc_mbedtls_rng(void * p_param, unsigned char * p_data, size_t size)
{
#if NRF_MODULE_ENABLED(NRF_CRYPTO_RNG)
ret_code_t result;
result = nrf_crypto_rng_vector_generate(p_data, size);
if (result != NRF_SUCCESS)
{
return MBEDTLS_ERR_ECP_RANDOM_FAILED;
}
return 0;
#else
return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
#endif
}
ret_code_t nrf_crypto_backend_mbedtls_key_pair_generate(
void * p_context,
void * p_private_key,
void * p_public_key)
{
int result;
mbedtls_ecp_group group;
nrf_crypto_backend_mbedtls_ecc_private_key_t * p_prv =
(nrf_crypto_backend_mbedtls_ecc_private_key_t *)p_private_key;
nrf_crypto_backend_mbedtls_ecc_public_key_t * p_pub =
(nrf_crypto_backend_mbedtls_ecc_public_key_t *)p_public_key;
nrf_crypto_ecc_curve_info_t const * p_info = p_pub->header.p_info;
if (!nrf_crypto_backend_mbedtls_ecc_group_load(&group, p_info))
{
return NRF_ERROR_CRYPTO_INTERNAL;
}
mbedtls_ecp_point_init(&p_pub->key);
mbedtls_mpi_init(&p_prv->key);
result = mbedtls_ecp_gen_keypair(&group,
&p_prv->key,
&p_pub->key,
nrf_crypto_backend_mbedtls_ecc_mbedtls_rng,
NULL);
mbedtls_ecp_group_free(&group);
if (result != 0)
{
mbedtls_mpi_free(&p_prv->key);
mbedtls_ecp_point_free(&p_pub->key);
return NRF_ERROR_CRYPTO_INTERNAL;
}
return NRF_SUCCESS;
}
ret_code_t nrf_crypto_backend_mbedtls_public_key_calculate(
void * p_context,
void const * p_private_key,
void * p_public_key)
{
int result;
mbedtls_ecp_group group;
nrf_crypto_backend_mbedtls_ecc_private_key_t const * p_prv =
(nrf_crypto_backend_mbedtls_ecc_private_key_t const *)p_private_key;
nrf_crypto_backend_mbedtls_ecc_public_key_t * p_pub =
(nrf_crypto_backend_mbedtls_ecc_public_key_t *)p_public_key;
nrf_crypto_ecc_curve_info_t const * p_info = p_pub->header.p_info;
if (!nrf_crypto_backend_mbedtls_ecc_group_load(&group, p_info))
{
return NRF_ERROR_CRYPTO_INTERNAL;
}
mbedtls_ecp_point_init(&p_pub->key);
result = mbedtls_ecp_mul(&group,
&p_pub->key,
&p_prv->key,
&group.G,
nrf_crypto_backend_mbedtls_ecc_mbedtls_rng,
NULL);
mbedtls_ecp_group_free(&group);
if (result != 0)
{
mbedtls_ecp_point_free(&p_pub->key);
return NRF_ERROR_CRYPTO_INTERNAL;
}
return NRF_SUCCESS;
}
ret_code_t nrf_crypto_backend_mbedtls_private_key_from_raw(
void * p_private_key,
uint8_t const * p_raw_data)
{
int result;
mbedtls_ecp_group group;
nrf_crypto_backend_mbedtls_ecc_private_key_t * p_prv =
(nrf_crypto_backend_mbedtls_ecc_private_key_t *)p_private_key;
nrf_crypto_ecc_curve_info_t const * p_info = p_prv->header.p_info;
uint8_t const * p_input = p_raw_data;
#if NRF_CRYPTO_ECC_CURVE25519_ENABLED && !NRF_MODULE_ENABLED(NRF_CRYPTO_CURVE25519_BIG_ENDIAN)
uint8_t temp[NRF_CRYPTO_ECC_CURVE25519_RAW_PRIVATE_KEY_SIZE];
if (p_info == &g_nrf_crypto_ecc_curve25519_curve_info)
{
nrf_crypto_internal_swap_endian(temp,
p_raw_data,
NRF_CRYPTO_ECC_CURVE25519_RAW_PRIVATE_KEY_SIZE);
p_input = temp;
}
#endif
if (!nrf_crypto_backend_mbedtls_ecc_group_load(&group, p_info))
{
return NRF_ERROR_CRYPTO_INTERNAL;
}
mbedtls_mpi_init(&p_prv->key);
result = mbedtls_mpi_read_binary(&p_prv->key, p_input, p_info->raw_private_key_size);
if (result == 0)
{
#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_CURVE25519)
// Update bits in Curve25519 private key
if (p_prv->header.p_info->curve_type == NRF_CRYPTO_ECC_CURVE25519_CURVE_TYPE)
{
result = mbedtls_mpi_set_bit(&p_prv->key, 0, 0);
ASSERT(result == 0);
result = mbedtls_mpi_set_bit(&p_prv->key, 1, 0);
ASSERT(result == 0);
result = mbedtls_mpi_set_bit(&p_prv->key, 2, 0);
ASSERT(result == 0);
result = mbedtls_mpi_set_bit(&p_prv->key, 254, 1);
ASSERT(result == 0);
result = mbedtls_mpi_set_bit(&p_prv->key, 255, 0);
ASSERT(result == 0);
}
#endif
if (mbedtls_ecp_check_privkey(&group, &p_prv->key) != 0)
{
result = MBEDTLS_ERR_ECP_INVALID_KEY;
}
}
mbedtls_ecp_group_free(&group);
if (result != 0)
{
mbedtls_mpi_free(&p_prv->key);
return NRF_ERROR_CRYPTO_INTERNAL;
}
return NRF_SUCCESS;
}
ret_code_t nrf_crypto_backend_mbedtls_private_key_to_raw(
void const * p_private_key,
uint8_t * p_raw_data)
{
int result;
nrf_crypto_backend_mbedtls_ecc_private_key_t const * p_prv =
(nrf_crypto_backend_mbedtls_ecc_private_key_t const *)p_private_key;
nrf_crypto_ecc_curve_info_t const * p_info = p_prv->header.p_info;
result = mbedtls_mpi_write_binary(&p_prv->key, p_raw_data, p_info->raw_private_key_size);
#if NRF_CRYPTO_ECC_CURVE25519_ENABLED && !NRF_MODULE_ENABLED(NRF_CRYPTO_CURVE25519_BIG_ENDIAN)
if (p_info == &g_nrf_crypto_ecc_curve25519_curve_info)
{
nrf_crypto_internal_swap_endian_in_place(p_raw_data,
NRF_CRYPTO_ECC_CURVE25519_RAW_PRIVATE_KEY_SIZE);
}
#endif
if (result != 0)
{
return NRF_ERROR_CRYPTO_INTERNAL;
}
return NRF_SUCCESS;
}
ret_code_t nrf_crypto_backend_mbedtls_public_key_from_raw(
void * p_public_key,
uint8_t const * p_raw_data)
{
int result;
mbedtls_ecp_group group;
nrf_crypto_backend_mbedtls_ecc_public_key_t * p_pub =
(nrf_crypto_backend_mbedtls_ecc_public_key_t *)p_public_key;
nrf_crypto_ecc_curve_info_t const * p_info = p_pub->header.p_info;
uint8_t const * p_input = p_raw_data;
#if NRF_CRYPTO_ECC_CURVE25519_ENABLED && !NRF_MODULE_ENABLED(NRF_CRYPTO_CURVE25519_BIG_ENDIAN)
uint8_t temp[NRF_CRYPTO_ECC_CURVE25519_RAW_PUBLIC_KEY_SIZE];
if (p_info == &g_nrf_crypto_ecc_curve25519_curve_info)
{
nrf_crypto_internal_swap_endian(temp,
p_raw_data,
NRF_CRYPTO_ECC_CURVE25519_RAW_PUBLIC_KEY_SIZE);
p_input = temp;
}
#endif
mbedtls_ecp_point_init(&p_pub->key);
result = mbedtls_mpi_read_binary(&p_pub->key.X,
p_input,
p_info->raw_private_key_size);
if (result != 0)
{
goto error_exit;
}
if (p_info->raw_public_key_size > p_info->raw_private_key_size)
{
result = mbedtls_mpi_read_binary(&p_pub->key.Y,
&p_raw_data[p_info->raw_private_key_size],
p_info->raw_private_key_size);
}
if (result != 0)
{
goto error_exit;
}
result = mbedtls_mpi_lset(&p_pub->key.Z, 1);
if (result == 0)
{
if (!nrf_crypto_backend_mbedtls_ecc_group_load(&group, p_info))
{
goto error_exit;
}
result = mbedtls_ecp_check_pubkey(&group, &p_pub->key);
mbedtls_ecp_group_free(&group);
}
if (result != 0)
{
goto error_exit;
}
return NRF_SUCCESS;
error_exit:
mbedtls_ecp_point_free(&p_pub->key);
return NRF_ERROR_CRYPTO_INTERNAL;
}
ret_code_t nrf_crypto_backend_mbedtls_public_key_to_raw(
void const * p_public_key,
uint8_t * p_raw_data)
{
int result;
nrf_crypto_backend_mbedtls_ecc_public_key_t const * p_pub =
(nrf_crypto_backend_mbedtls_ecc_public_key_t const *)p_public_key;
nrf_crypto_ecc_curve_info_t const * p_info = p_pub->header.p_info;
result = mbedtls_mpi_write_binary(&p_pub->key.X,
p_raw_data,
p_info->raw_private_key_size);
if (result != 0)
{
return NRF_ERROR_CRYPTO_INTERNAL;
}
if (p_info->raw_public_key_size > p_info->raw_private_key_size)
{
result = mbedtls_mpi_write_binary(&p_pub->key.Y,
&p_raw_data[p_info->raw_private_key_size],
p_info->raw_private_key_size);
}
#if NRF_CRYPTO_ECC_CURVE25519_ENABLED && !NRF_MODULE_ENABLED(NRF_CRYPTO_CURVE25519_BIG_ENDIAN)
if (p_info == &g_nrf_crypto_ecc_curve25519_curve_info)
{
nrf_crypto_internal_swap_endian_in_place(p_raw_data,
NRF_CRYPTO_ECC_CURVE25519_RAW_PUBLIC_KEY_SIZE);
}
#endif
if (result != 0)
{
return NRF_ERROR_CRYPTO_INTERNAL;
}
return NRF_SUCCESS;
}
ret_code_t nrf_crypto_backend_mbedtls_private_key_free(
void * p_private_key)
{
nrf_crypto_backend_mbedtls_ecc_private_key_t * p_prv =
(nrf_crypto_backend_mbedtls_ecc_private_key_t *)p_private_key;
mbedtls_mpi_free(&p_prv->key);
return NRF_SUCCESS;
}
ret_code_t nrf_crypto_backend_mbedtls_public_key_free(
void * p_public_key)
{
nrf_crypto_backend_mbedtls_ecc_public_key_t * p_pub =
(nrf_crypto_backend_mbedtls_ecc_public_key_t *)p_public_key;
mbedtls_ecp_point_free(&p_pub->key);
return NRF_SUCCESS;
}
#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP192R1)
const nrf_crypto_ecc_curve_info_t g_nrf_crypto_ecc_secp192r1_curve_info =
{
.public_key_size = sizeof(nrf_crypto_backend_mbedtls_ecc_public_key_t),
.private_key_size = sizeof(nrf_crypto_backend_mbedtls_ecc_private_key_t),
.curve_type = NRF_CRYPTO_ECC_SECP192R1_CURVE_TYPE,
.raw_private_key_size = NRF_CRYPTO_ECC_SECP192R1_RAW_PRIVATE_KEY_SIZE,
.raw_public_key_size = NRF_CRYPTO_ECC_SECP192R1_RAW_PUBLIC_KEY_SIZE,
.p_backend_data = (void *)MBEDTLS_ECP_DP_SECP192R1,
};
#endif
#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP224R1)
const nrf_crypto_ecc_curve_info_t g_nrf_crypto_ecc_secp224r1_curve_info =
{
.public_key_size = sizeof(nrf_crypto_backend_mbedtls_ecc_public_key_t),
.private_key_size = sizeof(nrf_crypto_backend_mbedtls_ecc_private_key_t),
.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,
.p_backend_data = (void *)MBEDTLS_ECP_DP_SECP224R1,
};
#endif
#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP256R1)
const nrf_crypto_ecc_curve_info_t g_nrf_crypto_ecc_secp256r1_curve_info =
{
.public_key_size = sizeof(nrf_crypto_backend_mbedtls_ecc_public_key_t),
.private_key_size = sizeof(nrf_crypto_backend_mbedtls_ecc_private_key_t),
.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,
.p_backend_data = (void *)MBEDTLS_ECP_DP_SECP256R1,
};
#endif
#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP384R1)
const nrf_crypto_ecc_curve_info_t g_nrf_crypto_ecc_secp384r1_curve_info =
{
.public_key_size = sizeof(nrf_crypto_backend_mbedtls_ecc_public_key_t),
.private_key_size = sizeof(nrf_crypto_backend_mbedtls_ecc_private_key_t),
.curve_type = NRF_CRYPTO_ECC_SECP384R1_CURVE_TYPE,
.raw_private_key_size = NRF_CRYPTO_ECC_SECP384R1_RAW_PRIVATE_KEY_SIZE,
.raw_public_key_size = NRF_CRYPTO_ECC_SECP384R1_RAW_PUBLIC_KEY_SIZE,
.p_backend_data = (void *)MBEDTLS_ECP_DP_SECP384R1,
};
#endif
#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP521R1)
const nrf_crypto_ecc_curve_info_t g_nrf_crypto_ecc_secp521r1_curve_info =
{
.public_key_size = sizeof(nrf_crypto_backend_mbedtls_ecc_public_key_t),
.private_key_size = sizeof(nrf_crypto_backend_mbedtls_ecc_private_key_t),
.curve_type = NRF_CRYPTO_ECC_SECP521R1_CURVE_TYPE,
.raw_private_key_size = NRF_CRYPTO_ECC_SECP521R1_RAW_PRIVATE_KEY_SIZE,
.raw_public_key_size = NRF_CRYPTO_ECC_SECP521R1_RAW_PUBLIC_KEY_SIZE,
.p_backend_data = (void *)MBEDTLS_ECP_DP_SECP521R1,
};
#endif
#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP192K1)
const nrf_crypto_ecc_curve_info_t g_nrf_crypto_ecc_secp192k1_curve_info =
{
.public_key_size = sizeof(nrf_crypto_backend_mbedtls_ecc_public_key_t),
.private_key_size = sizeof(nrf_crypto_backend_mbedtls_ecc_private_key_t),
.curve_type = NRF_CRYPTO_ECC_SECP192K1_CURVE_TYPE,
.raw_private_key_size = NRF_CRYPTO_ECC_SECP192K1_RAW_PRIVATE_KEY_SIZE,
.raw_public_key_size = NRF_CRYPTO_ECC_SECP192K1_RAW_PUBLIC_KEY_SIZE,
.p_backend_data = (void *)MBEDTLS_ECP_DP_SECP192K1,
};
#endif
#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP224K1)
const nrf_crypto_ecc_curve_info_t g_nrf_crypto_ecc_secp224k1_curve_info =
{
.public_key_size = sizeof(nrf_crypto_backend_mbedtls_ecc_public_key_t),
.private_key_size = sizeof(nrf_crypto_backend_mbedtls_ecc_private_key_t),
.curve_type = NRF_CRYPTO_ECC_SECP224K1_CURVE_TYPE,
.raw_private_key_size = NRF_CRYPTO_ECC_SECP224K1_RAW_PRIVATE_KEY_SIZE,
.raw_public_key_size = NRF_CRYPTO_ECC_SECP224K1_RAW_PUBLIC_KEY_SIZE,
.p_backend_data = (void *)MBEDTLS_ECP_DP_SECP224K1,
};
#endif
#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP256K1)
const nrf_crypto_ecc_curve_info_t g_nrf_crypto_ecc_secp256k1_curve_info =
{
.public_key_size = sizeof(nrf_crypto_backend_mbedtls_ecc_public_key_t),
.private_key_size = sizeof(nrf_crypto_backend_mbedtls_ecc_private_key_t),
.curve_type = NRF_CRYPTO_ECC_SECP256K1_CURVE_TYPE,
.raw_private_key_size = NRF_CRYPTO_ECC_SECP256K1_RAW_PRIVATE_KEY_SIZE,
.raw_public_key_size = NRF_CRYPTO_ECC_SECP256K1_RAW_PUBLIC_KEY_SIZE,
.p_backend_data = (void *)MBEDTLS_ECP_DP_SECP256K1,
};
#endif
#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_BP256R1)
const nrf_crypto_ecc_curve_info_t g_nrf_crypto_ecc_bp256r1_curve_info =
{
.public_key_size = sizeof(nrf_crypto_backend_mbedtls_ecc_public_key_t),
.private_key_size = sizeof(nrf_crypto_backend_mbedtls_ecc_private_key_t),
.curve_type = NRF_CRYPTO_ECC_BP256R1_CURVE_TYPE,
.raw_private_key_size = NRF_CRYPTO_ECC_BP256R1_RAW_PRIVATE_KEY_SIZE,
.raw_public_key_size = NRF_CRYPTO_ECC_BP256R1_RAW_PUBLIC_KEY_SIZE,
.p_backend_data = (void *)MBEDTLS_ECP_DP_BP256R1,
};
#endif
#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_BP384R1)
const nrf_crypto_ecc_curve_info_t g_nrf_crypto_ecc_bp384r1_curve_info =
{
.public_key_size = sizeof(nrf_crypto_backend_mbedtls_ecc_public_key_t),
.private_key_size = sizeof(nrf_crypto_backend_mbedtls_ecc_private_key_t),
.curve_type = NRF_CRYPTO_ECC_BP384R1_CURVE_TYPE,
.raw_private_key_size = NRF_CRYPTO_ECC_BP384R1_RAW_PRIVATE_KEY_SIZE,
.raw_public_key_size = NRF_CRYPTO_ECC_BP384R1_RAW_PUBLIC_KEY_SIZE,
.p_backend_data = (void *)MBEDTLS_ECP_DP_BP384R1,
};
#endif
#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_BP512R1)
const nrf_crypto_ecc_curve_info_t g_nrf_crypto_ecc_bp512r1_curve_info =
{
.public_key_size = sizeof(nrf_crypto_backend_mbedtls_ecc_public_key_t),
.private_key_size = sizeof(nrf_crypto_backend_mbedtls_ecc_private_key_t),
.curve_type = NRF_CRYPTO_ECC_BP512R1_CURVE_TYPE,
.raw_private_key_size = NRF_CRYPTO_ECC_BP512R1_RAW_PRIVATE_KEY_SIZE,
.raw_public_key_size = NRF_CRYPTO_ECC_BP512R1_RAW_PUBLIC_KEY_SIZE,
.p_backend_data = (void *)MBEDTLS_ECP_DP_BP512R1,
};
#endif
#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_CURVE25519)
const nrf_crypto_ecc_curve_info_t g_nrf_crypto_ecc_curve25519_curve_info =
{
.public_key_size = sizeof(nrf_crypto_backend_mbedtls_ecc_public_key_t),
.private_key_size = sizeof(nrf_crypto_backend_mbedtls_ecc_private_key_t),
.curve_type = NRF_CRYPTO_ECC_CURVE25519_CURVE_TYPE,
.raw_private_key_size = NRF_CRYPTO_ECC_CURVE25519_RAW_PRIVATE_KEY_SIZE,
.raw_public_key_size = NRF_CRYPTO_ECC_CURVE25519_RAW_PUBLIC_KEY_SIZE,
.p_backend_data = (void *)MBEDTLS_ECP_DP_CURVE25519,
};
#endif
#endif // NRF_MODULE_ENABLED(NRF_CRYPTO) && NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS)

View File

@@ -0,0 +1,519 @@
/**
* Copyright (c) 2018 - 2020, 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 MBEDTLS_BACKEND_ECC_H__
#define MBEDTLS_BACKEND_ECC_H__
#include "sdk_config.h"
#include "nordic_common.h"
#if NRF_MODULE_ENABLED(NRF_CRYPTO) && NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS)
#include <stdint.h>
#include <stdbool.h>
#include "nrf_crypto_ecc_shared.h"
/*lint -save -e????*/
#if !defined(MBEDTLS_CONFIG_FILE)
#include "mbedtls/config.h"
#else
#include MBEDTLS_CONFIG_FILE
#endif
#include "mbedtls/ecp.h"
/*lint -restore*/
#ifdef __cplusplus
extern "C" {
#endif
/** @internal @brief Common structure holding private key for mbed TLS.
*/
typedef struct
{
nrf_crypto_internal_ecc_key_header_t header; /**< @internal @brief Common ECC key header */
mbedtls_mpi key; /**< @internal @brief mbed TLS specific key representation */
} nrf_crypto_backend_mbedtls_ecc_private_key_t;
/** @internal @brief Common structure holding public key for mbed TLS.
*/
typedef struct
{
nrf_crypto_internal_ecc_key_header_t header; /**< @internal @brief Common ECC key header */
mbedtls_ecp_point key; /**< @internal @brief mbed TLS specific key representation */
} nrf_crypto_backend_mbedtls_ecc_public_key_t;
/** @internal See @ref nrf_crypto_backend_ecc_key_pair_generate_fn_t.
*/
ret_code_t nrf_crypto_backend_mbedtls_key_pair_generate(
void * p_context,
void * p_private_key,
void * p_public_key);
/** @internal See @ref nrf_crypto_backend_ecc_public_key_calculate_fn_t.
*/
ret_code_t nrf_crypto_backend_mbedtls_public_key_calculate(
void * p_context,
void const * p_private_key,
void * p_public_key);
/** @internal See @ref nrf_crypto_backend_ecc_private_key_from_raw_fn_t.
*/
ret_code_t nrf_crypto_backend_mbedtls_private_key_from_raw(
void * p_private_key,
uint8_t const * p_raw_data);
/** @internal See @ref nrf_crypto_backend_ecc_private_key_to_raw_fn_t.
*/
ret_code_t nrf_crypto_backend_mbedtls_private_key_to_raw(
void const * p_private_key,
uint8_t * p_raw_data);
/** @internal See @ref nrf_crypto_backend_ecc_public_key_from_raw_fn_t.
*/
ret_code_t nrf_crypto_backend_mbedtls_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_mbedtls_public_key_to_raw(
void const * p_public_key,
uint8_t * p_raw_data);
/** @internal See @ref nrf_crypto_backend_ecc_key_free_fn_t.
*/
ret_code_t nrf_crypto_backend_mbedtls_private_key_free(
void * p_private_key);
/** @internal See @ref nrf_crypto_backend_ecc_key_free_fn_t.
*/
ret_code_t nrf_crypto_backend_mbedtls_public_key_free(
void * p_public_key);
/** @internal @brief Loads mbed TLS ECC group of specified curve type.
*
* @param[out] p_group Pointer to place where to load a group. Data have to be later deallocated.
* @param[in] curve_type ECC curve type from enum @ref nrf_crypto_ecc_curve_type_t.
* @returns true on success, false if curve is not supported or no found in mbed TLS.
*/
bool nrf_crypto_backend_mbedtls_ecc_group_load(
mbedtls_ecp_group * p_group,
struct nrf_crypto_ecc_curve_info_s const * p_info);
/** @internal @brief Function that can be used as a parameter to mbed TLS functions requiring random
* number generator.
*
* It uses RNG from libary front end to generate random numbers.
*
* @param[in] p_param Opaque pointer passed by mbed TLS. Unused by this implementation.
* @param[out] p_data Pointer where to put random number.
* @returns 0 on success, mbed TLS error code on error.
*/
int nrf_crypto_backend_mbedtls_ecc_mbedtls_rng(void * p_param, unsigned char * p_data, size_t size);
#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP192R1)
#if NRF_MODULE_ENABLED(NRF_CRYPTO_ECC_SECP192R1)
#error "More than one backend enabled for secp192r1 (NIST 192-bit).");
#endif
#define NRF_CRYPTO_ECC_SECP192R1_ENABLED 1
// Aliases for one common MBEDTLS implementation
#define nrf_crypto_backend_secp192r1_key_pair_generate nrf_crypto_backend_mbedtls_key_pair_generate
#define nrf_crypto_backend_secp192r1_public_key_calculate nrf_crypto_backend_mbedtls_public_key_calculate
#define nrf_crypto_backend_secp192r1_private_key_from_raw nrf_crypto_backend_mbedtls_private_key_from_raw
#define nrf_crypto_backend_secp192r1_private_key_to_raw nrf_crypto_backend_mbedtls_private_key_to_raw
#define nrf_crypto_backend_secp192r1_public_key_from_raw nrf_crypto_backend_mbedtls_public_key_from_raw
#define nrf_crypto_backend_secp192r1_public_key_to_raw nrf_crypto_backend_mbedtls_public_key_to_raw
#define nrf_crypto_backend_secp192r1_private_key_free nrf_crypto_backend_mbedtls_private_key_free
#define nrf_crypto_backend_secp192r1_public_key_free nrf_crypto_backend_mbedtls_public_key_free
// mbed TLS does not require context, so its size is 0.
#define NRF_CRYPTO_BACKEND_SECP192R1_KEY_PAIR_GENERATE_CONTEXT_SIZE 0
#define NRF_CRYPTO_BACKEND_SECP192R1_PUBLIC_KEY_CALCULATE_CONTEXT_SIZE 0
// All MBEDTLS curve types share the same data structures
typedef nrf_crypto_backend_mbedtls_ecc_private_key_t nrf_crypto_backend_secp192r1_private_key_t;
typedef nrf_crypto_backend_mbedtls_ecc_public_key_t nrf_crypto_backend_secp192r1_public_key_t;
// Dummy typedef for unused context
typedef uint32_t nrf_crypto_backend_secp192r1_key_pair_generate_context_t;
typedef uint32_t nrf_crypto_backend_secp192r1_public_key_calculate_context_t;
#endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP192R1)
#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_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
// Aliases for one common MBEDTLS implementation
#define nrf_crypto_backend_secp224r1_key_pair_generate nrf_crypto_backend_mbedtls_key_pair_generate
#define nrf_crypto_backend_secp224r1_public_key_calculate nrf_crypto_backend_mbedtls_public_key_calculate
#define nrf_crypto_backend_secp224r1_private_key_from_raw nrf_crypto_backend_mbedtls_private_key_from_raw
#define nrf_crypto_backend_secp224r1_private_key_to_raw nrf_crypto_backend_mbedtls_private_key_to_raw
#define nrf_crypto_backend_secp224r1_public_key_from_raw nrf_crypto_backend_mbedtls_public_key_from_raw
#define nrf_crypto_backend_secp224r1_public_key_to_raw nrf_crypto_backend_mbedtls_public_key_to_raw
#define nrf_crypto_backend_secp224r1_private_key_free nrf_crypto_backend_mbedtls_private_key_free
#define nrf_crypto_backend_secp224r1_public_key_free nrf_crypto_backend_mbedtls_public_key_free
// mbed TLS does not require context, so its size is 0.
#define NRF_CRYPTO_BACKEND_SECP224R1_KEY_PAIR_GENERATE_CONTEXT_SIZE 0
#define NRF_CRYPTO_BACKEND_SECP224R1_PUBLIC_KEY_CALCULATE_CONTEXT_SIZE 0
// All MBEDTLS curve types share the same data structures
typedef nrf_crypto_backend_mbedtls_ecc_private_key_t nrf_crypto_backend_secp224r1_private_key_t;
typedef nrf_crypto_backend_mbedtls_ecc_public_key_t nrf_crypto_backend_secp224r1_public_key_t;
// Dummy typedef for unused context
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_MBEDTLS_ECC_SECP224R1)
#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_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
// Aliases for one common MBEDTLS implementation
#define nrf_crypto_backend_secp256r1_key_pair_generate nrf_crypto_backend_mbedtls_key_pair_generate
#define nrf_crypto_backend_secp256r1_public_key_calculate nrf_crypto_backend_mbedtls_public_key_calculate
#define nrf_crypto_backend_secp256r1_private_key_from_raw nrf_crypto_backend_mbedtls_private_key_from_raw
#define nrf_crypto_backend_secp256r1_private_key_to_raw nrf_crypto_backend_mbedtls_private_key_to_raw
#define nrf_crypto_backend_secp256r1_public_key_from_raw nrf_crypto_backend_mbedtls_public_key_from_raw
#define nrf_crypto_backend_secp256r1_public_key_to_raw nrf_crypto_backend_mbedtls_public_key_to_raw
#define nrf_crypto_backend_secp256r1_private_key_free nrf_crypto_backend_mbedtls_private_key_free
#define nrf_crypto_backend_secp256r1_public_key_free nrf_crypto_backend_mbedtls_public_key_free
// mbed TLS does not require context, so its size is 0.
#define NRF_CRYPTO_BACKEND_SECP256R1_KEY_PAIR_GENERATE_CONTEXT_SIZE 0
#define NRF_CRYPTO_BACKEND_SECP256R1_PUBLIC_KEY_CALCULATE_CONTEXT_SIZE 0
// All MBEDTLS curve types share the same data structures
typedef nrf_crypto_backend_mbedtls_ecc_private_key_t nrf_crypto_backend_secp256r1_private_key_t;
typedef nrf_crypto_backend_mbedtls_ecc_public_key_t nrf_crypto_backend_secp256r1_public_key_t;
// Dummy typedef for unused context
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_MBEDTLS_ECC_SECP256R1)
#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP384R1)
#if NRF_MODULE_ENABLED(NRF_CRYPTO_ECC_SECP384R1)
#error "More than one backend enabled for secp384r1 (NIST 384-bit).");
#endif
#define NRF_CRYPTO_ECC_SECP384R1_ENABLED 1
// Aliases for one common MBEDTLS implementation
#define nrf_crypto_backend_secp384r1_key_pair_generate nrf_crypto_backend_mbedtls_key_pair_generate
#define nrf_crypto_backend_secp384r1_public_key_calculate nrf_crypto_backend_mbedtls_public_key_calculate
#define nrf_crypto_backend_secp384r1_private_key_from_raw nrf_crypto_backend_mbedtls_private_key_from_raw
#define nrf_crypto_backend_secp384r1_private_key_to_raw nrf_crypto_backend_mbedtls_private_key_to_raw
#define nrf_crypto_backend_secp384r1_public_key_from_raw nrf_crypto_backend_mbedtls_public_key_from_raw
#define nrf_crypto_backend_secp384r1_public_key_to_raw nrf_crypto_backend_mbedtls_public_key_to_raw
#define nrf_crypto_backend_secp384r1_private_key_free nrf_crypto_backend_mbedtls_private_key_free
#define nrf_crypto_backend_secp384r1_public_key_free nrf_crypto_backend_mbedtls_public_key_free
// mbed TLS does not require context, so its size is 0.
#define NRF_CRYPTO_BACKEND_SECP384R1_KEY_PAIR_GENERATE_CONTEXT_SIZE 0
#define NRF_CRYPTO_BACKEND_SECP384R1_PUBLIC_KEY_CALCULATE_CONTEXT_SIZE 0
// All MBEDTLS curve types share the same data structures
typedef nrf_crypto_backend_mbedtls_ecc_private_key_t nrf_crypto_backend_secp384r1_private_key_t;
typedef nrf_crypto_backend_mbedtls_ecc_public_key_t nrf_crypto_backend_secp384r1_public_key_t;
// Dummy typedef for unused context
typedef uint32_t nrf_crypto_backend_secp384r1_key_pair_generate_context_t;
typedef uint32_t nrf_crypto_backend_secp384r1_public_key_calculate_context_t;
#endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP384R1)
#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP521R1)
#if NRF_MODULE_ENABLED(NRF_CRYPTO_ECC_SECP521R1)
#error "More than one backend enabled for secp521r1 (NIST 521-bit).");
#endif
#define NRF_CRYPTO_ECC_SECP521R1_ENABLED 1
// Aliases for one common MBEDTLS implementation
#define nrf_crypto_backend_secp521r1_key_pair_generate nrf_crypto_backend_mbedtls_key_pair_generate
#define nrf_crypto_backend_secp521r1_public_key_calculate nrf_crypto_backend_mbedtls_public_key_calculate
#define nrf_crypto_backend_secp521r1_private_key_from_raw nrf_crypto_backend_mbedtls_private_key_from_raw
#define nrf_crypto_backend_secp521r1_private_key_to_raw nrf_crypto_backend_mbedtls_private_key_to_raw
#define nrf_crypto_backend_secp521r1_public_key_from_raw nrf_crypto_backend_mbedtls_public_key_from_raw
#define nrf_crypto_backend_secp521r1_public_key_to_raw nrf_crypto_backend_mbedtls_public_key_to_raw
#define nrf_crypto_backend_secp521r1_private_key_free nrf_crypto_backend_mbedtls_private_key_free
#define nrf_crypto_backend_secp521r1_public_key_free nrf_crypto_backend_mbedtls_public_key_free
// mbed TLS does not require context, so its size is 0.
#define NRF_CRYPTO_BACKEND_SECP521R1_KEY_PAIR_GENERATE_CONTEXT_SIZE 0
#define NRF_CRYPTO_BACKEND_SECP521R1_PUBLIC_KEY_CALCULATE_CONTEXT_SIZE 0
// All MBEDTLS curve types share the same data structures
typedef nrf_crypto_backend_mbedtls_ecc_private_key_t nrf_crypto_backend_secp521r1_private_key_t;
typedef nrf_crypto_backend_mbedtls_ecc_public_key_t nrf_crypto_backend_secp521r1_public_key_t;
// Dummy typedef for unused context
typedef uint32_t nrf_crypto_backend_secp521r1_key_pair_generate_context_t;
typedef uint32_t nrf_crypto_backend_secp521r1_public_key_calculate_context_t;
#endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP521R1)
#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP192K1)
#if NRF_MODULE_ENABLED(NRF_CRYPTO_ECC_SECP192K1)
#error "More than one backend enabled for secp192k1 (Koblitz 192-bit).");
#endif
#define NRF_CRYPTO_ECC_SECP192K1_ENABLED 1
// Aliases for one common MBEDTLS implementation
#define nrf_crypto_backend_secp192k1_key_pair_generate nrf_crypto_backend_mbedtls_key_pair_generate
#define nrf_crypto_backend_secp192k1_public_key_calculate nrf_crypto_backend_mbedtls_public_key_calculate
#define nrf_crypto_backend_secp192k1_private_key_from_raw nrf_crypto_backend_mbedtls_private_key_from_raw
#define nrf_crypto_backend_secp192k1_private_key_to_raw nrf_crypto_backend_mbedtls_private_key_to_raw
#define nrf_crypto_backend_secp192k1_public_key_from_raw nrf_crypto_backend_mbedtls_public_key_from_raw
#define nrf_crypto_backend_secp192k1_public_key_to_raw nrf_crypto_backend_mbedtls_public_key_to_raw
#define nrf_crypto_backend_secp192k1_private_key_free nrf_crypto_backend_mbedtls_private_key_free
#define nrf_crypto_backend_secp192k1_public_key_free nrf_crypto_backend_mbedtls_public_key_free
// mbed TLS does not require context, so its size is 0.
#define NRF_CRYPTO_BACKEND_SECP192K1_KEY_PAIR_GENERATE_CONTEXT_SIZE 0
#define NRF_CRYPTO_BACKEND_SECP192K1_PUBLIC_KEY_CALCULATE_CONTEXT_SIZE 0
// All MBEDTLS curve types share the same data structures
typedef nrf_crypto_backend_mbedtls_ecc_private_key_t nrf_crypto_backend_secp192k1_private_key_t;
typedef nrf_crypto_backend_mbedtls_ecc_public_key_t nrf_crypto_backend_secp192k1_public_key_t;
// Dummy typedef for unused context
typedef uint32_t nrf_crypto_backend_secp192k1_key_pair_generate_context_t;
typedef uint32_t nrf_crypto_backend_secp192k1_public_key_calculate_context_t;
#endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP192K1)
#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP224K1)
#if NRF_MODULE_ENABLED(NRF_CRYPTO_ECC_SECP224K1)
#error "More than one backend enabled for secp224k1 (Koblitz 224-bit).");
#endif
#define NRF_CRYPTO_ECC_SECP224K1_ENABLED 1
// Aliases for one common MBEDTLS implementation
#define nrf_crypto_backend_secp224k1_key_pair_generate nrf_crypto_backend_mbedtls_key_pair_generate
#define nrf_crypto_backend_secp224k1_public_key_calculate nrf_crypto_backend_mbedtls_public_key_calculate
#define nrf_crypto_backend_secp224k1_private_key_from_raw nrf_crypto_backend_mbedtls_private_key_from_raw
#define nrf_crypto_backend_secp224k1_private_key_to_raw nrf_crypto_backend_mbedtls_private_key_to_raw
#define nrf_crypto_backend_secp224k1_public_key_from_raw nrf_crypto_backend_mbedtls_public_key_from_raw
#define nrf_crypto_backend_secp224k1_public_key_to_raw nrf_crypto_backend_mbedtls_public_key_to_raw
#define nrf_crypto_backend_secp224k1_private_key_free nrf_crypto_backend_mbedtls_private_key_free
#define nrf_crypto_backend_secp224k1_public_key_free nrf_crypto_backend_mbedtls_public_key_free
// mbed TLS does not require context, so its size is 0.
#define NRF_CRYPTO_BACKEND_SECP224K1_KEY_PAIR_GENERATE_CONTEXT_SIZE 0
#define NRF_CRYPTO_BACKEND_SECP224K1_PUBLIC_KEY_CALCULATE_CONTEXT_SIZE 0
// All MBEDTLS curve types share the same data structures
typedef nrf_crypto_backend_mbedtls_ecc_private_key_t nrf_crypto_backend_secp224k1_private_key_t;
typedef nrf_crypto_backend_mbedtls_ecc_public_key_t nrf_crypto_backend_secp224k1_public_key_t;
// Dummy typedef for unused context
typedef uint32_t nrf_crypto_backend_secp224k1_key_pair_generate_context_t;
typedef uint32_t nrf_crypto_backend_secp224k1_public_key_calculate_context_t;
#endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP224K1)
#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP256K1)
#if NRF_MODULE_ENABLED(NRF_CRYPTO_ECC_SECP256K1)
#error "More than one backend enabled for secp256k1 (Koblitz 256-bit).");
#endif
#define NRF_CRYPTO_ECC_SECP256K1_ENABLED 1
// Aliases for one common MBEDTLS implementation
#define nrf_crypto_backend_secp256k1_key_pair_generate nrf_crypto_backend_mbedtls_key_pair_generate
#define nrf_crypto_backend_secp256k1_public_key_calculate nrf_crypto_backend_mbedtls_public_key_calculate
#define nrf_crypto_backend_secp256k1_private_key_from_raw nrf_crypto_backend_mbedtls_private_key_from_raw
#define nrf_crypto_backend_secp256k1_private_key_to_raw nrf_crypto_backend_mbedtls_private_key_to_raw
#define nrf_crypto_backend_secp256k1_public_key_from_raw nrf_crypto_backend_mbedtls_public_key_from_raw
#define nrf_crypto_backend_secp256k1_public_key_to_raw nrf_crypto_backend_mbedtls_public_key_to_raw
#define nrf_crypto_backend_secp256k1_private_key_free nrf_crypto_backend_mbedtls_private_key_free
#define nrf_crypto_backend_secp256k1_public_key_free nrf_crypto_backend_mbedtls_public_key_free
// mbed TLS does not require context, so its size is 0.
#define NRF_CRYPTO_BACKEND_SECP256K1_KEY_PAIR_GENERATE_CONTEXT_SIZE 0
#define NRF_CRYPTO_BACKEND_SECP256K1_PUBLIC_KEY_CALCULATE_CONTEXT_SIZE 0
// All MBEDTLS curve types share the same data structures
typedef nrf_crypto_backend_mbedtls_ecc_private_key_t nrf_crypto_backend_secp256k1_private_key_t;
typedef nrf_crypto_backend_mbedtls_ecc_public_key_t nrf_crypto_backend_secp256k1_public_key_t;
// Dummy typedef for unused context
typedef uint32_t nrf_crypto_backend_secp256k1_key_pair_generate_context_t;
typedef uint32_t nrf_crypto_backend_secp256k1_public_key_calculate_context_t;
#endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP256K1)
#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_BP256R1)
#if NRF_MODULE_ENABLED(NRF_CRYPTO_ECC_BP256R1)
#error "More than one backend enabled for bp256r1 (Brainpool 256-bit).");
#endif
#define NRF_CRYPTO_ECC_BP256R1_ENABLED 1
// Aliases for one common MBEDTLS implementation
#define nrf_crypto_backend_bp256r1_key_pair_generate nrf_crypto_backend_mbedtls_key_pair_generate
#define nrf_crypto_backend_bp256r1_public_key_calculate nrf_crypto_backend_mbedtls_public_key_calculate
#define nrf_crypto_backend_bp256r1_private_key_from_raw nrf_crypto_backend_mbedtls_private_key_from_raw
#define nrf_crypto_backend_bp256r1_private_key_to_raw nrf_crypto_backend_mbedtls_private_key_to_raw
#define nrf_crypto_backend_bp256r1_public_key_from_raw nrf_crypto_backend_mbedtls_public_key_from_raw
#define nrf_crypto_backend_bp256r1_public_key_to_raw nrf_crypto_backend_mbedtls_public_key_to_raw
#define nrf_crypto_backend_bp256r1_private_key_free nrf_crypto_backend_mbedtls_private_key_free
#define nrf_crypto_backend_bp256r1_public_key_free nrf_crypto_backend_mbedtls_public_key_free
// mbed TLS does not require context, so its size is 0.
#define NRF_CRYPTO_BACKEND_BP256R1_KEY_PAIR_GENERATE_CONTEXT_SIZE 0
#define NRF_CRYPTO_BACKEND_BP256R1_PUBLIC_KEY_CALCULATE_CONTEXT_SIZE 0
// All MBEDTLS curve types share the same data structures
typedef nrf_crypto_backend_mbedtls_ecc_private_key_t nrf_crypto_backend_bp256r1_private_key_t;
typedef nrf_crypto_backend_mbedtls_ecc_public_key_t nrf_crypto_backend_bp256r1_public_key_t;
// Dummy typedef for unused context
typedef uint32_t nrf_crypto_backend_bp256r1_key_pair_generate_context_t;
typedef uint32_t nrf_crypto_backend_bp256r1_public_key_calculate_context_t;
#endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_BP256R1)
#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_BP384R1)
#if NRF_MODULE_ENABLED(NRF_CRYPTO_ECC_BP384R1)
#error "More than one backend enabled for bp384r1 (Brainpool 384-bit).");
#endif
#define NRF_CRYPTO_ECC_BP384R1_ENABLED 1
// Aliases for one common MBEDTLS implementation
#define nrf_crypto_backend_bp384r1_key_pair_generate nrf_crypto_backend_mbedtls_key_pair_generate
#define nrf_crypto_backend_bp384r1_public_key_calculate nrf_crypto_backend_mbedtls_public_key_calculate
#define nrf_crypto_backend_bp384r1_private_key_from_raw nrf_crypto_backend_mbedtls_private_key_from_raw
#define nrf_crypto_backend_bp384r1_private_key_to_raw nrf_crypto_backend_mbedtls_private_key_to_raw
#define nrf_crypto_backend_bp384r1_public_key_from_raw nrf_crypto_backend_mbedtls_public_key_from_raw
#define nrf_crypto_backend_bp384r1_public_key_to_raw nrf_crypto_backend_mbedtls_public_key_to_raw
#define nrf_crypto_backend_bp384r1_private_key_free nrf_crypto_backend_mbedtls_private_key_free
#define nrf_crypto_backend_bp384r1_public_key_free nrf_crypto_backend_mbedtls_public_key_free
// mbed TLS does not require context, so its size is 0.
#define NRF_CRYPTO_BACKEND_BP384R1_KEY_PAIR_GENERATE_CONTEXT_SIZE 0
#define NRF_CRYPTO_BACKEND_BP384R1_PUBLIC_KEY_CALCULATE_CONTEXT_SIZE 0
// All MBEDTLS curve types share the same data structures
typedef nrf_crypto_backend_mbedtls_ecc_private_key_t nrf_crypto_backend_bp384r1_private_key_t;
typedef nrf_crypto_backend_mbedtls_ecc_public_key_t nrf_crypto_backend_bp384r1_public_key_t;
// Dummy typedef for unused context
typedef uint32_t nrf_crypto_backend_bp384r1_key_pair_generate_context_t;
typedef uint32_t nrf_crypto_backend_bp384r1_public_key_calculate_context_t;
#endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_BP384R1)
#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_BP512R1)
#if NRF_MODULE_ENABLED(NRF_CRYPTO_ECC_BP512R1)
#error "More than one backend enabled for bp512r1 (Brainpool 512-bit).");
#endif
#define NRF_CRYPTO_ECC_BP512R1_ENABLED 1
// Aliases for one common MBEDTLS implementation
#define nrf_crypto_backend_bp512r1_key_pair_generate nrf_crypto_backend_mbedtls_key_pair_generate
#define nrf_crypto_backend_bp512r1_public_key_calculate nrf_crypto_backend_mbedtls_public_key_calculate
#define nrf_crypto_backend_bp512r1_private_key_from_raw nrf_crypto_backend_mbedtls_private_key_from_raw
#define nrf_crypto_backend_bp512r1_private_key_to_raw nrf_crypto_backend_mbedtls_private_key_to_raw
#define nrf_crypto_backend_bp512r1_public_key_from_raw nrf_crypto_backend_mbedtls_public_key_from_raw
#define nrf_crypto_backend_bp512r1_public_key_to_raw nrf_crypto_backend_mbedtls_public_key_to_raw
#define nrf_crypto_backend_bp512r1_private_key_free nrf_crypto_backend_mbedtls_private_key_free
#define nrf_crypto_backend_bp512r1_public_key_free nrf_crypto_backend_mbedtls_public_key_free
// mbed TLS does not require context, so its size is 0.
#define NRF_CRYPTO_BACKEND_BP512R1_KEY_PAIR_GENERATE_CONTEXT_SIZE 0
#define NRF_CRYPTO_BACKEND_BP512R1_PUBLIC_KEY_CALCULATE_CONTEXT_SIZE 0
// All MBEDTLS curve types share the same data structures
typedef nrf_crypto_backend_mbedtls_ecc_private_key_t nrf_crypto_backend_bp512r1_private_key_t;
typedef nrf_crypto_backend_mbedtls_ecc_public_key_t nrf_crypto_backend_bp512r1_public_key_t;
// Dummy typedef for unused context
typedef uint32_t nrf_crypto_backend_bp512r1_key_pair_generate_context_t;
typedef uint32_t nrf_crypto_backend_bp512r1_public_key_calculate_context_t;
#endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_BP512R1)
#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_CURVE25519)
#if NRF_MODULE_ENABLED(NRF_CRYPTO_ECC_CURVE25519)
#error "More than one backend enabled for Curve25519.");
#endif
#define NRF_CRYPTO_ECC_CURVE25519_ENABLED 1
// Aliases for one common MBEDTLS implementation
#define nrf_crypto_backend_curve25519_key_pair_generate nrf_crypto_backend_mbedtls_key_pair_generate
#define nrf_crypto_backend_curve25519_public_key_calculate nrf_crypto_backend_mbedtls_public_key_calculate
#define nrf_crypto_backend_curve25519_private_key_from_raw nrf_crypto_backend_mbedtls_private_key_from_raw
#define nrf_crypto_backend_curve25519_private_key_to_raw nrf_crypto_backend_mbedtls_private_key_to_raw
#define nrf_crypto_backend_curve25519_public_key_from_raw nrf_crypto_backend_mbedtls_public_key_from_raw
#define nrf_crypto_backend_curve25519_public_key_to_raw nrf_crypto_backend_mbedtls_public_key_to_raw
#define nrf_crypto_backend_curve25519_private_key_free nrf_crypto_backend_mbedtls_private_key_free
#define nrf_crypto_backend_curve25519_public_key_free nrf_crypto_backend_mbedtls_public_key_free
// mbed TLS does not require context, so its size is 0.
#define NRF_CRYPTO_BACKEND_CURVE25519_KEY_PAIR_GENERATE_CONTEXT_SIZE 0
#define NRF_CRYPTO_BACKEND_CURVE25519_PUBLIC_KEY_CALCULATE_CONTEXT_SIZE 0
// All MBEDTLS curve types share the same data structures
typedef nrf_crypto_backend_mbedtls_ecc_private_key_t nrf_crypto_backend_curve25519_private_key_t;
typedef nrf_crypto_backend_mbedtls_ecc_public_key_t nrf_crypto_backend_curve25519_public_key_t;
// Dummy typedef for unused context
typedef uint32_t nrf_crypto_backend_curve25519_key_pair_generate_context_t;
typedef uint32_t nrf_crypto_backend_curve25519_public_key_calculate_context_t;
#endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_CURVE25519)
#ifdef __cplusplus
}
#endif
#endif // NRF_MODULE_ENABLED(NRF_CRYPTO) && NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS)
#endif // MBEDTLS_BACKEND_ECC_H__

View File

@@ -0,0 +1,124 @@
/**
* Copyright (c) 2018 - 2020, 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_MBEDTLS)
#include <string.h>
#include "nrf_crypto_ecc_shared.h"
#include "nrf_crypto_ecdh_shared.h"
#include "nrf_crypto_shared.h"
#include "nrf_crypto_ecdh.h"
/*lint -save -e????*/
#if !defined(MBEDTLS_CONFIG_FILE)
#include "mbedtls/config.h"
#else
#include MBEDTLS_CONFIG_FILE
#endif
#include "mbedtls/ecp.h"
#include "mbedtls/ecdh.h"
/*lint -restore*/
ret_code_t nrf_crypto_backend_mbedtls_ecdh_compute(
void * p_context,
void const * p_private_key,
void const * p_public_key,
uint8_t * p_shared_secret)
{
int result;
mbedtls_mpi shared_secret_mpi;
mbedtls_ecp_group group;
nrf_crypto_backend_mbedtls_ecc_private_key_t const * p_prv =
(nrf_crypto_backend_mbedtls_ecc_private_key_t const *)p_private_key;
nrf_crypto_backend_mbedtls_ecc_public_key_t const * p_pub =
(nrf_crypto_backend_mbedtls_ecc_public_key_t const *)p_public_key;
nrf_crypto_ecc_curve_info_t const * p_info = p_prv->header.p_info;
if (!nrf_crypto_backend_mbedtls_ecc_group_load(&group, p_info))
{
return NRF_ERROR_CRYPTO_INTERNAL;
}
mbedtls_mpi_init(&shared_secret_mpi);
result = mbedtls_ecdh_compute_shared(&group,
&shared_secret_mpi,
&p_pub->key,
&p_prv->key,
nrf_crypto_backend_mbedtls_ecc_mbedtls_rng,
NULL);
if (result == 0)
{
result = mbedtls_mpi_write_binary(&shared_secret_mpi,
p_shared_secret,
p_info->raw_private_key_size);
}
#if NRF_CRYPTO_ECC_CURVE25519_ENABLED && !NRF_MODULE_ENABLED(NRF_CRYPTO_CURVE25519_BIG_ENDIAN)
if (p_info == &g_nrf_crypto_ecc_curve25519_curve_info)
{
nrf_crypto_internal_swap_endian_in_place(p_shared_secret,
NRF_CRYPTO_ECDH_CURVE25519_SHARED_SECRET_SIZE);
}
#endif
mbedtls_mpi_free(&shared_secret_mpi);
mbedtls_ecp_group_free(&group);
if (result != 0)
{
return NRF_ERROR_CRYPTO_INTERNAL;
}
return NRF_SUCCESS;
}
#endif // NRF_MODULE_ENABLED(NRF_CRYPTO) && NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS)

View File

@@ -0,0 +1,169 @@
/**
* Copyright (c) 2018 - 2020, 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 MBEDTLS_BACKEND_ECDH_H__
#define MBEDTLS_BACKEND_ECDH_H__
#include "sdk_config.h"
#include "nordic_common.h"
#if NRF_MODULE_ENABLED(NRF_CRYPTO) && NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS)
#include "nrf_crypto_ecc.h"
#include "nrf_crypto_ecdh_shared.h"
#ifdef __cplusplus
extern "C" {
#endif
/** @internal See @ref nrf_crypto_backend_ecdh_compute_fn_t.
*/
ret_code_t nrf_crypto_backend_mbedtls_ecdh_compute(
void * p_context,
void const * p_private_key,
void const * p_public_key,
uint8_t * p_shared_secret);
#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP192R1)
// Aliases for one common MBEDTLS implementation
#define nrf_crypto_backend_secp192r1_ecdh_compute nrf_crypto_backend_mbedtls_ecdh_compute
typedef uint32_t nrf_crypto_backend_secp192r1_ecdh_context_t;
#define NRF_CRYPTO_BACKEND_SECP192R1_ECDH_CONTEXT_SIZE 0
#endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP192R1)
#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP224R1)
// Aliases for one common MBEDTLS implementation
#define nrf_crypto_backend_secp224r1_ecdh_compute nrf_crypto_backend_mbedtls_ecdh_compute
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_MBEDTLS_ECC_SECP224R1)
#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP256R1)
// Aliases for one common MBEDTLS implementation
#define nrf_crypto_backend_secp256r1_ecdh_compute nrf_crypto_backend_mbedtls_ecdh_compute
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_MBEDTLS_ECC_SECP256R1)
#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP384R1)
// Aliases for one common MBEDTLS implementation
#define nrf_crypto_backend_secp384r1_ecdh_compute nrf_crypto_backend_mbedtls_ecdh_compute
typedef uint32_t nrf_crypto_backend_secp384r1_ecdh_context_t;
#define NRF_CRYPTO_BACKEND_SECP384R1_ECDH_CONTEXT_SIZE 0
#endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP384R1)
#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP521R1)
// Aliases for one common MBEDTLS implementation
#define nrf_crypto_backend_secp521r1_ecdh_compute nrf_crypto_backend_mbedtls_ecdh_compute
typedef uint32_t nrf_crypto_backend_secp521r1_ecdh_context_t;
#define NRF_CRYPTO_BACKEND_SECP521R1_ECDH_CONTEXT_SIZE 0
#endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP521R1)
#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP192K1)
// Aliases for one common MBEDTLS implementation
#define nrf_crypto_backend_secp192k1_ecdh_compute nrf_crypto_backend_mbedtls_ecdh_compute
typedef uint32_t nrf_crypto_backend_secp192k1_ecdh_context_t;
#define NRF_CRYPTO_BACKEND_SECP192K1_ECDH_CONTEXT_SIZE 0
#endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP192K1)
#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP224K1)
// Aliases for one common MBEDTLS implementation
#define nrf_crypto_backend_secp224k1_ecdh_compute nrf_crypto_backend_mbedtls_ecdh_compute
typedef uint32_t nrf_crypto_backend_secp224k1_ecdh_context_t;
#define NRF_CRYPTO_BACKEND_SECP224K1_ECDH_CONTEXT_SIZE 0
#endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP224K1)
#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP256K1)
// Aliases for one common MBEDTLS implementation
#define nrf_crypto_backend_secp256k1_ecdh_compute nrf_crypto_backend_mbedtls_ecdh_compute
typedef uint32_t nrf_crypto_backend_secp256k1_ecdh_context_t;
#define NRF_CRYPTO_BACKEND_SECP256K1_ECDH_CONTEXT_SIZE 0
#endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP256K1)
#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_BP256R1)
// Aliases for one common MBEDTLS implementation
#define nrf_crypto_backend_bp256r1_ecdh_compute nrf_crypto_backend_mbedtls_ecdh_compute
typedef uint32_t nrf_crypto_backend_bp256r1_ecdh_context_t;
#define NRF_CRYPTO_BACKEND_BP256R1_ECDH_CONTEXT_SIZE 0
#endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_BP256R1)
#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_BP384R1)
// Aliases for one common MBEDTLS implementation
#define nrf_crypto_backend_bp384r1_ecdh_compute nrf_crypto_backend_mbedtls_ecdh_compute
typedef uint32_t nrf_crypto_backend_bp384r1_ecdh_context_t;
#define NRF_CRYPTO_BACKEND_BP384R1_ECDH_CONTEXT_SIZE 0
#endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_BP384R1)
#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_BP512R1)
// Aliases for one common MBEDTLS implementation
#define nrf_crypto_backend_bp512r1_ecdh_compute nrf_crypto_backend_mbedtls_ecdh_compute
typedef uint32_t nrf_crypto_backend_bp512r1_ecdh_context_t;
#define NRF_CRYPTO_BACKEND_BP512R1_ECDH_CONTEXT_SIZE 0
#endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_BP512R1)
#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_CURVE25519)
// Aliases for one common MBEDTLS implementation
#define nrf_crypto_backend_curve25519_ecdh_compute nrf_crypto_backend_mbedtls_ecdh_compute
typedef uint32_t nrf_crypto_backend_curve25519_ecdh_context_t;
#define NRF_CRYPTO_BACKEND_CURVE25519_ECDH_CONTEXT_SIZE 0
#endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_CURVE25519)
#ifdef __cplusplus
}
#endif
#endif // NRF_MODULE_ENABLED(NRF_CRYPTO) && NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS)
#endif // MBEDTLS_BACKEND_ECDH_H__

View File

@@ -0,0 +1,176 @@
/**
* Copyright (c) 2018 - 2020, 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_MBEDTLS)
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include "nrf_crypto_ecc.h"
#include "nrf_crypto_ecdsa.h"
/*lint -save -e????*/
#if !defined(MBEDTLS_CONFIG_FILE)
#include "mbedtls/config.h"
#else
#include MBEDTLS_CONFIG_FILE
#endif
#include "mbedtls/ecp.h"
#include "mbedtls/ecdsa.h"
#include "mbedtls/sha256.h"
#include "mbedtls/sha512.h"
/*lint -restore*/
ret_code_t nrf_crypto_backend_mbedtls_sign(
void * p_context,
void const * p_private_key,
uint8_t const * p_data,
size_t data_size,
uint8_t * p_signature)
{
int result;
mbedtls_mpi r_mpi;
mbedtls_mpi s_mpi;
mbedtls_ecp_group group;
nrf_crypto_backend_mbedtls_ecc_private_key_t const * p_prv =
(nrf_crypto_backend_mbedtls_ecc_private_key_t const *)p_private_key;
nrf_crypto_ecc_curve_info_t const * p_info = p_prv->header.p_info;
if (!nrf_crypto_backend_mbedtls_ecc_group_load(&group, p_info))
{
return NRF_ERROR_CRYPTO_INTERNAL;
}
mbedtls_mpi_init(&r_mpi);
mbedtls_mpi_init(&s_mpi);
result = mbedtls_ecdsa_sign(&group,
&r_mpi,
&s_mpi,
&p_prv->key,
p_data,
data_size,
nrf_crypto_backend_mbedtls_ecc_mbedtls_rng,
NULL);
mbedtls_ecp_group_free(&group);
if (result == 0)
{
result = mbedtls_mpi_write_binary(&r_mpi, p_signature, p_info->raw_private_key_size);
if (result == 0)
{
result = mbedtls_mpi_write_binary(&s_mpi,
&p_signature[p_info->raw_private_key_size],
p_info->raw_private_key_size);
}
}
mbedtls_mpi_free(&r_mpi);
mbedtls_mpi_free(&s_mpi);
if (result != 0)
{
return NRF_ERROR_CRYPTO_INTERNAL;
}
return NRF_SUCCESS;
}
ret_code_t nrf_crypto_backend_mbedtls_verify(
void * p_context,
void const * p_public_key,
uint8_t const * p_data,
size_t data_size,
uint8_t const * p_signature)
{
int result;
mbedtls_mpi r_mpi;
mbedtls_mpi s_mpi;
mbedtls_ecp_group group;
nrf_crypto_backend_mbedtls_ecc_public_key_t const * p_pub =
(nrf_crypto_backend_mbedtls_ecc_public_key_t const *)p_public_key;
nrf_crypto_ecc_curve_info_t const * p_info = p_pub->header.p_info;
if (!nrf_crypto_backend_mbedtls_ecc_group_load(&group, p_info))
{
return NRF_ERROR_CRYPTO_INTERNAL;
}
mbedtls_mpi_init(&r_mpi);
mbedtls_mpi_init(&s_mpi);
result = mbedtls_mpi_read_binary(&r_mpi, p_signature, p_info->raw_private_key_size);
if (result == 0)
{
result = mbedtls_mpi_read_binary(&s_mpi,
&p_signature[p_info->raw_private_key_size],
p_info->raw_private_key_size);
if (result == 0)
{
result = mbedtls_ecdsa_verify(&group, p_data, data_size, &p_pub->key, &r_mpi, &s_mpi);
}
}
mbedtls_ecp_group_free(&group);
mbedtls_mpi_free(&r_mpi);
mbedtls_mpi_free(&s_mpi);
if (result == MBEDTLS_ERR_ECP_VERIFY_FAILED)
{
return NRF_ERROR_CRYPTO_ECDSA_INVALID_SIGNATURE;
}
else if (result != 0)
{
return NRF_ERROR_CRYPTO_INTERNAL;
}
return NRF_SUCCESS;
}
#endif // NRF_MODULE_ENABLED(NRF_CRYPTO) && NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS)

View File

@@ -0,0 +1,240 @@
/**
* Copyright (c) 2018 - 2020, 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 MBEDTLS_BACKEND_ECDSA_H__
#define MBEDTLS_BACKEND_ECDSA_H__
#include "sdk_config.h"
#include "nordic_common.h"
#if NRF_MODULE_ENABLED(NRF_CRYPTO) && NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS)
#include "nrf_crypto_ecc_shared.h"
#include "nrf_crypto_ecdsa_shared.h"
#ifdef __cplusplus
extern "C" {
#endif
/** @internal See @ref nrf_crypto_backend_ecdsa_sign_fn_t.
*/
ret_code_t nrf_crypto_backend_mbedtls_sign(
void * p_context,
void const * p_private_key,
uint8_t const * p_data,
size_t data_size,
uint8_t * p_signature);
/** @internal See @ref nrf_crypto_backend_ecdsa_verify_fn_t.
*/
ret_code_t nrf_crypto_backend_mbedtls_verify(
void * p_context,
void const * p_public_key,
uint8_t const * p_data,
size_t data_size,
uint8_t const * p_signature);
#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP192R1)
// Context is not used by mbed TLS, so its size is 0
#define NRF_CRYPTO_BACKEND_SECP192R1_SIGN_CONTEXT_SIZE 0
#define NRF_CRYPTO_BACKEND_SECP192R1_VERIFY_CONTEXT_SIZE 0
// Dummy typedefs for unused contexts
typedef uint32_t nrf_crypto_backend_secp192r1_sign_context_t;
typedef uint32_t nrf_crypto_backend_secp192r1_verify_context_t;
// Alias for common mbed TLS
#define nrf_crypto_backend_secp192r1_sign nrf_crypto_backend_mbedtls_sign
#define nrf_crypto_backend_secp192r1_verify nrf_crypto_backend_mbedtls_verify
#endif
#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP224R1)
// Context is not used by mbed TLS, so its size is 0
#define NRF_CRYPTO_BACKEND_SECP224R1_SIGN_CONTEXT_SIZE 0
#define NRF_CRYPTO_BACKEND_SECP224R1_VERIFY_CONTEXT_SIZE 0
// Dummy typedefs for unused contexts
typedef uint32_t nrf_crypto_backend_secp224r1_sign_context_t;
typedef uint32_t nrf_crypto_backend_secp224r1_verify_context_t;
// Alias for common mbed TLS
#define nrf_crypto_backend_secp224r1_sign nrf_crypto_backend_mbedtls_sign
#define nrf_crypto_backend_secp224r1_verify nrf_crypto_backend_mbedtls_verify
#endif
#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP256R1)
// Context is not used by mbed TLS, so its size is 0
#define NRF_CRYPTO_BACKEND_SECP256R1_SIGN_CONTEXT_SIZE 0
#define NRF_CRYPTO_BACKEND_SECP256R1_VERIFY_CONTEXT_SIZE 0
// Dummy typedefs for unused contexts
typedef uint32_t nrf_crypto_backend_secp256r1_sign_context_t;
typedef uint32_t nrf_crypto_backend_secp256r1_verify_context_t;
// Alias for common mbed TLS
#define nrf_crypto_backend_secp256r1_sign nrf_crypto_backend_mbedtls_sign
#define nrf_crypto_backend_secp256r1_verify nrf_crypto_backend_mbedtls_verify
#endif
#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP384R1)
// Context is not used by mbed TLS, so its size is 0
#define NRF_CRYPTO_BACKEND_SECP384R1_SIGN_CONTEXT_SIZE 0
#define NRF_CRYPTO_BACKEND_SECP384R1_VERIFY_CONTEXT_SIZE 0
// Dummy typedefs for unused contexts
typedef uint32_t nrf_crypto_backend_secp384r1_sign_context_t;
typedef uint32_t nrf_crypto_backend_secp384r1_verify_context_t;
// Alias for common mbed TLS
#define nrf_crypto_backend_secp384r1_sign nrf_crypto_backend_mbedtls_sign
#define nrf_crypto_backend_secp384r1_verify nrf_crypto_backend_mbedtls_verify
#endif
#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP521R1)
// Context is not used by mbed TLS, so its size is 0
#define NRF_CRYPTO_BACKEND_SECP521R1_SIGN_CONTEXT_SIZE 0
#define NRF_CRYPTO_BACKEND_SECP521R1_VERIFY_CONTEXT_SIZE 0
// Dummy typedefs for unused contexts
typedef uint32_t nrf_crypto_backend_secp521r1_sign_context_t;
typedef uint32_t nrf_crypto_backend_secp521r1_verify_context_t;
// Alias for common mbed TLS
#define nrf_crypto_backend_secp521r1_sign nrf_crypto_backend_mbedtls_sign
#define nrf_crypto_backend_secp521r1_verify nrf_crypto_backend_mbedtls_verify
#endif
#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP192K1)
// Context is not used by mbed TLS, so its size is 0
#define NRF_CRYPTO_BACKEND_SECP192K1_SIGN_CONTEXT_SIZE 0
#define NRF_CRYPTO_BACKEND_SECP192K1_VERIFY_CONTEXT_SIZE 0
// Dummy typedefs for unused contexts
typedef uint32_t nrf_crypto_backend_secp192k1_sign_context_t;
typedef uint32_t nrf_crypto_backend_secp192k1_verify_context_t;
// Alias for common mbed TLS
#define nrf_crypto_backend_secp192k1_sign nrf_crypto_backend_mbedtls_sign
#define nrf_crypto_backend_secp192k1_verify nrf_crypto_backend_mbedtls_verify
#endif
#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP224K1)
// Context is not used by mbed TLS, so its size is 0
#define NRF_CRYPTO_BACKEND_SECP224K1_SIGN_CONTEXT_SIZE 0
#define NRF_CRYPTO_BACKEND_SECP224K1_VERIFY_CONTEXT_SIZE 0
// Dummy typedefs for unused contexts
typedef uint32_t nrf_crypto_backend_secp224k1_sign_context_t;
typedef uint32_t nrf_crypto_backend_secp224k1_verify_context_t;
// Alias for common mbed TLS
#define nrf_crypto_backend_secp224k1_sign nrf_crypto_backend_mbedtls_sign
#define nrf_crypto_backend_secp224k1_verify nrf_crypto_backend_mbedtls_verify
#endif
#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP256K1)
// Context is not used by mbed TLS, so its size is 0
#define NRF_CRYPTO_BACKEND_SECP256K1_SIGN_CONTEXT_SIZE 0
#define NRF_CRYPTO_BACKEND_SECP256K1_VERIFY_CONTEXT_SIZE 0
// Dummy typedefs for unused contexts
typedef uint32_t nrf_crypto_backend_secp256k1_sign_context_t;
typedef uint32_t nrf_crypto_backend_secp256k1_verify_context_t;
// Alias for common mbed TLS
#define nrf_crypto_backend_secp256k1_sign nrf_crypto_backend_mbedtls_sign
#define nrf_crypto_backend_secp256k1_verify nrf_crypto_backend_mbedtls_verify
#endif
#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_BP256R1)
// Context is not used by mbed TLS, so its size is 0
#define NRF_CRYPTO_BACKEND_BP256R1_SIGN_CONTEXT_SIZE 0
#define NRF_CRYPTO_BACKEND_BP256R1_VERIFY_CONTEXT_SIZE 0
// Dummy typedefs for unused contexts
typedef uint32_t nrf_crypto_backend_bp256r1_sign_context_t;
typedef uint32_t nrf_crypto_backend_bp256r1_verify_context_t;
// Alias for common mbed TLS
#define nrf_crypto_backend_bp256r1_sign nrf_crypto_backend_mbedtls_sign
#define nrf_crypto_backend_bp256r1_verify nrf_crypto_backend_mbedtls_verify
#endif
#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_BP384R1)
// Context is not used by mbed TLS, so its size is 0
#define NRF_CRYPTO_BACKEND_BP384R1_SIGN_CONTEXT_SIZE 0
#define NRF_CRYPTO_BACKEND_BP384R1_VERIFY_CONTEXT_SIZE 0
// Dummy typedefs for unused contexts
typedef uint32_t nrf_crypto_backend_bp384r1_sign_context_t;
typedef uint32_t nrf_crypto_backend_bp384r1_verify_context_t;
// Alias for common mbed TLS
#define nrf_crypto_backend_bp384r1_sign nrf_crypto_backend_mbedtls_sign
#define nrf_crypto_backend_bp384r1_verify nrf_crypto_backend_mbedtls_verify
#endif
#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_BP512R1)
// Context is not used by mbed TLS, so its size is 0
#define NRF_CRYPTO_BACKEND_BP512R1_SIGN_CONTEXT_SIZE 0
#define NRF_CRYPTO_BACKEND_BP512R1_VERIFY_CONTEXT_SIZE 0
// Dummy typedefs for unused contexts
typedef uint32_t nrf_crypto_backend_bp512r1_sign_context_t;
typedef uint32_t nrf_crypto_backend_bp512r1_verify_context_t;
// Alias for common mbed TLS
#define nrf_crypto_backend_bp512r1_sign nrf_crypto_backend_mbedtls_sign
#define nrf_crypto_backend_bp512r1_verify nrf_crypto_backend_mbedtls_verify
#endif
#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_CURVE25519)
// Context is not used by mbed TLS, so its size is 0
#define NRF_CRYPTO_BACKEND_CURVE25519_SIGN_CONTEXT_SIZE 0
#define NRF_CRYPTO_BACKEND_CURVE25519_VERIFY_CONTEXT_SIZE 0
// Dummy typedefs for unused contexts
typedef uint32_t nrf_crypto_backend_curve25519_sign_context_t;
typedef uint32_t nrf_crypto_backend_curve25519_verify_context_t;
// No ECDSA implementation for Curve25519
#define nrf_crypto_backend_curve25519_sign NULL
#define nrf_crypto_backend_curve25519_verify NULL
#endif
#ifdef __cplusplus
}
#endif
#endif // NRF_MODULE_ENABLED(NRF_CRYPTO) && NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS)
#endif // MBEDTLS_BACKEND_ECDSA_H__

View File

@@ -0,0 +1,196 @@
/**
* Copyright (c) 2018 - 2020, 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_MBEDTLS)
#include "mbedtls_backend_hash.h"
#include "nrf_crypto_init.h"
#include "nrf_crypto_types.h"
#include "nrf_crypto_error.h"
#include "nrf_crypto_hash_shared.h"
#include "sdk_macros.h"
#include "nrf_log.h"
#include "nrf_assert.h"
/*lint -save -e????*/
#if !defined(MBEDTLS_CONFIG_FILE)
#include "mbedtls/config.h"
#else
#include MBEDTLS_CONFIG_FILE
#endif
#include "mbedtls/md.h"
#include "mbedtls/sha256.h"
#include "mbedtls/sha512.h"
/*lint -restore*/
#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_HASH_SHA256)
static ret_code_t mbedtls_backend_hash_sha256_init(void * const p_context)
{
// No parameter testing on this level.
// This has been done on upper level.
mbedtls_sha256_context * p_backend_context
= &(((nrf_crypto_backend_hash_sha256_context_t *)p_context)->context);
mbedtls_sha256_init(p_backend_context);
mbedtls_sha256_starts(p_backend_context, 0);
return NRF_SUCCESS;
}
static uint32_t mbedtls_backend_hash_sha256_update(void * const p_context,
uint8_t const * p_data,
size_t size)
{
// Limited parameter testing on this level.
// This has been done on upper level.
mbedtls_sha256_context * p_backend_context
= &(((nrf_crypto_backend_hash_sha256_context_t *)p_context)->context);
mbedtls_sha256_update(p_backend_context, p_data, size);
return NRF_SUCCESS;
}
static uint32_t mbedtls_backend_hash_sha256_finalize(void * const p_context,
uint8_t * p_digest,
size_t * const p_digest_size)
{
// Limited parameter testing on this level.
// This has been done on upper level.
mbedtls_sha256_context * p_backend_context
= &(((nrf_crypto_backend_hash_sha256_context_t *)p_context)->context);
mbedtls_sha256_finish(p_backend_context, p_digest);
*p_digest_size = NRF_CRYPTO_HASH_SIZE_SHA256;
return NRF_SUCCESS;
}
const nrf_crypto_hash_info_t g_nrf_crypto_hash_sha256_info =
{
.init_fn = mbedtls_backend_hash_sha256_init,
.update_fn = mbedtls_backend_hash_sha256_update,
.finalize_fn = mbedtls_backend_hash_sha256_finalize,
.digest_size = NRF_CRYPTO_HASH_SIZE_SHA256,
.context_size = sizeof(nrf_crypto_backend_hash_sha256_context_t),
.hash_mode = NRF_CRYPTO_HASH_MODE_SHA256
};
#endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_HASH_SHA256)
#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_HASH_SHA512)
static ret_code_t mbedtls_backend_hash_sha512_init(void * p_context)
{
// No parameter testing on this level.
// This has been done on upper level.
mbedtls_sha512_context * p_backend_context
= &(((nrf_crypto_backend_hash_sha512_context_t *)p_context)->context);
mbedtls_sha512_init(p_backend_context);
mbedtls_sha512_starts(p_backend_context, 0);
return NRF_SUCCESS;
}
static ret_code_t mbedtls_backend_hash_sha512_update(void * const p_context,
uint8_t const * p_data,
size_t size)
{
// Limited parameter testing on this level.
// This has been done on upper level.
mbedtls_sha512_context * p_backend_context
= &(((nrf_crypto_backend_hash_sha512_context_t *)p_context)->context);
mbedtls_sha512_update(p_backend_context, p_data, size);
return NRF_SUCCESS;
}
static ret_code_t mbedtls_backend_hash_sha512_finalize(void * const p_context,
uint8_t * p_digest,
size_t * const p_digest_size)
{
// Limited parameter testing on this level.
// This has been done on upper level.
mbedtls_sha512_context * p_backend_context
= &(((nrf_crypto_backend_hash_sha512_context_t *)p_context)->context);
mbedtls_sha512_finish(p_backend_context, p_digest);
*p_digest_size = NRF_CRYPTO_HASH_SIZE_SHA512;
return NRF_SUCCESS;
}
const nrf_crypto_hash_info_t g_nrf_crypto_hash_sha512_info =
{
.init_fn = mbedtls_backend_hash_sha512_init,
.update_fn = mbedtls_backend_hash_sha512_update,
.finalize_fn = mbedtls_backend_hash_sha512_finalize,
.digest_size = NRF_CRYPTO_HASH_SIZE_SHA512,
.context_size = sizeof(nrf_crypto_backend_hash_sha512_context_t),
.hash_mode = NRF_CRYPTO_HASH_MODE_SHA512
};
#endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_HASH_SHA512)
#endif // NRF_MODULE_ENABLED(NRF_CRYPTO) && NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS)

View File

@@ -0,0 +1,128 @@
/**
* Copyright (c) 2018 - 2020, 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 MBEDTLS_BACKEND_HASH_H__
#define MBEDTLS_BACKEND_HASH_H__
/** @file
*
* @defgroup nrf_crypto_mbedtls_backend_hash nrf_crypto mbedtls backend hash
* @{
* @ingroup nrf_crypto_mbedtls_backend
*
* @brief Hash functionality provided by the nrf_crypto mbedtls backend.
*/
#include "sdk_common.h"
#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS)
#include "sdk_errors.h"
#include "nrf_crypto_hash_shared.h"
/*lint -save -e????*/
#include "mbedtls/sha256.h"
#include "mbedtls/sha512.h"
/*lint -restore*/
#ifdef __cplusplus
extern "C" {
#endif
#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_HASH_SHA256)
// Flag that nrf_crypto_hash frontend can be compiled
#undef NRF_CRYPTO_HASH_ENABLED
#define NRF_CRYPTO_HASH_ENABLED 1
// Duplicate backend enabled test for SHA-256
#if NRF_MODULE_ENABLED(NRF_CRYPTO_HASH_SHA256)
#error "Duplicate definition of SHA-256. More than one backend enabled");
#endif
// Flag that SHA-256 is enabled in backend
#define NRF_CRYPTO_HASH_SHA256_ENABLED 1
/**brief nrf_crypto_hash context for SHA-256 in nrf_crypto mbedtls backend. */
typedef struct
{
nrf_crypto_hash_internal_context_t header; /**< Common header for context. */
mbedtls_sha256_context context; /**< Hash context internal to mbedtls. */
} nrf_crypto_backend_hash_sha256_context_t;
#endif // NRF_CRYPTO_BACKEND_MBEDTLS_HASH_SHA256_ENABLED
#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_HASH_SHA512)
// Flag that nrf_crypto_hash frontend can be compiled
#undef NRF_CRYPTO_HASH_ENABLED
#define NRF_CRYPTO_HASH_ENABLED 1
// Duplicate backend enabled test for SHA-512
#if NRF_MODULE_ENABLED(NRF_CRYPTO_HASH_SHA512)
#error "Duplicate definition of SHA-512. More than one backend enabled");
#endif
// Flag that SHA-512 is enabled in backend
#define NRF_CRYPTO_HASH_SHA512_ENABLED 1
/**brief nrf_crypto_hash context for SHA-512 in nrf_crypto mbedtls backend. */
typedef struct
{
nrf_crypto_hash_internal_context_t header; /**< Common header for context. */
mbedtls_sha512_context context; /**< Hash context internal to mbedtls. */
} nrf_crypto_backend_hash_sha512_context_t;
#endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_HASH_SHA512)
#ifdef __cplusplus
}
#endif
#endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS)
/**@} */
#endif //MBEDTLS_BACKEND_HASH_H__

View File

@@ -0,0 +1,230 @@
/**
* Copyright (c) 2018 - 2020, 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_MBEDTLS)
#include "nrf_log.h"
#include "nrf_crypto_error.h"
#include "nrf_crypto_types.h"
#include "mbedtls_backend_hmac.h"
#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_HMAC_SHA256)
static ret_code_t mbedtls_backend_hmac_init_sha256(void * const p_context,
uint8_t const * p_key,
size_t key_size)
{
int err_code;
nrf_crypto_backend_mbedtls_hmac_sha256_context_t * p_ctx =
(nrf_crypto_backend_mbedtls_hmac_sha256_context_t *)p_context;
// Memset context to 0. This is equevalend with a call to mbedtls_md_init().
memset(p_ctx->md_ctx_buffer, 0, sizeof(p_ctx->md_ctx_buffer));
memset(p_ctx->hmac_ctx_buffer, 0, sizeof(p_ctx->hmac_ctx_buffer));
// Set info and context pointers to buffer allocated by user.
// This is Normally handled by mbedtls_md_setup(), but has to be done here in order
// to avoid dynamic allocation of memory inside mbed TLS.
p_ctx->mbedtls_ctx.md_info = mbedtls_md_info_from_type(MBEDTLS_MD_SHA256);
p_ctx->mbedtls_ctx.md_ctx = p_ctx->md_ctx_buffer;
p_ctx->mbedtls_ctx.hmac_ctx = p_ctx->hmac_ctx_buffer;
// Enter key to start
err_code = mbedtls_md_hmac_starts(&p_ctx->mbedtls_ctx,
p_key,
key_size);
if (err_code != 0)
{
NRF_LOG_ERROR("Error in mbedtls_md_hmac_starts: %u", err_code);
return NRF_ERROR_CRYPTO_INTERNAL;
}
return NRF_SUCCESS;
}
static ret_code_t mbedtls_backend_hmac_update_sha256(void * const p_context,
uint8_t const * p_data,
size_t size)
{
int err_code;
nrf_crypto_backend_mbedtls_hmac_sha256_context_t * p_ctx =
(nrf_crypto_backend_mbedtls_hmac_sha256_context_t *)p_context;
err_code = mbedtls_md_hmac_update(&p_ctx->mbedtls_ctx, p_data, size);
if (err_code != 0)
{
NRF_LOG_ERROR("Error in mbedtls_md_hmac_update: %u", err_code);
return NRF_ERROR_CRYPTO_INTERNAL;
}
return NRF_SUCCESS;
}
static ret_code_t mbedtls_backend_hmac_finalize_sha256(void * const p_context,
uint8_t * p_digest,
size_t * const p_size)
{
int err_code;
nrf_crypto_backend_mbedtls_hmac_sha256_context_t * const p_ctx =
(nrf_crypto_backend_mbedtls_hmac_sha256_context_t *)p_context;
// Set the digest length to 0 so that this is used in case of any error.
*p_size = 0;
err_code = mbedtls_md_hmac_finish(&p_ctx->mbedtls_ctx, p_digest);
if (err_code != 0)
{
NRF_LOG_ERROR("Error in mbedtls_md_hmac_finish: %u", err_code);
return NRF_ERROR_CRYPTO_INTERNAL;
}
*p_size = p_ctx->header.p_info->digest_size;
return NRF_SUCCESS;
}
// Information structure for HMAC SHA256 using mbed TLS backend.
const nrf_crypto_hmac_info_t g_nrf_crypto_hmac_sha256_info =
{
.init_fn = mbedtls_backend_hmac_init_sha256,
.update_fn = mbedtls_backend_hmac_update_sha256,
.finalize_fn = mbedtls_backend_hmac_finalize_sha256,
.digest_size = NRF_CRYPTO_HASH_SIZE_SHA256,
.context_size = sizeof(nrf_crypto_backend_hmac_sha256_context_t),
.type = NRF_CRYPTO_HMAC_SHA256_TYPE
};
#endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_HMAC_SHA256)
#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_HMAC_SHA512)
static ret_code_t mbedtls_backend_hmac_init_sha512(void * const p_context,
uint8_t const * p_key,
size_t key_size)
{
int err_code;
nrf_crypto_backend_mbedtls_hmac_sha512_context_t * p_ctx =
(nrf_crypto_backend_mbedtls_hmac_sha512_context_t *)p_context;
// Memset context to 0. This is equevalend with a call to mbedtls_md_init().
memset(p_ctx->md_ctx_buffer, 0, sizeof(p_ctx->md_ctx_buffer));
memset(p_ctx->hmac_ctx_buffer, 0, sizeof(p_ctx->hmac_ctx_buffer));
// Set info and context pointers to buffer allocated by user.
// (Normally handled by mbedtls_md_setup())
p_ctx->mbedtls_ctx.md_info = mbedtls_md_info_from_type(MBEDTLS_MD_SHA512);
p_ctx->mbedtls_ctx.md_ctx = p_ctx->md_ctx_buffer;
p_ctx->mbedtls_ctx.hmac_ctx = p_ctx->hmac_ctx_buffer;
// Enter key to start
err_code = mbedtls_md_hmac_starts(&p_ctx->mbedtls_ctx, p_key, key_size);
if (err_code != 0)
{
NRF_LOG_ERROR("Error in mbedtls_md_hmac_starts: %u", err_code);
return NRF_ERROR_CRYPTO_INTERNAL;
}
return NRF_SUCCESS;
}
static ret_code_t mbedtls_backend_hmac_update_sha512(void * const p_context,
uint8_t const * p_data,
size_t size)
{
int err_code;
nrf_crypto_backend_mbedtls_hmac_sha512_context_t * p_ctx =
(nrf_crypto_backend_mbedtls_hmac_sha512_context_t *)p_context;
err_code = mbedtls_md_hmac_update(&p_ctx->mbedtls_ctx, p_data, size);
if (err_code != 0)
{
NRF_LOG_ERROR("Error in mbedtls_md_hmac_update: %u", err_code);
return NRF_ERROR_CRYPTO_INTERNAL;
}
return NRF_SUCCESS;
}
static ret_code_t mbedtls_backend_hmac_finalize_sha512(void * const p_context,
uint8_t * p_digest,
size_t * const p_size)
{
int err_code;
nrf_crypto_backend_mbedtls_hmac_sha512_context_t * p_ctx =
(nrf_crypto_backend_mbedtls_hmac_sha512_context_t *)p_context;
// Set the digest length to 0 so that this is used in case of any error.
*p_size = 0;
err_code = mbedtls_md_hmac_finish(&p_ctx->mbedtls_ctx, p_digest);
if (err_code != 0)
{
NRF_LOG_ERROR("Error in mbedtls_md_hmac_finish: %u", err_code);
return NRF_ERROR_CRYPTO_INTERNAL; }
*p_size = p_ctx->header.p_info->digest_size;
return NRF_SUCCESS;
}
// Information structure for HMAC SHA512 using mbed TLS backend.
const nrf_crypto_hmac_info_t g_nrf_crypto_hmac_sha512_info =
{
.init_fn = mbedtls_backend_hmac_init_sha512,
.update_fn = mbedtls_backend_hmac_update_sha512,
.finalize_fn = mbedtls_backend_hmac_finalize_sha512,
.digest_size = NRF_CRYPTO_HASH_SIZE_SHA512,
.context_size = sizeof(nrf_crypto_backend_hmac_sha512_context_t),
.type = NRF_CRYPTO_HMAC_SHA512_TYPE
};
#endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_HMAC_SHA512)
#endif // NRF_MODULE_ENABLED(NRF_CRYPTO) && NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS)

View File

@@ -0,0 +1,140 @@
/**
* Copyright (c) 2018 - 2020, 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 MBEDTLS_BACKEND_HMAC_H__
#define MBEDTLS_BACKEND_HMAC_H__
/** @file
*
* @defgroup nrf_crypto_mbedtls_backend_hmac mbed TLS backend for HMAC
* @{
* @ingroup nrf_crypto_mbedtls_backend
*
* @brief Backend wrapper for mbed TLS. None of these types should be used directly by the
* application.
*/
#include "sdk_common.h"
#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS) && \
( NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_HMAC_SHA256) || \
NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_HMAC_SHA512) )
#include "nrf_crypto_hmac_shared.h"
/*lint -save -e????*/
#include "mbedtls/md.h"
#include "mbedtls/sha256.h"
#include "mbedtls/sha512.h"
/*lint -restore*/
#ifdef __cplusplus
extern "C" {
#endif
#undef NRF_CRYPTO_HMAC_ENABLED
#define NRF_CRYPTO_HMAC_ENABLED 1
#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_HMAC_SHA256)
#if NRF_MODULE_ENABLED(NRF_CRYPTO_HMAC_SHA256)
#error "Duplicate definition of HMAC SHA-256. More than one backend enabled"
#endif // NRF_CRYPTO_HMAC_SHA256_ENABLED
#define NRF_CRYPTO_HMAC_SHA256_ENABLED 1
/**
* @internal @brief Internal context object used by the mbed TLS backend wrapper for HMAC SHA256.
*
* @note This should never be used directly. Use @ref nrf_crypto_backend_hmac_sha256_context_t
* instead.
*/
typedef struct
{
nrf_crypto_hmac_internal_context_t header; //!< Internal nrf_crypto_hmac context.
mbedtls_md_context_t mbedtls_ctx; //!< Mbed TLS context object.
uint8_t md_ctx_buffer[sizeof(mbedtls_sha256_context)]; //!< Message digest buffer for mbed TLS.
uint16_t hmac_ctx_buffer[64]; //!< Hash buffer for mbed TLS of size defined in mbedtls_sha256_info in md_internal.h.
} nrf_crypto_backend_mbedtls_hmac_sha256_context_t;
/**
* @internal @brief Context for HMAC SHA256 using mbed TLS backend.
*/
typedef nrf_crypto_backend_mbedtls_hmac_sha256_context_t nrf_crypto_backend_hmac_sha256_context_t;
#endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_HMAC_SHA256)
#if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_HMAC_SHA512)
#if NRF_MODULE_ENABLED(NRF_CRYPTO_HMAC_SHA512)
#error "Duplicate definition of HMAC SHA-512. More than one backend enabled"
#endif // NRF_CRYPTO_HMAC_SHA512_ENABLED
#define NRF_CRYPTO_HMAC_SHA512_ENABLED 1
/**
* @internal @brief Internal context object used by the mbed TLS backend wrapper for HMAC SHA512.
*
* @note This should never be used directly. Use @ref nrf_crypto_backend_hmac_sha512_context_t
* instead.
*/
typedef struct
{
nrf_crypto_hmac_internal_context_t header; //!< Internal nrf_crypto_hmac context header.
mbedtls_md_context_t mbedtls_ctx; //!< Mbed TLS context object.
uint8_t md_ctx_buffer[sizeof(mbedtls_sha512_context)]; //!< Message digest buffer for mbed TLS.
uint16_t hmac_ctx_buffer[128]; //!< Hash buffer for mbed TLS of size defined in mbedtls_sha512_info in md_internal.h.
} nrf_crypto_backend_mbedtls_hmac_sha512_context_t;
/**
* @internal @brief Context for HMAC SHA512 using mbed TLS backend.
*/
typedef nrf_crypto_backend_mbedtls_hmac_sha512_context_t nrf_crypto_backend_hmac_sha512_context_t;
#ifdef __cplusplus
}
#endif
#endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_HMAC_SHA512)
#endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS) && ( NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_HMAC_SHA256) || NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_HMAC_SHA512) )
/**@} */
#endif // MBEDTLS_BACKEND_HMAC_H__

View File

@@ -0,0 +1,106 @@
/**
* Copyright (c) 2018 - 2020, 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"
#include "sdk_config.h"
#if NRF_MODULE_ENABLED(NRF_CRYPTO) && NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS)
#include <string.h>
#include <stdint.h>
#include "nrf_crypto_init.h"
#include "nrf_crypto_mem.h"
/*lint -save -e????*/
#include "mbedtls/platform.h"
/*lint -restore*/
#if NRF_CRYPTO_ALLOC_ON_STACK
#error "MBED TLS backend does not support memory allocation on stack. Use different allocator."
#endif
/** @internal @brief Function to use NRF_CRYPTO_ALLOC for MBED TLS memory allocation.
*/
static void * mbedtls_backend_calloc(size_t count, size_t size)
{
size_t total_size = count * size;
void * p_data = NRF_CRYPTO_ALLOC(total_size);
if (p_data != NULL)
{
memset(p_data, 0, total_size);
}
return p_data;
}
/** @internal @brief Function to use NRF_CRYPTO_FREE for MBED TLS memory deallocation.
*/
static void mbedtls_backend_free(void * p_data)
{
NRF_CRYPTO_FREE(p_data);
}
/** @internal @brief Function to initialize MBED TLS backend - setup memory management for.
*/
static ret_code_t mbedtls_backend_init(void)
{
(void)mbedtls_platform_set_calloc_free(mbedtls_backend_calloc, mbedtls_backend_free);
return NRF_SUCCESS;
}
/** @internal @brief Function to uninitialize MBED TLS backend - currently no implementation is required.
*/
static ret_code_t mbedtls_backend_uninit(void)
{
// Empty implementation
return NRF_SUCCESS;
}
CRYPTO_BACKEND_REGISTER(nrf_crypto_backend_info_t const mbedtls_backend) =
{
.init_fn = mbedtls_backend_init,
.uninit_fn = mbedtls_backend_uninit,
};
#endif // NRF_MODULE_ENABLED(NRF_CRYPTO) && NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS)