2 ******************************************************************************
3 * @file stm32f7xx_hal_cryp.c
4 * @author MCD Application Team
7 * @brief CRYP HAL module driver.
8 * This file provides firmware functions to manage the following
9 * functionalities of the Cryptography (CRYP) peripheral:
10 * + Initialization and de-initialization functions
11 * + AES processing functions
12 * + DES processing functions
13 * + TDES processing functions
14 * + DMA callback functions
15 * + CRYP IRQ handler management
16 * + Peripheral State functions
19 ==============================================================================
20 ##### How to use this driver #####
21 ==============================================================================
23 The CRYP HAL driver can be used as follows:
25 (#)Initialize the CRYP low level resources by implementing the HAL_CRYP_MspInit():
26 (##) Enable the CRYP interface clock using __HAL_RCC_CRYP_CLK_ENABLE()
27 (##) In case of using interrupts (e.g. HAL_CRYP_AESECB_Encrypt_IT())
28 (+++) Configure the CRYP interrupt priority using HAL_NVIC_SetPriority()
29 (+++) Enable the CRYP IRQ handler using HAL_NVIC_EnableIRQ()
30 (+++) In CRYP IRQ handler, call HAL_CRYP_IRQHandler()
31 (##) In case of using DMA to control data transfer (e.g. HAL_CRYP_AESECB_Encrypt_DMA())
32 (+++) Enable the DMAx interface clock using __DMAx_CLK_ENABLE()
33 (+++) Configure and enable two DMA streams one for managing data transfer from
34 memory to peripheral (input stream) and another stream for managing data
35 transfer from peripheral to memory (output stream)
36 (+++) Associate the initialized DMA handle to the CRYP DMA handle
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 HAL_NVIC_SetPriority() and HAL_NVIC_EnableIRQ()
42 (#)Initialize the CRYP HAL using HAL_CRYP_Init(). This function configures mainly:
43 (##) The data type: 1-bit, 8-bit, 16-bit and 32-bit
44 (##) The key size: 128, 192 and 256. This parameter is relevant only for AES
45 (##) The encryption/decryption key. It's size depends on the algorithm
46 used for encryption/decryption
47 (##) The initialization vector (counter). It is not used ECB mode.
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()
60 (#)When the processing function is called at 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
64 When a new data block is to be processed, call HAL_CRYP_Init() then the
67 (#)Call HAL_CRYP_DeInit() to deinitialize the CRYP peripheral.
70 ******************************************************************************
73 * <h2><center>© COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
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.
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.
97 ******************************************************************************
100 /* Includes ------------------------------------------------------------------*/
101 #include "stm32f7xx_hal.h"
103 #ifdef HAL_CRYP_MODULE_ENABLED
107 /** @addtogroup STM32F7xx_HAL_Driver
110 /** @defgroup CRYP CRYP
111 * @brief CRYP HAL module driver.
115 /* Private typedef -----------------------------------------------------------*/
116 /* Private define ------------------------------------------------------------*/
117 /** @addtogroup CRYP_Private_define
120 #define CRYP_TIMEOUT_VALUE 1
125 /* Private macro -------------------------------------------------------------*/
126 /* Private variables ---------------------------------------------------------*/
127 /* Private function prototypes -----------------------------------------------*/
128 /** @addtogroup CRYP_Private_Functions_prototypes
131 static void CRYP_SetInitVector(CRYP_HandleTypeDef
*hcryp
, uint8_t *InitVector
, uint32_t IVSize
);
132 static void CRYP_SetKey(CRYP_HandleTypeDef
*hcryp
, uint8_t *Key
, uint32_t KeySize
);
133 static HAL_StatusTypeDef
CRYP_ProcessData(CRYP_HandleTypeDef
*hcryp
, uint8_t* Input
, uint16_t Ilength
, uint8_t* Output
, uint32_t Timeout
);
134 static HAL_StatusTypeDef
CRYP_ProcessData2Words(CRYP_HandleTypeDef
*hcryp
, uint8_t* Input
, uint16_t Ilength
, uint8_t* Output
, uint32_t Timeout
);
135 static void CRYP_DMAInCplt(DMA_HandleTypeDef
*hdma
);
136 static void CRYP_DMAOutCplt(DMA_HandleTypeDef
*hdma
);
137 static void CRYP_DMAError(DMA_HandleTypeDef
*hdma
);
138 static void CRYP_SetDMAConfig(CRYP_HandleTypeDef
*hcryp
, uint32_t inputaddr
, uint16_t Size
, uint32_t outputaddr
);
139 static void CRYP_SetTDESECBMode(CRYP_HandleTypeDef
*hcryp
, uint32_t Direction
);
140 static void CRYP_SetTDESCBCMode(CRYP_HandleTypeDef
*hcryp
, uint32_t Direction
);
141 static void CRYP_SetDESECBMode(CRYP_HandleTypeDef
*hcryp
, uint32_t Direction
);
142 static void CRYP_SetDESCBCMode(CRYP_HandleTypeDef
*hcryp
, uint32_t Direction
);
146 /* Private functions ---------------------------------------------------------*/
148 /** @addtogroup CRYP_Private_Functions
153 * @brief DMA CRYP Input Data process complete callback.
154 * @param hdma: DMA handle
157 static void CRYP_DMAInCplt(DMA_HandleTypeDef
*hdma
)
159 CRYP_HandleTypeDef
* hcryp
= (CRYP_HandleTypeDef
*)((DMA_HandleTypeDef
*)hdma
)->Parent
;
161 /* Disable the DMA transfer for input FIFO request by resetting the DIEN bit
162 in the DMACR register */
163 hcryp
->Instance
->DMACR
&= (uint32_t)(~CRYP_DMACR_DIEN
);
165 /* Call input data transfer complete callback */
166 HAL_CRYP_InCpltCallback(hcryp
);
170 * @brief DMA CRYP Output Data process complete callback.
171 * @param hdma: DMA handle
174 static void CRYP_DMAOutCplt(DMA_HandleTypeDef
*hdma
)
176 CRYP_HandleTypeDef
* hcryp
= (CRYP_HandleTypeDef
*)((DMA_HandleTypeDef
*)hdma
)->Parent
;
178 /* Disable the DMA transfer for output FIFO request by resetting the DOEN bit
179 in the DMACR register */
180 hcryp
->Instance
->DMACR
&= (uint32_t)(~CRYP_DMACR_DOEN
);
183 __HAL_CRYP_DISABLE(hcryp
);
185 /* Change the CRYP state to ready */
186 hcryp
->State
= HAL_CRYP_STATE_READY
;
188 /* Call output data transfer complete callback */
189 HAL_CRYP_OutCpltCallback(hcryp
);
193 * @brief DMA CRYP communication error callback.
194 * @param hdma: DMA handle
197 static void CRYP_DMAError(DMA_HandleTypeDef
*hdma
)
199 CRYP_HandleTypeDef
* hcryp
= (CRYP_HandleTypeDef
*)((DMA_HandleTypeDef
*)hdma
)->Parent
;
200 hcryp
->State
= HAL_CRYP_STATE_READY
;
201 HAL_CRYP_ErrorCallback(hcryp
);
205 * @brief Writes the Key in Key registers.
206 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
207 * the configuration information for CRYP module
208 * @param Key: Pointer to Key buffer
209 * @param KeySize: Size of Key
212 static void CRYP_SetKey(CRYP_HandleTypeDef
*hcryp
, uint8_t *Key
, uint32_t KeySize
)
214 uint32_t keyaddr
= (uint32_t)Key
;
218 case CRYP_KEYSIZE_256B
:
219 /* Key Initialisation */
220 hcryp
->Instance
->K0LR
= __REV(*(uint32_t*)(keyaddr
));
222 hcryp
->Instance
->K0RR
= __REV(*(uint32_t*)(keyaddr
));
224 hcryp
->Instance
->K1LR
= __REV(*(uint32_t*)(keyaddr
));
226 hcryp
->Instance
->K1RR
= __REV(*(uint32_t*)(keyaddr
));
228 hcryp
->Instance
->K2LR
= __REV(*(uint32_t*)(keyaddr
));
230 hcryp
->Instance
->K2RR
= __REV(*(uint32_t*)(keyaddr
));
232 hcryp
->Instance
->K3LR
= __REV(*(uint32_t*)(keyaddr
));
234 hcryp
->Instance
->K3RR
= __REV(*(uint32_t*)(keyaddr
));
236 case CRYP_KEYSIZE_192B
:
237 hcryp
->Instance
->K1LR
= __REV(*(uint32_t*)(keyaddr
));
239 hcryp
->Instance
->K1RR
= __REV(*(uint32_t*)(keyaddr
));
241 hcryp
->Instance
->K2LR
= __REV(*(uint32_t*)(keyaddr
));
243 hcryp
->Instance
->K2RR
= __REV(*(uint32_t*)(keyaddr
));
245 hcryp
->Instance
->K3LR
= __REV(*(uint32_t*)(keyaddr
));
247 hcryp
->Instance
->K3RR
= __REV(*(uint32_t*)(keyaddr
));
249 case CRYP_KEYSIZE_128B
:
250 hcryp
->Instance
->K2LR
= __REV(*(uint32_t*)(keyaddr
));
252 hcryp
->Instance
->K2RR
= __REV(*(uint32_t*)(keyaddr
));
254 hcryp
->Instance
->K3LR
= __REV(*(uint32_t*)(keyaddr
));
256 hcryp
->Instance
->K3RR
= __REV(*(uint32_t*)(keyaddr
));
264 * @brief Writes the InitVector/InitCounter in IV registers.
265 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
266 * the configuration information for CRYP module
267 * @param InitVector: Pointer to InitVector/InitCounter buffer
268 * @param IVSize: Size of the InitVector/InitCounter
271 static void CRYP_SetInitVector(CRYP_HandleTypeDef
*hcryp
, uint8_t *InitVector
, uint32_t IVSize
)
273 uint32_t ivaddr
= (uint32_t)InitVector
;
277 case CRYP_KEYSIZE_128B
:
278 hcryp
->Instance
->IV0LR
= __REV(*(uint32_t*)(ivaddr
));
280 hcryp
->Instance
->IV0RR
= __REV(*(uint32_t*)(ivaddr
));
282 hcryp
->Instance
->IV1LR
= __REV(*(uint32_t*)(ivaddr
));
284 hcryp
->Instance
->IV1RR
= __REV(*(uint32_t*)(ivaddr
));
286 /* Whatever key size 192 or 256, Init vector is written in IV0LR and IV0RR */
287 case CRYP_KEYSIZE_192B
:
288 hcryp
->Instance
->IV0LR
= __REV(*(uint32_t*)(ivaddr
));
290 hcryp
->Instance
->IV0RR
= __REV(*(uint32_t*)(ivaddr
));
292 case CRYP_KEYSIZE_256B
:
293 hcryp
->Instance
->IV0LR
= __REV(*(uint32_t*)(ivaddr
));
295 hcryp
->Instance
->IV0RR
= __REV(*(uint32_t*)(ivaddr
));
303 * @brief Process Data: Writes Input data in polling mode and read the output data
304 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
305 * the configuration information for CRYP module
306 * @param Input: Pointer to the Input buffer
307 * @param Ilength: Length of the Input buffer, must be a multiple of 16.
308 * @param Output: Pointer to the returned buffer
309 * @param Timeout: Timeout value
312 static HAL_StatusTypeDef
CRYP_ProcessData(CRYP_HandleTypeDef
*hcryp
, uint8_t* Input
, uint16_t Ilength
, uint8_t* Output
, uint32_t Timeout
)
314 uint32_t tickstart
= 0;
317 uint32_t inputaddr
= (uint32_t)Input
;
318 uint32_t outputaddr
= (uint32_t)Output
;
320 for(i
=0; (i
< Ilength
); i
+=16)
322 /* Write the Input block in the IN FIFO */
323 hcryp
->Instance
->DR
= *(uint32_t*)(inputaddr
);
325 hcryp
->Instance
->DR
= *(uint32_t*)(inputaddr
);
327 hcryp
->Instance
->DR
= *(uint32_t*)(inputaddr
);
329 hcryp
->Instance
->DR
= *(uint32_t*)(inputaddr
);
333 tickstart
= HAL_GetTick();
335 while(HAL_IS_BIT_CLR(hcryp
->Instance
->SR
, CRYP_FLAG_OFNE
))
337 /* Check for the Timeout */
338 if(Timeout
!= HAL_MAX_DELAY
)
340 if((Timeout
== 0)||((HAL_GetTick() - tickstart
) > Timeout
))
343 hcryp
->State
= HAL_CRYP_STATE_TIMEOUT
;
345 /* Process Unlocked */
352 /* Read the Output block from the Output FIFO */
353 *(uint32_t*)(outputaddr
) = hcryp
->Instance
->DOUT
;
355 *(uint32_t*)(outputaddr
) = hcryp
->Instance
->DOUT
;
357 *(uint32_t*)(outputaddr
) = hcryp
->Instance
->DOUT
;
359 *(uint32_t*)(outputaddr
) = hcryp
->Instance
->DOUT
;
362 /* Return function status */
367 * @brief Process Data: Write Input data in polling mode.
368 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
369 * the configuration information for CRYP module
370 * @param Input: Pointer to the Input buffer
371 * @param Ilength: Length of the Input buffer, must be a multiple of 8
372 * @param Output: Pointer to the returned buffer
373 * @param Timeout: Specify Timeout value
376 static HAL_StatusTypeDef
CRYP_ProcessData2Words(CRYP_HandleTypeDef
*hcryp
, uint8_t* Input
, uint16_t Ilength
, uint8_t* Output
, uint32_t Timeout
)
378 uint32_t tickstart
= 0;
381 uint32_t inputaddr
= (uint32_t)Input
;
382 uint32_t outputaddr
= (uint32_t)Output
;
384 for(i
=0; (i
< Ilength
); i
+=8)
386 /* Write the Input block in the IN FIFO */
387 hcryp
->Instance
->DR
= *(uint32_t*)(inputaddr
);
389 hcryp
->Instance
->DR
= *(uint32_t*)(inputaddr
);
393 tickstart
= HAL_GetTick();
395 while(HAL_IS_BIT_CLR(hcryp
->Instance
->SR
, CRYP_FLAG_OFNE
))
397 /* Check for the Timeout */
398 if(Timeout
!= HAL_MAX_DELAY
)
400 if((Timeout
== 0)||((HAL_GetTick() - tickstart
) > Timeout
))
403 hcryp
->State
= HAL_CRYP_STATE_TIMEOUT
;
405 /* Process Unlocked */
412 /* Read the Output block from the Output FIFO */
413 *(uint32_t*)(outputaddr
) = hcryp
->Instance
->DOUT
;
415 *(uint32_t*)(outputaddr
) = hcryp
->Instance
->DOUT
;
418 /* Return function status */
423 * @brief Set the DMA configuration and start the DMA transfer
424 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
425 * the configuration information for CRYP module
426 * @param inputaddr: address of the Input buffer
427 * @param Size: Size of the Input buffer, must be a multiple of 16.
428 * @param outputaddr: address of the Output buffer
431 static void CRYP_SetDMAConfig(CRYP_HandleTypeDef
*hcryp
, uint32_t inputaddr
, uint16_t Size
, uint32_t outputaddr
)
433 /* Set the CRYP DMA transfer complete callback */
434 hcryp
->hdmain
->XferCpltCallback
= CRYP_DMAInCplt
;
435 /* Set the DMA error callback */
436 hcryp
->hdmain
->XferErrorCallback
= CRYP_DMAError
;
438 /* Set the CRYP DMA transfer complete callback */
439 hcryp
->hdmaout
->XferCpltCallback
= CRYP_DMAOutCplt
;
440 /* Set the DMA error callback */
441 hcryp
->hdmaout
->XferErrorCallback
= CRYP_DMAError
;
444 __HAL_CRYP_ENABLE(hcryp
);
446 /* Enable the DMA In DMA Stream */
447 HAL_DMA_Start_IT(hcryp
->hdmain
, inputaddr
, (uint32_t)&hcryp
->Instance
->DR
, Size
/4);
449 /* Enable In DMA request */
450 hcryp
->Instance
->DMACR
= (CRYP_DMACR_DIEN
);
452 /* Enable the DMA Out DMA Stream */
453 HAL_DMA_Start_IT(hcryp
->hdmaout
, (uint32_t)&hcryp
->Instance
->DOUT
, outputaddr
, Size
/4);
455 /* Enable Out DMA request */
456 hcryp
->Instance
->DMACR
|= CRYP_DMACR_DOEN
;
461 * @brief Sets the CRYP peripheral in DES ECB mode.
462 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
463 * the configuration information for CRYP module
464 * @param Direction: Encryption or decryption
467 static void CRYP_SetDESECBMode(CRYP_HandleTypeDef
*hcryp
, uint32_t Direction
)
469 /* Check if initialization phase has already been performed */
470 if(hcryp
->Phase
== HAL_CRYP_PHASE_READY
)
472 /* Set the CRYP peripheral in AES ECB mode */
473 __HAL_CRYP_SET_MODE(hcryp
, CRYP_CR_ALGOMODE_DES_ECB
| Direction
);
476 hcryp
->Instance
->K1LR
= __REV(*(uint32_t*)(hcryp
->Init
.pKey
));
477 hcryp
->Instance
->K1RR
= __REV(*(uint32_t*)(hcryp
->Init
.pKey
+4));
480 __HAL_CRYP_FIFO_FLUSH(hcryp
);
483 hcryp
->Phase
= HAL_CRYP_PHASE_PROCESS
;
488 * @brief Sets the CRYP peripheral in DES CBC mode.
489 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
490 * the configuration information for CRYP module
491 * @param Direction: Encryption or decryption
494 static void CRYP_SetDESCBCMode(CRYP_HandleTypeDef
*hcryp
, uint32_t Direction
)
496 /* Check if initialization phase has already been performed */
497 if(hcryp
->Phase
== HAL_CRYP_PHASE_READY
)
499 /* Set the CRYP peripheral in AES ECB mode */
500 __HAL_CRYP_SET_MODE(hcryp
, CRYP_CR_ALGOMODE_DES_CBC
| Direction
);
503 hcryp
->Instance
->K1LR
= __REV(*(uint32_t*)(hcryp
->Init
.pKey
));
504 hcryp
->Instance
->K1RR
= __REV(*(uint32_t*)(hcryp
->Init
.pKey
+4));
506 /* Set the Initialization Vector */
507 CRYP_SetInitVector(hcryp
, hcryp
->Init
.pInitVect
, CRYP_KEYSIZE_256B
);
510 __HAL_CRYP_FIFO_FLUSH(hcryp
);
513 hcryp
->Phase
= HAL_CRYP_PHASE_PROCESS
;
518 * @brief Sets the CRYP peripheral in TDES ECB mode.
519 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
520 * the configuration information for CRYP module
521 * @param Direction: Encryption or decryption
524 static void CRYP_SetTDESECBMode(CRYP_HandleTypeDef
*hcryp
, uint32_t Direction
)
526 /* Check if initialization phase has already been performed */
527 if(hcryp
->Phase
== HAL_CRYP_PHASE_READY
)
529 /* Set the CRYP peripheral in AES ECB mode */
530 __HAL_CRYP_SET_MODE(hcryp
, CRYP_CR_ALGOMODE_TDES_ECB
| Direction
);
533 CRYP_SetKey(hcryp
, hcryp
->Init
.pKey
, CRYP_KEYSIZE_192B
);
536 __HAL_CRYP_FIFO_FLUSH(hcryp
);
539 hcryp
->Phase
= HAL_CRYP_PHASE_PROCESS
;
544 * @brief Sets the CRYP peripheral in TDES CBC mode
545 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
546 * the configuration information for CRYP module
547 * @param Direction: Encryption or decryption
550 static void CRYP_SetTDESCBCMode(CRYP_HandleTypeDef
*hcryp
, uint32_t Direction
)
552 /* Check if initialization phase has already been performed */
553 if(hcryp
->Phase
== HAL_CRYP_PHASE_READY
)
555 /* Set the CRYP peripheral in AES CBC mode */
556 __HAL_CRYP_SET_MODE(hcryp
, CRYP_CR_ALGOMODE_TDES_CBC
| Direction
);
559 CRYP_SetKey(hcryp
, hcryp
->Init
.pKey
, CRYP_KEYSIZE_192B
);
561 /* Set the Initialization Vector */
562 CRYP_SetInitVector(hcryp
, hcryp
->Init
.pInitVect
, CRYP_KEYSIZE_256B
);
565 __HAL_CRYP_FIFO_FLUSH(hcryp
);
568 hcryp
->Phase
= HAL_CRYP_PHASE_PROCESS
;
576 /* Exported functions --------------------------------------------------------*/
577 /** @addtogroup CRYP_Exported_Functions
581 /** @defgroup CRYP_Exported_Functions_Group1 Initialization and de-initialization functions
582 * @brief Initialization and Configuration functions.
585 ==============================================================================
586 ##### Initialization and de-initialization functions #####
587 ==============================================================================
588 [..] This section provides functions allowing to:
589 (+) Initialize the CRYP according to the specified parameters
590 in the CRYP_InitTypeDef and creates the associated handle
591 (+) DeInitialize the CRYP peripheral
592 (+) Initialize the CRYP MSP
593 (+) DeInitialize CRYP MSP
600 * @brief Initializes the CRYP according to the specified
601 * parameters in the CRYP_InitTypeDef and creates the associated handle.
602 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
603 * the configuration information for CRYP module
606 HAL_StatusTypeDef
HAL_CRYP_Init(CRYP_HandleTypeDef
*hcryp
)
608 /* Check the CRYP handle allocation */
614 /* Check the parameters */
615 assert_param(IS_CRYP_KEYSIZE(hcryp
->Init
.KeySize
));
616 assert_param(IS_CRYP_DATATYPE(hcryp
->Init
.DataType
));
618 if(hcryp
->State
== HAL_CRYP_STATE_RESET
)
620 /* Allocate lock resource and initialize it */
621 hcryp
->Lock
= HAL_UNLOCKED
;
622 /* Init the low level hardware */
623 HAL_CRYP_MspInit(hcryp
);
626 /* Change the CRYP state */
627 hcryp
->State
= HAL_CRYP_STATE_BUSY
;
629 /* Set the key size and data type*/
630 CRYP
->CR
= (uint32_t) (hcryp
->Init
.KeySize
| hcryp
->Init
.DataType
);
632 /* Reset CrypInCount and CrypOutCount */
633 hcryp
->CrypInCount
= 0;
634 hcryp
->CrypOutCount
= 0;
636 /* Change the CRYP state */
637 hcryp
->State
= HAL_CRYP_STATE_READY
;
639 /* Set the default CRYP phase */
640 hcryp
->Phase
= HAL_CRYP_PHASE_READY
;
642 /* Return function status */
647 * @brief DeInitializes the CRYP peripheral.
648 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
649 * the configuration information for CRYP module
652 HAL_StatusTypeDef
HAL_CRYP_DeInit(CRYP_HandleTypeDef
*hcryp
)
654 /* Check the CRYP handle allocation */
660 /* Change the CRYP state */
661 hcryp
->State
= HAL_CRYP_STATE_BUSY
;
663 /* Set the default CRYP phase */
664 hcryp
->Phase
= HAL_CRYP_PHASE_READY
;
666 /* Reset CrypInCount and CrypOutCount */
667 hcryp
->CrypInCount
= 0;
668 hcryp
->CrypOutCount
= 0;
670 /* Disable the CRYP Peripheral Clock */
671 __HAL_CRYP_DISABLE(hcryp
);
673 /* DeInit the low level hardware: CLOCK, NVIC.*/
674 HAL_CRYP_MspDeInit(hcryp
);
676 /* Change the CRYP state */
677 hcryp
->State
= HAL_CRYP_STATE_RESET
;
682 /* Return function status */
687 * @brief Initializes the CRYP MSP.
688 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
689 * the configuration information for CRYP module
692 __weak
void HAL_CRYP_MspInit(CRYP_HandleTypeDef
*hcryp
)
694 /* Prevent unused argument(s) compilation warning */
697 /* NOTE : This function Should not be modified, when the callback is needed,
698 the HAL_CRYP_MspInit could be implemented in the user file
703 * @brief DeInitializes CRYP MSP.
704 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
705 * the configuration information for CRYP module
708 __weak
void HAL_CRYP_MspDeInit(CRYP_HandleTypeDef
*hcryp
)
710 /* Prevent unused argument(s) compilation warning */
713 /* NOTE : This function Should not be modified, when the callback is needed,
714 the HAL_CRYP_MspDeInit could be implemented in the user file
722 /** @defgroup CRYP_Exported_Functions_Group2 AES processing functions
723 * @brief processing functions.
726 ==============================================================================
727 ##### AES processing functions #####
728 ==============================================================================
729 [..] This section provides functions allowing to:
730 (+) Encrypt plaintext using AES-128/192/256 using chaining modes
731 (+) Decrypt cyphertext using AES-128/192/256 using chaining modes
732 [..] Three processing functions are available:
742 * @brief Initializes the CRYP peripheral in AES ECB encryption mode
743 * then encrypt pPlainData. The cypher data are available in pCypherData
744 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
745 * the configuration information for CRYP module
746 * @param pPlainData: Pointer to the plaintext buffer
747 * @param Size: Length of the plaintext buffer, must be a multiple of 16.
748 * @param pCypherData: Pointer to the cyphertext buffer
749 * @param Timeout: Specify Timeout value
752 HAL_StatusTypeDef
HAL_CRYP_AESECB_Encrypt(CRYP_HandleTypeDef
*hcryp
, uint8_t *pPlainData
, uint16_t Size
, uint8_t *pCypherData
, uint32_t Timeout
)
757 /* Change the CRYP state */
758 hcryp
->State
= HAL_CRYP_STATE_BUSY
;
760 /* Check if initialization phase has already been performed */
761 if(hcryp
->Phase
== HAL_CRYP_PHASE_READY
)
764 CRYP_SetKey(hcryp
, hcryp
->Init
.pKey
, hcryp
->Init
.KeySize
);
766 /* Set the CRYP peripheral in AES ECB mode */
767 __HAL_CRYP_SET_MODE(hcryp
, CRYP_CR_ALGOMODE_AES_ECB
);
770 __HAL_CRYP_FIFO_FLUSH(hcryp
);
773 __HAL_CRYP_ENABLE(hcryp
);
776 hcryp
->Phase
= HAL_CRYP_PHASE_PROCESS
;
779 /* Write Plain Data and Get Cypher Data */
780 if(CRYP_ProcessData(hcryp
, pPlainData
, Size
, pCypherData
, Timeout
) != HAL_OK
)
785 /* Change the CRYP state */
786 hcryp
->State
= HAL_CRYP_STATE_READY
;
788 /* Process Unlocked */
791 /* Return function status */
796 * @brief Initializes the CRYP peripheral in AES CBC encryption mode
797 * then encrypt pPlainData. The cypher data are available in pCypherData
798 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
799 * the configuration information for CRYP module
800 * @param pPlainData: Pointer to the plaintext buffer
801 * @param Size: Length of the plaintext buffer, must be a multiple of 16.
802 * @param pCypherData: Pointer to the cyphertext buffer
803 * @param Timeout: Specify Timeout value
806 HAL_StatusTypeDef
HAL_CRYP_AESCBC_Encrypt(CRYP_HandleTypeDef
*hcryp
, uint8_t *pPlainData
, uint16_t Size
, uint8_t *pCypherData
, uint32_t Timeout
)
811 /* Change the CRYP state */
812 hcryp
->State
= HAL_CRYP_STATE_BUSY
;
814 /* Check if initialization phase has already been performed */
815 if(hcryp
->Phase
== HAL_CRYP_PHASE_READY
)
818 CRYP_SetKey(hcryp
, hcryp
->Init
.pKey
, hcryp
->Init
.KeySize
);
820 /* Set the CRYP peripheral in AES ECB mode */
821 __HAL_CRYP_SET_MODE(hcryp
, CRYP_CR_ALGOMODE_AES_CBC
);
823 /* Set the Initialization Vector */
824 CRYP_SetInitVector(hcryp
, hcryp
->Init
.pInitVect
, CRYP_KEYSIZE_128B
);
827 __HAL_CRYP_FIFO_FLUSH(hcryp
);
830 __HAL_CRYP_ENABLE(hcryp
);
833 hcryp
->Phase
= HAL_CRYP_PHASE_PROCESS
;
836 /* Write Plain Data and Get Cypher Data */
837 if(CRYP_ProcessData(hcryp
,pPlainData
, Size
, pCypherData
, Timeout
) != HAL_OK
)
842 /* Change the CRYP state */
843 hcryp
->State
= HAL_CRYP_STATE_READY
;
845 /* Process Unlocked */
848 /* Return function status */
853 * @brief Initializes the CRYP peripheral in AES CTR encryption mode
854 * then encrypt pPlainData. The cypher data are available in pCypherData
855 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
856 * the configuration information for CRYP module
857 * @param pPlainData: Pointer to the plaintext buffer
858 * @param Size: Length of the plaintext buffer, must be a multiple of 16.
859 * @param pCypherData: Pointer to the cyphertext buffer
860 * @param Timeout: Specify Timeout value
863 HAL_StatusTypeDef
HAL_CRYP_AESCTR_Encrypt(CRYP_HandleTypeDef
*hcryp
, uint8_t *pPlainData
, uint16_t Size
, uint8_t *pCypherData
, uint32_t Timeout
)
868 /* Change the CRYP state */
869 hcryp
->State
= HAL_CRYP_STATE_BUSY
;
871 /* Check if initialization phase has already been performed */
872 if(hcryp
->Phase
== HAL_CRYP_PHASE_READY
)
875 CRYP_SetKey(hcryp
, hcryp
->Init
.pKey
, hcryp
->Init
.KeySize
);
877 /* Set the CRYP peripheral in AES ECB mode */
878 __HAL_CRYP_SET_MODE(hcryp
, CRYP_CR_ALGOMODE_AES_CTR
);
880 /* Set the Initialization Vector */
881 CRYP_SetInitVector(hcryp
, hcryp
->Init
.pInitVect
, CRYP_KEYSIZE_128B
);
884 __HAL_CRYP_FIFO_FLUSH(hcryp
);
887 __HAL_CRYP_ENABLE(hcryp
);
890 hcryp
->Phase
= HAL_CRYP_PHASE_PROCESS
;
893 /* Write Plain Data and Get Cypher Data */
894 if(CRYP_ProcessData(hcryp
, pPlainData
, Size
, pCypherData
, Timeout
) != HAL_OK
)
899 /* Change the CRYP state */
900 hcryp
->State
= HAL_CRYP_STATE_READY
;
902 /* Process Unlocked */
905 /* Return function status */
912 * @brief Initializes the CRYP peripheral in AES ECB decryption mode
913 * then decrypted pCypherData. The cypher data are available in pPlainData
914 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
915 * the configuration information for CRYP module
916 * @param pCypherData: Pointer to the cyphertext buffer
917 * @param Size: Length of the plaintext buffer, must be a multiple of 16.
918 * @param pPlainData: Pointer to the plaintext buffer
919 * @param Timeout: Specify Timeout value
922 HAL_StatusTypeDef
HAL_CRYP_AESECB_Decrypt(CRYP_HandleTypeDef
*hcryp
, uint8_t *pCypherData
, uint16_t Size
, uint8_t *pPlainData
, uint32_t Timeout
)
924 uint32_t tickstart
= 0;
929 /* Change the CRYP state */
930 hcryp
->State
= HAL_CRYP_STATE_BUSY
;
932 /* Check if initialization phase has already been performed */
933 if(hcryp
->Phase
== HAL_CRYP_PHASE_READY
)
936 CRYP_SetKey(hcryp
, hcryp
->Init
.pKey
, hcryp
->Init
.KeySize
);
938 /* Set the CRYP peripheral in AES Key mode */
939 __HAL_CRYP_SET_MODE(hcryp
, CRYP_CR_ALGOMODE_AES_KEY
| CRYP_CR_ALGODIR
);
942 __HAL_CRYP_ENABLE(hcryp
);
945 tickstart
= HAL_GetTick();
947 while(HAL_IS_BIT_SET(hcryp
->Instance
->SR
, CRYP_FLAG_BUSY
))
949 /* Check for the Timeout */
950 if(Timeout
!= HAL_MAX_DELAY
)
952 if((Timeout
== 0)||((HAL_GetTick() - tickstart
) > Timeout
))
955 hcryp
->State
= HAL_CRYP_STATE_TIMEOUT
;
957 /* Process Unlocked */
966 __HAL_CRYP_DISABLE(hcryp
);
968 /* Reset the ALGOMODE bits*/
969 CRYP
->CR
&= (uint32_t)(~CRYP_CR_ALGOMODE
);
971 /* Set the CRYP peripheral in AES ECB decryption mode */
972 __HAL_CRYP_SET_MODE(hcryp
, CRYP_CR_ALGOMODE_AES_ECB
| CRYP_CR_ALGODIR
);
974 __HAL_CRYP_FIFO_FLUSH(hcryp
);
977 __HAL_CRYP_ENABLE(hcryp
);
980 hcryp
->Phase
= HAL_CRYP_PHASE_PROCESS
;
983 /* Write Plain Data and Get Cypher Data */
984 if(CRYP_ProcessData(hcryp
, pCypherData
, Size
, pPlainData
, Timeout
) != HAL_OK
)
989 /* Change the CRYP state */
990 hcryp
->State
= HAL_CRYP_STATE_READY
;
992 /* Process Unlocked */
995 /* Return function status */
1000 * @brief Initializes the CRYP peripheral in AES ECB decryption mode
1001 * then decrypted pCypherData. The cypher data are available in pPlainData
1002 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
1003 * the configuration information for CRYP module
1004 * @param pCypherData: Pointer to the cyphertext buffer
1005 * @param Size: Length of the plaintext buffer, must be a multiple of 16.
1006 * @param pPlainData: Pointer to the plaintext buffer
1007 * @param Timeout: Specify Timeout value
1008 * @retval HAL status
1010 HAL_StatusTypeDef
HAL_CRYP_AESCBC_Decrypt(CRYP_HandleTypeDef
*hcryp
, uint8_t *pCypherData
, uint16_t Size
, uint8_t *pPlainData
, uint32_t Timeout
)
1012 uint32_t tickstart
= 0;
1014 /* Process Locked */
1017 /* Change the CRYP state */
1018 hcryp
->State
= HAL_CRYP_STATE_BUSY
;
1020 /* Check if initialization phase has already been performed */
1021 if(hcryp
->Phase
== HAL_CRYP_PHASE_READY
)
1024 CRYP_SetKey(hcryp
, hcryp
->Init
.pKey
, hcryp
->Init
.KeySize
);
1026 /* Set the CRYP peripheral in AES Key mode */
1027 __HAL_CRYP_SET_MODE(hcryp
, CRYP_CR_ALGOMODE_AES_KEY
| CRYP_CR_ALGODIR
);
1030 __HAL_CRYP_ENABLE(hcryp
);
1033 tickstart
= HAL_GetTick();
1035 while(HAL_IS_BIT_SET(hcryp
->Instance
->SR
, CRYP_FLAG_BUSY
))
1037 /* Check for the Timeout */
1038 if(Timeout
!= HAL_MAX_DELAY
)
1040 if((Timeout
== 0)||((HAL_GetTick() - tickstart
) > Timeout
))
1043 hcryp
->State
= HAL_CRYP_STATE_TIMEOUT
;
1045 /* Process Unlocked */
1046 __HAL_UNLOCK(hcryp
);
1053 /* Reset the ALGOMODE bits*/
1054 CRYP
->CR
&= (uint32_t)(~CRYP_CR_ALGOMODE
);
1056 /* Set the CRYP peripheral in AES CBC decryption mode */
1057 __HAL_CRYP_SET_MODE(hcryp
, CRYP_CR_ALGOMODE_AES_CBC
| CRYP_CR_ALGODIR
);
1059 /* Set the Initialization Vector */
1060 CRYP_SetInitVector(hcryp
, hcryp
->Init
.pInitVect
, CRYP_KEYSIZE_128B
);
1063 __HAL_CRYP_FIFO_FLUSH(hcryp
);
1066 __HAL_CRYP_ENABLE(hcryp
);
1069 hcryp
->Phase
= HAL_CRYP_PHASE_PROCESS
;
1072 /* Write Plain Data and Get Cypher Data */
1073 if(CRYP_ProcessData(hcryp
, pCypherData
, Size
, pPlainData
, Timeout
) != HAL_OK
)
1078 /* Change the CRYP state */
1079 hcryp
->State
= HAL_CRYP_STATE_READY
;
1081 /* Process Unlocked */
1082 __HAL_UNLOCK(hcryp
);
1084 /* Return function status */
1089 * @brief Initializes the CRYP peripheral in AES CTR decryption mode
1090 * then decrypted pCypherData. The cypher data are available in pPlainData
1091 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
1092 * the configuration information for CRYP module
1093 * @param pCypherData: Pointer to the cyphertext buffer
1094 * @param Size: Length of the plaintext buffer, must be a multiple of 16.
1095 * @param pPlainData: Pointer to the plaintext buffer
1096 * @param Timeout: Specify Timeout value
1097 * @retval HAL status
1099 HAL_StatusTypeDef
HAL_CRYP_AESCTR_Decrypt(CRYP_HandleTypeDef
*hcryp
, uint8_t *pCypherData
, uint16_t Size
, uint8_t *pPlainData
, uint32_t Timeout
)
1101 /* Process Locked */
1104 /* Check if initialization phase has already been performed */
1105 if(hcryp
->Phase
== HAL_CRYP_PHASE_READY
)
1107 /* Change the CRYP state */
1108 hcryp
->State
= HAL_CRYP_STATE_BUSY
;
1111 CRYP_SetKey(hcryp
, hcryp
->Init
.pKey
, hcryp
->Init
.KeySize
);
1113 /* Set the CRYP peripheral in AES CTR mode */
1114 __HAL_CRYP_SET_MODE(hcryp
, CRYP_CR_ALGOMODE_AES_CTR
| CRYP_CR_ALGODIR
);
1116 /* Set the Initialization Vector */
1117 CRYP_SetInitVector(hcryp
, hcryp
->Init
.pInitVect
, CRYP_KEYSIZE_128B
);
1120 __HAL_CRYP_FIFO_FLUSH(hcryp
);
1123 __HAL_CRYP_ENABLE(hcryp
);
1126 hcryp
->Phase
= HAL_CRYP_PHASE_PROCESS
;
1129 /* Write Plain Data and Get Cypher Data */
1130 if(CRYP_ProcessData(hcryp
, pCypherData
, Size
, pPlainData
, Timeout
) != HAL_OK
)
1135 /* Change the CRYP state */
1136 hcryp
->State
= HAL_CRYP_STATE_READY
;
1138 /* Process Unlocked */
1139 __HAL_UNLOCK(hcryp
);
1141 /* Return function status */
1146 * @brief Initializes the CRYP peripheral in AES ECB encryption mode using Interrupt.
1147 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
1148 * the configuration information for CRYP module
1149 * @param pPlainData: Pointer to the plaintext buffer
1150 * @param Size: Length of the plaintext buffer, must be a multiple of 16 bytes
1151 * @param pCypherData: Pointer to the cyphertext buffer
1152 * @retval HAL status
1154 HAL_StatusTypeDef
HAL_CRYP_AESECB_Encrypt_IT(CRYP_HandleTypeDef
*hcryp
, uint8_t *pPlainData
, uint16_t Size
, uint8_t *pCypherData
)
1157 uint32_t outputaddr
;
1159 if(hcryp
->State
== HAL_CRYP_STATE_READY
)
1161 /* Process Locked */
1164 hcryp
->CrypInCount
= Size
;
1165 hcryp
->pCrypInBuffPtr
= pPlainData
;
1166 hcryp
->pCrypOutBuffPtr
= pCypherData
;
1167 hcryp
->CrypOutCount
= Size
;
1169 /* Change the CRYP state */
1170 hcryp
->State
= HAL_CRYP_STATE_BUSY
;
1172 /* Check if initialization phase has already been performed */
1173 if(hcryp
->Phase
== HAL_CRYP_PHASE_READY
)
1176 CRYP_SetKey(hcryp
, hcryp
->Init
.pKey
, hcryp
->Init
.KeySize
);
1178 /* Set the CRYP peripheral in AES ECB mode */
1179 __HAL_CRYP_SET_MODE(hcryp
, CRYP_CR_ALGOMODE_AES_ECB
);
1182 __HAL_CRYP_FIFO_FLUSH(hcryp
);
1185 hcryp
->Phase
= HAL_CRYP_PHASE_PROCESS
;
1188 /* Enable Interrupts */
1189 __HAL_CRYP_ENABLE_IT(hcryp
, CRYP_IT_INI
| CRYP_IT_OUTI
);
1192 __HAL_CRYP_ENABLE(hcryp
);
1194 /* Return function status */
1197 else if(__HAL_CRYP_GET_IT(hcryp
, CRYP_IT_INI
))
1199 inputaddr
= (uint32_t)hcryp
->pCrypInBuffPtr
;
1200 /* Write the Input block in the IN FIFO */
1201 hcryp
->Instance
->DR
= *(uint32_t*)(inputaddr
);
1203 hcryp
->Instance
->DR
= *(uint32_t*)(inputaddr
);
1205 hcryp
->Instance
->DR
= *(uint32_t*)(inputaddr
);
1207 hcryp
->Instance
->DR
= *(uint32_t*)(inputaddr
);
1208 hcryp
->pCrypInBuffPtr
+= 16;
1209 hcryp
->CrypInCount
-= 16;
1210 if(hcryp
->CrypInCount
== 0)
1212 __HAL_CRYP_DISABLE_IT(hcryp
, CRYP_IT_INI
);
1213 /* Call the Input data transfer complete callback */
1214 HAL_CRYP_InCpltCallback(hcryp
);
1217 else if(__HAL_CRYP_GET_IT(hcryp
, CRYP_IT_OUTI
))
1219 outputaddr
= (uint32_t)hcryp
->pCrypOutBuffPtr
;
1220 /* Read the Output block from the Output FIFO */
1221 *(uint32_t*)(outputaddr
) = hcryp
->Instance
->DOUT
;
1223 *(uint32_t*)(outputaddr
) = hcryp
->Instance
->DOUT
;
1225 *(uint32_t*)(outputaddr
) = hcryp
->Instance
->DOUT
;
1227 *(uint32_t*)(outputaddr
) = hcryp
->Instance
->DOUT
;
1228 hcryp
->pCrypOutBuffPtr
+= 16;
1229 hcryp
->CrypOutCount
-= 16;
1230 if(hcryp
->CrypOutCount
== 0)
1232 __HAL_CRYP_DISABLE_IT(hcryp
, CRYP_IT_OUTI
);
1233 /* Process Locked */
1234 __HAL_UNLOCK(hcryp
);
1235 /* Change the CRYP state */
1236 hcryp
->State
= HAL_CRYP_STATE_READY
;
1237 /* Call Input transfer complete callback */
1238 HAL_CRYP_OutCpltCallback(hcryp
);
1242 /* Return function status */
1247 * @brief Initializes the CRYP peripheral in AES CBC encryption mode using Interrupt.
1248 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
1249 * the configuration information for CRYP module
1250 * @param pPlainData: Pointer to the plaintext buffer
1251 * @param Size: Length of the plaintext buffer, must be a multiple of 16 bytes
1252 * @param pCypherData: Pointer to the cyphertext buffer
1253 * @retval HAL status
1255 HAL_StatusTypeDef
HAL_CRYP_AESCBC_Encrypt_IT(CRYP_HandleTypeDef
*hcryp
, uint8_t *pPlainData
, uint16_t Size
, uint8_t *pCypherData
)
1258 uint32_t outputaddr
;
1260 if(hcryp
->State
== HAL_CRYP_STATE_READY
)
1262 /* Process Locked */
1265 hcryp
->CrypInCount
= Size
;
1266 hcryp
->pCrypInBuffPtr
= pPlainData
;
1267 hcryp
->pCrypOutBuffPtr
= pCypherData
;
1268 hcryp
->CrypOutCount
= Size
;
1270 /* Change the CRYP state */
1271 hcryp
->State
= HAL_CRYP_STATE_BUSY
;
1273 /* Check if initialization phase has already been performed */
1274 if(hcryp
->Phase
== HAL_CRYP_PHASE_READY
)
1277 CRYP_SetKey(hcryp
, hcryp
->Init
.pKey
, hcryp
->Init
.KeySize
);
1279 /* Set the CRYP peripheral in AES CBC mode */
1280 __HAL_CRYP_SET_MODE(hcryp
, CRYP_CR_ALGOMODE_AES_CBC
);
1282 /* Set the Initialization Vector */
1283 CRYP_SetInitVector(hcryp
, hcryp
->Init
.pInitVect
, CRYP_KEYSIZE_128B
);
1286 __HAL_CRYP_FIFO_FLUSH(hcryp
);
1289 hcryp
->Phase
= HAL_CRYP_PHASE_PROCESS
;
1291 /* Enable Interrupts */
1292 __HAL_CRYP_ENABLE_IT(hcryp
, CRYP_IT_INI
| CRYP_IT_OUTI
);
1295 __HAL_CRYP_ENABLE(hcryp
);
1297 /* Return function status */
1300 else if(__HAL_CRYP_GET_IT(hcryp
, CRYP_IT_INI
))
1302 inputaddr
= (uint32_t)hcryp
->pCrypInBuffPtr
;
1303 /* Write the Input block in the IN FIFO */
1304 hcryp
->Instance
->DR
= *(uint32_t*)(inputaddr
);
1306 hcryp
->Instance
->DR
= *(uint32_t*)(inputaddr
);
1308 hcryp
->Instance
->DR
= *(uint32_t*)(inputaddr
);
1310 hcryp
->Instance
->DR
= *(uint32_t*)(inputaddr
);
1311 hcryp
->pCrypInBuffPtr
+= 16;
1312 hcryp
->CrypInCount
-= 16;
1313 if(hcryp
->CrypInCount
== 0)
1315 __HAL_CRYP_DISABLE_IT(hcryp
, CRYP_IT_INI
);
1316 /* Call the Input data transfer complete callback */
1317 HAL_CRYP_InCpltCallback(hcryp
);
1320 else if(__HAL_CRYP_GET_IT(hcryp
, CRYP_IT_OUTI
))
1322 outputaddr
= (uint32_t)hcryp
->pCrypOutBuffPtr
;
1323 /* Read the Output block from the Output FIFO */
1324 *(uint32_t*)(outputaddr
) = hcryp
->Instance
->DOUT
;
1326 *(uint32_t*)(outputaddr
) = hcryp
->Instance
->DOUT
;
1328 *(uint32_t*)(outputaddr
) = hcryp
->Instance
->DOUT
;
1330 *(uint32_t*)(outputaddr
) = hcryp
->Instance
->DOUT
;
1331 hcryp
->pCrypOutBuffPtr
+= 16;
1332 hcryp
->CrypOutCount
-= 16;
1333 if(hcryp
->CrypOutCount
== 0)
1335 __HAL_CRYP_DISABLE_IT(hcryp
, CRYP_IT_OUTI
);
1336 /* Process Locked */
1337 __HAL_UNLOCK(hcryp
);
1338 /* Change the CRYP state */
1339 hcryp
->State
= HAL_CRYP_STATE_READY
;
1340 /* Call Input transfer complete callback */
1341 HAL_CRYP_OutCpltCallback(hcryp
);
1345 /* Return function status */
1350 * @brief Initializes the CRYP peripheral in AES CTR encryption mode using Interrupt.
1351 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
1352 * the configuration information for CRYP module
1353 * @param pPlainData: Pointer to the plaintext buffer
1354 * @param Size: Length of the plaintext buffer, must be a multiple of 16 bytes
1355 * @param pCypherData: Pointer to the cyphertext buffer
1356 * @retval HAL status
1358 HAL_StatusTypeDef
HAL_CRYP_AESCTR_Encrypt_IT(CRYP_HandleTypeDef
*hcryp
, uint8_t *pPlainData
, uint16_t Size
, uint8_t *pCypherData
)
1361 uint32_t outputaddr
;
1363 if(hcryp
->State
== HAL_CRYP_STATE_READY
)
1365 /* Process Locked */
1368 hcryp
->CrypInCount
= Size
;
1369 hcryp
->pCrypInBuffPtr
= pPlainData
;
1370 hcryp
->pCrypOutBuffPtr
= pCypherData
;
1371 hcryp
->CrypOutCount
= Size
;
1373 /* Change the CRYP state */
1374 hcryp
->State
= HAL_CRYP_STATE_BUSY
;
1376 /* Check if initialization phase has already been performed */
1377 if(hcryp
->Phase
== HAL_CRYP_PHASE_READY
)
1380 CRYP_SetKey(hcryp
, hcryp
->Init
.pKey
, hcryp
->Init
.KeySize
);
1382 /* Set the CRYP peripheral in AES CTR mode */
1383 __HAL_CRYP_SET_MODE(hcryp
, CRYP_CR_ALGOMODE_AES_CTR
);
1385 /* Set the Initialization Vector */
1386 CRYP_SetInitVector(hcryp
, hcryp
->Init
.pInitVect
, CRYP_KEYSIZE_128B
);
1389 __HAL_CRYP_FIFO_FLUSH(hcryp
);
1392 hcryp
->Phase
= HAL_CRYP_PHASE_PROCESS
;
1394 /* Enable Interrupts */
1395 __HAL_CRYP_ENABLE_IT(hcryp
, CRYP_IT_INI
| CRYP_IT_OUTI
);
1398 __HAL_CRYP_ENABLE(hcryp
);
1400 /* Return function status */
1403 else if(__HAL_CRYP_GET_IT(hcryp
, CRYP_IT_INI
))
1405 inputaddr
= (uint32_t)hcryp
->pCrypInBuffPtr
;
1406 /* Write the Input block in the IN FIFO */
1407 hcryp
->Instance
->DR
= *(uint32_t*)(inputaddr
);
1409 hcryp
->Instance
->DR
= *(uint32_t*)(inputaddr
);
1411 hcryp
->Instance
->DR
= *(uint32_t*)(inputaddr
);
1413 hcryp
->Instance
->DR
= *(uint32_t*)(inputaddr
);
1414 hcryp
->pCrypInBuffPtr
+= 16;
1415 hcryp
->CrypInCount
-= 16;
1416 if(hcryp
->CrypInCount
== 0)
1418 __HAL_CRYP_DISABLE_IT(hcryp
, CRYP_IT_INI
);
1419 /* Call the Input data transfer complete callback */
1420 HAL_CRYP_InCpltCallback(hcryp
);
1423 else if(__HAL_CRYP_GET_IT(hcryp
, CRYP_IT_OUTI
))
1425 outputaddr
= (uint32_t)hcryp
->pCrypOutBuffPtr
;
1426 /* Read the Output block from the Output FIFO */
1427 *(uint32_t*)(outputaddr
) = hcryp
->Instance
->DOUT
;
1429 *(uint32_t*)(outputaddr
) = hcryp
->Instance
->DOUT
;
1431 *(uint32_t*)(outputaddr
) = hcryp
->Instance
->DOUT
;
1433 *(uint32_t*)(outputaddr
) = hcryp
->Instance
->DOUT
;
1434 hcryp
->pCrypOutBuffPtr
+= 16;
1435 hcryp
->CrypOutCount
-= 16;
1436 if(hcryp
->CrypOutCount
== 0)
1438 __HAL_CRYP_DISABLE_IT(hcryp
, CRYP_IT_OUTI
);
1439 /* Process Unlocked */
1440 __HAL_UNLOCK(hcryp
);
1441 /* Change the CRYP state */
1442 hcryp
->State
= HAL_CRYP_STATE_READY
;
1443 /* Call Input transfer complete callback */
1444 HAL_CRYP_OutCpltCallback(hcryp
);
1448 /* Return function status */
1454 * @brief Initializes the CRYP peripheral in AES ECB decryption mode using Interrupt.
1455 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
1456 * the configuration information for CRYP module
1457 * @param pCypherData: Pointer to the cyphertext buffer
1458 * @param Size: Length of the plaintext buffer, must be a multiple of 16.
1459 * @param pPlainData: Pointer to the plaintext buffer
1460 * @retval HAL status
1462 HAL_StatusTypeDef
HAL_CRYP_AESECB_Decrypt_IT(CRYP_HandleTypeDef
*hcryp
, uint8_t *pCypherData
, uint16_t Size
, uint8_t *pPlainData
)
1464 uint32_t tickstart
= 0;
1467 uint32_t outputaddr
;
1469 if(hcryp
->State
== HAL_CRYP_STATE_READY
)
1471 /* Process Locked */
1474 hcryp
->CrypInCount
= Size
;
1475 hcryp
->pCrypInBuffPtr
= pCypherData
;
1476 hcryp
->pCrypOutBuffPtr
= pPlainData
;
1477 hcryp
->CrypOutCount
= Size
;
1479 /* Change the CRYP state */
1480 hcryp
->State
= HAL_CRYP_STATE_BUSY
;
1482 /* Check if initialization phase has already been performed */
1483 if(hcryp
->Phase
== HAL_CRYP_PHASE_READY
)
1486 CRYP_SetKey(hcryp
, hcryp
->Init
.pKey
, hcryp
->Init
.KeySize
);
1488 /* Set the CRYP peripheral in AES Key mode */
1489 __HAL_CRYP_SET_MODE(hcryp
, CRYP_CR_ALGOMODE_AES_KEY
| CRYP_CR_ALGODIR
);
1491 __HAL_CRYP_ENABLE(hcryp
);
1494 tickstart
= HAL_GetTick();
1496 while(HAL_IS_BIT_SET(hcryp
->Instance
->SR
, CRYP_FLAG_BUSY
))
1498 /* Check for the Timeout */
1499 if((HAL_GetTick() - tickstart
) > CRYP_TIMEOUT_VALUE
)
1502 hcryp
->State
= HAL_CRYP_STATE_TIMEOUT
;
1504 /* Process Unlocked */
1505 __HAL_UNLOCK(hcryp
);
1511 /* Reset the ALGOMODE bits*/
1512 CRYP
->CR
&= (uint32_t)(~CRYP_CR_ALGOMODE
);
1514 /* Set the CRYP peripheral in AES ECB decryption mode */
1515 __HAL_CRYP_SET_MODE(hcryp
, CRYP_CR_ALGOMODE_AES_ECB
| CRYP_CR_ALGODIR
);
1518 __HAL_CRYP_FIFO_FLUSH(hcryp
);
1521 hcryp
->Phase
= HAL_CRYP_PHASE_PROCESS
;
1524 /* Enable Interrupts */
1525 __HAL_CRYP_ENABLE_IT(hcryp
, CRYP_IT_INI
| CRYP_IT_OUTI
);
1528 __HAL_CRYP_ENABLE(hcryp
);
1530 /* Return function status */
1533 else if(__HAL_CRYP_GET_IT(hcryp
, CRYP_IT_INI
))
1535 inputaddr
= (uint32_t)hcryp
->pCrypInBuffPtr
;
1536 /* Write the Input block in the IN FIFO */
1537 hcryp
->Instance
->DR
= *(uint32_t*)(inputaddr
);
1539 hcryp
->Instance
->DR
= *(uint32_t*)(inputaddr
);
1541 hcryp
->Instance
->DR
= *(uint32_t*)(inputaddr
);
1543 hcryp
->Instance
->DR
= *(uint32_t*)(inputaddr
);
1544 hcryp
->pCrypInBuffPtr
+= 16;
1545 hcryp
->CrypInCount
-= 16;
1546 if(hcryp
->CrypInCount
== 0)
1548 __HAL_CRYP_DISABLE_IT(hcryp
, CRYP_IT_INI
);
1549 /* Call the Input data transfer complete callback */
1550 HAL_CRYP_InCpltCallback(hcryp
);
1553 else if(__HAL_CRYP_GET_IT(hcryp
, CRYP_IT_OUTI
))
1555 outputaddr
= (uint32_t)hcryp
->pCrypOutBuffPtr
;
1556 /* Read the Output block from the Output FIFO */
1557 *(uint32_t*)(outputaddr
) = hcryp
->Instance
->DOUT
;
1559 *(uint32_t*)(outputaddr
) = hcryp
->Instance
->DOUT
;
1561 *(uint32_t*)(outputaddr
) = hcryp
->Instance
->DOUT
;
1563 *(uint32_t*)(outputaddr
) = hcryp
->Instance
->DOUT
;
1564 hcryp
->pCrypOutBuffPtr
+= 16;
1565 hcryp
->CrypOutCount
-= 16;
1566 if(hcryp
->CrypOutCount
== 0)
1568 __HAL_CRYP_DISABLE_IT(hcryp
, CRYP_IT_OUTI
);
1569 /* Process Unlocked */
1570 __HAL_UNLOCK(hcryp
);
1571 /* Change the CRYP state */
1572 hcryp
->State
= HAL_CRYP_STATE_READY
;
1573 /* Call Input transfer complete callback */
1574 HAL_CRYP_OutCpltCallback(hcryp
);
1578 /* Return function status */
1583 * @brief Initializes the CRYP peripheral in AES CBC decryption mode using IT.
1584 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
1585 * the configuration information for CRYP module
1586 * @param pCypherData: Pointer to the cyphertext buffer
1587 * @param Size: Length of the plaintext buffer, must be a multiple of 16
1588 * @param pPlainData: Pointer to the plaintext buffer
1589 * @retval HAL status
1591 HAL_StatusTypeDef
HAL_CRYP_AESCBC_Decrypt_IT(CRYP_HandleTypeDef
*hcryp
, uint8_t *pCypherData
, uint16_t Size
, uint8_t *pPlainData
)
1594 uint32_t tickstart
= 0;
1596 uint32_t outputaddr
;
1598 if(hcryp
->State
== HAL_CRYP_STATE_READY
)
1600 /* Process Locked */
1603 /* Get the buffer addresses and sizes */
1604 hcryp
->CrypInCount
= Size
;
1605 hcryp
->pCrypInBuffPtr
= pCypherData
;
1606 hcryp
->pCrypOutBuffPtr
= pPlainData
;
1607 hcryp
->CrypOutCount
= Size
;
1609 /* Change the CRYP state */
1610 hcryp
->State
= HAL_CRYP_STATE_BUSY
;
1612 /* Check if initialization phase has already been performed */
1613 if(hcryp
->Phase
== HAL_CRYP_PHASE_READY
)
1616 CRYP_SetKey(hcryp
, hcryp
->Init
.pKey
, hcryp
->Init
.KeySize
);
1618 /* Set the CRYP peripheral in AES Key mode */
1619 __HAL_CRYP_SET_MODE(hcryp
, CRYP_CR_ALGOMODE_AES_KEY
| CRYP_CR_ALGODIR
);
1622 __HAL_CRYP_ENABLE(hcryp
);
1625 tickstart
= HAL_GetTick();
1627 while(HAL_IS_BIT_SET(hcryp
->Instance
->SR
, CRYP_FLAG_BUSY
))
1629 /* Check for the Timeout */
1630 if((HAL_GetTick() - tickstart
) > CRYP_TIMEOUT_VALUE
)
1633 hcryp
->State
= HAL_CRYP_STATE_TIMEOUT
;
1635 /* Process Unlocked */
1636 __HAL_UNLOCK(hcryp
);
1642 /* Reset the ALGOMODE bits*/
1643 CRYP
->CR
&= (uint32_t)(~CRYP_CR_ALGOMODE
);
1645 /* Set the CRYP peripheral in AES CBC decryption mode */
1646 __HAL_CRYP_SET_MODE(hcryp
, CRYP_CR_ALGOMODE_AES_CBC
| CRYP_CR_ALGODIR
);
1648 /* Set the Initialization Vector */
1649 CRYP_SetInitVector(hcryp
, hcryp
->Init
.pInitVect
, CRYP_KEYSIZE_128B
);
1652 __HAL_CRYP_FIFO_FLUSH(hcryp
);
1655 __HAL_CRYP_ENABLE(hcryp
);
1658 hcryp
->Phase
= HAL_CRYP_PHASE_PROCESS
;
1661 /* Enable Interrupts */
1662 __HAL_CRYP_ENABLE_IT(hcryp
, CRYP_IT_INI
| CRYP_IT_OUTI
);
1665 __HAL_CRYP_ENABLE(hcryp
);
1667 /* Return function status */
1670 else if(__HAL_CRYP_GET_IT(hcryp
, CRYP_IT_INI
))
1672 inputaddr
= (uint32_t)hcryp
->pCrypInBuffPtr
;
1673 /* Write the Input block in the IN FIFO */
1674 hcryp
->Instance
->DR
= *(uint32_t*)(inputaddr
);
1676 hcryp
->Instance
->DR
= *(uint32_t*)(inputaddr
);
1678 hcryp
->Instance
->DR
= *(uint32_t*)(inputaddr
);
1680 hcryp
->Instance
->DR
= *(uint32_t*)(inputaddr
);
1681 hcryp
->pCrypInBuffPtr
+= 16;
1682 hcryp
->CrypInCount
-= 16;
1683 if(hcryp
->CrypInCount
== 0)
1685 __HAL_CRYP_DISABLE_IT(hcryp
, CRYP_IT_INI
);
1686 /* Call the Input data transfer complete callback */
1687 HAL_CRYP_InCpltCallback(hcryp
);
1690 else if(__HAL_CRYP_GET_IT(hcryp
, CRYP_IT_OUTI
))
1692 outputaddr
= (uint32_t)hcryp
->pCrypOutBuffPtr
;
1693 /* Read the Output block from the Output FIFO */
1694 *(uint32_t*)(outputaddr
) = hcryp
->Instance
->DOUT
;
1696 *(uint32_t*)(outputaddr
) = hcryp
->Instance
->DOUT
;
1698 *(uint32_t*)(outputaddr
) = hcryp
->Instance
->DOUT
;
1700 *(uint32_t*)(outputaddr
) = hcryp
->Instance
->DOUT
;
1701 hcryp
->pCrypOutBuffPtr
+= 16;
1702 hcryp
->CrypOutCount
-= 16;
1703 if(hcryp
->CrypOutCount
== 0)
1705 __HAL_CRYP_DISABLE_IT(hcryp
, CRYP_IT_OUTI
);
1706 /* Process Unlocked */
1707 __HAL_UNLOCK(hcryp
);
1708 /* Change the CRYP state */
1709 hcryp
->State
= HAL_CRYP_STATE_READY
;
1710 /* Call Input transfer complete callback */
1711 HAL_CRYP_OutCpltCallback(hcryp
);
1715 /* Return function status */
1720 * @brief Initializes the CRYP peripheral in AES CTR decryption mode using Interrupt.
1721 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
1722 * the configuration information for CRYP module
1723 * @param pCypherData: Pointer to the cyphertext buffer
1724 * @param Size: Length of the plaintext buffer, must be a multiple of 16
1725 * @param pPlainData: Pointer to the plaintext buffer
1726 * @retval HAL status
1728 HAL_StatusTypeDef
HAL_CRYP_AESCTR_Decrypt_IT(CRYP_HandleTypeDef
*hcryp
, uint8_t *pCypherData
, uint16_t Size
, uint8_t *pPlainData
)
1731 uint32_t outputaddr
;
1733 if(hcryp
->State
== HAL_CRYP_STATE_READY
)
1735 /* Process Locked */
1738 /* Get the buffer addresses and sizes */
1739 hcryp
->CrypInCount
= Size
;
1740 hcryp
->pCrypInBuffPtr
= pCypherData
;
1741 hcryp
->pCrypOutBuffPtr
= pPlainData
;
1742 hcryp
->CrypOutCount
= Size
;
1744 /* Change the CRYP state */
1745 hcryp
->State
= HAL_CRYP_STATE_BUSY
;
1747 /* Check if initialization phase has already been performed */
1748 if(hcryp
->Phase
== HAL_CRYP_PHASE_READY
)
1751 CRYP_SetKey(hcryp
, hcryp
->Init
.pKey
, hcryp
->Init
.KeySize
);
1753 /* Set the CRYP peripheral in AES CTR mode */
1754 __HAL_CRYP_SET_MODE(hcryp
, CRYP_CR_ALGOMODE_AES_CTR
| CRYP_CR_ALGODIR
);
1756 /* Set the Initialization Vector */
1757 CRYP_SetInitVector(hcryp
, hcryp
->Init
.pInitVect
, CRYP_KEYSIZE_128B
);
1760 __HAL_CRYP_FIFO_FLUSH(hcryp
);
1763 hcryp
->Phase
= HAL_CRYP_PHASE_PROCESS
;
1766 /* Enable Interrupts */
1767 __HAL_CRYP_ENABLE_IT(hcryp
, CRYP_IT_INI
| CRYP_IT_OUTI
);
1770 __HAL_CRYP_ENABLE(hcryp
);
1772 /* Return function status */
1775 else if(__HAL_CRYP_GET_IT(hcryp
, CRYP_IT_INI
))
1777 inputaddr
= (uint32_t)hcryp
->pCrypInBuffPtr
;
1778 /* Write the Input block in the IN FIFO */
1779 hcryp
->Instance
->DR
= *(uint32_t*)(inputaddr
);
1781 hcryp
->Instance
->DR
= *(uint32_t*)(inputaddr
);
1783 hcryp
->Instance
->DR
= *(uint32_t*)(inputaddr
);
1785 hcryp
->Instance
->DR
= *(uint32_t*)(inputaddr
);
1786 hcryp
->pCrypInBuffPtr
+= 16;
1787 hcryp
->CrypInCount
-= 16;
1788 if(hcryp
->CrypInCount
== 0)
1790 __HAL_CRYP_DISABLE_IT(hcryp
, CRYP_IT_INI
);
1791 /* Call the Input data transfer complete callback */
1792 HAL_CRYP_InCpltCallback(hcryp
);
1795 else if(__HAL_CRYP_GET_IT(hcryp
, CRYP_IT_OUTI
))
1797 outputaddr
= (uint32_t)hcryp
->pCrypOutBuffPtr
;
1798 /* Read the Output block from the Output FIFO */
1799 *(uint32_t*)(outputaddr
) = hcryp
->Instance
->DOUT
;
1801 *(uint32_t*)(outputaddr
) = hcryp
->Instance
->DOUT
;
1803 *(uint32_t*)(outputaddr
) = hcryp
->Instance
->DOUT
;
1805 *(uint32_t*)(outputaddr
) = hcryp
->Instance
->DOUT
;
1806 hcryp
->pCrypOutBuffPtr
+= 16;
1807 hcryp
->CrypOutCount
-= 16;
1808 if(hcryp
->CrypOutCount
== 0)
1810 __HAL_CRYP_DISABLE_IT(hcryp
, CRYP_IT_OUTI
);
1811 /* Process Unlocked */
1812 __HAL_UNLOCK(hcryp
);
1813 /* Change the CRYP state */
1814 hcryp
->State
= HAL_CRYP_STATE_READY
;
1815 /* Call Input transfer complete callback */
1816 HAL_CRYP_OutCpltCallback(hcryp
);
1820 /* Return function status */
1825 * @brief Initializes the CRYP peripheral in AES ECB encryption mode using DMA.
1826 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
1827 * the configuration information for CRYP module
1828 * @param pPlainData: Pointer to the plaintext buffer
1829 * @param Size: Length of the plaintext buffer, must be a multiple of 16 bytes
1830 * @param pCypherData: Pointer to the cyphertext buffer
1831 * @retval HAL status
1833 HAL_StatusTypeDef
HAL_CRYP_AESECB_Encrypt_DMA(CRYP_HandleTypeDef
*hcryp
, uint8_t *pPlainData
, uint16_t Size
, uint8_t *pCypherData
)
1836 uint32_t outputaddr
;
1838 if((hcryp
->State
== HAL_CRYP_STATE_READY
) || (hcryp
->Phase
== HAL_CRYP_PHASE_PROCESS
))
1840 /* Process Locked */
1843 inputaddr
= (uint32_t)pPlainData
;
1844 outputaddr
= (uint32_t)pCypherData
;
1846 /* Change the CRYP state */
1847 hcryp
->State
= HAL_CRYP_STATE_BUSY
;
1849 /* Check if initialization phase has already been performed */
1850 if(hcryp
->Phase
== HAL_CRYP_PHASE_READY
)
1853 CRYP_SetKey(hcryp
, hcryp
->Init
.pKey
, hcryp
->Init
.KeySize
);
1855 /* Set the CRYP peripheral in AES ECB mode */
1856 __HAL_CRYP_SET_MODE(hcryp
, CRYP_CR_ALGOMODE_AES_ECB
);
1859 __HAL_CRYP_FIFO_FLUSH(hcryp
);
1862 hcryp
->Phase
= HAL_CRYP_PHASE_PROCESS
;
1864 /* Set the input and output addresses and start DMA transfer */
1865 CRYP_SetDMAConfig(hcryp
, inputaddr
, Size
, outputaddr
);
1867 /* Process Unlocked */
1868 __HAL_UNLOCK(hcryp
);
1870 /* Return function status */
1880 * @brief Initializes the CRYP peripheral in AES CBC encryption mode using DMA.
1881 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
1882 * the configuration information for CRYP module
1883 * @param pPlainData: Pointer to the plaintext buffer
1884 * @param Size: Length of the plaintext buffer, must be a multiple of 16.
1885 * @param pCypherData: Pointer to the cyphertext buffer
1886 * @retval HAL status
1888 HAL_StatusTypeDef
HAL_CRYP_AESCBC_Encrypt_DMA(CRYP_HandleTypeDef
*hcryp
, uint8_t *pPlainData
, uint16_t Size
, uint8_t *pCypherData
)
1891 uint32_t outputaddr
;
1893 if((hcryp
->State
== HAL_CRYP_STATE_READY
) || (hcryp
->Phase
== HAL_CRYP_PHASE_PROCESS
))
1895 /* Process Locked */
1898 inputaddr
= (uint32_t)pPlainData
;
1899 outputaddr
= (uint32_t)pCypherData
;
1901 /* Change the CRYP state */
1902 hcryp
->State
= HAL_CRYP_STATE_BUSY
;
1904 /* Check if initialization phase has already been performed */
1905 if(hcryp
->Phase
== HAL_CRYP_PHASE_READY
)
1908 CRYP_SetKey(hcryp
, hcryp
->Init
.pKey
, hcryp
->Init
.KeySize
);
1910 /* Set the CRYP peripheral in AES ECB mode */
1911 __HAL_CRYP_SET_MODE(hcryp
, CRYP_CR_ALGOMODE_AES_CBC
);
1913 /* Set the Initialization Vector */
1914 CRYP_SetInitVector(hcryp
, hcryp
->Init
.pInitVect
, CRYP_KEYSIZE_128B
);
1917 __HAL_CRYP_FIFO_FLUSH(hcryp
);
1920 hcryp
->Phase
= HAL_CRYP_PHASE_PROCESS
;
1922 /* Set the input and output addresses and start DMA transfer */
1923 CRYP_SetDMAConfig(hcryp
, inputaddr
, Size
, outputaddr
);
1925 /* Process Unlocked */
1926 __HAL_UNLOCK(hcryp
);
1928 /* Return function status */
1938 * @brief Initializes the CRYP peripheral in AES CTR encryption mode using DMA.
1939 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
1940 * the configuration information for CRYP module
1941 * @param pPlainData: Pointer to the plaintext buffer
1942 * @param Size: Length of the plaintext buffer, must be a multiple of 16.
1943 * @param pCypherData: Pointer to the cyphertext buffer
1944 * @retval HAL status
1946 HAL_StatusTypeDef
HAL_CRYP_AESCTR_Encrypt_DMA(CRYP_HandleTypeDef
*hcryp
, uint8_t *pPlainData
, uint16_t Size
, uint8_t *pCypherData
)
1949 uint32_t outputaddr
;
1951 if((hcryp
->State
== HAL_CRYP_STATE_READY
) || (hcryp
->Phase
== HAL_CRYP_PHASE_PROCESS
))
1953 /* Process Locked */
1956 inputaddr
= (uint32_t)pPlainData
;
1957 outputaddr
= (uint32_t)pCypherData
;
1959 /* Change the CRYP state */
1960 hcryp
->State
= HAL_CRYP_STATE_BUSY
;
1962 /* Check if initialization phase has already been performed */
1963 if(hcryp
->Phase
== HAL_CRYP_PHASE_READY
)
1966 CRYP_SetKey(hcryp
, hcryp
->Init
.pKey
, hcryp
->Init
.KeySize
);
1968 /* Set the CRYP peripheral in AES ECB mode */
1969 __HAL_CRYP_SET_MODE(hcryp
, CRYP_CR_ALGOMODE_AES_CTR
);
1971 /* Set the Initialization Vector */
1972 CRYP_SetInitVector(hcryp
, hcryp
->Init
.pInitVect
, CRYP_KEYSIZE_128B
);
1975 __HAL_CRYP_FIFO_FLUSH(hcryp
);
1978 hcryp
->Phase
= HAL_CRYP_PHASE_PROCESS
;
1981 /* Set the input and output addresses and start DMA transfer */
1982 CRYP_SetDMAConfig(hcryp
, inputaddr
, Size
, outputaddr
);
1984 /* Process Unlocked */
1985 __HAL_UNLOCK(hcryp
);
1987 /* Return function status */
1997 * @brief Initializes the CRYP peripheral in AES ECB decryption mode using DMA.
1998 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
1999 * the configuration information for CRYP module
2000 * @param pCypherData: Pointer to the cyphertext buffer
2001 * @param Size: Length of the plaintext buffer, must be a multiple of 16 bytes
2002 * @param pPlainData: Pointer to the plaintext buffer
2003 * @retval HAL status
2005 HAL_StatusTypeDef
HAL_CRYP_AESECB_Decrypt_DMA(CRYP_HandleTypeDef
*hcryp
, uint8_t *pCypherData
, uint16_t Size
, uint8_t *pPlainData
)
2007 uint32_t tickstart
= 0;
2009 uint32_t outputaddr
;
2011 if((hcryp
->State
== HAL_CRYP_STATE_READY
) || (hcryp
->Phase
== HAL_CRYP_PHASE_PROCESS
))
2013 /* Process Locked */
2016 inputaddr
= (uint32_t)pCypherData
;
2017 outputaddr
= (uint32_t)pPlainData
;
2019 /* Change the CRYP state */
2020 hcryp
->State
= HAL_CRYP_STATE_BUSY
;
2022 /* Check if initialization phase has already been performed */
2023 if(hcryp
->Phase
== HAL_CRYP_PHASE_READY
)
2026 CRYP_SetKey(hcryp
, hcryp
->Init
.pKey
, hcryp
->Init
.KeySize
);
2028 /* Set the CRYP peripheral in AES Key mode */
2029 __HAL_CRYP_SET_MODE(hcryp
, CRYP_CR_ALGOMODE_AES_KEY
| CRYP_CR_ALGODIR
);
2032 __HAL_CRYP_ENABLE(hcryp
);
2035 tickstart
= HAL_GetTick();
2037 while(HAL_IS_BIT_SET(hcryp
->Instance
->SR
, CRYP_FLAG_BUSY
))
2039 /* Check for the Timeout */
2040 if((HAL_GetTick() - tickstart
) > CRYP_TIMEOUT_VALUE
)
2043 hcryp
->State
= HAL_CRYP_STATE_TIMEOUT
;
2045 /* Process Unlocked */
2046 __HAL_UNLOCK(hcryp
);
2052 /* Reset the ALGOMODE bits*/
2053 CRYP
->CR
&= (uint32_t)(~CRYP_CR_ALGOMODE
);
2055 /* Set the CRYP peripheral in AES ECB decryption mode */
2056 __HAL_CRYP_SET_MODE(hcryp
, CRYP_CR_ALGOMODE_AES_ECB
| CRYP_CR_ALGODIR
);
2059 __HAL_CRYP_FIFO_FLUSH(hcryp
);
2062 hcryp
->Phase
= HAL_CRYP_PHASE_PROCESS
;
2065 /* Set the input and output addresses and start DMA transfer */
2066 CRYP_SetDMAConfig(hcryp
, inputaddr
, Size
, outputaddr
);
2068 /* Process Unlocked */
2069 __HAL_UNLOCK(hcryp
);
2071 /* Return function status */
2081 * @brief Initializes the CRYP peripheral in AES CBC encryption mode using DMA.
2082 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
2083 * the configuration information for CRYP module
2084 * @param pCypherData: Pointer to the cyphertext buffer
2085 * @param Size: Length of the plaintext buffer, must be a multiple of 16 bytes
2086 * @param pPlainData: Pointer to the plaintext buffer
2087 * @retval HAL status
2089 HAL_StatusTypeDef
HAL_CRYP_AESCBC_Decrypt_DMA(CRYP_HandleTypeDef
*hcryp
, uint8_t *pCypherData
, uint16_t Size
, uint8_t *pPlainData
)
2091 uint32_t tickstart
= 0;
2093 uint32_t outputaddr
;
2095 if((hcryp
->State
== HAL_CRYP_STATE_READY
) || (hcryp
->Phase
== HAL_CRYP_PHASE_PROCESS
))
2097 /* Process Locked */
2100 inputaddr
= (uint32_t)pCypherData
;
2101 outputaddr
= (uint32_t)pPlainData
;
2103 /* Change the CRYP state */
2104 hcryp
->State
= HAL_CRYP_STATE_BUSY
;
2106 /* Check if initialization phase has already been performed */
2107 if(hcryp
->Phase
== HAL_CRYP_PHASE_READY
)
2110 CRYP_SetKey(hcryp
, hcryp
->Init
.pKey
, hcryp
->Init
.KeySize
);
2112 /* Set the CRYP peripheral in AES Key mode */
2113 __HAL_CRYP_SET_MODE(hcryp
, CRYP_CR_ALGOMODE_AES_KEY
| CRYP_CR_ALGODIR
);
2116 __HAL_CRYP_ENABLE(hcryp
);
2119 tickstart
= HAL_GetTick();
2121 while(HAL_IS_BIT_SET(hcryp
->Instance
->SR
, CRYP_FLAG_BUSY
))
2123 /* Check for the Timeout */
2124 if((HAL_GetTick() - tickstart
) > CRYP_TIMEOUT_VALUE
)
2127 hcryp
->State
= HAL_CRYP_STATE_TIMEOUT
;
2129 /* Process Unlocked */
2130 __HAL_UNLOCK(hcryp
);
2136 /* Reset the ALGOMODE bits*/
2137 CRYP
->CR
&= (uint32_t)(~CRYP_CR_ALGOMODE
);
2139 /* Set the CRYP peripheral in AES CBC decryption mode */
2140 __HAL_CRYP_SET_MODE(hcryp
, CRYP_CR_ALGOMODE_AES_CBC
| CRYP_CR_ALGODIR
);
2142 /* Set the Initialization Vector */
2143 CRYP_SetInitVector(hcryp
, hcryp
->Init
.pInitVect
, CRYP_KEYSIZE_128B
);
2146 __HAL_CRYP_FIFO_FLUSH(hcryp
);
2149 hcryp
->Phase
= HAL_CRYP_PHASE_PROCESS
;
2152 /* Set the input and output addresses and start DMA transfer */
2153 CRYP_SetDMAConfig(hcryp
, inputaddr
, Size
, outputaddr
);
2155 /* Process Unlocked */
2156 __HAL_UNLOCK(hcryp
);
2158 /* Return function status */
2168 * @brief Initializes the CRYP peripheral in AES CTR decryption mode using DMA.
2169 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
2170 * the configuration information for CRYP module
2171 * @param pCypherData: Pointer to the cyphertext buffer
2172 * @param Size: Length of the plaintext buffer, must be a multiple of 16
2173 * @param pPlainData: Pointer to the plaintext buffer
2174 * @retval HAL status
2176 HAL_StatusTypeDef
HAL_CRYP_AESCTR_Decrypt_DMA(CRYP_HandleTypeDef
*hcryp
, uint8_t *pCypherData
, uint16_t Size
, uint8_t *pPlainData
)
2179 uint32_t outputaddr
;
2181 if((hcryp
->State
== HAL_CRYP_STATE_READY
) || (hcryp
->Phase
== HAL_CRYP_PHASE_PROCESS
))
2183 /* Process Locked */
2186 inputaddr
= (uint32_t)pCypherData
;
2187 outputaddr
= (uint32_t)pPlainData
;
2189 /* Change the CRYP state */
2190 hcryp
->State
= HAL_CRYP_STATE_BUSY
;
2192 /* Check if initialization phase has already been performed */
2193 if(hcryp
->Phase
== HAL_CRYP_PHASE_READY
)
2196 CRYP_SetKey(hcryp
, hcryp
->Init
.pKey
, hcryp
->Init
.KeySize
);
2198 /* Set the CRYP peripheral in AES CTR mode */
2199 __HAL_CRYP_SET_MODE(hcryp
, CRYP_CR_ALGOMODE_AES_CTR
| CRYP_CR_ALGODIR
);
2201 /* Set the Initialization Vector */
2202 CRYP_SetInitVector(hcryp
, hcryp
->Init
.pInitVect
, CRYP_KEYSIZE_128B
);
2205 __HAL_CRYP_FIFO_FLUSH(hcryp
);
2208 hcryp
->Phase
= HAL_CRYP_PHASE_PROCESS
;
2211 /* Set the input and output addresses and start DMA transfer */
2212 CRYP_SetDMAConfig(hcryp
, inputaddr
, Size
, outputaddr
);
2214 /* Process Unlocked */
2215 __HAL_UNLOCK(hcryp
);
2217 /* Return function status */
2231 /** @defgroup CRYP_Exported_Functions_Group3 DES processing functions
2232 * @brief processing functions.
2235 ==============================================================================
2236 ##### DES processing functions #####
2237 ==============================================================================
2238 [..] This section provides functions allowing to:
2239 (+) Encrypt plaintext using DES using ECB or CBC chaining modes
2240 (+) Decrypt cyphertext using ECB or CBC chaining modes
2241 [..] Three processing functions are available:
2251 * @brief Initializes the CRYP peripheral in DES ECB encryption mode.
2252 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
2253 * the configuration information for CRYP module
2254 * @param pPlainData: Pointer to the plaintext buffer
2255 * @param Size: Length of the plaintext buffer, must be a multiple of 8
2256 * @param pCypherData: Pointer to the cyphertext buffer
2257 * @param Timeout: Specify Timeout value
2258 * @retval HAL status
2260 HAL_StatusTypeDef
HAL_CRYP_DESECB_Encrypt(CRYP_HandleTypeDef
*hcryp
, uint8_t *pPlainData
, uint16_t Size
, uint8_t *pCypherData
, uint32_t Timeout
)
2262 /* Process Locked */
2265 /* Change the CRYP state */
2266 hcryp
->State
= HAL_CRYP_STATE_BUSY
;
2268 /* Set CRYP peripheral in DES ECB encryption mode */
2269 CRYP_SetDESECBMode(hcryp
, 0);
2272 __HAL_CRYP_ENABLE(hcryp
);
2274 /* Write Plain Data and Get Cypher Data */
2275 if(CRYP_ProcessData2Words(hcryp
, pPlainData
, Size
, pCypherData
, Timeout
) != HAL_OK
)
2280 /* Change the CRYP state */
2281 hcryp
->State
= HAL_CRYP_STATE_READY
;
2283 /* Process Unlocked */
2284 __HAL_UNLOCK(hcryp
);
2286 /* Return function status */
2291 * @brief Initializes the CRYP peripheral in DES ECB decryption mode.
2292 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
2293 * the configuration information for CRYP module
2294 * @param pCypherData: Pointer to the cyphertext buffer
2295 * @param Size: Length of the plaintext buffer, must be a multiple of 8
2296 * @param pPlainData: Pointer to the plaintext buffer
2297 * @param Timeout: Specify Timeout value
2298 * @retval HAL status
2300 HAL_StatusTypeDef
HAL_CRYP_DESECB_Decrypt(CRYP_HandleTypeDef
*hcryp
, uint8_t *pCypherData
, uint16_t Size
, uint8_t *pPlainData
, uint32_t Timeout
)
2302 /* Process Locked */
2305 /* Change the CRYP state */
2306 hcryp
->State
= HAL_CRYP_STATE_BUSY
;
2308 /* Set CRYP peripheral in DES ECB decryption mode */
2309 CRYP_SetDESECBMode(hcryp
, CRYP_CR_ALGODIR
);
2312 __HAL_CRYP_ENABLE(hcryp
);
2314 /* Write Plain Data and Get Cypher Data */
2315 if(CRYP_ProcessData2Words(hcryp
, pCypherData
, Size
, pPlainData
, Timeout
) != HAL_OK
)
2320 /* Change the CRYP state */
2321 hcryp
->State
= HAL_CRYP_STATE_READY
;
2323 /* Process Unlocked */
2324 __HAL_UNLOCK(hcryp
);
2326 /* Return function status */
2331 * @brief Initializes the CRYP peripheral in DES CBC encryption mode.
2332 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
2333 * the configuration information for CRYP module
2334 * @param pPlainData: Pointer to the plaintext buffer
2335 * @param Size: Length of the plaintext buffer, must be a multiple of 8
2336 * @param pCypherData: Pointer to the cyphertext buffer
2337 * @param Timeout: Specify Timeout value
2338 * @retval HAL status
2340 HAL_StatusTypeDef
HAL_CRYP_DESCBC_Encrypt(CRYP_HandleTypeDef
*hcryp
, uint8_t *pPlainData
, uint16_t Size
, uint8_t *pCypherData
, uint32_t Timeout
)
2342 /* Process Locked */
2345 /* Change the CRYP state */
2346 hcryp
->State
= HAL_CRYP_STATE_BUSY
;
2348 /* Set CRYP peripheral in DES CBC encryption mode */
2349 CRYP_SetDESCBCMode(hcryp
, 0);
2352 __HAL_CRYP_ENABLE(hcryp
);
2354 /* Write Plain Data and Get Cypher Data */
2355 if(CRYP_ProcessData2Words(hcryp
, pPlainData
, Size
, pCypherData
, Timeout
) != HAL_OK
)
2360 /* Change the CRYP state */
2361 hcryp
->State
= HAL_CRYP_STATE_READY
;
2363 /* Process Unlocked */
2364 __HAL_UNLOCK(hcryp
);
2366 /* Return function status */
2371 * @brief Initializes the CRYP peripheral in DES ECB decryption mode.
2372 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
2373 * the configuration information for CRYP module
2374 * @param pCypherData: Pointer to the cyphertext buffer
2375 * @param Size: Length of the plaintext buffer, must be a multiple of 8
2376 * @param pPlainData: Pointer to the plaintext buffer
2377 * @param Timeout: Specify Timeout value
2378 * @retval HAL status
2380 HAL_StatusTypeDef
HAL_CRYP_DESCBC_Decrypt(CRYP_HandleTypeDef
*hcryp
, uint8_t *pCypherData
, uint16_t Size
, uint8_t *pPlainData
, uint32_t Timeout
)
2382 /* Process Locked */
2385 /* Change the CRYP state */
2386 hcryp
->State
= HAL_CRYP_STATE_BUSY
;
2388 /* Set CRYP peripheral in DES CBC decryption mode */
2389 CRYP_SetDESCBCMode(hcryp
, CRYP_CR_ALGODIR
);
2392 __HAL_CRYP_ENABLE(hcryp
);
2394 /* Write Plain Data and Get Cypher Data */
2395 if(CRYP_ProcessData2Words(hcryp
, pCypherData
, Size
, pPlainData
, Timeout
) != HAL_OK
)
2400 /* Change the CRYP state */
2401 hcryp
->State
= HAL_CRYP_STATE_READY
;
2403 /* Process Unlocked */
2404 __HAL_UNLOCK(hcryp
);
2406 /* Return function status */
2411 * @brief Initializes the CRYP peripheral in DES ECB encryption mode using IT.
2412 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
2413 * the configuration information for CRYP module
2414 * @param pPlainData: Pointer to the plaintext buffer
2415 * @param Size: Length of the plaintext buffer, must be a multiple of 8
2416 * @param pCypherData: Pointer to the cyphertext buffer
2417 * @retval HAL status
2419 HAL_StatusTypeDef
HAL_CRYP_DESECB_Encrypt_IT(CRYP_HandleTypeDef
*hcryp
, uint8_t *pPlainData
, uint16_t Size
, uint8_t *pCypherData
)
2422 uint32_t outputaddr
;
2424 if(hcryp
->State
== HAL_CRYP_STATE_READY
)
2426 /* Process Locked */
2429 hcryp
->CrypInCount
= Size
;
2430 hcryp
->pCrypInBuffPtr
= pPlainData
;
2431 hcryp
->pCrypOutBuffPtr
= pCypherData
;
2432 hcryp
->CrypOutCount
= Size
;
2434 /* Change the CRYP state */
2435 hcryp
->State
= HAL_CRYP_STATE_BUSY
;
2437 /* Set CRYP peripheral in DES ECB encryption mode */
2438 CRYP_SetDESECBMode(hcryp
, 0);
2440 /* Enable Interrupts */
2441 __HAL_CRYP_ENABLE_IT(hcryp
, CRYP_IT_INI
| CRYP_IT_OUTI
);
2444 __HAL_CRYP_ENABLE(hcryp
);
2446 /* Return function status */
2449 else if(__HAL_CRYP_GET_IT(hcryp
, CRYP_IT_INI
))
2451 inputaddr
= (uint32_t)hcryp
->pCrypInBuffPtr
;
2452 /* Write the Input block in the IN FIFO */
2453 hcryp
->Instance
->DR
= *(uint32_t*)(inputaddr
);
2455 hcryp
->Instance
->DR
= *(uint32_t*)(inputaddr
);
2457 hcryp
->pCrypInBuffPtr
+= 8;
2458 hcryp
->CrypInCount
-= 8;
2459 if(hcryp
->CrypInCount
== 0)
2461 __HAL_CRYP_DISABLE_IT(hcryp
, CRYP_IT_INI
);
2462 /* Call the Input data transfer complete callback */
2463 HAL_CRYP_InCpltCallback(hcryp
);
2466 else if(__HAL_CRYP_GET_IT(hcryp
, CRYP_IT_OUTI
))
2468 outputaddr
= (uint32_t)hcryp
->pCrypOutBuffPtr
;
2469 /* Read the Output block from the Output FIFO */
2470 *(uint32_t*)(outputaddr
) = hcryp
->Instance
->DOUT
;
2472 *(uint32_t*)(outputaddr
) = hcryp
->Instance
->DOUT
;
2474 hcryp
->pCrypOutBuffPtr
+= 8;
2475 hcryp
->CrypOutCount
-= 8;
2476 if(hcryp
->CrypOutCount
== 0)
2479 __HAL_CRYP_DISABLE_IT(hcryp
, CRYP_IT_OUTI
);
2481 __HAL_CRYP_DISABLE(hcryp
);
2482 /* Process Unlocked */
2483 __HAL_UNLOCK(hcryp
);
2484 /* Change the CRYP state */
2485 hcryp
->State
= HAL_CRYP_STATE_READY
;
2486 /* Call Input transfer complete callback */
2487 HAL_CRYP_OutCpltCallback(hcryp
);
2491 /* Return function status */
2496 * @brief Initializes the CRYP peripheral in DES CBC encryption mode using interrupt.
2497 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
2498 * the configuration information for CRYP module
2499 * @param pPlainData: Pointer to the plaintext buffer
2500 * @param Size: Length of the plaintext buffer, must be a multiple of 8
2501 * @param pCypherData: Pointer to the cyphertext buffer
2502 * @retval HAL status
2504 HAL_StatusTypeDef
HAL_CRYP_DESCBC_Encrypt_IT(CRYP_HandleTypeDef
*hcryp
, uint8_t *pPlainData
, uint16_t Size
, uint8_t *pCypherData
)
2507 uint32_t outputaddr
;
2509 if(hcryp
->State
== HAL_CRYP_STATE_READY
)
2511 /* Process Locked */
2514 hcryp
->CrypInCount
= Size
;
2515 hcryp
->pCrypInBuffPtr
= pPlainData
;
2516 hcryp
->pCrypOutBuffPtr
= pCypherData
;
2517 hcryp
->CrypOutCount
= Size
;
2519 /* Change the CRYP state */
2520 hcryp
->State
= HAL_CRYP_STATE_BUSY
;
2522 /* Set CRYP peripheral in DES CBC encryption mode */
2523 CRYP_SetDESCBCMode(hcryp
, 0);
2525 /* Enable Interrupts */
2526 __HAL_CRYP_ENABLE_IT(hcryp
, CRYP_IT_INI
| CRYP_IT_OUTI
);
2529 __HAL_CRYP_ENABLE(hcryp
);
2531 /* Return function status */
2535 else if(__HAL_CRYP_GET_IT(hcryp
, CRYP_IT_INI
))
2537 inputaddr
= (uint32_t)hcryp
->pCrypInBuffPtr
;
2538 /* Write the Input block in the IN FIFO */
2539 hcryp
->Instance
->DR
= *(uint32_t*)(inputaddr
);
2541 hcryp
->Instance
->DR
= *(uint32_t*)(inputaddr
);
2543 hcryp
->pCrypInBuffPtr
+= 8;
2544 hcryp
->CrypInCount
-= 8;
2545 if(hcryp
->CrypInCount
== 0)
2547 __HAL_CRYP_DISABLE_IT(hcryp
, CRYP_IT_INI
);
2548 /* Call the Input data transfer complete callback */
2549 HAL_CRYP_InCpltCallback(hcryp
);
2552 else if(__HAL_CRYP_GET_IT(hcryp
, CRYP_IT_OUTI
))
2554 outputaddr
= (uint32_t)hcryp
->pCrypOutBuffPtr
;
2555 /* Read the Output block from the Output FIFO */
2556 *(uint32_t*)(outputaddr
) = hcryp
->Instance
->DOUT
;
2558 *(uint32_t*)(outputaddr
) = hcryp
->Instance
->DOUT
;
2560 hcryp
->pCrypOutBuffPtr
+= 8;
2561 hcryp
->CrypOutCount
-= 8;
2562 if(hcryp
->CrypOutCount
== 0)
2565 __HAL_CRYP_DISABLE_IT(hcryp
, CRYP_IT_OUTI
);
2567 __HAL_CRYP_DISABLE(hcryp
);
2568 /* Process Unlocked */
2569 __HAL_UNLOCK(hcryp
);
2570 /* Change the CRYP state */
2571 hcryp
->State
= HAL_CRYP_STATE_READY
;
2572 /* Call Input transfer complete callback */
2573 HAL_CRYP_OutCpltCallback(hcryp
);
2577 /* Return function status */
2582 * @brief Initializes the CRYP peripheral in DES ECB decryption mode using IT.
2583 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
2584 * the configuration information for CRYP module
2585 * @param pPlainData: Pointer to the plaintext buffer
2586 * @param Size: Length of the plaintext buffer, must be a multiple of 8
2587 * @param pCypherData: Pointer to the cyphertext buffer
2588 * @retval HAL status
2590 HAL_StatusTypeDef
HAL_CRYP_DESECB_Decrypt_IT(CRYP_HandleTypeDef
*hcryp
, uint8_t *pCypherData
, uint16_t Size
, uint8_t *pPlainData
)
2593 uint32_t outputaddr
;
2595 if(hcryp
->State
== HAL_CRYP_STATE_READY
)
2597 /* Process Locked */
2600 hcryp
->CrypInCount
= Size
;
2601 hcryp
->pCrypInBuffPtr
= pCypherData
;
2602 hcryp
->pCrypOutBuffPtr
= pPlainData
;
2603 hcryp
->CrypOutCount
= Size
;
2605 /* Change the CRYP state */
2606 hcryp
->State
= HAL_CRYP_STATE_BUSY
;
2608 /* Set CRYP peripheral in DES ECB decryption mode */
2609 CRYP_SetDESECBMode(hcryp
, CRYP_CR_ALGODIR
);
2611 /* Enable Interrupts */
2612 __HAL_CRYP_ENABLE_IT(hcryp
, CRYP_IT_INI
| CRYP_IT_OUTI
);
2615 __HAL_CRYP_ENABLE(hcryp
);
2617 /* Return function status */
2620 else if(__HAL_CRYP_GET_IT(hcryp
, CRYP_IT_INI
))
2622 inputaddr
= (uint32_t)hcryp
->pCrypInBuffPtr
;
2623 /* Write the Input block in the IN FIFO */
2624 hcryp
->Instance
->DR
= *(uint32_t*)(inputaddr
);
2626 hcryp
->Instance
->DR
= *(uint32_t*)(inputaddr
);
2628 hcryp
->pCrypInBuffPtr
+= 8;
2629 hcryp
->CrypInCount
-= 8;
2630 if(hcryp
->CrypInCount
== 0)
2632 __HAL_CRYP_DISABLE_IT(hcryp
, CRYP_IT_INI
);
2633 /* Call the Input data transfer complete callback */
2634 HAL_CRYP_InCpltCallback(hcryp
);
2637 else if(__HAL_CRYP_GET_IT(hcryp
, CRYP_IT_OUTI
))
2639 outputaddr
= (uint32_t)hcryp
->pCrypOutBuffPtr
;
2640 /* Read the Output block from the Output FIFO */
2641 *(uint32_t*)(outputaddr
) = hcryp
->Instance
->DOUT
;
2643 *(uint32_t*)(outputaddr
) = hcryp
->Instance
->DOUT
;
2645 hcryp
->pCrypOutBuffPtr
+= 8;
2646 hcryp
->CrypOutCount
-= 8;
2647 if(hcryp
->CrypOutCount
== 0)
2650 __HAL_CRYP_DISABLE_IT(hcryp
, CRYP_IT_OUTI
);
2652 __HAL_CRYP_DISABLE(hcryp
);
2653 /* Process Unlocked */
2654 __HAL_UNLOCK(hcryp
);
2655 /* Change the CRYP state */
2656 hcryp
->State
= HAL_CRYP_STATE_READY
;
2657 /* Call Input transfer complete callback */
2658 HAL_CRYP_OutCpltCallback(hcryp
);
2662 /* Return function status */
2667 * @brief Initializes the CRYP peripheral in DES ECB decryption mode using interrupt.
2668 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
2669 * the configuration information for CRYP module
2670 * @param pPlainData: Pointer to the plaintext buffer
2671 * @param Size: Length of the plaintext buffer, must be a multiple of 8
2672 * @param pCypherData: Pointer to the cyphertext buffer
2673 * @retval HAL status
2675 HAL_StatusTypeDef
HAL_CRYP_DESCBC_Decrypt_IT(CRYP_HandleTypeDef
*hcryp
, uint8_t *pCypherData
, uint16_t Size
, uint8_t *pPlainData
)
2678 uint32_t outputaddr
;
2680 if(hcryp
->State
== HAL_CRYP_STATE_READY
)
2682 /* Process Locked */
2685 hcryp
->CrypInCount
= Size
;
2686 hcryp
->pCrypInBuffPtr
= pCypherData
;
2687 hcryp
->pCrypOutBuffPtr
= pPlainData
;
2688 hcryp
->CrypOutCount
= Size
;
2690 /* Change the CRYP state */
2691 hcryp
->State
= HAL_CRYP_STATE_BUSY
;
2693 /* Set CRYP peripheral in DES CBC decryption mode */
2694 CRYP_SetDESCBCMode(hcryp
, CRYP_CR_ALGODIR
);
2696 /* Enable Interrupts */
2697 __HAL_CRYP_ENABLE_IT(hcryp
, CRYP_IT_INI
| CRYP_IT_OUTI
);
2700 __HAL_CRYP_ENABLE(hcryp
);
2702 /* Return function status */
2705 else if(__HAL_CRYP_GET_IT(hcryp
, CRYP_IT_INI
))
2707 inputaddr
= (uint32_t)hcryp
->pCrypInBuffPtr
;
2708 /* Write the Input block in the IN FIFO */
2709 hcryp
->Instance
->DR
= *(uint32_t*)(inputaddr
);
2711 hcryp
->Instance
->DR
= *(uint32_t*)(inputaddr
);
2713 hcryp
->pCrypInBuffPtr
+= 8;
2714 hcryp
->CrypInCount
-= 8;
2715 if(hcryp
->CrypInCount
== 0)
2717 __HAL_CRYP_DISABLE_IT(hcryp
, CRYP_IT_INI
);
2718 /* Call the Input data transfer complete callback */
2719 HAL_CRYP_InCpltCallback(hcryp
);
2722 else if(__HAL_CRYP_GET_IT(hcryp
, CRYP_IT_OUTI
))
2724 outputaddr
= (uint32_t)hcryp
->pCrypOutBuffPtr
;
2725 /* Read the Output block from the Output FIFO */
2726 *(uint32_t*)(outputaddr
) = hcryp
->Instance
->DOUT
;
2728 *(uint32_t*)(outputaddr
) = hcryp
->Instance
->DOUT
;
2730 hcryp
->pCrypOutBuffPtr
+= 8;
2731 hcryp
->CrypOutCount
-= 8;
2732 if(hcryp
->CrypOutCount
== 0)
2735 __HAL_CRYP_DISABLE_IT(hcryp
, CRYP_IT_OUTI
);
2737 __HAL_CRYP_DISABLE(hcryp
);
2738 /* Process Unlocked */
2739 __HAL_UNLOCK(hcryp
);
2740 /* Change the CRYP state */
2741 hcryp
->State
= HAL_CRYP_STATE_READY
;
2742 /* Call Input transfer complete callback */
2743 HAL_CRYP_OutCpltCallback(hcryp
);
2747 /* Return function status */
2752 * @brief Initializes the CRYP peripheral in DES ECB encryption mode using DMA.
2753 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
2754 * the configuration information for CRYP module
2755 * @param pPlainData: Pointer to the plaintext buffer
2756 * @param Size: Length of the plaintext buffer, must be a multiple of 8
2757 * @param pCypherData: Pointer to the cyphertext buffer
2758 * @retval HAL status
2760 HAL_StatusTypeDef
HAL_CRYP_DESECB_Encrypt_DMA(CRYP_HandleTypeDef
*hcryp
, uint8_t *pPlainData
, uint16_t Size
, uint8_t *pCypherData
)
2763 uint32_t outputaddr
;
2765 if((hcryp
->State
== HAL_CRYP_STATE_READY
) || (hcryp
->Phase
== HAL_CRYP_PHASE_PROCESS
))
2767 /* Process Locked */
2770 inputaddr
= (uint32_t)pPlainData
;
2771 outputaddr
= (uint32_t)pCypherData
;
2773 /* Change the CRYP state */
2774 hcryp
->State
= HAL_CRYP_STATE_BUSY
;
2776 /* Set CRYP peripheral in DES ECB encryption mode */
2777 CRYP_SetDESECBMode(hcryp
, 0);
2779 /* Set the input and output addresses and start DMA transfer */
2780 CRYP_SetDMAConfig(hcryp
, inputaddr
, Size
, outputaddr
);
2782 /* Process Unlocked */
2783 __HAL_UNLOCK(hcryp
);
2785 /* Return function status */
2795 * @brief Initializes the CRYP peripheral in DES CBC encryption mode using DMA.
2796 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
2797 * the configuration information for CRYP module
2798 * @param pPlainData: Pointer to the plaintext buffer
2799 * @param Size: Length of the plaintext buffer, must be a multiple of 8
2800 * @param pCypherData: Pointer to the cyphertext buffer
2801 * @retval HAL status
2803 HAL_StatusTypeDef
HAL_CRYP_DESCBC_Encrypt_DMA(CRYP_HandleTypeDef
*hcryp
, uint8_t *pPlainData
, uint16_t Size
, uint8_t *pCypherData
)
2806 uint32_t outputaddr
;
2808 if((hcryp
->State
== HAL_CRYP_STATE_READY
) || (hcryp
->Phase
== HAL_CRYP_PHASE_PROCESS
))
2810 /* Process Locked */
2813 inputaddr
= (uint32_t)pPlainData
;
2814 outputaddr
= (uint32_t)pCypherData
;
2816 /* Change the CRYP state */
2817 hcryp
->State
= HAL_CRYP_STATE_BUSY
;
2819 /* Set CRYP peripheral in DES CBC encryption mode */
2820 CRYP_SetDESCBCMode(hcryp
, 0);
2822 /* Set the input and output addresses and start DMA transfer */
2823 CRYP_SetDMAConfig(hcryp
, inputaddr
, Size
, outputaddr
);
2825 /* Process Unlocked */
2826 __HAL_UNLOCK(hcryp
);
2828 /* Return function status */
2838 * @brief Initializes the CRYP peripheral in DES ECB decryption mode using DMA.
2839 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
2840 * the configuration information for CRYP module
2841 * @param pPlainData: Pointer to the plaintext buffer
2842 * @param Size: Length of the plaintext buffer, must be a multiple of 8
2843 * @param pCypherData: Pointer to the cyphertext buffer
2844 * @retval HAL status
2846 HAL_StatusTypeDef
HAL_CRYP_DESECB_Decrypt_DMA(CRYP_HandleTypeDef
*hcryp
, uint8_t *pCypherData
, uint16_t Size
, uint8_t *pPlainData
)
2849 uint32_t outputaddr
;
2851 if((hcryp
->State
== HAL_CRYP_STATE_READY
) || (hcryp
->Phase
== HAL_CRYP_PHASE_PROCESS
))
2853 /* Process Locked */
2856 inputaddr
= (uint32_t)pCypherData
;
2857 outputaddr
= (uint32_t)pPlainData
;
2859 /* Change the CRYP state */
2860 hcryp
->State
= HAL_CRYP_STATE_BUSY
;
2862 /* Set CRYP peripheral in DES ECB decryption mode */
2863 CRYP_SetDESECBMode(hcryp
, CRYP_CR_ALGODIR
);
2865 /* Set the input and output addresses and start DMA transfer */
2866 CRYP_SetDMAConfig(hcryp
, inputaddr
, Size
, outputaddr
);
2868 /* Process Unlocked */
2869 __HAL_UNLOCK(hcryp
);
2871 /* Return function status */
2881 * @brief Initializes the CRYP peripheral in DES ECB decryption mode using DMA.
2882 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
2883 * the configuration information for CRYP module
2884 * @param pPlainData: Pointer to the plaintext buffer
2885 * @param Size: Length of the plaintext buffer, must be a multiple of 8
2886 * @param pCypherData: Pointer to the cyphertext buffer
2887 * @retval HAL status
2889 HAL_StatusTypeDef
HAL_CRYP_DESCBC_Decrypt_DMA(CRYP_HandleTypeDef
*hcryp
, uint8_t *pCypherData
, uint16_t Size
, uint8_t *pPlainData
)
2892 uint32_t outputaddr
;
2894 if((hcryp
->State
== HAL_CRYP_STATE_READY
) || (hcryp
->Phase
== HAL_CRYP_PHASE_PROCESS
))
2896 /* Process Locked */
2899 inputaddr
= (uint32_t)pCypherData
;
2900 outputaddr
= (uint32_t)pPlainData
;
2902 /* Change the CRYP state */
2903 hcryp
->State
= HAL_CRYP_STATE_BUSY
;
2905 /* Set CRYP peripheral in DES CBC decryption mode */
2906 CRYP_SetDESCBCMode(hcryp
, CRYP_CR_ALGODIR
);
2908 /* Set the input and output addresses and start DMA transfer */
2909 CRYP_SetDMAConfig(hcryp
, inputaddr
, Size
, outputaddr
);
2911 /* Process Unlocked */
2912 __HAL_UNLOCK(hcryp
);
2914 /* Return function status */
2927 /** @defgroup CRYP_Exported_Functions_Group4 TDES processing functions
2928 * @brief processing functions.
2931 ==============================================================================
2932 ##### TDES processing functions #####
2933 ==============================================================================
2934 [..] This section provides functions allowing to:
2935 (+) Encrypt plaintext using TDES based on ECB or CBC chaining modes
2936 (+) Decrypt cyphertext using TDES based on ECB or CBC chaining modes
2937 [..] Three processing functions are available:
2947 * @brief Initializes the CRYP peripheral in TDES ECB encryption mode
2948 * then encrypt pPlainData. The cypher data are available in pCypherData
2949 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
2950 * the configuration information for CRYP module
2951 * @param pPlainData: Pointer to the plaintext buffer
2952 * @param Size: Length of the plaintext buffer, must be a multiple of 8
2953 * @param pCypherData: Pointer to the cyphertext buffer
2954 * @param Timeout: Specify Timeout value
2955 * @retval HAL status
2957 HAL_StatusTypeDef
HAL_CRYP_TDESECB_Encrypt(CRYP_HandleTypeDef
*hcryp
, uint8_t *pPlainData
, uint16_t Size
, uint8_t *pCypherData
, uint32_t Timeout
)
2959 /* Process Locked */
2962 /* Change the CRYP state */
2963 hcryp
->State
= HAL_CRYP_STATE_BUSY
;
2965 /* Set CRYP peripheral in TDES ECB encryption mode */
2966 CRYP_SetTDESECBMode(hcryp
, 0);
2969 __HAL_CRYP_ENABLE(hcryp
);
2971 /* Write Plain Data and Get Cypher Data */
2972 if(CRYP_ProcessData2Words(hcryp
, pPlainData
, Size
, pCypherData
, Timeout
) != HAL_OK
)
2977 /* Change the CRYP state */
2978 hcryp
->State
= HAL_CRYP_STATE_READY
;
2980 /* Process Unlocked */
2981 __HAL_UNLOCK(hcryp
);
2983 /* Return function status */
2988 * @brief Initializes the CRYP peripheral in TDES ECB decryption mode
2989 * then decrypted pCypherData. The cypher data are available in pPlainData
2990 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
2991 * the configuration information for CRYP module
2992 * @param pPlainData: Pointer to the plaintext buffer
2993 * @param Size: Length of the plaintext buffer, must be a multiple of 8
2994 * @param pCypherData: Pointer to the cyphertext buffer
2995 * @param Timeout: Specify Timeout value
2996 * @retval HAL status
2998 HAL_StatusTypeDef
HAL_CRYP_TDESECB_Decrypt(CRYP_HandleTypeDef
*hcryp
, uint8_t *pCypherData
, uint16_t Size
, uint8_t *pPlainData
, uint32_t Timeout
)
3000 /* Process Locked */
3003 /* Change the CRYP state */
3004 hcryp
->State
= HAL_CRYP_STATE_BUSY
;
3006 /* Set CRYP peripheral in TDES ECB decryption mode */
3007 CRYP_SetTDESECBMode(hcryp
, CRYP_CR_ALGODIR
);
3010 __HAL_CRYP_ENABLE(hcryp
);
3012 /* Write Cypher Data and Get Plain Data */
3013 if(CRYP_ProcessData2Words(hcryp
, pCypherData
, Size
, pPlainData
, Timeout
) != HAL_OK
)
3018 /* Change the CRYP state */
3019 hcryp
->State
= HAL_CRYP_STATE_READY
;
3021 /* Process Unlocked */
3022 __HAL_UNLOCK(hcryp
);
3024 /* Return function status */
3029 * @brief Initializes the CRYP peripheral in TDES CBC encryption mode
3030 * then encrypt pPlainData. The cypher data are available in pCypherData
3031 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
3032 * the configuration information for CRYP module
3033 * @param pPlainData: Pointer to the plaintext buffer
3034 * @param Size: Length of the plaintext buffer, must be a multiple of 8
3035 * @param pCypherData: Pointer to the cyphertext buffer
3036 * @param Timeout: Specify Timeout value
3037 * @retval HAL status
3039 HAL_StatusTypeDef
HAL_CRYP_TDESCBC_Encrypt(CRYP_HandleTypeDef
*hcryp
, uint8_t *pPlainData
, uint16_t Size
, uint8_t *pCypherData
, uint32_t Timeout
)
3041 /* Process Locked */
3044 /* Change the CRYP state */
3045 hcryp
->State
= HAL_CRYP_STATE_BUSY
;
3047 /* Set CRYP peripheral in TDES CBC encryption mode */
3048 CRYP_SetTDESCBCMode(hcryp
, 0);
3051 __HAL_CRYP_ENABLE(hcryp
);
3053 /* Write Plain Data and Get Cypher Data */
3054 if(CRYP_ProcessData2Words(hcryp
, pPlainData
, Size
, pCypherData
, Timeout
) != HAL_OK
)
3059 /* Change the CRYP state */
3060 hcryp
->State
= HAL_CRYP_STATE_READY
;
3062 /* Process Unlocked */
3063 __HAL_UNLOCK(hcryp
);
3065 /* Return function status */
3070 * @brief Initializes the CRYP peripheral in TDES CBC decryption mode
3071 * then decrypted pCypherData. The cypher data are available in pPlainData
3072 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
3073 * the configuration information for CRYP module
3074 * @param pCypherData: Pointer to the cyphertext buffer
3075 * @param Size: Length of the plaintext buffer, must be a multiple of 8
3076 * @param pPlainData: Pointer to the plaintext buffer
3077 * @param Timeout: Specify Timeout value
3078 * @retval HAL status
3080 HAL_StatusTypeDef
HAL_CRYP_TDESCBC_Decrypt(CRYP_HandleTypeDef
*hcryp
, uint8_t *pCypherData
, uint16_t Size
, uint8_t *pPlainData
, uint32_t Timeout
)
3082 /* Process Locked */
3085 /* Change the CRYP state */
3086 hcryp
->State
= HAL_CRYP_STATE_BUSY
;
3088 /* Set CRYP peripheral in TDES CBC decryption mode */
3089 CRYP_SetTDESCBCMode(hcryp
, CRYP_CR_ALGODIR
);
3092 __HAL_CRYP_ENABLE(hcryp
);
3094 /* Write Cypher Data and Get Plain Data */
3095 if(CRYP_ProcessData2Words(hcryp
, pCypherData
, Size
, pPlainData
, Timeout
) != HAL_OK
)
3100 /* Change the CRYP state */
3101 hcryp
->State
= HAL_CRYP_STATE_READY
;
3103 /* Process Unlocked */
3104 __HAL_UNLOCK(hcryp
);
3106 /* Return function status */
3111 * @brief Initializes the CRYP peripheral in TDES ECB encryption mode using interrupt.
3112 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
3113 * the configuration information for CRYP module
3114 * @param pPlainData: Pointer to the plaintext buffer
3115 * @param Size: Length of the plaintext buffer, must be a multiple of 8
3116 * @param pCypherData: Pointer to the cyphertext buffer
3117 * @retval HAL status
3119 HAL_StatusTypeDef
HAL_CRYP_TDESECB_Encrypt_IT(CRYP_HandleTypeDef
*hcryp
, uint8_t *pPlainData
, uint16_t Size
, uint8_t *pCypherData
)
3122 uint32_t outputaddr
;
3124 if(hcryp
->State
== HAL_CRYP_STATE_READY
)
3126 /* Process Locked */
3129 hcryp
->CrypInCount
= Size
;
3130 hcryp
->pCrypInBuffPtr
= pPlainData
;
3131 hcryp
->pCrypOutBuffPtr
= pCypherData
;
3132 hcryp
->CrypOutCount
= Size
;
3134 /* Change the CRYP state */
3135 hcryp
->State
= HAL_CRYP_STATE_BUSY
;
3137 /* Set CRYP peripheral in TDES ECB encryption mode */
3138 CRYP_SetTDESECBMode(hcryp
, 0);
3140 /* Enable Interrupts */
3141 __HAL_CRYP_ENABLE_IT(hcryp
, CRYP_IT_INI
| CRYP_IT_OUTI
);
3144 __HAL_CRYP_ENABLE(hcryp
);
3146 /* Return function status */
3149 else if(__HAL_CRYP_GET_IT(hcryp
, CRYP_IT_INI
))
3151 inputaddr
= (uint32_t)hcryp
->pCrypInBuffPtr
;
3152 /* Write the Input block in the IN FIFO */
3153 hcryp
->Instance
->DR
= *(uint32_t*)(inputaddr
);
3155 hcryp
->Instance
->DR
= *(uint32_t*)(inputaddr
);
3157 hcryp
->pCrypInBuffPtr
+= 8;
3158 hcryp
->CrypInCount
-= 8;
3159 if(hcryp
->CrypInCount
== 0)
3161 __HAL_CRYP_DISABLE_IT(hcryp
, CRYP_IT_INI
);
3162 /* Call the Input data transfer complete callback */
3163 HAL_CRYP_InCpltCallback(hcryp
);
3166 else if(__HAL_CRYP_GET_IT(hcryp
, CRYP_IT_OUTI
))
3168 outputaddr
= (uint32_t)hcryp
->pCrypOutBuffPtr
;
3169 /* Read the Output block from the Output FIFO */
3170 *(uint32_t*)(outputaddr
) = hcryp
->Instance
->DOUT
;
3172 *(uint32_t*)(outputaddr
) = hcryp
->Instance
->DOUT
;
3174 hcryp
->pCrypOutBuffPtr
+= 8;
3175 hcryp
->CrypOutCount
-= 8;
3176 if(hcryp
->CrypOutCount
== 0)
3179 __HAL_CRYP_DISABLE_IT(hcryp
, CRYP_IT_OUTI
);
3181 __HAL_CRYP_DISABLE(hcryp
);
3182 /* Process Unlocked */
3183 __HAL_UNLOCK(hcryp
);
3184 /* Change the CRYP state */
3185 hcryp
->State
= HAL_CRYP_STATE_READY
;
3186 /* Call the Output data transfer complete callback */
3187 HAL_CRYP_OutCpltCallback(hcryp
);
3191 /* Return function status */
3196 * @brief Initializes the CRYP peripheral in TDES CBC encryption mode.
3197 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
3198 * the configuration information for CRYP module
3199 * @param pPlainData: Pointer to the plaintext buffer
3200 * @param Size: Length of the plaintext buffer, must be a multiple of 8
3201 * @param pCypherData: Pointer to the cyphertext buffer
3202 * @retval HAL status
3204 HAL_StatusTypeDef
HAL_CRYP_TDESCBC_Encrypt_IT(CRYP_HandleTypeDef
*hcryp
, uint8_t *pPlainData
, uint16_t Size
, uint8_t *pCypherData
)
3207 uint32_t outputaddr
;
3209 if(hcryp
->State
== HAL_CRYP_STATE_READY
)
3211 /* Process Locked */
3214 hcryp
->CrypInCount
= Size
;
3215 hcryp
->pCrypInBuffPtr
= pPlainData
;
3216 hcryp
->pCrypOutBuffPtr
= pCypherData
;
3217 hcryp
->CrypOutCount
= Size
;
3219 /* Change the CRYP state */
3220 hcryp
->State
= HAL_CRYP_STATE_BUSY
;
3222 /* Set CRYP peripheral in TDES CBC encryption mode */
3223 CRYP_SetTDESCBCMode(hcryp
, 0);
3225 /* Enable Interrupts */
3226 __HAL_CRYP_ENABLE_IT(hcryp
, CRYP_IT_INI
| CRYP_IT_OUTI
);
3229 __HAL_CRYP_ENABLE(hcryp
);
3231 /* Return function status */
3234 else if(__HAL_CRYP_GET_IT(hcryp
, CRYP_IT_INI
))
3236 inputaddr
= (uint32_t)hcryp
->pCrypInBuffPtr
;
3237 /* Write the Input block in the IN FIFO */
3238 hcryp
->Instance
->DR
= *(uint32_t*)(inputaddr
);
3240 hcryp
->Instance
->DR
= *(uint32_t*)(inputaddr
);
3242 hcryp
->pCrypInBuffPtr
+= 8;
3243 hcryp
->CrypInCount
-= 8;
3244 if(hcryp
->CrypInCount
== 0)
3246 __HAL_CRYP_DISABLE_IT(hcryp
, CRYP_IT_INI
);
3247 /* Call the Input data transfer complete callback */
3248 HAL_CRYP_InCpltCallback(hcryp
);
3251 else if(__HAL_CRYP_GET_IT(hcryp
, CRYP_IT_OUTI
))
3253 outputaddr
= (uint32_t)hcryp
->pCrypOutBuffPtr
;
3254 /* Read the Output block from the Output FIFO */
3255 *(uint32_t*)(outputaddr
) = hcryp
->Instance
->DOUT
;
3257 *(uint32_t*)(outputaddr
) = hcryp
->Instance
->DOUT
;
3259 hcryp
->pCrypOutBuffPtr
+= 8;
3260 hcryp
->CrypOutCount
-= 8;
3261 if(hcryp
->CrypOutCount
== 0)
3263 __HAL_CRYP_DISABLE_IT(hcryp
, CRYP_IT_OUTI
);
3265 __HAL_CRYP_DISABLE(hcryp
);
3266 /* Process Unlocked */
3267 __HAL_UNLOCK(hcryp
);
3268 /* Change the CRYP state */
3269 hcryp
->State
= HAL_CRYP_STATE_READY
;
3270 /* Call Input transfer complete callback */
3271 HAL_CRYP_OutCpltCallback(hcryp
);
3275 /* Return function status */
3280 * @brief Initializes the CRYP peripheral in TDES ECB decryption mode.
3281 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
3282 * the configuration information for CRYP module
3283 * @param pPlainData: Pointer to the plaintext buffer
3284 * @param Size: Length of the plaintext buffer, must be a multiple of 8
3285 * @param pCypherData: Pointer to the cyphertext buffer
3286 * @retval HAL status
3288 HAL_StatusTypeDef
HAL_CRYP_TDESECB_Decrypt_IT(CRYP_HandleTypeDef
*hcryp
, uint8_t *pCypherData
, uint16_t Size
, uint8_t *pPlainData
)
3291 uint32_t outputaddr
;
3293 if(hcryp
->State
== HAL_CRYP_STATE_READY
)
3295 /* Process Locked */
3298 hcryp
->CrypInCount
= Size
;
3299 hcryp
->pCrypInBuffPtr
= pCypherData
;
3300 hcryp
->pCrypOutBuffPtr
= pPlainData
;
3301 hcryp
->CrypOutCount
= Size
;
3303 /* Change the CRYP state */
3304 hcryp
->State
= HAL_CRYP_STATE_BUSY
;
3306 /* Set CRYP peripheral in TDES ECB decryption mode */
3307 CRYP_SetTDESECBMode(hcryp
, CRYP_CR_ALGODIR
);
3309 /* Enable Interrupts */
3310 __HAL_CRYP_ENABLE_IT(hcryp
, CRYP_IT_INI
| CRYP_IT_OUTI
);
3313 __HAL_CRYP_ENABLE(hcryp
);
3315 /* Return function status */
3318 else if(__HAL_CRYP_GET_IT(hcryp
, CRYP_IT_INI
))
3320 inputaddr
= (uint32_t)hcryp
->pCrypInBuffPtr
;
3321 /* Write the Input block in the IN FIFO */
3322 hcryp
->Instance
->DR
= *(uint32_t*)(inputaddr
);
3324 hcryp
->Instance
->DR
= *(uint32_t*)(inputaddr
);
3326 hcryp
->pCrypInBuffPtr
+= 8;
3327 hcryp
->CrypInCount
-= 8;
3328 if(hcryp
->CrypInCount
== 0)
3330 __HAL_CRYP_DISABLE_IT(hcryp
, CRYP_IT_INI
);
3331 /* Call the Input data transfer complete callback */
3332 HAL_CRYP_InCpltCallback(hcryp
);
3335 else if(__HAL_CRYP_GET_IT(hcryp
, CRYP_IT_OUTI
))
3337 outputaddr
= (uint32_t)hcryp
->pCrypOutBuffPtr
;
3338 /* Read the Output block from the Output FIFO */
3339 *(uint32_t*)(outputaddr
) = hcryp
->Instance
->DOUT
;
3341 *(uint32_t*)(outputaddr
) = hcryp
->Instance
->DOUT
;
3343 hcryp
->pCrypOutBuffPtr
+= 8;
3344 hcryp
->CrypOutCount
-= 8;
3345 if(hcryp
->CrypOutCount
== 0)
3347 __HAL_CRYP_DISABLE_IT(hcryp
, CRYP_IT_OUTI
);
3349 __HAL_CRYP_DISABLE(hcryp
);
3350 /* Process Unlocked */
3351 __HAL_UNLOCK(hcryp
);
3352 /* Change the CRYP state */
3353 hcryp
->State
= HAL_CRYP_STATE_READY
;
3354 /* Call Input transfer complete callback */
3355 HAL_CRYP_OutCpltCallback(hcryp
);
3359 /* Return function status */
3364 * @brief Initializes the CRYP peripheral in TDES CBC decryption mode.
3365 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
3366 * the configuration information for CRYP module
3367 * @param pCypherData: Pointer to the cyphertext buffer
3368 * @param Size: Length of the plaintext buffer, must be a multiple of 8
3369 * @param pPlainData: Pointer to the plaintext buffer
3370 * @retval HAL status
3372 HAL_StatusTypeDef
HAL_CRYP_TDESCBC_Decrypt_IT(CRYP_HandleTypeDef
*hcryp
, uint8_t *pCypherData
, uint16_t Size
, uint8_t *pPlainData
)
3375 uint32_t outputaddr
;
3377 if(hcryp
->State
== HAL_CRYP_STATE_READY
)
3379 /* Process Locked */
3382 hcryp
->CrypInCount
= Size
;
3383 hcryp
->pCrypInBuffPtr
= pCypherData
;
3384 hcryp
->pCrypOutBuffPtr
= pPlainData
;
3385 hcryp
->CrypOutCount
= Size
;
3387 /* Change the CRYP state */
3388 hcryp
->State
= HAL_CRYP_STATE_BUSY
;
3390 /* Set CRYP peripheral in TDES CBC decryption mode */
3391 CRYP_SetTDESCBCMode(hcryp
, CRYP_CR_ALGODIR
);
3393 /* Enable Interrupts */
3394 __HAL_CRYP_ENABLE_IT(hcryp
, CRYP_IT_INI
| CRYP_IT_OUTI
);
3397 __HAL_CRYP_ENABLE(hcryp
);
3399 /* Return function status */
3402 else if(__HAL_CRYP_GET_IT(hcryp
, CRYP_IT_INI
))
3404 inputaddr
= (uint32_t)hcryp
->pCrypInBuffPtr
;
3405 /* Write the Input block in the IN FIFO */
3406 hcryp
->Instance
->DR
= *(uint32_t*)(inputaddr
);
3408 hcryp
->Instance
->DR
= *(uint32_t*)(inputaddr
);
3410 hcryp
->pCrypInBuffPtr
+= 8;
3411 hcryp
->CrypInCount
-= 8;
3412 if(hcryp
->CrypInCount
== 0)
3414 __HAL_CRYP_DISABLE_IT(hcryp
, CRYP_IT_INI
);
3415 /* Call the Input data transfer complete callback */
3416 HAL_CRYP_InCpltCallback(hcryp
);
3419 else if(__HAL_CRYP_GET_IT(hcryp
, CRYP_IT_OUTI
))
3421 outputaddr
= (uint32_t)hcryp
->pCrypOutBuffPtr
;
3422 /* Read the Output block from the Output FIFO */
3423 *(uint32_t*)(outputaddr
) = hcryp
->Instance
->DOUT
;
3425 *(uint32_t*)(outputaddr
) = hcryp
->Instance
->DOUT
;
3427 hcryp
->pCrypOutBuffPtr
+= 8;
3428 hcryp
->CrypOutCount
-= 8;
3429 if(hcryp
->CrypOutCount
== 0)
3431 __HAL_CRYP_DISABLE_IT(hcryp
, CRYP_IT_OUTI
);
3433 __HAL_CRYP_DISABLE(hcryp
);
3434 /* Process Unlocked */
3435 __HAL_UNLOCK(hcryp
);
3436 /* Change the CRYP state */
3437 hcryp
->State
= HAL_CRYP_STATE_READY
;
3438 /* Call Input transfer complete callback */
3439 HAL_CRYP_OutCpltCallback(hcryp
);
3443 /* Return function status */
3448 * @brief Initializes the CRYP peripheral in TDES ECB encryption mode using DMA.
3449 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
3450 * the configuration information for CRYP module
3451 * @param pPlainData: Pointer to the plaintext buffer
3452 * @param Size: Length of the plaintext buffer, must be a multiple of 8
3453 * @param pCypherData: Pointer to the cyphertext buffer
3454 * @retval HAL status
3456 HAL_StatusTypeDef
HAL_CRYP_TDESECB_Encrypt_DMA(CRYP_HandleTypeDef
*hcryp
, uint8_t *pPlainData
, uint16_t Size
, uint8_t *pCypherData
)
3459 uint32_t outputaddr
;
3461 if((hcryp
->State
== HAL_CRYP_STATE_READY
) || (hcryp
->Phase
== HAL_CRYP_PHASE_PROCESS
))
3463 /* Process Locked */
3466 inputaddr
= (uint32_t)pPlainData
;
3467 outputaddr
= (uint32_t)pCypherData
;
3469 /* Change the CRYP state */
3470 hcryp
->State
= HAL_CRYP_STATE_BUSY
;
3472 /* Set CRYP peripheral in TDES ECB encryption mode */
3473 CRYP_SetTDESECBMode(hcryp
, 0);
3475 /* Set the input and output addresses and start DMA transfer */
3476 CRYP_SetDMAConfig(hcryp
, inputaddr
, Size
, outputaddr
);
3478 /* Process Unlocked */
3479 __HAL_UNLOCK(hcryp
);
3481 /* Return function status */
3491 * @brief Initializes the CRYP peripheral in TDES CBC encryption mode using DMA.
3492 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
3493 * the configuration information for CRYP module
3494 * @param pPlainData: Pointer to the plaintext buffer
3495 * @param Size: Length of the plaintext buffer, must be a multiple of 8
3496 * @param pCypherData: Pointer to the cyphertext buffer
3497 * @retval HAL status
3499 HAL_StatusTypeDef
HAL_CRYP_TDESCBC_Encrypt_DMA(CRYP_HandleTypeDef
*hcryp
, uint8_t *pPlainData
, uint16_t Size
, uint8_t *pCypherData
)
3502 uint32_t outputaddr
;
3504 if((hcryp
->State
== HAL_CRYP_STATE_READY
) || (hcryp
->Phase
== HAL_CRYP_PHASE_PROCESS
))
3506 /* Process Locked */
3509 inputaddr
= (uint32_t)pPlainData
;
3510 outputaddr
= (uint32_t)pCypherData
;
3512 /* Change the CRYP state */
3513 hcryp
->State
= HAL_CRYP_STATE_BUSY
;
3515 /* Set CRYP peripheral in TDES CBC encryption mode */
3516 CRYP_SetTDESCBCMode(hcryp
, 0);
3518 /* Set the input and output addresses and start DMA transfer */
3519 CRYP_SetDMAConfig(hcryp
, inputaddr
, Size
, outputaddr
);
3521 /* Process Unlocked */
3522 __HAL_UNLOCK(hcryp
);
3524 /* Return function status */
3534 * @brief Initializes the CRYP peripheral in TDES ECB decryption mode using DMA.
3535 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
3536 * the configuration information for CRYP module
3537 * @param pPlainData: Pointer to the plaintext buffer
3538 * @param Size: Length of the plaintext buffer, must be a multiple of 8
3539 * @param pCypherData: Pointer to the cyphertext buffer
3540 * @retval HAL status
3542 HAL_StatusTypeDef
HAL_CRYP_TDESECB_Decrypt_DMA(CRYP_HandleTypeDef
*hcryp
, uint8_t *pCypherData
, uint16_t Size
, uint8_t *pPlainData
)
3545 uint32_t outputaddr
;
3547 if((hcryp
->State
== HAL_CRYP_STATE_READY
) || (hcryp
->Phase
== HAL_CRYP_PHASE_PROCESS
))
3549 /* Process Locked */
3552 inputaddr
= (uint32_t)pCypherData
;
3553 outputaddr
= (uint32_t)pPlainData
;
3555 /* Change the CRYP state */
3556 hcryp
->State
= HAL_CRYP_STATE_BUSY
;
3558 /* Set CRYP peripheral in TDES ECB decryption mode */
3559 CRYP_SetTDESECBMode(hcryp
, CRYP_CR_ALGODIR
);
3561 /* Set the input and output addresses and start DMA transfer */
3562 CRYP_SetDMAConfig(hcryp
, inputaddr
, Size
, outputaddr
);
3564 /* Process Unlocked */
3565 __HAL_UNLOCK(hcryp
);
3567 /* Return function status */
3577 * @brief Initializes the CRYP peripheral in TDES CBC decryption mode using DMA.
3578 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
3579 * the configuration information for CRYP module
3580 * @param pCypherData: Pointer to the cyphertext buffer
3581 * @param Size: Length of the plaintext buffer, must be a multiple of 8
3582 * @param pPlainData: Pointer to the plaintext buffer
3583 * @retval HAL status
3585 HAL_StatusTypeDef
HAL_CRYP_TDESCBC_Decrypt_DMA(CRYP_HandleTypeDef
*hcryp
, uint8_t *pCypherData
, uint16_t Size
, uint8_t *pPlainData
)
3588 uint32_t outputaddr
;
3590 if((hcryp
->State
== HAL_CRYP_STATE_READY
) || (hcryp
->Phase
== HAL_CRYP_PHASE_PROCESS
))
3592 /* Process Locked */
3595 inputaddr
= (uint32_t)pCypherData
;
3596 outputaddr
= (uint32_t)pPlainData
;
3598 /* Change the CRYP state */
3599 hcryp
->State
= HAL_CRYP_STATE_BUSY
;
3601 /* Set CRYP peripheral in TDES CBC decryption mode */
3602 CRYP_SetTDESCBCMode(hcryp
, CRYP_CR_ALGODIR
);
3604 /* Set the input and output addresses and start DMA transfer */
3605 CRYP_SetDMAConfig(hcryp
, inputaddr
, Size
, outputaddr
);
3607 /* Process Unlocked */
3608 __HAL_UNLOCK(hcryp
);
3610 /* Return function status */
3623 /** @defgroup CRYP_Exported_Functions_Group5 DMA callback functions
3624 * @brief DMA callback functions.
3627 ==============================================================================
3628 ##### DMA callback functions #####
3629 ==============================================================================
3630 [..] This section provides DMA callback functions:
3631 (+) DMA Input data transfer complete
3632 (+) DMA Output data transfer complete
3640 * @brief Input FIFO transfer completed callbacks.
3641 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
3642 * the configuration information for CRYP module
3645 __weak
void HAL_CRYP_InCpltCallback(CRYP_HandleTypeDef
*hcryp
)
3647 /* Prevent unused argument(s) compilation warning */
3650 /* NOTE : This function Should not be modified, when the callback is needed,
3651 the HAL_CRYP_InCpltCallback could be implemented in the user file
3656 * @brief Output FIFO transfer completed callbacks.
3657 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
3658 * the configuration information for CRYP module
3661 __weak
void HAL_CRYP_OutCpltCallback(CRYP_HandleTypeDef
*hcryp
)
3663 /* Prevent unused argument(s) compilation warning */
3666 /* NOTE : This function Should not be modified, when the callback is needed,
3667 the HAL_CRYP_OutCpltCallback could be implemented in the user file
3672 * @brief CRYP error callbacks.
3673 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
3674 * the configuration information for CRYP module
3677 __weak
void HAL_CRYP_ErrorCallback(CRYP_HandleTypeDef
*hcryp
)
3679 /* Prevent unused argument(s) compilation warning */
3682 /* NOTE : This function Should not be modified, when the callback is needed,
3683 the HAL_CRYP_ErrorCallback could be implemented in the user file
3691 /** @defgroup CRYP_Exported_Functions_Group6 CRYP IRQ handler management
3692 * @brief CRYP IRQ handler.
3695 ==============================================================================
3696 ##### CRYP IRQ handler management #####
3697 ==============================================================================
3698 [..] This section provides CRYP IRQ handler function.
3705 * @brief This function handles CRYP interrupt request.
3706 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
3707 * the configuration information for CRYP module
3710 void HAL_CRYP_IRQHandler(CRYP_HandleTypeDef
*hcryp
)
3712 switch(CRYP
->CR
& CRYP_CR_ALGOMODE_DIRECTION
)
3714 case CRYP_CR_ALGOMODE_TDES_ECB_ENCRYPT
:
3715 HAL_CRYP_TDESECB_Encrypt_IT(hcryp
, NULL
, 0, NULL
);
3718 case CRYP_CR_ALGOMODE_TDES_ECB_DECRYPT
:
3719 HAL_CRYP_TDESECB_Decrypt_IT(hcryp
, NULL
, 0, NULL
);
3722 case CRYP_CR_ALGOMODE_TDES_CBC_ENCRYPT
:
3723 HAL_CRYP_TDESCBC_Encrypt_IT(hcryp
, NULL
, 0, NULL
);
3726 case CRYP_CR_ALGOMODE_TDES_CBC_DECRYPT
:
3727 HAL_CRYP_TDESCBC_Decrypt_IT(hcryp
, NULL
, 0, NULL
);
3730 case CRYP_CR_ALGOMODE_DES_ECB_ENCRYPT
:
3731 HAL_CRYP_DESECB_Encrypt_IT(hcryp
, NULL
, 0, NULL
);
3734 case CRYP_CR_ALGOMODE_DES_ECB_DECRYPT
:
3735 HAL_CRYP_DESECB_Decrypt_IT(hcryp
, NULL
, 0, NULL
);
3738 case CRYP_CR_ALGOMODE_DES_CBC_ENCRYPT
:
3739 HAL_CRYP_DESCBC_Encrypt_IT(hcryp
, NULL
, 0, NULL
);
3742 case CRYP_CR_ALGOMODE_DES_CBC_DECRYPT
:
3743 HAL_CRYP_DESCBC_Decrypt_IT(hcryp
, NULL
, 0, NULL
);
3746 case CRYP_CR_ALGOMODE_AES_ECB_ENCRYPT
:
3747 HAL_CRYP_AESECB_Encrypt_IT(hcryp
, NULL
, 0, NULL
);
3750 case CRYP_CR_ALGOMODE_AES_ECB_DECRYPT
:
3751 HAL_CRYP_AESECB_Decrypt_IT(hcryp
, NULL
, 0, NULL
);
3754 case CRYP_CR_ALGOMODE_AES_CBC_ENCRYPT
:
3755 HAL_CRYP_AESCBC_Encrypt_IT(hcryp
, NULL
, 0, NULL
);
3758 case CRYP_CR_ALGOMODE_AES_CBC_DECRYPT
:
3759 HAL_CRYP_AESCBC_Decrypt_IT(hcryp
, NULL
, 0, NULL
);
3762 case CRYP_CR_ALGOMODE_AES_CTR_ENCRYPT
:
3763 HAL_CRYP_AESCTR_Encrypt_IT(hcryp
, NULL
, 0, NULL
);
3766 case CRYP_CR_ALGOMODE_AES_CTR_DECRYPT
:
3767 HAL_CRYP_AESCTR_Decrypt_IT(hcryp
, NULL
, 0, NULL
);
3779 /** @defgroup CRYP_Exported_Functions_Group7 Peripheral State functions
3780 * @brief Peripheral State functions.
3783 ==============================================================================
3784 ##### Peripheral State functions #####
3785 ==============================================================================
3787 This subsection permits to get in run-time the status of the peripheral.
3794 * @brief Returns the CRYP state.
3795 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
3796 * the configuration information for CRYP module
3799 HAL_CRYP_STATETypeDef
HAL_CRYP_GetState(CRYP_HandleTypeDef
*hcryp
)
3801 return hcryp
->State
;
3822 /** @addtogroup STM32F7xx_HAL_Driver
3826 /** @defgroup AES AES
3827 * @brief AES HAL module driver.
3833 /* Private typedef -----------------------------------------------------------*/
3834 /* Private define ------------------------------------------------------------*/
3835 /* Private macro -------------------------------------------------------------*/
3836 /* Private variables ---------------------------------------------------------*/
3837 /* Private functions --------------------------------------------------------*/
3839 /** @defgroup CRYP_Private_Functions CRYP Private Functions
3843 static HAL_StatusTypeDef
CRYP_SetInitVector(CRYP_HandleTypeDef
*hcryp
);
3844 static HAL_StatusTypeDef
CRYP_SetKey(CRYP_HandleTypeDef
*hcryp
);
3845 static HAL_StatusTypeDef
CRYP_AES_IT(CRYP_HandleTypeDef
*hcryp
);
3851 /* Exported functions ---------------------------------------------------------*/
3853 /** @defgroup CRYP_Exported_Functions CRYP Exported Functions
3857 /** @defgroup CRYP_Exported_Functions_Group1 Initialization and deinitialization functions
3858 * @brief Initialization and Configuration functions.
3861 ==============================================================================
3862 ##### Initialization and deinitialization functions #####
3863 ==============================================================================
3864 [..] This section provides functions allowing to:
3865 (+) Initialize the CRYP according to the specified parameters
3866 in the CRYP_InitTypeDef and creates the associated handle
3867 (+) DeInitialize the CRYP peripheral
3868 (+) Initialize the CRYP MSP (MCU Specific Package)
3869 (+) De-Initialize the CRYP MSP
3872 (@) Specific care must be taken to format the key and the Initialization Vector IV!
3874 [..] If the key is defined as a 128-bit long array key[127..0] = {b127 ... b0} where
3875 b127 is the MSB and b0 the LSB, the key must be stored in MCU memory
3876 (+) as a sequence of words where the MSB word comes first (occupies the
3877 lowest memory address)
3878 (+) where each word is byte-swapped:
3879 (++) address n+0 : 0b b103 .. b96 b111 .. b104 b119 .. b112 b127 .. b120
3880 (++) address n+4 : 0b b71 .. b64 b79 .. b72 b87 .. b80 b95 .. b88
3881 (++) address n+8 : 0b b39 .. b32 b47 .. b40 b55 .. b48 b63 .. b56
3882 (++) address n+C : 0b b7 .. b0 b15 .. b8 b23 .. b16 b31 .. b24
3883 [..] Hereafter, another illustration when considering a 128-bit long key made of 16 bytes {B15..B0}.
3884 The 4 32-bit words that make the key must be stored as follows in MCU memory:
3885 (+) address n+0 : 0x B12 B13 B14 B15
3886 (+) address n+4 : 0x B8 B9 B10 B11
3887 (+) address n+8 : 0x B4 B5 B6 B7
3888 (+) address n+C : 0x B0 B1 B2 B3
3889 [..] which leads to the expected setting
3890 (+) AES_KEYR3 = 0x B15 B14 B13 B12
3891 (+) AES_KEYR2 = 0x B11 B10 B9 B8
3892 (+) AES_KEYR1 = 0x B7 B6 B5 B4
3893 (+) AES_KEYR0 = 0x B3 B2 B1 B0
3895 [..] Same format must be applied for a 256-bit long key made of 32 bytes {B31..B0}.
3896 The 8 32-bit words that make the key must be stored as follows in MCU memory:
3897 (+) address n+00 : 0x B28 B29 B30 B31
3898 (+) address n+04 : 0x B24 B25 B26 B27
3899 (+) address n+08 : 0x B20 B21 B22 B23
3900 (+) address n+0C : 0x B16 B17 B18 B19
3901 (+) address n+10 : 0x B12 B13 B14 B15
3902 (+) address n+14 : 0x B8 B9 B10 B11
3903 (+) address n+18 : 0x B4 B5 B6 B7
3904 (+) address n+1C : 0x B0 B1 B2 B3
3905 [..] which leads to the expected setting
3906 (+) AES_KEYR7 = 0x B31 B30 B29 B28
3907 (+) AES_KEYR6 = 0x B27 B26 B25 B24
3908 (+) AES_KEYR5 = 0x B23 B22 B21 B20
3909 (+) AES_KEYR4 = 0x B19 B18 B17 B16
3910 (+) AES_KEYR3 = 0x B15 B14 B13 B12
3911 (+) AES_KEYR2 = 0x B11 B10 B9 B8
3912 (+) AES_KEYR1 = 0x B7 B6 B5 B4
3913 (+) AES_KEYR0 = 0x B3 B2 B1 B0
3915 [..] Initialization Vector IV (4 32-bit words) format must follow the same as
3916 that of a 128-bit long key.
3925 * @brief Initialize the CRYP according to the specified
3926 * parameters in the CRYP_InitTypeDef and initialize the associated handle.
3927 * @note Specific care must be taken to format the key and the Initialization Vector IV
3928 * stored in the MCU memory before calling HAL_CRYP_Init(). Refer to explanations
3930 * @retval HAL status
3932 HAL_StatusTypeDef
HAL_CRYP_Init(CRYP_HandleTypeDef
*hcryp
)
3934 /* Check the CRYP handle allocation */
3940 /* Check the instance */
3941 assert_param(IS_AES_ALL_INSTANCE(hcryp
->Instance
));
3943 /* Check the parameters */
3944 assert_param(IS_CRYP_KEYSIZE(hcryp
->Init
.KeySize
));
3945 assert_param(IS_CRYP_DATATYPE(hcryp
->Init
.DataType
));
3946 assert_param(IS_CRYP_ALGOMODE(hcryp
->Init
.OperatingMode
));
3947 /* ChainingMode parameter is irrelevant when mode is set to Key derivation */
3948 if (hcryp
->Init
.OperatingMode
!= CRYP_ALGOMODE_KEYDERIVATION
)
3950 assert_param(IS_CRYP_CHAINMODE(hcryp
->Init
.ChainingMode
));
3952 assert_param(IS_CRYP_WRITE(hcryp
->Init
.KeyWriteFlag
));
3954 /*========================================================*/
3955 /* Check the proper operating/chaining modes combinations */
3956 /*========================================================*/
3957 /* Check the proper chaining when the operating mode is key derivation and decryption */
3958 #if defined(AES_CR_NPBLB)
3959 if ((hcryp
->Init
.OperatingMode
== CRYP_ALGOMODE_KEYDERIVATION_DECRYPT
) &&\
3960 ((hcryp
->Init
.ChainingMode
== CRYP_CHAINMODE_AES_CTR
) \
3961 || (hcryp
->Init
.ChainingMode
== CRYP_CHAINMODE_AES_GCM_GMAC
) \
3962 || (hcryp
->Init
.ChainingMode
== CRYP_CHAINMODE_AES_CCM_CMAC
)))
3964 if ((hcryp
->Init
.OperatingMode
== CRYP_ALGOMODE_KEYDERIVATION_DECRYPT
) &&\
3965 ((hcryp
->Init
.ChainingMode
== CRYP_CHAINMODE_AES_CTR
) \
3966 || (hcryp
->Init
.ChainingMode
== CRYP_CHAINMODE_AES_GCM_GMAC
) \
3967 || (hcryp
->Init
.ChainingMode
== CRYP_CHAINMODE_AES_CMAC
)))
3972 /* Check that key derivation is not set in CMAC mode or CCM mode when applicable */
3973 #if defined(AES_CR_NPBLB)
3974 if ((hcryp
->Init
.OperatingMode
== CRYP_ALGOMODE_KEYDERIVATION
)
3975 && (hcryp
->Init
.ChainingMode
== CRYP_CHAINMODE_AES_CCM_CMAC
))
3977 if ((hcryp
->Init
.OperatingMode
== CRYP_ALGOMODE_KEYDERIVATION
)
3978 && (hcryp
->Init
.ChainingMode
== CRYP_CHAINMODE_AES_CMAC
))
3985 /*================*/
3986 /* Initialization */
3987 /*================*/
3988 /* Initialization start */
3989 if(hcryp
->State
== HAL_CRYP_STATE_RESET
)
3991 /* Allocate lock resource and initialize it */
3992 hcryp
->Lock
= HAL_UNLOCKED
;
3994 /* Init the low level hardware */
3995 HAL_CRYP_MspInit(hcryp
);
3998 /* Change the CRYP state */
3999 hcryp
->State
= HAL_CRYP_STATE_BUSY
;
4001 /* Disable the Peripheral */
4002 __HAL_CRYP_DISABLE();
4004 /*=============================================================*/
4005 /* AES initialization common to all operating modes */
4006 /*=============================================================*/
4007 /* Set the Key size selection */
4008 MODIFY_REG(hcryp
->Instance
->CR
, AES_CR_KEYSIZE
, hcryp
->Init
.KeySize
);
4010 /* Set the default CRYP phase when this parameter is not used.
4011 Phase is updated below in case of GCM/GMAC/CMAC(/CCM) setting. */
4012 hcryp
->Phase
= HAL_CRYP_PHASE_NOT_USED
;
4016 /*=============================================================*/
4017 /* Carry on the initialization based on the AES operating mode */
4018 /*=============================================================*/
4019 /* Key derivation */
4020 if (hcryp
->Init
.OperatingMode
== CRYP_ALGOMODE_KEYDERIVATION
)
4022 MODIFY_REG(hcryp
->Instance
->CR
, AES_CR_MODE
, CRYP_ALGOMODE_KEYDERIVATION
);
4024 /* Configure the Key registers */
4025 if (CRYP_SetKey(hcryp
) != HAL_OK
)
4031 /* Encryption / Decryption (with or without key derivation) / authentication */
4033 /* Set data type, operating and chaining modes.
4034 In case of GCM or GMAC, data type is forced to 0b00 */
4035 if (hcryp
->Init
.ChainingMode
== CRYP_CHAINMODE_AES_GCM_GMAC
)
4037 MODIFY_REG(hcryp
->Instance
->CR
, AES_CR_DATATYPE
|AES_CR_MODE
|AES_CR_CHMOD
, hcryp
->Init
.OperatingMode
|hcryp
->Init
.ChainingMode
);
4041 MODIFY_REG(hcryp
->Instance
->CR
, AES_CR_DATATYPE
|AES_CR_MODE
|AES_CR_CHMOD
, hcryp
->Init
.DataType
|hcryp
->Init
.OperatingMode
|hcryp
->Init
.ChainingMode
);
4045 /* Specify the encryption/decryption phase in case of Galois counter mode (GCM),
4046 Galois message authentication code (GMAC), cipher message authentication code (CMAC)
4047 or Counter with Cipher Mode (CCM) when applicable */
4048 #if defined(AES_CR_NPBLB)
4049 if ((hcryp
->Init
.ChainingMode
== CRYP_CHAINMODE_AES_GCM_GMAC
)
4050 || (hcryp
->Init
.ChainingMode
== CRYP_CHAINMODE_AES_CCM_CMAC
))
4052 if ((hcryp
->Init
.ChainingMode
== CRYP_CHAINMODE_AES_GCM_GMAC
)
4053 || (hcryp
->Init
.ChainingMode
== CRYP_CHAINMODE_AES_CMAC
))
4056 MODIFY_REG(hcryp
->Instance
->CR
, AES_CR_GCMPH
, hcryp
->Init
.GCMCMACPhase
);
4057 hcryp
->Phase
= HAL_CRYP_PHASE_START
;
4061 /* Configure the Key registers if no need to bypass this step */
4062 if (hcryp
->Init
.KeyWriteFlag
== CRYP_KEY_WRITE_ENABLE
)
4064 if (CRYP_SetKey(hcryp
) != HAL_OK
)
4070 /* If applicable, configure the Initialization Vector */
4071 if (hcryp
->Init
.ChainingMode
!= CRYP_CHAINMODE_AES_ECB
)
4073 if (CRYP_SetInitVector(hcryp
) != HAL_OK
)
4080 #if defined(AES_CR_NPBLB)
4081 /* Clear NPBLB field */
4082 CLEAR_BIT(hcryp
->Instance
->CR
, AES_CR_NPBLB
);
4085 /* Reset CrypInCount and CrypOutCount */
4086 hcryp
->CrypInCount
= 0;
4087 hcryp
->CrypOutCount
= 0;
4089 /* Reset ErrorCode field */
4090 hcryp
->ErrorCode
= HAL_CRYP_ERROR_NONE
;
4092 /* Reset Mode suspension request */
4093 hcryp
->SuspendRequest
= HAL_CRYP_SUSPEND_NONE
;
4095 /* Change the CRYP state */
4096 hcryp
->State
= HAL_CRYP_STATE_READY
;
4098 /* Enable the Peripheral */
4099 __HAL_CRYP_ENABLE();
4101 /* Return function status */
4106 * @brief DeInitialize the CRYP peripheral.
4107 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
4108 * the configuration information for CRYP module
4109 * @retval HAL status
4111 HAL_StatusTypeDef
HAL_CRYP_DeInit(CRYP_HandleTypeDef
*hcryp
)
4113 /* Check the CRYP handle allocation */
4119 /* Change the CRYP state */
4120 hcryp
->State
= HAL_CRYP_STATE_BUSY
;
4122 /* Set the default CRYP phase */
4123 hcryp
->Phase
= HAL_CRYP_PHASE_READY
;
4125 /* Reset CrypInCount and CrypOutCount */
4126 hcryp
->CrypInCount
= 0;
4127 hcryp
->CrypOutCount
= 0;
4129 /* Disable the CRYP Peripheral Clock */
4130 __HAL_CRYP_DISABLE();
4132 /* DeInit the low level hardware: CLOCK, NVIC.*/
4133 HAL_CRYP_MspDeInit(hcryp
);
4135 /* Change the CRYP state */
4136 hcryp
->State
= HAL_CRYP_STATE_RESET
;
4139 __HAL_UNLOCK(hcryp
);
4141 /* Return function status */
4146 * @brief Initialize the CRYP MSP.
4147 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
4148 * the configuration information for CRYP module
4151 __weak
void HAL_CRYP_MspInit(CRYP_HandleTypeDef
*hcryp
)
4153 /* Prevent unused argument(s) compilation warning */
4156 /* NOTE : This function should not be modified; when the callback is needed,
4157 the HAL_CRYP_MspInit can be implemented in the user file
4162 * @brief DeInitialize CRYP MSP.
4163 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
4164 * the configuration information for CRYP module
4167 __weak
void HAL_CRYP_MspDeInit(CRYP_HandleTypeDef
*hcryp
)
4169 /* Prevent unused argument(s) compilation warning */
4172 /* NOTE : This function should not be modified; when the callback is needed,
4173 the HAL_CRYP_MspDeInit can be implemented in the user file
4181 /** @defgroup CRYP_Exported_Functions_Group2 AES processing functions
4182 * @brief Processing functions.
4185 ==============================================================================
4186 ##### AES processing functions #####
4187 ==============================================================================
4188 [..] This section provides functions allowing to:
4189 (+) Encrypt plaintext using AES algorithm in different chaining modes
4190 (+) Decrypt cyphertext using AES algorithm in different chaining modes
4191 [..] Three processing functions are available:
4202 * @brief Encrypt pPlainData in AES ECB encryption mode. The cypher data are available in pCypherData.
4203 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
4204 * the configuration information for CRYP module
4205 * @param pPlainData: Pointer to the plaintext buffer
4206 * @param Size: Length of the plaintext buffer in bytes, must be a multiple of 16.
4207 * @param pCypherData: Pointer to the cyphertext buffer
4208 * @param Timeout: Specify Timeout value
4209 * @note This API is provided only to maintain compatibility with legacy software. Users should directly
4210 * resort to generic HAL_CRYPEx_AES() API instead (usage recommended).
4211 * @retval HAL status
4213 HAL_StatusTypeDef
HAL_CRYP_AESECB_Encrypt(CRYP_HandleTypeDef
*hcryp
, uint8_t *pPlainData
, uint16_t Size
, uint8_t *pCypherData
, uint32_t Timeout
)
4215 /* Re-initialize AES IP with proper parameters */
4216 if (HAL_CRYP_DeInit(hcryp
) != HAL_OK
)
4220 hcryp
->Init
.OperatingMode
= CRYP_ALGOMODE_ENCRYPT
;
4221 hcryp
->Init
.ChainingMode
= CRYP_CHAINMODE_AES_ECB
;
4222 hcryp
->Init
.KeyWriteFlag
= CRYP_KEY_WRITE_ENABLE
;
4223 if (HAL_CRYP_Init(hcryp
) != HAL_OK
)
4228 return HAL_CRYPEx_AES(hcryp
, pPlainData
, Size
, pCypherData
, Timeout
);
4233 * @brief Encrypt pPlainData in AES CBC encryption mode with key derivation. The cypher data are available in pCypherData.
4234 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
4235 * the configuration information for CRYP module
4236 * @param pPlainData: Pointer to the plaintext buffer
4237 * @param Size: Length of the plaintext buffer in bytes, must be a multiple of 16.
4238 * @param pCypherData: Pointer to the cyphertext buffer
4239 * @param Timeout: Specify Timeout value
4240 * @note This API is provided only to maintain compatibility with legacy software. Users should directly
4241 * resort to generic HAL_CRYPEx_AES() API instead (usage recommended).
4242 * @retval HAL status
4244 HAL_StatusTypeDef
HAL_CRYP_AESCBC_Encrypt(CRYP_HandleTypeDef
*hcryp
, uint8_t *pPlainData
, uint16_t Size
, uint8_t *pCypherData
, uint32_t Timeout
)
4246 /* Re-initialize AES IP with proper parameters */
4247 if (HAL_CRYP_DeInit(hcryp
) != HAL_OK
)
4251 hcryp
->Init
.OperatingMode
= CRYP_ALGOMODE_ENCRYPT
;
4252 hcryp
->Init
.ChainingMode
= CRYP_CHAINMODE_AES_CBC
;
4253 hcryp
->Init
.KeyWriteFlag
= CRYP_KEY_WRITE_ENABLE
;
4254 if (HAL_CRYP_Init(hcryp
) != HAL_OK
)
4259 return HAL_CRYPEx_AES(hcryp
, pPlainData
, Size
, pCypherData
, Timeout
);
4264 * @brief Encrypt pPlainData in AES CTR encryption mode. The cypher data are available in pCypherData
4265 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
4266 * the configuration information for CRYP module
4267 * @param pPlainData: Pointer to the plaintext buffer
4268 * @param Size: Length of the plaintext buffer in bytes, must be a multiple of 16.
4269 * @param pCypherData: Pointer to the cyphertext buffer
4270 * @param Timeout: Specify Timeout value
4271 * @note This API is provided only to maintain compatibility with legacy software. Users should directly
4272 * resort to generic HAL_CRYPEx_AES() API instead (usage recommended).
4273 * @retval HAL status
4275 HAL_StatusTypeDef
HAL_CRYP_AESCTR_Encrypt(CRYP_HandleTypeDef
*hcryp
, uint8_t *pPlainData
, uint16_t Size
, uint8_t *pCypherData
, uint32_t Timeout
)
4277 /* Re-initialize AES IP with proper parameters */
4278 if (HAL_CRYP_DeInit(hcryp
) != HAL_OK
)
4282 hcryp
->Init
.OperatingMode
= CRYP_ALGOMODE_ENCRYPT
;
4283 hcryp
->Init
.ChainingMode
= CRYP_CHAINMODE_AES_CTR
;
4284 hcryp
->Init
.KeyWriteFlag
= CRYP_KEY_WRITE_ENABLE
;
4285 if (HAL_CRYP_Init(hcryp
) != HAL_OK
)
4290 return HAL_CRYPEx_AES(hcryp
, pPlainData
, Size
, pCypherData
, Timeout
);
4294 * @brief Decrypt pCypherData in AES ECB decryption mode with key derivation,
4295 * the decyphered data are available in pPlainData.
4296 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
4297 * the configuration information for CRYP module
4298 * @param pCypherData: Pointer to the cyphertext buffer
4299 * @param Size: Length of the plaintext buffer in bytes, must be a multiple of 16.
4300 * @param pPlainData: Pointer to the plaintext buffer
4301 * @param Timeout: Specify Timeout value
4302 * @note This API is provided only to maintain compatibility with legacy software. Users should directly
4303 * resort to generic HAL_CRYPEx_AES() API instead (usage recommended).
4304 * @retval HAL status
4306 HAL_StatusTypeDef
HAL_CRYP_AESECB_Decrypt(CRYP_HandleTypeDef
*hcryp
, uint8_t *pCypherData
, uint16_t Size
, uint8_t *pPlainData
, uint32_t Timeout
)
4308 /* Re-initialize AES IP with proper parameters */
4309 if (HAL_CRYP_DeInit(hcryp
) != HAL_OK
)
4313 hcryp
->Init
.OperatingMode
= CRYP_ALGOMODE_KEYDERIVATION_DECRYPT
;
4314 hcryp
->Init
.ChainingMode
= CRYP_CHAINMODE_AES_ECB
;
4315 hcryp
->Init
.KeyWriteFlag
= CRYP_KEY_WRITE_ENABLE
;
4316 if (HAL_CRYP_Init(hcryp
) != HAL_OK
)
4321 return HAL_CRYPEx_AES(hcryp
, pCypherData
, Size
, pPlainData
, Timeout
);
4325 * @brief Decrypt pCypherData in AES ECB decryption mode with key derivation,
4326 * the decyphered data are available in pPlainData.
4327 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
4328 * the configuration information for CRYP module
4329 * @param pCypherData: Pointer to the cyphertext buffer
4330 * @param Size: Length of the plaintext buffer in bytes, must be a multiple of 16.
4331 * @param pPlainData: Pointer to the plaintext buffer
4332 * @param Timeout: Specify Timeout value
4333 * @note This API is provided only to maintain compatibility with legacy software. Users should directly
4334 * resort to generic HAL_CRYPEx_AES() API instead (usage recommended).
4335 * @retval HAL status
4337 HAL_StatusTypeDef
HAL_CRYP_AESCBC_Decrypt(CRYP_HandleTypeDef
*hcryp
, uint8_t *pCypherData
, uint16_t Size
, uint8_t *pPlainData
, uint32_t Timeout
)
4339 /* Re-initialize AES IP with proper parameters */
4340 if (HAL_CRYP_DeInit(hcryp
) != HAL_OK
)
4344 hcryp
->Init
.OperatingMode
= CRYP_ALGOMODE_KEYDERIVATION_DECRYPT
;
4345 hcryp
->Init
.ChainingMode
= CRYP_CHAINMODE_AES_CBC
;
4346 hcryp
->Init
.KeyWriteFlag
= CRYP_KEY_WRITE_ENABLE
;
4347 if (HAL_CRYP_Init(hcryp
) != HAL_OK
)
4352 return HAL_CRYPEx_AES(hcryp
, pCypherData
, Size
, pPlainData
, Timeout
);
4356 * @brief Decrypt pCypherData in AES CTR decryption mode,
4357 * the decyphered data are available in pPlainData.
4358 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
4359 * the configuration information for CRYP module
4360 * @param pCypherData: Pointer to the cyphertext buffer
4361 * @param Size: Length of the plaintext buffer in bytes, must be a multiple of 16.
4362 * @param pPlainData: Pointer to the plaintext buffer
4363 * @param Timeout: Specify Timeout value
4364 * @note This API is provided only to maintain compatibility with legacy software. Users should directly
4365 * resort to generic HAL_CRYPEx_AES() API instead (usage recommended).
4366 * @retval HAL status
4368 HAL_StatusTypeDef
HAL_CRYP_AESCTR_Decrypt(CRYP_HandleTypeDef
*hcryp
, uint8_t *pCypherData
, uint16_t Size
, uint8_t *pPlainData
, uint32_t Timeout
)
4370 /* Re-initialize AES IP with proper parameters */
4371 if (HAL_CRYP_DeInit(hcryp
) != HAL_OK
)
4375 hcryp
->Init
.OperatingMode
= CRYP_ALGOMODE_DECRYPT
;
4376 hcryp
->Init
.ChainingMode
= CRYP_CHAINMODE_AES_CTR
;
4377 hcryp
->Init
.KeyWriteFlag
= CRYP_KEY_WRITE_ENABLE
;
4378 if (HAL_CRYP_Init(hcryp
) != HAL_OK
)
4383 return HAL_CRYPEx_AES(hcryp
, pCypherData
, Size
, pPlainData
, Timeout
);
4387 * @brief Encrypt pPlainData in AES ECB encryption mode using Interrupt,
4388 * the cypher data are available in pCypherData.
4389 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
4390 * the configuration information for CRYP module
4391 * @param pPlainData: Pointer to the plaintext buffer
4392 * @param Size: Length of the plaintext buffer in bytes, must be a multiple of 16.
4393 * @param pCypherData: Pointer to the cyphertext buffer
4394 * @note This API is provided only to maintain compatibility with legacy software. Users should directly
4395 * resort to generic HAL_CRYPEx_AES_IT() API instead (usage recommended).
4396 * @retval HAL status
4398 HAL_StatusTypeDef
HAL_CRYP_AESECB_Encrypt_IT(CRYP_HandleTypeDef
*hcryp
, uint8_t *pPlainData
, uint16_t Size
, uint8_t *pCypherData
)
4400 /* Re-initialize AES IP with proper parameters */
4401 if (HAL_CRYP_DeInit(hcryp
) != HAL_OK
)
4405 hcryp
->Init
.OperatingMode
= CRYP_ALGOMODE_ENCRYPT
;
4406 hcryp
->Init
.ChainingMode
= CRYP_CHAINMODE_AES_ECB
;
4407 hcryp
->Init
.KeyWriteFlag
= CRYP_KEY_WRITE_ENABLE
;
4408 if (HAL_CRYP_Init(hcryp
) != HAL_OK
)
4413 return HAL_CRYPEx_AES_IT(hcryp
, pPlainData
, Size
, pCypherData
);
4417 * @brief Encrypt pPlainData in AES CBC encryption mode using Interrupt,
4418 * the cypher data are available in pCypherData.
4419 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
4420 * the configuration information for CRYP module
4421 * @param pPlainData: Pointer to the plaintext buffer
4422 * @param Size: Length of the plaintext buffer in bytes, must be a multiple of 16.
4423 * @param pCypherData: Pointer to the cyphertext buffer
4424 * @note This API is provided only to maintain compatibility with legacy software. Users should directly
4425 * resort to generic HAL_CRYPEx_AES_IT() API instead (usage recommended).
4426 * @retval HAL status
4428 HAL_StatusTypeDef
HAL_CRYP_AESCBC_Encrypt_IT(CRYP_HandleTypeDef
*hcryp
, uint8_t *pPlainData
, uint16_t Size
, uint8_t *pCypherData
)
4430 /* Re-initialize AES IP with proper parameters */
4431 if (HAL_CRYP_DeInit(hcryp
) != HAL_OK
)
4435 hcryp
->Init
.OperatingMode
= CRYP_ALGOMODE_ENCRYPT
;
4436 hcryp
->Init
.ChainingMode
= CRYP_CHAINMODE_AES_CBC
;
4437 hcryp
->Init
.KeyWriteFlag
= CRYP_KEY_WRITE_ENABLE
;
4438 if (HAL_CRYP_Init(hcryp
) != HAL_OK
)
4443 return HAL_CRYPEx_AES_IT(hcryp
, pPlainData
, Size
, pCypherData
);
4448 * @brief Encrypt pPlainData in AES CTR encryption mode using Interrupt,
4449 * the cypher data are available in pCypherData.
4450 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
4451 * the configuration information for CRYP module
4452 * @param pPlainData: Pointer to the plaintext buffer
4453 * @param Size: Length of the plaintext buffer in bytes, must be a multiple of 16.
4454 * @param pCypherData: Pointer to the cyphertext buffer
4455 * @note This API is provided only to maintain compatibility with legacy software. Users should directly
4456 * resort to generic HAL_CRYPEx_AES_IT() API instead (usage recommended).
4457 * @retval HAL status
4459 HAL_StatusTypeDef
HAL_CRYP_AESCTR_Encrypt_IT(CRYP_HandleTypeDef
*hcryp
, uint8_t *pPlainData
, uint16_t Size
, uint8_t *pCypherData
)
4461 /* Re-initialize AES IP with proper parameters */
4462 if (HAL_CRYP_DeInit(hcryp
) != HAL_OK
)
4466 hcryp
->Init
.OperatingMode
= CRYP_ALGOMODE_ENCRYPT
;
4467 hcryp
->Init
.ChainingMode
= CRYP_CHAINMODE_AES_CTR
;
4468 hcryp
->Init
.KeyWriteFlag
= CRYP_KEY_WRITE_ENABLE
;
4469 if (HAL_CRYP_Init(hcryp
) != HAL_OK
)
4474 return HAL_CRYPEx_AES_IT(hcryp
, pPlainData
, Size
, pCypherData
);
4478 * @brief Decrypt pCypherData in AES ECB decryption mode using Interrupt,
4479 * the decyphered data are available in pPlainData.
4480 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
4481 * the configuration information for CRYP module
4482 * @param pCypherData: Pointer to the cyphertext buffer
4483 * @param Size: Length of the plaintext buffer in bytes, must be a multiple of 16.
4484 * @param pPlainData: Pointer to the plaintext buffer.
4485 * @note This API is provided only to maintain compatibility with legacy software. Users should directly
4486 * resort to generic HAL_CRYPEx_AES_IT() API instead (usage recommended).
4487 * @retval HAL status
4489 HAL_StatusTypeDef
HAL_CRYP_AESECB_Decrypt_IT(CRYP_HandleTypeDef
*hcryp
, uint8_t *pCypherData
, uint16_t Size
, uint8_t *pPlainData
)
4491 /* Re-initialize AES IP with proper parameters */
4492 if (HAL_CRYP_DeInit(hcryp
) != HAL_OK
)
4496 hcryp
->Init
.OperatingMode
= CRYP_ALGOMODE_KEYDERIVATION_DECRYPT
;
4497 hcryp
->Init
.ChainingMode
= CRYP_CHAINMODE_AES_ECB
;
4498 hcryp
->Init
.KeyWriteFlag
= CRYP_KEY_WRITE_ENABLE
;
4499 if (HAL_CRYP_Init(hcryp
) != HAL_OK
)
4504 return HAL_CRYPEx_AES_IT(hcryp
, pCypherData
, Size
, pPlainData
);
4508 * @brief Decrypt pCypherData in AES CBC decryption mode using Interrupt,
4509 * the decyphered data are available in pPlainData.
4510 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
4511 * the configuration information for CRYP module
4512 * @param pCypherData: Pointer to the cyphertext buffer
4513 * @param Size: Length of the plaintext buffer in bytes, must be a multiple of 16.
4514 * @param pPlainData: Pointer to the plaintext buffer
4515 * @note This API is provided only to maintain compatibility with legacy software. Users should directly
4516 * resort to generic HAL_CRYPEx_AES_IT() API instead (usage recommended).
4517 * @retval HAL status
4519 HAL_StatusTypeDef
HAL_CRYP_AESCBC_Decrypt_IT(CRYP_HandleTypeDef
*hcryp
, uint8_t *pCypherData
, uint16_t Size
, uint8_t *pPlainData
)
4521 /* Re-initialize AES IP with proper parameters */
4522 if (HAL_CRYP_DeInit(hcryp
) != HAL_OK
)
4526 hcryp
->Init
.OperatingMode
= CRYP_ALGOMODE_KEYDERIVATION_DECRYPT
;
4527 hcryp
->Init
.ChainingMode
= CRYP_CHAINMODE_AES_CBC
;
4528 hcryp
->Init
.KeyWriteFlag
= CRYP_KEY_WRITE_ENABLE
;
4529 if (HAL_CRYP_Init(hcryp
) != HAL_OK
)
4534 return HAL_CRYPEx_AES_IT(hcryp
, pCypherData
, Size
, pPlainData
);
4538 * @brief Decrypt pCypherData in AES CTR decryption mode using Interrupt,
4539 * the decyphered data are available in pPlainData.
4540 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
4541 * the configuration information for CRYP module
4542 * @param pCypherData: Pointer to the cyphertext buffer
4543 * @param Size: Length of the plaintext buffer in bytes, must be a multiple of 16.
4544 * @param pPlainData: Pointer to the plaintext buffer
4545 * @note This API is provided only to maintain compatibility with legacy software. Users should directly
4546 * resort to generic HAL_CRYPEx_AES_IT() API instead (usage recommended).
4547 * @retval HAL status
4549 HAL_StatusTypeDef
HAL_CRYP_AESCTR_Decrypt_IT(CRYP_HandleTypeDef
*hcryp
, uint8_t *pCypherData
, uint16_t Size
, uint8_t *pPlainData
)
4551 /* Re-initialize AES IP with proper parameters */
4552 if (HAL_CRYP_DeInit(hcryp
) != HAL_OK
)
4556 hcryp
->Init
.OperatingMode
= CRYP_ALGOMODE_DECRYPT
;
4557 hcryp
->Init
.ChainingMode
= CRYP_CHAINMODE_AES_CTR
;
4558 hcryp
->Init
.KeyWriteFlag
= CRYP_KEY_WRITE_ENABLE
;
4559 if (HAL_CRYP_Init(hcryp
) != HAL_OK
)
4564 return HAL_CRYPEx_AES_IT(hcryp
, pCypherData
, Size
, pPlainData
);
4568 * @brief Encrypt pPlainData in AES ECB encryption mode using DMA,
4569 * the cypher data are available in pCypherData.
4570 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
4571 * the configuration information for CRYP module
4572 * @param pPlainData: Pointer to the plaintext buffer
4573 * @param Size: Length of the plaintext buffer in bytes, must be a multiple of 16.
4574 * @param pCypherData: Pointer to the cyphertext buffer
4575 * @note This API is provided only to maintain compatibility with legacy software. Users should directly
4576 * resort to generic HAL_CRYPEx_AES_DMA() API instead (usage recommended).
4577 * @note pPlainData and pCypherData buffers must be 32-bit aligned to ensure a correct DMA transfer to and from the IP.
4578 * @retval HAL status
4580 HAL_StatusTypeDef
HAL_CRYP_AESECB_Encrypt_DMA(CRYP_HandleTypeDef
*hcryp
, uint8_t *pPlainData
, uint16_t Size
, uint8_t *pCypherData
)
4582 /* Re-initialize AES IP with proper parameters */
4583 if (HAL_CRYP_DeInit(hcryp
) != HAL_OK
)
4587 hcryp
->Init
.OperatingMode
= CRYP_ALGOMODE_ENCRYPT
;
4588 hcryp
->Init
.ChainingMode
= CRYP_CHAINMODE_AES_ECB
;
4589 hcryp
->Init
.KeyWriteFlag
= CRYP_KEY_WRITE_ENABLE
;
4590 if (HAL_CRYP_Init(hcryp
) != HAL_OK
)
4595 return HAL_CRYPEx_AES_DMA(hcryp
, pPlainData
, Size
, pCypherData
);
4601 * @brief Encrypt pPlainData in AES CBC encryption mode using DMA,
4602 * the cypher data are available in pCypherData.
4603 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
4604 * the configuration information for CRYP module
4605 * @param pPlainData: Pointer to the plaintext buffer
4606 * @param Size: Length of the plaintext buffer, must be a multiple of 16.
4607 * @param pCypherData: Pointer to the cyphertext buffer
4608 * @note This API is provided only to maintain compatibility with legacy software. Users should directly
4609 * resort to generic HAL_CRYPEx_AES_DMA() API instead (usage recommended).
4610 * @note pPlainData and pCypherData buffers must be 32-bit aligned to ensure a correct DMA transfer to and from the IP.
4611 * @retval HAL status
4613 HAL_StatusTypeDef
HAL_CRYP_AESCBC_Encrypt_DMA(CRYP_HandleTypeDef
*hcryp
, uint8_t *pPlainData
, uint16_t Size
, uint8_t *pCypherData
)
4615 /* Re-initialize AES IP with proper parameters */
4616 if (HAL_CRYP_DeInit(hcryp
) != HAL_OK
)
4620 hcryp
->Init
.OperatingMode
= CRYP_ALGOMODE_ENCRYPT
;
4621 hcryp
->Init
.ChainingMode
= CRYP_CHAINMODE_AES_CBC
;
4622 hcryp
->Init
.KeyWriteFlag
= CRYP_KEY_WRITE_ENABLE
;
4623 if (HAL_CRYP_Init(hcryp
) != HAL_OK
)
4628 return HAL_CRYPEx_AES_DMA(hcryp
, pPlainData
, Size
, pCypherData
);
4632 * @brief Encrypt pPlainData in AES CTR encryption mode using DMA,
4633 * the cypher data are available in pCypherData.
4634 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
4635 * the configuration information for CRYP module
4636 * @param pPlainData: Pointer to the plaintext buffer
4637 * @param Size: Length of the plaintext buffer in bytes, must be a multiple of 16.
4638 * @param pCypherData: Pointer to the cyphertext buffer.
4639 * @note This API is provided only to maintain compatibility with legacy software. Users should directly
4640 * resort to generic HAL_CRYPEx_AES_DMA() API instead (usage recommended).
4641 * @note pPlainData and pCypherData buffers must be 32-bit aligned to ensure a correct DMA transfer to and from the IP.
4642 * @retval HAL status
4644 HAL_StatusTypeDef
HAL_CRYP_AESCTR_Encrypt_DMA(CRYP_HandleTypeDef
*hcryp
, uint8_t *pPlainData
, uint16_t Size
, uint8_t *pCypherData
)
4646 /* Re-initialize AES IP with proper parameters */
4647 if (HAL_CRYP_DeInit(hcryp
) != HAL_OK
)
4651 hcryp
->Init
.OperatingMode
= CRYP_ALGOMODE_ENCRYPT
;
4652 hcryp
->Init
.ChainingMode
= CRYP_CHAINMODE_AES_CTR
;
4653 hcryp
->Init
.KeyWriteFlag
= CRYP_KEY_WRITE_ENABLE
;
4654 if (HAL_CRYP_Init(hcryp
) != HAL_OK
)
4659 return HAL_CRYPEx_AES_DMA(hcryp
, pPlainData
, Size
, pCypherData
);
4663 * @brief Decrypt pCypherData in AES ECB decryption mode using DMA,
4664 * the decyphered data are available in pPlainData.
4665 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
4666 * the configuration information for CRYP module
4667 * @param pCypherData: Pointer to the cyphertext buffer
4668 * @param Size: Length of the plaintext buffer in bytes, must be a multiple of 16.
4669 * @param pPlainData: Pointer to the plaintext buffer
4670 * @note This API is provided only to maintain compatibility with legacy software. Users should directly
4671 * resort to generic HAL_CRYPEx_AES_DMA() API instead (usage recommended).
4672 * @note pPlainData and pCypherData buffers must be 32-bit aligned to ensure a correct DMA transfer to and from the IP.
4673 * @retval HAL status
4675 HAL_StatusTypeDef
HAL_CRYP_AESECB_Decrypt_DMA(CRYP_HandleTypeDef
*hcryp
, uint8_t *pCypherData
, uint16_t Size
, uint8_t *pPlainData
)
4677 /* Re-initialize AES IP with proper parameters */
4678 if (HAL_CRYP_DeInit(hcryp
) != HAL_OK
)
4682 hcryp
->Init
.OperatingMode
= CRYP_ALGOMODE_KEYDERIVATION_DECRYPT
;
4683 hcryp
->Init
.ChainingMode
= CRYP_CHAINMODE_AES_ECB
;
4684 hcryp
->Init
.KeyWriteFlag
= CRYP_KEY_WRITE_ENABLE
;
4685 if (HAL_CRYP_Init(hcryp
) != HAL_OK
)
4690 return HAL_CRYPEx_AES_DMA(hcryp
, pCypherData
, Size
, pPlainData
);
4694 * @brief Decrypt pCypherData in AES CBC decryption mode using DMA,
4695 * the decyphered data are available in pPlainData.
4696 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
4697 * the configuration information for CRYP module
4698 * @param pCypherData: Pointer to the cyphertext buffer
4699 * @param Size: Length of the plaintext buffer in bytes, must be a multiple of 16.
4700 * @param pPlainData: Pointer to the plaintext buffer
4701 * @note This API is provided only to maintain compatibility with legacy software. Users should directly
4702 * resort to generic HAL_CRYPEx_AES_DMA() API instead (usage recommended).
4703 * @note pPlainData and pCypherData buffers must be 32-bit aligned to ensure a correct DMA transfer to and from the IP.
4704 * @retval HAL status
4706 HAL_StatusTypeDef
HAL_CRYP_AESCBC_Decrypt_DMA(CRYP_HandleTypeDef
*hcryp
, uint8_t *pCypherData
, uint16_t Size
, uint8_t *pPlainData
)
4708 /* Re-initialize AES IP with proper parameters */
4709 if (HAL_CRYP_DeInit(hcryp
) != HAL_OK
)
4713 hcryp
->Init
.OperatingMode
= CRYP_ALGOMODE_KEYDERIVATION_DECRYPT
;
4714 hcryp
->Init
.ChainingMode
= CRYP_CHAINMODE_AES_CBC
;
4715 hcryp
->Init
.KeyWriteFlag
= CRYP_KEY_WRITE_ENABLE
;
4716 if (HAL_CRYP_Init(hcryp
) != HAL_OK
)
4721 return HAL_CRYPEx_AES_DMA(hcryp
, pCypherData
, Size
, pPlainData
);
4725 * @brief Decrypt pCypherData in AES CTR decryption mode using DMA,
4726 * the decyphered data are available in pPlainData.
4727 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
4728 * the configuration information for CRYP module
4729 * @param pCypherData: Pointer to the cyphertext buffer
4730 * @param Size: Length of the plaintext buffer in bytes, must be a multiple of 16.
4731 * @param pPlainData: Pointer to the plaintext buffer
4732 * @note This API is provided only to maintain compatibility with legacy software. Users should directly
4733 * resort to generic HAL_CRYPEx_AES_DMA() API instead (usage recommended).
4734 * @note pPlainData and pCypherData buffers must be 32-bit aligned to ensure a correct DMA transfer to and from the IP.
4735 * @retval HAL status
4737 HAL_StatusTypeDef
HAL_CRYP_AESCTR_Decrypt_DMA(CRYP_HandleTypeDef
*hcryp
, uint8_t *pCypherData
, uint16_t Size
, uint8_t *pPlainData
)
4739 /* Re-initialize AES IP with proper parameters */
4740 if (HAL_CRYP_DeInit(hcryp
) != HAL_OK
)
4744 hcryp
->Init
.OperatingMode
= CRYP_ALGOMODE_DECRYPT
;
4745 hcryp
->Init
.ChainingMode
= CRYP_CHAINMODE_AES_CTR
;
4746 hcryp
->Init
.KeyWriteFlag
= CRYP_KEY_WRITE_ENABLE
;
4747 if (HAL_CRYP_Init(hcryp
) != HAL_OK
)
4752 return HAL_CRYPEx_AES_DMA(hcryp
, pCypherData
, Size
, pPlainData
);
4760 /** @defgroup CRYP_Exported_Functions_Group3 Callback functions
4761 * @brief Callback functions.
4764 ==============================================================================
4765 ##### Callback functions #####
4766 ==============================================================================
4767 [..] This section provides Interruption and DMA callback functions:
4768 (+) DMA Input data transfer complete
4769 (+) DMA Output data transfer complete
4770 (+) DMA or Interrupt error
4777 * @brief CRYP error callback.
4778 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
4779 * the configuration information for CRYP module
4782 __weak
void HAL_CRYP_ErrorCallback(CRYP_HandleTypeDef
*hcryp
)
4784 /* Prevent unused argument(s) compilation warning */
4787 /* NOTE : This function should not be modified; when the callback is needed,
4788 the HAL_CRYP_ErrorCallback can be implemented in the user file
4793 * @brief Input DMA transfer complete callback.
4794 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
4795 * the configuration information for CRYP module
4798 __weak
void HAL_CRYP_InCpltCallback(CRYP_HandleTypeDef
*hcryp
)
4800 /* Prevent unused argument(s) compilation warning */
4803 /* NOTE : This function should not be modified; when the callback is needed,
4804 the HAL_CRYP_InCpltCallback can be implemented in the user file
4809 * @brief Output DMA transfer complete callback.
4810 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
4811 * the configuration information for CRYP module
4814 __weak
void HAL_CRYP_OutCpltCallback(CRYP_HandleTypeDef
*hcryp
)
4816 /* Prevent unused argument(s) compilation warning */
4819 /* NOTE : This function should not be modified; when the callback is needed,
4820 the HAL_CRYP_OutCpltCallback can be implemented in the user file
4828 /** @defgroup CRYP_Exported_Functions_Group4 CRYP IRQ handler
4829 * @brief AES IRQ handler.
4832 ==============================================================================
4833 ##### AES IRQ handler management #####
4834 ==============================================================================
4835 [..] This section provides AES IRQ handler function.
4842 * @brief Handle AES interrupt request.
4843 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
4844 * the configuration information for CRYP module
4847 void HAL_CRYP_IRQHandler(CRYP_HandleTypeDef
*hcryp
)
4849 /* Check if error occurred */
4850 if (__HAL_CRYP_GET_IT_SOURCE(CRYP_IT_ERRIE
) != RESET
)
4852 /* If Write Error occurred */
4853 if (__HAL_CRYP_GET_FLAG(CRYP_IT_WRERR
) != RESET
)
4855 hcryp
->ErrorCode
|= HAL_CRYP_WRITE_ERROR
;
4856 hcryp
->State
= HAL_CRYP_STATE_ERROR
;
4858 /* If Read Error occurred */
4859 if (__HAL_CRYP_GET_FLAG(CRYP_IT_RDERR
) != RESET
)
4861 hcryp
->ErrorCode
|= HAL_CRYP_READ_ERROR
;
4862 hcryp
->State
= HAL_CRYP_STATE_ERROR
;
4865 /* If an error has been reported */
4866 if (hcryp
->State
== HAL_CRYP_STATE_ERROR
)
4868 /* Disable Error and Computation Complete Interrupts */
4869 __HAL_CRYP_DISABLE_IT(CRYP_IT_CCFIE
|CRYP_IT_ERRIE
);
4870 /* Clear all Interrupt flags */
4871 __HAL_CRYP_CLEAR_FLAG(CRYP_ERR_CLEAR
|CRYP_CCF_CLEAR
);
4873 /* Process Unlocked */
4874 __HAL_UNLOCK(hcryp
);
4876 HAL_CRYP_ErrorCallback(hcryp
);
4882 /* Check if computation complete interrupt is enabled
4883 and if the computation complete flag is raised */
4884 if((__HAL_CRYP_GET_FLAG(CRYP_IT_CCF
) != RESET
) && (__HAL_CRYP_GET_IT_SOURCE(CRYP_IT_CCFIE
) != RESET
))
4886 #if defined(AES_CR_NPBLB)
4887 if ((hcryp
->Init
.ChainingMode
== CRYP_CHAINMODE_AES_GCM_GMAC
)
4888 || (hcryp
->Init
.ChainingMode
== CRYP_CHAINMODE_AES_CCM_CMAC
))
4890 if ((hcryp
->Init
.ChainingMode
== CRYP_CHAINMODE_AES_GCM_GMAC
)
4891 || (hcryp
->Init
.ChainingMode
== CRYP_CHAINMODE_AES_CMAC
))
4894 /* To ensure proper suspension requests management, CCF flag
4895 is reset in CRYP_AES_Auth_IT() according to the current
4896 phase under handling */
4897 CRYP_AES_Auth_IT(hcryp
);
4901 /* Clear Computation Complete Flag */
4902 __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR
);
4912 /** @defgroup CRYP_Exported_Functions_Group5 Peripheral State functions
4913 * @brief Peripheral State functions.
4916 ==============================================================================
4917 ##### Peripheral State functions #####
4918 ==============================================================================
4920 This subsection permits to get in run-time the status of the peripheral.
4927 * @brief Return the CRYP handle state.
4928 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
4929 * the configuration information for CRYP module
4932 HAL_CRYP_STATETypeDef
HAL_CRYP_GetState(CRYP_HandleTypeDef
*hcryp
)
4934 /* Return CRYP handle state */
4935 return hcryp
->State
;
4939 * @brief Return the CRYP peripheral error.
4940 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
4941 * the configuration information for CRYP module
4942 * @note The returned error is a bit-map combination of possible errors
4943 * @retval Error bit-map
4945 uint32_t HAL_CRYP_GetError(CRYP_HandleTypeDef
*hcryp
)
4947 return hcryp
->ErrorCode
;
4958 /** @addtogroup CRYP_Private_Functions
4964 * @brief Write the Key in KeyRx registers.
4965 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
4966 * the configuration information for CRYP module
4969 static HAL_StatusTypeDef
CRYP_SetKey(CRYP_HandleTypeDef
*hcryp
)
4971 uint32_t keyaddr
= 0x0;
4973 if ((uint32_t)(hcryp
->Init
.pKey
== NULL
))
4979 keyaddr
= (uint32_t)(hcryp
->Init
.pKey
);
4981 if (hcryp
->Init
.KeySize
== CRYP_KEYSIZE_256B
)
4983 hcryp
->Instance
->KEYR7
= __REV(*(uint32_t*)(keyaddr
));
4985 hcryp
->Instance
->KEYR6
= __REV(*(uint32_t*)(keyaddr
));
4987 hcryp
->Instance
->KEYR5
= __REV(*(uint32_t*)(keyaddr
));
4989 hcryp
->Instance
->KEYR4
= __REV(*(uint32_t*)(keyaddr
));
4993 hcryp
->Instance
->KEYR3
= __REV(*(uint32_t*)(keyaddr
));
4995 hcryp
->Instance
->KEYR2
= __REV(*(uint32_t*)(keyaddr
));
4997 hcryp
->Instance
->KEYR1
= __REV(*(uint32_t*)(keyaddr
));
4999 hcryp
->Instance
->KEYR0
= __REV(*(uint32_t*)(keyaddr
));
5005 * @brief Write the InitVector/InitCounter in IVRx registers.
5006 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
5007 * the configuration information for CRYP module
5010 static HAL_StatusTypeDef
CRYP_SetInitVector(CRYP_HandleTypeDef
*hcryp
)
5012 uint32_t ivaddr
= 0x0;
5014 #if !defined(AES_CR_NPBLB)
5015 if (hcryp
->Init
.ChainingMode
== CRYP_CHAINMODE_AES_CMAC
)
5017 hcryp
->Instance
->IVR3
= 0;
5018 hcryp
->Instance
->IVR2
= 0;
5019 hcryp
->Instance
->IVR1
= 0;
5020 hcryp
->Instance
->IVR0
= 0;
5025 if (hcryp
->Init
.pInitVect
== NULL
)
5030 ivaddr
= (uint32_t)(hcryp
->Init
.pInitVect
);
5032 hcryp
->Instance
->IVR3
= __REV(*(uint32_t*)(ivaddr
));
5034 hcryp
->Instance
->IVR2
= __REV(*(uint32_t*)(ivaddr
));
5036 hcryp
->Instance
->IVR1
= __REV(*(uint32_t*)(ivaddr
));
5038 hcryp
->Instance
->IVR0
= __REV(*(uint32_t*)(ivaddr
));
5046 * @brief Handle CRYP block input/output data handling under interruption.
5047 * @note The function is called under interruption only, once
5048 * interruptions have been enabled by HAL_CRYPEx_AES_IT().
5049 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
5050 * the configuration information for CRYP module.
5051 * @retval HAL status
5053 static HAL_StatusTypeDef
CRYP_AES_IT(CRYP_HandleTypeDef
*hcryp
)
5055 uint32_t inputaddr
= 0;
5056 uint32_t outputaddr
= 0;
5058 if(hcryp
->State
== HAL_CRYP_STATE_BUSY
)
5060 if (hcryp
->Init
.OperatingMode
!= CRYP_ALGOMODE_KEYDERIVATION
)
5062 /* Get the output data address */
5063 outputaddr
= (uint32_t)hcryp
->pCrypOutBuffPtr
;
5065 /* Read the last available output block from the Data Output Register */
5066 *(uint32_t*)(outputaddr
) = hcryp
->Instance
->DOUTR
;
5068 *(uint32_t*)(outputaddr
) = hcryp
->Instance
->DOUTR
;
5070 *(uint32_t*)(outputaddr
) = hcryp
->Instance
->DOUTR
;
5072 *(uint32_t*)(outputaddr
) = hcryp
->Instance
->DOUTR
;
5073 hcryp
->pCrypOutBuffPtr
+= 16;
5074 hcryp
->CrypOutCount
-= 16;
5079 /* Read the derived key from the Key registers */
5080 if (hcryp
->Init
.KeySize
== CRYP_KEYSIZE_256B
)
5082 *(uint32_t*)(outputaddr
) = __REV(hcryp
->Instance
->KEYR7
);
5084 *(uint32_t*)(outputaddr
) = __REV(hcryp
->Instance
->KEYR6
);
5086 *(uint32_t*)(outputaddr
) = __REV(hcryp
->Instance
->KEYR5
);
5088 *(uint32_t*)(outputaddr
) = __REV(hcryp
->Instance
->KEYR4
);
5092 *(uint32_t*)(outputaddr
) = __REV(hcryp
->Instance
->KEYR3
);
5094 *(uint32_t*)(outputaddr
) = __REV(hcryp
->Instance
->KEYR2
);
5096 *(uint32_t*)(outputaddr
) = __REV(hcryp
->Instance
->KEYR1
);
5098 *(uint32_t*)(outputaddr
) = __REV(hcryp
->Instance
->KEYR0
);
5101 /* In case of ciphering or deciphering, check if all output text has been retrieved;
5102 In case of key derivation, stop right there */
5103 if ((hcryp
->CrypOutCount
== 0) || (hcryp
->Init
.OperatingMode
== CRYP_ALGOMODE_KEYDERIVATION
))
5105 /* Disable Computation Complete Flag and Errors Interrupts */
5106 __HAL_CRYP_DISABLE_IT(CRYP_IT_CCFIE
|CRYP_IT_ERRIE
);
5107 /* Change the CRYP state */
5108 hcryp
->State
= HAL_CRYP_STATE_READY
;
5110 /* Process Unlocked */
5111 __HAL_UNLOCK(hcryp
);
5113 /* Call computation complete callback */
5114 HAL_CRYPEx_ComputationCpltCallback(hcryp
);
5118 /* If suspension flag has been raised, suspend processing */
5119 else if (hcryp
->SuspendRequest
== HAL_CRYP_SUSPEND
)
5121 /* reset ModeSuspend */
5122 hcryp
->SuspendRequest
= HAL_CRYP_SUSPEND_NONE
;
5124 /* Disable Computation Complete Flag and Errors Interrupts */
5125 __HAL_CRYP_DISABLE_IT(CRYP_IT_CCFIE
|CRYP_IT_ERRIE
);
5126 /* Change the CRYP state */
5127 hcryp
->State
= HAL_CRYP_STATE_SUSPENDED
;
5129 /* Process Unlocked */
5130 __HAL_UNLOCK(hcryp
);
5134 else /* Process the rest of input data */
5136 /* Get the Intput data address */
5137 inputaddr
= (uint32_t)hcryp
->pCrypInBuffPtr
;
5139 /* Increment/decrement instance pointer/counter */
5140 hcryp
->pCrypInBuffPtr
+= 16;
5141 hcryp
->CrypInCount
-= 16;
5143 /* Write the next input block in the Data Input register */
5144 hcryp
->Instance
->DINR
= *(uint32_t*)(inputaddr
);
5146 hcryp
->Instance
->DINR
= *(uint32_t*)(inputaddr
);
5148 hcryp
->Instance
->DINR
= *(uint32_t*)(inputaddr
);
5150 hcryp
->Instance
->DINR
= *(uint32_t*)(inputaddr
);
5180 #endif /* HAL_CRYP_MODULE_ENABLED */
5182 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/