sec_crypto_aes.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*
  2. * Copyright (C) 2017-2020 Alibaba Group Holding Limited
  3. */
  4. /******************************************************************************
  5. * @file seccrypt_aes.h
  6. * @brief Header File for AES
  7. * @version V1.0
  8. * @date 20. Jul 2020
  9. * @model aes
  10. ******************************************************************************/
  11. #ifndef _SC_AES_H_
  12. #define _SC_AES_H_
  13. #include "sec_include_config.h"
  14. #include <stdint.h>
  15. #include "sec_crypto_errcode.h"
  16. #ifdef CONFIG_SYSTEM_SECURE
  17. #ifdef SEC_LIB_VERSION
  18. #include <drv/aes.h>
  19. #else
  20. #include "aes.h"
  21. #endif
  22. #endif
  23. #ifdef CONFIG_SEC_CRYPTO_AES_SW
  24. #include "crypto_aes.h"
  25. #endif
  26. #ifdef __cplusplus
  27. extern "C" {
  28. #endif
  29. typedef enum {
  30. SC_AES_KEY_LEN_BITS_128 = 0U, ///< 128 Data bits
  31. SC_AES_KEY_LEN_BITS_192, ///< 192 Data bits
  32. SC_AES_KEY_LEN_BITS_256 ///< 256 Data bits
  33. } sc_aes_key_bits_t;
  34. /**
  35. \brief AES Ctrl Block
  36. */
  37. typedef struct {
  38. #ifdef CONFIG_SYSTEM_SECURE
  39. #ifdef CONFIG_CSI_V1
  40. aes_handle_t handle;
  41. unsigned char key[32];
  42. unsigned int key_len;
  43. #endif
  44. #ifdef CONFIG_CSI_V2
  45. csi_aes_t csi_aes;
  46. //unsigned char sc_ctx[SC_AES_CTX_SIZE];
  47. #endif
  48. #endif
  49. #if defined(CONFIG_TEE_CA)
  50. unsigned char key[32];
  51. unsigned int key_len;
  52. #endif
  53. #if defined(CONFIG_SEC_CRYPTO_AES_SW)
  54. sc_mbedtls_aes_context aes_ctx;
  55. #endif
  56. //void *ctx;
  57. } sc_aes_t;
  58. // Function documentation
  59. /**
  60. \brief Initialize AES Interface. Initializes the resources needed for the AES interface
  61. \param[in] aes operate handle
  62. \param[in] idx device id
  63. \return error code \ref uint32_t
  64. */
  65. uint32_t sc_aes_init(sc_aes_t *aes, uint32_t idx);
  66. /**
  67. \brief De-initialize AES Interface. stops operation and releases the software resources used by the interface
  68. \param[in] aes handle to operate
  69. \return None
  70. */
  71. void sc_aes_uninit(sc_aes_t *aes);
  72. /**
  73. \brief Set encrypt key
  74. \param[in] aes handle to operate
  75. \param[in] key Pointer to the key buf
  76. \param[in] key_len Pointer to \ref sc_aes_key_bits_t
  77. \return error code \ref uint32_t
  78. */
  79. uint32_t sc_aes_set_encrypt_key(sc_aes_t *aes, void *key, sc_aes_key_bits_t key_len);
  80. /**
  81. \brief Set decrypt key
  82. \param[in] aes handle to operate
  83. \param[in] key Pointer to the key buf
  84. \param[in] key_len Pointer to \ref sc_aes_key_bits_t
  85. \return error code \ref uint32_t
  86. */
  87. uint32_t sc_aes_set_decrypt_key(sc_aes_t *aes, void *key, sc_aes_key_bits_t key_len);
  88. /**
  89. \brief Aes ecb encrypt
  90. \param[in] aes handle to operate
  91. \param[in] in Pointer to the Source data
  92. \param[out] out Pointer to the Result data
  93. \param[in] size the Source data size
  94. \return error code \ref uint32_t
  95. */
  96. uint32_t sc_aes_ecb_encrypt(sc_aes_t *aes, void *in, void *out, uint32_t size);
  97. /**
  98. \brief Aes ecb decrypt
  99. \param[in] aes handle to operate
  100. \param[in] in Pointer to the Source data
  101. \param[out] out Pointer to the Result data
  102. \param[in] size the Source data size
  103. \return error code \ref uint32_t
  104. */
  105. uint32_t sc_aes_ecb_decrypt(sc_aes_t *aes, void *in, void *out, uint32_t size);
  106. /**
  107. \brief Aes cbc encrypt
  108. \param[in] aes handle to operate
  109. \param[in] in Pointer to the Source data
  110. \param[out] out Pointer to the Result data
  111. \param[in] size the Source data size
  112. \param[in] iv init vector
  113. \return error code \ref uint32_t
  114. */
  115. uint32_t sc_aes_cbc_encrypt(sc_aes_t *aes, void *in, void *out, uint32_t size, void *iv);
  116. /**
  117. \brief Aes cbc decrypt
  118. \param[in] aes handle to operate
  119. \param[in] in Pointer to the Source data
  120. \param[out] out Pointer to the Result data
  121. \param[in] size the Source data size
  122. \param[in] iv init vector
  123. \return error code \ref uint32_t
  124. */
  125. uint32_t sc_aes_cbc_decrypt(sc_aes_t *aes, void *in, void *out, uint32_t size, void *iv);
  126. /**
  127. \brief Aes cfb1 encrypt
  128. \param[in] aes handle to operate
  129. \param[in] in Pointer to the Source data
  130. \param[out] out Pointer to the Result data
  131. \param[in] size the Source data size
  132. \param[in] iv init vector
  133. \return error code \ref uint32_t
  134. */
  135. uint32_t sc_aes_cfb1_encrypt(sc_aes_t *aes, void *in, void *out, uint32_t size, void *iv);
  136. /**
  137. \brief Aes cfb1 decrypt
  138. \param[in] aes handle to operate
  139. \param[in] in Pointer to the Source data
  140. \param[out] out Pointer to the Result data
  141. \param[in] size the Source data size
  142. \param[in] iv init vector
  143. \return error code \ref uint32_t
  144. */
  145. uint32_t sc_aes_cfb1_decrypt(sc_aes_t *aes, void *in, void *out, uint32_t size, void *iv);
  146. /**
  147. \brief Aes cfb8 encrypt
  148. \param[in] aes handle to operate
  149. \param[in] in Pointer to the Source data
  150. \param[out] out Pointer to the Result data
  151. \param[in] size the Source data size
  152. \param[in] iv init vector
  153. \return error code \ref uint32_t
  154. */
  155. uint32_t sc_aes_cfb8_encrypt(sc_aes_t *aes, void *in, void *out, uint32_t size, void *iv);
  156. /**
  157. \brief Aes cfb8 decrypt
  158. \param[in] aes handle to operate
  159. \param[in] in Pointer to the Source data
  160. \param[out] out Pointer to the Result data
  161. \param[in] size the Source data size
  162. \param[in] iv init vector
  163. \return error code \ref uint32_t
  164. */
  165. uint32_t sc_aes_cfb8_decrypt(sc_aes_t *aes, void *in, void *out, uint32_t size, void *iv);
  166. /**
  167. \brief Aes cfb128 decrypt
  168. \param[in] aes handle to operate
  169. \param[in] in Pointer to the Source data
  170. \param[out] out Pointer to the Result data
  171. \param[in] size the Source data size
  172. \param[in] iv init vector
  173. \param[out] num the number of the 128-bit block we have used
  174. \return error code \ref uint32_t
  175. */
  176. uint32_t sc_aes_cfb128_decrypt(sc_aes_t *aes, void *in, void *out, uint32_t size, void *iv);
  177. /**
  178. \brief Aes cfb128 encrypt
  179. \param[in] aes handle to operate
  180. \param[in] in Pointer to the Source data
  181. \param[out] out Pointer to the Result data
  182. \param[in] size the Source data size
  183. \param[in] iv init vector
  184. \param[out] num the number of the 128-bit block we have used
  185. \return error code \ref uint32_t
  186. */
  187. uint32_t sc_aes_cfb128_encrypt(sc_aes_t *aes, void *in, void *out, uint32_t size, void *iv);
  188. /**
  189. \brief Aes ofb encrypt
  190. \param[in] aes handle to operate
  191. \param[in] in Pointer to the Source data
  192. \param[out] out Pointer to the Result data
  193. \param[in] size the Source data size
  194. \param[in] iv init vector
  195. \param[in] key_len key bits
  196. \return error code \ref uint32_t
  197. */
  198. uint32_t sc_aes_ofb_encrypt(sc_aes_t *aes, void *in, void *out, uint32_t size, void *iv);
  199. /**
  200. \brief Aes ofb decrypt
  201. \param[in] aes handle to operate
  202. \param[in] in Pointer to the Source data
  203. \param[out] out Pointer to the Result data
  204. \param[in] size the Source data size
  205. \param[in] iv init vector
  206. \param[in] key_len key bits
  207. \return error code \ref uint32_t
  208. */
  209. uint32_t sc_aes_ofb_decrypt(sc_aes_t *aes, void *in, void *out, uint32_t size, void *iv);
  210. /**
  211. \brief Aes ctr encrypt
  212. \param[in] aes handle to operate
  213. \param[in] in Pointer to the Source data
  214. \param[out] out Pointer to the Result data
  215. \param[in] size the Source data size
  216. \param[in] iv init vector
  217. \return error code \ref uint32_t
  218. */
  219. uint32_t sc_aes_ctr_encrypt(sc_aes_t *aes, void *in, void *out, uint32_t size,void *iv);
  220. /**
  221. \brief Aes ctr decrypt
  222. \param[in] aes handle to operate
  223. \param[in] in Pointer to the Source data
  224. \param[out] out Pointer to the Result data
  225. \param[in] size the Source data size
  226. \param[in] iv init vecotr
  227. \return error code \ref uint32_t
  228. */
  229. uint32_t sc_aes_ctr_decrypt(sc_aes_t *aes, void *in, void *out, uint32_t size,void *iv);
  230. /**
  231. \brief Aes gcm encrypt
  232. \param[in] dev_aes dev_aes handle to operate
  233. \param[in] in Pointer to the Source data.
  234. \param[out] out Pointer to the Result data
  235. \param[in] size the Source data size
  236. \param[in] iv init vector
  237. \return error code \ref csi_error_t
  238. */
  239. uint32_t sc_aes_gcm_encrypt(sc_aes_t *aes, void *in, void *out,uint32_t size, uint32_t add_len, void *iv);
  240. /**
  241. \brief Aes gcm decrypt
  242. \param[in] dev_aes dev_aes handle to operate
  243. \param[in] in Pointer to the Source data.
  244. \param[out] out Pointer to the Result data
  245. \param[in] size the Source data size
  246. \param[in] iv init vecotr
  247. \return error code \ref csi_error_t
  248. */
  249. uint32_t sc_aes_gcm_decrypt(sc_aes_t *aes, void *in, void *out,uint32_t size, uint32_t add_len, void *iv);
  250. /**
  251. \brief Aes gcm encrypt
  252. \param[in] dev_aes dev_aes handle to operate
  253. \param[in] in Pointer to the Source data.
  254. \param[out] out Pointer to the Result data
  255. \param[in] size the Source data size
  256. \param[in] iv init vector
  257. \param[in] tag_out tag output ,parse null if not needed
  258. \return error code \ref csi_error_t
  259. */
  260. uint32_t sc_aes_ccm_encrypt(sc_aes_t *aes, void *in, void *out,uint32_t size, uint32_t add_len, void *iv, uint8_t* tag_out);
  261. /**
  262. \brief Aes gcm decrypt
  263. \param[in] dev_aes dev_aes handle to operate
  264. \param[in] in Pointer to the Source data.
  265. \param[out] out Pointer to the Result data
  266. \param[in] size the Source data size
  267. \param[in] iv init vecotr
  268. \param[in] tag_out tag output,parse null if not needed
  269. \return error code \ref csi_error_t
  270. */
  271. uint32_t sc_aes_ccm_decrypt(sc_aes_t *aes, void *in, void *out,uint32_t size, uint32_t add_len, void *iv, uint8_t* tag_out);
  272. void sc_aes_dma_enable(sc_aes_t *aes, uint8_t en);
  273. #ifdef __cplusplus
  274. }
  275. #endif
  276. #endif /* _SC_AES_H_ */