1 /** 2 ****************************************************************************** 3 * @file stm32f7xx_hal_hash.h 4 * @author MCD Application Team 5 * @version V1.0.1 6 * @date 25-June-2015 7 * @brief Header file of HASH HAL module. 8 ****************************************************************************** 9 * @attention 10 * 11 * <h2><center>© COPYRIGHT(c) 2015 STMicroelectronics</center></h2> 12 * 13 * Redistribution and use in source and binary forms, with or without modification, 14 * are permitted provided that the following conditions are met: 15 * 1. Redistributions of source code must retain the above copyright notice, 16 * this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright notice, 18 * this list of conditions and the following disclaimer in the documentation 19 * and/or other materials provided with the distribution. 20 * 3. Neither the name of STMicroelectronics nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 27 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 30 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 31 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 * 35 ****************************************************************************** 36 */ 37 38 /* Define to prevent recursive inclusion -------------------------------------*/ 39 #ifndef __STM32F7xx_HAL_HASH_H 40 #define __STM32F7xx_HAL_HASH_H 41 42 #ifdef __cplusplus 43 extern "C" { 44 #endif 45 46 #if defined(STM32F756xx) 47 /* Includes ------------------------------------------------------------------*/ 48 #include "stm32f7xx_hal_def.h" 49 50 /** @addtogroup STM32F7xx_HAL_Driver 51 * @{ 52 */ 53 54 /** @addtogroup HASH 55 * @brief HASH HAL module driver 56 * @{ 57 */ 58 59 /* Exported types ------------------------------------------------------------*/ 60 /** @defgroup HASH_Exported_Types HASH Exported Types 61 * @{ 62 */ 63 64 /** @defgroup HASH_Exported_Types_Group1 HASH Configuration Structure definition 65 * @{ 66 */ 67 68 typedef struct { 69 uint32_t DataType; /*!< 32-bit data, 16-bit data, 8-bit data or 1-bit string. 70 This parameter can be a value of @ref HASH_Data_Type */ 71 72 uint32_t KeySize; /*!< The key size is used only in HMAC operation */ 73 74 uint8_t* pKey; /*!< The key is used only in HMAC operation */ 75 } HASH_InitTypeDef; 76 77 /** 78 * @} 79 */ 80 81 /** @defgroup HASH_Exported_Types_Group2 HASH State structures definition 82 * @{ 83 */ 84 85 typedef enum { 86 HAL_HASH_STATE_RESET = 0x00, /*!< HASH not yet initialized or disabled */ 87 HAL_HASH_STATE_READY = 0x01, /*!< HASH initialized and ready for use */ 88 HAL_HASH_STATE_BUSY = 0x02, /*!< HASH internal process is ongoing */ 89 HAL_HASH_STATE_TIMEOUT = 0x03, /*!< HASH timeout state */ 90 HAL_HASH_STATE_ERROR = 0x04 /*!< HASH error state */ 91 } HAL_HASH_STATETypeDef; 92 93 /** 94 * @} 95 */ 96 97 /** @defgroup HASH_Exported_Types_Group3 HASH phase structures definition 98 * @{ 99 */ 100 101 typedef enum { 102 HAL_HASH_PHASE_READY = 0x01, /*!< HASH peripheral is ready for initialization */ 103 HAL_HASH_PHASE_PROCESS = 0x02, /*!< HASH peripheral is in processing phase */ 104 } HAL_HASHPhaseTypeDef; 105 106 /** 107 * @} 108 */ 109 110 /** @defgroup HASH_Exported_Types_Group4 HASH Handle structures definition 111 * @{ 112 */ 113 114 typedef struct { 115 HASH_InitTypeDef Init; /*!< HASH required parameters */ 116 117 uint8_t *pHashInBuffPtr; /*!< Pointer to input buffer */ 118 119 uint8_t *pHashOutBuffPtr; /*!< Pointer to input buffer */ 120 121 __IO uint32_t HashBuffSize; /*!< Size of buffer to be processed */ 122 123 __IO uint32_t HashInCount; /*!< Counter of inputed data */ 124 125 __IO uint32_t HashITCounter; /*!< Counter of issued interrupts */ 126 127 HAL_StatusTypeDef Status; /*!< HASH peripheral status */ 128 129 HAL_HASHPhaseTypeDef Phase; /*!< HASH peripheral phase */ 130 131 DMA_HandleTypeDef *hdmain; /*!< HASH In DMA handle parameters */ 132 133 HAL_LockTypeDef Lock; /*!< HASH locking object */ 134 135 __IO HAL_HASH_STATETypeDef State; /*!< HASH peripheral state */ 136 } HASH_HandleTypeDef; 137 138 /** 139 * @} 140 */ 141 142 143 /** 144 * @} 145 */ 146 147 /* Exported constants --------------------------------------------------------*/ 148 /** @defgroup HASH_Exported_Constants HASH Exported Constants 149 * @{ 150 */ 151 152 /** @defgroup HASH_Exported_Constants_Group1 HASH Algorithm Selection 153 * @{ 154 */ 155 #define HASH_ALGOSELECTION_SHA1 ((uint32_t)0x0000) /*!< HASH function is SHA1 */ 156 #define HASH_ALGOSELECTION_SHA224 HASH_CR_ALGO_1 /*!< HASH function is SHA224 */ 157 #define HASH_ALGOSELECTION_SHA256 HASH_CR_ALGO /*!< HASH function is SHA256 */ 158 #define HASH_ALGOSELECTION_MD5 HASH_CR_ALGO_0 /*!< HASH function is MD5 */ 159 /** 160 * @} 161 */ 162 163 /** @defgroup HASH_Exported_Constants_Group2 HASH Algorithm Mode 164 * @{ 165 */ 166 #define HASH_ALGOMODE_HASH ((uint32_t)0x00000000) /*!< Algorithm is HASH */ 167 #define HASH_ALGOMODE_HMAC HASH_CR_MODE /*!< Algorithm is HMAC */ 168 /** 169 * @} 170 */ 171 172 /** @defgroup HASH_Data_Type HASH Data Type 173 * @{ 174 */ 175 #define HASH_DATATYPE_32B ((uint32_t)0x0000) /*!< 32-bit data. No swapping */ 176 #define HASH_DATATYPE_16B HASH_CR_DATATYPE_0 /*!< 16-bit data. Each half word is swapped */ 177 #define HASH_DATATYPE_8B HASH_CR_DATATYPE_1 /*!< 8-bit data. All bytes are swapped */ 178 #define HASH_DATATYPE_1B HASH_CR_DATATYPE /*!< 1-bit data. In the word all bits are swapped */ 179 /** 180 * @} 181 */ 182 183 /** @defgroup HASH_Exported_Constants_Group4 HASH HMAC Long key 184 * @brief HASH HMAC Long key used only for HMAC mode 185 * @{ 186 */ 187 #define HASH_HMAC_KEYTYPE_SHORTKEY ((uint32_t)0x00000000) /*!< HMAC Key is <= 64 bytes */ 188 #define HASH_HMAC_KEYTYPE_LONGKEY HASH_CR_LKEY /*!< HMAC Key is > 64 bytes */ 189 /** 190 * @} 191 */ 192 193 /** @defgroup HASH_Exported_Constants_Group5 HASH Flags definition 194 * @{ 195 */ 196 #define HASH_FLAG_DINIS HASH_SR_DINIS /*!< 16 locations are free in the DIN : A new block can be entered into the input buffer */ 197 #define HASH_FLAG_DCIS HASH_SR_DCIS /*!< Digest calculation complete */ 198 #define HASH_FLAG_DMAS HASH_SR_DMAS /*!< DMA interface is enabled (DMAE=1) or a transfer is ongoing */ 199 #define HASH_FLAG_BUSY HASH_SR_BUSY /*!< The hash core is Busy : processing a block of data */ 200 #define HASH_FLAG_DINNE HASH_CR_DINNE /*!< DIN not empty : The input buffer contains at least one word of data */ 201 /** 202 * @} 203 */ 204 205 /** @defgroup HASH_Exported_Constants_Group6 HASH Interrupts definition 206 * @{ 207 */ 208 #define HASH_IT_DINI HASH_IMR_DINIE /*!< A new block can be entered into the input buffer (DIN) */ 209 #define HASH_IT_DCI HASH_IMR_DCIE /*!< Digest calculation complete */ 210 /** 211 * @} 212 */ 213 214 /** 215 * @} 216 */ 217 218 /* Exported macro ------------------------------------------------------------*/ 219 /** @defgroup HASH_Exported_Macros HASH Exported Macros 220 * @{ 221 */ 222 223 /** @brief Reset HASH handle state 224 * @param __HANDLE__: specifies the HASH handle. 225 * @retval None 226 */ 227 #define __HAL_HASH_RESET_HANDLE_STATE(__HANDLE__) ((__HANDLE__)->State = HAL_HASH_STATE_RESET) 228 229 /** @brief Check whether the specified HASH flag is set or not. 230 * @param __FLAG__: specifies the flag to check. 231 * This parameter can be one of the following values: 232 * @arg HASH_FLAG_DINIS: A new block can be entered into the input buffer. 233 * @arg HASH_FLAG_DCIS: Digest calculation complete 234 * @arg HASH_FLAG_DMAS: DMA interface is enabled (DMAE=1) or a transfer is ongoing 235 * @arg HASH_FLAG_BUSY: The hash core is Busy : processing a block of data 236 * @arg HASH_FLAG_DINNE: DIN not empty : The input buffer contains at least one word of data 237 * @retval The new state of __FLAG__ (TRUE or FALSE). 238 */ 239 #define __HAL_HASH_GET_FLAG(__FLAG__) (((__FLAG__) > 8U) ? ((HASH->CR & (__FLAG__)) == (__FLAG__)) :\ 240 ((HASH->SR & (__FLAG__)) == (__FLAG__))) 241 242 /** 243 * @brief Enable the multiple DMA mode. 244 * This feature is available only in STM32F429x and STM32F439x devices. 245 * @retval None 246 */ 247 #define __HAL_HASH_SET_MDMAT() HASH->CR |= HASH_CR_MDMAT 248 249 /** 250 * @brief Disable the multiple DMA mode. 251 * @retval None 252 */ 253 #define __HAL_HASH_RESET_MDMAT() HASH->CR &= (uint32_t)(~HASH_CR_MDMAT) 254 255 /** 256 * @brief Start the digest computation 257 * @retval None 258 */ 259 #define __HAL_HASH_START_DIGEST() HASH->STR |= HASH_STR_DCAL 260 261 /** 262 * @brief Set the number of valid bits in last word written in Data register 263 * @param SIZE: size in byte of last data written in Data register. 264 * @retval None 265 */ 266 #define __HAL_HASH_SET_NBVALIDBITS(SIZE) do{HASH->STR &= ~(HASH_STR_NBW);\ 267 HASH->STR |= 8 * ((SIZE) % 4);\ 268 }while(0) 269 270 /** 271 * @} 272 */ 273 274 /* Include HASH HAL Extension module */ 275 #include "stm32f7xx_hal_hash_ex.h" 276 /* Exported functions --------------------------------------------------------*/ 277 278 /** @defgroup HASH_Exported_Functions HASH Exported Functions 279 * @{ 280 */ 281 282 /** @addtogroup HASH_Exported_Functions_Group1 283 * @{ 284 */ 285 HAL_StatusTypeDef HAL_HASH_Init(HASH_HandleTypeDef *hhash); 286 HAL_StatusTypeDef HAL_HASH_DeInit(HASH_HandleTypeDef *hhash); 287 /** 288 * @} 289 */ 290 291 /** @addtogroup HASH_Exported_Functions_Group2 292 * @{ 293 */ 294 HAL_StatusTypeDef HAL_HASH_SHA1_Start(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size, uint8_t* pOutBuffer, uint32_t Timeout); 295 HAL_StatusTypeDef HAL_HASH_MD5_Start(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size, uint8_t* pOutBuffer, uint32_t Timeout); 296 HAL_StatusTypeDef HAL_HASH_MD5_Accumulate(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size); 297 HAL_StatusTypeDef HAL_HASH_SHA1_Accumulate(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size); 298 /** 299 * @} 300 */ 301 302 /** @addtogroup HASH_Exported_Functions_Group3 303 * @{ 304 */ 305 HAL_StatusTypeDef HAL_HMAC_SHA1_Start(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size, uint8_t* pOutBuffer, uint32_t Timeout); 306 HAL_StatusTypeDef HAL_HMAC_MD5_Start(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size, uint8_t* pOutBuffer, uint32_t Timeout); 307 /** 308 * @} 309 */ 310 311 /** @addtogroup HASH_Exported_Functions_Group4 312 * @{ 313 */ 314 HAL_StatusTypeDef HAL_HASH_SHA1_Start_IT(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size, uint8_t* pOutBuffer); 315 HAL_StatusTypeDef HAL_HASH_MD5_Start_IT(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size, uint8_t* pOutBuffer); 316 /** 317 * @} 318 */ 319 320 /** @addtogroup HASH_Exported_Functions_Group5 321 * @{ 322 */ 323 HAL_StatusTypeDef HAL_HASH_SHA1_Start_DMA(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size); 324 HAL_StatusTypeDef HAL_HASH_SHA1_Finish(HASH_HandleTypeDef *hhash, uint8_t* pOutBuffer, uint32_t Timeout); 325 HAL_StatusTypeDef HAL_HASH_MD5_Start_DMA(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size); 326 HAL_StatusTypeDef HAL_HASH_MD5_Finish(HASH_HandleTypeDef *hhash, uint8_t* pOutBuffer, uint32_t Timeout); 327 /** 328 * @} 329 */ 330 331 /** @addtogroup HASH_Exported_Functions_Group6 332 * @{ 333 */ 334 HAL_StatusTypeDef HAL_HMAC_SHA1_Start_DMA(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size); 335 HAL_StatusTypeDef HAL_HMAC_MD5_Start_DMA(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size); 336 /** 337 * @} 338 */ 339 340 /** @addtogroup HASH_Exported_Functions_Group7 341 * @{ 342 */ 343 void HAL_HASH_IRQHandler(HASH_HandleTypeDef *hhash); 344 /** 345 * @} 346 */ 347 348 /** @addtogroup HASH_Exported_Functions_Group8 349 * @{ 350 */ 351 HAL_HASH_STATETypeDef HAL_HASH_GetState(HASH_HandleTypeDef *hhash); 352 void HAL_HASH_MspInit(HASH_HandleTypeDef *hhash); 353 void HAL_HASH_MspDeInit(HASH_HandleTypeDef *hhash); 354 void HAL_HASH_InCpltCallback(HASH_HandleTypeDef *hhash); 355 void HAL_HASH_DgstCpltCallback(HASH_HandleTypeDef *hhash); 356 void HAL_HASH_ErrorCallback(HASH_HandleTypeDef *hhash); 357 /** 358 * @} 359 */ 360 361 /** 362 * @} 363 */ 364 365 /* Private types -------------------------------------------------------------*/ 366 /** @defgroup HASH_Private_Types HASH Private Types 367 * @{ 368 */ 369 370 /** 371 * @} 372 */ 373 374 /* Private variables ---------------------------------------------------------*/ 375 /** @defgroup HASH_Private_Variables HASH Private Variables 376 * @{ 377 */ 378 379 /** 380 * @} 381 */ 382 383 /* Private constants ---------------------------------------------------------*/ 384 /** @defgroup HASH_Private_Constants HASH Private Constants 385 * @{ 386 */ 387 388 /** 389 * @} 390 */ 391 392 /* Private macros ------------------------------------------------------------*/ 393 /** @defgroup HASH_Private_Macros HASH Private Macros 394 * @{ 395 */ 396 #define IS_HASH_ALGOSELECTION(__ALGOSELECTION__) (((__ALGOSELECTION__) == HASH_ALGOSELECTION_SHA1) || \ 397 ((__ALGOSELECTION__) == HASH_ALGOSELECTION_SHA224) || \ 398 ((__ALGOSELECTION__) == HASH_ALGOSELECTION_SHA256) || \ 399 ((__ALGOSELECTION__) == HASH_ALGOSELECTION_MD5)) 400 401 402 #define IS_HASH_ALGOMODE(__ALGOMODE__) (((__ALGOMODE__) == HASH_ALGOMODE_HASH) || \ 403 ((__ALGOMODE__) == HASH_ALGOMODE_HMAC)) 404 405 406 #define IS_HASH_DATATYPE(__DATATYPE__) (((__DATATYPE__) == HASH_DATATYPE_32B)|| \ 407 ((__DATATYPE__) == HASH_DATATYPE_16B)|| \ 408 ((__DATATYPE__) == HASH_DATATYPE_8B) || \ 409 ((__DATATYPE__) == HASH_DATATYPE_1B)) 410 411 412 #define IS_HASH_HMAC_KEYTYPE(__KEYTYPE__) (((__KEYTYPE__) == HASH_HMAC_KEYTYPE_SHORTKEY) || \ 413 ((__KEYTYPE__) == HASH_HMAC_KEYTYPE_LONGKEY)) 414 415 #define IS_HASH_SHA1_BUFFER_SIZE(__SIZE__) ((((__SIZE__)%4) != 0)? 0U: 1U) 416 417 418 /** 419 * @} 420 */ 421 422 /* Private functions ---------------------------------------------------------*/ 423 /** @defgroup HASH_Private_Functions HASH Private Functions 424 * @{ 425 */ 426 427 /** 428 * @} 429 */ 430 431 /** 432 * @} 433 */ 434 #endif /* STM32F756xx */ 435 /** 436 * @} 437 */ 438 439 #ifdef __cplusplus 440 } 441 #endif 442 443 444 #endif /* __STM32F7xx_HAL_HASH_H */ 445 446 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 447