2 ******************************************************************************
3 * @file stm32f4xx_cryp_tdes.c
4 * @author MCD Application Team
7 * @brief This file provides high level functions to encrypt and decrypt an
8 * input message using TDES in ECB/CBC modes .
9 * It uses the stm32f4xx_cryp.c/.h drivers to access the STM32F4xx CRYP
14 ===============================================================================
15 ##### How to use this driver #####
16 ===============================================================================
18 (#) Enable The CRYP controller clock using
19 RCC_AHB2PeriphClockCmd(RCC_AHB2Periph_CRYP, ENABLE); function.
21 (#) Encrypt and decrypt using TDES in ECB Mode using CRYP_TDES_ECB() function.
23 (#) Encrypt and decrypt using TDES in CBC Mode using CRYP_TDES_CBC() function.
27 ******************************************************************************
30 * <h2><center>© COPYRIGHT 2016 STMicroelectronics</center></h2>
32 * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
33 * You may not use this file except in compliance with the License.
34 * You may obtain a copy of the License at:
36 * http://www.st.com/software_license_agreement_liberty_v2
38 * Unless required by applicable law or agreed to in writing, software
39 * distributed under the License is distributed on an "AS IS" BASIS,
40 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
41 * See the License for the specific language governing permissions and
42 * limitations under the License.
44 ******************************************************************************
47 /* Includes ------------------------------------------------------------------*/
48 #include "stm32f4xx_cryp.h"
51 /** @addtogroup STM32F4xx_StdPeriph_Driver
56 * @brief CRYP driver modules
60 /* Private typedef -----------------------------------------------------------*/
61 /* Private define ------------------------------------------------------------*/
62 #define TDESBUSY_TIMEOUT ((uint32_t) 0x00010000)
64 /* Private macro -------------------------------------------------------------*/
65 /* Private variables ---------------------------------------------------------*/
66 /* Private function prototypes -----------------------------------------------*/
67 /* Private functions ---------------------------------------------------------*/
70 /** @defgroup CRYP_Private_Functions
74 /** @defgroup CRYP_Group7 High Level TDES functions
75 * @brief High Level TDES functions
78 ===============================================================================
79 ##### High Level TDES functions #####
80 ===============================================================================
87 * @brief Encrypt and decrypt using TDES in ECB Mode
88 * @param Mode: encryption or decryption Mode.
89 * This parameter can be one of the following values:
90 * @arg MODE_ENCRYPT: Encryption
91 * @arg MODE_DECRYPT: Decryption
92 * @param Key: Key used for TDES algorithm.
93 * @param Ilength: length of the Input buffer, must be a multiple of 8.
94 * @param Input: pointer to the Input buffer.
95 * @param Output: pointer to the returned buffer.
96 * @retval An ErrorStatus enumeration value:
97 * - SUCCESS: Operation done
98 * - ERROR: Operation failed
100 ErrorStatus
CRYP_TDES_ECB(uint8_t Mode
, uint8_t Key
[24], uint8_t *Input
,
101 uint32_t Ilength
, uint8_t *Output
)
103 CRYP_InitTypeDef TDES_CRYP_InitStructure
;
104 CRYP_KeyInitTypeDef TDES_CRYP_KeyInitStructure
;
105 __IO
uint32_t counter
= 0;
106 uint32_t busystatus
= 0;
107 ErrorStatus status
= SUCCESS
;
108 uint32_t keyaddr
= (uint32_t)Key
;
109 uint32_t inputaddr
= (uint32_t)Input
;
110 uint32_t outputaddr
= (uint32_t)Output
;
113 /* Crypto structures initialisation*/
114 CRYP_KeyStructInit(&TDES_CRYP_KeyInitStructure
);
116 /* Crypto Init for Encryption process */
117 if(Mode
== MODE_ENCRYPT
) /* TDES encryption */
119 TDES_CRYP_InitStructure
.CRYP_AlgoDir
= CRYP_AlgoDir_Encrypt
;
121 else /*if(Mode == MODE_DECRYPT)*/ /* TDES decryption */
123 TDES_CRYP_InitStructure
.CRYP_AlgoDir
= CRYP_AlgoDir_Decrypt
;
126 TDES_CRYP_InitStructure
.CRYP_AlgoMode
= CRYP_AlgoMode_TDES_ECB
;
127 TDES_CRYP_InitStructure
.CRYP_DataType
= CRYP_DataType_8b
;
128 CRYP_Init(&TDES_CRYP_InitStructure
);
130 /* Key Initialisation */
131 TDES_CRYP_KeyInitStructure
.CRYP_Key1Left
= __REV(*(uint32_t*)(keyaddr
));
133 TDES_CRYP_KeyInitStructure
.CRYP_Key1Right
= __REV(*(uint32_t*)(keyaddr
));
135 TDES_CRYP_KeyInitStructure
.CRYP_Key2Left
= __REV(*(uint32_t*)(keyaddr
));
137 TDES_CRYP_KeyInitStructure
.CRYP_Key2Right
= __REV(*(uint32_t*)(keyaddr
));
139 TDES_CRYP_KeyInitStructure
.CRYP_Key3Left
= __REV(*(uint32_t*)(keyaddr
));
141 TDES_CRYP_KeyInitStructure
.CRYP_Key3Right
= __REV(*(uint32_t*)(keyaddr
));
142 CRYP_KeyInit(& TDES_CRYP_KeyInitStructure
);
144 /* Flush IN/OUT FIFO */
147 /* Enable Crypto processor */
150 if(CRYP_GetCmdStatus() == DISABLE
)
152 /* The CRYP peripheral clock is not enabled or the device doesn't embed
153 the CRYP peripheral (please check the device sales type. */
156 for(i
=0; ((i
<Ilength
) && (status
!= ERROR
)); i
+=8)
158 /* Write the Input block in the Input FIFO */
159 CRYP_DataIn(*(uint32_t*)(inputaddr
));
161 CRYP_DataIn(*(uint32_t*)(inputaddr
));
164 /* Wait until the complete message has been processed */
168 busystatus
= CRYP_GetFlagStatus(CRYP_FLAG_BUSY
);
170 }while ((counter
!= TDESBUSY_TIMEOUT
) && (busystatus
!= RESET
));
172 if (busystatus
!= RESET
)
179 /* Read the Output block from the Output FIFO */
180 *(uint32_t*)(outputaddr
) = CRYP_DataOut();
182 *(uint32_t*)(outputaddr
) = CRYP_DataOut();
194 * @brief Encrypt and decrypt using TDES in CBC Mode
195 * @param Mode: encryption or decryption Mode.
196 * This parameter can be one of the following values:
197 * @arg MODE_ENCRYPT: Encryption
198 * @arg MODE_DECRYPT: Decryption
199 * @param Key: Key used for TDES algorithm.
200 * @param InitVectors: Initialisation Vectors used for TDES algorithm.
201 * @param Input: pointer to the Input buffer.
202 * @param Ilength: length of the Input buffer, must be a multiple of 8.
203 * @param Output: pointer to the returned buffer.
204 * @retval An ErrorStatus enumeration value:
205 * - SUCCESS: Operation done
206 * - ERROR: Operation failed
208 ErrorStatus
CRYP_TDES_CBC(uint8_t Mode
, uint8_t Key
[24], uint8_t InitVectors
[8],
209 uint8_t *Input
, uint32_t Ilength
, uint8_t *Output
)
211 CRYP_InitTypeDef TDES_CRYP_InitStructure
;
212 CRYP_KeyInitTypeDef TDES_CRYP_KeyInitStructure
;
213 CRYP_IVInitTypeDef TDES_CRYP_IVInitStructure
;
214 __IO
uint32_t counter
= 0;
215 uint32_t busystatus
= 0;
216 ErrorStatus status
= SUCCESS
;
217 uint32_t keyaddr
= (uint32_t)Key
;
218 uint32_t inputaddr
= (uint32_t)Input
;
219 uint32_t outputaddr
= (uint32_t)Output
;
220 uint32_t ivaddr
= (uint32_t)InitVectors
;
223 /* Crypto structures initialisation*/
224 CRYP_KeyStructInit(&TDES_CRYP_KeyInitStructure
);
226 /* Crypto Init for Encryption process */
227 if(Mode
== MODE_ENCRYPT
) /* TDES encryption */
229 TDES_CRYP_InitStructure
.CRYP_AlgoDir
= CRYP_AlgoDir_Encrypt
;
233 TDES_CRYP_InitStructure
.CRYP_AlgoDir
= CRYP_AlgoDir_Decrypt
;
235 TDES_CRYP_InitStructure
.CRYP_AlgoMode
= CRYP_AlgoMode_TDES_CBC
;
236 TDES_CRYP_InitStructure
.CRYP_DataType
= CRYP_DataType_8b
;
238 CRYP_Init(&TDES_CRYP_InitStructure
);
240 /* Key Initialisation */
241 TDES_CRYP_KeyInitStructure
.CRYP_Key1Left
= __REV(*(uint32_t*)(keyaddr
));
243 TDES_CRYP_KeyInitStructure
.CRYP_Key1Right
= __REV(*(uint32_t*)(keyaddr
));
245 TDES_CRYP_KeyInitStructure
.CRYP_Key2Left
= __REV(*(uint32_t*)(keyaddr
));
247 TDES_CRYP_KeyInitStructure
.CRYP_Key2Right
= __REV(*(uint32_t*)(keyaddr
));
249 TDES_CRYP_KeyInitStructure
.CRYP_Key3Left
= __REV(*(uint32_t*)(keyaddr
));
251 TDES_CRYP_KeyInitStructure
.CRYP_Key3Right
= __REV(*(uint32_t*)(keyaddr
));
252 CRYP_KeyInit(& TDES_CRYP_KeyInitStructure
);
254 /* Initialization Vectors */
255 TDES_CRYP_IVInitStructure
.CRYP_IV0Left
= __REV(*(uint32_t*)(ivaddr
));
257 TDES_CRYP_IVInitStructure
.CRYP_IV0Right
= __REV(*(uint32_t*)(ivaddr
));
258 CRYP_IVInit(&TDES_CRYP_IVInitStructure
);
260 /* Flush IN/OUT FIFO */
263 /* Enable Crypto processor */
266 if(CRYP_GetCmdStatus() == DISABLE
)
268 /* The CRYP peripheral clock is not enabled or the device doesn't embed
269 the CRYP peripheral (please check the device sales type. */
273 for(i
=0; ((i
<Ilength
) && (status
!= ERROR
)); i
+=8)
275 /* Write the Input block in the Input FIFO */
276 CRYP_DataIn(*(uint32_t*)(inputaddr
));
278 CRYP_DataIn(*(uint32_t*)(inputaddr
));
281 /* Wait until the complete message has been processed */
285 busystatus
= CRYP_GetFlagStatus(CRYP_FLAG_BUSY
);
287 }while ((counter
!= TDESBUSY_TIMEOUT
) && (busystatus
!= RESET
));
289 if (busystatus
!= RESET
)
296 /* Read the Output block from the Output FIFO */
297 *(uint32_t*)(outputaddr
) = CRYP_DataOut();
299 *(uint32_t*)(outputaddr
) = CRYP_DataOut();
325 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/