drv_crypto.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-04-21 wolfJane first version
  9. */
  10. #include <board.h>
  11. #include "drv_crypto.h"
  12. #ifdef RT_USING_HWCRYPTO
  13. #define DRV_DEBUG
  14. #define LOG_TAG "drv.crypto"
  15. #include <drv_log.h>
  16. #ifdef RT_HWCRYPTO_USING_CRC
  17. #define BSP_USING_CRC
  18. #endif
  19. static uint16_t _reverse_16bit(uint16_t x)
  20. {
  21. x = (((x & 0xaaaa) >> 1) | ((x & 0x5555) << 1));
  22. x = (((x & 0xcccc) >> 2) | ((x & 0x3333) << 2));
  23. x = (((x & 0xf0f0) >> 4) | ((x & 0x0f0f) << 4));
  24. return ((x >> 8) | (x << 8));
  25. }
  26. static uint16_t _get_crc16(struct hwcrypto_crc *ctx, const rt_uint8_t *in, rt_size_t length)
  27. {
  28. uint16_t temp = 0;
  29. if (ctx ->crc_cfg.poly != CRC16_PLOY)
  30. {
  31. LOG_E("n32g45x crc16 is available only use poly 0x%04X", CRC16_PLOY);
  32. return 0;
  33. }
  34. if (ctx ->crc_cfg.flags & CRC_FLAG_REFIN)
  35. {
  36. CRC->CRC16CTRL |= CRC16_CTRL_LITTLE;
  37. }
  38. else
  39. {
  40. CRC->CRC16CTRL &= (~CRC16_CTRL_LITTLE);
  41. }
  42. CRC->CRC16CTRL |= CRC16_CTRL_RESET;
  43. CRC->CRC16D = ctx ->crc_cfg.last_val;
  44. CRC->LRC = 0;
  45. while (length--)
  46. {
  47. CRC->CRC16DAT = *in++;
  48. }
  49. temp = CRC->CRC16D;
  50. if (ctx ->crc_cfg.flags & CRC_FLAG_REFOUT)
  51. {
  52. temp = _reverse_16bit(temp);
  53. }
  54. return temp;
  55. }
  56. static uint32_t _get_crc32(struct hwcrypto_crc *ctx, const rt_uint8_t *in, rt_size_t length)
  57. {
  58. uint32_t result = 0;
  59. int length_4 = length >> 2;
  60. uint8_t *xdat = (uint8_t *)in;
  61. if (ctx ->crc_cfg.poly != CRC32_PLOY)
  62. {
  63. LOG_E("n32g45x crc32 is available only use poly 0x%08X", CRC32_PLOY);
  64. return 0;
  65. }
  66. switch (ctx ->crc_cfg.flags & 0x03)
  67. {
  68. case 0:
  69. {
  70. while (length_4--)
  71. {
  72. result = ((uint32_t)xdat[0] << 24) | ((uint32_t)xdat[1] << 16)
  73. | ((uint32_t)xdat[2] << 8) | xdat[3];
  74. CRC->CRC32DAT = result;
  75. xdat += 4;
  76. }
  77. result = CRC->CRC32DAT;
  78. length_4 = length & 0x03;
  79. if (length_4 < 1)
  80. {
  81. break;
  82. }
  83. uint8_t i = 0;
  84. while (length_4--)
  85. {
  86. result ^= (*xdat++) << 24;
  87. for (i = 0; i < 8; i++)
  88. {
  89. if (result & 0x80000000)
  90. {
  91. result = (result << 1) ^ CRC32_PLOY;
  92. }
  93. else
  94. {
  95. result <<= 1;
  96. }
  97. }
  98. }
  99. }
  100. break;
  101. case CRC_FLAG_REFIN:
  102. /* TODO: add control command handle */
  103. case CRC_FLAG_REFOUT:
  104. /* TODO: add control command handle */
  105. case (CRC_FLAG_REFIN|CRC_FLAG_REFOUT):
  106. /* TODO: add control command handle */
  107. default:
  108. break;
  109. }
  110. return result;
  111. }
  112. static rt_uint32_t _crc_update(struct hwcrypto_crc *ctx, const rt_uint8_t *in, rt_size_t length)
  113. {
  114. rt_uint32_t result = 0;
  115. struct n32_hwcrypto_device *n32_hw_dev = (struct n32_hwcrypto_device *)ctx->parent.device->user_data;
  116. rt_mutex_take(&n32_hw_dev->mutex, RT_WAITING_FOREVER);
  117. if (ctx ->crc_cfg.flags >> 2)
  118. {
  119. LOG_E("flags out range 0x%08X", ctx ->crc_cfg.flags);
  120. goto _exit;
  121. }
  122. if (ctx ->crc_cfg.width == 16)
  123. {
  124. result = _get_crc16(ctx, in, length);
  125. }
  126. else if (ctx ->crc_cfg.width == 32)
  127. {
  128. result = _get_crc32(ctx, in, length);
  129. }
  130. _exit:
  131. rt_mutex_release(&n32_hw_dev->mutex);
  132. return result;
  133. }
  134. static const struct hwcrypto_crc_ops crc_ops =
  135. {
  136. .update = _crc_update,
  137. };
  138. static rt_err_t _crypto_create(struct rt_hwcrypto_ctx *ctx)
  139. {
  140. rt_err_t res = RT_EOK;
  141. switch (ctx->type & HWCRYPTO_MAIN_TYPE_MASK)
  142. {
  143. #if defined(BSP_USING_RNG)
  144. case HWCRYPTO_TYPE_RNG:
  145. {
  146. break;
  147. }
  148. #endif /* BSP_USING_RNG */
  149. #if defined(BSP_USING_CRC)
  150. case HWCRYPTO_TYPE_CRC:
  151. {
  152. RCC_EnableAHBPeriphClk(RCC_AHB_PERIPH_CRC, ENABLE);
  153. ((struct hwcrypto_crc *)ctx)->ops = &crc_ops;
  154. break;
  155. }
  156. #endif /* BSP_USING_CRC */
  157. #if defined(BSP_USING_HASH)
  158. case HWCRYPTO_TYPE_MD5:
  159. case HWCRYPTO_TYPE_SHA1:
  160. case HWCRYPTO_TYPE_SHA2:
  161. {
  162. break;
  163. }
  164. #endif /* BSP_USING_HASH */
  165. #if defined(BSP_USING_CRYP)
  166. case HWCRYPTO_TYPE_AES:
  167. case HWCRYPTO_TYPE_DES:
  168. case HWCRYPTO_TYPE_3DES:
  169. case HWCRYPTO_TYPE_RC4:
  170. case HWCRYPTO_TYPE_GCM:
  171. {
  172. break;
  173. }
  174. #endif /* BSP_USING_CRYP */
  175. default:
  176. res = -RT_ERROR;
  177. break;
  178. }
  179. return res;
  180. }
  181. static void _crypto_destroy(struct rt_hwcrypto_ctx *ctx)
  182. {
  183. /* TODO: add control command handle */
  184. switch (ctx->type & HWCRYPTO_MAIN_TYPE_MASK)
  185. {
  186. #if defined(BSP_USING_RNG)
  187. case HWCRYPTO_TYPE_RNG:
  188. break;
  189. #endif /* BSP_USING_RNG */
  190. #if defined(BSP_USING_CRC)
  191. case HWCRYPTO_TYPE_CRC:
  192. break;
  193. #endif /* BSP_USING_CRC */
  194. #if defined(BSP_USING_HASH)
  195. case HWCRYPTO_TYPE_MD5:
  196. case HWCRYPTO_TYPE_SHA1:
  197. case HWCRYPTO_TYPE_SHA2:
  198. break;
  199. #endif /* BSP_USING_HASH */
  200. #if defined(BSP_USING_CRYP)
  201. case HWCRYPTO_TYPE_AES:
  202. case HWCRYPTO_TYPE_DES:
  203. case HWCRYPTO_TYPE_3DES:
  204. case HWCRYPTO_TYPE_RC4:
  205. case HWCRYPTO_TYPE_GCM:
  206. break;
  207. #endif /* BSP_USING_CRYP */
  208. default:
  209. break;
  210. }
  211. if (ctx->contex)
  212. rt_free(ctx->contex);
  213. }
  214. static rt_err_t _crypto_clone(struct rt_hwcrypto_ctx *des, const struct rt_hwcrypto_ctx *src)
  215. {
  216. rt_err_t res = RT_EOK;
  217. /* TODO: add control command handle */
  218. switch (src->type & HWCRYPTO_MAIN_TYPE_MASK)
  219. {
  220. #if defined(BSP_USING_RNG)
  221. case HWCRYPTO_TYPE_RNG:
  222. break;
  223. #endif /* BSP_USING_RNG */
  224. #if defined(BSP_USING_CRC)
  225. case HWCRYPTO_TYPE_CRC:
  226. break;
  227. #endif /* BSP_USING_CRC */
  228. #if defined(BSP_USING_HASH)
  229. case HWCRYPTO_TYPE_MD5:
  230. case HWCRYPTO_TYPE_SHA1:
  231. case HWCRYPTO_TYPE_SHA2:
  232. break;
  233. #endif /* BSP_USING_HASH */
  234. #if defined(BSP_USING_CRYP)
  235. case HWCRYPTO_TYPE_AES:
  236. case HWCRYPTO_TYPE_DES:
  237. case HWCRYPTO_TYPE_3DES:
  238. case HWCRYPTO_TYPE_RC4:
  239. case HWCRYPTO_TYPE_GCM:
  240. break;
  241. #endif /* BSP_USING_CRYP */
  242. default:
  243. res = -RT_ERROR;
  244. break;
  245. }
  246. return res;
  247. }
  248. static void _crypto_reset(struct rt_hwcrypto_ctx *ctx)
  249. {
  250. /* TODO: add control command handle */
  251. switch (ctx->type & HWCRYPTO_MAIN_TYPE_MASK)
  252. {
  253. #if defined(BSP_USING_RNG)
  254. case HWCRYPTO_TYPE_RNG:
  255. break;
  256. #endif /* BSP_USING_RNG */
  257. #if defined(BSP_USING_CRC)
  258. case HWCRYPTO_TYPE_CRC:
  259. break;
  260. #endif /* BSP_USING_CRC */
  261. #if defined(BSP_USING_HASH)
  262. case HWCRYPTO_TYPE_MD5:
  263. case HWCRYPTO_TYPE_SHA1:
  264. case HWCRYPTO_TYPE_SHA2:
  265. break;
  266. #endif /* BSP_USING_HASH*/
  267. #if defined(BSP_USING_CRYP)
  268. case HWCRYPTO_TYPE_AES:
  269. case HWCRYPTO_TYPE_DES:
  270. case HWCRYPTO_TYPE_3DES:
  271. case HWCRYPTO_TYPE_RC4:
  272. case HWCRYPTO_TYPE_GCM:
  273. break;
  274. #endif /* BSP_USING_CRYP */
  275. default:
  276. break;
  277. }
  278. }
  279. static const struct rt_hwcrypto_ops _ops =
  280. {
  281. .create = _crypto_create,
  282. .destroy = _crypto_destroy,
  283. .copy = _crypto_clone,
  284. .reset = _crypto_reset,
  285. };
  286. int n32_hw_crypto_device_init(void)
  287. {
  288. static struct n32_hwcrypto_device _crypto_dev;
  289. rt_uint32_t cpuid[3] = {0};
  290. _crypto_dev.dev.ops = &_ops;
  291. #if defined(BSP_USING_UDID)
  292. /* TODO: add control command handle */
  293. #endif /* BSP_USING_UDID */
  294. _crypto_dev.dev.id = 0;
  295. rt_memcpy(&_crypto_dev.dev.id, cpuid, 8);
  296. _crypto_dev.dev.user_data = &_crypto_dev;
  297. if (rt_hwcrypto_register(&_crypto_dev.dev, RT_HWCRYPTO_DEFAULT_NAME) != RT_EOK)
  298. {
  299. return -1;
  300. }
  301. rt_mutex_init(&_crypto_dev.mutex, RT_HWCRYPTO_DEFAULT_NAME, RT_IPC_FLAG_PRIO);
  302. return 0;
  303. }
  304. INIT_DEVICE_EXPORT(n32_hw_crypto_device_init);
  305. static void crc_demo(uint8_t argc, char **argv)
  306. {
  307. struct hwcrypto_crc_cfg modbus_cfg =
  308. {
  309. .last_val = 0xFFFF,
  310. .poly = 0x8005,
  311. .width = 16,
  312. .xorout = 0x0000,
  313. .flags = CRC_FLAG_REFIN | CRC_FLAG_REFOUT,
  314. };
  315. struct hwcrypto_crc_cfg crc32mpeg2_cfg =
  316. {
  317. .last_val = 0xFFFFFFFF,
  318. .poly = 0x04C11DB7,
  319. .width = 32,
  320. .xorout = 0x00000000,
  321. .flags = 0,
  322. };
  323. struct rt_hwcrypto_ctx *ctx;
  324. rt_uint32_t result = 0;
  325. rt_uint8_t temp[] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
  326. rt_uint8_t i = 0;
  327. rt_uint8_t len = sizeof(temp);
  328. ctx = rt_hwcrypto_crc_create(rt_hwcrypto_dev_default(), HWCRYPTO_CRC_CRC32);
  329. rt_kprintf("crc dat:\n");
  330. for (i = 0; i < len; i++)
  331. {
  332. rt_kprintf("%02X ", temp[i]);
  333. }
  334. rt_kprintf("\n");
  335. rt_hwcrypto_crc_cfg(ctx, &crc32mpeg2_cfg);
  336. result = rt_hwcrypto_crc_update(ctx, temp, len);
  337. rt_kprintf("crc32mpeg2: %x \n", result);
  338. rt_hwcrypto_crc_cfg(ctx, &modbus_cfg);
  339. result = rt_hwcrypto_crc_update(ctx, temp, len);
  340. rt_kprintf("crc16modbus: %x \n", result);
  341. /*chang to msb*/
  342. modbus_cfg.flags = 0;
  343. rt_hwcrypto_crc_cfg(ctx, &modbus_cfg);
  344. result = rt_hwcrypto_crc_update(ctx, temp, len);
  345. rt_kprintf("crc16 msb: %x \n", result);
  346. rt_hwcrypto_crc_destroy(ctx);
  347. }
  348. MSH_CMD_EXPORT(crc_demo, demo for hardwave crc.);
  349. #endif