drv_crypto.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  1. /**************************************************************************//**
  2. *
  3. * @copyright (C) 2020 Nuvoton Technology Corp. All rights reserved.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Change Logs:
  8. * Date Author Notes
  9. * 2020-12-4 Wayne First version
  10. *
  11. ******************************************************************************/
  12. #include <rtconfig.h>
  13. #if defined(BSP_USING_CRYPTO) && defined(RT_USING_HWCRYPTO)
  14. #include <rtdevice.h>
  15. #include <rtdbg.h>
  16. #include <board.h>
  17. #include "NuMicro.h"
  18. #include "drv_sys.h"
  19. #include <nu_bitutil.h>
  20. /* Private typedef --------------------------------------------------------------*/
  21. typedef struct
  22. {
  23. uint8_t *pu8SHATempBuf;
  24. uint32_t u32SHATempBufLen;
  25. uint32_t u32DMAMode;
  26. uint32_t u32BlockSize;
  27. } S_SHA_CONTEXT;
  28. /* Private functions ------------------------------------------------------------*/
  29. static rt_err_t nu_hwcrypto_create(struct rt_hwcrypto_ctx *ctx);
  30. static void nu_hwcrypto_destroy(struct rt_hwcrypto_ctx *ctx);
  31. static rt_err_t nu_hwcrypto_clone(struct rt_hwcrypto_ctx *des, const struct rt_hwcrypto_ctx *src);
  32. static void nu_hwcrypto_reset(struct rt_hwcrypto_ctx *ctx);
  33. /* Private variables ------------------------------------------------------------*/
  34. static const struct rt_hwcrypto_ops nu_hwcrypto_ops =
  35. {
  36. .create = nu_hwcrypto_create,
  37. .destroy = nu_hwcrypto_destroy,
  38. .copy = nu_hwcrypto_clone,
  39. .reset = nu_hwcrypto_reset,
  40. };
  41. /* Crypto engine operation ------------------------------------------------------------*/
  42. #define NU_HWCRYPTO_AES_NAME "nu_AES"
  43. #define NU_HWCRYPTO_SHA_NAME "nu_SHA"
  44. #define NU_HWCRYPTO_PRNG_NAME "nu_PRNG"
  45. static struct rt_mutex s_AES_mutex;
  46. static struct rt_mutex s_SHA_mutex;
  47. static struct rt_mutex s_PRNG_mutex;
  48. static void nu_crypto_isr(int vector, void *param)
  49. {
  50. /* Nothing */
  51. }
  52. static rt_err_t nu_aes_crypt_run(
  53. rt_bool_t bEncrypt,
  54. uint32_t u32OpMode,
  55. uint8_t *pu8Key,
  56. uint32_t u32KeySize,
  57. uint8_t *pu8IV,
  58. uint8_t *pu8InData,
  59. uint8_t *pu8OutData,
  60. uint32_t u32DataLen
  61. )
  62. {
  63. uint32_t au32SwapKey[8];
  64. uint32_t au32SwapIV[4];
  65. rt_err_t result;
  66. au32SwapKey[0] = nu_get32_be(&pu8Key[0]);
  67. au32SwapKey[1] = nu_get32_be(&pu8Key[4]);
  68. au32SwapKey[2] = nu_get32_be(&pu8Key[8]);
  69. au32SwapKey[3] = nu_get32_be(&pu8Key[12]);
  70. if ((u32KeySize == AES_KEY_SIZE_192) || (u32KeySize == AES_KEY_SIZE_256))
  71. {
  72. au32SwapKey[4] = nu_get32_be(&pu8Key[16]);
  73. au32SwapKey[5] = nu_get32_be(&pu8Key[20]);
  74. }
  75. if (u32KeySize == AES_KEY_SIZE_256)
  76. {
  77. au32SwapKey[6] = nu_get32_be(&pu8Key[24]);
  78. au32SwapKey[7] = nu_get32_be(&pu8Key[28]);
  79. }
  80. au32SwapIV[0] = nu_get32_be(&pu8IV[0]);
  81. au32SwapIV[1] = nu_get32_be(&pu8IV[4]);
  82. au32SwapIV[2] = nu_get32_be(&pu8IV[8]);
  83. au32SwapIV[3] = nu_get32_be(&pu8IV[12]);
  84. result = rt_mutex_take(&s_AES_mutex, RT_WAITING_FOREVER);
  85. RT_ASSERT(result == RT_EOK);
  86. AES_Open(CRPT, bEncrypt, u32OpMode, u32KeySize, AES_IN_OUT_SWAP);
  87. AES_SetKey(CRPT, (uint32_t *)&au32SwapKey[0], u32KeySize);
  88. AES_SetInitVect(CRPT, (uint32_t *)au32SwapIV);
  89. /* Setup AES DMA Description */
  90. AES_SetDMATransfer(CRPT, (uint32_t)pu8InData, (uint32_t)pu8OutData, u32DataLen);
  91. #if defined(BSP_USING_MMU)
  92. /* Writeback data in dcache to memory before transferring. */
  93. {
  94. /* Flush Src buffer into memory. */
  95. if (pu8InData)
  96. mmu_clean_invalidated_dcache((uint32_t)pu8InData, u32DataLen);
  97. /* Flush Dst buffer into memory. */
  98. if (pu8OutData)
  99. mmu_clean_invalidated_dcache((uint32_t)pu8OutData, u32DataLen);
  100. }
  101. #endif
  102. /* Clear AES interrupt status */
  103. AES_CLR_INT_FLAG(CRPT);
  104. /* Start AES encryption/decryption */
  105. AES_Start(CRPT, CRYPTO_DMA_ONE_SHOT);
  106. /* Wait done */
  107. while (!(CRPT->INTSTS & CRPT_INTEN_AESIEN_Msk)) {};
  108. if ((u32DataLen % 16) && (CRPT->AES_STS & (CRPT_AES_STS_OUTBUFEMPTY_Msk | CRPT_AES_STS_INBUFEMPTY_Msk)))
  109. rt_kprintf("AES WARNING - AES Data length(%d) is not enough. -> %d \n", u32DataLen, RT_ALIGN(u32DataLen, 16));
  110. else if (CRPT->INTSTS & (CRPT_INTSTS_AESEIF_Msk) || (CRPT->AES_STS & (CRPT_AES_STS_BUSERR_Msk | CRPT_AES_STS_CNTERR_Msk)))
  111. rt_kprintf("AES ERROR - CRPT->INTSTS-%08x, CRPT->AES_STS-%08x\n", CRPT->INTSTS, CRPT->AES_STS);
  112. /* Clear AES interrupt status */
  113. AES_CLR_INT_FLAG(CRPT);
  114. result = rt_mutex_release(&s_AES_mutex);
  115. RT_ASSERT(result == RT_EOK);
  116. return RT_EOK;
  117. }
  118. //Using PRNG instead of TRNG
  119. static void nu_prng_open(uint32_t u32Seed)
  120. {
  121. rt_err_t result;
  122. result = rt_mutex_take(&s_PRNG_mutex, RT_WAITING_FOREVER);
  123. RT_ASSERT(result == RT_EOK);
  124. //Open PRNG 64 bits. But always return 32 bits
  125. PRNG_Open(CRPT, PRNG_KEY_SIZE_64, PRNG_SEED_RELOAD, u32Seed);
  126. result = rt_mutex_release(&s_PRNG_mutex);
  127. RT_ASSERT(result == RT_EOK);
  128. }
  129. static rt_uint32_t nu_prng_run(void)
  130. {
  131. uint32_t au32RNGValue[2];
  132. rt_err_t result;
  133. result = rt_mutex_take(&s_PRNG_mutex, RT_WAITING_FOREVER);
  134. RT_ASSERT(result == RT_EOK);
  135. PRNG_Start(CRPT);
  136. while ((CRPT->PRNG_CTL & CRPT_PRNG_CTL_BUSY_Msk)) {};
  137. /* Clear PRNG interrupt status */
  138. PRNG_CLR_INT_FLAG(CRPT);
  139. PRNG_Read(CRPT, &au32RNGValue[0]);
  140. result = rt_mutex_release(&s_PRNG_mutex);
  141. RT_ASSERT(result == RT_EOK);
  142. return au32RNGValue[0];
  143. }
  144. static rt_err_t nu_aes_crypt(struct hwcrypto_symmetric *symmetric_ctx, struct hwcrypto_symmetric_info *symmetric_info)
  145. {
  146. uint32_t u32AESOpMode;
  147. uint32_t u32AESKeySize;
  148. unsigned char *in, *out;
  149. unsigned char in_align_flag = 0;
  150. unsigned char out_align_flag = 0;
  151. unsigned char iv_temp[16];
  152. RT_ASSERT(symmetric_ctx != RT_NULL);
  153. RT_ASSERT(symmetric_info != RT_NULL);
  154. if ((symmetric_info->length % 4) != 0)
  155. {
  156. return -RT_EINVAL;
  157. }
  158. //Checking key length
  159. if (symmetric_ctx->key_bitlen == 128)
  160. {
  161. u32AESKeySize = AES_KEY_SIZE_128;
  162. }
  163. else if (symmetric_ctx->key_bitlen == 192)
  164. {
  165. u32AESKeySize = AES_KEY_SIZE_192;
  166. }
  167. else if (symmetric_ctx->key_bitlen == 256)
  168. {
  169. u32AESKeySize = AES_KEY_SIZE_256;
  170. }
  171. else
  172. {
  173. return -RT_EINVAL;
  174. }
  175. //Select AES operation mode
  176. switch (symmetric_ctx->parent.type & (HWCRYPTO_MAIN_TYPE_MASK | HWCRYPTO_SUB_TYPE_MASK))
  177. {
  178. case HWCRYPTO_TYPE_AES_ECB:
  179. u32AESOpMode = AES_MODE_ECB;
  180. break;
  181. case HWCRYPTO_TYPE_AES_CBC:
  182. u32AESOpMode = AES_MODE_CBC;
  183. break;
  184. case HWCRYPTO_TYPE_AES_CFB:
  185. u32AESOpMode = AES_MODE_CFB;
  186. break;
  187. case HWCRYPTO_TYPE_AES_OFB:
  188. u32AESOpMode = AES_MODE_OFB;
  189. break;
  190. case HWCRYPTO_TYPE_AES_CTR:
  191. u32AESOpMode = AES_MODE_CTR;
  192. break;
  193. default :
  194. return -RT_ERROR;
  195. }
  196. in = (unsigned char *)symmetric_info->in;
  197. out = (unsigned char *)symmetric_info->out;
  198. //Checking in/out data buffer address not alignment or out of SRAM
  199. if (((rt_uint32_t)in % 4) != 0)
  200. {
  201. in = rt_malloc(symmetric_info->length);
  202. if (in == RT_NULL)
  203. {
  204. LOG_E("fun[%s] memory allocate %d bytes failed!", __FUNCTION__, symmetric_info->length);
  205. return -RT_ENOMEM;
  206. }
  207. rt_memcpy(in, symmetric_info->in, symmetric_info->length);
  208. in_align_flag = 1;
  209. }
  210. if (((rt_uint32_t)out % 4) != 0)
  211. {
  212. out = rt_malloc(symmetric_info->length);
  213. if (out == RT_NULL)
  214. {
  215. if (in_align_flag)
  216. rt_free(in);
  217. LOG_E("fun[%s] memory allocate %d bytes failed!", __FUNCTION__, symmetric_info->length);
  218. return -RT_ENOMEM;
  219. }
  220. out_align_flag = 1;
  221. }
  222. if ((u32AESOpMode == AES_MODE_CBC) && (symmetric_info->mode == HWCRYPTO_MODE_DECRYPT))
  223. {
  224. uint32_t loop;
  225. loop = (symmetric_info->length - 1) / 16;
  226. rt_memcpy(iv_temp, in + (loop * 16), 16);
  227. }
  228. nu_aes_crypt_run(symmetric_info->mode == HWCRYPTO_MODE_ENCRYPT ? TRUE : FALSE, u32AESOpMode, symmetric_ctx->key, u32AESKeySize, symmetric_ctx->iv, in, out, symmetric_info->length);
  229. if (u32AESOpMode == AES_MODE_CBC)
  230. {
  231. if (symmetric_info->mode == HWCRYPTO_MODE_DECRYPT)
  232. {
  233. rt_memcpy(symmetric_ctx->iv, iv_temp, 16);
  234. }
  235. else
  236. {
  237. uint32_t loop;
  238. loop = (symmetric_info->length - 1) / 16;
  239. rt_memcpy(symmetric_ctx->iv, out + (loop * 16), 16);
  240. }
  241. }
  242. if (out_align_flag)
  243. {
  244. rt_memcpy(symmetric_info->out, out, symmetric_info->length);
  245. rt_free(out);
  246. }
  247. if (in_align_flag)
  248. {
  249. rt_free(in);
  250. }
  251. return RT_EOK;
  252. }
  253. static void SHABlockUpdate(uint32_t u32OpMode, uint32_t u32SrcAddr, uint32_t u32Len, uint32_t u32Mode)
  254. {
  255. SHA_Open(CRPT, u32OpMode, SHA_IN_OUT_SWAP, 0);
  256. //Setup SHA DMA
  257. SHA_SetDMATransfer(CRPT, u32SrcAddr, u32Len);
  258. if (u32Mode == CRYPTO_DMA_FIRST)
  259. {
  260. u32Mode = CRYPTO_DMA_CONTINUE;
  261. }
  262. #if defined(BSP_USING_MMU)
  263. /* Writeback data in dcache to memory before transferring. */
  264. {
  265. /* Flush Src buffer into memory. */
  266. if (u32SrcAddr)
  267. mmu_clean_invalidated_dcache(u32SrcAddr, u32Len);
  268. }
  269. #endif
  270. //Start SHA
  271. SHA_CLR_INT_FLAG(CRPT);
  272. SHA_Start(CRPT, u32Mode);
  273. /* Wait done */
  274. while (!(CRPT->INTSTS & CRPT_INTSTS_HMACIF_Msk)) {};
  275. if (CRPT->INTSTS & (CRPT_INTSTS_HMACEIF_Msk) || (CRPT->HMAC_STS & (CRPT_HMAC_STS_DMAERR_Msk)))
  276. rt_kprintf("SHA ERROR - CRPT->INTSTS-%08x, CRPT->HMAC_STS-%08x\n", CRPT->INTSTS, CRPT->HMAC_STS);
  277. /* Clear SHA interrupt status */
  278. SHA_CLR_INT_FLAG(CRPT);
  279. }
  280. static rt_err_t nu_sha_hash_run(
  281. S_SHA_CONTEXT *psSHACtx,
  282. uint32_t u32OpMode,
  283. uint8_t *pu8InData,
  284. uint32_t u32DataLen
  285. )
  286. {
  287. rt_err_t result;
  288. RT_ASSERT(psSHACtx != RT_NULL);
  289. RT_ASSERT(pu8InData != RT_NULL);
  290. result = rt_mutex_take(&s_SHA_mutex, RT_WAITING_FOREVER);
  291. RT_ASSERT(result == RT_EOK);
  292. uint8_t *pu8SrcAddr = (uint8_t *)pu8InData;
  293. uint32_t u32CopyLen = 0;
  294. while ((psSHACtx->u32SHATempBufLen + u32DataLen) > psSHACtx->u32BlockSize)
  295. {
  296. if (psSHACtx->pu8SHATempBuf)
  297. {
  298. if (psSHACtx->u32SHATempBufLen == psSHACtx->u32BlockSize)
  299. {
  300. //Trigger SHA block update
  301. SHABlockUpdate(u32OpMode, (uint32_t)psSHACtx->pu8SHATempBuf, psSHACtx->u32BlockSize, psSHACtx->u32DMAMode);
  302. psSHACtx->u32DMAMode = CRYPTO_DMA_CONTINUE;
  303. //free SHATempBuff
  304. rt_free(psSHACtx->pu8SHATempBuf);
  305. psSHACtx->pu8SHATempBuf = NULL;
  306. psSHACtx->u32SHATempBufLen = 0;
  307. continue;
  308. }
  309. else
  310. {
  311. u32CopyLen = psSHACtx->u32BlockSize - psSHACtx->u32SHATempBufLen;
  312. if (u32DataLen < u32CopyLen)
  313. u32CopyLen = u32DataLen;
  314. rt_memcpy(psSHACtx->pu8SHATempBuf + psSHACtx->u32SHATempBufLen, pu8SrcAddr, u32CopyLen);
  315. psSHACtx->u32SHATempBufLen += u32CopyLen;
  316. pu8SrcAddr += u32CopyLen;
  317. u32DataLen -= u32CopyLen;
  318. continue;
  319. }
  320. }
  321. if ((uint32_t) pu8SrcAddr & 3) //address not aligned 4
  322. {
  323. psSHACtx->pu8SHATempBuf = rt_malloc(psSHACtx->u32BlockSize);
  324. if (psSHACtx->pu8SHATempBuf == RT_NULL)
  325. {
  326. LOG_E("fun[%s] memory allocate %d bytes failed!", __FUNCTION__, psSHACtx->u32BlockSize);
  327. result = rt_mutex_release(&s_SHA_mutex);
  328. RT_ASSERT(result == RT_EOK);
  329. return -RT_ENOMEM;
  330. }
  331. rt_memcpy(psSHACtx->pu8SHATempBuf, pu8SrcAddr, psSHACtx->u32BlockSize);
  332. psSHACtx->u32SHATempBufLen = psSHACtx->u32BlockSize;
  333. pu8SrcAddr += psSHACtx->u32BlockSize;
  334. u32DataLen -= psSHACtx->u32BlockSize;
  335. continue;
  336. }
  337. //Trigger SHA block update
  338. SHABlockUpdate(u32OpMode, (uint32_t)pu8SrcAddr, psSHACtx->u32BlockSize, psSHACtx->u32DMAMode);
  339. psSHACtx->u32DMAMode = CRYPTO_DMA_CONTINUE;
  340. pu8SrcAddr += psSHACtx->u32BlockSize;
  341. u32DataLen -= psSHACtx->u32BlockSize;
  342. }
  343. if (u32DataLen)
  344. {
  345. if (psSHACtx->pu8SHATempBuf == NULL)
  346. {
  347. psSHACtx->pu8SHATempBuf = rt_malloc(psSHACtx->u32BlockSize);
  348. if (psSHACtx->pu8SHATempBuf == RT_NULL)
  349. {
  350. LOG_E("fun[%s] memory allocate %d bytes failed!", __FUNCTION__, psSHACtx->u32BlockSize);
  351. result = rt_mutex_release(&s_SHA_mutex);
  352. RT_ASSERT(result == RT_EOK);
  353. return -RT_ENOMEM;
  354. }
  355. psSHACtx->u32SHATempBufLen = 0;
  356. }
  357. rt_memcpy(psSHACtx->pu8SHATempBuf, pu8SrcAddr, u32DataLen);
  358. psSHACtx->u32SHATempBufLen += u32DataLen;
  359. }
  360. result = rt_mutex_release(&s_SHA_mutex);
  361. RT_ASSERT(result == RT_EOK);
  362. return RT_EOK;
  363. }
  364. static rt_err_t nu_sha_update(struct hwcrypto_hash *hash_ctx, const rt_uint8_t *in, rt_size_t length)
  365. {
  366. uint32_t u32SHAOpMode;
  367. unsigned char *nu_in;
  368. unsigned char in_align_flag = 0;
  369. RT_ASSERT(hash_ctx != RT_NULL);
  370. RT_ASSERT(in != RT_NULL);
  371. //Select SHA operation mode
  372. switch (hash_ctx->parent.type & (HWCRYPTO_MAIN_TYPE_MASK | HWCRYPTO_SUB_TYPE_MASK))
  373. {
  374. case HWCRYPTO_TYPE_SHA1:
  375. u32SHAOpMode = SHA_MODE_SHA1;
  376. break;
  377. case HWCRYPTO_TYPE_SHA224:
  378. u32SHAOpMode = SHA_MODE_SHA224;
  379. break;
  380. case HWCRYPTO_TYPE_SHA256:
  381. u32SHAOpMode = SHA_MODE_SHA256;
  382. break;
  383. case HWCRYPTO_TYPE_SHA384:
  384. u32SHAOpMode = SHA_MODE_SHA384;
  385. break;
  386. case HWCRYPTO_TYPE_SHA512:
  387. u32SHAOpMode = SHA_MODE_SHA512;
  388. break;
  389. default :
  390. return -RT_ERROR;
  391. }
  392. nu_in = (unsigned char *)in;
  393. //Checking in data buffer address not alignment or out of SRAM
  394. if (((rt_uint32_t)nu_in % 4) != 0)
  395. {
  396. nu_in = rt_malloc(length);
  397. if (nu_in == RT_NULL)
  398. {
  399. LOG_E("fun[%s] memory allocate %d bytes failed!", __FUNCTION__, length);
  400. return -RT_ENOMEM;
  401. }
  402. rt_memcpy(nu_in, in, length);
  403. in_align_flag = 1;
  404. }
  405. nu_sha_hash_run(hash_ctx->parent.contex, u32SHAOpMode, nu_in, length);
  406. if (in_align_flag)
  407. {
  408. rt_free(nu_in);
  409. }
  410. return RT_EOK;
  411. }
  412. static rt_err_t nu_sha_finish(struct hwcrypto_hash *hash_ctx, rt_uint8_t *out, rt_size_t length)
  413. {
  414. unsigned char *nu_out;
  415. unsigned char out_align_flag = 0;
  416. uint32_t u32SHAOpMode;
  417. S_SHA_CONTEXT *psSHACtx = RT_NULL;
  418. RT_ASSERT(hash_ctx != RT_NULL);
  419. RT_ASSERT(out != RT_NULL);
  420. psSHACtx = hash_ctx->parent.contex;
  421. //Check SHA Hash value buffer length
  422. switch (hash_ctx->parent.type & (HWCRYPTO_MAIN_TYPE_MASK | HWCRYPTO_SUB_TYPE_MASK))
  423. {
  424. case HWCRYPTO_TYPE_SHA1:
  425. u32SHAOpMode = SHA_MODE_SHA1;
  426. if (length < 5UL)
  427. {
  428. return -RT_EINVAL;
  429. }
  430. break;
  431. case HWCRYPTO_TYPE_SHA224:
  432. u32SHAOpMode = SHA_MODE_SHA224;
  433. if (length < 7UL)
  434. {
  435. return -RT_EINVAL;
  436. }
  437. break;
  438. case HWCRYPTO_TYPE_SHA256:
  439. u32SHAOpMode = SHA_MODE_SHA256;
  440. if (length < 8UL)
  441. {
  442. return -RT_EINVAL;
  443. }
  444. break;
  445. case HWCRYPTO_TYPE_SHA384:
  446. u32SHAOpMode = SHA_MODE_SHA384;
  447. if (length < 12UL)
  448. {
  449. return -RT_EINVAL;
  450. }
  451. break;
  452. case HWCRYPTO_TYPE_SHA512:
  453. u32SHAOpMode = SHA_MODE_SHA512;
  454. if (length < 16UL)
  455. {
  456. return -RT_EINVAL;
  457. }
  458. break;
  459. default :
  460. return -RT_ERROR;
  461. }
  462. nu_out = (unsigned char *)out;
  463. //Checking out data buffer address alignment or not
  464. if (((rt_uint32_t)nu_out % 4) != 0)
  465. {
  466. nu_out = rt_malloc(length);
  467. if (nu_out == RT_NULL)
  468. {
  469. LOG_E("fun[%s] memory allocate %d bytes failed!", __FUNCTION__, length);
  470. return -RT_ENOMEM;
  471. }
  472. out_align_flag = 1;
  473. }
  474. if (psSHACtx->pu8SHATempBuf)
  475. {
  476. if (psSHACtx->u32DMAMode == CRYPTO_DMA_FIRST)
  477. SHABlockUpdate(u32SHAOpMode, (uint32_t)psSHACtx->pu8SHATempBuf, psSHACtx->u32SHATempBufLen, CRYPTO_DMA_ONE_SHOT);
  478. else
  479. SHABlockUpdate(u32SHAOpMode, (uint32_t)psSHACtx->pu8SHATempBuf, psSHACtx->u32SHATempBufLen, CRYPTO_DMA_LAST);
  480. //free SHATempBuf
  481. rt_free(psSHACtx->pu8SHATempBuf);
  482. psSHACtx->pu8SHATempBuf = RT_NULL;
  483. psSHACtx->u32SHATempBufLen = 0;
  484. }
  485. else
  486. {
  487. SHABlockUpdate(u32SHAOpMode, (uint32_t)NULL, 0, CRYPTO_DMA_LAST);
  488. }
  489. SHA_Read(CRPT, (uint32_t *)nu_out);
  490. if (out_align_flag)
  491. {
  492. rt_memcpy(out, nu_out, length);
  493. rt_free(nu_out);
  494. }
  495. return RT_EOK;
  496. }
  497. static rt_uint32_t nu_prng_rand(struct hwcrypto_rng *ctx)
  498. {
  499. return nu_prng_run();
  500. }
  501. static const struct hwcrypto_symmetric_ops nu_aes_ops =
  502. {
  503. .crypt = nu_aes_crypt,
  504. };
  505. static const struct hwcrypto_hash_ops nu_sha_ops =
  506. {
  507. .update = nu_sha_update,
  508. .finish = nu_sha_finish,
  509. };
  510. /* PRNG operation ------------------------------------------------------------*/
  511. static const struct hwcrypto_rng_ops nu_rng_ops =
  512. {
  513. .update = nu_prng_rand,
  514. };
  515. /* Register crypto interface ----------------------------------------------------------*/
  516. static rt_err_t nu_hwcrypto_create(struct rt_hwcrypto_ctx *ctx)
  517. {
  518. rt_err_t res = RT_EOK;
  519. RT_ASSERT(ctx != RT_NULL);
  520. switch (ctx->type & HWCRYPTO_MAIN_TYPE_MASK)
  521. {
  522. case HWCRYPTO_TYPE_AES:
  523. {
  524. ctx->contex = RT_NULL;
  525. //Setup AES operation
  526. ((struct hwcrypto_symmetric *)ctx)->ops = &nu_aes_ops;
  527. break;
  528. }
  529. case HWCRYPTO_TYPE_SHA1:
  530. case HWCRYPTO_TYPE_SHA2:
  531. {
  532. ctx->contex = rt_malloc(sizeof(S_SHA_CONTEXT));
  533. if (ctx->contex == RT_NULL)
  534. return -RT_ERROR;
  535. rt_memset(ctx->contex, 0, sizeof(S_SHA_CONTEXT));
  536. //Setup SHA2 operation
  537. ((struct hwcrypto_hash *)ctx)->ops = &nu_sha_ops;
  538. break;
  539. }
  540. case HWCRYPTO_TYPE_RNG:
  541. {
  542. ctx->contex = RT_NULL;
  543. ((struct hwcrypto_rng *)ctx)->ops = &nu_rng_ops;
  544. #if defined(NU_PRNG_USE_SEED)
  545. nu_prng_open(NU_PRNG_SEED_VALUE);
  546. #else
  547. nu_prng_open(rt_tick_get());
  548. #endif
  549. break;
  550. }
  551. default:
  552. res = -RT_ERROR;
  553. break;
  554. }
  555. return res;
  556. }
  557. static void nu_hwcrypto_destroy(struct rt_hwcrypto_ctx *ctx)
  558. {
  559. RT_ASSERT(ctx != RT_NULL);
  560. if (ctx->contex)
  561. rt_free(ctx->contex);
  562. }
  563. static rt_err_t nu_hwcrypto_clone(struct rt_hwcrypto_ctx *des, const struct rt_hwcrypto_ctx *src)
  564. {
  565. rt_err_t res = RT_EOK;
  566. RT_ASSERT(des != RT_NULL);
  567. RT_ASSERT(src != RT_NULL);
  568. if (des->contex && src->contex)
  569. {
  570. rt_memcpy(des->contex, src->contex, sizeof(struct rt_hwcrypto_ctx));
  571. }
  572. else
  573. return -RT_EINVAL;
  574. return res;
  575. }
  576. static void nu_hwcrypto_reset(struct rt_hwcrypto_ctx *ctx)
  577. {
  578. switch (ctx->type & HWCRYPTO_MAIN_TYPE_MASK)
  579. {
  580. case HWCRYPTO_TYPE_RNG:
  581. {
  582. #if defined(NU_PRNG_USE_SEED)
  583. nu_prng_open(NU_PRNG_SEED_VALUE);
  584. #else
  585. nu_prng_open(rt_tick_get());
  586. #endif
  587. break;
  588. }
  589. case HWCRYPTO_TYPE_SHA1:
  590. case HWCRYPTO_TYPE_SHA2:
  591. {
  592. S_SHA_CONTEXT *psSHACtx = (S_SHA_CONTEXT *)ctx->contex;
  593. if (psSHACtx->pu8SHATempBuf)
  594. {
  595. rt_free(psSHACtx->pu8SHATempBuf);
  596. }
  597. psSHACtx->pu8SHATempBuf = RT_NULL;
  598. psSHACtx->u32SHATempBufLen = 0;
  599. psSHACtx->u32DMAMode = CRYPTO_DMA_FIRST;
  600. if ((ctx->type == HWCRYPTO_TYPE_SHA384) || (ctx->type == HWCRYPTO_TYPE_SHA512))
  601. {
  602. psSHACtx->u32BlockSize = 128;
  603. }
  604. else
  605. {
  606. psSHACtx->u32BlockSize = 64;
  607. }
  608. break;
  609. }
  610. default:
  611. break;
  612. }
  613. }
  614. /* Init and register nu_hwcrypto_dev */
  615. int nu_hwcrypto_device_init(void)
  616. {
  617. rt_err_t result;
  618. static struct rt_hwcrypto_device nu_hwcrypto_dev;
  619. nu_hwcrypto_dev.ops = &nu_hwcrypto_ops;
  620. nu_hwcrypto_dev.id = 0;
  621. nu_hwcrypto_dev.user_data = &nu_hwcrypto_dev;
  622. nu_sys_ipclk_enable(CRYPTOCKEN);
  623. nu_sys_ip_reset(CRYPTORST);
  624. /* init cipher mutex */
  625. #if defined(RT_HWCRYPTO_USING_AES)
  626. result = rt_mutex_init(&s_AES_mutex, NU_HWCRYPTO_AES_NAME, RT_IPC_FLAG_FIFO);
  627. RT_ASSERT(result == RT_EOK);
  628. AES_ENABLE_INT(CRPT);
  629. #endif
  630. #if defined(RT_HWCRYPTO_USING_SHA1) || defined(RT_HWCRYPTO_USING_SHA2)
  631. result = rt_mutex_init(&s_SHA_mutex, NU_HWCRYPTO_SHA_NAME, RT_IPC_FLAG_FIFO);
  632. RT_ASSERT(result == RT_EOK);
  633. SHA_ENABLE_INT(CRPT);
  634. #endif
  635. #if defined(RT_HWCRYPTO_USING_RNG)
  636. result = rt_mutex_init(&s_PRNG_mutex, NU_HWCRYPTO_PRNG_NAME, RT_IPC_FLAG_FIFO);
  637. RT_ASSERT(result == RT_EOK);
  638. #endif
  639. /* register hwcrypto operation */
  640. result = rt_hwcrypto_register(&nu_hwcrypto_dev, RT_HWCRYPTO_DEFAULT_NAME);
  641. RT_ASSERT(result == RT_EOK);
  642. /* Enable Crypto engine interrupt */
  643. rt_hw_interrupt_install(IRQ_CRYPTO, nu_crypto_isr, RT_NULL, "crypto");
  644. return 0;
  645. }
  646. INIT_DEVICE_EXPORT(nu_hwcrypto_device_init);
  647. #endif //#if defined(BSP_USING_CRYPTO) && defined(RT_USING_HWCRYPTO)