aes.h 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  1. /* Copyright 2018 Canaan Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. #ifndef _DRIVER_AES_H
  16. #define _DRIVER_AES_H
  17. #include <stdint.h>
  18. #include <stdlib.h>
  19. #include "dmac.h"
  20. #include "platform.h"
  21. #ifdef __cplusplus
  22. extern "C" {
  23. #endif
  24. typedef enum _aes_cipher_mode
  25. {
  26. AES_ECB = 0,
  27. AES_CBC = 1,
  28. AES_GCM = 2,
  29. AES_CIPHER_MAX,
  30. } aes_cipher_mode_t;
  31. typedef enum _aes_kmode
  32. {
  33. AES_128 = 16,
  34. AES_192 = 24,
  35. AES_256 = 32,
  36. } aes_kmode_t;
  37. typedef enum _aes_iv_len
  38. {
  39. IV_LEN_96 = 12,
  40. IV_LEN_128 = 16,
  41. } aes_iv_len_t;
  42. typedef enum _aes_encrypt_sel
  43. {
  44. AES_HARD_ENCRYPTION = 0,
  45. AES_HARD_DECRYPTION = 1,
  46. } aes_encrypt_sel_t;
  47. typedef struct _aes_mode_ctl
  48. {
  49. /* [2:0]:000:ecb; 001:cbc,010:gcm */
  50. uint32_t cipher_mode : 3;
  51. /* [4:3]:00:aes-128; 01:aes-192; 10:aes-256;11:reserved*/
  52. uint32_t kmode : 2;
  53. /* [6:5]:input key order 1:little endian; 0: big endian */
  54. uint32_t key_order : 2;
  55. /* [8:7]:input data order 1:little endian; 0: big endian */
  56. uint32_t input_order : 2;
  57. /* [10:9]:output data order 1:little endian; 0: big endian */
  58. uint32_t output_order : 2;
  59. uint32_t reserved : 21;
  60. } __attribute__((packed, aligned(4))) aes_mode_ctl_t;
  61. /**
  62. * @brief AES
  63. */
  64. typedef struct _aes
  65. {
  66. /* (0x00) customer key.1st~4th byte key */
  67. uint32_t aes_key[4];
  68. /* (0x10) 0: encryption; 1: decryption */
  69. uint32_t encrypt_sel;
  70. /* (0x14) aes mode reg */
  71. aes_mode_ctl_t mode_ctl;
  72. /* (0x18) Initialisation Vector. GCM support 96bit. CBC support 128bit */
  73. uint32_t aes_iv[4];
  74. /* (0x28) input data endian;1:little endian; 0:big endian */
  75. uint32_t aes_endian;
  76. /* (0x2c) calculate status. 1:finish; 0:not finish */
  77. uint32_t aes_finish;
  78. /* (0x30) aes out data to dma 0:cpu 1:dma */
  79. uint32_t dma_sel;
  80. /* (0x34) gcm Additional authenticated data number */
  81. uint32_t gb_aad_num;
  82. uint32_t reserved;
  83. /* (0x3c) aes plantext/ciphter text input data number */
  84. uint32_t gb_pc_num;
  85. /* (0x40) aes plantext/ciphter text input data */
  86. uint32_t aes_text_data;
  87. /* (0x44) Additional authenticated data */
  88. uint32_t aes_aad_data;
  89. /**
  90. * (0x48) [1:0],b'00:check not finish; b'01:check fail; b'10:check success;
  91. * b'11:reversed
  92. */
  93. uint32_t tag_chk;
  94. /* (0x4c) data can input flag. 1: data can input; 0 : data cannot input */
  95. uint32_t data_in_flag;
  96. /* (0x50) gcm input tag for compare with the calculate tag */
  97. uint32_t gcm_in_tag[4];
  98. /* (0x60) aes plantext/ciphter text output data */
  99. uint32_t aes_out_data;
  100. /* (0x64) aes module enable */
  101. uint32_t gb_aes_en;
  102. /* (0x68) data can output flag 1: data ready 0: data not ready */
  103. uint32_t data_out_flag;
  104. /* (0x6c) allow tag input when use gcm */
  105. uint32_t tag_in_flag;
  106. /* (0x70) clear tag_chk */
  107. uint32_t tag_clear;
  108. uint32_t gcm_out_tag[4];
  109. /* (0x84) customer key for aes-192 aes-256.5th~8th byte key */
  110. uint32_t aes_key_ext[4];
  111. } __attribute__((packed, aligned(4))) aes_t;
  112. typedef struct _gcm_context
  113. {
  114. /* The buffer holding the encryption or decryption key. */
  115. uint8_t *input_key;
  116. /* The initialization vector. must be 96 bit */
  117. uint8_t *iv;
  118. /* The buffer holding the Additional authenticated data. or NULL */
  119. uint8_t *gcm_aad;
  120. /* The length of the Additional authenticated data. or 0L */
  121. size_t gcm_aad_len;
  122. } gcm_context_t;
  123. typedef struct _cbc_context
  124. {
  125. /* The buffer holding the encryption or decryption key. */
  126. uint8_t *input_key;
  127. /* The initialization vector. must be 128 bit */
  128. uint8_t *iv;
  129. } cbc_context_t;
  130. /**
  131. * @brief AES-ECB-128 decryption
  132. *
  133. * @param[in] input_key The decryption key. must be 16bytes.
  134. * @param[in] input_data The buffer holding the input data.
  135. * @param[in] input_len The length of a data unit in bytes.
  136. * This can be any length between 16 bytes and 2^31 bytes inclusive
  137. * (between 1 and 2^27 block cipher blocks).
  138. * @param[out] output_data The buffer holding the output data.
  139. * The buffer size must be larger than the size after padding by 16 byte multiples.
  140. */
  141. void aes_ecb128_hard_decrypt(uint8_t *input_key, uint8_t *input_data, size_t input_len, uint8_t *output_data);
  142. /**
  143. * @brief AES-ECB-128 encryption
  144. *
  145. * @param[in] input_key The encryption key. must be 16bytes.
  146. * @param[in] input_data The buffer holding the input data.
  147. * @param[in] input_len The length of a data unit in bytes.
  148. * This can be any length between 16 bytes and 2^31 bytes inclusive
  149. * (between 1 and 2^27 block cipher blocks).
  150. * @param[out] output_data The buffer holding the output data.
  151. * The buffer size must be larger than the size after padding by 16 byte multiples.
  152. */
  153. void aes_ecb128_hard_encrypt(uint8_t *input_key, uint8_t *input_data, size_t input_len, uint8_t *output_data);
  154. /**
  155. * @brief AES-ECB-192 decryption
  156. *
  157. * @param[in] input_key The decryption key. must be 24bytes.
  158. * @param[in] input_data The buffer holding the input data.
  159. * @param[in] input_len The length of a data unit in bytes.
  160. * This can be any length between 16 bytes and 2^31 bytes inclusive
  161. * (between 1 and 2^27 block cipher blocks).
  162. * @param[out] output_data The buffer holding the output data.
  163. * The buffer size must be larger than the size after padding by 16 byte multiples.
  164. */
  165. void aes_ecb192_hard_decrypt(uint8_t *input_key, uint8_t *input_data, size_t input_len, uint8_t *output_data);
  166. /**
  167. * @brief AES-ECB-192 encryption
  168. *
  169. * @param[in] input_key The encryption key. must be 24bytes.
  170. * @param[in] input_data The buffer holding the input data.
  171. * @param[in] input_len The length of a data unit in bytes.
  172. * This can be any length between 16 bytes and 2^31 bytes inclusive
  173. * (between 1 and 2^27 block cipher blocks).
  174. * @param[out] output_data The buffer holding the output data.
  175. * The buffer size must be larger than the size after padding by 16 byte multiples.
  176. */
  177. void aes_ecb192_hard_encrypt(uint8_t *input_key, uint8_t *input_data, size_t input_len, uint8_t *output_data);
  178. /**
  179. * @brief AES-ECB-256 decryption
  180. *
  181. * @param[in] input_key The decryption key. must be 32bytes.
  182. * @param[in] input_data The buffer holding the input data.
  183. * @param[in] input_len The length of a data unit in bytes.
  184. * This can be any length between 16 bytes and 2^31 bytes inclusive
  185. * (between 1 and 2^27 block cipher blocks).
  186. * @param[out] output_data The buffer holding the output data.
  187. * The buffer size must be larger than the size after padding by 16 byte multiples.
  188. */
  189. void aes_ecb256_hard_decrypt(uint8_t *input_key, uint8_t *input_data, size_t input_len, uint8_t *output_data);
  190. /**
  191. * @brief AES-ECB-256 encryption
  192. *
  193. * @param[in] input_key The encryption key. must be 32bytes.
  194. * @param[in] input_data The buffer holding the input data.
  195. * @param[in] input_len The length of a data unit in bytes.
  196. * This can be any length between 16 bytes and 2^31 bytes inclusive
  197. * (between 1 and 2^27 block cipher blocks).
  198. * @param[out] output_data The buffer holding the output data.
  199. * The buffer size must be larger than the size after padding by 16 byte multiples.
  200. */
  201. void aes_ecb256_hard_encrypt(uint8_t *input_key, uint8_t *input_data, size_t input_len, uint8_t *output_data);
  202. /**
  203. * @brief AES-CBC-128 decryption
  204. *
  205. * @param[in] context The cbc context to use for encryption or decryption.
  206. * @param[in] input_key The decryption key. must be 16bytes.
  207. * @param[in] input_data The buffer holding the input data.
  208. * @param[in] input_len The length of a data unit in bytes.
  209. * This can be any length between 16 bytes and 2^31 bytes inclusive
  210. * (between 1 and 2^27 block cipher blocks).
  211. * @param[out] output_data The buffer holding the output data.
  212. * The buffer size must be larger than the size after padding by 16 byte multiples.
  213. */
  214. void aes_cbc128_hard_decrypt(cbc_context_t *context, uint8_t *input_data, size_t input_len, uint8_t *output_data);
  215. /**
  216. * @brief AES-CBC-128 encryption
  217. *
  218. * @param[in] context The cbc context to use for encryption or decryption.
  219. * @param[in] input_key The encryption key. must be 16bytes.
  220. * @param[in] input_data The buffer holding the input data.
  221. * @param[in] input_len The length of a data unit in bytes.
  222. * This can be any length between 16 bytes and 2^31 bytes inclusive
  223. * (between 1 and 2^27 block cipher blocks).
  224. * @param[out] output_data The buffer holding the output data.
  225. * The buffer size must be larger than the size after padding by 16 byte multiples.
  226. */
  227. void aes_cbc128_hard_encrypt(cbc_context_t *context, uint8_t *input_data, size_t input_len, uint8_t *output_data);
  228. /**
  229. * @brief AES-CBC-192 decryption
  230. *
  231. * @param[in] context The cbc context to use for encryption or decryption.
  232. * @param[in] input_key The decryption key. must be 24bytes.
  233. * @param[in] input_data The buffer holding the input data.
  234. * @param[in] input_len The length of a data unit in bytes.
  235. * This can be any length between 16 bytes and 2^31 bytes inclusive
  236. * (between 1 and 2^27 block cipher blocks).
  237. * @param[out] output_data The buffer holding the output data.
  238. * The buffer size must be larger than the size after padding by 16 byte multiples.
  239. */
  240. void aes_cbc192_hard_decrypt(cbc_context_t *context, uint8_t *input_data, size_t input_len, uint8_t *output_data);
  241. /**
  242. * @brief AES-CBC-192 encryption
  243. *
  244. * @param[in] context The cbc context to use for encryption or decryption.
  245. * @param[in] input_key The encryption key. must be 24bytes.
  246. * @param[in] input_data The buffer holding the input data.
  247. * @param[in] input_len The length of a data unit in bytes.
  248. * This can be any length between 16 bytes and 2^31 bytes inclusive
  249. * (between 1 and 2^27 block cipher blocks).
  250. * @param[out] output_data The buffer holding the output data.
  251. * The buffer size must be larger than the size after padding by 16 byte multiples.
  252. */
  253. void aes_cbc192_hard_encrypt(cbc_context_t *context, uint8_t *input_data, size_t input_len, uint8_t *output_data);
  254. /**
  255. * @brief AES-CBC-256 decryption
  256. *
  257. * @param[in] context The cbc context to use for encryption or decryption.
  258. * @param[in] input_key The decryption key. must be 32bytes.
  259. * @param[in] input_data The buffer holding the input data.
  260. * @param[in] input_len The length of a data unit in bytes.
  261. * This can be any length between 16 bytes and 2^31 bytes inclusive
  262. * (between 1 and 2^27 block cipher blocks).
  263. * @param[out] output_data The buffer holding the output data.
  264. * The buffer size must be larger than the size after padding by 16 byte multiples.
  265. */
  266. void aes_cbc256_hard_decrypt(cbc_context_t *context, uint8_t *input_data, size_t input_len, uint8_t *output_data);
  267. /**
  268. * @brief AES-CBC-256 encryption
  269. *
  270. * @param[in] context The cbc context to use for encryption or decryption.
  271. * @param[in] input_key The encryption key. must be 32bytes.
  272. * @param[in] input_data The buffer holding the input data.
  273. * @param[in] input_len The length of a data unit in bytes.
  274. * This can be any length between 16 bytes and 2^31 bytes inclusive
  275. * (between 1 and 2^27 block cipher blocks).
  276. * @param[out] output_data The buffer holding the output data.
  277. * The buffer size must be larger than the size after padding by 16 byte multiples.
  278. */
  279. void aes_cbc256_hard_encrypt(cbc_context_t *context, uint8_t *input_data, size_t input_len, uint8_t *output_data);
  280. /**
  281. * @brief AES-GCM-128 decryption
  282. *
  283. * @param[in] context The gcm context to use for encryption or decryption.
  284. * @param[in] input_key The decryption key. must be 16bytes.
  285. * @param[in] input_data The buffer holding the input data.
  286. * @param[in] input_len The length of a data unit in bytes.
  287. * This can be any length between 16 bytes and 2^31 bytes inclusive
  288. * (between 1 and 2^27 block cipher blocks).
  289. * @param[out] output_data The buffer holding the output data.
  290. * @param[out] gcm_tag The buffer for holding the tag.The length of the tag must be 4 bytes.
  291. */
  292. void aes_gcm128_hard_decrypt(gcm_context_t *context, uint8_t *input_data, size_t input_len, uint8_t *output_data, uint8_t *gcm_tag);
  293. /**
  294. * @brief AES-GCM-128 encryption
  295. *
  296. * @param[in] context The gcm context to use for encryption or decryption.
  297. * @param[in] input_key The encryption key. must be 16bytes.
  298. * @param[in] input_data The buffer holding the input data.
  299. * @param[in] input_len The length of a data unit in bytes.
  300. * This can be any length between 16 bytes and 2^31 bytes inclusive
  301. * (between 1 and 2^27 block cipher blocks).
  302. * @param[out] output_data The buffer holding the output data.
  303. * @param[out] gcm_tag The buffer for holding the tag.The length of the tag must be 4 bytes.
  304. */
  305. void aes_gcm128_hard_encrypt(gcm_context_t *context, uint8_t *input_data, size_t input_len, uint8_t *output_data, uint8_t *gcm_tag);
  306. /**
  307. * @brief AES-GCM-192 decryption
  308. *
  309. * @param[in] context The gcm context to use for encryption or decryption.
  310. * @param[in] input_key The decryption key. must be 24bytes.
  311. * @param[in] input_data The buffer holding the input data.
  312. * @param[in] input_len The length of a data unit in bytes.
  313. * This can be any length between 16 bytes and 2^31 bytes inclusive
  314. * (between 1 and 2^27 block cipher blocks).
  315. * @param[out] output_data The buffer holding the output data.
  316. * @param[out] gcm_tag The buffer for holding the tag.The length of the tag must be 4 bytes.
  317. */
  318. void aes_gcm192_hard_decrypt(gcm_context_t *context, uint8_t *input_data, size_t input_len, uint8_t *output_data, uint8_t *gcm_tag);
  319. /**
  320. * @brief AES-GCM-192 encryption
  321. *
  322. * @param[in] context The gcm context to use for encryption or decryption.
  323. * @param[in] input_key The encryption key. must be 24bytes.
  324. * @param[in] input_data The buffer holding the input data.
  325. * @param[in] input_len The length of a data unit in bytes.
  326. * This can be any length between 16 bytes and 2^31 bytes inclusive
  327. * (between 1 and 2^27 block cipher blocks).
  328. * @param[out] output_data The buffer holding the output data.
  329. * @param[out] gcm_tag The buffer for holding the tag.The length of the tag must be 4 bytes.
  330. */
  331. void aes_gcm192_hard_encrypt(gcm_context_t *context, uint8_t *input_data, size_t input_len, uint8_t *output_data, uint8_t *gcm_tag);
  332. /**
  333. * @brief AES-GCM-256 decryption
  334. *
  335. * @param[in] context The gcm context to use for encryption or decryption.
  336. * @param[in] input_key The decryption key. must be 32bytes.
  337. * @param[in] input_data The buffer holding the input data.
  338. * @param[in] input_len The length of a data unit in bytes.
  339. * This can be any length between 16 bytes and 2^31 bytes inclusive
  340. * (between 1 and 2^27 block cipher blocks).
  341. * @param[out] output_data The buffer holding the output data.
  342. * @param[out] gcm_tag The buffer for holding the tag.The length of the tag must be 4 bytes.
  343. */
  344. void aes_gcm256_hard_decrypt(gcm_context_t *context, uint8_t *input_data, size_t input_len, uint8_t *output_data, uint8_t *gcm_tag);
  345. /**
  346. * @brief AES-GCM-256 encryption
  347. *
  348. * @param[in] context The gcm context to use for encryption or decryption.
  349. * @param[in] input_key The encryption key. must be 32bytes.
  350. * @param[in] input_data The buffer holding the input data.
  351. * @param[in] input_len The length of a data unit in bytes.
  352. * This can be any length between 16 bytes and 2^31 bytes inclusive
  353. * (between 1 and 2^27 block cipher blocks).
  354. * @param[out] output_data The buffer holding the output data.
  355. * @param[out] gcm_tag The buffer for holding the tag.The length of the tag must be 4 bytes.
  356. */
  357. void aes_gcm256_hard_encrypt(gcm_context_t *context, uint8_t *input_data, size_t input_len, uint8_t *output_data, uint8_t *gcm_tag);
  358. /**
  359. * @brief AES-ECB-128 decryption by dma
  360. *
  361. * @param[in] dma_receive_channel_num Dmac receive channel number.
  362. * @param[in] input_key The decryption key. must be 16bytes.
  363. * @param[in] input_data The buffer holding the input data.
  364. * @param[in] input_len The length of a data unit in bytes.
  365. * This can be any length between 16 bytes and 2^31 bytes inclusive
  366. * (between 1 and 2^27 block cipher blocks).
  367. * @param[out] output_data The buffer holding the output data.
  368. * The buffer size must be larger than the size after padding by 16 byte multiples.
  369. */
  370. void aes_ecb128_hard_decrypt_dma(dmac_channel_number_t dma_receive_channel_num,
  371. uint8_t *input_key,
  372. uint8_t *input_data,
  373. size_t input_len,
  374. uint8_t *output_data);
  375. /**
  376. * @brief AES-ECB-128 encryption by dma
  377. *
  378. * @param[in] dma_receive_channel_num Dmac receive channel number.
  379. * @param[in] input_key The encryption key. must be 16bytes.
  380. * @param[in] input_data The buffer holding the input data.
  381. * @param[in] input_len The length of a data unit in bytes.
  382. * This can be any length between 16 bytes and 2^31 bytes inclusive
  383. * (between 1 and 2^27 block cipher blocks).
  384. * @param[out] output_data The buffer holding the output data.
  385. * The buffer size must be larger than the size after padding by 16 byte multiples.
  386. */
  387. void aes_ecb128_hard_encrypt_dma(dmac_channel_number_t dma_receive_channel_num,
  388. uint8_t *input_key,
  389. uint8_t *input_data,
  390. size_t input_len,
  391. uint8_t *output_data);
  392. /**
  393. * @brief AES-ECB-192 decryption by dma
  394. *
  395. * @param[in] dma_receive_channel_num Dmac receive channel number.
  396. * @param[in] input_key The decryption key. must be 16bytes.
  397. * @param[in] input_data The buffer holding the input data.
  398. * @param[in] input_len The length of a data unit in bytes.
  399. * This can be any length between 16 bytes and 2^31 bytes inclusive
  400. * (between 1 and 2^27 block cipher blocks).
  401. * @param[out] output_data The buffer holding the output data.
  402. * The buffer size must be larger than the size after padding by 16 byte multiples.
  403. */
  404. void aes_ecb192_hard_decrypt_dma(dmac_channel_number_t dma_receive_channel_num,
  405. uint8_t *input_key,
  406. uint8_t *input_data,
  407. size_t input_len,
  408. uint8_t *output_data);
  409. /**
  410. * @brief AES-ECB-192 encryption by dma
  411. *
  412. * @param[in] dma_receive_channel_num Dmac receive channel number.
  413. * @param[in] input_key The encryption key. must be 16bytes.
  414. * @param[in] input_data The buffer holding the input data.
  415. * @param[in] input_len The length of a data unit in bytes.
  416. * This can be any length between 16 bytes and 2^31 bytes inclusive
  417. * (between 1 and 2^27 block cipher blocks).
  418. * @param[out] output_data The buffer holding the output data.
  419. * The buffer size must be larger than the size after padding by 16 byte multiples.
  420. */
  421. void aes_ecb192_hard_encrypt_dma(dmac_channel_number_t dma_receive_channel_num,
  422. uint8_t *input_key,
  423. uint8_t *input_data,
  424. size_t input_len,
  425. uint8_t *output_data);
  426. /**
  427. * @brief AES-ECB-256 decryption by dma
  428. *
  429. * @param[in] dma_receive_channel_num Dmac receive channel number.
  430. * @param[in] input_key The decryption key. must be 16bytes.
  431. * @param[in] input_data The buffer holding the input data.
  432. * @param[in] input_len The length of a data unit in bytes.
  433. * This can be any length between 16 bytes and 2^31 bytes inclusive
  434. * (between 1 and 2^27 block cipher blocks).
  435. * @param[out] output_data The buffer holding the output data.
  436. * The buffer size must be larger than the size after padding by 16 byte multiples.
  437. */
  438. void aes_ecb256_hard_decrypt_dma(dmac_channel_number_t dma_receive_channel_num,
  439. uint8_t *input_key,
  440. uint8_t *input_data,
  441. size_t input_len,
  442. uint8_t *output_data);
  443. /**
  444. * @brief AES-ECB-256 encryption by dma
  445. *
  446. * @param[in] dma_receive_channel_num Dmac receive channel number.
  447. * @param[in] input_key The encryption key. must be 16bytes.
  448. * @param[in] input_data The buffer holding the input data.
  449. * @param[in] input_len The length of a data unit in bytes.
  450. * This can be any length between 16 bytes and 2^31 bytes inclusive
  451. * (between 1 and 2^27 block cipher blocks).
  452. * @param[out] output_data The buffer holding the output data.
  453. * The buffer size must be larger than the size after padding by 16 byte multiples.
  454. */
  455. void aes_ecb256_hard_encrypt_dma(dmac_channel_number_t dma_receive_channel_num,
  456. uint8_t *input_key,
  457. uint8_t *input_data,
  458. size_t input_len,
  459. uint8_t *output_data);
  460. /**
  461. * @brief AES-CBC-128 decryption
  462. *
  463. * @param[in] dma_receive_channel_num Dmac receive channel number.
  464. * @param[in] context The cbc context to use for encryption or decryption.
  465. * @param[in] input_key The encryption key. must be 24bytes.
  466. * @param[in] input_data The buffer holding the input data.
  467. * @param[in] input_len The length of a data unit in bytes.
  468. * This can be any length between 16 bytes and 2^31 bytes inclusive
  469. * (between 1 and 2^27 block cipher blocks).
  470. * @param[out] output_data The buffer holding the output data.
  471. * The buffer size must be larger than the size after padding by 16 byte multiples.
  472. */
  473. void aes_cbc128_hard_decrypt_dma(dmac_channel_number_t dma_receive_channel_num,
  474. cbc_context_t *context,
  475. uint8_t *input_data,
  476. size_t input_len,
  477. uint8_t *output_data);
  478. /**
  479. * @brief AES-CBC-128 encryption
  480. *
  481. * @param[in] dma_receive_channel_num Dmac receive channel number.
  482. * @param[in] context The cbc context to use for encryption or decryption.
  483. * @param[in] input_key The encryption key. must be 24bytes.
  484. * @param[in] input_data The buffer holding the input data.
  485. * @param[in] input_len The length of a data unit in bytes.
  486. * This can be any length between 16 bytes and 2^31 bytes inclusive
  487. * (between 1 and 2^27 block cipher blocks).
  488. * @param[out] output_data The buffer holding the output data.
  489. * The buffer size must be larger than the size after padding by 16 byte multiples.
  490. */
  491. void aes_cbc128_hard_encrypt_dma(dmac_channel_number_t dma_receive_channel_num,
  492. cbc_context_t *context,
  493. uint8_t *input_data,
  494. size_t input_len,
  495. uint8_t *output_data);
  496. /**
  497. * @brief AES-CBC-192 decryption
  498. *
  499. * @param[in] dma_receive_channel_num Dmac receive channel number.
  500. * @param[in] context The cbc context to use for encryption or decryption.
  501. * @param[in] input_key The decryption key. must be 24bytes.
  502. * @param[in] input_data The buffer holding the input data.
  503. * @param[in] input_len The length of a data unit in bytes.
  504. * This can be any length between 16 bytes and 2^31 bytes inclusive
  505. * (between 1 and 2^27 block cipher blocks).
  506. * @param[out] output_data The buffer holding the output data.
  507. * The buffer size must be larger than the size after padding by 16 byte multiples.
  508. */
  509. void aes_cbc192_hard_decrypt_dma(dmac_channel_number_t dma_receive_channel_num,
  510. cbc_context_t *context,
  511. uint8_t *input_data,
  512. size_t input_len,
  513. uint8_t *output_data);
  514. /**
  515. * @brief AES-CBC-192 encryption
  516. *
  517. * @param[in] dma_receive_channel_num Dmac receive channel number.
  518. * @param[in] context The cbc context to use for encryption or decryption.
  519. * @param[in] input_key The encryption key. must be 24bytes.
  520. * @param[in] input_data The buffer holding the input data.
  521. * @param[in] input_len The length of a data unit in bytes.
  522. * This can be any length between 16 bytes and 2^31 bytes inclusive
  523. * (between 1 and 2^27 block cipher blocks).
  524. * @param[out] output_data The buffer holding the output data.
  525. * The buffer size must be larger than the size after padding by 16 byte multiples.
  526. */
  527. void aes_cbc192_hard_encrypt_dma(dmac_channel_number_t dma_receive_channel_num,
  528. cbc_context_t *context,
  529. uint8_t *input_data,
  530. size_t input_len,
  531. uint8_t *output_data);
  532. /**
  533. * @brief AES-CBC-256 decryption
  534. *
  535. * @param[in] dma_receive_channel_num Dmac receive channel number.
  536. * @param[in] context The cbc context to use for encryption or decryption.
  537. * @param[in] input_key The decryption key. must be 24bytes.
  538. * @param[in] input_data The buffer holding the input data.
  539. * @param[in] input_len The length of a data unit in bytes.
  540. * This can be any length between 16 bytes and 2^31 bytes inclusive
  541. * (between 1 and 2^27 block cipher blocks).
  542. * @param[out] output_data The buffer holding the output data.
  543. * The buffer size must be larger than the size after padding by 16 byte multiples.
  544. */
  545. void aes_cbc256_hard_decrypt_dma(dmac_channel_number_t dma_receive_channel_num,
  546. cbc_context_t *context,
  547. uint8_t *input_data,
  548. size_t input_len,
  549. uint8_t *output_data);
  550. /**
  551. * @brief AES-CBC-256 encryption
  552. *
  553. * @param[in] dma_receive_channel_num Dmac receive channel number.
  554. * @param[in] context The cbc context to use for encryption or decryption.
  555. * @param[in] input_key The encryption key. must be 24bytes.
  556. * @param[in] input_data The buffer holding the input data.
  557. * @param[in] input_len The length of a data unit in bytes.
  558. * This can be any length between 16 bytes and 2^31 bytes inclusive
  559. * (between 1 and 2^27 block cipher blocks).
  560. * @param[out] output_data The buffer holding the output data.
  561. * The buffer size must be larger than the size after padding by 16 byte multiples.
  562. */
  563. void aes_cbc256_hard_encrypt_dma(dmac_channel_number_t dma_receive_channel_num,
  564. cbc_context_t *context,
  565. uint8_t *input_data,
  566. size_t input_len,
  567. uint8_t *output_data);
  568. /**
  569. * @brief AES-GCM-128 decryption
  570. *
  571. * @param[in] dma_receive_channel_num Dmac receive channel number.
  572. * @param[in] context The gcm context to use for encryption or decryption.
  573. * @param[in] input_key The decryption key. must be 16bytes.
  574. * @param[in] input_data The buffer holding the input data.
  575. * @param[in] input_len The length of a data unit in bytes. Must be 4 byte multiples.
  576. * This can be any length between 16 bytes and 2^31 bytes inclusive
  577. * (between 1 and 2^27 block cipher blocks).
  578. * @param[out] output_data The buffer holding the output data.
  579. * @param[out] gcm_tag The buffer for holding the tag.The length of the tag must be 4 bytes.
  580. */
  581. void aes_gcm128_hard_decrypt_dma(dmac_channel_number_t dma_receive_channel_num,
  582. gcm_context_t *context,
  583. uint8_t *input_data,
  584. size_t input_len,
  585. uint8_t *output_data,
  586. uint8_t *gcm_tag);
  587. /**
  588. * @brief AES-GCM-128 encryption
  589. *
  590. * @param[in] dma_receive_channel_num Dmac receive channel number.
  591. * @param[in] context The gcm context to use for encryption or decryption.
  592. * @param[in] input_key The encryption key. must be 16bytes.
  593. * @param[in] input_data The buffer holding the input data.
  594. * @param[in] input_len The length of a data unit in bytes. Must be 4 byte multiples.
  595. * This can be any length between 16 bytes and 2^31 bytes inclusive
  596. * (between 1 and 2^27 block cipher blocks).
  597. * @param[out] output_data The buffer holding the output data.
  598. * @param[out] gcm_tag The buffer for holding the tag.The length of the tag must be 4 bytes.
  599. */
  600. void aes_gcm128_hard_encrypt_dma(dmac_channel_number_t dma_receive_channel_num,
  601. gcm_context_t *context,
  602. uint8_t *input_data,
  603. size_t input_len,
  604. uint8_t *output_data,
  605. uint8_t *gcm_tag);
  606. /**
  607. * @brief AES-GCM-192 decryption
  608. *
  609. * @param[in] dma_receive_channel_num Dmac receive channel number.
  610. * @param[in] context The gcm context to use for encryption or decryption.
  611. * @param[in] input_key The decryption key. must be 16bytes.
  612. * @param[in] input_data The buffer holding the input data.
  613. * @param[in] input_len The length of a data unit in bytes. Must be 4 byte multiples.
  614. * This can be any length between 16 bytes and 2^31 bytes inclusive
  615. * (between 1 and 2^27 block cipher blocks).
  616. * @param[out] output_data The buffer holding the output data.
  617. * @param[out] gcm_tag The buffer for holding the tag.The length of the tag must be 4 bytes.
  618. */
  619. void aes_gcm192_hard_decrypt_dma(dmac_channel_number_t dma_receive_channel_num,
  620. gcm_context_t *context,
  621. uint8_t *input_data,
  622. size_t input_len,
  623. uint8_t *output_data,
  624. uint8_t *gcm_tag);
  625. /**
  626. * @brief AES-GCM-192 encryption
  627. *
  628. * @param[in] dma_receive_channel_num Dmac receive channel number.
  629. * @param[in] context The gcm context to use for encryption or decryption.
  630. * @param[in] input_key The encryption key. must be 16bytes.
  631. * @param[in] input_data The buffer holding the input data.
  632. * @param[in] input_len The length of a data unit in bytes. Must be 4 byte multiples.
  633. * This can be any length between 16 bytes and 2^31 bytes inclusive
  634. * (between 1 and 2^27 block cipher blocks).
  635. * @param[out] output_data The buffer holding the output data.
  636. * @param[out] gcm_tag The buffer for holding the tag.The length of the tag must be 4 bytes.
  637. */
  638. void aes_gcm192_hard_encrypt_dma(dmac_channel_number_t dma_receive_channel_num,
  639. gcm_context_t *context,
  640. uint8_t *input_data,
  641. size_t input_len,
  642. uint8_t *output_data,
  643. uint8_t *gcm_tag);
  644. /**
  645. * @brief AES-GCM-256 decryption
  646. *
  647. * @param[in] dma_receive_channel_num Dmac receive channel number.
  648. * @param[in] context The gcm context to use for encryption or decryption.
  649. * @param[in] input_key The decryption key. must be 16bytes.
  650. * @param[in] input_data The buffer holding the input data.
  651. * @param[in] input_len The length of a data unit in bytes. Must be 4 byte multiples.
  652. * This can be any length between 16 bytes and 2^31 bytes inclusive
  653. * (between 1 and 2^27 block cipher blocks).
  654. * @param[out] output_data The buffer holding the output data.
  655. * @param[out] gcm_tag The buffer for holding the tag.The length of the tag must be 4 bytes.
  656. */
  657. void aes_gcm256_hard_decrypt_dma(dmac_channel_number_t dma_receive_channel_num,
  658. gcm_context_t *context,
  659. uint8_t *input_data,
  660. size_t input_len,
  661. uint8_t *output_data,
  662. uint8_t *gcm_tag);
  663. /**
  664. * @brief AES-GCM-256 encryption
  665. *
  666. * @param[in] dma_receive_channel_num Dmac receive channel number.
  667. * @param[in] context The gcm context to use for encryption or decryption.
  668. * @param[in] input_key The encryption key. must be 16bytes.
  669. * @param[in] input_data The buffer holding the input data.
  670. * @param[in] input_len The length of a data unit in bytes. Must be 4 byte multiples.
  671. * This can be any length between 16 bytes and 2^31 bytes inclusive
  672. * (between 1 and 2^27 block cipher blocks).
  673. * @param[out] output_data The buffer holding the output data.
  674. * @param[out] gcm_tag The buffer for holding the tag.The length of the tag must be 4 bytes.
  675. */
  676. void aes_gcm256_hard_encrypt_dma(dmac_channel_number_t dma_receive_channel_num,
  677. gcm_context_t *context,
  678. uint8_t *input_data,
  679. size_t input_len,
  680. uint8_t *output_data,
  681. uint8_t *gcm_tag);
  682. /**
  683. * @brief This function initializes the AES hard module.
  684. *
  685. * @param[in] input_key The buffer holding the encryption or decryption key.
  686. * @param[in] input_key_len The length of the input_key.must be 16bytes || 24bytes || 32bytes.
  687. * @param[in] iv The initialization vector.
  688. * @param[in] iv_len The length of the iv.GCM must be 12bytes. CBC must be 16bytes. ECB set 0L.
  689. * @param[in] gcm_aad The buffer holding the Additional authenticated data. or NULL
  690. * @param[in] cipher_mode Cipher Modes.must be AES_CBC || AES_ECB || AES_GCM.
  691. * Other cipher modes, please look forward to the next generation of kendryte.
  692. * @param[in] encrypt_sel The operation to perform:encryption or decryption.
  693. * @param[in] gcm_aad_len The length of the gcm_aad.
  694. * @param[in] input_data_len The length of the input_data.
  695. */
  696. void aes_init(uint8_t *input_key, size_t input_key_len, uint8_t *iv, size_t iv_len, uint8_t *gcm_aad,
  697. aes_cipher_mode_t cipher_mode, aes_encrypt_sel_t encrypt_sel, size_t gcm_aad_len, size_t input_data_len);
  698. /**
  699. * @brief This function feeds an input buffer into an encryption or decryption operation.
  700. *
  701. * @param[in] input_data The buffer holding the input data.
  702. * @param[out] output_data The buffer holding the output data.
  703. * @param[in] input_data_len The length of the input_data.
  704. * @param[in] cipher_mode Cipher Modes.must be AES_CBC || AES_ECB || AES_GCM.
  705. * Other cipher modes, please look forward to the next generation of kendryte.
  706. */
  707. void aes_process(uint8_t *input_data, uint8_t *output_data, size_t input_data_len, aes_cipher_mode_t cipher_mode);
  708. /**
  709. * @brief This function get the gcm tag to verify.
  710. *
  711. * @param[out] gcm_tag The buffer holding the gcm tag.The length of the tag must be 16bytes.
  712. */
  713. void gcm_get_tag(uint8_t *gcm_tag);
  714. #ifdef __cplusplus
  715. }
  716. #endif
  717. #endif /* _DRIVER_AES_H */