|
@@ -30,7 +30,6 @@
|
|
#endif
|
|
#endif
|
|
|
|
|
|
/* Private typedef --------------------------------------------------------------*/
|
|
/* Private typedef --------------------------------------------------------------*/
|
|
-
|
|
|
|
typedef struct
|
|
typedef struct
|
|
{
|
|
{
|
|
uint8_t *pu8SHATempBuf;
|
|
uint8_t *pu8SHATempBuf;
|
|
@@ -72,76 +71,38 @@ static struct rt_mutex s_SHA_mutex;
|
|
|
|
|
|
#if !defined(BSP_USING_TRNG)
|
|
#if !defined(BSP_USING_TRNG)
|
|
static struct rt_mutex s_PRNG_mutex;
|
|
static struct rt_mutex s_PRNG_mutex;
|
|
- static volatile int s_PRNG_done;
|
|
|
|
#endif
|
|
#endif
|
|
|
|
|
|
-static volatile int s_AES_done;
|
|
|
|
-static volatile int s_TDES_done;
|
|
|
|
-static volatile int s_SHA_done;
|
|
|
|
-
|
|
|
|
static rt_err_t nu_crypto_init(void)
|
|
static rt_err_t nu_crypto_init(void)
|
|
{
|
|
{
|
|
- rt_err_t result;
|
|
|
|
|
|
+ rt_err_t result = RT_EOK;
|
|
|
|
|
|
- /* Enable Crypto engine interrupt */
|
|
|
|
- NVIC_EnableIRQ(CRPT_IRQn);
|
|
|
|
-
|
|
|
|
- AES_ENABLE_INT(CRPT);
|
|
|
|
- TDES_ENABLE_INT(CRPT);
|
|
|
|
- SHA_ENABLE_INT(CRPT);
|
|
|
|
-
|
|
|
|
- //init cipher mutex
|
|
|
|
|
|
+ /* init cipher mutex */
|
|
|
|
+#if defined(RT_HWCRYPTO_USING_AES)
|
|
result = rt_mutex_init(&s_AES_mutex, NU_HWCRYPTO_AES_NAME, RT_IPC_FLAG_PRIO);
|
|
result = rt_mutex_init(&s_AES_mutex, NU_HWCRYPTO_AES_NAME, RT_IPC_FLAG_PRIO);
|
|
RT_ASSERT(result == RT_EOK);
|
|
RT_ASSERT(result == RT_EOK);
|
|
|
|
+ AES_ENABLE_INT(CRPT);
|
|
|
|
+#endif
|
|
|
|
|
|
|
|
+#if defined(RT_HWCRYPTO_USING_3DES)
|
|
result = rt_mutex_init(&s_TDES_mutex, NU_HWCRYPTO_TDES_NAME, RT_IPC_FLAG_PRIO);
|
|
result = rt_mutex_init(&s_TDES_mutex, NU_HWCRYPTO_TDES_NAME, RT_IPC_FLAG_PRIO);
|
|
RT_ASSERT(result == RT_EOK);
|
|
RT_ASSERT(result == RT_EOK);
|
|
|
|
+ TDES_ENABLE_INT(CRPT);
|
|
|
|
+#endif
|
|
|
|
|
|
|
|
+#if defined(RT_HWCRYPTO_USING_SHA1) || defined(RT_HWCRYPTO_USING_SHA2)
|
|
result = rt_mutex_init(&s_SHA_mutex, NU_HWCRYPTO_SHA_NAME, RT_IPC_FLAG_PRIO);
|
|
result = rt_mutex_init(&s_SHA_mutex, NU_HWCRYPTO_SHA_NAME, RT_IPC_FLAG_PRIO);
|
|
RT_ASSERT(result == RT_EOK);
|
|
RT_ASSERT(result == RT_EOK);
|
|
|
|
+ SHA_ENABLE_INT(CRPT);
|
|
|
|
+#endif
|
|
|
|
|
|
-#if !defined(BSP_USING_TRNG)
|
|
|
|
- PRNG_ENABLE_INT(CRPT);
|
|
|
|
|
|
+#if defined(RT_HWCRYPTO_USING_RNG) && !defined(BSP_USING_TRNG)
|
|
result = rt_mutex_init(&s_PRNG_mutex, NU_HWCRYPTO_PRNG_NAME, RT_IPC_FLAG_PRIO);
|
|
result = rt_mutex_init(&s_PRNG_mutex, NU_HWCRYPTO_PRNG_NAME, RT_IPC_FLAG_PRIO);
|
|
RT_ASSERT(result == RT_EOK);
|
|
RT_ASSERT(result == RT_EOK);
|
|
|
|
+ PRNG_ENABLE_INT(CRPT);
|
|
#endif
|
|
#endif
|
|
|
|
|
|
- return RT_EOK;
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-//Crypto engine IRQ handler
|
|
|
|
-
|
|
|
|
-void CRYPTO_IRQHandler()
|
|
|
|
-{
|
|
|
|
- if (AES_GET_INT_FLAG(CRPT))
|
|
|
|
- {
|
|
|
|
- if (CRPT->INTSTS & (CRPT_INTSTS_AESEIF_Msk) || (CRPT->AES_STS & (CRPT_AES_STS_BUSERR_Msk | CRPT_AES_STS_CNTERR_Msk | (0x1ul << 21))))
|
|
|
|
- rt_kprintf("AES ERROR\n");
|
|
|
|
- s_AES_done = 1;
|
|
|
|
- AES_CLR_INT_FLAG(CRPT);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- if (TDES_GET_INT_FLAG(CRPT))
|
|
|
|
- {
|
|
|
|
- s_TDES_done = 1;
|
|
|
|
- TDES_CLR_INT_FLAG(CRPT);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- if (SHA_GET_INT_FLAG(CRPT))
|
|
|
|
- {
|
|
|
|
- if (CRPT->INTSTS & (CRPT_INTSTS_HMACEIF_Msk) || (CRPT->HMAC_STS & (CRPT_HMAC_STS_DMAERR_Msk | (0x1ul << 9))))
|
|
|
|
- rt_kprintf("SHA ERROR\n");
|
|
|
|
- s_SHA_done = 1;
|
|
|
|
- SHA_CLR_INT_FLAG(CRPT);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
-#if !defined(BSP_USING_TRNG)
|
|
|
|
- if (PRNG_GET_INT_FLAG(CRPT))
|
|
|
|
- {
|
|
|
|
- s_PRNG_done = 1;
|
|
|
|
- PRNG_CLR_INT_FLAG(CRPT);
|
|
|
|
- }
|
|
|
|
-#endif
|
|
|
|
|
|
+ return result;
|
|
}
|
|
}
|
|
|
|
|
|
static rt_err_t nu_aes_crypt_run(
|
|
static rt_err_t nu_aes_crypt_run(
|
|
@@ -155,10 +116,9 @@ static rt_err_t nu_aes_crypt_run(
|
|
uint32_t u32DataLen
|
|
uint32_t u32DataLen
|
|
)
|
|
)
|
|
{
|
|
{
|
|
- rt_err_t result;
|
|
|
|
-
|
|
|
|
uint32_t au32SwapKey[8];
|
|
uint32_t au32SwapKey[8];
|
|
uint32_t au32SwapIV[4];
|
|
uint32_t au32SwapIV[4];
|
|
|
|
+ rt_err_t result;
|
|
|
|
|
|
au32SwapKey[0] = nu_get32_be(&pu8Key[0]);
|
|
au32SwapKey[0] = nu_get32_be(&pu8Key[0]);
|
|
au32SwapKey[1] = nu_get32_be(&pu8Key[4]);
|
|
au32SwapKey[1] = nu_get32_be(&pu8Key[4]);
|
|
@@ -187,16 +147,26 @@ static rt_err_t nu_aes_crypt_run(
|
|
|
|
|
|
//Using Channel 0
|
|
//Using Channel 0
|
|
AES_Open(CRPT, 0, bEncrypt, u32OpMode, u32KeySize, AES_IN_OUT_SWAP);
|
|
AES_Open(CRPT, 0, bEncrypt, u32OpMode, u32KeySize, AES_IN_OUT_SWAP);
|
|
- AES_SetKey(CRPT, 0, (uint32_t *)au32SwapKey, u32KeySize);
|
|
|
|
|
|
+ AES_SetKey(CRPT, 0, (uint32_t *)&au32SwapKey[0], u32KeySize);
|
|
AES_SetInitVect(CRPT, 0, (uint32_t *)au32SwapIV);
|
|
AES_SetInitVect(CRPT, 0, (uint32_t *)au32SwapIV);
|
|
|
|
|
|
//Setup AES DMA
|
|
//Setup AES DMA
|
|
AES_SetDMATransfer(CRPT, 0, (uint32_t)pu8InData, (uint32_t)pu8OutData, u32DataLen);
|
|
AES_SetDMATransfer(CRPT, 0, (uint32_t)pu8InData, (uint32_t)pu8OutData, u32DataLen);
|
|
AES_CLR_INT_FLAG(CRPT);
|
|
AES_CLR_INT_FLAG(CRPT);
|
|
- //Start AES encryption/decryption
|
|
|
|
- s_AES_done = 0;
|
|
|
|
|
|
+
|
|
|
|
+ /* Start AES encryption/decryption */
|
|
AES_Start(CRPT, 0, CRYPTO_DMA_ONE_SHOT);
|
|
AES_Start(CRPT, 0, CRYPTO_DMA_ONE_SHOT);
|
|
- while (!s_AES_done) {};
|
|
|
|
|
|
+
|
|
|
|
+ /* Wait done */
|
|
|
|
+ while (!(CRPT->INTSTS & CRPT_INTEN_AESIEN_Msk)) {};
|
|
|
|
+
|
|
|
|
+ if ((u32DataLen % 16) && (CRPT->AES_STS & (CRPT_AES_STS_OUTBUFEMPTY_Msk | CRPT_AES_STS_INBUFEMPTY_Msk)))
|
|
|
|
+ rt_kprintf("AES WARNING - AES Data length(%d) is not enough. -> %d \n", u32DataLen, RT_ALIGN(u32DataLen, 16));
|
|
|
|
+ else if (CRPT->INTSTS & (CRPT_INTSTS_AESEIF_Msk) || (CRPT->AES_STS & (CRPT_AES_STS_BUSERR_Msk | CRPT_AES_STS_CNTERR_Msk)))
|
|
|
|
+ rt_kprintf("AES ERROR - CRPT->INTSTS-%08x, CRPT->AES_STS-%08x\n", CRPT->INTSTS, CRPT->AES_STS);
|
|
|
|
+
|
|
|
|
+ /* Clear AES interrupt status */
|
|
|
|
+ AES_CLR_INT_FLAG(CRPT);
|
|
|
|
|
|
result = rt_mutex_release(&s_AES_mutex);
|
|
result = rt_mutex_release(&s_AES_mutex);
|
|
RT_ASSERT(result == RT_EOK);
|
|
RT_ASSERT(result == RT_EOK);
|
|
@@ -213,32 +183,33 @@ static void nu_prng_open(uint32_t u32Seed)
|
|
result = rt_mutex_take(&s_PRNG_mutex, RT_WAITING_FOREVER);
|
|
result = rt_mutex_take(&s_PRNG_mutex, RT_WAITING_FOREVER);
|
|
RT_ASSERT(result == RT_EOK);
|
|
RT_ASSERT(result == RT_EOK);
|
|
|
|
|
|
- //Open PRNG 64 bits. But always return 32 bits
|
|
|
|
|
|
+ //Open PRNG 64 bits.
|
|
PRNG_Open(CRPT, PRNG_KEY_SIZE_64, PRNG_SEED_RELOAD, u32Seed);
|
|
PRNG_Open(CRPT, PRNG_KEY_SIZE_64, PRNG_SEED_RELOAD, u32Seed);
|
|
|
|
|
|
result = rt_mutex_release(&s_PRNG_mutex);
|
|
result = rt_mutex_release(&s_PRNG_mutex);
|
|
RT_ASSERT(result == RT_EOK);
|
|
RT_ASSERT(result == RT_EOK);
|
|
-
|
|
|
|
}
|
|
}
|
|
|
|
|
|
static rt_uint32_t nu_prng_run(void)
|
|
static rt_uint32_t nu_prng_run(void)
|
|
{
|
|
{
|
|
- rt_err_t result;
|
|
|
|
uint32_t au32RNGValue[2];
|
|
uint32_t au32RNGValue[2];
|
|
|
|
+ rt_err_t result;
|
|
|
|
|
|
result = rt_mutex_take(&s_PRNG_mutex, RT_WAITING_FOREVER);
|
|
result = rt_mutex_take(&s_PRNG_mutex, RT_WAITING_FOREVER);
|
|
RT_ASSERT(result == RT_EOK);
|
|
RT_ASSERT(result == RT_EOK);
|
|
|
|
|
|
- s_PRNG_done = 0;
|
|
|
|
PRNG_Start(CRPT);
|
|
PRNG_Start(CRPT);
|
|
- while (!s_PRNG_done) {};
|
|
|
|
|
|
+ while ((CRPT->PRNG_CTL & CRPT_PRNG_CTL_BUSY_Msk)) {};
|
|
|
|
+
|
|
|
|
+ /* Clear PRNG interrupt status */
|
|
|
|
+ PRNG_CLR_INT_FLAG(CRPT);
|
|
|
|
|
|
- PRNG_Read(CRPT, au32RNGValue);
|
|
|
|
|
|
+ PRNG_Read(CRPT, &au32RNGValue[0]);
|
|
|
|
|
|
result = rt_mutex_release(&s_PRNG_mutex);
|
|
result = rt_mutex_release(&s_PRNG_mutex);
|
|
RT_ASSERT(result == RT_EOK);
|
|
RT_ASSERT(result == RT_EOK);
|
|
|
|
|
|
- return au32RNGValue[0];
|
|
|
|
|
|
+ return au32RNGValue[0] ^ au32RNGValue[1];
|
|
}
|
|
}
|
|
|
|
|
|
#endif
|
|
#endif
|
|
@@ -251,6 +222,8 @@ static rt_err_t nu_aes_crypt(struct hwcrypto_symmetric *symmetric_ctx, struct hw
|
|
unsigned char in_align_flag = 0;
|
|
unsigned char in_align_flag = 0;
|
|
unsigned char out_align_flag = 0;
|
|
unsigned char out_align_flag = 0;
|
|
unsigned char iv_temp[16];
|
|
unsigned char iv_temp[16];
|
|
|
|
+ RT_ASSERT(symmetric_ctx != RT_NULL);
|
|
|
|
+ RT_ASSERT(symmetric_info != RT_NULL);
|
|
|
|
|
|
if ((symmetric_info->length % 4) != 0)
|
|
if ((symmetric_info->length % 4) != 0)
|
|
{
|
|
{
|
|
@@ -408,10 +381,11 @@ static rt_err_t nu_des_crypt_run(
|
|
//Setup TDES DMA
|
|
//Setup TDES DMA
|
|
TDES_SetDMATransfer(CRPT, 0, (uint32_t)pu8InData, (uint32_t)pu8OutData, u32DataLen);
|
|
TDES_SetDMATransfer(CRPT, 0, (uint32_t)pu8InData, (uint32_t)pu8OutData, u32DataLen);
|
|
TDES_CLR_INT_FLAG(CRPT);
|
|
TDES_CLR_INT_FLAG(CRPT);
|
|
|
|
+
|
|
//Start TDES encryption/decryption
|
|
//Start TDES encryption/decryption
|
|
- s_TDES_done = 0;
|
|
|
|
TDES_Start(CRPT, 0, CRYPTO_DMA_ONE_SHOT);
|
|
TDES_Start(CRPT, 0, CRYPTO_DMA_ONE_SHOT);
|
|
- while (!s_TDES_done) {};
|
|
|
|
|
|
+ /* Wait done */
|
|
|
|
+ while (!(CRPT->INTSTS & CRPT_INTEN_TDESIEN_Msk)) {};
|
|
|
|
|
|
result = rt_mutex_release(&s_TDES_mutex);
|
|
result = rt_mutex_release(&s_TDES_mutex);
|
|
RT_ASSERT(result == RT_EOK);
|
|
RT_ASSERT(result == RT_EOK);
|
|
@@ -521,10 +495,6 @@ static void SHABlockUpdate(uint32_t u32OpMode, uint32_t u32SrcAddr, uint32_t u32
|
|
|
|
|
|
//Setup SHA DMA
|
|
//Setup SHA DMA
|
|
SHA_SetDMATransfer(CRPT, u32SrcAddr, u32Len);
|
|
SHA_SetDMATransfer(CRPT, u32SrcAddr, u32Len);
|
|
- SHA_CLR_INT_FLAG(CRPT);
|
|
|
|
-
|
|
|
|
- //Start SHA
|
|
|
|
- s_SHA_done = 0;
|
|
|
|
|
|
|
|
if (u32Mode == CRYPTO_DMA_FIRST)
|
|
if (u32Mode == CRYPTO_DMA_FIRST)
|
|
{
|
|
{
|
|
@@ -547,10 +517,18 @@ static void SHABlockUpdate(uint32_t u32OpMode, uint32_t u32SrcAddr, uint32_t u32
|
|
CRPT->HMAC_CTL &= ~CRPT_HMAC_CTL_DMAFIRST_Msk;
|
|
CRPT->HMAC_CTL &= ~CRPT_HMAC_CTL_DMAFIRST_Msk;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+ //Start SHA
|
|
|
|
+ SHA_CLR_INT_FLAG(CRPT);
|
|
SHA_Start(CRPT, u32Mode);
|
|
SHA_Start(CRPT, u32Mode);
|
|
|
|
|
|
- while (!s_SHA_done) {};
|
|
|
|
|
|
+ /* Wait done */
|
|
|
|
+ while (!(CRPT->INTSTS & CRPT_INTSTS_HMACIF_Msk)) {};
|
|
|
|
+
|
|
|
|
+ if (CRPT->INTSTS & (CRPT_INTSTS_HMACEIF_Msk) || (CRPT->HMAC_STS & (CRPT_HMAC_STS_DMAERR_Msk)))
|
|
|
|
+ rt_kprintf("SHA ERROR - CRPT->INTSTS-%08x, CRPT->HMAC_STS-%08x\n", CRPT->INTSTS, CRPT->HMAC_STS);
|
|
|
|
+
|
|
|
|
+ /* Clear SHA interrupt status */
|
|
|
|
+ SHA_CLR_INT_FLAG(CRPT);
|
|
}
|
|
}
|
|
|
|
|
|
static rt_err_t nu_sha_hash_run(
|
|
static rt_err_t nu_sha_hash_run(
|
|
@@ -562,6 +540,9 @@ static rt_err_t nu_sha_hash_run(
|
|
{
|
|
{
|
|
rt_err_t result;
|
|
rt_err_t result;
|
|
|
|
|
|
|
|
+ RT_ASSERT(psSHACtx != RT_NULL);
|
|
|
|
+ RT_ASSERT(pu8InData != RT_NULL);
|
|
|
|
+
|
|
result = rt_mutex_take(&s_SHA_mutex, RT_WAITING_FOREVER);
|
|
result = rt_mutex_take(&s_SHA_mutex, RT_WAITING_FOREVER);
|
|
RT_ASSERT(result == RT_EOK);
|
|
RT_ASSERT(result == RT_EOK);
|
|
|
|
|
|
@@ -603,10 +584,8 @@ static rt_err_t nu_sha_hash_run(
|
|
if (psSHACtx->pu8SHATempBuf == RT_NULL)
|
|
if (psSHACtx->pu8SHATempBuf == RT_NULL)
|
|
{
|
|
{
|
|
LOG_E("fun[%s] memory allocate %d bytes failed!", __FUNCTION__, psSHACtx->u32BlockSize);
|
|
LOG_E("fun[%s] memory allocate %d bytes failed!", __FUNCTION__, psSHACtx->u32BlockSize);
|
|
-
|
|
|
|
result = rt_mutex_release(&s_SHA_mutex);
|
|
result = rt_mutex_release(&s_SHA_mutex);
|
|
RT_ASSERT(result == RT_EOK);
|
|
RT_ASSERT(result == RT_EOK);
|
|
-
|
|
|
|
return -RT_ENOMEM;
|
|
return -RT_ENOMEM;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -634,10 +613,8 @@ static rt_err_t nu_sha_hash_run(
|
|
if (psSHACtx->pu8SHATempBuf == RT_NULL)
|
|
if (psSHACtx->pu8SHATempBuf == RT_NULL)
|
|
{
|
|
{
|
|
LOG_E("fun[%s] memory allocate %d bytes failed!", __FUNCTION__, psSHACtx->u32BlockSize);
|
|
LOG_E("fun[%s] memory allocate %d bytes failed!", __FUNCTION__, psSHACtx->u32BlockSize);
|
|
-
|
|
|
|
result = rt_mutex_release(&s_SHA_mutex);
|
|
result = rt_mutex_release(&s_SHA_mutex);
|
|
RT_ASSERT(result == RT_EOK);
|
|
RT_ASSERT(result == RT_EOK);
|
|
-
|
|
|
|
return -RT_ENOMEM;
|
|
return -RT_ENOMEM;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -659,6 +636,8 @@ static rt_err_t nu_sha_update(struct hwcrypto_hash *hash_ctx, const rt_uint8_t *
|
|
uint32_t u32SHAOpMode;
|
|
uint32_t u32SHAOpMode;
|
|
unsigned char *nu_in;
|
|
unsigned char *nu_in;
|
|
unsigned char in_align_flag = 0;
|
|
unsigned char in_align_flag = 0;
|
|
|
|
+ RT_ASSERT(hash_ctx != RT_NULL);
|
|
|
|
+ RT_ASSERT(in != RT_NULL);
|
|
|
|
|
|
//Select SHA operation mode
|
|
//Select SHA operation mode
|
|
switch (hash_ctx->parent.type & (HWCRYPTO_MAIN_TYPE_MASK | HWCRYPTO_SUB_TYPE_MASK))
|
|
switch (hash_ctx->parent.type & (HWCRYPTO_MAIN_TYPE_MASK | HWCRYPTO_SUB_TYPE_MASK))
|
|
@@ -713,7 +692,11 @@ static rt_err_t nu_sha_finish(struct hwcrypto_hash *hash_ctx, rt_uint8_t *out, r
|
|
unsigned char *nu_out;
|
|
unsigned char *nu_out;
|
|
unsigned char out_align_flag = 0;
|
|
unsigned char out_align_flag = 0;
|
|
uint32_t u32SHAOpMode;
|
|
uint32_t u32SHAOpMode;
|
|
- S_SHA_CONTEXT *psSHACtx = hash_ctx->parent.contex;
|
|
|
|
|
|
+ S_SHA_CONTEXT *psSHACtx = RT_NULL;
|
|
|
|
+ RT_ASSERT(hash_ctx != RT_NULL);
|
|
|
|
+ RT_ASSERT(out != RT_NULL);
|
|
|
|
+
|
|
|
|
+ psSHACtx = hash_ctx->parent.contex;
|
|
|
|
|
|
//Check SHA Hash value buffer length
|
|
//Check SHA Hash value buffer length
|
|
switch (hash_ctx->parent.type & (HWCRYPTO_MAIN_TYPE_MASK | HWCRYPTO_SUB_TYPE_MASK))
|
|
switch (hash_ctx->parent.type & (HWCRYPTO_MAIN_TYPE_MASK | HWCRYPTO_SUB_TYPE_MASK))
|
|
@@ -857,6 +840,7 @@ static const struct hwcrypto_rng_ops nu_rng_ops =
|
|
static rt_err_t nu_hwcrypto_create(struct rt_hwcrypto_ctx *ctx)
|
|
static rt_err_t nu_hwcrypto_create(struct rt_hwcrypto_ctx *ctx)
|
|
{
|
|
{
|
|
rt_err_t res = RT_EOK;
|
|
rt_err_t res = RT_EOK;
|
|
|
|
+ RT_ASSERT(ctx != RT_NULL);
|
|
|
|
|
|
switch (ctx->type & HWCRYPTO_MAIN_TYPE_MASK)
|
|
switch (ctx->type & HWCRYPTO_MAIN_TYPE_MASK)
|
|
{
|
|
{
|
|
@@ -890,35 +874,16 @@ static rt_err_t nu_hwcrypto_create(struct rt_hwcrypto_ctx *ctx)
|
|
}
|
|
}
|
|
|
|
|
|
case HWCRYPTO_TYPE_DES:
|
|
case HWCRYPTO_TYPE_DES:
|
|
- {
|
|
|
|
- ctx->contex = RT_NULL;
|
|
|
|
- //Setup DES operation
|
|
|
|
- ((struct hwcrypto_symmetric *)ctx)->ops = &nu_des_ops;
|
|
|
|
- break;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
case HWCRYPTO_TYPE_3DES:
|
|
case HWCRYPTO_TYPE_3DES:
|
|
{
|
|
{
|
|
ctx->contex = RT_NULL;
|
|
ctx->contex = RT_NULL;
|
|
- //Setup 3DES operation
|
|
|
|
|
|
+ //Setup operation
|
|
((struct hwcrypto_symmetric *)ctx)->ops = &nu_des_ops;
|
|
((struct hwcrypto_symmetric *)ctx)->ops = &nu_des_ops;
|
|
break;
|
|
break;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
case HWCRYPTO_TYPE_SHA1:
|
|
case HWCRYPTO_TYPE_SHA1:
|
|
- {
|
|
|
|
- ctx->contex = rt_malloc(sizeof(S_SHA_CONTEXT));
|
|
|
|
-
|
|
|
|
- if (ctx->contex == RT_NULL)
|
|
|
|
- return -RT_ERROR;
|
|
|
|
-
|
|
|
|
- rt_memset(ctx->contex, 0, sizeof(S_SHA_CONTEXT));
|
|
|
|
- //Setup SHA1 operation
|
|
|
|
- ((struct hwcrypto_hash *)ctx)->ops = &nu_sha_ops;
|
|
|
|
- break;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
case HWCRYPTO_TYPE_SHA2:
|
|
case HWCRYPTO_TYPE_SHA2:
|
|
{
|
|
{
|
|
ctx->contex = rt_malloc(sizeof(S_SHA_CONTEXT));
|
|
ctx->contex = rt_malloc(sizeof(S_SHA_CONTEXT));
|
|
@@ -927,7 +892,7 @@ static rt_err_t nu_hwcrypto_create(struct rt_hwcrypto_ctx *ctx)
|
|
return -RT_ERROR;
|
|
return -RT_ERROR;
|
|
|
|
|
|
rt_memset(ctx->contex, 0, sizeof(S_SHA_CONTEXT));
|
|
rt_memset(ctx->contex, 0, sizeof(S_SHA_CONTEXT));
|
|
- //Setup SHA2 operation
|
|
|
|
|
|
+ //Setup operation
|
|
((struct hwcrypto_hash *)ctx)->ops = &nu_sha_ops;
|
|
((struct hwcrypto_hash *)ctx)->ops = &nu_sha_ops;
|
|
break;
|
|
break;
|
|
}
|
|
}
|
|
@@ -953,11 +918,15 @@ static rt_err_t nu_hwcrypto_create(struct rt_hwcrypto_ctx *ctx)
|
|
break;
|
|
break;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ nu_hwcrypto_reset(ctx);
|
|
|
|
+
|
|
return res;
|
|
return res;
|
|
}
|
|
}
|
|
|
|
|
|
static void nu_hwcrypto_destroy(struct rt_hwcrypto_ctx *ctx)
|
|
static void nu_hwcrypto_destroy(struct rt_hwcrypto_ctx *ctx)
|
|
{
|
|
{
|
|
|
|
+ RT_ASSERT(ctx != RT_NULL);
|
|
|
|
+
|
|
if (ctx->contex)
|
|
if (ctx->contex)
|
|
rt_free(ctx->contex);
|
|
rt_free(ctx->contex);
|
|
}
|
|
}
|
|
@@ -965,6 +934,8 @@ static void nu_hwcrypto_destroy(struct rt_hwcrypto_ctx *ctx)
|
|
static rt_err_t nu_hwcrypto_clone(struct rt_hwcrypto_ctx *des, const struct rt_hwcrypto_ctx *src)
|
|
static rt_err_t nu_hwcrypto_clone(struct rt_hwcrypto_ctx *des, const struct rt_hwcrypto_ctx *src)
|
|
{
|
|
{
|
|
rt_err_t res = RT_EOK;
|
|
rt_err_t res = RT_EOK;
|
|
|
|
+ RT_ASSERT(des != RT_NULL);
|
|
|
|
+ RT_ASSERT(src != RT_NULL);
|
|
|
|
|
|
if (des->contex && src->contex)
|
|
if (des->contex && src->contex)
|
|
{
|
|
{
|
|
@@ -1015,8 +986,8 @@ static void nu_hwcrypto_reset(struct rt_hwcrypto_ctx *ctx)
|
|
}
|
|
}
|
|
break;
|
|
break;
|
|
}
|
|
}
|
|
-#endif
|
|
|
|
|
|
|
|
|
|
+#endif
|
|
default:
|
|
default:
|
|
break;
|
|
break;
|
|
}
|
|
}
|
|
@@ -1026,6 +997,7 @@ static void nu_hwcrypto_reset(struct rt_hwcrypto_ctx *ctx)
|
|
|
|
|
|
int nu_hwcrypto_device_init(void)
|
|
int nu_hwcrypto_device_init(void)
|
|
{
|
|
{
|
|
|
|
+ rt_err_t result;
|
|
static struct rt_hwcrypto_device nu_hwcrypto_dev;
|
|
static struct rt_hwcrypto_device nu_hwcrypto_dev;
|
|
|
|
|
|
nu_hwcrypto_dev.ops = &nu_hwcrypto_ops;
|
|
nu_hwcrypto_dev.ops = &nu_hwcrypto_ops;
|
|
@@ -1044,11 +1016,9 @@ int nu_hwcrypto_device_init(void)
|
|
nu_trng_init();
|
|
nu_trng_init();
|
|
#endif
|
|
#endif
|
|
|
|
|
|
- // register hwcrypto operation
|
|
|
|
- if (rt_hwcrypto_register(&nu_hwcrypto_dev, RT_HWCRYPTO_DEFAULT_NAME) != RT_EOK)
|
|
|
|
- {
|
|
|
|
- return -1;
|
|
|
|
- }
|
|
|
|
|
|
+ /* register hwcrypto operation */
|
|
|
|
+ result = rt_hwcrypto_register(&nu_hwcrypto_dev, RT_HWCRYPTO_DEFAULT_NAME);
|
|
|
|
+ RT_ASSERT(result == RT_EOK);
|
|
|
|
|
|
return 0;
|
|
return 0;
|
|
}
|
|
}
|