stm32f2xx_hash.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /**
  2. ******************************************************************************
  3. * @file stm32f2xx_hash.h
  4. * @author MCD Application Team
  5. * @version V1.1.3
  6. * @date 31-December-2021
  7. * @brief This file contains all the functions prototypes for the HASH
  8. * firmware library.
  9. ******************************************************************************
  10. * @attention
  11. *
  12. * Copyright (c) 2012 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 __STM32F2xx_HASH_H
  23. #define __STM32F2xx_HASH_H
  24. #ifdef __cplusplus
  25. extern "C" {
  26. #endif
  27. /* Includes ------------------------------------------------------------------*/
  28. #include "stm32f2xx.h"
  29. /** @addtogroup STM32F2xx_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 or MD5. This parameter can be a value
  42. 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[5]; /*!< Message digest result : 5x 32bit words for SHA1 or
  57. 4x 32bit words for MD5 */
  58. } HASH_MsgDigest;
  59. /**
  60. * @brief HASH context swapping structure definition
  61. */
  62. typedef struct
  63. {
  64. uint32_t HASH_IMR;
  65. uint32_t HASH_STR;
  66. uint32_t HASH_CR;
  67. uint32_t HASH_CSR[51];
  68. }HASH_Context;
  69. /* Exported constants --------------------------------------------------------*/
  70. /** @defgroup HASH_Exported_Constants
  71. * @{
  72. */
  73. /** @defgroup HASH_Algo_Selection
  74. * @{
  75. */
  76. #define HASH_AlgoSelection_SHA1 ((uint16_t)0x0000) /*!< HASH function is SHA1 */
  77. #define HASH_AlgoSelection_MD5 ((uint16_t)0x0080) /*!< HASH function is MD5 */
  78. #define IS_HASH_ALGOSELECTION(ALGOSELECTION) (((ALGOSELECTION) == HASH_AlgoSelection_SHA1) || \
  79. ((ALGOSELECTION) == HASH_AlgoSelection_MD5))
  80. /**
  81. * @}
  82. */
  83. /** @defgroup HASH_processor_Algorithm_Mode
  84. * @{
  85. */
  86. #define HASH_AlgoMode_HASH ((uint16_t)0x0000) /*!< Algorithm is HASH */
  87. #define HASH_AlgoMode_HMAC ((uint16_t)0x0040) /*!< Algorithm is HMAC */
  88. #define IS_HASH_ALGOMODE(ALGOMODE) (((ALGOMODE) == HASH_AlgoMode_HASH) || \
  89. ((ALGOMODE) == HASH_AlgoMode_HMAC))
  90. /**
  91. * @}
  92. */
  93. /** @defgroup HASH_Data_Type
  94. * @{
  95. */
  96. #define HASH_DataType_32b ((uint16_t)0x0000)
  97. #define HASH_DataType_16b ((uint16_t)0x0010)
  98. #define HASH_DataType_8b ((uint16_t)0x0020)
  99. #define HASH_DataType_1b ((uint16_t)0x0030)
  100. #define IS_HASH_DATATYPE(DATATYPE) (((DATATYPE) == HASH_DataType_32b)|| \
  101. ((DATATYPE) == HASH_DataType_16b)|| \
  102. ((DATATYPE) == HASH_DataType_8b)|| \
  103. ((DATATYPE) == HASH_DataType_1b))
  104. /**
  105. * @}
  106. */
  107. /** @defgroup HASH_HMAC_Long_key_only_for_HMAC_mode
  108. * @{
  109. */
  110. #define HASH_HMACKeyType_ShortKey ((uint32_t)0x00000000) /*!< HMAC Key is <= 64 bytes */
  111. #define HASH_HMACKeyType_LongKey ((uint32_t)0x00010000) /*!< HMAC Key is > 64 bytes */
  112. #define IS_HASH_HMAC_KEYTYPE(KEYTYPE) (((KEYTYPE) == HASH_HMACKeyType_ShortKey) || \
  113. ((KEYTYPE) == HASH_HMACKeyType_LongKey))
  114. /**
  115. * @}
  116. */
  117. /** @defgroup Number_of_valid_bits_in_last_word_of_the_message
  118. * @{
  119. */
  120. #define IS_HASH_VALIDBITSNUMBER(VALIDBITS) ((VALIDBITS) <= 0x1F)
  121. /**
  122. * @}
  123. */
  124. /** @defgroup HASH_interrupts_definition
  125. * @{
  126. */
  127. #define HASH_IT_DINI ((uint8_t)0x01) /*!< A new block can be entered into the input buffer (DIN)*/
  128. #define HASH_IT_DCI ((uint8_t)0x02) /*!< Digest calculation complete */
  129. #define IS_HASH_IT(IT) ((((IT) & (uint8_t)0xFC) == 0x00) && ((IT) != 0x00))
  130. #define IS_HASH_GET_IT(IT) (((IT) == HASH_IT_DINI) || ((IT) == HASH_IT_DCI))
  131. /**
  132. * @}
  133. */
  134. /** @defgroup HASH_flags_definition
  135. * @{
  136. */
  137. #define HASH_FLAG_DINIS ((uint16_t)0x0001) /*!< 16 locations are free in the DIN : A new block can be entered into the input buffer.*/
  138. #define HASH_FLAG_DCIS ((uint16_t)0x0002) /*!< Digest calculation complete */
  139. #define HASH_FLAG_DMAS ((uint16_t)0x0004) /*!< DMA interface is enabled (DMAE=1) or a transfer is ongoing */
  140. #define HASH_FLAG_BUSY ((uint16_t)0x0008) /*!< The hash core is Busy : processing a block of data */
  141. #define HASH_FLAG_DINNE ((uint16_t)0x1000) /*!< DIN not empty : The input buffer contains at least one word of data */
  142. #define IS_HASH_GET_FLAG(FLAG) (((FLAG) == HASH_FLAG_DINIS) || \
  143. ((FLAG) == HASH_FLAG_DCIS) || \
  144. ((FLAG) == HASH_FLAG_DMAS) || \
  145. ((FLAG) == HASH_FLAG_BUSY) || \
  146. ((FLAG) == HASH_FLAG_DINNE))
  147. #define IS_HASH_CLEAR_FLAG(FLAG)(((FLAG) == HASH_FLAG_DINIS) || \
  148. ((FLAG) == HASH_FLAG_DCIS))
  149. /**
  150. * @}
  151. */
  152. /**
  153. * @}
  154. */
  155. /* Exported macro ------------------------------------------------------------*/
  156. /* Exported functions --------------------------------------------------------*/
  157. /* Function used to set the HASH configuration to the default reset state ****/
  158. void HASH_DeInit(void);
  159. /* HASH Configuration function ************************************************/
  160. void HASH_Init(HASH_InitTypeDef* HASH_InitStruct);
  161. void HASH_StructInit(HASH_InitTypeDef* HASH_InitStruct);
  162. void HASH_Reset(void);
  163. /* HASH Message Digest generation functions ***********************************/
  164. void HASH_DataIn(uint32_t Data);
  165. uint8_t HASH_GetInFIFOWordsNbr(void);
  166. void HASH_SetLastWordValidBitsNbr(uint16_t ValidNumber);
  167. void HASH_StartDigest(void);
  168. void HASH_GetDigest(HASH_MsgDigest* HASH_MessageDigest);
  169. /* HASH Context swapping functions ********************************************/
  170. void HASH_SaveContext(HASH_Context* HASH_ContextSave);
  171. void HASH_RestoreContext(HASH_Context* HASH_ContextRestore);
  172. /* HASH's DMA interface function **********************************************/
  173. void HASH_DMACmd(FunctionalState NewState);
  174. /* HASH Interrupts and flags management functions *****************************/
  175. void HASH_ITConfig(uint8_t HASH_IT, FunctionalState NewState);
  176. FlagStatus HASH_GetFlagStatus(uint16_t HASH_FLAG);
  177. void HASH_ClearFlag(uint16_t HASH_FLAG);
  178. ITStatus HASH_GetITStatus(uint8_t HASH_IT);
  179. void HASH_ClearITPendingBit(uint8_t HASH_IT);
  180. /* High Level SHA1 functions **************************************************/
  181. ErrorStatus HASH_SHA1(uint8_t *Input, uint32_t Ilen, uint8_t Output[20]);
  182. ErrorStatus HMAC_SHA1(uint8_t *Key, uint32_t Keylen,
  183. uint8_t *Input, uint32_t Ilen,
  184. uint8_t Output[20]);
  185. /* High Level MD5 functions ***************************************************/
  186. ErrorStatus HASH_MD5(uint8_t *Input, uint32_t Ilen, uint8_t Output[16]);
  187. ErrorStatus HMAC_MD5(uint8_t *Key, uint32_t Keylen,
  188. uint8_t *Input, uint32_t Ilen,
  189. uint8_t Output[16]);
  190. #ifdef __cplusplus
  191. }
  192. #endif
  193. #endif /*__STM32F2xx_HASH_H */
  194. /**
  195. * @}
  196. */
  197. /**
  198. * @}
  199. */