1 /**
2   ******************************************************************************
3   * @file    stm32f4xx_hash.h
4   * @author  MCD Application Team
5   * @version V1.5.1
6   * @date    22-May-2015
7   * @brief   This file contains all the functions prototypes for the HASH
8   *          firmware library.
9   ******************************************************************************
10   * @attention
11   *
12   * <h2><center>&copy; COPYRIGHT 2015 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 __STM32F4xx_HASH_H
31 #define __STM32F4xx_HASH_H
32 
33 #ifdef __cplusplus
34  extern "C" {
35 #endif
36 
37 /* Includes ------------------------------------------------------------------*/
38 #include "stm32f4xx.h"
39 
40 /** @addtogroup STM32F4xx_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, SHA-224, SHA-256 or MD5. This parameter
56                                     can be a value 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[8];      /*!< Message digest result : 8x 32bit wors for SHA-256,
72                                                       7x 32bit wors for SHA-224,
73                                                       5x 32bit words for SHA-1 or
74                                                       4x 32bit words for MD5  */
75 } HASH_MsgDigest;
76 
77 /**
78   * @brief  HASH context swapping structure definition
79   */
80 typedef struct
81 {
82   uint32_t HASH_IMR;
83   uint32_t HASH_STR;
84   uint32_t HASH_CR;
85   uint32_t HASH_CSR[54];
86 }HASH_Context;
87 
88 /* Exported constants --------------------------------------------------------*/
89 
90 /** @defgroup HASH_Exported_Constants
91   * @{
92   */
93 
94 /** @defgroup HASH_Algo_Selection
95   * @{
96   */
97 #define HASH_AlgoSelection_SHA1      ((uint32_t)0x0000) /*!< HASH function is SHA1   */
98 #define HASH_AlgoSelection_SHA224    HASH_CR_ALGO_1     /*!< HASH function is SHA224 */
99 #define HASH_AlgoSelection_SHA256    HASH_CR_ALGO       /*!< HASH function is SHA256 */
100 #define HASH_AlgoSelection_MD5       HASH_CR_ALGO_0     /*!< HASH function is MD5    */
101 
102 #define IS_HASH_ALGOSELECTION(ALGOSELECTION) (((ALGOSELECTION) == HASH_AlgoSelection_SHA1) || \
103                                               ((ALGOSELECTION) == HASH_AlgoSelection_SHA224) || \
104                                               ((ALGOSELECTION) == HASH_AlgoSelection_SHA256) || \
105                                               ((ALGOSELECTION) == HASH_AlgoSelection_MD5))
106 /**
107   * @}
108   */
109 
110 /** @defgroup HASH_processor_Algorithm_Mode
111   * @{
112   */
113 #define HASH_AlgoMode_HASH         ((uint32_t)0x00000000) /*!< Algorithm is HASH */
114 #define HASH_AlgoMode_HMAC         HASH_CR_MODE           /*!< Algorithm is HMAC */
115 
116 #define IS_HASH_ALGOMODE(ALGOMODE) (((ALGOMODE) == HASH_AlgoMode_HASH) || \
117                                     ((ALGOMODE) == HASH_AlgoMode_HMAC))
118 /**
119   * @}
120   */
121 
122 /** @defgroup HASH_Data_Type
123   * @{
124   */
125 #define HASH_DataType_32b          ((uint32_t)0x0000) /*!< 32-bit data. No swapping                     */
126 #define HASH_DataType_16b          HASH_CR_DATATYPE_0 /*!< 16-bit data. Each half word is swapped       */
127 #define HASH_DataType_8b           HASH_CR_DATATYPE_1 /*!< 8-bit data. All bytes are swapped            */
128 #define HASH_DataType_1b           HASH_CR_DATATYPE   /*!< 1-bit data. In the word all bits are swapped */
129 
130 #define IS_HASH_DATATYPE(DATATYPE) (((DATATYPE) == HASH_DataType_32b)|| \
131                                     ((DATATYPE) == HASH_DataType_16b)|| \
132                                     ((DATATYPE) == HASH_DataType_8b) || \
133                                     ((DATATYPE) == HASH_DataType_1b))
134 /**
135   * @}
136   */
137 
138 /** @defgroup HASH_HMAC_Long_key_only_for_HMAC_mode
139   * @{
140   */
141 #define HASH_HMACKeyType_ShortKey      ((uint32_t)0x00000000) /*!< HMAC Key is <= 64 bytes */
142 #define HASH_HMACKeyType_LongKey       HASH_CR_LKEY           /*!< HMAC Key is > 64 bytes  */
143 
144 #define IS_HASH_HMAC_KEYTYPE(KEYTYPE) (((KEYTYPE) == HASH_HMACKeyType_ShortKey) || \
145                                        ((KEYTYPE) == HASH_HMACKeyType_LongKey))
146 /**
147   * @}
148   */
149 
150 /** @defgroup Number_of_valid_bits_in_last_word_of_the_message
151   * @{
152   */
153 #define IS_HASH_VALIDBITSNUMBER(VALIDBITS) ((VALIDBITS) <= 0x1F)
154 
155 /**
156   * @}
157   */
158 
159 /** @defgroup HASH_interrupts_definition
160   * @{
161   */
162 #define HASH_IT_DINI               HASH_IMR_DINIM  /*!< A new block can be entered into the input buffer (DIN) */
163 #define HASH_IT_DCI                HASH_IMR_DCIM   /*!< Digest calculation complete                            */
164 
165 #define IS_HASH_IT(IT) ((((IT) & (uint32_t)0xFFFFFFFC) == 0x00000000) && ((IT) != 0x00000000))
166 #define IS_HASH_GET_IT(IT) (((IT) == HASH_IT_DINI) || ((IT) == HASH_IT_DCI))
167 
168 /**
169   * @}
170   */
171 
172 /** @defgroup HASH_flags_definition
173   * @{
174   */
175 #define HASH_FLAG_DINIS            HASH_SR_DINIS  /*!< 16 locations are free in the DIN : A new block can be entered into the input buffer */
176 #define HASH_FLAG_DCIS             HASH_SR_DCIS   /*!< Digest calculation complete                                                         */
177 #define HASH_FLAG_DMAS             HASH_SR_DMAS   /*!< DMA interface is enabled (DMAE=1) or a transfer is ongoing                          */
178 #define HASH_FLAG_BUSY             HASH_SR_BUSY   /*!< The hash core is Busy : processing a block of data                                  */
179 #define HASH_FLAG_DINNE            HASH_CR_DINNE  /*!< DIN not empty : The input buffer contains at least one word of data                 */
180 
181 #define IS_HASH_GET_FLAG(FLAG) (((FLAG) == HASH_FLAG_DINIS) || \
182                                 ((FLAG) == HASH_FLAG_DCIS)  || \
183                                 ((FLAG) == HASH_FLAG_DMAS)  || \
184                                 ((FLAG) == HASH_FLAG_BUSY)  || \
185                                 ((FLAG) == HASH_FLAG_DINNE))
186 
187 #define IS_HASH_CLEAR_FLAG(FLAG)(((FLAG) == HASH_FLAG_DINIS) || \
188                                  ((FLAG) == HASH_FLAG_DCIS))
189 
190 /**
191   * @}
192   */
193 
194 /**
195   * @}
196   */
197 
198 /* Exported macro ------------------------------------------------------------*/
199 /* Exported functions --------------------------------------------------------*/
200 
201 /*  Function used to set the HASH configuration to the default reset state ****/
202 void HASH_DeInit(void);
203 
204 /* HASH Configuration function ************************************************/
205 void HASH_Init(HASH_InitTypeDef* HASH_InitStruct);
206 void HASH_StructInit(HASH_InitTypeDef* HASH_InitStruct);
207 void HASH_Reset(void);
208 
209 /* HASH Message Digest generation functions ***********************************/
210 void HASH_DataIn(uint32_t Data);
211 uint8_t HASH_GetInFIFOWordsNbr(void);
212 void HASH_SetLastWordValidBitsNbr(uint16_t ValidNumber);
213 void HASH_StartDigest(void);
214 void HASH_AutoStartDigest(FunctionalState NewState);
215 void HASH_GetDigest(HASH_MsgDigest* HASH_MessageDigest);
216 
217 /* HASH Context swapping functions ********************************************/
218 void HASH_SaveContext(HASH_Context* HASH_ContextSave);
219 void HASH_RestoreContext(HASH_Context* HASH_ContextRestore);
220 
221 /* HASH DMA interface function ************************************************/
222 void HASH_DMACmd(FunctionalState NewState);
223 
224 /* HASH Interrupts and flags management functions *****************************/
225 void HASH_ITConfig(uint32_t HASH_IT, FunctionalState NewState);
226 FlagStatus HASH_GetFlagStatus(uint32_t HASH_FLAG);
227 void HASH_ClearFlag(uint32_t HASH_FLAG);
228 ITStatus HASH_GetITStatus(uint32_t HASH_IT);
229 void HASH_ClearITPendingBit(uint32_t HASH_IT);
230 
231 /* High Level SHA1 functions **************************************************/
232 ErrorStatus HASH_SHA1(uint8_t *Input, uint32_t Ilen, uint8_t Output[20]);
233 ErrorStatus HMAC_SHA1(uint8_t *Key, uint32_t Keylen,
234                       uint8_t *Input, uint32_t Ilen,
235                       uint8_t Output[20]);
236 
237 /* High Level MD5 functions ***************************************************/
238 ErrorStatus HASH_MD5(uint8_t *Input, uint32_t Ilen, uint8_t Output[16]);
239 ErrorStatus HMAC_MD5(uint8_t *Key, uint32_t Keylen,
240                      uint8_t *Input, uint32_t Ilen,
241                      uint8_t Output[16]);
242 
243 #ifdef __cplusplus
244 }
245 #endif
246 
247 #endif /*__STM32F4xx_HASH_H */
248 
249 /**
250   * @}
251   */
252 
253 /**
254   * @}
255   */
256 
257 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
258