hw_gcm.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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-05-14 tyx the first version
  9. */
  10. #include <rtthread.h>
  11. #include <rtdevice.h>
  12. #include <hw_gcm.h>
  13. /**
  14. * @brief Creating GCM Context
  15. *
  16. * @param device Hardware crypto device
  17. * @param type Type of symmetric crypto context
  18. *
  19. * @return GCM context
  20. */
  21. struct rt_hwcrypto_ctx *rt_hwcrypto_gcm_create(struct rt_hwcrypto_device *device,
  22. hwcrypto_type crypt_type)
  23. {
  24. struct rt_hwcrypto_ctx *ctx;
  25. ctx = rt_hwcrypto_ctx_create(device, HWCRYPTO_TYPE_GCM, sizeof(struct hwcrypto_gcm));
  26. if (ctx)
  27. {
  28. ((struct hwcrypto_gcm *)ctx)->crypt_type = crypt_type;
  29. }
  30. return ctx;
  31. }
  32. /**
  33. * @brief Destroy GCM Context
  34. *
  35. * @param ctx GCM context
  36. */
  37. void rt_hwcrypto_gcm_destroy(struct rt_hwcrypto_ctx *ctx)
  38. {
  39. rt_hwcrypto_ctx_destroy(ctx);
  40. }
  41. /**
  42. * @brief This function starts a GCM encryption or decryption operation
  43. *
  44. * @param ctx GCM context
  45. * @param add The buffer holding the additional data
  46. * @param add_len The length of the additional data
  47. *
  48. * @return RT_EOK on success.
  49. */
  50. rt_err_t rt_hwcrypto_gcm_start(struct rt_hwcrypto_ctx *ctx, const rt_uint8_t *add,
  51. rt_size_t add_len)
  52. {
  53. struct hwcrypto_gcm *gcm_ctx = (struct hwcrypto_gcm *)ctx;
  54. if (gcm_ctx && gcm_ctx->ops->start)
  55. {
  56. return gcm_ctx->ops->start(gcm_ctx, add, add_len);
  57. }
  58. return -RT_EINVAL;
  59. }
  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. struct hwcrypto_gcm *gcm_ctx = (struct hwcrypto_gcm *)ctx;
  73. if (gcm_ctx && gcm_ctx->ops->finish)
  74. {
  75. return gcm_ctx->ops->finish(gcm_ctx, tag, tag_len);
  76. }
  77. return -RT_EINVAL;
  78. }
  79. /**
  80. * @brief This function performs a symmetric encryption or decryption operation
  81. *
  82. * @param ctx GCM context
  83. * @param mode Operation mode. HWCRYPTO_MODE_ENCRYPT or HWCRYPTO_MODE_DECRYPT
  84. * @param length The length of the input data in Bytes. This must be a multiple of the block size
  85. * @param in The buffer holding the input data
  86. * @param out The buffer holding the output data
  87. *
  88. * @return RT_EOK on success.
  89. */
  90. rt_err_t rt_hwcrypto_gcm_crypt(struct rt_hwcrypto_ctx *ctx, hwcrypto_mode mode,
  91. rt_size_t length, const rt_uint8_t *in, rt_uint8_t *out)
  92. {
  93. return rt_hwcrypto_symmetric_crypt(ctx, mode, length, in, out);
  94. }
  95. /**
  96. * @brief Set Symmetric Encryption and Decryption Key
  97. *
  98. * @param ctx GCM context
  99. * @param key The crypto key
  100. * @param bitlen The crypto key bit length
  101. *
  102. * @return RT_EOK on success.
  103. */
  104. rt_err_t rt_hwcrypto_gcm_setkey(struct rt_hwcrypto_ctx *ctx,
  105. const rt_uint8_t *key, rt_uint32_t bitlen)
  106. {
  107. return rt_hwcrypto_symmetric_setkey(ctx, key, bitlen);
  108. }
  109. /**
  110. * @brief Get Symmetric Encryption and Decryption Key
  111. *
  112. * @param ctx GCM context
  113. * @param key The crypto key buffer
  114. * @param bitlen The crypto key bit length
  115. *
  116. * @return Key length of copy
  117. */
  118. rt_err_t rt_hwcrypto_gcm_getkey(struct rt_hwcrypto_ctx *ctx,
  119. rt_uint8_t *key, rt_uint32_t bitlen)
  120. {
  121. return rt_hwcrypto_symmetric_getkey(ctx, key, bitlen);
  122. }
  123. /**
  124. * @brief Set Symmetric Encryption and Decryption initialization vector
  125. *
  126. * @param ctx GCM context
  127. * @param iv The crypto initialization vector
  128. * @param len The crypto initialization vector length
  129. *
  130. * @return RT_EOK on success.
  131. */
  132. rt_err_t rt_hwcrypto_gcm_setiv(struct rt_hwcrypto_ctx *ctx,
  133. const rt_uint8_t *iv, rt_size_t len)
  134. {
  135. return rt_hwcrypto_symmetric_setiv(ctx, iv, len);
  136. }
  137. /**
  138. * @brief Get Symmetric Encryption and Decryption initialization vector
  139. *
  140. * @param ctx GCM context
  141. * @param iv The crypto initialization vector buffer
  142. * @param len The crypto initialization vector buffer length
  143. *
  144. * @return IV length of copy
  145. */
  146. rt_err_t rt_hwcrypto_gcm_getiv(struct rt_hwcrypto_ctx *ctx,
  147. rt_uint8_t *iv, rt_size_t len)
  148. {
  149. return rt_hwcrypto_symmetric_getiv(ctx, iv, len);
  150. }
  151. /**
  152. * @brief Set offset in initialization vector
  153. *
  154. * @param ctx GCM context
  155. * @param iv_off The offset in IV
  156. */
  157. void rt_hwcrypto_gcm_set_ivoff(struct rt_hwcrypto_ctx *ctx, rt_int32_t iv_off)
  158. {
  159. rt_hwcrypto_symmetric_set_ivoff(ctx, iv_off);
  160. }
  161. /**
  162. * @brief Get offset in initialization vector
  163. *
  164. * @param ctx GCM context
  165. * @param iv_off It must point to a valid memory
  166. */
  167. void rt_hwcrypto_gcm_get_ivoff(struct rt_hwcrypto_ctx *ctx, rt_int32_t *iv_off)
  168. {
  169. rt_hwcrypto_symmetric_get_ivoff(ctx, iv_off);
  170. }
  171. /**
  172. * @brief This function copy GCM context
  173. *
  174. * @param des The destination GCM context
  175. * @param src The GCM context to be copy
  176. *
  177. * @return RT_EOK on success.
  178. */
  179. rt_err_t rt_hwcrypto_gcm_cpy(struct rt_hwcrypto_ctx *des,
  180. const struct rt_hwcrypto_ctx *src)
  181. {
  182. struct hwcrypto_gcm *gcm_des = (struct hwcrypto_gcm *)des;
  183. struct hwcrypto_gcm *gcm_src = (struct hwcrypto_gcm *)src;
  184. if (des != RT_NULL && src != RT_NULL)
  185. {
  186. gcm_des->crypt_type = gcm_src->crypt_type;
  187. /* symmetric crypto context copy */
  188. return rt_hwcrypto_symmetric_cpy(des, src);
  189. }
  190. return -RT_EINVAL;
  191. }
  192. /**
  193. * @brief Reset GCM context
  194. *
  195. * @param ctx GCM context
  196. */
  197. void rt_hwcrypto_gcm_reset(struct rt_hwcrypto_ctx *ctx)
  198. {
  199. rt_hwcrypto_symmetric_reset(ctx);
  200. }