stm32f4xx_cryp.c 33 KB

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