stm32f2xx_cryp_aes.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  1. /**
  2. ******************************************************************************
  3. * @file stm32f2xx_cryp_aes.c
  4. * @author MCD Application Team
  5. * @version V1.1.3
  6. * @date 31-December-2021
  7. * @brief This file provides high level functions to encrypt and decrypt an
  8. * input message using AES in ECB/CBC/CTR modes.
  9. * It uses the stm32f2xx_cryp.c/.h drivers to access the STM32F2xx CRYP
  10. * peripheral.
  11. *
  12. * @verbatim
  13. *
  14. * ===================================================================
  15. * How to use this driver
  16. * ===================================================================
  17. * 1. Enable The CRYP controller clock using
  18. * RCC_AHB2PeriphClockCmd(RCC_AHB2Periph_CRYP, ENABLE); function.
  19. *
  20. * 2. Encrypt and decrypt using AES in ECB Mode using CRYP_AES_ECB()
  21. * function.
  22. *
  23. * 3. Encrypt and decrypt using AES in CBC Mode using CRYP_AES_CBC()
  24. * function.
  25. *
  26. * 4. Encrypt and decrypt using AES in CTR Mode using CRYP_AES_CTR()
  27. * function.
  28. *
  29. * @endverbatim
  30. *
  31. ******************************************************************************
  32. * @attention
  33. *
  34. * Copyright (c) 2012 STMicroelectronics.
  35. * All rights reserved.
  36. *
  37. * This software is licensed under terms that can be found in the LICENSE file
  38. * in the root directory of this software component.
  39. * If no LICENSE file comes with this software, it is provided AS-IS.
  40. *
  41. ******************************************************************************
  42. */
  43. /* Includes ------------------------------------------------------------------*/
  44. #include "stm32f2xx_cryp.h"
  45. /** @addtogroup STM32F2xx_StdPeriph_Driver
  46. * @{
  47. */
  48. /** @defgroup CRYP
  49. * @brief CRYP driver modules
  50. * @{
  51. */
  52. /* Private typedef -----------------------------------------------------------*/
  53. /* Private define ------------------------------------------------------------*/
  54. #define AESBUSY_TIMEOUT ((uint32_t) 0x00010000)
  55. /* Private macro -------------------------------------------------------------*/
  56. /* Private variables ---------------------------------------------------------*/
  57. /* Private function prototypes -----------------------------------------------*/
  58. /* Private functions ---------------------------------------------------------*/
  59. /** @defgroup CRYP_Private_Functions
  60. * @{
  61. */
  62. /** @defgroup CRYP_Group6 High Level AES functions
  63. * @brief High Level AES functions
  64. *
  65. @verbatim
  66. ===============================================================================
  67. High Level AES functions
  68. ===============================================================================
  69. @endverbatim
  70. * @{
  71. */
  72. /**
  73. * @brief Encrypt and decrypt using AES in ECB Mode
  74. * @param Mode: encryption or decryption Mode.
  75. * This parameter can be one of the following values:
  76. * @arg MODE_ENCRYPT: Encryption
  77. * @arg MODE_DECRYPT: Decryption
  78. * @param Key: Key used for AES algorithm.
  79. * @param Keysize: length of the Key, must be a 128, 192 or 256.
  80. * @param Input: pointer to the Input buffer.
  81. * @param Ilength: length of the Input buffer, must be a multiple of 16.
  82. * @param Output: pointer to the returned buffer.
  83. * @retval An ErrorStatus enumeration value:
  84. * - SUCCESS: Operation done
  85. * - ERROR: Operation failed
  86. */
  87. ErrorStatus CRYP_AES_ECB(uint8_t Mode, uint8_t* Key, uint16_t Keysize,
  88. uint8_t* Input, uint32_t Ilength, uint8_t* Output)
  89. {
  90. CRYP_InitTypeDef AES_CRYP_InitStructure;
  91. CRYP_KeyInitTypeDef AES_CRYP_KeyInitStructure;
  92. __IO uint32_t counter = 0;
  93. uint32_t busystatus = 0;
  94. ErrorStatus status = SUCCESS;
  95. uint32_t keyaddr = (uint32_t)Key;
  96. uint32_t inputaddr = (uint32_t)Input;
  97. uint32_t outputaddr = (uint32_t)Output;
  98. uint32_t i = 0;
  99. /* Crypto structures initialisation*/
  100. CRYP_KeyStructInit(&AES_CRYP_KeyInitStructure);
  101. switch(Keysize)
  102. {
  103. case 128:
  104. AES_CRYP_InitStructure.CRYP_KeySize = CRYP_KeySize_128b;
  105. AES_CRYP_KeyInitStructure.CRYP_Key2Left = __REV(*(uint32_t*)(keyaddr));
  106. keyaddr+=4;
  107. AES_CRYP_KeyInitStructure.CRYP_Key2Right= __REV(*(uint32_t*)(keyaddr));
  108. keyaddr+=4;
  109. AES_CRYP_KeyInitStructure.CRYP_Key3Left = __REV(*(uint32_t*)(keyaddr));
  110. keyaddr+=4;
  111. AES_CRYP_KeyInitStructure.CRYP_Key3Right= __REV(*(uint32_t*)(keyaddr));
  112. break;
  113. case 192:
  114. AES_CRYP_InitStructure.CRYP_KeySize = CRYP_KeySize_192b;
  115. AES_CRYP_KeyInitStructure.CRYP_Key1Left = __REV(*(uint32_t*)(keyaddr));
  116. keyaddr+=4;
  117. AES_CRYP_KeyInitStructure.CRYP_Key1Right= __REV(*(uint32_t*)(keyaddr));
  118. keyaddr+=4;
  119. AES_CRYP_KeyInitStructure.CRYP_Key2Left = __REV(*(uint32_t*)(keyaddr));
  120. keyaddr+=4;
  121. AES_CRYP_KeyInitStructure.CRYP_Key2Right= __REV(*(uint32_t*)(keyaddr));
  122. keyaddr+=4;
  123. AES_CRYP_KeyInitStructure.CRYP_Key3Left = __REV(*(uint32_t*)(keyaddr));
  124. keyaddr+=4;
  125. AES_CRYP_KeyInitStructure.CRYP_Key3Right= __REV(*(uint32_t*)(keyaddr));
  126. break;
  127. case 256:
  128. AES_CRYP_InitStructure.CRYP_KeySize = CRYP_KeySize_256b;
  129. AES_CRYP_KeyInitStructure.CRYP_Key0Left = __REV(*(uint32_t*)(keyaddr));
  130. keyaddr+=4;
  131. AES_CRYP_KeyInitStructure.CRYP_Key0Right= __REV(*(uint32_t*)(keyaddr));
  132. keyaddr+=4;
  133. AES_CRYP_KeyInitStructure.CRYP_Key1Left = __REV(*(uint32_t*)(keyaddr));
  134. keyaddr+=4;
  135. AES_CRYP_KeyInitStructure.CRYP_Key1Right= __REV(*(uint32_t*)(keyaddr));
  136. keyaddr+=4;
  137. AES_CRYP_KeyInitStructure.CRYP_Key2Left = __REV(*(uint32_t*)(keyaddr));
  138. keyaddr+=4;
  139. AES_CRYP_KeyInitStructure.CRYP_Key2Right= __REV(*(uint32_t*)(keyaddr));
  140. keyaddr+=4;
  141. AES_CRYP_KeyInitStructure.CRYP_Key3Left = __REV(*(uint32_t*)(keyaddr));
  142. keyaddr+=4;
  143. AES_CRYP_KeyInitStructure.CRYP_Key3Right= __REV(*(uint32_t*)(keyaddr));
  144. break;
  145. default:
  146. break;
  147. }
  148. /*------------------ AES Decryption ------------------*/
  149. if(Mode == MODE_DECRYPT) /* AES decryption */
  150. {
  151. /* Flush IN/OUT FIFOs */
  152. CRYP_FIFOFlush();
  153. /* Crypto Init for Key preparation for decryption process */
  154. AES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Decrypt;
  155. AES_CRYP_InitStructure.CRYP_AlgoMode = CRYP_AlgoMode_AES_Key;
  156. AES_CRYP_InitStructure.CRYP_DataType = CRYP_DataType_32b;
  157. CRYP_Init(&AES_CRYP_InitStructure);
  158. /* Key Initialisation */
  159. CRYP_KeyInit(&AES_CRYP_KeyInitStructure);
  160. /* Enable Crypto processor */
  161. CRYP_Cmd(ENABLE);
  162. /* wait until the Busy flag is RESET */
  163. do
  164. {
  165. busystatus = CRYP_GetFlagStatus(CRYP_FLAG_BUSY);
  166. counter++;
  167. }while ((counter != AESBUSY_TIMEOUT) && (busystatus != RESET));
  168. if (busystatus != RESET)
  169. {
  170. status = ERROR;
  171. }
  172. else
  173. {
  174. /* Crypto Init for decryption process */
  175. AES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Decrypt;
  176. }
  177. }
  178. /*------------------ AES Encryption ------------------*/
  179. else /* AES encryption */
  180. {
  181. CRYP_KeyInit(&AES_CRYP_KeyInitStructure);
  182. /* Crypto Init for Encryption process */
  183. AES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Encrypt;
  184. }
  185. AES_CRYP_InitStructure.CRYP_AlgoMode = CRYP_AlgoMode_AES_ECB;
  186. AES_CRYP_InitStructure.CRYP_DataType = CRYP_DataType_8b;
  187. CRYP_Init(&AES_CRYP_InitStructure);
  188. /* Flush IN/OUT FIFOs */
  189. CRYP_FIFOFlush();
  190. /* Enable Crypto processor */
  191. CRYP_Cmd(ENABLE);
  192. for(i=0; ((i<Ilength) && (status != ERROR)); i+=16)
  193. {
  194. /* Write the Input block in the IN FIFO */
  195. CRYP_DataIn(*(uint32_t*)(inputaddr));
  196. inputaddr+=4;
  197. CRYP_DataIn(*(uint32_t*)(inputaddr));
  198. inputaddr+=4;
  199. CRYP_DataIn(*(uint32_t*)(inputaddr));
  200. inputaddr+=4;
  201. CRYP_DataIn(*(uint32_t*)(inputaddr));
  202. inputaddr+=4;
  203. /* Wait until the complete message has been processed */
  204. counter = 0;
  205. do
  206. {
  207. busystatus = CRYP_GetFlagStatus(CRYP_FLAG_BUSY);
  208. counter++;
  209. }while ((counter != AESBUSY_TIMEOUT) && (busystatus != RESET));
  210. if (busystatus != RESET)
  211. {
  212. status = ERROR;
  213. }
  214. else
  215. {
  216. /* Read the Output block from the Output FIFO */
  217. *(uint32_t*)(outputaddr) = CRYP_DataOut();
  218. outputaddr+=4;
  219. *(uint32_t*)(outputaddr) = CRYP_DataOut();
  220. outputaddr+=4;
  221. *(uint32_t*)(outputaddr) = CRYP_DataOut();
  222. outputaddr+=4;
  223. *(uint32_t*)(outputaddr) = CRYP_DataOut();
  224. outputaddr+=4;
  225. }
  226. }
  227. /* Disable Crypto */
  228. CRYP_Cmd(DISABLE);
  229. return status;
  230. }
  231. /**
  232. * @brief Encrypt and decrypt using AES in CBC Mode
  233. * @param Mode: encryption or decryption Mode.
  234. * This parameter can be one of the following values:
  235. * @arg MODE_ENCRYPT: Encryption
  236. * @arg MODE_DECRYPT: Decryption
  237. * @param InitVectors: Initialisation Vectors used for AES algorithm.
  238. * @param Key: Key used for AES algorithm.
  239. * @param Keysize: length of the Key, must be a 128, 192 or 256.
  240. * @param Input: pointer to the Input buffer.
  241. * @param Ilength: length of the Input buffer, must be a multiple of 16.
  242. * @param Output: pointer to the returned buffer.
  243. * @retval An ErrorStatus enumeration value:
  244. * - SUCCESS: Operation done
  245. * - ERROR: Operation failed
  246. */
  247. ErrorStatus CRYP_AES_CBC(uint8_t Mode, uint8_t InitVectors[16], uint8_t *Key,
  248. uint16_t Keysize, uint8_t *Input, uint32_t Ilength,
  249. uint8_t *Output)
  250. {
  251. CRYP_InitTypeDef AES_CRYP_InitStructure;
  252. CRYP_KeyInitTypeDef AES_CRYP_KeyInitStructure;
  253. CRYP_IVInitTypeDef AES_CRYP_IVInitStructure;
  254. __IO uint32_t counter = 0;
  255. uint32_t busystatus = 0;
  256. ErrorStatus status = SUCCESS;
  257. uint32_t keyaddr = (uint32_t)Key;
  258. uint32_t inputaddr = (uint32_t)Input;
  259. uint32_t outputaddr = (uint32_t)Output;
  260. uint32_t ivaddr = (uint32_t)InitVectors;
  261. uint32_t i = 0;
  262. /* Crypto structures initialisation*/
  263. CRYP_KeyStructInit(&AES_CRYP_KeyInitStructure);
  264. switch(Keysize)
  265. {
  266. case 128:
  267. AES_CRYP_InitStructure.CRYP_KeySize = CRYP_KeySize_128b;
  268. AES_CRYP_KeyInitStructure.CRYP_Key2Left = __REV(*(uint32_t*)(keyaddr));
  269. keyaddr+=4;
  270. AES_CRYP_KeyInitStructure.CRYP_Key2Right= __REV(*(uint32_t*)(keyaddr));
  271. keyaddr+=4;
  272. AES_CRYP_KeyInitStructure.CRYP_Key3Left = __REV(*(uint32_t*)(keyaddr));
  273. keyaddr+=4;
  274. AES_CRYP_KeyInitStructure.CRYP_Key3Right= __REV(*(uint32_t*)(keyaddr));
  275. break;
  276. case 192:
  277. AES_CRYP_InitStructure.CRYP_KeySize = CRYP_KeySize_192b;
  278. AES_CRYP_KeyInitStructure.CRYP_Key1Left = __REV(*(uint32_t*)(keyaddr));
  279. keyaddr+=4;
  280. AES_CRYP_KeyInitStructure.CRYP_Key1Right= __REV(*(uint32_t*)(keyaddr));
  281. keyaddr+=4;
  282. AES_CRYP_KeyInitStructure.CRYP_Key2Left = __REV(*(uint32_t*)(keyaddr));
  283. keyaddr+=4;
  284. AES_CRYP_KeyInitStructure.CRYP_Key2Right= __REV(*(uint32_t*)(keyaddr));
  285. keyaddr+=4;
  286. AES_CRYP_KeyInitStructure.CRYP_Key3Left = __REV(*(uint32_t*)(keyaddr));
  287. keyaddr+=4;
  288. AES_CRYP_KeyInitStructure.CRYP_Key3Right= __REV(*(uint32_t*)(keyaddr));
  289. break;
  290. case 256:
  291. AES_CRYP_InitStructure.CRYP_KeySize = CRYP_KeySize_256b;
  292. AES_CRYP_KeyInitStructure.CRYP_Key0Left = __REV(*(uint32_t*)(keyaddr));
  293. keyaddr+=4;
  294. AES_CRYP_KeyInitStructure.CRYP_Key0Right= __REV(*(uint32_t*)(keyaddr));
  295. keyaddr+=4;
  296. AES_CRYP_KeyInitStructure.CRYP_Key1Left = __REV(*(uint32_t*)(keyaddr));
  297. keyaddr+=4;
  298. AES_CRYP_KeyInitStructure.CRYP_Key1Right= __REV(*(uint32_t*)(keyaddr));
  299. keyaddr+=4;
  300. AES_CRYP_KeyInitStructure.CRYP_Key2Left = __REV(*(uint32_t*)(keyaddr));
  301. keyaddr+=4;
  302. AES_CRYP_KeyInitStructure.CRYP_Key2Right= __REV(*(uint32_t*)(keyaddr));
  303. keyaddr+=4;
  304. AES_CRYP_KeyInitStructure.CRYP_Key3Left = __REV(*(uint32_t*)(keyaddr));
  305. keyaddr+=4;
  306. AES_CRYP_KeyInitStructure.CRYP_Key3Right= __REV(*(uint32_t*)(keyaddr));
  307. break;
  308. default:
  309. break;
  310. }
  311. /* CRYP Initialization Vectors */
  312. AES_CRYP_IVInitStructure.CRYP_IV0Left = __REV(*(uint32_t*)(ivaddr));
  313. ivaddr+=4;
  314. AES_CRYP_IVInitStructure.CRYP_IV0Right= __REV(*(uint32_t*)(ivaddr));
  315. ivaddr+=4;
  316. AES_CRYP_IVInitStructure.CRYP_IV1Left = __REV(*(uint32_t*)(ivaddr));
  317. ivaddr+=4;
  318. AES_CRYP_IVInitStructure.CRYP_IV1Right= __REV(*(uint32_t*)(ivaddr));
  319. /*------------------ AES Decryption ------------------*/
  320. if(Mode == MODE_DECRYPT) /* AES decryption */
  321. {
  322. /* Flush IN/OUT FIFOs */
  323. CRYP_FIFOFlush();
  324. /* Crypto Init for Key preparation for decryption process */
  325. AES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Decrypt;
  326. AES_CRYP_InitStructure.CRYP_AlgoMode = CRYP_AlgoMode_AES_Key;
  327. AES_CRYP_InitStructure.CRYP_DataType = CRYP_DataType_32b;
  328. CRYP_Init(&AES_CRYP_InitStructure);
  329. /* Key Initialisation */
  330. CRYP_KeyInit(&AES_CRYP_KeyInitStructure);
  331. /* Enable Crypto processor */
  332. CRYP_Cmd(ENABLE);
  333. /* wait until the Busy flag is RESET */
  334. do
  335. {
  336. busystatus = CRYP_GetFlagStatus(CRYP_FLAG_BUSY);
  337. counter++;
  338. }while ((counter != AESBUSY_TIMEOUT) && (busystatus != RESET));
  339. if (busystatus != RESET)
  340. {
  341. status = ERROR;
  342. }
  343. else
  344. {
  345. /* Crypto Init for decryption process */
  346. AES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Decrypt;
  347. }
  348. }
  349. /*------------------ AES Encryption ------------------*/
  350. else /* AES encryption */
  351. {
  352. CRYP_KeyInit(&AES_CRYP_KeyInitStructure);
  353. /* Crypto Init for Encryption process */
  354. AES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Encrypt;
  355. }
  356. AES_CRYP_InitStructure.CRYP_AlgoMode = CRYP_AlgoMode_AES_CBC;
  357. AES_CRYP_InitStructure.CRYP_DataType = CRYP_DataType_8b;
  358. CRYP_Init(&AES_CRYP_InitStructure);
  359. /* CRYP Initialization Vectors */
  360. CRYP_IVInit(&AES_CRYP_IVInitStructure);
  361. /* Flush IN/OUT FIFOs */
  362. CRYP_FIFOFlush();
  363. /* Enable Crypto processor */
  364. CRYP_Cmd(ENABLE);
  365. for(i=0; ((i<Ilength) && (status != ERROR)); i+=16)
  366. {
  367. /* Write the Input block in the IN FIFO */
  368. CRYP_DataIn(*(uint32_t*)(inputaddr));
  369. inputaddr+=4;
  370. CRYP_DataIn(*(uint32_t*)(inputaddr));
  371. inputaddr+=4;
  372. CRYP_DataIn(*(uint32_t*)(inputaddr));
  373. inputaddr+=4;
  374. CRYP_DataIn(*(uint32_t*)(inputaddr));
  375. inputaddr+=4;
  376. /* Wait until the complete message has been processed */
  377. counter = 0;
  378. do
  379. {
  380. busystatus = CRYP_GetFlagStatus(CRYP_FLAG_BUSY);
  381. counter++;
  382. }while ((counter != AESBUSY_TIMEOUT) && (busystatus != RESET));
  383. if (busystatus != RESET)
  384. {
  385. status = ERROR;
  386. }
  387. else
  388. {
  389. /* Read the Output block from the Output FIFO */
  390. *(uint32_t*)(outputaddr) = CRYP_DataOut();
  391. outputaddr+=4;
  392. *(uint32_t*)(outputaddr) = CRYP_DataOut();
  393. outputaddr+=4;
  394. *(uint32_t*)(outputaddr) = CRYP_DataOut();
  395. outputaddr+=4;
  396. *(uint32_t*)(outputaddr) = CRYP_DataOut();
  397. outputaddr+=4;
  398. }
  399. }
  400. /* Disable Crypto */
  401. CRYP_Cmd(DISABLE);
  402. return status;
  403. }
  404. /**
  405. * @brief Encrypt and decrypt using AES in CTR Mode
  406. * @param Mode: encryption or decryption Mode.
  407. * This parameter can be one of the following values:
  408. * @arg MODE_ENCRYPT: Encryption
  409. * @arg MODE_DECRYPT: Decryption
  410. * @param InitVectors: Initialisation Vectors used for AES algorithm.
  411. * @param Key: Key used for AES algorithm.
  412. * @param Keysize: length of the Key, must be a 128, 192 or 256.
  413. * @param Input: pointer to the Input buffer.
  414. * @param Ilength: length of the Input buffer, must be a multiple of 16.
  415. * @param Output: pointer to the returned buffer.
  416. * @retval An ErrorStatus enumeration value:
  417. * - SUCCESS: Operation done
  418. * - ERROR: Operation failed
  419. */
  420. ErrorStatus CRYP_AES_CTR(uint8_t Mode, uint8_t InitVectors[16], uint8_t *Key,
  421. uint16_t Keysize, uint8_t *Input, uint32_t Ilength,
  422. uint8_t *Output)
  423. {
  424. CRYP_InitTypeDef AES_CRYP_InitStructure;
  425. CRYP_KeyInitTypeDef AES_CRYP_KeyInitStructure;
  426. CRYP_IVInitTypeDef AES_CRYP_IVInitStructure;
  427. __IO uint32_t counter = 0;
  428. uint32_t busystatus = 0;
  429. ErrorStatus status = SUCCESS;
  430. uint32_t keyaddr = (uint32_t)Key;
  431. uint32_t inputaddr = (uint32_t)Input;
  432. uint32_t outputaddr = (uint32_t)Output;
  433. uint32_t ivaddr = (uint32_t)InitVectors;
  434. uint32_t i = 0;
  435. /* Crypto structures initialisation*/
  436. CRYP_KeyStructInit(&AES_CRYP_KeyInitStructure);
  437. switch(Keysize)
  438. {
  439. case 128:
  440. AES_CRYP_InitStructure.CRYP_KeySize = CRYP_KeySize_128b;
  441. AES_CRYP_KeyInitStructure.CRYP_Key2Left = __REV(*(uint32_t*)(keyaddr));
  442. keyaddr+=4;
  443. AES_CRYP_KeyInitStructure.CRYP_Key2Right= __REV(*(uint32_t*)(keyaddr));
  444. keyaddr+=4;
  445. AES_CRYP_KeyInitStructure.CRYP_Key3Left = __REV(*(uint32_t*)(keyaddr));
  446. keyaddr+=4;
  447. AES_CRYP_KeyInitStructure.CRYP_Key3Right= __REV(*(uint32_t*)(keyaddr));
  448. break;
  449. case 192:
  450. AES_CRYP_InitStructure.CRYP_KeySize = CRYP_KeySize_192b;
  451. AES_CRYP_KeyInitStructure.CRYP_Key1Left = __REV(*(uint32_t*)(keyaddr));
  452. keyaddr+=4;
  453. AES_CRYP_KeyInitStructure.CRYP_Key1Right= __REV(*(uint32_t*)(keyaddr));
  454. keyaddr+=4;
  455. AES_CRYP_KeyInitStructure.CRYP_Key2Left = __REV(*(uint32_t*)(keyaddr));
  456. keyaddr+=4;
  457. AES_CRYP_KeyInitStructure.CRYP_Key2Right= __REV(*(uint32_t*)(keyaddr));
  458. keyaddr+=4;
  459. AES_CRYP_KeyInitStructure.CRYP_Key3Left = __REV(*(uint32_t*)(keyaddr));
  460. keyaddr+=4;
  461. AES_CRYP_KeyInitStructure.CRYP_Key3Right= __REV(*(uint32_t*)(keyaddr));
  462. break;
  463. case 256:
  464. AES_CRYP_InitStructure.CRYP_KeySize = CRYP_KeySize_256b;
  465. AES_CRYP_KeyInitStructure.CRYP_Key0Left = __REV(*(uint32_t*)(keyaddr));
  466. keyaddr+=4;
  467. AES_CRYP_KeyInitStructure.CRYP_Key0Right= __REV(*(uint32_t*)(keyaddr));
  468. keyaddr+=4;
  469. AES_CRYP_KeyInitStructure.CRYP_Key1Left = __REV(*(uint32_t*)(keyaddr));
  470. keyaddr+=4;
  471. AES_CRYP_KeyInitStructure.CRYP_Key1Right= __REV(*(uint32_t*)(keyaddr));
  472. keyaddr+=4;
  473. AES_CRYP_KeyInitStructure.CRYP_Key2Left = __REV(*(uint32_t*)(keyaddr));
  474. keyaddr+=4;
  475. AES_CRYP_KeyInitStructure.CRYP_Key2Right= __REV(*(uint32_t*)(keyaddr));
  476. keyaddr+=4;
  477. AES_CRYP_KeyInitStructure.CRYP_Key3Left = __REV(*(uint32_t*)(keyaddr));
  478. keyaddr+=4;
  479. AES_CRYP_KeyInitStructure.CRYP_Key3Right= __REV(*(uint32_t*)(keyaddr));
  480. break;
  481. default:
  482. break;
  483. }
  484. /* CRYP Initialization Vectors */
  485. AES_CRYP_IVInitStructure.CRYP_IV0Left = __REV(*(uint32_t*)(ivaddr));
  486. ivaddr+=4;
  487. AES_CRYP_IVInitStructure.CRYP_IV0Right= __REV(*(uint32_t*)(ivaddr));
  488. ivaddr+=4;
  489. AES_CRYP_IVInitStructure.CRYP_IV1Left = __REV(*(uint32_t*)(ivaddr));
  490. ivaddr+=4;
  491. AES_CRYP_IVInitStructure.CRYP_IV1Right= __REV(*(uint32_t*)(ivaddr));
  492. /* Key Initialisation */
  493. CRYP_KeyInit(&AES_CRYP_KeyInitStructure);
  494. /*------------------ AES Decryption ------------------*/
  495. if(Mode == MODE_DECRYPT) /* AES decryption */
  496. {
  497. /* Crypto Init for decryption process */
  498. AES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Decrypt;
  499. }
  500. /*------------------ AES Encryption ------------------*/
  501. else /* AES encryption */
  502. {
  503. /* Crypto Init for Encryption process */
  504. AES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Encrypt;
  505. }
  506. AES_CRYP_InitStructure.CRYP_AlgoMode = CRYP_AlgoMode_AES_CTR;
  507. AES_CRYP_InitStructure.CRYP_DataType = CRYP_DataType_8b;
  508. CRYP_Init(&AES_CRYP_InitStructure);
  509. /* CRYP Initialization Vectors */
  510. CRYP_IVInit(&AES_CRYP_IVInitStructure);
  511. /* Flush IN/OUT FIFOs */
  512. CRYP_FIFOFlush();
  513. /* Enable Crypto processor */
  514. CRYP_Cmd(ENABLE);
  515. for(i=0; ((i<Ilength) && (status != ERROR)); i+=16)
  516. {
  517. /* Write the Input block in the IN FIFO */
  518. CRYP_DataIn(*(uint32_t*)(inputaddr));
  519. inputaddr+=4;
  520. CRYP_DataIn(*(uint32_t*)(inputaddr));
  521. inputaddr+=4;
  522. CRYP_DataIn(*(uint32_t*)(inputaddr));
  523. inputaddr+=4;
  524. CRYP_DataIn(*(uint32_t*)(inputaddr));
  525. inputaddr+=4;
  526. /* Wait until the complete message has been processed */
  527. counter = 0;
  528. do
  529. {
  530. busystatus = CRYP_GetFlagStatus(CRYP_FLAG_BUSY);
  531. counter++;
  532. }while ((counter != AESBUSY_TIMEOUT) && (busystatus != RESET));
  533. if (busystatus != RESET)
  534. {
  535. status = ERROR;
  536. }
  537. else
  538. {
  539. /* Read the Output block from the Output FIFO */
  540. *(uint32_t*)(outputaddr) = CRYP_DataOut();
  541. outputaddr+=4;
  542. *(uint32_t*)(outputaddr) = CRYP_DataOut();
  543. outputaddr+=4;
  544. *(uint32_t*)(outputaddr) = CRYP_DataOut();
  545. outputaddr+=4;
  546. *(uint32_t*)(outputaddr) = CRYP_DataOut();
  547. outputaddr+=4;
  548. }
  549. }
  550. /* Disable Crypto */
  551. CRYP_Cmd(DISABLE);
  552. return status;
  553. }
  554. /**
  555. * @}
  556. */
  557. /**
  558. * @}
  559. */
  560. /**
  561. * @}
  562. */
  563. /**
  564. * @}
  565. */