2 ******************************************************************************
3 * @file stm32f4xx_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 __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 initilalized 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) 2014 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 "stm32f4xx_hal.h"
103 /** @addtogroup STM32F4xx_HAL_Driver
108 * @brief CRYP HAL module driver.
112 #ifdef HAL_CRYP_MODULE_ENABLED
114 #if defined(STM32F415xx) || defined(STM32F417xx) || defined(STM32F437xx) || defined(STM32F439xx)
116 /* Private typedef -----------------------------------------------------------*/
117 /* Private define ------------------------------------------------------------*/
118 #define CRYP_TIMEOUT_VALUE 1
119 /* Private macro -------------------------------------------------------------*/
120 /* Private variables ---------------------------------------------------------*/
121 /* Private function prototypes -----------------------------------------------*/
122 static void CRYP_SetInitVector(CRYP_HandleTypeDef *hcryp, uint8_t *InitVector, uint32_t IVSize);
123 static void CRYP_SetKey(CRYP_HandleTypeDef *hcryp, uint8_t *Key, uint32_t KeySize);
124 static HAL_StatusTypeDef CRYP_ProcessData(CRYP_HandleTypeDef *hcryp, uint8_t* Input, uint16_t Ilength, uint8_t* Output, uint32_t Timeout);
125 static HAL_StatusTypeDef CRYP_ProcessData2Words(CRYP_HandleTypeDef *hcryp, uint8_t* Input, uint16_t Ilength, uint8_t* Output, uint32_t Timeout);
126 static void CRYP_DMAInCplt(DMA_HandleTypeDef *hdma);
127 static void CRYP_DMAOutCplt(DMA_HandleTypeDef *hdma);
128 static void CRYP_DMAError(DMA_HandleTypeDef *hdma);
129 static void CRYP_SetDMAConfig(CRYP_HandleTypeDef *hcryp, uint32_t inputaddr, uint16_t Size, uint32_t outputaddr);
130 static void CRYP_SetTDESECBMode(CRYP_HandleTypeDef *hcryp, uint32_t Direction);
131 static void CRYP_SetTDESCBCMode(CRYP_HandleTypeDef *hcryp, uint32_t Direction);
132 static void CRYP_SetDESECBMode(CRYP_HandleTypeDef *hcryp, uint32_t Direction);
133 static void CRYP_SetDESCBCMode(CRYP_HandleTypeDef *hcryp, uint32_t Direction);
135 /* Private functions ---------------------------------------------------------*/
137 /** @defgroup CRYP_Private_Functions
141 /** @defgroup CRYP_Group1 Initialization and de-initialization functions
142 * @brief Initialization and Configuration functions.
145 ==============================================================================
146 ##### Initialization and de-initialization functions #####
147 ==============================================================================
148 [..] This section provides functions allowing to:
149 (+) Initialize the CRYP according to the specified parameters
150 in the CRYP_InitTypeDef and creates the associated handle
151 (+) DeInitialize the CRYP peripheral
152 (+) Initialize the CRYP MSP
153 (+) DeInitialize CRYP MSP
160 * @brief Initializes the CRYP according to the specified
161 * parameters in the CRYP_InitTypeDef and creates the associated handle.
162 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
163 * the configuration information for CRYP module
166 HAL_StatusTypeDef HAL_CRYP_Init(CRYP_HandleTypeDef *hcryp)
168 /* Check the CRYP handle allocation */
169 if(hcryp == HAL_NULL)
174 /* Check the parameters */
175 assert_param(IS_CRYP_KEYSIZE(hcryp->Init.KeySize));
176 assert_param(IS_CRYP_DATATYPE(hcryp->Init.DataType));
178 if(hcryp->State == HAL_CRYP_STATE_RESET)
180 /* Init the low level hardware */
181 HAL_CRYP_MspInit(hcryp);
184 /* Change the CRYP state */
185 hcryp->State = HAL_CRYP_STATE_BUSY;
187 /* Set the key size and data type*/
188 CRYP->CR = (uint32_t) (hcryp->Init.KeySize | hcryp->Init.DataType);
190 /* Reset CrypInCount and CrypOutCount */
191 hcryp->CrypInCount = 0;
192 hcryp->CrypOutCount = 0;
194 /* Change the CRYP state */
195 hcryp->State = HAL_CRYP_STATE_READY;
197 /* Set the default CRYP phase */
198 hcryp->Phase = HAL_CRYP_PHASE_READY;
200 /* Return function status */
205 * @brief DeInitializes the CRYP peripheral.
206 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
207 * the configuration information for CRYP module
210 HAL_StatusTypeDef HAL_CRYP_DeInit(CRYP_HandleTypeDef *hcryp)
212 /* Check the CRYP handle allocation */
213 if(hcryp == HAL_NULL)
218 /* Change the CRYP state */
219 hcryp->State = HAL_CRYP_STATE_BUSY;
221 /* Set the default CRYP phase */
222 hcryp->Phase = HAL_CRYP_PHASE_READY;
224 /* Reset CrypInCount and CrypOutCount */
225 hcryp->CrypInCount = 0;
226 hcryp->CrypOutCount = 0;
228 /* Disable the CRYP Peripheral Clock */
229 __HAL_CRYP_DISABLE();
231 /* DeInit the low level hardware: CLOCK, NVIC.*/
232 HAL_CRYP_MspDeInit(hcryp);
234 /* Change the CRYP state */
235 hcryp->State = HAL_CRYP_STATE_RESET;
240 /* Return function status */
245 * @brief Initializes the CRYP MSP.
246 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
247 * the configuration information for CRYP module
250 __weak void HAL_CRYP_MspInit(CRYP_HandleTypeDef *hcryp)
252 /* NOTE : This function Should not be modified, when the callback is needed,
253 the HAL_CRYP_MspInit could be implemented in the user file
258 * @brief DeInitializes CRYP MSP.
259 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
260 * the configuration information for CRYP module
263 __weak void HAL_CRYP_MspDeInit(CRYP_HandleTypeDef *hcryp)
265 /* NOTE : This function Should not be modified, when the callback is needed,
266 the HAL_CRYP_MspDeInit could be implemented in the user file
274 /** @defgroup CRYP_Group2 AES processing functions
275 * @brief processing functions.
278 ==============================================================================
279 ##### AES processing functions #####
280 ==============================================================================
281 [..] This section provides functions allowing to:
282 (+) Encrypt plaintext using AES-128/192/256 using chaining modes
283 (+) Decrypt cyphertext using AES-128/192/256 using chaining modes
284 [..] Three processing functions are available:
294 * @brief Initializes the CRYP peripheral in AES ECB encryption mode
295 * then encrypt pPlainData. The cypher data are available in pCypherData
296 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
297 * the configuration information for CRYP module
298 * @param pPlainData: Pointer to the plaintext buffer
299 * @param Size: Length of the plaintext buffer, must be a multiple of 16.
300 * @param pCypherData: Pointer to the cyphertext buffer
301 * @param Timeout: Specify Timeout value
304 HAL_StatusTypeDef HAL_CRYP_AESECB_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout)
309 /* Change the CRYP state */
310 hcryp->State = HAL_CRYP_STATE_BUSY;
312 /* Check if initialization phase has already been performed */
313 if(hcryp->Phase == HAL_CRYP_PHASE_READY)
316 CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
318 /* Set the CRYP peripheral in AES ECB mode */
319 __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_ECB);
322 __HAL_CRYP_FIFO_FLUSH();
328 hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
331 /* Write Plain Data and Get Cypher Data */
332 if(CRYP_ProcessData(hcryp,pPlainData, Size, pCypherData, Timeout) != HAL_OK)
337 /* Change the CRYP state */
338 hcryp->State = HAL_CRYP_STATE_READY;
340 /* Process Unlocked */
343 /* Return function status */
348 * @brief Initializes the CRYP peripheral in AES CBC encryption mode
349 * then encrypt pPlainData. The cypher data are available in pCypherData
350 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
351 * the configuration information for CRYP module
352 * @param pPlainData: Pointer to the plaintext buffer
353 * @param Size: Length of the plaintext buffer, must be a multiple of 16.
354 * @param pCypherData: Pointer to the cyphertext buffer
355 * @param Timeout: Specify Timeout value
358 HAL_StatusTypeDef HAL_CRYP_AESCBC_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout)
363 /* Change the CRYP state */
364 hcryp->State = HAL_CRYP_STATE_BUSY;
366 /* Check if initialization phase has already been performed */
367 if(hcryp->Phase == HAL_CRYP_PHASE_READY)
370 CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
372 /* Set the CRYP peripheral in AES ECB mode */
373 __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_CBC);
375 /* Set the Initialization Vector */
376 CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_128B);
379 __HAL_CRYP_FIFO_FLUSH();
385 hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
388 /* Write Plain Data and Get Cypher Data */
389 if(CRYP_ProcessData(hcryp,pPlainData, Size, pCypherData, Timeout) != HAL_OK)
394 /* Change the CRYP state */
395 hcryp->State = HAL_CRYP_STATE_READY;
397 /* Process Unlocked */
400 /* Return function status */
405 * @brief Initializes the CRYP peripheral in AES CTR encryption mode
406 * then encrypt pPlainData. The cypher data are available in pCypherData
407 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
408 * the configuration information for CRYP module
409 * @param pPlainData: Pointer to the plaintext buffer
410 * @param Size: Length of the plaintext buffer, must be a multiple of 16.
411 * @param pCypherData: Pointer to the cyphertext buffer
412 * @param Timeout: Specify Timeout value
415 HAL_StatusTypeDef HAL_CRYP_AESCTR_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout)
420 /* Change the CRYP state */
421 hcryp->State = HAL_CRYP_STATE_BUSY;
423 /* Check if initialization phase has already been performed */
424 if(hcryp->Phase == HAL_CRYP_PHASE_READY)
427 CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
429 /* Set the CRYP peripheral in AES ECB mode */
430 __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_CTR);
432 /* Set the Initialization Vector */
433 CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_128B);
436 __HAL_CRYP_FIFO_FLUSH();
442 hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
445 /* Write Plain Data and Get Cypher Data */
446 if(CRYP_ProcessData(hcryp, pPlainData, Size, pCypherData, Timeout) != HAL_OK)
451 /* Change the CRYP state */
452 hcryp->State = HAL_CRYP_STATE_READY;
454 /* Process Unlocked */
457 /* Return function status */
464 * @brief Initializes the CRYP peripheral in AES ECB decryption mode
465 * then decrypted pCypherData. The cypher data are available in pPlainData
466 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
467 * the configuration information for CRYP module
468 * @param pCypherData: Pointer to the cyphertext buffer
469 * @param Size: Length of the plaintext buffer, must be a multiple of 16.
470 * @param pPlainData: Pointer to the plaintext buffer
471 * @param Timeout: Specify Timeout value
474 HAL_StatusTypeDef HAL_CRYP_AESECB_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout)
476 uint32_t tickstart = 0;
481 /* Change the CRYP state */
482 hcryp->State = HAL_CRYP_STATE_BUSY;
484 /* Check if initialization phase has already been performed */
485 if(hcryp->Phase == HAL_CRYP_PHASE_READY)
488 CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
490 /* Set the CRYP peripheral in AES Key mode */
491 __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_KEY | CRYP_CR_ALGODIR);
497 tickstart = HAL_GetTick();
499 while(HAL_IS_BIT_SET(CRYP->SR, CRYP_FLAG_BUSY))
501 /* Check for the Timeout */
502 if(Timeout != HAL_MAX_DELAY)
504 if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
507 hcryp->State = HAL_CRYP_STATE_TIMEOUT;
509 /* Process Unlocked */
518 __HAL_CRYP_DISABLE();
520 /* Reset the ALGOMODE bits*/
521 CRYP->CR &= (uint32_t)(~CRYP_CR_ALGOMODE);
523 /* Set the CRYP peripheral in AES ECB decryption mode */
524 __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_ECB | CRYP_CR_ALGODIR);
526 __HAL_CRYP_FIFO_FLUSH();
532 hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
535 /* Write Plain Data and Get Cypher Data */
536 if(CRYP_ProcessData(hcryp, pCypherData, Size, pPlainData, Timeout) != HAL_OK)
541 /* Change the CRYP state */
542 hcryp->State = HAL_CRYP_STATE_READY;
544 /* Process Unlocked */
547 /* Return function status */
552 * @brief Initializes the CRYP peripheral in AES ECB decryption mode
553 * then decrypted pCypherData. The cypher data are available in pPlainData
554 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
555 * the configuration information for CRYP module
556 * @param pCypherData: Pointer to the cyphertext buffer
557 * @param Size: Length of the plaintext buffer, must be a multiple of 16.
558 * @param pPlainData: Pointer to the plaintext buffer
559 * @param Timeout: Specify Timeout value
562 HAL_StatusTypeDef HAL_CRYP_AESCBC_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout)
564 uint32_t tickstart = 0;
569 /* Change the CRYP state */
570 hcryp->State = HAL_CRYP_STATE_BUSY;
572 /* Check if initialization phase has already been performed */
573 if(hcryp->Phase == HAL_CRYP_PHASE_READY)
576 CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
578 /* Set the CRYP peripheral in AES Key mode */
579 __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_KEY | CRYP_CR_ALGODIR);
585 tickstart = HAL_GetTick();
587 while(HAL_IS_BIT_SET(CRYP->SR, CRYP_FLAG_BUSY))
589 /* Check for the Timeout */
590 if(Timeout != HAL_MAX_DELAY)
592 if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
595 hcryp->State = HAL_CRYP_STATE_TIMEOUT;
597 /* Process Unlocked */
605 /* Reset the ALGOMODE bits*/
606 CRYP->CR &= (uint32_t)(~CRYP_CR_ALGOMODE);
608 /* Set the CRYP peripheral in AES CBC decryption mode */
609 __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_CBC | CRYP_CR_ALGODIR);
611 /* Set the Initialization Vector */
612 CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_128B);
615 __HAL_CRYP_FIFO_FLUSH();
621 hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
624 /* Write Plain Data and Get Cypher Data */
625 if(CRYP_ProcessData(hcryp, pCypherData, Size, pPlainData, Timeout) != HAL_OK)
630 /* Change the CRYP state */
631 hcryp->State = HAL_CRYP_STATE_READY;
633 /* Process Unlocked */
636 /* Return function status */
641 * @brief Initializes the CRYP peripheral in AES CTR decryption mode
642 * then decrypted pCypherData. The cypher data are available in pPlainData
643 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
644 * the configuration information for CRYP module
645 * @param pCypherData: Pointer to the cyphertext buffer
646 * @param Size: Length of the plaintext buffer, must be a multiple of 16.
647 * @param pPlainData: Pointer to the plaintext buffer
648 * @param Timeout: Specify Timeout value
651 HAL_StatusTypeDef HAL_CRYP_AESCTR_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout)
656 /* Check if initialization phase has already been performed */
657 if(hcryp->Phase == HAL_CRYP_PHASE_READY)
659 /* Change the CRYP state */
660 hcryp->State = HAL_CRYP_STATE_BUSY;
663 CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
665 /* Set the CRYP peripheral in AES CTR mode */
666 __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_CTR | CRYP_CR_ALGODIR);
668 /* Set the Initialization Vector */
669 CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_128B);
672 __HAL_CRYP_FIFO_FLUSH();
678 hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
681 /* Write Plain Data and Get Cypher Data */
682 if(CRYP_ProcessData(hcryp, pCypherData, Size, pPlainData, Timeout) != HAL_OK)
687 /* Change the CRYP state */
688 hcryp->State = HAL_CRYP_STATE_READY;
690 /* Process Unlocked */
693 /* Return function status */
698 * @brief Initializes the CRYP peripheral in AES ECB encryption mode using Interrupt.
699 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
700 * the configuration information for CRYP module
701 * @param pPlainData: Pointer to the plaintext buffer
702 * @param Size: Length of the plaintext buffer, must be a multiple of 16 bytes
703 * @param pCypherData: Pointer to the cyphertext buffer
706 HAL_StatusTypeDef HAL_CRYP_AESECB_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
711 if(hcryp->State == HAL_CRYP_STATE_READY)
716 hcryp->CrypInCount = Size;
717 hcryp->pCrypInBuffPtr = pPlainData;
718 hcryp->pCrypOutBuffPtr = pCypherData;
719 hcryp->CrypOutCount = Size;
721 /* Change the CRYP state */
722 hcryp->State = HAL_CRYP_STATE_BUSY;
724 /* Check if initialization phase has already been performed */
725 if(hcryp->Phase == HAL_CRYP_PHASE_READY)
728 CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
730 /* Set the CRYP peripheral in AES ECB mode */
731 __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_ECB);
734 __HAL_CRYP_FIFO_FLUSH();
737 hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
740 /* Enable Interrupts */
741 __HAL_CRYP_ENABLE_IT(CRYP_IT_INI | CRYP_IT_OUTI);
746 /* Return function status */
749 else if(__HAL_CRYP_GET_IT(CRYP_IT_INI))
751 inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
752 /* Write the Input block in the IN FIFO */
753 CRYP->DR = *(uint32_t*)(inputaddr);
755 CRYP->DR = *(uint32_t*)(inputaddr);
757 CRYP->DR = *(uint32_t*)(inputaddr);
759 CRYP->DR = *(uint32_t*)(inputaddr);
760 hcryp->pCrypInBuffPtr += 16;
761 hcryp->CrypInCount -= 16;
762 if(hcryp->CrypInCount == 0)
764 __HAL_CRYP_DISABLE_IT(CRYP_IT_INI);
765 /* Call the Input data transfer complete callback */
766 HAL_CRYP_InCpltCallback(hcryp);
769 else if(__HAL_CRYP_GET_IT(CRYP_IT_OUTI))
771 outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
772 /* Read the Output block from the Output FIFO */
773 *(uint32_t*)(outputaddr) = CRYP->DOUT;
775 *(uint32_t*)(outputaddr) = CRYP->DOUT;
777 *(uint32_t*)(outputaddr) = CRYP->DOUT;
779 *(uint32_t*)(outputaddr) = CRYP->DOUT;
780 hcryp->pCrypOutBuffPtr += 16;
781 hcryp->CrypOutCount -= 16;
782 if(hcryp->CrypOutCount == 0)
784 __HAL_CRYP_DISABLE_IT(CRYP_IT_OUTI);
787 /* Change the CRYP state */
788 hcryp->State = HAL_CRYP_STATE_READY;
789 /* Call Input transfer complete callback */
790 HAL_CRYP_OutCpltCallback(hcryp);
794 /* Return function status */
799 * @brief Initializes the CRYP peripheral in AES CBC encryption mode using Interrupt.
800 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
801 * the configuration information for CRYP module
802 * @param pPlainData: Pointer to the plaintext buffer
803 * @param Size: Length of the plaintext buffer, must be a multiple of 16 bytes
804 * @param pCypherData: Pointer to the cyphertext buffer
807 HAL_StatusTypeDef HAL_CRYP_AESCBC_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
812 if(hcryp->State == HAL_CRYP_STATE_READY)
817 hcryp->CrypInCount = Size;
818 hcryp->pCrypInBuffPtr = pPlainData;
819 hcryp->pCrypOutBuffPtr = pCypherData;
820 hcryp->CrypOutCount = Size;
822 /* Change the CRYP state */
823 hcryp->State = HAL_CRYP_STATE_BUSY;
825 /* Check if initialization phase has already been performed */
826 if(hcryp->Phase == HAL_CRYP_PHASE_READY)
829 CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
831 /* Set the CRYP peripheral in AES CBC mode */
832 __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_CBC);
834 /* Set the Initialization Vector */
835 CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_128B);
838 __HAL_CRYP_FIFO_FLUSH();
841 hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
843 /* Enable Interrupts */
844 __HAL_CRYP_ENABLE_IT(CRYP_IT_INI | CRYP_IT_OUTI);
849 /* Return function status */
852 else if(__HAL_CRYP_GET_IT(CRYP_IT_INI))
854 inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
855 /* Write the Input block in the IN FIFO */
856 CRYP->DR = *(uint32_t*)(inputaddr);
858 CRYP->DR = *(uint32_t*)(inputaddr);
860 CRYP->DR = *(uint32_t*)(inputaddr);
862 CRYP->DR = *(uint32_t*)(inputaddr);
863 hcryp->pCrypInBuffPtr += 16;
864 hcryp->CrypInCount -= 16;
865 if(hcryp->CrypInCount == 0)
867 __HAL_CRYP_DISABLE_IT(CRYP_IT_INI);
868 /* Call the Input data transfer complete callback */
869 HAL_CRYP_InCpltCallback(hcryp);
872 else if(__HAL_CRYP_GET_IT(CRYP_IT_OUTI))
874 outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
875 /* Read the Output block from the Output FIFO */
876 *(uint32_t*)(outputaddr) = CRYP->DOUT;
878 *(uint32_t*)(outputaddr) = CRYP->DOUT;
880 *(uint32_t*)(outputaddr) = CRYP->DOUT;
882 *(uint32_t*)(outputaddr) = CRYP->DOUT;
883 hcryp->pCrypOutBuffPtr += 16;
884 hcryp->CrypOutCount -= 16;
885 if(hcryp->CrypOutCount == 0)
887 __HAL_CRYP_DISABLE_IT(CRYP_IT_OUTI);
890 /* Change the CRYP state */
891 hcryp->State = HAL_CRYP_STATE_READY;
892 /* Call Input transfer complete callback */
893 HAL_CRYP_OutCpltCallback(hcryp);
897 /* Return function status */
902 * @brief Initializes the CRYP peripheral in AES CTR encryption mode using Interrupt.
903 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
904 * the configuration information for CRYP module
905 * @param pPlainData: Pointer to the plaintext buffer
906 * @param Size: Length of the plaintext buffer, must be a multiple of 16 bytes
907 * @param pCypherData: Pointer to the cyphertext buffer
910 HAL_StatusTypeDef HAL_CRYP_AESCTR_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
915 if(hcryp->State == HAL_CRYP_STATE_READY)
920 hcryp->CrypInCount = Size;
921 hcryp->pCrypInBuffPtr = pPlainData;
922 hcryp->pCrypOutBuffPtr = pCypherData;
923 hcryp->CrypOutCount = Size;
925 /* Change the CRYP state */
926 hcryp->State = HAL_CRYP_STATE_BUSY;
928 /* Check if initialization phase has already been performed */
929 if(hcryp->Phase == HAL_CRYP_PHASE_READY)
932 CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
934 /* Set the CRYP peripheral in AES CTR mode */
935 __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_CTR);
937 /* Set the Initialization Vector */
938 CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_128B);
941 __HAL_CRYP_FIFO_FLUSH();
944 hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
946 /* Enable Interrupts */
947 __HAL_CRYP_ENABLE_IT(CRYP_IT_INI | CRYP_IT_OUTI);
952 /* Return function status */
955 else if(__HAL_CRYP_GET_IT(CRYP_IT_INI))
957 inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
958 /* Write the Input block in the IN FIFO */
959 CRYP->DR = *(uint32_t*)(inputaddr);
961 CRYP->DR = *(uint32_t*)(inputaddr);
963 CRYP->DR = *(uint32_t*)(inputaddr);
965 CRYP->DR = *(uint32_t*)(inputaddr);
966 hcryp->pCrypInBuffPtr += 16;
967 hcryp->CrypInCount -= 16;
968 if(hcryp->CrypInCount == 0)
970 __HAL_CRYP_DISABLE_IT(CRYP_IT_INI);
971 /* Call the Input data transfer complete callback */
972 HAL_CRYP_InCpltCallback(hcryp);
975 else if(__HAL_CRYP_GET_IT(CRYP_IT_OUTI))
977 outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
978 /* Read the Output block from the Output FIFO */
979 *(uint32_t*)(outputaddr) = CRYP->DOUT;
981 *(uint32_t*)(outputaddr) = CRYP->DOUT;
983 *(uint32_t*)(outputaddr) = CRYP->DOUT;
985 *(uint32_t*)(outputaddr) = CRYP->DOUT;
986 hcryp->pCrypOutBuffPtr += 16;
987 hcryp->CrypOutCount -= 16;
988 if(hcryp->CrypOutCount == 0)
990 __HAL_CRYP_DISABLE_IT(CRYP_IT_OUTI);
991 /* Process Unlocked */
993 /* Change the CRYP state */
994 hcryp->State = HAL_CRYP_STATE_READY;
995 /* Call Input transfer complete callback */
996 HAL_CRYP_OutCpltCallback(hcryp);
1000 /* Return function status */
1006 * @brief Initializes the CRYP peripheral in AES ECB decryption mode using Interrupt.
1007 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
1008 * the configuration information for CRYP module
1009 * @param pCypherData: Pointer to the cyphertext buffer
1010 * @param Size: Length of the plaintext buffer, must be a multiple of 16.
1011 * @param pPlainData: Pointer to the plaintext buffer
1012 * @retval HAL status
1014 HAL_StatusTypeDef HAL_CRYP_AESECB_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
1016 uint32_t tickstart = 0;
1019 uint32_t outputaddr;
1021 if(hcryp->State == HAL_CRYP_STATE_READY)
1023 /* Process Locked */
1026 hcryp->CrypInCount = Size;
1027 hcryp->pCrypInBuffPtr = pCypherData;
1028 hcryp->pCrypOutBuffPtr = pPlainData;
1029 hcryp->CrypOutCount = Size;
1031 /* Change the CRYP state */
1032 hcryp->State = HAL_CRYP_STATE_BUSY;
1034 /* Check if initialization phase has already been performed */
1035 if(hcryp->Phase == HAL_CRYP_PHASE_READY)
1038 CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
1040 /* Set the CRYP peripheral in AES Key mode */
1041 __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_KEY | CRYP_CR_ALGODIR);
1043 __HAL_CRYP_ENABLE();
1046 tickstart = HAL_GetTick();
1048 while(HAL_IS_BIT_SET(CRYP->SR, CRYP_FLAG_BUSY))
1050 /* Check for the Timeout */
1051 if((HAL_GetTick() - tickstart ) > CRYP_TIMEOUT_VALUE)
1054 hcryp->State = HAL_CRYP_STATE_TIMEOUT;
1056 /* Process Unlocked */
1057 __HAL_UNLOCK(hcryp);
1063 /* Reset the ALGOMODE bits*/
1064 CRYP->CR &= (uint32_t)(~CRYP_CR_ALGOMODE);
1066 /* Set the CRYP peripheral in AES ECB decryption mode */
1067 __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_ECB | CRYP_CR_ALGODIR);
1070 __HAL_CRYP_FIFO_FLUSH();
1073 hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
1076 /* Enable Interrupts */
1077 __HAL_CRYP_ENABLE_IT(CRYP_IT_INI | CRYP_IT_OUTI);
1080 __HAL_CRYP_ENABLE();
1082 /* Return function status */
1085 else if(__HAL_CRYP_GET_IT(CRYP_IT_INI))
1087 inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
1088 /* Write the Input block in the IN FIFO */
1089 CRYP->DR = *(uint32_t*)(inputaddr);
1091 CRYP->DR = *(uint32_t*)(inputaddr);
1093 CRYP->DR = *(uint32_t*)(inputaddr);
1095 CRYP->DR = *(uint32_t*)(inputaddr);
1096 hcryp->pCrypInBuffPtr += 16;
1097 hcryp->CrypInCount -= 16;
1098 if(hcryp->CrypInCount == 0)
1100 __HAL_CRYP_DISABLE_IT(CRYP_IT_INI);
1101 /* Call the Input data transfer complete callback */
1102 HAL_CRYP_InCpltCallback(hcryp);
1105 else if(__HAL_CRYP_GET_IT(CRYP_IT_OUTI))
1107 outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
1108 /* Read the Output block from the Output FIFO */
1109 *(uint32_t*)(outputaddr) = CRYP->DOUT;
1111 *(uint32_t*)(outputaddr) = CRYP->DOUT;
1113 *(uint32_t*)(outputaddr) = CRYP->DOUT;
1115 *(uint32_t*)(outputaddr) = CRYP->DOUT;
1116 hcryp->pCrypOutBuffPtr += 16;
1117 hcryp->CrypOutCount -= 16;
1118 if(hcryp->CrypOutCount == 0)
1120 __HAL_CRYP_DISABLE_IT(CRYP_IT_OUTI);
1121 /* Process Unlocked */
1122 __HAL_UNLOCK(hcryp);
1123 /* Change the CRYP state */
1124 hcryp->State = HAL_CRYP_STATE_READY;
1125 /* Call Input transfer complete callback */
1126 HAL_CRYP_OutCpltCallback(hcryp);
1130 /* Return function status */
1135 * @brief Initializes the CRYP peripheral in AES CBC decryption mode using IT.
1136 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
1137 * the configuration information for CRYP module
1138 * @param pCypherData: Pointer to the cyphertext buffer
1139 * @param Size: Length of the plaintext buffer, must be a multiple of 16
1140 * @param pPlainData: Pointer to the plaintext buffer
1141 * @retval HAL status
1143 HAL_StatusTypeDef HAL_CRYP_AESCBC_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
1146 uint32_t tickstart = 0;
1148 uint32_t outputaddr;
1150 if(hcryp->State == HAL_CRYP_STATE_READY)
1152 /* Process Locked */
1155 /* Get the buffer addresses and sizes */
1156 hcryp->CrypInCount = Size;
1157 hcryp->pCrypInBuffPtr = pCypherData;
1158 hcryp->pCrypOutBuffPtr = pPlainData;
1159 hcryp->CrypOutCount = Size;
1161 /* Change the CRYP state */
1162 hcryp->State = HAL_CRYP_STATE_BUSY;
1164 /* Check if initialization phase has already been performed */
1165 if(hcryp->Phase == HAL_CRYP_PHASE_READY)
1168 CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
1170 /* Set the CRYP peripheral in AES Key mode */
1171 __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_KEY | CRYP_CR_ALGODIR);
1174 __HAL_CRYP_ENABLE();
1177 tickstart = HAL_GetTick();
1179 while(HAL_IS_BIT_SET(CRYP->SR, CRYP_FLAG_BUSY))
1181 /* Check for the Timeout */
1182 if((HAL_GetTick() - tickstart ) > CRYP_TIMEOUT_VALUE)
1185 hcryp->State = HAL_CRYP_STATE_TIMEOUT;
1187 /* Process Unlocked */
1188 __HAL_UNLOCK(hcryp);
1194 /* Reset the ALGOMODE bits*/
1195 CRYP->CR &= (uint32_t)(~CRYP_CR_ALGOMODE);
1197 /* Set the CRYP peripheral in AES CBC decryption mode */
1198 __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_CBC | CRYP_CR_ALGODIR);
1200 /* Set the Initialization Vector */
1201 CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_128B);
1204 __HAL_CRYP_FIFO_FLUSH();
1207 __HAL_CRYP_ENABLE();
1210 hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
1213 /* Enable Interrupts */
1214 __HAL_CRYP_ENABLE_IT(CRYP_IT_INI | CRYP_IT_OUTI);
1217 __HAL_CRYP_ENABLE();
1219 /* Return function status */
1222 else if(__HAL_CRYP_GET_IT(CRYP_IT_INI))
1224 inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
1225 /* Write the Input block in the IN FIFO */
1226 CRYP->DR = *(uint32_t*)(inputaddr);
1228 CRYP->DR = *(uint32_t*)(inputaddr);
1230 CRYP->DR = *(uint32_t*)(inputaddr);
1232 CRYP->DR = *(uint32_t*)(inputaddr);
1233 hcryp->pCrypInBuffPtr += 16;
1234 hcryp->CrypInCount -= 16;
1235 if(hcryp->CrypInCount == 0)
1237 __HAL_CRYP_DISABLE_IT(CRYP_IT_INI);
1238 /* Call the Input data transfer complete callback */
1239 HAL_CRYP_InCpltCallback(hcryp);
1242 else if(__HAL_CRYP_GET_IT(CRYP_IT_OUTI))
1244 outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
1245 /* Read the Output block from the Output FIFO */
1246 *(uint32_t*)(outputaddr) = CRYP->DOUT;
1248 *(uint32_t*)(outputaddr) = CRYP->DOUT;
1250 *(uint32_t*)(outputaddr) = CRYP->DOUT;
1252 *(uint32_t*)(outputaddr) = CRYP->DOUT;
1253 hcryp->pCrypOutBuffPtr += 16;
1254 hcryp->CrypOutCount -= 16;
1255 if(hcryp->CrypOutCount == 0)
1257 __HAL_CRYP_DISABLE_IT(CRYP_IT_OUTI);
1258 /* Process Unlocked */
1259 __HAL_UNLOCK(hcryp);
1260 /* Change the CRYP state */
1261 hcryp->State = HAL_CRYP_STATE_READY;
1262 /* Call Input transfer complete callback */
1263 HAL_CRYP_OutCpltCallback(hcryp);
1267 /* Return function status */
1272 * @brief Initializes the CRYP peripheral in AES CTR decryption mode using Interrupt.
1273 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
1274 * the configuration information for CRYP module
1275 * @param pCypherData: Pointer to the cyphertext buffer
1276 * @param Size: Length of the plaintext buffer, must be a multiple of 16
1277 * @param pPlainData: Pointer to the plaintext buffer
1278 * @retval HAL status
1280 HAL_StatusTypeDef HAL_CRYP_AESCTR_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
1283 uint32_t outputaddr;
1285 if(hcryp->State == HAL_CRYP_STATE_READY)
1287 /* Process Locked */
1290 /* Get the buffer addresses and sizes */
1291 hcryp->CrypInCount = Size;
1292 hcryp->pCrypInBuffPtr = pCypherData;
1293 hcryp->pCrypOutBuffPtr = pPlainData;
1294 hcryp->CrypOutCount = Size;
1296 /* Change the CRYP state */
1297 hcryp->State = HAL_CRYP_STATE_BUSY;
1299 /* Check if initialization phase has already been performed */
1300 if(hcryp->Phase == HAL_CRYP_PHASE_READY)
1303 CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
1305 /* Set the CRYP peripheral in AES CTR mode */
1306 __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_CTR | CRYP_CR_ALGODIR);
1308 /* Set the Initialization Vector */
1309 CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_128B);
1312 __HAL_CRYP_FIFO_FLUSH();
1315 hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
1318 /* Enable Interrupts */
1319 __HAL_CRYP_ENABLE_IT(CRYP_IT_INI | CRYP_IT_OUTI);
1322 __HAL_CRYP_ENABLE();
1324 /* Return function status */
1327 else if(__HAL_CRYP_GET_IT(CRYP_IT_INI))
1329 inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
1330 /* Write the Input block in the IN FIFO */
1331 CRYP->DR = *(uint32_t*)(inputaddr);
1333 CRYP->DR = *(uint32_t*)(inputaddr);
1335 CRYP->DR = *(uint32_t*)(inputaddr);
1337 CRYP->DR = *(uint32_t*)(inputaddr);
1338 hcryp->pCrypInBuffPtr += 16;
1339 hcryp->CrypInCount -= 16;
1340 if(hcryp->CrypInCount == 0)
1342 __HAL_CRYP_DISABLE_IT(CRYP_IT_INI);
1343 /* Call the Input data transfer complete callback */
1344 HAL_CRYP_InCpltCallback(hcryp);
1347 else if(__HAL_CRYP_GET_IT(CRYP_IT_OUTI))
1349 outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
1350 /* Read the Output block from the Output FIFO */
1351 *(uint32_t*)(outputaddr) = CRYP->DOUT;
1353 *(uint32_t*)(outputaddr) = CRYP->DOUT;
1355 *(uint32_t*)(outputaddr) = CRYP->DOUT;
1357 *(uint32_t*)(outputaddr) = CRYP->DOUT;
1358 hcryp->pCrypOutBuffPtr += 16;
1359 hcryp->CrypOutCount -= 16;
1360 if(hcryp->CrypOutCount == 0)
1362 __HAL_CRYP_DISABLE_IT(CRYP_IT_OUTI);
1363 /* Process Unlocked */
1364 __HAL_UNLOCK(hcryp);
1365 /* Change the CRYP state */
1366 hcryp->State = HAL_CRYP_STATE_READY;
1367 /* Call Input transfer complete callback */
1368 HAL_CRYP_OutCpltCallback(hcryp);
1372 /* Return function status */
1377 * @brief Initializes the CRYP peripheral in AES ECB encryption mode using DMA.
1378 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
1379 * the configuration information for CRYP module
1380 * @param pPlainData: Pointer to the plaintext buffer
1381 * @param Size: Length of the plaintext buffer, must be a multiple of 16 bytes
1382 * @param pCypherData: Pointer to the cyphertext buffer
1383 * @retval HAL status
1385 HAL_StatusTypeDef HAL_CRYP_AESECB_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
1388 uint32_t outputaddr;
1390 if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS))
1392 /* Process Locked */
1395 inputaddr = (uint32_t)pPlainData;
1396 outputaddr = (uint32_t)pCypherData;
1398 /* Change the CRYP state */
1399 hcryp->State = HAL_CRYP_STATE_BUSY;
1401 /* Check if initialization phase has already been performed */
1402 if(hcryp->Phase == HAL_CRYP_PHASE_READY)
1405 CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
1407 /* Set the CRYP peripheral in AES ECB mode */
1408 __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_ECB);
1411 __HAL_CRYP_FIFO_FLUSH();
1414 hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
1416 /* Set the input and output addresses and start DMA transfer */
1417 CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
1419 /* Process Unlocked */
1420 __HAL_UNLOCK(hcryp);
1422 /* Return function status */
1432 * @brief Initializes the CRYP peripheral in AES CBC encryption mode using DMA.
1433 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
1434 * the configuration information for CRYP module
1435 * @param pPlainData: Pointer to the plaintext buffer
1436 * @param Size: Length of the plaintext buffer, must be a multiple of 16.
1437 * @param pCypherData: Pointer to the cyphertext buffer
1438 * @retval HAL status
1440 HAL_StatusTypeDef HAL_CRYP_AESCBC_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
1443 uint32_t outputaddr;
1445 if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS))
1447 /* Process Locked */
1450 inputaddr = (uint32_t)pPlainData;
1451 outputaddr = (uint32_t)pCypherData;
1453 /* Change the CRYP state */
1454 hcryp->State = HAL_CRYP_STATE_BUSY;
1456 /* Check if initialization phase has already been performed */
1457 if(hcryp->Phase == HAL_CRYP_PHASE_READY)
1460 CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
1462 /* Set the CRYP peripheral in AES ECB mode */
1463 __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_CBC);
1465 /* Set the Initialization Vector */
1466 CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_128B);
1469 __HAL_CRYP_FIFO_FLUSH();
1472 hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
1474 /* Set the input and output addresses and start DMA transfer */
1475 CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
1477 /* Process Unlocked */
1478 __HAL_UNLOCK(hcryp);
1480 /* Return function status */
1490 * @brief Initializes the CRYP peripheral in AES CTR encryption mode using DMA.
1491 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
1492 * the configuration information for CRYP module
1493 * @param pPlainData: Pointer to the plaintext buffer
1494 * @param Size: Length of the plaintext buffer, must be a multiple of 16.
1495 * @param pCypherData: Pointer to the cyphertext buffer
1496 * @retval HAL status
1498 HAL_StatusTypeDef HAL_CRYP_AESCTR_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
1501 uint32_t outputaddr;
1503 if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS))
1505 /* Process Locked */
1508 inputaddr = (uint32_t)pPlainData;
1509 outputaddr = (uint32_t)pCypherData;
1511 /* Change the CRYP state */
1512 hcryp->State = HAL_CRYP_STATE_BUSY;
1514 /* Check if initialization phase has already been performed */
1515 if(hcryp->Phase == HAL_CRYP_PHASE_READY)
1518 CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
1520 /* Set the CRYP peripheral in AES ECB mode */
1521 __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_CTR);
1523 /* Set the Initialization Vector */
1524 CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_128B);
1527 __HAL_CRYP_FIFO_FLUSH();
1530 hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
1533 /* Set the input and output addresses and start DMA transfer */
1534 CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
1536 /* Process Unlocked */
1537 __HAL_UNLOCK(hcryp);
1539 /* Return function status */
1549 * @brief Initializes the CRYP peripheral in AES ECB decryption mode using DMA.
1550 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
1551 * the configuration information for CRYP module
1552 * @param pCypherData: Pointer to the cyphertext buffer
1553 * @param Size: Length of the plaintext buffer, must be a multiple of 16 bytes
1554 * @param pPlainData: Pointer to the plaintext buffer
1555 * @retval HAL status
1557 HAL_StatusTypeDef HAL_CRYP_AESECB_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
1559 uint32_t tickstart = 0;
1561 uint32_t outputaddr;
1563 if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS))
1565 /* Process Locked */
1568 inputaddr = (uint32_t)pCypherData;
1569 outputaddr = (uint32_t)pPlainData;
1571 /* Change the CRYP state */
1572 hcryp->State = HAL_CRYP_STATE_BUSY;
1574 /* Check if initialization phase has already been performed */
1575 if(hcryp->Phase == HAL_CRYP_PHASE_READY)
1578 CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
1580 /* Set the CRYP peripheral in AES Key mode */
1581 __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_KEY | CRYP_CR_ALGODIR);
1584 __HAL_CRYP_ENABLE();
1587 tickstart = HAL_GetTick();
1589 while(HAL_IS_BIT_SET(CRYP->SR, CRYP_FLAG_BUSY))
1591 /* Check for the Timeout */
1592 if((HAL_GetTick() - tickstart ) > CRYP_TIMEOUT_VALUE)
1595 hcryp->State = HAL_CRYP_STATE_TIMEOUT;
1597 /* Process Unlocked */
1598 __HAL_UNLOCK(hcryp);
1604 /* Reset the ALGOMODE bits*/
1605 CRYP->CR &= (uint32_t)(~CRYP_CR_ALGOMODE);
1607 /* Set the CRYP peripheral in AES ECB decryption mode */
1608 __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_ECB | CRYP_CR_ALGODIR);
1611 __HAL_CRYP_FIFO_FLUSH();
1614 hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
1617 /* Set the input and output addresses and start DMA transfer */
1618 CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
1620 /* Process Unlocked */
1621 __HAL_UNLOCK(hcryp);
1623 /* Return function status */
1633 * @brief Initializes the CRYP peripheral in AES CBC encryption mode using DMA.
1634 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
1635 * the configuration information for CRYP module
1636 * @param pCypherData: Pointer to the cyphertext buffer
1637 * @param Size: Length of the plaintext buffer, must be a multiple of 16 bytes
1638 * @param pPlainData: Pointer to the plaintext buffer
1639 * @retval HAL status
1641 HAL_StatusTypeDef HAL_CRYP_AESCBC_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
1643 uint32_t tickstart = 0;
1645 uint32_t outputaddr;
1647 if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS))
1649 /* Process Locked */
1652 inputaddr = (uint32_t)pCypherData;
1653 outputaddr = (uint32_t)pPlainData;
1655 /* Change the CRYP state */
1656 hcryp->State = HAL_CRYP_STATE_BUSY;
1658 /* Check if initialization phase has already been performed */
1659 if(hcryp->Phase == HAL_CRYP_PHASE_READY)
1662 CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
1664 /* Set the CRYP peripheral in AES Key mode */
1665 __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_KEY | CRYP_CR_ALGODIR);
1668 __HAL_CRYP_ENABLE();
1671 tickstart = HAL_GetTick();
1673 while(HAL_IS_BIT_SET(CRYP->SR, CRYP_FLAG_BUSY))
1675 /* Check for the Timeout */
1676 if((HAL_GetTick() - tickstart ) > CRYP_TIMEOUT_VALUE)
1679 hcryp->State = HAL_CRYP_STATE_TIMEOUT;
1681 /* Process Unlocked */
1682 __HAL_UNLOCK(hcryp);
1688 /* Reset the ALGOMODE bits*/
1689 CRYP->CR &= (uint32_t)(~CRYP_CR_ALGOMODE);
1691 /* Set the CRYP peripheral in AES CBC decryption mode */
1692 __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_CBC | CRYP_CR_ALGODIR);
1694 /* Set the Initialization Vector */
1695 CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_128B);
1698 __HAL_CRYP_FIFO_FLUSH();
1701 hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
1704 /* Set the input and output addresses and start DMA transfer */
1705 CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
1707 /* Process Unlocked */
1708 __HAL_UNLOCK(hcryp);
1710 /* Return function status */
1720 * @brief Initializes the CRYP peripheral in AES CTR decryption mode using DMA.
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_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
1731 uint32_t outputaddr;
1733 if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS))
1735 /* Process Locked */
1738 inputaddr = (uint32_t)pCypherData;
1739 outputaddr = (uint32_t)pPlainData;
1741 /* Change the CRYP state */
1742 hcryp->State = HAL_CRYP_STATE_BUSY;
1744 /* Check if initialization phase has already been performed */
1745 if(hcryp->Phase == HAL_CRYP_PHASE_READY)
1748 CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
1750 /* Set the CRYP peripheral in AES CTR mode */
1751 __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_CTR | CRYP_CR_ALGODIR);
1753 /* Set the Initialization Vector */
1754 CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_128B);
1757 __HAL_CRYP_FIFO_FLUSH();
1760 hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
1763 /* Set the input and output addresses and start DMA transfer */
1764 CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
1766 /* Process Unlocked */
1767 __HAL_UNLOCK(hcryp);
1769 /* Return function status */
1782 /** @defgroup CRYP_Group3 DES processing functions
1783 * @brief processing functions.
1786 ==============================================================================
1787 ##### DES processing functions #####
1788 ==============================================================================
1789 [..] This section provides functions allowing to:
1790 (+) Encrypt plaintext using DES using ECB or CBC chaining modes
1791 (+) Decrypt cyphertext using ECB or CBC chaining modes
1792 [..] Three processing functions are available:
1802 * @brief Initializes the CRYP peripheral in DES ECB encryption mode.
1803 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
1804 * the configuration information for CRYP module
1805 * @param pPlainData: Pointer to the plaintext buffer
1806 * @param Size: Length of the plaintext buffer, must be a multiple of 8
1807 * @param pCypherData: Pointer to the cyphertext buffer
1808 * @param Timeout: Specify Timeout value
1809 * @retval HAL status
1811 HAL_StatusTypeDef HAL_CRYP_DESECB_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout)
1813 /* Process Locked */
1816 /* Change the CRYP state */
1817 hcryp->State = HAL_CRYP_STATE_BUSY;
1819 /* Set CRYP peripheral in DES ECB encryption mode */
1820 CRYP_SetDESECBMode(hcryp, 0);
1823 __HAL_CRYP_ENABLE();
1825 /* Write Plain Data and Get Cypher Data */
1826 if(CRYP_ProcessData2Words(hcryp, pPlainData, Size, pCypherData, Timeout) != HAL_OK)
1831 /* Change the CRYP state */
1832 hcryp->State = HAL_CRYP_STATE_READY;
1834 /* Process Unlocked */
1835 __HAL_UNLOCK(hcryp);
1837 /* Return function status */
1842 * @brief Initializes the CRYP peripheral in DES ECB decryption mode.
1843 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
1844 * the configuration information for CRYP module
1845 * @param pPlainData: Pointer to the plaintext buffer
1846 * @param Size: Length of the plaintext buffer, must be a multiple of 8
1847 * @param pCypherData: Pointer to the cyphertext buffer
1848 * @param Timeout: Specify Timeout value
1849 * @retval HAL status
1851 HAL_StatusTypeDef HAL_CRYP_DESECB_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout)
1853 /* Process Locked */
1856 /* Change the CRYP state */
1857 hcryp->State = HAL_CRYP_STATE_BUSY;
1859 /* Set CRYP peripheral in DES ECB decryption mode */
1860 CRYP_SetDESECBMode(hcryp, CRYP_CR_ALGODIR);
1863 __HAL_CRYP_ENABLE();
1865 /* Write Plain Data and Get Cypher Data */
1866 if(CRYP_ProcessData2Words(hcryp, pPlainData, Size, pCypherData, Timeout) != HAL_OK)
1871 /* Change the CRYP state */
1872 hcryp->State = HAL_CRYP_STATE_READY;
1874 /* Process Unlocked */
1875 __HAL_UNLOCK(hcryp);
1877 /* Return function status */
1882 * @brief Initializes the CRYP peripheral in DES CBC encryption mode.
1883 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
1884 * the configuration information for CRYP module
1885 * @param pPlainData: Pointer to the plaintext buffer
1886 * @param Size: Length of the plaintext buffer, must be a multiple of 8
1887 * @param pCypherData: Pointer to the cyphertext buffer
1888 * @param Timeout: Specify Timeout value
1889 * @retval HAL status
1891 HAL_StatusTypeDef HAL_CRYP_DESCBC_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout)
1893 /* Process Locked */
1896 /* Change the CRYP state */
1897 hcryp->State = HAL_CRYP_STATE_BUSY;
1899 /* Set CRYP peripheral in DES CBC encryption mode */
1900 CRYP_SetDESCBCMode(hcryp, 0);
1903 __HAL_CRYP_ENABLE();
1905 /* Write Plain Data and Get Cypher Data */
1906 if(CRYP_ProcessData2Words(hcryp, pPlainData, Size, pCypherData, Timeout) != HAL_OK)
1911 /* Change the CRYP state */
1912 hcryp->State = HAL_CRYP_STATE_READY;
1914 /* Process Unlocked */
1915 __HAL_UNLOCK(hcryp);
1917 /* Return function status */
1922 * @brief Initializes the CRYP peripheral in DES ECB decryption mode.
1923 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
1924 * the configuration information for CRYP module
1925 * @param pPlainData: Pointer to the plaintext buffer
1926 * @param Size: Length of the plaintext buffer, must be a multiple of 8
1927 * @param pCypherData: Pointer to the cyphertext buffer
1928 * @param Timeout: Specify Timeout value
1929 * @retval HAL status
1931 HAL_StatusTypeDef HAL_CRYP_DESCBC_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout)
1933 /* Process Locked */
1936 /* Change the CRYP state */
1937 hcryp->State = HAL_CRYP_STATE_BUSY;
1939 /* Set CRYP peripheral in DES CBC decryption mode */
1940 CRYP_SetDESCBCMode(hcryp, CRYP_CR_ALGODIR);
1943 __HAL_CRYP_ENABLE();
1945 /* Write Plain Data and Get Cypher Data */
1946 if(CRYP_ProcessData2Words(hcryp, pPlainData, Size, pCypherData, Timeout) != HAL_OK)
1951 /* Change the CRYP state */
1952 hcryp->State = HAL_CRYP_STATE_READY;
1954 /* Process Unlocked */
1955 __HAL_UNLOCK(hcryp);
1957 /* Return function status */
1962 * @brief Initializes the CRYP peripheral in DES ECB encryption mode using IT.
1963 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
1964 * the configuration information for CRYP module
1965 * @param pPlainData: Pointer to the plaintext buffer
1966 * @param Size: Length of the plaintext buffer, must be a multiple of 8
1967 * @param pCypherData: Pointer to the cyphertext buffer
1968 * @retval HAL status
1970 HAL_StatusTypeDef HAL_CRYP_DESECB_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
1973 uint32_t outputaddr;
1975 if(hcryp->State == HAL_CRYP_STATE_READY)
1977 /* Process Locked */
1980 hcryp->CrypInCount = Size;
1981 hcryp->pCrypInBuffPtr = pPlainData;
1982 hcryp->pCrypOutBuffPtr = pCypherData;
1983 hcryp->CrypOutCount = Size;
1985 /* Change the CRYP state */
1986 hcryp->State = HAL_CRYP_STATE_BUSY;
1988 /* Set CRYP peripheral in DES ECB encryption mode */
1989 CRYP_SetDESECBMode(hcryp, 0);
1991 /* Enable Interrupts */
1992 __HAL_CRYP_ENABLE_IT(CRYP_IT_INI | CRYP_IT_OUTI);
1995 __HAL_CRYP_ENABLE();
1997 /* Return function status */
2000 else if(__HAL_CRYP_GET_IT(CRYP_IT_INI))
2002 inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
2003 /* Write the Input block in the IN FIFO */
2004 CRYP->DR = *(uint32_t*)(inputaddr);
2006 CRYP->DR = *(uint32_t*)(inputaddr);
2008 hcryp->pCrypInBuffPtr += 8;
2009 hcryp->CrypInCount -= 8;
2010 if(hcryp->CrypInCount == 0)
2012 __HAL_CRYP_DISABLE_IT(CRYP_IT_INI);
2013 /* Call the Input data transfer complete callback */
2014 HAL_CRYP_InCpltCallback(hcryp);
2017 else if(__HAL_CRYP_GET_IT(CRYP_IT_OUTI))
2019 outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
2020 /* Read the Output block from the Output FIFO */
2021 *(uint32_t*)(outputaddr) = CRYP->DOUT;
2023 *(uint32_t*)(outputaddr) = CRYP->DOUT;
2025 hcryp->pCrypOutBuffPtr += 8;
2026 hcryp->CrypOutCount -= 8;
2027 if(hcryp->CrypOutCount == 0)
2030 __HAL_CRYP_DISABLE_IT(CRYP_IT_OUTI);
2032 __HAL_CRYP_DISABLE();
2033 /* Process Unlocked */
2034 __HAL_UNLOCK(hcryp);
2035 /* Change the CRYP state */
2036 hcryp->State = HAL_CRYP_STATE_READY;
2037 /* Call Input transfer complete callback */
2038 HAL_CRYP_OutCpltCallback(hcryp);
2042 /* Return function status */
2047 * @brief Initializes the CRYP peripheral in DES CBC encryption mode using interrupt.
2048 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
2049 * the configuration information for CRYP module
2050 * @param pPlainData: Pointer to the plaintext buffer
2051 * @param Size: Length of the plaintext buffer, must be a multiple of 8
2052 * @param pCypherData: Pointer to the cyphertext buffer
2053 * @retval HAL status
2055 HAL_StatusTypeDef HAL_CRYP_DESCBC_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
2058 uint32_t outputaddr;
2060 if(hcryp->State == HAL_CRYP_STATE_READY)
2062 /* Process Locked */
2065 hcryp->CrypInCount = Size;
2066 hcryp->pCrypInBuffPtr = pPlainData;
2067 hcryp->pCrypOutBuffPtr = pCypherData;
2068 hcryp->CrypOutCount = Size;
2070 /* Change the CRYP state */
2071 hcryp->State = HAL_CRYP_STATE_BUSY;
2073 /* Set CRYP peripheral in DES CBC encryption mode */
2074 CRYP_SetDESCBCMode(hcryp, 0);
2076 /* Enable Interrupts */
2077 __HAL_CRYP_ENABLE_IT(CRYP_IT_INI | CRYP_IT_OUTI);
2080 __HAL_CRYP_ENABLE();
2082 /* Return function status */
2086 else if(__HAL_CRYP_GET_IT(CRYP_IT_INI))
2088 inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
2089 /* Write the Input block in the IN FIFO */
2090 CRYP->DR = *(uint32_t*)(inputaddr);
2092 CRYP->DR = *(uint32_t*)(inputaddr);
2094 hcryp->pCrypInBuffPtr += 8;
2095 hcryp->CrypInCount -= 8;
2096 if(hcryp->CrypInCount == 0)
2098 __HAL_CRYP_DISABLE_IT(CRYP_IT_INI);
2099 /* Call the Input data transfer complete callback */
2100 HAL_CRYP_InCpltCallback(hcryp);
2103 else if(__HAL_CRYP_GET_IT(CRYP_IT_OUTI))
2105 outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
2106 /* Read the Output block from the Output FIFO */
2107 *(uint32_t*)(outputaddr) = CRYP->DOUT;
2109 *(uint32_t*)(outputaddr) = CRYP->DOUT;
2111 hcryp->pCrypOutBuffPtr += 8;
2112 hcryp->CrypOutCount -= 8;
2113 if(hcryp->CrypOutCount == 0)
2116 __HAL_CRYP_DISABLE_IT(CRYP_IT_OUTI);
2118 __HAL_CRYP_DISABLE();
2119 /* Process Unlocked */
2120 __HAL_UNLOCK(hcryp);
2121 /* Change the CRYP state */
2122 hcryp->State = HAL_CRYP_STATE_READY;
2123 /* Call Input transfer complete callback */
2124 HAL_CRYP_OutCpltCallback(hcryp);
2128 /* Return function status */
2133 * @brief Initializes the CRYP peripheral in DES ECB decryption mode using IT.
2134 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
2135 * the configuration information for CRYP module
2136 * @param pPlainData: Pointer to the plaintext buffer
2137 * @param Size: Length of the plaintext buffer, must be a multiple of 8
2138 * @param pCypherData: Pointer to the cyphertext buffer
2139 * @retval HAL status
2141 HAL_StatusTypeDef HAL_CRYP_DESECB_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
2144 uint32_t outputaddr;
2146 if(hcryp->State == HAL_CRYP_STATE_READY)
2148 /* Process Locked */
2151 hcryp->CrypInCount = Size;
2152 hcryp->pCrypInBuffPtr = pCypherData;
2153 hcryp->pCrypOutBuffPtr = pPlainData;
2154 hcryp->CrypOutCount = Size;
2156 /* Change the CRYP state */
2157 hcryp->State = HAL_CRYP_STATE_BUSY;
2159 /* Set CRYP peripheral in DES ECB decryption mode */
2160 CRYP_SetDESECBMode(hcryp, CRYP_CR_ALGODIR);
2162 /* Enable Interrupts */
2163 __HAL_CRYP_ENABLE_IT(CRYP_IT_INI | CRYP_IT_OUTI);
2166 __HAL_CRYP_ENABLE();
2168 /* Return function status */
2171 else if(__HAL_CRYP_GET_IT(CRYP_IT_INI))
2173 inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
2174 /* Write the Input block in the IN FIFO */
2175 CRYP->DR = *(uint32_t*)(inputaddr);
2177 CRYP->DR = *(uint32_t*)(inputaddr);
2179 hcryp->pCrypInBuffPtr += 8;
2180 hcryp->CrypInCount -= 8;
2181 if(hcryp->CrypInCount == 0)
2183 __HAL_CRYP_DISABLE_IT(CRYP_IT_INI);
2184 /* Call the Input data transfer complete callback */
2185 HAL_CRYP_InCpltCallback(hcryp);
2188 else if(__HAL_CRYP_GET_IT(CRYP_IT_OUTI))
2190 outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
2191 /* Read the Output block from the Output FIFO */
2192 *(uint32_t*)(outputaddr) = CRYP->DOUT;
2194 *(uint32_t*)(outputaddr) = CRYP->DOUT;
2196 hcryp->pCrypOutBuffPtr += 8;
2197 hcryp->CrypOutCount -= 8;
2198 if(hcryp->CrypOutCount == 0)
2201 __HAL_CRYP_DISABLE_IT(CRYP_IT_OUTI);
2203 __HAL_CRYP_DISABLE();
2204 /* Process Unlocked */
2205 __HAL_UNLOCK(hcryp);
2206 /* Change the CRYP state */
2207 hcryp->State = HAL_CRYP_STATE_READY;
2208 /* Call Input transfer complete callback */
2209 HAL_CRYP_OutCpltCallback(hcryp);
2213 /* Return function status */
2218 * @brief Initializes the CRYP peripheral in DES ECB decryption mode using interrupt.
2219 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
2220 * the configuration information for CRYP module
2221 * @param pPlainData: Pointer to the plaintext buffer
2222 * @param Size: Length of the plaintext buffer, must be a multiple of 8
2223 * @param pCypherData: Pointer to the cyphertext buffer
2224 * @retval HAL status
2226 HAL_StatusTypeDef HAL_CRYP_DESCBC_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
2229 uint32_t outputaddr;
2231 if(hcryp->State == HAL_CRYP_STATE_READY)
2233 /* Process Locked */
2236 hcryp->CrypInCount = Size;
2237 hcryp->pCrypInBuffPtr = pCypherData;
2238 hcryp->pCrypOutBuffPtr = pPlainData;
2239 hcryp->CrypOutCount = Size;
2241 /* Change the CRYP state */
2242 hcryp->State = HAL_CRYP_STATE_BUSY;
2244 /* Set CRYP peripheral in DES CBC decryption mode */
2245 CRYP_SetDESCBCMode(hcryp, CRYP_CR_ALGODIR);
2247 /* Enable Interrupts */
2248 __HAL_CRYP_ENABLE_IT(CRYP_IT_INI | CRYP_IT_OUTI);
2251 __HAL_CRYP_ENABLE();
2253 /* Return function status */
2256 else if(__HAL_CRYP_GET_IT(CRYP_IT_INI))
2258 inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
2259 /* Write the Input block in the IN FIFO */
2260 CRYP->DR = *(uint32_t*)(inputaddr);
2262 CRYP->DR = *(uint32_t*)(inputaddr);
2264 hcryp->pCrypInBuffPtr += 8;
2265 hcryp->CrypInCount -= 8;
2266 if(hcryp->CrypInCount == 0)
2268 __HAL_CRYP_DISABLE_IT(CRYP_IT_INI);
2269 /* Call the Input data transfer complete callback */
2270 HAL_CRYP_InCpltCallback(hcryp);
2273 else if(__HAL_CRYP_GET_IT(CRYP_IT_OUTI))
2275 outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
2276 /* Read the Output block from the Output FIFO */
2277 *(uint32_t*)(outputaddr) = CRYP->DOUT;
2279 *(uint32_t*)(outputaddr) = CRYP->DOUT;
2281 hcryp->pCrypOutBuffPtr += 8;
2282 hcryp->CrypOutCount -= 8;
2283 if(hcryp->CrypOutCount == 0)
2286 __HAL_CRYP_DISABLE_IT(CRYP_IT_OUTI);
2288 __HAL_CRYP_DISABLE();
2289 /* Process Unlocked */
2290 __HAL_UNLOCK(hcryp);
2291 /* Change the CRYP state */
2292 hcryp->State = HAL_CRYP_STATE_READY;
2293 /* Call Input transfer complete callback */
2294 HAL_CRYP_OutCpltCallback(hcryp);
2298 /* Return function status */
2303 * @brief Initializes the CRYP peripheral in DES ECB encryption mode using DMA.
2304 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
2305 * the configuration information for CRYP module
2306 * @param pPlainData: Pointer to the plaintext buffer
2307 * @param Size: Length of the plaintext buffer, must be a multiple of 8
2308 * @param pCypherData: Pointer to the cyphertext buffer
2309 * @retval HAL status
2311 HAL_StatusTypeDef HAL_CRYP_DESECB_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
2314 uint32_t outputaddr;
2316 if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS))
2318 /* Process Locked */
2321 inputaddr = (uint32_t)pPlainData;
2322 outputaddr = (uint32_t)pCypherData;
2324 /* Change the CRYP state */
2325 hcryp->State = HAL_CRYP_STATE_BUSY;
2327 /* Set CRYP peripheral in DES ECB encryption mode */
2328 CRYP_SetDESECBMode(hcryp, 0);
2330 /* Set the input and output addresses and start DMA transfer */
2331 CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
2333 /* Process Unlocked */
2334 __HAL_UNLOCK(hcryp);
2336 /* Return function status */
2346 * @brief Initializes the CRYP peripheral in DES CBC encryption mode using DMA.
2347 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
2348 * the configuration information for CRYP module
2349 * @param pPlainData: Pointer to the plaintext buffer
2350 * @param Size: Length of the plaintext buffer, must be a multiple of 8
2351 * @param pCypherData: Pointer to the cyphertext buffer
2352 * @retval HAL status
2354 HAL_StatusTypeDef HAL_CRYP_DESCBC_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
2357 uint32_t outputaddr;
2359 if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS))
2361 /* Process Locked */
2364 inputaddr = (uint32_t)pPlainData;
2365 outputaddr = (uint32_t)pCypherData;
2367 /* Change the CRYP state */
2368 hcryp->State = HAL_CRYP_STATE_BUSY;
2370 /* Set CRYP peripheral in DES CBC encryption mode */
2371 CRYP_SetDESCBCMode(hcryp, 0);
2373 /* Set the input and output addresses and start DMA transfer */
2374 CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
2376 /* Process Unlocked */
2377 __HAL_UNLOCK(hcryp);
2379 /* Return function status */
2389 * @brief Initializes the CRYP peripheral in DES ECB decryption mode using DMA.
2390 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
2391 * the configuration information for CRYP module
2392 * @param pPlainData: Pointer to the plaintext buffer
2393 * @param Size: Length of the plaintext buffer, must be a multiple of 8
2394 * @param pCypherData: Pointer to the cyphertext buffer
2395 * @retval HAL status
2397 HAL_StatusTypeDef HAL_CRYP_DESECB_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
2400 uint32_t outputaddr;
2402 if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS))
2404 /* Process Locked */
2407 inputaddr = (uint32_t)pCypherData;
2408 outputaddr = (uint32_t)pPlainData;
2410 /* Change the CRYP state */
2411 hcryp->State = HAL_CRYP_STATE_BUSY;
2413 /* Set CRYP peripheral in DES ECB decryption mode */
2414 CRYP_SetDESECBMode(hcryp, CRYP_CR_ALGODIR);
2416 /* Set the input and output addresses and start DMA transfer */
2417 CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
2419 /* Process Unlocked */
2420 __HAL_UNLOCK(hcryp);
2422 /* Return function status */
2432 * @brief Initializes the CRYP peripheral in DES ECB decryption mode using DMA.
2433 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
2434 * the configuration information for CRYP module
2435 * @param pPlainData: Pointer to the plaintext buffer
2436 * @param Size: Length of the plaintext buffer, must be a multiple of 8
2437 * @param pCypherData: Pointer to the cyphertext buffer
2438 * @retval HAL status
2440 HAL_StatusTypeDef HAL_CRYP_DESCBC_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
2443 uint32_t outputaddr;
2445 if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS))
2447 /* Process Locked */
2450 inputaddr = (uint32_t)pCypherData;
2451 outputaddr = (uint32_t)pPlainData;
2453 /* Change the CRYP state */
2454 hcryp->State = HAL_CRYP_STATE_BUSY;
2456 /* Set CRYP peripheral in DES CBC decryption mode */
2457 CRYP_SetDESCBCMode(hcryp, CRYP_CR_ALGODIR);
2459 /* Set the input and output addresses and start DMA transfer */
2460 CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
2462 /* Process Unlocked */
2463 __HAL_UNLOCK(hcryp);
2465 /* Return function status */
2478 /** @defgroup CRYP_Group4 TDES processing functions
2479 * @brief processing functions.
2482 ==============================================================================
2483 ##### TDES processing functions #####
2484 ==============================================================================
2485 [..] This section provides functions allowing to:
2486 (+) Encrypt plaintext using TDES based on ECB or CBC chaining modes
2487 (+) Decrypt cyphertext using TDES based on ECB or CBC chaining modes
2488 [..] Three processing functions are available:
2498 * @brief Initializes the CRYP peripheral in TDES ECB encryption mode
2499 * then encrypt pPlainData. The cypher data are available in pCypherData
2500 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
2501 * the configuration information for CRYP module
2502 * @param pPlainData: Pointer to the plaintext buffer
2503 * @param Size: Length of the plaintext buffer, must be a multiple of 8
2504 * @param pCypherData: Pointer to the cyphertext buffer
2505 * @param Timeout: Specify Timeout value
2506 * @retval HAL status
2508 HAL_StatusTypeDef HAL_CRYP_TDESECB_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout)
2510 /* Process Locked */
2513 /* Change the CRYP state */
2514 hcryp->State = HAL_CRYP_STATE_BUSY;
2516 /* Set CRYP peripheral in TDES ECB encryption mode */
2517 CRYP_SetTDESECBMode(hcryp, 0);
2520 __HAL_CRYP_ENABLE();
2522 /* Write Plain Data and Get Cypher Data */
2523 if(CRYP_ProcessData2Words(hcryp, pPlainData, Size, pCypherData, Timeout) != HAL_OK)
2528 /* Change the CRYP state */
2529 hcryp->State = HAL_CRYP_STATE_READY;
2531 /* Process Unlocked */
2532 __HAL_UNLOCK(hcryp);
2534 /* Return function status */
2539 * @brief Initializes the CRYP peripheral in TDES ECB decryption mode
2540 * then decrypted pCypherData. The cypher data are available in pPlainData
2541 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
2542 * the configuration information for CRYP module
2543 * @param pPlainData: Pointer to the plaintext buffer
2544 * @param Size: Length of the plaintext buffer, must be a multiple of 8
2545 * @param pCypherData: Pointer to the cyphertext buffer
2546 * @param Timeout: Specify Timeout value
2547 * @retval HAL status
2549 HAL_StatusTypeDef HAL_CRYP_TDESECB_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout)
2551 /* Process Locked */
2554 /* Change the CRYP state */
2555 hcryp->State = HAL_CRYP_STATE_BUSY;
2557 /* Set CRYP peripheral in TDES ECB decryption mode */
2558 CRYP_SetTDESECBMode(hcryp, CRYP_CR_ALGODIR);
2561 __HAL_CRYP_ENABLE();
2563 /* Write Cypher Data and Get Plain Data */
2564 if(CRYP_ProcessData2Words(hcryp, pCypherData, Size, pPlainData, Timeout) != HAL_OK)
2569 /* Change the CRYP state */
2570 hcryp->State = HAL_CRYP_STATE_READY;
2572 /* Process Unlocked */
2573 __HAL_UNLOCK(hcryp);
2575 /* Return function status */
2580 * @brief Initializes the CRYP peripheral in TDES CBC encryption mode
2581 * then encrypt pPlainData. The cypher data are available in pCypherData
2582 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
2583 * the configuration information for CRYP module
2584 * @param pPlainData: Pointer to the plaintext buffer
2585 * @param Size: Length of the plaintext buffer, must be a multiple of 8
2586 * @param pCypherData: Pointer to the cyphertext buffer
2587 * @param Timeout: Specify Timeout value
2588 * @retval HAL status
2590 HAL_StatusTypeDef HAL_CRYP_TDESCBC_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout)
2592 /* Process Locked */
2595 /* Change the CRYP state */
2596 hcryp->State = HAL_CRYP_STATE_BUSY;
2598 /* Set CRYP peripheral in TDES CBC encryption mode */
2599 CRYP_SetTDESCBCMode(hcryp, 0);
2602 __HAL_CRYP_ENABLE();
2604 /* Write Plain Data and Get Cypher Data */
2605 if(CRYP_ProcessData2Words(hcryp, pPlainData, Size, pCypherData, Timeout) != HAL_OK)
2610 /* Change the CRYP state */
2611 hcryp->State = HAL_CRYP_STATE_READY;
2613 /* Process Unlocked */
2614 __HAL_UNLOCK(hcryp);
2616 /* Return function status */
2621 * @brief Initializes the CRYP peripheral in TDES CBC decryption mode
2622 * then decrypted pCypherData. The cypher data are available in pPlainData
2623 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
2624 * the configuration information for CRYP module
2625 * @param pCypherData: Pointer to the cyphertext buffer
2626 * @param Size: Length of the plaintext buffer, must be a multiple of 8
2627 * @param pPlainData: Pointer to the plaintext buffer
2628 * @param Timeout: Specify Timeout value
2629 * @retval HAL status
2631 HAL_StatusTypeDef HAL_CRYP_TDESCBC_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout)
2633 /* Process Locked */
2636 /* Change the CRYP state */
2637 hcryp->State = HAL_CRYP_STATE_BUSY;
2639 /* Set CRYP peripheral in TDES CBC decryption mode */
2640 CRYP_SetTDESCBCMode(hcryp, CRYP_CR_ALGODIR);
2643 __HAL_CRYP_ENABLE();
2645 /* Write Cypher Data and Get Plain Data */
2646 if(CRYP_ProcessData2Words(hcryp, pCypherData, Size, pPlainData, Timeout) != HAL_OK)
2651 /* Change the CRYP state */
2652 hcryp->State = HAL_CRYP_STATE_READY;
2654 /* Process Unlocked */
2655 __HAL_UNLOCK(hcryp);
2657 /* Return function status */
2662 * @brief Initializes the CRYP peripheral in TDES ECB encryption mode using interrupt.
2663 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
2664 * the configuration information for CRYP module
2665 * @param pPlainData: Pointer to the plaintext buffer
2666 * @param Size: Length of the plaintext buffer, must be a multiple of 8
2667 * @param pCypherData: Pointer to the cyphertext buffer
2668 * @retval HAL status
2670 HAL_StatusTypeDef HAL_CRYP_TDESECB_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
2673 uint32_t outputaddr;
2675 if(hcryp->State == HAL_CRYP_STATE_READY)
2677 /* Process Locked */
2680 hcryp->CrypInCount = Size;
2681 hcryp->pCrypInBuffPtr = pPlainData;
2682 hcryp->pCrypOutBuffPtr = pCypherData;
2683 hcryp->CrypOutCount = Size;
2685 /* Change the CRYP state */
2686 hcryp->State = HAL_CRYP_STATE_BUSY;
2688 /* Set CRYP peripheral in TDES ECB encryption mode */
2689 CRYP_SetTDESECBMode(hcryp, 0);
2691 /* Enable Interrupts */
2692 __HAL_CRYP_ENABLE_IT(CRYP_IT_INI | CRYP_IT_OUTI);
2695 __HAL_CRYP_ENABLE();
2697 /* Return function status */
2700 else if(__HAL_CRYP_GET_IT(CRYP_IT_INI))
2702 inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
2703 /* Write the Input block in the IN FIFO */
2704 CRYP->DR = *(uint32_t*)(inputaddr);
2706 CRYP->DR = *(uint32_t*)(inputaddr);
2708 hcryp->pCrypInBuffPtr += 8;
2709 hcryp->CrypInCount -= 8;
2710 if(hcryp->CrypInCount == 0)
2712 __HAL_CRYP_DISABLE_IT(CRYP_IT_INI);
2713 /* Call the Input data transfer complete callback */
2714 HAL_CRYP_InCpltCallback(hcryp);
2717 else if(__HAL_CRYP_GET_IT(CRYP_IT_OUTI))
2719 outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
2720 /* Read the Output block from the Output FIFO */
2721 *(uint32_t*)(outputaddr) = CRYP->DOUT;
2723 *(uint32_t*)(outputaddr) = CRYP->DOUT;
2725 hcryp->pCrypOutBuffPtr += 8;
2726 hcryp->CrypOutCount -= 8;
2727 if(hcryp->CrypOutCount == 0)
2730 __HAL_CRYP_DISABLE_IT(CRYP_IT_OUTI);
2732 __HAL_CRYP_DISABLE();
2733 /* Process Unlocked */
2734 __HAL_UNLOCK(hcryp);
2735 /* Change the CRYP state */
2736 hcryp->State = HAL_CRYP_STATE_READY;
2737 /* Call the Output data transfer complete callback */
2738 HAL_CRYP_OutCpltCallback(hcryp);
2742 /* Return function status */
2747 * @brief Initializes the CRYP peripheral in TDES CBC encryption mode.
2748 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
2749 * the configuration information for CRYP module
2750 * @param pPlainData: Pointer to the plaintext buffer
2751 * @param Size: Length of the plaintext buffer, must be a multiple of 8
2752 * @param pCypherData: Pointer to the cyphertext buffer
2753 * @retval HAL status
2755 HAL_StatusTypeDef HAL_CRYP_TDESCBC_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
2758 uint32_t outputaddr;
2760 if(hcryp->State == HAL_CRYP_STATE_READY)
2762 /* Process Locked */
2765 hcryp->CrypInCount = Size;
2766 hcryp->pCrypInBuffPtr = pPlainData;
2767 hcryp->pCrypOutBuffPtr = pCypherData;
2768 hcryp->CrypOutCount = Size;
2770 /* Change the CRYP state */
2771 hcryp->State = HAL_CRYP_STATE_BUSY;
2773 /* Set CRYP peripheral in TDES CBC encryption mode */
2774 CRYP_SetTDESCBCMode(hcryp, 0);
2776 /* Enable Interrupts */
2777 __HAL_CRYP_ENABLE_IT(CRYP_IT_INI | CRYP_IT_OUTI);
2780 __HAL_CRYP_ENABLE();
2782 /* Return function status */
2785 else if(__HAL_CRYP_GET_IT(CRYP_IT_INI))
2787 inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
2788 /* Write the Input block in the IN FIFO */
2789 CRYP->DR = *(uint32_t*)(inputaddr);
2791 CRYP->DR = *(uint32_t*)(inputaddr);
2793 hcryp->pCrypInBuffPtr += 8;
2794 hcryp->CrypInCount -= 8;
2795 if(hcryp->CrypInCount == 0)
2797 __HAL_CRYP_DISABLE_IT(CRYP_IT_INI);
2798 /* Call the Input data transfer complete callback */
2799 HAL_CRYP_InCpltCallback(hcryp);
2802 else if(__HAL_CRYP_GET_IT(CRYP_IT_OUTI))
2804 outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
2805 /* Read the Output block from the Output FIFO */
2806 *(uint32_t*)(outputaddr) = CRYP->DOUT;
2808 *(uint32_t*)(outputaddr) = CRYP->DOUT;
2810 hcryp->pCrypOutBuffPtr += 8;
2811 hcryp->CrypOutCount -= 8;
2812 if(hcryp->CrypOutCount == 0)
2814 __HAL_CRYP_DISABLE_IT(CRYP_IT_OUTI);
2816 __HAL_CRYP_DISABLE();
2817 /* Process Unlocked */
2818 __HAL_UNLOCK(hcryp);
2819 /* Change the CRYP state */
2820 hcryp->State = HAL_CRYP_STATE_READY;
2821 /* Call Input transfer complete callback */
2822 HAL_CRYP_OutCpltCallback(hcryp);
2826 /* Return function status */
2831 * @brief Initializes the CRYP peripheral in TDES ECB decryption mode.
2832 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
2833 * the configuration information for CRYP module
2834 * @param pPlainData: Pointer to the plaintext buffer
2835 * @param Size: Length of the plaintext buffer, must be a multiple of 8
2836 * @param pCypherData: Pointer to the cyphertext buffer
2837 * @retval HAL status
2839 HAL_StatusTypeDef HAL_CRYP_TDESECB_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
2842 uint32_t outputaddr;
2844 if(hcryp->State == HAL_CRYP_STATE_READY)
2846 /* Process Locked */
2849 hcryp->CrypInCount = Size;
2850 hcryp->pCrypInBuffPtr = pCypherData;
2851 hcryp->pCrypOutBuffPtr = pPlainData;
2852 hcryp->CrypOutCount = Size;
2854 /* Change the CRYP state */
2855 hcryp->State = HAL_CRYP_STATE_BUSY;
2857 /* Set CRYP peripheral in TDES ECB decryption mode */
2858 CRYP_SetTDESECBMode(hcryp, CRYP_CR_ALGODIR);
2860 /* Enable Interrupts */
2861 __HAL_CRYP_ENABLE_IT(CRYP_IT_INI | CRYP_IT_OUTI);
2864 __HAL_CRYP_ENABLE();
2866 /* Return function status */
2869 else if(__HAL_CRYP_GET_IT(CRYP_IT_INI))
2871 inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
2872 /* Write the Input block in the IN FIFO */
2873 CRYP->DR = *(uint32_t*)(inputaddr);
2875 CRYP->DR = *(uint32_t*)(inputaddr);
2877 hcryp->pCrypInBuffPtr += 8;
2878 hcryp->CrypInCount -= 8;
2879 if(hcryp->CrypInCount == 0)
2881 __HAL_CRYP_DISABLE_IT(CRYP_IT_INI);
2882 /* Call the Input data transfer complete callback */
2883 HAL_CRYP_InCpltCallback(hcryp);
2886 else if(__HAL_CRYP_GET_IT(CRYP_IT_OUTI))
2888 outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
2889 /* Read the Output block from the Output FIFO */
2890 *(uint32_t*)(outputaddr) = CRYP->DOUT;
2892 *(uint32_t*)(outputaddr) = CRYP->DOUT;
2894 hcryp->pCrypOutBuffPtr += 8;
2895 hcryp->CrypOutCount -= 8;
2896 if(hcryp->CrypOutCount == 0)
2898 __HAL_CRYP_DISABLE_IT(CRYP_IT_OUTI);
2900 __HAL_CRYP_DISABLE();
2901 /* Process Unlocked */
2902 __HAL_UNLOCK(hcryp);
2903 /* Change the CRYP state */
2904 hcryp->State = HAL_CRYP_STATE_READY;
2905 /* Call Input transfer complete callback */
2906 HAL_CRYP_OutCpltCallback(hcryp);
2910 /* Return function status */
2915 * @brief Initializes the CRYP peripheral in TDES CBC decryption mode.
2916 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
2917 * the configuration information for CRYP module
2918 * @param pCypherData: Pointer to the cyphertext buffer
2919 * @param Size: Length of the plaintext buffer, must be a multiple of 8
2920 * @param pPlainData: Pointer to the plaintext buffer
2921 * @retval HAL status
2923 HAL_StatusTypeDef HAL_CRYP_TDESCBC_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
2926 uint32_t outputaddr;
2928 if(hcryp->State == HAL_CRYP_STATE_READY)
2930 /* Process Locked */
2933 hcryp->CrypInCount = Size;
2934 hcryp->pCrypInBuffPtr = pCypherData;
2935 hcryp->pCrypOutBuffPtr = pPlainData;
2936 hcryp->CrypOutCount = Size;
2938 /* Change the CRYP state */
2939 hcryp->State = HAL_CRYP_STATE_BUSY;
2941 /* Set CRYP peripheral in TDES CBC decryption mode */
2942 CRYP_SetTDESCBCMode(hcryp, CRYP_CR_ALGODIR);
2944 /* Enable Interrupts */
2945 __HAL_CRYP_ENABLE_IT(CRYP_IT_INI | CRYP_IT_OUTI);
2948 __HAL_CRYP_ENABLE();
2950 /* Return function status */
2953 else if(__HAL_CRYP_GET_IT(CRYP_IT_INI))
2955 inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
2956 /* Write the Input block in the IN FIFO */
2957 CRYP->DR = *(uint32_t*)(inputaddr);
2959 CRYP->DR = *(uint32_t*)(inputaddr);
2961 hcryp->pCrypInBuffPtr += 8;
2962 hcryp->CrypInCount -= 8;
2963 if(hcryp->CrypInCount == 0)
2965 __HAL_CRYP_DISABLE_IT(CRYP_IT_INI);
2966 /* Call the Input data transfer complete callback */
2967 HAL_CRYP_InCpltCallback(hcryp);
2970 else if(__HAL_CRYP_GET_IT(CRYP_IT_OUTI))
2972 outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
2973 /* Read the Output block from the Output FIFO */
2974 *(uint32_t*)(outputaddr) = CRYP->DOUT;
2976 *(uint32_t*)(outputaddr) = CRYP->DOUT;
2978 hcryp->pCrypOutBuffPtr += 8;
2979 hcryp->CrypOutCount -= 8;
2980 if(hcryp->CrypOutCount == 0)
2982 __HAL_CRYP_DISABLE_IT(CRYP_IT_OUTI);
2984 __HAL_CRYP_DISABLE();
2985 /* Process Unlocked */
2986 __HAL_UNLOCK(hcryp);
2987 /* Change the CRYP state */
2988 hcryp->State = HAL_CRYP_STATE_READY;
2989 /* Call Input transfer complete callback */
2990 HAL_CRYP_OutCpltCallback(hcryp);
2994 /* Return function status */
2999 * @brief Initializes the CRYP peripheral in TDES ECB encryption mode using DMA.
3000 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
3001 * the configuration information for CRYP module
3002 * @param pPlainData: Pointer to the plaintext buffer
3003 * @param Size: Length of the plaintext buffer, must be a multiple of 8
3004 * @param pCypherData: Pointer to the cyphertext buffer
3005 * @retval HAL status
3007 HAL_StatusTypeDef HAL_CRYP_TDESECB_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
3010 uint32_t outputaddr;
3012 if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS))
3014 /* Process Locked */
3017 inputaddr = (uint32_t)pPlainData;
3018 outputaddr = (uint32_t)pCypherData;
3020 /* Change the CRYP state */
3021 hcryp->State = HAL_CRYP_STATE_BUSY;
3023 /* Set CRYP peripheral in TDES ECB encryption mode */
3024 CRYP_SetTDESECBMode(hcryp, 0);
3026 /* Set the input and output addresses and start DMA transfer */
3027 CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
3029 /* Process Unlocked */
3030 __HAL_UNLOCK(hcryp);
3032 /* Return function status */
3042 * @brief Initializes the CRYP peripheral in TDES CBC encryption mode using DMA.
3043 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
3044 * the configuration information for CRYP module
3045 * @param pPlainData: Pointer to the plaintext buffer
3046 * @param Size: Length of the plaintext buffer, must be a multiple of 8
3047 * @param pCypherData: Pointer to the cyphertext buffer
3048 * @retval HAL status
3050 HAL_StatusTypeDef HAL_CRYP_TDESCBC_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
3053 uint32_t outputaddr;
3055 if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS))
3057 /* Process Locked */
3060 inputaddr = (uint32_t)pPlainData;
3061 outputaddr = (uint32_t)pCypherData;
3063 /* Change the CRYP state */
3064 hcryp->State = HAL_CRYP_STATE_BUSY;
3066 /* Set CRYP peripheral in TDES CBC encryption mode */
3067 CRYP_SetTDESCBCMode(hcryp, 0);
3069 /* Set the input and output addresses and start DMA transfer */
3070 CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
3072 /* Process Unlocked */
3073 __HAL_UNLOCK(hcryp);
3075 /* Return function status */
3085 * @brief Initializes the CRYP peripheral in TDES ECB decryption mode using DMA.
3086 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
3087 * the configuration information for CRYP module
3088 * @param pPlainData: Pointer to the plaintext buffer
3089 * @param Size: Length of the plaintext buffer, must be a multiple of 8
3090 * @param pCypherData: Pointer to the cyphertext buffer
3091 * @retval HAL status
3093 HAL_StatusTypeDef HAL_CRYP_TDESECB_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
3096 uint32_t outputaddr;
3098 if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS))
3100 /* Process Locked */
3103 inputaddr = (uint32_t)pCypherData;
3104 outputaddr = (uint32_t)pPlainData;
3106 /* Change the CRYP state */
3107 hcryp->State = HAL_CRYP_STATE_BUSY;
3109 /* Set CRYP peripheral in TDES ECB decryption mode */
3110 CRYP_SetTDESECBMode(hcryp, CRYP_CR_ALGODIR);
3112 /* Set the input and output addresses and start DMA transfer */
3113 CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
3115 /* Process Unlocked */
3116 __HAL_UNLOCK(hcryp);
3118 /* Return function status */
3128 * @brief Initializes the CRYP peripheral in TDES CBC decryption mode using DMA.
3129 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
3130 * the configuration information for CRYP module
3131 * @param pCypherData: Pointer to the cyphertext buffer
3132 * @param Size: Length of the plaintext buffer, must be a multiple of 8
3133 * @param pPlainData: Pointer to the plaintext buffer
3134 * @retval HAL status
3136 HAL_StatusTypeDef HAL_CRYP_TDESCBC_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
3139 uint32_t outputaddr;
3141 if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS))
3143 /* Process Locked */
3146 inputaddr = (uint32_t)pCypherData;
3147 outputaddr = (uint32_t)pPlainData;
3149 /* Change the CRYP state */
3150 hcryp->State = HAL_CRYP_STATE_BUSY;
3152 /* Set CRYP peripheral in TDES CBC decryption mode */
3153 CRYP_SetTDESCBCMode(hcryp, CRYP_CR_ALGODIR);
3155 /* Set the input and output addresses and start DMA transfer */
3156 CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
3158 /* Process Unlocked */
3159 __HAL_UNLOCK(hcryp);
3161 /* Return function status */
3174 /** @defgroup CRYP_Group5 DMA callback functions
3175 * @brief DMA callback functions.
3178 ==============================================================================
3179 ##### DMA callback functions #####
3180 ==============================================================================
3181 [..] This section provides DMA callback functions:
3182 (+) DMA Input data transfer complete
3183 (+) DMA Output data transfer complete
3191 * @brief Input FIFO transfer completed callbacks.
3192 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
3193 * the configuration information for CRYP module
3196 __weak void HAL_CRYP_InCpltCallback(CRYP_HandleTypeDef *hcryp)
3198 /* NOTE : This function Should not be modified, when the callback is needed,
3199 the HAL_CRYP_InCpltCallback could be implemented in the user file
3204 * @brief Output FIFO transfer completed callbacks.
3205 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
3206 * the configuration information for CRYP module
3209 __weak void HAL_CRYP_OutCpltCallback(CRYP_HandleTypeDef *hcryp)
3211 /* NOTE : This function Should not be modified, when the callback is needed,
3212 the HAL_CRYP_OutCpltCallback could be implemented in the user file
3217 * @brief CRYP error callbacks.
3218 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
3219 * the configuration information for CRYP module
3222 __weak void HAL_CRYP_ErrorCallback(CRYP_HandleTypeDef *hcryp)
3224 /* NOTE : This function Should not be modified, when the callback is needed,
3225 the HAL_CRYP_ErrorCallback could be implemented in the user file
3233 /** @defgroup CRYP_Group6 CRYP IRQ handler management
3234 * @brief CRYP IRQ handler.
3237 ==============================================================================
3238 ##### CRYP IRQ handler management #####
3239 ==============================================================================
3240 [..] This section provides CRYP IRQ handler function.
3247 * @brief This function handles CRYP interrupt request.
3248 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
3249 * the configuration information for CRYP module
3252 void HAL_CRYP_IRQHandler(CRYP_HandleTypeDef *hcryp)
3254 switch(CRYP->CR & CRYP_CR_ALGOMODE_DIRECTION)
3256 case CRYP_CR_ALGOMODE_TDES_ECB_ENCRYPT:
3257 HAL_CRYP_TDESECB_Encrypt_IT(hcryp, HAL_NULL, 0, HAL_NULL);
3260 case CRYP_CR_ALGOMODE_TDES_ECB_DECRYPT:
3261 HAL_CRYP_TDESECB_Decrypt_IT(hcryp, HAL_NULL, 0, HAL_NULL);
3264 case CRYP_CR_ALGOMODE_TDES_CBC_ENCRYPT:
3265 HAL_CRYP_TDESCBC_Encrypt_IT(hcryp, HAL_NULL, 0, HAL_NULL);
3268 case CRYP_CR_ALGOMODE_TDES_CBC_DECRYPT:
3269 HAL_CRYP_TDESCBC_Decrypt_IT(hcryp, HAL_NULL, 0, HAL_NULL);
3272 case CRYP_CR_ALGOMODE_DES_ECB_ENCRYPT:
3273 HAL_CRYP_DESECB_Encrypt_IT(hcryp, HAL_NULL, 0, HAL_NULL);
3276 case CRYP_CR_ALGOMODE_DES_ECB_DECRYPT:
3277 HAL_CRYP_DESECB_Decrypt_IT(hcryp, HAL_NULL, 0, HAL_NULL);
3280 case CRYP_CR_ALGOMODE_DES_CBC_ENCRYPT:
3281 HAL_CRYP_DESCBC_Encrypt_IT(hcryp, HAL_NULL, 0, HAL_NULL);
3284 case CRYP_CR_ALGOMODE_DES_CBC_DECRYPT:
3285 HAL_CRYP_DESCBC_Decrypt_IT(hcryp, HAL_NULL, 0, HAL_NULL);
3288 case CRYP_CR_ALGOMODE_AES_ECB_ENCRYPT:
3289 HAL_CRYP_AESECB_Encrypt_IT(hcryp, HAL_NULL, 0, HAL_NULL);
3292 case CRYP_CR_ALGOMODE_AES_ECB_DECRYPT:
3293 HAL_CRYP_AESECB_Decrypt_IT(hcryp, HAL_NULL, 0, HAL_NULL);
3296 case CRYP_CR_ALGOMODE_AES_CBC_ENCRYPT:
3297 HAL_CRYP_AESCBC_Encrypt_IT(hcryp, HAL_NULL, 0, HAL_NULL);
3300 case CRYP_CR_ALGOMODE_AES_CBC_DECRYPT:
3301 HAL_CRYP_AESCBC_Decrypt_IT(hcryp, HAL_NULL, 0, HAL_NULL);
3304 case CRYP_CR_ALGOMODE_AES_CTR_ENCRYPT:
3305 HAL_CRYP_AESCTR_Encrypt_IT(hcryp, HAL_NULL, 0, HAL_NULL);
3308 case CRYP_CR_ALGOMODE_AES_CTR_DECRYPT:
3309 HAL_CRYP_AESCTR_Decrypt_IT(hcryp, HAL_NULL, 0, HAL_NULL);
3321 /** @defgroup CRYP_Group7 Peripheral State functions
3322 * @brief Peripheral State functions.
3325 ==============================================================================
3326 ##### Peripheral State functions #####
3327 ==============================================================================
3329 This subsection permits to get in run-time the status of the peripheral.
3336 * @brief Returns the CRYP state.
3337 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
3338 * the configuration information for CRYP module
3341 HAL_CRYP_STATETypeDef HAL_CRYP_GetState(CRYP_HandleTypeDef *hcryp)
3343 return hcryp->State;
3351 * @brief DMA CRYP Input Data process complete callback.
3352 * @param hdma: DMA handle
3355 static void CRYP_DMAInCplt(DMA_HandleTypeDef *hdma)
3357 CRYP_HandleTypeDef* hcryp = (CRYP_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
3359 /* Disable the DMA transfer for input FIFO request by resetting the DIEN bit
3360 in the DMACR register */
3361 CRYP->DMACR &= (uint32_t)(~CRYP_DMACR_DIEN);
3363 /* Call input data transfer complete callback */
3364 HAL_CRYP_InCpltCallback(hcryp);
3368 * @brief DMA CRYP Output Data process complete callback.
3369 * @param hdma: DMA handle
3372 static void CRYP_DMAOutCplt(DMA_HandleTypeDef *hdma)
3374 CRYP_HandleTypeDef* hcryp = (CRYP_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
3376 /* Disable the DMA transfer for output FIFO request by resetting the DOEN bit
3377 in the DMACR register */
3378 CRYP->DMACR &= (uint32_t)(~CRYP_DMACR_DOEN);
3381 __HAL_CRYP_DISABLE();
3383 /* Change the CRYP state to ready */
3384 hcryp->State = HAL_CRYP_STATE_READY;
3386 /* Call output data transfer complete callback */
3387 HAL_CRYP_OutCpltCallback(hcryp);
3391 * @brief DMA CRYP communication error callback.
3392 * @param hdma: DMA handle
3395 static void CRYP_DMAError(DMA_HandleTypeDef *hdma)
3397 CRYP_HandleTypeDef* hcryp = (CRYP_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
3398 hcryp->State= HAL_CRYP_STATE_READY;
3399 HAL_CRYP_ErrorCallback(hcryp);
3403 * @brief Writes the Key in Key registers.
3404 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
3405 * the configuration information for CRYP module
3406 * @param Key: Pointer to Key buffer
3407 * @param KeySize: Size of Key
3410 static void CRYP_SetKey(CRYP_HandleTypeDef *hcryp, uint8_t *Key, uint32_t KeySize)
3412 uint32_t keyaddr = (uint32_t)Key;
3416 case CRYP_KEYSIZE_256B:
3417 /* Key Initialisation */
3418 CRYP->K0LR = __REV(*(uint32_t*)(keyaddr));
3420 CRYP->K0RR = __REV(*(uint32_t*)(keyaddr));
3422 CRYP->K1LR = __REV(*(uint32_t*)(keyaddr));
3424 CRYP->K1RR = __REV(*(uint32_t*)(keyaddr));
3426 CRYP->K2LR = __REV(*(uint32_t*)(keyaddr));
3428 CRYP->K2RR = __REV(*(uint32_t*)(keyaddr));
3430 CRYP->K3LR = __REV(*(uint32_t*)(keyaddr));
3432 CRYP->K3RR = __REV(*(uint32_t*)(keyaddr));
3434 case CRYP_KEYSIZE_192B:
3435 CRYP->K1LR = __REV(*(uint32_t*)(keyaddr));
3437 CRYP->K1RR = __REV(*(uint32_t*)(keyaddr));
3439 CRYP->K2LR = __REV(*(uint32_t*)(keyaddr));
3441 CRYP->K2RR = __REV(*(uint32_t*)(keyaddr));
3443 CRYP->K3LR = __REV(*(uint32_t*)(keyaddr));
3445 CRYP->K3RR = __REV(*(uint32_t*)(keyaddr));
3447 case CRYP_KEYSIZE_128B:
3448 CRYP->K2LR = __REV(*(uint32_t*)(keyaddr));
3450 CRYP->K2RR = __REV(*(uint32_t*)(keyaddr));
3452 CRYP->K3LR = __REV(*(uint32_t*)(keyaddr));
3454 CRYP->K3RR = __REV(*(uint32_t*)(keyaddr));
3462 * @brief Writes the InitVector/InitCounter in IV registers.
3463 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
3464 * the configuration information for CRYP module
3465 * @param InitVector: Pointer to InitVector/InitCounter buffer
3466 * @param IVSize: Size of the InitVector/InitCounter
3469 static void CRYP_SetInitVector(CRYP_HandleTypeDef *hcryp, uint8_t *InitVector, uint32_t IVSize)
3471 uint32_t ivaddr = (uint32_t)InitVector;
3475 case CRYP_KEYSIZE_128B:
3476 CRYP->IV0LR = __REV(*(uint32_t*)(ivaddr));
3478 CRYP->IV0RR = __REV(*(uint32_t*)(ivaddr));
3480 CRYP->IV1LR = __REV(*(uint32_t*)(ivaddr));
3482 CRYP->IV1RR = __REV(*(uint32_t*)(ivaddr));
3484 /* Whatever key size 192 or 256, Init vector is written in IV0LR and IV0RR */
3485 case CRYP_KEYSIZE_192B:
3486 CRYP->IV0LR = __REV(*(uint32_t*)(ivaddr));
3488 CRYP->IV0RR = __REV(*(uint32_t*)(ivaddr));
3490 case CRYP_KEYSIZE_256B:
3491 CRYP->IV0LR = __REV(*(uint32_t*)(ivaddr));
3493 CRYP->IV0RR = __REV(*(uint32_t*)(ivaddr));
3501 * @brief Process Data: Writes Input data in polling mode and read the output data
3502 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
3503 * the configuration information for CRYP module
3504 * @param Input: Pointer to the Input buffer
3505 * @param Ilength: Length of the Input buffer, must be a multiple of 16.
3506 * @param Output: Pointer to the returned buffer
3509 static HAL_StatusTypeDef CRYP_ProcessData(CRYP_HandleTypeDef *hcryp, uint8_t* Input, uint16_t Ilength, uint8_t* Output, uint32_t Timeout)
3511 uint32_t tickstart = 0;
3514 uint32_t inputaddr = (uint32_t)Input;
3515 uint32_t outputaddr = (uint32_t)Output;
3517 for(i=0; (i < Ilength); i+=16)
3519 /* Write the Input block in the IN FIFO */
3520 CRYP->DR = *(uint32_t*)(inputaddr);
3522 CRYP->DR = *(uint32_t*)(inputaddr);
3524 CRYP->DR = *(uint32_t*)(inputaddr);
3526 CRYP->DR = *(uint32_t*)(inputaddr);
3530 tickstart = HAL_GetTick();
3532 while(HAL_IS_BIT_CLR(CRYP->SR, CRYP_FLAG_OFNE))
3534 /* Check for the Timeout */
3535 if(Timeout != HAL_MAX_DELAY)
3537 if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
3540 hcryp->State = HAL_CRYP_STATE_TIMEOUT;
3542 /* Process Unlocked */
3543 __HAL_UNLOCK(hcryp);
3549 /* Read the Output block from the Output FIFO */
3550 *(uint32_t*)(outputaddr) = CRYP->DOUT;
3552 *(uint32_t*)(outputaddr) = CRYP->DOUT;
3554 *(uint32_t*)(outputaddr) = CRYP->DOUT;
3556 *(uint32_t*)(outputaddr) = CRYP->DOUT;
3559 /* Return function status */
3564 * @brief Process Data: Write Input data in polling mode.
3565 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
3566 * the configuration information for CRYP module
3567 * @param Input: Pointer to the Input buffer
3568 * @param Ilength: Length of the Input buffer, must be a multiple of 8
3569 * @param Output: Pointer to the returned buffer
3570 * @param Timeout: Specify Timeout value
3573 static HAL_StatusTypeDef CRYP_ProcessData2Words(CRYP_HandleTypeDef *hcryp, uint8_t* Input, uint16_t Ilength, uint8_t* Output, uint32_t Timeout)
3575 uint32_t tickstart = 0;
3578 uint32_t inputaddr = (uint32_t)Input;
3579 uint32_t outputaddr = (uint32_t)Output;
3581 for(i=0; (i < Ilength); i+=8)
3583 /* Write the Input block in the IN FIFO */
3584 CRYP->DR = *(uint32_t*)(inputaddr);
3586 CRYP->DR = *(uint32_t*)(inputaddr);
3590 tickstart = HAL_GetTick();
3592 while(HAL_IS_BIT_CLR(CRYP->SR, CRYP_FLAG_OFNE))
3594 /* Check for the Timeout */
3595 if(Timeout != HAL_MAX_DELAY)
3597 if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
3600 hcryp->State = HAL_CRYP_STATE_TIMEOUT;
3602 /* Process Unlocked */
3603 __HAL_UNLOCK(hcryp);
3609 /* Read the Output block from the Output FIFO */
3610 *(uint32_t*)(outputaddr) = CRYP->DOUT;
3612 *(uint32_t*)(outputaddr) = CRYP->DOUT;
3615 /* Return function status */
3620 * @brief Set the DMA configuration and start the DMA transfer
3621 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
3622 * the configuration information for CRYP module
3623 * @param inputaddr: address of the Input buffer
3624 * @param Size: Size of the Input buffer, must be a multiple of 16.
3625 * @param outputaddr: address of the Output buffer
3628 static void CRYP_SetDMAConfig(CRYP_HandleTypeDef *hcryp, uint32_t inputaddr, uint16_t Size, uint32_t outputaddr)
3630 /* Set the CRYP DMA transfer complete callback */
3631 hcryp->hdmain->XferCpltCallback = CRYP_DMAInCplt;
3632 /* Set the DMA error callback */
3633 hcryp->hdmain->XferErrorCallback = CRYP_DMAError;
3635 /* Set the CRYP DMA transfer complete callback */
3636 hcryp->hdmaout->XferCpltCallback = CRYP_DMAOutCplt;
3637 /* Set the DMA error callback */
3638 hcryp->hdmaout->XferErrorCallback = CRYP_DMAError;
3641 __HAL_CRYP_ENABLE();
3643 /* Enable the DMA In DMA Stream */
3644 HAL_DMA_Start_IT(hcryp->hdmain, inputaddr, (uint32_t)&CRYP->DR, Size/4);
3646 /* Enable In DMA request */
3647 CRYP->DMACR = (CRYP_DMACR_DIEN);
3649 /* Enable the DMA Out DMA Stream */
3650 HAL_DMA_Start_IT(hcryp->hdmaout, (uint32_t)&CRYP->DOUT, outputaddr, Size/4);
3652 /* Enable Out DMA request */
3653 CRYP->DMACR |= CRYP_DMACR_DOEN;
3658 * @brief Sets the CRYP peripheral in DES ECB mode.
3659 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
3660 * the configuration information for CRYP module
3661 * @param Direction: Encryption or decryption
3664 static void CRYP_SetDESECBMode(CRYP_HandleTypeDef *hcryp, uint32_t Direction)
3666 /* Check if initialization phase has already been performed */
3667 if(hcryp->Phase == HAL_CRYP_PHASE_READY)
3669 /* Set the CRYP peripheral in AES ECB mode */
3670 __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_DES_ECB | Direction);
3673 CRYP->K1LR = __REV(*(uint32_t*)(hcryp->Init.pKey));
3674 CRYP->K1RR = __REV(*(uint32_t*)(hcryp->Init.pKey+4));
3677 __HAL_CRYP_FIFO_FLUSH();
3680 hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
3685 * @brief Sets the CRYP peripheral in DES CBC mode.
3686 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
3687 * the configuration information for CRYP module
3688 * @param Direction: Encryption or decryption
3691 static void CRYP_SetDESCBCMode(CRYP_HandleTypeDef *hcryp, uint32_t Direction)
3693 /* Check if initialization phase has already been performed */
3694 if(hcryp->Phase == HAL_CRYP_PHASE_READY)
3696 /* Set the CRYP peripheral in AES ECB mode */
3697 __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_DES_CBC | Direction);
3700 CRYP->K1LR = __REV(*(uint32_t*)(hcryp->Init.pKey));
3701 CRYP->K1RR = __REV(*(uint32_t*)(hcryp->Init.pKey+4));
3703 /* Set the Initialization Vector */
3704 CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_256B);
3707 __HAL_CRYP_FIFO_FLUSH();
3710 hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
3715 * @brief Sets the CRYP peripheral in TDES ECB mode.
3716 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
3717 * the configuration information for CRYP module
3718 * @param Direction: Encryption or decryption
3721 static void CRYP_SetTDESECBMode(CRYP_HandleTypeDef *hcryp, uint32_t Direction)
3723 /* Check if initialization phase has already been performed */
3724 if(hcryp->Phase == HAL_CRYP_PHASE_READY)
3726 /* Set the CRYP peripheral in AES ECB mode */
3727 __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_TDES_ECB | Direction);
3730 CRYP_SetKey(hcryp, hcryp->Init.pKey, CRYP_KEYSIZE_192B);
3733 __HAL_CRYP_FIFO_FLUSH();
3736 hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
3741 * @brief Sets the CRYP peripheral in TDES CBC mode
3742 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
3743 * the configuration information for CRYP module
3744 * @param Direction: Encryption or decryption
3747 static void CRYP_SetTDESCBCMode(CRYP_HandleTypeDef *hcryp, uint32_t Direction)
3749 /* Check if initialization phase has already been performed */
3750 if(hcryp->Phase == HAL_CRYP_PHASE_READY)
3752 /* Set the CRYP peripheral in AES CBC mode */
3753 __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_TDES_CBC | Direction);
3756 CRYP_SetKey(hcryp, hcryp->Init.pKey, CRYP_KEYSIZE_192B);
3758 /* Set the Initialization Vector */
3759 CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_256B);
3762 __HAL_CRYP_FIFO_FLUSH();
3765 hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
3773 #endif /* STM32F415xx || STM32F417xx || STM32F437xx || STM32F439xx */
3775 #endif /* HAL_CRYP_MODULE_ENABLED */
3784 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/