drv_crypto.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. * 2021-02-26 klcheng First version
  10. *
  11. ******************************************************************************/
  12. #include <rtconfig.h>
  13. #if ((defined(BSP_USING_CRC)) && defined(RT_USING_HWCRYPTO))
  14. #include <rtdevice.h>
  15. #include <rtdbg.h>
  16. #include <board.h>
  17. #include "NuMicro.h"
  18. #include <nu_bitutil.h>
  19. #if defined(BSP_USING_CRC)
  20. #include "drv_crc.h"
  21. #endif
  22. /* Private functions ------------------------------------------------------------*/
  23. static rt_err_t nu_hwcrypto_create(struct rt_hwcrypto_ctx *ctx);
  24. static void nu_hwcrypto_destroy(struct rt_hwcrypto_ctx *ctx);
  25. static rt_err_t nu_hwcrypto_clone(struct rt_hwcrypto_ctx *des, const struct rt_hwcrypto_ctx *src);
  26. static void nu_hwcrypto_reset(struct rt_hwcrypto_ctx *ctx);
  27. /* Private variables ------------------------------------------------------------*/
  28. static const struct rt_hwcrypto_ops nu_hwcrypto_ops =
  29. {
  30. .create = nu_hwcrypto_create,
  31. .destroy = nu_hwcrypto_destroy,
  32. .copy = nu_hwcrypto_clone,
  33. .reset = nu_hwcrypto_reset,
  34. };
  35. /* CRC operation ------------------------------------------------------------*/
  36. #if defined(BSP_USING_CRC)
  37. static const struct hwcrypto_crc_ops nu_crc_ops =
  38. {
  39. .update = nu_crc_update,
  40. };
  41. #endif
  42. /* Register crypto interface ----------------------------------------------------------*/
  43. static rt_err_t nu_hwcrypto_create(struct rt_hwcrypto_ctx *ctx)
  44. {
  45. rt_err_t res = RT_EOK;
  46. switch (ctx->type & HWCRYPTO_MAIN_TYPE_MASK)
  47. {
  48. #if defined(BSP_USING_CRC)
  49. case HWCRYPTO_TYPE_CRC:
  50. {
  51. ctx->contex = RT_NULL;
  52. //Setup CRC operation
  53. ((struct hwcrypto_crc *)ctx)->ops = &nu_crc_ops;
  54. break;
  55. }
  56. #endif /* BSP_USING_CRC */
  57. default:
  58. res = -RT_ERROR;
  59. break;
  60. }
  61. return res;
  62. }
  63. static void nu_hwcrypto_destroy(struct rt_hwcrypto_ctx *ctx)
  64. {
  65. if (ctx->contex)
  66. rt_free(ctx->contex);
  67. }
  68. static rt_err_t nu_hwcrypto_clone(struct rt_hwcrypto_ctx *des, const struct rt_hwcrypto_ctx *src)
  69. {
  70. rt_err_t res = RT_EOK;
  71. if (des->contex && src->contex)
  72. {
  73. rt_memcpy(des->contex, src->contex, sizeof(struct rt_hwcrypto_ctx));
  74. }
  75. else
  76. return -RT_EINVAL;
  77. return res;
  78. }
  79. static void nu_hwcrypto_reset(struct rt_hwcrypto_ctx *ctx)
  80. {
  81. /* no need to implement */
  82. return;
  83. }
  84. /* Init and register nu_hwcrypto_dev */
  85. int nu_hwcrypto_device_init(void)
  86. {
  87. static struct rt_hwcrypto_device nu_hwcrypto_dev;
  88. nu_hwcrypto_dev.ops = &nu_hwcrypto_ops;
  89. nu_hwcrypto_dev.id = 0;
  90. nu_hwcrypto_dev.user_data = &nu_hwcrypto_dev;
  91. #if defined(BSP_USING_CRC)
  92. nu_crc_init();
  93. #endif
  94. // register hwcrypto operation
  95. if (rt_hwcrypto_register(&nu_hwcrypto_dev, RT_HWCRYPTO_DEFAULT_NAME) != RT_EOK)
  96. {
  97. return -1;
  98. }
  99. return 0;
  100. }
  101. INIT_DEVICE_EXPORT(nu_hwcrypto_device_init);
  102. #endif //#if ((defined(BSP_USING_CRC)) && defined(RT_USING_HWCRYPTO))