sec_crypto_aes.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. /**
  30. \brief AES data transfer mode config
  31. */
  32. typedef enum {
  33. SC_AES_SLAVE_MODE = 0U, ///< slave mode
  34. SC_AES_DMA_MODE, ///< dma mode
  35. } sc_aes_trans_mode_t;
  36. /**
  37. \brief AES key-len-bits type
  38. */
  39. typedef enum {
  40. SC_AES_KEY_LEN_BITS_128 = 0U, ///< 128 Data bits
  41. SC_AES_KEY_LEN_BITS_192, ///< 192 Data bits
  42. SC_AES_KEY_LEN_BITS_256 ///< 256 Data bits
  43. } sc_aes_key_bits_t;
  44. /**
  45. \brief AES Ctrl Block
  46. */
  47. typedef struct {
  48. #ifdef CONFIG_SYSTEM_SECURE
  49. #ifdef CONFIG_CSI_V1
  50. aes_handle_t handle;
  51. unsigned char key[32];
  52. unsigned int key_len;
  53. #endif
  54. #ifdef CONFIG_CSI_V2
  55. csi_aes_t csi_aes;
  56. //unsigned char sc_ctx[SC_AES_CTX_SIZE];
  57. #endif
  58. #endif
  59. #if defined(CONFIG_TEE_CA)
  60. unsigned char key[32];
  61. unsigned int key_len;
  62. #endif
  63. #if defined(CONFIG_SEC_CRYPTO_AES_SW)
  64. sc_mbedtls_aes_context aes_ctx;
  65. #endif
  66. //void *ctx;
  67. } sc_aes_t;
  68. // Function documentation
  69. /**
  70. \brief Initialize AES Interface. Initializes the resources needed for the AES interface
  71. \param[in] aes operate handle
  72. \param[in] idx device id
  73. \return error code \ref uint32_t
  74. */
  75. uint32_t sc_aes_init(sc_aes_t *aes, uint32_t idx);
  76. /**
  77. \brief De-initialize AES Interface. stops operation and releases the software resources used by the interface
  78. \param[in] aes handle to operate
  79. \return None
  80. */
  81. void sc_aes_uninit(sc_aes_t *aes);
  82. /**
  83. \brief Set encrypt key
  84. \param[in] aes handle to operate
  85. \param[in] key Pointer to the key buf
  86. \param[in] key_len Pointer to \ref sc_aes_key_bits_t
  87. \return error code \ref uint32_t
  88. */
  89. uint32_t sc_aes_set_encrypt_key(sc_aes_t *aes, void *key, sc_aes_key_bits_t key_len);
  90. /**
  91. \brief Set decrypt key
  92. \param[in] aes handle to operate
  93. \param[in] key Pointer to the key buf
  94. \param[in] key_len Pointer to \ref sc_aes_key_bits_t
  95. \return error code \ref uint32_t
  96. */
  97. uint32_t sc_aes_set_decrypt_key(sc_aes_t *aes, void *key, sc_aes_key_bits_t key_len);
  98. /**
  99. \brief Aes ecb encrypt
  100. \param[in] aes handle to operate
  101. \param[in] in Pointer to the Source data
  102. \param[out] out Pointer to the Result data
  103. \param[in] size the Source data size
  104. \return error code \ref uint32_t
  105. */
  106. uint32_t sc_aes_ecb_encrypt(sc_aes_t *aes, void *in, void *out, uint32_t size);
  107. /**
  108. \brief Aes ecb decrypt
  109. \param[in] aes handle to operate
  110. \param[in] in Pointer to the Source data
  111. \param[out] out Pointer to the Result data
  112. \param[in] size the Source data size
  113. \return error code \ref uint32_t
  114. */
  115. uint32_t sc_aes_ecb_decrypt(sc_aes_t *aes, void *in, void *out, uint32_t size);
  116. /**
  117. \brief Aes cbc encrypt
  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_encrypt(sc_aes_t *aes, void *in, void *out, uint32_t size, void *iv);
  126. /**
  127. \brief Aes cbc decrypt
  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_cbc_decrypt(sc_aes_t *aes, void *in, void *out, uint32_t size, void *iv);
  136. /**
  137. \brief Aes cfb1 encrypt
  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_encrypt(sc_aes_t *aes, void *in, void *out, uint32_t size, void *iv);
  146. /**
  147. \brief Aes cfb1 decrypt
  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_cfb1_decrypt(sc_aes_t *aes, void *in, void *out, uint32_t size, void *iv);
  156. /**
  157. \brief Aes cfb8 encrypt
  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_encrypt(sc_aes_t *aes, void *in, void *out, uint32_t size, void *iv);
  166. /**
  167. \brief Aes cfb8 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. \return error code \ref uint32_t
  174. */
  175. uint32_t sc_aes_cfb8_decrypt(sc_aes_t *aes, void *in, void *out, uint32_t size, void *iv);
  176. /**
  177. \brief Aes cfb128 decrypt
  178. \param[in] aes handle to operate
  179. \param[in] in Pointer to the Source data
  180. \param[out] out Pointer to the Result data
  181. \param[in] size the Source data size
  182. \param[in] iv init vector
  183. \param[out] num the number of the 128-bit block we have used
  184. \return error code \ref uint32_t
  185. */
  186. uint32_t sc_aes_cfb128_decrypt(sc_aes_t *aes, void *in, void *out, uint32_t size, void *iv);
  187. /**
  188. \brief Aes cfb128 encrypt
  189. \param[in] aes handle to operate
  190. \param[in] in Pointer to the Source data
  191. \param[out] out Pointer to the Result data
  192. \param[in] size the Source data size
  193. \param[in] iv init vector
  194. \param[out] num the number of the 128-bit block we have used
  195. \return error code \ref uint32_t
  196. */
  197. uint32_t sc_aes_cfb128_encrypt(sc_aes_t *aes, void *in, void *out, uint32_t size, void *iv);
  198. /**
  199. \brief Aes ofb encrypt
  200. \param[in] aes handle to operate
  201. \param[in] in Pointer to the Source data
  202. \param[out] out Pointer to the Result data
  203. \param[in] size the Source data size
  204. \param[in] iv init vector
  205. \param[in] key_len key bits
  206. \return error code \ref uint32_t
  207. */
  208. uint32_t sc_aes_ofb_encrypt(sc_aes_t *aes, void *in, void *out, uint32_t size, void *iv);
  209. /**
  210. \brief Aes ofb decrypt
  211. \param[in] aes handle to operate
  212. \param[in] in Pointer to the Source data
  213. \param[out] out Pointer to the Result data
  214. \param[in] size the Source data size
  215. \param[in] iv init vector
  216. \param[in] key_len key bits
  217. \return error code \ref uint32_t
  218. */
  219. uint32_t sc_aes_ofb_decrypt(sc_aes_t *aes, void *in, void *out, uint32_t size, void *iv);
  220. /**
  221. \brief Aes ctr encrypt
  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 vector
  227. \return error code \ref uint32_t
  228. */
  229. uint32_t sc_aes_ctr_encrypt(sc_aes_t *aes, void *in, void *out, uint32_t size,void *iv);
  230. /**
  231. \brief Aes ctr decrypt
  232. \param[in] 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 vecotr
  237. \return error code \ref uint32_t
  238. */
  239. uint32_t sc_aes_ctr_decrypt(sc_aes_t *aes, void *in, void *out, uint32_t size,void *iv);
  240. /**
  241. \brief Aes gcm encrypt
  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 vector
  247. \return error code \ref csi_error_t
  248. */
  249. uint32_t sc_aes_gcm_encrypt(sc_aes_t *aes, void *in, void *out,uint32_t size, uint32_t add_len, void *iv);
  250. /**
  251. \brief Aes gcm decrypt
  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 vecotr
  257. \return error code \ref csi_error_t
  258. */
  259. uint32_t sc_aes_gcm_decrypt(sc_aes_t *aes, void *in, void *out,uint32_t size, uint32_t add_len, void *iv);
  260. /**
  261. \brief Aes gcm encrypt
  262. \param[in] dev_aes dev_aes handle to operate
  263. \param[in] in Pointer to the Source data.
  264. \param[out] out Pointer to the Result data
  265. \param[in] size the Source data size
  266. \param[in] iv init vector
  267. \param[in] tag_out tag output ,parse null if not needed
  268. \return error code \ref csi_error_t
  269. */
  270. 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);
  271. /**
  272. \brief Aes gcm decrypt
  273. \param[in] dev_aes dev_aes handle to operate
  274. \param[in] in Pointer to the Source data.
  275. \param[out] out Pointer to the Result data
  276. \param[in] size the Source data size
  277. \param[in] iv init vecotr
  278. \param[in] tag_out tag output,parse null if not needed
  279. \return error code \ref csi_error_t
  280. */
  281. 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);
  282. /**
  283. \brief Aes data transfer config
  284. */
  285. void sc_aes_trans_config(sc_aes_t *aes, sc_aes_trans_mode_t mode) ;
  286. #ifdef __cplusplus
  287. }
  288. #endif
  289. #endif /* _SC_AES_H_ */