aes.h 12 KB

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