hw_symmetric.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. #ifndef __HW_SYMMETRIC_H__
  11. #define __HW_SYMMETRIC_H__
  12. #include <hwcrypto.h>
  13. #ifndef RT_HWCRYPTO_IV_MAX_SIZE
  14. #define RT_HWCRYPTO_IV_MAX_SIZE (16)
  15. #endif
  16. #ifndef RT_HWCRYPTO_KEYBIT_MAX_SIZE
  17. #define RT_HWCRYPTO_KEYBIT_MAX_SIZE (256)
  18. #endif
  19. #define SYMMTRIC_MODIFY_KEY (0x1 << 0)
  20. #define SYMMTRIC_MODIFY_IV (0x1 << 1)
  21. #define SYMMTRIC_MODIFY_IVOFF (0x1 << 2)
  22. #ifdef __cplusplus
  23. extern "C" {
  24. #endif
  25. struct hwcrypto_symmetric;
  26. struct hwcrypto_symmetric_info;
  27. struct hwcrypto_symmetric_ops
  28. {
  29. rt_err_t (*crypt)(struct hwcrypto_symmetric *symmetric_ctx,
  30. struct hwcrypto_symmetric_info *symmetric_info); /**< Hardware Symmetric Encryption and Decryption Callback */
  31. };
  32. /**
  33. * @brief Hardware driver usage, including input and output information
  34. */
  35. struct hwcrypto_symmetric_info
  36. {
  37. hwcrypto_mode mode; /**< crypto mode. HWCRYPTO_MODE_ENCRYPT or HWCRYPTO_MODE_DECRYPT */
  38. const rt_uint8_t *in; /**< Input data */
  39. rt_uint8_t *out; /**< Output data will be written */
  40. rt_size_t length; /**< The length of the input data in Bytes. It's a multiple of block size. */
  41. };
  42. /**
  43. * @brief Symmetric crypto context. Hardware driver usage
  44. */
  45. struct hwcrypto_symmetric
  46. {
  47. struct rt_hwcrypto_ctx parent; /**< Inheritance from hardware crypto context */
  48. rt_uint16_t flags; /**< key or iv or ivoff has been changed. The flag will be set up */
  49. rt_uint16_t iv_len; /**< initialization vector effective length */
  50. rt_uint16_t iv_off; /**< The offset in IV */
  51. rt_uint16_t key_bitlen; /**< The crypto key bit length */
  52. rt_uint8_t iv[RT_HWCRYPTO_IV_MAX_SIZE]; /**< The initialization vector */
  53. rt_uint8_t key[RT_HWCRYPTO_KEYBIT_MAX_SIZE >> 3]; /**< The crypto key */
  54. const struct hwcrypto_symmetric_ops *ops; /**< !! Hardware initializes this value when creating context !! */
  55. };
  56. /**
  57. * @brief Creating Symmetric Encryption and Decryption Context
  58. *
  59. * @param device Hardware crypto device
  60. * @param type Type of symmetric crypto context
  61. *
  62. * @return Symmetric crypto context
  63. */
  64. struct rt_hwcrypto_ctx *rt_hwcrypto_symmetric_create(struct rt_hwcrypto_device *device,
  65. hwcrypto_type type);
  66. /**
  67. * @brief Destroy Symmetric Encryption and Decryption Context
  68. *
  69. * @param ctx Symmetric crypto context
  70. */
  71. void rt_hwcrypto_symmetric_destroy(struct rt_hwcrypto_ctx *ctx);
  72. /**
  73. * @brief This function performs a symmetric encryption or decryption operation
  74. *
  75. * @param ctx Symmetric crypto context
  76. * @param mode Operation mode. HWCRYPTO_MODE_ENCRYPT or HWCRYPTO_MODE_DECRYPT
  77. * @param length The length of the input data in Bytes. This must be a multiple of the block size
  78. * @param in The buffer holding the input data
  79. * @param out The buffer holding the output data
  80. *
  81. * @return RT_EOK on success.
  82. */
  83. rt_err_t rt_hwcrypto_symmetric_crypt(struct rt_hwcrypto_ctx *ctx, hwcrypto_mode mode,
  84. rt_size_t length, const rt_uint8_t *in, rt_uint8_t *out);
  85. /**
  86. * @brief Set Symmetric Encryption and Decryption Key
  87. *
  88. * @param ctx Symmetric crypto context
  89. * @param key The crypto key
  90. * @param bitlen The crypto key bit length
  91. *
  92. * @return RT_EOK on success.
  93. */
  94. rt_err_t rt_hwcrypto_symmetric_setkey(struct rt_hwcrypto_ctx *ctx, const rt_uint8_t *key, rt_uint32_t bitlen);
  95. /**
  96. * @brief Get Symmetric Encryption and Decryption Key
  97. *
  98. * @param ctx Symmetric crypto context
  99. * @param key The crypto key buffer
  100. * @param bitlen The crypto key bit length
  101. *
  102. * @return Key length of copy
  103. */
  104. int rt_hwcrypto_symmetric_getkey(struct rt_hwcrypto_ctx *ctx, rt_uint8_t *key, rt_uint32_t bitlen);
  105. /**
  106. * @brief Set Symmetric Encryption and Decryption initialization vector
  107. *
  108. * @param ctx Symmetric crypto context
  109. * @param iv The crypto initialization vector
  110. * @param len The crypto initialization vector length
  111. *
  112. * @return RT_EOK on success.
  113. */
  114. rt_err_t rt_hwcrypto_symmetric_setiv(struct rt_hwcrypto_ctx *ctx, const rt_uint8_t *iv, rt_size_t len);
  115. /**
  116. * @brief Get Symmetric Encryption and Decryption initialization vector
  117. *
  118. * @param ctx Symmetric crypto context
  119. * @param iv The crypto initialization vector buffer
  120. * @param len The crypto initialization vector buffer length
  121. *
  122. * @return IV length of copy
  123. */
  124. int rt_hwcrypto_symmetric_getiv(struct rt_hwcrypto_ctx *ctx, rt_uint8_t *iv, rt_size_t len);
  125. /**
  126. * @brief Set offset in initialization vector
  127. *
  128. * @param ctx Symmetric crypto context
  129. * @param iv_off The offset in IV
  130. */
  131. void rt_hwcrypto_symmetric_set_ivoff(struct rt_hwcrypto_ctx *ctx, rt_int32_t iv_off);
  132. /**
  133. * @brief Get offset in initialization vector
  134. *
  135. * @param ctx Symmetric crypto context
  136. * @param iv_off It must point to a valid memory
  137. */
  138. void rt_hwcrypto_symmetric_get_ivoff(struct rt_hwcrypto_ctx *ctx, rt_int32_t *iv_off);
  139. /**
  140. * @brief This function copy symmetric crypto context
  141. *
  142. * @param des The destination symmetric crypto context
  143. * @param src The symmetric crypto context to be copy
  144. *
  145. * @return RT_EOK on success.
  146. */
  147. rt_err_t rt_hwcrypto_symmetric_cpy(struct rt_hwcrypto_ctx *des, const struct rt_hwcrypto_ctx *src);
  148. /**
  149. * @brief Reset symmetric crypto context
  150. *
  151. * @param ctx Symmetric crypto context
  152. */
  153. void rt_hwcrypto_symmetric_reset(struct rt_hwcrypto_ctx *ctx);
  154. /**
  155. * @brief Setting symmetric crypto context type
  156. *
  157. * @param ctx Symmetric crypto context
  158. * @param type Types of settings
  159. *
  160. * @return RT_EOK on success.
  161. */
  162. rt_err_t rt_hwcrypto_symmetric_set_type(struct rt_hwcrypto_ctx *ctx, hwcrypto_type type);
  163. #ifdef __cplusplus
  164. }
  165. #endif
  166. #endif