stm32f4xx_cryp_aes.c 21 KB

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