drv_crypto.c 21 KB

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