STM32F439xx HAL User Manual
|
00001 /** 00002 ****************************************************************************** 00003 * @file stm32f4xx_hal_cryp.c 00004 * @author MCD Application Team 00005 * @brief CRYP HAL module driver. 00006 * This file provides firmware functions to manage the following 00007 * functionalities of the Cryptography (CRYP) peripheral: 00008 * + Initialization and de-initialization functions 00009 * + AES processing functions 00010 * + DES processing functions 00011 * + TDES processing functions 00012 * + DMA callback functions 00013 * + CRYP IRQ handler management 00014 * + Peripheral State functions 00015 * 00016 @verbatim 00017 ============================================================================== 00018 ##### How to use this driver ##### 00019 ============================================================================== 00020 [..] 00021 The CRYP HAL driver can be used as follows: 00022 00023 (#)Initialize the CRYP low level resources by implementing the HAL_CRYP_MspInit(): 00024 (##) Enable the CRYP interface clock using __HAL_RCC_CRYP_CLK_ENABLE() 00025 (##) In case of using interrupts (e.g. HAL_CRYP_AESECB_Encrypt_IT()) 00026 (+++) Configure the CRYP interrupt priority using HAL_NVIC_SetPriority() 00027 (+++) Enable the CRYP IRQ handler using HAL_NVIC_EnableIRQ() 00028 (+++) In CRYP IRQ handler, call HAL_CRYP_IRQHandler() 00029 (##) In case of using DMA to control data transfer (e.g. HAL_CRYP_AESECB_Encrypt_DMA()) 00030 (+++) Enable the DMAx interface clock using __DMAx_CLK_ENABLE() 00031 (+++) Configure and enable two DMA streams one for managing data transfer from 00032 memory to peripheral (input stream) and another stream for managing data 00033 transfer from peripheral to memory (output stream) 00034 (+++) Associate the initialized DMA handle to the CRYP DMA handle 00035 using __HAL_LINKDMA() 00036 (+++) Configure the priority and enable the NVIC for the transfer complete 00037 interrupt on the two DMA Streams. The output stream should have higher 00038 priority than the input stream HAL_NVIC_SetPriority() and HAL_NVIC_EnableIRQ() 00039 00040 (#)Initialize the CRYP HAL using HAL_CRYP_Init(). This function configures mainly: 00041 (##) The data type: 1-bit, 8-bit, 16-bit and 32-bit 00042 (##) The key size: 128, 192 and 256. This parameter is relevant only for AES 00043 (##) The encryption/decryption key. It's size depends on the algorithm 00044 used for encryption/decryption 00045 (##) The initialization vector (counter). It is not used ECB mode. 00046 00047 (#)Three processing (encryption/decryption) functions are available: 00048 (##) Polling mode: encryption and decryption APIs are blocking functions 00049 i.e. they process the data and wait till the processing is finished, 00050 e.g. HAL_CRYP_AESCBC_Encrypt() 00051 (##) Interrupt mode: encryption and decryption APIs are not blocking functions 00052 i.e. they process the data under interrupt, 00053 e.g. HAL_CRYP_AESCBC_Encrypt_IT() 00054 (##) DMA mode: encryption and decryption APIs are not blocking functions 00055 i.e. the data transfer is ensured by DMA, 00056 e.g. HAL_CRYP_AESCBC_Encrypt_DMA() 00057 00058 (#)When the processing function is called at first time after HAL_CRYP_Init() 00059 the CRYP peripheral is initialized and processes the buffer in input. 00060 At second call, the processing function performs an append of the already 00061 processed buffer. 00062 When a new data block is to be processed, call HAL_CRYP_Init() then the 00063 processing function. 00064 00065 (#)Call HAL_CRYP_DeInit() to deinitialize the CRYP peripheral. 00066 00067 @endverbatim 00068 ****************************************************************************** 00069 * @attention 00070 * 00071 * <h2><center>© COPYRIGHT(c) 2017 STMicroelectronics</center></h2> 00072 * 00073 * Redistribution and use in source and binary forms, with or without modification, 00074 * are permitted provided that the following conditions are met: 00075 * 1. Redistributions of source code must retain the above copyright notice, 00076 * this list of conditions and the following disclaimer. 00077 * 2. Redistributions in binary form must reproduce the above copyright notice, 00078 * this list of conditions and the following disclaimer in the documentation 00079 * and/or other materials provided with the distribution. 00080 * 3. Neither the name of STMicroelectronics nor the names of its contributors 00081 * may be used to endorse or promote products derived from this software 00082 * without specific prior written permission. 00083 * 00084 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00085 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00086 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00087 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 00088 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00089 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 00090 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00091 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 00092 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00093 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00094 * 00095 ****************************************************************************** 00096 */ 00097 00098 /* Includes ------------------------------------------------------------------*/ 00099 #include "stm32f4xx_hal.h" 00100 00101 /** @addtogroup STM32F4xx_HAL_Driver 00102 * @{ 00103 */ 00104 00105 #ifdef HAL_CRYP_MODULE_ENABLED 00106 00107 #if defined(CRYP) 00108 00109 /** @defgroup CRYP CRYP 00110 * @brief CRYP HAL module driver. 00111 * @{ 00112 */ 00113 00114 /* Private typedef -----------------------------------------------------------*/ 00115 /* Private define ------------------------------------------------------------*/ 00116 /** @addtogroup CRYP_Private_define 00117 * @{ 00118 */ 00119 #define CRYP_TIMEOUT_VALUE 1U 00120 /** 00121 * @} 00122 */ 00123 00124 /* Private macro -------------------------------------------------------------*/ 00125 /* Private variables ---------------------------------------------------------*/ 00126 /* Private function prototypes -----------------------------------------------*/ 00127 /** @addtogroup CRYP_Private_Functions_prototypes 00128 * @{ 00129 */ 00130 static void CRYP_SetInitVector(CRYP_HandleTypeDef *hcryp, uint8_t *InitVector, uint32_t IVSize); 00131 static void CRYP_SetKey(CRYP_HandleTypeDef *hcryp, uint8_t *Key, uint32_t KeySize); 00132 static HAL_StatusTypeDef CRYP_ProcessData(CRYP_HandleTypeDef *hcryp, uint8_t* Input, uint16_t Ilength, uint8_t* Output, uint32_t Timeout); 00133 static HAL_StatusTypeDef CRYP_ProcessData2Words(CRYP_HandleTypeDef *hcryp, uint8_t* Input, uint16_t Ilength, uint8_t* Output, uint32_t Timeout); 00134 static void CRYP_DMAInCplt(DMA_HandleTypeDef *hdma); 00135 static void CRYP_DMAOutCplt(DMA_HandleTypeDef *hdma); 00136 static void CRYP_DMAError(DMA_HandleTypeDef *hdma); 00137 static void CRYP_SetDMAConfig(CRYP_HandleTypeDef *hcryp, uint32_t inputaddr, uint16_t Size, uint32_t outputaddr); 00138 static void CRYP_SetTDESECBMode(CRYP_HandleTypeDef *hcryp, uint32_t Direction); 00139 static void CRYP_SetTDESCBCMode(CRYP_HandleTypeDef *hcryp, uint32_t Direction); 00140 static void CRYP_SetDESECBMode(CRYP_HandleTypeDef *hcryp, uint32_t Direction); 00141 static void CRYP_SetDESCBCMode(CRYP_HandleTypeDef *hcryp, uint32_t Direction); 00142 /** 00143 * @} 00144 */ 00145 00146 00147 /* Private functions ---------------------------------------------------------*/ 00148 00149 /** @addtogroup CRYP_Private_Functions 00150 * @{ 00151 */ 00152 00153 00154 /** 00155 * @brief DMA CRYP Input Data process complete callback. 00156 * @param hdma DMA handle 00157 * @retval None 00158 */ 00159 static void CRYP_DMAInCplt(DMA_HandleTypeDef *hdma) 00160 { 00161 CRYP_HandleTypeDef* hcryp = (CRYP_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent; 00162 00163 /* Disable the DMA transfer for input FIFO request by resetting the DIEN bit 00164 in the DMACR register */ 00165 hcryp->Instance->DMACR &= (uint32_t)(~CRYP_DMACR_DIEN); 00166 00167 /* Call input data transfer complete callback */ 00168 HAL_CRYP_InCpltCallback(hcryp); 00169 } 00170 00171 /** 00172 * @brief DMA CRYP Output Data process complete callback. 00173 * @param hdma DMA handle 00174 * @retval None 00175 */ 00176 static void CRYP_DMAOutCplt(DMA_HandleTypeDef *hdma) 00177 { 00178 CRYP_HandleTypeDef* hcryp = (CRYP_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent; 00179 00180 /* Disable the DMA transfer for output FIFO request by resetting the DOEN bit 00181 in the DMACR register */ 00182 hcryp->Instance->DMACR &= (uint32_t)(~CRYP_DMACR_DOEN); 00183 00184 /* Disable CRYP */ 00185 __HAL_CRYP_DISABLE(hcryp); 00186 00187 /* Change the CRYP state to ready */ 00188 hcryp->State = HAL_CRYP_STATE_READY; 00189 00190 /* Call output data transfer complete callback */ 00191 HAL_CRYP_OutCpltCallback(hcryp); 00192 } 00193 00194 /** 00195 * @brief DMA CRYP communication error callback. 00196 * @param hdma DMA handle 00197 * @retval None 00198 */ 00199 static void CRYP_DMAError(DMA_HandleTypeDef *hdma) 00200 { 00201 CRYP_HandleTypeDef* hcryp = (CRYP_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent; 00202 hcryp->State= HAL_CRYP_STATE_READY; 00203 HAL_CRYP_ErrorCallback(hcryp); 00204 } 00205 00206 /** 00207 * @brief Writes the Key in Key registers. 00208 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 00209 * the configuration information for CRYP module 00210 * @param Key Pointer to Key buffer 00211 * @param KeySize Size of Key 00212 * @retval None 00213 */ 00214 static void CRYP_SetKey(CRYP_HandleTypeDef *hcryp, uint8_t *Key, uint32_t KeySize) 00215 { 00216 uint32_t keyaddr = (uint32_t)Key; 00217 00218 switch(KeySize) 00219 { 00220 case CRYP_KEYSIZE_256B: 00221 /* Key Initialisation */ 00222 hcryp->Instance->K0LR = __REV(*(uint32_t*)(keyaddr)); 00223 keyaddr+=4U; 00224 hcryp->Instance->K0RR = __REV(*(uint32_t*)(keyaddr)); 00225 keyaddr+=4U; 00226 hcryp->Instance->K1LR = __REV(*(uint32_t*)(keyaddr)); 00227 keyaddr+=4U; 00228 hcryp->Instance->K1RR = __REV(*(uint32_t*)(keyaddr)); 00229 keyaddr+=4U; 00230 hcryp->Instance->K2LR = __REV(*(uint32_t*)(keyaddr)); 00231 keyaddr+=4U; 00232 hcryp->Instance->K2RR = __REV(*(uint32_t*)(keyaddr)); 00233 keyaddr+=4U; 00234 hcryp->Instance->K3LR = __REV(*(uint32_t*)(keyaddr)); 00235 keyaddr+=4U; 00236 hcryp->Instance->K3RR = __REV(*(uint32_t*)(keyaddr)); 00237 break; 00238 case CRYP_KEYSIZE_192B: 00239 hcryp->Instance->K1LR = __REV(*(uint32_t*)(keyaddr)); 00240 keyaddr+=4U; 00241 hcryp->Instance->K1RR = __REV(*(uint32_t*)(keyaddr)); 00242 keyaddr+=4U; 00243 hcryp->Instance->K2LR = __REV(*(uint32_t*)(keyaddr)); 00244 keyaddr+=4U; 00245 hcryp->Instance->K2RR = __REV(*(uint32_t*)(keyaddr)); 00246 keyaddr+=4U; 00247 hcryp->Instance->K3LR = __REV(*(uint32_t*)(keyaddr)); 00248 keyaddr+=4U; 00249 hcryp->Instance->K3RR = __REV(*(uint32_t*)(keyaddr)); 00250 break; 00251 case CRYP_KEYSIZE_128B: 00252 hcryp->Instance->K2LR = __REV(*(uint32_t*)(keyaddr)); 00253 keyaddr+=4U; 00254 hcryp->Instance->K2RR = __REV(*(uint32_t*)(keyaddr)); 00255 keyaddr+=4U; 00256 hcryp->Instance->K3LR = __REV(*(uint32_t*)(keyaddr)); 00257 keyaddr+=4U; 00258 hcryp->Instance->K3RR = __REV(*(uint32_t*)(keyaddr)); 00259 break; 00260 default: 00261 break; 00262 } 00263 } 00264 00265 /** 00266 * @brief Writes the InitVector/InitCounter in IV registers. 00267 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 00268 * the configuration information for CRYP module 00269 * @param InitVector Pointer to InitVector/InitCounter buffer 00270 * @param IVSize Size of the InitVector/InitCounter 00271 * @retval None 00272 */ 00273 static void CRYP_SetInitVector(CRYP_HandleTypeDef *hcryp, uint8_t *InitVector, uint32_t IVSize) 00274 { 00275 uint32_t ivaddr = (uint32_t)InitVector; 00276 00277 switch(IVSize) 00278 { 00279 case CRYP_KEYSIZE_128B: 00280 hcryp->Instance->IV0LR = __REV(*(uint32_t*)(ivaddr)); 00281 ivaddr+=4U; 00282 hcryp->Instance->IV0RR = __REV(*(uint32_t*)(ivaddr)); 00283 ivaddr+=4U; 00284 hcryp->Instance->IV1LR = __REV(*(uint32_t*)(ivaddr)); 00285 ivaddr+=4U; 00286 hcryp->Instance->IV1RR = __REV(*(uint32_t*)(ivaddr)); 00287 break; 00288 /* Whatever key size 192 or 256, Init vector is written in IV0LR and IV0RR */ 00289 case CRYP_KEYSIZE_192B: 00290 hcryp->Instance->IV0LR = __REV(*(uint32_t*)(ivaddr)); 00291 ivaddr+=4U; 00292 hcryp->Instance->IV0RR = __REV(*(uint32_t*)(ivaddr)); 00293 break; 00294 case CRYP_KEYSIZE_256B: 00295 hcryp->Instance->IV0LR = __REV(*(uint32_t*)(ivaddr)); 00296 ivaddr+=4U; 00297 hcryp->Instance->IV0RR = __REV(*(uint32_t*)(ivaddr)); 00298 break; 00299 default: 00300 break; 00301 } 00302 } 00303 00304 /** 00305 * @brief Process Data: Writes Input data in polling mode and read the output data 00306 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 00307 * the configuration information for CRYP module 00308 * @param Input Pointer to the Input buffer 00309 * @param Ilength Length of the Input buffer, must be a multiple of 16. 00310 * @param Output Pointer to the returned buffer 00311 * @param Timeout Timeout value 00312 * @retval None 00313 */ 00314 static HAL_StatusTypeDef CRYP_ProcessData(CRYP_HandleTypeDef *hcryp, uint8_t* Input, uint16_t Ilength, uint8_t* Output, uint32_t Timeout) 00315 { 00316 uint32_t tickstart = 0U; 00317 00318 uint32_t i = 0U; 00319 uint32_t inputaddr = (uint32_t)Input; 00320 uint32_t outputaddr = (uint32_t)Output; 00321 00322 for(i=0U; (i < Ilength); i+=16U) 00323 { 00324 /* Write the Input block in the IN FIFO */ 00325 hcryp->Instance->DR = *(uint32_t*)(inputaddr); 00326 inputaddr+=4U; 00327 hcryp->Instance->DR = *(uint32_t*)(inputaddr); 00328 inputaddr+=4U; 00329 hcryp->Instance->DR = *(uint32_t*)(inputaddr); 00330 inputaddr+=4U; 00331 hcryp->Instance->DR = *(uint32_t*)(inputaddr); 00332 inputaddr+=4U; 00333 00334 /* Get tick */ 00335 tickstart = HAL_GetTick(); 00336 00337 while(HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_OFNE)) 00338 { 00339 /* Check for the Timeout */ 00340 if(Timeout != HAL_MAX_DELAY) 00341 { 00342 if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout)) 00343 { 00344 /* Change state */ 00345 hcryp->State = HAL_CRYP_STATE_TIMEOUT; 00346 00347 /* Process Unlocked */ 00348 __HAL_UNLOCK(hcryp); 00349 00350 return HAL_TIMEOUT; 00351 } 00352 } 00353 } 00354 /* Read the Output block from the Output FIFO */ 00355 *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; 00356 outputaddr+=4U; 00357 *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; 00358 outputaddr+=4U; 00359 *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; 00360 outputaddr+=4U; 00361 *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; 00362 outputaddr+=4U; 00363 } 00364 /* Return function status */ 00365 return HAL_OK; 00366 } 00367 00368 /** 00369 * @brief Process Data: Write Input data in polling mode. 00370 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 00371 * the configuration information for CRYP module 00372 * @param Input Pointer to the Input buffer 00373 * @param Ilength Length of the Input buffer, must be a multiple of 8 00374 * @param Output Pointer to the returned buffer 00375 * @param Timeout Specify Timeout value 00376 * @retval None 00377 */ 00378 static HAL_StatusTypeDef CRYP_ProcessData2Words(CRYP_HandleTypeDef *hcryp, uint8_t* Input, uint16_t Ilength, uint8_t* Output, uint32_t Timeout) 00379 { 00380 uint32_t tickstart = 0U; 00381 00382 uint32_t i = 0U; 00383 uint32_t inputaddr = (uint32_t)Input; 00384 uint32_t outputaddr = (uint32_t)Output; 00385 00386 for(i=0U; (i < Ilength); i+=8U) 00387 { 00388 /* Write the Input block in the IN FIFO */ 00389 hcryp->Instance->DR = *(uint32_t*)(inputaddr); 00390 inputaddr+=4U; 00391 hcryp->Instance->DR = *(uint32_t*)(inputaddr); 00392 inputaddr+=4U; 00393 00394 /* Get tick */ 00395 tickstart = HAL_GetTick(); 00396 00397 while(HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_OFNE)) 00398 { 00399 /* Check for the Timeout */ 00400 if(Timeout != HAL_MAX_DELAY) 00401 { 00402 if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout)) 00403 { 00404 /* Change state */ 00405 hcryp->State = HAL_CRYP_STATE_TIMEOUT; 00406 00407 /* Process Unlocked */ 00408 __HAL_UNLOCK(hcryp); 00409 00410 return HAL_TIMEOUT; 00411 } 00412 } 00413 } 00414 /* Read the Output block from the Output FIFO */ 00415 *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; 00416 outputaddr+=4U; 00417 *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; 00418 outputaddr+=4U; 00419 } 00420 /* Return function status */ 00421 return HAL_OK; 00422 } 00423 00424 /** 00425 * @brief Set the DMA configuration and start the DMA transfer 00426 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 00427 * the configuration information for CRYP module 00428 * @param inputaddr address of the Input buffer 00429 * @param Size Size of the Input buffer, must be a multiple of 16. 00430 * @param outputaddr address of the Output buffer 00431 * @retval None 00432 */ 00433 static void CRYP_SetDMAConfig(CRYP_HandleTypeDef *hcryp, uint32_t inputaddr, uint16_t Size, uint32_t outputaddr) 00434 { 00435 /* Set the CRYP DMA transfer complete callback */ 00436 hcryp->hdmain->XferCpltCallback = CRYP_DMAInCplt; 00437 /* Set the DMA error callback */ 00438 hcryp->hdmain->XferErrorCallback = CRYP_DMAError; 00439 00440 /* Set the CRYP DMA transfer complete callback */ 00441 hcryp->hdmaout->XferCpltCallback = CRYP_DMAOutCplt; 00442 /* Set the DMA error callback */ 00443 hcryp->hdmaout->XferErrorCallback = CRYP_DMAError; 00444 00445 /* Enable CRYP */ 00446 __HAL_CRYP_ENABLE(hcryp); 00447 00448 /* Enable the DMA In DMA Stream */ 00449 HAL_DMA_Start_IT(hcryp->hdmain, inputaddr, (uint32_t)&hcryp->Instance->DR, Size/4U); 00450 00451 /* Enable In DMA request */ 00452 hcryp->Instance->DMACR = (CRYP_DMACR_DIEN); 00453 00454 /* Enable the DMA Out DMA Stream */ 00455 HAL_DMA_Start_IT(hcryp->hdmaout, (uint32_t)&hcryp->Instance->DOUT, outputaddr, Size/4U); 00456 00457 /* Enable Out DMA request */ 00458 hcryp->Instance->DMACR |= CRYP_DMACR_DOEN; 00459 00460 } 00461 00462 /** 00463 * @brief Sets the CRYP peripheral in DES ECB mode. 00464 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 00465 * the configuration information for CRYP module 00466 * @param Direction Encryption or decryption 00467 * @retval None 00468 */ 00469 static void CRYP_SetDESECBMode(CRYP_HandleTypeDef *hcryp, uint32_t Direction) 00470 { 00471 /* Check if initialization phase has already been performed */ 00472 if(hcryp->Phase == HAL_CRYP_PHASE_READY) 00473 { 00474 /* Set the CRYP peripheral in AES ECB mode */ 00475 __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_DES_ECB | Direction); 00476 00477 /* Set the key */ 00478 hcryp->Instance->K1LR = __REV(*(uint32_t*)(hcryp->Init.pKey)); 00479 hcryp->Instance->K1RR = __REV(*(uint32_t*)(hcryp->Init.pKey+4U)); 00480 00481 /* Flush FIFO */ 00482 __HAL_CRYP_FIFO_FLUSH(hcryp); 00483 00484 /* Set the phase */ 00485 hcryp->Phase = HAL_CRYP_PHASE_PROCESS; 00486 } 00487 } 00488 00489 /** 00490 * @brief Sets the CRYP peripheral in DES CBC mode. 00491 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 00492 * the configuration information for CRYP module 00493 * @param Direction Encryption or decryption 00494 * @retval None 00495 */ 00496 static void CRYP_SetDESCBCMode(CRYP_HandleTypeDef *hcryp, uint32_t Direction) 00497 { 00498 /* Check if initialization phase has already been performed */ 00499 if(hcryp->Phase == HAL_CRYP_PHASE_READY) 00500 { 00501 /* Set the CRYP peripheral in AES ECB mode */ 00502 __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_DES_CBC | Direction); 00503 00504 /* Set the key */ 00505 hcryp->Instance->K1LR = __REV(*(uint32_t*)(hcryp->Init.pKey)); 00506 hcryp->Instance->K1RR = __REV(*(uint32_t*)(hcryp->Init.pKey+4U)); 00507 00508 /* Set the Initialization Vector */ 00509 CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_256B); 00510 00511 /* Flush FIFO */ 00512 __HAL_CRYP_FIFO_FLUSH(hcryp); 00513 00514 /* Set the phase */ 00515 hcryp->Phase = HAL_CRYP_PHASE_PROCESS; 00516 } 00517 } 00518 00519 /** 00520 * @brief Sets the CRYP peripheral in TDES ECB mode. 00521 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 00522 * the configuration information for CRYP module 00523 * @param Direction Encryption or decryption 00524 * @retval None 00525 */ 00526 static void CRYP_SetTDESECBMode(CRYP_HandleTypeDef *hcryp, uint32_t Direction) 00527 { 00528 /* Check if initialization phase has already been performed */ 00529 if(hcryp->Phase == HAL_CRYP_PHASE_READY) 00530 { 00531 /* Set the CRYP peripheral in AES ECB mode */ 00532 __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_TDES_ECB | Direction); 00533 00534 /* Set the key */ 00535 CRYP_SetKey(hcryp, hcryp->Init.pKey, CRYP_KEYSIZE_192B); 00536 00537 /* Flush FIFO */ 00538 __HAL_CRYP_FIFO_FLUSH(hcryp); 00539 00540 /* Set the phase */ 00541 hcryp->Phase = HAL_CRYP_PHASE_PROCESS; 00542 } 00543 } 00544 00545 /** 00546 * @brief Sets the CRYP peripheral in TDES CBC mode 00547 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 00548 * the configuration information for CRYP module 00549 * @param Direction Encryption or decryption 00550 * @retval None 00551 */ 00552 static void CRYP_SetTDESCBCMode(CRYP_HandleTypeDef *hcryp, uint32_t Direction) 00553 { 00554 /* Check if initialization phase has already been performed */ 00555 if(hcryp->Phase == HAL_CRYP_PHASE_READY) 00556 { 00557 /* Set the CRYP peripheral in AES CBC mode */ 00558 __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_TDES_CBC | Direction); 00559 00560 /* Set the key */ 00561 CRYP_SetKey(hcryp, hcryp->Init.pKey, CRYP_KEYSIZE_192B); 00562 00563 /* Set the Initialization Vector */ 00564 CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_256B); 00565 00566 /* Flush FIFO */ 00567 __HAL_CRYP_FIFO_FLUSH(hcryp); 00568 00569 /* Set the phase */ 00570 hcryp->Phase = HAL_CRYP_PHASE_PROCESS; 00571 } 00572 } 00573 00574 /** 00575 * @} 00576 */ 00577 00578 /* Exported functions --------------------------------------------------------*/ 00579 /** @addtogroup CRYP_Exported_Functions 00580 * @{ 00581 */ 00582 00583 /** @defgroup CRYP_Exported_Functions_Group1 Initialization and de-initialization functions 00584 * @brief Initialization and Configuration functions. 00585 * 00586 @verbatim 00587 ============================================================================== 00588 ##### Initialization and de-initialization functions ##### 00589 ============================================================================== 00590 [..] This section provides functions allowing to: 00591 (+) Initialize the CRYP according to the specified parameters 00592 in the CRYP_InitTypeDef and creates the associated handle 00593 (+) DeInitialize the CRYP peripheral 00594 (+) Initialize the CRYP MSP 00595 (+) DeInitialize CRYP MSP 00596 00597 @endverbatim 00598 * @{ 00599 */ 00600 00601 /** 00602 * @brief Initializes the CRYP according to the specified 00603 * parameters in the CRYP_InitTypeDef and creates the associated handle. 00604 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 00605 * the configuration information for CRYP module 00606 * @retval HAL status 00607 */ 00608 HAL_StatusTypeDef HAL_CRYP_Init(CRYP_HandleTypeDef *hcryp) 00609 { 00610 /* Check the CRYP handle allocation */ 00611 if(hcryp == NULL) 00612 { 00613 return HAL_ERROR; 00614 } 00615 00616 /* Check the parameters */ 00617 assert_param(IS_CRYP_KEYSIZE(hcryp->Init.KeySize)); 00618 assert_param(IS_CRYP_DATATYPE(hcryp->Init.DataType)); 00619 00620 if(hcryp->State == HAL_CRYP_STATE_RESET) 00621 { 00622 /* Allocate lock resource and initialize it */ 00623 hcryp->Lock = HAL_UNLOCKED; 00624 /* Init the low level hardware */ 00625 HAL_CRYP_MspInit(hcryp); 00626 } 00627 00628 /* Change the CRYP state */ 00629 hcryp->State = HAL_CRYP_STATE_BUSY; 00630 00631 /* Set the key size and data type*/ 00632 CRYP->CR = (uint32_t) (hcryp->Init.KeySize | hcryp->Init.DataType); 00633 00634 /* Reset CrypInCount and CrypOutCount */ 00635 hcryp->CrypInCount = 0U; 00636 hcryp->CrypOutCount = 0U; 00637 00638 /* Change the CRYP state */ 00639 hcryp->State = HAL_CRYP_STATE_READY; 00640 00641 /* Set the default CRYP phase */ 00642 hcryp->Phase = HAL_CRYP_PHASE_READY; 00643 00644 /* Return function status */ 00645 return HAL_OK; 00646 } 00647 00648 /** 00649 * @brief DeInitializes the CRYP peripheral. 00650 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 00651 * the configuration information for CRYP module 00652 * @retval HAL status 00653 */ 00654 HAL_StatusTypeDef HAL_CRYP_DeInit(CRYP_HandleTypeDef *hcryp) 00655 { 00656 /* Check the CRYP handle allocation */ 00657 if(hcryp == NULL) 00658 { 00659 return HAL_ERROR; 00660 } 00661 00662 /* Change the CRYP state */ 00663 hcryp->State = HAL_CRYP_STATE_BUSY; 00664 00665 /* Set the default CRYP phase */ 00666 hcryp->Phase = HAL_CRYP_PHASE_READY; 00667 00668 /* Reset CrypInCount and CrypOutCount */ 00669 hcryp->CrypInCount = 0U; 00670 hcryp->CrypOutCount = 0U; 00671 00672 /* Disable the CRYP Peripheral Clock */ 00673 __HAL_CRYP_DISABLE(hcryp); 00674 00675 /* DeInit the low level hardware: CLOCK, NVIC.*/ 00676 HAL_CRYP_MspDeInit(hcryp); 00677 00678 /* Change the CRYP state */ 00679 hcryp->State = HAL_CRYP_STATE_RESET; 00680 00681 /* Release Lock */ 00682 __HAL_UNLOCK(hcryp); 00683 00684 /* Return function status */ 00685 return HAL_OK; 00686 } 00687 00688 /** 00689 * @brief Initializes the CRYP MSP. 00690 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 00691 * the configuration information for CRYP module 00692 * @retval None 00693 */ 00694 __weak void HAL_CRYP_MspInit(CRYP_HandleTypeDef *hcryp) 00695 { 00696 /* Prevent unused argument(s) compilation warning */ 00697 UNUSED(hcryp); 00698 /* NOTE : This function Should not be modified, when the callback is needed, 00699 the HAL_CRYP_MspInit could be implemented in the user file 00700 */ 00701 } 00702 00703 /** 00704 * @brief DeInitializes CRYP MSP. 00705 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 00706 * the configuration information for CRYP module 00707 * @retval None 00708 */ 00709 __weak void HAL_CRYP_MspDeInit(CRYP_HandleTypeDef *hcryp) 00710 { 00711 /* Prevent unused argument(s) compilation warning */ 00712 UNUSED(hcryp); 00713 /* NOTE : This function Should not be modified, when the callback is needed, 00714 the HAL_CRYP_MspDeInit could be implemented in the user file 00715 */ 00716 } 00717 00718 /** 00719 * @} 00720 */ 00721 00722 /** @defgroup CRYP_Exported_Functions_Group2 AES processing functions 00723 * @brief processing functions. 00724 * 00725 @verbatim 00726 ============================================================================== 00727 ##### AES processing functions ##### 00728 ============================================================================== 00729 [..] This section provides functions allowing to: 00730 (+) Encrypt plaintext using AES-128/192/256 using chaining modes 00731 (+) Decrypt cyphertext using AES-128/192/256 using chaining modes 00732 [..] Three processing functions are available: 00733 (+) Polling mode 00734 (+) Interrupt mode 00735 (+) DMA mode 00736 00737 @endverbatim 00738 * @{ 00739 */ 00740 00741 /** 00742 * @brief Initializes the CRYP peripheral in AES ECB encryption mode 00743 * then encrypt pPlainData. The cypher data are available in pCypherData 00744 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 00745 * the configuration information for CRYP module 00746 * @param pPlainData Pointer to the plaintext buffer 00747 * @param Size Length of the plaintext buffer, must be a multiple of 16. 00748 * @param pCypherData Pointer to the cyphertext buffer 00749 * @param Timeout Specify Timeout value 00750 * @retval HAL status 00751 */ 00752 HAL_StatusTypeDef HAL_CRYP_AESECB_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout) 00753 { 00754 /* Process Locked */ 00755 __HAL_LOCK(hcryp); 00756 00757 /* Change the CRYP state */ 00758 hcryp->State = HAL_CRYP_STATE_BUSY; 00759 00760 /* Check if initialization phase has already been performed */ 00761 if(hcryp->Phase == HAL_CRYP_PHASE_READY) 00762 { 00763 /* Set the key */ 00764 CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize); 00765 00766 /* Set the CRYP peripheral in AES ECB mode */ 00767 __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_ECB); 00768 00769 /* Flush FIFO */ 00770 __HAL_CRYP_FIFO_FLUSH(hcryp); 00771 00772 /* Enable CRYP */ 00773 __HAL_CRYP_ENABLE(hcryp); 00774 00775 /* Set the phase */ 00776 hcryp->Phase = HAL_CRYP_PHASE_PROCESS; 00777 } 00778 00779 /* Write Plain Data and Get Cypher Data */ 00780 if(CRYP_ProcessData(hcryp, pPlainData, Size, pCypherData, Timeout) != HAL_OK) 00781 { 00782 return HAL_TIMEOUT; 00783 } 00784 00785 /* Change the CRYP state */ 00786 hcryp->State = HAL_CRYP_STATE_READY; 00787 00788 /* Process Unlocked */ 00789 __HAL_UNLOCK(hcryp); 00790 00791 /* Return function status */ 00792 return HAL_OK; 00793 } 00794 00795 /** 00796 * @brief Initializes the CRYP peripheral in AES CBC encryption mode 00797 * then encrypt pPlainData. The cypher data are available in pCypherData 00798 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 00799 * the configuration information for CRYP module 00800 * @param pPlainData Pointer to the plaintext buffer 00801 * @param Size Length of the plaintext buffer, must be a multiple of 16. 00802 * @param pCypherData Pointer to the cyphertext buffer 00803 * @param Timeout Specify Timeout value 00804 * @retval HAL status 00805 */ 00806 HAL_StatusTypeDef HAL_CRYP_AESCBC_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout) 00807 { 00808 /* Process Locked */ 00809 __HAL_LOCK(hcryp); 00810 00811 /* Change the CRYP state */ 00812 hcryp->State = HAL_CRYP_STATE_BUSY; 00813 00814 /* Check if initialization phase has already been performed */ 00815 if(hcryp->Phase == HAL_CRYP_PHASE_READY) 00816 { 00817 /* Set the key */ 00818 CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize); 00819 00820 /* Set the CRYP peripheral in AES ECB mode */ 00821 __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CBC); 00822 00823 /* Set the Initialization Vector */ 00824 CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_128B); 00825 00826 /* Flush FIFO */ 00827 __HAL_CRYP_FIFO_FLUSH(hcryp); 00828 00829 /* Enable CRYP */ 00830 __HAL_CRYP_ENABLE(hcryp); 00831 00832 /* Set the phase */ 00833 hcryp->Phase = HAL_CRYP_PHASE_PROCESS; 00834 } 00835 00836 /* Write Plain Data and Get Cypher Data */ 00837 if(CRYP_ProcessData(hcryp,pPlainData, Size, pCypherData, Timeout) != HAL_OK) 00838 { 00839 return HAL_TIMEOUT; 00840 } 00841 00842 /* Change the CRYP state */ 00843 hcryp->State = HAL_CRYP_STATE_READY; 00844 00845 /* Process Unlocked */ 00846 __HAL_UNLOCK(hcryp); 00847 00848 /* Return function status */ 00849 return HAL_OK; 00850 } 00851 00852 /** 00853 * @brief Initializes the CRYP peripheral in AES CTR encryption mode 00854 * then encrypt pPlainData. The cypher data are available in pCypherData 00855 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 00856 * the configuration information for CRYP module 00857 * @param pPlainData Pointer to the plaintext buffer 00858 * @param Size Length of the plaintext buffer, must be a multiple of 16. 00859 * @param pCypherData Pointer to the cyphertext buffer 00860 * @param Timeout Specify Timeout value 00861 * @retval HAL status 00862 */ 00863 HAL_StatusTypeDef HAL_CRYP_AESCTR_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout) 00864 { 00865 /* Process Locked */ 00866 __HAL_LOCK(hcryp); 00867 00868 /* Change the CRYP state */ 00869 hcryp->State = HAL_CRYP_STATE_BUSY; 00870 00871 /* Check if initialization phase has already been performed */ 00872 if(hcryp->Phase == HAL_CRYP_PHASE_READY) 00873 { 00874 /* Set the key */ 00875 CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize); 00876 00877 /* Set the CRYP peripheral in AES ECB mode */ 00878 __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CTR); 00879 00880 /* Set the Initialization Vector */ 00881 CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_128B); 00882 00883 /* Flush FIFO */ 00884 __HAL_CRYP_FIFO_FLUSH(hcryp); 00885 00886 /* Enable CRYP */ 00887 __HAL_CRYP_ENABLE(hcryp); 00888 00889 /* Set the phase */ 00890 hcryp->Phase = HAL_CRYP_PHASE_PROCESS; 00891 } 00892 00893 /* Write Plain Data and Get Cypher Data */ 00894 if(CRYP_ProcessData(hcryp, pPlainData, Size, pCypherData, Timeout) != HAL_OK) 00895 { 00896 return HAL_TIMEOUT; 00897 } 00898 00899 /* Change the CRYP state */ 00900 hcryp->State = HAL_CRYP_STATE_READY; 00901 00902 /* Process Unlocked */ 00903 __HAL_UNLOCK(hcryp); 00904 00905 /* Return function status */ 00906 return HAL_OK; 00907 } 00908 00909 00910 00911 /** 00912 * @brief Initializes the CRYP peripheral in AES ECB decryption mode 00913 * then decrypted pCypherData. The cypher data are available in pPlainData 00914 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 00915 * the configuration information for CRYP module 00916 * @param pCypherData Pointer to the cyphertext buffer 00917 * @param Size Length of the plaintext buffer, must be a multiple of 16. 00918 * @param pPlainData Pointer to the plaintext buffer 00919 * @param Timeout Specify Timeout value 00920 * @retval HAL status 00921 */ 00922 HAL_StatusTypeDef HAL_CRYP_AESECB_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout) 00923 { 00924 uint32_t tickstart = 0U; 00925 00926 /* Process Locked */ 00927 __HAL_LOCK(hcryp); 00928 00929 /* Change the CRYP state */ 00930 hcryp->State = HAL_CRYP_STATE_BUSY; 00931 00932 /* Check if initialization phase has already been performed */ 00933 if(hcryp->Phase == HAL_CRYP_PHASE_READY) 00934 { 00935 /* Set the key */ 00936 CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize); 00937 00938 /* Set the CRYP peripheral in AES Key mode */ 00939 __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_KEY | CRYP_CR_ALGODIR); 00940 00941 /* Enable CRYP */ 00942 __HAL_CRYP_ENABLE(hcryp); 00943 00944 /* Get tick */ 00945 tickstart = HAL_GetTick(); 00946 00947 while(HAL_IS_BIT_SET(hcryp->Instance->SR, CRYP_FLAG_BUSY)) 00948 { 00949 /* Check for the Timeout */ 00950 if(Timeout != HAL_MAX_DELAY) 00951 { 00952 if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout)) 00953 { 00954 /* Change state */ 00955 hcryp->State = HAL_CRYP_STATE_TIMEOUT; 00956 00957 /* Process Unlocked */ 00958 __HAL_UNLOCK(hcryp); 00959 00960 return HAL_TIMEOUT; 00961 } 00962 } 00963 } 00964 00965 /* Disable CRYP */ 00966 __HAL_CRYP_DISABLE(hcryp); 00967 00968 /* Reset the ALGOMODE bits*/ 00969 CRYP->CR &= (uint32_t)(~CRYP_CR_ALGOMODE); 00970 00971 /* Set the CRYP peripheral in AES ECB decryption mode */ 00972 __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_ECB | CRYP_CR_ALGODIR); 00973 /* Flush FIFO */ 00974 __HAL_CRYP_FIFO_FLUSH(hcryp); 00975 00976 /* Enable CRYP */ 00977 __HAL_CRYP_ENABLE(hcryp); 00978 00979 /* Set the phase */ 00980 hcryp->Phase = HAL_CRYP_PHASE_PROCESS; 00981 } 00982 00983 /* Write Plain Data and Get Cypher Data */ 00984 if(CRYP_ProcessData(hcryp, pCypherData, Size, pPlainData, Timeout) != HAL_OK) 00985 { 00986 return HAL_TIMEOUT; 00987 } 00988 00989 /* Change the CRYP state */ 00990 hcryp->State = HAL_CRYP_STATE_READY; 00991 00992 /* Process Unlocked */ 00993 __HAL_UNLOCK(hcryp); 00994 00995 /* Return function status */ 00996 return HAL_OK; 00997 } 00998 00999 /** 01000 * @brief Initializes the CRYP peripheral in AES ECB decryption mode 01001 * then decrypted pCypherData. The cypher data are available in pPlainData 01002 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 01003 * the configuration information for CRYP module 01004 * @param pCypherData Pointer to the cyphertext buffer 01005 * @param Size Length of the plaintext buffer, must be a multiple of 16. 01006 * @param pPlainData Pointer to the plaintext buffer 01007 * @param Timeout Specify Timeout value 01008 * @retval HAL status 01009 */ 01010 HAL_StatusTypeDef HAL_CRYP_AESCBC_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout) 01011 { 01012 uint32_t tickstart = 0U; 01013 01014 /* Process Locked */ 01015 __HAL_LOCK(hcryp); 01016 01017 /* Change the CRYP state */ 01018 hcryp->State = HAL_CRYP_STATE_BUSY; 01019 01020 /* Check if initialization phase has already been performed */ 01021 if(hcryp->Phase == HAL_CRYP_PHASE_READY) 01022 { 01023 /* Set the key */ 01024 CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize); 01025 01026 /* Set the CRYP peripheral in AES Key mode */ 01027 __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_KEY | CRYP_CR_ALGODIR); 01028 01029 /* Enable CRYP */ 01030 __HAL_CRYP_ENABLE(hcryp); 01031 01032 /* Get tick */ 01033 tickstart = HAL_GetTick(); 01034 01035 while(HAL_IS_BIT_SET(hcryp->Instance->SR, CRYP_FLAG_BUSY)) 01036 { 01037 /* Check for the Timeout */ 01038 if(Timeout != HAL_MAX_DELAY) 01039 { 01040 if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout)) 01041 { 01042 /* Change state */ 01043 hcryp->State = HAL_CRYP_STATE_TIMEOUT; 01044 01045 /* Process Unlocked */ 01046 __HAL_UNLOCK(hcryp); 01047 01048 return HAL_TIMEOUT; 01049 } 01050 } 01051 } 01052 01053 /* Reset the ALGOMODE bits*/ 01054 CRYP->CR &= (uint32_t)(~CRYP_CR_ALGOMODE); 01055 01056 /* Set the CRYP peripheral in AES CBC decryption mode */ 01057 __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CBC | CRYP_CR_ALGODIR); 01058 01059 /* Set the Initialization Vector */ 01060 CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_128B); 01061 01062 /* Flush FIFO */ 01063 __HAL_CRYP_FIFO_FLUSH(hcryp); 01064 01065 /* Enable CRYP */ 01066 __HAL_CRYP_ENABLE(hcryp); 01067 01068 /* Set the phase */ 01069 hcryp->Phase = HAL_CRYP_PHASE_PROCESS; 01070 } 01071 01072 /* Write Plain Data and Get Cypher Data */ 01073 if(CRYP_ProcessData(hcryp, pCypherData, Size, pPlainData, Timeout) != HAL_OK) 01074 { 01075 return HAL_TIMEOUT; 01076 } 01077 01078 /* Change the CRYP state */ 01079 hcryp->State = HAL_CRYP_STATE_READY; 01080 01081 /* Process Unlocked */ 01082 __HAL_UNLOCK(hcryp); 01083 01084 /* Return function status */ 01085 return HAL_OK; 01086 } 01087 01088 /** 01089 * @brief Initializes the CRYP peripheral in AES CTR decryption mode 01090 * then decrypted pCypherData. The cypher data are available in pPlainData 01091 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 01092 * the configuration information for CRYP module 01093 * @param pCypherData Pointer to the cyphertext buffer 01094 * @param Size Length of the plaintext buffer, must be a multiple of 16. 01095 * @param pPlainData Pointer to the plaintext buffer 01096 * @param Timeout Specify Timeout value 01097 * @retval HAL status 01098 */ 01099 HAL_StatusTypeDef HAL_CRYP_AESCTR_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout) 01100 { 01101 /* Process Locked */ 01102 __HAL_LOCK(hcryp); 01103 01104 /* Check if initialization phase has already been performed */ 01105 if(hcryp->Phase == HAL_CRYP_PHASE_READY) 01106 { 01107 /* Change the CRYP state */ 01108 hcryp->State = HAL_CRYP_STATE_BUSY; 01109 01110 /* Set the key */ 01111 CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize); 01112 01113 /* Set the CRYP peripheral in AES CTR mode */ 01114 __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CTR | CRYP_CR_ALGODIR); 01115 01116 /* Set the Initialization Vector */ 01117 CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_128B); 01118 01119 /* Flush FIFO */ 01120 __HAL_CRYP_FIFO_FLUSH(hcryp); 01121 01122 /* Enable CRYP */ 01123 __HAL_CRYP_ENABLE(hcryp); 01124 01125 /* Set the phase */ 01126 hcryp->Phase = HAL_CRYP_PHASE_PROCESS; 01127 } 01128 01129 /* Write Plain Data and Get Cypher Data */ 01130 if(CRYP_ProcessData(hcryp, pCypherData, Size, pPlainData, Timeout) != HAL_OK) 01131 { 01132 return HAL_TIMEOUT; 01133 } 01134 01135 /* Change the CRYP state */ 01136 hcryp->State = HAL_CRYP_STATE_READY; 01137 01138 /* Process Unlocked */ 01139 __HAL_UNLOCK(hcryp); 01140 01141 /* Return function status */ 01142 return HAL_OK; 01143 } 01144 01145 /** 01146 * @brief Initializes the CRYP peripheral in AES ECB encryption mode using Interrupt. 01147 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 01148 * the configuration information for CRYP module 01149 * @param pPlainData Pointer to the plaintext buffer 01150 * @param Size Length of the plaintext buffer, must be a multiple of 16 bytes 01151 * @param pCypherData Pointer to the cyphertext buffer 01152 * @retval HAL status 01153 */ 01154 HAL_StatusTypeDef HAL_CRYP_AESECB_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData) 01155 { 01156 uint32_t inputaddr; 01157 uint32_t outputaddr; 01158 01159 if(hcryp->State == HAL_CRYP_STATE_READY) 01160 { 01161 /* Process Locked */ 01162 __HAL_LOCK(hcryp); 01163 01164 hcryp->CrypInCount = Size; 01165 hcryp->pCrypInBuffPtr = pPlainData; 01166 hcryp->pCrypOutBuffPtr = pCypherData; 01167 hcryp->CrypOutCount = Size; 01168 01169 /* Change the CRYP state */ 01170 hcryp->State = HAL_CRYP_STATE_BUSY; 01171 01172 /* Check if initialization phase has already been performed */ 01173 if(hcryp->Phase == HAL_CRYP_PHASE_READY) 01174 { 01175 /* Set the key */ 01176 CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize); 01177 01178 /* Set the CRYP peripheral in AES ECB mode */ 01179 __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_ECB); 01180 01181 /* Flush FIFO */ 01182 __HAL_CRYP_FIFO_FLUSH(hcryp); 01183 01184 /* Set the phase */ 01185 hcryp->Phase = HAL_CRYP_PHASE_PROCESS; 01186 } 01187 01188 /* Enable Interrupts */ 01189 __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI); 01190 01191 /* Enable CRYP */ 01192 __HAL_CRYP_ENABLE(hcryp); 01193 01194 /* Return function status */ 01195 return HAL_OK; 01196 } 01197 else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI)) 01198 { 01199 inputaddr = (uint32_t)hcryp->pCrypInBuffPtr; 01200 /* Write the Input block in the IN FIFO */ 01201 hcryp->Instance->DR = *(uint32_t*)(inputaddr); 01202 inputaddr+=4U; 01203 hcryp->Instance->DR = *(uint32_t*)(inputaddr); 01204 inputaddr+=4U; 01205 hcryp->Instance->DR = *(uint32_t*)(inputaddr); 01206 inputaddr+=4U; 01207 hcryp->Instance->DR = *(uint32_t*)(inputaddr); 01208 hcryp->pCrypInBuffPtr += 16U; 01209 hcryp->CrypInCount -= 16U; 01210 if(hcryp->CrypInCount == 0U) 01211 { 01212 __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI); 01213 /* Call the Input data transfer complete callback */ 01214 HAL_CRYP_InCpltCallback(hcryp); 01215 } 01216 } 01217 else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI)) 01218 { 01219 outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr; 01220 /* Read the Output block from the Output FIFO */ 01221 *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; 01222 outputaddr+=4U; 01223 *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; 01224 outputaddr+=4U; 01225 *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; 01226 outputaddr+=4U; 01227 *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; 01228 hcryp->pCrypOutBuffPtr += 16U; 01229 hcryp->CrypOutCount -= 16U; 01230 if(hcryp->CrypOutCount == 0U) 01231 { 01232 __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI); 01233 /* Process Locked */ 01234 __HAL_UNLOCK(hcryp); 01235 /* Change the CRYP state */ 01236 hcryp->State = HAL_CRYP_STATE_READY; 01237 /* Call Input transfer complete callback */ 01238 HAL_CRYP_OutCpltCallback(hcryp); 01239 } 01240 } 01241 01242 /* Return function status */ 01243 return HAL_OK; 01244 } 01245 01246 /** 01247 * @brief Initializes the CRYP peripheral in AES CBC encryption mode using Interrupt. 01248 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 01249 * the configuration information for CRYP module 01250 * @param pPlainData Pointer to the plaintext buffer 01251 * @param Size Length of the plaintext buffer, must be a multiple of 16 bytes 01252 * @param pCypherData Pointer to the cyphertext buffer 01253 * @retval HAL status 01254 */ 01255 HAL_StatusTypeDef HAL_CRYP_AESCBC_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData) 01256 { 01257 uint32_t inputaddr; 01258 uint32_t outputaddr; 01259 01260 if(hcryp->State == HAL_CRYP_STATE_READY) 01261 { 01262 /* Process Locked */ 01263 __HAL_LOCK(hcryp); 01264 01265 hcryp->CrypInCount = Size; 01266 hcryp->pCrypInBuffPtr = pPlainData; 01267 hcryp->pCrypOutBuffPtr = pCypherData; 01268 hcryp->CrypOutCount = Size; 01269 01270 /* Change the CRYP state */ 01271 hcryp->State = HAL_CRYP_STATE_BUSY; 01272 01273 /* Check if initialization phase has already been performed */ 01274 if(hcryp->Phase == HAL_CRYP_PHASE_READY) 01275 { 01276 /* Set the key */ 01277 CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize); 01278 01279 /* Set the CRYP peripheral in AES CBC mode */ 01280 __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CBC); 01281 01282 /* Set the Initialization Vector */ 01283 CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_128B); 01284 01285 /* Flush FIFO */ 01286 __HAL_CRYP_FIFO_FLUSH(hcryp); 01287 01288 /* Set the phase */ 01289 hcryp->Phase = HAL_CRYP_PHASE_PROCESS; 01290 } 01291 /* Enable Interrupts */ 01292 __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI); 01293 01294 /* Enable CRYP */ 01295 __HAL_CRYP_ENABLE(hcryp); 01296 01297 /* Return function status */ 01298 return HAL_OK; 01299 } 01300 else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI)) 01301 { 01302 inputaddr = (uint32_t)hcryp->pCrypInBuffPtr; 01303 /* Write the Input block in the IN FIFO */ 01304 hcryp->Instance->DR = *(uint32_t*)(inputaddr); 01305 inputaddr+=4U; 01306 hcryp->Instance->DR = *(uint32_t*)(inputaddr); 01307 inputaddr+=4U; 01308 hcryp->Instance->DR = *(uint32_t*)(inputaddr); 01309 inputaddr+=4U; 01310 hcryp->Instance->DR = *(uint32_t*)(inputaddr); 01311 hcryp->pCrypInBuffPtr += 16U; 01312 hcryp->CrypInCount -= 16U; 01313 if(hcryp->CrypInCount == 0U) 01314 { 01315 __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI); 01316 /* Call the Input data transfer complete callback */ 01317 HAL_CRYP_InCpltCallback(hcryp); 01318 } 01319 } 01320 else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI)) 01321 { 01322 outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr; 01323 /* Read the Output block from the Output FIFO */ 01324 *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; 01325 outputaddr+=4U; 01326 *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; 01327 outputaddr+=4U; 01328 *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; 01329 outputaddr+=4U; 01330 *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; 01331 hcryp->pCrypOutBuffPtr += 16U; 01332 hcryp->CrypOutCount -= 16U; 01333 if(hcryp->CrypOutCount == 0U) 01334 { 01335 __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI); 01336 /* Process Locked */ 01337 __HAL_UNLOCK(hcryp); 01338 /* Change the CRYP state */ 01339 hcryp->State = HAL_CRYP_STATE_READY; 01340 /* Call Input transfer complete callback */ 01341 HAL_CRYP_OutCpltCallback(hcryp); 01342 } 01343 } 01344 01345 /* Return function status */ 01346 return HAL_OK; 01347 } 01348 01349 /** 01350 * @brief Initializes the CRYP peripheral in AES CTR encryption mode using Interrupt. 01351 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 01352 * the configuration information for CRYP module 01353 * @param pPlainData Pointer to the plaintext buffer 01354 * @param Size Length of the plaintext buffer, must be a multiple of 16 bytes 01355 * @param pCypherData Pointer to the cyphertext buffer 01356 * @retval HAL status 01357 */ 01358 HAL_StatusTypeDef HAL_CRYP_AESCTR_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData) 01359 { 01360 uint32_t inputaddr; 01361 uint32_t outputaddr; 01362 01363 if(hcryp->State == HAL_CRYP_STATE_READY) 01364 { 01365 /* Process Locked */ 01366 __HAL_LOCK(hcryp); 01367 01368 hcryp->CrypInCount = Size; 01369 hcryp->pCrypInBuffPtr = pPlainData; 01370 hcryp->pCrypOutBuffPtr = pCypherData; 01371 hcryp->CrypOutCount = Size; 01372 01373 /* Change the CRYP state */ 01374 hcryp->State = HAL_CRYP_STATE_BUSY; 01375 01376 /* Check if initialization phase has already been performed */ 01377 if(hcryp->Phase == HAL_CRYP_PHASE_READY) 01378 { 01379 /* Set the key */ 01380 CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize); 01381 01382 /* Set the CRYP peripheral in AES CTR mode */ 01383 __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CTR); 01384 01385 /* Set the Initialization Vector */ 01386 CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_128B); 01387 01388 /* Flush FIFO */ 01389 __HAL_CRYP_FIFO_FLUSH(hcryp); 01390 01391 /* Set the phase */ 01392 hcryp->Phase = HAL_CRYP_PHASE_PROCESS; 01393 } 01394 /* Enable Interrupts */ 01395 __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI); 01396 01397 /* Enable CRYP */ 01398 __HAL_CRYP_ENABLE(hcryp); 01399 01400 /* Return function status */ 01401 return HAL_OK; 01402 } 01403 else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI)) 01404 { 01405 inputaddr = (uint32_t)hcryp->pCrypInBuffPtr; 01406 /* Write the Input block in the IN FIFO */ 01407 hcryp->Instance->DR = *(uint32_t*)(inputaddr); 01408 inputaddr+=4U; 01409 hcryp->Instance->DR = *(uint32_t*)(inputaddr); 01410 inputaddr+=4U; 01411 hcryp->Instance->DR = *(uint32_t*)(inputaddr); 01412 inputaddr+=4U; 01413 hcryp->Instance->DR = *(uint32_t*)(inputaddr); 01414 hcryp->pCrypInBuffPtr += 16U; 01415 hcryp->CrypInCount -= 16U; 01416 if(hcryp->CrypInCount == 0U) 01417 { 01418 __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI); 01419 /* Call the Input data transfer complete callback */ 01420 HAL_CRYP_InCpltCallback(hcryp); 01421 } 01422 } 01423 else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI)) 01424 { 01425 outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr; 01426 /* Read the Output block from the Output FIFO */ 01427 *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; 01428 outputaddr+=4U; 01429 *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; 01430 outputaddr+=4U; 01431 *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; 01432 outputaddr+=4U; 01433 *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; 01434 hcryp->pCrypOutBuffPtr += 16U; 01435 hcryp->CrypOutCount -= 16U; 01436 if(hcryp->CrypOutCount == 0U) 01437 { 01438 __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI); 01439 /* Process Unlocked */ 01440 __HAL_UNLOCK(hcryp); 01441 /* Change the CRYP state */ 01442 hcryp->State = HAL_CRYP_STATE_READY; 01443 /* Call Input transfer complete callback */ 01444 HAL_CRYP_OutCpltCallback(hcryp); 01445 } 01446 } 01447 01448 /* Return function status */ 01449 return HAL_OK; 01450 } 01451 01452 01453 /** 01454 * @brief Initializes the CRYP peripheral in AES ECB decryption mode using Interrupt. 01455 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 01456 * the configuration information for CRYP module 01457 * @param pCypherData Pointer to the cyphertext buffer 01458 * @param Size Length of the plaintext buffer, must be a multiple of 16. 01459 * @param pPlainData Pointer to the plaintext buffer 01460 * @retval HAL status 01461 */ 01462 HAL_StatusTypeDef HAL_CRYP_AESECB_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData) 01463 { 01464 uint32_t tickstart = 0U; 01465 01466 uint32_t inputaddr; 01467 uint32_t outputaddr; 01468 01469 if(hcryp->State == HAL_CRYP_STATE_READY) 01470 { 01471 /* Process Locked */ 01472 __HAL_LOCK(hcryp); 01473 01474 hcryp->CrypInCount = Size; 01475 hcryp->pCrypInBuffPtr = pCypherData; 01476 hcryp->pCrypOutBuffPtr = pPlainData; 01477 hcryp->CrypOutCount = Size; 01478 01479 /* Change the CRYP state */ 01480 hcryp->State = HAL_CRYP_STATE_BUSY; 01481 01482 /* Check if initialization phase has already been performed */ 01483 if(hcryp->Phase == HAL_CRYP_PHASE_READY) 01484 { 01485 /* Set the key */ 01486 CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize); 01487 01488 /* Set the CRYP peripheral in AES Key mode */ 01489 __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_KEY | CRYP_CR_ALGODIR); 01490 /* Enable CRYP */ 01491 __HAL_CRYP_ENABLE(hcryp); 01492 01493 /* Get tick */ 01494 tickstart = HAL_GetTick(); 01495 01496 while(HAL_IS_BIT_SET(hcryp->Instance->SR, CRYP_FLAG_BUSY)) 01497 { 01498 /* Check for the Timeout */ 01499 if((HAL_GetTick() - tickstart ) > CRYP_TIMEOUT_VALUE) 01500 { 01501 /* Change state */ 01502 hcryp->State = HAL_CRYP_STATE_TIMEOUT; 01503 01504 /* Process Unlocked */ 01505 __HAL_UNLOCK(hcryp); 01506 01507 return HAL_TIMEOUT; 01508 } 01509 } 01510 01511 /* Reset the ALGOMODE bits*/ 01512 CRYP->CR &= (uint32_t)(~CRYP_CR_ALGOMODE); 01513 01514 /* Set the CRYP peripheral in AES ECB decryption mode */ 01515 __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_ECB | CRYP_CR_ALGODIR); 01516 01517 /* Flush FIFO */ 01518 __HAL_CRYP_FIFO_FLUSH(hcryp); 01519 01520 /* Set the phase */ 01521 hcryp->Phase = HAL_CRYP_PHASE_PROCESS; 01522 } 01523 01524 /* Enable Interrupts */ 01525 __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI); 01526 01527 /* Enable CRYP */ 01528 __HAL_CRYP_ENABLE(hcryp); 01529 01530 /* Return function status */ 01531 return HAL_OK; 01532 } 01533 else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI)) 01534 { 01535 inputaddr = (uint32_t)hcryp->pCrypInBuffPtr; 01536 /* Write the Input block in the IN FIFO */ 01537 hcryp->Instance->DR = *(uint32_t*)(inputaddr); 01538 inputaddr+=4U; 01539 hcryp->Instance->DR = *(uint32_t*)(inputaddr); 01540 inputaddr+=4U; 01541 hcryp->Instance->DR = *(uint32_t*)(inputaddr); 01542 inputaddr+=4U; 01543 hcryp->Instance->DR = *(uint32_t*)(inputaddr); 01544 hcryp->pCrypInBuffPtr += 16U; 01545 hcryp->CrypInCount -= 16U; 01546 if(hcryp->CrypInCount == 0U) 01547 { 01548 __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI); 01549 /* Call the Input data transfer complete callback */ 01550 HAL_CRYP_InCpltCallback(hcryp); 01551 } 01552 } 01553 else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI)) 01554 { 01555 outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr; 01556 /* Read the Output block from the Output FIFO */ 01557 *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; 01558 outputaddr+=4U; 01559 *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; 01560 outputaddr+=4U; 01561 *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; 01562 outputaddr+=4U; 01563 *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; 01564 hcryp->pCrypOutBuffPtr += 16U; 01565 hcryp->CrypOutCount -= 16U; 01566 if(hcryp->CrypOutCount == 0U) 01567 { 01568 __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI); 01569 /* Process Unlocked */ 01570 __HAL_UNLOCK(hcryp); 01571 /* Change the CRYP state */ 01572 hcryp->State = HAL_CRYP_STATE_READY; 01573 /* Call Input transfer complete callback */ 01574 HAL_CRYP_OutCpltCallback(hcryp); 01575 } 01576 } 01577 01578 /* Return function status */ 01579 return HAL_OK; 01580 } 01581 01582 /** 01583 * @brief Initializes the CRYP peripheral in AES CBC decryption mode using IT. 01584 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 01585 * the configuration information for CRYP module 01586 * @param pCypherData Pointer to the cyphertext buffer 01587 * @param Size Length of the plaintext buffer, must be a multiple of 16 01588 * @param pPlainData Pointer to the plaintext buffer 01589 * @retval HAL status 01590 */ 01591 HAL_StatusTypeDef HAL_CRYP_AESCBC_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData) 01592 { 01593 01594 uint32_t tickstart = 0U; 01595 uint32_t inputaddr; 01596 uint32_t outputaddr; 01597 01598 if(hcryp->State == HAL_CRYP_STATE_READY) 01599 { 01600 /* Process Locked */ 01601 __HAL_LOCK(hcryp); 01602 01603 /* Get the buffer addresses and sizes */ 01604 hcryp->CrypInCount = Size; 01605 hcryp->pCrypInBuffPtr = pCypherData; 01606 hcryp->pCrypOutBuffPtr = pPlainData; 01607 hcryp->CrypOutCount = Size; 01608 01609 /* Change the CRYP state */ 01610 hcryp->State = HAL_CRYP_STATE_BUSY; 01611 01612 /* Check if initialization phase has already been performed */ 01613 if(hcryp->Phase == HAL_CRYP_PHASE_READY) 01614 { 01615 /* Set the key */ 01616 CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize); 01617 01618 /* Set the CRYP peripheral in AES Key mode */ 01619 __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_KEY | CRYP_CR_ALGODIR); 01620 01621 /* Enable CRYP */ 01622 __HAL_CRYP_ENABLE(hcryp); 01623 01624 /* Get tick */ 01625 tickstart = HAL_GetTick(); 01626 01627 while(HAL_IS_BIT_SET(hcryp->Instance->SR, CRYP_FLAG_BUSY)) 01628 { 01629 /* Check for the Timeout */ 01630 if((HAL_GetTick() - tickstart ) > CRYP_TIMEOUT_VALUE) 01631 { 01632 /* Change state */ 01633 hcryp->State = HAL_CRYP_STATE_TIMEOUT; 01634 01635 /* Process Unlocked */ 01636 __HAL_UNLOCK(hcryp); 01637 01638 return HAL_TIMEOUT; 01639 } 01640 } 01641 01642 /* Reset the ALGOMODE bits*/ 01643 CRYP->CR &= (uint32_t)(~CRYP_CR_ALGOMODE); 01644 01645 /* Set the CRYP peripheral in AES CBC decryption mode */ 01646 __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CBC | CRYP_CR_ALGODIR); 01647 01648 /* Set the Initialization Vector */ 01649 CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_128B); 01650 01651 /* Flush FIFO */ 01652 __HAL_CRYP_FIFO_FLUSH(hcryp); 01653 01654 /* Enable CRYP */ 01655 __HAL_CRYP_ENABLE(hcryp); 01656 01657 /* Set the phase */ 01658 hcryp->Phase = HAL_CRYP_PHASE_PROCESS; 01659 } 01660 01661 /* Enable Interrupts */ 01662 __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI); 01663 01664 /* Enable CRYP */ 01665 __HAL_CRYP_ENABLE(hcryp); 01666 01667 /* Return function status */ 01668 return HAL_OK; 01669 } 01670 else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI)) 01671 { 01672 inputaddr = (uint32_t)hcryp->pCrypInBuffPtr; 01673 /* Write the Input block in the IN FIFO */ 01674 hcryp->Instance->DR = *(uint32_t*)(inputaddr); 01675 inputaddr+=4U; 01676 hcryp->Instance->DR = *(uint32_t*)(inputaddr); 01677 inputaddr+=4U; 01678 hcryp->Instance->DR = *(uint32_t*)(inputaddr); 01679 inputaddr+=4U; 01680 hcryp->Instance->DR = *(uint32_t*)(inputaddr); 01681 hcryp->pCrypInBuffPtr += 16U; 01682 hcryp->CrypInCount -= 16U; 01683 if(hcryp->CrypInCount == 0U) 01684 { 01685 __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI); 01686 /* Call the Input data transfer complete callback */ 01687 HAL_CRYP_InCpltCallback(hcryp); 01688 } 01689 } 01690 else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI)) 01691 { 01692 outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr; 01693 /* Read the Output block from the Output FIFO */ 01694 *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; 01695 outputaddr+=4U; 01696 *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; 01697 outputaddr+=4U; 01698 *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; 01699 outputaddr+=4U; 01700 *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; 01701 hcryp->pCrypOutBuffPtr += 16U; 01702 hcryp->CrypOutCount -= 16U; 01703 if(hcryp->CrypOutCount == 0U) 01704 { 01705 __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI); 01706 /* Process Unlocked */ 01707 __HAL_UNLOCK(hcryp); 01708 /* Change the CRYP state */ 01709 hcryp->State = HAL_CRYP_STATE_READY; 01710 /* Call Input transfer complete callback */ 01711 HAL_CRYP_OutCpltCallback(hcryp); 01712 } 01713 } 01714 01715 /* Return function status */ 01716 return HAL_OK; 01717 } 01718 01719 /** 01720 * @brief Initializes the CRYP peripheral in AES CTR decryption mode using Interrupt. 01721 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 01722 * the configuration information for CRYP module 01723 * @param pCypherData Pointer to the cyphertext buffer 01724 * @param Size Length of the plaintext buffer, must be a multiple of 16 01725 * @param pPlainData Pointer to the plaintext buffer 01726 * @retval HAL status 01727 */ 01728 HAL_StatusTypeDef HAL_CRYP_AESCTR_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData) 01729 { 01730 uint32_t inputaddr; 01731 uint32_t outputaddr; 01732 01733 if(hcryp->State == HAL_CRYP_STATE_READY) 01734 { 01735 /* Process Locked */ 01736 __HAL_LOCK(hcryp); 01737 01738 /* Get the buffer addresses and sizes */ 01739 hcryp->CrypInCount = Size; 01740 hcryp->pCrypInBuffPtr = pCypherData; 01741 hcryp->pCrypOutBuffPtr = pPlainData; 01742 hcryp->CrypOutCount = Size; 01743 01744 /* Change the CRYP state */ 01745 hcryp->State = HAL_CRYP_STATE_BUSY; 01746 01747 /* Check if initialization phase has already been performed */ 01748 if(hcryp->Phase == HAL_CRYP_PHASE_READY) 01749 { 01750 /* Set the key */ 01751 CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize); 01752 01753 /* Set the CRYP peripheral in AES CTR mode */ 01754 __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CTR | CRYP_CR_ALGODIR); 01755 01756 /* Set the Initialization Vector */ 01757 CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_128B); 01758 01759 /* Flush FIFO */ 01760 __HAL_CRYP_FIFO_FLUSH(hcryp); 01761 01762 /* Set the phase */ 01763 hcryp->Phase = HAL_CRYP_PHASE_PROCESS; 01764 } 01765 01766 /* Enable Interrupts */ 01767 __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI); 01768 01769 /* Enable CRYP */ 01770 __HAL_CRYP_ENABLE(hcryp); 01771 01772 /* Return function status */ 01773 return HAL_OK; 01774 } 01775 else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI)) 01776 { 01777 inputaddr = (uint32_t)hcryp->pCrypInBuffPtr; 01778 /* Write the Input block in the IN FIFO */ 01779 hcryp->Instance->DR = *(uint32_t*)(inputaddr); 01780 inputaddr+=4U; 01781 hcryp->Instance->DR = *(uint32_t*)(inputaddr); 01782 inputaddr+=4U; 01783 hcryp->Instance->DR = *(uint32_t*)(inputaddr); 01784 inputaddr+=4U; 01785 hcryp->Instance->DR = *(uint32_t*)(inputaddr); 01786 hcryp->pCrypInBuffPtr += 16U; 01787 hcryp->CrypInCount -= 16U; 01788 if(hcryp->CrypInCount == 0U) 01789 { 01790 __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI); 01791 /* Call the Input data transfer complete callback */ 01792 HAL_CRYP_InCpltCallback(hcryp); 01793 } 01794 } 01795 else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI)) 01796 { 01797 outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr; 01798 /* Read the Output block from the Output FIFO */ 01799 *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; 01800 outputaddr+=4U; 01801 *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; 01802 outputaddr+=4U; 01803 *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; 01804 outputaddr+=4U; 01805 *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; 01806 hcryp->pCrypOutBuffPtr += 16U; 01807 hcryp->CrypOutCount -= 16U; 01808 if(hcryp->CrypOutCount == 0U) 01809 { 01810 __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI); 01811 /* Process Unlocked */ 01812 __HAL_UNLOCK(hcryp); 01813 /* Change the CRYP state */ 01814 hcryp->State = HAL_CRYP_STATE_READY; 01815 /* Call Input transfer complete callback */ 01816 HAL_CRYP_OutCpltCallback(hcryp); 01817 } 01818 } 01819 01820 /* Return function status */ 01821 return HAL_OK; 01822 } 01823 01824 /** 01825 * @brief Initializes the CRYP peripheral in AES ECB encryption mode using DMA. 01826 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 01827 * the configuration information for CRYP module 01828 * @param pPlainData Pointer to the plaintext buffer 01829 * @param Size Length of the plaintext buffer, must be a multiple of 16 bytes 01830 * @param pCypherData Pointer to the cyphertext buffer 01831 * @retval HAL status 01832 */ 01833 HAL_StatusTypeDef HAL_CRYP_AESECB_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData) 01834 { 01835 uint32_t inputaddr; 01836 uint32_t outputaddr; 01837 01838 if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS)) 01839 { 01840 /* Process Locked */ 01841 __HAL_LOCK(hcryp); 01842 01843 inputaddr = (uint32_t)pPlainData; 01844 outputaddr = (uint32_t)pCypherData; 01845 01846 /* Change the CRYP state */ 01847 hcryp->State = HAL_CRYP_STATE_BUSY; 01848 01849 /* Check if initialization phase has already been performed */ 01850 if(hcryp->Phase == HAL_CRYP_PHASE_READY) 01851 { 01852 /* Set the key */ 01853 CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize); 01854 01855 /* Set the CRYP peripheral in AES ECB mode */ 01856 __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_ECB); 01857 01858 /* Flush FIFO */ 01859 __HAL_CRYP_FIFO_FLUSH(hcryp); 01860 01861 /* Set the phase */ 01862 hcryp->Phase = HAL_CRYP_PHASE_PROCESS; 01863 } 01864 /* Set the input and output addresses and start DMA transfer */ 01865 CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr); 01866 01867 /* Process Unlocked */ 01868 __HAL_UNLOCK(hcryp); 01869 01870 /* Return function status */ 01871 return HAL_OK; 01872 } 01873 else 01874 { 01875 return HAL_ERROR; 01876 } 01877 } 01878 01879 /** 01880 * @brief Initializes the CRYP peripheral in AES CBC encryption mode using DMA. 01881 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 01882 * the configuration information for CRYP module 01883 * @param pPlainData Pointer to the plaintext buffer 01884 * @param Size Length of the plaintext buffer, must be a multiple of 16. 01885 * @param pCypherData Pointer to the cyphertext buffer 01886 * @retval HAL status 01887 */ 01888 HAL_StatusTypeDef HAL_CRYP_AESCBC_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData) 01889 { 01890 uint32_t inputaddr; 01891 uint32_t outputaddr; 01892 01893 if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS)) 01894 { 01895 /* Process Locked */ 01896 __HAL_LOCK(hcryp); 01897 01898 inputaddr = (uint32_t)pPlainData; 01899 outputaddr = (uint32_t)pCypherData; 01900 01901 /* Change the CRYP state */ 01902 hcryp->State = HAL_CRYP_STATE_BUSY; 01903 01904 /* Check if initialization phase has already been performed */ 01905 if(hcryp->Phase == HAL_CRYP_PHASE_READY) 01906 { 01907 /* Set the key */ 01908 CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize); 01909 01910 /* Set the CRYP peripheral in AES ECB mode */ 01911 __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CBC); 01912 01913 /* Set the Initialization Vector */ 01914 CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_128B); 01915 01916 /* Flush FIFO */ 01917 __HAL_CRYP_FIFO_FLUSH(hcryp); 01918 01919 /* Set the phase */ 01920 hcryp->Phase = HAL_CRYP_PHASE_PROCESS; 01921 } 01922 /* Set the input and output addresses and start DMA transfer */ 01923 CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr); 01924 01925 /* Process Unlocked */ 01926 __HAL_UNLOCK(hcryp); 01927 01928 /* Return function status */ 01929 return HAL_OK; 01930 } 01931 else 01932 { 01933 return HAL_ERROR; 01934 } 01935 } 01936 01937 /** 01938 * @brief Initializes the CRYP peripheral in AES CTR encryption mode using DMA. 01939 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 01940 * the configuration information for CRYP module 01941 * @param pPlainData Pointer to the plaintext buffer 01942 * @param Size Length of the plaintext buffer, must be a multiple of 16. 01943 * @param pCypherData Pointer to the cyphertext buffer 01944 * @retval HAL status 01945 */ 01946 HAL_StatusTypeDef HAL_CRYP_AESCTR_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData) 01947 { 01948 uint32_t inputaddr; 01949 uint32_t outputaddr; 01950 01951 if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS)) 01952 { 01953 /* Process Locked */ 01954 __HAL_LOCK(hcryp); 01955 01956 inputaddr = (uint32_t)pPlainData; 01957 outputaddr = (uint32_t)pCypherData; 01958 01959 /* Change the CRYP state */ 01960 hcryp->State = HAL_CRYP_STATE_BUSY; 01961 01962 /* Check if initialization phase has already been performed */ 01963 if(hcryp->Phase == HAL_CRYP_PHASE_READY) 01964 { 01965 /* Set the key */ 01966 CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize); 01967 01968 /* Set the CRYP peripheral in AES ECB mode */ 01969 __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CTR); 01970 01971 /* Set the Initialization Vector */ 01972 CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_128B); 01973 01974 /* Flush FIFO */ 01975 __HAL_CRYP_FIFO_FLUSH(hcryp); 01976 01977 /* Set the phase */ 01978 hcryp->Phase = HAL_CRYP_PHASE_PROCESS; 01979 } 01980 01981 /* Set the input and output addresses and start DMA transfer */ 01982 CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr); 01983 01984 /* Process Unlocked */ 01985 __HAL_UNLOCK(hcryp); 01986 01987 /* Return function status */ 01988 return HAL_OK; 01989 } 01990 else 01991 { 01992 return HAL_ERROR; 01993 } 01994 } 01995 01996 /** 01997 * @brief Initializes the CRYP peripheral in AES ECB decryption mode using DMA. 01998 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 01999 * the configuration information for CRYP module 02000 * @param pCypherData Pointer to the cyphertext buffer 02001 * @param Size Length of the plaintext buffer, must be a multiple of 16 bytes 02002 * @param pPlainData Pointer to the plaintext buffer 02003 * @retval HAL status 02004 */ 02005 HAL_StatusTypeDef HAL_CRYP_AESECB_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData) 02006 { 02007 uint32_t tickstart = 0U; 02008 uint32_t inputaddr; 02009 uint32_t outputaddr; 02010 02011 if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS)) 02012 { 02013 /* Process Locked */ 02014 __HAL_LOCK(hcryp); 02015 02016 inputaddr = (uint32_t)pCypherData; 02017 outputaddr = (uint32_t)pPlainData; 02018 02019 /* Change the CRYP state */ 02020 hcryp->State = HAL_CRYP_STATE_BUSY; 02021 02022 /* Check if initialization phase has already been performed */ 02023 if(hcryp->Phase == HAL_CRYP_PHASE_READY) 02024 { 02025 /* Set the key */ 02026 CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize); 02027 02028 /* Set the CRYP peripheral in AES Key mode */ 02029 __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_KEY | CRYP_CR_ALGODIR); 02030 02031 /* Enable CRYP */ 02032 __HAL_CRYP_ENABLE(hcryp); 02033 02034 /* Get tick */ 02035 tickstart = HAL_GetTick(); 02036 02037 while(HAL_IS_BIT_SET(hcryp->Instance->SR, CRYP_FLAG_BUSY)) 02038 { 02039 /* Check for the Timeout */ 02040 if((HAL_GetTick() - tickstart ) > CRYP_TIMEOUT_VALUE) 02041 { 02042 /* Change state */ 02043 hcryp->State = HAL_CRYP_STATE_TIMEOUT; 02044 02045 /* Process Unlocked */ 02046 __HAL_UNLOCK(hcryp); 02047 02048 return HAL_TIMEOUT; 02049 } 02050 } 02051 02052 /* Reset the ALGOMODE bits*/ 02053 CRYP->CR &= (uint32_t)(~CRYP_CR_ALGOMODE); 02054 02055 /* Set the CRYP peripheral in AES ECB decryption mode */ 02056 __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_ECB | CRYP_CR_ALGODIR); 02057 02058 /* Flush FIFO */ 02059 __HAL_CRYP_FIFO_FLUSH(hcryp); 02060 02061 /* Set the phase */ 02062 hcryp->Phase = HAL_CRYP_PHASE_PROCESS; 02063 } 02064 02065 /* Set the input and output addresses and start DMA transfer */ 02066 CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr); 02067 02068 /* Process Unlocked */ 02069 __HAL_UNLOCK(hcryp); 02070 02071 /* Return function status */ 02072 return HAL_OK; 02073 } 02074 else 02075 { 02076 return HAL_ERROR; 02077 } 02078 } 02079 02080 /** 02081 * @brief Initializes the CRYP peripheral in AES CBC encryption mode using DMA. 02082 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 02083 * the configuration information for CRYP module 02084 * @param pCypherData Pointer to the cyphertext buffer 02085 * @param Size Length of the plaintext buffer, must be a multiple of 16 bytes 02086 * @param pPlainData Pointer to the plaintext buffer 02087 * @retval HAL status 02088 */ 02089 HAL_StatusTypeDef HAL_CRYP_AESCBC_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData) 02090 { 02091 uint32_t tickstart = 0U; 02092 uint32_t inputaddr; 02093 uint32_t outputaddr; 02094 02095 if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS)) 02096 { 02097 /* Process Locked */ 02098 __HAL_LOCK(hcryp); 02099 02100 inputaddr = (uint32_t)pCypherData; 02101 outputaddr = (uint32_t)pPlainData; 02102 02103 /* Change the CRYP state */ 02104 hcryp->State = HAL_CRYP_STATE_BUSY; 02105 02106 /* Check if initialization phase has already been performed */ 02107 if(hcryp->Phase == HAL_CRYP_PHASE_READY) 02108 { 02109 /* Set the key */ 02110 CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize); 02111 02112 /* Set the CRYP peripheral in AES Key mode */ 02113 __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_KEY | CRYP_CR_ALGODIR); 02114 02115 /* Enable CRYP */ 02116 __HAL_CRYP_ENABLE(hcryp); 02117 02118 /* Get tick */ 02119 tickstart = HAL_GetTick(); 02120 02121 while(HAL_IS_BIT_SET(hcryp->Instance->SR, CRYP_FLAG_BUSY)) 02122 { 02123 /* Check for the Timeout */ 02124 if((HAL_GetTick() - tickstart ) > CRYP_TIMEOUT_VALUE) 02125 { 02126 /* Change state */ 02127 hcryp->State = HAL_CRYP_STATE_TIMEOUT; 02128 02129 /* Process Unlocked */ 02130 __HAL_UNLOCK(hcryp); 02131 02132 return HAL_TIMEOUT; 02133 } 02134 } 02135 02136 /* Reset the ALGOMODE bits*/ 02137 CRYP->CR &= (uint32_t)(~CRYP_CR_ALGOMODE); 02138 02139 /* Set the CRYP peripheral in AES CBC decryption mode */ 02140 __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CBC | CRYP_CR_ALGODIR); 02141 02142 /* Set the Initialization Vector */ 02143 CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_128B); 02144 02145 /* Flush FIFO */ 02146 __HAL_CRYP_FIFO_FLUSH(hcryp); 02147 02148 /* Set the phase */ 02149 hcryp->Phase = HAL_CRYP_PHASE_PROCESS; 02150 } 02151 02152 /* Set the input and output addresses and start DMA transfer */ 02153 CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr); 02154 02155 /* Process Unlocked */ 02156 __HAL_UNLOCK(hcryp); 02157 02158 /* Return function status */ 02159 return HAL_OK; 02160 } 02161 else 02162 { 02163 return HAL_ERROR; 02164 } 02165 } 02166 02167 /** 02168 * @brief Initializes the CRYP peripheral in AES CTR decryption mode using DMA. 02169 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 02170 * the configuration information for CRYP module 02171 * @param pCypherData Pointer to the cyphertext buffer 02172 * @param Size Length of the plaintext buffer, must be a multiple of 16 02173 * @param pPlainData Pointer to the plaintext buffer 02174 * @retval HAL status 02175 */ 02176 HAL_StatusTypeDef HAL_CRYP_AESCTR_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData) 02177 { 02178 uint32_t inputaddr; 02179 uint32_t outputaddr; 02180 02181 if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS)) 02182 { 02183 /* Process Locked */ 02184 __HAL_LOCK(hcryp); 02185 02186 inputaddr = (uint32_t)pCypherData; 02187 outputaddr = (uint32_t)pPlainData; 02188 02189 /* Change the CRYP state */ 02190 hcryp->State = HAL_CRYP_STATE_BUSY; 02191 02192 /* Check if initialization phase has already been performed */ 02193 if(hcryp->Phase == HAL_CRYP_PHASE_READY) 02194 { 02195 /* Set the key */ 02196 CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize); 02197 02198 /* Set the CRYP peripheral in AES CTR mode */ 02199 __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CTR | CRYP_CR_ALGODIR); 02200 02201 /* Set the Initialization Vector */ 02202 CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect, CRYP_KEYSIZE_128B); 02203 02204 /* Flush FIFO */ 02205 __HAL_CRYP_FIFO_FLUSH(hcryp); 02206 02207 /* Set the phase */ 02208 hcryp->Phase = HAL_CRYP_PHASE_PROCESS; 02209 } 02210 02211 /* Set the input and output addresses and start DMA transfer */ 02212 CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr); 02213 02214 /* Process Unlocked */ 02215 __HAL_UNLOCK(hcryp); 02216 02217 /* Return function status */ 02218 return HAL_OK; 02219 } 02220 else 02221 { 02222 return HAL_ERROR; 02223 } 02224 } 02225 02226 02227 /** 02228 * @} 02229 */ 02230 02231 /** @defgroup CRYP_Exported_Functions_Group3 DES processing functions 02232 * @brief processing functions. 02233 * 02234 @verbatim 02235 ============================================================================== 02236 ##### DES processing functions ##### 02237 ============================================================================== 02238 [..] This section provides functions allowing to: 02239 (+) Encrypt plaintext using DES using ECB or CBC chaining modes 02240 (+) Decrypt cyphertext using ECB or CBC chaining modes 02241 [..] Three processing functions are available: 02242 (+) Polling mode 02243 (+) Interrupt mode 02244 (+) DMA mode 02245 02246 @endverbatim 02247 * @{ 02248 */ 02249 02250 /** 02251 * @brief Initializes the CRYP peripheral in DES ECB encryption mode. 02252 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 02253 * the configuration information for CRYP module 02254 * @param pPlainData Pointer to the plaintext buffer 02255 * @param Size Length of the plaintext buffer, must be a multiple of 8 02256 * @param pCypherData Pointer to the cyphertext buffer 02257 * @param Timeout Specify Timeout value 02258 * @retval HAL status 02259 */ 02260 HAL_StatusTypeDef HAL_CRYP_DESECB_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout) 02261 { 02262 /* Process Locked */ 02263 __HAL_LOCK(hcryp); 02264 02265 /* Change the CRYP state */ 02266 hcryp->State = HAL_CRYP_STATE_BUSY; 02267 02268 /* Set CRYP peripheral in DES ECB encryption mode */ 02269 CRYP_SetDESECBMode(hcryp, 0U); 02270 02271 /* Enable CRYP */ 02272 __HAL_CRYP_ENABLE(hcryp); 02273 02274 /* Write Plain Data and Get Cypher Data */ 02275 if(CRYP_ProcessData2Words(hcryp, pPlainData, Size, pCypherData, Timeout) != HAL_OK) 02276 { 02277 return HAL_TIMEOUT; 02278 } 02279 02280 /* Change the CRYP state */ 02281 hcryp->State = HAL_CRYP_STATE_READY; 02282 02283 /* Process Unlocked */ 02284 __HAL_UNLOCK(hcryp); 02285 02286 /* Return function status */ 02287 return HAL_OK; 02288 } 02289 02290 /** 02291 * @brief Initializes the CRYP peripheral in DES ECB decryption mode. 02292 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 02293 * the configuration information for CRYP module 02294 * @param pCypherData Pointer to the cyphertext buffer 02295 * @param Size Length of the plaintext buffer, must be a multiple of 8 02296 * @param pPlainData Pointer to the plaintext buffer 02297 * @param Timeout Specify Timeout value 02298 * @retval HAL status 02299 */ 02300 HAL_StatusTypeDef HAL_CRYP_DESECB_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout) 02301 { 02302 /* Process Locked */ 02303 __HAL_LOCK(hcryp); 02304 02305 /* Change the CRYP state */ 02306 hcryp->State = HAL_CRYP_STATE_BUSY; 02307 02308 /* Set CRYP peripheral in DES ECB decryption mode */ 02309 CRYP_SetDESECBMode(hcryp, CRYP_CR_ALGODIR); 02310 02311 /* Enable CRYP */ 02312 __HAL_CRYP_ENABLE(hcryp); 02313 02314 /* Write Plain Data and Get Cypher Data */ 02315 if(CRYP_ProcessData2Words(hcryp, pCypherData, Size, pPlainData, Timeout) != HAL_OK) 02316 { 02317 return HAL_TIMEOUT; 02318 } 02319 02320 /* Change the CRYP state */ 02321 hcryp->State = HAL_CRYP_STATE_READY; 02322 02323 /* Process Unlocked */ 02324 __HAL_UNLOCK(hcryp); 02325 02326 /* Return function status */ 02327 return HAL_OK; 02328 } 02329 02330 /** 02331 * @brief Initializes the CRYP peripheral in DES CBC encryption mode. 02332 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 02333 * the configuration information for CRYP module 02334 * @param pPlainData Pointer to the plaintext buffer 02335 * @param Size Length of the plaintext buffer, must be a multiple of 8 02336 * @param pCypherData Pointer to the cyphertext buffer 02337 * @param Timeout Specify Timeout value 02338 * @retval HAL status 02339 */ 02340 HAL_StatusTypeDef HAL_CRYP_DESCBC_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout) 02341 { 02342 /* Process Locked */ 02343 __HAL_LOCK(hcryp); 02344 02345 /* Change the CRYP state */ 02346 hcryp->State = HAL_CRYP_STATE_BUSY; 02347 02348 /* Set CRYP peripheral in DES CBC encryption mode */ 02349 CRYP_SetDESCBCMode(hcryp, 0U); 02350 02351 /* Enable CRYP */ 02352 __HAL_CRYP_ENABLE(hcryp); 02353 02354 /* Write Plain Data and Get Cypher Data */ 02355 if(CRYP_ProcessData2Words(hcryp, pPlainData, Size, pCypherData, Timeout) != HAL_OK) 02356 { 02357 return HAL_TIMEOUT; 02358 } 02359 02360 /* Change the CRYP state */ 02361 hcryp->State = HAL_CRYP_STATE_READY; 02362 02363 /* Process Unlocked */ 02364 __HAL_UNLOCK(hcryp); 02365 02366 /* Return function status */ 02367 return HAL_OK; 02368 } 02369 02370 /** 02371 * @brief Initializes the CRYP peripheral in DES ECB decryption mode. 02372 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 02373 * the configuration information for CRYP module 02374 * @param pCypherData Pointer to the cyphertext buffer 02375 * @param Size Length of the plaintext buffer, must be a multiple of 8 02376 * @param pPlainData Pointer to the plaintext buffer 02377 * @param Timeout Specify Timeout value 02378 * @retval HAL status 02379 */ 02380 HAL_StatusTypeDef HAL_CRYP_DESCBC_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout) 02381 { 02382 /* Process Locked */ 02383 __HAL_LOCK(hcryp); 02384 02385 /* Change the CRYP state */ 02386 hcryp->State = HAL_CRYP_STATE_BUSY; 02387 02388 /* Set CRYP peripheral in DES CBC decryption mode */ 02389 CRYP_SetDESCBCMode(hcryp, CRYP_CR_ALGODIR); 02390 02391 /* Enable CRYP */ 02392 __HAL_CRYP_ENABLE(hcryp); 02393 02394 /* Write Plain Data and Get Cypher Data */ 02395 if(CRYP_ProcessData2Words(hcryp, pCypherData, Size, pPlainData, Timeout) != HAL_OK) 02396 { 02397 return HAL_TIMEOUT; 02398 } 02399 02400 /* Change the CRYP state */ 02401 hcryp->State = HAL_CRYP_STATE_READY; 02402 02403 /* Process Unlocked */ 02404 __HAL_UNLOCK(hcryp); 02405 02406 /* Return function status */ 02407 return HAL_OK; 02408 } 02409 02410 /** 02411 * @brief Initializes the CRYP peripheral in DES ECB encryption mode using IT. 02412 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 02413 * the configuration information for CRYP module 02414 * @param pPlainData Pointer to the plaintext buffer 02415 * @param Size Length of the plaintext buffer, must be a multiple of 8 02416 * @param pCypherData Pointer to the cyphertext buffer 02417 * @retval HAL status 02418 */ 02419 HAL_StatusTypeDef HAL_CRYP_DESECB_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData) 02420 { 02421 uint32_t inputaddr; 02422 uint32_t outputaddr; 02423 02424 if(hcryp->State == HAL_CRYP_STATE_READY) 02425 { 02426 /* Process Locked */ 02427 __HAL_LOCK(hcryp); 02428 02429 hcryp->CrypInCount = Size; 02430 hcryp->pCrypInBuffPtr = pPlainData; 02431 hcryp->pCrypOutBuffPtr = pCypherData; 02432 hcryp->CrypOutCount = Size; 02433 02434 /* Change the CRYP state */ 02435 hcryp->State = HAL_CRYP_STATE_BUSY; 02436 02437 /* Set CRYP peripheral in DES ECB encryption mode */ 02438 CRYP_SetDESECBMode(hcryp, 0U); 02439 02440 /* Enable Interrupts */ 02441 __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI); 02442 02443 /* Enable CRYP */ 02444 __HAL_CRYP_ENABLE(hcryp); 02445 02446 /* Return function status */ 02447 return HAL_OK; 02448 } 02449 else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI)) 02450 { 02451 inputaddr = (uint32_t)hcryp->pCrypInBuffPtr; 02452 /* Write the Input block in the IN FIFO */ 02453 hcryp->Instance->DR = *(uint32_t*)(inputaddr); 02454 inputaddr+=4U; 02455 hcryp->Instance->DR = *(uint32_t*)(inputaddr); 02456 02457 hcryp->pCrypInBuffPtr += 8U; 02458 hcryp->CrypInCount -= 8U; 02459 if(hcryp->CrypInCount == 0U) 02460 { 02461 __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI); 02462 /* Call the Input data transfer complete callback */ 02463 HAL_CRYP_InCpltCallback(hcryp); 02464 } 02465 } 02466 else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI)) 02467 { 02468 outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr; 02469 /* Read the Output block from the Output FIFO */ 02470 *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; 02471 outputaddr+=4U; 02472 *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; 02473 02474 hcryp->pCrypOutBuffPtr += 8U; 02475 hcryp->CrypOutCount -= 8U; 02476 if(hcryp->CrypOutCount == 0U) 02477 { 02478 /* Disable IT */ 02479 __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI); 02480 /* Disable CRYP */ 02481 __HAL_CRYP_DISABLE(hcryp); 02482 /* Process Unlocked */ 02483 __HAL_UNLOCK(hcryp); 02484 /* Change the CRYP state */ 02485 hcryp->State = HAL_CRYP_STATE_READY; 02486 /* Call Input transfer complete callback */ 02487 HAL_CRYP_OutCpltCallback(hcryp); 02488 } 02489 } 02490 02491 /* Return function status */ 02492 return HAL_OK; 02493 } 02494 02495 /** 02496 * @brief Initializes the CRYP peripheral in DES CBC encryption mode using interrupt. 02497 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 02498 * the configuration information for CRYP module 02499 * @param pPlainData Pointer to the plaintext buffer 02500 * @param Size Length of the plaintext buffer, must be a multiple of 8 02501 * @param pCypherData Pointer to the cyphertext buffer 02502 * @retval HAL status 02503 */ 02504 HAL_StatusTypeDef HAL_CRYP_DESCBC_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData) 02505 { 02506 uint32_t inputaddr; 02507 uint32_t outputaddr; 02508 02509 if(hcryp->State == HAL_CRYP_STATE_READY) 02510 { 02511 /* Process Locked */ 02512 __HAL_LOCK(hcryp); 02513 02514 hcryp->CrypInCount = Size; 02515 hcryp->pCrypInBuffPtr = pPlainData; 02516 hcryp->pCrypOutBuffPtr = pCypherData; 02517 hcryp->CrypOutCount = Size; 02518 02519 /* Change the CRYP state */ 02520 hcryp->State = HAL_CRYP_STATE_BUSY; 02521 02522 /* Set CRYP peripheral in DES CBC encryption mode */ 02523 CRYP_SetDESCBCMode(hcryp, 0U); 02524 02525 /* Enable Interrupts */ 02526 __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI); 02527 02528 /* Enable CRYP */ 02529 __HAL_CRYP_ENABLE(hcryp); 02530 02531 /* Return function status */ 02532 return HAL_OK; 02533 } 02534 02535 else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI)) 02536 { 02537 inputaddr = (uint32_t)hcryp->pCrypInBuffPtr; 02538 /* Write the Input block in the IN FIFO */ 02539 hcryp->Instance->DR = *(uint32_t*)(inputaddr); 02540 inputaddr+=4U; 02541 hcryp->Instance->DR = *(uint32_t*)(inputaddr); 02542 02543 hcryp->pCrypInBuffPtr += 8U; 02544 hcryp->CrypInCount -= 8U; 02545 if(hcryp->CrypInCount == 0U) 02546 { 02547 __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI); 02548 /* Call the Input data transfer complete callback */ 02549 HAL_CRYP_InCpltCallback(hcryp); 02550 } 02551 } 02552 else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI)) 02553 { 02554 outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr; 02555 /* Read the Output block from the Output FIFO */ 02556 *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; 02557 outputaddr+=4U; 02558 *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; 02559 02560 hcryp->pCrypOutBuffPtr += 8U; 02561 hcryp->CrypOutCount -= 8U; 02562 if(hcryp->CrypOutCount == 0U) 02563 { 02564 /* Disable IT */ 02565 __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI); 02566 /* Disable CRYP */ 02567 __HAL_CRYP_DISABLE(hcryp); 02568 /* Process Unlocked */ 02569 __HAL_UNLOCK(hcryp); 02570 /* Change the CRYP state */ 02571 hcryp->State = HAL_CRYP_STATE_READY; 02572 /* Call Input transfer complete callback */ 02573 HAL_CRYP_OutCpltCallback(hcryp); 02574 } 02575 } 02576 02577 /* Return function status */ 02578 return HAL_OK; 02579 } 02580 02581 /** 02582 * @brief Initializes the CRYP peripheral in DES ECB decryption mode using IT. 02583 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 02584 * the configuration information for CRYP module 02585 * @param pPlainData Pointer to the plaintext buffer 02586 * @param Size Length of the plaintext buffer, must be a multiple of 8 02587 * @param pCypherData Pointer to the cyphertext buffer 02588 * @retval HAL status 02589 */ 02590 HAL_StatusTypeDef HAL_CRYP_DESECB_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData) 02591 { 02592 uint32_t inputaddr; 02593 uint32_t outputaddr; 02594 02595 if(hcryp->State == HAL_CRYP_STATE_READY) 02596 { 02597 /* Process Locked */ 02598 __HAL_LOCK(hcryp); 02599 02600 hcryp->CrypInCount = Size; 02601 hcryp->pCrypInBuffPtr = pCypherData; 02602 hcryp->pCrypOutBuffPtr = pPlainData; 02603 hcryp->CrypOutCount = Size; 02604 02605 /* Change the CRYP state */ 02606 hcryp->State = HAL_CRYP_STATE_BUSY; 02607 02608 /* Set CRYP peripheral in DES ECB decryption mode */ 02609 CRYP_SetDESECBMode(hcryp, CRYP_CR_ALGODIR); 02610 02611 /* Enable Interrupts */ 02612 __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI); 02613 02614 /* Enable CRYP */ 02615 __HAL_CRYP_ENABLE(hcryp); 02616 02617 /* Return function status */ 02618 return HAL_OK; 02619 } 02620 else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI)) 02621 { 02622 inputaddr = (uint32_t)hcryp->pCrypInBuffPtr; 02623 /* Write the Input block in the IN FIFO */ 02624 hcryp->Instance->DR = *(uint32_t*)(inputaddr); 02625 inputaddr+=4U; 02626 hcryp->Instance->DR = *(uint32_t*)(inputaddr); 02627 02628 hcryp->pCrypInBuffPtr += 8U; 02629 hcryp->CrypInCount -= 8U; 02630 if(hcryp->CrypInCount == 0U) 02631 { 02632 __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI); 02633 /* Call the Input data transfer complete callback */ 02634 HAL_CRYP_InCpltCallback(hcryp); 02635 } 02636 } 02637 else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI)) 02638 { 02639 outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr; 02640 /* Read the Output block from the Output FIFO */ 02641 *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; 02642 outputaddr+=4U; 02643 *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; 02644 02645 hcryp->pCrypOutBuffPtr += 8U; 02646 hcryp->CrypOutCount -= 8U; 02647 if(hcryp->CrypOutCount == 0U) 02648 { 02649 /* Disable IT */ 02650 __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI); 02651 /* Disable CRYP */ 02652 __HAL_CRYP_DISABLE(hcryp); 02653 /* Process Unlocked */ 02654 __HAL_UNLOCK(hcryp); 02655 /* Change the CRYP state */ 02656 hcryp->State = HAL_CRYP_STATE_READY; 02657 /* Call Input transfer complete callback */ 02658 HAL_CRYP_OutCpltCallback(hcryp); 02659 } 02660 } 02661 02662 /* Return function status */ 02663 return HAL_OK; 02664 } 02665 02666 /** 02667 * @brief Initializes the CRYP peripheral in DES ECB decryption mode using interrupt. 02668 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 02669 * the configuration information for CRYP module 02670 * @param pPlainData Pointer to the plaintext buffer 02671 * @param Size Length of the plaintext buffer, must be a multiple of 8 02672 * @param pCypherData Pointer to the cyphertext buffer 02673 * @retval HAL status 02674 */ 02675 HAL_StatusTypeDef HAL_CRYP_DESCBC_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData) 02676 { 02677 uint32_t inputaddr; 02678 uint32_t outputaddr; 02679 02680 if(hcryp->State == HAL_CRYP_STATE_READY) 02681 { 02682 /* Process Locked */ 02683 __HAL_LOCK(hcryp); 02684 02685 hcryp->CrypInCount = Size; 02686 hcryp->pCrypInBuffPtr = pCypherData; 02687 hcryp->pCrypOutBuffPtr = pPlainData; 02688 hcryp->CrypOutCount = Size; 02689 02690 /* Change the CRYP state */ 02691 hcryp->State = HAL_CRYP_STATE_BUSY; 02692 02693 /* Set CRYP peripheral in DES CBC decryption mode */ 02694 CRYP_SetDESCBCMode(hcryp, CRYP_CR_ALGODIR); 02695 02696 /* Enable Interrupts */ 02697 __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI); 02698 02699 /* Enable CRYP */ 02700 __HAL_CRYP_ENABLE(hcryp); 02701 02702 /* Return function status */ 02703 return HAL_OK; 02704 } 02705 else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI)) 02706 { 02707 inputaddr = (uint32_t)hcryp->pCrypInBuffPtr; 02708 /* Write the Input block in the IN FIFO */ 02709 hcryp->Instance->DR = *(uint32_t*)(inputaddr); 02710 inputaddr+=4U; 02711 hcryp->Instance->DR = *(uint32_t*)(inputaddr); 02712 02713 hcryp->pCrypInBuffPtr += 8U; 02714 hcryp->CrypInCount -= 8U; 02715 if(hcryp->CrypInCount == 0U) 02716 { 02717 __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI); 02718 /* Call the Input data transfer complete callback */ 02719 HAL_CRYP_InCpltCallback(hcryp); 02720 } 02721 } 02722 else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI)) 02723 { 02724 outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr; 02725 /* Read the Output block from the Output FIFO */ 02726 *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; 02727 outputaddr+=4U; 02728 *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; 02729 02730 hcryp->pCrypOutBuffPtr += 8U; 02731 hcryp->CrypOutCount -= 8U; 02732 if(hcryp->CrypOutCount == 0U) 02733 { 02734 /* Disable IT */ 02735 __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI); 02736 /* Disable CRYP */ 02737 __HAL_CRYP_DISABLE(hcryp); 02738 /* Process Unlocked */ 02739 __HAL_UNLOCK(hcryp); 02740 /* Change the CRYP state */ 02741 hcryp->State = HAL_CRYP_STATE_READY; 02742 /* Call Input transfer complete callback */ 02743 HAL_CRYP_OutCpltCallback(hcryp); 02744 } 02745 } 02746 02747 /* Return function status */ 02748 return HAL_OK; 02749 } 02750 02751 /** 02752 * @brief Initializes the CRYP peripheral in DES ECB encryption mode using DMA. 02753 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 02754 * the configuration information for CRYP module 02755 * @param pPlainData Pointer to the plaintext buffer 02756 * @param Size Length of the plaintext buffer, must be a multiple of 8 02757 * @param pCypherData Pointer to the cyphertext buffer 02758 * @retval HAL status 02759 */ 02760 HAL_StatusTypeDef HAL_CRYP_DESECB_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData) 02761 { 02762 uint32_t inputaddr; 02763 uint32_t outputaddr; 02764 02765 if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS)) 02766 { 02767 /* Process Locked */ 02768 __HAL_LOCK(hcryp); 02769 02770 inputaddr = (uint32_t)pPlainData; 02771 outputaddr = (uint32_t)pCypherData; 02772 02773 /* Change the CRYP state */ 02774 hcryp->State = HAL_CRYP_STATE_BUSY; 02775 02776 /* Set CRYP peripheral in DES ECB encryption mode */ 02777 CRYP_SetDESECBMode(hcryp, 0U); 02778 02779 /* Set the input and output addresses and start DMA transfer */ 02780 CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr); 02781 02782 /* Process Unlocked */ 02783 __HAL_UNLOCK(hcryp); 02784 02785 /* Return function status */ 02786 return HAL_OK; 02787 } 02788 else 02789 { 02790 return HAL_ERROR; 02791 } 02792 } 02793 02794 /** 02795 * @brief Initializes the CRYP peripheral in DES CBC encryption mode using DMA. 02796 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 02797 * the configuration information for CRYP module 02798 * @param pPlainData Pointer to the plaintext buffer 02799 * @param Size Length of the plaintext buffer, must be a multiple of 8 02800 * @param pCypherData Pointer to the cyphertext buffer 02801 * @retval HAL status 02802 */ 02803 HAL_StatusTypeDef HAL_CRYP_DESCBC_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData) 02804 { 02805 uint32_t inputaddr; 02806 uint32_t outputaddr; 02807 02808 if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS)) 02809 { 02810 /* Process Locked */ 02811 __HAL_LOCK(hcryp); 02812 02813 inputaddr = (uint32_t)pPlainData; 02814 outputaddr = (uint32_t)pCypherData; 02815 02816 /* Change the CRYP state */ 02817 hcryp->State = HAL_CRYP_STATE_BUSY; 02818 02819 /* Set CRYP peripheral in DES CBC encryption mode */ 02820 CRYP_SetDESCBCMode(hcryp, 0U); 02821 02822 /* Set the input and output addresses and start DMA transfer */ 02823 CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr); 02824 02825 /* Process Unlocked */ 02826 __HAL_UNLOCK(hcryp); 02827 02828 /* Return function status */ 02829 return HAL_OK; 02830 } 02831 else 02832 { 02833 return HAL_ERROR; 02834 } 02835 } 02836 02837 /** 02838 * @brief Initializes the CRYP peripheral in DES ECB decryption mode using DMA. 02839 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 02840 * the configuration information for CRYP module 02841 * @param pPlainData Pointer to the plaintext buffer 02842 * @param Size Length of the plaintext buffer, must be a multiple of 8 02843 * @param pCypherData Pointer to the cyphertext buffer 02844 * @retval HAL status 02845 */ 02846 HAL_StatusTypeDef HAL_CRYP_DESECB_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData) 02847 { 02848 uint32_t inputaddr; 02849 uint32_t outputaddr; 02850 02851 if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS)) 02852 { 02853 /* Process Locked */ 02854 __HAL_LOCK(hcryp); 02855 02856 inputaddr = (uint32_t)pCypherData; 02857 outputaddr = (uint32_t)pPlainData; 02858 02859 /* Change the CRYP state */ 02860 hcryp->State = HAL_CRYP_STATE_BUSY; 02861 02862 /* Set CRYP peripheral in DES ECB decryption mode */ 02863 CRYP_SetDESECBMode(hcryp, CRYP_CR_ALGODIR); 02864 02865 /* Set the input and output addresses and start DMA transfer */ 02866 CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr); 02867 02868 /* Process Unlocked */ 02869 __HAL_UNLOCK(hcryp); 02870 02871 /* Return function status */ 02872 return HAL_OK; 02873 } 02874 else 02875 { 02876 return HAL_ERROR; 02877 } 02878 } 02879 02880 /** 02881 * @brief Initializes the CRYP peripheral in DES ECB decryption mode using DMA. 02882 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 02883 * the configuration information for CRYP module 02884 * @param pPlainData Pointer to the plaintext buffer 02885 * @param Size Length of the plaintext buffer, must be a multiple of 8 02886 * @param pCypherData Pointer to the cyphertext buffer 02887 * @retval HAL status 02888 */ 02889 HAL_StatusTypeDef HAL_CRYP_DESCBC_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData) 02890 { 02891 uint32_t inputaddr; 02892 uint32_t outputaddr; 02893 02894 if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS)) 02895 { 02896 /* Process Locked */ 02897 __HAL_LOCK(hcryp); 02898 02899 inputaddr = (uint32_t)pCypherData; 02900 outputaddr = (uint32_t)pPlainData; 02901 02902 /* Change the CRYP state */ 02903 hcryp->State = HAL_CRYP_STATE_BUSY; 02904 02905 /* Set CRYP peripheral in DES CBC decryption mode */ 02906 CRYP_SetDESCBCMode(hcryp, CRYP_CR_ALGODIR); 02907 02908 /* Set the input and output addresses and start DMA transfer */ 02909 CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr); 02910 02911 /* Process Unlocked */ 02912 __HAL_UNLOCK(hcryp); 02913 02914 /* Return function status */ 02915 return HAL_OK; 02916 } 02917 else 02918 { 02919 return HAL_ERROR; 02920 } 02921 } 02922 02923 /** 02924 * @} 02925 */ 02926 02927 /** @defgroup CRYP_Exported_Functions_Group4 TDES processing functions 02928 * @brief processing functions. 02929 * 02930 @verbatim 02931 ============================================================================== 02932 ##### TDES processing functions ##### 02933 ============================================================================== 02934 [..] This section provides functions allowing to: 02935 (+) Encrypt plaintext using TDES based on ECB or CBC chaining modes 02936 (+) Decrypt cyphertext using TDES based on ECB or CBC chaining modes 02937 [..] Three processing functions are available: 02938 (+) Polling mode 02939 (+) Interrupt mode 02940 (+) DMA mode 02941 02942 @endverbatim 02943 * @{ 02944 */ 02945 02946 /** 02947 * @brief Initializes the CRYP peripheral in TDES ECB encryption mode 02948 * then encrypt pPlainData. The cypher data are available in pCypherData 02949 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 02950 * the configuration information for CRYP module 02951 * @param pPlainData Pointer to the plaintext buffer 02952 * @param Size Length of the plaintext buffer, must be a multiple of 8 02953 * @param pCypherData Pointer to the cyphertext buffer 02954 * @param Timeout Specify Timeout value 02955 * @retval HAL status 02956 */ 02957 HAL_StatusTypeDef HAL_CRYP_TDESECB_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout) 02958 { 02959 /* Process Locked */ 02960 __HAL_LOCK(hcryp); 02961 02962 /* Change the CRYP state */ 02963 hcryp->State = HAL_CRYP_STATE_BUSY; 02964 02965 /* Set CRYP peripheral in TDES ECB encryption mode */ 02966 CRYP_SetTDESECBMode(hcryp, 0U); 02967 02968 /* Enable CRYP */ 02969 __HAL_CRYP_ENABLE(hcryp); 02970 02971 /* Write Plain Data and Get Cypher Data */ 02972 if(CRYP_ProcessData2Words(hcryp, pPlainData, Size, pCypherData, Timeout) != HAL_OK) 02973 { 02974 return HAL_TIMEOUT; 02975 } 02976 02977 /* Change the CRYP state */ 02978 hcryp->State = HAL_CRYP_STATE_READY; 02979 02980 /* Process Unlocked */ 02981 __HAL_UNLOCK(hcryp); 02982 02983 /* Return function status */ 02984 return HAL_OK; 02985 } 02986 02987 /** 02988 * @brief Initializes the CRYP peripheral in TDES ECB decryption mode 02989 * then decrypted pCypherData. The cypher data are available in pPlainData 02990 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 02991 * the configuration information for CRYP module 02992 * @param pPlainData Pointer to the plaintext buffer 02993 * @param Size Length of the plaintext buffer, must be a multiple of 8 02994 * @param pCypherData Pointer to the cyphertext buffer 02995 * @param Timeout Specify Timeout value 02996 * @retval HAL status 02997 */ 02998 HAL_StatusTypeDef HAL_CRYP_TDESECB_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout) 02999 { 03000 /* Process Locked */ 03001 __HAL_LOCK(hcryp); 03002 03003 /* Change the CRYP state */ 03004 hcryp->State = HAL_CRYP_STATE_BUSY; 03005 03006 /* Set CRYP peripheral in TDES ECB decryption mode */ 03007 CRYP_SetTDESECBMode(hcryp, CRYP_CR_ALGODIR); 03008 03009 /* Enable CRYP */ 03010 __HAL_CRYP_ENABLE(hcryp); 03011 03012 /* Write Cypher Data and Get Plain Data */ 03013 if(CRYP_ProcessData2Words(hcryp, pCypherData, Size, pPlainData, Timeout) != HAL_OK) 03014 { 03015 return HAL_TIMEOUT; 03016 } 03017 03018 /* Change the CRYP state */ 03019 hcryp->State = HAL_CRYP_STATE_READY; 03020 03021 /* Process Unlocked */ 03022 __HAL_UNLOCK(hcryp); 03023 03024 /* Return function status */ 03025 return HAL_OK; 03026 } 03027 03028 /** 03029 * @brief Initializes the CRYP peripheral in TDES CBC encryption mode 03030 * then encrypt pPlainData. The cypher data are available in pCypherData 03031 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 03032 * the configuration information for CRYP module 03033 * @param pPlainData Pointer to the plaintext buffer 03034 * @param Size Length of the plaintext buffer, must be a multiple of 8 03035 * @param pCypherData Pointer to the cyphertext buffer 03036 * @param Timeout Specify Timeout value 03037 * @retval HAL status 03038 */ 03039 HAL_StatusTypeDef HAL_CRYP_TDESCBC_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout) 03040 { 03041 /* Process Locked */ 03042 __HAL_LOCK(hcryp); 03043 03044 /* Change the CRYP state */ 03045 hcryp->State = HAL_CRYP_STATE_BUSY; 03046 03047 /* Set CRYP peripheral in TDES CBC encryption mode */ 03048 CRYP_SetTDESCBCMode(hcryp, 0U); 03049 03050 /* Enable CRYP */ 03051 __HAL_CRYP_ENABLE(hcryp); 03052 03053 /* Write Plain Data and Get Cypher Data */ 03054 if(CRYP_ProcessData2Words(hcryp, pPlainData, Size, pCypherData, Timeout) != HAL_OK) 03055 { 03056 return HAL_TIMEOUT; 03057 } 03058 03059 /* Change the CRYP state */ 03060 hcryp->State = HAL_CRYP_STATE_READY; 03061 03062 /* Process Unlocked */ 03063 __HAL_UNLOCK(hcryp); 03064 03065 /* Return function status */ 03066 return HAL_OK; 03067 } 03068 03069 /** 03070 * @brief Initializes the CRYP peripheral in TDES CBC decryption mode 03071 * then decrypted pCypherData. The cypher data are available in pPlainData 03072 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 03073 * the configuration information for CRYP module 03074 * @param pCypherData Pointer to the cyphertext buffer 03075 * @param Size Length of the plaintext buffer, must be a multiple of 8 03076 * @param pPlainData Pointer to the plaintext buffer 03077 * @param Timeout Specify Timeout value 03078 * @retval HAL status 03079 */ 03080 HAL_StatusTypeDef HAL_CRYP_TDESCBC_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout) 03081 { 03082 /* Process Locked */ 03083 __HAL_LOCK(hcryp); 03084 03085 /* Change the CRYP state */ 03086 hcryp->State = HAL_CRYP_STATE_BUSY; 03087 03088 /* Set CRYP peripheral in TDES CBC decryption mode */ 03089 CRYP_SetTDESCBCMode(hcryp, CRYP_CR_ALGODIR); 03090 03091 /* Enable CRYP */ 03092 __HAL_CRYP_ENABLE(hcryp); 03093 03094 /* Write Cypher Data and Get Plain Data */ 03095 if(CRYP_ProcessData2Words(hcryp, pCypherData, Size, pPlainData, Timeout) != HAL_OK) 03096 { 03097 return HAL_TIMEOUT; 03098 } 03099 03100 /* Change the CRYP state */ 03101 hcryp->State = HAL_CRYP_STATE_READY; 03102 03103 /* Process Unlocked */ 03104 __HAL_UNLOCK(hcryp); 03105 03106 /* Return function status */ 03107 return HAL_OK; 03108 } 03109 03110 /** 03111 * @brief Initializes the CRYP peripheral in TDES ECB encryption mode using interrupt. 03112 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 03113 * the configuration information for CRYP module 03114 * @param pPlainData Pointer to the plaintext buffer 03115 * @param Size Length of the plaintext buffer, must be a multiple of 8 03116 * @param pCypherData Pointer to the cyphertext buffer 03117 * @retval HAL status 03118 */ 03119 HAL_StatusTypeDef HAL_CRYP_TDESECB_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData) 03120 { 03121 uint32_t inputaddr; 03122 uint32_t outputaddr; 03123 03124 if(hcryp->State == HAL_CRYP_STATE_READY) 03125 { 03126 /* Process Locked */ 03127 __HAL_LOCK(hcryp); 03128 03129 hcryp->CrypInCount = Size; 03130 hcryp->pCrypInBuffPtr = pPlainData; 03131 hcryp->pCrypOutBuffPtr = pCypherData; 03132 hcryp->CrypOutCount = Size; 03133 03134 /* Change the CRYP state */ 03135 hcryp->State = HAL_CRYP_STATE_BUSY; 03136 03137 /* Set CRYP peripheral in TDES ECB encryption mode */ 03138 CRYP_SetTDESECBMode(hcryp, 0U); 03139 03140 /* Enable Interrupts */ 03141 __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI); 03142 03143 /* Enable CRYP */ 03144 __HAL_CRYP_ENABLE(hcryp); 03145 03146 /* Return function status */ 03147 return HAL_OK; 03148 } 03149 else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI)) 03150 { 03151 inputaddr = (uint32_t)hcryp->pCrypInBuffPtr; 03152 /* Write the Input block in the IN FIFO */ 03153 hcryp->Instance->DR = *(uint32_t*)(inputaddr); 03154 inputaddr+=4U; 03155 hcryp->Instance->DR = *(uint32_t*)(inputaddr); 03156 03157 hcryp->pCrypInBuffPtr += 8U; 03158 hcryp->CrypInCount -= 8U; 03159 if(hcryp->CrypInCount == 0U) 03160 { 03161 __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI); 03162 /* Call the Input data transfer complete callback */ 03163 HAL_CRYP_InCpltCallback(hcryp); 03164 } 03165 } 03166 else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI)) 03167 { 03168 outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr; 03169 /* Read the Output block from the Output FIFO */ 03170 *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; 03171 outputaddr+=4U; 03172 *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; 03173 03174 hcryp->pCrypOutBuffPtr += 8U; 03175 hcryp->CrypOutCount -= 8U; 03176 if(hcryp->CrypOutCount == 0U) 03177 { 03178 /* Disable IT */ 03179 __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI); 03180 /* Disable CRYP */ 03181 __HAL_CRYP_DISABLE(hcryp); 03182 /* Process Unlocked */ 03183 __HAL_UNLOCK(hcryp); 03184 /* Change the CRYP state */ 03185 hcryp->State = HAL_CRYP_STATE_READY; 03186 /* Call the Output data transfer complete callback */ 03187 HAL_CRYP_OutCpltCallback(hcryp); 03188 } 03189 } 03190 03191 /* Return function status */ 03192 return HAL_OK; 03193 } 03194 03195 /** 03196 * @brief Initializes the CRYP peripheral in TDES CBC encryption mode. 03197 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 03198 * the configuration information for CRYP module 03199 * @param pPlainData Pointer to the plaintext buffer 03200 * @param Size Length of the plaintext buffer, must be a multiple of 8 03201 * @param pCypherData Pointer to the cyphertext buffer 03202 * @retval HAL status 03203 */ 03204 HAL_StatusTypeDef HAL_CRYP_TDESCBC_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData) 03205 { 03206 uint32_t inputaddr; 03207 uint32_t outputaddr; 03208 03209 if(hcryp->State == HAL_CRYP_STATE_READY) 03210 { 03211 /* Process Locked */ 03212 __HAL_LOCK(hcryp); 03213 03214 hcryp->CrypInCount = Size; 03215 hcryp->pCrypInBuffPtr = pPlainData; 03216 hcryp->pCrypOutBuffPtr = pCypherData; 03217 hcryp->CrypOutCount = Size; 03218 03219 /* Change the CRYP state */ 03220 hcryp->State = HAL_CRYP_STATE_BUSY; 03221 03222 /* Set CRYP peripheral in TDES CBC encryption mode */ 03223 CRYP_SetTDESCBCMode(hcryp, 0U); 03224 03225 /* Enable Interrupts */ 03226 __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI); 03227 03228 /* Enable CRYP */ 03229 __HAL_CRYP_ENABLE(hcryp); 03230 03231 /* Return function status */ 03232 return HAL_OK; 03233 } 03234 else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI)) 03235 { 03236 inputaddr = (uint32_t)hcryp->pCrypInBuffPtr; 03237 /* Write the Input block in the IN FIFO */ 03238 hcryp->Instance->DR = *(uint32_t*)(inputaddr); 03239 inputaddr+=4U; 03240 hcryp->Instance->DR = *(uint32_t*)(inputaddr); 03241 03242 hcryp->pCrypInBuffPtr += 8U; 03243 hcryp->CrypInCount -= 8U; 03244 if(hcryp->CrypInCount == 0U) 03245 { 03246 __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI); 03247 /* Call the Input data transfer complete callback */ 03248 HAL_CRYP_InCpltCallback(hcryp); 03249 } 03250 } 03251 else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI)) 03252 { 03253 outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr; 03254 /* Read the Output block from the Output FIFO */ 03255 *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; 03256 outputaddr+=4U; 03257 *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; 03258 03259 hcryp->pCrypOutBuffPtr += 8U; 03260 hcryp->CrypOutCount -= 8U; 03261 if(hcryp->CrypOutCount == 0U) 03262 { 03263 __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI); 03264 /* Disable CRYP */ 03265 __HAL_CRYP_DISABLE(hcryp); 03266 /* Process Unlocked */ 03267 __HAL_UNLOCK(hcryp); 03268 /* Change the CRYP state */ 03269 hcryp->State = HAL_CRYP_STATE_READY; 03270 /* Call Input transfer complete callback */ 03271 HAL_CRYP_OutCpltCallback(hcryp); 03272 } 03273 } 03274 03275 /* Return function status */ 03276 return HAL_OK; 03277 } 03278 03279 /** 03280 * @brief Initializes the CRYP peripheral in TDES ECB decryption mode. 03281 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 03282 * the configuration information for CRYP module 03283 * @param pPlainData Pointer to the plaintext buffer 03284 * @param Size Length of the plaintext buffer, must be a multiple of 8 03285 * @param pCypherData Pointer to the cyphertext buffer 03286 * @retval HAL status 03287 */ 03288 HAL_StatusTypeDef HAL_CRYP_TDESECB_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData) 03289 { 03290 uint32_t inputaddr; 03291 uint32_t outputaddr; 03292 03293 if(hcryp->State == HAL_CRYP_STATE_READY) 03294 { 03295 /* Process Locked */ 03296 __HAL_LOCK(hcryp); 03297 03298 hcryp->CrypInCount = Size; 03299 hcryp->pCrypInBuffPtr = pCypherData; 03300 hcryp->pCrypOutBuffPtr = pPlainData; 03301 hcryp->CrypOutCount = Size; 03302 03303 /* Change the CRYP state */ 03304 hcryp->State = HAL_CRYP_STATE_BUSY; 03305 03306 /* Set CRYP peripheral in TDES ECB decryption mode */ 03307 CRYP_SetTDESECBMode(hcryp, CRYP_CR_ALGODIR); 03308 03309 /* Enable Interrupts */ 03310 __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI); 03311 03312 /* Enable CRYP */ 03313 __HAL_CRYP_ENABLE(hcryp); 03314 03315 /* Return function status */ 03316 return HAL_OK; 03317 } 03318 else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI)) 03319 { 03320 inputaddr = (uint32_t)hcryp->pCrypInBuffPtr; 03321 /* Write the Input block in the IN FIFO */ 03322 hcryp->Instance->DR = *(uint32_t*)(inputaddr); 03323 inputaddr+=4U; 03324 hcryp->Instance->DR = *(uint32_t*)(inputaddr); 03325 03326 hcryp->pCrypInBuffPtr += 8U; 03327 hcryp->CrypInCount -= 8U; 03328 if(hcryp->CrypInCount == 0U) 03329 { 03330 __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI); 03331 /* Call the Input data transfer complete callback */ 03332 HAL_CRYP_InCpltCallback(hcryp); 03333 } 03334 } 03335 else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI)) 03336 { 03337 outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr; 03338 /* Read the Output block from the Output FIFO */ 03339 *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; 03340 outputaddr+=4U; 03341 *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; 03342 03343 hcryp->pCrypOutBuffPtr += 8U; 03344 hcryp->CrypOutCount -= 8U; 03345 if(hcryp->CrypOutCount == 0U) 03346 { 03347 __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI); 03348 /* Disable CRYP */ 03349 __HAL_CRYP_DISABLE(hcryp); 03350 /* Process Unlocked */ 03351 __HAL_UNLOCK(hcryp); 03352 /* Change the CRYP state */ 03353 hcryp->State = HAL_CRYP_STATE_READY; 03354 /* Call Input transfer complete callback */ 03355 HAL_CRYP_OutCpltCallback(hcryp); 03356 } 03357 } 03358 03359 /* Return function status */ 03360 return HAL_OK; 03361 } 03362 03363 /** 03364 * @brief Initializes the CRYP peripheral in TDES CBC decryption mode. 03365 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 03366 * the configuration information for CRYP module 03367 * @param pCypherData Pointer to the cyphertext buffer 03368 * @param Size Length of the plaintext buffer, must be a multiple of 8 03369 * @param pPlainData Pointer to the plaintext buffer 03370 * @retval HAL status 03371 */ 03372 HAL_StatusTypeDef HAL_CRYP_TDESCBC_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData) 03373 { 03374 uint32_t inputaddr; 03375 uint32_t outputaddr; 03376 03377 if(hcryp->State == HAL_CRYP_STATE_READY) 03378 { 03379 /* Process Locked */ 03380 __HAL_LOCK(hcryp); 03381 03382 hcryp->CrypInCount = Size; 03383 hcryp->pCrypInBuffPtr = pCypherData; 03384 hcryp->pCrypOutBuffPtr = pPlainData; 03385 hcryp->CrypOutCount = Size; 03386 03387 /* Change the CRYP state */ 03388 hcryp->State = HAL_CRYP_STATE_BUSY; 03389 03390 /* Set CRYP peripheral in TDES CBC decryption mode */ 03391 CRYP_SetTDESCBCMode(hcryp, CRYP_CR_ALGODIR); 03392 03393 /* Enable Interrupts */ 03394 __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI); 03395 03396 /* Enable CRYP */ 03397 __HAL_CRYP_ENABLE(hcryp); 03398 03399 /* Return function status */ 03400 return HAL_OK; 03401 } 03402 else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI)) 03403 { 03404 inputaddr = (uint32_t)hcryp->pCrypInBuffPtr; 03405 /* Write the Input block in the IN FIFO */ 03406 hcryp->Instance->DR = *(uint32_t*)(inputaddr); 03407 inputaddr+=4U; 03408 hcryp->Instance->DR = *(uint32_t*)(inputaddr); 03409 03410 hcryp->pCrypInBuffPtr += 8U; 03411 hcryp->CrypInCount -= 8U; 03412 if(hcryp->CrypInCount == 0U) 03413 { 03414 __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI); 03415 /* Call the Input data transfer complete callback */ 03416 HAL_CRYP_InCpltCallback(hcryp); 03417 } 03418 } 03419 else if(__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI)) 03420 { 03421 outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr; 03422 /* Read the Output block from the Output FIFO */ 03423 *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; 03424 outputaddr+=4U; 03425 *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT; 03426 03427 hcryp->pCrypOutBuffPtr += 8U; 03428 hcryp->CrypOutCount -= 8U; 03429 if(hcryp->CrypOutCount == 0U) 03430 { 03431 __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI); 03432 /* Disable CRYP */ 03433 __HAL_CRYP_DISABLE(hcryp); 03434 /* Process Unlocked */ 03435 __HAL_UNLOCK(hcryp); 03436 /* Change the CRYP state */ 03437 hcryp->State = HAL_CRYP_STATE_READY; 03438 /* Call Input transfer complete callback */ 03439 HAL_CRYP_OutCpltCallback(hcryp); 03440 } 03441 } 03442 03443 /* Return function status */ 03444 return HAL_OK; 03445 } 03446 03447 /** 03448 * @brief Initializes the CRYP peripheral in TDES ECB encryption mode using DMA. 03449 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 03450 * the configuration information for CRYP module 03451 * @param pPlainData Pointer to the plaintext buffer 03452 * @param Size Length of the plaintext buffer, must be a multiple of 8 03453 * @param pCypherData Pointer to the cyphertext buffer 03454 * @retval HAL status 03455 */ 03456 HAL_StatusTypeDef HAL_CRYP_TDESECB_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData) 03457 { 03458 uint32_t inputaddr; 03459 uint32_t outputaddr; 03460 03461 if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS)) 03462 { 03463 /* Process Locked */ 03464 __HAL_LOCK(hcryp); 03465 03466 inputaddr = (uint32_t)pPlainData; 03467 outputaddr = (uint32_t)pCypherData; 03468 03469 /* Change the CRYP state */ 03470 hcryp->State = HAL_CRYP_STATE_BUSY; 03471 03472 /* Set CRYP peripheral in TDES ECB encryption mode */ 03473 CRYP_SetTDESECBMode(hcryp, 0U); 03474 03475 /* Set the input and output addresses and start DMA transfer */ 03476 CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr); 03477 03478 /* Process Unlocked */ 03479 __HAL_UNLOCK(hcryp); 03480 03481 /* Return function status */ 03482 return HAL_OK; 03483 } 03484 else 03485 { 03486 return HAL_ERROR; 03487 } 03488 } 03489 03490 /** 03491 * @brief Initializes the CRYP peripheral in TDES CBC encryption mode using DMA. 03492 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 03493 * the configuration information for CRYP module 03494 * @param pPlainData Pointer to the plaintext buffer 03495 * @param Size Length of the plaintext buffer, must be a multiple of 8 03496 * @param pCypherData Pointer to the cyphertext buffer 03497 * @retval HAL status 03498 */ 03499 HAL_StatusTypeDef HAL_CRYP_TDESCBC_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData) 03500 { 03501 uint32_t inputaddr; 03502 uint32_t outputaddr; 03503 03504 if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS)) 03505 { 03506 /* Process Locked */ 03507 __HAL_LOCK(hcryp); 03508 03509 inputaddr = (uint32_t)pPlainData; 03510 outputaddr = (uint32_t)pCypherData; 03511 03512 /* Change the CRYP state */ 03513 hcryp->State = HAL_CRYP_STATE_BUSY; 03514 03515 /* Set CRYP peripheral in TDES CBC encryption mode */ 03516 CRYP_SetTDESCBCMode(hcryp, 0U); 03517 03518 /* Set the input and output addresses and start DMA transfer */ 03519 CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr); 03520 03521 /* Process Unlocked */ 03522 __HAL_UNLOCK(hcryp); 03523 03524 /* Return function status */ 03525 return HAL_OK; 03526 } 03527 else 03528 { 03529 return HAL_ERROR; 03530 } 03531 } 03532 03533 /** 03534 * @brief Initializes the CRYP peripheral in TDES ECB decryption mode using DMA. 03535 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 03536 * the configuration information for CRYP module 03537 * @param pPlainData Pointer to the plaintext buffer 03538 * @param Size Length of the plaintext buffer, must be a multiple of 8 03539 * @param pCypherData Pointer to the cyphertext buffer 03540 * @retval HAL status 03541 */ 03542 HAL_StatusTypeDef HAL_CRYP_TDESECB_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData) 03543 { 03544 uint32_t inputaddr; 03545 uint32_t outputaddr; 03546 03547 if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS)) 03548 { 03549 /* Process Locked */ 03550 __HAL_LOCK(hcryp); 03551 03552 inputaddr = (uint32_t)pCypherData; 03553 outputaddr = (uint32_t)pPlainData; 03554 03555 /* Change the CRYP state */ 03556 hcryp->State = HAL_CRYP_STATE_BUSY; 03557 03558 /* Set CRYP peripheral in TDES ECB decryption mode */ 03559 CRYP_SetTDESECBMode(hcryp, CRYP_CR_ALGODIR); 03560 03561 /* Set the input and output addresses and start DMA transfer */ 03562 CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr); 03563 03564 /* Process Unlocked */ 03565 __HAL_UNLOCK(hcryp); 03566 03567 /* Return function status */ 03568 return HAL_OK; 03569 } 03570 else 03571 { 03572 return HAL_ERROR; 03573 } 03574 } 03575 03576 /** 03577 * @brief Initializes the CRYP peripheral in TDES CBC decryption mode using DMA. 03578 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 03579 * the configuration information for CRYP module 03580 * @param pCypherData Pointer to the cyphertext buffer 03581 * @param Size Length of the plaintext buffer, must be a multiple of 8 03582 * @param pPlainData Pointer to the plaintext buffer 03583 * @retval HAL status 03584 */ 03585 HAL_StatusTypeDef HAL_CRYP_TDESCBC_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData) 03586 { 03587 uint32_t inputaddr; 03588 uint32_t outputaddr; 03589 03590 if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS)) 03591 { 03592 /* Process Locked */ 03593 __HAL_LOCK(hcryp); 03594 03595 inputaddr = (uint32_t)pCypherData; 03596 outputaddr = (uint32_t)pPlainData; 03597 03598 /* Change the CRYP state */ 03599 hcryp->State = HAL_CRYP_STATE_BUSY; 03600 03601 /* Set CRYP peripheral in TDES CBC decryption mode */ 03602 CRYP_SetTDESCBCMode(hcryp, CRYP_CR_ALGODIR); 03603 03604 /* Set the input and output addresses and start DMA transfer */ 03605 CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr); 03606 03607 /* Process Unlocked */ 03608 __HAL_UNLOCK(hcryp); 03609 03610 /* Return function status */ 03611 return HAL_OK; 03612 } 03613 else 03614 { 03615 return HAL_ERROR; 03616 } 03617 } 03618 03619 /** 03620 * @} 03621 */ 03622 03623 /** @defgroup CRYP_Exported_Functions_Group5 DMA callback functions 03624 * @brief DMA callback functions. 03625 * 03626 @verbatim 03627 ============================================================================== 03628 ##### DMA callback functions ##### 03629 ============================================================================== 03630 [..] This section provides DMA callback functions: 03631 (+) DMA Input data transfer complete 03632 (+) DMA Output data transfer complete 03633 (+) DMA error 03634 03635 @endverbatim 03636 * @{ 03637 */ 03638 03639 /** 03640 * @brief Input FIFO transfer completed callbacks. 03641 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 03642 * the configuration information for CRYP module 03643 * @retval None 03644 */ 03645 __weak void HAL_CRYP_InCpltCallback(CRYP_HandleTypeDef *hcryp) 03646 { 03647 /* Prevent unused argument(s) compilation warning */ 03648 UNUSED(hcryp); 03649 /* NOTE : This function Should not be modified, when the callback is needed, 03650 the HAL_CRYP_InCpltCallback could be implemented in the user file 03651 */ 03652 } 03653 03654 /** 03655 * @brief Output FIFO transfer completed callbacks. 03656 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 03657 * the configuration information for CRYP module 03658 * @retval None 03659 */ 03660 __weak void HAL_CRYP_OutCpltCallback(CRYP_HandleTypeDef *hcryp) 03661 { 03662 /* Prevent unused argument(s) compilation warning */ 03663 UNUSED(hcryp); 03664 /* NOTE : This function Should not be modified, when the callback is needed, 03665 the HAL_CRYP_OutCpltCallback could be implemented in the user file 03666 */ 03667 } 03668 03669 /** 03670 * @brief CRYP error callbacks. 03671 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 03672 * the configuration information for CRYP module 03673 * @retval None 03674 */ 03675 __weak void HAL_CRYP_ErrorCallback(CRYP_HandleTypeDef *hcryp) 03676 { 03677 /* Prevent unused argument(s) compilation warning */ 03678 UNUSED(hcryp); 03679 /* NOTE : This function Should not be modified, when the callback is needed, 03680 the HAL_CRYP_ErrorCallback could be implemented in the user file 03681 */ 03682 } 03683 03684 /** 03685 * @} 03686 */ 03687 03688 /** @defgroup CRYP_Exported_Functions_Group6 CRYP IRQ handler management 03689 * @brief CRYP IRQ handler. 03690 * 03691 @verbatim 03692 ============================================================================== 03693 ##### CRYP IRQ handler management ##### 03694 ============================================================================== 03695 [..] This section provides CRYP IRQ handler function. 03696 03697 @endverbatim 03698 * @{ 03699 */ 03700 03701 /** 03702 * @brief This function handles CRYP interrupt request. 03703 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 03704 * the configuration information for CRYP module 03705 * @retval None 03706 */ 03707 void HAL_CRYP_IRQHandler(CRYP_HandleTypeDef *hcryp) 03708 { 03709 switch(CRYP->CR & CRYP_CR_ALGOMODE_DIRECTION) 03710 { 03711 case CRYP_CR_ALGOMODE_TDES_ECB_ENCRYPT: 03712 HAL_CRYP_TDESECB_Encrypt_IT(hcryp, NULL, 0U, NULL); 03713 break; 03714 03715 case CRYP_CR_ALGOMODE_TDES_ECB_DECRYPT: 03716 HAL_CRYP_TDESECB_Decrypt_IT(hcryp, NULL, 0U, NULL); 03717 break; 03718 03719 case CRYP_CR_ALGOMODE_TDES_CBC_ENCRYPT: 03720 HAL_CRYP_TDESCBC_Encrypt_IT(hcryp, NULL, 0U, NULL); 03721 break; 03722 03723 case CRYP_CR_ALGOMODE_TDES_CBC_DECRYPT: 03724 HAL_CRYP_TDESCBC_Decrypt_IT(hcryp, NULL, 0U, NULL); 03725 break; 03726 03727 case CRYP_CR_ALGOMODE_DES_ECB_ENCRYPT: 03728 HAL_CRYP_DESECB_Encrypt_IT(hcryp, NULL, 0U, NULL); 03729 break; 03730 03731 case CRYP_CR_ALGOMODE_DES_ECB_DECRYPT: 03732 HAL_CRYP_DESECB_Decrypt_IT(hcryp, NULL, 0U, NULL); 03733 break; 03734 03735 case CRYP_CR_ALGOMODE_DES_CBC_ENCRYPT: 03736 HAL_CRYP_DESCBC_Encrypt_IT(hcryp, NULL, 0U, NULL); 03737 break; 03738 03739 case CRYP_CR_ALGOMODE_DES_CBC_DECRYPT: 03740 HAL_CRYP_DESCBC_Decrypt_IT(hcryp, NULL, 0U, NULL); 03741 break; 03742 03743 case CRYP_CR_ALGOMODE_AES_ECB_ENCRYPT: 03744 HAL_CRYP_AESECB_Encrypt_IT(hcryp, NULL, 0U, NULL); 03745 break; 03746 03747 case CRYP_CR_ALGOMODE_AES_ECB_DECRYPT: 03748 HAL_CRYP_AESECB_Decrypt_IT(hcryp, NULL, 0U, NULL); 03749 break; 03750 03751 case CRYP_CR_ALGOMODE_AES_CBC_ENCRYPT: 03752 HAL_CRYP_AESCBC_Encrypt_IT(hcryp, NULL, 0U, NULL); 03753 break; 03754 03755 case CRYP_CR_ALGOMODE_AES_CBC_DECRYPT: 03756 HAL_CRYP_AESCBC_Decrypt_IT(hcryp, NULL, 0U, NULL); 03757 break; 03758 03759 case CRYP_CR_ALGOMODE_AES_CTR_ENCRYPT: 03760 HAL_CRYP_AESCTR_Encrypt_IT(hcryp, NULL, 0U, NULL); 03761 break; 03762 03763 case CRYP_CR_ALGOMODE_AES_CTR_DECRYPT: 03764 HAL_CRYP_AESCTR_Decrypt_IT(hcryp, NULL, 0U, NULL); 03765 break; 03766 03767 default: 03768 break; 03769 } 03770 } 03771 03772 /** 03773 * @} 03774 */ 03775 03776 /** @defgroup CRYP_Exported_Functions_Group7 Peripheral State functions 03777 * @brief Peripheral State functions. 03778 * 03779 @verbatim 03780 ============================================================================== 03781 ##### Peripheral State functions ##### 03782 ============================================================================== 03783 [..] 03784 This subsection permits to get in run-time the status of the peripheral. 03785 03786 @endverbatim 03787 * @{ 03788 */ 03789 03790 /** 03791 * @brief Returns the CRYP state. 03792 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 03793 * the configuration information for CRYP module 03794 * @retval HAL state 03795 */ 03796 HAL_CRYP_STATETypeDef HAL_CRYP_GetState(CRYP_HandleTypeDef *hcryp) 03797 { 03798 return hcryp->State; 03799 } 03800 03801 /** 03802 * @} 03803 */ 03804 03805 03806 /** 03807 * @} 03808 */ 03809 03810 #endif /* CRYP */ 03811 03812 #if defined (AES) 03813 03814 /** @defgroup AES AES 03815 * @brief AES HAL module driver. 03816 * @{ 03817 */ 03818 03819 /* Private typedef -----------------------------------------------------------*/ 03820 /* Private define ------------------------------------------------------------*/ 03821 /* Private macro -------------------------------------------------------------*/ 03822 /* Private variables ---------------------------------------------------------*/ 03823 /* Private functions --------------------------------------------------------*/ 03824 03825 /** @defgroup CRYP_Private_Functions CRYP Private Functions 03826 * @{ 03827 */ 03828 03829 static HAL_StatusTypeDef CRYP_SetInitVector(CRYP_HandleTypeDef *hcryp); 03830 static HAL_StatusTypeDef CRYP_SetKey(CRYP_HandleTypeDef *hcryp); 03831 static HAL_StatusTypeDef CRYP_AES_IT(CRYP_HandleTypeDef *hcryp); 03832 03833 /** 03834 * @} 03835 */ 03836 03837 /* Exported functions ---------------------------------------------------------*/ 03838 03839 /** @defgroup CRYP_Exported_Functions CRYP Exported Functions 03840 * @{ 03841 */ 03842 03843 /** @defgroup CRYP_Exported_Functions_Group1 Initialization and deinitialization functions 03844 * @brief Initialization and Configuration functions. 03845 * 03846 @verbatim 03847 ============================================================================== 03848 ##### Initialization and deinitialization functions ##### 03849 ============================================================================== 03850 [..] This section provides functions allowing to: 03851 (+) Initialize the CRYP according to the specified parameters 03852 in the CRYP_InitTypeDef and creates the associated handle 03853 (+) DeInitialize the CRYP peripheral 03854 (+) Initialize the CRYP MSP (MCU Specific Package) 03855 (+) De-Initialize the CRYP MSP 03856 03857 [..] 03858 (@) Specific care must be taken to format the key and the Initialization Vector IV! 03859 03860 [..] If the key is defined as a 128-bit long array key[127..0] = {b127 ... b0} where 03861 b127 is the MSB and b0 the LSB, the key must be stored in MCU memory 03862 (+) as a sequence of words where the MSB word comes first (occupies the 03863 lowest memory address) 03864 (+) where each word is byte-swapped: 03865 (++) address n+0 : 0b b103 .. b96 b111 .. b104 b119 .. b112 b127 .. b120 03866 (++) address n+4 : 0b b71 .. b64 b79 .. b72 b87 .. b80 b95 .. b88 03867 (++) address n+8 : 0b b39 .. b32 b47 .. b40 b55 .. b48 b63 .. b56 03868 (++) address n+C : 0b b7 .. b0 b15 .. b8 b23 .. b16 b31 .. b24 03869 [..] Hereafter, another illustration when considering a 128-bit long key made of 16 bytes {B15..B0}. 03870 The 4 32-bit words that make the key must be stored as follows in MCU memory: 03871 (+) address n+0 : 0x B12 B13 B14 B15 03872 (+) address n+4 : 0x B8 B9 B10 B11 03873 (+) address n+8 : 0x B4 B5 B6 B7 03874 (+) address n+C : 0x B0 B1 B2 B3 03875 [..] which leads to the expected setting 03876 (+) AES_KEYR3 = 0x B15 B14 B13 B12 03877 (+) AES_KEYR2 = 0x B11 B10 B9 B8 03878 (+) AES_KEYR1 = 0x B7 B6 B5 B4 03879 (+) AES_KEYR0 = 0x B3 B2 B1 B0 03880 03881 [..] Same format must be applied for a 256-bit long key made of 32 bytes {B31..B0}. 03882 The 8 32-bit words that make the key must be stored as follows in MCU memory: 03883 (+) address n+00 : 0x B28 B29 B30 B31 03884 (+) address n+04 : 0x B24 B25 B26 B27 03885 (+) address n+08 : 0x B20 B21 B22 B23 03886 (+) address n+0C : 0x B16 B17 B18 B19 03887 (+) address n+10 : 0x B12 B13 B14 B15 03888 (+) address n+14 : 0x B8 B9 B10 B11 03889 (+) address n+18 : 0x B4 B5 B6 B7 03890 (+) address n+1C : 0x B0 B1 B2 B3 03891 [..] which leads to the expected setting 03892 (+) AES_KEYR7 = 0x B31 B30 B29 B28 03893 (+) AES_KEYR6 = 0x B27 B26 B25 B24 03894 (+) AES_KEYR5 = 0x B23 B22 B21 B20 03895 (+) AES_KEYR4 = 0x B19 B18 B17 B16 03896 (+) AES_KEYR3 = 0x B15 B14 B13 B12 03897 (+) AES_KEYR2 = 0x B11 B10 B9 B8 03898 (+) AES_KEYR1 = 0x B7 B6 B5 B4 03899 (+) AES_KEYR0 = 0x B3 B2 B1 B0 03900 03901 [..] Initialization Vector IV (4 32-bit words) format must follow the same as 03902 that of a 128-bit long key. 03903 03904 [..] 03905 03906 @endverbatim 03907 * @{ 03908 */ 03909 03910 /** 03911 * @brief Initialize the CRYP according to the specified 03912 * parameters in the CRYP_InitTypeDef and initialize the associated handle. 03913 * @note Specific care must be taken to format the key and the Initialization Vector IV 03914 * stored in the MCU memory before calling HAL_CRYP_Init(). Refer to explanations 03915 * hereabove. 03916 * @retval HAL status 03917 */ 03918 HAL_StatusTypeDef HAL_CRYP_Init(CRYP_HandleTypeDef *hcryp) 03919 { 03920 /* Check the CRYP handle allocation */ 03921 if(hcryp == NULL) 03922 { 03923 return HAL_ERROR; 03924 } 03925 03926 /* Check the instance */ 03927 assert_param(IS_AES_ALL_INSTANCE(hcryp->Instance)); 03928 03929 /* Check the parameters */ 03930 assert_param(IS_CRYP_KEYSIZE(hcryp->Init.KeySize)); 03931 assert_param(IS_CRYP_DATATYPE(hcryp->Init.DataType)); 03932 assert_param(IS_CRYP_ALGOMODE(hcryp->Init.OperatingMode)); 03933 /* ChainingMode parameter is irrelevant when mode is set to Key derivation */ 03934 if (hcryp->Init.OperatingMode != CRYP_ALGOMODE_KEYDERIVATION) 03935 { 03936 assert_param(IS_CRYP_CHAINMODE(hcryp->Init.ChainingMode)); 03937 } 03938 assert_param(IS_CRYP_WRITE(hcryp->Init.KeyWriteFlag)); 03939 03940 /*========================================================*/ 03941 /* Check the proper operating/chaining modes combinations */ 03942 /*========================================================*/ 03943 /* Check the proper chaining when the operating mode is key derivation and decryption */ 03944 #if defined(AES_CR_NPBLB) 03945 if ((hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION_DECRYPT) &&\ 03946 ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CTR) \ 03947 || (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC) \ 03948 || (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM_CMAC))) 03949 #else 03950 if ((hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION_DECRYPT) &&\ 03951 ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CTR) \ 03952 || (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC) \ 03953 || (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC))) 03954 #endif 03955 { 03956 return HAL_ERROR; 03957 } 03958 /* Check that key derivation is not set in CMAC mode or CCM mode when applicable */ 03959 #if defined(AES_CR_NPBLB) 03960 if ((hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION) 03961 && (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM_CMAC)) 03962 #else 03963 if ((hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION) 03964 && (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC)) 03965 #endif 03966 { 03967 return HAL_ERROR; 03968 } 03969 03970 03971 /*================*/ 03972 /* Initialization */ 03973 /*================*/ 03974 /* Initialization start */ 03975 if(hcryp->State == HAL_CRYP_STATE_RESET) 03976 { 03977 /* Allocate lock resource and initialize it */ 03978 hcryp->Lock = HAL_UNLOCKED; 03979 03980 /* Init the low level hardware */ 03981 HAL_CRYP_MspInit(hcryp); 03982 } 03983 03984 /* Change the CRYP state */ 03985 hcryp->State = HAL_CRYP_STATE_BUSY; 03986 03987 /* Disable the Peripheral */ 03988 __HAL_CRYP_DISABLE(); 03989 03990 /*=============================================================*/ 03991 /* AES initialization common to all operating modes */ 03992 /*=============================================================*/ 03993 /* Set the Key size selection */ 03994 MODIFY_REG(hcryp->Instance->CR, AES_CR_KEYSIZE, hcryp->Init.KeySize); 03995 03996 /* Set the default CRYP phase when this parameter is not used. 03997 Phase is updated below in case of GCM/GMAC/CMAC(/CCM) setting. */ 03998 hcryp->Phase = HAL_CRYP_PHASE_NOT_USED; 03999 04000 04001 04002 /*=============================================================*/ 04003 /* Carry on the initialization based on the AES operating mode */ 04004 /*=============================================================*/ 04005 /* Key derivation */ 04006 if (hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION) 04007 { 04008 MODIFY_REG(hcryp->Instance->CR, AES_CR_MODE, CRYP_ALGOMODE_KEYDERIVATION); 04009 04010 /* Configure the Key registers */ 04011 if (CRYP_SetKey(hcryp) != HAL_OK) 04012 { 04013 return HAL_ERROR; 04014 } 04015 } 04016 else 04017 /* Encryption / Decryption (with or without key derivation) / authentication */ 04018 { 04019 /* Set data type, operating and chaining modes. 04020 In case of GCM or GMAC, data type is forced to 0b00 */ 04021 if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC) 04022 { 04023 MODIFY_REG(hcryp->Instance->CR, AES_CR_DATATYPE|AES_CR_MODE|AES_CR_CHMOD, hcryp->Init.OperatingMode|hcryp->Init.ChainingMode); 04024 } 04025 else 04026 { 04027 MODIFY_REG(hcryp->Instance->CR, AES_CR_DATATYPE|AES_CR_MODE|AES_CR_CHMOD, hcryp->Init.DataType|hcryp->Init.OperatingMode|hcryp->Init.ChainingMode); 04028 } 04029 04030 04031 /* Specify the encryption/decryption phase in case of Galois counter mode (GCM), 04032 Galois message authentication code (GMAC), cipher message authentication code (CMAC) 04033 or Counter with Cipher Mode (CCM) when applicable */ 04034 #if defined(AES_CR_NPBLB) 04035 if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC) 04036 || (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM_CMAC)) 04037 #else 04038 if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC) 04039 || (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC)) 04040 #endif 04041 { 04042 MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, hcryp->Init.GCMCMACPhase); 04043 hcryp->Phase = HAL_CRYP_PHASE_START; 04044 } 04045 04046 04047 /* Configure the Key registers if no need to bypass this step */ 04048 if (hcryp->Init.KeyWriteFlag == CRYP_KEY_WRITE_ENABLE) 04049 { 04050 if (CRYP_SetKey(hcryp) != HAL_OK) 04051 { 04052 return HAL_ERROR; 04053 } 04054 } 04055 04056 /* If applicable, configure the Initialization Vector */ 04057 if (hcryp->Init.ChainingMode != CRYP_CHAINMODE_AES_ECB) 04058 { 04059 if (CRYP_SetInitVector(hcryp) != HAL_OK) 04060 { 04061 return HAL_ERROR; 04062 } 04063 } 04064 } 04065 04066 #if defined(AES_CR_NPBLB) 04067 /* Clear NPBLB field */ 04068 CLEAR_BIT(hcryp->Instance->CR, AES_CR_NPBLB); 04069 #endif 04070 04071 /* Reset CrypInCount and CrypOutCount */ 04072 hcryp->CrypInCount = 0U; 04073 hcryp->CrypOutCount = 0U; 04074 04075 /* Reset ErrorCode field */ 04076 hcryp->ErrorCode = HAL_CRYP_ERROR_NONE; 04077 04078 /* Reset Mode suspension request */ 04079 hcryp->SuspendRequest = HAL_CRYP_SUSPEND_NONE; 04080 04081 /* Change the CRYP state */ 04082 hcryp->State = HAL_CRYP_STATE_READY; 04083 04084 /* Enable the Peripheral */ 04085 __HAL_CRYP_ENABLE(); 04086 04087 /* Return function status */ 04088 return HAL_OK; 04089 } 04090 04091 /** 04092 * @brief DeInitialize the CRYP peripheral. 04093 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 04094 * the configuration information for CRYP module 04095 * @retval HAL status 04096 */ 04097 HAL_StatusTypeDef HAL_CRYP_DeInit(CRYP_HandleTypeDef *hcryp) 04098 { 04099 /* Check the CRYP handle allocation */ 04100 if(hcryp == NULL) 04101 { 04102 return HAL_ERROR; 04103 } 04104 04105 /* Change the CRYP state */ 04106 hcryp->State = HAL_CRYP_STATE_BUSY; 04107 04108 /* Set the default CRYP phase */ 04109 hcryp->Phase = HAL_CRYP_PHASE_READY; 04110 04111 /* Reset CrypInCount and CrypOutCount */ 04112 hcryp->CrypInCount = 0U; 04113 hcryp->CrypOutCount = 0U; 04114 04115 /* Disable the CRYP Peripheral Clock */ 04116 __HAL_CRYP_DISABLE(); 04117 04118 /* DeInit the low level hardware: CLOCK, NVIC.*/ 04119 HAL_CRYP_MspDeInit(hcryp); 04120 04121 /* Change the CRYP state */ 04122 hcryp->State = HAL_CRYP_STATE_RESET; 04123 04124 /* Release Lock */ 04125 __HAL_UNLOCK(hcryp); 04126 04127 /* Return function status */ 04128 return HAL_OK; 04129 } 04130 04131 /** 04132 * @brief Initialize the CRYP MSP. 04133 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 04134 * the configuration information for CRYP module 04135 * @retval None 04136 */ 04137 __weak void HAL_CRYP_MspInit(CRYP_HandleTypeDef *hcryp) 04138 { 04139 /* Prevent unused argument(s) compilation warning */ 04140 UNUSED(hcryp); 04141 04142 /* NOTE : This function should not be modified; when the callback is needed, 04143 the HAL_CRYP_MspInit can be implemented in the user file 04144 */ 04145 } 04146 04147 /** 04148 * @brief DeInitialize CRYP MSP. 04149 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 04150 * the configuration information for CRYP module 04151 * @retval None 04152 */ 04153 __weak void HAL_CRYP_MspDeInit(CRYP_HandleTypeDef *hcryp) 04154 { 04155 /* Prevent unused argument(s) compilation warning */ 04156 UNUSED(hcryp); 04157 04158 /* NOTE : This function should not be modified; when the callback is needed, 04159 the HAL_CRYP_MspDeInit can be implemented in the user file 04160 */ 04161 } 04162 04163 /** 04164 * @} 04165 */ 04166 04167 /** @defgroup CRYP_Exported_Functions_Group2 AES processing functions 04168 * @brief Processing functions. 04169 * 04170 @verbatim 04171 ============================================================================== 04172 ##### AES processing functions ##### 04173 ============================================================================== 04174 [..] This section provides functions allowing to: 04175 (+) Encrypt plaintext using AES algorithm in different chaining modes 04176 (+) Decrypt cyphertext using AES algorithm in different chaining modes 04177 [..] Three processing functions are available: 04178 (+) Polling mode 04179 (+) Interrupt mode 04180 (+) DMA mode 04181 04182 @endverbatim 04183 * @{ 04184 */ 04185 04186 04187 /** 04188 * @brief Encrypt pPlainData in AES ECB encryption mode. The cypher data are available in pCypherData. 04189 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 04190 * the configuration information for CRYP module 04191 * @param pPlainData Pointer to the plaintext buffer 04192 * @param Size Length of the plaintext buffer in bytes, must be a multiple of 16. 04193 * @param pCypherData Pointer to the cyphertext buffer 04194 * @param Timeout Specify Timeout value 04195 * @note This API is provided only to maintain compatibility with legacy software. Users should directly 04196 * resort to generic HAL_CRYPEx_AES() API instead (usage recommended). 04197 * @retval HAL status 04198 */ 04199 HAL_StatusTypeDef HAL_CRYP_AESECB_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout) 04200 { 04201 /* Re-initialize AES IP with proper parameters */ 04202 if (HAL_CRYP_DeInit(hcryp) != HAL_OK) 04203 { 04204 return HAL_ERROR; 04205 } 04206 hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT; 04207 hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_ECB; 04208 hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE; 04209 if (HAL_CRYP_Init(hcryp) != HAL_OK) 04210 { 04211 return HAL_ERROR; 04212 } 04213 04214 return HAL_CRYPEx_AES(hcryp, pPlainData, Size, pCypherData, Timeout); 04215 } 04216 04217 04218 /** 04219 * @brief Encrypt pPlainData in AES CBC encryption mode with key derivation. The cypher data are available in pCypherData. 04220 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 04221 * the configuration information for CRYP module 04222 * @param pPlainData Pointer to the plaintext buffer 04223 * @param Size Length of the plaintext buffer in bytes, must be a multiple of 16. 04224 * @param pCypherData Pointer to the cyphertext buffer 04225 * @param Timeout Specify Timeout value 04226 * @note This API is provided only to maintain compatibility with legacy software. Users should directly 04227 * resort to generic HAL_CRYPEx_AES() API instead (usage recommended). 04228 * @retval HAL status 04229 */ 04230 HAL_StatusTypeDef HAL_CRYP_AESCBC_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout) 04231 { 04232 /* Re-initialize AES IP with proper parameters */ 04233 if (HAL_CRYP_DeInit(hcryp) != HAL_OK) 04234 { 04235 return HAL_ERROR; 04236 } 04237 hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT; 04238 hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CBC; 04239 hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE; 04240 if (HAL_CRYP_Init(hcryp) != HAL_OK) 04241 { 04242 return HAL_ERROR; 04243 } 04244 04245 return HAL_CRYPEx_AES(hcryp, pPlainData, Size, pCypherData, Timeout); 04246 } 04247 04248 04249 /** 04250 * @brief Encrypt pPlainData in AES CTR encryption mode. The cypher data are available in pCypherData 04251 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 04252 * the configuration information for CRYP module 04253 * @param pPlainData Pointer to the plaintext buffer 04254 * @param Size Length of the plaintext buffer in bytes, must be a multiple of 16. 04255 * @param pCypherData Pointer to the cyphertext buffer 04256 * @param Timeout Specify Timeout value 04257 * @note This API is provided only to maintain compatibility with legacy software. Users should directly 04258 * resort to generic HAL_CRYPEx_AES() API instead (usage recommended). 04259 * @retval HAL status 04260 */ 04261 HAL_StatusTypeDef HAL_CRYP_AESCTR_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout) 04262 { 04263 /* Re-initialize AES IP with proper parameters */ 04264 if (HAL_CRYP_DeInit(hcryp) != HAL_OK) 04265 { 04266 return HAL_ERROR; 04267 } 04268 hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT; 04269 hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CTR; 04270 hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE; 04271 if (HAL_CRYP_Init(hcryp) != HAL_OK) 04272 { 04273 return HAL_ERROR; 04274 } 04275 04276 return HAL_CRYPEx_AES(hcryp, pPlainData, Size, pCypherData, Timeout); 04277 } 04278 04279 /** 04280 * @brief Decrypt pCypherData in AES ECB decryption mode with key derivation, 04281 * the decyphered data are available in pPlainData. 04282 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 04283 * the configuration information for CRYP module 04284 * @param pCypherData Pointer to the cyphertext buffer 04285 * @param Size Length of the plaintext buffer in bytes, must be a multiple of 16. 04286 * @param pPlainData Pointer to the plaintext buffer 04287 * @param Timeout Specify Timeout value 04288 * @note This API is provided only to maintain compatibility with legacy software. Users should directly 04289 * resort to generic HAL_CRYPEx_AES() API instead (usage recommended). 04290 * @retval HAL status 04291 */ 04292 HAL_StatusTypeDef HAL_CRYP_AESECB_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout) 04293 { 04294 /* Re-initialize AES IP with proper parameters */ 04295 if (HAL_CRYP_DeInit(hcryp) != HAL_OK) 04296 { 04297 return HAL_ERROR; 04298 } 04299 hcryp->Init.OperatingMode = CRYP_ALGOMODE_KEYDERIVATION_DECRYPT; 04300 hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_ECB; 04301 hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE; 04302 if (HAL_CRYP_Init(hcryp) != HAL_OK) 04303 { 04304 return HAL_ERROR; 04305 } 04306 04307 return HAL_CRYPEx_AES(hcryp, pCypherData, Size, pPlainData, Timeout); 04308 } 04309 04310 /** 04311 * @brief Decrypt pCypherData in AES ECB decryption mode with key derivation, 04312 * the decyphered data are available in pPlainData. 04313 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 04314 * the configuration information for CRYP module 04315 * @param pCypherData Pointer to the cyphertext buffer 04316 * @param Size Length of the plaintext buffer in bytes, must be a multiple of 16. 04317 * @param pPlainData Pointer to the plaintext buffer 04318 * @param Timeout Specify Timeout value 04319 * @note This API is provided only to maintain compatibility with legacy software. Users should directly 04320 * resort to generic HAL_CRYPEx_AES() API instead (usage recommended). 04321 * @retval HAL status 04322 */ 04323 HAL_StatusTypeDef HAL_CRYP_AESCBC_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout) 04324 { 04325 /* Re-initialize AES IP with proper parameters */ 04326 if (HAL_CRYP_DeInit(hcryp) != HAL_OK) 04327 { 04328 return HAL_ERROR; 04329 } 04330 hcryp->Init.OperatingMode = CRYP_ALGOMODE_KEYDERIVATION_DECRYPT; 04331 hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CBC; 04332 hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE; 04333 if (HAL_CRYP_Init(hcryp) != HAL_OK) 04334 { 04335 return HAL_ERROR; 04336 } 04337 04338 return HAL_CRYPEx_AES(hcryp, pCypherData, Size, pPlainData, Timeout); 04339 } 04340 04341 /** 04342 * @brief Decrypt pCypherData in AES CTR decryption mode, 04343 * the decyphered data are available in pPlainData. 04344 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 04345 * the configuration information for CRYP module 04346 * @param pCypherData Pointer to the cyphertext buffer 04347 * @param Size Length of the plaintext buffer in bytes, must be a multiple of 16. 04348 * @param pPlainData Pointer to the plaintext buffer 04349 * @param Timeout Specify Timeout value 04350 * @note This API is provided only to maintain compatibility with legacy software. Users should directly 04351 * resort to generic HAL_CRYPEx_AES() API instead (usage recommended). 04352 * @retval HAL status 04353 */ 04354 HAL_StatusTypeDef HAL_CRYP_AESCTR_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout) 04355 { 04356 /* Re-initialize AES IP with proper parameters */ 04357 if (HAL_CRYP_DeInit(hcryp) != HAL_OK) 04358 { 04359 return HAL_ERROR; 04360 } 04361 hcryp->Init.OperatingMode = CRYP_ALGOMODE_DECRYPT; 04362 hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CTR; 04363 hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE; 04364 if (HAL_CRYP_Init(hcryp) != HAL_OK) 04365 { 04366 return HAL_ERROR; 04367 } 04368 04369 return HAL_CRYPEx_AES(hcryp, pCypherData, Size, pPlainData, Timeout); 04370 } 04371 04372 /** 04373 * @brief Encrypt pPlainData in AES ECB encryption mode using Interrupt, 04374 * the cypher data are available in pCypherData. 04375 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 04376 * the configuration information for CRYP module 04377 * @param pPlainData Pointer to the plaintext buffer 04378 * @param Size Length of the plaintext buffer in bytes, must be a multiple of 16. 04379 * @param pCypherData Pointer to the cyphertext buffer 04380 * @note This API is provided only to maintain compatibility with legacy software. Users should directly 04381 * resort to generic HAL_CRYPEx_AES_IT() API instead (usage recommended). 04382 * @retval HAL status 04383 */ 04384 HAL_StatusTypeDef HAL_CRYP_AESECB_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData) 04385 { 04386 /* Re-initialize AES IP with proper parameters */ 04387 if (HAL_CRYP_DeInit(hcryp) != HAL_OK) 04388 { 04389 return HAL_ERROR; 04390 } 04391 hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT; 04392 hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_ECB; 04393 hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE; 04394 if (HAL_CRYP_Init(hcryp) != HAL_OK) 04395 { 04396 return HAL_ERROR; 04397 } 04398 04399 return HAL_CRYPEx_AES_IT(hcryp, pPlainData, Size, pCypherData); 04400 } 04401 04402 /** 04403 * @brief Encrypt pPlainData in AES CBC encryption mode using Interrupt, 04404 * the cypher data are available in pCypherData. 04405 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 04406 * the configuration information for CRYP module 04407 * @param pPlainData Pointer to the plaintext buffer 04408 * @param Size Length of the plaintext buffer in bytes, must be a multiple of 16. 04409 * @param pCypherData Pointer to the cyphertext buffer 04410 * @note This API is provided only to maintain compatibility with legacy software. Users should directly 04411 * resort to generic HAL_CRYPEx_AES_IT() API instead (usage recommended). 04412 * @retval HAL status 04413 */ 04414 HAL_StatusTypeDef HAL_CRYP_AESCBC_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData) 04415 { 04416 /* Re-initialize AES IP with proper parameters */ 04417 if (HAL_CRYP_DeInit(hcryp) != HAL_OK) 04418 { 04419 return HAL_ERROR; 04420 } 04421 hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT; 04422 hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CBC; 04423 hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE; 04424 if (HAL_CRYP_Init(hcryp) != HAL_OK) 04425 { 04426 return HAL_ERROR; 04427 } 04428 04429 return HAL_CRYPEx_AES_IT(hcryp, pPlainData, Size, pCypherData); 04430 } 04431 04432 04433 /** 04434 * @brief Encrypt pPlainData in AES CTR encryption mode using Interrupt, 04435 * the cypher data are available in pCypherData. 04436 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 04437 * the configuration information for CRYP module 04438 * @param pPlainData Pointer to the plaintext buffer 04439 * @param Size Length of the plaintext buffer in bytes, must be a multiple of 16. 04440 * @param pCypherData Pointer to the cyphertext buffer 04441 * @note This API is provided only to maintain compatibility with legacy software. Users should directly 04442 * resort to generic HAL_CRYPEx_AES_IT() API instead (usage recommended). 04443 * @retval HAL status 04444 */ 04445 HAL_StatusTypeDef HAL_CRYP_AESCTR_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData) 04446 { 04447 /* Re-initialize AES IP with proper parameters */ 04448 if (HAL_CRYP_DeInit(hcryp) != HAL_OK) 04449 { 04450 return HAL_ERROR; 04451 } 04452 hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT; 04453 hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CTR; 04454 hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE; 04455 if (HAL_CRYP_Init(hcryp) != HAL_OK) 04456 { 04457 return HAL_ERROR; 04458 } 04459 04460 return HAL_CRYPEx_AES_IT(hcryp, pPlainData, Size, pCypherData); 04461 } 04462 04463 /** 04464 * @brief Decrypt pCypherData in AES ECB decryption mode using Interrupt, 04465 * the decyphered data are available in pPlainData. 04466 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 04467 * the configuration information for CRYP module 04468 * @param pCypherData Pointer to the cyphertext buffer 04469 * @param Size Length of the plaintext buffer in bytes, must be a multiple of 16. 04470 * @param pPlainData Pointer to the plaintext buffer. 04471 * @note This API is provided only to maintain compatibility with legacy software. Users should directly 04472 * resort to generic HAL_CRYPEx_AES_IT() API instead (usage recommended). 04473 * @retval HAL status 04474 */ 04475 HAL_StatusTypeDef HAL_CRYP_AESECB_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData) 04476 { 04477 /* Re-initialize AES IP with proper parameters */ 04478 if (HAL_CRYP_DeInit(hcryp) != HAL_OK) 04479 { 04480 return HAL_ERROR; 04481 } 04482 hcryp->Init.OperatingMode = CRYP_ALGOMODE_KEYDERIVATION_DECRYPT; 04483 hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_ECB; 04484 hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE; 04485 if (HAL_CRYP_Init(hcryp) != HAL_OK) 04486 { 04487 return HAL_ERROR; 04488 } 04489 04490 return HAL_CRYPEx_AES_IT(hcryp, pCypherData, Size, pPlainData); 04491 } 04492 04493 /** 04494 * @brief Decrypt pCypherData in AES CBC decryption mode using Interrupt, 04495 * the decyphered data are available in pPlainData. 04496 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 04497 * the configuration information for CRYP module 04498 * @param pCypherData Pointer to the cyphertext buffer 04499 * @param Size Length of the plaintext buffer in bytes, must be a multiple of 16. 04500 * @param pPlainData Pointer to the plaintext buffer 04501 * @note This API is provided only to maintain compatibility with legacy software. Users should directly 04502 * resort to generic HAL_CRYPEx_AES_IT() API instead (usage recommended). 04503 * @retval HAL status 04504 */ 04505 HAL_StatusTypeDef HAL_CRYP_AESCBC_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData) 04506 { 04507 /* Re-initialize AES IP with proper parameters */ 04508 if (HAL_CRYP_DeInit(hcryp) != HAL_OK) 04509 { 04510 return HAL_ERROR; 04511 } 04512 hcryp->Init.OperatingMode = CRYP_ALGOMODE_KEYDERIVATION_DECRYPT; 04513 hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CBC; 04514 hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE; 04515 if (HAL_CRYP_Init(hcryp) != HAL_OK) 04516 { 04517 return HAL_ERROR; 04518 } 04519 04520 return HAL_CRYPEx_AES_IT(hcryp, pCypherData, Size, pPlainData); 04521 } 04522 04523 /** 04524 * @brief Decrypt pCypherData in AES CTR decryption mode using Interrupt, 04525 * the decyphered data are available in pPlainData. 04526 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 04527 * the configuration information for CRYP module 04528 * @param pCypherData Pointer to the cyphertext buffer 04529 * @param Size Length of the plaintext buffer in bytes, must be a multiple of 16. 04530 * @param pPlainData Pointer to the plaintext buffer 04531 * @note This API is provided only to maintain compatibility with legacy software. Users should directly 04532 * resort to generic HAL_CRYPEx_AES_IT() API instead (usage recommended). 04533 * @retval HAL status 04534 */ 04535 HAL_StatusTypeDef HAL_CRYP_AESCTR_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData) 04536 { 04537 /* Re-initialize AES IP with proper parameters */ 04538 if (HAL_CRYP_DeInit(hcryp) != HAL_OK) 04539 { 04540 return HAL_ERROR; 04541 } 04542 hcryp->Init.OperatingMode = CRYP_ALGOMODE_DECRYPT; 04543 hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CTR; 04544 hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE; 04545 if (HAL_CRYP_Init(hcryp) != HAL_OK) 04546 { 04547 return HAL_ERROR; 04548 } 04549 04550 return HAL_CRYPEx_AES_IT(hcryp, pCypherData, Size, pPlainData); 04551 } 04552 04553 /** 04554 * @brief Encrypt pPlainData in AES ECB encryption mode using DMA, 04555 * the cypher data are available in pCypherData. 04556 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 04557 * the configuration information for CRYP module 04558 * @param pPlainData Pointer to the plaintext buffer 04559 * @param Size Length of the plaintext buffer in bytes, must be a multiple of 16. 04560 * @param pCypherData Pointer to the cyphertext buffer 04561 * @note This API is provided only to maintain compatibility with legacy software. Users should directly 04562 * resort to generic HAL_CRYPEx_AES_DMA() API instead (usage recommended). 04563 * @note pPlainData and pCypherData buffers must be 32-bit aligned to ensure a correct DMA transfer to and from the IP. 04564 * @retval HAL status 04565 */ 04566 HAL_StatusTypeDef HAL_CRYP_AESECB_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData) 04567 { 04568 /* Re-initialize AES IP with proper parameters */ 04569 if (HAL_CRYP_DeInit(hcryp) != HAL_OK) 04570 { 04571 return HAL_ERROR; 04572 } 04573 hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT; 04574 hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_ECB; 04575 hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE; 04576 if (HAL_CRYP_Init(hcryp) != HAL_OK) 04577 { 04578 return HAL_ERROR; 04579 } 04580 04581 return HAL_CRYPEx_AES_DMA(hcryp, pPlainData, Size, pCypherData); 04582 } 04583 04584 04585 04586 /** 04587 * @brief Encrypt pPlainData in AES CBC encryption mode using DMA, 04588 * the cypher data are available in pCypherData. 04589 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 04590 * the configuration information for CRYP module 04591 * @param pPlainData Pointer to the plaintext buffer 04592 * @param Size Length of the plaintext buffer, must be a multiple of 16. 04593 * @param pCypherData Pointer to the cyphertext buffer 04594 * @note This API is provided only to maintain compatibility with legacy software. Users should directly 04595 * resort to generic HAL_CRYPEx_AES_DMA() API instead (usage recommended). 04596 * @note pPlainData and pCypherData buffers must be 32-bit aligned to ensure a correct DMA transfer to and from the IP. 04597 * @retval HAL status 04598 */ 04599 HAL_StatusTypeDef HAL_CRYP_AESCBC_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData) 04600 { 04601 /* Re-initialize AES IP with proper parameters */ 04602 if (HAL_CRYP_DeInit(hcryp) != HAL_OK) 04603 { 04604 return HAL_ERROR; 04605 } 04606 hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT; 04607 hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CBC; 04608 hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE; 04609 if (HAL_CRYP_Init(hcryp) != HAL_OK) 04610 { 04611 return HAL_ERROR; 04612 } 04613 04614 return HAL_CRYPEx_AES_DMA(hcryp, pPlainData, Size, pCypherData); 04615 } 04616 04617 /** 04618 * @brief Encrypt pPlainData in AES CTR encryption mode using DMA, 04619 * the cypher data are available in pCypherData. 04620 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 04621 * the configuration information for CRYP module 04622 * @param pPlainData Pointer to the plaintext buffer 04623 * @param Size Length of the plaintext buffer in bytes, must be a multiple of 16. 04624 * @param pCypherData Pointer to the cyphertext buffer. 04625 * @note This API is provided only to maintain compatibility with legacy software. Users should directly 04626 * resort to generic HAL_CRYPEx_AES_DMA() API instead (usage recommended). 04627 * @note pPlainData and pCypherData buffers must be 32-bit aligned to ensure a correct DMA transfer to and from the IP. 04628 * @retval HAL status 04629 */ 04630 HAL_StatusTypeDef HAL_CRYP_AESCTR_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData) 04631 { 04632 /* Re-initialize AES IP with proper parameters */ 04633 if (HAL_CRYP_DeInit(hcryp) != HAL_OK) 04634 { 04635 return HAL_ERROR; 04636 } 04637 hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT; 04638 hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CTR; 04639 hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE; 04640 if (HAL_CRYP_Init(hcryp) != HAL_OK) 04641 { 04642 return HAL_ERROR; 04643 } 04644 04645 return HAL_CRYPEx_AES_DMA(hcryp, pPlainData, Size, pCypherData); 04646 } 04647 04648 /** 04649 * @brief Decrypt pCypherData in AES ECB decryption mode using DMA, 04650 * the decyphered data are available in pPlainData. 04651 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 04652 * the configuration information for CRYP module 04653 * @param pCypherData Pointer to the cyphertext buffer 04654 * @param Size Length of the plaintext buffer in bytes, must be a multiple of 16. 04655 * @param pPlainData Pointer to the plaintext buffer 04656 * @note This API is provided only to maintain compatibility with legacy software. Users should directly 04657 * resort to generic HAL_CRYPEx_AES_DMA() API instead (usage recommended). 04658 * @note pPlainData and pCypherData buffers must be 32-bit aligned to ensure a correct DMA transfer to and from the IP. 04659 * @retval HAL status 04660 */ 04661 HAL_StatusTypeDef HAL_CRYP_AESECB_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData) 04662 { 04663 /* Re-initialize AES IP with proper parameters */ 04664 if (HAL_CRYP_DeInit(hcryp) != HAL_OK) 04665 { 04666 return HAL_ERROR; 04667 } 04668 hcryp->Init.OperatingMode = CRYP_ALGOMODE_KEYDERIVATION_DECRYPT; 04669 hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_ECB; 04670 hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE; 04671 if (HAL_CRYP_Init(hcryp) != HAL_OK) 04672 { 04673 return HAL_ERROR; 04674 } 04675 04676 return HAL_CRYPEx_AES_DMA(hcryp, pCypherData, Size, pPlainData); 04677 } 04678 04679 /** 04680 * @brief Decrypt pCypherData in AES CBC decryption mode using DMA, 04681 * the decyphered data are available in pPlainData. 04682 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 04683 * the configuration information for CRYP module 04684 * @param pCypherData Pointer to the cyphertext buffer 04685 * @param Size Length of the plaintext buffer in bytes, must be a multiple of 16. 04686 * @param pPlainData Pointer to the plaintext buffer 04687 * @note This API is provided only to maintain compatibility with legacy software. Users should directly 04688 * resort to generic HAL_CRYPEx_AES_DMA() API instead (usage recommended). 04689 * @note pPlainData and pCypherData buffers must be 32-bit aligned to ensure a correct DMA transfer to and from the IP. 04690 * @retval HAL status 04691 */ 04692 HAL_StatusTypeDef HAL_CRYP_AESCBC_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData) 04693 { 04694 /* Re-initialize AES IP with proper parameters */ 04695 if (HAL_CRYP_DeInit(hcryp) != HAL_OK) 04696 { 04697 return HAL_ERROR; 04698 } 04699 hcryp->Init.OperatingMode = CRYP_ALGOMODE_KEYDERIVATION_DECRYPT; 04700 hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CBC; 04701 hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE; 04702 if (HAL_CRYP_Init(hcryp) != HAL_OK) 04703 { 04704 return HAL_ERROR; 04705 } 04706 04707 return HAL_CRYPEx_AES_DMA(hcryp, pCypherData, Size, pPlainData); 04708 } 04709 04710 /** 04711 * @brief Decrypt pCypherData in AES CTR decryption mode using DMA, 04712 * the decyphered data are available in pPlainData. 04713 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 04714 * the configuration information for CRYP module 04715 * @param pCypherData Pointer to the cyphertext buffer 04716 * @param Size Length of the plaintext buffer in bytes, must be a multiple of 16. 04717 * @param pPlainData Pointer to the plaintext buffer 04718 * @note This API is provided only to maintain compatibility with legacy software. Users should directly 04719 * resort to generic HAL_CRYPEx_AES_DMA() API instead (usage recommended). 04720 * @note pPlainData and pCypherData buffers must be 32-bit aligned to ensure a correct DMA transfer to and from the IP. 04721 * @retval HAL status 04722 */ 04723 HAL_StatusTypeDef HAL_CRYP_AESCTR_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData) 04724 { 04725 /* Re-initialize AES IP with proper parameters */ 04726 if (HAL_CRYP_DeInit(hcryp) != HAL_OK) 04727 { 04728 return HAL_ERROR; 04729 } 04730 hcryp->Init.OperatingMode = CRYP_ALGOMODE_DECRYPT; 04731 hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CTR; 04732 hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE; 04733 if (HAL_CRYP_Init(hcryp) != HAL_OK) 04734 { 04735 return HAL_ERROR; 04736 } 04737 04738 return HAL_CRYPEx_AES_DMA(hcryp, pCypherData, Size, pPlainData); 04739 } 04740 04741 04742 /** 04743 * @} 04744 */ 04745 04746 /** @defgroup CRYP_Exported_Functions_Group3 Callback functions 04747 * @brief Callback functions. 04748 * 04749 @verbatim 04750 ============================================================================== 04751 ##### Callback functions ##### 04752 ============================================================================== 04753 [..] This section provides Interruption and DMA callback functions: 04754 (+) DMA Input data transfer complete 04755 (+) DMA Output data transfer complete 04756 (+) DMA or Interrupt error 04757 04758 @endverbatim 04759 * @{ 04760 */ 04761 04762 /** 04763 * @brief CRYP error callback. 04764 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 04765 * the configuration information for CRYP module 04766 * @retval None 04767 */ 04768 __weak void HAL_CRYP_ErrorCallback(CRYP_HandleTypeDef *hcryp) 04769 { 04770 /* Prevent unused argument(s) compilation warning */ 04771 UNUSED(hcryp); 04772 04773 /* NOTE : This function should not be modified; when the callback is needed, 04774 the HAL_CRYP_ErrorCallback can be implemented in the user file 04775 */ 04776 } 04777 04778 /** 04779 * @brief Input DMA transfer complete callback. 04780 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 04781 * the configuration information for CRYP module 04782 * @retval None 04783 */ 04784 __weak void HAL_CRYP_InCpltCallback(CRYP_HandleTypeDef *hcryp) 04785 { 04786 /* Prevent unused argument(s) compilation warning */ 04787 UNUSED(hcryp); 04788 04789 /* NOTE : This function should not be modified; when the callback is needed, 04790 the HAL_CRYP_InCpltCallback can be implemented in the user file 04791 */ 04792 } 04793 04794 /** 04795 * @brief Output DMA transfer complete callback. 04796 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 04797 * the configuration information for CRYP module 04798 * @retval None 04799 */ 04800 __weak void HAL_CRYP_OutCpltCallback(CRYP_HandleTypeDef *hcryp) 04801 { 04802 /* Prevent unused argument(s) compilation warning */ 04803 UNUSED(hcryp); 04804 04805 /* NOTE : This function should not be modified; when the callback is needed, 04806 the HAL_CRYP_OutCpltCallback can be implemented in the user file 04807 */ 04808 } 04809 04810 /** 04811 * @} 04812 */ 04813 04814 /** @defgroup CRYP_Exported_Functions_Group4 CRYP IRQ handler 04815 * @brief AES IRQ handler. 04816 * 04817 @verbatim 04818 ============================================================================== 04819 ##### AES IRQ handler management ##### 04820 ============================================================================== 04821 [..] This section provides AES IRQ handler function. 04822 04823 @endverbatim 04824 * @{ 04825 */ 04826 04827 /** 04828 * @brief Handle AES interrupt request. 04829 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 04830 * the configuration information for CRYP module 04831 * @retval None 04832 */ 04833 void HAL_CRYP_IRQHandler(CRYP_HandleTypeDef *hcryp) 04834 { 04835 /* Check if error occurred */ 04836 if (__HAL_CRYP_GET_IT_SOURCE(CRYP_IT_ERRIE) != RESET) 04837 { 04838 /* If Write Error occurred */ 04839 if (__HAL_CRYP_GET_FLAG(CRYP_IT_WRERR) != RESET) 04840 { 04841 hcryp->ErrorCode |= HAL_CRYP_WRITE_ERROR; 04842 hcryp->State = HAL_CRYP_STATE_ERROR; 04843 } 04844 /* If Read Error occurred */ 04845 if (__HAL_CRYP_GET_FLAG(CRYP_IT_RDERR) != RESET) 04846 { 04847 hcryp->ErrorCode |= HAL_CRYP_READ_ERROR; 04848 hcryp->State = HAL_CRYP_STATE_ERROR; 04849 } 04850 04851 /* If an error has been reported */ 04852 if (hcryp->State == HAL_CRYP_STATE_ERROR) 04853 { 04854 /* Disable Error and Computation Complete Interrupts */ 04855 __HAL_CRYP_DISABLE_IT(CRYP_IT_CCFIE|CRYP_IT_ERRIE); 04856 /* Clear all Interrupt flags */ 04857 __HAL_CRYP_CLEAR_FLAG(CRYP_ERR_CLEAR|CRYP_CCF_CLEAR); 04858 04859 /* Process Unlocked */ 04860 __HAL_UNLOCK(hcryp); 04861 04862 HAL_CRYP_ErrorCallback(hcryp); 04863 04864 return; 04865 } 04866 } 04867 04868 /* Check if computation complete interrupt is enabled 04869 and if the computation complete flag is raised */ 04870 if((__HAL_CRYP_GET_FLAG(CRYP_IT_CCF) != RESET) && (__HAL_CRYP_GET_IT_SOURCE(CRYP_IT_CCFIE) != RESET)) 04871 { 04872 #if defined(AES_CR_NPBLB) 04873 if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC) 04874 || (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM_CMAC)) 04875 #else 04876 if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC) 04877 || (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC)) 04878 #endif 04879 { 04880 /* To ensure proper suspension requests management, CCF flag 04881 is reset in CRYP_AES_Auth_IT() according to the current 04882 phase under handling */ 04883 CRYP_AES_Auth_IT(hcryp); 04884 } 04885 else 04886 { 04887 /* Clear Computation Complete Flag */ 04888 __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR); 04889 CRYP_AES_IT(hcryp); 04890 } 04891 } 04892 } 04893 04894 /** 04895 * @} 04896 */ 04897 04898 /** @defgroup CRYP_Exported_Functions_Group5 Peripheral State functions 04899 * @brief Peripheral State functions. 04900 * 04901 @verbatim 04902 ============================================================================== 04903 ##### Peripheral State functions ##### 04904 ============================================================================== 04905 [..] 04906 This subsection permits to get in run-time the status of the peripheral. 04907 04908 @endverbatim 04909 * @{ 04910 */ 04911 04912 /** 04913 * @brief Return the CRYP handle state. 04914 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 04915 * the configuration information for CRYP module 04916 * @retval HAL state 04917 */ 04918 HAL_CRYP_STATETypeDef HAL_CRYP_GetState(CRYP_HandleTypeDef *hcryp) 04919 { 04920 /* Return CRYP handle state */ 04921 return hcryp->State; 04922 } 04923 04924 /** 04925 * @brief Return the CRYP peripheral error. 04926 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 04927 * the configuration information for CRYP module 04928 * @note The returned error is a bit-map combination of possible errors 04929 * @retval Error bit-map 04930 */ 04931 uint32_t HAL_CRYP_GetError(CRYP_HandleTypeDef *hcryp) 04932 { 04933 return hcryp->ErrorCode; 04934 } 04935 04936 /** 04937 * @} 04938 */ 04939 04940 /** 04941 * @} 04942 */ 04943 04944 /** @addtogroup CRYP_Private_Functions 04945 * @{ 04946 */ 04947 04948 04949 /** 04950 * @brief Write the Key in KeyRx registers. 04951 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 04952 * the configuration information for CRYP module 04953 * @retval None 04954 */ 04955 static HAL_StatusTypeDef CRYP_SetKey(CRYP_HandleTypeDef *hcryp) 04956 { 04957 uint32_t keyaddr = 0x0U; 04958 04959 if ((uint32_t)(hcryp->Init.pKey == NULL)) 04960 { 04961 return HAL_ERROR; 04962 } 04963 04964 04965 keyaddr = (uint32_t)(hcryp->Init.pKey); 04966 04967 if (hcryp->Init.KeySize == CRYP_KEYSIZE_256B) 04968 { 04969 hcryp->Instance->KEYR7 = __REV(*(uint32_t*)(keyaddr)); 04970 keyaddr+=4U; 04971 hcryp->Instance->KEYR6 = __REV(*(uint32_t*)(keyaddr)); 04972 keyaddr+=4U; 04973 hcryp->Instance->KEYR5 = __REV(*(uint32_t*)(keyaddr)); 04974 keyaddr+=4U; 04975 hcryp->Instance->KEYR4 = __REV(*(uint32_t*)(keyaddr)); 04976 keyaddr+=4U; 04977 } 04978 04979 hcryp->Instance->KEYR3 = __REV(*(uint32_t*)(keyaddr)); 04980 keyaddr+=4U; 04981 hcryp->Instance->KEYR2 = __REV(*(uint32_t*)(keyaddr)); 04982 keyaddr+=4U; 04983 hcryp->Instance->KEYR1 = __REV(*(uint32_t*)(keyaddr)); 04984 keyaddr+=4U; 04985 hcryp->Instance->KEYR0 = __REV(*(uint32_t*)(keyaddr)); 04986 04987 return HAL_OK; 04988 } 04989 04990 /** 04991 * @brief Write the InitVector/InitCounter in IVRx registers. 04992 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 04993 * the configuration information for CRYP module 04994 * @retval None 04995 */ 04996 static HAL_StatusTypeDef CRYP_SetInitVector(CRYP_HandleTypeDef *hcryp) 04997 { 04998 uint32_t ivaddr = 0x0U; 04999 05000 #if !defined(AES_CR_NPBLB) 05001 if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC) 05002 { 05003 hcryp->Instance->IVR3 = 0U; 05004 hcryp->Instance->IVR2 = 0U; 05005 hcryp->Instance->IVR1 = 0U; 05006 hcryp->Instance->IVR0 = 0U; 05007 } 05008 else 05009 #endif 05010 { 05011 if (hcryp->Init.pInitVect == NULL) 05012 { 05013 return HAL_ERROR; 05014 } 05015 05016 ivaddr = (uint32_t)(hcryp->Init.pInitVect); 05017 05018 hcryp->Instance->IVR3 = __REV(*(uint32_t*)(ivaddr)); 05019 ivaddr+=4U; 05020 hcryp->Instance->IVR2 = __REV(*(uint32_t*)(ivaddr)); 05021 ivaddr+=4U; 05022 hcryp->Instance->IVR1 = __REV(*(uint32_t*)(ivaddr)); 05023 ivaddr+=4U; 05024 hcryp->Instance->IVR0 = __REV(*(uint32_t*)(ivaddr)); 05025 } 05026 return HAL_OK; 05027 } 05028 05029 05030 05031 /** 05032 * @brief Handle CRYP block input/output data handling under interruption. 05033 * @note The function is called under interruption only, once 05034 * interruptions have been enabled by HAL_CRYPEx_AES_IT(). 05035 * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains 05036 * the configuration information for CRYP module. 05037 * @retval HAL status 05038 */ 05039 static HAL_StatusTypeDef CRYP_AES_IT(CRYP_HandleTypeDef *hcryp) 05040 { 05041 uint32_t inputaddr = 0U; 05042 uint32_t outputaddr = 0U; 05043 05044 if(hcryp->State == HAL_CRYP_STATE_BUSY) 05045 { 05046 if (hcryp->Init.OperatingMode != CRYP_ALGOMODE_KEYDERIVATION) 05047 { 05048 /* Get the output data address */ 05049 outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr; 05050 05051 /* Read the last available output block from the Data Output Register */ 05052 *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR; 05053 outputaddr+=4U; 05054 *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR; 05055 outputaddr+=4U; 05056 *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR; 05057 outputaddr+=4U; 05058 *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR; 05059 hcryp->pCrypOutBuffPtr += 16U; 05060 hcryp->CrypOutCount -= 16U; 05061 05062 } 05063 else 05064 { 05065 /* Read the derived key from the Key registers */ 05066 if (hcryp->Init.KeySize == CRYP_KEYSIZE_256B) 05067 { 05068 *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR7); 05069 outputaddr+=4U; 05070 *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR6); 05071 outputaddr+=4U; 05072 *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR5); 05073 outputaddr+=4U; 05074 *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR4); 05075 outputaddr+=4U; 05076 } 05077 05078 *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR3); 05079 outputaddr+=4U; 05080 *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR2); 05081 outputaddr+=4U; 05082 *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR1); 05083 outputaddr+=4U; 05084 *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR0); 05085 } 05086 05087 /* In case of ciphering or deciphering, check if all output text has been retrieved; 05088 In case of key derivation, stop right there */ 05089 if ((hcryp->CrypOutCount == 0U) || (hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION)) 05090 { 05091 /* Disable Computation Complete Flag and Errors Interrupts */ 05092 __HAL_CRYP_DISABLE_IT(CRYP_IT_CCFIE|CRYP_IT_ERRIE); 05093 /* Change the CRYP state */ 05094 hcryp->State = HAL_CRYP_STATE_READY; 05095 05096 /* Process Unlocked */ 05097 __HAL_UNLOCK(hcryp); 05098 05099 /* Call computation complete callback */ 05100 HAL_CRYPEx_ComputationCpltCallback(hcryp); 05101 05102 return HAL_OK; 05103 } 05104 /* If suspension flag has been raised, suspend processing */ 05105 else if (hcryp->SuspendRequest == HAL_CRYP_SUSPEND) 05106 { 05107 /* reset ModeSuspend */ 05108 hcryp->SuspendRequest = HAL_CRYP_SUSPEND_NONE; 05109 05110 /* Disable Computation Complete Flag and Errors Interrupts */ 05111 __HAL_CRYP_DISABLE_IT(CRYP_IT_CCFIE|CRYP_IT_ERRIE); 05112 /* Change the CRYP state */ 05113 hcryp->State = HAL_CRYP_STATE_SUSPENDED; 05114 05115 /* Process Unlocked */ 05116 __HAL_UNLOCK(hcryp); 05117 05118 return HAL_OK; 05119 } 05120 else /* Process the rest of input data */ 05121 { 05122 /* Get the Intput data address */ 05123 inputaddr = (uint32_t)hcryp->pCrypInBuffPtr; 05124 05125 /* Increment/decrement instance pointer/counter */ 05126 hcryp->pCrypInBuffPtr += 16U; 05127 hcryp->CrypInCount -= 16U; 05128 05129 /* Write the next input block in the Data Input register */ 05130 hcryp->Instance->DINR = *(uint32_t*)(inputaddr); 05131 inputaddr+=4U; 05132 hcryp->Instance->DINR = *(uint32_t*)(inputaddr); 05133 inputaddr+=4U; 05134 hcryp->Instance->DINR = *(uint32_t*)(inputaddr); 05135 inputaddr+=4U; 05136 hcryp->Instance->DINR = *(uint32_t*)(inputaddr); 05137 05138 return HAL_OK; 05139 } 05140 } 05141 else 05142 { 05143 return HAL_BUSY; 05144 } 05145 } 05146 05147 /** 05148 * @} 05149 */ 05150 05151 #endif /* AES */ 05152 05153 #endif /* HAL_CRYP_MODULE_ENABLED */ 05154 05155 /** 05156 * @} 05157 */ 05158 05159 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/