aes.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /*
  2. * Copyright (C) 2017-2020 Alibaba Group Holding Limited
  3. */
  4. /******************************************************************************
  5. * @file drv/aes.h
  6. * @brief Header File for AES Driver
  7. * @version V1.0
  8. * @date 9. Oct 2020
  9. * @model aes
  10. ******************************************************************************/
  11. #ifndef _DRV_AES_H_
  12. #define _DRV_AES_H_
  13. #include <stdint.h>
  14. #include "common.h"
  15. #ifdef __cplusplus
  16. extern "C" {
  17. #endif
  18. /*----- Encrypt & Decrypt: Config key length -----*/
  19. #define AES_KEY_LEN_BYTES_32 (32)
  20. #define AES_KEY_LEN_BYTES_24 (24)
  21. #define AES_KEY_LEN_BYTES_16 (16)
  22. #define AES_BLOCK_IV_SIZE (16)
  23. #define AES_BLOCK_TAG_SIZE (16)
  24. #define AES_BLOCK_CRYPTO_SIZE (16)
  25. #define AES_DIR_ENCRYPT (1)
  26. #define AES_DIR_DECRYPT (0)
  27. #define KEY_128_BITS (0x08)
  28. #define KEY_192_BITS (0x10)
  29. #define KEY_256_BITS (0x18)
  30. #define AES_DMA_ENABLE (1)
  31. #define AES_DMA_DISABLE (0)
  32. /**
  33. \brief DES data transfer mode config
  34. */
  35. typedef enum {
  36. AES_SLAVE_MODE = 0U, ///< slave mode
  37. AES_DMA_MODE, ///< dma mode
  38. } csi_aes_trans_mode_t;
  39. /**
  40. \brief AES Keylen type
  41. */
  42. typedef enum {
  43. AES_KEY_LEN_BITS_128 = 0, ///< 128 Data bits
  44. AES_KEY_LEN_BITS_192, ///< 192 Data bits
  45. AES_KEY_LEN_BITS_256 ///< 256 Data bits
  46. } csi_aes_key_bits_t;
  47. /**
  48. \brief AES mode config
  49. */
  50. typedef enum{
  51. AES_MODE_ECB = 0,
  52. AES_MODE_CBC = 0x20000020,
  53. AES_MODE_CTR = 0x200001c0,
  54. AES_MODE_CFB = 0x20000400,
  55. AES_MODE_GCM = 0x20030040,
  56. AES_MODE_CCM = 0x21D40040,
  57. AES_MODE_OFB = 0x24000000,
  58. } csi_aes_mode_t;
  59. /**
  60. \brief AES state
  61. */
  62. typedef struct {
  63. uint32_t busy : 1; ///< Calculate busy flag
  64. uint32_t error : 1; ///< Calculate error flag
  65. } csi_aes_state_t;
  66. /**
  67. \brief AES Context
  68. */
  69. typedef struct {
  70. uint32_t key_len_byte;
  71. uint8_t key[32]; ///< Data block being processed
  72. uint32_t sca;
  73. uint32_t is_kdf;
  74. uint32_t is_dma;
  75. } csi_aes_context_t;
  76. /**
  77. \brief AES Ctrl Block
  78. */
  79. typedef struct {
  80. csi_aes_state_t state;
  81. csi_aes_context_t context;
  82. csi_dev_t dev;
  83. void *priv;
  84. } csi_aes_t;
  85. /**
  86. \brief Initialize AES interface. Initializes the resources needed for the AES interface
  87. \param[in] aes Handle to operate
  88. \param[in] idx Device id
  89. \return Error code \ref csi_error_t
  90. */
  91. csi_error_t csi_aes_init(csi_aes_t *aes, uint32_t idx);
  92. /**
  93. \brief De-initialize AES interface. Stops operation and releases the software resources used by the interface
  94. \param[in] aes Dandle to operate
  95. \return None
  96. */
  97. void csi_aes_uninit(csi_aes_t *aes);
  98. /**
  99. \brief Set encrypt key
  100. \param[in] aes Handle to operate
  101. \param[in] key Pointer to the key buf
  102. \param[in] key_len Pointer to \ref csi_aes_key_bits_t
  103. \return Error code \ref Csi_error_t
  104. */
  105. csi_error_t csi_aes_set_encrypt_key(csi_aes_t *aes, void *key, csi_aes_key_bits_t key_len);
  106. /**
  107. \brief Set decrypt key
  108. \param[in] aes Handle to operate
  109. \param[in] key Pointer to the key buf
  110. \param[in] key_len Pointer to \ref csi_aes_key_bits_t
  111. \return Error code \ref Csi_error_t
  112. */
  113. csi_error_t csi_aes_set_decrypt_key(csi_aes_t *aes, void *key, csi_aes_key_bits_t key_len);
  114. /**
  115. \brief AES ecb encrypt
  116. \param[in] aes Handle to operate
  117. \param[in] in Pointer to the source data
  118. \param[out] out Pointer to the result data
  119. \param[in] size The source data size
  120. \return Error code \ref Csi_error_t
  121. */
  122. csi_error_t csi_aes_ecb_encrypt(csi_aes_t *aes, void *in, void *out, uint32_t size);
  123. /**
  124. \brief AES ecb decrypt
  125. \param[in] aes Handle to operate
  126. \param[in] in Pointer to the source data
  127. \param[out] out Pointer to the result data
  128. \param[in] size The source data size
  129. \return Error code \ref Csi_error_t
  130. */
  131. csi_error_t csi_aes_ecb_decrypt(csi_aes_t *aes, void *in, void *out, uint32_t size);
  132. /**
  133. \brief AES cbc encrypt
  134. \param[in] aes Handle to operate
  135. \param[in] in Pointer to the source data
  136. \param[out] out Pointer to the result data
  137. \param[in] size The source data size
  138. \param[in] iv Init vector
  139. \return Error code \ref Csi_error_t
  140. */
  141. csi_error_t csi_aes_cbc_encrypt(csi_aes_t *aes, void *in, void *out, uint32_t size, void *iv) ;
  142. /**
  143. \brief AES cbc decrypt
  144. \param[in] aes Handle to operate
  145. \param[in] in Pointer to the source data
  146. \param[out] out Pointer to the result data
  147. \param[in] size The source data size
  148. \param[in] iv Init vector
  149. \return Error code \ref Csi_error_t
  150. */
  151. csi_error_t csi_aes_cbc_decrypt(csi_aes_t *aes, void *in, void *out, uint32_t size, void *iv);
  152. /**
  153. \brief AES cfb1 encrypt
  154. \param[in] aes Handle to operate
  155. \param[in] in Pointer to the source data
  156. \param[out] out Pointer to the result data
  157. \param[in] size The source data size
  158. \param[in] iv Init vector
  159. \return Error code \ref Csi_error_t
  160. */
  161. csi_error_t csi_aes_cfb1_encrypt(csi_aes_t *aes, void *in, void *out, uint32_t size, void *iv);
  162. /**
  163. \brief AES cfb1 decrypt
  164. \param[in] aes Handle to operate
  165. \param[in] in Pointer to the source data
  166. \param[out] out Pointer to the result data
  167. \param[in] size The source data size
  168. \param[in] iv Init vector
  169. \return Error code \ref Csi_error_t
  170. */
  171. csi_error_t csi_aes_cfb1_decrypt(csi_aes_t *aes, void *in, void *out, uint32_t size, void *iv);
  172. /**
  173. \brief AES cfb8 encrypt
  174. \param[in] aes Handle to operate
  175. \param[in] in Pointer to the source data
  176. \param[out] out Pointer to the result data
  177. \param[in] size The source data size
  178. \param[in] iv Init vector
  179. \return Error code \ref Csi_error_t
  180. */
  181. csi_error_t csi_aes_cfb8_encrypt(csi_aes_t *aes, void *in, void *out, uint32_t size, void *iv);
  182. /**
  183. \brief AES cfb8 decrypt
  184. \param[in] aes Handle to operate
  185. \param[in] in Pointer to the source data
  186. \param[out] out Pointer to the result data
  187. \param[in] size The source data size
  188. \param[in] iv Init vector
  189. \return Error code \ref Csi_error_t
  190. */
  191. csi_error_t csi_aes_cfb8_decrypt(csi_aes_t *aes, void *in, void *out, uint32_t size, void *iv);
  192. /**
  193. \brief AES cfb128 decrypt
  194. \param[in] aes Handle to operate
  195. \param[in] in Pointer to the source data
  196. \param[out] out Pointer to the result data
  197. \param[in] size The source data size
  198. \param[in] iv Init vector
  199. \return Error code \ref csi_error_t
  200. */
  201. csi_error_t csi_aes_cfb128_decrypt(csi_aes_t *aes, void *in, void *out, uint32_t size, void *iv);
  202. /**
  203. \brief AES cfb128 encrypt
  204. \param[in] aes Handle to operate
  205. \param[in] in Pointer to the source data
  206. \param[out] out Pointer to the result data
  207. \param[in] size The source data size
  208. \param[in] iv Init vector
  209. \return Error code \ref csi_error_t
  210. */
  211. csi_error_t csi_aes_cfb128_encrypt(csi_aes_t *aes, void *in, void *out, uint32_t size, void *iv);
  212. /**
  213. \brief AES ofb encrypt
  214. \param[in] aes Handle to operate
  215. \param[in] in Pointer to the source data
  216. \param[out] out Pointer to the result data
  217. \param[in] size The source data size
  218. \param[in] iv Init vector
  219. \param[in] key_len key bits
  220. \return Error code \ref csi_error_t
  221. */
  222. csi_error_t csi_aes_ofb_encrypt(csi_aes_t *aes, void *in, void *out, uint32_t size, void *iv);
  223. /**
  224. \brief Aes ofb decrypt
  225. \param[in] dev_aes dev_aes handle to operate
  226. \param[in] in Pointer to the Source data
  227. \param[out] out Pointer to the Result data
  228. \param[in] size the Source data size
  229. \param[in] iv init vector
  230. \param[in] key_len key bits
  231. \return error code \ref csi_error_t
  232. */
  233. csi_error_t csi_aes_ofb_decrypt(csi_aes_t *aes, void *in, void *out,uint32_t size, void *iv);
  234. /**
  235. \brief AES ctr encrypt
  236. \param[in] aes Handle to operate
  237. \param[in] in Pointer to the source data
  238. \param[out] out Pointer to the result data
  239. \param[in] size The source data size
  240. \param[in] iv Init vector
  241. \return Error code \ref csi_error_t
  242. */
  243. csi_error_t csi_aes_ctr_encrypt(csi_aes_t *aes,void *in,void *out,uint32_t size,void *iv);
  244. /**
  245. \brief AES ctr decrypt
  246. \param[in] aes Handle to operate
  247. \param[in] in Pointer to the source data
  248. \param[out] out Pointer to the result data
  249. \param[in] size The source data size
  250. \param[in] iv Init vecotr
  251. \return Error code \ref csi_error_t
  252. */
  253. csi_error_t csi_aes_ctr_decrypt(csi_aes_t *aes,void *in,void *out,uint32_t size,void *iv);
  254. /**
  255. \brief Aes gcm encrypt
  256. \param[in] dev_aes dev_aes handle to operate
  257. \param[in] in Pointer to the Source data
  258. \param[out] out Pointer to the Result data
  259. \param[in] size the Source data size
  260. \param[in] iv init vector
  261. \return error code \ref csi_error_t
  262. */
  263. csi_error_t csi_aes_gcm_encrypt(csi_aes_t *aes, void *in, void *out,uint32_t size, uint32_t add_len, void *iv);
  264. /**
  265. \brief Aes gcm decrypt
  266. \param[in] dev_aes dev_aes handle to operate
  267. \param[in] in Pointer to the Source data.
  268. \param[out] out Pointer to the Result data
  269. \param[in] size the Source data size
  270. \param[in] iv init vecotr
  271. \return error code \ref csi_error_t
  272. */
  273. csi_error_t csi_aes_gcm_decrypt(csi_aes_t *aes, void *in, void *out,uint32_t size, uint32_t add_len, void *iv);
  274. /**
  275. \brief Aes ccm encrypt
  276. \param[in] dev_aes dev_aes handle to operate
  277. \param[in] in Pointer to the Source data
  278. \param[out] out Pointer to the Result data
  279. \param[in] size the Source data size
  280. \param[in] iv init vector
  281. \param[in] tag_out tag output
  282. \return error code \ref csi_error_t
  283. */
  284. csi_error_t csi_aes_ccm_encrypt(csi_aes_t *aes, void *in, void *out,uint32_t size, uint32_t add_len, void *iv, uint8_t *tag_out);
  285. /**
  286. \brief Aes ccm decrypt
  287. \param[in] dev_aes dev_aes handle to operate
  288. \param[in] in Pointer to the Source data
  289. \param[out] out Pointer to the Result data
  290. \param[in] size the Source data size
  291. \param[in] iv init vecotr
  292. \param[in] tag_out tag output
  293. \return error code \ref csi_error_t
  294. */
  295. csi_error_t csi_aes_ccm_decrypt(csi_aes_t *aes, void *in, void *out,uint32_t size, uint32_t add_len, void *iv, uint8_t *tag_out);
  296. /**
  297. \brief Enable AES power manage
  298. \param[in] aes Handle to operate
  299. \return Error code \ref csi_error_t
  300. */
  301. csi_error_t csi_aes_enable_pm(csi_aes_t *aes);
  302. /**
  303. \brief Disable AES power manage
  304. \param[in] aes Handle to operate
  305. \return None
  306. */
  307. void csi_aes_disable_pm(csi_aes_t *aes);
  308. /**
  309. \brief Config AES data transfer mode
  310. \param[in] mode \ref csi_des_trans_mode_t
  311. \return None
  312. */
  313. void csi_aes_trans_config(csi_aes_t *aes, csi_aes_trans_mode_t mode);
  314. #ifdef __cplusplus
  315. }
  316. #endif
  317. #endif /* _DRV_AES_H_ */