hw_crc.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Copyright (c) 2006-2021, 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. #ifndef __HW_CRC_H__
  11. #define __HW_CRC_H__
  12. #include <hwcrypto.h>
  13. #define CRC_FLAG_REFIN (0x1 << 0)
  14. #define CRC_FLAG_REFOUT (0x1 << 1)
  15. #define HWCRYPTO_CRC8_CFG \
  16. { \
  17. .last_val = 0x00, \
  18. .poly = 0x07, \
  19. .width = 8, \
  20. .xorout = 0x00, \
  21. .flags = 0, \
  22. }
  23. #define HWCRYPTO_CRC16_CFG \
  24. { \
  25. .last_val = 0x0000, \
  26. .poly = 0x8005, \
  27. .width = 16, \
  28. .xorout = 0x0000, \
  29. .flags = 0, \
  30. }
  31. #define HWCRYPTO_CRC32_CFG \
  32. { \
  33. .last_val = 0x00000000, \
  34. .poly = 0x04C11DB7, \
  35. .width = 32, \
  36. .xorout = 0x00000000, \
  37. .flags = 0, \
  38. }
  39. #define HWCRYPTO_CRC_CCITT_CFG \
  40. { \
  41. .last_val = 0x0000, \
  42. .poly = 0x1021, \
  43. .width = 16, \
  44. .xorout = 0x0000, \
  45. .flags = CRC_FLAG_REFIN | CRC_FLAG_REFOUT, \
  46. }
  47. #define HWCRYPTO_CRC_DNP_CFG \
  48. { \
  49. .last_val = 0x0000, \
  50. .poly = 0x3D65, \
  51. .width = 16, \
  52. .xorout = 0xffff, \
  53. .flags = CRC_FLAG_REFIN | CRC_FLAG_REFOUT, \
  54. }
  55. #ifdef __cplusplus
  56. extern "C" {
  57. #endif
  58. struct hwcrypto_crc;
  59. typedef enum
  60. {
  61. HWCRYPTO_CRC_CUSTOM, /**< Custom CRC mode */
  62. HWCRYPTO_CRC_CRC8, /**< poly : 0x07 */
  63. HWCRYPTO_CRC_CRC16, /**< poly : 0x8005 */
  64. HWCRYPTO_CRC_CRC32, /**< poly : 0x04C11DB7 */
  65. HWCRYPTO_CRC_CCITT, /**< poly : 0x1021 */
  66. HWCRYPTO_CRC_DNP, /**< poly : 0x3D65 */
  67. } hwcrypto_crc_mode;
  68. struct hwcrypto_crc_cfg
  69. {
  70. rt_uint32_t last_val; /**< Last CRC value cache */
  71. rt_uint32_t poly; /**< CRC polynomial */
  72. rt_uint16_t width; /**< CRC value width */
  73. rt_uint32_t xorout; /**< Result XOR Value */
  74. rt_uint16_t flags; /**< Input or output data reverse. CRC_FLAG_REFIN or CRC_FLAG_REFOUT */
  75. };
  76. struct hwcrypto_crc_ops
  77. {
  78. rt_uint32_t (*update)(struct hwcrypto_crc *ctx,
  79. const rt_uint8_t *in, rt_size_t length); /**< Perform a CRC calculation. return CRC value */
  80. };
  81. /**
  82. * @brief CRC context. Hardware driver usage
  83. */
  84. struct hwcrypto_crc
  85. {
  86. struct rt_hwcrypto_ctx parent; /**< Inherited from the standard device */
  87. struct hwcrypto_crc_cfg crc_cfg; /**< CRC configure */
  88. const struct hwcrypto_crc_ops *ops; /**< !! Hardware initializes this value when creating context !! */
  89. };
  90. /**
  91. * @brief Creating CRC Context
  92. *
  93. * @param device Hardware crypto device
  94. * @param mode Setting default mode or custom mode
  95. *
  96. * @return CRC context
  97. */
  98. struct rt_hwcrypto_ctx *rt_hwcrypto_crc_create(struct rt_hwcrypto_device *device,
  99. hwcrypto_crc_mode mode);
  100. /**
  101. * @brief Destroy CRC Context
  102. *
  103. * @param ctx CRC context
  104. */
  105. void rt_hwcrypto_crc_destroy(struct rt_hwcrypto_ctx *ctx);
  106. /**
  107. * @brief Processing a packet of data
  108. *
  109. * @param ctx CRC context
  110. * @param input Data buffer to be Processed
  111. * @param length Data Buffer length
  112. *
  113. * @return CRC value
  114. */
  115. rt_uint32_t rt_hwcrypto_crc_update(struct rt_hwcrypto_ctx *ctx,
  116. const rt_uint8_t *input, rt_size_t length);
  117. /**
  118. * @brief CRC context configuration
  119. *
  120. * @param ctx CRC context
  121. * @param cfg CRC config
  122. */
  123. void rt_hwcrypto_crc_cfg(struct rt_hwcrypto_ctx *ctx,
  124. struct hwcrypto_crc_cfg *cfg);
  125. #ifdef __cplusplus
  126. }
  127. #endif
  128. #endif