stm32f4xx_cryp.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_cryp.c
  4. * @author MCD Application Team
  5. * @version V1.0.0
  6. * @date 30-September-2011
  7. * @brief This file provides firmware functions to manage the following
  8. * functionalities of the Cryptographic processor (CRYP) peripheral:
  9. * - Initialization and Configuration functions
  10. * - Data treatment functions
  11. * - Context swapping functions
  12. * - DMA interface function
  13. * - Interrupts and flags management
  14. *
  15. * @verbatim
  16. *
  17. * ===================================================================
  18. * How to use this driver
  19. * ===================================================================
  20. * 1. Enable the CRYP controller clock using
  21. * RCC_AHB2PeriphClockCmd(RCC_AHB2Periph_CRYP, ENABLE); function.
  22. *
  23. * 2. Initialise the CRYP using CRYP_Init(), CRYP_KeyInit() and if
  24. * needed CRYP_IVInit().
  25. *
  26. * 3. Flush the IN and OUT FIFOs by using CRYP_FIFOFlush() function.
  27. *
  28. * 4. Enable the CRYP controller using the CRYP_Cmd() function.
  29. *
  30. * 5. If using DMA for Data input and output transfer,
  31. * Activate the needed DMA Requests using CRYP_DMACmd() function
  32. * 6. If DMA is not used for data transfer, use CRYP_DataIn() and
  33. * CRYP_DataOut() functions to enter data to IN FIFO and get result
  34. * from OUT FIFO.
  35. *
  36. * 7. To control CRYP events you can use one of the following
  37. * two methods:
  38. * - Check on CRYP flags using the CRYP_GetFlagStatus() function.
  39. * - Use CRYP interrupts through the function CRYP_ITConfig() at
  40. * initialization phase and CRYP_GetITStatus() function into
  41. * interrupt routines in processing phase.
  42. *
  43. * 8. Save and restore Cryptographic processor context using
  44. * CRYP_SaveContext() and CRYP_RestoreContext() functions.
  45. *
  46. *
  47. * ===================================================================
  48. * Procedure to perform an encryption or a decryption
  49. * ===================================================================
  50. *
  51. * Initialization
  52. * ===============
  53. * 1. Initialize the peripheral using CRYP_Init(), CRYP_KeyInit() and
  54. * CRYP_IVInit functions:
  55. * - Configure the key size (128-, 192- or 256-bit, in the AES only)
  56. * - Enter the symmetric key
  57. * - Configure the data type
  58. * - In case of decryption in AES-ECB or AES-CBC, you must prepare
  59. * the key: configure the key preparation mode. Then Enable the CRYP
  60. * peripheral using CRYP_Cmd() function: the BUSY flag is set.
  61. * Wait until BUSY flag is reset : the key is prepared for decryption
  62. * - Configure the algorithm and chaining (the DES/TDES in ECB/CBC, the
  63. * AES in ECB/CBC/CTR)
  64. * - Configure the direction (encryption/decryption).
  65. * - Write the initialization vectors (in CBC or CTR modes only)
  66. *
  67. * 2. Flush the IN and OUT FIFOs using the CRYP_FIFOFlush() function
  68. *
  69. *
  70. * Basic Processing mode (polling mode)
  71. * ====================================
  72. * 1. Enable the cryptographic processor using CRYP_Cmd() function.
  73. *
  74. * 2. Write the first blocks in the input FIFO (2 to 8 words) using
  75. * CRYP_DataIn() function.
  76. *
  77. * 3. Repeat the following sequence until the complete message has been
  78. * processed:
  79. *
  80. * a) Wait for flag CRYP_FLAG_OFNE occurs (using CRYP_GetFlagStatus()
  81. * function), then read the OUT-FIFO using CRYP_DataOut() function
  82. * (1 block or until the FIFO is empty)
  83. *
  84. * b) Wait for flag CRYP_FLAG_IFNF occurs, (using CRYP_GetFlagStatus()
  85. * function then write the IN FIFO using CRYP_DataIn() function
  86. * (1 block or until the FIFO is full)
  87. *
  88. * 4. At the end of the processing, CRYP_FLAG_BUSY flag will be reset and
  89. * both FIFOs are empty (CRYP_FLAG_IFEM is set and CRYP_FLAG_OFNE is
  90. * reset). You can disable the peripheral using CRYP_Cmd() function.
  91. *
  92. * Interrupts Processing mode
  93. * ===========================
  94. * In this mode, Processing is done when the data are transferred by the
  95. * CPU during interrupts.
  96. *
  97. * 1. Enable the interrupts CRYP_IT_INI and CRYP_IT_OUTI using
  98. * CRYP_ITConfig() function.
  99. *
  100. * 2. Enable the cryptographic processor using CRYP_Cmd() function.
  101. *
  102. * 3. In the CRYP_IT_INI interrupt handler : load the input message into the
  103. * IN FIFO using CRYP_DataIn() function . You can load 2 or 4 words at a
  104. * time, or load data until the IN FIFO is full. When the last word of
  105. * the message has been entered into the IN FIFO, disable the CRYP_IT_INI
  106. * interrupt (using CRYP_ITConfig() function).
  107. *
  108. * 4. In the CRYP_IT_OUTI interrupt handler : read the output message from
  109. * the OUT FIFO using CRYP_DataOut() function. You can read 1 block (2 or
  110. * 4 words) at a time or read data until the FIFO is empty.
  111. * When the last word has been read, INIM=0, BUSY=0 and both FIFOs are
  112. * empty (CRYP_FLAG_IFEM is set and CRYP_FLAG_OFNE is reset).
  113. * You can disable the CRYP_IT_OUTI interrupt (using CRYP_ITConfig()
  114. * function) and you can disable the peripheral using CRYP_Cmd() function.
  115. *
  116. * DMA Processing mode
  117. * ====================
  118. * In this mode, Processing is done when the DMA is used to transfer the
  119. * data from/to the memory.
  120. *
  121. * 1. Configure the DMA controller to transfer the input data from the
  122. * memory using DMA_Init() function.
  123. * The transfer length is the length of the message.
  124. * As message padding is not managed by the peripheral, the message
  125. * length must be an entire number of blocks. The data are transferred
  126. * in burst mode. The burst length is 4 words in the AES and 2 or 4
  127. * words in the DES/TDES. The DMA should be configured to set an
  128. * interrupt on transfer completion of the output data to indicate that
  129. * the processing is finished.
  130. * Refer to DMA peripheral driver for more details.
  131. *
  132. * 2. Enable the cryptographic processor using CRYP_Cmd() function.
  133. * Enable the DMA requests CRYP_DMAReq_DataIN and CRYP_DMAReq_DataOUT
  134. * using CRYP_DMACmd() function.
  135. *
  136. * 3. All the transfers and processing are managed by the DMA and the
  137. * cryptographic processor. The DMA transfer complete interrupt indicates
  138. * that the processing is complete. Both FIFOs are normally empty and
  139. * CRYP_FLAG_BUSY flag is reset.
  140. *
  141. * @endverbatim
  142. *
  143. ******************************************************************************
  144. * @attention
  145. *
  146. * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  147. * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  148. * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  149. * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  150. * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  151. * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  152. *
  153. * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
  154. ******************************************************************************
  155. */
  156. /* Includes ------------------------------------------------------------------*/
  157. #include "stm32f4xx_cryp.h"
  158. #include "stm32f4xx_rcc.h"
  159. /** @addtogroup STM32F4xx_StdPeriph_Driver
  160. * @{
  161. */
  162. /** @defgroup CRYP
  163. * @brief CRYP driver modules
  164. * @{
  165. */
  166. /* Private typedef -----------------------------------------------------------*/
  167. /* Private define ------------------------------------------------------------*/
  168. #define FLAG_MASK ((uint8_t)0x20)
  169. #define MAX_TIMEOUT ((uint16_t)0xFFFF)
  170. /* Private macro -------------------------------------------------------------*/
  171. /* Private variables ---------------------------------------------------------*/
  172. /* Private function prototypes -----------------------------------------------*/
  173. /* Private functions ---------------------------------------------------------*/
  174. /** @defgroup CRYP_Private_Functions
  175. * @{
  176. */
  177. /** @defgroup CRYP_Group1 Initialization and Configuration functions
  178. * @brief Initialization and Configuration functions
  179. *
  180. @verbatim
  181. ===============================================================================
  182. Initialization and Configuration functions
  183. ===============================================================================
  184. This section provides functions allowing to
  185. - Initialize the cryptographic Processor using CRYP_Init() function
  186. - Encrypt or Decrypt
  187. - mode : TDES-ECB, TDES-CBC,
  188. DES-ECB, DES-CBC,
  189. AES-ECB, AES-CBC, AES-CTR, AES-Key
  190. - DataType : 32-bit data, 16-bit data, bit data or bit-string
  191. - Key Size (only in AES modes)
  192. - Configure the Encrypt or Decrypt Key using CRYP_KeyInit() function
  193. - Configure the Initialization Vectors(IV) for CBC and CTR modes using
  194. CRYP_IVInit() function.
  195. - Flushes the IN and OUT FIFOs : using CRYP_FIFOFlush() function.
  196. - Enable or disable the CRYP Processor using CRYP_Cmd() function
  197. @endverbatim
  198. * @{
  199. */
  200. /**
  201. * @brief Deinitializes the CRYP peripheral registers to their default reset values
  202. * @param None
  203. * @retval None
  204. */
  205. void CRYP_DeInit(void)
  206. {
  207. /* Enable CRYP reset state */
  208. RCC_AHB2PeriphResetCmd(RCC_AHB2Periph_CRYP, ENABLE);
  209. /* Release CRYP from reset state */
  210. RCC_AHB2PeriphResetCmd(RCC_AHB2Periph_CRYP, DISABLE);
  211. }
  212. /**
  213. * @brief Initializes the CRYP peripheral according to the specified parameters
  214. * in the CRYP_InitStruct.
  215. * @param CRYP_InitStruct: pointer to a CRYP_InitTypeDef structure that contains
  216. * the configuration information for the CRYP peripheral.
  217. * @retval None
  218. */
  219. void CRYP_Init(CRYP_InitTypeDef* CRYP_InitStruct)
  220. {
  221. /* Check the parameters */
  222. assert_param(IS_CRYP_ALGOMODE(CRYP_InitStruct->CRYP_AlgoMode));
  223. assert_param(IS_CRYP_DATATYPE(CRYP_InitStruct->CRYP_DataType));
  224. assert_param(IS_CRYP_ALGODIR(CRYP_InitStruct->CRYP_AlgoDir));
  225. /* Select Algorithm mode*/
  226. CRYP->CR &= ~CRYP_CR_ALGOMODE;
  227. CRYP->CR |= CRYP_InitStruct->CRYP_AlgoMode;
  228. /* Select dataType */
  229. CRYP->CR &= ~CRYP_CR_DATATYPE;
  230. CRYP->CR |= CRYP_InitStruct->CRYP_DataType;
  231. /* select Key size (used only with AES algorithm) */
  232. if ((CRYP_InitStruct->CRYP_AlgoMode == CRYP_AlgoMode_AES_ECB) ||
  233. (CRYP_InitStruct->CRYP_AlgoMode == CRYP_AlgoMode_AES_CBC) ||
  234. (CRYP_InitStruct->CRYP_AlgoMode == CRYP_AlgoMode_AES_CTR) ||
  235. (CRYP_InitStruct->CRYP_AlgoMode == CRYP_AlgoMode_AES_Key))
  236. {
  237. assert_param(IS_CRYP_KEYSIZE(CRYP_InitStruct->CRYP_KeySize));
  238. CRYP->CR &= ~CRYP_CR_KEYSIZE;
  239. CRYP->CR |= CRYP_InitStruct->CRYP_KeySize; /* Key size and value must be
  240. configured once the key has
  241. been prepared */
  242. }
  243. /* Select data Direction */
  244. CRYP->CR &= ~CRYP_CR_ALGODIR;
  245. CRYP->CR |= CRYP_InitStruct->CRYP_AlgoDir;
  246. }
  247. /**
  248. * @brief Fills each CRYP_InitStruct member with its default value.
  249. * @param CRYP_InitStruct: pointer to a CRYP_InitTypeDef structure which will
  250. * be initialized.
  251. * @retval None
  252. */
  253. void CRYP_StructInit(CRYP_InitTypeDef* CRYP_InitStruct)
  254. {
  255. /* Initialize the CRYP_AlgoDir member */
  256. CRYP_InitStruct->CRYP_AlgoDir = CRYP_AlgoDir_Encrypt;
  257. /* initialize the CRYP_AlgoMode member */
  258. CRYP_InitStruct->CRYP_AlgoMode = CRYP_AlgoMode_TDES_ECB;
  259. /* initialize the CRYP_DataType member */
  260. CRYP_InitStruct->CRYP_DataType = CRYP_DataType_32b;
  261. /* Initialize the CRYP_KeySize member */
  262. CRYP_InitStruct->CRYP_KeySize = CRYP_KeySize_128b;
  263. }
  264. /**
  265. * @brief Initializes the CRYP Keys according to the specified parameters in
  266. * the CRYP_KeyInitStruct.
  267. * @param CRYP_KeyInitStruct: pointer to a CRYP_KeyInitTypeDef structure that
  268. * contains the configuration information for the CRYP Keys.
  269. * @retval None
  270. */
  271. void CRYP_KeyInit(CRYP_KeyInitTypeDef* CRYP_KeyInitStruct)
  272. {
  273. /* Key Initialisation */
  274. CRYP->K0LR = CRYP_KeyInitStruct->CRYP_Key0Left;
  275. CRYP->K0RR = CRYP_KeyInitStruct->CRYP_Key0Right;
  276. CRYP->K1LR = CRYP_KeyInitStruct->CRYP_Key1Left;
  277. CRYP->K1RR = CRYP_KeyInitStruct->CRYP_Key1Right;
  278. CRYP->K2LR = CRYP_KeyInitStruct->CRYP_Key2Left;
  279. CRYP->K2RR = CRYP_KeyInitStruct->CRYP_Key2Right;
  280. CRYP->K3LR = CRYP_KeyInitStruct->CRYP_Key3Left;
  281. CRYP->K3RR = CRYP_KeyInitStruct->CRYP_Key3Right;
  282. }
  283. /**
  284. * @brief Fills each CRYP_KeyInitStruct member with its default value.
  285. * @param CRYP_KeyInitStruct: pointer to a CRYP_KeyInitTypeDef structure
  286. * which will be initialized.
  287. * @retval None
  288. */
  289. void CRYP_KeyStructInit(CRYP_KeyInitTypeDef* CRYP_KeyInitStruct)
  290. {
  291. CRYP_KeyInitStruct->CRYP_Key0Left = 0;
  292. CRYP_KeyInitStruct->CRYP_Key0Right = 0;
  293. CRYP_KeyInitStruct->CRYP_Key1Left = 0;
  294. CRYP_KeyInitStruct->CRYP_Key1Right = 0;
  295. CRYP_KeyInitStruct->CRYP_Key2Left = 0;
  296. CRYP_KeyInitStruct->CRYP_Key2Right = 0;
  297. CRYP_KeyInitStruct->CRYP_Key3Left = 0;
  298. CRYP_KeyInitStruct->CRYP_Key3Right = 0;
  299. }
  300. /**
  301. * @brief Initializes the CRYP Initialization Vectors(IV) according to the
  302. * specified parameters in the CRYP_IVInitStruct.
  303. * @param CRYP_IVInitStruct: pointer to a CRYP_IVInitTypeDef structure that contains
  304. * the configuration information for the CRYP Initialization Vectors(IV).
  305. * @retval None
  306. */
  307. void CRYP_IVInit(CRYP_IVInitTypeDef* CRYP_IVInitStruct)
  308. {
  309. CRYP->IV0LR = CRYP_IVInitStruct->CRYP_IV0Left;
  310. CRYP->IV0RR = CRYP_IVInitStruct->CRYP_IV0Right;
  311. CRYP->IV1LR = CRYP_IVInitStruct->CRYP_IV1Left;
  312. CRYP->IV1RR = CRYP_IVInitStruct->CRYP_IV1Right;
  313. }
  314. /**
  315. * @brief Fills each CRYP_IVInitStruct member with its default value.
  316. * @param CRYP_IVInitStruct: pointer to a CRYP_IVInitTypeDef Initialization
  317. * Vectors(IV) structure which will be initialized.
  318. * @retval None
  319. */
  320. void CRYP_IVStructInit(CRYP_IVInitTypeDef* CRYP_IVInitStruct)
  321. {
  322. CRYP_IVInitStruct->CRYP_IV0Left = 0;
  323. CRYP_IVInitStruct->CRYP_IV0Right = 0;
  324. CRYP_IVInitStruct->CRYP_IV1Left = 0;
  325. CRYP_IVInitStruct->CRYP_IV1Right = 0;
  326. }
  327. /**
  328. * @brief Flushes the IN and OUT FIFOs (that is read and write pointers of the
  329. * FIFOs are reset)
  330. * @note The FIFOs must be flushed only when BUSY flag is reset.
  331. * @param None
  332. * @retval None
  333. */
  334. void CRYP_FIFOFlush(void)
  335. {
  336. /* Reset the read and write pointers of the FIFOs */
  337. CRYP->CR |= CRYP_CR_FFLUSH;
  338. }
  339. /**
  340. * @brief Enables or disables the CRYP peripheral.
  341. * @param NewState: new state of the CRYP peripheral.
  342. * This parameter can be: ENABLE or DISABLE.
  343. * @retval None
  344. */
  345. void CRYP_Cmd(FunctionalState NewState)
  346. {
  347. /* Check the parameters */
  348. assert_param(IS_FUNCTIONAL_STATE(NewState));
  349. if (NewState != DISABLE)
  350. {
  351. /* Enable the Cryptographic processor */
  352. CRYP->CR |= CRYP_CR_CRYPEN;
  353. }
  354. else
  355. {
  356. /* Disable the Cryptographic processor */
  357. CRYP->CR &= ~CRYP_CR_CRYPEN;
  358. }
  359. }
  360. /**
  361. * @}
  362. */
  363. /** @defgroup CRYP_Group2 CRYP Data processing functions
  364. * @brief CRYP Data processing functions
  365. *
  366. @verbatim
  367. ===============================================================================
  368. CRYP Data processing functions
  369. ===============================================================================
  370. This section provides functions allowing the encryption and decryption
  371. operations:
  372. - Enter data to be treated in the IN FIFO : using CRYP_DataIn() function.
  373. - Get the data result from the OUT FIFO : using CRYP_DataOut() function.
  374. @endverbatim
  375. * @{
  376. */
  377. /**
  378. * @brief Writes data in the Data Input register (DIN).
  379. * @note After the DIN register has been read once or several times,
  380. * the FIFO must be flushed (using CRYP_FIFOFlush() function).
  381. * @param Data: data to write in Data Input register
  382. * @retval None
  383. */
  384. void CRYP_DataIn(uint32_t Data)
  385. {
  386. CRYP->DR = Data;
  387. }
  388. /**
  389. * @brief Returns the last data entered into the output FIFO.
  390. * @param None
  391. * @retval Last data entered into the output FIFO.
  392. */
  393. uint32_t CRYP_DataOut(void)
  394. {
  395. return CRYP->DOUT;
  396. }
  397. /**
  398. * @}
  399. */
  400. /** @defgroup CRYP_Group3 Context swapping functions
  401. * @brief Context swapping functions
  402. *
  403. @verbatim
  404. ===============================================================================
  405. Context swapping functions
  406. ===============================================================================
  407. This section provides functions allowing to save and store CRYP Context
  408. It is possible to interrupt an encryption/ decryption/ key generation process
  409. to perform another processing with a higher priority, and to complete the
  410. interrupted process later on, when the higher-priority task is complete. To do
  411. so, the context of the interrupted task must be saved from the CRYP registers
  412. to memory, and then be restored from memory to the CRYP registers.
  413. 1. To save the current context, use CRYP_SaveContext() function
  414. 2. To restore the saved context, use CRYP_RestoreContext() function
  415. @endverbatim
  416. * @{
  417. */
  418. /**
  419. * @brief Saves the CRYP peripheral Context.
  420. * @note This function stops DMA transfer before to save the context. After
  421. * restoring the context, you have to enable the DMA again (if the DMA
  422. * was previously used).
  423. * @param CRYP_ContextSave: pointer to a CRYP_Context structure that contains
  424. * the repository for current context.
  425. * @param CRYP_KeyInitStruct: pointer to a CRYP_KeyInitTypeDef structure that
  426. * contains the configuration information for the CRYP Keys.
  427. * @retval None
  428. */
  429. ErrorStatus CRYP_SaveContext(CRYP_Context* CRYP_ContextSave,
  430. CRYP_KeyInitTypeDef* CRYP_KeyInitStruct)
  431. {
  432. __IO uint32_t timeout = 0;
  433. uint32_t ckeckmask = 0, bitstatus;
  434. ErrorStatus status = ERROR;
  435. /* Stop DMA transfers on the IN FIFO by clearing the DIEN bit in the CRYP_DMACR */
  436. CRYP->DMACR &= ~(uint32_t)CRYP_DMACR_DIEN;
  437. /* Wait until both the IN and OUT FIFOs are empty
  438. (IFEM=1 and OFNE=0 in the CRYP_SR register) and the
  439. BUSY bit is cleared. */
  440. if ((CRYP->CR & (uint32_t)(CRYP_CR_ALGOMODE_TDES_ECB | CRYP_CR_ALGOMODE_TDES_CBC)) != (uint32_t)0 )/* TDES */
  441. {
  442. ckeckmask = CRYP_SR_IFEM | CRYP_SR_BUSY ;
  443. }
  444. else /* AES or DES */
  445. {
  446. ckeckmask = CRYP_SR_IFEM | CRYP_SR_BUSY | CRYP_SR_OFNE;
  447. }
  448. do
  449. {
  450. bitstatus = CRYP->SR & ckeckmask;
  451. timeout++;
  452. }
  453. while ((timeout != MAX_TIMEOUT) && (bitstatus != CRYP_SR_IFEM));
  454. if ((CRYP->SR & ckeckmask) != CRYP_SR_IFEM)
  455. {
  456. status = ERROR;
  457. }
  458. else
  459. {
  460. /* Stop DMA transfers on the OUT FIFO by
  461. - writing the DOEN bit to 0 in the CRYP_DMACR register
  462. - and clear the CRYPEN bit. */
  463. CRYP->DMACR &= ~(uint32_t)CRYP_DMACR_DOEN;
  464. CRYP->CR &= ~(uint32_t)CRYP_CR_CRYPEN;
  465. /* Save the current configuration (bits [9:2] in the CRYP_CR register) */
  466. CRYP_ContextSave->CR_bits9to2 = CRYP->CR & (CRYP_CR_KEYSIZE |
  467. CRYP_CR_DATATYPE |
  468. CRYP_CR_ALGOMODE |
  469. CRYP_CR_ALGODIR);
  470. /* and, if not in ECB mode, the initialization vectors. */
  471. CRYP_ContextSave->CRYP_IV0LR = CRYP->IV0LR;
  472. CRYP_ContextSave->CRYP_IV0RR = CRYP->IV0RR;
  473. CRYP_ContextSave->CRYP_IV1LR = CRYP->IV1LR;
  474. CRYP_ContextSave->CRYP_IV1RR = CRYP->IV1RR;
  475. /* save The key value */
  476. CRYP_ContextSave->CRYP_K0LR = CRYP_KeyInitStruct->CRYP_Key0Left;
  477. CRYP_ContextSave->CRYP_K0RR = CRYP_KeyInitStruct->CRYP_Key0Right;
  478. CRYP_ContextSave->CRYP_K1LR = CRYP_KeyInitStruct->CRYP_Key1Left;
  479. CRYP_ContextSave->CRYP_K1RR = CRYP_KeyInitStruct->CRYP_Key1Right;
  480. CRYP_ContextSave->CRYP_K2LR = CRYP_KeyInitStruct->CRYP_Key2Left;
  481. CRYP_ContextSave->CRYP_K2RR = CRYP_KeyInitStruct->CRYP_Key2Right;
  482. CRYP_ContextSave->CRYP_K3LR = CRYP_KeyInitStruct->CRYP_Key3Left;
  483. CRYP_ContextSave->CRYP_K3RR = CRYP_KeyInitStruct->CRYP_Key3Right;
  484. /* When needed, save the DMA status (pointers for IN and OUT messages,
  485. number of remaining bytes, etc.) */
  486. status = SUCCESS;
  487. }
  488. return status;
  489. }
  490. /**
  491. * @brief Restores the CRYP peripheral Context.
  492. * @note Since teh DMA transfer is stopped in CRYP_SaveContext() function,
  493. * after restoring the context, you have to enable the DMA again (if the
  494. * DMA was previously used).
  495. * @param CRYP_ContextRestore: pointer to a CRYP_Context structure that contains
  496. * the repository for saved context.
  497. * @note The data that were saved during context saving must be rewrited into
  498. * the IN FIFO.
  499. * @retval None
  500. */
  501. void CRYP_RestoreContext(CRYP_Context* CRYP_ContextRestore)
  502. {
  503. /* Configure the processor with the saved configuration */
  504. CRYP->CR = CRYP_ContextRestore->CR_bits9to2;
  505. /* restore The key value */
  506. CRYP->K0LR = CRYP_ContextRestore->CRYP_K0LR;
  507. CRYP->K0RR = CRYP_ContextRestore->CRYP_K0RR;
  508. CRYP->K1LR = CRYP_ContextRestore->CRYP_K1LR;
  509. CRYP->K1RR = CRYP_ContextRestore->CRYP_K1RR;
  510. CRYP->K2LR = CRYP_ContextRestore->CRYP_K2LR;
  511. CRYP->K2RR = CRYP_ContextRestore->CRYP_K2RR;
  512. CRYP->K3LR = CRYP_ContextRestore->CRYP_K3LR;
  513. CRYP->K3RR = CRYP_ContextRestore->CRYP_K3RR;
  514. /* and the initialization vectors. */
  515. CRYP->IV0LR = CRYP_ContextRestore->CRYP_IV0LR;
  516. CRYP->IV0RR = CRYP_ContextRestore->CRYP_IV0RR;
  517. CRYP->IV1LR = CRYP_ContextRestore->CRYP_IV1LR;
  518. CRYP->IV1RR = CRYP_ContextRestore->CRYP_IV1RR;
  519. /* Enable the cryptographic processor */
  520. CRYP->CR |= CRYP_CR_CRYPEN;
  521. }
  522. /**
  523. * @}
  524. */
  525. /** @defgroup CRYP_Group4 CRYP's DMA interface Configuration function
  526. * @brief CRYP's DMA interface Configuration function
  527. *
  528. @verbatim
  529. ===============================================================================
  530. CRYP's DMA interface Configuration function
  531. ===============================================================================
  532. This section provides functions allowing to configure the DMA interface for
  533. CRYP data input and output transfer.
  534. When the DMA mode is enabled (using the CRYP_DMACmd() function), data can be
  535. transferred:
  536. - From memory to the CRYP IN FIFO using the DMA peripheral by enabling
  537. the CRYP_DMAReq_DataIN request.
  538. - From the CRYP OUT FIFO to the memory using the DMA peripheral by enabling
  539. the CRYP_DMAReq_DataOUT request.
  540. @endverbatim
  541. * @{
  542. */
  543. /**
  544. * @brief Enables or disables the CRYP DMA interface.
  545. * @param CRYP_DMAReq: specifies the CRYP DMA transfer request to be enabled or disabled.
  546. * This parameter can be any combination of the following values:
  547. * @arg CRYP_DMAReq_DataOUT: DMA for outgoing(Tx) data transfer
  548. * @arg CRYP_DMAReq_DataIN: DMA for incoming(Rx) data transfer
  549. * @param NewState: new state of the selected CRYP DMA transfer request.
  550. * This parameter can be: ENABLE or DISABLE.
  551. * @retval None
  552. */
  553. void CRYP_DMACmd(uint8_t CRYP_DMAReq, FunctionalState NewState)
  554. {
  555. /* Check the parameters */
  556. assert_param(IS_CRYP_DMAREQ(CRYP_DMAReq));
  557. assert_param(IS_FUNCTIONAL_STATE(NewState));
  558. if (NewState != DISABLE)
  559. {
  560. /* Enable the selected CRYP DMA request */
  561. CRYP->DMACR |= CRYP_DMAReq;
  562. }
  563. else
  564. {
  565. /* Disable the selected CRYP DMA request */
  566. CRYP->DMACR &= (uint8_t)~CRYP_DMAReq;
  567. }
  568. }
  569. /**
  570. * @}
  571. */
  572. /** @defgroup CRYP_Group5 Interrupts and flags management functions
  573. * @brief Interrupts and flags management functions
  574. *
  575. @verbatim
  576. ===============================================================================
  577. Interrupts and flags management functions
  578. ===============================================================================
  579. This section provides functions allowing to configure the CRYP Interrupts and
  580. to get the status and Interrupts pending bits.
  581. The CRYP provides 2 Interrupts sources and 7 Flags:
  582. Flags :
  583. -------
  584. 1. CRYP_FLAG_IFEM : Set when Input FIFO is empty.
  585. This Flag is cleared only by hardware.
  586. 2. CRYP_FLAG_IFNF : Set when Input FIFO is not full.
  587. This Flag is cleared only by hardware.
  588. 3. CRYP_FLAG_INRIS : Set when Input FIFO Raw interrupt is pending
  589. it gives the raw interrupt state prior to masking
  590. of the input FIFO service interrupt.
  591. This Flag is cleared only by hardware.
  592. 4. CRYP_FLAG_OFNE : Set when Output FIFO not empty.
  593. This Flag is cleared only by hardware.
  594. 5. CRYP_FLAG_OFFU : Set when Output FIFO is full.
  595. This Flag is cleared only by hardware.
  596. 6. CRYP_FLAG_OUTRIS : Set when Output FIFO Raw interrupt is pending
  597. it gives the raw interrupt state prior to masking
  598. of the output FIFO service interrupt.
  599. This Flag is cleared only by hardware.
  600. 7. CRYP_FLAG_BUSY : Set when the CRYP core is currently processing a
  601. block of data or a key preparation (for AES
  602. decryption).
  603. This Flag is cleared only by hardware.
  604. To clear it, the CRYP core must be disabled and the
  605. last processing has completed.
  606. Interrupts :
  607. ------------
  608. 1. CRYP_IT_INI : The input FIFO service interrupt is asserted when there
  609. are less than 4 words in the input FIFO.
  610. This interrupt is associated to CRYP_FLAG_INRIS flag.
  611. @note This interrupt is cleared by performing write operations
  612. to the input FIFO until it holds 4 or more words. The
  613. input FIFO service interrupt INMIS is enabled with the
  614. CRYP enable bit. Consequently, when CRYP is disabled, the
  615. INMIS signal is low even if the input FIFO is empty.
  616. 2. CRYP_IT_OUTI : The output FIFO service interrupt is asserted when there
  617. is one or more (32-bit word) data items in the output FIFO.
  618. This interrupt is associated to CRYP_FLAG_OUTRIS flag.
  619. @note This interrupt is cleared by reading data from the output
  620. FIFO until there is no valid (32-bit) word left (that is,
  621. the interrupt follows the state of the OFNE (output FIFO
  622. not empty) flag).
  623. Managing the CRYP controller events :
  624. ------------------------------------
  625. The user should identify which mode will be used in his application to manage
  626. the CRYP controller events: Polling mode or Interrupt mode.
  627. 1. In the Polling Mode it is advised to use the following functions:
  628. - CRYP_GetFlagStatus() : to check if flags events occur.
  629. @note The CRYPT flags do not need to be cleared since they are cleared as
  630. soon as the associated event are reset.
  631. 2. In the Interrupt Mode it is advised to use the following functions:
  632. - CRYP_ITConfig() : to enable or disable the interrupt source.
  633. - CRYP_GetITStatus() : to check if Interrupt occurs.
  634. @note The CRYPT interrupts have no pending bits, the interrupt is cleared as
  635. soon as the associated event is reset.
  636. @endverbatim
  637. * @{
  638. */
  639. /**
  640. * @brief Enables or disables the specified CRYP interrupts.
  641. * @param CRYP_IT: specifies the CRYP interrupt source to be enabled or disabled.
  642. * This parameter can be any combination of the following values:
  643. * @arg CRYP_IT_INI: Input FIFO interrupt
  644. * @arg CRYP_IT_OUTI: Output FIFO interrupt
  645. * @param NewState: new state of the specified CRYP interrupt.
  646. * This parameter can be: ENABLE or DISABLE.
  647. * @retval None
  648. */
  649. void CRYP_ITConfig(uint8_t CRYP_IT, FunctionalState NewState)
  650. {
  651. /* Check the parameters */
  652. assert_param(IS_CRYP_CONFIG_IT(CRYP_IT));
  653. assert_param(IS_FUNCTIONAL_STATE(NewState));
  654. if (NewState != DISABLE)
  655. {
  656. /* Enable the selected CRYP interrupt */
  657. CRYP->IMSCR |= CRYP_IT;
  658. }
  659. else
  660. {
  661. /* Disable the selected CRYP interrupt */
  662. CRYP->IMSCR &= (uint8_t)~CRYP_IT;
  663. }
  664. }
  665. /**
  666. * @brief Checks whether the specified CRYP interrupt has occurred or not.
  667. * @note This function checks the status of the masked interrupt (i.e the
  668. * interrupt should be previously enabled).
  669. * @param CRYP_IT: specifies the CRYP (masked) interrupt source to check.
  670. * This parameter can be one of the following values:
  671. * @arg CRYP_IT_INI: Input FIFO interrupt
  672. * @arg CRYP_IT_OUTI: Output FIFO interrupt
  673. * @retval The new state of CRYP_IT (SET or RESET).
  674. */
  675. ITStatus CRYP_GetITStatus(uint8_t CRYP_IT)
  676. {
  677. ITStatus bitstatus = RESET;
  678. /* Check the parameters */
  679. assert_param(IS_CRYP_GET_IT(CRYP_IT));
  680. /* Check the status of the specified CRYP interrupt */
  681. if ((CRYP->MISR & CRYP_IT) != (uint8_t)RESET)
  682. {
  683. /* CRYP_IT is set */
  684. bitstatus = SET;
  685. }
  686. else
  687. {
  688. /* CRYP_IT is reset */
  689. bitstatus = RESET;
  690. }
  691. /* Return the CRYP_IT status */
  692. return bitstatus;
  693. }
  694. /**
  695. * @brief Checks whether the specified CRYP flag is set or not.
  696. * @param CRYP_FLAG: specifies the CRYP flag to check.
  697. * This parameter can be one of the following values:
  698. * @arg CRYP_FLAG_IFEM: Input FIFO Empty flag.
  699. * @arg CRYP_FLAG_IFNF: Input FIFO Not Full flag.
  700. * @arg CRYP_FLAG_OFNE: Output FIFO Not Empty flag.
  701. * @arg CRYP_FLAG_OFFU: Output FIFO Full flag.
  702. * @arg CRYP_FLAG_BUSY: Busy flag.
  703. * @arg CRYP_FLAG_OUTRIS: Output FIFO raw interrupt flag.
  704. * @arg CRYP_FLAG_INRIS: Input FIFO raw interrupt flag.
  705. * @retval The new state of CRYP_FLAG (SET or RESET).
  706. */
  707. FlagStatus CRYP_GetFlagStatus(uint8_t CRYP_FLAG)
  708. {
  709. FlagStatus bitstatus = RESET;
  710. uint32_t tempreg = 0;
  711. /* Check the parameters */
  712. assert_param(IS_CRYP_GET_FLAG(CRYP_FLAG));
  713. /* check if the FLAG is in RISR register */
  714. if ((CRYP_FLAG & FLAG_MASK) != 0x00)
  715. {
  716. tempreg = CRYP->RISR;
  717. }
  718. else /* The FLAG is in SR register */
  719. {
  720. tempreg = CRYP->SR;
  721. }
  722. /* Check the status of the specified CRYP flag */
  723. if ((tempreg & CRYP_FLAG ) != (uint8_t)RESET)
  724. {
  725. /* CRYP_FLAG is set */
  726. bitstatus = SET;
  727. }
  728. else
  729. {
  730. /* CRYP_FLAG is reset */
  731. bitstatus = RESET;
  732. }
  733. /* Return the CRYP_FLAG status */
  734. return bitstatus;
  735. }
  736. /**
  737. * @}
  738. */
  739. /**
  740. * @}
  741. */
  742. /**
  743. * @}
  744. */
  745. /**
  746. * @}
  747. */
  748. /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/