stm32f4xx_cryp_aes.c 21 KB

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