Added STM32F4xx StdPeriph Driver sources
[fw/stlink] / example / stm32f4 / STM32F4xx_StdPeriph_Driver / inc / stm32f4xx_hash.h
1 /**
2   ******************************************************************************
3   * @file    stm32f4xx_hash.h
4   * @author  MCD Application Team
5   * @version V1.0.0RC1
6   * @date    25-August-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
23 /* Define to prevent recursive inclusion -------------------------------------*/
24 #ifndef __STM32F4xx_HASH_H
25 #define __STM32F4xx_HASH_H
26
27 #ifdef __cplusplus
28  extern "C" {
29 #endif
30
31 /* Includes ------------------------------------------------------------------*/
32 #include "stm32f4xx.h"
33
34 /** @addtogroup STM32F4xx_StdPeriph_Driver
35   * @{
36   */
37
38 /** @addtogroup HASH
39   * @{
40   */ 
41
42 /* Exported types ------------------------------------------------------------*/
43
44 /** 
45   * @brief   HASH Init structure definition
46   */ 
47 typedef struct
48 {
49   uint32_t HASH_AlgoSelection; /*!< SHA-1 or MD5. This parameter can be a value 
50                                     of @ref HASH_Algo_Selection */
51   uint32_t HASH_AlgoMode;      /*!< HASH or HMAC. This parameter can be a value 
52                                     of @ref HASH_processor_Algorithm_Mode */
53   uint32_t HASH_DataType;      /*!< 32-bit data, 16-bit data, 8-bit data or 
54                                     bit-string. This parameter can be a value of
55                                     @ref HASH_Data_Type */
56   uint32_t HASH_HMACKeyType;   /*!< HMAC Short key or HMAC Long Key. This parameter
57                                     can be a value of @ref HASH_HMAC_Long_key_only_for_HMAC_mode */
58 }HASH_InitTypeDef;
59
60 /** 
61   * @brief  HASH message digest result structure definition  
62   */ 
63 typedef struct
64 {
65   uint32_t Data[5];      /*!< Message digest result : 5x 32bit words for SHA1 or 
66                                                       4x 32bit words for MD5  */
67 } HASH_MsgDigest; 
68
69 /** 
70   * @brief  HASH context swapping structure definition  
71   */ 
72 typedef struct
73 {
74   uint32_t HASH_IMR; 
75   uint32_t HASH_STR;      
76   uint32_t HASH_CR;     
77   uint32_t HASH_CSR[51];       
78 }HASH_Context;
79
80 /* Exported constants --------------------------------------------------------*/
81
82 /** @defgroup HASH_Exported_Constants
83   * @{
84   */ 
85
86 /** @defgroup HASH_Algo_Selection 
87   * @{
88   */ 
89 #define HASH_AlgoSelection_SHA1    ((uint16_t)0x0000) /*!< HASH function is SHA1 */
90 #define HASH_AlgoSelection_MD5     ((uint16_t)0x0080) /*!< HASH function is MD5 */
91
92 #define IS_HASH_ALGOSELECTION(ALGOSELECTION) (((ALGOSELECTION) == HASH_AlgoSelection_SHA1) || \
93                                               ((ALGOSELECTION) == HASH_AlgoSelection_MD5))
94 /**
95   * @}
96   */
97
98 /** @defgroup HASH_processor_Algorithm_Mode 
99   * @{
100   */ 
101 #define HASH_AlgoMode_HASH         ((uint16_t)0x0000) /*!< Algorithm is HASH */ 
102 #define HASH_AlgoMode_HMAC         ((uint16_t)0x0040) /*!< Algorithm is HMAC */
103
104 #define IS_HASH_ALGOMODE(ALGOMODE) (((ALGOMODE) == HASH_AlgoMode_HASH) || \
105                                     ((ALGOMODE) == HASH_AlgoMode_HMAC))
106 /**
107   * @}
108   */
109
110 /** @defgroup HASH_Data_Type  
111   * @{
112   */  
113 #define HASH_DataType_32b          ((uint16_t)0x0000)
114 #define HASH_DataType_16b          ((uint16_t)0x0010)
115 #define HASH_DataType_8b           ((uint16_t)0x0020)
116 #define HASH_DataType_1b           ((uint16_t)0x0030)
117
118 #define IS_HASH_DATATYPE(DATATYPE) (((DATATYPE) == HASH_DataType_32b)|| \
119                                     ((DATATYPE) == HASH_DataType_16b)|| \
120                                     ((DATATYPE) == HASH_DataType_8b)|| \
121                                     ((DATATYPE) == HASH_DataType_1b))
122 /**
123   * @}
124   */
125
126 /** @defgroup HASH_HMAC_Long_key_only_for_HMAC_mode  
127   * @{
128   */ 
129 #define HASH_HMACKeyType_ShortKey      ((uint32_t)0x00000000) /*!< HMAC Key is <= 64 bytes */
130 #define HASH_HMACKeyType_LongKey       ((uint32_t)0x00010000) /*!< HMAC Key is > 64 bytes */
131
132 #define IS_HASH_HMAC_KEYTYPE(KEYTYPE) (((KEYTYPE) == HASH_HMACKeyType_ShortKey) || \
133                                   ((KEYTYPE) == HASH_HMACKeyType_LongKey))
134 /**
135   * @}
136   */
137
138 /** @defgroup Number_of_valid_bits_in_last_word_of_the_message   
139   * @{
140   */  
141 #define IS_HASH_VALIDBITSNUMBER(VALIDBITS) ((VALIDBITS) <= 0x1F)
142
143 /**
144   * @}
145   */
146
147 /** @defgroup HASH_interrupts_definition   
148   * @{
149   */  
150 #define HASH_IT_DINI               ((uint8_t)0x01)  /*!< A new block can be entered into the input buffer (DIN)*/
151 #define HASH_IT_DCI                ((uint8_t)0x02)  /*!< Digest calculation complete */
152
153 #define IS_HASH_IT(IT) ((((IT) & (uint8_t)0xFC) == 0x00) && ((IT) != 0x00))
154 #define IS_HASH_GET_IT(IT) (((IT) == HASH_IT_DINI) || ((IT) == HASH_IT_DCI))
155                                    
156 /**
157   * @}
158   */
159
160 /** @defgroup HASH_flags_definition   
161   * @{
162   */  
163 #define HASH_FLAG_DINIS            ((uint16_t)0x0001)  /*!< 16 locations are free in the DIN : A new block can be entered into the input buffer.*/
164 #define HASH_FLAG_DCIS             ((uint16_t)0x0002)  /*!< Digest calculation complete */
165 #define HASH_FLAG_DMAS             ((uint16_t)0x0004)  /*!< DMA interface is enabled (DMAE=1) or a transfer is ongoing */
166 #define HASH_FLAG_BUSY             ((uint16_t)0x0008)  /*!< The hash core is Busy : processing a block of data */
167 #define HASH_FLAG_DINNE            ((uint16_t)0x1000)  /*!< DIN not empty : The input buffer contains at least one word of data */
168
169 #define IS_HASH_GET_FLAG(FLAG) (((FLAG) == HASH_FLAG_DINIS) || \
170                                 ((FLAG) == HASH_FLAG_DCIS)  || \
171                                 ((FLAG) == HASH_FLAG_DMAS)  || \
172                                 ((FLAG) == HASH_FLAG_BUSY)  || \
173                                 ((FLAG) == HASH_FLAG_DINNE)) 
174
175 #define IS_HASH_CLEAR_FLAG(FLAG)(((FLAG) == HASH_FLAG_DINIS) || \
176                                  ((FLAG) == HASH_FLAG_DCIS))                                 
177
178 /**
179   * @}
180   */ 
181
182 /**
183   * @}
184   */ 
185
186 /* Exported macro ------------------------------------------------------------*/
187 /* Exported functions --------------------------------------------------------*/ 
188   
189 /*  Function used to set the HASH configuration to the default reset state ****/
190 void HASH_DeInit(void);
191
192 /* HASH Configuration function ************************************************/
193 void HASH_Init(HASH_InitTypeDef* HASH_InitStruct);
194 void HASH_StructInit(HASH_InitTypeDef* HASH_InitStruct);
195 void HASH_Reset(void);
196
197 /* HASH Message Digest generation functions ***********************************/
198 void HASH_DataIn(uint32_t Data);
199 uint8_t HASH_GetInFIFOWordsNbr(void);
200 void HASH_SetLastWordValidBitsNbr(uint16_t ValidNumber);
201 void HASH_StartDigest(void);
202 void HASH_GetDigest(HASH_MsgDigest* HASH_MessageDigest);
203
204 /* HASH Context swapping functions ********************************************/
205 void HASH_SaveContext(HASH_Context* HASH_ContextSave);
206 void HASH_RestoreContext(HASH_Context* HASH_ContextRestore);
207
208 /* HASH's DMA interface function **********************************************/
209 void HASH_DMACmd(FunctionalState NewState);
210
211 /* HASH Interrupts and flags management functions *****************************/
212 void HASH_ITConfig(uint8_t HASH_IT, FunctionalState NewState);
213 FlagStatus HASH_GetFlagStatus(uint16_t HASH_FLAG);
214 void HASH_ClearFlag(uint16_t HASH_FLAG);
215 ITStatus HASH_GetITStatus(uint8_t HASH_IT);
216 void HASH_ClearITPendingBit(uint8_t HASH_IT);
217
218 /* High Level SHA1 functions **************************************************/
219 ErrorStatus HASH_SHA1(uint8_t *Input, uint32_t Ilen, uint8_t Output[20]);
220 ErrorStatus HMAC_SHA1(uint8_t *Key, uint32_t Keylen,
221                       uint8_t *Input, uint32_t Ilen,
222                       uint8_t Output[20]);
223
224 /* High Level MD5 functions ***************************************************/
225 ErrorStatus HASH_MD5(uint8_t *Input, uint32_t Ilen, uint8_t Output[16]);
226 ErrorStatus HMAC_MD5(uint8_t *Key, uint32_t Keylen,
227                      uint8_t *Input, uint32_t Ilen,
228                      uint8_t Output[16]);
229
230 #ifdef __cplusplus
231 }
232 #endif
233
234 #endif /*__STM32F4xx_HASH_H */
235
236 /**
237   * @}
238   */ 
239
240 /**
241   * @}
242   */ 
243
244 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/