Added STM32F4xx StdPeriph Driver sources
[fw/stlink] / example / stm32f4 / STM32F4xx_StdPeriph_Driver / inc / stm32f4xx_cryp.h
1 /**
2   ******************************************************************************
3   * @file    stm32f4xx_cryp.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 Cryptographic
8   *          processor(CRYP) 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_CRYP_H
25 #define __STM32F4xx_CRYP_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 CRYP
39   * @{
40   */ 
41
42 /* Exported types ------------------------------------------------------------*/
43
44 /** 
45   * @brief   CRYP Init structure definition  
46   */ 
47 typedef struct
48 {
49   uint16_t CRYP_AlgoDir;   /*!< Encrypt or Decrypt. This parameter can be a 
50                                 value of @ref CRYP_Algorithm_Direction */
51   uint16_t CRYP_AlgoMode;  /*!< TDES-ECB, TDES-CBC, DES-ECB, DES-CBC, AES-ECB, 
52                                 AES-CBC, AES-CTR, AES-Key. This parameter can be
53                                 a value of @ref CRYP_Algorithm_Mode */
54   uint16_t CRYP_DataType;  /*!< 32-bit data, 16-bit data, bit data or bit-string.
55                                 This parameter can be a value of @ref CRYP_Data_Type */ 
56   uint16_t CRYP_KeySize;   /*!< Used only in AES mode only : 128, 192 or 256 bit 
57                                 key length. This parameter can be a value of 
58                                 @ref CRYP_Key_Size_for_AES_only */
59 }CRYP_InitTypeDef;
60
61 /** 
62   * @brief   CRYP Key(s) structure definition  
63   */ 
64 typedef struct
65 {
66   uint32_t CRYP_Key0Left;  /*!< Key 0 Left  */
67   uint32_t CRYP_Key0Right; /*!< Key 0 Right */
68   uint32_t CRYP_Key1Left;  /*!< Key 1 left  */
69   uint32_t CRYP_Key1Right; /*!< Key 1 Right */
70   uint32_t CRYP_Key2Left;  /*!< Key 2 left  */
71   uint32_t CRYP_Key2Right; /*!< Key 2 Right */
72   uint32_t CRYP_Key3Left;  /*!< Key 3 left  */
73   uint32_t CRYP_Key3Right; /*!< Key 3 Right */
74 }CRYP_KeyInitTypeDef;
75 /** 
76   * @brief   CRYP Initialization Vectors (IV) structure definition  
77   */ 
78 typedef struct
79 {
80   uint32_t CRYP_IV0Left;  /*!< Init Vector 0 Left  */
81   uint32_t CRYP_IV0Right; /*!< Init Vector 0 Right */
82   uint32_t CRYP_IV1Left;  /*!< Init Vector 1 left  */
83   uint32_t CRYP_IV1Right; /*!< Init Vector 1 Right */
84 }CRYP_IVInitTypeDef;
85
86 /** 
87   * @brief  CRYP context swapping structure definition  
88   */ 
89 typedef struct
90 {
91   /*!< Configuration */
92   uint32_t CR_bits9to2;
93   /*!< KEY */
94   uint32_t CRYP_IV0LR;
95   uint32_t CRYP_IV0RR;
96   uint32_t CRYP_IV1LR;
97   uint32_t CRYP_IV1RR;
98   /*!< IV */
99   uint32_t CRYP_K0LR;
100   uint32_t CRYP_K0RR;
101   uint32_t CRYP_K1LR;
102   uint32_t CRYP_K1RR;
103   uint32_t CRYP_K2LR;
104   uint32_t CRYP_K2RR;
105   uint32_t CRYP_K3LR;
106   uint32_t CRYP_K3RR;
107 }CRYP_Context;
108
109
110 /* Exported constants --------------------------------------------------------*/
111
112 /** @defgroup CRYP_Exported_Constants
113   * @{
114   */
115
116 /** @defgroup CRYP_Algorithm_Direction 
117   * @{
118   */
119 #define CRYP_AlgoDir_Encrypt      ((uint16_t)0x0000)
120 #define CRYP_AlgoDir_Decrypt      ((uint16_t)0x0004)
121 #define IS_CRYP_ALGODIR(ALGODIR) (((ALGODIR) == CRYP_AlgoDir_Encrypt) || \
122                                   ((ALGODIR) == CRYP_AlgoDir_Decrypt))
123
124 /**
125   * @}
126   */ 
127  
128 /** @defgroup CRYP_Algorithm_Mode 
129   * @{
130   */
131
132 /*!< TDES Modes */
133 #define CRYP_AlgoMode_TDES_ECB    ((uint16_t)0x0000)
134 #define CRYP_AlgoMode_TDES_CBC    ((uint16_t)0x0008)
135
136 /*!< DES Modes */
137 #define CRYP_AlgoMode_DES_ECB     ((uint16_t)0x0010)
138 #define CRYP_AlgoMode_DES_CBC     ((uint16_t)0x0018)
139
140 /*!< AES Modes */
141 #define CRYP_AlgoMode_AES_ECB     ((uint16_t)0x0020)
142 #define CRYP_AlgoMode_AES_CBC     ((uint16_t)0x0028)
143 #define CRYP_AlgoMode_AES_CTR     ((uint16_t)0x0030)
144 #define CRYP_AlgoMode_AES_Key     ((uint16_t)0x0038)
145
146 #define IS_CRYP_ALGOMODE(ALGOMODE) (((ALGOMODE) == CRYP_AlgoMode_TDES_ECB) || \
147                                    ((ALGOMODE) == CRYP_AlgoMode_TDES_CBC)|| \
148                                    ((ALGOMODE) == CRYP_AlgoMode_DES_ECB)|| \
149                                    ((ALGOMODE) == CRYP_AlgoMode_DES_CBC) || \
150                                    ((ALGOMODE) == CRYP_AlgoMode_AES_ECB) || \
151                                    ((ALGOMODE) == CRYP_AlgoMode_AES_CBC) || \
152                                    ((ALGOMODE) == CRYP_AlgoMode_AES_CTR) || \
153                                    ((ALGOMODE) == CRYP_AlgoMode_AES_Key))
154 /**
155   * @}
156   */ 
157  
158 /** @defgroup CRYP_Data_Type 
159   * @{
160   */
161 #define CRYP_DataType_32b         ((uint16_t)0x0000)
162 #define CRYP_DataType_16b         ((uint16_t)0x0040)
163 #define CRYP_DataType_8b          ((uint16_t)0x0080)
164 #define CRYP_DataType_1b          ((uint16_t)0x00C0)
165 #define IS_CRYP_DATATYPE(DATATYPE) (((DATATYPE) == CRYP_DataType_32b) || \
166                                     ((DATATYPE) == CRYP_DataType_16b)|| \
167                                     ((DATATYPE) == CRYP_DataType_8b)|| \
168                                     ((DATATYPE) == CRYP_DataType_1b))  
169 /**
170   * @}
171   */
172                                      
173 /** @defgroup CRYP_Key_Size_for_AES_only 
174   * @{
175   */
176 #define CRYP_KeySize_128b         ((uint16_t)0x0000)
177 #define CRYP_KeySize_192b         ((uint16_t)0x0100)
178 #define CRYP_KeySize_256b         ((uint16_t)0x0200)
179 #define IS_CRYP_KEYSIZE(KEYSIZE) (((KEYSIZE) == CRYP_KeySize_128b)|| \
180                                   ((KEYSIZE) == CRYP_KeySize_192b)|| \
181                                   ((KEYSIZE) == CRYP_KeySize_256b))
182 /**
183   * @}
184   */
185
186 /** @defgroup CRYP_flags_definition 
187   * @{
188   */
189 #define CRYP_FLAG_BUSY            ((uint8_t)0x10)  /*!< The CRYP core is currently 
190                                                         processing a block of data 
191                                                         or a key preparation (for 
192                                                         AES decryption). */
193 #define CRYP_FLAG_IFEM            ((uint8_t)0x01)  /*!< Input Fifo Empty */
194 #define CRYP_FLAG_IFNF            ((uint8_t)0x02)  /*!< Input Fifo is Not Full */
195 #define CRYP_FLAG_INRIS           ((uint8_t)0x22)  /*!< Raw interrupt pending */
196 #define CRYP_FLAG_OFNE            ((uint8_t)0x04)  /*!< Input Fifo service raw 
197                                                         interrupt status */
198 #define CRYP_FLAG_OFFU            ((uint8_t)0x08)  /*!< Output Fifo is Full */
199 #define CRYP_FLAG_OUTRIS          ((uint8_t)0x21)  /*!< Output Fifo service raw 
200                                                         interrupt status */
201
202 #define IS_CRYP_GET_FLAG(FLAG) (((FLAG) == CRYP_FLAG_IFEM)  || \
203                                 ((FLAG) == CRYP_FLAG_IFNF)  || \
204                                 ((FLAG) == CRYP_FLAG_OFNE)  || \
205                                 ((FLAG) == CRYP_FLAG_OFFU)  || \
206                                 ((FLAG) == CRYP_FLAG_BUSY)  || \
207                                 ((FLAG) == CRYP_FLAG_OUTRIS)|| \
208                                 ((FLAG) == CRYP_FLAG_INRIS))
209 /**
210   * @}
211   */
212
213 /** @defgroup CRYP_interrupts_definition 
214   * @{
215   */
216 #define CRYP_IT_INI               ((uint8_t)0x01) /*!< IN Fifo Interrupt */
217 #define CRYP_IT_OUTI              ((uint8_t)0x02) /*!< OUT Fifo Interrupt */
218 #define IS_CRYP_CONFIG_IT(IT) ((((IT) & (uint8_t)0xFC) == 0x00) && ((IT) != 0x00))
219 #define IS_CRYP_GET_IT(IT) (((IT) == CRYP_IT_INI) || ((IT) == CRYP_IT_OUTI))
220
221 /**
222   * @}
223   */
224
225 /** @defgroup CRYP_Encryption_Decryption_modes_definition 
226   * @{
227   */
228 #define MODE_ENCRYPT             ((uint8_t)0x01)
229 #define MODE_DECRYPT             ((uint8_t)0x00)
230
231 /**
232   * @}
233   */
234
235 /** @defgroup CRYP_DMA_transfer_requests 
236   * @{
237   */
238 #define CRYP_DMAReq_DataIN             ((uint8_t)0x01)
239 #define CRYP_DMAReq_DataOUT            ((uint8_t)0x02)
240 #define IS_CRYP_DMAREQ(DMAREQ) ((((DMAREQ) & (uint8_t)0xFC) == 0x00) && ((DMAREQ) != 0x00))
241 /**
242   * @}
243   */ 
244
245 /**
246   * @}
247   */ 
248
249 /* Exported macro ------------------------------------------------------------*/
250 /* Exported functions --------------------------------------------------------*/
251
252 /*  Function used to set the CRYP configuration to the default reset state ****/
253 void CRYP_DeInit(void);
254
255 /* CRYP Initialization and Configuration functions ****************************/
256 void CRYP_Init(CRYP_InitTypeDef* CRYP_InitStruct);
257 void CRYP_StructInit(CRYP_InitTypeDef* CRYP_InitStruct);
258 void CRYP_KeyInit(CRYP_KeyInitTypeDef* CRYP_KeyInitStruct);
259 void CRYP_KeyStructInit(CRYP_KeyInitTypeDef* CRYP_KeyInitStruct);
260 void CRYP_IVInit(CRYP_IVInitTypeDef* CRYP_IVInitStruct);
261 void CRYP_IVStructInit(CRYP_IVInitTypeDef* CRYP_IVInitStruct);
262 void CRYP_Cmd(FunctionalState NewState);
263
264 /* CRYP Data processing functions *********************************************/
265 void CRYP_DataIn(uint32_t Data);
266 uint32_t CRYP_DataOut(void);
267 void CRYP_FIFOFlush(void);
268
269 /* CRYP Context swapping functions ********************************************/
270 ErrorStatus CRYP_SaveContext(CRYP_Context* CRYP_ContextSave,
271                              CRYP_KeyInitTypeDef* CRYP_KeyInitStruct);
272 void CRYP_RestoreContext(CRYP_Context* CRYP_ContextRestore);
273
274 /* CRYP's DMA interface function **********************************************/
275 void CRYP_DMACmd(uint8_t CRYP_DMAReq, FunctionalState NewState);
276
277 /* Interrupts and flags management functions **********************************/
278 void CRYP_ITConfig(uint8_t CRYP_IT, FunctionalState NewState);
279 ITStatus CRYP_GetITStatus(uint8_t CRYP_IT);
280 FlagStatus CRYP_GetFlagStatus(uint8_t CRYP_FLAG);
281
282 /* High Level AES functions **************************************************/
283 ErrorStatus CRYP_AES_ECB(uint8_t Mode,
284                          uint8_t *Key, uint16_t Keysize,
285                          uint8_t *Input, uint32_t Ilength,
286                          uint8_t *Output);
287
288 ErrorStatus CRYP_AES_CBC(uint8_t Mode,
289                          uint8_t InitVectors[16],
290                          uint8_t *Key, uint16_t Keysize,
291                          uint8_t *Input, uint32_t Ilength,
292                          uint8_t *Output);
293
294 ErrorStatus CRYP_AES_CTR(uint8_t Mode,
295                          uint8_t InitVectors[16],
296                          uint8_t *Key, uint16_t Keysize,
297                          uint8_t *Input, uint32_t Ilength,
298                          uint8_t *Output);
299
300 /* High Level TDES functions **************************************************/
301 ErrorStatus CRYP_TDES_ECB(uint8_t Mode,
302                            uint8_t Key[24], 
303                            uint8_t *Input, uint32_t Ilength,
304                            uint8_t *Output);
305
306 ErrorStatus CRYP_TDES_CBC(uint8_t Mode,
307                           uint8_t Key[24],
308                           uint8_t InitVectors[8],
309                           uint8_t *Input, uint32_t Ilength,
310                           uint8_t *Output);
311
312 /* High Level DES functions **************************************************/
313 ErrorStatus CRYP_DES_ECB(uint8_t Mode,
314                          uint8_t Key[8],
315                          uint8_t *Input, uint32_t Ilength,
316                          uint8_t *Output);
317
318 ErrorStatus CRYP_DES_CBC(uint8_t Mode,
319                          uint8_t Key[8],
320                          uint8_t InitVectors[8],
321                          uint8_t *Input,uint32_t Ilength,
322                          uint8_t *Output);
323
324 #ifdef __cplusplus
325 }
326 #endif
327
328 #endif /*__STM32F4xx_CRYP_H */
329
330 /**
331   * @}
332   */
333
334 /**
335   * @}
336   */ 
337
338 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/