2 ******************************************************************************
3 * @file stm32f4xx_cryp_des.c
4 * @author MCD Application Team
7 * @brief This file provides high level functions to encrypt and decrypt an
8 * input message using DES 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 DES in ECB Mode using CRYP_DES_ECB() function.
23 (#) Encrypt and decrypt using DES in CBC Mode using CRYP_DES_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 DESBUSY_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_Group8 High Level DES functions
75 * @brief High Level DES functions
78 ===============================================================================
79 ##### High Level DES functions #####
80 ===============================================================================
86 * @brief Encrypt and decrypt using DES in ECB Mode
87 * @param Mode: encryption or decryption Mode.
88 * This parameter can be one of the following values:
89 * @arg MODE_ENCRYPT: Encryption
90 * @arg MODE_DECRYPT: Decryption
91 * @param Key: Key used for DES algorithm.
92 * @param Ilength: length of the Input buffer, must be a multiple of 8.
93 * @param Input: pointer to the Input buffer.
94 * @param Output: pointer to the returned buffer.
95 * @retval An ErrorStatus enumeration value:
96 * - SUCCESS: Operation done
97 * - ERROR: Operation failed
99 ErrorStatus
CRYP_DES_ECB(uint8_t Mode
, uint8_t Key
[8], uint8_t *Input
,
100 uint32_t Ilength
, uint8_t *Output
)
102 CRYP_InitTypeDef DES_CRYP_InitStructure
;
103 CRYP_KeyInitTypeDef DES_CRYP_KeyInitStructure
;
104 __IO
uint32_t counter
= 0;
105 uint32_t busystatus
= 0;
106 ErrorStatus status
= SUCCESS
;
107 uint32_t keyaddr
= (uint32_t)Key
;
108 uint32_t inputaddr
= (uint32_t)Input
;
109 uint32_t outputaddr
= (uint32_t)Output
;
112 /* Crypto structures initialisation*/
113 CRYP_KeyStructInit(&DES_CRYP_KeyInitStructure
);
115 /* Crypto Init for Encryption process */
116 if( Mode
== MODE_ENCRYPT
) /* DES encryption */
118 DES_CRYP_InitStructure
.CRYP_AlgoDir
= CRYP_AlgoDir_Encrypt
;
120 else/* if( Mode == MODE_DECRYPT )*/ /* DES decryption */
122 DES_CRYP_InitStructure
.CRYP_AlgoDir
= CRYP_AlgoDir_Decrypt
;
125 DES_CRYP_InitStructure
.CRYP_AlgoMode
= CRYP_AlgoMode_DES_ECB
;
126 DES_CRYP_InitStructure
.CRYP_DataType
= CRYP_DataType_8b
;
127 CRYP_Init(&DES_CRYP_InitStructure
);
129 /* Key Initialisation */
130 DES_CRYP_KeyInitStructure
.CRYP_Key1Left
= __REV(*(uint32_t*)(keyaddr
));
132 DES_CRYP_KeyInitStructure
.CRYP_Key1Right
= __REV(*(uint32_t*)(keyaddr
));
133 CRYP_KeyInit(& DES_CRYP_KeyInitStructure
);
135 /* Flush IN/OUT FIFO */
138 /* Enable Crypto processor */
141 if(CRYP_GetCmdStatus() == DISABLE
)
143 /* The CRYP peripheral clock is not enabled or the device doesn't embed
144 the CRYP peripheral (please check the device sales type. */
147 for(i
=0; ((i
<Ilength
) && (status
!= ERROR
)); i
+=8)
150 /* Write the Input block in the Input FIFO */
151 CRYP_DataIn(*(uint32_t*)(inputaddr
));
153 CRYP_DataIn(*(uint32_t*)(inputaddr
));
156 /* Wait until the complete message has been processed */
160 busystatus
= CRYP_GetFlagStatus(CRYP_FLAG_BUSY
);
162 }while ((counter
!= DESBUSY_TIMEOUT
) && (busystatus
!= RESET
));
164 if (busystatus
!= RESET
)
171 /* Read the Output block from the Output FIFO */
172 *(uint32_t*)(outputaddr
) = CRYP_DataOut();
174 *(uint32_t*)(outputaddr
) = CRYP_DataOut();
186 * @brief Encrypt and decrypt using DES in CBC Mode
187 * @param Mode: encryption or decryption Mode.
188 * This parameter can be one of the following values:
189 * @arg MODE_ENCRYPT: Encryption
190 * @arg MODE_DECRYPT: Decryption
191 * @param Key: Key used for DES algorithm.
192 * @param InitVectors: Initialisation Vectors used for DES algorithm.
193 * @param Ilength: length of the Input buffer, must be a multiple of 8.
194 * @param Input: pointer to the Input buffer.
195 * @param Output: pointer to the returned buffer.
196 * @retval An ErrorStatus enumeration value:
197 * - SUCCESS: Operation done
198 * - ERROR: Operation failed
200 ErrorStatus
CRYP_DES_CBC(uint8_t Mode
, uint8_t Key
[8], uint8_t InitVectors
[8],
201 uint8_t *Input
, uint32_t Ilength
, uint8_t *Output
)
203 CRYP_InitTypeDef DES_CRYP_InitStructure
;
204 CRYP_KeyInitTypeDef DES_CRYP_KeyInitStructure
;
205 CRYP_IVInitTypeDef DES_CRYP_IVInitStructure
;
206 __IO
uint32_t counter
= 0;
207 uint32_t busystatus
= 0;
208 ErrorStatus status
= SUCCESS
;
209 uint32_t keyaddr
= (uint32_t)Key
;
210 uint32_t inputaddr
= (uint32_t)Input
;
211 uint32_t outputaddr
= (uint32_t)Output
;
212 uint32_t ivaddr
= (uint32_t)InitVectors
;
215 /* Crypto structures initialisation*/
216 CRYP_KeyStructInit(&DES_CRYP_KeyInitStructure
);
218 /* Crypto Init for Encryption process */
219 if(Mode
== MODE_ENCRYPT
) /* DES encryption */
221 DES_CRYP_InitStructure
.CRYP_AlgoDir
= CRYP_AlgoDir_Encrypt
;
223 else /*if(Mode == MODE_DECRYPT)*/ /* DES decryption */
225 DES_CRYP_InitStructure
.CRYP_AlgoDir
= CRYP_AlgoDir_Decrypt
;
228 DES_CRYP_InitStructure
.CRYP_AlgoMode
= CRYP_AlgoMode_DES_CBC
;
229 DES_CRYP_InitStructure
.CRYP_DataType
= CRYP_DataType_8b
;
230 CRYP_Init(&DES_CRYP_InitStructure
);
232 /* Key Initialisation */
233 DES_CRYP_KeyInitStructure
.CRYP_Key1Left
= __REV(*(uint32_t*)(keyaddr
));
235 DES_CRYP_KeyInitStructure
.CRYP_Key1Right
= __REV(*(uint32_t*)(keyaddr
));
236 CRYP_KeyInit(& DES_CRYP_KeyInitStructure
);
238 /* Initialization Vectors */
239 DES_CRYP_IVInitStructure
.CRYP_IV0Left
= __REV(*(uint32_t*)(ivaddr
));
241 DES_CRYP_IVInitStructure
.CRYP_IV0Right
= __REV(*(uint32_t*)(ivaddr
));
242 CRYP_IVInit(&DES_CRYP_IVInitStructure
);
244 /* Flush IN/OUT FIFO */
247 /* Enable Crypto processor */
250 if(CRYP_GetCmdStatus() == DISABLE
)
252 /* The CRYP peripheral clock is not enabled or the device doesn't embed
253 the CRYP peripheral (please check the device sales type. */
256 for(i
=0; ((i
<Ilength
) && (status
!= ERROR
)); i
+=8)
258 /* Write the Input block in the Input FIFO */
259 CRYP_DataIn(*(uint32_t*)(inputaddr
));
261 CRYP_DataIn(*(uint32_t*)(inputaddr
));
264 /* Wait until the complete message has been processed */
268 busystatus
= CRYP_GetFlagStatus(CRYP_FLAG_BUSY
);
270 }while ((counter
!= DESBUSY_TIMEOUT
) && (busystatus
!= RESET
));
272 if (busystatus
!= RESET
)
278 /* Read the Output block from the Output FIFO */
279 *(uint32_t*)(outputaddr
) = CRYP_DataOut();
281 *(uint32_t*)(outputaddr
) = CRYP_DataOut();
308 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/