Create release.yml
[betaflight.git] / lib / main / STM32F4 / Drivers / STM32F4xx_StdPeriph_Driver / src / stm32f4xx_cryp_des.c
blob0fd815b759ee152961e58c2d10fd09c5aa3c9480
1 /**
2 ******************************************************************************
3 * @file stm32f4xx_cryp_des.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 DES 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 DES in ECB Mode using CRYP_DES_ECB() function.
23 (#) Encrypt and decrypt using DES in CBC Mode using CRYP_DES_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 ******************************************************************************
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 DESBUSY_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_Group8 High Level DES functions
75 * @brief High Level DES functions
77 @verbatim
78 ===============================================================================
79 ##### High Level DES functions #####
80 ===============================================================================
81 @endverbatim
82 * @{
85 /**
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;
110 uint32_t i = 0;
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));
131 keyaddr+=4;
132 DES_CRYP_KeyInitStructure.CRYP_Key1Right= __REV(*(uint32_t*)(keyaddr));
133 CRYP_KeyInit(& DES_CRYP_KeyInitStructure);
135 /* Flush IN/OUT FIFO */
136 CRYP_FIFOFlush();
138 /* Enable Crypto processor */
139 CRYP_Cmd(ENABLE);
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. */
145 return(ERROR);
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));
152 inputaddr+=4;
153 CRYP_DataIn(*(uint32_t*)(inputaddr));
154 inputaddr+=4;
156 /* Wait until the complete message has been processed */
157 counter = 0;
160 busystatus = CRYP_GetFlagStatus(CRYP_FLAG_BUSY);
161 counter++;
162 }while ((counter != DESBUSY_TIMEOUT) && (busystatus != RESET));
164 if (busystatus != RESET)
166 status = ERROR;
168 else
171 /* Read the Output block from the Output FIFO */
172 *(uint32_t*)(outputaddr) = CRYP_DataOut();
173 outputaddr+=4;
174 *(uint32_t*)(outputaddr) = CRYP_DataOut();
175 outputaddr+=4;
179 /* Disable Crypto */
180 CRYP_Cmd(DISABLE);
182 return status;
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;
213 uint32_t i = 0;
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));
234 keyaddr+=4;
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));
240 ivaddr+=4;
241 DES_CRYP_IVInitStructure.CRYP_IV0Right= __REV(*(uint32_t*)(ivaddr));
242 CRYP_IVInit(&DES_CRYP_IVInitStructure);
244 /* Flush IN/OUT FIFO */
245 CRYP_FIFOFlush();
247 /* Enable Crypto processor */
248 CRYP_Cmd(ENABLE);
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. */
254 return(ERROR);
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));
260 inputaddr+=4;
261 CRYP_DataIn(*(uint32_t*)(inputaddr));
262 inputaddr+=4;
264 /* Wait until the complete message has been processed */
265 counter = 0;
268 busystatus = CRYP_GetFlagStatus(CRYP_FLAG_BUSY);
269 counter++;
270 }while ((counter != DESBUSY_TIMEOUT) && (busystatus != RESET));
272 if (busystatus != RESET)
274 status = ERROR;
276 else
278 /* Read the Output block from the Output FIFO */
279 *(uint32_t*)(outputaddr) = CRYP_DataOut();
280 outputaddr+=4;
281 *(uint32_t*)(outputaddr) = CRYP_DataOut();
282 outputaddr+=4;
286 /* Disable Crypto */
287 CRYP_Cmd(DISABLE);
289 return status;
293 * @}
297 * @}
301 * @}
305 * @}
308 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/