drv_crypto.c 26 KB

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