stm32f4xx_hash.h 9.6 KB

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