drv_crypto.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * Copyright (c) 2006-2022, Synwit Technology Co.,Ltd.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020-07-10 lik first version
  9. */
  10. #include "drv_crypto.h"
  11. #include <string.h>
  12. #ifdef RT_USING_HWCRYPTO
  13. //#define DRV_DEBUG
  14. #define LOG_TAG "drv.crypto"
  15. #include <drv_log.h>
  16. struct swm_hwcrypto_device
  17. {
  18. struct rt_hwcrypto_device dev;
  19. struct rt_mutex mutex;
  20. };
  21. static struct swm_hwcrypto_device hwcrypto_obj;
  22. #ifdef BSP_USING_CRC
  23. #define DEFAULT_CRC (CRC)
  24. #define DEFAULT_INIVAL (0x00000000)
  25. #define DEFAULT_INBITS (2)
  26. #define DEFAULT_CRC1632 (0)
  27. #define DEFAULT_OUT_NOT (0)
  28. #define DEFAULT_OUT_REV (0)
  29. struct swm_crc_cfg
  30. {
  31. CRC_TypeDef *CRCx;
  32. uint32_t inival;
  33. uint8_t crc_inbits;
  34. uint8_t crc_1632;
  35. uint8_t crc_out_not;
  36. uint8_t crc_out_rev;
  37. };
  38. static struct hwcrypto_crc_cfg swm_crc_cfg;
  39. static rt_uint32_t swm_crc_update(struct hwcrypto_crc *ctx, const rt_uint8_t *in, rt_size_t length)
  40. {
  41. rt_uint32_t result = 0;
  42. struct swm_hwcrypto_device *hwcrypto_dev = (struct swm_hwcrypto_device *)ctx->parent.device->user_data;
  43. struct swm_crc_cfg *crc_cfg = (struct swm_crc_cfg *)(ctx->parent.contex);
  44. rt_mutex_take(&hwcrypto_dev->mutex, RT_WAITING_FOREVER);
  45. if (memcmp(&swm_crc_cfg, &ctx->crc_cfg, sizeof(struct hwcrypto_crc_cfg)) != 0)
  46. {
  47. crc_cfg->CRCx = CRC;
  48. crc_cfg->inival = ctx->crc_cfg.last_val;
  49. switch (ctx->crc_cfg.poly)
  50. {
  51. case 0x1021:
  52. crc_cfg->crc_1632 = 1;
  53. break;
  54. case 0x04C11DB7:
  55. crc_cfg->crc_1632 = 0;
  56. break;
  57. default:
  58. goto _exit;
  59. }
  60. switch (ctx->crc_cfg.width)
  61. {
  62. case 8:
  63. crc_cfg->crc_inbits = 2;
  64. break;
  65. case 16:
  66. crc_cfg->crc_inbits = 1;
  67. break;
  68. case 32:
  69. crc_cfg->crc_inbits = 0;
  70. break;
  71. default:
  72. goto _exit;
  73. }
  74. crc_cfg->crc_out_not = 0;
  75. switch (ctx->crc_cfg.flags)
  76. {
  77. case 0:
  78. case CRC_FLAG_REFIN:
  79. crc_cfg->crc_out_rev = 0;
  80. break;
  81. case CRC_FLAG_REFOUT:
  82. case CRC_FLAG_REFIN | CRC_FLAG_REFOUT:
  83. crc_cfg->crc_out_rev = 1;
  84. break;
  85. default:
  86. goto _exit;
  87. }
  88. CRC_Init(crc_cfg->CRCx, (crc_cfg->crc_inbits << 1) | crc_cfg->crc_1632, crc_cfg->crc_out_not, crc_cfg->crc_out_rev, crc_cfg->inival);
  89. memcpy(&swm_crc_cfg, &ctx->crc_cfg, sizeof(struct hwcrypto_crc_cfg));
  90. }
  91. for (uint32_t i = 0; i < length; i++)
  92. CRC_Write((uint32_t)in[i]);
  93. result = CRC_Result();
  94. ctx->crc_cfg.last_val = result;
  95. swm_crc_cfg.last_val = ctx->crc_cfg.last_val;
  96. result = (result ? result ^ (ctx->crc_cfg.xorout) : result);
  97. _exit:
  98. rt_mutex_release(&hwcrypto_dev->mutex);
  99. return result;
  100. }
  101. static const struct hwcrypto_crc_ops swm_crc_ops =
  102. {
  103. .update = swm_crc_update,
  104. };
  105. #endif /* BSP_USING_CRC */
  106. static rt_err_t swm_crypto_create(struct rt_hwcrypto_ctx *ctx)
  107. {
  108. rt_err_t res = RT_EOK;
  109. switch (ctx->type & HWCRYPTO_MAIN_TYPE_MASK)
  110. {
  111. #if defined(BSP_USING_CRC)
  112. case HWCRYPTO_TYPE_CRC:
  113. {
  114. struct swm_crc_cfg *crc_cfg = rt_calloc(1, sizeof(struct swm_crc_cfg));
  115. if (RT_NULL == crc_cfg)
  116. {
  117. res = -RT_ERROR;
  118. break;
  119. }
  120. crc_cfg->CRCx = DEFAULT_CRC;
  121. crc_cfg->inival = DEFAULT_INIVAL;
  122. crc_cfg->crc_inbits = DEFAULT_INBITS;
  123. crc_cfg->crc_1632 = DEFAULT_CRC1632;
  124. crc_cfg->crc_out_not = DEFAULT_OUT_NOT;
  125. crc_cfg->crc_out_rev = DEFAULT_OUT_REV;
  126. ctx->contex = crc_cfg;
  127. ((struct hwcrypto_crc *)ctx)->ops = &swm_crc_ops;
  128. break;
  129. }
  130. #endif /* BSP_USING_CRC */
  131. default:
  132. res = -RT_ERROR;
  133. break;
  134. }
  135. return res;
  136. }
  137. static void swm_crypto_destroy(struct rt_hwcrypto_ctx *ctx)
  138. {
  139. struct swm_crc_cfg *crc_cfg = (struct swm_crc_cfg *)(ctx->contex);
  140. switch (ctx->type & HWCRYPTO_MAIN_TYPE_MASK)
  141. {
  142. #if defined(BSP_USING_CRC)
  143. case HWCRYPTO_TYPE_CRC:
  144. crc_cfg->CRCx->CR &= ~CRC_CR_EN_Msk;
  145. break;
  146. #endif /* BSP_USING_CRC */
  147. default:
  148. break;
  149. }
  150. rt_free(ctx->contex);
  151. }
  152. static rt_err_t swm_crypto_clone(struct rt_hwcrypto_ctx *des, const struct rt_hwcrypto_ctx *src)
  153. {
  154. rt_err_t res = RT_EOK;
  155. switch (src->type & HWCRYPTO_MAIN_TYPE_MASK)
  156. {
  157. #if defined(BSP_USING_CRC)
  158. case HWCRYPTO_TYPE_CRC:
  159. if (des->contex && src->contex)
  160. {
  161. rt_memcpy(des->contex, src->contex, sizeof(struct swm_crc_cfg));
  162. }
  163. break;
  164. #endif /* BSP_USING_CRC */
  165. default:
  166. res = -RT_ERROR;
  167. break;
  168. }
  169. return res;
  170. }
  171. static void swm_crypto_reset(struct rt_hwcrypto_ctx *ctx)
  172. {
  173. struct swm_crc_cfg *crc_cfg = (struct swm_crc_cfg *)(ctx->contex);
  174. switch (ctx->type & HWCRYPTO_MAIN_TYPE_MASK)
  175. {
  176. #if defined(BSP_USING_CRC)
  177. case HWCRYPTO_TYPE_CRC:
  178. crc_cfg->CRCx->CR &= ~CRC_CR_EN_Msk;
  179. break;
  180. #endif /* BSP_USING_CRC */
  181. default:
  182. break;
  183. }
  184. }
  185. static const struct rt_hwcrypto_ops swm_hwcrypto_ops =
  186. {
  187. .create = swm_crypto_create,
  188. .destroy = swm_crypto_destroy,
  189. .copy = swm_crypto_clone,
  190. .reset = swm_crypto_reset,
  191. };
  192. int swm_crypto_init(void)
  193. {
  194. rt_uint32_t cpuid[2] = {0};
  195. hwcrypto_obj.dev.ops = &swm_hwcrypto_ops;
  196. cpuid[0] = SCB->CPUID;
  197. hwcrypto_obj.dev.id = 0;
  198. rt_memcpy(&hwcrypto_obj.dev.id, cpuid, 8);
  199. hwcrypto_obj.dev.user_data = &hwcrypto_obj;
  200. if (rt_hwcrypto_register(&hwcrypto_obj.dev, RT_HWCRYPTO_DEFAULT_NAME) != RT_EOK)
  201. {
  202. return -1;
  203. }
  204. rt_mutex_init(&hwcrypto_obj.mutex, RT_HWCRYPTO_DEFAULT_NAME, RT_IPC_FLAG_PRIO);
  205. return 0;
  206. }
  207. INIT_BOARD_EXPORT(swm_crypto_init);
  208. #endif /* RT_USING_HWCRYPTO */