drv_crypto.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  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. * 2022-3-15 Wayne First version
  10. *
  11. ******************************************************************************/
  12. #include <rtconfig.h>
  13. #if ((defined(BSP_USING_CRYPTO) || defined(BSP_USING_TRNG) || defined(BSP_USING_CRC)) && defined(RT_USING_HWCRYPTO))
  14. #include <rtdevice.h>
  15. #include <board.h>
  16. #include "NuMicro.h"
  17. #include <nu_bitutil.h>
  18. #if defined(BSP_USING_TRNG)
  19. #include "drv_trng.h"
  20. #endif
  21. #if defined(BSP_USING_CRC)
  22. #include "drv_crc.h"
  23. #endif
  24. /* Private typedef --------------------------------------------------------------*/
  25. #define LOG_TAG "CRYPTO"
  26. #define DBG_ENABLE
  27. #define DBG_SECTION_NAME "CRYPTO"
  28. #define DBG_LEVEL DBG_INFO
  29. #define DBG_COLOR
  30. #include <rtdbg.h>
  31. typedef struct
  32. {
  33. uint8_t *pu8SHATempBuf;
  34. uint32_t u32SHATempBufLen;
  35. uint32_t u32DMAMode;
  36. uint32_t u32BlockSize;
  37. } S_SHA_CONTEXT;
  38. /* Private functions ------------------------------------------------------------*/
  39. static rt_err_t nu_hwcrypto_create(struct rt_hwcrypto_ctx *ctx);
  40. static void nu_hwcrypto_destroy(struct rt_hwcrypto_ctx *ctx);
  41. static rt_err_t nu_hwcrypto_clone(struct rt_hwcrypto_ctx *des, const struct rt_hwcrypto_ctx *src);
  42. static void nu_hwcrypto_reset(struct rt_hwcrypto_ctx *ctx);
  43. /* Private variables ------------------------------------------------------------*/
  44. static const struct rt_hwcrypto_ops nu_hwcrypto_ops =
  45. {
  46. .create = nu_hwcrypto_create,
  47. .destroy = nu_hwcrypto_destroy,
  48. .copy = nu_hwcrypto_clone,
  49. .reset = nu_hwcrypto_reset,
  50. };
  51. /* Crypto engine operation ------------------------------------------------------------*/
  52. #if defined(BSP_USING_CRYPTO)
  53. #define NU_HWCRYPTO_AES_NAME "nu_AES"
  54. #define NU_HWCRYPTO_SHA_NAME "nu_SHA"
  55. #define NU_HWCRYPTO_PRNG_NAME "nu_PRNG"
  56. static struct rt_mutex s_AES_mutex;
  57. static struct rt_mutex s_SHA_mutex;
  58. static rt_err_t nu_crypto_init(void)
  59. {
  60. rt_err_t result = RT_EOK;
  61. /* init cipher mutex */
  62. #if defined(RT_HWCRYPTO_USING_AES)
  63. result = rt_mutex_init(&s_AES_mutex, NU_HWCRYPTO_AES_NAME, RT_IPC_FLAG_PRIO);
  64. RT_ASSERT(result == RT_EOK);
  65. AES_ENABLE_INT(CRPT);
  66. #endif
  67. #if defined(RT_HWCRYPTO_USING_SHA1) || defined(RT_HWCRYPTO_USING_SHA2)
  68. result = rt_mutex_init(&s_SHA_mutex, NU_HWCRYPTO_SHA_NAME, RT_IPC_FLAG_PRIO);
  69. RT_ASSERT(result == RT_EOK);
  70. SHA_ENABLE_INT(CRPT);
  71. #endif
  72. return result;
  73. }
  74. static rt_err_t nu_aes_crypt_run(
  75. rt_bool_t bEncrypt,
  76. uint32_t u32OpMode,
  77. uint8_t *pu8Key,
  78. uint32_t u32KeySize,
  79. uint8_t *pu8IV,
  80. uint8_t *pu8InData,
  81. uint8_t *pu8OutData,
  82. uint32_t u32DataLen
  83. )
  84. {
  85. uint32_t au32SwapKey[8];
  86. uint32_t au32SwapIV[4];
  87. rt_err_t result;
  88. au32SwapKey[0] = nu_get32_be(&pu8Key[0]);
  89. au32SwapKey[1] = nu_get32_be(&pu8Key[4]);
  90. au32SwapKey[2] = nu_get32_be(&pu8Key[8]);
  91. au32SwapKey[3] = nu_get32_be(&pu8Key[12]);
  92. if ((u32KeySize == AES_KEY_SIZE_192) || (u32KeySize == AES_KEY_SIZE_256))
  93. {
  94. au32SwapKey[4] = nu_get32_be(&pu8Key[16]);
  95. au32SwapKey[5] = nu_get32_be(&pu8Key[20]);
  96. }
  97. if (u32KeySize == AES_KEY_SIZE_256)
  98. {
  99. au32SwapKey[6] = nu_get32_be(&pu8Key[24]);
  100. au32SwapKey[7] = nu_get32_be(&pu8Key[28]);
  101. }
  102. au32SwapIV[0] = nu_get32_be(&pu8IV[0]);
  103. au32SwapIV[1] = nu_get32_be(&pu8IV[4]);
  104. au32SwapIV[2] = nu_get32_be(&pu8IV[8]);
  105. au32SwapIV[3] = nu_get32_be(&pu8IV[12]);
  106. result = rt_mutex_take(&s_AES_mutex, RT_WAITING_FOREVER);
  107. RT_ASSERT(result == RT_EOK);
  108. //Using Channel 0
  109. AES_Open(CRPT, 0, bEncrypt, u32OpMode, u32KeySize, AES_IN_OUT_SWAP);
  110. AES_SetKey(CRPT, 0, (uint32_t *)&au32SwapKey[0], u32KeySize);
  111. AES_SetInitVect(CRPT, 0, (uint32_t *)au32SwapIV);
  112. //Setup AES DMA
  113. AES_SetDMATransfer(CRPT, 0, (uint32_t)pu8InData, (uint32_t)pu8OutData, u32DataLen);
  114. AES_CLR_INT_FLAG(CRPT);
  115. /* Start AES encryption/decryption */
  116. AES_Start(CRPT, 0, CRYPTO_DMA_ONE_SHOT);
  117. /* Wait done */
  118. while (!(CRPT->INTSTS & CRPT_INTEN_AESIEN_Msk)) {};
  119. if ((u32DataLen % 16) && (CRPT->AES_STS & (CRPT_AES_STS_OUTBUFEMPTY_Msk | CRPT_AES_STS_INBUFEMPTY_Msk)))
  120. rt_kprintf("AES WARNING - AES Data length(%d) is not enough. -> %d \n", u32DataLen, RT_ALIGN(u32DataLen, 16));
  121. else if (CRPT->INTSTS & (CRPT_INTSTS_AESEIF_Msk) || (CRPT->AES_STS & (CRPT_AES_STS_BUSERR_Msk | CRPT_AES_STS_CNTERR_Msk)))
  122. rt_kprintf("AES ERROR - CRPT->INTSTS-%08x, CRPT->AES_STS-%08x\n", CRPT->INTSTS, CRPT->AES_STS);
  123. /* Clear AES interrupt status */
  124. AES_CLR_INT_FLAG(CRPT);
  125. result = rt_mutex_release(&s_AES_mutex);
  126. RT_ASSERT(result == RT_EOK);
  127. return RT_EOK;
  128. }
  129. static rt_err_t nu_prng_init(void)
  130. {
  131. uint32_t u32Seed;
  132. #if defined(NU_PRNG_USE_SEED)
  133. u32Seed = NU_PRNG_SEED_VALUE;
  134. #else
  135. u32Seed = (uint32_t)rt_tick_get();
  136. #endif
  137. //Open PRNG 128 bits.
  138. PRNG_Open(CRPT, PRNG_KEY_SIZE_128, PRNG_SEED_RELOAD, u32Seed);
  139. return RT_EOK;
  140. }
  141. static rt_uint32_t nu_prng_rand(struct hwcrypto_rng *ctx)
  142. {
  143. uint32_t au32RNGValue[4];
  144. PRNG_Start(CRPT);
  145. PRNG_Read(CRPT, &au32RNGValue[0]);
  146. return au32RNGValue[0] ^ au32RNGValue[1] ^ au32RNGValue[2] ^ au32RNGValue[3];
  147. }
  148. static rt_err_t nu_aes_crypt(struct hwcrypto_symmetric *symmetric_ctx, struct hwcrypto_symmetric_info *symmetric_info)
  149. {
  150. uint32_t u32AESOpMode;
  151. uint32_t u32AESKeySize;
  152. unsigned char *in, *out;
  153. unsigned char in_align_flag = 0;
  154. unsigned char out_align_flag = 0;
  155. unsigned char iv_temp[16];
  156. RT_ASSERT(symmetric_ctx != RT_NULL);
  157. RT_ASSERT(symmetric_info != RT_NULL);
  158. if ((symmetric_info->length % 4) != 0)
  159. {
  160. return -RT_EINVAL;
  161. }
  162. //Checking key length
  163. if (symmetric_ctx->key_bitlen == 128)
  164. {
  165. u32AESKeySize = AES_KEY_SIZE_128;
  166. }
  167. else if (symmetric_ctx->key_bitlen == 192)
  168. {
  169. u32AESKeySize = AES_KEY_SIZE_192;
  170. }
  171. else if (symmetric_ctx->key_bitlen == 256)
  172. {
  173. u32AESKeySize = AES_KEY_SIZE_256;
  174. }
  175. else
  176. {
  177. return -RT_EINVAL;
  178. }
  179. //Select AES operation mode
  180. switch (symmetric_ctx->parent.type & (HWCRYPTO_MAIN_TYPE_MASK | HWCRYPTO_SUB_TYPE_MASK))
  181. {
  182. case HWCRYPTO_TYPE_AES_ECB:
  183. u32AESOpMode = AES_MODE_ECB;
  184. break;
  185. case HWCRYPTO_TYPE_AES_CBC:
  186. u32AESOpMode = AES_MODE_CBC;
  187. break;
  188. case HWCRYPTO_TYPE_AES_CFB:
  189. u32AESOpMode = AES_MODE_CFB;
  190. break;
  191. case HWCRYPTO_TYPE_AES_OFB:
  192. u32AESOpMode = AES_MODE_OFB;
  193. break;
  194. case HWCRYPTO_TYPE_AES_CTR:
  195. u32AESOpMode = AES_MODE_CTR;
  196. break;
  197. default :
  198. return -RT_ERROR;
  199. }
  200. in = (unsigned char *)symmetric_info->in;
  201. out = (unsigned char *)symmetric_info->out;
  202. //Checking in/out data buffer address not alignment or out of SRAM
  203. if (((rt_uint32_t)in % 4) != 0 || ((rt_uint32_t)in < SRAM_BASE) || ((rt_uint32_t)in > SRAM_END))
  204. {
  205. in = rt_malloc(symmetric_info->length);
  206. if (in == RT_NULL)
  207. {
  208. LOG_E("fun[%s] memory allocate %d bytes failed!", __FUNCTION__, symmetric_info->length);
  209. return -RT_ENOMEM;
  210. }
  211. rt_memcpy(in, symmetric_info->in, symmetric_info->length);
  212. in_align_flag = 1;
  213. }
  214. if (((rt_uint32_t)out % 4) != 0 || ((rt_uint32_t)out < SRAM_BASE) || ((rt_uint32_t)out > SRAM_END))
  215. {
  216. out = rt_malloc(symmetric_info->length);
  217. if (out == RT_NULL)
  218. {
  219. if (in_align_flag)
  220. rt_free(in);
  221. LOG_E("fun[%s] memory allocate %d bytes failed!", __FUNCTION__, symmetric_info->length);
  222. return -RT_ENOMEM;
  223. }
  224. out_align_flag = 1;
  225. }
  226. if ((u32AESOpMode == AES_MODE_CBC) && (symmetric_info->mode == HWCRYPTO_MODE_DECRYPT))
  227. {
  228. uint32_t loop;
  229. loop = (symmetric_info->length - 1) / 16;
  230. rt_memcpy(iv_temp, in + (loop * 16), 16);
  231. }
  232. 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);
  233. if (u32AESOpMode == AES_MODE_CBC)
  234. {
  235. if (symmetric_info->mode == HWCRYPTO_MODE_DECRYPT)
  236. {
  237. rt_memcpy(symmetric_ctx->iv, iv_temp, 16);
  238. }
  239. else
  240. {
  241. uint32_t loop;
  242. loop = (symmetric_info->length - 1) / 16;
  243. rt_memcpy(symmetric_ctx->iv, out + (loop * 16), 16);
  244. }
  245. }
  246. if (out_align_flag)
  247. {
  248. rt_memcpy(symmetric_info->out, out, symmetric_info->length);
  249. rt_free(out);
  250. }
  251. if (in_align_flag)
  252. {
  253. rt_free(in);
  254. }
  255. return RT_EOK;
  256. }
  257. static void SHABlockUpdate(uint32_t u32OpMode, uint32_t u32SrcAddr, uint32_t u32Len, uint32_t u32Mode)
  258. {
  259. SHA_Open(CRPT, u32OpMode, SHA_IN_OUT_SWAP, 0);
  260. //Setup SHA DMA
  261. SHA_SetDMATransfer(CRPT, u32SrcAddr, u32Len);
  262. if (u32Mode == CRYPTO_DMA_FIRST)
  263. CRPT->HMAC_CTL |= CRPT_HMAC_CTL_DMAFIRST_Msk;
  264. else
  265. CRPT->HMAC_CTL &= ~CRPT_HMAC_CTL_DMAFIRST_Msk;
  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 || ((rt_uint32_t)nu_in < SRAM_BASE) || ((rt_uint32_t)nu_in > SRAM_END))
  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 const struct hwcrypto_symmetric_ops nu_aes_ops =
  494. {
  495. .crypt = nu_aes_crypt,
  496. };
  497. static const struct hwcrypto_hash_ops nu_sha_ops =
  498. {
  499. .update = nu_sha_update,
  500. .finish = nu_sha_finish,
  501. };
  502. #endif
  503. /* CRC operation ------------------------------------------------------------*/
  504. #if defined(BSP_USING_CRC)
  505. static const struct hwcrypto_crc_ops nu_crc_ops =
  506. {
  507. .update = nu_crc_update,
  508. };
  509. #endif
  510. #if defined(RT_HWCRYPTO_USING_RNG)
  511. /* RNG operation ------------------------------------------------------------*/
  512. static struct hwcrypto_rng_ops nu_rng_ops;
  513. #endif
  514. /* Register crypto interface ----------------------------------------------------------*/
  515. static rt_err_t nu_hwcrypto_create(struct rt_hwcrypto_ctx *ctx)
  516. {
  517. rt_err_t res = RT_EOK;
  518. RT_ASSERT(ctx != RT_NULL);
  519. switch (ctx->type & HWCRYPTO_MAIN_TYPE_MASK)
  520. {
  521. #if defined(RT_HWCRYPTO_USING_RNG)
  522. case HWCRYPTO_TYPE_RNG:
  523. {
  524. ctx->contex = RT_NULL;
  525. //Setup RNG operation
  526. ((struct hwcrypto_rng *)ctx)->ops = &nu_rng_ops;
  527. break;
  528. }
  529. #endif /* RT_HWCRYPTO_USING_RNG */
  530. #if defined(BSP_USING_CRC) && defined(RT_HWCRYPTO_USING_CRC)
  531. case HWCRYPTO_TYPE_CRC:
  532. {
  533. ctx->contex = RT_NULL;
  534. //Setup CRC operation
  535. ((struct hwcrypto_crc *)ctx)->ops = &nu_crc_ops;
  536. break;
  537. }
  538. #endif /* BSP_USING_CRC && defined(RT_HWCRYPTO_USING_CRC) */
  539. #if defined(BSP_USING_CRYPTO)
  540. case HWCRYPTO_TYPE_AES:
  541. {
  542. ctx->contex = RT_NULL;
  543. //Setup AES operation
  544. ((struct hwcrypto_symmetric *)ctx)->ops = &nu_aes_ops;
  545. break;
  546. }
  547. case HWCRYPTO_TYPE_SHA1:
  548. case HWCRYPTO_TYPE_SHA2:
  549. {
  550. ctx->contex = rt_malloc(sizeof(S_SHA_CONTEXT));
  551. if (ctx->contex == RT_NULL)
  552. return -RT_ERROR;
  553. rt_memset(ctx->contex, 0, sizeof(S_SHA_CONTEXT));
  554. //Setup operation
  555. ((struct hwcrypto_hash *)ctx)->ops = &nu_sha_ops;
  556. break;
  557. }
  558. #endif /* BSP_USING_CRYPTO */
  559. default:
  560. res = -RT_ERROR;
  561. break;
  562. }
  563. nu_hwcrypto_reset(ctx);
  564. return res;
  565. }
  566. static void nu_hwcrypto_destroy(struct rt_hwcrypto_ctx *ctx)
  567. {
  568. RT_ASSERT(ctx != RT_NULL);
  569. if (ctx->contex)
  570. rt_free(ctx->contex);
  571. }
  572. static rt_err_t nu_hwcrypto_clone(struct rt_hwcrypto_ctx *des, const struct rt_hwcrypto_ctx *src)
  573. {
  574. rt_err_t res = RT_EOK;
  575. RT_ASSERT(des != RT_NULL);
  576. RT_ASSERT(src != RT_NULL);
  577. if (des->contex && src->contex)
  578. {
  579. rt_memcpy(des->contex, src->contex, sizeof(struct rt_hwcrypto_ctx));
  580. }
  581. else
  582. return -RT_EINVAL;
  583. return res;
  584. }
  585. static void nu_hwcrypto_reset(struct rt_hwcrypto_ctx *ctx)
  586. {
  587. switch (ctx->type & HWCRYPTO_MAIN_TYPE_MASK)
  588. {
  589. #if defined(BSP_USING_CRYPTO)
  590. case HWCRYPTO_TYPE_SHA1:
  591. case HWCRYPTO_TYPE_SHA2:
  592. {
  593. S_SHA_CONTEXT *psSHACtx = (S_SHA_CONTEXT *)ctx->contex;
  594. if (psSHACtx->pu8SHATempBuf)
  595. {
  596. rt_free(psSHACtx->pu8SHATempBuf);
  597. }
  598. psSHACtx->pu8SHATempBuf = RT_NULL;
  599. psSHACtx->u32SHATempBufLen = 0;
  600. psSHACtx->u32DMAMode = CRYPTO_DMA_FIRST;
  601. if ((ctx->type == HWCRYPTO_TYPE_SHA384) || (ctx->type == HWCRYPTO_TYPE_SHA512))
  602. {
  603. psSHACtx->u32BlockSize = 128;
  604. }
  605. else
  606. {
  607. psSHACtx->u32BlockSize = 64;
  608. }
  609. break;
  610. }
  611. #endif
  612. default:
  613. break;
  614. }
  615. }
  616. /* Init and register nu_hwcrypto_dev */
  617. int nu_hwcrypto_device_init(void)
  618. {
  619. rt_err_t result;
  620. static struct rt_hwcrypto_device nu_hwcrypto_dev;
  621. nu_hwcrypto_dev.ops = &nu_hwcrypto_ops;
  622. nu_hwcrypto_dev.id = 0;
  623. nu_hwcrypto_dev.user_data = &nu_hwcrypto_dev;
  624. #if defined(BSP_USING_CRYPTO)
  625. nu_crypto_init();
  626. #endif
  627. #if defined(BSP_USING_CRC)
  628. nu_crc_init();
  629. #endif
  630. #if defined(RT_HWCRYPTO_USING_RNG)
  631. #if defined(BSP_USING_TRNG)
  632. result = nu_trng_init();
  633. if (result == RT_EOK)
  634. {
  635. LOG_I("TRNG is used as default RNG.");
  636. nu_rng_ops.update = nu_trng_rand;
  637. }
  638. else
  639. #endif
  640. {
  641. result = nu_prng_init();
  642. RT_ASSERT(result == RT_EOK);
  643. LOG_I("PRNG is used as default RNG.");
  644. nu_rng_ops.update = nu_prng_rand;
  645. }
  646. #endif
  647. /* register hwcrypto operation */
  648. result = rt_hwcrypto_register(&nu_hwcrypto_dev, RT_HWCRYPTO_DEFAULT_NAME);
  649. RT_ASSERT(result == RT_EOK);
  650. return 0;
  651. }
  652. INIT_DEVICE_EXPORT(nu_hwcrypto_device_init);
  653. #endif //#if ((defined(BSP_USING_CRYPTO) || defined(BSP_USING_TRNG) || defined(BSP_USING_CRC)) && defined(RT_USING_HWCRYPTO))