• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2   ******************************************************************************
3   * @file    stm32f4xx_cryp.h
4   * @author  MCD Application Team
5   * @version V1.4.0
6   * @date    04-August-2014
7   * @brief   This file contains all the functions prototypes for the Cryptographic
8   *          processor(CRYP) firmware library.
9   ******************************************************************************
10   * @attention
11   *
12   * <h2><center>&copy; COPYRIGHT 2014 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_CRYP_H
31 #define __STM32F4xx_CRYP_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 CRYP
45   * @{
46   */
47 
48 /* Exported types ------------------------------------------------------------*/
49 
50 /**
51   * @brief   CRYP Init structure definition
52   */
53 typedef struct
54 {
55   uint32_t CRYP_AlgoDir;   /*!< Encrypt or Decrypt. This parameter can be a
56                                 value of @ref CRYP_Algorithm_Direction */
57   uint32_t CRYP_AlgoMode;  /*!< TDES-ECB, TDES-CBC, DES-ECB, DES-CBC, AES-ECB,
58                                 AES-CBC, AES-CTR, AES-Key, AES-GCM and AES-CCM.
59                                 This parameter can be a value of @ref CRYP_Algorithm_Mode */
60   uint32_t CRYP_DataType;  /*!< 32-bit data, 16-bit data, bit data or bit string.
61                                 This parameter can be a value of @ref CRYP_Data_Type */
62   uint32_t CRYP_KeySize;   /*!< Used only in AES mode only : 128, 192 or 256 bit
63                                 key length. This parameter can be a value of
64                                 @ref CRYP_Key_Size_for_AES_only */
65 }CRYP_InitTypeDef;
66 
67 /**
68   * @brief   CRYP Key(s) structure definition
69   */
70 typedef struct
71 {
72   uint32_t CRYP_Key0Left;  /*!< Key 0 Left  */
73   uint32_t CRYP_Key0Right; /*!< Key 0 Right */
74   uint32_t CRYP_Key1Left;  /*!< Key 1 left  */
75   uint32_t CRYP_Key1Right; /*!< Key 1 Right */
76   uint32_t CRYP_Key2Left;  /*!< Key 2 left  */
77   uint32_t CRYP_Key2Right; /*!< Key 2 Right */
78   uint32_t CRYP_Key3Left;  /*!< Key 3 left  */
79   uint32_t CRYP_Key3Right; /*!< Key 3 Right */
80 }CRYP_KeyInitTypeDef;
81 /**
82   * @brief   CRYP Initialization Vectors (IV) structure definition
83   */
84 typedef struct
85 {
86   uint32_t CRYP_IV0Left;  /*!< Init Vector 0 Left  */
87   uint32_t CRYP_IV0Right; /*!< Init Vector 0 Right */
88   uint32_t CRYP_IV1Left;  /*!< Init Vector 1 left  */
89   uint32_t CRYP_IV1Right; /*!< Init Vector 1 Right */
90 }CRYP_IVInitTypeDef;
91 
92 /**
93   * @brief  CRYP context swapping structure definition
94   */
95 typedef struct
96 {
97   /*!< Current Configuration */
98   uint32_t CR_CurrentConfig;
99   /*!< IV */
100   uint32_t CRYP_IV0LR;
101   uint32_t CRYP_IV0RR;
102   uint32_t CRYP_IV1LR;
103   uint32_t CRYP_IV1RR;
104   /*!< KEY */
105   uint32_t CRYP_K0LR;
106   uint32_t CRYP_K0RR;
107   uint32_t CRYP_K1LR;
108   uint32_t CRYP_K1RR;
109   uint32_t CRYP_K2LR;
110   uint32_t CRYP_K2RR;
111   uint32_t CRYP_K3LR;
112   uint32_t CRYP_K3RR;
113   uint32_t CRYP_CSGCMCCMR[8];
114   uint32_t CRYP_CSGCMR[8];
115 }CRYP_Context;
116 
117 
118 /* Exported constants --------------------------------------------------------*/
119 
120 /** @defgroup CRYP_Exported_Constants
121   * @{
122   */
123 
124 /** @defgroup CRYP_Algorithm_Direction
125   * @{
126   */
127 #define CRYP_AlgoDir_Encrypt      ((uint16_t)0x0000)
128 #define CRYP_AlgoDir_Decrypt      ((uint16_t)0x0004)
129 #define IS_CRYP_ALGODIR(ALGODIR) (((ALGODIR) == CRYP_AlgoDir_Encrypt) || \
130                                   ((ALGODIR) == CRYP_AlgoDir_Decrypt))
131 
132 /**
133   * @}
134   */
135 
136 /** @defgroup CRYP_Algorithm_Mode
137   * @{
138   */
139 
140 /*!< TDES Modes */
141 #define CRYP_AlgoMode_TDES_ECB    ((uint32_t)0x00000000)
142 #define CRYP_AlgoMode_TDES_CBC    ((uint32_t)0x00000008)
143 
144 /*!< DES Modes */
145 #define CRYP_AlgoMode_DES_ECB     ((uint32_t)0x00000010)
146 #define CRYP_AlgoMode_DES_CBC     ((uint32_t)0x00000018)
147 
148 /*!< AES Modes */
149 #define CRYP_AlgoMode_AES_ECB     ((uint32_t)0x00000020)
150 #define CRYP_AlgoMode_AES_CBC     ((uint32_t)0x00000028)
151 #define CRYP_AlgoMode_AES_CTR     ((uint32_t)0x00000030)
152 #define CRYP_AlgoMode_AES_Key     ((uint32_t)0x00000038)
153 #define CRYP_AlgoMode_AES_GCM     ((uint32_t)0x00080000)
154 #define CRYP_AlgoMode_AES_CCM     ((uint32_t)0x00080008)
155 
156 #define IS_CRYP_ALGOMODE(ALGOMODE) (((ALGOMODE) == CRYP_AlgoMode_TDES_ECB) || \
157                                    ((ALGOMODE) == CRYP_AlgoMode_TDES_CBC)|| \
158                                    ((ALGOMODE) == CRYP_AlgoMode_DES_ECB) || \
159                                    ((ALGOMODE) == CRYP_AlgoMode_DES_CBC) || \
160                                    ((ALGOMODE) == CRYP_AlgoMode_AES_ECB) || \
161                                    ((ALGOMODE) == CRYP_AlgoMode_AES_CBC) || \
162                                    ((ALGOMODE) == CRYP_AlgoMode_AES_CTR) || \
163                                    ((ALGOMODE) == CRYP_AlgoMode_AES_Key) || \
164                                    ((ALGOMODE) == CRYP_AlgoMode_AES_GCM) || \
165                                    ((ALGOMODE) == CRYP_AlgoMode_AES_CCM))
166 /**
167   * @}
168   */
169 
170 /** @defgroup CRYP_Phase
171   * @{
172   */
173 
174 /*!< The phases are valid only for AES-GCM and AES-CCM modes */
175 #define CRYP_Phase_Init           ((uint32_t)0x00000000)
176 #define CRYP_Phase_Header         CRYP_CR_GCM_CCMPH_0
177 #define CRYP_Phase_Payload        CRYP_CR_GCM_CCMPH_1
178 #define CRYP_Phase_Final          CRYP_CR_GCM_CCMPH
179 
180 #define IS_CRYP_PHASE(PHASE) (((PHASE) == CRYP_Phase_Init)    || \
181                               ((PHASE) == CRYP_Phase_Header)  || \
182                               ((PHASE) == CRYP_Phase_Payload) || \
183                               ((PHASE) == CRYP_Phase_Final))
184 
185 /**
186   * @}
187   */
188 
189 /** @defgroup CRYP_Data_Type
190   * @{
191   */
192 #define CRYP_DataType_32b         ((uint16_t)0x0000)
193 #define CRYP_DataType_16b         ((uint16_t)0x0040)
194 #define CRYP_DataType_8b          ((uint16_t)0x0080)
195 #define CRYP_DataType_1b          ((uint16_t)0x00C0)
196 #define IS_CRYP_DATATYPE(DATATYPE) (((DATATYPE) == CRYP_DataType_32b) || \
197                                     ((DATATYPE) == CRYP_DataType_16b)|| \
198                                     ((DATATYPE) == CRYP_DataType_8b)|| \
199                                     ((DATATYPE) == CRYP_DataType_1b))
200 /**
201   * @}
202   */
203 
204 /** @defgroup CRYP_Key_Size_for_AES_only
205   * @{
206   */
207 #define CRYP_KeySize_128b         ((uint16_t)0x0000)
208 #define CRYP_KeySize_192b         ((uint16_t)0x0100)
209 #define CRYP_KeySize_256b         ((uint16_t)0x0200)
210 #define IS_CRYP_KEYSIZE(KEYSIZE) (((KEYSIZE) == CRYP_KeySize_128b)|| \
211                                   ((KEYSIZE) == CRYP_KeySize_192b)|| \
212                                   ((KEYSIZE) == CRYP_KeySize_256b))
213 /**
214   * @}
215   */
216 
217 /** @defgroup CRYP_flags_definition
218   * @{
219   */
220 #define CRYP_FLAG_BUSY            ((uint8_t)0x10)  /*!< The CRYP core is currently
221                                                         processing a block of data
222                                                         or a key preparation (for
223                                                         AES decryption). */
224 #define CRYP_FLAG_IFEM            ((uint8_t)0x01)  /*!< Input Fifo Empty */
225 #define CRYP_FLAG_IFNF            ((uint8_t)0x02)  /*!< Input Fifo is Not Full */
226 #define CRYP_FLAG_INRIS           ((uint8_t)0x22)  /*!< Raw interrupt pending */
227 #define CRYP_FLAG_OFNE            ((uint8_t)0x04)  /*!< Input Fifo service raw
228                                                         interrupt status */
229 #define CRYP_FLAG_OFFU            ((uint8_t)0x08)  /*!< Output Fifo is Full */
230 #define CRYP_FLAG_OUTRIS          ((uint8_t)0x21)  /*!< Output Fifo service raw
231                                                         interrupt status */
232 
233 #define IS_CRYP_GET_FLAG(FLAG) (((FLAG) == CRYP_FLAG_IFEM)  || \
234                                 ((FLAG) == CRYP_FLAG_IFNF)  || \
235                                 ((FLAG) == CRYP_FLAG_OFNE)  || \
236                                 ((FLAG) == CRYP_FLAG_OFFU)  || \
237                                 ((FLAG) == CRYP_FLAG_BUSY)  || \
238                                 ((FLAG) == CRYP_FLAG_OUTRIS)|| \
239                                 ((FLAG) == CRYP_FLAG_INRIS))
240 /**
241   * @}
242   */
243 
244 /** @defgroup CRYP_interrupts_definition
245   * @{
246   */
247 #define CRYP_IT_INI               ((uint8_t)0x01) /*!< IN Fifo Interrupt */
248 #define CRYP_IT_OUTI              ((uint8_t)0x02) /*!< OUT Fifo Interrupt */
249 #define IS_CRYP_CONFIG_IT(IT) ((((IT) & (uint8_t)0xFC) == 0x00) && ((IT) != 0x00))
250 #define IS_CRYP_GET_IT(IT) (((IT) == CRYP_IT_INI) || ((IT) == CRYP_IT_OUTI))
251 
252 /**
253   * @}
254   */
255 
256 /** @defgroup CRYP_Encryption_Decryption_modes_definition
257   * @{
258   */
259 #define MODE_ENCRYPT             ((uint8_t)0x01)
260 #define MODE_DECRYPT             ((uint8_t)0x00)
261 
262 /**
263   * @}
264   */
265 
266 /** @defgroup CRYP_DMA_transfer_requests
267   * @{
268   */
269 #define CRYP_DMAReq_DataIN             ((uint8_t)0x01)
270 #define CRYP_DMAReq_DataOUT            ((uint8_t)0x02)
271 #define IS_CRYP_DMAREQ(DMAREQ) ((((DMAREQ) & (uint8_t)0xFC) == 0x00) && ((DMAREQ) != 0x00))
272 /**
273   * @}
274   */
275 
276 /**
277   * @}
278   */
279 
280 /* Exported macro ------------------------------------------------------------*/
281 /* Exported functions --------------------------------------------------------*/
282 
283 /*  Function used to set the CRYP configuration to the default reset state ****/
284 void CRYP_DeInit(void);
285 
286 /* CRYP Initialization and Configuration functions ****************************/
287 void CRYP_Init(CRYP_InitTypeDef* CRYP_InitStruct);
288 void CRYP_StructInit(CRYP_InitTypeDef* CRYP_InitStruct);
289 void CRYP_KeyInit(CRYP_KeyInitTypeDef* CRYP_KeyInitStruct);
290 void CRYP_KeyStructInit(CRYP_KeyInitTypeDef* CRYP_KeyInitStruct);
291 void CRYP_IVInit(CRYP_IVInitTypeDef* CRYP_IVInitStruct);
292 void CRYP_IVStructInit(CRYP_IVInitTypeDef* CRYP_IVInitStruct);
293 void CRYP_Cmd(FunctionalState NewState);
294 void CRYP_PhaseConfig(uint32_t CRYP_Phase);
295 void CRYP_FIFOFlush(void);
296 /* CRYP Data processing functions *********************************************/
297 void CRYP_DataIn(uint32_t Data);
298 uint32_t CRYP_DataOut(void);
299 
300 /* CRYP Context swapping functions ********************************************/
301 ErrorStatus CRYP_SaveContext(CRYP_Context* CRYP_ContextSave,
302                              CRYP_KeyInitTypeDef* CRYP_KeyInitStruct);
303 void CRYP_RestoreContext(CRYP_Context* CRYP_ContextRestore);
304 
305 /* CRYP DMA interface function ************************************************/
306 void CRYP_DMACmd(uint8_t CRYP_DMAReq, FunctionalState NewState);
307 
308 /* Interrupts and flags management functions **********************************/
309 void CRYP_ITConfig(uint8_t CRYP_IT, FunctionalState NewState);
310 ITStatus CRYP_GetITStatus(uint8_t CRYP_IT);
311 FunctionalState CRYP_GetCmdStatus(void);
312 FlagStatus CRYP_GetFlagStatus(uint8_t CRYP_FLAG);
313 
314 /* High Level AES functions **************************************************/
315 ErrorStatus CRYP_AES_ECB(uint8_t Mode,
316                          uint8_t *Key, uint16_t Keysize,
317                          uint8_t *Input, uint32_t Ilength,
318                          uint8_t *Output);
319 
320 ErrorStatus CRYP_AES_CBC(uint8_t Mode,
321                          uint8_t InitVectors[16],
322                          uint8_t *Key, uint16_t Keysize,
323                          uint8_t *Input, uint32_t Ilength,
324                          uint8_t *Output);
325 
326 ErrorStatus CRYP_AES_CTR(uint8_t Mode,
327                          uint8_t InitVectors[16],
328                          uint8_t *Key, uint16_t Keysize,
329                          uint8_t *Input, uint32_t Ilength,
330                          uint8_t *Output);
331 
332 ErrorStatus CRYP_AES_GCM(uint8_t Mode, uint8_t InitVectors[16],
333                          uint8_t *Key, uint16_t Keysize,
334                          uint8_t *Input, uint32_t ILength,
335                          uint8_t *Header, uint32_t HLength,
336                          uint8_t *Output, uint8_t *AuthTAG);
337 
338 ErrorStatus CRYP_AES_CCM(uint8_t Mode,
339                          uint8_t* Nonce, uint32_t NonceSize,
340                          uint8_t* Key, uint16_t Keysize,
341                          uint8_t* Input, uint32_t ILength,
342                          uint8_t* Header, uint32_t HLength, uint8_t *HBuffer,
343                          uint8_t* Output,
344                          uint8_t* AuthTAG, uint32_t TAGSize);
345 
346 /* High Level TDES functions **************************************************/
347 ErrorStatus CRYP_TDES_ECB(uint8_t Mode,
348                            uint8_t Key[24],
349                            uint8_t *Input, uint32_t Ilength,
350                            uint8_t *Output);
351 
352 ErrorStatus CRYP_TDES_CBC(uint8_t Mode,
353                           uint8_t Key[24],
354                           uint8_t InitVectors[8],
355                           uint8_t *Input, uint32_t Ilength,
356                           uint8_t *Output);
357 
358 /* High Level DES functions **************************************************/
359 ErrorStatus CRYP_DES_ECB(uint8_t Mode,
360                          uint8_t Key[8],
361                          uint8_t *Input, uint32_t Ilength,
362                          uint8_t *Output);
363 
364 ErrorStatus CRYP_DES_CBC(uint8_t Mode,
365                          uint8_t Key[8],
366                          uint8_t InitVectors[8],
367                          uint8_t *Input,uint32_t Ilength,
368                          uint8_t *Output);
369 
370 #ifdef __cplusplus
371 }
372 #endif
373 
374 #endif /*__STM32F4xx_CRYP_H */
375 
376 /**
377   * @}
378   */
379 
380 /**
381   * @}
382   */
383 
384 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
385