Create release.yml
[betaflight.git] / lib / main / STM32F4 / Drivers / STM32F4xx_StdPeriph_Driver / src / stm32f4xx_cryp_tdes.c
blob93e06a45e0d6a5ff67497e88871cad0d226cb91e
1 /**
2 ******************************************************************************
3 * @file stm32f4xx_cryp_tdes.c
4 * @author MCD Application Team
5 * @version V1.7.1
6 * @date 20-May-2016
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
10 * peripheral.
12 @verbatim
14 ===============================================================================
15 ##### How to use this driver #####
16 ===============================================================================
17 [..]
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.
25 @endverbatim
27 ******************************************************************************
28 * @attention
30 * <h2><center>&copy; 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 ******************************************************************************
45 */
47 /* Includes ------------------------------------------------------------------*/
48 #include "stm32f4xx_cryp.h"
51 /** @addtogroup STM32F4xx_StdPeriph_Driver
52 * @{
55 /** @defgroup CRYP
56 * @brief CRYP driver modules
57 * @{
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
71 * @{
72 */
74 /** @defgroup CRYP_Group7 High Level TDES functions
75 * @brief High Level TDES functions
77 @verbatim
78 ===============================================================================
79 ##### High Level TDES functions #####
80 ===============================================================================
82 @endverbatim
83 * @{
86 /**
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;
111 uint32_t i = 0;
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));
132 keyaddr+=4;
133 TDES_CRYP_KeyInitStructure.CRYP_Key1Right= __REV(*(uint32_t*)(keyaddr));
134 keyaddr+=4;
135 TDES_CRYP_KeyInitStructure.CRYP_Key2Left = __REV(*(uint32_t*)(keyaddr));
136 keyaddr+=4;
137 TDES_CRYP_KeyInitStructure.CRYP_Key2Right= __REV(*(uint32_t*)(keyaddr));
138 keyaddr+=4;
139 TDES_CRYP_KeyInitStructure.CRYP_Key3Left = __REV(*(uint32_t*)(keyaddr));
140 keyaddr+=4;
141 TDES_CRYP_KeyInitStructure.CRYP_Key3Right= __REV(*(uint32_t*)(keyaddr));
142 CRYP_KeyInit(& TDES_CRYP_KeyInitStructure);
144 /* Flush IN/OUT FIFO */
145 CRYP_FIFOFlush();
147 /* Enable Crypto processor */
148 CRYP_Cmd(ENABLE);
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. */
154 return(ERROR);
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));
160 inputaddr+=4;
161 CRYP_DataIn(*(uint32_t*)(inputaddr));
162 inputaddr+=4;
164 /* Wait until the complete message has been processed */
165 counter = 0;
168 busystatus = CRYP_GetFlagStatus(CRYP_FLAG_BUSY);
169 counter++;
170 }while ((counter != TDESBUSY_TIMEOUT) && (busystatus != RESET));
172 if (busystatus != RESET)
174 status = ERROR;
176 else
179 /* Read the Output block from the Output FIFO */
180 *(uint32_t*)(outputaddr) = CRYP_DataOut();
181 outputaddr+=4;
182 *(uint32_t*)(outputaddr) = CRYP_DataOut();
183 outputaddr+=4;
187 /* Disable Crypto */
188 CRYP_Cmd(DISABLE);
190 return status;
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;
221 uint32_t i = 0;
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;
231 else
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));
242 keyaddr+=4;
243 TDES_CRYP_KeyInitStructure.CRYP_Key1Right= __REV(*(uint32_t*)(keyaddr));
244 keyaddr+=4;
245 TDES_CRYP_KeyInitStructure.CRYP_Key2Left = __REV(*(uint32_t*)(keyaddr));
246 keyaddr+=4;
247 TDES_CRYP_KeyInitStructure.CRYP_Key2Right= __REV(*(uint32_t*)(keyaddr));
248 keyaddr+=4;
249 TDES_CRYP_KeyInitStructure.CRYP_Key3Left = __REV(*(uint32_t*)(keyaddr));
250 keyaddr+=4;
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));
256 ivaddr+=4;
257 TDES_CRYP_IVInitStructure.CRYP_IV0Right= __REV(*(uint32_t*)(ivaddr));
258 CRYP_IVInit(&TDES_CRYP_IVInitStructure);
260 /* Flush IN/OUT FIFO */
261 CRYP_FIFOFlush();
263 /* Enable Crypto processor */
264 CRYP_Cmd(ENABLE);
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. */
270 return(ERROR);
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));
277 inputaddr+=4;
278 CRYP_DataIn(*(uint32_t*)(inputaddr));
279 inputaddr+=4;
281 /* Wait until the complete message has been processed */
282 counter = 0;
285 busystatus = CRYP_GetFlagStatus(CRYP_FLAG_BUSY);
286 counter++;
287 }while ((counter != TDESBUSY_TIMEOUT) && (busystatus != RESET));
289 if (busystatus != RESET)
291 status = ERROR;
293 else
296 /* Read the Output block from the Output FIFO */
297 *(uint32_t*)(outputaddr) = CRYP_DataOut();
298 outputaddr+=4;
299 *(uint32_t*)(outputaddr) = CRYP_DataOut();
300 outputaddr+=4;
304 /* Disable Crypto */
305 CRYP_Cmd(DISABLE);
307 return status;
310 * @}
314 * @}
318 * @}
322 * @}
325 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/