hw_symmetric.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. #include <rtthread.h>
  11. #include <rtdevice.h>
  12. #include <hw_symmetric.h>
  13. /**
  14. * @brief Creating Symmetric Encryption and Decryption Context
  15. *
  16. * @param device Hardware crypto device
  17. * @param type Type of symmetric crypto context
  18. *
  19. * @return Symmetric crypto context
  20. */
  21. struct rt_hwcrypto_ctx *rt_hwcrypto_symmetric_create(struct rt_hwcrypto_device *device, hwcrypto_type type)
  22. {
  23. struct rt_hwcrypto_ctx *ctx;
  24. ctx = rt_hwcrypto_ctx_create(device, type, sizeof(struct hwcrypto_symmetric));
  25. return ctx;
  26. }
  27. /**
  28. * @brief Destroy Symmetric Encryption and Decryption Context
  29. *
  30. * @param ctx Symmetric crypto context
  31. */
  32. void rt_hwcrypto_symmetric_destroy(struct rt_hwcrypto_ctx *ctx)
  33. {
  34. rt_hwcrypto_ctx_destroy(ctx);
  35. }
  36. /**
  37. * @brief This function performs a symmetric encryption or decryption operation
  38. *
  39. * @param ctx Symmetric crypto context
  40. * @param mode Operation mode. HWCRYPTO_MODE_ENCRYPT or HWCRYPTO_MODE_DECRYPT
  41. * @param length The length of the input data in Bytes. This must be a multiple of the block size
  42. * @param in The buffer holding the input data
  43. * @param out The buffer holding the output data
  44. *
  45. * @return RT_EOK on success.
  46. */
  47. rt_err_t rt_hwcrypto_symmetric_crypt(struct rt_hwcrypto_ctx *ctx, hwcrypto_mode mode, rt_size_t length, const rt_uint8_t *in, rt_uint8_t *out)
  48. {
  49. struct hwcrypto_symmetric *symmetric_ctx;
  50. struct hwcrypto_symmetric_info symmetric_info;
  51. rt_err_t err;
  52. if (ctx == RT_NULL)
  53. {
  54. return -RT_EINVAL;
  55. }
  56. symmetric_ctx = (struct hwcrypto_symmetric *)ctx;
  57. if (symmetric_ctx->ops->crypt == RT_NULL)
  58. {
  59. return -RT_ERROR;
  60. }
  61. if (mode != HWCRYPTO_MODE_ENCRYPT && mode != HWCRYPTO_MODE_DECRYPT)
  62. {
  63. return -RT_EINVAL;
  64. }
  65. /* Input information packaging */
  66. symmetric_info.mode = mode;
  67. symmetric_info.in = in;
  68. symmetric_info.out = out;
  69. symmetric_info.length = length;
  70. /* Calling Hardware Encryption and Decryption Function */
  71. err = symmetric_ctx->ops->crypt(symmetric_ctx, &symmetric_info);
  72. /* clean up flags */
  73. symmetric_ctx->flags &= ~(SYMMTRIC_MODIFY_KEY | SYMMTRIC_MODIFY_IV | SYMMTRIC_MODIFY_IVOFF);
  74. return err;
  75. }
  76. /**
  77. * @brief Set Symmetric Encryption and Decryption Key
  78. *
  79. * @param ctx Symmetric crypto context
  80. * @param key The crypto key
  81. * @param bitlen The crypto key bit length
  82. *
  83. * @return RT_EOK on success.
  84. */
  85. rt_err_t rt_hwcrypto_symmetric_setkey(struct rt_hwcrypto_ctx *ctx, const rt_uint8_t *key, rt_uint32_t bitlen)
  86. {
  87. struct hwcrypto_symmetric *symmetric_ctx;
  88. if (ctx && bitlen <= RT_HWCRYPTO_KEYBIT_MAX_SIZE)
  89. {
  90. symmetric_ctx = (struct hwcrypto_symmetric *)ctx;
  91. rt_memcpy(symmetric_ctx->key, key, bitlen >> 3);
  92. /* Record key length */
  93. symmetric_ctx->key_bitlen = bitlen;
  94. /* Key change flag set up */
  95. symmetric_ctx->flags |= SYMMTRIC_MODIFY_KEY;
  96. return RT_EOK;
  97. }
  98. return -RT_EINVAL;
  99. }
  100. /**
  101. * @brief Get Symmetric Encryption and Decryption Key
  102. *
  103. * @param ctx Symmetric crypto context
  104. * @param key The crypto key buffer
  105. * @param bitlen The crypto key bit length
  106. *
  107. * @return Key length of copy
  108. */
  109. int rt_hwcrypto_symmetric_getkey(struct rt_hwcrypto_ctx *ctx, rt_uint8_t *key, rt_uint32_t bitlen)
  110. {
  111. struct hwcrypto_symmetric *symmetric_ctx = (struct hwcrypto_symmetric *)ctx;
  112. if (ctx && bitlen >= symmetric_ctx->key_bitlen)
  113. {
  114. rt_memcpy(key, symmetric_ctx->key, symmetric_ctx->key_bitlen >> 3);
  115. return symmetric_ctx->key_bitlen;
  116. }
  117. return 0;
  118. }
  119. /**
  120. * @brief Set Symmetric Encryption and Decryption initialization vector
  121. *
  122. * @param ctx Symmetric crypto context
  123. * @param iv The crypto initialization vector
  124. * @param len The crypto initialization vector length
  125. *
  126. * @return RT_EOK on success.
  127. */
  128. rt_err_t rt_hwcrypto_symmetric_setiv(struct rt_hwcrypto_ctx *ctx, const rt_uint8_t *iv, rt_size_t len)
  129. {
  130. struct hwcrypto_symmetric *symmetric_ctx;
  131. if (ctx && len <= RT_HWCRYPTO_IV_MAX_SIZE)
  132. {
  133. symmetric_ctx = (struct hwcrypto_symmetric *)ctx;
  134. rt_memcpy(symmetric_ctx->iv, iv, len);
  135. symmetric_ctx->iv_len = len;
  136. /* IV change flag set up */
  137. symmetric_ctx->flags |= SYMMTRIC_MODIFY_IV;
  138. return RT_EOK;
  139. }
  140. return -RT_EINVAL;
  141. }
  142. /**
  143. * @brief Get Symmetric Encryption and Decryption initialization vector
  144. *
  145. * @param ctx Symmetric crypto context
  146. * @param iv The crypto initialization vector buffer
  147. * @param len The crypto initialization vector buffer length
  148. *
  149. * @return IV length of copy
  150. */
  151. int rt_hwcrypto_symmetric_getiv(struct rt_hwcrypto_ctx *ctx, rt_uint8_t *iv, rt_size_t len)
  152. {
  153. struct hwcrypto_symmetric *symmetric_ctx = (struct hwcrypto_symmetric *)ctx;;
  154. if (ctx && len >= symmetric_ctx->iv_len)
  155. {
  156. rt_memcpy(iv, symmetric_ctx->iv, symmetric_ctx->iv_len);
  157. return symmetric_ctx->iv_len;
  158. }
  159. return 0;
  160. }
  161. /**
  162. * @brief Set offset in initialization vector
  163. *
  164. * @param ctx Symmetric crypto context
  165. * @param iv_off The offset in IV
  166. */
  167. void rt_hwcrypto_symmetric_set_ivoff(struct rt_hwcrypto_ctx *ctx, rt_int32_t iv_off)
  168. {
  169. if (ctx)
  170. {
  171. ((struct hwcrypto_symmetric *)ctx)->iv_off = iv_off;
  172. /* iv_off change flag set up */
  173. ((struct hwcrypto_symmetric *)ctx)->flags |= SYMMTRIC_MODIFY_IVOFF;
  174. }
  175. }
  176. /**
  177. * @brief Get offset in initialization vector
  178. *
  179. * @param ctx Symmetric crypto context
  180. * @param iv_off It must point to a valid memory
  181. */
  182. void rt_hwcrypto_symmetric_get_ivoff(struct rt_hwcrypto_ctx *ctx, rt_int32_t *iv_off)
  183. {
  184. if (ctx && iv_off)
  185. {
  186. *iv_off = ((struct hwcrypto_symmetric *)ctx)->iv_off;
  187. }
  188. }
  189. /**
  190. * @brief This function copy symmetric crypto context
  191. *
  192. * @param des The destination symmetric crypto context
  193. * @param src The symmetric crypto context to be copy
  194. *
  195. * @return RT_EOK on success.
  196. */
  197. rt_err_t rt_hwcrypto_symmetric_cpy(struct rt_hwcrypto_ctx *des, const struct rt_hwcrypto_ctx *src)
  198. {
  199. struct hwcrypto_symmetric *symmetric_des = (struct hwcrypto_symmetric *)des;
  200. struct hwcrypto_symmetric *symmetric_src = (struct hwcrypto_symmetric *)src;
  201. if (des != RT_NULL && src != RT_NULL)
  202. {
  203. /* Copy Symmetric Encryption and Decryption Context Information */
  204. symmetric_des->flags = symmetric_src->flags ;
  205. symmetric_des->iv_len = symmetric_src->iv_len ;
  206. symmetric_des->iv_off = symmetric_src->iv_off ;
  207. symmetric_des->key_bitlen = symmetric_src->key_bitlen;
  208. rt_memcpy(symmetric_des->iv, symmetric_src->iv, symmetric_src->iv_len);
  209. rt_memcpy(symmetric_des->key, symmetric_src->key, symmetric_src->key_bitlen >> 3);
  210. /* Hardware context copy */
  211. return rt_hwcrypto_ctx_cpy(des, src);
  212. }
  213. return -RT_EINVAL;
  214. }
  215. /**
  216. * @brief Reset symmetric crypto context
  217. *
  218. * @param ctx Symmetric crypto context
  219. */
  220. void rt_hwcrypto_symmetric_reset(struct rt_hwcrypto_ctx *ctx)
  221. {
  222. struct hwcrypto_symmetric *symmetric_ctx = (struct hwcrypto_symmetric *)ctx;
  223. if (ctx != RT_NULL)
  224. {
  225. /* Copy Symmetric Encryption and Decryption Context Information */
  226. symmetric_ctx->flags = 0x00;
  227. symmetric_ctx->iv_len = 0x00;
  228. symmetric_ctx->iv_off = 0x00;
  229. symmetric_ctx->key_bitlen = 0x00;
  230. rt_memset(symmetric_ctx->iv, 0, RT_HWCRYPTO_IV_MAX_SIZE);
  231. rt_memset(symmetric_ctx->key, 0, RT_HWCRYPTO_KEYBIT_MAX_SIZE >> 3);
  232. /* Hardware context reset */
  233. rt_hwcrypto_ctx_reset(ctx);
  234. }
  235. }
  236. /**
  237. * @brief Setting symmetric crypto context type
  238. *
  239. * @param ctx Symmetric crypto context
  240. * @param type Types of settings
  241. *
  242. * @return RT_EOK on success.
  243. */
  244. rt_err_t rt_hwcrypto_symmetric_set_type(struct rt_hwcrypto_ctx *ctx, hwcrypto_type type)
  245. {
  246. return rt_hwcrypto_set_type(ctx, type);
  247. }