stm32f4xx_hash.h 8.6 KB

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