STM32F479xx 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, de-initialization, set config and get config functions 00009 * + DES/TDES, AES processing functions 00010 * + DMA callback functions 00011 * + CRYP IRQ handler management 00012 * + Peripheral State functions 00013 * 00014 @verbatim 00015 ============================================================================== 00016 ##### How to use this driver ##### 00017 ============================================================================== 00018 [..] 00019 The CRYP HAL driver can be used in CRYP or TinyAES IP as follows: 00020 00021 (#)Initialize the CRYP low level resources by implementing the HAL_CRYP_MspInit(): 00022 (##) Enable the CRYP interface clock using __HAL_RCC_CRYP_CLK_ENABLE()or __HAL_RCC_AES_CLK_ENABLE for TinyAES IP 00023 (##) In case of using interrupts (e.g. HAL_CRYP_Encrypt_IT()) 00024 (+++) Configure the CRYP interrupt priority using HAL_NVIC_SetPriority() 00025 (+++) Enable the CRYP IRQ handler using HAL_NVIC_EnableIRQ() 00026 (+++) In CRYP IRQ handler, call HAL_CRYP_IRQHandler() 00027 (##) In case of using DMA to control data transfer (e.g. HAL_CRYP_Encrypt_DMA()) 00028 (+++) Enable the DMAx interface clock using __RCC_DMAx_CLK_ENABLE() 00029 (+++) Configure and enable two DMA streams one for managing data transfer from 00030 memory to peripheral (input stream) and another stream for managing data 00031 transfer from peripheral to memory (output stream) 00032 (+++) Associate the initialized DMA handle to the CRYP DMA handle 00033 using __HAL_LINKDMA() 00034 (+++) Configure the priority and enable the NVIC for the transfer complete 00035 interrupt on the two DMA Streams. The output stream should have higher 00036 priority than the input stream HAL_NVIC_SetPriority() and HAL_NVIC_EnableIRQ() 00037 00038 (#)Initialize the CRYP according to the specified parameters : 00039 (##) The data type: 1-bit, 8-bit, 16-bit or 32-bit. 00040 (##) The key size: 128, 192 or 256. 00041 (##) The AlgoMode DES/ TDES Algorithm ECB/CBC or AES Algorithm ECB/CBC/CTR/GCM or CCM. 00042 (##) The initialization vector (counter). It is not used in ECB mode. 00043 (##) The key buffer used for encryption/decryption. 00044 (##) The Header used only in AES GCM and CCM Algorithm for authentication. 00045 (##) The HeaderSize The size of header buffer in word. 00046 (##) The B0 block is the first authentication block used only in AES CCM mode. 00047 00048 (#)Three processing (encryption/decryption) functions are available: 00049 (##) Polling mode: encryption and decryption APIs are blocking functions 00050 i.e. they process the data and wait till the processing is finished, 00051 e.g. HAL_CRYP_Encrypt & HAL_CRYP_Decrypt 00052 (##) Interrupt mode: encryption and decryption APIs are not blocking functions 00053 i.e. they process the data under interrupt, 00054 e.g. HAL_CRYP_Encrypt_IT & HAL_CRYP_Decrypt_IT 00055 (##) DMA mode: encryption and decryption APIs are not blocking functions 00056 i.e. the data transfer is ensured by DMA, 00057 e.g. HAL_CRYP_Encrypt_DMA & HAL_CRYP_Decrypt_DMA 00058 00059 (#)When the processing function is called at first time after HAL_CRYP_Init() 00060 the CRYP peripheral is configured and processes the buffer in input. 00061 At second call, no need to Initialize the CRYP, user have to get current configuration via 00062 HAL_CRYP_GetConfig() API, then only HAL_CRYP_SetConfig() is requested to set 00063 new parametres, finally user can start encryption/decryption. 00064 00065 (#)Call HAL_CRYP_DeInit() to deinitialize the CRYP peripheral. 00066 00067 (#)To process a single message with consecutive calls to HAL_CRYP_Encrypt() or HAL_CRYP_Decrypt() 00068 without having to configure again the Key or the Initialization Vector between each API call, 00069 the field KeyIVConfigSkip of the initialization structure must be set to CRYP_KEYIVCONFIG_ONCE. 00070 Same is true for consecutive calls of HAL_CRYP_Encrypt_IT(), HAL_CRYP_Decrypt_IT(), HAL_CRYP_Encrypt_DMA() 00071 or HAL_CRYP_Decrypt_DMA(). 00072 00073 [..] 00074 The cryptographic processor supports following standards: 00075 (#) The data encryption standard (DES) and Triple-DES (TDES) supported only by CRYP1 IP: 00076 (##)64-bit data block processing 00077 (##) chaining modes supported : 00078 (+++) Electronic Code Book(ECB) 00079 (+++) Cipher Block Chaining (CBC) 00080 (##) keys length supported :64-bit, 128-bit and 192-bit. 00081 (#) The advanced encryption standard (AES) supported by CRYP1 & TinyAES IP: 00082 (##)128-bit data block processing 00083 (##) chaining modes supported : 00084 (+++) Electronic Code Book(ECB) 00085 (+++) Cipher Block Chaining (CBC) 00086 (+++) Counter mode (CTR) 00087 (+++) Galois/counter mode (GCM/GMAC) 00088 (+++) Counter with Cipher Block Chaining-Message(CCM) 00089 (##) keys length Supported : 00090 (+++) for CRYP1 IP: 128-bit, 192-bit and 256-bit. 00091 (+++) for TinyAES IP: 128-bit and 256-bit 00092 00093 [..] This section describes the AES Galois/counter mode (GCM) supported by both CRYP1 IP: 00094 (#) Algorithm supported : 00095 (##) Galois/counter mode (GCM) 00096 (##) Galois message authentication code (GMAC) :is exactly the same as 00097 GCM algorithm composed only by an header. 00098 (#) Four phases are performed in GCM : 00099 (##) Init phase: IP prepares the GCM hash subkey (H) and do the IV processing 00100 (##) Header phase: IP processes the Additional Authenticated Data (AAD), with hash 00101 computation only. 00102 (##) Payload phase: IP processes the plaintext (P) with hash computation + keystream 00103 encryption + data XORing. It works in a similar way for ciphertext (C). 00104 (##) Final phase: IP generates the authenticated tag (T) using the last block of data. 00105 (#) structure of message construction in GCM is defined as below : 00106 (##) 16 bytes Initial Counter Block (ICB)composed of IV and counter 00107 (##) The authenticated header A (also knows as Additional Authentication Data AAD) 00108 this part of the message is only authenticated, not encrypted. 00109 (##) The plaintext message P is both authenticated and encrypted as ciphertext. 00110 GCM standard specifies that ciphertext has same bit length as the plaintext. 00111 (##) The last block is composed of the length of A (on 64 bits) and the length of ciphertext 00112 (on 64 bits) 00113 00114 [..] This section describe The AES Counter with Cipher Block Chaining-Message 00115 Authentication Code (CCM) supported by both CRYP1 IP: 00116 (#) Specific parameters for CCM : 00117 00118 (##) B0 block : According to NIST Special Publication 800-38C, 00119 The first block B0 is formatted as follows, where l(m) is encoded in 00120 most-significant-byte first order(see below table 3) 00121 00122 (+++) Q: a bit string representation of the octet length of P (plaintext) 00123 (+++) q The octet length of the binary representation of the octet length of the payload 00124 (+++) A nonce (N), n The octet length of the where n+q=15. 00125 (+++) Flags: most significant octet containing four flags for control information, 00126 (+++) t The octet length of the MAC. 00127 (##) B1 block (header) : associated data length(a) concatenated with Associated Data (A) 00128 the associated data length expressed in bytes (a) defined as below: 00129 (+++) If 0 < a < 216-28, then it is encoded as [a]16, i.e. two octets 00130 (+++) If 216-28 < a < 232, then it is encoded as 0xff || 0xfe || [a]32, i.e. six octets 00131 (+++) If 232 < a < 264, then it is encoded as 0xff || 0xff || [a]64, i.e. ten octets 00132 (##) CTRx block : control blocks 00133 (+++) Generation of CTR1 from first block B0 information : 00134 equal to B0 with first 5 bits zeroed and most significant bits storing octet 00135 length of P also zeroed, then incremented by one ( see below Table 4) 00136 (+++) Generation of CTR0: same as CTR1 with bit[0] set to zero. 00137 00138 (#) Four phases are performed in CCM for CRYP1 IP: 00139 (##) Init phase: IP prepares the GCM hash subkey (H) and do the IV processing 00140 (##) Header phase: IP processes the Additional Authenticated Data (AAD), with hash 00141 computation only. 00142 (##) Payload phase: IP processes the plaintext (P) with hash computation + keystream 00143 encryption + data XORing. It works in a similar way for ciphertext (C). 00144 (##) Final phase: IP generates the authenticated tag (T) using the last block of data. 00145 00146 *** Callback registration *** 00147 ============================================= 00148 00149 The compilation define USE_HAL_CRYP_REGISTER_CALLBACKS when set to 1 00150 allows the user to configure dynamically the driver callbacks. 00151 Use Functions @ref HAL_CRYP_RegisterCallback() or HAL_CRYP_RegisterXXXCallback() 00152 to register an interrupt callback. 00153 00154 Function @ref HAL_CRYP_RegisterCallback() allows to register following callbacks: 00155 (+) InCpltCallback : Input FIFO transfer completed callback. 00156 (+) OutCpltCallback : Output FIFO transfer completed callback. 00157 (+) ErrorCallback : callback for error detection. 00158 (+) MspInitCallback : CRYP MspInit. 00159 (+) MspDeInitCallback : CRYP MspDeInit. 00160 This function takes as parameters the HAL peripheral handle, the Callback ID 00161 and a pointer to the user callback function. 00162 00163 Use function @ref HAL_CRYP_UnRegisterCallback() to reset a callback to the default 00164 weak function. 00165 @ref HAL_CRYP_UnRegisterCallback() takes as parameters the HAL peripheral handle, 00166 and the Callback ID. 00167 This function allows to reset following callbacks: 00168 (+) InCpltCallback : Input FIFO transfer completed callback. 00169 (+) OutCpltCallback : Output FIFO transfer completed callback. 00170 (+) ErrorCallback : callback for error detection. 00171 (+) MspInitCallback : CRYP MspInit. 00172 (+) MspDeInitCallback : CRYP MspDeInit. 00173 00174 By default, after the @ref HAL_CRYP_Init() and when the state is HAL_CRYP_STATE_RESET 00175 all callbacks are set to the corresponding weak functions : 00176 examples @ref HAL_CRYP_InCpltCallback() , @ref HAL_CRYP_OutCpltCallback(). 00177 Exception done for MspInit and MspDeInit functions that are 00178 reset to the legacy weak function in the @ref HAL_CRYP_Init()/ @ref HAL_CRYP_DeInit() only when 00179 these callbacks are null (not registered beforehand). 00180 if not, MspInit or MspDeInit are not null, the @ref HAL_CRYP_Init() / @ref HAL_CRYP_DeInit() 00181 keep and use the user MspInit/MspDeInit functions (registered beforehand) 00182 00183 Callbacks can be registered/unregistered in HAL_CRYP_STATE_READY state only. 00184 Exception done MspInit/MspDeInit callbacks that can be registered/unregistered 00185 in HAL_CRYP_STATE_READY or HAL_CRYP_STATE_RESET state, 00186 thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit. 00187 In that case first register the MspInit/MspDeInit user callbacks 00188 using @ref HAL_CRYP_RegisterCallback() before calling @ref HAL_CRYP_DeInit() 00189 or @ref HAL_CRYP_Init() function. 00190 00191 When The compilation define USE_HAL_CRYP_REGISTER_CALLBACKS is set to 0 or 00192 not defined, the callback registration feature is not available and all callbacks 00193 are set to the corresponding weak functions. 00194 00195 Table 1. Initial Counter Block (ICB) 00196 +-------------------------------------------------------+ 00197 | Initialization vector (IV) | Counter | 00198 |----------------|----------------|-----------|---------| 00199 127 95 63 31 0 00200 00201 00202 Bit Number Register Contents 00203 ---------- --------------- ----------- 00204 127 ...96 CRYP_IV1R[31:0] ICB[127:96] 00205 95 ...64 CRYP_IV1L[31:0] B0[95:64] 00206 63 ... 32 CRYP_IV0R[31:0] ICB[63:32] 00207 31 ... 0 CRYP_IV0L[31:0] ICB[31:0], where 32-bit counter= 0x2 00208 00209 Table 2. GCM last block definition 00210 00211 +-------------------------------------------------------------------+ 00212 | Bit[0] | Bit[32] | Bit[64] | Bit[96] | 00213 |-----------|--------------------|-----------|----------------------| 00214 | 0x0 | Header length[31:0]| 0x0 | Payload length[31:0] | 00215 |-----------|--------------------|-----------|----------------------| 00216 00217 Table 3. B0 block 00218 Octet Number Contents 00219 ------------ --------- 00220 0 Flags 00221 1 ... 15-q Nonce N 00222 16-q ... 15 Q 00223 00224 the Flags field is formatted as follows: 00225 00226 Bit Number Contents 00227 ---------- ---------------------- 00228 7 Reserved (always zero) 00229 6 Adata 00230 5 ... 3 (t-2)/2 00231 2 ... 0 [q-1]3 00232 00233 Table 4. CTRx block 00234 Bit Number Register Contents 00235 ---------- --------------- ----------- 00236 127 ...96 CRYP_IV1R[31:0] B0[127:96], where Q length bits are set to 0, except for 00237 bit 0 that is set to 1 00238 95 ...64 CRYP_IV1L[31:0] B0[95:64] 00239 63 ... 32 CRYP_IV0R[31:0] B0[63:32] 00240 31 ... 0 CRYP_IV0L[31:0] B0[31:0], where flag bits set to 0 00241 00242 @endverbatim 00243 ****************************************************************************** 00244 * @attention 00245 * 00246 * <h2><center>© Copyright (c) 2016 STMicroelectronics. 00247 * All rights reserved.</center></h2> 00248 * 00249 * This software component is licensed by ST under BSD 3-Clause license, 00250 * the "License"; You may not use this file except in compliance with the 00251 * License. You may obtain a copy of the License at: 00252 * opensource.org/licenses/BSD-3-Clause 00253 * 00254 ****************************************************************************** 00255 */ 00256 00257 /* Includes ------------------------------------------------------------------*/ 00258 #include "stm32f4xx_hal.h" 00259 00260 /** @addtogroup STM32F4xx_HAL_Driver 00261 * @{ 00262 */ 00263 00264 #if defined (AES) || defined (CRYP) 00265 00266 /** @defgroup CRYP CRYP 00267 * @brief CRYP HAL module driver. 00268 * @{ 00269 */ 00270 00271 00272 #ifdef HAL_CRYP_MODULE_ENABLED 00273 00274 /* Private typedef -----------------------------------------------------------*/ 00275 /* Private define ------------------------------------------------------------*/ 00276 /** @addtogroup CRYP_Private_Defines 00277 * @{ 00278 */ 00279 #define CRYP_TIMEOUT_KEYPREPARATION 82U /*The latency of key preparation operation is 82 clock cycles.*/ 00280 #define CRYP_TIMEOUT_GCMCCMINITPHASE 299U /* The latency of GCM/CCM init phase to prepare hash subkey is 299 clock cycles.*/ 00281 #define CRYP_TIMEOUT_GCMCCMHEADERPHASE 290U /* The latency of GCM/CCM header phase is 290 clock cycles.*/ 00282 00283 #define CRYP_PHASE_READY 0x00000001U /*!< CRYP peripheral is ready for initialization. */ 00284 #define CRYP_PHASE_PROCESS 0x00000002U /*!< CRYP peripheral is in processing phase */ 00285 00286 #if defined(AES) 00287 #define CRYP_OPERATINGMODE_ENCRYPT 0x00000000U /*!< Encryption mode(Mode 1) */ 00288 #define CRYP_OPERATINGMODE_KEYDERIVATION AES_CR_MODE_0 /*!< Key derivation mode only used when performing ECB and CBC decryptions (Mode 2) */ 00289 #define CRYP_OPERATINGMODE_DECRYPT AES_CR_MODE_1 /*!< Decryption (Mode 3) */ 00290 #define CRYP_OPERATINGMODE_KEYDERIVATION_DECRYPT AES_CR_MODE /*!< Key derivation and decryption only used when performing ECB and CBC decryptions (Mode 4) */ 00291 #define CRYP_PHASE_INIT 0x00000000U /*!< GCM/GMAC (or CCM) init phase */ 00292 #define CRYP_PHASE_HEADER AES_CR_GCMPH_0 /*!< GCM/GMAC or CCM header phase */ 00293 #define CRYP_PHASE_PAYLOAD AES_CR_GCMPH_1 /*!< GCM(/CCM) payload phase */ 00294 #define CRYP_PHASE_FINAL AES_CR_GCMPH /*!< GCM/GMAC or CCM final phase */ 00295 #else /* CRYP */ 00296 #define CRYP_PHASE_INIT 0x00000000U /*!< GCM/GMAC (or CCM) init phase */ 00297 #define CRYP_PHASE_HEADER CRYP_CR_GCM_CCMPH_0 /*!< GCM/GMAC or CCM header phase */ 00298 #define CRYP_PHASE_PAYLOAD CRYP_CR_GCM_CCMPH_1 /*!< GCM(/CCM) payload phase */ 00299 #define CRYP_PHASE_FINAL CRYP_CR_GCM_CCMPH /*!< GCM/GMAC or CCM final phase */ 00300 #define CRYP_OPERATINGMODE_ENCRYPT 0x00000000U /*!< Encryption mode */ 00301 #define CRYP_OPERATINGMODE_DECRYPT CRYP_CR_ALGODIR /*!< Decryption */ 00302 #endif /* End CRYP or AES */ 00303 00304 /* CTR1 information to use in CCM algorithm */ 00305 #define CRYP_CCM_CTR1_0 0x07FFFFFFU 00306 #define CRYP_CCM_CTR1_1 0xFFFFFF00U 00307 #define CRYP_CCM_CTR1_2 0x00000001U 00308 00309 00310 /** 00311 * @} 00312 */ 00313 00314 00315 /* Private macro -------------------------------------------------------------*/ 00316 /** @addtogroup CRYP_Private_Macros 00317 * @{ 00318 */ 00319 00320 #if defined(CRYP) 00321 00322 #define CRYP_SET_PHASE(__HANDLE__, __PHASE__) do{(__HANDLE__)->Instance->CR &= (uint32_t)(~CRYP_CR_GCM_CCMPH);\ 00323 (__HANDLE__)->Instance->CR |= (uint32_t)(__PHASE__);\ 00324 }while(0) 00325 00326 #define HAL_CRYP_FIFO_FLUSH(__HANDLE__) ((__HANDLE__)->Instance->CR |= CRYP_CR_FFLUSH) 00327 00328 #else /*AES*/ 00329 #define CRYP_SET_PHASE(__HANDLE__, __PHASE__) do{(__HANDLE__)->Instance->CR &= (uint32_t)(~AES_CR_GCMPH);\ 00330 (__HANDLE__)->Instance->CR |= (uint32_t)(__PHASE__);\ 00331 }while(0) 00332 #endif /* End AES or CRYP*/ 00333 00334 00335 /** 00336 * @} 00337 */ 00338 00339 /* Private struct -------------------------------------------------------------*/ 00340 /* Private variables ---------------------------------------------------------*/ 00341 /* Private function prototypes -----------------------------------------------*/ 00342 /** @addtogroup CRYP_Private_Functions_prototypes 00343 * @{ 00344 */ 00345 00346 static void CRYP_SetDMAConfig(CRYP_HandleTypeDef *hcryp, uint32_t inputaddr, uint16_t Size, uint32_t outputaddr); 00347 static void CRYP_DMAInCplt(DMA_HandleTypeDef *hdma); 00348 static void CRYP_DMAOutCplt(DMA_HandleTypeDef *hdma); 00349 static void CRYP_DMAError(DMA_HandleTypeDef *hdma); 00350 static void CRYP_SetKey(CRYP_HandleTypeDef *hcryp, uint32_t KeySize); 00351 static void CRYP_AES_IT(CRYP_HandleTypeDef *hcryp); 00352 #if defined (CRYP_CR_ALGOMODE_AES_GCM)|| defined (AES) 00353 static HAL_StatusTypeDef CRYP_GCMCCM_SetHeaderPhase(CRYP_HandleTypeDef *hcryp, uint32_t Timeout); 00354 static void CRYP_GCMCCM_SetPayloadPhase_IT(CRYP_HandleTypeDef *hcryp); 00355 static void CRYP_GCMCCM_SetHeaderPhase_IT(CRYP_HandleTypeDef *hcryp); 00356 static HAL_StatusTypeDef CRYP_GCMCCM_SetHeaderPhase_DMA(CRYP_HandleTypeDef *hcryp); 00357 static void CRYP_Workaround(CRYP_HandleTypeDef *hcryp, uint32_t Timeout); 00358 static HAL_StatusTypeDef CRYP_AESGCM_Process_DMA(CRYP_HandleTypeDef *hcryp); 00359 static HAL_StatusTypeDef CRYP_AESGCM_Process_IT(CRYP_HandleTypeDef *hcryp); 00360 static HAL_StatusTypeDef CRYP_AESGCM_Process(CRYP_HandleTypeDef *hcryp, uint32_t Timeout); 00361 static HAL_StatusTypeDef CRYP_AESCCM_Process(CRYP_HandleTypeDef *hcryp, uint32_t Timeout); 00362 static HAL_StatusTypeDef CRYP_AESCCM_Process_IT(CRYP_HandleTypeDef *hcryp); 00363 static HAL_StatusTypeDef CRYP_AESCCM_Process_DMA(CRYP_HandleTypeDef *hcryp); 00364 #endif /* AES or GCM CCM defined*/ 00365 static void CRYP_AES_ProcessData(CRYP_HandleTypeDef *hcrypt, uint32_t Timeout); 00366 static HAL_StatusTypeDef CRYP_AES_Encrypt(CRYP_HandleTypeDef *hcryp, uint32_t Timeout); 00367 static HAL_StatusTypeDef CRYP_AES_Decrypt(CRYP_HandleTypeDef *hcryp, uint32_t Timeout); 00368 static HAL_StatusTypeDef CRYP_AES_Decrypt_IT(CRYP_HandleTypeDef *hcryp); 00369 static HAL_StatusTypeDef CRYP_AES_Encrypt_IT(CRYP_HandleTypeDef *hcryp); 00370 static HAL_StatusTypeDef CRYP_AES_Decrypt_DMA(CRYP_HandleTypeDef *hcryp); 00371 #if defined (CRYP) 00372 static void CRYP_TDES_IT(CRYP_HandleTypeDef *hcryp); 00373 #if defined (CRYP_CR_ALGOMODE_AES_GCM) 00374 static HAL_StatusTypeDef CRYP_WaitOnIFEMFlag(CRYP_HandleTypeDef *hcryp, uint32_t Timeout); 00375 #endif /* GCM CCM defined*/ 00376 static HAL_StatusTypeDef CRYP_WaitOnBUSYFlag(CRYP_HandleTypeDef *hcryp, uint32_t Timeout); 00377 static HAL_StatusTypeDef CRYP_WaitOnOFNEFlag(CRYP_HandleTypeDef *hcryp, uint32_t Timeout); 00378 static HAL_StatusTypeDef CRYP_TDES_Process(CRYP_HandleTypeDef *hcryp, uint32_t Timeout); 00379 #else /*AES*/ 00380 static HAL_StatusTypeDef CRYP_WaitOnCCFlag(CRYP_HandleTypeDef *hcryp, uint32_t Timeout); 00381 #endif /* End CRYP or AES */ 00382 00383 /** 00384 * @} 00385 */ 00386 00387 /* Exported functions ---------------------------------------------------------*/ 00388 00389 /** @defgroup CRYP_Exported_Functions CRYP Exported Functions 00390 * @{ 00391 */ 00392 00393 00394 /** @defgroup CRYP_Exported_Functions_Group1 Initialization and de-initialization functions 00395 * @brief Initialization and Configuration functions. 00396 * 00397 @verbatim 00398 ======================================================================================== 00399 ##### Initialization, de-initialization and Set and Get configuration functions ##### 00400 ======================================================================================== 00401 [..] This section provides functions allowing to: 00402 (+) Initialize the CRYP 00403 (+) DeInitialize the CRYP 00404 (+) Initialize the CRYP MSP 00405 (+) DeInitialize the CRYP MSP 00406 (+) configure CRYP (HAL_CRYP_SetConfig) with the specified parameters in the CRYP_ConfigTypeDef 00407 Parameters which are configured in This section are : 00408 (+) Key size 00409 (+) Data Type : 32,16, 8 or 1bit 00410 (+) AlgoMode : 00411 - for CRYP1 IP : 00412 ECB and CBC in DES/TDES Standard 00413 ECB,CBC,CTR,GCM/GMAC and CCM in AES Standard. 00414 - for TinyAES2 IP, only ECB,CBC,CTR,GCM/GMAC and CCM in AES Standard are supported. 00415 (+) Get CRYP configuration (HAL_CRYP_GetConfig) from the specified parameters in the CRYP_HandleTypeDef 00416 00417 00418 @endverbatim 00419 * @{ 00420 */ 00421 00422 00423 /** 00424 * @brief Initializes the CRYP according to the specified 00425 * parameters in the CRYP_ConfigTypeDef and creates the associated handle. 00426 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains 00427 * the configuration information for CRYP module 00428 * @retval HAL status 00429 */ 00430 HAL_StatusTypeDef HAL_CRYP_Init(CRYP_HandleTypeDef *hcryp) 00431 { 00432 /* Check the CRYP handle allocation */ 00433 if (hcryp == NULL) 00434 { 00435 return HAL_ERROR; 00436 } 00437 00438 /* Check parameters */ 00439 assert_param(IS_CRYP_KEYSIZE(hcryp->Init.KeySize)); 00440 assert_param(IS_CRYP_DATATYPE(hcryp->Init.DataType)); 00441 assert_param(IS_CRYP_ALGORITHM(hcryp->Init.Algorithm)); 00442 assert_param(IS_CRYP_INIT(hcryp->Init.KeyIVConfigSkip)); 00443 00444 #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1) 00445 if (hcryp->State == HAL_CRYP_STATE_RESET) 00446 { 00447 /* Allocate lock resource and initialize it */ 00448 hcryp->Lock = HAL_UNLOCKED; 00449 00450 hcryp->InCpltCallback = HAL_CRYP_InCpltCallback; /* Legacy weak InCpltCallback */ 00451 hcryp->OutCpltCallback = HAL_CRYP_OutCpltCallback; /* Legacy weak OutCpltCallback */ 00452 hcryp->ErrorCallback = HAL_CRYP_ErrorCallback; /* Legacy weak ErrorCallback */ 00453 00454 if (hcryp->MspInitCallback == NULL) 00455 { 00456 hcryp->MspInitCallback = HAL_CRYP_MspInit; /* Legacy weak MspInit */ 00457 } 00458 00459 /* Init the low level hardware */ 00460 hcryp->MspInitCallback(hcryp); 00461 } 00462 #else 00463 if (hcryp->State == HAL_CRYP_STATE_RESET) 00464 { 00465 /* Allocate lock resource and initialize it */ 00466 hcryp->Lock = HAL_UNLOCKED; 00467 00468 /* Init the low level hardware */ 00469 HAL_CRYP_MspInit(hcryp); 00470 } 00471 #endif /* (USE_HAL_CRYP_REGISTER_CALLBACKS) */ 00472 00473 /* Set the key size(This bit field is don’t care in the DES or TDES modes) data type and Algorithm */ 00474 #if defined (CRYP) 00475 00476 MODIFY_REG(hcryp->Instance->CR, CRYP_CR_DATATYPE | CRYP_CR_KEYSIZE | CRYP_CR_ALGOMODE, 00477 hcryp->Init.DataType | hcryp->Init.KeySize | hcryp->Init.Algorithm); 00478 00479 #else /*AES*/ 00480 00481 MODIFY_REG(hcryp->Instance->CR, AES_CR_DATATYPE | AES_CR_KEYSIZE | AES_CR_CHMOD, 00482 hcryp->Init.DataType | hcryp->Init.KeySize | hcryp->Init.Algorithm); 00483 00484 #endif /* End AES or CRYP*/ 00485 00486 /* Reset Error Code field */ 00487 hcryp->ErrorCode = HAL_CRYP_ERROR_NONE; 00488 00489 /* Change the CRYP state */ 00490 hcryp->State = HAL_CRYP_STATE_READY; 00491 00492 /* Set the default CRYP phase */ 00493 hcryp->Phase = CRYP_PHASE_READY; 00494 00495 /* Return function status */ 00496 return HAL_OK; 00497 } 00498 00499 /** 00500 * @brief De-Initializes the CRYP peripheral. 00501 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains 00502 * the configuration information for CRYP module 00503 * @retval HAL status 00504 */ 00505 HAL_StatusTypeDef HAL_CRYP_DeInit(CRYP_HandleTypeDef *hcryp) 00506 { 00507 /* Check the CRYP handle allocation */ 00508 if (hcryp == NULL) 00509 { 00510 return HAL_ERROR; 00511 } 00512 00513 /* Set the default CRYP phase */ 00514 hcryp->Phase = CRYP_PHASE_READY; 00515 00516 /* Reset CrypInCount and CrypOutCount */ 00517 hcryp->CrypInCount = 0; 00518 hcryp->CrypOutCount = 0; 00519 hcryp->CrypHeaderCount = 0; 00520 00521 /* Disable the CRYP peripheral clock */ 00522 __HAL_CRYP_DISABLE(hcryp); 00523 00524 #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1) 00525 00526 if (hcryp->MspDeInitCallback == NULL) 00527 { 00528 hcryp->MspDeInitCallback = HAL_CRYP_MspDeInit; /* Legacy weak MspDeInit */ 00529 } 00530 /* DeInit the low level hardware */ 00531 hcryp->MspDeInitCallback(hcryp); 00532 00533 #else 00534 00535 /* DeInit the low level hardware: CLOCK, NVIC.*/ 00536 HAL_CRYP_MspDeInit(hcryp); 00537 00538 #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */ 00539 00540 /* Change the CRYP state */ 00541 hcryp->State = HAL_CRYP_STATE_RESET; 00542 00543 /* Release Lock */ 00544 __HAL_UNLOCK(hcryp); 00545 00546 /* Return function status */ 00547 return HAL_OK; 00548 } 00549 00550 /** 00551 * @brief Configure the CRYP according to the specified 00552 * parameters in the CRYP_ConfigTypeDef 00553 * @param hcryp: pointer to a CRYP_HandleTypeDef structure 00554 * @param pConf: pointer to a CRYP_ConfigTypeDef structure that contains 00555 * the configuration information for CRYP module 00556 * @retval HAL status 00557 */ 00558 HAL_StatusTypeDef HAL_CRYP_SetConfig(CRYP_HandleTypeDef *hcryp, CRYP_ConfigTypeDef *pConf) 00559 { 00560 /* Check the CRYP handle allocation */ 00561 if ((hcryp == NULL) || (pConf == NULL)) 00562 { 00563 return HAL_ERROR; 00564 } 00565 00566 /* Check parameters */ 00567 assert_param(IS_CRYP_KEYSIZE(pConf->KeySize)); 00568 assert_param(IS_CRYP_DATATYPE(pConf->DataType)); 00569 assert_param(IS_CRYP_ALGORITHM(pConf->Algorithm)); 00570 00571 if (hcryp->State == HAL_CRYP_STATE_READY) 00572 { 00573 /* Change the CRYP state */ 00574 hcryp->State = HAL_CRYP_STATE_BUSY; 00575 00576 /* Process locked */ 00577 __HAL_LOCK(hcryp); 00578 00579 /* Set CRYP parameters */ 00580 hcryp->Init.DataType = pConf->DataType; 00581 hcryp->Init.pKey = pConf->pKey; 00582 hcryp->Init.Algorithm = pConf->Algorithm; 00583 hcryp->Init.KeySize = pConf->KeySize; 00584 hcryp->Init.pInitVect = pConf->pInitVect; 00585 hcryp->Init.Header = pConf->Header; 00586 hcryp->Init.HeaderSize = pConf->HeaderSize; 00587 hcryp->Init.B0 = pConf->B0; 00588 hcryp->Init.DataWidthUnit = pConf->DataWidthUnit; 00589 hcryp->Init.KeyIVConfigSkip = pConf->KeyIVConfigSkip; 00590 hcryp->Init.HeaderWidthUnit = pConf->HeaderWidthUnit; 00591 00592 /* Set the key size(This bit field is don’t care in the DES or TDES modes) data type, AlgoMode and operating mode*/ 00593 #if defined (CRYP) 00594 00595 MODIFY_REG(hcryp->Instance->CR, CRYP_CR_DATATYPE | CRYP_CR_KEYSIZE | CRYP_CR_ALGOMODE, 00596 hcryp->Init.DataType | hcryp->Init.KeySize | hcryp->Init.Algorithm); 00597 00598 #else /*AES*/ 00599 MODIFY_REG(hcryp->Instance->CR, AES_CR_DATATYPE | AES_CR_KEYSIZE | AES_CR_CHMOD, 00600 hcryp->Init.DataType | hcryp->Init.KeySize | hcryp->Init.Algorithm); 00601 00602 /*clear error flags*/ 00603 __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_ERR_CLEAR); 00604 00605 #endif /* End AES or CRYP */ 00606 00607 /* Process Unlocked */ 00608 __HAL_UNLOCK(hcryp); 00609 00610 /* Reset Error Code field */ 00611 hcryp->ErrorCode = HAL_CRYP_ERROR_NONE; 00612 00613 /* Change the CRYP state */ 00614 hcryp->State = HAL_CRYP_STATE_READY; 00615 00616 /* Set the default CRYP phase */ 00617 hcryp->Phase = CRYP_PHASE_READY; 00618 00619 /* Return function status */ 00620 return HAL_OK; 00621 } 00622 else 00623 { 00624 /* Process Unlocked */ 00625 __HAL_UNLOCK(hcryp); 00626 00627 /* Busy error code field */ 00628 hcryp->ErrorCode |= HAL_CRYP_ERROR_BUSY; 00629 return HAL_ERROR; 00630 } 00631 } 00632 00633 /** 00634 * @brief Get CRYP Configuration parameters in associated handle. 00635 * @param pConf: pointer to a CRYP_ConfigTypeDef structure 00636 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains 00637 * the configuration information for CRYP module 00638 * @retval HAL status 00639 */ 00640 HAL_StatusTypeDef HAL_CRYP_GetConfig(CRYP_HandleTypeDef *hcryp, CRYP_ConfigTypeDef *pConf) 00641 { 00642 /* Check the CRYP handle allocation */ 00643 if ((hcryp == NULL) || (pConf == NULL)) 00644 { 00645 return HAL_ERROR; 00646 } 00647 00648 if (hcryp->State == HAL_CRYP_STATE_READY) 00649 { 00650 /* Change the CRYP state */ 00651 hcryp->State = HAL_CRYP_STATE_BUSY; 00652 00653 /* Process locked */ 00654 __HAL_LOCK(hcryp); 00655 00656 /* Get CRYP parameters */ 00657 pConf->DataType = hcryp->Init.DataType; 00658 pConf->pKey = hcryp->Init.pKey; 00659 pConf->Algorithm = hcryp->Init.Algorithm; 00660 pConf->KeySize = hcryp->Init.KeySize ; 00661 pConf->pInitVect = hcryp->Init.pInitVect; 00662 pConf->Header = hcryp->Init.Header ; 00663 pConf->HeaderSize = hcryp->Init.HeaderSize; 00664 pConf->B0 = hcryp->Init.B0; 00665 pConf->DataWidthUnit = hcryp->Init.DataWidthUnit; 00666 pConf->KeyIVConfigSkip = hcryp->Init.KeyIVConfigSkip; 00667 pConf->HeaderWidthUnit = hcryp->Init.HeaderWidthUnit; 00668 00669 /* Process Unlocked */ 00670 __HAL_UNLOCK(hcryp); 00671 00672 /* Change the CRYP state */ 00673 hcryp->State = HAL_CRYP_STATE_READY; 00674 00675 /* Return function status */ 00676 return HAL_OK; 00677 } 00678 else 00679 { 00680 /* Process Unlocked */ 00681 __HAL_UNLOCK(hcryp); 00682 00683 /* Busy error code field */ 00684 hcryp->ErrorCode |= HAL_CRYP_ERROR_BUSY; 00685 return HAL_ERROR; 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 00699 /* NOTE : This function should not be modified, when the callback is needed, 00700 the HAL_CRYP_MspInit can be implemented in the user file 00701 */ 00702 } 00703 00704 /** 00705 * @brief DeInitializes CRYP MSP. 00706 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains 00707 * the configuration information for CRYP module 00708 * @retval None 00709 */ 00710 __weak void HAL_CRYP_MspDeInit(CRYP_HandleTypeDef *hcryp) 00711 { 00712 /* Prevent unused argument(s) compilation warning */ 00713 UNUSED(hcryp); 00714 00715 /* NOTE : This function should not be modified, when the callback is needed, 00716 the HAL_CRYP_MspDeInit can be implemented in the user file 00717 */ 00718 } 00719 00720 #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1) 00721 /** 00722 * @brief Register a User CRYP Callback 00723 * To be used instead of the weak predefined callback 00724 * @param hcryp cryp handle 00725 * @param CallbackID ID of the callback to be registered 00726 * This parameter can be one of the following values: 00727 * @arg @ref HAL_CRYP_INPUT_COMPLETE_CB_ID Input FIFO transfer completed callback ID 00728 * @arg @ref HAL_CRYP_OUTPUT_COMPLETE_CB_ID Output FIFO transfer completed callback ID 00729 * @arg @ref HAL_CRYP_ERROR_CB_ID Error callback ID 00730 * @arg @ref HAL_CRYP_MSPINIT_CB_ID MspInit callback ID 00731 * @arg @ref HAL_CRYP_MSPDEINIT_CB_ID MspDeInit callback ID 00732 * @param pCallback pointer to the Callback function 00733 * @retval status 00734 */ 00735 HAL_StatusTypeDef HAL_CRYP_RegisterCallback(CRYP_HandleTypeDef *hcryp, HAL_CRYP_CallbackIDTypeDef CallbackID, 00736 pCRYP_CallbackTypeDef pCallback) 00737 { 00738 HAL_StatusTypeDef status = HAL_OK; 00739 00740 if (pCallback == NULL) 00741 { 00742 /* Update the error code */ 00743 hcryp->ErrorCode |= HAL_CRYP_ERROR_INVALID_CALLBACK; 00744 00745 return HAL_ERROR; 00746 } 00747 /* Process locked */ 00748 __HAL_LOCK(hcryp); 00749 00750 if (hcryp->State == HAL_CRYP_STATE_READY) 00751 { 00752 switch (CallbackID) 00753 { 00754 case HAL_CRYP_INPUT_COMPLETE_CB_ID : 00755 hcryp->InCpltCallback = pCallback; 00756 break; 00757 00758 case HAL_CRYP_OUTPUT_COMPLETE_CB_ID : 00759 hcryp->OutCpltCallback = pCallback; 00760 break; 00761 00762 case HAL_CRYP_ERROR_CB_ID : 00763 hcryp->ErrorCallback = pCallback; 00764 break; 00765 00766 case HAL_CRYP_MSPINIT_CB_ID : 00767 hcryp->MspInitCallback = pCallback; 00768 break; 00769 00770 case HAL_CRYP_MSPDEINIT_CB_ID : 00771 hcryp->MspDeInitCallback = pCallback; 00772 break; 00773 00774 default : 00775 /* Update the error code */ 00776 hcryp->ErrorCode |= HAL_CRYP_ERROR_INVALID_CALLBACK; 00777 /* Return error status */ 00778 status = HAL_ERROR; 00779 break; 00780 } 00781 } 00782 else if (hcryp->State == HAL_CRYP_STATE_RESET) 00783 { 00784 switch (CallbackID) 00785 { 00786 case HAL_CRYP_MSPINIT_CB_ID : 00787 hcryp->MspInitCallback = pCallback; 00788 break; 00789 00790 case HAL_CRYP_MSPDEINIT_CB_ID : 00791 hcryp->MspDeInitCallback = pCallback; 00792 break; 00793 00794 default : 00795 /* Update the error code */ 00796 hcryp->ErrorCode |= HAL_CRYP_ERROR_INVALID_CALLBACK; 00797 /* Return error status */ 00798 status = HAL_ERROR; 00799 break; 00800 } 00801 } 00802 else 00803 { 00804 /* Update the error code */ 00805 hcryp->ErrorCode |= HAL_CRYP_ERROR_INVALID_CALLBACK; 00806 /* Return error status */ 00807 status = HAL_ERROR; 00808 } 00809 00810 /* Release Lock */ 00811 __HAL_UNLOCK(hcryp); 00812 00813 return status; 00814 } 00815 00816 /** 00817 * @brief Unregister an CRYP Callback 00818 * CRYP callback is redirected to the weak predefined callback 00819 * @param hcryp cryp handle 00820 * @param CallbackID ID of the callback to be unregistered 00821 * This parameter can be one of the following values: 00822 * @arg @ref HAL_CRYP_INPUT_COMPLETE_CB_ID Input FIFO transfer completed callback ID 00823 * @arg @ref HAL_CRYP_OUTPUT_COMPLETE_CB_ID Output FIFO transfer completed callback ID 00824 * @arg @ref HAL_CRYP_ERROR_CB_ID Error callback ID 00825 * @arg @ref HAL_CRYP_MSPINIT_CB_ID MspInit callback ID 00826 * @arg @ref HAL_CRYP_MSPDEINIT_CB_ID MspDeInit callback ID 00827 * @retval status 00828 */ 00829 HAL_StatusTypeDef HAL_CRYP_UnRegisterCallback(CRYP_HandleTypeDef *hcryp, HAL_CRYP_CallbackIDTypeDef CallbackID) 00830 { 00831 HAL_StatusTypeDef status = HAL_OK; 00832 00833 /* Process locked */ 00834 __HAL_LOCK(hcryp); 00835 00836 if (hcryp->State == HAL_CRYP_STATE_READY) 00837 { 00838 switch (CallbackID) 00839 { 00840 case HAL_CRYP_INPUT_COMPLETE_CB_ID : 00841 hcryp->InCpltCallback = HAL_CRYP_InCpltCallback; /* Legacy weak InCpltCallback */ 00842 break; 00843 00844 case HAL_CRYP_OUTPUT_COMPLETE_CB_ID : 00845 hcryp->OutCpltCallback = HAL_CRYP_OutCpltCallback; /* Legacy weak OutCpltCallback */ 00846 break; 00847 00848 case HAL_CRYP_ERROR_CB_ID : 00849 hcryp->ErrorCallback = HAL_CRYP_ErrorCallback; /* Legacy weak ErrorCallback */ 00850 break; 00851 00852 case HAL_CRYP_MSPINIT_CB_ID : 00853 hcryp->MspInitCallback = HAL_CRYP_MspInit; 00854 break; 00855 00856 case HAL_CRYP_MSPDEINIT_CB_ID : 00857 hcryp->MspDeInitCallback = HAL_CRYP_MspDeInit; 00858 break; 00859 00860 default : 00861 /* Update the error code */ 00862 hcryp->ErrorCode |= HAL_CRYP_ERROR_INVALID_CALLBACK; 00863 /* Return error status */ 00864 status = HAL_ERROR; 00865 break; 00866 } 00867 } 00868 else if (hcryp->State == HAL_CRYP_STATE_RESET) 00869 { 00870 switch (CallbackID) 00871 { 00872 case HAL_CRYP_MSPINIT_CB_ID : 00873 hcryp->MspInitCallback = HAL_CRYP_MspInit; 00874 break; 00875 00876 case HAL_CRYP_MSPDEINIT_CB_ID : 00877 hcryp->MspDeInitCallback = HAL_CRYP_MspDeInit; 00878 break; 00879 00880 default : 00881 /* Update the error code */ 00882 hcryp->ErrorCode |= HAL_CRYP_ERROR_INVALID_CALLBACK; 00883 /* Return error status */ 00884 status = HAL_ERROR; 00885 break; 00886 } 00887 } 00888 else 00889 { 00890 /* Update the error code */ 00891 hcryp->ErrorCode |= HAL_CRYP_ERROR_INVALID_CALLBACK; 00892 /* Return error status */ 00893 status = HAL_ERROR; 00894 } 00895 00896 /* Release Lock */ 00897 __HAL_UNLOCK(hcryp); 00898 00899 return status; 00900 } 00901 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */ 00902 /** 00903 * @} 00904 */ 00905 00906 /** @defgroup CRYP_Exported_Functions_Group2 Encrypt Decrypt functions 00907 * @brief processing functions. 00908 * 00909 @verbatim 00910 ============================================================================== 00911 ##### Encrypt Decrypt functions ##### 00912 ============================================================================== 00913 [..] This section provides API allowing to Encrypt/Decrypt Data following 00914 Standard DES/TDES or AES, and Algorithm configured by the user: 00915 (+) Standard DES/TDES only supported by CRYP1 IP, below list of Algorithm supported : 00916 - Electronic Code Book(ECB) 00917 - Cipher Block Chaining (CBC) 00918 (+) Standard AES supported by CRYP1 IP & TinyAES, list of Algorithm supported: 00919 - Electronic Code Book(ECB) 00920 - Cipher Block Chaining (CBC) 00921 - Counter mode (CTR) 00922 - Cipher Block Chaining (CBC) 00923 - Counter mode (CTR) 00924 - Galois/counter mode (GCM) 00925 - Counter with Cipher Block Chaining-Message(CCM) 00926 [..] Three processing functions are available: 00927 (+) Polling mode : HAL_CRYP_Encrypt & HAL_CRYP_Decrypt 00928 (+) Interrupt mode : HAL_CRYP_Encrypt_IT & HAL_CRYP_Decrypt_IT 00929 (+) DMA mode : HAL_CRYP_Encrypt_DMA & HAL_CRYP_Decrypt_DMA 00930 00931 @endverbatim 00932 * @{ 00933 */ 00934 00935 00936 /** 00937 * @brief Encryption mode. 00938 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains 00939 * the configuration information for CRYP module 00940 * @param Input: Pointer to the input buffer (plaintext) 00941 * @param Size: Length of the plaintext buffer in word. 00942 * @param Output: Pointer to the output buffer(ciphertext) 00943 * @param Timeout: Specify Timeout value 00944 * @retval HAL status 00945 */ 00946 HAL_StatusTypeDef HAL_CRYP_Encrypt(CRYP_HandleTypeDef *hcryp, uint32_t *Input, uint16_t Size, uint32_t *Output, 00947 uint32_t Timeout) 00948 { 00949 uint32_t algo; 00950 HAL_StatusTypeDef status; 00951 00952 if (hcryp->State == HAL_CRYP_STATE_READY) 00953 { 00954 /* Change state Busy */ 00955 hcryp->State = HAL_CRYP_STATE_BUSY; 00956 00957 /* Process locked */ 00958 __HAL_LOCK(hcryp); 00959 00960 /* Reset CrypInCount, CrypOutCount and Initialize pCrypInBuffPtr and pCrypOutBuffPtr parameters*/ 00961 hcryp->CrypInCount = 0U; 00962 hcryp->CrypOutCount = 0U; 00963 hcryp->pCrypInBuffPtr = Input; 00964 hcryp->pCrypOutBuffPtr = Output; 00965 00966 /* Calculate Size parameter in Byte*/ 00967 if (hcryp->Init.DataWidthUnit == CRYP_DATAWIDTHUNIT_WORD) 00968 { 00969 hcryp->Size = Size * 4U; 00970 } 00971 else 00972 { 00973 hcryp->Size = Size; 00974 } 00975 00976 #if defined (CRYP) 00977 /* Set Encryption operating mode*/ 00978 MODIFY_REG(hcryp->Instance->CR, CRYP_CR_ALGODIR, CRYP_OPERATINGMODE_ENCRYPT); 00979 00980 /* algo get algorithm selected */ 00981 algo = hcryp->Instance->CR & CRYP_CR_ALGOMODE; 00982 00983 switch (algo) 00984 { 00985 case CRYP_DES_ECB: 00986 case CRYP_DES_CBC: 00987 case CRYP_TDES_ECB: 00988 case CRYP_TDES_CBC: 00989 00990 /*Set Key */ 00991 hcryp->Instance->K1LR = *(uint32_t *)(hcryp->Init.pKey); 00992 hcryp->Instance->K1RR = *(uint32_t *)(hcryp->Init.pKey + 1); 00993 if ((hcryp->Init.Algorithm == CRYP_TDES_ECB) || (hcryp->Init.Algorithm == CRYP_TDES_CBC)) 00994 { 00995 hcryp->Instance->K2LR = *(uint32_t *)(hcryp->Init.pKey + 2); 00996 hcryp->Instance->K2RR = *(uint32_t *)(hcryp->Init.pKey + 3); 00997 hcryp->Instance->K3LR = *(uint32_t *)(hcryp->Init.pKey + 4); 00998 hcryp->Instance->K3RR = *(uint32_t *)(hcryp->Init.pKey + 5); 00999 } 01000 01001 /*Set Initialization Vector (IV)*/ 01002 if ((hcryp->Init.Algorithm == CRYP_DES_CBC) || (hcryp->Init.Algorithm == CRYP_TDES_CBC)) 01003 { 01004 hcryp->Instance->IV0LR = *(uint32_t *)(hcryp->Init.pInitVect); 01005 hcryp->Instance->IV0RR = *(uint32_t *)(hcryp->Init.pInitVect + 1); 01006 } 01007 01008 /* Flush FIFO */ 01009 HAL_CRYP_FIFO_FLUSH(hcryp); 01010 01011 /* Set the phase */ 01012 hcryp->Phase = CRYP_PHASE_PROCESS; 01013 01014 /* Statrt DES/TDES encryption process */ 01015 status = CRYP_TDES_Process(hcryp, Timeout); 01016 break; 01017 01018 case CRYP_AES_ECB: 01019 case CRYP_AES_CBC: 01020 case CRYP_AES_CTR: 01021 01022 /* AES encryption */ 01023 status = CRYP_AES_Encrypt(hcryp, Timeout); 01024 break; 01025 #if defined (CRYP_CR_ALGOMODE_AES_GCM) 01026 case CRYP_AES_GCM: 01027 01028 /* AES GCM encryption */ 01029 status = CRYP_AESGCM_Process(hcryp, Timeout); 01030 01031 break; 01032 01033 case CRYP_AES_CCM: 01034 01035 /* AES CCM encryption */ 01036 status = CRYP_AESCCM_Process(hcryp, Timeout); 01037 break; 01038 #endif /* GCM CCM defined*/ 01039 default: 01040 hcryp->ErrorCode |= HAL_CRYP_ERROR_NOT_SUPPORTED; 01041 /* Change the CRYP peripheral state */ 01042 hcryp->State = HAL_CRYP_STATE_READY; 01043 /* Process unlocked */ 01044 __HAL_UNLOCK(hcryp); 01045 return HAL_ERROR; 01046 } 01047 01048 #else /*AES*/ 01049 01050 /* Set the operating mode*/ 01051 MODIFY_REG(hcryp->Instance->CR, AES_CR_MODE, CRYP_OPERATINGMODE_ENCRYPT); 01052 01053 /* algo get algorithm selected */ 01054 algo = hcryp->Instance->CR & AES_CR_CHMOD; 01055 01056 switch (algo) 01057 { 01058 01059 case CRYP_AES_ECB: 01060 case CRYP_AES_CBC: 01061 case CRYP_AES_CTR: 01062 01063 /* AES encryption */ 01064 status = CRYP_AES_Encrypt(hcryp, Timeout); 01065 break; 01066 01067 case CRYP_AES_GCM_GMAC: 01068 01069 /* AES GCM encryption */ 01070 status = CRYP_AESGCM_Process(hcryp, Timeout) ; 01071 break; 01072 01073 case CRYP_AES_CCM: 01074 01075 /* AES CCM encryption */ 01076 status = CRYP_AESCCM_Process(hcryp, Timeout); 01077 break; 01078 01079 default: 01080 hcryp->ErrorCode |= HAL_CRYP_ERROR_NOT_SUPPORTED; 01081 /* Change the CRYP peripheral state */ 01082 hcryp->State = HAL_CRYP_STATE_READY; 01083 /* Process unlocked */ 01084 __HAL_UNLOCK(hcryp); 01085 return HAL_ERROR; 01086 } 01087 #endif /*end AES or CRYP */ 01088 01089 if (status == HAL_OK) 01090 { 01091 /* Change the CRYP peripheral state */ 01092 hcryp->State = HAL_CRYP_STATE_READY; 01093 01094 /* Process unlocked */ 01095 __HAL_UNLOCK(hcryp); 01096 } 01097 } 01098 else 01099 { 01100 /* Busy error code field */ 01101 hcryp->ErrorCode |= HAL_CRYP_ERROR_BUSY; 01102 return HAL_ERROR; 01103 } 01104 01105 /* Return function status */ 01106 return HAL_OK; 01107 } 01108 01109 /** 01110 * @brief Decryption mode. 01111 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains 01112 * the configuration information for CRYP module 01113 * @param Input: Pointer to the input buffer (ciphertext ) 01114 * @param Size: Length of the plaintext buffer in word. 01115 * @param Output: Pointer to the output buffer(plaintext) 01116 * @param Timeout: Specify Timeout value 01117 * @retval HAL status 01118 */ 01119 HAL_StatusTypeDef HAL_CRYP_Decrypt(CRYP_HandleTypeDef *hcryp, uint32_t *Input, uint16_t Size, uint32_t *Output, 01120 uint32_t Timeout) 01121 { 01122 HAL_StatusTypeDef status; 01123 uint32_t algo; 01124 01125 if (hcryp->State == HAL_CRYP_STATE_READY) 01126 { 01127 /* Change state Busy */ 01128 hcryp->State = HAL_CRYP_STATE_BUSY; 01129 01130 /* Process locked */ 01131 __HAL_LOCK(hcryp); 01132 01133 /* Reset CrypInCount, CrypOutCount and Initialize pCrypInBuffPtr and pCrypOutBuffPtr parameters*/ 01134 hcryp->CrypInCount = 0U; 01135 hcryp->CrypOutCount = 0U; 01136 hcryp->pCrypInBuffPtr = Input; 01137 hcryp->pCrypOutBuffPtr = Output; 01138 01139 /* Calculate Size parameter in Byte*/ 01140 if (hcryp->Init.DataWidthUnit == CRYP_DATAWIDTHUNIT_WORD) 01141 { 01142 hcryp->Size = Size * 4U; 01143 } 01144 else 01145 { 01146 hcryp->Size = Size; 01147 } 01148 01149 #if defined (CRYP) 01150 01151 /* Set Decryption operating mode*/ 01152 MODIFY_REG(hcryp->Instance->CR, CRYP_CR_ALGODIR, CRYP_OPERATINGMODE_DECRYPT); 01153 01154 /* algo get algorithm selected */ 01155 algo = hcryp->Instance->CR & CRYP_CR_ALGOMODE; 01156 01157 switch (algo) 01158 { 01159 case CRYP_DES_ECB: 01160 case CRYP_DES_CBC: 01161 case CRYP_TDES_ECB: 01162 case CRYP_TDES_CBC: 01163 01164 /*Set Key */ 01165 hcryp->Instance->K1LR = *(uint32_t *)(hcryp->Init.pKey); 01166 hcryp->Instance->K1RR = *(uint32_t *)(hcryp->Init.pKey + 1); 01167 if ((hcryp->Init.Algorithm == CRYP_TDES_ECB) || (hcryp->Init.Algorithm == CRYP_TDES_CBC)) 01168 { 01169 hcryp->Instance->K2LR = *(uint32_t *)(hcryp->Init.pKey + 2); 01170 hcryp->Instance->K2RR = *(uint32_t *)(hcryp->Init.pKey + 3); 01171 hcryp->Instance->K3LR = *(uint32_t *)(hcryp->Init.pKey + 4); 01172 hcryp->Instance->K3RR = *(uint32_t *)(hcryp->Init.pKey + 5); 01173 } 01174 01175 /*Set Initialization Vector (IV)*/ 01176 if ((hcryp->Init.Algorithm == CRYP_DES_CBC) || (hcryp->Init.Algorithm == CRYP_TDES_CBC)) 01177 { 01178 hcryp->Instance->IV0LR = *(uint32_t *)(hcryp->Init.pInitVect); 01179 hcryp->Instance->IV0RR = *(uint32_t *)(hcryp->Init.pInitVect + 1); 01180 } 01181 01182 /* Flush FIFO */ 01183 HAL_CRYP_FIFO_FLUSH(hcryp); 01184 01185 /* Set the phase */ 01186 hcryp->Phase = CRYP_PHASE_PROCESS; 01187 01188 /* Start DES/TDES decryption process */ 01189 status = CRYP_TDES_Process(hcryp, Timeout); 01190 01191 break; 01192 01193 case CRYP_AES_ECB: 01194 case CRYP_AES_CBC: 01195 case CRYP_AES_CTR: 01196 01197 /* AES decryption */ 01198 status = CRYP_AES_Decrypt(hcryp, Timeout); 01199 break; 01200 #if defined (CRYP_CR_ALGOMODE_AES_GCM) 01201 case CRYP_AES_GCM: 01202 01203 /* AES GCM decryption */ 01204 status = CRYP_AESGCM_Process(hcryp, Timeout) ; 01205 break; 01206 01207 case CRYP_AES_CCM: 01208 01209 /* AES CCM decryption */ 01210 status = CRYP_AESCCM_Process(hcryp, Timeout); 01211 break; 01212 #endif /* GCM CCM defined*/ 01213 default: 01214 hcryp->ErrorCode |= HAL_CRYP_ERROR_NOT_SUPPORTED; 01215 /* Change the CRYP peripheral state */ 01216 hcryp->State = HAL_CRYP_STATE_READY; 01217 /* Process unlocked */ 01218 __HAL_UNLOCK(hcryp); 01219 return HAL_ERROR; 01220 } 01221 01222 #else /*AES*/ 01223 01224 /* Set Decryption operating mode*/ 01225 MODIFY_REG(hcryp->Instance->CR, AES_CR_MODE, CRYP_OPERATINGMODE_DECRYPT); 01226 01227 /* algo get algorithm selected */ 01228 algo = hcryp->Instance->CR & AES_CR_CHMOD; 01229 01230 switch (algo) 01231 { 01232 01233 case CRYP_AES_ECB: 01234 case CRYP_AES_CBC: 01235 case CRYP_AES_CTR: 01236 01237 /* AES decryption */ 01238 status = CRYP_AES_Decrypt(hcryp, Timeout); 01239 break; 01240 01241 case CRYP_AES_GCM_GMAC: 01242 01243 /* AES GCM decryption */ 01244 status = CRYP_AESGCM_Process(hcryp, Timeout) ; 01245 break; 01246 01247 case CRYP_AES_CCM: 01248 01249 /* AES CCM decryption */ 01250 status = CRYP_AESCCM_Process(hcryp, Timeout); 01251 break; 01252 01253 default: 01254 hcryp->ErrorCode |= HAL_CRYP_ERROR_NOT_SUPPORTED; 01255 /* Change the CRYP peripheral state */ 01256 hcryp->State = HAL_CRYP_STATE_READY; 01257 /* Process unlocked */ 01258 __HAL_UNLOCK(hcryp); 01259 return HAL_ERROR; 01260 } 01261 #endif /* End AES or CRYP */ 01262 01263 if (status == HAL_OK) 01264 { 01265 /* Change the CRYP peripheral state */ 01266 hcryp->State = HAL_CRYP_STATE_READY; 01267 01268 /* Process unlocked */ 01269 __HAL_UNLOCK(hcryp); 01270 } 01271 } 01272 else 01273 { 01274 /* Busy error code field */ 01275 hcryp->ErrorCode |= HAL_CRYP_ERROR_BUSY; 01276 return HAL_ERROR; 01277 } 01278 01279 /* Return function status */ 01280 return HAL_OK; 01281 } 01282 01283 /** 01284 * @brief Encryption in interrupt mode. 01285 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains 01286 * the configuration information for CRYP module 01287 * @param Input: Pointer to the input buffer (plaintext) 01288 * @param Size: Length of the plaintext buffer in word 01289 * @param Output: Pointer to the output buffer(ciphertext) 01290 * @retval HAL status 01291 */ 01292 HAL_StatusTypeDef HAL_CRYP_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint32_t *Input, uint16_t Size, uint32_t *Output) 01293 { 01294 uint32_t algo; 01295 HAL_StatusTypeDef status = HAL_OK; 01296 01297 if (hcryp->State == HAL_CRYP_STATE_READY) 01298 { 01299 /* Change state Busy */ 01300 hcryp->State = HAL_CRYP_STATE_BUSY; 01301 01302 /* Process locked */ 01303 __HAL_LOCK(hcryp); 01304 01305 /* Reset CrypInCount, CrypOutCount and Initialize pCrypInBuffPtr and pCrypOutBuffPtr parameters*/ 01306 hcryp->CrypInCount = 0U; 01307 hcryp->CrypOutCount = 0U; 01308 hcryp->pCrypInBuffPtr = Input; 01309 hcryp->pCrypOutBuffPtr = Output; 01310 01311 /* Calculate Size parameter in Byte*/ 01312 if (hcryp->Init.DataWidthUnit == CRYP_DATAWIDTHUNIT_WORD) 01313 { 01314 hcryp->Size = Size * 4U; 01315 } 01316 else 01317 { 01318 hcryp->Size = Size; 01319 } 01320 01321 #if defined (CRYP) 01322 01323 /* Set encryption operating mode*/ 01324 MODIFY_REG(hcryp->Instance->CR, CRYP_CR_ALGODIR, CRYP_OPERATINGMODE_ENCRYPT); 01325 01326 /* algo get algorithm selected */ 01327 algo = (hcryp->Instance->CR & CRYP_CR_ALGOMODE); 01328 01329 switch (algo) 01330 { 01331 case CRYP_DES_ECB: 01332 case CRYP_DES_CBC: 01333 case CRYP_TDES_ECB: 01334 case CRYP_TDES_CBC: 01335 01336 /*Set Key */ 01337 hcryp->Instance->K1LR = *(uint32_t *)(hcryp->Init.pKey); 01338 hcryp->Instance->K1RR = *(uint32_t *)(hcryp->Init.pKey + 1); 01339 if ((hcryp->Init.Algorithm == CRYP_TDES_ECB) || (hcryp->Init.Algorithm == CRYP_TDES_CBC)) 01340 { 01341 hcryp->Instance->K2LR = *(uint32_t *)(hcryp->Init.pKey + 2); 01342 hcryp->Instance->K2RR = *(uint32_t *)(hcryp->Init.pKey + 3); 01343 hcryp->Instance->K3LR = *(uint32_t *)(hcryp->Init.pKey + 4); 01344 hcryp->Instance->K3RR = *(uint32_t *)(hcryp->Init.pKey + 5); 01345 } 01346 /* Set the Initialization Vector*/ 01347 if ((hcryp->Init.Algorithm == CRYP_DES_CBC) || (hcryp->Init.Algorithm == CRYP_TDES_CBC)) 01348 { 01349 hcryp->Instance->IV0LR = *(uint32_t *)(hcryp->Init.pInitVect); 01350 hcryp->Instance->IV0RR = *(uint32_t *)(hcryp->Init.pInitVect + 1); 01351 } 01352 01353 /* Flush FIFO */ 01354 HAL_CRYP_FIFO_FLUSH(hcryp); 01355 01356 /* Set the phase */ 01357 hcryp->Phase = CRYP_PHASE_PROCESS; 01358 01359 /* Enable interrupts */ 01360 __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI); 01361 01362 /* Enable CRYP to start DES/TDES process*/ 01363 __HAL_CRYP_ENABLE(hcryp); 01364 break; 01365 01366 case CRYP_AES_ECB: 01367 case CRYP_AES_CBC: 01368 case CRYP_AES_CTR: 01369 01370 status = CRYP_AES_Encrypt_IT(hcryp); 01371 break; 01372 #if defined (CRYP_CR_ALGOMODE_AES_GCM) 01373 case CRYP_AES_GCM: 01374 01375 status = CRYP_AESGCM_Process_IT(hcryp) ; 01376 break; 01377 01378 case CRYP_AES_CCM: 01379 01380 status = CRYP_AESCCM_Process_IT(hcryp); 01381 break; 01382 #endif /* GCM CCM defined*/ 01383 default: 01384 hcryp->ErrorCode |= HAL_CRYP_ERROR_NOT_SUPPORTED; 01385 /* Change the CRYP peripheral state */ 01386 hcryp->State = HAL_CRYP_STATE_READY; 01387 /* Process unlocked */ 01388 __HAL_UNLOCK(hcryp); 01389 status = HAL_ERROR; 01390 break; 01391 } 01392 01393 #else /* AES */ 01394 01395 /* Set encryption operating mode*/ 01396 MODIFY_REG(hcryp->Instance->CR, AES_CR_MODE, CRYP_OPERATINGMODE_ENCRYPT); 01397 01398 /* algo get algorithm selected */ 01399 algo = hcryp->Instance->CR & AES_CR_CHMOD; 01400 01401 switch (algo) 01402 { 01403 case CRYP_AES_ECB: 01404 case CRYP_AES_CBC: 01405 case CRYP_AES_CTR: 01406 01407 /* AES encryption */ 01408 status = CRYP_AES_Encrypt_IT(hcryp); 01409 break; 01410 01411 case CRYP_AES_GCM_GMAC: 01412 01413 /* AES GCM encryption */ 01414 status = CRYP_AESGCM_Process_IT(hcryp) ; 01415 break; 01416 01417 case CRYP_AES_CCM: 01418 01419 /* AES CCM encryption */ 01420 status = CRYP_AESCCM_Process_IT(hcryp); 01421 break; 01422 01423 default: 01424 hcryp->ErrorCode |= HAL_CRYP_ERROR_NOT_SUPPORTED; 01425 /* Change the CRYP peripheral state */ 01426 hcryp->State = HAL_CRYP_STATE_READY; 01427 /* Process unlocked */ 01428 __HAL_UNLOCK(hcryp); 01429 status = HAL_ERROR; 01430 break; 01431 } 01432 #endif /*end AES or CRYP*/ 01433 01434 } 01435 else 01436 { 01437 /* Busy error code field */ 01438 hcryp->ErrorCode |= HAL_CRYP_ERROR_BUSY; 01439 status = HAL_ERROR; 01440 } 01441 01442 /* Return function status */ 01443 return status; 01444 } 01445 01446 /** 01447 * @brief Decryption in itnterrupt mode. 01448 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains 01449 * the configuration information for CRYP module 01450 * @param Input: Pointer to the input buffer (ciphertext ) 01451 * @param Size: Length of the plaintext buffer in word. 01452 * @param Output: Pointer to the output buffer(plaintext) 01453 * @retval HAL status 01454 */ 01455 HAL_StatusTypeDef HAL_CRYP_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint32_t *Input, uint16_t Size, uint32_t *Output) 01456 { 01457 uint32_t algo; 01458 HAL_StatusTypeDef status = HAL_OK; 01459 01460 if (hcryp->State == HAL_CRYP_STATE_READY) 01461 { 01462 /* Change state Busy */ 01463 hcryp->State = HAL_CRYP_STATE_BUSY; 01464 01465 /* Process locked */ 01466 __HAL_LOCK(hcryp); 01467 01468 /* Reset CrypInCount, CrypOutCount and Initialize pCrypInBuffPtr and pCrypOutBuffPtr parameters*/ 01469 hcryp->CrypInCount = 0U; 01470 hcryp->CrypOutCount = 0U; 01471 hcryp->pCrypInBuffPtr = Input; 01472 hcryp->pCrypOutBuffPtr = Output; 01473 01474 /* Calculate Size parameter in Byte*/ 01475 if (hcryp->Init.DataWidthUnit == CRYP_DATAWIDTHUNIT_WORD) 01476 { 01477 hcryp->Size = Size * 4U; 01478 } 01479 else 01480 { 01481 hcryp->Size = Size; 01482 } 01483 01484 #if defined (CRYP) 01485 01486 /* Set decryption operating mode*/ 01487 MODIFY_REG(hcryp->Instance->CR, CRYP_CR_ALGODIR, CRYP_OPERATINGMODE_DECRYPT); 01488 01489 /* algo get algorithm selected */ 01490 algo = hcryp->Instance->CR & CRYP_CR_ALGOMODE; 01491 01492 switch (algo) 01493 { 01494 case CRYP_DES_ECB: 01495 case CRYP_DES_CBC: 01496 case CRYP_TDES_ECB: 01497 case CRYP_TDES_CBC: 01498 01499 /*Set Key */ 01500 hcryp->Instance->K1LR = *(uint32_t *)(hcryp->Init.pKey); 01501 hcryp->Instance->K1RR = *(uint32_t *)(hcryp->Init.pKey + 1); 01502 if ((hcryp->Init.Algorithm == CRYP_TDES_ECB) || (hcryp->Init.Algorithm == CRYP_TDES_CBC)) 01503 { 01504 hcryp->Instance->K2LR = *(uint32_t *)(hcryp->Init.pKey + 2); 01505 hcryp->Instance->K2RR = *(uint32_t *)(hcryp->Init.pKey + 3); 01506 hcryp->Instance->K3LR = *(uint32_t *)(hcryp->Init.pKey + 4); 01507 hcryp->Instance->K3RR = *(uint32_t *)(hcryp->Init.pKey + 5); 01508 } 01509 01510 /* Set the Initialization Vector*/ 01511 if ((hcryp->Init.Algorithm == CRYP_DES_CBC) || (hcryp->Init.Algorithm == CRYP_TDES_CBC)) 01512 { 01513 hcryp->Instance->IV0LR = *(uint32_t *)(hcryp->Init.pInitVect); 01514 hcryp->Instance->IV0RR = *(uint32_t *)(hcryp->Init.pInitVect + 1); 01515 } 01516 /* Flush FIFO */ 01517 HAL_CRYP_FIFO_FLUSH(hcryp); 01518 01519 /* Set the phase */ 01520 hcryp->Phase = CRYP_PHASE_PROCESS; 01521 01522 /* Enable interrupts */ 01523 __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI); 01524 01525 /* Enable CRYP and start DES/TDES process*/ 01526 __HAL_CRYP_ENABLE(hcryp); 01527 01528 break; 01529 01530 case CRYP_AES_ECB: 01531 case CRYP_AES_CBC: 01532 case CRYP_AES_CTR: 01533 01534 /* AES decryption */ 01535 status = CRYP_AES_Decrypt_IT(hcryp); 01536 break; 01537 #if defined (CRYP_CR_ALGOMODE_AES_GCM) 01538 case CRYP_AES_GCM: 01539 01540 /* AES GCM decryption */ 01541 status = CRYP_AESGCM_Process_IT(hcryp) ; 01542 break; 01543 01544 case CRYP_AES_CCM: 01545 01546 /* AES CCMdecryption */ 01547 status = CRYP_AESCCM_Process_IT(hcryp); 01548 break; 01549 #endif /* GCM CCM defined*/ 01550 default: 01551 hcryp->ErrorCode |= HAL_CRYP_ERROR_NOT_SUPPORTED; 01552 /* Change the CRYP peripheral state */ 01553 hcryp->State = HAL_CRYP_STATE_READY; 01554 /* Process unlocked */ 01555 __HAL_UNLOCK(hcryp); 01556 status = HAL_ERROR; 01557 break; 01558 } 01559 01560 #else /*AES*/ 01561 01562 /* Set decryption operating mode*/ 01563 MODIFY_REG(hcryp->Instance->CR, AES_CR_MODE, CRYP_OPERATINGMODE_DECRYPT); 01564 01565 /* algo get algorithm selected */ 01566 algo = hcryp->Instance->CR & AES_CR_CHMOD; 01567 01568 switch (algo) 01569 { 01570 case CRYP_AES_ECB: 01571 case CRYP_AES_CBC: 01572 case CRYP_AES_CTR: 01573 01574 /* AES decryption */ 01575 status = CRYP_AES_Decrypt_IT(hcryp); 01576 break; 01577 01578 case CRYP_AES_GCM_GMAC: 01579 01580 /* AES GCM decryption */ 01581 status = CRYP_AESGCM_Process_IT(hcryp) ; 01582 break; 01583 01584 case CRYP_AES_CCM: 01585 01586 /* AES CCM decryption */ 01587 status = CRYP_AESCCM_Process_IT(hcryp); 01588 break; 01589 01590 default: 01591 hcryp->ErrorCode |= HAL_CRYP_ERROR_NOT_SUPPORTED; 01592 /* Change the CRYP peripheral state */ 01593 hcryp->State = HAL_CRYP_STATE_READY; 01594 /* Process unlocked */ 01595 __HAL_UNLOCK(hcryp); 01596 status = HAL_ERROR; 01597 break; 01598 } 01599 #endif /* End AES or CRYP */ 01600 01601 } 01602 else 01603 { 01604 /* Busy error code field */ 01605 hcryp->ErrorCode |= HAL_CRYP_ERROR_BUSY; 01606 status = HAL_ERROR; 01607 } 01608 01609 /* Return function status */ 01610 return status; 01611 } 01612 01613 /** 01614 * @brief Encryption in DMA mode. 01615 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains 01616 * the configuration information for CRYP module 01617 * @param Input: Pointer to the input buffer (plaintext) 01618 * @param Size: Length of the plaintext buffer in word. 01619 * @param Output: Pointer to the output buffer(ciphertext) 01620 * @retval HAL status 01621 */ 01622 HAL_StatusTypeDef HAL_CRYP_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint32_t *Input, uint16_t Size, uint32_t *Output) 01623 { 01624 uint32_t algo; 01625 HAL_StatusTypeDef status = HAL_OK; 01626 uint32_t DoKeyIVConfig = 1U; /* By default, carry out peripheral Key and IV configuration */ 01627 01628 if (hcryp->State == HAL_CRYP_STATE_READY) 01629 { 01630 /* Change state Busy */ 01631 hcryp->State = HAL_CRYP_STATE_BUSY; 01632 01633 /* Process locked */ 01634 __HAL_LOCK(hcryp); 01635 01636 /* Reset CrypInCount, CrypOutCount and Initialize pCrypInBuffPtr and pCrypOutBuffPtr parameters*/ 01637 hcryp->CrypInCount = 0U; 01638 hcryp->CrypOutCount = 0U; 01639 hcryp->pCrypInBuffPtr = Input; 01640 hcryp->pCrypOutBuffPtr = Output; 01641 01642 /* Calculate Size parameter in Byte*/ 01643 if (hcryp->Init.DataWidthUnit == CRYP_DATAWIDTHUNIT_WORD) 01644 { 01645 hcryp->Size = Size * 4U; 01646 } 01647 else 01648 { 01649 hcryp->Size = Size; 01650 } 01651 01652 #if defined (CRYP) 01653 01654 /* Set encryption operating mode*/ 01655 MODIFY_REG(hcryp->Instance->CR, CRYP_CR_ALGODIR, CRYP_OPERATINGMODE_ENCRYPT); 01656 01657 /* algo get algorithm selected */ 01658 algo = hcryp->Instance->CR & CRYP_CR_ALGOMODE; 01659 01660 switch (algo) 01661 { 01662 case CRYP_DES_ECB: 01663 case CRYP_DES_CBC: 01664 case CRYP_TDES_ECB: 01665 case CRYP_TDES_CBC: 01666 01667 /*Set Key */ 01668 hcryp->Instance->K1LR = *(uint32_t *)(hcryp->Init.pKey); 01669 hcryp->Instance->K1RR = *(uint32_t *)(hcryp->Init.pKey + 1); 01670 if ((hcryp->Init.Algorithm == CRYP_TDES_ECB) || (hcryp->Init.Algorithm == CRYP_TDES_CBC)) 01671 { 01672 hcryp->Instance->K2LR = *(uint32_t *)(hcryp->Init.pKey + 2); 01673 hcryp->Instance->K2RR = *(uint32_t *)(hcryp->Init.pKey + 3); 01674 hcryp->Instance->K3LR = *(uint32_t *)(hcryp->Init.pKey + 4); 01675 hcryp->Instance->K3RR = *(uint32_t *)(hcryp->Init.pKey + 5); 01676 } 01677 01678 /* Set the Initialization Vector*/ 01679 if ((hcryp->Init.Algorithm == CRYP_DES_CBC) || (hcryp->Init.Algorithm == CRYP_TDES_CBC)) 01680 { 01681 hcryp->Instance->IV0LR = *(uint32_t *)(hcryp->Init.pInitVect); 01682 hcryp->Instance->IV0RR = *(uint32_t *)(hcryp->Init.pInitVect + 1); 01683 } 01684 01685 /* Flush FIFO */ 01686 HAL_CRYP_FIFO_FLUSH(hcryp); 01687 01688 /* Set the phase */ 01689 hcryp->Phase = CRYP_PHASE_PROCESS; 01690 01691 /* Start DMA process transfer for DES/TDES */ 01692 CRYP_SetDMAConfig(hcryp, (uint32_t)(hcryp->pCrypInBuffPtr), ((uint16_t)(hcryp->Size) / 4U), 01693 (uint32_t)(hcryp->pCrypOutBuffPtr)); 01694 break; 01695 01696 case CRYP_AES_ECB: 01697 case CRYP_AES_CBC: 01698 case CRYP_AES_CTR: 01699 01700 if (hcryp->Init.KeyIVConfigSkip == CRYP_KEYIVCONFIG_ONCE) 01701 { 01702 if (hcryp->KeyIVConfig == 1U) 01703 { 01704 /* If the Key and IV configuration has to be done only once 01705 and if it has already been done, skip it */ 01706 DoKeyIVConfig = 0U; 01707 } 01708 else 01709 { 01710 /* If the Key and IV configuration has to be done only once 01711 and if it has not been done already, do it and set KeyIVConfig 01712 to keep track it won't have to be done again next time */ 01713 hcryp->KeyIVConfig = 1U; 01714 } 01715 } 01716 01717 if (DoKeyIVConfig == 1U) 01718 { 01719 /* Set the Key*/ 01720 CRYP_SetKey(hcryp, hcryp->Init.KeySize); 01721 01722 /* Set the Initialization Vector*/ 01723 if (hcryp->Init.Algorithm != CRYP_AES_ECB) 01724 { 01725 hcryp->Instance->IV0LR = *(uint32_t *)(hcryp->Init.pInitVect); 01726 hcryp->Instance->IV0RR = *(uint32_t *)(hcryp->Init.pInitVect + 1U); 01727 hcryp->Instance->IV1LR = *(uint32_t *)(hcryp->Init.pInitVect + 2U); 01728 hcryp->Instance->IV1RR = *(uint32_t *)(hcryp->Init.pInitVect + 3U); 01729 } 01730 } /* if (DoKeyIVConfig == 1U) */ 01731 01732 /* Set the phase */ 01733 hcryp->Phase = CRYP_PHASE_PROCESS; 01734 01735 /* Start DMA process transfer for AES */ 01736 CRYP_SetDMAConfig(hcryp, (uint32_t)(hcryp->pCrypInBuffPtr), ((uint16_t)(hcryp->Size) / 4U), 01737 (uint32_t)(hcryp->pCrypOutBuffPtr)); 01738 break; 01739 #if defined (CRYP_CR_ALGOMODE_AES_GCM) 01740 case CRYP_AES_GCM: 01741 /* AES GCM encryption */ 01742 status = CRYP_AESGCM_Process_DMA(hcryp) ; 01743 break; 01744 01745 case CRYP_AES_CCM: 01746 /* AES CCM encryption */ 01747 status = CRYP_AESCCM_Process_DMA(hcryp); 01748 break; 01749 #endif /* GCM CCM defined*/ 01750 default: 01751 hcryp->ErrorCode |= HAL_CRYP_ERROR_NOT_SUPPORTED; 01752 /* Change the CRYP peripheral state */ 01753 hcryp->State = HAL_CRYP_STATE_READY; 01754 /* Process unlocked */ 01755 __HAL_UNLOCK(hcryp); 01756 status = HAL_ERROR; 01757 break; 01758 } 01759 01760 #else /*AES*/ 01761 /* Set encryption operating mode*/ 01762 MODIFY_REG(hcryp->Instance->CR, AES_CR_MODE, CRYP_OPERATINGMODE_ENCRYPT); 01763 01764 /* algo get algorithm selected */ 01765 algo = hcryp->Instance->CR & AES_CR_CHMOD; 01766 01767 switch (algo) 01768 { 01769 01770 case CRYP_AES_ECB: 01771 case CRYP_AES_CBC: 01772 case CRYP_AES_CTR: 01773 01774 if (hcryp->Init.KeyIVConfigSkip == CRYP_KEYIVCONFIG_ONCE) 01775 { 01776 if (hcryp->KeyIVConfig == 1U) 01777 { 01778 /* If the Key and IV configuration has to be done only once 01779 and if it has already been done, skip it */ 01780 DoKeyIVConfig = 0U; 01781 } 01782 else 01783 { 01784 /* If the Key and IV configuration has to be done only once 01785 and if it has not been done already, do it and set KeyIVConfig 01786 to keep track it won't have to be done again next time */ 01787 hcryp->KeyIVConfig = 1U; 01788 } 01789 } 01790 01791 if (DoKeyIVConfig == 1U) 01792 { 01793 /* Set the Key*/ 01794 CRYP_SetKey(hcryp, hcryp->Init.KeySize); 01795 01796 /* Set the Initialization Vector*/ 01797 if (hcryp->Init.Algorithm != CRYP_AES_ECB) 01798 { 01799 hcryp->Instance->IVR3 = *(uint32_t *)(hcryp->Init.pInitVect); 01800 hcryp->Instance->IVR2 = *(uint32_t *)(hcryp->Init.pInitVect + 1); 01801 hcryp->Instance->IVR1 = *(uint32_t *)(hcryp->Init.pInitVect + 2); 01802 hcryp->Instance->IVR0 = *(uint32_t *)(hcryp->Init.pInitVect + 3); 01803 } 01804 } /* if (DoKeyIVConfig == 1U) */ 01805 /* Set the phase */ 01806 hcryp->Phase = CRYP_PHASE_PROCESS; 01807 01808 /* Start DMA process transfer for AES */ 01809 CRYP_SetDMAConfig(hcryp, (uint32_t)(hcryp->pCrypInBuffPtr), (hcryp->Size / 4U), (uint32_t)(hcryp->pCrypOutBuffPtr)); 01810 break; 01811 01812 case CRYP_AES_GCM_GMAC: 01813 /* AES GCM encryption */ 01814 status = CRYP_AESGCM_Process_DMA(hcryp) ; 01815 break; 01816 01817 case CRYP_AES_CCM: 01818 /* AES CCM encryption */ 01819 status = CRYP_AESCCM_Process_DMA(hcryp); 01820 break; 01821 01822 default: 01823 hcryp->ErrorCode |= HAL_CRYP_ERROR_NOT_SUPPORTED; 01824 /* Change the CRYP peripheral state */ 01825 hcryp->State = HAL_CRYP_STATE_READY; 01826 /* Process unlocked */ 01827 __HAL_UNLOCK(hcryp); 01828 status = HAL_ERROR; 01829 break; 01830 } 01831 #endif /* End AES or CRYP */ 01832 01833 } 01834 else 01835 { 01836 /* Busy error code field */ 01837 hcryp->ErrorCode |= HAL_CRYP_ERROR_BUSY; 01838 status = HAL_ERROR; 01839 } 01840 01841 /* Return function status */ 01842 return status; 01843 } 01844 01845 /** 01846 * @brief Decryption in DMA mode. 01847 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains 01848 * the configuration information for CRYP module 01849 * @param Input: Pointer to the input buffer (ciphertext ) 01850 * @param Size: Length of the plaintext buffer in word 01851 * @param Output: Pointer to the output buffer(plaintext) 01852 * @retval HAL status 01853 */ 01854 HAL_StatusTypeDef HAL_CRYP_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint32_t *Input, uint16_t Size, uint32_t *Output) 01855 { 01856 uint32_t algo; 01857 HAL_StatusTypeDef status = HAL_OK; 01858 01859 if (hcryp->State == HAL_CRYP_STATE_READY) 01860 { 01861 01862 /* Change state Busy */ 01863 hcryp->State = HAL_CRYP_STATE_BUSY; 01864 01865 /* Process locked */ 01866 __HAL_LOCK(hcryp); 01867 01868 /* Reset CrypInCount, CrypOutCount and Initialize pCrypInBuffPtr, pCrypOutBuffPtr and Size parameters*/ 01869 hcryp->CrypInCount = 0U; 01870 hcryp->CrypOutCount = 0U; 01871 hcryp->pCrypInBuffPtr = Input; 01872 hcryp->pCrypOutBuffPtr = Output; 01873 01874 /* Calculate Size parameter in Byte*/ 01875 if (hcryp->Init.DataWidthUnit == CRYP_DATAWIDTHUNIT_WORD) 01876 { 01877 hcryp->Size = Size * 4U; 01878 } 01879 else 01880 { 01881 hcryp->Size = Size; 01882 } 01883 01884 #if defined (CRYP) 01885 01886 /* Set decryption operating mode*/ 01887 MODIFY_REG(hcryp->Instance->CR, CRYP_CR_ALGODIR, CRYP_OPERATINGMODE_DECRYPT); 01888 01889 /* algo get algorithm selected */ 01890 algo = hcryp->Instance->CR & CRYP_CR_ALGOMODE; 01891 01892 switch (algo) 01893 { 01894 case CRYP_DES_ECB: 01895 case CRYP_DES_CBC: 01896 case CRYP_TDES_ECB: 01897 case CRYP_TDES_CBC: 01898 01899 /*Set Key */ 01900 hcryp->Instance->K1LR = *(uint32_t *)(hcryp->Init.pKey); 01901 hcryp->Instance->K1RR = *(uint32_t *)(hcryp->Init.pKey + 1); 01902 if ((hcryp->Init.Algorithm == CRYP_TDES_ECB) || (hcryp->Init.Algorithm == CRYP_TDES_CBC)) 01903 { 01904 hcryp->Instance->K2LR = *(uint32_t *)(hcryp->Init.pKey + 2); 01905 hcryp->Instance->K2RR = *(uint32_t *)(hcryp->Init.pKey + 3); 01906 hcryp->Instance->K3LR = *(uint32_t *)(hcryp->Init.pKey + 4); 01907 hcryp->Instance->K3RR = *(uint32_t *)(hcryp->Init.pKey + 5); 01908 } 01909 01910 /* Set the Initialization Vector*/ 01911 if ((hcryp->Init.Algorithm == CRYP_DES_CBC) || (hcryp->Init.Algorithm == CRYP_TDES_CBC)) 01912 { 01913 hcryp->Instance->IV0LR = *(uint32_t *)(hcryp->Init.pInitVect); 01914 hcryp->Instance->IV0RR = *(uint32_t *)(hcryp->Init.pInitVect + 1); 01915 } 01916 01917 /* Flush FIFO */ 01918 HAL_CRYP_FIFO_FLUSH(hcryp); 01919 01920 /* Set the phase */ 01921 hcryp->Phase = CRYP_PHASE_PROCESS; 01922 01923 /* Start DMA process transfer for DES/TDES */ 01924 CRYP_SetDMAConfig(hcryp, (uint32_t)(hcryp->pCrypInBuffPtr), ((uint16_t)(hcryp->Size) / 4U), 01925 (uint32_t)(hcryp->pCrypOutBuffPtr)); 01926 break; 01927 01928 case CRYP_AES_ECB: 01929 case CRYP_AES_CBC: 01930 case CRYP_AES_CTR: 01931 01932 /* AES decryption */ 01933 status = CRYP_AES_Decrypt_DMA(hcryp); 01934 break; 01935 #if defined (CRYP_CR_ALGOMODE_AES_GCM) 01936 case CRYP_AES_GCM: 01937 /* AES GCM decryption */ 01938 status = CRYP_AESGCM_Process_DMA(hcryp) ; 01939 break; 01940 01941 case CRYP_AES_CCM: 01942 /* AES CCM decryption */ 01943 status = CRYP_AESCCM_Process_DMA(hcryp); 01944 break; 01945 #endif /* GCM CCM defined*/ 01946 default: 01947 hcryp->ErrorCode |= HAL_CRYP_ERROR_NOT_SUPPORTED; 01948 /* Change the CRYP peripheral state */ 01949 hcryp->State = HAL_CRYP_STATE_READY; 01950 /* Process unlocked */ 01951 __HAL_UNLOCK(hcryp); 01952 status = HAL_ERROR; 01953 break; 01954 } 01955 01956 #else /*AES*/ 01957 01958 /* Set decryption operating mode*/ 01959 MODIFY_REG(hcryp->Instance->CR, AES_CR_MODE, CRYP_OPERATINGMODE_DECRYPT); 01960 01961 /* algo get algorithm selected */ 01962 algo = hcryp->Instance->CR & AES_CR_CHMOD; 01963 01964 switch (algo) 01965 { 01966 01967 case CRYP_AES_ECB: 01968 case CRYP_AES_CBC: 01969 case CRYP_AES_CTR: 01970 01971 /* AES decryption */ 01972 status = CRYP_AES_Decrypt_DMA(hcryp); 01973 break; 01974 01975 case CRYP_AES_GCM_GMAC: 01976 /* AES GCM decryption */ 01977 status = CRYP_AESGCM_Process_DMA(hcryp) ; 01978 break; 01979 01980 case CRYP_AES_CCM: 01981 /* AES CCM decryption */ 01982 status = CRYP_AESCCM_Process_DMA(hcryp); 01983 break; 01984 01985 default: 01986 hcryp->ErrorCode |= HAL_CRYP_ERROR_NOT_SUPPORTED; 01987 /* Change the CRYP peripheral state */ 01988 hcryp->State = HAL_CRYP_STATE_READY; 01989 /* Process unlocked */ 01990 __HAL_UNLOCK(hcryp); 01991 status = HAL_ERROR; 01992 break; 01993 } 01994 #endif /* End AES or CRYP */ 01995 } 01996 else 01997 { 01998 /* Busy error code field */ 01999 hcryp->ErrorCode |= HAL_CRYP_ERROR_BUSY; 02000 status = HAL_ERROR; 02001 } 02002 02003 /* Return function status */ 02004 return status; 02005 } 02006 02007 /** 02008 * @} 02009 */ 02010 02011 /** @defgroup CRYP_Exported_Functions_Group3 CRYP IRQ handler management 02012 * @brief CRYP IRQ handler. 02013 * 02014 @verbatim 02015 ============================================================================== 02016 ##### CRYP IRQ handler management ##### 02017 ============================================================================== 02018 [..] This section provides CRYP IRQ handler and callback functions. 02019 (+) HAL_CRYP_IRQHandler CRYP interrupt request 02020 (+) HAL_CRYP_InCpltCallback input data transfer complete callback 02021 (+) HAL_CRYP_OutCpltCallback output data transfer complete callback 02022 (+) HAL_CRYP_ErrorCallback CRYP error callback 02023 (+) HAL_CRYP_GetState return the CRYP state 02024 (+) HAL_CRYP_GetError return the CRYP error code 02025 @endverbatim 02026 * @{ 02027 */ 02028 02029 /** 02030 * @brief This function handles cryptographic interrupt request. 02031 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains 02032 * the configuration information for CRYP module 02033 * @retval None 02034 */ 02035 void HAL_CRYP_IRQHandler(CRYP_HandleTypeDef *hcryp) 02036 { 02037 02038 #if defined (CRYP) 02039 02040 uint32_t itstatus = hcryp->Instance->MISR; 02041 02042 if ((itstatus & (CRYP_IT_INI | CRYP_IT_OUTI)) != 0U) 02043 { 02044 if ((hcryp->Init.Algorithm == CRYP_DES_ECB) || (hcryp->Init.Algorithm == CRYP_DES_CBC) 02045 || (hcryp->Init.Algorithm == CRYP_TDES_ECB) || (hcryp->Init.Algorithm == CRYP_TDES_CBC)) 02046 { 02047 CRYP_TDES_IT(hcryp); /* DES or TDES*/ 02048 } 02049 else if ((hcryp->Init.Algorithm == CRYP_AES_ECB) || (hcryp->Init.Algorithm == CRYP_AES_CBC) 02050 || (hcryp->Init.Algorithm == CRYP_AES_CTR)) 02051 { 02052 CRYP_AES_IT(hcryp); /*AES*/ 02053 } 02054 #if defined (CRYP_CR_ALGOMODE_AES_GCM) 02055 else if ((hcryp->Init.Algorithm == CRYP_AES_GCM) || (hcryp->Init.Algorithm == CRYP_CR_ALGOMODE_AES_CCM)) 02056 { 02057 /* if header phase */ 02058 if ((hcryp->Instance->CR & CRYP_PHASE_HEADER) == CRYP_PHASE_HEADER) 02059 { 02060 CRYP_GCMCCM_SetHeaderPhase_IT(hcryp); 02061 } 02062 else /* if payload phase */ 02063 { 02064 CRYP_GCMCCM_SetPayloadPhase_IT(hcryp); 02065 } 02066 } 02067 #endif /* GCM CCM defined*/ 02068 else 02069 { 02070 /* Nothing to do */ 02071 } 02072 } 02073 02074 #else /*AES*/ 02075 if (__HAL_CRYP_GET_FLAG(hcryp, CRYP_IT_CCF) != RESET) 02076 { 02077 if (__HAL_CRYP_GET_IT_SOURCE(hcryp, CRYP_IT_CCFIE) != RESET) 02078 { 02079 02080 /* Clear computation complete flag */ 02081 __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR); 02082 02083 if (hcryp->Init.Algorithm == CRYP_AES_GCM_GMAC) 02084 { 02085 02086 /* if header phase */ 02087 if ((hcryp->Instance->CR & CRYP_PHASE_HEADER) == CRYP_PHASE_HEADER) 02088 { 02089 CRYP_GCMCCM_SetHeaderPhase_IT(hcryp); 02090 } 02091 else /* if payload phase */ 02092 { 02093 CRYP_GCMCCM_SetPayloadPhase_IT(hcryp); 02094 } 02095 } 02096 else if (hcryp->Init.Algorithm == CRYP_AES_CCM) 02097 { 02098 /* if header phase */ 02099 if (hcryp->Init.HeaderSize >= hcryp->CrypHeaderCount) 02100 { 02101 CRYP_GCMCCM_SetHeaderPhase_IT(hcryp); 02102 } 02103 else /* if payload phase */ 02104 { 02105 CRYP_GCMCCM_SetPayloadPhase_IT(hcryp); 02106 } 02107 } 02108 else /* AES Algorithm ECB,CBC or CTR*/ 02109 { 02110 CRYP_AES_IT(hcryp); 02111 } 02112 } 02113 } 02114 /* Check if error occurred */ 02115 if (__HAL_CRYP_GET_IT_SOURCE(hcryp, CRYP_IT_ERRIE) != RESET) 02116 { 02117 /* If write Error occurred */ 02118 if (__HAL_CRYP_GET_FLAG(hcryp, CRYP_IT_WRERR) != RESET) 02119 { 02120 hcryp->ErrorCode |= HAL_CRYP_ERROR_WRITE; 02121 } 02122 /* If read Error occurred */ 02123 if (__HAL_CRYP_GET_FLAG(hcryp, CRYP_IT_RDERR) != RESET) 02124 { 02125 hcryp->ErrorCode |= HAL_CRYP_ERROR_READ; 02126 } 02127 } 02128 #endif /* End AES or CRYP */ 02129 } 02130 02131 /** 02132 * @brief Return the CRYP error code. 02133 * @param hcryp : pointer to a CRYP_HandleTypeDef structure that contains 02134 * the configuration information for the CRYP IP 02135 * @retval CRYP error code 02136 */ 02137 uint32_t HAL_CRYP_GetError(CRYP_HandleTypeDef *hcryp) 02138 { 02139 return hcryp->ErrorCode; 02140 } 02141 02142 /** 02143 * @brief Returns the CRYP state. 02144 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains 02145 * the configuration information for CRYP module. 02146 * @retval HAL state 02147 */ 02148 HAL_CRYP_STATETypeDef HAL_CRYP_GetState(CRYP_HandleTypeDef *hcryp) 02149 { 02150 return hcryp->State; 02151 } 02152 02153 /** 02154 * @brief Input FIFO transfer completed callback. 02155 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains 02156 * the configuration information for CRYP module. 02157 * @retval None 02158 */ 02159 __weak void HAL_CRYP_InCpltCallback(CRYP_HandleTypeDef *hcryp) 02160 { 02161 /* Prevent unused argument(s) compilation warning */ 02162 UNUSED(hcryp); 02163 02164 /* NOTE : This function should not be modified, when the callback is needed, 02165 the HAL_CRYP_InCpltCallback can be implemented in the user file 02166 */ 02167 } 02168 02169 /** 02170 * @brief Output FIFO transfer completed callback. 02171 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains 02172 * the configuration information for CRYP module. 02173 * @retval None 02174 */ 02175 __weak void HAL_CRYP_OutCpltCallback(CRYP_HandleTypeDef *hcryp) 02176 { 02177 /* Prevent unused argument(s) compilation warning */ 02178 UNUSED(hcryp); 02179 02180 /* NOTE : This function should not be modified, when the callback is needed, 02181 the HAL_CRYP_OutCpltCallback can be implemented in the user file 02182 */ 02183 } 02184 02185 /** 02186 * @brief CRYP error callback. 02187 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains 02188 * the configuration information for CRYP module. 02189 * @retval None 02190 */ 02191 __weak void HAL_CRYP_ErrorCallback(CRYP_HandleTypeDef *hcryp) 02192 { 02193 /* Prevent unused argument(s) compilation warning */ 02194 UNUSED(hcryp); 02195 02196 /* NOTE : This function Should not be modified, when the callback is needed, 02197 the HAL_CRYP_ErrorCallback could be implemented in the user file 02198 */ 02199 } 02200 /** 02201 * @} 02202 */ 02203 02204 /* Private functions ---------------------------------------------------------*/ 02205 /** @addtogroup CRYP_Private_Functions 02206 * @{ 02207 */ 02208 02209 #if defined (CRYP) 02210 02211 /** 02212 * @brief Encryption in ECB/CBC Algorithm with DES/TDES standard. 02213 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains 02214 * the configuration information for CRYP module 02215 * @param Timeout: specify Timeout value 02216 * @retval HAL status 02217 */ 02218 static HAL_StatusTypeDef CRYP_TDES_Process(CRYP_HandleTypeDef *hcryp, uint32_t Timeout) 02219 { 02220 uint32_t temp[2]; /* Temporary CrypOutBuff */ 02221 uint16_t incount; /* Temporary CrypInCount Value */ 02222 uint16_t outcount; /* Temporary CrypOutCount Value */ 02223 uint32_t i; 02224 02225 /* Enable CRYP */ 02226 __HAL_CRYP_ENABLE(hcryp); 02227 /*Temporary CrypOutCount Value*/ 02228 outcount = hcryp->CrypOutCount; 02229 02230 /*Start processing*/ 02231 while ((hcryp->CrypInCount < (hcryp->Size / 4U)) && (outcount < (hcryp->Size / 4U))) 02232 { 02233 /* Temporary CrypInCount Value */ 02234 incount = hcryp->CrypInCount; 02235 /* Write plain data and get cipher data */ 02236 if (((hcryp->Instance->SR & CRYP_FLAG_IFNF) != 0x0U) && (incount < (hcryp->Size / 4U))) 02237 { 02238 /* Write the input block in the IN FIFO */ 02239 hcryp->Instance->DIN = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount); 02240 hcryp->CrypInCount++; 02241 hcryp->Instance->DIN = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount); 02242 hcryp->CrypInCount++; 02243 } 02244 02245 /* Wait for OFNE flag to be raised */ 02246 if (CRYP_WaitOnOFNEFlag(hcryp, Timeout) != HAL_OK) 02247 { 02248 /* Disable the CRYP peripheral clock */ 02249 __HAL_CRYP_DISABLE(hcryp); 02250 02251 /* Change state & errorCode*/ 02252 hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT; 02253 hcryp->State = HAL_CRYP_STATE_READY; 02254 02255 /* Process unlocked */ 02256 __HAL_UNLOCK(hcryp); 02257 #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1) 02258 /*Call registered error callback*/ 02259 hcryp->ErrorCallback(hcryp); 02260 #else 02261 /*Call legacy weak error callback*/ 02262 HAL_CRYP_ErrorCallback(hcryp); 02263 #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */ 02264 } 02265 02266 /*Temporary CrypOutCount Value*/ 02267 outcount = hcryp->CrypOutCount; 02268 02269 if (((hcryp->Instance->SR & CRYP_FLAG_OFNE) != 0x0U) && (outcount < (hcryp->Size / 4U))) 02270 { 02271 /* Read the output block from the Output FIFO and put them in temporary Buffer then get CrypOutBuff from temporary buffer */ 02272 for (i = 0U; i < 2U; i++) 02273 { 02274 temp[i] = hcryp->Instance->DOUT; 02275 } 02276 i = 0U; 02277 while (((hcryp->CrypOutCount < ((hcryp->Size) / 4U))) && (i < 2U)) 02278 { 02279 *(uint32_t *)(hcryp->pCrypOutBuffPtr + hcryp->CrypOutCount) = temp[i]; 02280 hcryp->CrypOutCount++; 02281 i++; 02282 } 02283 } 02284 /*Temporary CrypOutCount Value*/ 02285 outcount = hcryp->CrypOutCount; 02286 } 02287 /* Disable CRYP */ 02288 __HAL_CRYP_DISABLE(hcryp); 02289 /* Change the CRYP state */ 02290 hcryp->State = HAL_CRYP_STATE_READY; 02291 02292 /* Return function status */ 02293 return HAL_OK; 02294 } 02295 02296 /** 02297 * @brief CRYP block input/output data handling under interruption with DES/TDES standard. 02298 * @note The function is called under interruption only, once 02299 * interruptions have been enabled by CRYP_Decrypt_IT() and CRYP_Encrypt_IT(). 02300 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains 02301 * the configuration information for CRYP module. 02302 * @retval none 02303 */ 02304 static void CRYP_TDES_IT(CRYP_HandleTypeDef *hcryp) 02305 { 02306 uint32_t temp[2]; /* Temporary CrypOutBuff */ 02307 uint32_t i; 02308 02309 if (hcryp->State == HAL_CRYP_STATE_BUSY) 02310 { 02311 if (__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI) != 0x0U) 02312 { 02313 if (__HAL_CRYP_GET_FLAG(hcryp, CRYP_FLAG_INRIS) != 0x0U) 02314 { 02315 /* Write input block in the IN FIFO */ 02316 hcryp->Instance->DIN = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount); 02317 hcryp->CrypInCount++; 02318 hcryp->Instance->DIN = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount); 02319 hcryp->CrypInCount++; 02320 02321 if (hcryp->CrypInCount == ((uint16_t)(hcryp->Size) / 4U)) 02322 { 02323 /* Disable interruption */ 02324 __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI); 02325 /* Call the input data transfer complete callback */ 02326 #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1U) 02327 /*Call registered Input complete callback*/ 02328 hcryp->InCpltCallback(hcryp); 02329 #else 02330 /*Call legacy weak Input complete callback*/ 02331 HAL_CRYP_InCpltCallback(hcryp); 02332 #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */ 02333 } 02334 } 02335 } 02336 if (__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI) != 0x0U) 02337 { 02338 if (__HAL_CRYP_GET_FLAG(hcryp, CRYP_FLAG_OUTRIS) != 0x0U) 02339 { 02340 /* Read the output block from the Output FIFO and put them in temporary Buffer then get CrypOutBuff from temporary buffer */ 02341 for (i = 0U; i < 2U; i++) 02342 { 02343 temp[i] = hcryp->Instance->DOUT; 02344 } 02345 i = 0U; 02346 while (((hcryp->CrypOutCount < ((hcryp->Size) / 4U))) && (i < 2U)) 02347 { 02348 *(uint32_t *)(hcryp->pCrypOutBuffPtr + hcryp->CrypOutCount) = temp[i]; 02349 hcryp->CrypOutCount++; 02350 i++; 02351 } 02352 if (hcryp->CrypOutCount == ((uint16_t)(hcryp->Size) / 4U)) 02353 { 02354 /* Disable interruption */ 02355 __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI); 02356 02357 /* Disable CRYP */ 02358 __HAL_CRYP_DISABLE(hcryp); 02359 02360 /* Process unlocked */ 02361 __HAL_UNLOCK(hcryp); 02362 02363 /* Change the CRYP state */ 02364 hcryp->State = HAL_CRYP_STATE_READY; 02365 /* Call output transfer complete callback */ 02366 #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1) 02367 /*Call registered Output complete callback*/ 02368 hcryp->OutCpltCallback(hcryp); 02369 #else 02370 /*Call legacy weak Output complete callback*/ 02371 HAL_CRYP_OutCpltCallback(hcryp); 02372 #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */ 02373 } 02374 } 02375 } 02376 } 02377 else 02378 { 02379 /* Process unlocked */ 02380 __HAL_UNLOCK(hcryp); 02381 /* Busy error code field */ 02382 hcryp->ErrorCode |= HAL_CRYP_ERROR_BUSY; 02383 #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1) 02384 /*Call registered error callback*/ 02385 hcryp->ErrorCallback(hcryp); 02386 #else 02387 /*Call legacy weak error callback*/ 02388 HAL_CRYP_ErrorCallback(hcryp); 02389 #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */ 02390 } 02391 } 02392 02393 #endif /* CRYP */ 02394 02395 /** 02396 * @brief Encryption in ECB/CBC & CTR Algorithm with AES Standard 02397 * @param hcryp: pointer to a CRYP_HandleTypeDef structure 02398 * @param Timeout: specify Timeout value 02399 * @retval HAL status 02400 */ 02401 static HAL_StatusTypeDef CRYP_AES_Encrypt(CRYP_HandleTypeDef *hcryp, uint32_t Timeout) 02402 { 02403 uint16_t outcount; /* Temporary CrypOutCount Value */ 02404 uint32_t DoKeyIVConfig = 1U; /* By default, carry out peripheral Key and IV configuration */ 02405 02406 if (hcryp->Init.KeyIVConfigSkip == CRYP_KEYIVCONFIG_ONCE) 02407 { 02408 if (hcryp->KeyIVConfig == 1U) 02409 { 02410 /* If the Key and IV configuration has to be done only once 02411 and if it has already been done, skip it */ 02412 DoKeyIVConfig = 0U; 02413 } 02414 else 02415 { 02416 /* If the Key and IV configuration has to be done only once 02417 and if it has not been done already, do it and set KeyIVConfig 02418 to keep track it won't have to be done again next time */ 02419 hcryp->KeyIVConfig = 1U; 02420 } 02421 } 02422 02423 if (DoKeyIVConfig == 1U) 02424 { 02425 02426 /* Set the Key*/ 02427 CRYP_SetKey(hcryp, hcryp->Init.KeySize); 02428 02429 if (hcryp->Init.Algorithm != CRYP_AES_ECB) 02430 { 02431 /* Set the Initialization Vector*/ 02432 #if defined (AES) 02433 hcryp->Instance->IVR3 = *(uint32_t *)(hcryp->Init.pInitVect); 02434 hcryp->Instance->IVR2 = *(uint32_t *)(hcryp->Init.pInitVect + 1); 02435 hcryp->Instance->IVR1 = *(uint32_t *)(hcryp->Init.pInitVect + 2); 02436 hcryp->Instance->IVR0 = *(uint32_t *)(hcryp->Init.pInitVect + 3); 02437 #else /* CRYP */ 02438 hcryp->Instance->IV0LR = *(uint32_t *)(hcryp->Init.pInitVect); 02439 hcryp->Instance->IV0RR = *(uint32_t *)(hcryp->Init.pInitVect + 1); 02440 hcryp->Instance->IV1LR = *(uint32_t *)(hcryp->Init.pInitVect + 2); 02441 hcryp->Instance->IV1RR = *(uint32_t *)(hcryp->Init.pInitVect + 3); 02442 #endif /* End AES or CRYP */ 02443 } 02444 } /* if (DoKeyIVConfig == 1U) */ 02445 02446 /* Set the phase */ 02447 hcryp->Phase = CRYP_PHASE_PROCESS; 02448 02449 /* Enable CRYP */ 02450 __HAL_CRYP_ENABLE(hcryp); 02451 02452 /*Temporary CrypOutCount Value*/ 02453 outcount = hcryp->CrypOutCount; 02454 02455 while ((hcryp->CrypInCount < (hcryp->Size / 4U)) && (outcount < (hcryp->Size / 4U))) 02456 { 02457 /* Write plain Ddta and get cipher data */ 02458 CRYP_AES_ProcessData(hcryp, Timeout); 02459 /*Temporary CrypOutCount Value*/ 02460 outcount = hcryp->CrypOutCount; 02461 } 02462 02463 /* Disable CRYP */ 02464 __HAL_CRYP_DISABLE(hcryp); 02465 02466 /* Change the CRYP state */ 02467 hcryp->State = HAL_CRYP_STATE_READY; 02468 02469 /* Return function status */ 02470 return HAL_OK; 02471 } 02472 02473 /** 02474 * @brief Encryption in ECB/CBC & CTR mode with AES Standard using interrupt mode 02475 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains 02476 * the configuration information for CRYP module 02477 * @retval HAL status 02478 */ 02479 static HAL_StatusTypeDef CRYP_AES_Encrypt_IT(CRYP_HandleTypeDef *hcryp) 02480 { 02481 uint32_t DoKeyIVConfig = 1U; /* By default, carry out peripheral Key and IV configuration */ 02482 02483 if (hcryp->Init.KeyIVConfigSkip == CRYP_KEYIVCONFIG_ONCE) 02484 { 02485 if (hcryp->KeyIVConfig == 1U) 02486 { 02487 /* If the Key and IV configuration has to be done only once 02488 and if it has already been done, skip it */ 02489 DoKeyIVConfig = 0U; 02490 } 02491 else 02492 { 02493 /* If the Key and IV configuration has to be done only once 02494 and if it has not been done already, do it and set KeyIVConfig 02495 to keep track it won't have to be done again next time */ 02496 hcryp->KeyIVConfig = 1U; 02497 } 02498 } 02499 02500 if (DoKeyIVConfig == 1U) 02501 { 02502 /* Set the Key*/ 02503 CRYP_SetKey(hcryp, hcryp->Init.KeySize); 02504 02505 if (hcryp->Init.Algorithm != CRYP_AES_ECB) 02506 { 02507 /* Set the Initialization Vector*/ 02508 #if defined (AES) 02509 hcryp->Instance->IVR3 = *(uint32_t *)(hcryp->Init.pInitVect); 02510 hcryp->Instance->IVR2 = *(uint32_t *)(hcryp->Init.pInitVect + 1); 02511 hcryp->Instance->IVR1 = *(uint32_t *)(hcryp->Init.pInitVect + 2); 02512 hcryp->Instance->IVR0 = *(uint32_t *)(hcryp->Init.pInitVect + 3); 02513 02514 #else /* CRYP */ 02515 hcryp->Instance->IV0LR = *(uint32_t *)(hcryp->Init.pInitVect); 02516 hcryp->Instance->IV0RR = *(uint32_t *)(hcryp->Init.pInitVect + 1); 02517 hcryp->Instance->IV1LR = *(uint32_t *)(hcryp->Init.pInitVect + 2); 02518 hcryp->Instance->IV1RR = *(uint32_t *)(hcryp->Init.pInitVect + 3); 02519 #endif /* End AES or CRYP */ 02520 } 02521 } /* if (DoKeyIVConfig == 1U) */ 02522 02523 /* Set the phase */ 02524 hcryp->Phase = CRYP_PHASE_PROCESS; 02525 02526 if (hcryp->Size != 0U) 02527 { 02528 #if defined (AES) 02529 02530 /* Enable computation complete flag and error interrupts */ 02531 __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_CCFIE | CRYP_IT_ERRIE); 02532 02533 /* Enable CRYP */ 02534 __HAL_CRYP_ENABLE(hcryp); 02535 02536 /* Write the input block in the IN FIFO */ 02537 hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount); 02538 hcryp->CrypInCount++; 02539 hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount); 02540 hcryp->CrypInCount++; 02541 hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount); 02542 hcryp->CrypInCount++; 02543 hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount); 02544 hcryp->CrypInCount++; 02545 02546 #else /* CRYP */ 02547 02548 /* Enable interrupts */ 02549 __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI); 02550 02551 /* Enable CRYP */ 02552 __HAL_CRYP_ENABLE(hcryp); 02553 02554 #endif /* End AES or CRYP */ 02555 } 02556 else 02557 { 02558 /* Change the CRYP state */ 02559 hcryp->State = HAL_CRYP_STATE_READY; 02560 02561 /* Process unlocked */ 02562 __HAL_UNLOCK(hcryp); 02563 } 02564 02565 /* Return function status */ 02566 return HAL_OK; 02567 } 02568 02569 /** 02570 * @brief Decryption in ECB/CBC & CTR mode with AES Standard 02571 * @param hcryp: pointer to a CRYP_HandleTypeDef structure 02572 * @param Timeout: Specify Timeout value 02573 * @retval HAL status 02574 */ 02575 static HAL_StatusTypeDef CRYP_AES_Decrypt(CRYP_HandleTypeDef *hcryp, uint32_t Timeout) 02576 { 02577 uint16_t outcount; /* Temporary CrypOutCount Value */ 02578 uint32_t DoKeyIVConfig = 1U; /* By default, carry out peripheral Key and IV configuration */ 02579 02580 if (hcryp->Init.KeyIVConfigSkip == CRYP_KEYIVCONFIG_ONCE) 02581 { 02582 if (hcryp->KeyIVConfig == 1U) 02583 { 02584 /* If the Key and IV configuration has to be done only once 02585 and if it has already been done, skip it */ 02586 DoKeyIVConfig = 0U; 02587 } 02588 else 02589 { 02590 /* If the Key and IV configuration has to be done only once 02591 and if it has not been done already, do it and set KeyIVConfig 02592 to keep track it won't have to be done again next time */ 02593 hcryp->KeyIVConfig = 1U; 02594 } 02595 } 02596 02597 if (DoKeyIVConfig == 1U) 02598 { 02599 /* Key preparation for ECB/CBC */ 02600 if (hcryp->Init.Algorithm != CRYP_AES_CTR) 02601 { 02602 #if defined (AES) 02603 if (hcryp->AutoKeyDerivation == DISABLE)/*Mode 2 Key preparation*/ 02604 { 02605 /* Set key preparation for decryption operating mode*/ 02606 MODIFY_REG(hcryp->Instance->CR, AES_CR_MODE, CRYP_OPERATINGMODE_KEYDERIVATION); 02607 02608 /* Set the Key*/ 02609 CRYP_SetKey(hcryp, hcryp->Init.KeySize); 02610 02611 /* Enable CRYP */ 02612 __HAL_CRYP_ENABLE(hcryp); 02613 02614 /* Wait for CCF flag to be raised */ 02615 if (CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK) 02616 { 02617 /* Disable the CRYP peripheral clock */ 02618 __HAL_CRYP_DISABLE(hcryp); 02619 02620 /* Change state & error code*/ 02621 hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT; 02622 hcryp->State = HAL_CRYP_STATE_READY; 02623 02624 /* Process unlocked */ 02625 __HAL_UNLOCK(hcryp); 02626 return HAL_ERROR; 02627 } 02628 /* Clear CCF Flag */ 02629 __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR); 02630 02631 /* Return to decryption operating mode(Mode 3)*/ 02632 MODIFY_REG(hcryp->Instance->CR, AES_CR_MODE, CRYP_OPERATINGMODE_DECRYPT); 02633 } 02634 else /*Mode 4 : decryption & Key preparation*/ 02635 { 02636 /* Set the Key*/ 02637 CRYP_SetKey(hcryp, hcryp->Init.KeySize); 02638 02639 /* Set decryption & Key preparation operating mode*/ 02640 MODIFY_REG(hcryp->Instance->CR, AES_CR_MODE, CRYP_OPERATINGMODE_KEYDERIVATION_DECRYPT); 02641 } 02642 #else /* CRYP */ 02643 /* change ALGOMODE to key preparation for decryption*/ 02644 MODIFY_REG(hcryp->Instance->CR, CRYP_CR_ALGOMODE, CRYP_CR_ALGOMODE_AES_KEY); 02645 02646 /* Set the Key*/ 02647 CRYP_SetKey(hcryp, hcryp->Init.KeySize); 02648 02649 /* Enable CRYP */ 02650 __HAL_CRYP_ENABLE(hcryp); 02651 02652 /* Wait for BUSY flag to be raised */ 02653 if (CRYP_WaitOnBUSYFlag(hcryp, Timeout) != HAL_OK) 02654 { 02655 /* Disable the CRYP peripheral clock */ 02656 __HAL_CRYP_DISABLE(hcryp); 02657 02658 /* Change state */ 02659 hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT; 02660 hcryp->State = HAL_CRYP_STATE_READY; 02661 02662 /* Process unlocked */ 02663 __HAL_UNLOCK(hcryp); 02664 return HAL_ERROR; 02665 } 02666 /* Turn back to ALGOMODE of the configuration */ 02667 MODIFY_REG(hcryp->Instance->CR, CRYP_CR_ALGOMODE, hcryp->Init.Algorithm); 02668 02669 #endif /* End AES or CRYP */ 02670 } 02671 else /*Algorithm CTR */ 02672 { 02673 /* Set the Key*/ 02674 CRYP_SetKey(hcryp, hcryp->Init.KeySize); 02675 } 02676 02677 /* Set IV */ 02678 if (hcryp->Init.Algorithm != CRYP_AES_ECB) 02679 { 02680 /* Set the Initialization Vector*/ 02681 #if defined (AES) 02682 hcryp->Instance->IVR3 = *(uint32_t *)(hcryp->Init.pInitVect); 02683 hcryp->Instance->IVR2 = *(uint32_t *)(hcryp->Init.pInitVect + 1); 02684 hcryp->Instance->IVR1 = *(uint32_t *)(hcryp->Init.pInitVect + 2); 02685 hcryp->Instance->IVR0 = *(uint32_t *)(hcryp->Init.pInitVect + 3); 02686 #else /* CRYP */ 02687 hcryp->Instance->IV0LR = *(uint32_t *)(hcryp->Init.pInitVect); 02688 hcryp->Instance->IV0RR = *(uint32_t *)(hcryp->Init.pInitVect + 1); 02689 hcryp->Instance->IV1LR = *(uint32_t *)(hcryp->Init.pInitVect + 2); 02690 hcryp->Instance->IV1RR = *(uint32_t *)(hcryp->Init.pInitVect + 3); 02691 #endif /* End AES or CRYP */ 02692 } 02693 } /* if (DoKeyIVConfig == 1U) */ 02694 /* Set the phase */ 02695 hcryp->Phase = CRYP_PHASE_PROCESS; 02696 02697 /* Enable CRYP */ 02698 __HAL_CRYP_ENABLE(hcryp); 02699 02700 /*Temporary CrypOutCount Value*/ 02701 outcount = hcryp->CrypOutCount; 02702 02703 while ((hcryp->CrypInCount < (hcryp->Size / 4U)) && (outcount < (hcryp->Size / 4U))) 02704 { 02705 /* Write plain data and get cipher data */ 02706 CRYP_AES_ProcessData(hcryp, Timeout); 02707 /*Temporary CrypOutCount Value*/ 02708 outcount = hcryp->CrypOutCount; 02709 } 02710 02711 /* Disable CRYP */ 02712 __HAL_CRYP_DISABLE(hcryp); 02713 02714 /* Change the CRYP state */ 02715 hcryp->State = HAL_CRYP_STATE_READY; 02716 02717 /* Return function status */ 02718 return HAL_OK; 02719 } 02720 /** 02721 * @brief Decryption in ECB/CBC & CTR mode with AES Standard using interrupt mode 02722 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains 02723 * the configuration information for CRYP module 02724 * @retval HAL status 02725 */ 02726 static HAL_StatusTypeDef CRYP_AES_Decrypt_IT(CRYP_HandleTypeDef *hcryp) 02727 { 02728 __IO uint32_t count = 0U; 02729 uint32_t DoKeyIVConfig = 1U; /* By default, carry out peripheral Key and IV configuration */ 02730 02731 if (hcryp->Init.KeyIVConfigSkip == CRYP_KEYIVCONFIG_ONCE) 02732 { 02733 if (hcryp->KeyIVConfig == 1U) 02734 { 02735 /* If the Key and IV configuration has to be done only once 02736 and if it has already been done, skip it */ 02737 DoKeyIVConfig = 0U; 02738 } 02739 else 02740 { 02741 /* If the Key and IV configuration has to be done only once 02742 and if it has not been done already, do it and set KeyIVConfig 02743 to keep track it won't have to be done again next time */ 02744 hcryp->KeyIVConfig = 1U; 02745 } 02746 } 02747 02748 if (DoKeyIVConfig == 1U) 02749 { 02750 /* Key preparation for ECB/CBC */ 02751 if (hcryp->Init.Algorithm != CRYP_AES_CTR) 02752 { 02753 #if defined (AES) 02754 if (hcryp->AutoKeyDerivation == DISABLE)/*Mode 2 Key preparation*/ 02755 { 02756 /* Set key preparation for decryption operating mode*/ 02757 MODIFY_REG(hcryp->Instance->CR, AES_CR_MODE, CRYP_OPERATINGMODE_KEYDERIVATION); 02758 02759 /* Set the Key*/ 02760 CRYP_SetKey(hcryp, hcryp->Init.KeySize); 02761 02762 /* Enable CRYP */ 02763 __HAL_CRYP_ENABLE(hcryp); 02764 02765 /* Wait for CCF flag to be raised */ 02766 count = CRYP_TIMEOUT_KEYPREPARATION; 02767 do 02768 { 02769 count-- ; 02770 if (count == 0U) 02771 { 02772 /* Disable the CRYP peripheral clock */ 02773 __HAL_CRYP_DISABLE(hcryp); 02774 02775 /* Change state */ 02776 hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT; 02777 hcryp->State = HAL_CRYP_STATE_READY; 02778 02779 /* Process unlocked */ 02780 __HAL_UNLOCK(hcryp); 02781 return HAL_ERROR; 02782 } 02783 } while (HAL_IS_BIT_CLR(hcryp->Instance->SR, AES_SR_CCF)); 02784 02785 /* Clear CCF Flag */ 02786 __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR); 02787 02788 /* Return to decryption operating mode(Mode 3)*/ 02789 MODIFY_REG(hcryp->Instance->CR, AES_CR_MODE, CRYP_OPERATINGMODE_DECRYPT); 02790 } 02791 else /*Mode 4 : decryption & key preparation*/ 02792 { 02793 /* Set the Key*/ 02794 CRYP_SetKey(hcryp, hcryp->Init.KeySize); 02795 02796 /* Set decryption & key preparation operating mode*/ 02797 MODIFY_REG(hcryp->Instance->CR, AES_CR_MODE, CRYP_OPERATINGMODE_KEYDERIVATION_DECRYPT); 02798 } 02799 #else /* CRYP */ 02800 02801 /* change ALGOMODE to key preparation for decryption*/ 02802 MODIFY_REG(hcryp->Instance->CR, CRYP_CR_ALGOMODE, CRYP_CR_ALGOMODE_AES_KEY); 02803 02804 /* Set the Key*/ 02805 CRYP_SetKey(hcryp, hcryp->Init.KeySize); 02806 02807 /* Enable CRYP */ 02808 __HAL_CRYP_ENABLE(hcryp); 02809 02810 /* Wait for BUSY flag to be raised */ 02811 count = CRYP_TIMEOUT_KEYPREPARATION; 02812 do 02813 { 02814 count-- ; 02815 if (count == 0U) 02816 { 02817 /* Change state */ 02818 hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT; 02819 hcryp->State = HAL_CRYP_STATE_READY; 02820 02821 /* Process unlocked */ 02822 __HAL_UNLOCK(hcryp); 02823 return HAL_ERROR; 02824 } 02825 } while (HAL_IS_BIT_SET(hcryp->Instance->SR, CRYP_FLAG_BUSY)); 02826 02827 /* Turn back to ALGOMODE of the configuration */ 02828 MODIFY_REG(hcryp->Instance->CR, CRYP_CR_ALGOMODE, hcryp->Init.Algorithm); 02829 02830 #endif /* End AES or CRYP */ 02831 } 02832 02833 else /*Algorithm CTR */ 02834 { 02835 /* Set the Key*/ 02836 CRYP_SetKey(hcryp, hcryp->Init.KeySize); 02837 } 02838 02839 /* Set IV */ 02840 if (hcryp->Init.Algorithm != CRYP_AES_ECB) 02841 { 02842 /* Set the Initialization Vector*/ 02843 #if defined (AES) 02844 hcryp->Instance->IVR3 = *(uint32_t *)(hcryp->Init.pInitVect); 02845 hcryp->Instance->IVR2 = *(uint32_t *)(hcryp->Init.pInitVect + 1); 02846 hcryp->Instance->IVR1 = *(uint32_t *)(hcryp->Init.pInitVect + 2); 02847 hcryp->Instance->IVR0 = *(uint32_t *)(hcryp->Init.pInitVect + 3); 02848 #else /* CRYP */ 02849 hcryp->Instance->IV0LR = *(uint32_t *)(hcryp->Init.pInitVect); 02850 hcryp->Instance->IV0RR = *(uint32_t *)(hcryp->Init.pInitVect + 1); 02851 hcryp->Instance->IV1LR = *(uint32_t *)(hcryp->Init.pInitVect + 2); 02852 hcryp->Instance->IV1RR = *(uint32_t *)(hcryp->Init.pInitVect + 3); 02853 #endif /* End AES or CRYP */ 02854 } 02855 } /* if (DoKeyIVConfig == 1U) */ 02856 02857 /* Set the phase */ 02858 hcryp->Phase = CRYP_PHASE_PROCESS; 02859 if (hcryp->Size != 0U) 02860 { 02861 02862 #if defined (AES) 02863 02864 /* Enable computation complete flag and error interrupts */ 02865 __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_CCFIE | CRYP_IT_ERRIE); 02866 02867 /* Enable CRYP */ 02868 __HAL_CRYP_ENABLE(hcryp); 02869 02870 /* Write the input block in the IN FIFO */ 02871 hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount); 02872 hcryp->CrypInCount++; 02873 hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount); 02874 hcryp->CrypInCount++; 02875 hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount); 02876 hcryp->CrypInCount++; 02877 hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount); 02878 hcryp->CrypInCount++; 02879 02880 #else /* CRYP */ 02881 02882 /* Enable interrupts */ 02883 __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI); 02884 02885 /* Enable CRYP */ 02886 __HAL_CRYP_ENABLE(hcryp); 02887 02888 #endif /* End AES or CRYP */ 02889 } 02890 else 02891 { 02892 /* Process locked */ 02893 __HAL_UNLOCK(hcryp); 02894 02895 /* Change the CRYP state */ 02896 hcryp->State = HAL_CRYP_STATE_READY; 02897 } 02898 02899 /* Return function status */ 02900 return HAL_OK; 02901 } 02902 /** 02903 * @brief Decryption in ECB/CBC & CTR mode with AES Standard using DMA mode 02904 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains 02905 * the configuration information for CRYP module 02906 * @retval HAL status 02907 */ 02908 static HAL_StatusTypeDef CRYP_AES_Decrypt_DMA(CRYP_HandleTypeDef *hcryp) 02909 { 02910 __IO uint32_t count = 0U; 02911 uint32_t DoKeyIVConfig = 1U; /* By default, carry out peripheral Key and IV configuration */ 02912 02913 if (hcryp->Init.KeyIVConfigSkip == CRYP_KEYIVCONFIG_ONCE) 02914 { 02915 if (hcryp->KeyIVConfig == 1U) 02916 { 02917 /* If the Key and IV configuration has to be done only once 02918 and if it has already been done, skip it */ 02919 DoKeyIVConfig = 0U; 02920 } 02921 else 02922 { 02923 /* If the Key and IV configuration has to be done only once 02924 and if it has not been done already, do it and set KeyIVConfig 02925 to keep track it won't have to be done again next time */ 02926 hcryp->KeyIVConfig = 1U; 02927 } 02928 } 02929 if (DoKeyIVConfig == 1U) 02930 { 02931 /* Key preparation for ECB/CBC */ 02932 if (hcryp->Init.Algorithm != CRYP_AES_CTR) 02933 { 02934 #if defined (AES) 02935 if (hcryp->AutoKeyDerivation == DISABLE)/*Mode 2 key preparation*/ 02936 { 02937 /* Set key preparation for decryption operating mode*/ 02938 MODIFY_REG(hcryp->Instance->CR, AES_CR_MODE, CRYP_OPERATINGMODE_KEYDERIVATION); 02939 02940 /* Set the Key*/ 02941 CRYP_SetKey(hcryp, hcryp->Init.KeySize); 02942 02943 /* Enable CRYP */ 02944 __HAL_CRYP_ENABLE(hcryp); 02945 02946 /* Wait for CCF flag to be raised */ 02947 count = CRYP_TIMEOUT_KEYPREPARATION; 02948 do 02949 { 02950 count-- ; 02951 if (count == 0U) 02952 { 02953 /* Disable the CRYP peripheral clock */ 02954 __HAL_CRYP_DISABLE(hcryp); 02955 02956 /* Change state */ 02957 hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT; 02958 hcryp->State = HAL_CRYP_STATE_READY; 02959 02960 /* Process unlocked */ 02961 __HAL_UNLOCK(hcryp); 02962 return HAL_ERROR; 02963 } 02964 } while (HAL_IS_BIT_CLR(hcryp->Instance->SR, AES_SR_CCF)); 02965 02966 /* Clear CCF Flag */ 02967 __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR); 02968 02969 /* Return to decryption operating mode(Mode 3)*/ 02970 MODIFY_REG(hcryp->Instance->CR, AES_CR_MODE, CRYP_OPERATINGMODE_DECRYPT); 02971 } 02972 else /*Mode 4 : decryption & key preparation*/ 02973 { 02974 /* Set the Key*/ 02975 CRYP_SetKey(hcryp, hcryp->Init.KeySize); 02976 02977 /* Set decryption & Key preparation operating mode*/ 02978 MODIFY_REG(hcryp->Instance->CR, AES_CR_MODE, CRYP_OPERATINGMODE_KEYDERIVATION_DECRYPT); 02979 } 02980 #else /* CRYP */ 02981 /* change ALGOMODE to key preparation for decryption*/ 02982 MODIFY_REG(hcryp->Instance->CR, CRYP_CR_ALGOMODE, CRYP_CR_ALGOMODE_AES_KEY); 02983 02984 /* Set the Key*/ 02985 CRYP_SetKey(hcryp, hcryp->Init.KeySize); 02986 02987 /* Enable CRYP */ 02988 __HAL_CRYP_ENABLE(hcryp); 02989 02990 /* Wait for BUSY flag to be raised */ 02991 count = CRYP_TIMEOUT_KEYPREPARATION; 02992 do 02993 { 02994 count-- ; 02995 if (count == 0U) 02996 { 02997 /* Disable the CRYP peripheral clock */ 02998 __HAL_CRYP_DISABLE(hcryp); 02999 03000 /* Change state */ 03001 hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT; 03002 hcryp->State = HAL_CRYP_STATE_READY; 03003 03004 /* Process unlocked */ 03005 __HAL_UNLOCK(hcryp); 03006 return HAL_ERROR; 03007 } 03008 } while (HAL_IS_BIT_SET(hcryp->Instance->SR, CRYP_FLAG_BUSY)); 03009 03010 /* Turn back to ALGOMODE of the configuration */ 03011 MODIFY_REG(hcryp->Instance->CR, CRYP_CR_ALGOMODE, hcryp->Init.Algorithm); 03012 03013 #endif /* End AES or CRYP */ 03014 } 03015 else /*Algorithm CTR */ 03016 { 03017 /* Set the Key*/ 03018 CRYP_SetKey(hcryp, hcryp->Init.KeySize); 03019 } 03020 03021 if (hcryp->Init.Algorithm != CRYP_AES_ECB) 03022 { 03023 /* Set the Initialization Vector*/ 03024 #if defined (AES) 03025 hcryp->Instance->IVR3 = *(uint32_t *)(hcryp->Init.pInitVect); 03026 hcryp->Instance->IVR2 = *(uint32_t *)(hcryp->Init.pInitVect + 1); 03027 hcryp->Instance->IVR1 = *(uint32_t *)(hcryp->Init.pInitVect + 2); 03028 hcryp->Instance->IVR0 = *(uint32_t *)(hcryp->Init.pInitVect + 3); 03029 #else /* CRYP */ 03030 hcryp->Instance->IV0LR = *(uint32_t *)(hcryp->Init.pInitVect); 03031 hcryp->Instance->IV0RR = *(uint32_t *)(hcryp->Init.pInitVect + 1); 03032 hcryp->Instance->IV1LR = *(uint32_t *)(hcryp->Init.pInitVect + 2); 03033 hcryp->Instance->IV1RR = *(uint32_t *)(hcryp->Init.pInitVect + 3); 03034 #endif /* End AES or CRYP */ 03035 } 03036 } /* if (DoKeyIVConfig == 1U) */ 03037 03038 /* Set the phase */ 03039 hcryp->Phase = CRYP_PHASE_PROCESS; 03040 03041 if (hcryp->Size != 0U) 03042 { 03043 /* Set the input and output addresses and start DMA transfer */ 03044 CRYP_SetDMAConfig(hcryp, (uint32_t)(hcryp->pCrypInBuffPtr), (hcryp->Size / 4U), (uint32_t)(hcryp->pCrypOutBuffPtr)); 03045 } 03046 else 03047 { 03048 /* Process unlocked */ 03049 __HAL_UNLOCK(hcryp); 03050 03051 /* Change the CRYP state */ 03052 hcryp->State = HAL_CRYP_STATE_READY; 03053 } 03054 03055 /* Return function status */ 03056 return HAL_OK; 03057 } 03058 03059 03060 /** 03061 * @brief DMA CRYP input data process complete callback. 03062 * @param hdma: DMA handle 03063 * @retval None 03064 */ 03065 static void CRYP_DMAInCplt(DMA_HandleTypeDef *hdma) 03066 { 03067 CRYP_HandleTypeDef *hcryp = (CRYP_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent; 03068 03069 /* Disable the DMA transfer for input FIFO request by resetting the DIEN bit 03070 in the DMACR register */ 03071 #if defined (CRYP) 03072 hcryp->Instance->DMACR &= (uint32_t)(~CRYP_DMACR_DIEN); 03073 03074 #else /* AES */ 03075 CLEAR_BIT(hcryp->Instance->CR, AES_CR_DMAINEN); 03076 03077 /* TinyAES2, No output on CCM AES, unlock should be done when input data process complete */ 03078 if ((hcryp->Init.Algorithm & CRYP_AES_CCM) == CRYP_AES_CCM) 03079 { 03080 /* Clear CCF flag */ 03081 __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR); 03082 03083 /* Change the CRYP state to ready */ 03084 hcryp->State = HAL_CRYP_STATE_READY; 03085 03086 /* Process Unlocked */ 03087 __HAL_UNLOCK(hcryp); 03088 } 03089 #endif /* End AES or CRYP */ 03090 03091 /* Call input data transfer complete callback */ 03092 #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1) 03093 /*Call registered Input complete callback*/ 03094 hcryp->InCpltCallback(hcryp); 03095 #else 03096 /*Call legacy weak Input complete callback*/ 03097 HAL_CRYP_InCpltCallback(hcryp); 03098 #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */ 03099 } 03100 03101 /** 03102 * @brief DMA CRYP output data process complete callback. 03103 * @param hdma: DMA handle 03104 * @retval None 03105 */ 03106 static void CRYP_DMAOutCplt(DMA_HandleTypeDef *hdma) 03107 { 03108 CRYP_HandleTypeDef *hcryp = (CRYP_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent; 03109 03110 /* Disable the DMA transfer for output FIFO request by resetting 03111 the DOEN bit in the DMACR register */ 03112 03113 #if defined (CRYP) 03114 03115 hcryp->Instance->DMACR &= (uint32_t)(~CRYP_DMACR_DOEN); 03116 #if defined (CRYP_CR_ALGOMODE_AES_GCM) 03117 if ((hcryp->Init.Algorithm & CRYP_AES_GCM) != CRYP_AES_GCM) 03118 { 03119 /* Disable CRYP (not allowed in GCM)*/ 03120 __HAL_CRYP_DISABLE(hcryp); 03121 } 03122 03123 #else /*NO GCM CCM */ 03124 /* Disable CRYP */ 03125 __HAL_CRYP_DISABLE(hcryp); 03126 #endif /* GCM CCM defined*/ 03127 #else /* AES */ 03128 03129 CLEAR_BIT(hcryp->Instance->CR, AES_CR_DMAOUTEN); 03130 03131 /* Clear CCF flag */ 03132 __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR); 03133 03134 if ((hcryp->Init.Algorithm & CRYP_AES_GCM_GMAC) != CRYP_AES_GCM_GMAC) 03135 { 03136 /* Disable CRYP (not allowed in GCM)*/ 03137 __HAL_CRYP_DISABLE(hcryp); 03138 } 03139 #endif /* End AES or CRYP */ 03140 03141 /* Change the CRYP state to ready */ 03142 hcryp->State = HAL_CRYP_STATE_READY; 03143 03144 /* Process unlocked */ 03145 __HAL_UNLOCK(hcryp); 03146 /* Call output data transfer complete callback */ 03147 #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1) 03148 /*Call registered Output complete callback*/ 03149 hcryp->OutCpltCallback(hcryp); 03150 #else 03151 /*Call legacy weak Output complete callback*/ 03152 HAL_CRYP_OutCpltCallback(hcryp); 03153 #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */ 03154 } 03155 03156 /** 03157 * @brief DMA CRYP communication error callback. 03158 * @param hdma: DMA handle 03159 * @retval None 03160 */ 03161 static void CRYP_DMAError(DMA_HandleTypeDef *hdma) 03162 { 03163 CRYP_HandleTypeDef *hcryp = (CRYP_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent; 03164 03165 /* Change the CRYP peripheral state */ 03166 hcryp->State = HAL_CRYP_STATE_READY; 03167 03168 /* DMA error code field */ 03169 hcryp->ErrorCode |= HAL_CRYP_ERROR_DMA; 03170 03171 #if defined (AES) 03172 03173 /* Clear CCF flag */ 03174 __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR); 03175 03176 #endif /* AES */ 03177 03178 /* Call error callback */ 03179 #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1) 03180 /*Call registered error callback*/ 03181 hcryp->ErrorCallback(hcryp); 03182 #else 03183 /*Call legacy weak error callback*/ 03184 HAL_CRYP_ErrorCallback(hcryp); 03185 #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */ 03186 } 03187 03188 /** 03189 * @brief Set the DMA configuration and start the DMA transfer 03190 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains 03191 * the configuration information for CRYP module 03192 * @param inputaddr: address of the input buffer 03193 * @param Size: size of the input buffer, must be a multiple of 16. 03194 * @param outputaddr: address of the output buffer 03195 * @retval None 03196 */ 03197 static void CRYP_SetDMAConfig(CRYP_HandleTypeDef *hcryp, uint32_t inputaddr, uint16_t Size, uint32_t outputaddr) 03198 { 03199 /* Set the CRYP DMA transfer complete callback */ 03200 hcryp->hdmain->XferCpltCallback = CRYP_DMAInCplt; 03201 03202 /* Set the DMA input error callback */ 03203 hcryp->hdmain->XferErrorCallback = CRYP_DMAError; 03204 03205 /* Set the CRYP DMA transfer complete callback */ 03206 hcryp->hdmaout->XferCpltCallback = CRYP_DMAOutCplt; 03207 03208 /* Set the DMA output error callback */ 03209 hcryp->hdmaout->XferErrorCallback = CRYP_DMAError; 03210 03211 #if defined (CRYP) 03212 03213 /* Enable CRYP */ 03214 __HAL_CRYP_ENABLE(hcryp); 03215 03216 /* Enable the input DMA Stream */ 03217 if (HAL_DMA_Start_IT(hcryp->hdmain, inputaddr, (uint32_t)&hcryp->Instance->DIN, Size) != HAL_OK) 03218 { 03219 /* DMA error code field */ 03220 hcryp->ErrorCode |= HAL_CRYP_ERROR_DMA; 03221 03222 /* Call error callback */ 03223 #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1) 03224 /*Call registered error callback*/ 03225 hcryp->ErrorCallback(hcryp); 03226 #else 03227 /*Call legacy weak error callback*/ 03228 HAL_CRYP_ErrorCallback(hcryp); 03229 #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */ 03230 } 03231 /* Enable the output DMA Stream */ 03232 if (HAL_DMA_Start_IT(hcryp->hdmaout, (uint32_t)&hcryp->Instance->DOUT, outputaddr, Size) != HAL_OK) 03233 { 03234 /* DMA error code field */ 03235 hcryp->ErrorCode |= HAL_CRYP_ERROR_DMA; 03236 03237 /* Call error callback */ 03238 #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1) 03239 /*Call registered error callback*/ 03240 hcryp->ErrorCallback(hcryp); 03241 #else 03242 /*Call legacy weak error callback*/ 03243 HAL_CRYP_ErrorCallback(hcryp); 03244 #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */ 03245 } 03246 /* Enable In/Out DMA request */ 03247 hcryp->Instance->DMACR = CRYP_DMACR_DOEN | CRYP_DMACR_DIEN; 03248 03249 #else /* AES */ 03250 03251 if (((hcryp->Init.Algorithm & CRYP_AES_GCM_GMAC) != CRYP_AES_GCM_GMAC) 03252 && ((hcryp->Init.Algorithm & CRYP_AES_CCM) != CRYP_AES_CCM)) 03253 { 03254 /* Enable CRYP (not allowed in GCM & CCM)*/ 03255 __HAL_CRYP_ENABLE(hcryp); 03256 } 03257 03258 /* Enable the DMA input stream */ 03259 if (HAL_DMA_Start_IT(hcryp->hdmain, inputaddr, (uint32_t)&hcryp->Instance->DINR, Size) != HAL_OK) 03260 { 03261 /* DMA error code field */ 03262 hcryp->ErrorCode |= HAL_CRYP_ERROR_DMA; 03263 03264 /* Call error callback */ 03265 #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1) 03266 /*Call registered error callback*/ 03267 hcryp->ErrorCallback(hcryp); 03268 #else 03269 /*Call legacy weak error callback*/ 03270 HAL_CRYP_ErrorCallback(hcryp); 03271 #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */ 03272 } 03273 /* Enable the DMA output stream */ 03274 if (HAL_DMA_Start_IT(hcryp->hdmaout, (uint32_t)&hcryp->Instance->DOUTR, outputaddr, Size) != HAL_OK) 03275 { 03276 /* DMA error code field */ 03277 hcryp->ErrorCode |= HAL_CRYP_ERROR_DMA; 03278 03279 /* Call error callback */ 03280 #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1) 03281 /*Call registered error callback*/ 03282 hcryp->ErrorCallback(hcryp); 03283 #else 03284 /*Call legacy weak error callback*/ 03285 HAL_CRYP_ErrorCallback(hcryp); 03286 #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */ 03287 } 03288 /*AES2v1.1.1 : CCM authentication : no init phase, only header and final phase */ 03289 /* Enable In and Out DMA requests */ 03290 if ((hcryp->Init.Algorithm & CRYP_AES_CCM) == CRYP_AES_CCM) 03291 { 03292 /* Enable only In DMA requests for CCM*/ 03293 SET_BIT(hcryp->Instance->CR, (AES_CR_DMAINEN)); 03294 } 03295 else 03296 { 03297 /* Enable In and Out DMA requests */ 03298 SET_BIT(hcryp->Instance->CR, (AES_CR_DMAINEN | AES_CR_DMAOUTEN)); 03299 } 03300 #endif /* End AES or CRYP */ 03301 } 03302 03303 /** 03304 * @brief Process Data: Write Input data in polling mode and used in AES functions. 03305 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains 03306 * the configuration information for CRYP module 03307 * @param Timeout: Specify Timeout value 03308 * @retval None 03309 */ 03310 static void CRYP_AES_ProcessData(CRYP_HandleTypeDef *hcryp, uint32_t Timeout) 03311 { 03312 03313 uint32_t temp[4]; /* Temporary CrypOutBuff */ 03314 uint32_t i; 03315 #if defined (CRYP) 03316 uint16_t incount; /* Temporary CrypInCount Value */ 03317 uint16_t outcount; /* Temporary CrypOutCount Value */ 03318 #endif 03319 03320 #if defined (CRYP) 03321 03322 /*Temporary CrypOutCount Value*/ 03323 incount = hcryp->CrypInCount; 03324 03325 if (((hcryp->Instance->SR & CRYP_FLAG_IFNF) != 0x0U) && (incount < (hcryp->Size / 4U))) 03326 { 03327 /* Write the input block in the IN FIFO */ 03328 hcryp->Instance->DIN = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount); 03329 hcryp->CrypInCount++; 03330 hcryp->Instance->DIN = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount); 03331 hcryp->CrypInCount++; 03332 hcryp->Instance->DIN = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount); 03333 hcryp->CrypInCount++; 03334 hcryp->Instance->DIN = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount); 03335 hcryp->CrypInCount++; 03336 } 03337 03338 /* Wait for OFNE flag to be raised */ 03339 if (CRYP_WaitOnOFNEFlag(hcryp, Timeout) != HAL_OK) 03340 { 03341 /* Disable the CRYP peripheral clock */ 03342 __HAL_CRYP_DISABLE(hcryp); 03343 03344 /* Change state & error code*/ 03345 hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT; 03346 hcryp->State = HAL_CRYP_STATE_READY; 03347 03348 /* Process unlocked */ 03349 __HAL_UNLOCK(hcryp); 03350 #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1) 03351 /*Call registered error callback*/ 03352 hcryp->ErrorCallback(hcryp); 03353 #else 03354 /*Call legacy weak error callback*/ 03355 HAL_CRYP_ErrorCallback(hcryp); 03356 #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */ 03357 } 03358 03359 /*Temporary CrypOutCount Value*/ 03360 outcount = hcryp->CrypOutCount; 03361 03362 if (((hcryp->Instance->SR & CRYP_FLAG_OFNE) != 0x0U) && (outcount < (hcryp->Size / 4U))) 03363 { 03364 /* Read the output block from the Output FIFO and put them in temporary buffer then get CrypOutBuff from temporary buffer */ 03365 for (i = 0U; i < 4U; i++) 03366 { 03367 temp[i] = hcryp->Instance->DOUT; 03368 } 03369 i = 0U; 03370 while (((hcryp->CrypOutCount < ((hcryp->Size) / 4U))) && (i < 4U)) 03371 { 03372 *(uint32_t *)(hcryp->pCrypOutBuffPtr + hcryp->CrypOutCount) = temp[i]; 03373 hcryp->CrypOutCount++; 03374 i++; 03375 } 03376 } 03377 03378 #else /* AES */ 03379 03380 /* Write the input block in the IN FIFO */ 03381 hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount); 03382 hcryp->CrypInCount++; 03383 hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount); 03384 hcryp->CrypInCount++; 03385 hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount); 03386 hcryp->CrypInCount++; 03387 hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount); 03388 hcryp->CrypInCount++; 03389 03390 /* Wait for CCF flag to be raised */ 03391 if (CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK) 03392 { 03393 /* Disable the CRYP peripheral clock */ 03394 __HAL_CRYP_DISABLE(hcryp); 03395 03396 /* Change state */ 03397 hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT; 03398 hcryp->State = HAL_CRYP_STATE_READY; 03399 03400 /* Process unlocked */ 03401 __HAL_UNLOCK(hcryp); 03402 #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1) 03403 /*Call registered error callback*/ 03404 hcryp->ErrorCallback(hcryp); 03405 #else 03406 /*Call legacy weak error callback*/ 03407 HAL_CRYP_ErrorCallback(hcryp); 03408 #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */ 03409 } 03410 03411 /* Clear CCF Flag */ 03412 __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR); 03413 03414 /* Read the output block from the output FIFO and put them in temporary buffer then get CrypOutBuff from temporary buffer*/ 03415 for (i = 0U; i < 4U; i++) 03416 { 03417 temp[i] = hcryp->Instance->DOUTR; 03418 } 03419 i = 0U; 03420 while ((hcryp->CrypOutCount < ((hcryp->Size + 3U) / 4U)) && (i < 4U)) 03421 { 03422 *(uint32_t *)(hcryp->pCrypOutBuffPtr + hcryp->CrypOutCount) = temp[i]; 03423 hcryp->CrypOutCount++; 03424 i++; 03425 } 03426 #endif /* End AES or CRYP */ 03427 } 03428 03429 /** 03430 * @brief Handle CRYP block input/output data handling under interruption. 03431 * @note The function is called under interruption only, once 03432 * interruptions have been enabled by HAL_CRYP_Encrypt_IT or HAL_CRYP_Decrypt_IT. 03433 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains 03434 * the configuration information for CRYP module. 03435 * @retval HAL status 03436 */ 03437 static void CRYP_AES_IT(CRYP_HandleTypeDef *hcryp) 03438 { 03439 uint32_t temp[4]; /* Temporary CrypOutBuff */ 03440 uint32_t i; 03441 #if defined (CRYP) 03442 uint16_t incount; /* Temporary CrypInCount Value */ 03443 uint16_t outcount; /* Temporary CrypOutCount Value */ 03444 #endif 03445 03446 if (hcryp->State == HAL_CRYP_STATE_BUSY) 03447 { 03448 #if defined (CRYP) 03449 03450 /*Temporary CrypOutCount Value*/ 03451 incount = hcryp->CrypInCount; 03452 if (((hcryp->Instance->SR & CRYP_FLAG_IFNF) != 0x0U) && (incount < (hcryp->Size / 4U))) 03453 { 03454 /* Write the input block in the IN FIFO */ 03455 hcryp->Instance->DIN = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount); 03456 hcryp->CrypInCount++; 03457 hcryp->Instance->DIN = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount); 03458 hcryp->CrypInCount++; 03459 hcryp->Instance->DIN = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount); 03460 hcryp->CrypInCount++; 03461 hcryp->Instance->DIN = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount); 03462 hcryp->CrypInCount++; 03463 if (hcryp->CrypInCount == ((uint16_t)(hcryp->Size) / 4U)) 03464 { 03465 /* Disable interrupts */ 03466 __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI); 03467 03468 /* Call the input data transfer complete callback */ 03469 #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1) 03470 /*Call registered Input complete callback*/ 03471 hcryp->InCpltCallback(hcryp); 03472 #else 03473 /*Call legacy weak Input complete callback*/ 03474 HAL_CRYP_InCpltCallback(hcryp); 03475 #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */ 03476 } 03477 } 03478 /*Temporary CrypOutCount Value*/ 03479 outcount = hcryp->CrypOutCount; 03480 03481 if (((hcryp->Instance->SR & CRYP_FLAG_OFNE) != 0x0U) && (outcount < (hcryp->Size / 4U))) 03482 { 03483 /* Read the output block from the output FIFO and put them in temporary buffer then get CrypOutBuff from temporary buffer */ 03484 for (i = 0U; i < 4U; i++) 03485 { 03486 temp[i] = hcryp->Instance->DOUT; 03487 } 03488 i = 0U; 03489 while (((hcryp->CrypOutCount < ((hcryp->Size) / 4U))) && (i < 4U)) 03490 { 03491 *(uint32_t *)(hcryp->pCrypOutBuffPtr + hcryp->CrypOutCount) = temp[i]; 03492 hcryp->CrypOutCount++; 03493 i++; 03494 } 03495 if (hcryp->CrypOutCount == ((uint16_t)(hcryp->Size) / 4U)) 03496 { 03497 /* Disable interrupts */ 03498 __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI); 03499 03500 /* Change the CRYP state */ 03501 hcryp->State = HAL_CRYP_STATE_READY; 03502 03503 /* Disable CRYP */ 03504 __HAL_CRYP_DISABLE(hcryp); 03505 03506 /* Process unlocked */ 03507 __HAL_UNLOCK(hcryp); 03508 03509 /* Call Output transfer complete callback */ 03510 #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1) 03511 /*Call registered Output complete callback*/ 03512 hcryp->OutCpltCallback(hcryp); 03513 #else 03514 /*Call legacy weak Output complete callback*/ 03515 HAL_CRYP_OutCpltCallback(hcryp); 03516 #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */ 03517 } 03518 } 03519 03520 #else /*AES*/ 03521 03522 /* Read the output block from the output FIFO and put them in temporary buffer then get CrypOutBuff from temporary buffer*/ 03523 for (i = 0U; i < 4U; i++) 03524 { 03525 temp[i] = hcryp->Instance->DOUTR; 03526 } 03527 i = 0U; 03528 while ((hcryp->CrypOutCount < ((hcryp->Size + 3U) / 4U)) && (i < 4U)) 03529 { 03530 *(uint32_t *)(hcryp->pCrypOutBuffPtr + hcryp->CrypOutCount) = temp[i]; 03531 hcryp->CrypOutCount++; 03532 i++; 03533 } 03534 03535 if (hcryp->CrypOutCount == (hcryp->Size / 4U)) 03536 { 03537 /* Disable Computation Complete flag and errors interrupts */ 03538 __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_CCFIE | CRYP_IT_ERRIE); 03539 03540 /* Change the CRYP state */ 03541 hcryp->State = HAL_CRYP_STATE_READY; 03542 03543 /* Disable CRYP */ 03544 __HAL_CRYP_DISABLE(hcryp); 03545 03546 /* Process Unlocked */ 03547 __HAL_UNLOCK(hcryp); 03548 03549 /* Call Output transfer complete callback */ 03550 #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1) 03551 /*Call registered Output complete callback*/ 03552 hcryp->OutCpltCallback(hcryp); 03553 #else 03554 /*Call legacy weak Output complete callback*/ 03555 HAL_CRYP_OutCpltCallback(hcryp); 03556 #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */ 03557 } 03558 else 03559 { 03560 /* Write the input block in the IN FIFO */ 03561 hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount); 03562 hcryp->CrypInCount++; 03563 hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount); 03564 hcryp->CrypInCount++; 03565 hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount); 03566 hcryp->CrypInCount++; 03567 hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount); 03568 hcryp->CrypInCount++; 03569 03570 if (hcryp->CrypInCount == (hcryp->Size / 4U)) 03571 { 03572 /* Call Input transfer complete callback */ 03573 #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1U) 03574 /*Call registered Input complete callback*/ 03575 hcryp->InCpltCallback(hcryp); 03576 #else 03577 /*Call legacy weak Input complete callback*/ 03578 HAL_CRYP_InCpltCallback(hcryp); 03579 #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */ 03580 } 03581 } 03582 #endif /* End AES or CRYP */ 03583 03584 } 03585 else 03586 { 03587 /* Busy error code field */ 03588 hcryp->ErrorCode |= HAL_CRYP_ERROR_BUSY; 03589 #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1) 03590 /*Call registered error callback*/ 03591 hcryp->ErrorCallback(hcryp); 03592 #else 03593 /*Call legacy weak error callback*/ 03594 HAL_CRYP_ErrorCallback(hcryp); 03595 #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */ 03596 } 03597 } 03598 03599 /** 03600 * @brief Writes Key in Key registers. 03601 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains 03602 * the configuration information for CRYP module 03603 * @param KeySize: Size of Key 03604 * @retval None 03605 */ 03606 static void CRYP_SetKey(CRYP_HandleTypeDef *hcryp, uint32_t KeySize) 03607 { 03608 #if defined (CRYP) 03609 03610 switch (KeySize) 03611 { 03612 case CRYP_KEYSIZE_256B: 03613 hcryp->Instance->K0LR = *(uint32_t *)(hcryp->Init.pKey); 03614 hcryp->Instance->K0RR = *(uint32_t *)(hcryp->Init.pKey + 1); 03615 hcryp->Instance->K1LR = *(uint32_t *)(hcryp->Init.pKey + 2); 03616 hcryp->Instance->K1RR = *(uint32_t *)(hcryp->Init.pKey + 3); 03617 hcryp->Instance->K2LR = *(uint32_t *)(hcryp->Init.pKey + 4); 03618 hcryp->Instance->K2RR = *(uint32_t *)(hcryp->Init.pKey + 5); 03619 hcryp->Instance->K3LR = *(uint32_t *)(hcryp->Init.pKey + 6); 03620 hcryp->Instance->K3RR = *(uint32_t *)(hcryp->Init.pKey + 7); 03621 break; 03622 case CRYP_KEYSIZE_192B: 03623 hcryp->Instance->K1LR = *(uint32_t *)(hcryp->Init.pKey); 03624 hcryp->Instance->K1RR = *(uint32_t *)(hcryp->Init.pKey + 1); 03625 hcryp->Instance->K2LR = *(uint32_t *)(hcryp->Init.pKey + 2); 03626 hcryp->Instance->K2RR = *(uint32_t *)(hcryp->Init.pKey + 3); 03627 hcryp->Instance->K3LR = *(uint32_t *)(hcryp->Init.pKey + 4); 03628 hcryp->Instance->K3RR = *(uint32_t *)(hcryp->Init.pKey + 5); 03629 break; 03630 case CRYP_KEYSIZE_128B: 03631 hcryp->Instance->K2LR = *(uint32_t *)(hcryp->Init.pKey); 03632 hcryp->Instance->K2RR = *(uint32_t *)(hcryp->Init.pKey + 1); 03633 hcryp->Instance->K3LR = *(uint32_t *)(hcryp->Init.pKey + 2); 03634 hcryp->Instance->K3RR = *(uint32_t *)(hcryp->Init.pKey + 3); 03635 03636 break; 03637 default: 03638 break; 03639 } 03640 #else /*AES*/ 03641 switch (KeySize) 03642 { 03643 case CRYP_KEYSIZE_256B: 03644 hcryp->Instance->KEYR7 = *(uint32_t *)(hcryp->Init.pKey); 03645 hcryp->Instance->KEYR6 = *(uint32_t *)(hcryp->Init.pKey + 1); 03646 hcryp->Instance->KEYR5 = *(uint32_t *)(hcryp->Init.pKey + 2); 03647 hcryp->Instance->KEYR4 = *(uint32_t *)(hcryp->Init.pKey + 3); 03648 hcryp->Instance->KEYR3 = *(uint32_t *)(hcryp->Init.pKey + 4); 03649 hcryp->Instance->KEYR2 = *(uint32_t *)(hcryp->Init.pKey + 5); 03650 hcryp->Instance->KEYR1 = *(uint32_t *)(hcryp->Init.pKey + 6); 03651 hcryp->Instance->KEYR0 = *(uint32_t *)(hcryp->Init.pKey + 7); 03652 break; 03653 case CRYP_KEYSIZE_128B: 03654 hcryp->Instance->KEYR3 = *(uint32_t *)(hcryp->Init.pKey); 03655 hcryp->Instance->KEYR2 = *(uint32_t *)(hcryp->Init.pKey + 1); 03656 hcryp->Instance->KEYR1 = *(uint32_t *)(hcryp->Init.pKey + 2); 03657 hcryp->Instance->KEYR0 = *(uint32_t *)(hcryp->Init.pKey + 3); 03658 03659 break; 03660 default: 03661 break; 03662 } 03663 #endif /* End AES or CRYP */ 03664 } 03665 03666 #if defined (CRYP_CR_ALGOMODE_AES_GCM)|| defined (AES) 03667 /** 03668 * @brief Encryption/Decryption process in AES GCM mode and prepare the authentication TAG 03669 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains 03670 * the configuration information for CRYP module 03671 * @param Timeout: Timeout duration 03672 * @retval HAL status 03673 */ 03674 static HAL_StatusTypeDef CRYP_AESGCM_Process(CRYP_HandleTypeDef *hcryp, uint32_t Timeout) 03675 { 03676 uint32_t tickstart; 03677 uint32_t wordsize = (uint32_t)(hcryp->Size) / 4U ; 03678 uint16_t outcount; /* Temporary CrypOutCount Value */ 03679 uint32_t DoKeyIVConfig = 1U; /* By default, carry out peripheral Key and IV configuration */ 03680 03681 if (hcryp->Init.KeyIVConfigSkip == CRYP_KEYIVCONFIG_ONCE) 03682 { 03683 if (hcryp->KeyIVConfig == 1U) 03684 { 03685 /* If the Key and IV configuration has to be done only once 03686 and if it has already been done, skip it */ 03687 DoKeyIVConfig = 0U; 03688 hcryp->SizesSum += hcryp->Size; /* Compute message total payload length */ 03689 } 03690 else 03691 { 03692 /* If the Key and IV configuration has to be done only once 03693 and if it has not been done already, do it and set KeyIVConfig 03694 to keep track it won't have to be done again next time */ 03695 hcryp->KeyIVConfig = 1U; 03696 hcryp->SizesSum = hcryp->Size; /* Merely store payload length */ 03697 } 03698 } 03699 else 03700 { 03701 hcryp->SizesSum = hcryp->Size; 03702 } 03703 03704 if (DoKeyIVConfig == 1U) 03705 { 03706 /* Reset CrypHeaderCount */ 03707 hcryp->CrypHeaderCount = 0U; 03708 03709 /****************************** Init phase **********************************/ 03710 03711 CRYP_SET_PHASE(hcryp, CRYP_PHASE_INIT); 03712 03713 /* Set the key */ 03714 CRYP_SetKey(hcryp, hcryp->Init.KeySize); 03715 03716 #if defined(CRYP) 03717 03718 /* Set the initialization vector and the counter : Initial Counter Block (ICB)*/ 03719 hcryp->Instance->IV0LR = *(uint32_t *)(hcryp->Init.pInitVect); 03720 hcryp->Instance->IV0RR = *(uint32_t *)(hcryp->Init.pInitVect + 1); 03721 hcryp->Instance->IV1LR = *(uint32_t *)(hcryp->Init.pInitVect + 2); 03722 hcryp->Instance->IV1RR = *(uint32_t *)(hcryp->Init.pInitVect + 3); 03723 03724 /* Enable the CRYP peripheral */ 03725 __HAL_CRYP_ENABLE(hcryp); 03726 03727 /* Get tick */ 03728 tickstart = HAL_GetTick(); 03729 03730 /*Wait for the CRYPEN bit to be cleared*/ 03731 while ((hcryp->Instance->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN) 03732 { 03733 /* Check for the Timeout */ 03734 if (Timeout != HAL_MAX_DELAY) 03735 { 03736 if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U)) 03737 { 03738 /* Disable the CRYP peripheral clock */ 03739 __HAL_CRYP_DISABLE(hcryp); 03740 03741 /* Change state */ 03742 hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT; 03743 hcryp->State = HAL_CRYP_STATE_READY; 03744 03745 /* Process unlocked */ 03746 __HAL_UNLOCK(hcryp); 03747 return HAL_ERROR; 03748 } 03749 } 03750 } 03751 03752 #else /* AES */ 03753 /* Workaround 1 : only AES. 03754 Datatype configuration must be 32 bits during Init phase. Only, after Init, and before re 03755 enabling the IP, datatype different from 32 bits can be configured.*/ 03756 /* Select DATATYPE 32 */ 03757 MODIFY_REG(hcryp->Instance->CR, AES_CR_DATATYPE, CRYP_DATATYPE_32B); 03758 03759 /* Set the initialization vector and the counter : Initial Counter Block (ICB)*/ 03760 hcryp->Instance->IVR3 = *(uint32_t *)(hcryp->Init.pInitVect); 03761 hcryp->Instance->IVR2 = *(uint32_t *)(hcryp->Init.pInitVect + 1); 03762 hcryp->Instance->IVR1 = *(uint32_t *)(hcryp->Init.pInitVect + 2); 03763 hcryp->Instance->IVR0 = *(uint32_t *)(hcryp->Init.pInitVect + 3); 03764 03765 /* Enable the CRYP peripheral */ 03766 __HAL_CRYP_ENABLE(hcryp); 03767 03768 /* just wait for hash computation */ 03769 if (CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK) 03770 { 03771 /* Change state */ 03772 hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT; 03773 hcryp->State = HAL_CRYP_STATE_READY; 03774 03775 /* Process unlocked & return error */ 03776 __HAL_UNLOCK(hcryp); 03777 return HAL_ERROR; 03778 } 03779 /* Clear CCF flag */ 03780 __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR); 03781 03782 #endif /* End AES or CRYP */ 03783 03784 /************************ Header phase *************************************/ 03785 03786 if (CRYP_GCMCCM_SetHeaderPhase(hcryp, Timeout) != HAL_OK) 03787 { 03788 return HAL_ERROR; 03789 } 03790 03791 /*************************Payload phase ************************************/ 03792 03793 /* Set the phase */ 03794 hcryp->Phase = CRYP_PHASE_PROCESS; 03795 03796 #if defined(CRYP) 03797 03798 /* Disable the CRYP peripheral */ 03799 __HAL_CRYP_DISABLE(hcryp); 03800 03801 /* Select payload phase once the header phase is performed */ 03802 CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD); 03803 03804 /* Enable the CRYP peripheral */ 03805 __HAL_CRYP_ENABLE(hcryp); 03806 03807 #else /* AES */ 03808 03809 /* Select payload phase once the header phase is performed */ 03810 CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD); 03811 03812 #endif /* End AES or CRYP */ 03813 } /* if (DoKeyIVConfig == 1U) */ 03814 03815 if ((hcryp->Size % 16U) != 0U) 03816 { 03817 /* recalculate wordsize */ 03818 wordsize = ((wordsize / 4U) * 4U) ; 03819 } 03820 03821 /* Get tick */ 03822 tickstart = HAL_GetTick(); 03823 /*Temporary CrypOutCount Value*/ 03824 outcount = hcryp->CrypOutCount; 03825 03826 /* Write input data and get output Data */ 03827 while ((hcryp->CrypInCount < wordsize) && (outcount < wordsize)) 03828 { 03829 /* Write plain data and get cipher data */ 03830 CRYP_AES_ProcessData(hcryp, Timeout); 03831 03832 /*Temporary CrypOutCount Value*/ 03833 outcount = hcryp->CrypOutCount; 03834 03835 /* Check for the Timeout */ 03836 if (Timeout != HAL_MAX_DELAY) 03837 { 03838 if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U)) 03839 { 03840 /* Disable the CRYP peripheral clock */ 03841 __HAL_CRYP_DISABLE(hcryp); 03842 03843 /* Change state & error code */ 03844 hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT; 03845 hcryp->State = HAL_CRYP_STATE_READY; 03846 03847 /* Process unlocked */ 03848 __HAL_UNLOCK(hcryp); 03849 return HAL_ERROR; 03850 } 03851 } 03852 } 03853 03854 if ((hcryp->Size % 16U) != 0U) 03855 { 03856 /* Workaround 2 : CRYP1 & AES generates correct TAG for GCM mode only when input block size is multiple of 03857 128 bits. If lthe size of the last block of payload is inferior to 128 bits, when GCM encryption 03858 is selected, then the TAG message will be wrong.*/ 03859 CRYP_Workaround(hcryp, Timeout); 03860 } 03861 03862 /* Return function status */ 03863 return HAL_OK; 03864 } 03865 03866 /** 03867 * @brief Encryption/Decryption process in AES GCM mode and prepare the authentication TAG in interrupt mode 03868 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains 03869 * the configuration information for CRYP module 03870 * @retval HAL status 03871 */ 03872 static HAL_StatusTypeDef CRYP_AESGCM_Process_IT(CRYP_HandleTypeDef *hcryp) 03873 { 03874 __IO uint32_t count = 0U; 03875 uint32_t DoKeyIVConfig = 1U; /* By default, carry out peripheral Key and IV configuration */ 03876 #if defined(AES) 03877 uint32_t loopcounter; 03878 uint32_t lastwordsize; 03879 uint32_t npblb; 03880 #endif /* AES */ 03881 03882 if (hcryp->Init.KeyIVConfigSkip == CRYP_KEYIVCONFIG_ONCE) 03883 { 03884 if (hcryp->KeyIVConfig == 1U) 03885 { 03886 /* If the Key and IV configuration has to be done only once 03887 and if it has already been done, skip it */ 03888 DoKeyIVConfig = 0U; 03889 hcryp->SizesSum += hcryp->Size; /* Compute message total payload length */ 03890 } 03891 else 03892 { 03893 /* If the Key and IV configuration has to be done only once 03894 and if it has not been done already, do it and set KeyIVConfig 03895 to keep track it won't have to be done again next time */ 03896 hcryp->KeyIVConfig = 1U; 03897 hcryp->SizesSum = hcryp->Size; /* Merely store payload length */ 03898 } 03899 } 03900 else 03901 { 03902 hcryp->SizesSum = hcryp->Size; 03903 } 03904 03905 /* Configure Key, IV and process message (header and payload) */ 03906 if (DoKeyIVConfig == 1U) 03907 { 03908 /* Reset CrypHeaderCount */ 03909 hcryp->CrypHeaderCount = 0U; 03910 03911 /******************************* Init phase *********************************/ 03912 03913 CRYP_SET_PHASE(hcryp, CRYP_PHASE_INIT); 03914 03915 /* Set the key */ 03916 CRYP_SetKey(hcryp, hcryp->Init.KeySize); 03917 03918 #if defined(CRYP) 03919 /* Set the initialization vector and the counter : Initial Counter Block (ICB)*/ 03920 hcryp->Instance->IV0LR = *(uint32_t *)(hcryp->Init.pInitVect); 03921 hcryp->Instance->IV0RR = *(uint32_t *)(hcryp->Init.pInitVect + 1); 03922 hcryp->Instance->IV1LR = *(uint32_t *)(hcryp->Init.pInitVect + 2); 03923 hcryp->Instance->IV1RR = *(uint32_t *)(hcryp->Init.pInitVect + 3); 03924 03925 /* Enable the CRYP peripheral */ 03926 __HAL_CRYP_ENABLE(hcryp); 03927 03928 /*Wait for the CRYPEN bit to be cleared*/ 03929 count = CRYP_TIMEOUT_GCMCCMINITPHASE; 03930 do 03931 { 03932 count-- ; 03933 if (count == 0U) 03934 { 03935 /* Disable the CRYP peripheral clock */ 03936 __HAL_CRYP_DISABLE(hcryp); 03937 03938 /* Change state */ 03939 hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT; 03940 hcryp->State = HAL_CRYP_STATE_READY; 03941 03942 /* Process unlocked */ 03943 __HAL_UNLOCK(hcryp); 03944 return HAL_ERROR; 03945 } 03946 } while ((hcryp->Instance->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN); 03947 03948 #else /* AES */ 03949 03950 /* Workaround 1 : only AES 03951 Datatype configuration must be 32 bits during INIT phase. Only, after INIT, and before re 03952 enabling the IP, datatype different from 32 bits can be configured.*/ 03953 /* Select DATATYPE 32 */ 03954 MODIFY_REG(hcryp->Instance->CR, AES_CR_DATATYPE, CRYP_DATATYPE_32B); 03955 03956 /* Set the initialization vector and the counter : Initial Counter Block (ICB)*/ 03957 hcryp->Instance->IVR3 = *(uint32_t *)(hcryp->Init.pInitVect); 03958 hcryp->Instance->IVR2 = *(uint32_t *)(hcryp->Init.pInitVect + 1); 03959 hcryp->Instance->IVR1 = *(uint32_t *)(hcryp->Init.pInitVect + 2); 03960 hcryp->Instance->IVR0 = *(uint32_t *)(hcryp->Init.pInitVect + 3); 03961 03962 /* Enable the CRYP peripheral */ 03963 __HAL_CRYP_ENABLE(hcryp); 03964 03965 /* just wait for hash computation */ 03966 count = CRYP_TIMEOUT_GCMCCMINITPHASE; 03967 do 03968 { 03969 count-- ; 03970 if (count == 0U) 03971 { 03972 /* Disable the CRYP peripheral clock */ 03973 __HAL_CRYP_DISABLE(hcryp); 03974 03975 /* Change state */ 03976 hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT; 03977 hcryp->State = HAL_CRYP_STATE_READY; 03978 03979 /* Process unlocked */ 03980 __HAL_UNLOCK(hcryp); 03981 return HAL_ERROR; 03982 } 03983 } while (HAL_IS_BIT_CLR(hcryp->Instance->SR, AES_SR_CCF)); 03984 03985 /* Clear CCF flag */ 03986 __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR); 03987 03988 #endif /* End AES or CRYP */ 03989 03990 /***************************** Header phase *********************************/ 03991 03992 #if defined(CRYP) 03993 03994 /* Select header phase */ 03995 CRYP_SET_PHASE(hcryp, CRYP_PHASE_HEADER); 03996 03997 /* Enable interrupts */ 03998 __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI); 03999 04000 /* Enable CRYP */ 04001 __HAL_CRYP_ENABLE(hcryp); 04002 04003 #else /* AES */ 04004 04005 /* Workaround 1: only AES , before re-enabling the IP, datatype can be configured*/ 04006 MODIFY_REG(hcryp->Instance->CR, AES_CR_DATATYPE, hcryp->Init.DataType); 04007 04008 /* Select header phase */ 04009 CRYP_SET_PHASE(hcryp, CRYP_PHASE_HEADER); 04010 04011 /* Enable computation complete flag and error interrupts */ 04012 __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_CCFIE | CRYP_IT_ERRIE); 04013 04014 /* Enable the CRYP peripheral */ 04015 __HAL_CRYP_ENABLE(hcryp); 04016 04017 if (hcryp->Init.HeaderSize == 0U) /*header phase is skipped*/ 04018 { 04019 /* Set the phase */ 04020 hcryp->Phase = CRYP_PHASE_PROCESS; 04021 04022 /* Select payload phase once the header phase is performed */ 04023 MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_PHASE_PAYLOAD); 04024 04025 /* Write the payload Input block in the IN FIFO */ 04026 if (hcryp->Size == 0U) 04027 { 04028 /* Disable interrupts */ 04029 __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_CCFIE | CRYP_IT_ERRIE); 04030 04031 /* Change the CRYP state */ 04032 hcryp->State = HAL_CRYP_STATE_READY; 04033 04034 /* Process unlocked */ 04035 __HAL_UNLOCK(hcryp); 04036 } 04037 else if (hcryp->Size >= 16U) 04038 { 04039 hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount); 04040 hcryp->CrypInCount++; 04041 hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount); 04042 hcryp->CrypInCount++; 04043 hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount); 04044 hcryp->CrypInCount++; 04045 hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount); 04046 hcryp->CrypInCount++; 04047 if (hcryp->CrypInCount == (hcryp->Size / 4U)) 04048 { 04049 /* Call Input transfer complete callback */ 04050 #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1) 04051 /*Call registered Input complete callback*/ 04052 hcryp->InCpltCallback(hcryp); 04053 #else 04054 /*Call legacy weak Input complete callback*/ 04055 HAL_CRYP_InCpltCallback(hcryp); 04056 #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */ 04057 } 04058 } 04059 else /* Size < 16Bytes : first block is the last block*/ 04060 { 04061 /* Workaround not implemented*/ 04062 /* Size should be %4 otherwise Tag will be incorrectly generated for GCM Encryption: 04063 Workaround is implemented in polling mode, so if last block of 04064 payload <128bit don't use CRYP_Encrypt_IT otherwise TAG is incorrectly generated for GCM Encryption. */ 04065 04066 /* Compute the number of padding bytes in last block of payload */ 04067 npblb = 16U - (uint32_t)(hcryp->Size); 04068 04069 /* Number of valid words (lastwordsize) in last block */ 04070 if ((npblb % 4U) == 0U) 04071 { 04072 lastwordsize = (16U - npblb) / 4U; 04073 } 04074 else 04075 { 04076 lastwordsize = ((16U - npblb) / 4U) + 1U; 04077 } 04078 04079 /* last block optionally pad the data with zeros*/ 04080 for (loopcounter = 0U; loopcounter < lastwordsize ; loopcounter++) 04081 { 04082 hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount); 04083 hcryp->CrypInCount++; 04084 } 04085 while (loopcounter < 4U) 04086 { 04087 /* pad the data with zeros to have a complete block */ 04088 hcryp->Instance->DINR = 0x0U; 04089 loopcounter++; 04090 } 04091 } 04092 } 04093 else if ((hcryp->Init.HeaderSize) < 4U) 04094 { 04095 for (loopcounter = 0U; loopcounter < hcryp->Init.HeaderSize ; loopcounter++) 04096 { 04097 hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount); 04098 hcryp->CrypHeaderCount++ ; 04099 } 04100 while (loopcounter < 4U) 04101 { 04102 /* pad the data with zeros to have a complete block */ 04103 hcryp->Instance->DINR = 0x0U; 04104 loopcounter++; 04105 } 04106 /* Set the phase */ 04107 hcryp->Phase = CRYP_PHASE_PROCESS; 04108 04109 /* Select payload phase once the header phase is performed */ 04110 CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD); 04111 04112 /* Call Input transfer complete callback */ 04113 #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1) 04114 /*Call registered Input complete callback*/ 04115 hcryp->InCpltCallback(hcryp); 04116 #else 04117 /*Call legacy weak Input complete callback*/ 04118 HAL_CRYP_InCpltCallback(hcryp); 04119 #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */ 04120 } 04121 else if ((hcryp->Init.HeaderSize) >= 4U) 04122 { 04123 /* Write the input block in the IN FIFO */ 04124 hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount); 04125 hcryp->CrypHeaderCount++; 04126 hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount); 04127 hcryp->CrypHeaderCount++; 04128 hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount); 04129 hcryp->CrypHeaderCount++; 04130 hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount); 04131 hcryp->CrypHeaderCount++; 04132 } 04133 else 04134 { 04135 /* Nothing to do */ 04136 } 04137 04138 #endif /* End AES or CRYP */ 04139 } /* end of if (DoKeyIVConfig == 1U) */ 04140 04141 /* Return function status */ 04142 return HAL_OK; 04143 } 04144 04145 04146 /** 04147 * @brief Encryption/Decryption process in AES GCM mode and prepare the authentication TAG using DMA 04148 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains 04149 * the configuration information for CRYP module 04150 * @retval HAL status 04151 */ 04152 static HAL_StatusTypeDef CRYP_AESGCM_Process_DMA(CRYP_HandleTypeDef *hcryp) 04153 { 04154 __IO uint32_t count = 0U; 04155 uint32_t wordsize; 04156 uint32_t DoKeyIVConfig = 1U; /* By default, carry out peripheral Key and IV configuration */ 04157 04158 if (hcryp->Init.KeyIVConfigSkip == CRYP_KEYIVCONFIG_ONCE) 04159 { 04160 if (hcryp->KeyIVConfig == 1U) 04161 { 04162 /* If the Key and IV configuration has to be done only once 04163 and if it has already been done, skip it */ 04164 DoKeyIVConfig = 0U; 04165 hcryp->SizesSum += hcryp->Size; /* Compute message total payload length */ 04166 } 04167 else 04168 { 04169 /* If the Key and IV configuration has to be done only once 04170 and if it has not been done already, do it and set KeyIVConfig 04171 to keep track it won't have to be done again next time */ 04172 hcryp->KeyIVConfig = 1U; 04173 hcryp->SizesSum = hcryp->Size; /* Merely store payload length */ 04174 } 04175 } 04176 else 04177 { 04178 hcryp->SizesSum = hcryp->Size; 04179 } 04180 04181 if (DoKeyIVConfig == 1U) 04182 { 04183 /* Reset CrypHeaderCount */ 04184 hcryp->CrypHeaderCount = 0U; 04185 04186 /*************************** Init phase ************************************/ 04187 04188 CRYP_SET_PHASE(hcryp, CRYP_PHASE_INIT); 04189 04190 /* Set the key */ 04191 CRYP_SetKey(hcryp, hcryp->Init.KeySize); 04192 04193 #if defined(CRYP) 04194 /* Set the initialization vector and the counter : Initial Counter Block (ICB)*/ 04195 hcryp->Instance->IV0LR = *(uint32_t *)(hcryp->Init.pInitVect); 04196 hcryp->Instance->IV0RR = *(uint32_t *)(hcryp->Init.pInitVect + 1); 04197 hcryp->Instance->IV1LR = *(uint32_t *)(hcryp->Init.pInitVect + 2); 04198 hcryp->Instance->IV1RR = *(uint32_t *)(hcryp->Init.pInitVect + 3); 04199 04200 /* Enable the CRYP peripheral */ 04201 __HAL_CRYP_ENABLE(hcryp); 04202 04203 /*Wait for the CRYPEN bit to be cleared*/ 04204 count = CRYP_TIMEOUT_GCMCCMINITPHASE; 04205 do 04206 { 04207 count-- ; 04208 if (count == 0U) 04209 { 04210 /* Disable the CRYP peripheral clock */ 04211 __HAL_CRYP_DISABLE(hcryp); 04212 04213 /* Change state */ 04214 hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT; 04215 hcryp->State = HAL_CRYP_STATE_READY; 04216 04217 /* Process unlocked */ 04218 __HAL_UNLOCK(hcryp); 04219 return HAL_ERROR; 04220 } 04221 } while ((hcryp->Instance->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN); 04222 04223 #else /* AES */ 04224 04225 /*Workaround 1 : only AES 04226 Datatype configuration must be 32 bits during Init phase. Only, after Init, and before re 04227 enabling the IP, datatype different from 32 bits can be configured.*/ 04228 /* Select DATATYPE 32 */ 04229 MODIFY_REG(hcryp->Instance->CR, AES_CR_DATATYPE, CRYP_DATATYPE_32B); 04230 04231 /* Set the initialization vector and the counter : Initial Counter Block (ICB)*/ 04232 hcryp->Instance->IVR3 = *(uint32_t *)(hcryp->Init.pInitVect); 04233 hcryp->Instance->IVR2 = *(uint32_t *)(hcryp->Init.pInitVect + 1); 04234 hcryp->Instance->IVR1 = *(uint32_t *)(hcryp->Init.pInitVect + 2); 04235 hcryp->Instance->IVR0 = *(uint32_t *)(hcryp->Init.pInitVect + 3); 04236 04237 /* Enable the CRYP peripheral */ 04238 __HAL_CRYP_ENABLE(hcryp); 04239 04240 /* just wait for hash computation */ 04241 count = CRYP_TIMEOUT_GCMCCMINITPHASE; 04242 do 04243 { 04244 count-- ; 04245 if (count == 0U) 04246 { 04247 /* Disable the CRYP peripheral clock */ 04248 __HAL_CRYP_DISABLE(hcryp); 04249 04250 /* Change state */ 04251 hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT; 04252 hcryp->State = HAL_CRYP_STATE_READY; 04253 04254 /* Process unlocked */ 04255 __HAL_UNLOCK(hcryp); 04256 return HAL_ERROR; 04257 } 04258 } while (HAL_IS_BIT_CLR(hcryp->Instance->SR, AES_SR_CCF)); 04259 04260 /* Clear CCF flag */ 04261 __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR); 04262 04263 #endif /* End AES or CRYP */ 04264 04265 /************************ Header phase *************************************/ 04266 04267 if (CRYP_GCMCCM_SetHeaderPhase_DMA(hcryp) != HAL_OK) 04268 { 04269 return HAL_ERROR; 04270 } 04271 04272 /************************ Payload phase ************************************/ 04273 04274 /* Set the phase */ 04275 hcryp->Phase = CRYP_PHASE_PROCESS; 04276 04277 #if defined(CRYP) 04278 04279 /* Disable the CRYP peripheral */ 04280 __HAL_CRYP_DISABLE(hcryp); 04281 04282 #endif /* CRYP */ 04283 04284 /* Select payload phase once the header phase is performed */ 04285 CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD); 04286 04287 } /* if (DoKeyIVConfig == 1U) */ 04288 04289 if (hcryp->Size != 0U) 04290 { 04291 /* CRYP1 IP V < 2.2.1 Size should be %4 otherwise Tag will be incorrectly generated for GCM Encryption: 04292 Workaround is implemented in polling mode, so if last block of 04293 payload <128bit don't use DMA mode otherwise TAG is incorrectly generated . */ 04294 /* Set the input and output addresses and start DMA transfer */ 04295 if ((hcryp->Size % 16U) == 0U) 04296 { 04297 CRYP_SetDMAConfig(hcryp, (uint32_t)(hcryp->pCrypInBuffPtr), (hcryp->Size / 4U), (uint32_t)(hcryp->pCrypOutBuffPtr)); 04298 } 04299 else /*to compute last word<128bits, otherwise it will not be encrypted/decrypted */ 04300 { 04301 wordsize = (uint32_t)(hcryp->Size) + (16U - ((uint32_t)(hcryp->Size) % 16U)) ; 04302 04303 /* Set the input and output addresses and start DMA transfer, pCrypOutBuffPtr size should be %4 */ 04304 CRYP_SetDMAConfig(hcryp, (uint32_t)(hcryp->pCrypInBuffPtr), ((uint16_t)wordsize / 4U), 04305 (uint32_t)(hcryp->pCrypOutBuffPtr)); 04306 } 04307 } 04308 else 04309 { 04310 /* Process unLocked */ 04311 __HAL_UNLOCK(hcryp); 04312 04313 /* Change the CRYP state and phase */ 04314 hcryp->State = HAL_CRYP_STATE_READY; 04315 } 04316 04317 /* Return function status */ 04318 return HAL_OK; 04319 } 04320 04321 04322 /** 04323 * @brief AES CCM encryption/decryption processing in polling mode 04324 * for TinyAES IP, no encrypt/decrypt performed, only authentication preparation. 04325 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains 04326 * the configuration information for CRYP module 04327 * @param Timeout: Timeout duration 04328 * @retval HAL status 04329 */ 04330 static HAL_StatusTypeDef CRYP_AESCCM_Process(CRYP_HandleTypeDef *hcryp, uint32_t Timeout) 04331 { 04332 uint32_t tickstart; 04333 uint32_t wordsize = (uint32_t)(hcryp->Size) / 4U; 04334 uint16_t outcount; /* Temporary CrypOutCount Value */ 04335 uint32_t DoKeyIVConfig = 1U; /* By default, carry out peripheral Key and IV configuration */ 04336 #if defined(AES) 04337 uint32_t loopcounter; 04338 uint32_t npblb; 04339 uint32_t lastwordsize; 04340 #endif /* AES */ 04341 04342 if (hcryp->Init.KeyIVConfigSkip == CRYP_KEYIVCONFIG_ONCE) 04343 { 04344 if (hcryp->KeyIVConfig == 1U) 04345 { 04346 /* If the Key and IV configuration has to be done only once 04347 and if it has already been done, skip it */ 04348 DoKeyIVConfig = 0U; 04349 hcryp->SizesSum += hcryp->Size; /* Compute message total payload length */ 04350 } 04351 else 04352 { 04353 /* If the Key and IV configuration has to be done only once 04354 and if it has not been done already, do it and set KeyIVConfig 04355 to keep track it won't have to be done again next time */ 04356 hcryp->KeyIVConfig = 1U; 04357 hcryp->SizesSum = hcryp->Size; /* Merely store payload length */ 04358 } 04359 } 04360 else 04361 { 04362 hcryp->SizesSum = hcryp->Size; 04363 } 04364 04365 if (DoKeyIVConfig == 1U) 04366 { 04367 04368 /* Reset CrypHeaderCount */ 04369 hcryp->CrypHeaderCount = 0U; 04370 04371 #if defined(CRYP) 04372 04373 /********************** Init phase ******************************************/ 04374 04375 CRYP_SET_PHASE(hcryp, CRYP_PHASE_INIT); 04376 04377 /* Set the key */ 04378 CRYP_SetKey(hcryp, hcryp->Init.KeySize); 04379 04380 /* Set the initialization vector (IV) with CTR1 information */ 04381 hcryp->Instance->IV0LR = (hcryp->Init.B0[0]) & CRYP_CCM_CTR1_0; 04382 hcryp->Instance->IV0RR = hcryp->Init.B0[1]; 04383 hcryp->Instance->IV1LR = hcryp->Init.B0[2]; 04384 hcryp->Instance->IV1RR = (hcryp->Init.B0[3] & CRYP_CCM_CTR1_1) | CRYP_CCM_CTR1_2; 04385 04386 04387 /* Enable the CRYP peripheral */ 04388 __HAL_CRYP_ENABLE(hcryp); 04389 04390 /*Write B0 packet into CRYP_DIN Register*/ 04391 if (hcryp->Init.DataType == CRYP_DATATYPE_8B) 04392 { 04393 hcryp->Instance->DIN = __REV(*(uint32_t *)(hcryp->Init.B0)); 04394 hcryp->Instance->DIN = __REV(*(uint32_t *)(hcryp->Init.B0 + 1)); 04395 hcryp->Instance->DIN = __REV(*(uint32_t *)(hcryp->Init.B0 + 2)); 04396 hcryp->Instance->DIN = __REV(*(uint32_t *)(hcryp->Init.B0 + 3)); 04397 } 04398 else if (hcryp->Init.DataType == CRYP_DATATYPE_16B) 04399 { 04400 hcryp->Instance->DIN = __ROR(*(uint32_t *)(hcryp->Init.B0), 16); 04401 hcryp->Instance->DIN = __ROR(*(uint32_t *)(hcryp->Init.B0 + 1), 16); 04402 hcryp->Instance->DIN = __ROR(*(uint32_t *)(hcryp->Init.B0 + 2), 16); 04403 hcryp->Instance->DIN = __ROR(*(uint32_t *)(hcryp->Init.B0 + 3), 16); 04404 } 04405 else if (hcryp->Init.DataType == CRYP_DATATYPE_1B) 04406 { 04407 hcryp->Instance->DIN = __RBIT(*(uint32_t *)(hcryp->Init.B0)); 04408 hcryp->Instance->DIN = __RBIT(*(uint32_t *)(hcryp->Init.B0 + 1)); 04409 hcryp->Instance->DIN = __RBIT(*(uint32_t *)(hcryp->Init.B0 + 2)); 04410 hcryp->Instance->DIN = __RBIT(*(uint32_t *)(hcryp->Init.B0 + 3)); 04411 } 04412 else 04413 { 04414 hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.B0); 04415 hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.B0 + 1); 04416 hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.B0 + 2); 04417 hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.B0 + 3); 04418 } 04419 /* Get tick */ 04420 tickstart = HAL_GetTick(); 04421 04422 /*Wait for the CRYPEN bit to be cleared*/ 04423 while ((hcryp->Instance->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN) 04424 { 04425 /* Check for the Timeout */ 04426 if (Timeout != HAL_MAX_DELAY) 04427 { 04428 if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U)) 04429 { 04430 /* Disable the CRYP peripheral clock */ 04431 __HAL_CRYP_DISABLE(hcryp); 04432 04433 /* Change state */ 04434 hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT; 04435 hcryp->State = HAL_CRYP_STATE_READY; 04436 04437 /* Process unlocked */ 04438 __HAL_UNLOCK(hcryp); 04439 return HAL_ERROR; 04440 } 04441 } 04442 } 04443 #else /* AES */ 04444 /*AES2v1.1.1 : CCM authentication : no init phase, only header and final phase */ 04445 /* Select header phase */ 04446 CRYP_SET_PHASE(hcryp, CRYP_PHASE_HEADER); 04447 04448 /* configured encryption mode */ 04449 MODIFY_REG(hcryp->Instance->CR, AES_CR_MODE, CRYP_OPERATINGMODE_ENCRYPT); 04450 04451 /* Set the key */ 04452 CRYP_SetKey(hcryp, hcryp->Init.KeySize); 04453 04454 /* Set the initialization vector with zero values*/ 04455 hcryp->Instance->IVR3 = 0U; 04456 hcryp->Instance->IVR2 = 0U; 04457 hcryp->Instance->IVR1 = 0U; 04458 hcryp->Instance->IVR0 = 0U; 04459 04460 /* Enable the CRYP peripheral */ 04461 __HAL_CRYP_ENABLE(hcryp); 04462 04463 /*Write the B0 packet into CRYP_DIN*/ 04464 hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.B0); 04465 hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.B0 + 1); 04466 hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.B0 + 2); 04467 hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.B0 + 3); 04468 04469 /* wait until the end of computation */ 04470 if (CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK) 04471 { 04472 /* Change state */ 04473 hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT; 04474 hcryp->State = HAL_CRYP_STATE_READY; 04475 04476 /* Process unlocked & return error */ 04477 __HAL_UNLOCK(hcryp); 04478 return HAL_ERROR; 04479 } 04480 /* Clear CCF flag */ 04481 __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR); 04482 04483 /* Set the phase */ 04484 hcryp->Phase = CRYP_PHASE_PROCESS; 04485 04486 /* From that point the whole message must be processed, first the Header then the payload. 04487 First the Header block(B1) : associated data length expressed in bytes concatenated with Associated Data (A)*/ 04488 04489 if (hcryp->Init.HeaderSize != 0U) 04490 { 04491 if ((hcryp->Init.HeaderSize % 4U) == 0U) 04492 { 04493 /* HeaderSize %4, no padding */ 04494 for (loopcounter = 0U; (loopcounter < hcryp->Init.HeaderSize); loopcounter += 4U) 04495 { 04496 /* Write the Input block in the Data Input register */ 04497 hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount); 04498 hcryp->CrypHeaderCount++ ; 04499 hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount); 04500 hcryp->CrypHeaderCount++ ; 04501 hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount); 04502 hcryp->CrypHeaderCount++ ; 04503 hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount); 04504 hcryp->CrypHeaderCount++ ; 04505 04506 if (CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK) 04507 { 04508 /* Disable the CRYP peripheral clock */ 04509 __HAL_CRYP_DISABLE(hcryp); 04510 04511 /* Change state */ 04512 hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT; 04513 hcryp->State = HAL_CRYP_STATE_READY; 04514 04515 /* Process unlocked */ 04516 __HAL_UNLOCK(hcryp); 04517 return HAL_ERROR; 04518 } 04519 /* Clear CCF Flag */ 04520 __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR); 04521 } 04522 } 04523 else 04524 { 04525 /*Write Header block in the IN FIFO without last block */ 04526 for (loopcounter = 0U; (loopcounter < ((hcryp->Init.HeaderSize) - (hcryp->Init.HeaderSize % 4U))); loopcounter += 4U) 04527 { 04528 /* Write the input block in the data input register */ 04529 hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount); 04530 hcryp->CrypHeaderCount++ ; 04531 hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount); 04532 hcryp->CrypHeaderCount++ ; 04533 hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount); 04534 hcryp->CrypHeaderCount++ ; 04535 hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount); 04536 hcryp->CrypHeaderCount++ ; 04537 04538 if (CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK) 04539 { 04540 /* Disable the CRYP peripheral clock */ 04541 __HAL_CRYP_DISABLE(hcryp); 04542 04543 /* Change state */ 04544 hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT; 04545 hcryp->State = HAL_CRYP_STATE_READY; 04546 04547 /* Process unlocked */ 04548 __HAL_UNLOCK(hcryp); 04549 return HAL_ERROR; 04550 } 04551 /* Clear CCF Flag */ 04552 __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR); 04553 } 04554 /* Last block optionally pad the data with zeros*/ 04555 for (loopcounter = 0U; (loopcounter < (hcryp->Init.HeaderSize % 4U)); loopcounter++) 04556 { 04557 hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount); 04558 hcryp->CrypHeaderCount++ ; 04559 } 04560 while (loopcounter < 4U) 04561 { 04562 /* Pad the data with zeros to have a complete block */ 04563 hcryp->Instance->DINR = 0x0U; 04564 loopcounter++; 04565 } 04566 04567 if (CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK) 04568 { 04569 /* Disable the CRYP peripheral clock */ 04570 __HAL_CRYP_DISABLE(hcryp); 04571 04572 /* Change state */ 04573 hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT; 04574 hcryp->State = HAL_CRYP_STATE_READY; 04575 04576 /* Process unlocked */ 04577 __HAL_UNLOCK(hcryp); 04578 return HAL_ERROR; 04579 } 04580 /* Clear CCF flag */ 04581 __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR); 04582 } 04583 } 04584 } /* if (DoKeyIVConfig == 1U) */ 04585 /* Then the payload: cleartext payload (not the ciphertext payload). 04586 Write input Data, no output Data to get */ 04587 if (hcryp->Size != 0U) 04588 { 04589 if ((hcryp->Size % 16U) != 0U) 04590 { 04591 /* recalculate wordsize */ 04592 wordsize = ((wordsize / 4U) * 4U) ; 04593 } 04594 04595 /* Get tick */ 04596 tickstart = HAL_GetTick(); 04597 /*Temporary CrypOutCount Value*/ 04598 outcount = hcryp->CrypOutCount; 04599 04600 while ((hcryp->CrypInCount < wordsize) && (outcount < wordsize)) 04601 { 04602 /* Write plain data and get cipher data */ 04603 CRYP_AES_ProcessData(hcryp, Timeout); 04604 04605 /*Temporary CrypOutCount Value*/ 04606 outcount = hcryp->CrypOutCount; 04607 04608 /* Check for the Timeout */ 04609 if (Timeout != HAL_MAX_DELAY) 04610 { 04611 if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U)) 04612 { 04613 /* Disable the CRYP peripheral clock */ 04614 __HAL_CRYP_DISABLE(hcryp); 04615 04616 /* Change state */ 04617 hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT; 04618 hcryp->State = HAL_CRYP_STATE_READY; 04619 04620 /* Process unlocked */ 04621 __HAL_UNLOCK(hcryp); 04622 return HAL_ERROR; 04623 } 04624 } 04625 } 04626 04627 if ((hcryp->Size % 16U) != 0U) 04628 { 04629 /* Compute the number of padding bytes in last block of payload */ 04630 npblb = ((((uint32_t)(hcryp->Size) / 16U) + 1U) * 16U) - (uint32_t)(hcryp->Size); 04631 04632 /* Number of valid words (lastwordsize) in last block */ 04633 if ((npblb % 4U) == 0U) 04634 { 04635 lastwordsize = (16U - npblb) / 4U; 04636 } 04637 else 04638 { 04639 lastwordsize = ((16U - npblb) / 4U) + 1U; 04640 } 04641 /* Last block optionally pad the data with zeros*/ 04642 for (loopcounter = 0U; loopcounter < lastwordsize; loopcounter ++) 04643 { 04644 /* Write the last input block in the IN FIFO */ 04645 hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount); 04646 hcryp->CrypInCount++; 04647 } 04648 while (loopcounter < 4U) 04649 { 04650 /* Pad the data with zeros to have a complete block */ 04651 hcryp->Instance->DINR = 0U; 04652 loopcounter++; 04653 } 04654 /* Wait for CCF flag to be raised */ 04655 if (CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK) 04656 { 04657 /* Disable the CRYP peripheral clock */ 04658 __HAL_CRYP_DISABLE(hcryp); 04659 04660 /* Change state */ 04661 hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT; 04662 hcryp->State = HAL_CRYP_STATE_READY; 04663 04664 /* Process unlocked */ 04665 __HAL_UNLOCK(hcryp); 04666 return HAL_ERROR; 04667 } 04668 /* Clear CCF flag */ 04669 __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR); 04670 04671 } 04672 } 04673 #endif /* End AES or CRYP */ 04674 04675 #if defined(CRYP) 04676 04677 /************************* Header phase *************************************/ 04678 /* Header block(B1) : associated data length expressed in bytes concatenated 04679 with Associated Data (A)*/ 04680 04681 if (CRYP_GCMCCM_SetHeaderPhase(hcryp, Timeout) != HAL_OK) 04682 { 04683 return HAL_ERROR; 04684 } 04685 04686 /********************** Payload phase ***************************************/ 04687 04688 /* Set the phase */ 04689 hcryp->Phase = CRYP_PHASE_PROCESS; 04690 04691 /* Disable the CRYP peripheral */ 04692 __HAL_CRYP_DISABLE(hcryp); 04693 04694 /* Select payload phase once the header phase is performed */ 04695 CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD); 04696 04697 /* Enable the CRYP peripheral */ 04698 __HAL_CRYP_ENABLE(hcryp); 04699 04700 } /* if (DoKeyIVConfig == 1U) */ 04701 04702 if ((hcryp->Size % 16U) != 0U) 04703 { 04704 /* recalculate wordsize */ 04705 wordsize = ((wordsize / 4U) * 4U) ; 04706 } 04707 /* Get tick */ 04708 tickstart = HAL_GetTick(); 04709 /*Temporary CrypOutCount Value*/ 04710 outcount = hcryp->CrypOutCount; 04711 04712 /* Write input data and get output data */ 04713 while ((hcryp->CrypInCount < wordsize) && (outcount < wordsize)) 04714 { 04715 /* Write plain data and get cipher data */ 04716 CRYP_AES_ProcessData(hcryp, Timeout); 04717 04718 /* Check for the Timeout */ 04719 if (Timeout != HAL_MAX_DELAY) 04720 { 04721 if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U)) 04722 { 04723 /* Disable the CRYP peripheral clock */ 04724 __HAL_CRYP_DISABLE(hcryp); 04725 04726 /* Change state */ 04727 hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT; 04728 hcryp->State = HAL_CRYP_STATE_READY; 04729 04730 /* Process unlocked */ 04731 __HAL_UNLOCK(hcryp); 04732 return HAL_ERROR; 04733 } 04734 } 04735 } 04736 04737 if ((hcryp->Size % 16U) != 0U) 04738 { 04739 /* CRYP Workaround : CRYP1 generates correct TAG during CCM decryption only when ciphertext blocks size is multiple of 04740 128 bits. If lthe size of the last block of payload is inferior to 128 bits, when CCM decryption 04741 is selected, then the TAG message will be wrong.*/ 04742 CRYP_Workaround(hcryp, Timeout); 04743 } 04744 #endif /* CRYP */ 04745 04746 /* Return function status */ 04747 return HAL_OK; 04748 } 04749 04750 /** 04751 * @brief AES CCM encryption/decryption process in interrupt mode 04752 * for TinyAES IP, no encrypt/decrypt performed, only authentication preparation. 04753 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains 04754 * the configuration information for CRYP module 04755 * @retval HAL status 04756 */ 04757 static HAL_StatusTypeDef CRYP_AESCCM_Process_IT(CRYP_HandleTypeDef *hcryp) 04758 { 04759 uint32_t DoKeyIVConfig = 1U; /* By default, carry out peripheral Key and IV configuration */ 04760 #if defined(CRYP) 04761 __IO uint32_t count = 0U; 04762 #endif /* CRYP */ 04763 04764 if (hcryp->Init.KeyIVConfigSkip == CRYP_KEYIVCONFIG_ONCE) 04765 { 04766 if (hcryp->KeyIVConfig == 1U) 04767 { 04768 /* If the Key and IV configuration has to be done only once 04769 and if it has already been done, skip it */ 04770 DoKeyIVConfig = 0U; 04771 hcryp->SizesSum += hcryp->Size; /* Compute message total payload length */ 04772 } 04773 else 04774 { 04775 /* If the Key and IV configuration has to be done only once 04776 and if it has not been done already, do it and set KeyIVConfig 04777 to keep track it won't have to be done again next time */ 04778 hcryp->KeyIVConfig = 1U; 04779 hcryp->SizesSum = hcryp->Size; /* Merely store payload length */ 04780 } 04781 } 04782 else 04783 { 04784 hcryp->SizesSum = hcryp->Size; 04785 } 04786 04787 /* Configure Key, IV and process message (header and payload) */ 04788 if (DoKeyIVConfig == 1U) 04789 { 04790 /* Reset CrypHeaderCount */ 04791 hcryp->CrypHeaderCount = 0U; 04792 04793 #if defined(CRYP) 04794 04795 /************ Init phase ************/ 04796 04797 CRYP_SET_PHASE(hcryp, CRYP_PHASE_INIT); 04798 04799 /* Set the key */ 04800 CRYP_SetKey(hcryp, hcryp->Init.KeySize); 04801 04802 /* Set the initialization vector (IV) with CTR1 information */ 04803 hcryp->Instance->IV0LR = (hcryp->Init.B0[0]) & CRYP_CCM_CTR1_0; 04804 hcryp->Instance->IV0RR = hcryp->Init.B0[1]; 04805 hcryp->Instance->IV1LR = hcryp->Init.B0[2]; 04806 hcryp->Instance->IV1RR = (hcryp->Init.B0[3] & CRYP_CCM_CTR1_1) | CRYP_CCM_CTR1_2; 04807 04808 /* Enable the CRYP peripheral */ 04809 __HAL_CRYP_ENABLE(hcryp); 04810 04811 /*Write the B0 packet into CRYP_DIN Register*/ 04812 if (hcryp->Init.DataType == CRYP_DATATYPE_8B) 04813 { 04814 hcryp->Instance->DIN = __REV(*(uint32_t *)(hcryp->Init.B0)); 04815 hcryp->Instance->DIN = __REV(*(uint32_t *)(hcryp->Init.B0 + 1)); 04816 hcryp->Instance->DIN = __REV(*(uint32_t *)(hcryp->Init.B0 + 2)); 04817 hcryp->Instance->DIN = __REV(*(uint32_t *)(hcryp->Init.B0 + 3)); 04818 } 04819 else if (hcryp->Init.DataType == CRYP_DATATYPE_16B) 04820 { 04821 hcryp->Instance->DIN = __ROR(*(uint32_t *)(hcryp->Init.B0), 16); 04822 hcryp->Instance->DIN = __ROR(*(uint32_t *)(hcryp->Init.B0 + 1), 16); 04823 hcryp->Instance->DIN = __ROR(*(uint32_t *)(hcryp->Init.B0 + 2), 16); 04824 hcryp->Instance->DIN = __ROR(*(uint32_t *)(hcryp->Init.B0 + 3), 16); 04825 } 04826 else if (hcryp->Init.DataType == CRYP_DATATYPE_1B) 04827 { 04828 hcryp->Instance->DIN = __RBIT(*(uint32_t *)(hcryp->Init.B0)); 04829 hcryp->Instance->DIN = __RBIT(*(uint32_t *)(hcryp->Init.B0 + 1)); 04830 hcryp->Instance->DIN = __RBIT(*(uint32_t *)(hcryp->Init.B0 + 2)); 04831 hcryp->Instance->DIN = __RBIT(*(uint32_t *)(hcryp->Init.B0 + 3)); 04832 } 04833 else 04834 { 04835 hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.B0); 04836 hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.B0 + 1); 04837 hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.B0 + 2); 04838 hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.B0 + 3); 04839 } 04840 /*Wait for the CRYPEN bit to be cleared*/ 04841 count = CRYP_TIMEOUT_GCMCCMINITPHASE; 04842 do 04843 { 04844 count-- ; 04845 if (count == 0U) 04846 { 04847 /* Disable the CRYP peripheral clock */ 04848 __HAL_CRYP_DISABLE(hcryp); 04849 04850 /* Change state */ 04851 hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT; 04852 hcryp->State = HAL_CRYP_STATE_READY; 04853 04854 /* Process unlocked */ 04855 __HAL_UNLOCK(hcryp); 04856 return HAL_ERROR; 04857 } 04858 } while ((hcryp->Instance->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN); 04859 04860 /* Select header phase */ 04861 CRYP_SET_PHASE(hcryp, CRYP_PHASE_HEADER); 04862 04863 } /* end of if (DoKeyIVConfig == 1U) */ 04864 04865 /* Enable interrupts */ 04866 __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI); 04867 04868 /* Enable CRYP */ 04869 __HAL_CRYP_ENABLE(hcryp); 04870 04871 #else /* AES */ 04872 04873 /*AES2v1.1.1 : CCM authentication : no init phase, only header and final phase */ 04874 /* Select header phase */ 04875 CRYP_SET_PHASE(hcryp, CRYP_PHASE_HEADER); 04876 04877 /* configured mode and encryption mode */ 04878 MODIFY_REG(hcryp->Instance->CR, AES_CR_MODE, CRYP_OPERATINGMODE_ENCRYPT); 04879 04880 /* Set the key */ 04881 CRYP_SetKey(hcryp, hcryp->Init.KeySize); 04882 04883 /* Set the initialization vector with zero values*/ 04884 hcryp->Instance->IVR3 = 0U; 04885 hcryp->Instance->IVR2 = 0U; 04886 hcryp->Instance->IVR1 = 0U; 04887 hcryp->Instance->IVR0 = 0U; 04888 04889 /* Enable interrupts */ 04890 __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_CCFIE | CRYP_IT_ERRIE); 04891 /* Enable the CRYP peripheral */ 04892 __HAL_CRYP_ENABLE(hcryp); 04893 04894 /*Write the B0 packet into CRYP_DIN*/ 04895 hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.B0); 04896 hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.B0 + 1); 04897 hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.B0 + 2); 04898 hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.B0 + 3); 04899 04900 } /* end of if (DoKeyIVConfig == 1U) */ 04901 #endif /* End AES or CRYP */ 04902 04903 /* Return function status */ 04904 return HAL_OK; 04905 } 04906 /** 04907 * @brief AES CCM encryption/decryption process in DMA mode 04908 * for TinyAES IP, no encrypt/decrypt performed, only authentication preparation. 04909 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains 04910 * the configuration information for CRYP module 04911 * @retval HAL status 04912 */ 04913 static HAL_StatusTypeDef CRYP_AESCCM_Process_DMA(CRYP_HandleTypeDef *hcryp) 04914 { 04915 uint32_t wordsize; 04916 __IO uint32_t count = 0U; 04917 uint32_t DoKeyIVConfig = 1U; /* By default, carry out peripheral Key and IV configuration */ 04918 #if defined(AES) 04919 uint32_t loopcounter; 04920 uint32_t npblb; 04921 uint32_t lastwordsize; 04922 #endif 04923 04924 if (hcryp->Init.KeyIVConfigSkip == CRYP_KEYIVCONFIG_ONCE) 04925 { 04926 if (hcryp->KeyIVConfig == 1U) 04927 { 04928 /* If the Key and IV configuration has to be done only once 04929 and if it has already been done, skip it */ 04930 DoKeyIVConfig = 0U; 04931 hcryp->SizesSum += hcryp->Size; /* Compute message total payload length */ 04932 } 04933 else 04934 { 04935 /* If the Key and IV configuration has to be done only once 04936 and if it has not been done already, do it and set KeyIVConfig 04937 to keep track it won't have to be done again next time */ 04938 hcryp->KeyIVConfig = 1U; 04939 hcryp->SizesSum = hcryp->Size; /* Merely store payload length */ 04940 } 04941 } 04942 else 04943 { 04944 hcryp->SizesSum = hcryp->Size; 04945 } 04946 04947 if (DoKeyIVConfig == 1U) 04948 { 04949 04950 /* Reset CrypHeaderCount */ 04951 hcryp->CrypHeaderCount = 0U; 04952 04953 #if defined(CRYP) 04954 04955 /************************** Init phase **************************************/ 04956 04957 CRYP_SET_PHASE(hcryp, CRYP_PHASE_INIT); 04958 04959 /* Set the key */ 04960 CRYP_SetKey(hcryp, hcryp->Init.KeySize); 04961 04962 /* Set the initialization vector (IV) with CTR1 information */ 04963 hcryp->Instance->IV0LR = (hcryp->Init.B0[0]) & CRYP_CCM_CTR1_0; 04964 hcryp->Instance->IV0RR = hcryp->Init.B0[1]; 04965 hcryp->Instance->IV1LR = hcryp->Init.B0[2]; 04966 hcryp->Instance->IV1RR = (hcryp->Init.B0[3] & CRYP_CCM_CTR1_1) | CRYP_CCM_CTR1_2; 04967 04968 /* Enable the CRYP peripheral */ 04969 __HAL_CRYP_ENABLE(hcryp); 04970 04971 /*Write the B0 packet into CRYP_DIN Register*/ 04972 if (hcryp->Init.DataType == CRYP_DATATYPE_8B) 04973 { 04974 hcryp->Instance->DIN = __REV(*(uint32_t *)(hcryp->Init.B0)); 04975 hcryp->Instance->DIN = __REV(*(uint32_t *)(hcryp->Init.B0 + 1)); 04976 hcryp->Instance->DIN = __REV(*(uint32_t *)(hcryp->Init.B0 + 2)); 04977 hcryp->Instance->DIN = __REV(*(uint32_t *)(hcryp->Init.B0 + 3)); 04978 } 04979 else if (hcryp->Init.DataType == CRYP_DATATYPE_16B) 04980 { 04981 hcryp->Instance->DIN = __ROR(*(uint32_t *)(hcryp->Init.B0), 16); 04982 hcryp->Instance->DIN = __ROR(*(uint32_t *)(hcryp->Init.B0 + 1), 16); 04983 hcryp->Instance->DIN = __ROR(*(uint32_t *)(hcryp->Init.B0 + 2), 16); 04984 hcryp->Instance->DIN = __ROR(*(uint32_t *)(hcryp->Init.B0 + 3), 16); 04985 } 04986 else if (hcryp->Init.DataType == CRYP_DATATYPE_1B) 04987 { 04988 hcryp->Instance->DIN = __RBIT(*(uint32_t *)(hcryp->Init.B0)); 04989 hcryp->Instance->DIN = __RBIT(*(uint32_t *)(hcryp->Init.B0 + 1)); 04990 hcryp->Instance->DIN = __RBIT(*(uint32_t *)(hcryp->Init.B0 + 2)); 04991 hcryp->Instance->DIN = __RBIT(*(uint32_t *)(hcryp->Init.B0 + 3)); 04992 } 04993 else 04994 { 04995 hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.B0); 04996 hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.B0 + 1); 04997 hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.B0 + 2); 04998 hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.B0 + 3); 04999 } 05000 05001 /*Wait for the CRYPEN bit to be cleared*/ 05002 count = CRYP_TIMEOUT_GCMCCMINITPHASE; 05003 do 05004 { 05005 count-- ; 05006 if (count == 0U) 05007 { 05008 /* Disable the CRYP peripheral clock */ 05009 __HAL_CRYP_DISABLE(hcryp); 05010 05011 /* Change state */ 05012 hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT; 05013 hcryp->State = HAL_CRYP_STATE_READY; 05014 05015 /* Process unlocked */ 05016 __HAL_UNLOCK(hcryp); 05017 return HAL_ERROR; 05018 } 05019 } while ((hcryp->Instance->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN); 05020 05021 #else /* AES */ 05022 05023 /*AES2v1.1.1 : CCM authentication : no init phase, only header and final phase */ 05024 /* Select header phase */ 05025 CRYP_SET_PHASE(hcryp, CRYP_PHASE_HEADER); 05026 05027 /* configured encryption mode */ 05028 MODIFY_REG(hcryp->Instance->CR, AES_CR_MODE, CRYP_OPERATINGMODE_ENCRYPT); 05029 05030 /* Set the key */ 05031 CRYP_SetKey(hcryp, hcryp->Init.KeySize); 05032 05033 /* Set the initialization vector with zero values*/ 05034 hcryp->Instance->IVR3 = 0U; 05035 hcryp->Instance->IVR2 = 0U; 05036 hcryp->Instance->IVR1 = 0U; 05037 hcryp->Instance->IVR0 = 0U; 05038 05039 /* Enable the CRYP peripheral */ 05040 __HAL_CRYP_ENABLE(hcryp); 05041 05042 /*Write the B0 packet into CRYP_DIN*/ 05043 hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.B0); 05044 hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.B0 + 1); 05045 hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.B0 + 2); 05046 hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.B0 + 3); 05047 05048 count = CRYP_TIMEOUT_GCMCCMINITPHASE; 05049 do 05050 { 05051 count-- ; 05052 if (count == 0U) 05053 { 05054 /* Disable the CRYP peripheral clock */ 05055 __HAL_CRYP_DISABLE(hcryp); 05056 05057 /* Change state */ 05058 hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT; 05059 hcryp->State = HAL_CRYP_STATE_READY; 05060 05061 /* Process Unlocked */ 05062 __HAL_UNLOCK(hcryp); 05063 return HAL_ERROR; 05064 } 05065 } while (HAL_IS_BIT_CLR(hcryp->Instance->SR, AES_SR_CCF)); 05066 /* Clear CCF flag */ 05067 __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR); 05068 05069 /* Set the phase */ 05070 hcryp->Phase = CRYP_PHASE_PROCESS; 05071 05072 /* From that point the whole message must be processed, first the Header then the payload. 05073 First the Header block(B1) : associated data length expressed in bytes concatenated with Associated Data (A)*/ 05074 05075 if (hcryp->Init.HeaderSize != 0U) 05076 { 05077 if ((hcryp->Init.HeaderSize % 4U) == 0U) 05078 { 05079 /* HeaderSize %4, no padding */ 05080 for (loopcounter = 0U; (loopcounter < hcryp->Init.HeaderSize); loopcounter += 4U) 05081 { 05082 /* Write the Input block in the Data Input register */ 05083 hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount); 05084 hcryp->CrypHeaderCount++ ; 05085 hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount); 05086 hcryp->CrypHeaderCount++ ; 05087 hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount); 05088 hcryp->CrypHeaderCount++ ; 05089 hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount); 05090 hcryp->CrypHeaderCount++ ; 05091 05092 /* wait until the end of computation */ 05093 count = CRYP_TIMEOUT_GCMCCMINITPHASE; 05094 do 05095 { 05096 count-- ; 05097 if (count == 0U) 05098 { 05099 /* Disable the CRYP peripheral clock */ 05100 __HAL_CRYP_DISABLE(hcryp); 05101 05102 /* Change state */ 05103 hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT; 05104 hcryp->State = HAL_CRYP_STATE_READY; 05105 05106 /* Process Unlocked */ 05107 __HAL_UNLOCK(hcryp); 05108 return HAL_ERROR; 05109 } 05110 } while (HAL_IS_BIT_CLR(hcryp->Instance->SR, AES_SR_CCF)); 05111 /* Clear CCF flag */ 05112 __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR); 05113 } 05114 } 05115 else 05116 { 05117 /*Write Header block in the IN FIFO without last block */ 05118 for (loopcounter = 0U; (loopcounter < ((hcryp->Init.HeaderSize) - (hcryp->Init.HeaderSize % 4U))); loopcounter += 4U) 05119 { 05120 /* Write the input block in the data input register */ 05121 hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount); 05122 hcryp->CrypHeaderCount++ ; 05123 hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount); 05124 hcryp->CrypHeaderCount++ ; 05125 hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount); 05126 hcryp->CrypHeaderCount++ ; 05127 hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount); 05128 hcryp->CrypHeaderCount++ ; 05129 05130 count = CRYP_TIMEOUT_GCMCCMINITPHASE; 05131 do 05132 { 05133 count-- ; 05134 if (count == 0U) 05135 { 05136 /* Disable the CRYP peripheral clock */ 05137 __HAL_CRYP_DISABLE(hcryp); 05138 05139 /* Change state */ 05140 hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT; 05141 hcryp->State = HAL_CRYP_STATE_READY; 05142 05143 /* Process Unlocked */ 05144 __HAL_UNLOCK(hcryp); 05145 return HAL_ERROR; 05146 } 05147 } while (HAL_IS_BIT_CLR(hcryp->Instance->SR, AES_SR_CCF)); 05148 /* Clear CCF flag */ 05149 __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR); 05150 } 05151 /* Last block optionally pad the data with zeros*/ 05152 for (loopcounter = 0U; (loopcounter < (hcryp->Init.HeaderSize % 4U)); loopcounter++) 05153 { 05154 hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount); 05155 hcryp->CrypHeaderCount++ ; 05156 } 05157 while (loopcounter < 4U) 05158 { 05159 /* Pad the data with zeros to have a complete block */ 05160 hcryp->Instance->DINR = 0x0U; 05161 loopcounter++; 05162 } 05163 05164 count = CRYP_TIMEOUT_GCMCCMINITPHASE; 05165 do 05166 { 05167 count-- ; 05168 if (count == 0U) 05169 { 05170 /* Disable the CRYP peripheral clock */ 05171 __HAL_CRYP_DISABLE(hcryp); 05172 05173 /* Change state */ 05174 hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT; 05175 hcryp->State = HAL_CRYP_STATE_READY; 05176 05177 /* Process Unlocked */ 05178 __HAL_UNLOCK(hcryp); 05179 return HAL_ERROR; 05180 } 05181 } while (HAL_IS_BIT_CLR(hcryp->Instance->SR, AES_SR_CCF)); 05182 /* Clear CCF flag */ 05183 __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR); 05184 } 05185 } 05186 } /* if (DoKeyIVConfig == 1U) */ 05187 /* Then the payload: cleartext payload (not the ciphertext payload). 05188 Write input Data, no output Data to get */ 05189 if (hcryp->Size != 0U) 05190 { 05191 if (hcryp->Size >= 16U) 05192 { 05193 if ((hcryp->Size % 16U) == 0U) 05194 { 05195 CRYP_SetDMAConfig(hcryp, (uint32_t)(hcryp->pCrypInBuffPtr), (hcryp->Size / 4U), (uint32_t)(hcryp->pCrypOutBuffPtr)); 05196 } 05197 else /*to compute last word<128bits, otherwise it will not be encrypted/decrypted */ 05198 { 05199 wordsize = (uint32_t)(hcryp->Size) + (16U - ((uint32_t)(hcryp->Size) % 16U)) ; 05200 05201 /* Set the input and output addresses and start DMA transfer, pCrypOutBuffPtr size should be %4 */ 05202 CRYP_SetDMAConfig(hcryp, (uint32_t)(hcryp->pCrypInBuffPtr), ((uint16_t)wordsize / 4U), 05203 (uint32_t)(hcryp->pCrypOutBuffPtr)); 05204 } 05205 } 05206 if ((hcryp->Size < 16U) != 0U) 05207 { 05208 /* Compute the number of padding bytes in last block of payload */ 05209 npblb = ((((uint32_t)(hcryp->Size) / 16U) + 1U) * 16U) - (uint32_t)(hcryp->Size); 05210 05211 /* Number of valid words (lastwordsize) in last block */ 05212 if ((npblb % 4U) == 0U) 05213 { 05214 lastwordsize = (16U - npblb) / 4U; 05215 } 05216 else 05217 { 05218 lastwordsize = ((16U - npblb) / 4U) + 1U; 05219 } 05220 /* Last block optionally pad the data with zeros*/ 05221 for (loopcounter = 0U; loopcounter < lastwordsize; loopcounter ++) 05222 { 05223 /* Write the last input block in the IN FIFO */ 05224 hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount); 05225 hcryp->CrypInCount++; 05226 } 05227 while (loopcounter < 4U) 05228 { 05229 /* Pad the data with zeros to have a complete block */ 05230 hcryp->Instance->DINR = 0U; 05231 loopcounter++; 05232 } 05233 count = CRYP_TIMEOUT_GCMCCMINITPHASE; 05234 do 05235 { 05236 count-- ; 05237 if (count == 0U) 05238 { 05239 /* Disable the CRYP peripheral clock */ 05240 __HAL_CRYP_DISABLE(hcryp); 05241 05242 /* Change state */ 05243 hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT; 05244 hcryp->State = HAL_CRYP_STATE_READY; 05245 05246 /* Process Unlocked */ 05247 __HAL_UNLOCK(hcryp); 05248 return HAL_ERROR; 05249 } 05250 } while (HAL_IS_BIT_CLR(hcryp->Instance->SR, AES_SR_CCF)); 05251 /* Clear CCF flag */ 05252 __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR); 05253 05254 /* Process unlocked */ 05255 __HAL_UNLOCK(hcryp); 05256 05257 /* Change the CRYP state and phase */ 05258 hcryp->State = HAL_CRYP_STATE_READY; 05259 } 05260 } 05261 else 05262 { 05263 /* Process unLocked */ 05264 __HAL_UNLOCK(hcryp); 05265 05266 /* Change the CRYP state and phase */ 05267 hcryp->State = HAL_CRYP_STATE_READY; 05268 } 05269 #endif /* AES */ 05270 #if defined(CRYP) 05271 /********************* Header phase *****************************************/ 05272 05273 if (CRYP_GCMCCM_SetHeaderPhase_DMA(hcryp) != HAL_OK) 05274 { 05275 return HAL_ERROR; 05276 } 05277 05278 /******************** Payload phase *****************************************/ 05279 05280 /* Set the phase */ 05281 hcryp->Phase = CRYP_PHASE_PROCESS; 05282 05283 /* Disable the CRYP peripheral */ 05284 __HAL_CRYP_DISABLE(hcryp); 05285 05286 /* Select payload phase once the header phase is performed */ 05287 CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD); 05288 05289 } /* if (DoKeyIVConfig == 1U) */ 05290 if (hcryp->Size != 0U) 05291 { 05292 /* Size should be %4 otherwise Tag will be incorrectly generated for GCM Encryption & CCM Decryption 05293 Workaround is implemented in polling mode, so if last block of 05294 payload <128bit don't use HAL_CRYP_AESGCM_DMA otherwise TAG is incorrectly generated for GCM Encryption. */ 05295 /* Set the input and output addresses and start DMA transfer */ 05296 if ((hcryp->Size % 16U) == 0U) 05297 { 05298 CRYP_SetDMAConfig(hcryp, (uint32_t)(hcryp->pCrypInBuffPtr), hcryp->Size / 4U, (uint32_t)(hcryp->pCrypOutBuffPtr)); 05299 } 05300 else 05301 { 05302 wordsize = (uint32_t)(hcryp->Size) + 16U - ((uint32_t)(hcryp->Size) % 16U) ; 05303 05304 /* Set the input and output addresses and start DMA transfer, pCrypOutBuffPtr size should be %4*/ 05305 CRYP_SetDMAConfig(hcryp, (uint32_t)(hcryp->pCrypInBuffPtr), (uint16_t)wordsize / 4U, 05306 (uint32_t)(hcryp->pCrypOutBuffPtr)); 05307 } 05308 } 05309 else /*Size = 0*/ 05310 { 05311 /* Process unlocked */ 05312 __HAL_UNLOCK(hcryp); 05313 05314 /* Change the CRYP state and phase */ 05315 hcryp->State = HAL_CRYP_STATE_READY; 05316 } 05317 #endif /* CRYP */ 05318 /* Return function status */ 05319 return HAL_OK; 05320 } 05321 05322 /** 05323 * @brief Sets the payload phase in iterrupt mode 05324 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains 05325 * the configuration information for CRYP module 05326 * @retval state 05327 */ 05328 static void CRYP_GCMCCM_SetPayloadPhase_IT(CRYP_HandleTypeDef *hcryp) 05329 { 05330 uint32_t loopcounter; 05331 uint32_t temp[4]; /* Temporary CrypOutBuff */ 05332 uint32_t lastwordsize; 05333 uint32_t npblb; 05334 uint32_t i; 05335 #if defined(AES) 05336 uint16_t outcount; /* Temporary CrypOutCount Value */ 05337 #endif /* AES */ 05338 05339 /***************************** Payload phase *******************************/ 05340 05341 #if defined(CRYP) 05342 if (hcryp->Size == 0U) 05343 { 05344 /* Disable interrupts */ 05345 __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI); 05346 05347 /* Process unlocked */ 05348 __HAL_UNLOCK(hcryp); 05349 05350 /* Change the CRYP state */ 05351 hcryp->State = HAL_CRYP_STATE_READY; 05352 } 05353 05354 else if (((hcryp->Size / 4U) - (hcryp->CrypInCount)) >= 4U) 05355 { 05356 /* Write the input block in the IN FIFO */ 05357 hcryp->Instance->DIN = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount); 05358 hcryp->CrypInCount++; 05359 hcryp->Instance->DIN = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount); 05360 hcryp->CrypInCount++; 05361 hcryp->Instance->DIN = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount); 05362 hcryp->CrypInCount++; 05363 hcryp->Instance->DIN = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount); 05364 hcryp->CrypInCount++; 05365 if (((hcryp->Size / 4U) == hcryp->CrypInCount) && ((hcryp->Size % 16U) == 0U)) 05366 { 05367 /* Disable interrupts */ 05368 __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI); 05369 05370 /* Call the input data transfer complete callback */ 05371 #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1U) 05372 /*Call registered Input complete callback*/ 05373 hcryp->InCpltCallback(hcryp); 05374 #else 05375 /*Call legacy weak Input complete callback*/ 05376 HAL_CRYP_InCpltCallback(hcryp); 05377 #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */ 05378 } 05379 if (hcryp->CrypOutCount < (hcryp->Size / 4U)) 05380 { 05381 /* Read the output block from the Output FIFO and put them in temporary buffer then get CrypOutBuff from temporary buffer */ 05382 for (i = 0U; i < 4U; i++) 05383 { 05384 temp[i] = hcryp->Instance->DOUT; 05385 } 05386 i = 0U; 05387 while (((hcryp->CrypOutCount < ((hcryp->Size) / 4U))) && (i < 4U)) 05388 { 05389 *(uint32_t *)(hcryp->pCrypOutBuffPtr + hcryp->CrypOutCount) = temp[i]; 05390 hcryp->CrypOutCount++; 05391 i++; 05392 } 05393 if (((hcryp->Size / 4U) == hcryp->CrypOutCount) && ((hcryp->Size % 16U) == 0U)) 05394 { 05395 /* Disable interrupts */ 05396 __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI); 05397 05398 /* Change the CRYP state */ 05399 hcryp->State = HAL_CRYP_STATE_READY; 05400 05401 /* Disable CRYP */ 05402 __HAL_CRYP_DISABLE(hcryp); 05403 05404 /* Process unlocked */ 05405 __HAL_UNLOCK(hcryp); 05406 05407 /* Call output transfer complete callback */ 05408 #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1) 05409 /*Call registered Output complete callback*/ 05410 hcryp->OutCpltCallback(hcryp); 05411 #else 05412 /*Call legacy weak Output complete callback*/ 05413 HAL_CRYP_OutCpltCallback(hcryp); 05414 #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */ 05415 } 05416 } 05417 } 05418 else if ((hcryp->Size % 16U) != 0U) 05419 { 05420 /* Size should be %4 in word and %16 in byte otherwise TAG will be incorrectly generated for GCM Encryption & CCM Decryption 05421 Workaround is implemented in polling mode, so if last block of 05422 payload <128bit don't use CRYP_AESGCM_Encrypt_IT otherwise TAG is incorrectly generated. */ 05423 05424 /* Compute the number of padding bytes in last block of payload */ 05425 npblb = ((((uint32_t)(hcryp->Size) / 16U) + 1U) * 16U) - (uint32_t)(hcryp->Size); 05426 05427 /* Number of valid words (lastwordsize) in last block */ 05428 if ((npblb % 4U) == 0U) 05429 { 05430 lastwordsize = (16U - npblb) / 4U; 05431 } 05432 else 05433 { 05434 lastwordsize = ((16U - npblb) / 4U) + 1U; 05435 } 05436 05437 /* Last block optionally pad the data with zeros*/ 05438 for (loopcounter = 0U; loopcounter < lastwordsize; loopcounter++) 05439 { 05440 hcryp->Instance->DIN = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount); 05441 hcryp->CrypInCount++; 05442 } 05443 while (loopcounter < 4U) 05444 { 05445 /* Pad the data with zeros to have a complete block */ 05446 hcryp->Instance->DIN = 0x0U; 05447 loopcounter++; 05448 } 05449 __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI); 05450 05451 if ((hcryp->Instance->SR & CRYP_FLAG_OFNE) != 0x0U) 05452 { 05453 for (i = 0U; i < 4U; i++) 05454 { 05455 temp[i] = hcryp->Instance->DOUT; 05456 } 05457 if (((hcryp->Size) / 4U) == 0U) 05458 { 05459 for (i = 0U; i < ((uint32_t)(hcryp->Size) % 4U); i++) 05460 { 05461 *(uint32_t *)(hcryp->pCrypOutBuffPtr + hcryp->CrypOutCount) = temp[i]; 05462 hcryp->CrypOutCount++; 05463 } 05464 } 05465 i = 0x0U; 05466 while (((hcryp->CrypOutCount < ((hcryp->Size) / 4U))) && (i < 4U)) 05467 { 05468 *(uint32_t *)(hcryp->pCrypOutBuffPtr + hcryp->CrypOutCount) = temp[i]; 05469 hcryp->CrypOutCount++; 05470 i++; 05471 } 05472 } 05473 if (hcryp->CrypOutCount >= (hcryp->Size / 4U)) 05474 { 05475 /* Disable interrupts */ 05476 __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI | CRYP_IT_INI); 05477 05478 /* Change the CRYP peripheral state */ 05479 hcryp->State = HAL_CRYP_STATE_READY; 05480 05481 /* Process unlocked */ 05482 __HAL_UNLOCK(hcryp); 05483 05484 /* Call output transfer complete callback */ 05485 #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1) 05486 /*Call registered Output complete callback*/ 05487 hcryp->OutCpltCallback(hcryp); 05488 #else 05489 /*Call legacy weak Output complete callback*/ 05490 HAL_CRYP_OutCpltCallback(hcryp); 05491 #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */ 05492 } 05493 } 05494 else 05495 { 05496 /* Nothing to do */ 05497 } 05498 #else /* AES */ 05499 05500 /* Read the output block from the output FIFO and put them in temporary buffer then get CrypOutBuff from temporary buffer*/ 05501 for (i = 0U; i < 4U; i++) 05502 { 05503 temp[i] = hcryp->Instance->DOUTR; 05504 } 05505 i = 0U; 05506 while ((hcryp->CrypOutCount < ((hcryp->Size + 3U) / 4U)) && (i < 4U)) 05507 { 05508 *(uint32_t *)(hcryp->pCrypOutBuffPtr + hcryp->CrypOutCount) = temp[i]; 05509 hcryp->CrypOutCount++; 05510 i++; 05511 } 05512 /*Temporary CrypOutCount Value*/ 05513 outcount = hcryp->CrypOutCount; 05514 05515 if ((hcryp->CrypOutCount >= (hcryp->Size / 4U)) && ((outcount * 4U) >= hcryp->Size)) 05516 { 05517 /* Disable computation complete flag and errors interrupts */ 05518 __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_CCFIE | CRYP_IT_ERRIE); 05519 05520 /* Change the CRYP state */ 05521 hcryp->State = HAL_CRYP_STATE_READY; 05522 05523 /* Process unlocked */ 05524 __HAL_UNLOCK(hcryp); 05525 05526 /* Call output transfer complete callback */ 05527 #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1) 05528 /*Call registered Output complete callback*/ 05529 hcryp->OutCpltCallback(hcryp); 05530 #else 05531 /*Call legacy weak Output complete callback*/ 05532 HAL_CRYP_OutCpltCallback(hcryp); 05533 #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */ 05534 } 05535 05536 else if (((hcryp->Size / 4U) - (hcryp->CrypInCount)) >= 4U) 05537 { 05538 /* Write the input block in the IN FIFO */ 05539 hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount); 05540 hcryp->CrypInCount++; 05541 hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount); 05542 hcryp->CrypInCount++; 05543 hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount); 05544 hcryp->CrypInCount++; 05545 hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount); 05546 hcryp->CrypInCount++; 05547 if ((hcryp->CrypInCount == (hcryp->Size / 4U)) && ((hcryp->Size % 16U) == 0U)) 05548 { 05549 /* Call Input transfer complete callback */ 05550 #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1) 05551 /*Call registered Input complete callback*/ 05552 hcryp->InCpltCallback(hcryp); 05553 #else 05554 /*Call legacy weak Input complete callback*/ 05555 HAL_CRYP_InCpltCallback(hcryp); 05556 #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */ 05557 } 05558 } 05559 else /* Last block of payload < 128bit*/ 05560 { 05561 /* Workaround not implemented, Size should be %4 otherwise Tag will be incorrectly 05562 generated for GCM Encryption & CCM Decryption. Workaround is implemented in polling mode, so if last block of 05563 payload <128bit don't use CRYP_Encrypt_IT otherwise TAG is incorrectly generated for GCM Encryption & CCM Decryption. */ 05564 05565 /* Compute the number of padding bytes in last block of payload */ 05566 npblb = ((((uint32_t)(hcryp->Size) / 16U) + 1U) * 16U) - (uint32_t)(hcryp->Size); 05567 05568 /* Number of valid words (lastwordsize) in last block */ 05569 if ((npblb % 4U) == 0U) 05570 { 05571 lastwordsize = (16U - npblb) / 4U; 05572 } 05573 else 05574 { 05575 lastwordsize = ((16U - npblb) / 4U) + 1U; 05576 } 05577 05578 /* Last block optionally pad the data with zeros*/ 05579 for (loopcounter = 0U; loopcounter < lastwordsize; loopcounter++) 05580 { 05581 hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount); 05582 hcryp->CrypInCount++; 05583 } 05584 while (loopcounter < 4U) 05585 { 05586 /* pad the data with zeros to have a complete block */ 05587 hcryp->Instance->DINR = 0x0U; 05588 loopcounter++; 05589 } 05590 } 05591 #endif /* AES */ 05592 05593 } 05594 05595 05596 /** 05597 * @brief Sets the header phase in polling mode 05598 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains 05599 * the configuration information for CRYP module(Header & HeaderSize) 05600 * @param Timeout: Timeout value 05601 * @retval state 05602 */ 05603 static HAL_StatusTypeDef CRYP_GCMCCM_SetHeaderPhase(CRYP_HandleTypeDef *hcryp, uint32_t Timeout) 05604 { 05605 uint32_t loopcounter; 05606 uint32_t size_in_bytes; 05607 uint32_t tmp; 05608 uint32_t mask[12] = {0x0U, 0xFF000000U, 0xFFFF0000U, 0xFFFFFF00U, /* 32-bit data type */ 05609 0x0U, 0x0000FF00U, 0x0000FFFFU, 0xFF00FFFFU, /* 16-bit data type */ 05610 0x0U, 0x000000FFU, 0x0000FFFFU, 0x00FFFFFFU}; /* 8-bit data type */ 05611 05612 /***************************** Header phase for GCM/GMAC or CCM *********************************/ 05613 05614 if (hcryp->Init.HeaderWidthUnit == CRYP_HEADERWIDTHUNIT_WORD) 05615 { 05616 size_in_bytes = hcryp->Init.HeaderSize * 4U; 05617 } 05618 else 05619 { 05620 size_in_bytes = hcryp->Init.HeaderSize; 05621 } 05622 05623 if (size_in_bytes != 0U) 05624 { 05625 05626 #if defined(CRYP) 05627 05628 /* Select header phase */ 05629 CRYP_SET_PHASE(hcryp, CRYP_PHASE_HEADER); 05630 05631 /* Enable the CRYP peripheral */ 05632 __HAL_CRYP_ENABLE(hcryp); 05633 05634 if ((size_in_bytes % 16U) == 0U) 05635 { 05636 /* HeaderSize %4, no padding */ 05637 for (loopcounter = 0U; (loopcounter < (size_in_bytes / 4U)); loopcounter += 4U) 05638 { 05639 hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount); 05640 hcryp->CrypHeaderCount++ ; 05641 hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount); 05642 hcryp->CrypHeaderCount++ ; 05643 hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount); 05644 hcryp->CrypHeaderCount++ ; 05645 hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount); 05646 hcryp->CrypHeaderCount++ ; 05647 05648 /* Wait for IFEM to be raised */ 05649 if (CRYP_WaitOnIFEMFlag(hcryp, Timeout) != HAL_OK) 05650 { 05651 /* Disable the CRYP peripheral clock */ 05652 __HAL_CRYP_DISABLE(hcryp); 05653 05654 /* Change state */ 05655 hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT; 05656 hcryp->State = HAL_CRYP_STATE_READY; 05657 05658 /* Process unlocked */ 05659 __HAL_UNLOCK(hcryp); 05660 return HAL_ERROR; 05661 } 05662 } 05663 } 05664 else 05665 { 05666 /*Write header block in the IN FIFO without last block */ 05667 for (loopcounter = 0U; (loopcounter < ((size_in_bytes / 16U) * 4U)); loopcounter += 4U) 05668 { 05669 hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount); 05670 hcryp->CrypHeaderCount++ ; 05671 hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount); 05672 hcryp->CrypHeaderCount++ ; 05673 hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount); 05674 hcryp->CrypHeaderCount++ ; 05675 hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount); 05676 hcryp->CrypHeaderCount++ ; 05677 05678 /* Wait for IFEM to be raised */ 05679 if (CRYP_WaitOnIFEMFlag(hcryp, Timeout) != HAL_OK) 05680 { 05681 /* Disable the CRYP peripheral clock */ 05682 __HAL_CRYP_DISABLE(hcryp); 05683 05684 /* Change state */ 05685 hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT; 05686 hcryp->State = HAL_CRYP_STATE_READY; 05687 05688 /* Process unlocked */ 05689 __HAL_UNLOCK(hcryp); 05690 return HAL_ERROR; 05691 } 05692 } 05693 /* Last block optionally pad the data with zeros*/ 05694 for (loopcounter = 0U; (loopcounter < ((size_in_bytes / 4U) % 4U)); loopcounter++) 05695 { 05696 hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount); 05697 hcryp->CrypHeaderCount++ ; 05698 } 05699 /* If the header size is a multiple of words */ 05700 if ((size_in_bytes % 4U) == 0U) 05701 { 05702 /* Pad the data with zeros to have a complete block */ 05703 while (loopcounter < 4U) 05704 { 05705 hcryp->Instance->DIN = 0x0U; 05706 loopcounter++; 05707 } 05708 } 05709 else 05710 { 05711 /* Enter last bytes, padded with zeroes */ 05712 tmp = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount); 05713 tmp &= mask[(hcryp->Init.DataType * 2U) + (size_in_bytes % 4U)]; 05714 hcryp->Instance->DIN = tmp; 05715 loopcounter++; 05716 /* Pad the data with zeros to have a complete block */ 05717 while (loopcounter < 4U) 05718 { 05719 hcryp->Instance->DIN = 0x0U; 05720 loopcounter++; 05721 } 05722 } 05723 /* Wait for CCF IFEM to be raised */ 05724 if (CRYP_WaitOnIFEMFlag(hcryp, Timeout) != HAL_OK) 05725 { 05726 /* Disable the CRYP peripheral clock */ 05727 __HAL_CRYP_DISABLE(hcryp); 05728 05729 /* Change state */ 05730 hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT; 05731 hcryp->State = HAL_CRYP_STATE_READY; 05732 05733 /* Process unlocked */ 05734 __HAL_UNLOCK(hcryp); 05735 return HAL_ERROR; 05736 } 05737 } 05738 /* Wait until the complete message has been processed */ 05739 if (CRYP_WaitOnBUSYFlag(hcryp, Timeout) != HAL_OK) 05740 { 05741 /* Disable the CRYP peripheral clock */ 05742 __HAL_CRYP_DISABLE(hcryp); 05743 05744 /* Change state */ 05745 hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT; 05746 hcryp->State = HAL_CRYP_STATE_READY; 05747 05748 /* Process unlocked & return error */ 05749 __HAL_UNLOCK(hcryp); 05750 return HAL_ERROR; 05751 } 05752 05753 #else /* AES */ 05754 05755 if (hcryp->Init.Algorithm == CRYP_AES_GCM_GMAC) 05756 { 05757 /* Workaround 1 :only AES before re-enabling the IP, datatype can be configured.*/ 05758 MODIFY_REG(hcryp->Instance->CR, AES_CR_DATATYPE, hcryp->Init.DataType); 05759 05760 /* Select header phase */ 05761 CRYP_SET_PHASE(hcryp, CRYP_PHASE_HEADER); 05762 05763 /* Enable the CRYP peripheral */ 05764 __HAL_CRYP_ENABLE(hcryp); 05765 05766 } 05767 /* If size_in_bytes is a multiple of blocks (a multiple of four 32-bits words ) */ 05768 if ((size_in_bytes % 16U) == 0U) 05769 { 05770 /* No padding */ 05771 for (loopcounter = 0U; (loopcounter < (size_in_bytes / 4U)); loopcounter += 4U) 05772 { 05773 /* Write the input block in the data input register */ 05774 hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount); 05775 hcryp->CrypHeaderCount++ ; 05776 hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount); 05777 hcryp->CrypHeaderCount++ ; 05778 hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount); 05779 hcryp->CrypHeaderCount++ ; 05780 hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount); 05781 hcryp->CrypHeaderCount++ ; 05782 05783 if (CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK) 05784 { 05785 /* Disable the CRYP peripheral clock */ 05786 __HAL_CRYP_DISABLE(hcryp); 05787 05788 /* Change state */ 05789 hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT; 05790 hcryp->State = HAL_CRYP_STATE_READY; 05791 05792 /* Process unlocked */ 05793 __HAL_UNLOCK(hcryp); 05794 return HAL_ERROR; 05795 } 05796 /* Clear CCF flag */ 05797 __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR); 05798 } 05799 } 05800 else 05801 { 05802 /*Write header block in the IN FIFO without last block */ 05803 for (loopcounter = 0U; (loopcounter < ((size_in_bytes / 16U) * 4U)); loopcounter += 4U) 05804 { 05805 /* Write the input block in the data input register */ 05806 hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount); 05807 hcryp->CrypHeaderCount++ ; 05808 hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount); 05809 hcryp->CrypHeaderCount++ ; 05810 hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount); 05811 hcryp->CrypHeaderCount++ ; 05812 hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount); 05813 hcryp->CrypHeaderCount++ ; 05814 05815 if (CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK) 05816 { 05817 /* Disable the CRYP peripheral clock */ 05818 __HAL_CRYP_DISABLE(hcryp); 05819 05820 /* Change state */ 05821 hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT; 05822 hcryp->State = HAL_CRYP_STATE_READY; 05823 05824 /* Process unlocked */ 05825 __HAL_UNLOCK(hcryp); 05826 return HAL_ERROR; 05827 } 05828 /* Clear CCF flag */ 05829 __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR); 05830 } 05831 /* Write last complete words */ 05832 for (loopcounter = 0U; (loopcounter < ((size_in_bytes / 4U) % 4U)); loopcounter++) 05833 { 05834 hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount); 05835 hcryp->CrypHeaderCount++ ; 05836 } 05837 /* If the header size is a multiple of words */ 05838 if ((size_in_bytes % 4U) == 0U) 05839 { 05840 /* Pad the data with zeros to have a complete block */ 05841 while (loopcounter < 4U) 05842 { 05843 hcryp->Instance->DINR = 0x0U; 05844 loopcounter++; 05845 } 05846 } 05847 else 05848 { 05849 /* Enter last bytes, padded with zeroes */ 05850 tmp = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount); 05851 tmp &= mask[(hcryp->Init.DataType * 2U) + (size_in_bytes % 4U)]; 05852 hcryp->Instance->DINR = tmp; 05853 loopcounter++; 05854 /* Pad the data with zeros to have a complete block */ 05855 while (loopcounter < 4U) 05856 { 05857 hcryp->Instance->DINR = 0x0U; 05858 loopcounter++; 05859 } 05860 } 05861 05862 if (CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK) 05863 { 05864 /* Disable the CRYP peripheral clock */ 05865 __HAL_CRYP_DISABLE(hcryp); 05866 05867 /* Change state */ 05868 hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT; 05869 hcryp->State = HAL_CRYP_STATE_READY; 05870 05871 /* Process unlocked */ 05872 __HAL_UNLOCK(hcryp); 05873 return HAL_ERROR; 05874 } 05875 /* Clear CCF flag */ 05876 __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR); 05877 } 05878 #endif /* End AES or CRYP */ 05879 } 05880 else 05881 { 05882 #if defined(AES) 05883 if (hcryp->Init.Algorithm == CRYP_AES_GCM_GMAC) 05884 { 05885 /*Workaround 1: only AES, before re-enabling the IP, datatype can be configured.*/ 05886 MODIFY_REG(hcryp->Instance->CR, AES_CR_DATATYPE, hcryp->Init.DataType); 05887 05888 /* Select header phase */ 05889 CRYP_SET_PHASE(hcryp, CRYP_PHASE_HEADER); 05890 05891 /* Enable the CRYP peripheral */ 05892 __HAL_CRYP_ENABLE(hcryp); 05893 } 05894 #endif /* AES */ 05895 } 05896 /* Return function status */ 05897 return HAL_OK; 05898 } 05899 05900 /** 05901 * @brief Sets the header phase when using DMA in process 05902 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains 05903 * the configuration information for CRYP module(Header & HeaderSize) 05904 * @retval None 05905 */ 05906 static HAL_StatusTypeDef CRYP_GCMCCM_SetHeaderPhase_DMA(CRYP_HandleTypeDef *hcryp) 05907 { 05908 __IO uint32_t count = 0U; 05909 uint32_t loopcounter; 05910 uint32_t headersize_in_bytes; 05911 uint32_t tmp; 05912 uint32_t mask[12] = {0x0U, 0xFF000000U, 0xFFFF0000U, 0xFFFFFF00U, /* 32-bit data type */ 05913 0x0U, 0x0000FF00U, 0x0000FFFFU, 0xFF00FFFFU, /* 16-bit data type */ 05914 0x0U, 0x000000FFU, 0x0000FFFFU, 0x00FFFFFFU}; /* 8-bit data type */ 05915 05916 /***************************** Header phase for GCM/GMAC or CCM *********************************/ 05917 if (hcryp->Init.HeaderWidthUnit == CRYP_HEADERWIDTHUNIT_WORD) 05918 { 05919 headersize_in_bytes = hcryp->Init.HeaderSize * 4U; 05920 } 05921 else 05922 { 05923 headersize_in_bytes = hcryp->Init.HeaderSize; 05924 } 05925 05926 if (headersize_in_bytes != 0U) 05927 { 05928 05929 #if defined(CRYP) 05930 05931 /* Select header phase */ 05932 CRYP_SET_PHASE(hcryp, CRYP_PHASE_HEADER); 05933 05934 /* Enable the CRYP peripheral */ 05935 __HAL_CRYP_ENABLE(hcryp); 05936 05937 if ((headersize_in_bytes % 16U) == 0U) 05938 { 05939 /* HeaderSize %4, no padding */ 05940 for (loopcounter = 0U; (loopcounter < (headersize_in_bytes / 4U)); loopcounter += 4U) 05941 { 05942 hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount); 05943 hcryp->CrypHeaderCount++ ; 05944 hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount); 05945 hcryp->CrypHeaderCount++ ; 05946 hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount); 05947 hcryp->CrypHeaderCount++ ; 05948 hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount); 05949 hcryp->CrypHeaderCount++ ; 05950 05951 /* Wait for IFEM to be raised */ 05952 count = CRYP_TIMEOUT_GCMCCMHEADERPHASE; 05953 do 05954 { 05955 count-- ; 05956 if (count == 0U) 05957 { 05958 /* Disable the CRYP peripheral clock */ 05959 __HAL_CRYP_DISABLE(hcryp); 05960 05961 /* Change state */ 05962 hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT; 05963 hcryp->State = HAL_CRYP_STATE_READY; 05964 05965 /* Process unlocked */ 05966 __HAL_UNLOCK(hcryp); 05967 return HAL_ERROR; 05968 } 05969 } while (HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_IFEM)); 05970 } 05971 } 05972 else 05973 { 05974 /*Write header block in the IN FIFO without last block */ 05975 for (loopcounter = 0U; (loopcounter < ((headersize_in_bytes / 16U) * 4U)); loopcounter += 4U) 05976 { 05977 hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount); 05978 hcryp->CrypHeaderCount++ ; 05979 hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount); 05980 hcryp->CrypHeaderCount++ ; 05981 hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount); 05982 hcryp->CrypHeaderCount++ ; 05983 hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount); 05984 hcryp->CrypHeaderCount++ ; 05985 05986 /* Wait for IFEM to be raised */ 05987 count = CRYP_TIMEOUT_GCMCCMHEADERPHASE; 05988 do 05989 { 05990 count-- ; 05991 if (count == 0U) 05992 { 05993 /* Disable the CRYP peripheral clock */ 05994 __HAL_CRYP_DISABLE(hcryp); 05995 05996 /* Change state */ 05997 hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT; 05998 hcryp->State = HAL_CRYP_STATE_READY; 05999 06000 /* Process unlocked */ 06001 __HAL_UNLOCK(hcryp); 06002 return HAL_ERROR; 06003 } 06004 } while (HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_IFEM)); 06005 } 06006 /* Last block optionally pad the data with zeros*/ 06007 for (loopcounter = 0U; (loopcounter < ((headersize_in_bytes / 4U) % 4U)); loopcounter++) 06008 { 06009 hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount); 06010 hcryp->CrypHeaderCount++ ; 06011 } 06012 /* If the header size is a multiple of words */ 06013 if ((headersize_in_bytes % 4U) == 0U) 06014 { 06015 /* Pad the data with zeros to have a complete block */ 06016 while (loopcounter < 4U) 06017 { 06018 hcryp->Instance->DIN = 0x0U; 06019 loopcounter++; 06020 } 06021 } 06022 else 06023 { 06024 /* Enter last bytes, padded with zeroes */ 06025 tmp = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount); 06026 tmp &= mask[(hcryp->Init.DataType * 2U) + (headersize_in_bytes % 4U)]; 06027 hcryp->Instance->DIN = tmp; 06028 loopcounter++; 06029 /* Pad the data with zeros to have a complete block */ 06030 while (loopcounter < 4U) 06031 { 06032 hcryp->Instance->DIN = 0x0U; 06033 loopcounter++; 06034 } 06035 } 06036 /* Wait for IFEM to be raised */ 06037 count = CRYP_TIMEOUT_GCMCCMHEADERPHASE; 06038 do 06039 { 06040 count-- ; 06041 if (count == 0U) 06042 { 06043 /* Disable the CRYP peripheral clock */ 06044 __HAL_CRYP_DISABLE(hcryp); 06045 06046 /* Change state */ 06047 hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT; 06048 hcryp->State = HAL_CRYP_STATE_READY; 06049 06050 /* Process unlocked */ 06051 __HAL_UNLOCK(hcryp); 06052 return HAL_ERROR; 06053 } 06054 } while (HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_IFEM)); 06055 } 06056 /* Wait until the complete message has been processed */ 06057 count = CRYP_TIMEOUT_GCMCCMHEADERPHASE; 06058 do 06059 { 06060 count-- ; 06061 if (count == 0U) 06062 { 06063 /* Disable the CRYP peripheral clock */ 06064 __HAL_CRYP_DISABLE(hcryp); 06065 06066 /* Change state */ 06067 hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT; 06068 hcryp->State = HAL_CRYP_STATE_READY; 06069 06070 /* Process unlocked */ 06071 __HAL_UNLOCK(hcryp); 06072 return HAL_ERROR; 06073 } 06074 } while (HAL_IS_BIT_SET(hcryp->Instance->SR, CRYP_FLAG_BUSY)); 06075 06076 #else /* AES */ 06077 06078 if (hcryp->Init.Algorithm == CRYP_AES_GCM_GMAC) 06079 { 06080 /* Workaround 1: only AES, before re-enabling the IP, datatype can be configured.*/ 06081 MODIFY_REG(hcryp->Instance->CR, AES_CR_DATATYPE, hcryp->Init.DataType); 06082 06083 /* Select header phase */ 06084 CRYP_SET_PHASE(hcryp, CRYP_PHASE_HEADER); 06085 06086 /* Enable the CRYP peripheral */ 06087 __HAL_CRYP_ENABLE(hcryp); 06088 } 06089 if ((headersize_in_bytes % 16U) == 0U) 06090 { 06091 /* HeaderSize %4, no padding */ 06092 for (loopcounter = 0U; (loopcounter < (headersize_in_bytes / 4U)); loopcounter += 4U) 06093 { 06094 /* Write the input block in the data input register */ 06095 hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount); 06096 hcryp->CrypHeaderCount++ ; 06097 hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount); 06098 hcryp->CrypHeaderCount++ ; 06099 hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount); 06100 hcryp->CrypHeaderCount++ ; 06101 hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount); 06102 hcryp->CrypHeaderCount++ ; 06103 06104 /*Wait on CCF flag*/ 06105 count = CRYP_TIMEOUT_GCMCCMHEADERPHASE; 06106 do 06107 { 06108 count-- ; 06109 if (count == 0U) 06110 { 06111 /* Disable the CRYP peripheral clock */ 06112 __HAL_CRYP_DISABLE(hcryp); 06113 06114 /* Change state */ 06115 hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT; 06116 hcryp->State = HAL_CRYP_STATE_READY; 06117 06118 /* Process unlocked */ 06119 __HAL_UNLOCK(hcryp); 06120 return HAL_ERROR; 06121 } 06122 } while (HAL_IS_BIT_CLR(hcryp->Instance->SR, AES_SR_CCF)); 06123 06124 /* Clear CCF flag */ 06125 __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR); 06126 } 06127 } 06128 else 06129 { 06130 /*Write header block in the IN FIFO without last block */ 06131 for (loopcounter = 0U; (loopcounter < ((headersize_in_bytes / 16U) * 4U)); loopcounter += 4U) 06132 { 06133 /* Write the Input block in the Data Input register */ 06134 hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount); 06135 hcryp->CrypHeaderCount++ ; 06136 hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount); 06137 hcryp->CrypHeaderCount++ ; 06138 hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount); 06139 hcryp->CrypHeaderCount++ ; 06140 hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount); 06141 hcryp->CrypHeaderCount++ ; 06142 06143 /*Wait on CCF flag*/ 06144 count = CRYP_TIMEOUT_GCMCCMHEADERPHASE; 06145 do 06146 { 06147 count-- ; 06148 if (count == 0U) 06149 { 06150 /* Disable the CRYP peripheral clock */ 06151 __HAL_CRYP_DISABLE(hcryp); 06152 06153 /* Change state */ 06154 hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT; 06155 hcryp->State = HAL_CRYP_STATE_READY; 06156 06157 /* Process unlocked */ 06158 __HAL_UNLOCK(hcryp); 06159 return HAL_ERROR; 06160 } 06161 } while (HAL_IS_BIT_CLR(hcryp->Instance->SR, AES_SR_CCF)); 06162 06163 /* Clear CCF flag */ 06164 __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR); 06165 } 06166 /* Last block optionally pad the data with zeros*/ 06167 for (loopcounter = 0U; (loopcounter < ((headersize_in_bytes /4U) % 4U)); loopcounter++) 06168 { 06169 hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount); 06170 hcryp->CrypHeaderCount++ ; 06171 } 06172 /* If the header size is a multiple of words */ 06173 if ((headersize_in_bytes % 4U) == 0U) 06174 { 06175 /* Pad the data with zeros to have a complete block */ 06176 while (loopcounter < 4U) 06177 { 06178 hcryp->Instance->DINR = 0x0U; 06179 loopcounter++; 06180 } 06181 } 06182 else 06183 { 06184 /* Enter last bytes, padded with zeroes */ 06185 tmp = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount); 06186 tmp &= mask[(hcryp->Init.DataType * 2U) + (headersize_in_bytes % 4U)]; 06187 hcryp->Instance->DINR = tmp; 06188 loopcounter++; 06189 /* Pad the data with zeros to have a complete block */ 06190 while (loopcounter < 4U) 06191 { 06192 hcryp->Instance->DINR = 0x0U; 06193 loopcounter++; 06194 } 06195 } 06196 /*Wait on CCF flag*/ 06197 count = CRYP_TIMEOUT_GCMCCMHEADERPHASE; 06198 do 06199 { 06200 count-- ; 06201 if (count == 0U) 06202 { 06203 /* Disable the CRYP peripheral clock */ 06204 __HAL_CRYP_DISABLE(hcryp); 06205 06206 /* Change state */ 06207 hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT; 06208 hcryp->State = HAL_CRYP_STATE_READY; 06209 06210 /* Process unlocked */ 06211 __HAL_UNLOCK(hcryp); 06212 return HAL_ERROR; 06213 } 06214 } while (HAL_IS_BIT_CLR(hcryp->Instance->SR, AES_SR_CCF)); 06215 06216 /* Clear CCF flag */ 06217 __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR); 06218 } 06219 #endif /* End AES or CRYP */ 06220 } 06221 else 06222 { 06223 #if defined(AES) 06224 if (hcryp->Init.Algorithm == CRYP_AES_GCM_GMAC) 06225 { 06226 /*Workaround 1: only AES, before re-enabling the IP, datatype can be configured.*/ 06227 MODIFY_REG(hcryp->Instance->CR, AES_CR_DATATYPE, hcryp->Init.DataType); 06228 06229 /* Select header phase */ 06230 CRYP_SET_PHASE(hcryp, CRYP_PHASE_HEADER); 06231 06232 /* Enable the CRYP peripheral */ 06233 __HAL_CRYP_ENABLE(hcryp); 06234 } 06235 #endif /* AES */ 06236 } 06237 /* Return function status */ 06238 return HAL_OK; 06239 } 06240 06241 /** 06242 * @brief Sets the header phase in interrupt mode 06243 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains 06244 * the configuration information for CRYP module(Header & HeaderSize) 06245 * @retval None 06246 */ 06247 static void CRYP_GCMCCM_SetHeaderPhase_IT(CRYP_HandleTypeDef *hcryp) 06248 { 06249 uint32_t loopcounter; 06250 #if defined(AES) 06251 uint32_t lastwordsize; 06252 uint32_t npblb; 06253 #endif 06254 uint32_t headersize_in_bytes; 06255 uint32_t tmp; 06256 uint32_t mask[12] = {0x0U, 0xFF000000U, 0xFFFF0000U, 0xFFFFFF00U, /* 32-bit data type */ 06257 0x0U, 0x0000FF00U, 0x0000FFFFU, 0xFF00FFFFU, /* 16-bit data type */ 06258 0x0U, 0x000000FFU, 0x0000FFFFU, 0x00FFFFFFU}; /* 8-bit data type */ 06259 06260 if (hcryp->Init.HeaderWidthUnit == CRYP_HEADERWIDTHUNIT_WORD) 06261 { 06262 headersize_in_bytes = hcryp->Init.HeaderSize * 4U; 06263 } 06264 else 06265 { 06266 headersize_in_bytes = hcryp->Init.HeaderSize; 06267 } 06268 06269 /***************************** Header phase *********************************/ 06270 06271 #if defined(CRYP) 06272 if (headersize_in_bytes <= ((uint32_t)(hcryp->CrypHeaderCount) * 4U)) 06273 { 06274 /* Disable interrupts */ 06275 __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI); 06276 06277 /* Disable the CRYP peripheral */ 06278 __HAL_CRYP_DISABLE(hcryp); 06279 06280 /* Set the phase */ 06281 hcryp->Phase = CRYP_PHASE_PROCESS; 06282 06283 /* Select payload phase once the header phase is performed */ 06284 CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD); 06285 06286 /* Enable Interrupts */ 06287 __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI); 06288 06289 /* Enable the CRYP peripheral */ 06290 __HAL_CRYP_ENABLE(hcryp); 06291 } 06292 else if (((headersize_in_bytes / 4U) - (hcryp->CrypHeaderCount)) >= 4U) 06293 06294 { 06295 /* HeaderSize %4, no padding */ 06296 hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount); 06297 hcryp->CrypHeaderCount++ ; 06298 hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount); 06299 hcryp->CrypHeaderCount++ ; 06300 hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount); 06301 hcryp->CrypHeaderCount++ ; 06302 hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount); 06303 hcryp->CrypHeaderCount++ ; 06304 } 06305 else 06306 { 06307 /* Last block optionally pad the data with zeros*/ 06308 for (loopcounter = 0U; loopcounter < ((headersize_in_bytes / 4U) % 4U); loopcounter++) 06309 { 06310 hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount); 06311 hcryp->CrypHeaderCount++ ; 06312 } 06313 if ((headersize_in_bytes % 4U) == 0U) 06314 { 06315 /* Pad the data with zeros to have a complete block */ 06316 while (loopcounter < 4U) 06317 { 06318 hcryp->Instance->DIN = 0x0U; 06319 loopcounter++; 06320 hcryp->CrypHeaderCount++; 06321 } 06322 } 06323 else 06324 { 06325 /* Enter last bytes, padded with zeros */ 06326 tmp = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount); 06327 tmp &= mask[(hcryp->Init.DataType * 2U) + (headersize_in_bytes % 4U)]; 06328 hcryp->Instance->DIN = tmp; 06329 loopcounter++; 06330 hcryp->CrypHeaderCount++; 06331 /* Pad the data with zeros to have a complete block */ 06332 while (loopcounter < 4U) 06333 { 06334 hcryp->Instance->DIN = 0x0U; 06335 loopcounter++; 06336 hcryp->CrypHeaderCount++; 06337 } 06338 } 06339 } 06340 #else /* AES */ 06341 06342 if (headersize_in_bytes <= ((uint32_t)(hcryp->CrypHeaderCount) * 4U)) 06343 { 06344 /* Set the phase */ 06345 hcryp->Phase = CRYP_PHASE_PROCESS; 06346 06347 /* Payload phase not supported in CCM AES2 */ 06348 if (hcryp->Init.Algorithm == CRYP_AES_GCM_GMAC) 06349 { 06350 /* Select payload phase once the header phase is performed */ 06351 MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_PHASE_PAYLOAD); 06352 } 06353 if (hcryp->Init.Algorithm == CRYP_AES_CCM) 06354 { 06355 /* Increment CrypHeaderCount to pass in CRYP_GCMCCM_SetPayloadPhase_IT */ 06356 hcryp->CrypHeaderCount++; 06357 } 06358 /* Write the payload Input block in the IN FIFO */ 06359 if (hcryp->Size == 0U) 06360 { 06361 /* Disable interrupts */ 06362 __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_CCFIE | CRYP_IT_ERRIE); 06363 06364 /* Change the CRYP state */ 06365 hcryp->State = HAL_CRYP_STATE_READY; 06366 06367 /* Process unlocked */ 06368 __HAL_UNLOCK(hcryp); 06369 } 06370 else if (hcryp->Size >= 16U) 06371 { 06372 hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount); 06373 hcryp->CrypInCount++; 06374 hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount); 06375 hcryp->CrypInCount++; 06376 hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount); 06377 hcryp->CrypInCount++; 06378 hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount); 06379 hcryp->CrypInCount++; 06380 06381 if ((hcryp->CrypInCount == (hcryp->Size / 4U)) && ((hcryp->Size % 16U) == 0U)) 06382 { 06383 /* Call the input data transfer complete callback */ 06384 #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1) 06385 /*Call registered Input complete callback*/ 06386 hcryp->InCpltCallback(hcryp); 06387 #else 06388 /*Call legacy weak Input complete callback*/ 06389 HAL_CRYP_InCpltCallback(hcryp); 06390 #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */ 06391 } 06392 } 06393 else /* Size < 4 words : first block is the last block*/ 06394 { 06395 /* Workaround not implemented, Size should be %4 otherwise Tag will be incorrectly 06396 generated for GCM Encryption. Workaround is implemented in polling mode, so if last block of 06397 payload <128bit don't use CRYP_Encrypt_IT otherwise TAG is incorrectly generated for GCM Encryption. */ 06398 06399 /* Compute the number of padding bytes in last block of payload */ 06400 npblb = ((((uint32_t)(hcryp->Size) / 16U) + 1U) * 16U) - (uint32_t)(hcryp->Size); 06401 06402 /* Number of valid words (lastwordsize) in last block */ 06403 if ((npblb % 4U) == 0U) 06404 { 06405 lastwordsize = (16U - npblb) / 4U; 06406 } 06407 else 06408 { 06409 lastwordsize = ((16U - npblb) / 4U) + 1U; 06410 } 06411 06412 /* Last block optionally pad the data with zeros*/ 06413 for (loopcounter = 0U; loopcounter < lastwordsize; loopcounter++) 06414 { 06415 hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount); 06416 hcryp->CrypInCount++; 06417 } 06418 while (loopcounter < 4U) 06419 { 06420 /* Pad the data with zeros to have a complete block */ 06421 hcryp->Instance->DINR = 0x0U; 06422 loopcounter++; 06423 } 06424 } 06425 } 06426 else if (((headersize_in_bytes / 4U) - (hcryp->CrypHeaderCount)) >= 4U) 06427 { 06428 /* Write the input block in the IN FIFO */ 06429 hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount); 06430 hcryp->CrypHeaderCount++; 06431 hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount); 06432 hcryp->CrypHeaderCount++; 06433 hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount); 06434 hcryp->CrypHeaderCount++; 06435 hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount); 06436 hcryp->CrypHeaderCount++; 06437 } 06438 else /*HeaderSize < 4 or HeaderSize >4 & HeaderSize %4 != 0*/ 06439 { 06440 /* Last block optionally pad the data with zeros*/ 06441 for (loopcounter = 0U; loopcounter < ((headersize_in_bytes / 4U) % 4U); loopcounter++) 06442 { 06443 hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount); 06444 hcryp->CrypHeaderCount++ ; 06445 } 06446 /* If the header size is a multiple of words */ 06447 if ((headersize_in_bytes % 4U) == 0U) 06448 { 06449 /* Pad the data with zeros to have a complete block */ 06450 while (loopcounter < 4U) 06451 { 06452 hcryp->Instance->DINR = 0x0U; 06453 loopcounter++; 06454 hcryp->CrypHeaderCount++; 06455 } 06456 } 06457 else 06458 { 06459 /* Enter last bytes, padded with zeros */ 06460 tmp = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount); 06461 tmp &= mask[(hcryp->Init.DataType * 2U) + (headersize_in_bytes % 4U)]; 06462 hcryp->Instance->DINR = tmp; 06463 loopcounter++; 06464 hcryp->CrypHeaderCount++; 06465 /* Pad the data with zeros to have a complete block */ 06466 while (loopcounter < 4U) 06467 { 06468 hcryp->Instance->DINR = 0x0U; 06469 loopcounter++; 06470 hcryp->CrypHeaderCount++; 06471 } 06472 } 06473 } 06474 #endif /* End AES or CRYP */ 06475 } 06476 06477 06478 /** 06479 * @brief Workaround used for GCM/CCM mode. 06480 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains 06481 * the configuration information for CRYP module 06482 * @param Timeout: specify Timeout value 06483 * @retval None 06484 */ 06485 static void CRYP_Workaround(CRYP_HandleTypeDef *hcryp, uint32_t Timeout) 06486 { 06487 uint32_t lastwordsize; 06488 uint32_t npblb; 06489 #if defined(CRYP) 06490 uint32_t iv1temp; 06491 uint32_t temp[4] = {0}; 06492 uint32_t temp2[4] = {0}; 06493 #endif /* CRYP */ 06494 uint32_t intermediate_data[4] = {0}; 06495 uint32_t index; 06496 06497 /* Compute the number of padding bytes in last block of payload */ 06498 npblb = ((((uint32_t)(hcryp->Size) / 16U) + 1U) * 16U) - (uint32_t)(hcryp->Size); 06499 06500 /* Number of valid words (lastwordsize) in last block */ 06501 if ((npblb % 4U) == 0U) 06502 { 06503 lastwordsize = (16U - npblb) / 4U; 06504 } 06505 else 06506 { 06507 lastwordsize = ((16U - npblb) / 4U) + 1U; 06508 } 06509 06510 #if defined(CRYP) 06511 06512 /* Workaround 2, case GCM encryption */ 06513 if (hcryp->Init.Algorithm == CRYP_AES_GCM) 06514 { 06515 if ((hcryp->Instance->CR & CRYP_CR_ALGODIR) == CRYP_OPERATINGMODE_ENCRYPT) 06516 { 06517 /*Workaround in order to properly compute authentication tags while doing 06518 a GCM encryption with the last block of payload size inferior to 128 bits*/ 06519 /* Disable CRYP to start the final phase */ 06520 __HAL_CRYP_DISABLE(hcryp); 06521 06522 /*Update CRYP_IV1R register and ALGOMODE*/ 06523 hcryp->Instance->IV1RR = ((hcryp->Instance->CSGCMCCM7R) - 1U); 06524 MODIFY_REG(hcryp->Instance->CR, CRYP_CR_ALGOMODE, CRYP_AES_CTR); 06525 06526 /* Enable CRYP to start the final phase */ 06527 __HAL_CRYP_ENABLE(hcryp); 06528 } 06529 /* Last block optionally pad the data with zeros*/ 06530 for (index = 0; index < lastwordsize; index ++) 06531 { 06532 /* Write the last input block in the IN FIFO */ 06533 hcryp->Instance->DIN = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount); 06534 hcryp->CrypInCount++; 06535 } 06536 while (index < 4U) 06537 { 06538 /* Pad the data with zeros to have a complete block */ 06539 hcryp->Instance->DIN = 0U; 06540 index++; 06541 } 06542 /* Wait for OFNE flag to be raised */ 06543 if (CRYP_WaitOnOFNEFlag(hcryp, Timeout) != HAL_OK) 06544 { 06545 /* Disable the CRYP peripheral clock */ 06546 __HAL_CRYP_DISABLE(hcryp); 06547 06548 /* Change state */ 06549 hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT; 06550 hcryp->State = HAL_CRYP_STATE_READY; 06551 06552 /* Process Unlocked */ 06553 __HAL_UNLOCK(hcryp); 06554 #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1) 06555 /*Call registered error callback*/ 06556 hcryp->ErrorCallback(hcryp); 06557 #else 06558 /*Call legacy weak error callback*/ 06559 HAL_CRYP_ErrorCallback(hcryp); 06560 #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */ 06561 } 06562 if ((hcryp->Instance->SR & CRYP_FLAG_OFNE) != 0x0U) 06563 { 06564 for (index = 0U; index < 4U; index++) 06565 { 06566 /* Read the output block from the output FIFO */ 06567 intermediate_data[index] = hcryp->Instance->DOUT; 06568 06569 /* Intermediate data buffer to be used in for the workaround*/ 06570 *(uint32_t *)(hcryp->pCrypOutBuffPtr + (hcryp->CrypOutCount)) = intermediate_data[index]; 06571 hcryp->CrypOutCount++; 06572 } 06573 } 06574 06575 if ((hcryp->Instance->CR & CRYP_CR_ALGODIR) == CRYP_OPERATINGMODE_ENCRYPT) 06576 { 06577 /*workaround in order to properly compute authentication tags while doing 06578 a GCM encryption with the last block of payload size inferior to 128 bits*/ 06579 /* Change the AES mode to GCM mode and Select Final phase */ 06580 /* configured CHMOD GCM */ 06581 MODIFY_REG(hcryp->Instance->CR, CRYP_CR_ALGOMODE, CRYP_AES_GCM); 06582 06583 /* configured final phase */ 06584 MODIFY_REG(hcryp->Instance->CR, CRYP_CR_GCM_CCMPH, CRYP_PHASE_FINAL); 06585 06586 if ((hcryp->Instance->CR & CRYP_CR_DATATYPE) == CRYP_DATATYPE_32B) 06587 { 06588 if ((npblb % 4U) == 1U) 06589 { 06590 intermediate_data[lastwordsize - 1U] &= 0xFFFFFF00U; 06591 } 06592 if ((npblb % 4U) == 2U) 06593 { 06594 intermediate_data[lastwordsize - 1U] &= 0xFFFF0000U; 06595 } 06596 if ((npblb % 4U) == 3U) 06597 { 06598 intermediate_data[lastwordsize - 1U] &= 0xFF000000U; 06599 } 06600 } 06601 else if ((hcryp->Instance->CR & CRYP_CR_DATATYPE) == CRYP_DATATYPE_8B) 06602 { 06603 if ((npblb % 4U) == 1U) 06604 { 06605 intermediate_data[lastwordsize - 1U] &= __REV(0xFFFFFF00U); 06606 } 06607 if ((npblb % 4U) == 2U) 06608 { 06609 intermediate_data[lastwordsize - 1U] &= __REV(0xFFFF0000U); 06610 } 06611 if ((npblb % 4U) == 3U) 06612 { 06613 intermediate_data[lastwordsize - 1U] &= __REV(0xFF000000U); 06614 } 06615 } 06616 else if ((hcryp->Instance->CR & CRYP_CR_DATATYPE) == CRYP_DATATYPE_16B) 06617 { 06618 if ((npblb % 4U) == 1U) 06619 { 06620 intermediate_data[lastwordsize - 1U] &= __ROR((0xFFFFFF00U), 16); 06621 } 06622 if ((npblb % 4U) == 2U) 06623 { 06624 intermediate_data[lastwordsize - 1U] &= __ROR((0xFFFF0000U), 16); 06625 } 06626 if ((npblb % 4U) == 3U) 06627 { 06628 intermediate_data[lastwordsize - 1U] &= __ROR((0xFF000000U), 16); 06629 } 06630 } 06631 else /*CRYP_DATATYPE_1B*/ 06632 { 06633 if ((npblb % 4U) == 1U) 06634 { 06635 intermediate_data[lastwordsize - 1U] &= __RBIT(0xFFFFFF00U); 06636 } 06637 if ((npblb % 4U) == 2U) 06638 { 06639 intermediate_data[lastwordsize - 1U] &= __RBIT(0xFFFF0000U); 06640 } 06641 if ((npblb % 4U) == 3U) 06642 { 06643 intermediate_data[lastwordsize - 1U] &= __RBIT(0xFF000000U); 06644 } 06645 } 06646 for (index = 0U; index < lastwordsize; index ++) 06647 { 06648 /*Write the intermediate_data in the IN FIFO */ 06649 hcryp->Instance->DIN = intermediate_data[index]; 06650 } 06651 while (index < 4U) 06652 { 06653 /* Pad the data with zeros to have a complete block */ 06654 hcryp->Instance->DIN = 0x0U; 06655 index++; 06656 } 06657 /* Wait for OFNE flag to be raised */ 06658 if (CRYP_WaitOnOFNEFlag(hcryp, Timeout) != HAL_OK) 06659 { 06660 /* Disable the CRYP peripheral clock */ 06661 __HAL_CRYP_DISABLE(hcryp); 06662 06663 /* Change state */ 06664 hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT; 06665 hcryp->State = HAL_CRYP_STATE_READY; 06666 06667 /* Process unlocked */ 06668 __HAL_UNLOCK(hcryp); 06669 #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1) 06670 /*Call registered error callback*/ 06671 hcryp->ErrorCallback(hcryp); 06672 #else 06673 /*Call legacy weak error callback*/ 06674 HAL_CRYP_ErrorCallback(hcryp); 06675 #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */ 06676 } 06677 06678 if ((hcryp->Instance->SR & CRYP_FLAG_OFNE) != 0x0U) 06679 { 06680 for (index = 0U; index < 4U; index++) 06681 { 06682 intermediate_data[index] = hcryp->Instance->DOUT; 06683 } 06684 } 06685 } 06686 } /* End of GCM encryption */ 06687 else 06688 { 06689 /* Workaround 2, case CCM decryption, in order to properly compute 06690 authentication tags while doing a CCM decryption with the last block 06691 of payload size inferior to 128 bits*/ 06692 06693 if ((hcryp->Instance->CR & CRYP_CR_ALGODIR) == CRYP_OPERATINGMODE_DECRYPT) 06694 { 06695 iv1temp = hcryp->Instance->CSGCMCCM7R; 06696 06697 /* Disable CRYP to start the final phase */ 06698 __HAL_CRYP_DISABLE(hcryp); 06699 06700 temp[0] = hcryp->Instance->CSGCMCCM0R; 06701 temp[1] = hcryp->Instance->CSGCMCCM1R; 06702 temp[2] = hcryp->Instance->CSGCMCCM2R; 06703 temp[3] = hcryp->Instance->CSGCMCCM3R; 06704 06705 hcryp->Instance->IV1RR = iv1temp; 06706 06707 /* Configured CHMOD CTR */ 06708 MODIFY_REG(hcryp->Instance->CR, CRYP_CR_ALGOMODE, CRYP_AES_CTR); 06709 06710 /* Enable CRYP to start the final phase */ 06711 __HAL_CRYP_ENABLE(hcryp); 06712 } 06713 /* Last block optionally pad the data with zeros*/ 06714 for (index = 0; index < lastwordsize; index ++) 06715 { 06716 /* Write the last Input block in the IN FIFO */ 06717 hcryp->Instance->DIN = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount); 06718 hcryp->CrypInCount++; 06719 } 06720 while (index < 4U) 06721 { 06722 /* Pad the data with zeros to have a complete block */ 06723 hcryp->Instance->DIN = 0U; 06724 index++; 06725 } 06726 /* Wait for OFNE flag to be raised */ 06727 if (CRYP_WaitOnOFNEFlag(hcryp, Timeout) != HAL_OK) 06728 { 06729 /* Disable the CRYP peripheral clock */ 06730 __HAL_CRYP_DISABLE(hcryp); 06731 06732 /* Change state */ 06733 hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT; 06734 hcryp->State = HAL_CRYP_STATE_READY; 06735 06736 /* Process Unlocked */ 06737 __HAL_UNLOCK(hcryp); 06738 #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1U) 06739 /*Call registered error callback*/ 06740 hcryp->ErrorCallback(hcryp); 06741 #else 06742 /*Call legacy weak error callback*/ 06743 HAL_CRYP_ErrorCallback(hcryp); 06744 #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */ 06745 } 06746 06747 if ((hcryp->Instance->SR & CRYP_FLAG_OFNE) != 0x0U) 06748 { 06749 for (index = 0U; index < 4U; index++) 06750 { 06751 /* Read the Output block from the Output FIFO */ 06752 intermediate_data[index] = hcryp->Instance->DOUT; 06753 06754 /*intermediate data buffer to be used in for the workaround*/ 06755 *(uint32_t *)(hcryp->pCrypOutBuffPtr + (hcryp->CrypOutCount)) = intermediate_data[index]; 06756 hcryp->CrypOutCount++; 06757 } 06758 } 06759 06760 if ((hcryp->Instance->CR & CRYP_CR_ALGODIR) == CRYP_OPERATINGMODE_DECRYPT) 06761 { 06762 temp2[0] = hcryp->Instance->CSGCMCCM0R; 06763 temp2[1] = hcryp->Instance->CSGCMCCM1R; 06764 temp2[2] = hcryp->Instance->CSGCMCCM2R; 06765 temp2[3] = hcryp->Instance->CSGCMCCM3R; 06766 06767 /* configured CHMOD CCM */ 06768 MODIFY_REG(hcryp->Instance->CR, CRYP_CR_ALGOMODE, CRYP_AES_CCM); 06769 06770 /* configured Header phase */ 06771 MODIFY_REG(hcryp->Instance->CR, CRYP_CR_GCM_CCMPH, CRYP_PHASE_HEADER); 06772 06773 /*set to zero the bits corresponding to the padded bits*/ 06774 for (index = lastwordsize; index < 4U; index ++) 06775 { 06776 intermediate_data[index] = 0U; 06777 } 06778 if ((npblb % 4U) == 1U) 06779 { 06780 intermediate_data[lastwordsize - 1U] &= 0xFFFFFF00U; 06781 } 06782 if ((npblb % 4U) == 2U) 06783 { 06784 intermediate_data[lastwordsize - 1U] &= 0xFFFF0000U; 06785 } 06786 if ((npblb % 4U) == 3U) 06787 { 06788 intermediate_data[lastwordsize - 1U] &= 0xFF000000U; 06789 } 06790 for (index = 0U; index < 4U ; index ++) 06791 { 06792 intermediate_data[index] ^= temp[index]; 06793 intermediate_data[index] ^= temp2[index]; 06794 } 06795 for (index = 0U; index < 4U; index ++) 06796 { 06797 /* Write the last Input block in the IN FIFO */ 06798 hcryp->Instance->DIN = intermediate_data[index] ; 06799 } 06800 06801 /* Wait for BUSY flag to be raised */ 06802 if (CRYP_WaitOnBUSYFlag(hcryp, Timeout) != HAL_OK) 06803 { 06804 /* Disable the CRYP peripheral clock */ 06805 __HAL_CRYP_DISABLE(hcryp); 06806 06807 /* Change state */ 06808 hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT; 06809 hcryp->State = HAL_CRYP_STATE_READY; 06810 06811 /* Process Unlocked */ 06812 __HAL_UNLOCK(hcryp); 06813 #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1U) 06814 /*Call registered error callback*/ 06815 hcryp->ErrorCallback(hcryp); 06816 #else 06817 /*Call legacy weak error callback*/ 06818 HAL_CRYP_ErrorCallback(hcryp); 06819 #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */ 06820 } 06821 } 06822 } /* End of CCM WKA*/ 06823 06824 /* Process Unlocked */ 06825 __HAL_UNLOCK(hcryp); 06826 06827 #else /* AES */ 06828 06829 /*Workaround 2: case GCM encryption, during payload phase and before inserting 06830 the last block of paylaod, which size is inferior to 128 bits */ 06831 06832 if ((hcryp->Instance->CR & AES_CR_MODE) == CRYP_OPERATINGMODE_ENCRYPT) 06833 { 06834 /* configured CHMOD CTR */ 06835 MODIFY_REG(hcryp->Instance->CR, AES_CR_CHMOD, CRYP_AES_CTR); 06836 } 06837 /* last block optionally pad the data with zeros*/ 06838 for (index = 0U; index < lastwordsize; index ++) 06839 { 06840 /* Write the last Input block in the IN FIFO */ 06841 hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount); 06842 hcryp->CrypInCount++; 06843 } 06844 while (index < 4U) 06845 { 06846 /* pad the data with zeros to have a complete block */ 06847 hcryp->Instance->DINR = 0U; 06848 index++; 06849 } 06850 /* Wait for CCF flag to be raised */ 06851 if (CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK) 06852 { 06853 hcryp->State = HAL_CRYP_STATE_READY; 06854 __HAL_UNLOCK(hcryp); 06855 #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1U) 06856 /*Call registered error callback*/ 06857 hcryp->ErrorCallback(hcryp); 06858 #else 06859 /*Call legacy weak error callback*/ 06860 HAL_CRYP_ErrorCallback(hcryp); 06861 #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */ 06862 } 06863 06864 /* Clear CCF Flag */ 06865 __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR); 06866 06867 for (index = 0U; index < 4U; index++) 06868 { 06869 /* Read the Output block from the Output FIFO */ 06870 intermediate_data[index] = hcryp->Instance->DOUTR; 06871 06872 /*intermediate data buffer to be used in the workaround*/ 06873 *(uint32_t *)(hcryp->pCrypOutBuffPtr + (hcryp->CrypOutCount)) = intermediate_data[index]; 06874 hcryp->CrypOutCount++; 06875 } 06876 06877 if ((hcryp->Instance->CR & AES_CR_MODE) == CRYP_OPERATINGMODE_ENCRYPT) 06878 { 06879 /* configured CHMOD GCM */ 06880 MODIFY_REG(hcryp->Instance->CR, AES_CR_CHMOD, CRYP_AES_GCM_GMAC); 06881 06882 /* Select final phase */ 06883 MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_PHASE_FINAL); 06884 06885 if ((hcryp->Instance->CR & AES_CR_DATATYPE) == CRYP_DATATYPE_32B) 06886 { 06887 if ((npblb % 4U) == 1U) 06888 { 06889 intermediate_data[lastwordsize - 1U] &= 0xFFFFFF00U; 06890 } 06891 if ((npblb % 4U) == 2U) 06892 { 06893 intermediate_data[lastwordsize - 1U] &= 0xFFFF0000U; 06894 } 06895 if ((npblb % 4U) == 3U) 06896 { 06897 intermediate_data[lastwordsize - 1U] &= 0xFF000000U; 06898 } 06899 } 06900 else if ((hcryp->Instance->CR & AES_CR_DATATYPE) == CRYP_DATATYPE_8B) 06901 { 06902 if ((npblb % 4U) == 1U) 06903 { 06904 intermediate_data[lastwordsize - 1U] &= __REV(0xFFFFFF00U); 06905 } 06906 if ((npblb % 4U) == 2U) 06907 { 06908 intermediate_data[lastwordsize - 1U] &= __REV(0xFFFF0000U); 06909 } 06910 if ((npblb % 4U) == 3U) 06911 { 06912 intermediate_data[lastwordsize - 1U] &= __REV(0xFF000000U); 06913 } 06914 } 06915 else if ((hcryp->Instance->CR & AES_CR_DATATYPE) == CRYP_DATATYPE_16B) 06916 { 06917 if ((npblb % 4U) == 1U) 06918 { 06919 intermediate_data[lastwordsize - 1U] &= __ROR((0xFFFFFF00U), 16); 06920 } 06921 if ((npblb % 4U) == 2U) 06922 { 06923 intermediate_data[lastwordsize - 1U] &= __ROR((0xFFFF0000U), 16); 06924 } 06925 if ((npblb % 4U) == 3U) 06926 { 06927 intermediate_data[lastwordsize - 1U] &= __ROR((0xFF000000U), 16); 06928 } 06929 } 06930 else /*CRYP_DATATYPE_1B*/ 06931 { 06932 if ((npblb % 4U) == 1U) 06933 { 06934 intermediate_data[lastwordsize - 1U] &= __RBIT(0xFFFFFF00U); 06935 } 06936 if ((npblb % 4U) == 2U) 06937 { 06938 intermediate_data[lastwordsize - 1U] &= __RBIT(0xFFFF0000U); 06939 } 06940 if ((npblb % 4U) == 3U) 06941 { 06942 intermediate_data[lastwordsize - 1U] &= __RBIT(0xFF000000U); 06943 } 06944 } 06945 06946 /*Write the intermediate_data in the IN FIFO */ 06947 for (index = 0U; index < lastwordsize; index ++) 06948 { 06949 hcryp->Instance->DINR = intermediate_data[index]; 06950 } 06951 while (index < 4U) 06952 { 06953 /* pad the data with zeros to have a complete block */ 06954 hcryp->Instance->DINR = 0U; 06955 index++; 06956 } 06957 /* Wait for CCF flag to be raised */ 06958 if (CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK) 06959 { 06960 /* Disable the CRYP peripheral clock */ 06961 __HAL_CRYP_DISABLE(hcryp); 06962 06963 /* Change state */ 06964 hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT; 06965 hcryp->State = HAL_CRYP_STATE_READY; 06966 06967 /* Process Unlocked */ 06968 __HAL_UNLOCK(hcryp); 06969 #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1U) 06970 /*Call registered error callback*/ 06971 hcryp->ErrorCallback(hcryp); 06972 #else 06973 /*Call legacy weak error callback*/ 06974 HAL_CRYP_ErrorCallback(hcryp); 06975 #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */ 06976 } 06977 /* Clear CCF Flag */ 06978 __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR); 06979 06980 for (index = 0U; index < 4U; index++) 06981 { 06982 intermediate_data[index] = hcryp->Instance->DOUTR; 06983 } 06984 }/*End of Workaround 2*/ 06985 #endif /* End AES or CRYP */ 06986 } 06987 #endif /* AES or GCM CCM defined*/ 06988 #if defined (CRYP) 06989 #if defined (CRYP_CR_ALGOMODE_AES_GCM) 06990 /** 06991 * @brief Handle CRYP hardware block Timeout when waiting for IFEM flag to be raised. 06992 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains 06993 * the configuration information for CRYP module. 06994 * @param Timeout: Timeout duration. 06995 * @retval HAL status 06996 */ 06997 static HAL_StatusTypeDef CRYP_WaitOnIFEMFlag(CRYP_HandleTypeDef *hcryp, uint32_t Timeout) 06998 { 06999 uint32_t tickstart; 07000 07001 /* Get timeout */ 07002 tickstart = HAL_GetTick(); 07003 07004 while (HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_IFEM)) 07005 { 07006 /* Check for the Timeout */ 07007 if (Timeout != HAL_MAX_DELAY) 07008 { 07009 if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U)) 07010 { 07011 return HAL_ERROR; 07012 } 07013 } 07014 } 07015 return HAL_OK; 07016 } 07017 #endif /* GCM CCM defined*/ 07018 /** 07019 * @brief Handle CRYP hardware block Timeout when waiting for BUSY flag to be raised. 07020 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains 07021 * the configuration information for CRYP module. 07022 * @param Timeout: Timeout duration. 07023 * @retval HAL status 07024 */ 07025 static HAL_StatusTypeDef CRYP_WaitOnBUSYFlag(CRYP_HandleTypeDef *hcryp, uint32_t Timeout) 07026 { 07027 uint32_t tickstart; 07028 07029 /* Get timeout */ 07030 tickstart = HAL_GetTick(); 07031 07032 while (HAL_IS_BIT_SET(hcryp->Instance->SR, CRYP_FLAG_BUSY)) 07033 { 07034 /* Check for the Timeout */ 07035 if (Timeout != HAL_MAX_DELAY) 07036 { 07037 if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U)) 07038 { 07039 return HAL_ERROR; 07040 } 07041 } 07042 } 07043 return HAL_OK; 07044 } 07045 07046 07047 /** 07048 * @brief Handle CRYP hardware block Timeout when waiting for OFNE flag to be raised. 07049 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains 07050 * the configuration information for CRYP module. 07051 * @param Timeout: Timeout duration. 07052 * @retval HAL status 07053 */ 07054 static HAL_StatusTypeDef CRYP_WaitOnOFNEFlag(CRYP_HandleTypeDef *hcryp, uint32_t Timeout) 07055 { 07056 uint32_t tickstart; 07057 07058 /* Get timeout */ 07059 tickstart = HAL_GetTick(); 07060 07061 while (HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_OFNE)) 07062 { 07063 /* Check for the Timeout */ 07064 if (Timeout != HAL_MAX_DELAY) 07065 { 07066 if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U)) 07067 { 07068 return HAL_ERROR; 07069 } 07070 } 07071 } 07072 return HAL_OK; 07073 } 07074 07075 #else /* AES */ 07076 07077 /** 07078 * @brief Handle CRYP hardware block Timeout when waiting for CCF flag to be raised. 07079 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains 07080 * the configuration information for CRYP module. 07081 * @param Timeout: Timeout duration. 07082 * @retval HAL status 07083 */ 07084 static HAL_StatusTypeDef CRYP_WaitOnCCFlag(CRYP_HandleTypeDef *hcryp, uint32_t Timeout) 07085 { 07086 uint32_t tickstart; 07087 07088 /* Get timeout */ 07089 tickstart = HAL_GetTick(); 07090 07091 while (HAL_IS_BIT_CLR(hcryp->Instance->SR, AES_SR_CCF)) 07092 { 07093 /* Check for the Timeout */ 07094 if (Timeout != HAL_MAX_DELAY) 07095 { 07096 if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U)) 07097 { 07098 return HAL_ERROR; 07099 } 07100 } 07101 } 07102 return HAL_OK; 07103 } 07104 07105 #endif /* End AES or CRYP */ 07106 07107 07108 /** 07109 * @} 07110 */ 07111 07112 07113 07114 /** 07115 * @} 07116 */ 07117 07118 /** 07119 * @} 07120 */ 07121 07122 #endif /* HAL_CRYP_MODULE_ENABLED */ 07123 07124 07125 /** 07126 * @} 07127 */ 07128 #endif /* TinyAES or CRYP*/ 07129 /** 07130 * @} 07131 */ 07132 07133 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/