1 /**
2 ******************************************************************************
3 * @file stm32f2xx_cryp_des.c
4 * @author MCD Application Team
5 * @version V1.1.2
6 * @date 05-March-2012
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 stm32f2xx_cryp.c/.h drivers to access the STM32F2xx CRYP
10 * peripheral.
11 *
12 * @verbatim
13 *
14 * ===================================================================
15 * How to use this driver
16 * ===================================================================
17 * 1. Enable The CRYP controller clock using
18 * RCC_AHB2PeriphClockCmd(RCC_AHB2Periph_CRYP, ENABLE); function.
19 *
20 * 2. Encrypt and decrypt using DES in ECB Mode using CRYP_DES_ECB()
21 * function.
22 *
23 * 3. Encrypt and decrypt using DES in CBC Mode using CRYP_DES_CBC()
24 * function.
25 *
26 * @endverbatim
27 *
28 ******************************************************************************
29 * @attention
30 *
31 * <h2><center>© COPYRIGHT 2012 STMicroelectronics</center></h2>
32 *
33 * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
34 * You may not use this file except in compliance with the License.
35 * You may obtain a copy of the License at:
36 *
37 * http://www.st.com/software_license_agreement_liberty_v2
38 *
39 * Unless required by applicable law or agreed to in writing, software
40 * distributed under the License is distributed on an "AS IS" BASIS,
41 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
42 * See the License for the specific language governing permissions and
43 * limitations under the License.
44 *
45 ******************************************************************************
46 */
47
48 /* Includes ------------------------------------------------------------------*/
49 #include "stm32f2xx_cryp.h"
50
51
52 /** @addtogroup STM32F2xx_StdPeriph_Driver
53 * @{
54 */
55
56 /** @defgroup CRYP
57 * @brief CRYP driver modules
58 * @{
59 */
60
61 /* Private typedef -----------------------------------------------------------*/
62 /* Private define ------------------------------------------------------------*/
63 #define DESBUSY_TIMEOUT ((uint32_t) 0x00010000)
64
65 /* Private macro -------------------------------------------------------------*/
66 /* Private variables ---------------------------------------------------------*/
67 /* Private function prototypes -----------------------------------------------*/
68 /* Private functions ---------------------------------------------------------*/
69
70
71 /** @defgroup CRYP_Private_Functions
72 * @{
73 */
74
75 /** @defgroup CRYP_Group8 High Level DES functions
76 * @brief High Level DES functions
77 *
78 @verbatim
79 ===============================================================================
80 High Level DES functions
81 ===============================================================================
82 @endverbatim
83 * @{
84 */
85
86 /**
87 * @brief Encrypt and decrypt using DES 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 DES 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
99 */
CRYP_DES_ECB(uint8_t Mode,uint8_t Key[8],uint8_t * Input,uint32_t Ilength,uint8_t * Output)100 ErrorStatus CRYP_DES_ECB(uint8_t Mode, uint8_t Key[8], uint8_t *Input,
101 uint32_t Ilength, uint8_t *Output)
102 {
103 CRYP_InitTypeDef DES_CRYP_InitStructure;
104 CRYP_KeyInitTypeDef DES_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;
111 uint32_t i = 0;
112
113 /* Crypto structures initialisation*/
114 CRYP_KeyStructInit(&DES_CRYP_KeyInitStructure);
115
116 /* Crypto Init for Encryption process */
117 if( Mode == MODE_ENCRYPT ) /* DES encryption */
118 {
119 DES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Encrypt;
120 }
121 else/* if( Mode == MODE_DECRYPT )*/ /* DES decryption */
122 {
123 DES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Decrypt;
124 }
125
126 DES_CRYP_InitStructure.CRYP_AlgoMode = CRYP_AlgoMode_DES_ECB;
127 DES_CRYP_InitStructure.CRYP_DataType = CRYP_DataType_8b;
128 CRYP_Init(&DES_CRYP_InitStructure);
129
130 /* Key Initialisation */
131 DES_CRYP_KeyInitStructure.CRYP_Key1Left = __REV(*(uint32_t*)(keyaddr));
132 keyaddr+=4;
133 DES_CRYP_KeyInitStructure.CRYP_Key1Right= __REV(*(uint32_t*)(keyaddr));
134 CRYP_KeyInit(& DES_CRYP_KeyInitStructure);
135
136 /* Flush IN/OUT FIFO */
137 CRYP_FIFOFlush();
138
139 /* Enable Crypto processor */
140 CRYP_Cmd(ENABLE);
141
142 for(i=0; ((i<Ilength) && (status != ERROR)); i+=8)
143 {
144
145 /* Write the Input block in the Input FIFO */
146 CRYP_DataIn(*(uint32_t*)(inputaddr));
147 inputaddr+=4;
148 CRYP_DataIn(*(uint32_t*)(inputaddr));
149 inputaddr+=4;
150
151 /* Wait until the complete message has been processed */
152 counter = 0;
153 do
154 {
155 busystatus = CRYP_GetFlagStatus(CRYP_FLAG_BUSY);
156 counter++;
157 }while ((counter != DESBUSY_TIMEOUT) && (busystatus != RESET));
158
159 if (busystatus != RESET)
160 {
161 status = ERROR;
162 }
163 else
164 {
165
166 /* Read the Output block from the Output FIFO */
167 *(uint32_t*)(outputaddr) = CRYP_DataOut();
168 outputaddr+=4;
169 *(uint32_t*)(outputaddr) = CRYP_DataOut();
170 outputaddr+=4;
171 }
172 }
173
174 /* Disable Crypto */
175 CRYP_Cmd(DISABLE);
176
177 return status;
178 }
179
180 /**
181 * @brief Encrypt and decrypt using DES in CBC Mode
182 * @param Mode: encryption or decryption Mode.
183 * This parameter can be one of the following values:
184 * @arg MODE_ENCRYPT: Encryption
185 * @arg MODE_DECRYPT: Decryption
186 * @param Key: Key used for DES algorithm.
187 * @param InitVectors: Initialisation Vectors used for DES algorithm.
188 * @param Ilength: length of the Input buffer, must be a multiple of 8.
189 * @param Input: pointer to the Input buffer.
190 * @param Output: pointer to the returned buffer.
191 * @retval An ErrorStatus enumeration value:
192 * - SUCCESS: Operation done
193 * - ERROR: Operation failed
194 */
CRYP_DES_CBC(uint8_t Mode,uint8_t Key[8],uint8_t InitVectors[8],uint8_t * Input,uint32_t Ilength,uint8_t * Output)195 ErrorStatus CRYP_DES_CBC(uint8_t Mode, uint8_t Key[8], uint8_t InitVectors[8],
196 uint8_t *Input, uint32_t Ilength, uint8_t *Output)
197 {
198 CRYP_InitTypeDef DES_CRYP_InitStructure;
199 CRYP_KeyInitTypeDef DES_CRYP_KeyInitStructure;
200 CRYP_IVInitTypeDef DES_CRYP_IVInitStructure;
201 __IO uint32_t counter = 0;
202 uint32_t busystatus = 0;
203 ErrorStatus status = SUCCESS;
204 uint32_t keyaddr = (uint32_t)Key;
205 uint32_t inputaddr = (uint32_t)Input;
206 uint32_t outputaddr = (uint32_t)Output;
207 uint32_t ivaddr = (uint32_t)InitVectors;
208 uint32_t i = 0;
209
210 /* Crypto structures initialisation*/
211 CRYP_KeyStructInit(&DES_CRYP_KeyInitStructure);
212
213 /* Crypto Init for Encryption process */
214 if(Mode == MODE_ENCRYPT) /* DES encryption */
215 {
216 DES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Encrypt;
217 }
218 else /*if(Mode == MODE_DECRYPT)*/ /* DES decryption */
219 {
220 DES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Decrypt;
221 }
222
223 DES_CRYP_InitStructure.CRYP_AlgoMode = CRYP_AlgoMode_DES_CBC;
224 DES_CRYP_InitStructure.CRYP_DataType = CRYP_DataType_8b;
225 CRYP_Init(&DES_CRYP_InitStructure);
226
227 /* Key Initialisation */
228 DES_CRYP_KeyInitStructure.CRYP_Key1Left = __REV(*(uint32_t*)(keyaddr));
229 keyaddr+=4;
230 DES_CRYP_KeyInitStructure.CRYP_Key1Right= __REV(*(uint32_t*)(keyaddr));
231 CRYP_KeyInit(& DES_CRYP_KeyInitStructure);
232
233 /* Initialization Vectors */
234 DES_CRYP_IVInitStructure.CRYP_IV0Left = __REV(*(uint32_t*)(ivaddr));
235 ivaddr+=4;
236 DES_CRYP_IVInitStructure.CRYP_IV0Right= __REV(*(uint32_t*)(ivaddr));
237 CRYP_IVInit(&DES_CRYP_IVInitStructure);
238
239 /* Flush IN/OUT FIFO */
240 CRYP_FIFOFlush();
241
242 /* Enable Crypto processor */
243 CRYP_Cmd(ENABLE);
244
245 for(i=0; ((i<Ilength) && (status != ERROR)); i+=8)
246 {
247 /* Write the Input block in the Input FIFO */
248 CRYP_DataIn(*(uint32_t*)(inputaddr));
249 inputaddr+=4;
250 CRYP_DataIn(*(uint32_t*)(inputaddr));
251 inputaddr+=4;
252
253 /* Wait until the complete message has been processed */
254 counter = 0;
255 do
256 {
257 busystatus = CRYP_GetFlagStatus(CRYP_FLAG_BUSY);
258 counter++;
259 }while ((counter != DESBUSY_TIMEOUT) && (busystatus != RESET));
260
261 if (busystatus != RESET)
262 {
263 status = ERROR;
264 }
265 else
266 {
267 /* Read the Output block from the Output FIFO */
268 *(uint32_t*)(outputaddr) = CRYP_DataOut();
269 outputaddr+=4;
270 *(uint32_t*)(outputaddr) = CRYP_DataOut();
271 outputaddr+=4;
272 }
273 }
274
275 /* Disable Crypto */
276 CRYP_Cmd(DISABLE);
277
278 return status;
279 }
280
281 /**
282 * @}
283 */
284
285 /**
286 * @}
287 */
288
289 /**
290 * @}
291 */
292
293 /**
294 * @}
295 */
296
297 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
298