aes.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. /*
  2. * Copyright (c) 2017 Actions Semiconductor Co., Ltd
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <kernel.h>
  7. #include <init.h>
  8. #include <device.h>
  9. #include <stdint.h>
  10. #include <stdbool.h>
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <drivers/dma.h>
  15. #include <drivers/se/se.h>
  16. #include <soc.h>
  17. #include <board_cfg.h>
  18. #include <logging/log.h>
  19. LOG_MODULE_REGISTER(aes, CONFIG_LOG_DEFAULT_LEVEL);
  20. /* AES Register Addresses ***************************************************/
  21. #define AES_CTRL (SE_REG_BASE+0x0000)
  22. #define AES_MODE (SE_REG_BASE+0x0004)
  23. #define AES_LEN (SE_REG_BASE+0x0008)
  24. #define AES_KEY0 (SE_REG_BASE+0x0010)
  25. #define AES_KEY1 (SE_REG_BASE+0x0014)
  26. #define AES_KEY2 (SE_REG_BASE+0x0018)
  27. #define AES_KEY3 (SE_REG_BASE+0x001c)
  28. #define AES_KEY4 (SE_REG_BASE+0x0020)
  29. #define AES_KEY5 (SE_REG_BASE+0x0024)
  30. #define AES_KEY6 (SE_REG_BASE+0x0028)
  31. #define AES_KEY7 (SE_REG_BASE+0x002c)
  32. #define AES_IV0 (SE_REG_BASE+0x0040)
  33. #define AES_IV1 (SE_REG_BASE+0x0044)
  34. #define AES_IV2 (SE_REG_BASE+0x0048)
  35. #define AES_IV3 (SE_REG_BASE+0x004c)
  36. #define AES_INOUTFIFOCTL (SE_REG_BASE+0x0080)
  37. #define AES_INFIFO (SE_REG_BASE+0x0084)
  38. #define AES_OUTFIFO (SE_REG_BASE+0x0088)
  39. /* AES Register Bit Definitions *********************************************/
  40. /* Control Register */
  41. #define AES_CTRL_EN_SHIFT (0)
  42. #define AES_CTRL_EN_MASK (0x1 << AES_CTRL_EN_SHIFT)
  43. #define AES_CTRL_EN (0x1 << AES_CTRL_EN_SHIFT)
  44. #define AES_CTRL_RESET_SHIFT (1)
  45. #define AES_CTRL_RESET_MASK (0x1 << AES_CTRL_RESET_SHIFT)
  46. #define AES_CTRL_RESET (0x1 << AES_CTRL_RESET_SHIFT)
  47. #define AES_CTRL_INT_EN_SHIFT (2)
  48. #define AES_CTRL_INT_EN_MASK (0x1 << AES_CTRL_INT_EN_SHIFT)
  49. #define AES_CTRL_INT_EN (0x1 << AES_CTRL_INT_EN_SHIFT)
  50. #define AES_CTRL_CLK_EN_SHIFT (3)
  51. #define AES_CTRL_CLK_EN_MASK (0x1 << AES_CTRL_CLK_EN_SHIFT)
  52. #define AES_CTRL_CLK_EN (0x1 << AES_CTRL_CLK_EN_SHIFT)
  53. #define AES_CTRL_CNT_OVF_SHIFT (30)
  54. #define AES_CTRL_CNT_OVF_MASK (0x1 << AES_CTRL_CNT_OVF_SHIFT)
  55. #define AES_CTRL_CNT_OVF (0x1 << AES_CTRL_CNT_OVF_SHIFT)
  56. #define AES_CTRL_END_SHIFT (31)
  57. #define AES_CTRL_END_MASK (0x1 << AES_CTRL_END_SHIFT)
  58. #define AES_CTRL_END (0x1 << AES_CTRL_END_SHIFT)
  59. /* Mode Register */
  60. #define AES_MODE_TYP_SHIFT (0)
  61. #define AES_MODE_TYP_MASK (0x1 << AES_MODE_TYP_SHIFT)
  62. #define AES_MODE_DECRYPT (0x0 << AES_MODE_TYP_SHIFT)
  63. #define AES_MODE_ENCRYPT (0x1 << AES_MODE_TYP_SHIFT)
  64. #define AES_MODE_MOD_SHIFT (2)
  65. #define AES_MODE_MOD_MASK (0x3 << AES_MODE_MOD_SHIFT)
  66. #define AES_MODE_MOD_ECB (0x0 << AES_MODE_MOD_SHIFT)
  67. #define AES_MODE_MOD_CTR (0x1 << AES_MODE_MOD_SHIFT)
  68. #define AES_MODE_MOD_CBC (0x2 << AES_MODE_MOD_SHIFT)
  69. #define AES_MODE_MOD_CBC_CTS (0x3 << AES_MODE_MOD_SHIFT)
  70. #define AES_MODE_BYPASS_SHIFT (4)
  71. #define AES_MODE_BYPASS_MASK (0x1 << AES_MODE_BYPASS_SHIFT)
  72. #define AES_MODE_BYPASS_DECRYPT (0x1 << AES_MODE_BYPASS_SHIFT)
  73. #define AES_MODE_IV_UPDAT_SHIFT (8)
  74. #define AES_MODE_IV_UPDAT_MASK (0x1 << AES_MODE_IV_UPDAT_SHIFT)
  75. #define AES_MODE_IV_UPDAT (0x1 << AES_MODE_IV_UPDAT_SHIFT)
  76. #define AES_MODE_KEY_SRC_SHIFT (16)
  77. #define AES_MODE_KEY_SRC_MASK (0x1 << AES_MODE_KEY_SRC_SHIFT)
  78. #define AES_MODE_REG_KEY (0x1 << AES_MODE_KEY_SRC_SHIFT)
  79. #define AES_MODE_KEY_SIZE_SHIFT (17)
  80. #define AES_MODE_KEY_SIZE_MASK (0x1 << AES_MODE_KEY_SIZE_SHIFT)
  81. #define AES_MODE_KEY_128BIT (0x0 << AES_MODE_KEY_SIZE_SHIFT)
  82. #define AES_MODE_KEY_192BIT (0x1 << AES_MODE_KEY_SIZE_SHIFT)
  83. #define AES_MODE_KEY_256BIT (0x2 << AES_MODE_KEY_SIZE_SHIFT)
  84. #define AES_MODE_KEY_EFUSE_SHIFT (19)
  85. #define AES_MODE_KEY_EFUSE_MASK (0x1 << AES_MODE_KEY_EFUSE_SHIFT)
  86. #define AES_MODE_IV0_SRC_SHIFT (20)
  87. #define AES_MODE_IV0_SRC_MASK (0x1 << AES_MODE_IV0_SRC_SHIFT)
  88. #define AES_MODE_REG_IV0 (0x1 << AES_MODE_IV0_SRC_SHIFT)
  89. /* InOutFIFO Control Register */
  90. #define AES_FIFOCTL_INFIFODE_SHIFT (1)
  91. #define AES_FIFOCTL_INFIFODE_MASK (0x1 << AES_FIFOCTL_INFIFODE_SHIFT)
  92. #define AES_FIFOCTL_INFIFODE (0x1 << AES_FIFOCTL_INFIFODE_SHIFT)
  93. #define AES_FIFOCTL_OUTFIFODE_SHIFT (5)
  94. #define AES_FIFOCTL_OUTFIFODE_MASK (0x1 << AES_FIFOCTL_OUTFIFODE_SHIFT)
  95. #define AES_FIFOCTL_OUTFIFODE (0x1 << AES_FIFOCTL_OUTFIFODE_SHIFT)
  96. #define AES_FIFOCTL_OUTFIFOFP_SHIFT (9)
  97. #define AES_FIFOCTL_OUTFIFOFP_MASK (0x1 << AES_FIFOCTL_OUTFIFOFP_SHIFT)
  98. #define AES_FIFOCTL_OUTFIFOFP (0x1 << AES_FIFOCTL_OUTFIFOFP_SHIFT)
  99. #define AES_FIFOCTL_OUTFIFOLP_SHIFT (10)
  100. #define AES_FIFOCTL_OUTFIFOLP_MASK (0x1 << AES_FIFOCTL_OUTFIFOLP_SHIFT)
  101. #define AES_FIFOCTL_OUTFIFOLP (0x1 << AES_FIFOCTL_OUTFIFOLP_SHIFT)
  102. #define SE_DMA_ID 17
  103. #define AES_BLOCK_SIZE 16
  104. #define AES_FIFO_DEPTH (4 * AES_BLOCK_SIZE)
  105. #define IN_DMA_TRANS_OK 0x01
  106. #define OUT_DMA_TRANS_OK 0x02
  107. #define ALL_DMA_TRANS_OK (IN_DMA_TRANS_OK | OUT_DMA_TRANS_OK)
  108. #ifdef CONFIG_AES_DMA
  109. struct aes_dev_s
  110. {
  111. const struct device *dma; /* AES DMA */
  112. struct k_sem waitsem; /* AES DMA crypto and trans complete sem */
  113. int in_chan; /* AES DMA input channel */
  114. int out_chan; /* AES DMA output channel */
  115. uint8_t trans_done; /* AES DMA input/output trans ok flag */
  116. };
  117. static struct aes_dev_s g_aesdev =
  118. {
  119. .dma = NULL,
  120. .waitsem = Z_SEM_INITIALIZER(g_aesdev.waitsem, 0, 1),
  121. .trans_done = 0,
  122. };
  123. #endif
  124. /****************************************************************************
  125. * Public Data
  126. ****************************************************************************/
  127. /****************************************************************************
  128. * Private Functions
  129. ****************************************************************************/
  130. #ifndef CONFIG_AES_DMA
  131. /****************************************************************************
  132. * Name: aes_encrypt_by_cpu
  133. *
  134. * Description:
  135. * aes encrypt/decrypt by cpu.
  136. *
  137. ****************************************************************************/
  138. static int aes_encrypt_by_cpu(void *out,
  139. const void *in, size_t size)
  140. {
  141. size_t size_remain;
  142. size_t aes_len;
  143. uint32_t aes_ctrl_val;
  144. uint32_t aes_inoutfifictl_val;
  145. uint8_t *aes_in = (uint8_t *)in;
  146. uint8_t *aes_out = (uint8_t *)out;
  147. /* set SE FIFO to AES */
  148. sys_write32(0, SE_FIFOCTRL);
  149. /* disable DRQ */
  150. aes_inoutfifictl_val = sys_read32(AES_INOUTFIFOCTL);
  151. aes_inoutfifictl_val &= ~AES_FIFOCTL_INFIFODE_MASK;
  152. aes_inoutfifictl_val &= ~AES_FIFOCTL_OUTFIFODE_MASK;
  153. sys_write32(aes_inoutfifictl_val, AES_INOUTFIFOCTL);
  154. size_remain = size;
  155. while (size_remain > 0) {
  156. if (size_remain >= AES_FIFO_DEPTH) {
  157. aes_len = AES_FIFO_DEPTH;
  158. size_remain -= AES_FIFO_DEPTH;
  159. } else {
  160. aes_len = size_remain;
  161. size_remain = 0;
  162. }
  163. sys_write32(aes_len, AES_LEN);
  164. se_memcpy((void *)AES_INFIFO,
  165. aes_in, aes_len, CPY_MEM_TO_FIFO);
  166. /* enable aes crypt */
  167. aes_ctrl_val = sys_read32(AES_CTRL);
  168. sys_write32(aes_ctrl_val | AES_CTRL_EN, AES_CTRL);
  169. while ((sys_read32(AES_INOUTFIFOCTL)
  170. & (AES_FIFOCTL_OUTFIFOFP | AES_FIFOCTL_OUTFIFOLP)) == 0);
  171. aes_ctrl_val = sys_read32(AES_CTRL);
  172. /* clear fifo last data irq pending or fifo full irq pending */
  173. sys_write32(aes_ctrl_val, AES_CTRL);
  174. aes_ctrl_val = sys_read32(AES_CTRL);
  175. /* Clear AES Ready */
  176. aes_ctrl_val |= AES_CTRL_END;
  177. sys_write32(aes_ctrl_val, AES_CTRL);
  178. if (out) {
  179. se_memcpy(aes_out, (void *)AES_OUTFIFO,
  180. aes_len, CPY_FIFO_TO_MEM);
  181. aes_out += aes_len;
  182. }
  183. aes_in += aes_len;
  184. /* enable AES reset */
  185. aes_ctrl_val = sys_read32(AES_CTRL);
  186. aes_ctrl_val |= AES_CTRL_RESET;
  187. sys_write32(aes_ctrl_val, AES_CTRL);
  188. /* Wait finished */
  189. while (sys_read32(AES_CTRL) & AES_CTRL_RESET);
  190. }
  191. return 0;
  192. }
  193. #endif
  194. #ifdef CONFIG_AES_DMA
  195. /****************************************************************************
  196. * Name: aes_in_dmacallback
  197. *
  198. * Description:
  199. * aes input data transfer done callback by dma.
  200. *
  201. ****************************************************************************/
  202. static void aes_in_dmacallback(const struct device *dev,
  203. void *user_data, uint32_t chan, int status)
  204. {
  205. g_aesdev.trans_done |= IN_DMA_TRANS_OK;
  206. if (g_aesdev.trans_done == ALL_DMA_TRANS_OK)
  207. k_sem_give(&g_aesdev.waitsem);
  208. }
  209. /****************************************************************************
  210. * Name: aes_out_dmacallback
  211. *
  212. * Description:
  213. * aes output data transfer done callback by dma.
  214. *
  215. ****************************************************************************/
  216. static void aes_out_dmacallback(const struct device *dev,
  217. void *user_data, uint32_t chan, int status)
  218. {
  219. g_aesdev.trans_done |= OUT_DMA_TRANS_OK;
  220. if (g_aesdev.trans_done == ALL_DMA_TRANS_OK)
  221. k_sem_give(&g_aesdev.waitsem);
  222. }
  223. /****************************************************************************
  224. * Name: aes_encrypt_by_dma
  225. *
  226. * Description:
  227. * aes encrypt/decrypt by dma.
  228. *
  229. ****************************************************************************/
  230. static int aes_encrypt_by_dma(void *out,
  231. const void *in, size_t size)
  232. {
  233. uint32_t aes_inoutfifictl_val;
  234. uint32_t aes_ctrl_val;
  235. struct dma_config dma_cfg = {0};
  236. struct dma_block_config dma_block_cfg = {0};
  237. uint8_t *aes_in;
  238. uint8_t *aes_out;
  239. int ret = 0;
  240. /* if not 4 bytes aligned(or in rom), malloc 4-aligned for dma trans */
  241. if ((((uint32_t)in & 0x3) != 0) ||
  242. (((uint32_t)in >= 0x10000000) && ((uint32_t)in < 0x18000000))) {
  243. aes_in = (uint8_t *)malloc(size);
  244. if (aes_in == NULL)
  245. return -EINVAL;
  246. memcpy(aes_in, (uint8_t *)in, size);
  247. } else {
  248. aes_in = (uint8_t *)in;
  249. }
  250. /* if not 4 bytes aligned, need malloc 4-aligned data for dma trans */
  251. if ((((uint32_t)out & 0x3) != 0) ||
  252. (((uint32_t)out >= 0x10000000) && ((uint32_t)out < 0x18000000))) {
  253. aes_out = (uint8_t *)malloc(size);
  254. if (aes_out == NULL)
  255. return -EINVAL;
  256. } else {
  257. aes_out = (uint8_t *)out;
  258. }
  259. /* set SE FIFO to AES */
  260. sys_write32(0, SE_FIFOCTRL);
  261. /* enable DRQ */
  262. aes_inoutfifictl_val = sys_read32(AES_INOUTFIFOCTL);
  263. aes_inoutfifictl_val |= AES_FIFOCTL_INFIFODE;
  264. aes_inoutfifictl_val |= AES_FIFOCTL_OUTFIFODE;
  265. sys_write32(aes_inoutfifictl_val, AES_INOUTFIFOCTL);
  266. /* set aes len */
  267. sys_write32(size, AES_LEN);
  268. /* clear transfer flag */
  269. g_aesdev.trans_done = 0;
  270. /* Configure the TX DMA */
  271. dma_cfg.dma_slot = SE_DMA_ID;
  272. dma_cfg.channel_direction = MEMORY_TO_PERIPHERAL;
  273. dma_cfg.source_data_size = 4U;
  274. dma_cfg.dest_data_size = 4U;
  275. dma_cfg.dma_callback = aes_in_dmacallback;
  276. dma_cfg.complete_callback_en = 1U;
  277. dma_cfg.block_count = 1U;
  278. dma_cfg.head_block = &dma_block_cfg;
  279. dma_block_cfg.source_address = (uint32_t)aes_in;
  280. dma_block_cfg.dest_address = AES_INFIFO;
  281. dma_block_cfg.block_size = size;
  282. dma_block_cfg.source_reload_en = 0;
  283. ret = dma_config(g_aesdev.dma, g_aesdev.in_chan, &dma_cfg);
  284. if (ret < 0)
  285. return -EBUSY;
  286. /* Configure the RX DMA */
  287. dma_cfg.channel_direction = PERIPHERAL_TO_MEMORY;
  288. dma_cfg.dma_callback = aes_out_dmacallback;
  289. dma_block_cfg.source_address = AES_OUTFIFO;
  290. dma_block_cfg.dest_address = (uint32_t)aes_out;
  291. dma_block_cfg.block_size = size;
  292. ret = dma_config(g_aesdev.dma, g_aesdev.out_chan, &dma_cfg);
  293. if (ret < 0)
  294. return -EBUSY;
  295. dma_start(g_aesdev.dma, g_aesdev.in_chan);
  296. dma_start(g_aesdev.dma, g_aesdev.out_chan);
  297. /* enable aes crypt */
  298. aes_ctrl_val = sys_read32(AES_CTRL);
  299. sys_write32(aes_ctrl_val | AES_CTRL_EN, AES_CTRL);
  300. /* wait aes end */
  301. ret = k_sem_take(&g_aesdev.waitsem, K_MSEC(50));
  302. if (ret < 0)
  303. goto wait_errout;
  304. /* Clear AES Ready */
  305. aes_ctrl_val |= AES_CTRL_END;
  306. sys_write32(aes_ctrl_val, AES_CTRL);
  307. /* enable AES reset */
  308. aes_ctrl_val = sys_read32(AES_CTRL);
  309. aes_ctrl_val |= AES_CTRL_RESET;
  310. sys_write32(aes_ctrl_val, AES_CTRL);
  311. /* Wait finished */
  312. while (sys_read32(AES_CTRL) & AES_CTRL_RESET);
  313. /* if out is not 4 aligned, we should copy aes_out to out */
  314. if ((((uint32_t)out & 0x3) != 0) ||
  315. (((uint32_t)out >= 0x10000000) && ((uint32_t)out < 0x18000000))) {
  316. memcpy((uint8_t *)out, aes_out, size);
  317. free(aes_out);
  318. }
  319. if ((((uint32_t)in & 0x3) != 0) ||
  320. (((uint32_t)in >= 0x10000000) && ((uint32_t)in < 0x18000000)))
  321. free(aes_in);
  322. wait_errout:
  323. dma_stop(g_aesdev.dma, g_aesdev.in_chan);
  324. dma_stop(g_aesdev.dma, g_aesdev.out_chan);
  325. return ret;
  326. }
  327. #endif
  328. /****************************************************************************
  329. * Name: aes_setup_mr
  330. *
  331. * Description:
  332. * set aes encrypt/decrypt mode and parameter.
  333. *
  334. ****************************************************************************/
  335. static int aes_setup_mr(uint32_t keysize,
  336. int mode, int encrypt)
  337. {
  338. uint32_t regval = AES_MODE_REG_KEY | AES_MODE_REG_IV0;
  339. if (encrypt)
  340. regval |= AES_MODE_ENCRYPT;
  341. else
  342. regval |= AES_MODE_DECRYPT;
  343. switch (keysize){
  344. case 16:
  345. regval |= AES_MODE_KEY_128BIT;
  346. break;
  347. case 24:
  348. regval |= AES_MODE_KEY_192BIT;
  349. break;
  350. case 32:
  351. regval |= AES_MODE_KEY_256BIT;
  352. break;
  353. default:
  354. return -EINVAL;
  355. }
  356. switch (mode){
  357. case AES_MODE_ECB:
  358. regval |= AES_MODE_MOD_ECB;
  359. break;
  360. case AES_MODE_CTR:
  361. regval |= AES_MODE_MOD_CTR;
  362. break;
  363. case AES_MODE_CBC:
  364. regval |= AES_MODE_MOD_CBC;
  365. break;
  366. case AES_MODE_CBC_CTS:
  367. regval |= AES_MODE_MOD_CBC_CTS;
  368. break;
  369. default:
  370. return -EINVAL;
  371. }
  372. sys_write32(regval, AES_MODE);
  373. return 0;
  374. }
  375. /****************************************************************************
  376. * Public Functions
  377. ****************************************************************************/
  378. /****************************************************************************
  379. * Name: aes_init
  380. *
  381. * Description:
  382. * before using aes, we should init aes clock and related things.
  383. *
  384. ****************************************************************************/
  385. static int aes_init(void)
  386. {
  387. uint32_t clken0_val;
  388. clken0_val = sys_read32(CMU_DEVCLKEN0);
  389. /* check whether the CLOCK_ID_SE has been enabled or not */
  390. if (!(clken0_val & (0x1 << CLOCK_ID_SE))){
  391. /* if not initialized, we should init it */
  392. /* SE CLK = HOSC/1 */
  393. sys_write32((0 << 8) | (0 << 0), CMU_SECCLK);
  394. /* enable SE controller clock */
  395. acts_clock_peripheral_enable(CLOCK_ID_SE);
  396. /* reset SE controller */
  397. acts_reset_peripheral(RESET_ID_SE);
  398. }
  399. /* enable AES controller clock */
  400. sys_write32(AES_CTRL_CLK_EN, AES_CTRL);
  401. return 0;
  402. }
  403. /****************************************************************************
  404. * Name: aes_deinit
  405. *
  406. * Description:
  407. * Disable AES for energy saving purpose.
  408. *
  409. ****************************************************************************/
  410. static void aes_deinit(void)
  411. {
  412. /* disable aes clk */
  413. sys_write32(0, AES_CTRL);
  414. }
  415. /****************************************************************************
  416. * Name: aes_cypher
  417. *
  418. * Description:
  419. * aes encypher/decrypter realization.
  420. *
  421. ****************************************************************************/
  422. int aes_cypher(void *out, const void *in, size_t size,
  423. const void *iv, const void *key, size_t keysize,
  424. int mode, int encrypt)
  425. {
  426. int ret = 0;
  427. #ifdef CONFIG_AES_DMA
  428. /* Need request aes dma chan in the first init */
  429. if (g_aesdev.dma == NULL){
  430. g_aesdev.dma = device_get_binding(CONFIG_DMA_0_NAME);
  431. if (!g_aesdev.dma) {
  432. LOG_ERR("Bind DMA device %s error", CONFIG_DMA_0_NAME);
  433. return -ENOENT;
  434. }
  435. g_aesdev.in_chan = dma_request(g_aesdev.dma, 0xff);
  436. g_aesdev.out_chan = dma_request(g_aesdev.dma, 0xff);
  437. if ((g_aesdev.in_chan < 0) || (g_aesdev.out_chan < 0)) {
  438. LOG_ERR("dma-dev rxchan config err chan\n");
  439. return -ENODEV;
  440. }
  441. }
  442. #endif
  443. if (size % 16){
  444. return -EINVAL;
  445. }
  446. k_mutex_lock(&se_lock, K_FOREVER);
  447. aes_init();
  448. ret = aes_setup_mr(keysize, mode, encrypt);
  449. if (ret < 0){
  450. k_mutex_unlock(&se_lock);
  451. LOG_ERR("unsupported AES mode");
  452. return ret;
  453. }
  454. se_memcpy((void *)AES_KEY0, key, keysize, CPY_MEM_TO_MEM);
  455. if (iv != NULL)
  456. se_memcpy((void *)AES_IV0, iv, AES_BLOCK_SIZE, CPY_MEM_TO_MEM);
  457. #ifndef CONFIG_AES_DMA
  458. aes_encrypt_by_cpu(out, in, size);
  459. #else
  460. ret = aes_encrypt_by_dma(out, in, size);
  461. #endif
  462. aes_deinit();
  463. k_mutex_unlock(&se_lock);
  464. return ret;
  465. }