2 ******************************************************************************
3 * @file stm32f4xx_cryp.h
4 * @author MCD Application Team
7 * @brief This file contains all the functions prototypes for the Cryptographic
8 * processor(CRYP) firmware library.
9 ******************************************************************************
12 * <h2><center>© COPYRIGHT 2016 STMicroelectronics</center></h2>
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:
18 * http://www.st.com/software_license_agreement_liberty_v2
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.
26 ******************************************************************************
29 /* Define to prevent recursive inclusion -------------------------------------*/
30 #ifndef __STM32F4xx_CRYP_H
31 #define __STM32F4xx_CRYP_H
37 /* Includes ------------------------------------------------------------------*/
38 #include "stm32f4xx.h"
40 /** @addtogroup STM32F4xx_StdPeriph_Driver
48 /* Exported types ------------------------------------------------------------*/
51 * @brief CRYP Init structure definition
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 */
68 * @brief CRYP Key(s) structure definition
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 */
82 * @brief CRYP Initialization Vectors (IV) structure definition
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 */
93 * @brief CRYP context swapping structure definition
97 /*!< Current Configuration */
98 uint32_t CR_CurrentConfig
;
113 uint32_t CRYP_CSGCMCCMR
[8];
114 uint32_t CRYP_CSGCMR
[8];
118 /* Exported constants --------------------------------------------------------*/
120 /** @defgroup CRYP_Exported_Constants
124 /** @defgroup CRYP_Algorithm_Direction
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))
136 /** @defgroup CRYP_Algorithm_Mode
141 #define CRYP_AlgoMode_TDES_ECB ((uint32_t)0x00000000)
142 #define CRYP_AlgoMode_TDES_CBC ((uint32_t)0x00000008)
145 #define CRYP_AlgoMode_DES_ECB ((uint32_t)0x00000010)
146 #define CRYP_AlgoMode_DES_CBC ((uint32_t)0x00000018)
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)
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))
170 /** @defgroup CRYP_Phase
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
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))
189 /** @defgroup CRYP_Data_Type
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))
204 /** @defgroup CRYP_Key_Size_for_AES_only
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))
217 /** @defgroup CRYP_flags_definition
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
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
229 #define CRYP_FLAG_OFFU ((uint8_t)0x08) /*!< Output Fifo is Full */
230 #define CRYP_FLAG_OUTRIS ((uint8_t)0x21) /*!< Output Fifo service raw
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))
244 /** @defgroup CRYP_interrupts_definition
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))
256 /** @defgroup CRYP_Encryption_Decryption_modes_definition
259 #define MODE_ENCRYPT ((uint8_t)0x01)
260 #define MODE_DECRYPT ((uint8_t)0x00)
266 /** @defgroup CRYP_DMA_transfer_requests
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))
280 /* Exported macro ------------------------------------------------------------*/
281 /* Exported functions --------------------------------------------------------*/
283 /* Function used to set the CRYP configuration to the default reset state ****/
284 void CRYP_DeInit(void);
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);
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
);
305 /* CRYP DMA interface function ************************************************/
306 void CRYP_DMACmd(uint8_t CRYP_DMAReq
, FunctionalState NewState
);
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
);
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
,
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
,
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
,
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
);
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
,
344 uint8_t* AuthTAG
, uint32_t TAGSize
);
346 /* High Level TDES functions **************************************************/
347 ErrorStatus
CRYP_TDES_ECB(uint8_t Mode
,
349 uint8_t *Input
, uint32_t Ilength
,
352 ErrorStatus
CRYP_TDES_CBC(uint8_t Mode
,
354 uint8_t InitVectors
[8],
355 uint8_t *Input
, uint32_t Ilength
,
358 /* High Level DES functions **************************************************/
359 ErrorStatus
CRYP_DES_ECB(uint8_t Mode
,
361 uint8_t *Input
, uint32_t Ilength
,
364 ErrorStatus
CRYP_DES_CBC(uint8_t Mode
,
366 uint8_t InitVectors
[8],
367 uint8_t *Input
,uint32_t Ilength
,
374 #endif /*__STM32F4xx_CRYP_H */
384 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/