1
0

hw_crc.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. * 2019-04-25 tyx the first version
  9. */
  10. #include <rtthread.h>
  11. #include <rtdevice.h>
  12. #include <hw_crc.h>
  13. /**
  14. * @brief Creating CRC Context
  15. *
  16. * @param device Hardware crypto device
  17. * @param mode Setting default mode or custom mode
  18. *
  19. * @return CRC context
  20. */
  21. struct rt_hwcrypto_ctx *rt_hwcrypto_crc_create(struct rt_hwcrypto_device *device,
  22. hwcrypto_crc_mode mode)
  23. {
  24. struct hwcrypto_crc *crc_ctx;
  25. crc_ctx = (struct hwcrypto_crc *)rt_hwcrypto_ctx_create(device, HWCRYPTO_TYPE_CRC, sizeof(struct hwcrypto_crc));
  26. if (crc_ctx == RT_NULL)
  27. {
  28. return RT_NULL;
  29. }
  30. switch (mode)
  31. {
  32. case HWCRYPTO_CRC_CRC8:
  33. {
  34. struct hwcrypto_crc_cfg temp = HWCRYPTO_CRC8_CFG;
  35. crc_ctx->crc_cfg = temp;
  36. break;
  37. }
  38. case HWCRYPTO_CRC_CRC16:
  39. {
  40. struct hwcrypto_crc_cfg temp = HWCRYPTO_CRC16_CFG;
  41. crc_ctx->crc_cfg = temp;
  42. break;
  43. }
  44. case HWCRYPTO_CRC_CRC32:
  45. {
  46. struct hwcrypto_crc_cfg temp = HWCRYPTO_CRC32_CFG;
  47. crc_ctx->crc_cfg = temp;
  48. break;
  49. }
  50. case HWCRYPTO_CRC_CCITT:
  51. {
  52. struct hwcrypto_crc_cfg temp = HWCRYPTO_CRC_CCITT_CFG;
  53. crc_ctx->crc_cfg = temp;
  54. break;
  55. }
  56. case HWCRYPTO_CRC_DNP:
  57. {
  58. struct hwcrypto_crc_cfg temp = HWCRYPTO_CRC_DNP_CFG;
  59. crc_ctx->crc_cfg = temp;
  60. break;
  61. }
  62. default:
  63. break;
  64. }
  65. return &crc_ctx->parent;
  66. }
  67. /**
  68. * @brief Destroy CRC Context
  69. *
  70. * @param ctx CRC context
  71. */
  72. void rt_hwcrypto_crc_destroy(struct rt_hwcrypto_ctx *ctx)
  73. {
  74. rt_hwcrypto_ctx_destroy(ctx);
  75. }
  76. /**
  77. * @brief Processing a packet of data
  78. *
  79. * @param ctx CRC context
  80. * @param input Data buffer to be Processed
  81. * @param length Data Buffer length
  82. *
  83. * @return CRC value
  84. */
  85. rt_uint32_t rt_hwcrypto_crc_update(struct rt_hwcrypto_ctx *ctx,
  86. const rt_uint8_t *input,
  87. rt_size_t length)
  88. {
  89. struct hwcrypto_crc *crc_ctx = (struct hwcrypto_crc *)ctx;
  90. if (ctx && crc_ctx->ops->update)
  91. {
  92. return crc_ctx->ops->update(crc_ctx, input, length);
  93. }
  94. return 0;
  95. }
  96. /**
  97. * @brief CRC context configuration
  98. *
  99. * @param ctx CRC context
  100. * @param cfg CRC config
  101. */
  102. void rt_hwcrypto_crc_cfg(struct rt_hwcrypto_ctx *ctx,
  103. struct hwcrypto_crc_cfg *cfg)
  104. {
  105. if (cfg)
  106. {
  107. ((struct hwcrypto_crc *)ctx)->crc_cfg = *cfg;
  108. }
  109. }