stm32f4xx_hash.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_hash.h
  4. * @author MCD Application Team
  5. * @version V1.0.0
  6. * @date 30-September-2011
  7. * @brief This file contains all the functions prototypes for the HASH
  8. * firmware library.
  9. ******************************************************************************
  10. * @attention
  11. *
  12. * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  13. * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  14. * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  15. * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  16. * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  17. * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  18. *
  19. * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
  20. ******************************************************************************
  21. */
  22. /* Define to prevent recursive inclusion -------------------------------------*/
  23. #ifndef __STM32F4xx_HASH_H
  24. #define __STM32F4xx_HASH_H
  25. #ifdef __cplusplus
  26. extern "C" {
  27. #endif
  28. /* Includes ------------------------------------------------------------------*/
  29. #include "stm32f4xx.h"
  30. /** @addtogroup STM32F4xx_StdPeriph_Driver
  31. * @{
  32. */
  33. /** @addtogroup HASH
  34. * @{
  35. */
  36. /* Exported types ------------------------------------------------------------*/
  37. /**
  38. * @brief HASH Init structure definition
  39. */
  40. typedef struct
  41. {
  42. uint32_t HASH_AlgoSelection; /*!< SHA-1 or MD5. This parameter can be a value
  43. of @ref HASH_Algo_Selection */
  44. uint32_t HASH_AlgoMode; /*!< HASH or HMAC. This parameter can be a value
  45. of @ref HASH_processor_Algorithm_Mode */
  46. uint32_t HASH_DataType; /*!< 32-bit data, 16-bit data, 8-bit data or
  47. bit-string. This parameter can be a value of
  48. @ref HASH_Data_Type */
  49. uint32_t HASH_HMACKeyType; /*!< HMAC Short key or HMAC Long Key. This parameter
  50. can be a value of @ref HASH_HMAC_Long_key_only_for_HMAC_mode */
  51. }HASH_InitTypeDef;
  52. /**
  53. * @brief HASH message digest result structure definition
  54. */
  55. typedef struct
  56. {
  57. uint32_t Data[5]; /*!< Message digest result : 5x 32bit words for SHA1 or
  58. 4x 32bit words for MD5 */
  59. } HASH_MsgDigest;
  60. /**
  61. * @brief HASH context swapping structure definition
  62. */
  63. typedef struct
  64. {
  65. uint32_t HASH_IMR;
  66. uint32_t HASH_STR;
  67. uint32_t HASH_CR;
  68. uint32_t HASH_CSR[51];
  69. }HASH_Context;
  70. /* Exported constants --------------------------------------------------------*/
  71. /** @defgroup HASH_Exported_Constants
  72. * @{
  73. */
  74. /** @defgroup HASH_Algo_Selection
  75. * @{
  76. */
  77. #define HASH_AlgoSelection_SHA1 ((uint16_t)0x0000) /*!< HASH function is SHA1 */
  78. #define HASH_AlgoSelection_MD5 ((uint16_t)0x0080) /*!< HASH function is MD5 */
  79. #define IS_HASH_ALGOSELECTION(ALGOSELECTION) (((ALGOSELECTION) == HASH_AlgoSelection_SHA1) || \
  80. ((ALGOSELECTION) == HASH_AlgoSelection_MD5))
  81. /**
  82. * @}
  83. */
  84. /** @defgroup HASH_processor_Algorithm_Mode
  85. * @{
  86. */
  87. #define HASH_AlgoMode_HASH ((uint16_t)0x0000) /*!< Algorithm is HASH */
  88. #define HASH_AlgoMode_HMAC ((uint16_t)0x0040) /*!< Algorithm is HMAC */
  89. #define IS_HASH_ALGOMODE(ALGOMODE) (((ALGOMODE) == HASH_AlgoMode_HASH) || \
  90. ((ALGOMODE) == HASH_AlgoMode_HMAC))
  91. /**
  92. * @}
  93. */
  94. /** @defgroup HASH_Data_Type
  95. * @{
  96. */
  97. #define HASH_DataType_32b ((uint16_t)0x0000)
  98. #define HASH_DataType_16b ((uint16_t)0x0010)
  99. #define HASH_DataType_8b ((uint16_t)0x0020)
  100. #define HASH_DataType_1b ((uint16_t)0x0030)
  101. #define IS_HASH_DATATYPE(DATATYPE) (((DATATYPE) == HASH_DataType_32b)|| \
  102. ((DATATYPE) == HASH_DataType_16b)|| \
  103. ((DATATYPE) == HASH_DataType_8b)|| \
  104. ((DATATYPE) == HASH_DataType_1b))
  105. /**
  106. * @}
  107. */
  108. /** @defgroup HASH_HMAC_Long_key_only_for_HMAC_mode
  109. * @{
  110. */
  111. #define HASH_HMACKeyType_ShortKey ((uint32_t)0x00000000) /*!< HMAC Key is <= 64 bytes */
  112. #define HASH_HMACKeyType_LongKey ((uint32_t)0x00010000) /*!< HMAC Key is > 64 bytes */
  113. #define IS_HASH_HMAC_KEYTYPE(KEYTYPE) (((KEYTYPE) == HASH_HMACKeyType_ShortKey) || \
  114. ((KEYTYPE) == HASH_HMACKeyType_LongKey))
  115. /**
  116. * @}
  117. */
  118. /** @defgroup Number_of_valid_bits_in_last_word_of_the_message
  119. * @{
  120. */
  121. #define IS_HASH_VALIDBITSNUMBER(VALIDBITS) ((VALIDBITS) <= 0x1F)
  122. /**
  123. * @}
  124. */
  125. /** @defgroup HASH_interrupts_definition
  126. * @{
  127. */
  128. #define HASH_IT_DINI ((uint8_t)0x01) /*!< A new block can be entered into the input buffer (DIN)*/
  129. #define HASH_IT_DCI ((uint8_t)0x02) /*!< Digest calculation complete */
  130. #define IS_HASH_IT(IT) ((((IT) & (uint8_t)0xFC) == 0x00) && ((IT) != 0x00))
  131. #define IS_HASH_GET_IT(IT) (((IT) == HASH_IT_DINI) || ((IT) == HASH_IT_DCI))
  132. /**
  133. * @}
  134. */
  135. /** @defgroup HASH_flags_definition
  136. * @{
  137. */
  138. #define HASH_FLAG_DINIS ((uint16_t)0x0001) /*!< 16 locations are free in the DIN : A new block can be entered into the input buffer.*/
  139. #define HASH_FLAG_DCIS ((uint16_t)0x0002) /*!< Digest calculation complete */
  140. #define HASH_FLAG_DMAS ((uint16_t)0x0004) /*!< DMA interface is enabled (DMAE=1) or a transfer is ongoing */
  141. #define HASH_FLAG_BUSY ((uint16_t)0x0008) /*!< The hash core is Busy : processing a block of data */
  142. #define HASH_FLAG_DINNE ((uint16_t)0x1000) /*!< DIN not empty : The input buffer contains at least one word of data */
  143. #define IS_HASH_GET_FLAG(FLAG) (((FLAG) == HASH_FLAG_DINIS) || \
  144. ((FLAG) == HASH_FLAG_DCIS) || \
  145. ((FLAG) == HASH_FLAG_DMAS) || \
  146. ((FLAG) == HASH_FLAG_BUSY) || \
  147. ((FLAG) == HASH_FLAG_DINNE))
  148. #define IS_HASH_CLEAR_FLAG(FLAG)(((FLAG) == HASH_FLAG_DINIS) || \
  149. ((FLAG) == HASH_FLAG_DCIS))
  150. /**
  151. * @}
  152. */
  153. /**
  154. * @}
  155. */
  156. /* Exported macro ------------------------------------------------------------*/
  157. /* Exported functions --------------------------------------------------------*/
  158. /* Function used to set the HASH configuration to the default reset state ****/
  159. void HASH_DeInit(void);
  160. /* HASH Configuration function ************************************************/
  161. void HASH_Init(HASH_InitTypeDef* HASH_InitStruct);
  162. void HASH_StructInit(HASH_InitTypeDef* HASH_InitStruct);
  163. void HASH_Reset(void);
  164. /* HASH Message Digest generation functions ***********************************/
  165. void HASH_DataIn(uint32_t Data);
  166. uint8_t HASH_GetInFIFOWordsNbr(void);
  167. void HASH_SetLastWordValidBitsNbr(uint16_t ValidNumber);
  168. void HASH_StartDigest(void);
  169. void HASH_GetDigest(HASH_MsgDigest* HASH_MessageDigest);
  170. /* HASH Context swapping functions ********************************************/
  171. void HASH_SaveContext(HASH_Context* HASH_ContextSave);
  172. void HASH_RestoreContext(HASH_Context* HASH_ContextRestore);
  173. /* HASH's DMA interface function **********************************************/
  174. void HASH_DMACmd(FunctionalState NewState);
  175. /* HASH Interrupts and flags management functions *****************************/
  176. void HASH_ITConfig(uint8_t HASH_IT, FunctionalState NewState);
  177. FlagStatus HASH_GetFlagStatus(uint16_t HASH_FLAG);
  178. void HASH_ClearFlag(uint16_t HASH_FLAG);
  179. ITStatus HASH_GetITStatus(uint8_t HASH_IT);
  180. void HASH_ClearITPendingBit(uint8_t HASH_IT);
  181. /* High Level SHA1 functions **************************************************/
  182. ErrorStatus HASH_SHA1(uint8_t *Input, uint32_t Ilen, uint8_t Output[20]);
  183. ErrorStatus HMAC_SHA1(uint8_t *Key, uint32_t Keylen,
  184. uint8_t *Input, uint32_t Ilen,
  185. uint8_t Output[20]);
  186. /* High Level MD5 functions ***************************************************/
  187. ErrorStatus HASH_MD5(uint8_t *Input, uint32_t Ilen, uint8_t Output[16]);
  188. ErrorStatus HMAC_MD5(uint8_t *Key, uint32_t Keylen,
  189. uint8_t *Input, uint32_t Ilen,
  190. uint8_t Output[16]);
  191. #ifdef __cplusplus
  192. }
  193. #endif
  194. #endif /*__STM32F4xx_HASH_H */
  195. /**
  196. * @}
  197. */
  198. /**
  199. * @}
  200. */
  201. /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/