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