drv_crypto.c 21 KB

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