hw_gcm.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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-05-14 tyx the first version
  9. */
  10. #ifndef __HW_GCM_H__
  11. #define __HW_GCM_H__
  12. #include "hw_symmetric.h"
  13. #ifdef __cplusplus
  14. extern "C" {
  15. #endif
  16. struct hwcrypto_gcm;
  17. struct hwcrypto_gcm_ops
  18. {
  19. rt_err_t (*start)(struct hwcrypto_gcm *gcm_ctx,
  20. const unsigned char *add, rt_size_t add_len); /**< Set additional data. start GCM operation */
  21. rt_err_t (*finish)(struct hwcrypto_gcm *gcm_ctx,
  22. const unsigned char *tag, rt_size_t tag_len); /**< finish GCM operation. get tag */
  23. };
  24. /**
  25. * @brief GCM context. Hardware driver usage
  26. */
  27. struct hwcrypto_gcm
  28. {
  29. struct hwcrypto_symmetric parent; /**< Inheritance from hardware symmetric crypto context */
  30. hwcrypto_type crypt_type; /**< symmetric crypto type. eg: AES/DES */
  31. const struct hwcrypto_gcm_ops *ops; /**< !! Hardware initializes this value when creating context !! */
  32. };
  33. /**
  34. * @brief Creating GCM Context
  35. *
  36. * @param device Hardware crypto device
  37. * @param type Type of symmetric crypto context
  38. *
  39. * @return GCM context
  40. */
  41. struct rt_hwcrypto_ctx *rt_hwcrypto_gcm_create(struct rt_hwcrypto_device *device,
  42. hwcrypto_type crypt_type);
  43. /**
  44. * @brief Destroy GCM Context
  45. *
  46. * @param ctx GCM context
  47. */
  48. void rt_hwcrypto_gcm_destroy(struct rt_hwcrypto_ctx *ctx);
  49. /**
  50. * @brief This function starts a GCM encryption or decryption operation
  51. *
  52. * @param ctx GCM context
  53. * @param add The buffer holding the additional data
  54. * @param add_len The length of the additional data
  55. *
  56. * @return RT_EOK on success.
  57. */
  58. rt_err_t rt_hwcrypto_gcm_start(struct rt_hwcrypto_ctx *ctx, const rt_uint8_t *add,
  59. rt_size_t add_len);
  60. /**
  61. * @brief This function finishes the GCM operation and generates the authentication tag
  62. *
  63. * @param ctx GCM context
  64. * @param tag The buffer for holding the tag
  65. * @param tag_len The length of the tag to generate
  66. *
  67. * @return RT_EOK on success.
  68. */
  69. rt_err_t rt_hwcrypto_gcm_finish(struct rt_hwcrypto_ctx *ctx, const rt_uint8_t *tag,
  70. rt_size_t tag_len);
  71. /**
  72. * @brief This function performs a symmetric encryption or decryption operation
  73. *
  74. * @param ctx GCM context
  75. * @param mode Operation mode. HWCRYPTO_MODE_ENCRYPT or HWCRYPTO_MODE_DECRYPT
  76. * @param length The length of the input data in Bytes. This must be a multiple of the block size
  77. * @param in The buffer holding the input data
  78. * @param out The buffer holding the output data
  79. *
  80. * @return RT_EOK on success.
  81. */
  82. rt_err_t rt_hwcrypto_gcm_crypt(struct rt_hwcrypto_ctx *ctx, hwcrypto_mode mode,
  83. rt_size_t length, const rt_uint8_t *in, rt_uint8_t *out);
  84. /**
  85. * @brief Set Symmetric Encryption and Decryption Key
  86. *
  87. * @param ctx GCM context
  88. * @param key The crypto key
  89. * @param bitlen The crypto key bit length
  90. *
  91. * @return RT_EOK on success.
  92. */
  93. rt_err_t rt_hwcrypto_gcm_setkey(struct rt_hwcrypto_ctx *ctx,
  94. const rt_uint8_t *key, rt_uint32_t bitlen);
  95. /**
  96. * @brief Get Symmetric Encryption and Decryption Key
  97. *
  98. * @param ctx GCM 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. rt_err_t rt_hwcrypto_gcm_getkey(struct rt_hwcrypto_ctx *ctx,
  105. rt_uint8_t *key, rt_uint32_t bitlen);
  106. /**
  107. * @brief Set Symmetric Encryption and Decryption initialization vector
  108. *
  109. * @param ctx GCM context
  110. * @param iv The crypto initialization vector
  111. * @param len The crypto initialization vector length
  112. *
  113. * @return RT_EOK on success.
  114. */
  115. rt_err_t rt_hwcrypto_gcm_setiv(struct rt_hwcrypto_ctx *ctx,
  116. const rt_uint8_t *iv, rt_size_t len);
  117. /**
  118. * @brief Get Symmetric Encryption and Decryption initialization vector
  119. *
  120. * @param ctx GCM context
  121. * @param iv The crypto initialization vector buffer
  122. * @param len The crypto initialization vector buffer length
  123. *
  124. * @return IV length of copy
  125. */
  126. rt_err_t rt_hwcrypto_gcm_getiv(struct rt_hwcrypto_ctx *ctx,
  127. rt_uint8_t *iv, rt_size_t len);
  128. /**
  129. * @brief Set offset in initialization vector
  130. *
  131. * @param ctx GCM context
  132. * @param iv_off The offset in IV
  133. */
  134. void rt_hwcrypto_gcm_set_ivoff(struct rt_hwcrypto_ctx *ctx, rt_int32_t iv_off);
  135. /**
  136. * @brief Get offset in initialization vector
  137. *
  138. * @param ctx GCM context
  139. * @param iv_off It must point to a valid memory
  140. */
  141. void rt_hwcrypto_gcm_get_ivoff(struct rt_hwcrypto_ctx *ctx, rt_int32_t *iv_off);
  142. /**
  143. * @brief This function copy GCM context
  144. *
  145. * @param des The destination GCM context
  146. * @param src The GCM context to be copy
  147. *
  148. * @return RT_EOK on success.
  149. */
  150. rt_err_t rt_hwcrypto_gcm_cpy(struct rt_hwcrypto_ctx *des,
  151. const struct rt_hwcrypto_ctx *src);
  152. /**
  153. * @brief Reset GCM context
  154. *
  155. * @param ctx GCM context
  156. */
  157. void rt_hwcrypto_gcm_reset(struct rt_hwcrypto_ctx *ctx);
  158. #ifdef __cplusplus
  159. }
  160. #endif
  161. #endif