]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/cmsis/TARGET_STM/TARGET_STM32L0/stm32l0xx_hal_cryp.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / mbed / targets / cmsis / TARGET_STM / TARGET_STM32L0 / stm32l0xx_hal_cryp.c
1 /**
2   ******************************************************************************
3   * @file    stm32l0xx_hal_cryp.c
4   * @author  MCD Application Team
5   * @version V1.2.0
6   * @date    06-February-2015
7   * @brief   CRYP HAL module driver.
8   *    
9   *          This file provides firmware functions to manage the following 
10   *          functionalities of the Cryptography (CRYP) peripheral:
11   *           + Initialization and de-initialization functions
12   *           + Processing functions by algorithm using polling mode
13   *           + Processing functions by algorithm using interrupt mode
14   *           + Processing functions by algorithm using DMA mode
15   *           + Peripheral State functions
16   *         
17   @verbatim
18   ==============================================================================
19                      ##### How to use this driver #####
20   ==============================================================================
21     [..]
22       The CRYP HAL driver can be used as follows:
23
24       (#)Initialize the CRYP low level resources by implementing the HAL_CRYP_MspInit():
25          (##) Enable the CRYP interface clock using __HAL_RCC_AES_CLK_ENABLE()
26          (##) In case of using interrupts (e.g. HAL_CRYP_AESECB_Encrypt_IT())
27              (+) Configure the CRYP interrupt priority using HAL_NVIC_SetPriority()
28              (+) Enable the CRYP IRQ handler using HAL_NVIC_EnableIRQ()
29              (+) In CRYP IRQ handler, call HAL_CRYP_IRQHandler()
30          (##) In case of using DMA to control data transfer (e.g. HAL_CRYP_AESECB_Encrypt_DMA())
31              (+) Enable the DMA1 interface clock using 
32                  (++) __HAL_RCC_DMA1_CLK_ENABLE()
33              (+) Configure and enable two DMA Channels one for managing data transfer from
34                  memory to peripheral (input channel) and another channel for managing data
35                  transfer from peripheral to memory (output channel)
36              (+) Associate the initialized DMA handle to the CRYP DMA handle
37                  using  __HAL_LINKDMA()
38              (+) Configure the priority and enable the NVIC for the transfer complete
39                  interrupt on the two DMA Streams. The output stream should have higher
40                  priority than the input stream.
41                  (++) HAL_NVIC_SetPriority()
42                  (++) HAL_NVIC_EnableIRQ()
43     
44       (#)Initialize the CRYP HAL using HAL_CRYP_Init(). This function configures mainly:
45          (##) The data type: 1-bit, 8-bit, 16-bit and 32-bit
46          (##) The encryption/decryption key.
47          (##) The initialization vector (counter). It is not used ECB mode.
48     
49       (#)Three processing (encryption/decryption) functions are available:
50          (##) Polling mode: encryption and decryption APIs are blocking functions
51               i.e. they process the data and wait till the processing is finished
52               e.g. HAL_CRYP_AESCBC_Encrypt()
53          (##) Interrupt mode: encryption and decryption APIs are not blocking functions
54               i.e. they process the data under interrupt
55               e.g. HAL_CRYP_AESCBC_Encrypt_IT()
56          (##) DMA mode: encryption and decryption APIs are not blocking functions
57               i.e. the data transfer is ensured by DMA
58               e.g. HAL_CRYP_AESCBC_Encrypt_DMA()
59     
60       (#)When the processing function is called for the first time after HAL_CRYP_Init()
61          the CRYP peripheral is initialized and processes the buffer in input.
62          At second call, the processing function performs an append of the already
63          processed buffer.
64          When a new data block is to be processed, call HAL_CRYP_Init() then the
65          processing function.
66          
67       (#)Call HAL_CRYP_DeInit() to deinitialize the CRYP peripheral.
68
69   @endverbatim
70   ******************************************************************************
71   * @attention
72   *
73   * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
74   *
75   * Redistribution and use in source and binary forms, with or without modification,
76   * are permitted provided that the following conditions are met:
77   *   1. Redistributions of source code must retain the above copyright notice,
78   *      this list of conditions and the following disclaimer.
79   *   2. Redistributions in binary form must reproduce the above copyright notice,
80   *      this list of conditions and the following disclaimer in the documentation
81   *      and/or other materials provided with the distribution.
82   *   3. Neither the name of STMicroelectronics nor the names of its contributors
83   *      may be used to endorse or promote products derived from this software
84   *      without specific prior written permission.
85   *
86   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
87   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
88   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
89   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
90   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
91   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
92   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
93   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
94   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
95   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
96   *
97   ******************************************************************************  
98   */ 
99
100 #if defined (STM32L041xx) || defined (STM32L061xx) || defined (STM32L062xx) || defined (STM32L063xx) || (STM32L081xx) || defined (STM32L082xx) || defined (STM32L083xx)
101 /* Includes ------------------------------------------------------------------*/
102 #include "stm32l0xx_hal.h"
103
104 #ifdef HAL_CRYP_MODULE_ENABLED
105 /** @addtogroup STM32L0xx_HAL_Driver
106   * @{
107   */
108
109 /** @defgroup CRYP CRYP
110   * @brief CRYP HAL module driver.
111   * @{
112   */
113
114
115 /* Private typedef -----------------------------------------------------------*/
116 /* Private define ------------------------------------------------------------*/
117
118 /** @defgroup CRYP_Private_Defines CRYP Private Defines
119   * @{
120   */
121
122 #define  CRYP_ALGO_CHAIN_MASK         (AES_CR_MODE | AES_CR_CHMOD)
123
124 /**
125   * @}
126   */
127
128 /* Private macro -------------------------------------------------------------*/
129 /* Private variables ---------------------------------------------------------*/
130 /* Private function prototypes -----------------------------------------------*/
131
132 /** @defgroup CRYP_Private_Functions CRYP Private Functions
133   * @{
134   */
135
136 static HAL_StatusTypeDef  CRYP_EncryptDecrypt_IT(CRYP_HandleTypeDef *hcryp);
137 static void               CRYP_SetInitVector(CRYP_HandleTypeDef *hcryp, uint8_t *InitVector);
138 static void               CRYP_SetKey(CRYP_HandleTypeDef *hcryp, uint8_t *Key);
139 static HAL_StatusTypeDef  CRYP_ProcessData(CRYP_HandleTypeDef *hcryp, uint8_t* Input, uint16_t Ilength, uint8_t* Output, uint32_t Timeout);
140 static void               CRYP_DMAInCplt(DMA_HandleTypeDef *hdma);
141 static void               CRYP_DMAOutCplt(DMA_HandleTypeDef *hdma);
142 static void               CRYP_DMAError(DMA_HandleTypeDef *hdma);
143 static void               CRYP_SetDMAConfig(CRYP_HandleTypeDef *hcryp, uint32_t inputaddr, uint16_t Size, uint32_t outputaddr);
144
145 /**
146   * @}
147   */
148
149 /* Private functions ---------------------------------------------------------*/
150
151 /** @defgroup CRYP_Exported_Functions CRYP Exported Functions
152   * @{
153   */
154
155 /** @defgroup CRYP_Exported_Functions_Group1 Initialization and de-initialization functions 
156  *  @brief    Initialization and Configuration functions. 
157  *
158 @verbatim    
159   ==============================================================================
160               ##### Initialization and de-initialization functions #####
161   ==============================================================================
162     [..]  This section provides functions allowing to:
163       (+) Initialize the CRYP according to the specified parameters 
164           in the CRYP_InitTypeDef and creates the associated handle
165       (+) DeInitialize the CRYP peripheral
166       (+) Initialize the CRYP MSP
167       (+) DeInitialize CRYP MSP 
168  
169 @endverbatim
170   * @{
171   */
172
173 /**
174   * @brief  Initializes the CRYP according to the specified
175   *         parameters in the CRYP_InitTypeDef and creates the associated handle.
176   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
177   *         the configuration information for CRYP module
178   * @retval HAL status
179   */
180 HAL_StatusTypeDef HAL_CRYP_Init(CRYP_HandleTypeDef *hcryp)
181
182   /* Check the CRYP handle allocation */
183   if(hcryp == NULL)
184   {
185     return HAL_ERROR;
186   }
187   
188   /* Check the parameters */
189   assert_param(IS_AES_ALL_INSTANCE(hcryp->Instance));
190   assert_param(IS_CRYP_DATATYPE(hcryp->Init.DataType));
191   
192   if(hcryp->State == HAL_CRYP_STATE_RESET)
193   {
194     /* Allocate lock resource and initialize it */
195     hcryp->Lock = HAL_UNLOCKED;
196
197     /* Init the low level hardware */
198     HAL_CRYP_MspInit(hcryp);
199   }
200   
201   /* Check if AES already enabled */
202   if (HAL_IS_BIT_CLR(hcryp->Instance->CR, AES_CR_EN))
203   {
204     /* Change the CRYP state */
205     hcryp->State = HAL_CRYP_STATE_BUSY;  
206
207     /* Set the data type*/
208     MODIFY_REG(hcryp->Instance->CR, AES_CR_DATATYPE, hcryp->Init.DataType);
209     
210     /* Reset CrypInCount and CrypOutCount */
211     hcryp->CrypInCount = 0;
212     hcryp->CrypOutCount = 0;
213     
214     /* Change the CRYP state */
215     hcryp->State = HAL_CRYP_STATE_READY;
216     
217     /* Set the default CRYP phase */
218     hcryp->Phase = HAL_CRYP_PHASE_READY;
219     
220     /* Return function status */
221     return HAL_OK;
222   }
223   else
224   {
225     /* The Datatype selection must be changed if the AES is disabled. Writing these bits while the AES is */
226     /* enabled is forbidden to avoid unpredictable AES behavior.*/
227
228     /* Return function status */
229     return HAL_ERROR;
230   }
231
232 }
233
234 /**
235   * @brief  DeInitializes the CRYP peripheral. 
236   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
237   *         the configuration information for CRYP module
238   * @retval HAL status
239   */
240 HAL_StatusTypeDef HAL_CRYP_DeInit(CRYP_HandleTypeDef *hcryp)
241 {
242   /* Check the CRYP handle allocation */
243   if(hcryp == NULL)
244   {
245     return HAL_ERROR;
246   }
247   
248   /* Change the CRYP state */
249   hcryp->State = HAL_CRYP_STATE_BUSY;
250   
251   /* Set the default CRYP phase */
252   hcryp->Phase = HAL_CRYP_PHASE_READY;
253   
254   /* Reset CrypInCount and CrypOutCount */
255   hcryp->CrypInCount = 0;
256   hcryp->CrypOutCount = 0;
257   
258   /* Disable the CRYP Peripheral Clock */
259   __HAL_CRYP_DISABLE(hcryp);
260   
261   /* DeInit the low level hardware: CLOCK, NVIC.*/
262   HAL_CRYP_MspDeInit(hcryp);
263   
264   /* Change the CRYP state */
265   hcryp->State = HAL_CRYP_STATE_RESET;
266   
267   /* Release Lock */
268   __HAL_UNLOCK(hcryp);
269   
270   /* Return function status */
271   return HAL_OK;
272 }
273
274 /**
275   * @brief  Initializes the CRYP MSP.
276   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
277   *         the configuration information for CRYP module
278   * @retval None
279   */
280 __weak void HAL_CRYP_MspInit(CRYP_HandleTypeDef *hcryp)
281 {
282   /* NOTE : This function should not be modified; when the callback is needed, 
283             the HAL_CRYP_MspInit can be implemented in the user file */
284 }
285
286 /**
287   * @brief  DeInitializes CRYP MSP.
288   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
289   *         the configuration information for CRYP module
290   * @retval None
291   */
292 __weak void HAL_CRYP_MspDeInit(CRYP_HandleTypeDef *hcryp)
293 {
294   /* NOTE : This function should not be modified; when the callback is needed, 
295             the HAL_CRYP_MspDeInit can be implemented in the user file */
296 }
297
298 /**
299   * @}
300   */
301
302 /** @defgroup CRYP_Exported_Functions_Group2 AES processing functions 
303  *  @brief   processing functions. 
304  *
305 @verbatim   
306   ==============================================================================
307                       ##### AES processing functions #####
308   ==============================================================================  
309     [..]  This section provides functions allowing to:
310       (+) Encrypt plaintext using AES algorithm in different chaining modes
311       (+) Decrypt cyphertext using AES algorithm in different chaining modes
312     [..]  Three processing functions are available:
313       (+) Polling mode
314       (+) Interrupt mode
315       (+) DMA mode
316
317 @endverbatim
318   * @{
319   */
320
321 /**
322   * @brief  Initializes the CRYP peripheral in AES ECB encryption mode
323   *         then encrypt pPlainData. The cypher data are available in pCypherData
324   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
325   *         the configuration information for CRYP module
326   * @param  pPlainData: Pointer to the plaintext buffer (aligned on u32)
327   * @param  Size: Length of the plaintext buffer, must be a multiple of 16.
328   * @param  pCypherData: Pointer to the cyphertext buffer (aligned on u32)
329   * @param  Timeout: Specify Timeout value 
330   * @retval HAL status
331   */
332 HAL_StatusTypeDef HAL_CRYP_AESECB_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout)
333 {
334   /* Process Locked */
335   __HAL_LOCK(hcryp);
336
337   /* Check that data aligned on u32 and Size multiple of 16*/
338   if((((uint32_t)pPlainData & (uint32_t)0x00000003) != 0) || (((uint32_t)pCypherData & (uint32_t)0x00000003) != 0) || ((Size & (uint16_t)0x000F) != 0))
339   {
340     /* Process Locked */
341     __HAL_UNLOCK(hcryp);
342
343     /* Return function status */
344     return HAL_ERROR;
345   }
346   
347   /* Check if HAL_CRYP_Init has been called */
348   if(hcryp->State != HAL_CRYP_STATE_RESET)
349   {
350     /* Change the CRYP state */
351     hcryp->State = HAL_CRYP_STATE_BUSY;
352     
353     /* Check if initialization phase has already been performed */
354     if(hcryp->Phase == HAL_CRYP_PHASE_READY)
355     {
356       /* Set the key */
357       CRYP_SetKey(hcryp, hcryp->Init.pKey);
358       
359       /* Reset the CHMOD & MODE bits */
360       CLEAR_BIT(hcryp->Instance->CR, CRYP_ALGO_CHAIN_MASK);
361       
362       /* Set the CRYP peripheral in AES ECB mode */
363       __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_ECB_ENCRYPT);
364       
365       /* Enable CRYP */
366       __HAL_CRYP_ENABLE(hcryp);
367       
368       /* Set the phase */
369       hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
370     }
371     
372     /* Write Plain Data and Get Cypher Data */
373     if(CRYP_ProcessData(hcryp, pPlainData, Size, pCypherData, Timeout) != HAL_OK)
374     {
375       return HAL_TIMEOUT;
376     }
377     
378     /* Change the CRYP state */
379     hcryp->State = HAL_CRYP_STATE_READY;
380     
381     /* Process Unlocked */
382     __HAL_UNLOCK(hcryp);
383     
384     /* Return function status */
385     return HAL_OK;
386   }
387   else
388   {
389     /* Process Locked */
390     __HAL_UNLOCK(hcryp);
391         
392     /* Return function status */
393     return HAL_ERROR;
394   }
395 }
396
397 /**
398   * @brief  Initializes the CRYP peripheral in AES CBC encryption mode
399   *         then encrypt pPlainData. The cypher data are available in pCypherData
400   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
401   *         the configuration information for CRYP module
402   * @param  pPlainData: Pointer to the plaintext buffer (aligned on u32)
403   * @param  Size: Length of the plaintext buffer, must be a multiple of 16.
404   * @param  pCypherData: Pointer to the cyphertext buffer (aligned on u32)
405   * @param  Timeout: Specify Timeout value  
406   * @retval HAL status
407   */
408 HAL_StatusTypeDef HAL_CRYP_AESCBC_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout)
409 {
410   /* Process Locked */
411   __HAL_LOCK(hcryp);
412   
413   /* Check that data aligned on u32 */
414   if((((uint32_t)pPlainData & (uint32_t)0x00000003) != 0) || (((uint32_t)pCypherData & (uint32_t)0x00000003) != 0) || ((Size & (uint16_t)0x000F) != 0))
415   {
416     /* Process Locked */
417     __HAL_UNLOCK(hcryp);
418
419     /* Return function status */
420     return HAL_ERROR;
421   }
422   
423   /* Check if HAL_CRYP_Init has been called */
424   if(hcryp->State != HAL_CRYP_STATE_RESET)
425   {
426     /* Change the CRYP state */
427     hcryp->State = HAL_CRYP_STATE_BUSY;
428     
429     /* Check if initialization phase has already been performed */
430     if(hcryp->Phase == HAL_CRYP_PHASE_READY)
431     {
432       /* Set the key */
433       CRYP_SetKey(hcryp, hcryp->Init.pKey);
434       
435       /* Reset the CHMOD & MODE bits */
436       CLEAR_BIT(hcryp->Instance->CR, CRYP_ALGO_CHAIN_MASK);
437       
438       /* Set the CRYP peripheral in AES CBC mode */
439       __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CBC_ENCRYPT);
440       
441       /* Set the Initialization Vector */
442       CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect);
443       
444       /* Enable CRYP */
445       __HAL_CRYP_ENABLE(hcryp);
446       
447       /* Set the phase */
448       hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
449     }
450     
451     /* Write Plain Data and Get Cypher Data */
452     if(CRYP_ProcessData(hcryp, pPlainData, Size, pCypherData, Timeout) != HAL_OK)
453     {
454       return HAL_TIMEOUT;
455     }
456     
457     /* Change the CRYP state */
458     hcryp->State = HAL_CRYP_STATE_READY;
459     
460     /* Process Unlocked */
461     __HAL_UNLOCK(hcryp);
462     
463     /* Return function status */
464     return HAL_OK;
465   }
466   else
467   {
468     /* Process Locked */
469     __HAL_UNLOCK(hcryp);
470
471     /* Return function status */
472     return HAL_ERROR;
473   }
474 }
475
476 /**
477   * @brief  Initializes the CRYP peripheral in AES CTR encryption mode
478   *         then encrypt pPlainData. The cypher data are available in pCypherData
479   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
480   *         the configuration information for CRYP module
481   * @param  pPlainData: Pointer to the plaintext buffer (aligned on u32)
482   * @param  Size: Length of the plaintext buffer, must be a multiple of 16.
483   * @param  pCypherData: Pointer to the cyphertext buffer (aligned on u32)
484   * @param  Timeout: Specify Timeout value  
485   * @retval HAL status
486   */
487 HAL_StatusTypeDef HAL_CRYP_AESCTR_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout)
488 {  
489   /* Process Locked */
490   __HAL_LOCK(hcryp);
491   
492   /* Check that data aligned on u32 */
493   if((((uint32_t)pPlainData & (uint32_t)0x00000003) != 0) || (((uint32_t)pCypherData & (uint32_t)0x00000003) != 0) || ((Size & (uint16_t)0x000F) != 0))
494   {
495     /* Process Locked */
496     __HAL_UNLOCK(hcryp);
497
498     /* Return function status */
499     return HAL_ERROR;
500   }
501   
502   /* Check if HAL_CRYP_Init has been called */
503   if(hcryp->State != HAL_CRYP_STATE_RESET)
504   {
505     /* Change the CRYP state */
506     hcryp->State = HAL_CRYP_STATE_BUSY;
507     
508     /* Check if initialization phase has already been performed */
509     if(hcryp->Phase == HAL_CRYP_PHASE_READY)
510     {
511       /* Set the key */
512       CRYP_SetKey(hcryp, hcryp->Init.pKey);
513       
514       /* Reset the CHMOD & MODE bits */
515       CLEAR_BIT(hcryp->Instance->CR, CRYP_ALGO_CHAIN_MASK);
516       
517       /* Set the CRYP peripheral in AES CTR mode */
518       __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CTR_ENCRYPT);
519       
520       /* Set the Initialization Vector */
521       CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect);
522       
523       /* Enable CRYP */
524       __HAL_CRYP_ENABLE(hcryp);
525       
526       /* Set the phase */
527       hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
528     }
529     
530     /* Write Plain Data and Get Cypher Data */
531     if(CRYP_ProcessData(hcryp, pPlainData, Size, pCypherData, Timeout) != HAL_OK)
532     {
533       return HAL_TIMEOUT;
534     }
535     
536     /* Change the CRYP state */
537     hcryp->State = HAL_CRYP_STATE_READY;
538     
539     /* Process Unlocked */
540     __HAL_UNLOCK(hcryp);
541     
542     /* Return function status */
543     return HAL_OK;
544   }
545   else
546   {
547     /* Release Lock */
548     __HAL_UNLOCK(hcryp);
549   
550     /* Return function status */
551     return HAL_ERROR;
552   }
553 }
554
555 /**
556   * @brief  Initializes the CRYP peripheral in AES ECB decryption mode
557   *         then decrypted pCypherData. The cypher data are available in pPlainData
558   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
559   *         the configuration information for CRYP module
560   * @param  pCypherData: Pointer to the cyphertext buffer (aligned on u32)
561   * @param  Size: Length of the plaintext buffer, must be a multiple of 16.
562   * @param  pPlainData: Pointer to the plaintext buffer (aligned on u32)
563   * @param  Timeout: Specify Timeout value  
564   * @retval HAL status
565   */
566 HAL_StatusTypeDef HAL_CRYP_AESECB_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout)
567 {
568   /* Process Locked */
569   __HAL_LOCK(hcryp);
570   
571   /* Check that data aligned on u32 */
572   if((((uint32_t)pPlainData & (uint32_t)0x00000003) != 0) || (((uint32_t)pCypherData & (uint32_t)0x00000003) != 0) || ((Size & (uint16_t)0x000F) != 0))
573   {
574     /* Process Locked */
575     __HAL_UNLOCK(hcryp);
576
577     /* Return function status */
578     return HAL_ERROR;
579   }
580   
581   /* Check if HAL_CRYP_Init has been called */
582   if(hcryp->State != HAL_CRYP_STATE_RESET)
583   {
584     /* Change the CRYP state */
585     hcryp->State = HAL_CRYP_STATE_BUSY;
586     
587     /* Check if initialization phase has already been performed */
588     if(hcryp->Phase == HAL_CRYP_PHASE_READY)
589     {
590       /* Set the key */
591       CRYP_SetKey(hcryp, hcryp->Init.pKey);
592       
593       /* Reset the CHMOD & MODE bits */
594       CLEAR_BIT(hcryp->Instance->CR, CRYP_ALGO_CHAIN_MASK);
595       
596       /* Set the CRYP peripheral in AES ECB decryption mode (with key derivation) */
597       __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_ECB_KEYDERDECRYPT);
598       
599       /* Enable CRYP */
600       __HAL_CRYP_ENABLE(hcryp);
601       
602       /* Set the phase */
603       hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
604     }
605     
606     /* Write Cypher Data and Get Plain Data */
607     if(CRYP_ProcessData(hcryp, pCypherData, Size, pPlainData, Timeout) != HAL_OK)
608     {
609       return HAL_TIMEOUT;
610     }
611     
612     /* Change the CRYP state */
613     hcryp->State = HAL_CRYP_STATE_READY;
614     
615     /* Process Unlocked */
616     __HAL_UNLOCK(hcryp);
617     
618     /* Return function status */
619     return HAL_OK;
620   }
621   else
622   {
623     /* Release Lock */
624     __HAL_UNLOCK(hcryp);
625   
626     /* Return function status */
627     return HAL_ERROR;
628   }
629 }
630
631 /**
632   * @brief  Initializes the CRYP peripheral in AES ECB decryption mode
633   *         then decrypted pCypherData. The cypher data are available in pPlainData
634   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
635   *         the configuration information for CRYP module
636   * @param  pCypherData: Pointer to the cyphertext buffer (aligned on u32)
637   * @param  Size: Length of the plaintext buffer, must be a multiple of 16.
638   * @param  pPlainData: Pointer to the plaintext buffer (aligned on u32)
639   * @param  Timeout: Specify Timeout value  
640   * @retval HAL status
641   */
642 HAL_StatusTypeDef HAL_CRYP_AESCBC_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout)
643 {
644   /* Process Locked */
645   __HAL_LOCK(hcryp);
646   
647   /* Check that data aligned on u32 */
648   if((((uint32_t)pPlainData & (uint32_t)0x00000003) != 0) || (((uint32_t)pCypherData & (uint32_t)0x00000003) != 0) || ((Size & (uint16_t)0x000F) != 0))
649   {
650     /* Process Locked */
651     __HAL_UNLOCK(hcryp);
652
653     /* Return function status */
654     return HAL_ERROR;
655   }
656   
657   /* Check if HAL_CRYP_Init has been called */
658   if(hcryp->State != HAL_CRYP_STATE_RESET)
659   {
660     /* Change the CRYP state */
661     hcryp->State = HAL_CRYP_STATE_BUSY;
662     
663     /* Check if initialization phase has already been performed */
664     if(hcryp->Phase == HAL_CRYP_PHASE_READY)
665     {
666       /* Set the key */
667       CRYP_SetKey(hcryp, hcryp->Init.pKey);
668       
669       /* Reset the CHMOD & MODE bits */
670       CLEAR_BIT(hcryp->Instance->CR, CRYP_ALGO_CHAIN_MASK);
671       
672       /* Set the CRYP peripheral in AES CBC decryption mode (with key derivation) */
673       __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CBC_KEYDERDECRYPT);
674       
675       /* Set the Initialization Vector */
676       CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect);
677       
678       /* Enable CRYP */
679       __HAL_CRYP_ENABLE(hcryp);
680       
681       /* Set the phase */
682       hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
683     }
684     
685     /* Write Cypher Data and Get Plain Data */
686     if(CRYP_ProcessData(hcryp, pCypherData, Size, pPlainData, Timeout) != HAL_OK)
687     {
688       return HAL_TIMEOUT;
689     }
690     
691     /* Change the CRYP state */
692     hcryp->State = HAL_CRYP_STATE_READY;
693     
694     /* Process Unlocked */
695     __HAL_UNLOCK(hcryp);
696     
697     /* Return function status */
698     return HAL_OK;
699   }
700   else
701   {
702     /* Release Lock */
703     __HAL_UNLOCK(hcryp);
704   
705     /* Return function status */
706     return HAL_ERROR;
707   }
708 }
709
710 /**
711   * @brief  Initializes the CRYP peripheral in AES CTR decryption mode
712   *         then decrypted pCypherData. The cypher data are available in pPlainData
713   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
714   *         the configuration information for CRYP module
715   * @param  pCypherData: Pointer to the cyphertext buffer (aligned on u32)
716   * @param  Size: Length of the plaintext buffer, must be a multiple of 16.
717   * @param  pPlainData: Pointer to the plaintext buffer (aligned on u32)
718   * @param  Timeout: Specify Timeout value   
719   * @retval HAL status
720   */
721 HAL_StatusTypeDef HAL_CRYP_AESCTR_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout)
722 {  
723   /* Process Locked */
724   __HAL_LOCK(hcryp);
725   
726   /* Check that data aligned on u32 */
727   if((((uint32_t)pPlainData & (uint32_t)0x00000003) != 0) || (((uint32_t)pCypherData & (uint32_t)0x00000003) != 0) || ((Size & (uint16_t)0x000F) != 0))
728   {
729     /* Process Locked */
730     __HAL_UNLOCK(hcryp);
731
732     /* Return function status */
733     return HAL_ERROR;
734   }
735   
736   /* Check if initialization phase has already been performed */
737   if ((hcryp->State != HAL_CRYP_STATE_RESET) && (hcryp->Phase == HAL_CRYP_PHASE_READY))
738   {
739     /* Change the CRYP state */
740     hcryp->State = HAL_CRYP_STATE_BUSY;
741     
742     /* Set the key */
743     CRYP_SetKey(hcryp, hcryp->Init.pKey);
744     
745     /* Reset the CHMOD & MODE bits */
746     CLEAR_BIT(hcryp->Instance->CR, CRYP_ALGO_CHAIN_MASK);
747     
748     /* Set the CRYP peripheral in AES CTR decryption mode */
749     __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CTR_DECRYPT);
750     
751     /* Set the Initialization Vector */
752     CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect);
753     
754     /* Enable CRYP */
755     __HAL_CRYP_ENABLE(hcryp);
756     
757     /* Set the phase */
758     hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
759   }
760   
761   /* Write Cypher Data and Get Plain Data */
762   if(CRYP_ProcessData(hcryp, pCypherData, Size, pPlainData, Timeout) != HAL_OK)
763   {
764     return HAL_TIMEOUT;
765   }
766   
767   /* Change the CRYP state */
768   hcryp->State = HAL_CRYP_STATE_READY;
769   
770   /* Process Unlocked */
771   __HAL_UNLOCK(hcryp);
772   
773   /* Return function status */
774   return HAL_OK;
775 }
776
777 /**
778   * @brief  Initializes the CRYP peripheral in AES ECB encryption mode using Interrupt.
779   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
780   *         the configuration information for CRYP module
781   * @param  pPlainData: Pointer to the plaintext buffer (aligned on u32)
782   * @param  Size: Length of the plaintext buffer, must be a multiple of 16 bytes
783   * @param  pCypherData: Pointer to the cyphertext buffer (aligned on u32)
784   * @retval HAL status
785   */
786 HAL_StatusTypeDef HAL_CRYP_AESECB_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
787 {
788   uint32_t inputaddr = 0;
789   
790   /* Check that data aligned on u32 */
791   if((((uint32_t)pPlainData & (uint32_t)0x00000003) != 0) || (((uint32_t)pCypherData & (uint32_t)0x00000003) != 0) || ((Size & (uint16_t)0x000F) != 0))
792   {
793     /* Process Locked */
794     __HAL_UNLOCK(hcryp);
795
796     /* Return function status */
797     return HAL_ERROR;
798   }
799   
800   if ((hcryp->State != HAL_CRYP_STATE_RESET) && (hcryp->State == HAL_CRYP_STATE_READY))
801   {
802     /* Process Locked */
803     __HAL_LOCK(hcryp);
804     
805     /* Get the buffer addresses and sizes */
806     hcryp->CrypInCount = Size;
807     hcryp->pCrypInBuffPtr = pPlainData;
808     hcryp->pCrypOutBuffPtr = pCypherData;
809     hcryp->CrypOutCount = Size;
810     
811     /* Change the CRYP state */
812     hcryp->State = HAL_CRYP_STATE_BUSY;
813     
814     /* Check if initialization phase has already been performed */
815     if(hcryp->Phase == HAL_CRYP_PHASE_READY)
816     {
817       /* Set the key */
818       CRYP_SetKey(hcryp, hcryp->Init.pKey);
819       
820       /* Reset the CHMOD & MODE bits */
821       CLEAR_BIT(hcryp->Instance->CR, CRYP_ALGO_CHAIN_MASK);
822       
823       /* Set the CRYP peripheral in AES ECB mode */
824       __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_ECB_ENCRYPT);
825       
826       /* Set the phase */
827       hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
828     }
829     
830     /* Enable Interrupts */
831     __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_CC);
832     
833     /* Enable CRYP */
834     __HAL_CRYP_ENABLE(hcryp);
835     
836     /* Get the last input data adress */
837     inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
838     
839     /* Write the Input block in the Data Input register */
840     hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
841     inputaddr+=4;
842     hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
843     inputaddr+=4;
844     hcryp->Instance->DINR  = *(uint32_t*)(inputaddr);
845     inputaddr+=4;
846     hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
847     hcryp->pCrypInBuffPtr += 16;
848     hcryp->CrypInCount -= 16;
849     
850     /* Return function status */
851     return HAL_OK;
852   }
853   else
854   {
855     /* Release Lock */
856     __HAL_UNLOCK(hcryp);
857   
858     /* Return function status */
859     return HAL_ERROR;
860   }
861 }
862
863 /**
864   * @brief  Initializes the CRYP peripheral in AES CBC encryption mode using Interrupt.
865   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
866   *         the configuration information for CRYP module
867   * @param  pPlainData: Pointer to the plaintext buffer (aligned on u32)
868   * @param  Size: Length of the plaintext buffer, must be a multiple of 16 bytes
869   * @param  pCypherData: Pointer to the cyphertext buffer (aligned on u32)
870   * @retval HAL status
871   */
872 HAL_StatusTypeDef HAL_CRYP_AESCBC_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
873 {
874   uint32_t inputaddr = 0;
875   
876   /* Check that data aligned on u32 */
877   if((((uint32_t)pPlainData & (uint32_t)0x00000003) != 0) || (((uint32_t)pCypherData & (uint32_t)0x00000003) != 0) || ((Size & (uint16_t)0x000F) != 0))
878   {
879     /* Process Locked */
880     __HAL_UNLOCK(hcryp);
881
882     /* Return function status */
883     return HAL_ERROR;
884   }
885   
886   if ((hcryp->State != HAL_CRYP_STATE_RESET) && (hcryp->State == HAL_CRYP_STATE_READY))
887   {
888     /* Process Locked */
889     __HAL_LOCK(hcryp);
890     
891     /* Get the buffer addresses and sizes */
892     hcryp->CrypInCount = Size;
893     hcryp->pCrypInBuffPtr = pPlainData;
894     hcryp->pCrypOutBuffPtr = pCypherData;
895     hcryp->CrypOutCount = Size;
896     
897     /* Change the CRYP state */
898     hcryp->State = HAL_CRYP_STATE_BUSY;
899     
900     /* Check if initialization phase has already been performed */
901     if(hcryp->Phase == HAL_CRYP_PHASE_READY)
902     {
903       /* Set the key */
904       CRYP_SetKey(hcryp, hcryp->Init.pKey);
905       
906       /* Reset the CHMOD & MODE bits */
907       CLEAR_BIT(hcryp->Instance->CR, CRYP_ALGO_CHAIN_MASK);
908       
909       /* Set the CRYP peripheral in AES CBC mode */
910       __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CBC_ENCRYPT);
911       
912       /* Set the Initialization Vector */
913       CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect);
914       
915       /* Set the phase */
916       hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
917     }
918     
919     /* Enable Interrupts */
920     __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_CC);
921     
922     /* Enable CRYP */
923     __HAL_CRYP_ENABLE(hcryp);
924     
925     /* Get the last input data adress */
926     inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
927     
928     /* Write the Input block in the Data Input register */
929     hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
930     inputaddr+=4;
931     hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
932     inputaddr+=4;
933     hcryp->Instance->DINR  = *(uint32_t*)(inputaddr);
934     inputaddr+=4;
935     hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
936     hcryp->pCrypInBuffPtr += 16;
937     hcryp->CrypInCount -= 16;
938     
939     /* Return function status */
940     return HAL_OK;
941   }
942   else
943   {
944     /* Release Lock */
945     __HAL_UNLOCK(hcryp);
946    
947     /* Return function status */
948     return HAL_ERROR;
949   }
950 }
951
952 /**
953   * @brief  Initializes the CRYP peripheral in AES CTR encryption mode using Interrupt.
954   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
955   *         the configuration information for CRYP module
956   * @param  pPlainData: Pointer to the plaintext buffer (aligned on u32)
957   * @param  Size: Length of the plaintext buffer, must be a multiple of 16 bytes
958   * @param  pCypherData: Pointer to the cyphertext buffer (aligned on u32)
959   * @retval HAL status
960   */
961 HAL_StatusTypeDef HAL_CRYP_AESCTR_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
962 {
963   uint32_t inputaddr = 0;
964   
965   /* Check that data aligned on u32 */
966   if((((uint32_t)pPlainData & (uint32_t)0x00000003) != 0) || (((uint32_t)pCypherData & (uint32_t)0x00000003) != 0) || ((Size & (uint16_t)0x000F) != 0))
967   {
968     /* Process Locked */
969     __HAL_UNLOCK(hcryp);
970
971     /* Return function status */
972     return HAL_ERROR;
973   }
974   
975   if ((hcryp->State != HAL_CRYP_STATE_RESET) && (hcryp->State == HAL_CRYP_STATE_READY))
976   {
977     /* Process Locked */
978     __HAL_LOCK(hcryp);
979     
980     /* Get the buffer addresses and sizes */
981     hcryp->CrypInCount = Size;
982     hcryp->pCrypInBuffPtr = pPlainData;
983     hcryp->pCrypOutBuffPtr = pCypherData;
984     hcryp->CrypOutCount = Size;
985     
986     /* Change the CRYP state */
987     hcryp->State = HAL_CRYP_STATE_BUSY;
988     
989     /* Check if initialization phase has already been performed */
990     if(hcryp->Phase == HAL_CRYP_PHASE_READY)
991     {
992       /* Set the key */
993       CRYP_SetKey(hcryp, hcryp->Init.pKey);
994       
995       /* Reset the CHMOD & MODE bits */
996       CLEAR_BIT(hcryp->Instance->CR, CRYP_ALGO_CHAIN_MASK);
997       
998       /* Set the CRYP peripheral in AES CTR mode */
999       __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CTR_ENCRYPT);
1000       
1001       /* Set the Initialization Vector */
1002       CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect);
1003       
1004       /* Set the phase */
1005       hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
1006     }
1007     
1008     /* Enable Interrupts */
1009     __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_CC);
1010     
1011     /* Enable CRYP */
1012     __HAL_CRYP_ENABLE(hcryp);
1013     
1014     /* Get the last input data adress */
1015     inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
1016     
1017     /* Write the Input block in the Data Input register */
1018     hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
1019     inputaddr+=4;
1020     hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
1021     inputaddr+=4;
1022     hcryp->Instance->DINR  = *(uint32_t*)(inputaddr);
1023     inputaddr+=4;
1024     hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
1025     hcryp->pCrypInBuffPtr += 16;
1026     hcryp->CrypInCount -= 16;
1027     
1028     /* Return function status */
1029     return HAL_OK;
1030   }
1031   else
1032   {
1033     /* Release Lock */
1034     __HAL_UNLOCK(hcryp);
1035   
1036     /* Return function status */
1037     return HAL_ERROR;
1038   }
1039 }
1040
1041 /**
1042   * @brief  Initializes the CRYP peripheral in AES ECB decryption mode using Interrupt.
1043   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
1044   *         the configuration information for CRYP module
1045   * @param  pCypherData: Pointer to the cyphertext buffer (aligned on u32)
1046   * @param  Size: Length of the plaintext buffer, must be a multiple of 16.
1047   * @param  pPlainData: Pointer to the plaintext buffer (aligned on u32)
1048   * @retval HAL status
1049   */
1050 HAL_StatusTypeDef HAL_CRYP_AESECB_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
1051 {
1052   uint32_t inputaddr = 0;
1053   
1054   /* Check that data aligned on u32 */
1055   if((((uint32_t)pPlainData & (uint32_t)0x00000003) != 0) || (((uint32_t)pCypherData & (uint32_t)0x00000003) != 0) || ((Size & (uint16_t)0x000F) != 0))
1056   {
1057     /* Process Locked */
1058     __HAL_UNLOCK(hcryp);
1059
1060     /* Return function status */
1061     return HAL_ERROR;
1062   }
1063   
1064   if ((hcryp->State != HAL_CRYP_STATE_RESET) && (hcryp->State == HAL_CRYP_STATE_READY))
1065   {
1066     /* Process Locked */
1067     __HAL_LOCK(hcryp);
1068     
1069     /* Get the buffer addresses and sizes */
1070     hcryp->CrypInCount = Size;
1071     hcryp->pCrypInBuffPtr = pCypherData;
1072     hcryp->pCrypOutBuffPtr = pPlainData;
1073     hcryp->CrypOutCount = Size;
1074     
1075     /* Change the CRYP state */
1076     hcryp->State = HAL_CRYP_STATE_BUSY;
1077     
1078     /* Check if initialization phase has already been performed */
1079     if(hcryp->Phase == HAL_CRYP_PHASE_READY)
1080     {
1081       /* Set the key */
1082       CRYP_SetKey(hcryp, hcryp->Init.pKey);
1083       
1084       /* Reset the CHMOD & MODE bits */
1085       CLEAR_BIT(hcryp->Instance->CR, CRYP_ALGO_CHAIN_MASK);
1086       
1087       /* Set the CRYP peripheral in AES ECB decryption mode (with key derivation) */
1088       __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_ECB_KEYDERDECRYPT);
1089       
1090       /* Set the phase */
1091       hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
1092     }
1093     
1094     /* Enable Interrupts */
1095     __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_CC);
1096     
1097     /* Enable CRYP */
1098     __HAL_CRYP_ENABLE(hcryp);
1099     
1100     /* Get the last input data adress */
1101     inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
1102     
1103     /* Write the Input block in the Data Input register */
1104     hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
1105     inputaddr+=4;
1106     hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
1107     inputaddr+=4;
1108     hcryp->Instance->DINR  = *(uint32_t*)(inputaddr);
1109     inputaddr+=4;
1110     hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
1111     hcryp->pCrypInBuffPtr += 16;
1112     hcryp->CrypInCount -= 16;    
1113     
1114     /* Return function status */
1115     return HAL_OK;
1116   }
1117   else
1118   {
1119     /* Release Lock */
1120     __HAL_UNLOCK(hcryp);
1121   
1122     /* Return function status */
1123     return HAL_ERROR;
1124   }
1125 }
1126
1127 /**
1128   * @brief  Initializes the CRYP peripheral in AES CBC decryption mode using IT.
1129   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
1130   *         the configuration information for CRYP module
1131   * @param  pCypherData: Pointer to the cyphertext buffer (aligned on u32)
1132   * @param  Size: Length of the plaintext buffer, must be a multiple of 16
1133   * @param  pPlainData: Pointer to the plaintext buffer (aligned on u32)
1134   * @retval HAL status
1135   */
1136 HAL_StatusTypeDef HAL_CRYP_AESCBC_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
1137 {
1138   uint32_t inputaddr = 0;
1139   
1140   /* Check that data aligned on u32 */
1141   if((((uint32_t)pPlainData & (uint32_t)0x00000003) != 0) || (((uint32_t)pCypherData & (uint32_t)0x00000003) != 0) || ((Size & (uint16_t)0x000F) != 0))
1142   {
1143     /* Process Locked */
1144     __HAL_UNLOCK(hcryp);
1145
1146     /* Return function status */
1147     return HAL_ERROR;
1148   }
1149   
1150   if ((hcryp->State != HAL_CRYP_STATE_RESET) && (hcryp->State == HAL_CRYP_STATE_READY))
1151   {
1152     /* Process Locked */
1153     __HAL_LOCK(hcryp);
1154     
1155     /* Get the buffer addresses and sizes */
1156     hcryp->CrypInCount = Size;
1157     hcryp->pCrypInBuffPtr = pCypherData;
1158     hcryp->pCrypOutBuffPtr = pPlainData;
1159     hcryp->CrypOutCount = Size;
1160     
1161     /* Change the CRYP state */
1162     hcryp->State = HAL_CRYP_STATE_BUSY;
1163     
1164     /* Check if initialization phase has already been performed */
1165     if(hcryp->Phase == HAL_CRYP_PHASE_READY)
1166     {
1167       /* Set the key */
1168       CRYP_SetKey(hcryp, hcryp->Init.pKey);
1169       
1170       /* Reset the CHMOD & MODE bits */
1171       CLEAR_BIT(hcryp->Instance->CR, CRYP_ALGO_CHAIN_MASK);
1172       
1173       /* Set the CRYP peripheral in AES CBC decryption mode (with key derivation) */
1174       __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CBC_KEYDERDECRYPT);
1175       
1176       /* Set the Initialization Vector */
1177       CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect);
1178       
1179       /* Set the phase */
1180       hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
1181     }
1182     
1183     /* Enable Interrupts */
1184     __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_CC);
1185     
1186     /* Enable CRYP */
1187     __HAL_CRYP_ENABLE(hcryp);
1188     
1189     /* Get the last input data adress */
1190     inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
1191     
1192     /* Write the Input block in the Data Input register */
1193     hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
1194     inputaddr+=4;
1195     hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
1196     inputaddr+=4;
1197     hcryp->Instance->DINR  = *(uint32_t*)(inputaddr);
1198     inputaddr+=4;
1199     hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
1200     hcryp->pCrypInBuffPtr += 16;
1201     hcryp->CrypInCount -= 16;    
1202     
1203     /* Return function status */
1204     return HAL_OK;
1205   }
1206   else
1207   {
1208     /* Release Lock */
1209     __HAL_UNLOCK(hcryp);
1210   
1211     /* Return function status */
1212     return HAL_ERROR;
1213   }
1214 }
1215
1216 /**
1217   * @brief  Initializes the CRYP peripheral in AES CTR decryption mode using Interrupt.
1218   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
1219   *         the configuration information for CRYP module
1220   * @param  pCypherData: Pointer to the cyphertext buffer (aligned on u32)
1221   * @param  Size: Length of the plaintext buffer, must be a multiple of 16
1222   * @param  pPlainData: Pointer to the plaintext buffer (aligned on u32)
1223   * @retval HAL status
1224   */
1225 HAL_StatusTypeDef HAL_CRYP_AESCTR_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
1226 {
1227   uint32_t inputaddr = 0;
1228   
1229   /* Check that data aligned on u32 */
1230   if((((uint32_t)pPlainData & (uint32_t)0x00000003) != 0) || (((uint32_t)pCypherData & (uint32_t)0x00000003) != 0) || ((Size & (uint16_t)0x000F) != 0))
1231   {
1232     /* Process Locked */
1233     __HAL_UNLOCK(hcryp);
1234
1235     /* Return function status */
1236     return HAL_ERROR;
1237   }
1238   
1239   if ((hcryp->State != HAL_CRYP_STATE_RESET) && (hcryp->State == HAL_CRYP_STATE_READY))
1240   {
1241     /* Process Locked */
1242     __HAL_LOCK(hcryp);
1243     
1244     /* Get the buffer addresses and sizes */
1245     hcryp->CrypInCount = Size;
1246     hcryp->pCrypInBuffPtr = pCypherData;
1247     hcryp->pCrypOutBuffPtr = pPlainData;
1248     hcryp->CrypOutCount = Size;
1249     
1250     /* Change the CRYP state */
1251     hcryp->State = HAL_CRYP_STATE_BUSY;
1252     
1253     /* Check if initialization phase has already been performed */
1254     if(hcryp->Phase == HAL_CRYP_PHASE_READY)
1255     {
1256       /* Set the key */
1257       CRYP_SetKey(hcryp, hcryp->Init.pKey);
1258       
1259       /* Reset the CHMOD & MODE bits */
1260       CLEAR_BIT(hcryp->Instance->CR, CRYP_ALGO_CHAIN_MASK);
1261       
1262       /* Set the CRYP peripheral in AES CTR decryption mode */
1263       __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CTR_DECRYPT);
1264       
1265       /* Set the Initialization Vector */
1266       CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect);
1267       
1268       /* Set the phase */
1269       hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
1270     }
1271     
1272     /* Enable Interrupts */
1273     __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_CC);
1274     
1275     /* Enable CRYP */
1276     __HAL_CRYP_ENABLE(hcryp);
1277     
1278     /* Get the last input data adress */
1279     inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
1280     
1281     /* Write the Input block in the Data Input register */
1282     hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
1283     inputaddr+=4;
1284     hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
1285     inputaddr+=4;
1286     hcryp->Instance->DINR  = *(uint32_t*)(inputaddr);
1287     inputaddr+=4;
1288     hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
1289     hcryp->pCrypInBuffPtr += 16;
1290     hcryp->CrypInCount -= 16;    
1291     
1292     /* Return function status */
1293     return HAL_OK;
1294   }
1295   else
1296   {
1297     /* Release Lock */
1298     __HAL_UNLOCK(hcryp);
1299   
1300     /* Return function status */
1301     return HAL_ERROR;
1302   }
1303 }
1304
1305 /**
1306   * @brief  Initializes the CRYP peripheral in AES ECB encryption mode using DMA.
1307   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
1308   *         the configuration information for CRYP module
1309   * @param  pPlainData: Pointer to the plaintext buffer (aligned on u32)
1310   * @param  Size: Length of the plaintext buffer, must be a multiple of 16 bytes
1311   * @param  pCypherData: Pointer to the cyphertext buffer (aligned on u32)
1312   * @retval HAL status
1313   */
1314 HAL_StatusTypeDef HAL_CRYP_AESECB_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
1315 {
1316   uint32_t inputaddr = 0, outputaddr = 0;
1317   
1318   /* Check that data aligned on u32 */
1319   if((((uint32_t)pPlainData & (uint32_t)0x00000003) != 0) || (((uint32_t)pCypherData & (uint32_t)0x00000003) != 0) || ((Size & (uint16_t)0x000F) != 0))
1320   {
1321     /* Process Locked */
1322     __HAL_UNLOCK(hcryp);
1323
1324     /* Return function status */
1325     return HAL_ERROR;
1326   }
1327   
1328   /* Check if HAL_CRYP_Init has been called */
1329   if ((hcryp->State != HAL_CRYP_STATE_RESET) && (hcryp->State == HAL_CRYP_STATE_READY))
1330   {
1331     /* Process Locked */
1332     __HAL_LOCK(hcryp);
1333     
1334     inputaddr  = (uint32_t)pPlainData;
1335     outputaddr = (uint32_t)pCypherData;
1336     
1337     /* Change the CRYP state */
1338     hcryp->State = HAL_CRYP_STATE_BUSY;
1339     
1340     /* Check if initialization phase has already been performed */
1341     if(hcryp->Phase == HAL_CRYP_PHASE_READY)
1342     {
1343       /* Set the key */
1344       CRYP_SetKey(hcryp, hcryp->Init.pKey);
1345       
1346       /* Set the CRYP peripheral in AES ECB mode */
1347       __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_ECB_ENCRYPT);
1348       
1349       /* Set the phase */
1350       hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
1351     }
1352     /* Set the input and output addresses and start DMA transfer */ 
1353     CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
1354     
1355     /* Process Unlocked */
1356     __HAL_UNLOCK(hcryp);
1357     
1358     /* Return function status */
1359     return HAL_OK;
1360   }
1361   else
1362   {  
1363     /* Release Lock */
1364     __HAL_UNLOCK(hcryp);
1365   
1366     return HAL_ERROR;   
1367   }
1368 }
1369
1370 /**
1371   * @brief  Initializes the CRYP peripheral in AES CBC encryption mode using DMA.
1372   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
1373   *         the configuration information for CRYP module
1374   * @param  pPlainData: Pointer to the plaintext buffer (aligned on u32)
1375   * @param  Size: Length of the plaintext buffer, must be a multiple of 16.
1376   * @param  pCypherData: Pointer to the cyphertext buffer (aligned on u32)
1377   * @retval HAL status
1378   */
1379 HAL_StatusTypeDef HAL_CRYP_AESCBC_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
1380 {
1381   uint32_t inputaddr = 0, outputaddr = 0;
1382   
1383   /* Check that data aligned on u32 */
1384   if((((uint32_t)pPlainData & (uint32_t)0x00000003) != 0) || (((uint32_t)pCypherData & (uint32_t)0x00000003) != 0) || ((Size & (uint16_t)0x000F) != 0))
1385   {
1386     /* Process Locked */
1387     __HAL_UNLOCK(hcryp);
1388
1389     /* Return function status */
1390     return HAL_ERROR;
1391   }
1392   
1393   /* Check if HAL_CRYP_Init has been called */
1394   if ((hcryp->State != HAL_CRYP_STATE_RESET) && (hcryp->State == HAL_CRYP_STATE_READY))
1395   {
1396     /* Process Locked */
1397     __HAL_LOCK(hcryp);
1398     
1399     inputaddr  = (uint32_t)pPlainData;
1400     outputaddr = (uint32_t)pCypherData;
1401     
1402     /* Change the CRYP state */
1403     hcryp->State = HAL_CRYP_STATE_BUSY;
1404     
1405     /* Check if initialization phase has already been performed */
1406     if(hcryp->Phase == HAL_CRYP_PHASE_READY)
1407     {
1408       /* Set the key */
1409       CRYP_SetKey(hcryp, hcryp->Init.pKey);
1410       
1411       /* Set the CRYP peripheral in AES CBC mode */
1412       __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CBC_ENCRYPT);
1413       
1414       /* Set the Initialization Vector */
1415       CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect);
1416       
1417       /* Set the phase */
1418       hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
1419     }
1420     /* Set the input and output addresses and start DMA transfer */ 
1421     CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
1422     
1423     /* Process Unlocked */
1424     __HAL_UNLOCK(hcryp);
1425     
1426     /* Return function status */
1427     return HAL_OK;
1428   }
1429   else
1430   {
1431     /* Release Lock */
1432     __HAL_UNLOCK(hcryp);
1433   
1434     return HAL_ERROR;   
1435   }
1436 }
1437
1438 /**
1439   * @brief  Initializes the CRYP peripheral in AES CTR encryption mode using DMA.
1440   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
1441   *         the configuration information for CRYP module
1442   * @param  pPlainData: Pointer to the plaintext buffer (aligned on u32)
1443   * @param  Size: Length of the plaintext buffer, must be a multiple of 16.
1444   * @param  pCypherData: Pointer to the cyphertext buffer (aligned on u32)
1445   * @retval HAL status
1446   */
1447 HAL_StatusTypeDef HAL_CRYP_AESCTR_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
1448 {
1449   uint32_t inputaddr = 0, outputaddr = 0;
1450   
1451   /* Check that data aligned on u32 */
1452   if((((uint32_t)pPlainData & (uint32_t)0x00000003) != 0) || (((uint32_t)pCypherData & (uint32_t)0x00000003) != 0) || ((Size & (uint16_t)0x000F) != 0))
1453   {
1454     /* Process Locked */
1455     __HAL_UNLOCK(hcryp);
1456
1457     /* Return function status */
1458     return HAL_ERROR;
1459   }
1460   
1461   /* Check if HAL_CRYP_Init has been called */
1462   if ((hcryp->State != HAL_CRYP_STATE_RESET) && (hcryp->State == HAL_CRYP_STATE_READY))
1463   {
1464     /* Process Locked */
1465     __HAL_LOCK(hcryp);
1466     
1467     inputaddr  = (uint32_t)pPlainData;
1468     outputaddr = (uint32_t)pCypherData;
1469     
1470     /* Change the CRYP state */
1471     hcryp->State = HAL_CRYP_STATE_BUSY;
1472     
1473     /* Check if initialization phase has already been performed */
1474     if(hcryp->Phase == HAL_CRYP_PHASE_READY)
1475     {
1476       /* Set the key */
1477       CRYP_SetKey(hcryp, hcryp->Init.pKey);
1478       
1479       /* Set the CRYP peripheral in AES CTR mode */
1480       __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CTR_ENCRYPT);
1481       
1482       /* Set the Initialization Vector */
1483       CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect);
1484       
1485       /* Set the phase */
1486       hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
1487     }
1488     
1489     /* Set the input and output addresses and start DMA transfer */ 
1490     CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
1491     
1492     /* Process Unlocked */
1493     __HAL_UNLOCK(hcryp);
1494     
1495     /* Return function status */
1496     return HAL_OK;
1497   }
1498   else
1499   {
1500     /* Release Lock */
1501     __HAL_UNLOCK(hcryp);
1502   
1503     return HAL_ERROR;   
1504   }
1505 }
1506
1507 /**
1508   * @brief  Initializes the CRYP peripheral in AES ECB decryption mode using DMA.
1509   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
1510   *         the configuration information for CRYP module
1511   * @param  pCypherData: Pointer to the cyphertext buffer (aligned on u32)
1512   * @param  Size: Length of the plaintext buffer, must be a multiple of 16 bytes
1513   * @param  pPlainData: Pointer to the plaintext buffer (aligned on u32)
1514   * @retval HAL status
1515   */
1516 HAL_StatusTypeDef HAL_CRYP_AESECB_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
1517 {  
1518   uint32_t inputaddr = 0, outputaddr = 0;
1519   
1520   /* Check that data aligned on u32 */
1521   if((((uint32_t)pPlainData & (uint32_t)0x00000003) != 0) || (((uint32_t)pCypherData & (uint32_t)0x00000003) != 0) || ((Size & (uint16_t)0x000F) != 0))
1522   {
1523     /* Process Locked */
1524     __HAL_UNLOCK(hcryp);
1525
1526     /* Return function status */
1527     return HAL_ERROR;
1528   }
1529   
1530   /* Check if HAL_CRYP_Init has been called */
1531   if ((hcryp->State != HAL_CRYP_STATE_RESET) && (hcryp->State == HAL_CRYP_STATE_READY))
1532   {
1533     /* Process Locked */
1534     __HAL_LOCK(hcryp);
1535     
1536     inputaddr  = (uint32_t)pCypherData;
1537     outputaddr = (uint32_t)pPlainData;
1538     
1539     /* Change the CRYP state */
1540     hcryp->State = HAL_CRYP_STATE_BUSY;
1541     
1542     /* Check if initialization phase has already been performed */
1543     if(hcryp->Phase == HAL_CRYP_PHASE_READY)
1544     {
1545       /* Set the key */
1546       CRYP_SetKey(hcryp, hcryp->Init.pKey);
1547       
1548       /* Reset the CHMOD & MODE bits */
1549       CLEAR_BIT(hcryp->Instance->CR, CRYP_ALGO_CHAIN_MASK);
1550       
1551       /* Set the CRYP peripheral in AES ECB decryption mode (with key derivation) */
1552       __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_ECB_KEYDERDECRYPT);
1553       
1554       /* Set the phase */
1555       hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
1556     }
1557     
1558     /* Set the input and output addresses and start DMA transfer */ 
1559     CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
1560     
1561     /* Process Unlocked */
1562     __HAL_UNLOCK(hcryp);
1563     
1564     /* Return function status */
1565     return HAL_OK;
1566   }
1567   else
1568   {
1569     /* Release Lock */
1570     __HAL_UNLOCK(hcryp);
1571   
1572     return HAL_ERROR;   
1573   }
1574 }
1575
1576 /**
1577   * @brief  Initializes the CRYP peripheral in AES CBC encryption mode using DMA.
1578   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
1579   *         the configuration information for CRYP module
1580   * @param  pCypherData: Pointer to the cyphertext buffer (aligned on u32)
1581   * @param  Size: Length of the plaintext buffer, must be a multiple of 16 bytes
1582   * @param  pPlainData: Pointer to the plaintext buffer (aligned on u32)
1583   * @retval HAL status
1584   */
1585 HAL_StatusTypeDef HAL_CRYP_AESCBC_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
1586 {
1587   uint32_t inputaddr = 0, outputaddr = 0;
1588   
1589   /* Check that data aligned on u32 */
1590   if((((uint32_t)pPlainData & (uint32_t)0x00000003) != 0) || (((uint32_t)pCypherData & (uint32_t)0x00000003) != 0) || ((Size & (uint16_t)0x000F) != 0))
1591   {
1592     /* Process Locked */
1593     __HAL_UNLOCK(hcryp);
1594
1595     /* Return function status */
1596     return HAL_ERROR;
1597   }
1598   
1599   /* Check if HAL_CRYP_Init has been called */
1600   if ((hcryp->State != HAL_CRYP_STATE_RESET) && (hcryp->State == HAL_CRYP_STATE_READY))
1601   {
1602     /* Process Locked */
1603     __HAL_LOCK(hcryp);
1604     
1605     inputaddr  = (uint32_t)pCypherData;
1606     outputaddr = (uint32_t)pPlainData;
1607     
1608     /* Change the CRYP state */
1609     hcryp->State = HAL_CRYP_STATE_BUSY;
1610     
1611     /* Check if initialization phase has already been performed */
1612     if(hcryp->Phase == HAL_CRYP_PHASE_READY)
1613     {
1614       /* Set the key */
1615       CRYP_SetKey(hcryp, hcryp->Init.pKey);
1616       
1617       /* Reset the CHMOD & MODE bits */
1618       CLEAR_BIT(hcryp->Instance->CR, CRYP_ALGO_CHAIN_MASK);
1619       
1620       /* Set the CRYP peripheral in AES CBC decryption mode (with key derivation) */
1621       __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CBC_KEYDERDECRYPT);
1622       
1623       /* Set the Initialization Vector */
1624       CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect);
1625       
1626       /* Set the phase */
1627       hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
1628     }
1629     
1630     /* Set the input and output addresses and start DMA transfer */ 
1631     CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
1632     
1633     /* Process Unlocked */
1634     __HAL_UNLOCK(hcryp);
1635     
1636     /* Return function status */
1637     return HAL_OK;
1638   }
1639   else
1640   {
1641     /* Release Lock */
1642     __HAL_UNLOCK(hcryp);
1643   
1644     return HAL_ERROR;   
1645   }
1646 }
1647
1648 /**
1649   * @brief  Initializes the CRYP peripheral in AES CTR decryption mode using DMA.
1650   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
1651   *         the configuration information for CRYP module
1652   * @param  pCypherData: Pointer to the cyphertext buffer (aligned on u32)
1653   * @param  Size: Length of the plaintext buffer, must be a multiple of 16
1654   * @param  pPlainData: Pointer to the plaintext buffer (aligned on u32)
1655   * @retval HAL status
1656   */
1657 HAL_StatusTypeDef HAL_CRYP_AESCTR_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
1658 {  
1659   uint32_t inputaddr = 0, outputaddr = 0;
1660   
1661   /* Check that data aligned on u32 */
1662   if((((uint32_t)pPlainData & (uint32_t)0x00000003) != 0) || (((uint32_t)pCypherData & (uint32_t)0x00000003) != 0) || ((Size & (uint16_t)0x000F) != 0))
1663   {
1664     /* Process Locked */
1665     __HAL_UNLOCK(hcryp);
1666
1667     /* Return function status */
1668     return HAL_ERROR;
1669   }
1670   
1671   /* Check if HAL_CRYP_Init has been called */
1672   if ((hcryp->State != HAL_CRYP_STATE_RESET) && (hcryp->State == HAL_CRYP_STATE_READY))
1673   {
1674     /* Process Locked */
1675     __HAL_LOCK(hcryp);
1676     
1677     inputaddr  = (uint32_t)pCypherData;
1678     outputaddr = (uint32_t)pPlainData;
1679     
1680     /* Change the CRYP state */
1681     hcryp->State = HAL_CRYP_STATE_BUSY;
1682     
1683     /* Check if initialization phase has already been performed */
1684     if(hcryp->Phase == HAL_CRYP_PHASE_READY)
1685     {
1686       /* Set the key */
1687       CRYP_SetKey(hcryp, hcryp->Init.pKey);
1688       
1689       /* Set the CRYP peripheral in AES CTR mode */
1690       __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CTR_DECRYPT);
1691       
1692       /* Set the Initialization Vector */
1693       CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect);
1694       
1695       /* Set the phase */
1696       hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
1697     }
1698     
1699     /* Set the input and output addresses and start DMA transfer */ 
1700     CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
1701     
1702     /* Process Unlocked */
1703     __HAL_UNLOCK(hcryp);
1704     
1705     /* Return function status */
1706     return HAL_OK;
1707   }
1708   else
1709   {
1710     /* Release Lock */
1711     __HAL_UNLOCK(hcryp);
1712   
1713     return HAL_ERROR;   
1714   }
1715 }
1716
1717 /**
1718   * @}
1719   */
1720
1721 /** @defgroup CRYP_Exported_Functions_Group3 DMA callback functions 
1722  *  @brief   DMA callback functions. 
1723  *
1724 @verbatim   
1725   ==============================================================================
1726                       ##### DMA callback functions  #####
1727   ==============================================================================  
1728     [..]  This section provides DMA callback functions:
1729       (+) DMA Input data transfer complete
1730       (+) DMA Output data transfer complete
1731       (+) DMA error
1732
1733 @endverbatim
1734   * @{
1735   */
1736
1737 /**
1738   * @brief  CRYP error callback.
1739   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
1740   *         the configuration information for CRYP module
1741   * @retval None
1742   */
1743  __weak void HAL_CRYP_ErrorCallback(CRYP_HandleTypeDef *hcryp)
1744 {
1745   /* NOTE : This function should not be modified; when the callback is needed, 
1746             the HAL_CRYP_ErrorCallback can be implemented in the user file
1747    */ 
1748 }
1749
1750 /**
1751   * @brief  Input transfer completed callback.
1752   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
1753   *         the configuration information for CRYP module
1754   * @retval None
1755   */
1756 __weak void HAL_CRYP_InCpltCallback(CRYP_HandleTypeDef *hcryp)
1757 {
1758   /* NOTE : This function should not be modified; when the callback is needed, 
1759             the HAL_CRYP_InCpltCallback can be implemented in the user file
1760    */ 
1761 }
1762
1763 /**
1764   * @brief  Output transfer completed callback.
1765   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
1766   *         the configuration information for CRYP module
1767   * @retval None
1768   */
1769 __weak void HAL_CRYP_OutCpltCallback(CRYP_HandleTypeDef *hcryp)
1770 {
1771   /* NOTE : This function should not be modified; when the callback is needed, 
1772             the HAL_CRYP_OutCpltCallback can be implemented in the user file
1773    */ 
1774 }
1775
1776 /**
1777   * @}
1778   */
1779
1780 /** @defgroup CRYP_Exported_Functions_Group4 CRYP IRQ handler 
1781  *  @brief   CRYP IRQ handler.
1782  *
1783 @verbatim   
1784   ==============================================================================
1785                 ##### CRYP IRQ handler management #####
1786   ==============================================================================  
1787 [..]  This section provides CRYP IRQ handler function.
1788
1789 @endverbatim
1790   * @{
1791   */
1792
1793 /**
1794   * @brief  This function handles CRYP interrupt request.
1795   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
1796   *         the configuration information for CRYP module
1797   * @retval None
1798   */
1799 void HAL_CRYP_IRQHandler(CRYP_HandleTypeDef *hcryp)
1800 {
1801   /* Check if error occurred*/
1802   if (__HAL_CRYP_GET_IT_SOURCE(hcryp, CRYP_IT_ERR) != RESET)
1803   {
1804     if (__HAL_CRYP_GET_FLAG(hcryp,CRYP_FLAG_RDERR) != RESET)
1805     {
1806       __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CLEARFLAG_RDERR);
1807     }
1808     
1809     if (__HAL_CRYP_GET_FLAG(hcryp,CRYP_FLAG_WRERR) != RESET)
1810     {
1811       __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CLEARFLAG_WRERR);
1812     }
1813     
1814     if (__HAL_CRYP_GET_FLAG(hcryp,CRYP_FLAG_CCF) != RESET)
1815     {
1816       __HAL_CRYP_CLEAR_FLAG(hcryp,CRYP_CLEARFLAG_CCF);
1817     }
1818     
1819     hcryp->State= HAL_CRYP_STATE_ERROR;
1820     /* Disable Computation Complete Interrupt */
1821     __HAL_CRYP_DISABLE_IT(hcryp,CRYP_IT_CC);
1822     __HAL_CRYP_DISABLE_IT(hcryp,CRYP_IT_ERR);
1823     
1824     HAL_CRYP_ErrorCallback(hcryp);
1825     
1826     /* Process Unlocked */
1827     __HAL_UNLOCK(hcryp);
1828     
1829     return;
1830   }
1831   
1832   /* Check if computation complete interrupt was enabled*/
1833   if (__HAL_CRYP_GET_IT_SOURCE(hcryp, CRYP_IT_CC) != RESET)
1834   {
1835     /* Clear CCF Flag */
1836     __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CLEARFLAG_CCF);
1837   
1838     CRYP_EncryptDecrypt_IT(hcryp);
1839   }
1840 }
1841
1842 /**
1843   * @}
1844   */
1845
1846 /** @defgroup CRYP_Exported_Functions_Group5 Peripheral State functions 
1847  *  @brief   Peripheral State functions. 
1848  *
1849 @verbatim   
1850   ==============================================================================
1851                       ##### Peripheral State functions #####
1852   ==============================================================================  
1853     [..]
1854     This subsection permits to get in run-time the status of the peripheral.
1855
1856 @endverbatim
1857   * @{
1858   */
1859
1860 /**
1861   * @brief  Returns the CRYP state.
1862   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
1863   *         the configuration information for CRYP module
1864   * @retval HAL state
1865   */
1866 HAL_CRYP_STATETypeDef HAL_CRYP_GetState(CRYP_HandleTypeDef *hcryp)
1867 {
1868   return hcryp->State;
1869 }
1870
1871 /**
1872   * @}
1873   */
1874
1875 /**
1876   * @}
1877   */
1878
1879 /** @addtogroup CRYP_Private_Functions
1880   * @{
1881   */
1882
1883 /**
1884   * @brief  IT function called under interruption context to continue encryption or decryption
1885   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
1886   *         the configuration information for CRYP module
1887   * @retval HAL status
1888   */
1889 static HAL_StatusTypeDef CRYP_EncryptDecrypt_IT(CRYP_HandleTypeDef *hcryp)
1890 {
1891   uint32_t inputaddr = 0, outputaddr = 0;
1892
1893   /* Get the last Output data adress */
1894   outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
1895   
1896   /* Read the Output block from the Output Register */
1897   *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
1898   outputaddr+=4;
1899   *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
1900   outputaddr+=4;
1901   *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
1902   outputaddr+=4;
1903   *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
1904   
1905   hcryp->pCrypOutBuffPtr += 16;
1906   hcryp->CrypOutCount -= 16;
1907   
1908   /* Check if all input text is encrypted or decrypted */
1909   if(hcryp->CrypOutCount == 0)
1910   {
1911     /* Disable Computation Complete Interrupt */
1912     __HAL_CRYP_DISABLE_IT(hcryp,CRYP_IT_CC);
1913     __HAL_CRYP_DISABLE_IT(hcryp,CRYP_IT_ERR);
1914     
1915     /* Process Unlocked */
1916     __HAL_UNLOCK(hcryp);
1917     
1918     /* Change the CRYP state */
1919     hcryp->State = HAL_CRYP_STATE_READY;
1920     
1921     /* Call computation complete callback */
1922     HAL_CRYPEx_ComputationCpltCallback(hcryp);
1923   }
1924   else /* Process the rest of input text */
1925   {
1926     /* Get the last Intput data adress */
1927     inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
1928     
1929     /* Write the Input block in the Data Input register */
1930     hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
1931     inputaddr+=4;
1932     hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
1933     inputaddr+=4;
1934     hcryp->Instance->DINR  = *(uint32_t*)(inputaddr);
1935     inputaddr+=4;
1936     hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
1937     hcryp->pCrypInBuffPtr += 16;
1938     hcryp->CrypInCount -= 16;      
1939   }
1940   return HAL_OK;
1941 }
1942 /**
1943   * @brief  DMA CRYP Input Data process complete callback.
1944   * @param  hdma: DMA handle
1945   * @retval None
1946   */
1947 static void CRYP_DMAInCplt(DMA_HandleTypeDef *hdma)  
1948 {
1949   CRYP_HandleTypeDef* hcryp = (CRYP_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
1950   
1951   /* Disable the DMA transfer for input request  */
1952   CLEAR_BIT(hcryp->Instance->CR, AES_CR_DMAINEN);
1953   
1954   /* Call input data transfer complete callback */
1955   HAL_CRYP_InCpltCallback(hcryp);
1956 }
1957
1958 /**
1959   * @brief  DMA CRYP Output Data process complete callback.
1960   * @param  hdma: DMA handle
1961   * @retval None
1962   */
1963 static void CRYP_DMAOutCplt(DMA_HandleTypeDef *hdma)
1964 {
1965   CRYP_HandleTypeDef* hcryp = (CRYP_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
1966   
1967   /* Disable the DMA transfer for output request by resetting the DMAOUTEN bit
1968      in the DMACR register */
1969   CLEAR_BIT(hcryp->Instance->CR, AES_CR_DMAOUTEN);
1970
1971   /* Clear CCF Flag */
1972   __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CLEARFLAG_CCF);
1973
1974   /* Disable CRYP */
1975   __HAL_CRYP_DISABLE(hcryp);
1976   
1977   /* Change the CRYP state to ready */
1978   hcryp->State = HAL_CRYP_STATE_READY;
1979   
1980   /* Call output data transfer complete callback */
1981   HAL_CRYP_OutCpltCallback(hcryp);
1982 }
1983
1984 /**
1985   * @brief  DMA CRYP communication error callback. 
1986   * @param  hdma: DMA handle
1987   * @retval None
1988   */
1989 static void CRYP_DMAError(DMA_HandleTypeDef *hdma)
1990 {
1991   CRYP_HandleTypeDef* hcryp = (CRYP_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
1992   hcryp->State= HAL_CRYP_STATE_ERROR;
1993   HAL_CRYP_ErrorCallback(hcryp);
1994 }
1995
1996 /**
1997   * @brief  Writes the Key in Key registers. 
1998   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
1999   *         the configuration information for CRYP module
2000   * @param  Key: Pointer to Key buffer
2001   * @note Key must be written as little endian.
2002   *         If Key pointer points at address n, 
2003   *         n[15:0] contains key[96:127], 
2004   *         (n+4)[15:0] contains key[64:95], 
2005   *         (n+8)[15:0] contains key[32:63] and 
2006   *         (n+12)[15:0] contains key[0:31]
2007   * @retval None
2008   */
2009 static void CRYP_SetKey(CRYP_HandleTypeDef *hcryp, uint8_t *Key)
2010 {  
2011   uint32_t keyaddr = (uint32_t)Key;
2012   
2013   hcryp->Instance->KEYR3 = __REV(*(uint32_t*)(keyaddr));
2014   keyaddr+=4;
2015   hcryp->Instance->KEYR2 = __REV(*(uint32_t*)(keyaddr));
2016   keyaddr+=4;
2017   hcryp->Instance->KEYR1 = __REV(*(uint32_t*)(keyaddr));
2018   keyaddr+=4;
2019   hcryp->Instance->KEYR0 = __REV(*(uint32_t*)(keyaddr));
2020 }
2021
2022 /**
2023   * @brief  Writes the InitVector/InitCounter in IV registers. 
2024   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
2025   *         the configuration information for CRYP module
2026   * @param  InitVector: Pointer to InitVector/InitCounter buffer
2027   * @note Init Vector must be written as little endian.
2028   *         If Init Vector pointer points at address n, 
2029   *         n[15:0] contains Vector[96:127], 
2030   *         (n+4)[15:0] contains Vector[64:95], 
2031   *         (n+8)[15:0] contains Vector[32:63] and 
2032   *         (n+12)[15:0] contains Vector[0:31]
2033   * @retval None
2034   */
2035 static void CRYP_SetInitVector(CRYP_HandleTypeDef *hcryp, uint8_t *InitVector)
2036 {
2037   uint32_t ivaddr = (uint32_t)InitVector;
2038   
2039   hcryp->Instance->IVR3 = __REV(*(uint32_t*)(ivaddr));
2040   ivaddr+=4;
2041   hcryp->Instance->IVR2 = __REV(*(uint32_t*)(ivaddr));
2042   ivaddr+=4;
2043   hcryp->Instance->IVR1 = __REV(*(uint32_t*)(ivaddr));
2044   ivaddr+=4;
2045   hcryp->Instance->IVR0 = __REV(*(uint32_t*)(ivaddr));
2046 }
2047
2048 /**
2049   * @brief  Process Data: Writes Input data in polling mode and reads the output data
2050   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
2051   *         the configuration information for CRYP module
2052   * @param  Input: Pointer to the Input buffer
2053   * @param  Ilength: Length of the Input buffer, must be a multiple of 16.
2054   * @param  Output: Pointer to the returned buffer
2055   * @param  Timeout: Specify Timeout value  
2056   * @retval None
2057   */
2058 static HAL_StatusTypeDef CRYP_ProcessData(CRYP_HandleTypeDef *hcryp, uint8_t* Input, uint16_t Ilength, uint8_t* Output, uint32_t Timeout)
2059 {
2060   uint32_t tickstart = 0;
2061   
2062   uint32_t index = 0;
2063   uint32_t inputaddr  = (uint32_t)Input;
2064   uint32_t outputaddr = (uint32_t)Output;
2065   
2066   for(index=0; (index < Ilength); index += 16)
2067   {
2068     /* Write the Input block in the Data Input register */
2069     hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
2070     inputaddr+=4;
2071     hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
2072     inputaddr+=4;
2073     hcryp->Instance->DINR  = *(uint32_t*)(inputaddr);
2074     inputaddr+=4;
2075     hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
2076     inputaddr+=4;
2077     
2078     /* Get timeout */
2079     tickstart = HAL_GetTick();
2080     
2081     while(HAL_IS_BIT_CLR(hcryp->Instance->SR, AES_SR_CCF))
2082     {    
2083       /* Check for the Timeout */
2084       if(Timeout != HAL_MAX_DELAY)
2085       {
2086         if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
2087         {
2088           /* Change state */
2089           hcryp->State = HAL_CRYP_STATE_TIMEOUT;
2090           
2091           /* Process Unlocked */          
2092           __HAL_UNLOCK(hcryp);
2093           
2094           return HAL_TIMEOUT;
2095         }
2096       }
2097     }
2098     /* Clear CCF Flag */
2099     __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CLEARFLAG_CCF);
2100     
2101     /* Read the Output block from the Data Output Register */
2102     *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
2103     outputaddr+=4;
2104     *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
2105     outputaddr+=4;
2106     *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
2107     outputaddr+=4;
2108     *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
2109     outputaddr+=4;
2110   }
2111   /* Return function status */
2112   return HAL_OK;
2113 }
2114
2115 /**
2116   * @brief  Set the DMA configuration and start the DMA transfer
2117   * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
2118   *         the configuration information for CRYP module
2119   * @param  inputaddr: address of the Input buffer
2120   * @param  Size: Size of the Input buffer, must be a multiple of 16.
2121   * @param  outputaddr: address of the Output buffer
2122   * @retval None
2123   */
2124 static void CRYP_SetDMAConfig(CRYP_HandleTypeDef *hcryp, uint32_t inputaddr, uint16_t Size, uint32_t outputaddr)
2125 {
2126   /* Set the CRYP DMA transfer complete callback */
2127   hcryp->hdmain->XferCpltCallback = CRYP_DMAInCplt;
2128   /* Set the DMA error callback */
2129   hcryp->hdmain->XferErrorCallback = CRYP_DMAError;
2130   
2131   /* Set the CRYP DMA transfer complete callback */
2132   hcryp->hdmaout->XferCpltCallback = CRYP_DMAOutCplt;
2133   /* Set the DMA error callback */
2134   hcryp->hdmaout->XferErrorCallback = CRYP_DMAError;
2135
2136   /* Enable the DMA In DMA Stream */
2137   HAL_DMA_Start_IT(hcryp->hdmain, inputaddr, (uint32_t)&hcryp->Instance->DINR, Size/4);
2138
2139   /* Enable the DMA Out DMA Stream */
2140   HAL_DMA_Start_IT(hcryp->hdmaout, (uint32_t)&hcryp->Instance->DOUTR, outputaddr, Size/4);
2141
2142   /* Enable In and Out DMA requests */
2143   SET_BIT(hcryp->Instance->CR, (AES_CR_DMAINEN | AES_CR_DMAOUTEN));
2144
2145   /* Enable CRYP */
2146   __HAL_CRYP_ENABLE(hcryp);
2147 }
2148
2149 /**
2150   * @}
2151   */
2152
2153 /**
2154   * @}
2155   */
2156
2157 /**
2158   * @}
2159   */
2160
2161 #endif /* HAL_CRYP_MODULE_ENABLED */
2162 #endif /* STM32L041xx || STM32L061xx || STM32L062xx || STM32L063xx || STM32L081xx || STM32L082xx || STM32L083xx */
2163 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
2164