stm32f4xx_hash.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_hash.h
  4. * @author MCD Application Team
  5. * @version V1.8.1
  6. * @date 27-January-2022
  7. * @brief This file contains all the functions prototypes for the HASH
  8. * firmware library.
  9. ******************************************************************************
  10. * @attention
  11. *
  12. * Copyright (c) 2016 STMicroelectronics.
  13. * All rights reserved.
  14. *
  15. * This software is licensed under terms that can be found in the LICENSE file
  16. * in the root directory of this software component.
  17. * If no LICENSE file comes with this software, it is provided AS-IS.
  18. *
  19. ******************************************************************************
  20. */
  21. /* Define to prevent recursive inclusion -------------------------------------*/
  22. #ifndef __STM32F4xx_HASH_H
  23. #define __STM32F4xx_HASH_H
  24. #ifdef __cplusplus
  25. extern "C" {
  26. #endif
  27. /* Includes ------------------------------------------------------------------*/
  28. #include "stm32f4xx.h"
  29. /** @addtogroup STM32F4xx_StdPeriph_Driver
  30. * @{
  31. */
  32. /** @addtogroup HASH
  33. * @{
  34. */
  35. /* Exported types ------------------------------------------------------------*/
  36. /**
  37. * @brief HASH Init structure definition
  38. */
  39. typedef struct
  40. {
  41. uint32_t HASH_AlgoSelection; /*!< SHA-1, SHA-224, SHA-256 or MD5. This parameter
  42. can be a value of @ref HASH_Algo_Selection */
  43. uint32_t HASH_AlgoMode; /*!< HASH or HMAC. This parameter can be a value
  44. of @ref HASH_processor_Algorithm_Mode */
  45. uint32_t HASH_DataType; /*!< 32-bit data, 16-bit data, 8-bit data or
  46. bit string. This parameter can be a value of
  47. @ref HASH_Data_Type */
  48. uint32_t HASH_HMACKeyType; /*!< HMAC Short key or HMAC Long Key. This parameter
  49. can be a value of @ref HASH_HMAC_Long_key_only_for_HMAC_mode */
  50. }HASH_InitTypeDef;
  51. /**
  52. * @brief HASH message digest result structure definition
  53. */
  54. typedef struct
  55. {
  56. uint32_t Data[8]; /*!< Message digest result : 8x 32bit wors for SHA-256,
  57. 7x 32bit wors for SHA-224,
  58. 5x 32bit words for SHA-1 or
  59. 4x 32bit words for MD5 */
  60. } HASH_MsgDigest;
  61. /**
  62. * @brief HASH context swapping structure definition
  63. */
  64. typedef struct
  65. {
  66. uint32_t HASH_IMR;
  67. uint32_t HASH_STR;
  68. uint32_t HASH_CR;
  69. uint32_t HASH_CSR[54];
  70. }HASH_Context;
  71. /* Exported constants --------------------------------------------------------*/
  72. /** @defgroup HASH_Exported_Constants
  73. * @{
  74. */
  75. /** @defgroup HASH_Algo_Selection
  76. * @{
  77. */
  78. #define HASH_AlgoSelection_SHA1 ((uint32_t)0x0000) /*!< HASH function is SHA1 */
  79. #define HASH_AlgoSelection_SHA224 HASH_CR_ALGO_1 /*!< HASH function is SHA224 */
  80. #define HASH_AlgoSelection_SHA256 HASH_CR_ALGO /*!< HASH function is SHA256 */
  81. #define HASH_AlgoSelection_MD5 HASH_CR_ALGO_0 /*!< HASH function is MD5 */
  82. #define IS_HASH_ALGOSELECTION(ALGOSELECTION) (((ALGOSELECTION) == HASH_AlgoSelection_SHA1) || \
  83. ((ALGOSELECTION) == HASH_AlgoSelection_SHA224) || \
  84. ((ALGOSELECTION) == HASH_AlgoSelection_SHA256) || \
  85. ((ALGOSELECTION) == HASH_AlgoSelection_MD5))
  86. /**
  87. * @}
  88. */
  89. /** @defgroup HASH_processor_Algorithm_Mode
  90. * @{
  91. */
  92. #define HASH_AlgoMode_HASH ((uint32_t)0x00000000) /*!< Algorithm is HASH */
  93. #define HASH_AlgoMode_HMAC HASH_CR_MODE /*!< Algorithm is HMAC */
  94. #define IS_HASH_ALGOMODE(ALGOMODE) (((ALGOMODE) == HASH_AlgoMode_HASH) || \
  95. ((ALGOMODE) == HASH_AlgoMode_HMAC))
  96. /**
  97. * @}
  98. */
  99. /** @defgroup HASH_Data_Type
  100. * @{
  101. */
  102. #define HASH_DataType_32b ((uint32_t)0x0000) /*!< 32-bit data. No swapping */
  103. #define HASH_DataType_16b HASH_CR_DATATYPE_0 /*!< 16-bit data. Each half word is swapped */
  104. #define HASH_DataType_8b HASH_CR_DATATYPE_1 /*!< 8-bit data. All bytes are swapped */
  105. #define HASH_DataType_1b HASH_CR_DATATYPE /*!< 1-bit data. In the word all bits are swapped */
  106. #define IS_HASH_DATATYPE(DATATYPE) (((DATATYPE) == HASH_DataType_32b)|| \
  107. ((DATATYPE) == HASH_DataType_16b)|| \
  108. ((DATATYPE) == HASH_DataType_8b) || \
  109. ((DATATYPE) == HASH_DataType_1b))
  110. /**
  111. * @}
  112. */
  113. /** @defgroup HASH_HMAC_Long_key_only_for_HMAC_mode
  114. * @{
  115. */
  116. #define HASH_HMACKeyType_ShortKey ((uint32_t)0x00000000) /*!< HMAC Key is <= 64 bytes */
  117. #define HASH_HMACKeyType_LongKey HASH_CR_LKEY /*!< HMAC Key is > 64 bytes */
  118. #define IS_HASH_HMAC_KEYTYPE(KEYTYPE) (((KEYTYPE) == HASH_HMACKeyType_ShortKey) || \
  119. ((KEYTYPE) == HASH_HMACKeyType_LongKey))
  120. /**
  121. * @}
  122. */
  123. /** @defgroup Number_of_valid_bits_in_last_word_of_the_message
  124. * @{
  125. */
  126. #define IS_HASH_VALIDBITSNUMBER(VALIDBITS) ((VALIDBITS) <= 0x1F)
  127. /**
  128. * @}
  129. */
  130. /** @defgroup HASH_interrupts_definition
  131. * @{
  132. */
  133. #define HASH_IT_DINI HASH_IMR_DINIM /*!< A new block can be entered into the input buffer (DIN) */
  134. #define HASH_IT_DCI HASH_IMR_DCIM /*!< Digest calculation complete */
  135. #define IS_HASH_IT(IT) ((((IT) & (uint32_t)0xFFFFFFFC) == 0x00000000) && ((IT) != 0x00000000))
  136. #define IS_HASH_GET_IT(IT) (((IT) == HASH_IT_DINI) || ((IT) == HASH_IT_DCI))
  137. /**
  138. * @}
  139. */
  140. /** @defgroup HASH_flags_definition
  141. * @{
  142. */
  143. #define HASH_FLAG_DINIS HASH_SR_DINIS /*!< 16 locations are free in the DIN : A new block can be entered into the input buffer */
  144. #define HASH_FLAG_DCIS HASH_SR_DCIS /*!< Digest calculation complete */
  145. #define HASH_FLAG_DMAS HASH_SR_DMAS /*!< DMA interface is enabled (DMAE=1) or a transfer is ongoing */
  146. #define HASH_FLAG_BUSY HASH_SR_BUSY /*!< The hash core is Busy : processing a block of data */
  147. #define HASH_FLAG_DINNE HASH_CR_DINNE /*!< DIN not empty : The input buffer contains at least one word of data */
  148. #define IS_HASH_GET_FLAG(FLAG) (((FLAG) == HASH_FLAG_DINIS) || \
  149. ((FLAG) == HASH_FLAG_DCIS) || \
  150. ((FLAG) == HASH_FLAG_DMAS) || \
  151. ((FLAG) == HASH_FLAG_BUSY) || \
  152. ((FLAG) == HASH_FLAG_DINNE))
  153. #define IS_HASH_CLEAR_FLAG(FLAG)(((FLAG) == HASH_FLAG_DINIS) || \
  154. ((FLAG) == HASH_FLAG_DCIS))
  155. /**
  156. * @}
  157. */
  158. /**
  159. * @}
  160. */
  161. /* Exported macro ------------------------------------------------------------*/
  162. /* Exported functions --------------------------------------------------------*/
  163. /* Function used to set the HASH configuration to the default reset state ****/
  164. void HASH_DeInit(void);
  165. /* HASH Configuration function ************************************************/
  166. void HASH_Init(HASH_InitTypeDef* HASH_InitStruct);
  167. void HASH_StructInit(HASH_InitTypeDef* HASH_InitStruct);
  168. void HASH_Reset(void);
  169. /* HASH Message Digest generation functions ***********************************/
  170. void HASH_DataIn(uint32_t Data);
  171. uint8_t HASH_GetInFIFOWordsNbr(void);
  172. void HASH_SetLastWordValidBitsNbr(uint16_t ValidNumber);
  173. void HASH_StartDigest(void);
  174. void HASH_AutoStartDigest(FunctionalState NewState);
  175. void HASH_GetDigest(HASH_MsgDigest* HASH_MessageDigest);
  176. /* HASH Context swapping functions ********************************************/
  177. void HASH_SaveContext(HASH_Context* HASH_ContextSave);
  178. void HASH_RestoreContext(HASH_Context* HASH_ContextRestore);
  179. /* HASH DMA interface function ************************************************/
  180. void HASH_DMACmd(FunctionalState NewState);
  181. /* HASH Interrupts and flags management functions *****************************/
  182. void HASH_ITConfig(uint32_t HASH_IT, FunctionalState NewState);
  183. FlagStatus HASH_GetFlagStatus(uint32_t HASH_FLAG);
  184. void HASH_ClearFlag(uint32_t HASH_FLAG);
  185. ITStatus HASH_GetITStatus(uint32_t HASH_IT);
  186. void HASH_ClearITPendingBit(uint32_t HASH_IT);
  187. /* High Level SHA1 functions **************************************************/
  188. ErrorStatus HASH_SHA1(uint8_t *Input, uint32_t Ilen, uint8_t Output[20]);
  189. ErrorStatus HMAC_SHA1(uint8_t *Key, uint32_t Keylen,
  190. uint8_t *Input, uint32_t Ilen,
  191. uint8_t Output[20]);
  192. /* High Level MD5 functions ***************************************************/
  193. ErrorStatus HASH_MD5(uint8_t *Input, uint32_t Ilen, uint8_t Output[16]);
  194. ErrorStatus HMAC_MD5(uint8_t *Key, uint32_t Keylen,
  195. uint8_t *Input, uint32_t Ilen,
  196. uint8_t Output[16]);
  197. #ifdef __cplusplus
  198. }
  199. #endif
  200. #endif /*__STM32F4xx_HASH_H */
  201. /**
  202. * @}
  203. */
  204. /**
  205. * @}
  206. */