#include <wolfssl/wolfcrypt/libwolfssl_sources.h>
#ifdef WOLFSSL_CRYPTOCELL_C
#ifdef WOLFSSL_CRYPTOCELL
#include <wolfssl/wolfcrypt/ecc.h>
#include <wolfssl/wolfcrypt/port/arm/cryptoCell.h>
#ifdef NO_INLINE
#include <wolfssl/wolfcrypt/misc.h>
#else
#define WOLFSSL_MISC_INCLUDED
#include <wolfcrypt/src/misc.c>
#endif
CRYS_RND_State_t wc_rndState;
CRYS_RND_WorkBuff_t wc_rndWorkBuff;
SaSiRndGenerateVectWorkFunc_t wc_rndGenVectFunc = CRYS_RND_GenerateVector;
static word32 cc310_enableCount = 0;
static void cc310_enable(void)
{
cc310_enableCount++;
NRF_CRYPTOCELL->ENABLE = 1;
NVIC_EnableIRQ(CRYPTOCELL_IRQn);
}
static void cc310_disable(void)
{
cc310_enableCount--;
if (cc310_enableCount == 0) {
NRF_CRYPTOCELL->ENABLE = 0;
NVIC_DisableIRQ(CRYPTOCELL_IRQn);
}
}
int cc310_Init(void)
{
int ret = 0;
static int initialized = 0;
if (!initialized) {
cc310_enable();
ret = SaSi_LibInit();
if (ret != SA_SILIB_RET_OK) {
WOLFSSL_MSG("Error SaSi_LibInit");
return ret;
}
ret = CRYS_RndInit(&wc_rndState, &wc_rndWorkBuff);
if (ret != CRYS_OK) {
WOLFSSL_MSG("Error CRYS_RndInit");
return ret;
}
initialized = 1;
}
return ret;
}
void cc310_Free(void)
{
CRYSError_t crys_result;
SaSi_LibFini();
crys_result = CRYS_RND_UnInstantiation(&wc_rndState);
if (crys_result != CRYS_OK) {
WOLFSSL_MSG("Error RYS_RND_UnInstantiation");
}
cc310_disable();
}
int cc310_random_generate(byte* output, word32 size)
{
CRYSError_t crys_result;
crys_result = CRYS_RND_GenerateVector(&wc_rndState, size, output);
return (crys_result == CRYS_OK) ? 0 : -1;
}
#ifdef HAVE_ECC
CRYS_ECPKI_DomainID_t cc310_mapCurve(int curve_id)
{
switch(curve_id)
{
case ECC_CURVE_DEF: return CRYS_ECPKI_DomainID_secp256r1;
case ECC_SECP160K1: return CRYS_ECPKI_DomainID_secp160k1;
case ECC_SECP160R1: return CRYS_ECPKI_DomainID_secp160r1;
case ECC_SECP160R2: return CRYS_ECPKI_DomainID_secp160r2;
case ECC_SECP192K1: return CRYS_ECPKI_DomainID_secp192k1;
case ECC_SECP192R1: return CRYS_ECPKI_DomainID_secp192r1;
case ECC_SECP224K1: return CRYS_ECPKI_DomainID_secp224k1;
case ECC_SECP224R1: return CRYS_ECPKI_DomainID_secp224r1;
case ECC_SECP256K1: return CRYS_ECPKI_DomainID_secp256k1;
case ECC_SECP256R1: return CRYS_ECPKI_DomainID_secp256r1;
case ECC_SECP384R1: return CRYS_ECPKI_DomainID_secp384r1;
case ECC_SECP521R1: return CRYS_ECPKI_DomainID_secp521r1;
default: WOLFSSL_MSG("Curve not identified");
return CRYS_ECPKI_DomainID_Builded;
}
}
#endif
#ifndef NO_RSA
CRYS_RSA_HASH_OpMode_t cc310_hashModeRSA(enum wc_HashType hash_type, int isHashed)
{
switch(hash_type)
{
case WC_HASH_TYPE_MD5:
#ifndef NO_MD5
return isHashed? CRYS_RSA_After_MD5_mode : CRYS_RSA_HASH_MD5_mode;
#endif
case WC_HASH_TYPE_SHA:
#ifndef NO_SHA
return isHashed? CRYS_RSA_After_SHA1_mode : CRYS_RSA_HASH_SHA1_mode;
#endif
case WC_HASH_TYPE_SHA224:
#ifdef WOLFSSL_SHA224
return isHashed? CRYS_RSA_After_SHA224_mode : CRYS_RSA_HASH_SHA224_mode;
#endif
case WC_HASH_TYPE_SHA256:
#ifndef NO_SHA256
return isHashed? CRYS_RSA_After_SHA256_mode : CRYS_RSA_HASH_SHA256_mode;
#endif
case WC_HASH_TYPE_SHA384:
#ifdef WOLFSSL_SHA384
return isHashed? CRYS_RSA_After_SHA384_mode : CRYS_RSA_HASH_SHA384_mode;
#endif
case WC_HASH_TYPE_SHA512:
#ifdef WOLFSSL_SHA512
return isHashed? CRYS_RSA_After_SHA512_mode : CRYS_RSA_HASH_SHA512_mode;
#endif
case WC_HASH_TYPE_NONE:
return isHashed? CRYS_RSA_After_SHA256_mode : CRYS_RSA_HASH_SHA256_mode;
default:
return CRYS_RSA_After_HASH_NOT_KNOWN_mode;
}
}
#endif
#ifdef HAVE_ECC
CRYS_ECPKI_HASH_OpMode_t cc310_hashModeECC(int hash_size)
{
CRYS_ECPKI_HASH_OpMode_t hash_mode;
switch (hash_size)
{
case 20:
hash_mode = CRYS_ECPKI_AFTER_HASH_SHA1_mode;
break;
case 28:
hash_mode = CRYS_ECPKI_AFTER_HASH_SHA224_mode;
break;
case 32:
hash_mode = CRYS_ECPKI_AFTER_HASH_SHA256_mode;
break;
case 48:
hash_mode = CRYS_ECPKI_AFTER_HASH_SHA384_mode;
break;
case 64:
hash_mode = CRYS_ECPKI_AFTER_HASH_SHA512_mode;
break;
default:
hash_mode = CRYS_ECPKI_HASH_OpModeLast;
break;
}
return hash_mode;
}
#endif
#endif
#if !defined(NO_CRYPT_BENCHMARK) && defined(WOLFSSL_nRF5x_SDK_15_2)
static int mRtcSec = 0;
static const nrfx_rtc_t rtc = NRFX_RTC_INSTANCE(0);
static void rtc_handler(nrfx_rtc_int_type_t int_type)
{
if (int_type == NRFX_RTC_INT_COMPARE0) {
mRtcSec++;
nrfx_rtc_counter_clear(&rtc);
nrfx_rtc_int_enable(&rtc, RTC_CHANNEL_INT_MASK(0));
#ifdef BSP_LED_1
nrf_gpio_pin_toggle(BSP_LED_1);
#endif
}
else if (int_type == NRF_DRV_RTC_INT_TICK) {
#ifdef BSP_LED_0
nrf_gpio_pin_toggle(BSP_LED_0);
#endif
}
}
static void rtc_config(void)
{
uint32_t err_code;
nrfx_rtc_config_t config = NRFX_RTC_DEFAULT_CONFIG;
bsp_board_init(BSP_INIT_LEDS);
err_code = nrf_drv_clock_init();
APP_ERROR_CHECK(err_code);
nrf_drv_clock_lfclk_request(NULL);
err_code = nrfx_rtc_init(&rtc, &config, rtc_handler);
APP_ERROR_CHECK(err_code);
nrfx_rtc_tick_enable(&rtc, false);
err_code = nrfx_rtc_cc_set(&rtc, 0, RTC_INPUT_FREQ, true);
APP_ERROR_CHECK(err_code);
nrfx_rtc_enable(&rtc);
}
static int rtc_get_ms(void)
{
int frequency = (RTC_INPUT_FREQ / (rtc_prescaler_get(rtc.p_reg) + 1));
uint32_t counter = nrfx_rtc_counter_get(&rtc);
return ((counter * 1000) + (frequency / 2) ) / frequency;
}
double current_time(int reset)
{
double time;
static int initialized = 0;
if (!initialized) {
rtc_config();
initialized = 1;
}
time = mRtcSec;
time += (double)rtc_get_ms() / 1000;
return time;
}
int nrf_random_generate(byte* output, word32 size)
{
uint32_t err_code;
static int initialized = 0;
if (!initialized) {
err_code = nrf_drv_rng_init(NULL);
if (err_code != NRF_SUCCESS) {
return -1;
}
initialized = 1;
}
nrf_drv_rng_block_rand(output, size);
return 0;
}
#endif
#endif