1 /** 2 ****************************************************************************** 3 * @file stm32l0xx_hal_cryp.h 4 * @author MCD Application Team 5 * @brief Header file of CRYP HAL module. 6 ****************************************************************************** 7 * @attention 8 * 9 * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2> 10 * 11 * Redistribution and use in source and binary forms, with or without modification, 12 * are permitted provided that the following conditions are met: 13 * 1. Redistributions of source code must retain the above copyright notice, 14 * this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright notice, 16 * this list of conditions and the following disclaimer in the documentation 17 * and/or other materials provided with the distribution. 18 * 3. Neither the name of STMicroelectronics nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 28 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 * 33 ****************************************************************************** 34 */ 35 36 /* Define to prevent recursive inclusion -------------------------------------*/ 37 #ifndef __STM32L0xx_HAL_CRYP_H 38 #define __STM32L0xx_HAL_CRYP_H 39 40 #ifdef __cplusplus 41 extern "C" { 42 #endif 43 44 #if defined (STM32L021xx) || defined (STM32L041xx) || defined (STM32L061xx) || defined (STM32L062xx) || defined (STM32L063xx) || defined (STM32L081xx) || defined (STM32L082xx) || defined (STM32L083xx) 45 46 /* Includes ------------------------------------------------------------------*/ 47 #include "stm32l0xx_hal_def.h" 48 49 /** @addtogroup STM32L0xx_HAL_Driver 50 * @{ 51 */ 52 53 /** @defgroup CRYP CRYP 54 * @{ 55 */ 56 57 /* Exported types ------------------------------------------------------------*/ 58 59 /** @defgroup CRYP_Exported_Types CRYP Exported Types 60 * @{ 61 */ 62 63 /** 64 * @brief CRYP Configuration Structure definition 65 */ 66 typedef struct 67 { 68 uint32_t DataType; /*!< 32-bit data, 16-bit data, 8-bit data or 1-bit string. 69 This parameter can be a value of @ref CRYP_Data_Type */ 70 71 uint8_t* pKey; /*!< The key used for encryption/decryption */ 72 73 uint8_t* pInitVect; /*!< The initialization vector used also as initialization 74 counter in CTR mode */ 75 76 }CRYP_InitTypeDef; 77 78 /** 79 * @brief HAL CRYP State structures definition 80 */ 81 typedef enum 82 { 83 HAL_CRYP_STATE_RESET = 0x00U, /*!< CRYP not yet initialized or disabled */ 84 HAL_CRYP_STATE_READY = 0x01U, /*!< CRYP initialized and ready for use */ 85 HAL_CRYP_STATE_BUSY = 0x02U, /*!< CRYP internal processing is ongoing */ 86 HAL_CRYP_STATE_TIMEOUT = 0x03U, /*!< CRYP timeout state */ 87 HAL_CRYP_STATE_ERROR = 0x04U /*!< CRYP error state */ 88 89 }HAL_CRYP_STATETypeDef; 90 91 /** 92 * @brief HAL CRYP phase structures definition 93 */ 94 typedef enum 95 { 96 HAL_CRYP_PHASE_READY = 0x01U, /*!< CRYP peripheral is ready for initialization. */ 97 HAL_CRYP_PHASE_PROCESS = 0x02U, /*!< CRYP peripheral is in processing phase */ 98 }HAL_PhaseTypeDef; 99 100 /** 101 * @brief CRYP handle Structure definition 102 */ 103 typedef struct 104 { 105 AES_TypeDef *Instance; /*!< Register base address */ 106 107 CRYP_InitTypeDef Init; /*!< CRYP required parameters */ 108 109 uint8_t *pCrypInBuffPtr; /*!< Pointer to CRYP processing (encryption, decryption,...) buffer */ 110 111 uint8_t *pCrypOutBuffPtr; /*!< Pointer to CRYP processing (encryption, decryption,...) buffer */ 112 113 __IO uint16_t CrypInCount; /*!< Counter of inputed data */ 114 115 __IO uint16_t CrypOutCount; /*!< Counter of outputed data */ 116 117 HAL_StatusTypeDef Status; /*!< CRYP peripheral status */ 118 119 HAL_PhaseTypeDef Phase; /*!< CRYP peripheral phase */ 120 121 DMA_HandleTypeDef *hdmain; /*!< CRYP In DMA handle parameters */ 122 123 DMA_HandleTypeDef *hdmaout; /*!< CRYP Out DMA handle parameters */ 124 125 HAL_LockTypeDef Lock; /*!< CRYP locking object */ 126 127 __IO HAL_CRYP_STATETypeDef State; /*!< CRYP peripheral state */ 128 129 }CRYP_HandleTypeDef; 130 131 /** 132 * @} 133 */ 134 135 /* Exported constants --------------------------------------------------------*/ 136 137 /** @defgroup CRYP_Exported_Constants CRYP Exported Constants 138 * @{ 139 */ 140 141 /** @defgroup CRYP_Data_Type CRYP Data Type 142 * @{ 143 */ 144 #define CRYP_DATATYPE_32B ((uint32_t)0x00000000U) 145 #define CRYP_DATATYPE_16B AES_CR_DATATYPE_0 146 #define CRYP_DATATYPE_8B AES_CR_DATATYPE_1 147 #define CRYP_DATATYPE_1B AES_CR_DATATYPE 148 149 #define IS_CRYP_DATATYPE(DATATYPE) (((DATATYPE) == CRYP_DATATYPE_32B) || \ 150 ((DATATYPE) == CRYP_DATATYPE_16B) || \ 151 ((DATATYPE) == CRYP_DATATYPE_8B) || \ 152 ((DATATYPE) == CRYP_DATATYPE_1B)) 153 /** 154 * @} 155 */ 156 157 /** @defgroup CRYP_AlgoModeDirection CRYP Algo Mode Direction 158 * @{ 159 */ 160 #define CRYP_CR_ALGOMODE_DIRECTION (uint32_t)(AES_CR_MODE|AES_CR_CHMOD) 161 162 #define CRYP_CR_ALGOMODE_AES_ECB_ENCRYPT ((uint32_t)0x00000000U) 163 #define CRYP_CR_ALGOMODE_AES_ECB_KEYDERDECRYPT (AES_CR_MODE) 164 #define CRYP_CR_ALGOMODE_AES_CBC_ENCRYPT (AES_CR_CHMOD_0) 165 #define CRYP_CR_ALGOMODE_AES_CBC_KEYDERDECRYPT ((uint32_t)(AES_CR_CHMOD_0|AES_CR_MODE)) 166 #define CRYP_CR_ALGOMODE_AES_CTR_ENCRYPT (AES_CR_CHMOD_1) 167 #define CRYP_CR_ALGOMODE_AES_CTR_DECRYPT ((uint32_t)(AES_CR_CHMOD_1 | AES_CR_MODE_1)) 168 /** 169 * @} 170 */ 171 172 /** @defgroup CRYP_AES_Interrupts AES Interrupts 173 * @{ 174 */ 175 #define CRYP_IT_CC AES_CR_CCIE /*!< Computation Complete interrupt */ 176 #define CRYP_IT_ERR AES_CR_ERRIE /*!< Error interrupt */ 177 178 /** 179 * @} 180 */ 181 182 183 /** @defgroup CRYP_AES_Flags AES Flags 184 * @{ 185 */ 186 #define CRYP_FLAG_CCF AES_SR_CCF /*!< Computation Complete Flag */ 187 #define CRYP_FLAG_RDERR AES_SR_RDERR /*!< Read Error Flag */ 188 #define CRYP_FLAG_WRERR AES_SR_WRERR /*!< Write Error Flag */ 189 190 /** 191 * @} 192 */ 193 194 /** @defgroup CRYP_AES_Clear_Flags AES Clear Flags 195 * @{ 196 */ 197 #define CRYP_CLEARFLAG_CCF AES_CR_CCFC /*!< Computation Complete Flag Clear */ 198 #define CRYP_CLEARFLAG_RDERR AES_CR_ERRC /*!< Read Error Clear */ 199 #define CRYP_CLEARFLAG_WRERR AES_CR_ERRC /*!< Write Error Clear */ 200 201 /** 202 * @} 203 */ 204 205 /** 206 * @} 207 */ 208 209 /* Exported macro ------------------------------------------------------------*/ 210 211 /** @defgroup CRYP_Exported_Macros CRYP Exported Macros 212 * @{ 213 */ 214 215 /** @brief Reset CRYP handle state 216 * @param __HANDLE__: specifies the CRYP handle. 217 * @retval None 218 */ 219 #define __HAL_CRYP_RESET_HANDLE_STATE(__HANDLE__) ((__HANDLE__)->State = HAL_CRYP_STATE_RESET) 220 221 /** 222 * @brief Enable/Disable the CRYP peripheral. 223 * @param __HANDLE__: specifies the CRYP handle. 224 * @retval None 225 */ 226 #define __HAL_CRYP_ENABLE(__HANDLE__) SET_BIT((__HANDLE__)->Instance->CR, AES_CR_EN) 227 #define __HAL_CRYP_DISABLE(__HANDLE__) CLEAR_BIT((__HANDLE__)->Instance->CR, AES_CR_EN) 228 229 /** 230 * @brief Set the algorithm mode: AES-ECB, AES-CBC, AES-CTR, DES-ECB, DES-CBC,... 231 * @param __HANDLE__: specifies the CRYP handle. 232 * @param __MODE__: The algorithm mode. 233 * @retval None 234 */ 235 #define __HAL_CRYP_SET_MODE(__HANDLE__,__MODE__) SET_BIT((__HANDLE__)->Instance->CR, (__MODE__)) 236 237 238 /** @brief Check whether the specified CRYP flag is set or not. 239 * @param __HANDLE__: specifies the CRYP handle. 240 * @param __FLAG__: specifies the flag to check. 241 * This parameter can be one of the following values: 242 * @arg CRYP_FLAG_CCF : Computation Complete Flag 243 * @arg CRYP_FLAG_RDERR : Read Error Flag 244 * @arg CRYP_FLAG_WRERR : Write Error Flag 245 * @retval The new state of __FLAG__ (TRUE or FALSE). 246 */ 247 #define __HAL_CRYP_GET_FLAG(__HANDLE__,__FLAG__) (((__HANDLE__)->Instance->SR & (__FLAG__)) == (__FLAG__)) 248 249 /** @brief Clear the CRYP pending flag. 250 * @param __HANDLE__: specifies the CRYP handle. 251 * @param __FLAG__: specifies the flag to clear. 252 * This parameter can be one of the following values: 253 * @arg CRYP_CLEARFLAG_CCF : Computation Complete Clear Flag 254 * @arg CRYP_CLEARFLAG_RDERR : Read Error Clear 255 * @arg CRYP_CLEARFLAG_WRERR : Write Error Clear 256 * @retval None 257 */ 258 #define __HAL_CRYP_CLEAR_FLAG(__HANDLE__, __FLAG__) SET_BIT((__HANDLE__)->Instance->CR, (__FLAG__)) 259 260 /** 261 * @brief Enable the CRYP interrupt. 262 * @param __HANDLE__: specifies the CRYP handle. 263 * @param __INTERRUPT__: CRYP Interrupt. 264 * @retval None 265 */ 266 #define __HAL_CRYP_ENABLE_IT(__HANDLE__,__INTERRUPT__) SET_BIT((__HANDLE__)->Instance->CR, (__INTERRUPT__)) 267 268 /** 269 * @brief Disable the CRYP interrupt. 270 * @param __HANDLE__: specifies the CRYP handle. 271 * @param __INTERRUPT__: CRYP interrupt. 272 * @retval None 273 */ 274 #define __HAL_CRYP_DISABLE_IT(__HANDLE__,__INTERRUPT__) CLEAR_BIT((__HANDLE__)->Instance->CR, (__INTERRUPT__)) 275 276 /** @brief Checks if the specified CRYP interrupt source is enabled or disabled. 277 * @param __HANDLE__: specifies the CRYP handle. 278 * @param __INTERRUPT__: CRYP interrupt source to check 279 * This parameter can be one of the following values: 280 * @arg CRYP_IT_CC : Computation Complete interrupt 281 * @arg CRYP_IT_ERR : Error interrupt (used for RDERR and WRERR) 282 * @retval State of interruption (SET or RESET) 283 */ 284 #define __HAL_CRYP_GET_IT_SOURCE(__HANDLE__, __INTERRUPT__) \ 285 (( ((__HANDLE__)->Instance->CR & (__INTERRUPT__)) == (__INTERRUPT__) \ 286 )? SET : RESET \ 287 ) 288 289 /** @brief Clear the CRYP pending IT. 290 * @param __HANDLE__: specifies the CRYP handle. 291 * @param __IT__: specifies the IT to clear. 292 * This parameter can be one of the following values: 293 * @arg CRYP_CLEARFLAG_CCF : Computation Complete Clear Flag 294 * @arg CRYP_CLEARFLAG_RDERR : Read Error Clear 295 * @arg CRYP_CLEARFLAG_WRERR : Write Error Clear 296 * @retval None 297 */ 298 #define __HAL_CRYP_CLEAR_IT(__HANDLE__, __IT__) SET_BIT((__HANDLE__)->Instance->CR, (__IT__)) 299 300 /** 301 * @} 302 */ 303 304 /* Include CRYP HAL Extension module */ 305 #include "stm32l0xx_hal_cryp_ex.h" 306 307 /* Exported functions --------------------------------------------------------*/ 308 309 /** @defgroup CRYP_Exported_Functions CRYP Exported Functions 310 * @{ 311 */ 312 313 /** @defgroup CRYP_Exported_Functions_Group1 Initialization and de-initialization functions 314 * @{ 315 */ 316 317 /* Initialization/de-initialization functions *********************************/ 318 HAL_StatusTypeDef HAL_CRYP_Init(CRYP_HandleTypeDef *hcryp); 319 HAL_StatusTypeDef HAL_CRYP_DeInit(CRYP_HandleTypeDef *hcryp); 320 321 /* MSP functions *************************************************************/ 322 void HAL_CRYP_MspInit(CRYP_HandleTypeDef *hcryp); 323 void HAL_CRYP_MspDeInit(CRYP_HandleTypeDef *hcryp); 324 325 /** 326 * @} 327 */ 328 329 /** @defgroup CRYP_Exported_Functions_Group2 AES processing functions 330 * @{ 331 */ 332 333 /* AES encryption/decryption using polling ***********************************/ 334 HAL_StatusTypeDef HAL_CRYP_AESECB_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout); 335 HAL_StatusTypeDef HAL_CRYP_AESECB_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout); 336 HAL_StatusTypeDef HAL_CRYP_AESCBC_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout); 337 HAL_StatusTypeDef HAL_CRYP_AESCBC_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout); 338 HAL_StatusTypeDef HAL_CRYP_AESCTR_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout); 339 HAL_StatusTypeDef HAL_CRYP_AESCTR_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout); 340 341 /* AES encryption/decryption using interrupt *********************************/ 342 HAL_StatusTypeDef HAL_CRYP_AESECB_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData); 343 HAL_StatusTypeDef HAL_CRYP_AESCBC_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData); 344 HAL_StatusTypeDef HAL_CRYP_AESCTR_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData); 345 HAL_StatusTypeDef HAL_CRYP_AESECB_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData); 346 HAL_StatusTypeDef HAL_CRYP_AESCTR_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData); 347 HAL_StatusTypeDef HAL_CRYP_AESCBC_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData); 348 349 /* AES encryption/decryption using DMA ***************************************/ 350 HAL_StatusTypeDef HAL_CRYP_AESECB_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData); 351 HAL_StatusTypeDef HAL_CRYP_AESECB_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData); 352 HAL_StatusTypeDef HAL_CRYP_AESCBC_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData); 353 HAL_StatusTypeDef HAL_CRYP_AESCBC_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData); 354 HAL_StatusTypeDef HAL_CRYP_AESCTR_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData); 355 HAL_StatusTypeDef HAL_CRYP_AESCTR_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData); 356 357 /** 358 * @} 359 */ 360 361 /** @addtogroup CRYP_Exported_Functions_Group3 DMA callback functions 362 * @{ 363 */ 364 365 /* CallBack functions ********************************************************/ 366 void HAL_CRYP_InCpltCallback(CRYP_HandleTypeDef *hcryp); 367 void HAL_CRYP_OutCpltCallback(CRYP_HandleTypeDef *hcryp); 368 void HAL_CRYP_ErrorCallback(CRYP_HandleTypeDef *hcryp); 369 370 /** 371 * @} 372 */ 373 374 /** @defgroup CRYP_Exported_Functions_Group4 CRYP IRQ handler 375 * @{ 376 */ 377 378 /* Processing functions ********************************************************/ 379 void HAL_CRYP_IRQHandler(CRYP_HandleTypeDef *hcryp); 380 381 /** 382 * @} 383 */ 384 385 /** @defgroup CRYP_Exported_Functions_Group5 Peripheral State functions 386 * @{ 387 */ 388 389 /* Peripheral State functions **************************************************/ 390 HAL_CRYP_STATETypeDef HAL_CRYP_GetState(CRYP_HandleTypeDef *hcryp); 391 392 /** 393 * @} 394 */ 395 396 /** 397 * @} 398 */ 399 400 /* Define the private group ***********************************/ 401 /**************************************************************/ 402 /** @defgroup CRYP_Private CRYP Private 403 * @{ 404 */ 405 /** 406 * @} 407 */ 408 /**************************************************************/ 409 410 411 /** 412 * @} 413 */ 414 415 /** 416 * @} 417 */ 418 419 #endif /* STM32L021xx || STM32L041xx || STM32L061xx || STM32L062xx || STM32L063xx || STM32L081xx || STM32L082xx || STM32L083xx */ 420 #ifdef __cplusplus 421 } 422 #endif 423 424 #endif /* __STM32L0xx_HAL_CRYP_H */ 425 426 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 427 428