STM32F439xx HAL User Manual
stm32f4xx_hal_hash.h
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32f4xx_hal_hash.h
00004   * @author  MCD Application Team
00005   * @brief   Header file of HASH HAL module.
00006   ******************************************************************************
00007   * @attention
00008   *
00009   * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
00010   *
00011   * Redistribution and use in source and binary forms, with or without modification,
00012   * are permitted provided that the following conditions are met:
00013   *   1. Redistributions of source code must retain the above copyright notice,
00014   *      this list of conditions and the following disclaimer.
00015   *   2. Redistributions in binary form must reproduce the above copyright notice,
00016   *      this list of conditions and the following disclaimer in the documentation
00017   *      and/or other materials provided with the distribution.
00018   *   3. Neither the name of STMicroelectronics nor the names of its contributors
00019   *      may be used to endorse or promote products derived from this software
00020   *      without specific prior written permission.
00021   *
00022   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00023   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00024   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00025   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00026   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00027   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00028   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00030   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00031   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00032   *
00033   ******************************************************************************
00034   */
00035 
00036 /* Define to prevent recursive inclusion -------------------------------------*/
00037 #ifndef __STM32F4xx_HAL_HASH_H
00038 #define __STM32F4xx_HAL_HASH_H
00039 
00040 #ifdef __cplusplus
00041  extern "C" {
00042 #endif
00043 
00044 #if defined(STM32F415xx) || defined(STM32F417xx) || defined(STM32F437xx) || defined(STM32F439xx) || defined(STM32F479xx)
00045 
00046 /* Includes ------------------------------------------------------------------*/
00047 #include "stm32f4xx_hal_def.h"
00048 
00049 /** @addtogroup STM32F4xx_HAL_Driver
00050   * @{
00051   */
00052 
00053 /** @addtogroup HASH    
00054   * @brief HASH HAL module driver 
00055   *  @{
00056   */ 
00057 
00058 /* Exported types ------------------------------------------------------------*/
00059 /** @defgroup HASH_Exported_Types HASH Exported Types
00060   * @{
00061   */
00062 
00063 /** @defgroup HASH_Exported_Types_Group1 HASH Configuration Structure definition
00064   * @{
00065   */
00066 
00067 typedef struct
00068 {
00069   uint32_t DataType;  /*!< 32-bit data, 16-bit data, 8-bit data or 1-bit string.
00070                            This parameter can be a value of @ref HASH_Data_Type */
00071 
00072   uint32_t KeySize;   /*!< The key size is used only in HMAC operation          */
00073 
00074   uint8_t* pKey;      /*!< The key is used only in HMAC operation               */
00075 }HASH_InitTypeDef;
00076 
00077 /** 
00078   * @}
00079   */
00080 
00081 /** @defgroup HASH_Exported_Types_Group2 HASH State structures definition
00082   * @{
00083   */
00084 
00085 typedef enum
00086 {
00087   HAL_HASH_STATE_RESET     = 0x00U,  /*!< HASH not yet initialized or disabled */
00088   HAL_HASH_STATE_READY     = 0x01U,  /*!< HASH initialized and ready for use   */
00089   HAL_HASH_STATE_BUSY      = 0x02U,  /*!< HASH internal process is ongoing     */
00090   HAL_HASH_STATE_TIMEOUT   = 0x03U,  /*!< HASH timeout state                   */
00091   HAL_HASH_STATE_ERROR     = 0x04U   /*!< HASH error state                     */
00092 }HAL_HASH_StateTypeDef; 
00093 
00094 /** 
00095   * @}
00096   */
00097   
00098 /** @defgroup HASH_Exported_Types_Group3 HASH phase structures definition
00099   * @{
00100   */
00101   
00102 typedef enum
00103 {
00104   HAL_HASH_PHASE_READY     = 0x01U,  /*!< HASH peripheral is ready for initialization */
00105   HAL_HASH_PHASE_PROCESS   = 0x02U  /*!< HASH peripheral is in processing phase      */
00106 }HAL_HASH_PhaseTypeDef;
00107 
00108 /** 
00109   * @}
00110   */
00111  
00112 /** @defgroup HASH_Exported_Types_Group4 HASH Handle structures definition
00113   * @{
00114   */ 
00115   
00116 typedef struct
00117 {
00118       HASH_InitTypeDef           Init;              /*!< HASH required parameters       */
00119 
00120       uint8_t                    *pHashInBuffPtr;   /*!< Pointer to input buffer        */
00121 
00122       uint8_t                    *pHashOutBuffPtr;  /*!< Pointer to input buffer        */
00123 
00124      __IO uint32_t               HashBuffSize;      /*!< Size of buffer to be processed */
00125 
00126      __IO uint32_t               HashInCount;       /*!< Counter of inputed data        */
00127 
00128      __IO uint32_t               HashITCounter;     /*!< Counter of issued interrupts   */
00129 
00130       HAL_StatusTypeDef          Status;            /*!< HASH peripheral status         */
00131 
00132       HAL_HASH_PhaseTypeDef       Phase;             /*!< HASH peripheral phase          */
00133 
00134       DMA_HandleTypeDef          *hdmain;           /*!< HASH In DMA handle parameters  */
00135 
00136       HAL_LockTypeDef            Lock;              /*!< HASH locking object            */
00137 
00138      __IO HAL_HASH_StateTypeDef  State;             /*!< HASH peripheral state          */
00139 } HASH_HandleTypeDef;
00140 
00141 /** 
00142   * @}
00143   */
00144   
00145 
00146 /**
00147   * @}
00148   */ 
00149   
00150 /* Exported constants --------------------------------------------------------*/
00151 /** @defgroup HASH_Exported_Constants HASH Exported Constants
00152   * @{
00153   */
00154 
00155 /** @defgroup HASH_Exported_Constants_Group1 HASH Algorithm Selection
00156   * @{
00157   */
00158 #define HASH_ALGOSELECTION_SHA1      0x00000000U         /*!< HASH function is SHA1   */
00159 #define HASH_ALGOSELECTION_SHA224    HASH_CR_ALGO_1      /*!< HASH function is SHA224 */
00160 #define HASH_ALGOSELECTION_SHA256    HASH_CR_ALGO        /*!< HASH function is SHA256 */
00161 #define HASH_ALGOSELECTION_MD5       HASH_CR_ALGO_0      /*!< HASH function is MD5    */
00162 /**
00163   * @}
00164   */
00165 
00166 /** @defgroup HASH_Exported_Constants_Group2 HASH Algorithm Mode
00167   * @{
00168   */
00169 #define HASH_ALGOMODE_HASH         0x00000000U           /*!< Algorithm is HASH */ 
00170 #define HASH_ALGOMODE_HMAC         HASH_CR_MODE          /*!< Algorithm is HMAC */
00171 /**
00172   * @}
00173   */
00174 
00175 /** @defgroup HASH_Data_Type HASH Data Type
00176   * @{
00177   */
00178 #define HASH_DATATYPE_32B          0x00000000U           /*!< 32-bit data. No swapping                     */
00179 #define HASH_DATATYPE_16B          HASH_CR_DATATYPE_0    /*!< 16-bit data. Each half word is swapped       */
00180 #define HASH_DATATYPE_8B           HASH_CR_DATATYPE_1    /*!< 8-bit data. All bytes are swapped            */
00181 #define HASH_DATATYPE_1B           HASH_CR_DATATYPE      /*!< 1-bit data. In the word all bits are swapped */
00182 /**
00183   * @}
00184   */
00185 
00186 /** @defgroup HASH_Exported_Constants_Group4 HASH HMAC Long key 
00187   * @brief HASH HMAC Long key used only for HMAC mode
00188   * @{
00189   */
00190 #define HASH_HMAC_KEYTYPE_SHORTKEY      0x00000000U      /*!< HMAC Key is <= 64 bytes */
00191 #define HASH_HMAC_KEYTYPE_LONGKEY       HASH_CR_LKEY     /*!< HMAC Key is > 64 bytes  */
00192 /**
00193   * @}
00194   */
00195 
00196 /** @defgroup HASH_Exported_Constants_Group5 HASH Flags definition 
00197   * @{
00198   */
00199 #define HASH_FLAG_DINIS            HASH_SR_DINIS         /*!< 16 locations are free in the DIN : A new block can be entered into the input buffer */
00200 #define HASH_FLAG_DCIS             HASH_SR_DCIS          /*!< Digest calculation complete                                                         */
00201 #define HASH_FLAG_DMAS             HASH_SR_DMAS          /*!< DMA interface is enabled (DMAE=1) or a transfer is ongoing                          */
00202 #define HASH_FLAG_BUSY             HASH_SR_BUSY          /*!< The hash core is Busy : processing a block of data                                  */
00203 #define HASH_FLAG_DINNE            HASH_CR_DINNE         /*!< DIN not empty : The input buffer contains at least one word of data                 */
00204 /**
00205   * @}
00206   */
00207 
00208 /** @defgroup HASH_Exported_Constants_Group6 HASH Interrupts definition 
00209   * @{
00210   */
00211 #define HASH_IT_DINI               HASH_IMR_DINIE        /*!< A new block can be entered into the input buffer (DIN) */
00212 #define HASH_IT_DCI                HASH_IMR_DCIE         /*!< Digest calculation complete                            */
00213 /**
00214   * @}
00215   */
00216 
00217 /**
00218   * @}
00219   */
00220 
00221 /* Exported macro ------------------------------------------------------------*/
00222 /** @defgroup HASH_Exported_Macros HASH Exported Macros
00223   * @{
00224   */
00225   
00226 /** @brief Reset HASH handle state
00227   * @param  __HANDLE__ specifies the HASH handle.
00228   * @retval None
00229   */
00230 #define __HAL_HASH_RESET_HANDLE_STATE(__HANDLE__) ((__HANDLE__)->State = HAL_HASH_STATE_RESET)
00231 
00232 /** @brief  Check whether the specified HASH flag is set or not.
00233   * @param  __FLAG__ specifies the flag to check.
00234   *         This parameter can be one of the following values:
00235   *            @arg HASH_FLAG_DINIS: A new block can be entered into the input buffer. 
00236   *            @arg HASH_FLAG_DCIS: Digest calculation complete
00237   *            @arg HASH_FLAG_DMAS: DMA interface is enabled (DMAE=1) or a transfer is ongoing
00238   *            @arg HASH_FLAG_BUSY: The hash core is Busy : processing a block of data
00239   *            @arg HASH_FLAG_DINNE: DIN not empty : The input buffer contains at least one word of data
00240   * @retval The new state of __FLAG__ (TRUE or FALSE).
00241   */
00242 #define __HAL_HASH_GET_FLAG(__FLAG__) (((__FLAG__) > 8U) ? ((HASH->CR & (__FLAG__)) == (__FLAG__)) :\
00243                                                            ((HASH->SR & (__FLAG__)) == (__FLAG__)))
00244 
00245 /**
00246   * @brief  Enable the multiple DMA mode. 
00247   *         This feature is available only in STM32F429x and STM32F439x devices.
00248   * @retval None
00249   */
00250 #define __HAL_HASH_SET_MDMAT()          HASH->CR |= HASH_CR_MDMAT
00251 
00252 /**
00253   * @brief  Disable the multiple DMA mode.
00254   * @retval None
00255   */
00256 #define __HAL_HASH_RESET_MDMAT()        HASH->CR &= (uint32_t)(~HASH_CR_MDMAT)
00257 
00258 /**
00259   * @brief  Start the digest computation
00260   * @retval None
00261   */
00262 #define __HAL_HASH_START_DIGEST()       HASH->STR |= HASH_STR_DCAL
00263 
00264 /**
00265   * @brief Set the number of valid bits in last word written in Data register
00266   * @param  SIZE size in byte of last data written in Data register.
00267   * @retval None
00268 */
00269 #define __HAL_HASH_SET_NBVALIDBITS(SIZE) do{HASH->STR &= ~(HASH_STR_NBLW);\
00270                                             HASH->STR |= 8U * ((SIZE) % 4U);\
00271                                            }while(0)
00272 
00273 /**
00274   * @}
00275   */ 
00276 
00277 /* Include HASH HAL Extension module */
00278 #include "stm32f4xx_hal_hash_ex.h"
00279 /* Exported functions --------------------------------------------------------*/
00280 
00281 /** @defgroup HASH_Exported_Functions HASH Exported Functions
00282   * @{
00283   */
00284 
00285 /** @addtogroup HASH_Exported_Functions_Group1
00286   * @{
00287   */  
00288 HAL_StatusTypeDef HAL_HASH_Init(HASH_HandleTypeDef *hhash);
00289 HAL_StatusTypeDef HAL_HASH_DeInit(HASH_HandleTypeDef *hhash);
00290 /**
00291   * @}
00292   */ 
00293 
00294 /** @addtogroup HASH_Exported_Functions_Group2
00295   * @{
00296   */  
00297 HAL_StatusTypeDef HAL_HASH_SHA1_Start(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size, uint8_t* pOutBuffer, uint32_t Timeout);
00298 HAL_StatusTypeDef HAL_HASH_MD5_Start(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size, uint8_t* pOutBuffer, uint32_t Timeout);
00299 HAL_StatusTypeDef HAL_HASH_MD5_Accumulate(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size);
00300 HAL_StatusTypeDef HAL_HASH_SHA1_Accumulate(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size);
00301 /**
00302   * @}
00303   */ 
00304   
00305 /** @addtogroup HASH_Exported_Functions_Group3
00306   * @{
00307   */  
00308 HAL_StatusTypeDef HAL_HMAC_SHA1_Start(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size, uint8_t* pOutBuffer, uint32_t Timeout);
00309 HAL_StatusTypeDef HAL_HMAC_MD5_Start(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size, uint8_t* pOutBuffer, uint32_t Timeout);
00310 /**
00311   * @}
00312   */ 
00313 
00314 /** @addtogroup HASH_Exported_Functions_Group4
00315   * @{
00316   */  
00317 HAL_StatusTypeDef HAL_HASH_SHA1_Start_IT(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size, uint8_t* pOutBuffer);
00318 HAL_StatusTypeDef HAL_HASH_MD5_Start_IT(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size, uint8_t* pOutBuffer);
00319 /**
00320   * @}
00321   */ 
00322 
00323 /** @addtogroup HASH_Exported_Functions_Group5
00324   * @{
00325   */    
00326 HAL_StatusTypeDef HAL_HASH_SHA1_Start_DMA(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size);
00327 HAL_StatusTypeDef HAL_HASH_SHA1_Finish(HASH_HandleTypeDef *hhash, uint8_t* pOutBuffer, uint32_t Timeout);
00328 HAL_StatusTypeDef HAL_HASH_MD5_Start_DMA(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size);
00329 HAL_StatusTypeDef HAL_HASH_MD5_Finish(HASH_HandleTypeDef *hhash, uint8_t* pOutBuffer, uint32_t Timeout);
00330 /**
00331   * @}
00332   */ 
00333 
00334 /** @addtogroup HASH_Exported_Functions_Group6
00335   * @{
00336   */  
00337 HAL_StatusTypeDef HAL_HMAC_SHA1_Start_DMA(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size);
00338 HAL_StatusTypeDef HAL_HMAC_MD5_Start_DMA(HASH_HandleTypeDef *hhash, uint8_t *pInBuffer, uint32_t Size);
00339 /**
00340   * @}
00341   */ 
00342 
00343 /** @addtogroup HASH_Exported_Functions_Group7
00344   * @{
00345   */  
00346 void HAL_HASH_IRQHandler(HASH_HandleTypeDef *hhash);
00347 /**
00348   * @}
00349   */ 
00350 
00351 /** @addtogroup HASH_Exported_Functions_Group8
00352   * @{
00353   */
00354 HAL_HASH_StateTypeDef HAL_HASH_GetState(HASH_HandleTypeDef *hhash);
00355 void HAL_HASH_MspInit(HASH_HandleTypeDef *hhash);
00356 void HAL_HASH_MspDeInit(HASH_HandleTypeDef *hhash);
00357 void HAL_HASH_InCpltCallback(HASH_HandleTypeDef *hhash);
00358 void HAL_HASH_DgstCpltCallback(HASH_HandleTypeDef *hhash);
00359 void HAL_HASH_ErrorCallback(HASH_HandleTypeDef *hhash);
00360 /**
00361   * @}
00362   */ 
00363   
00364  /**
00365   * @}
00366   */ 
00367  
00368  /* Private types -------------------------------------------------------------*/
00369 /** @defgroup HASH_Private_Types HASH Private Types
00370   * @{
00371   */
00372 
00373 /**
00374   * @}
00375   */ 
00376 
00377 /* Private variables ---------------------------------------------------------*/
00378 /** @defgroup HASH_Private_Variables HASH Private Variables
00379   * @{
00380   */
00381 
00382 /**
00383   * @}
00384   */ 
00385 
00386 /* Private constants ---------------------------------------------------------*/
00387 /** @defgroup HASH_Private_Constants HASH Private Constants
00388   * @{
00389   */
00390 
00391 /**
00392   * @}
00393   */ 
00394 
00395 /* Private macros ------------------------------------------------------------*/
00396 /** @defgroup HASH_Private_Macros HASH Private Macros
00397   * @{
00398   */
00399 #define IS_HASH_ALGOSELECTION(__ALGOSELECTION__) (((__ALGOSELECTION__) == HASH_ALGOSELECTION_SHA1)   || \
00400                                                   ((__ALGOSELECTION__) == HASH_ALGOSELECTION_SHA224) || \
00401                                                   ((__ALGOSELECTION__) == HASH_ALGOSELECTION_SHA256) || \
00402                                                   ((__ALGOSELECTION__) == HASH_ALGOSELECTION_MD5))
00403 
00404 
00405 #define IS_HASH_ALGOMODE(__ALGOMODE__) (((__ALGOMODE__) == HASH_ALGOMODE_HASH) || \
00406                                         ((__ALGOMODE__) == HASH_ALGOMODE_HMAC))
00407 
00408 
00409 #define IS_HASH_DATATYPE(__DATATYPE__) (((__DATATYPE__) == HASH_DATATYPE_32B)|| \
00410                                         ((__DATATYPE__) == HASH_DATATYPE_16B)|| \
00411                                         ((__DATATYPE__) == HASH_DATATYPE_8B) || \
00412                                         ((__DATATYPE__) == HASH_DATATYPE_1B))
00413 
00414 
00415 #define IS_HASH_HMAC_KEYTYPE(__KEYTYPE__) (((__KEYTYPE__) == HASH_HMAC_KEYTYPE_SHORTKEY) || \
00416                                            ((__KEYTYPE__) == HASH_HMAC_KEYTYPE_LONGKEY))
00417 
00418 #define IS_HASH_SHA1_BUFFER_SIZE(__SIZE__) ((((__SIZE__)%4U) != 0U)? 0U: 1U)
00419 
00420 /**
00421   * @}
00422   */ 
00423 
00424 /* Private functions ---------------------------------------------------------*/
00425 /** @defgroup HASH_Private_Functions HASH Private Functions
00426   * @{
00427   */
00428 
00429 /**
00430   * @}
00431   */
00432  
00433 #endif /* STM32F415xx || STM32F417xx || STM32F437xx || STM32F439xx || STM32F479xx */
00434 /**
00435   * @}
00436   */ 
00437 
00438 /**
00439   * @}
00440   */
00441   
00442 #ifdef __cplusplus
00443 }
00444 #endif
00445 
00446 
00447 #endif /* __STM32F4xx_HAL_HASH_H */
00448 
00449 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/