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 * ===================================================================
17 * 1. Enable The CRYP controller clock using
18 * RCC_AHB2PeriphClockCmd(RCC_AHB2Periph_CRYP, ENABLE); function.
20 * 2. Encrypt and decrypt using DES in ECB Mode using CRYP_DES_ECB()
23 * 3. Encrypt and decrypt using DES in CBC Mode using CRYP_DES_CBC()
28 ******************************************************************************
31 * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
32 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
33 * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
34 * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
35 * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
36 * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
38 * <h2><center>© COPYRIGHT 2011 STMicroelectronics</center></h2>
39 ******************************************************************************
42 /* Includes ------------------------------------------------------------------*/
43 #include "stm32f4xx_cryp.h"
46 /** @addtogroup STM32F4xx_StdPeriph_Driver
51 * @brief CRYP driver modules
55 /* Private typedef -----------------------------------------------------------*/
56 /* Private define ------------------------------------------------------------*/
57 #define DESBUSY_TIMEOUT ((uint32_t) 0x00010000)
59 /* Private macro -------------------------------------------------------------*/
60 /* Private variables ---------------------------------------------------------*/
61 /* Private function prototypes -----------------------------------------------*/
62 /* Private functions ---------------------------------------------------------*/
65 /** @defgroup CRYP_Private_Functions
69 /** @defgroup CRYP_Group8 High Level DES functions
70 * @brief High Level DES functions
73 ===============================================================================
74 High Level DES functions
75 ===============================================================================
81 * @brief Encrypt and decrypt using DES in ECB Mode
82 * @param Mode: encryption or decryption Mode.
83 * This parameter can be one of the following values:
84 * @arg MODE_ENCRYPT: Encryption
85 * @arg MODE_DECRYPT: Decryption
86 * @param Key: Key used for DES algorithm.
87 * @param Ilength: length of the Input buffer, must be a multiple of 8.
88 * @param Input: pointer to the Input buffer.
89 * @param Output: pointer to the returned buffer.
90 * @retval An ErrorStatus enumeration value:
91 * - SUCCESS: Operation done
92 * - ERROR: Operation failed
94 ErrorStatus CRYP_DES_ECB(uint8_t Mode, uint8_t Key[8], uint8_t *Input,
95 uint32_t Ilength, uint8_t *Output)
97 CRYP_InitTypeDef DES_CRYP_InitStructure;
98 CRYP_KeyInitTypeDef DES_CRYP_KeyInitStructure;
99 __IO uint32_t counter = 0;
100 uint32_t busystatus = 0;
101 ErrorStatus status = SUCCESS;
102 uint32_t keyaddr = (uint32_t)Key;
103 uint32_t inputaddr = (uint32_t)Input;
104 uint32_t outputaddr = (uint32_t)Output;
107 /* Crypto structures initialisation*/
108 CRYP_KeyStructInit(&DES_CRYP_KeyInitStructure);
110 /* Crypto Init for Encryption process */
111 if( Mode == MODE_ENCRYPT ) /* DES encryption */
113 DES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Encrypt;
115 else/* if( Mode == MODE_DECRYPT )*/ /* DES decryption */
117 DES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Decrypt;
120 DES_CRYP_InitStructure.CRYP_AlgoMode = CRYP_AlgoMode_DES_ECB;
121 DES_CRYP_InitStructure.CRYP_DataType = CRYP_DataType_8b;
122 CRYP_Init(&DES_CRYP_InitStructure);
124 /* Key Initialisation */
125 DES_CRYP_KeyInitStructure.CRYP_Key1Left = __REV(*(uint32_t*)(keyaddr));
127 DES_CRYP_KeyInitStructure.CRYP_Key1Right= __REV(*(uint32_t*)(keyaddr));
128 CRYP_KeyInit(& DES_CRYP_KeyInitStructure);
130 /* Flush IN/OUT FIFO */
133 /* Enable Crypto processor */
136 for(i=0; ((i<Ilength) && (status != ERROR)); i+=8)
139 /* Write the Input block in the Input FIFO */
140 CRYP_DataIn(*(uint32_t*)(inputaddr));
142 CRYP_DataIn(*(uint32_t*)(inputaddr));
145 /* Wait until the complete message has been processed */
149 busystatus = CRYP_GetFlagStatus(CRYP_FLAG_BUSY);
151 }while ((counter != DESBUSY_TIMEOUT) && (busystatus != RESET));
153 if (busystatus != RESET)
160 /* Read the Output block from the Output FIFO */
161 *(uint32_t*)(outputaddr) = CRYP_DataOut();
163 *(uint32_t*)(outputaddr) = CRYP_DataOut();
175 * @brief Encrypt and decrypt using DES in CBC Mode
176 * @param Mode: encryption or decryption Mode.
177 * This parameter can be one of the following values:
178 * @arg MODE_ENCRYPT: Encryption
179 * @arg MODE_DECRYPT: Decryption
180 * @param Key: Key used for DES algorithm.
181 * @param InitVectors: Initialisation Vectors used for DES algorithm.
182 * @param Ilength: length of the Input buffer, must be a multiple of 8.
183 * @param Input: pointer to the Input buffer.
184 * @param Output: pointer to the returned buffer.
185 * @retval An ErrorStatus enumeration value:
186 * - SUCCESS: Operation done
187 * - ERROR: Operation failed
189 ErrorStatus CRYP_DES_CBC(uint8_t Mode, uint8_t Key[8], uint8_t InitVectors[8],
190 uint8_t *Input, uint32_t Ilength, uint8_t *Output)
192 CRYP_InitTypeDef DES_CRYP_InitStructure;
193 CRYP_KeyInitTypeDef DES_CRYP_KeyInitStructure;
194 CRYP_IVInitTypeDef DES_CRYP_IVInitStructure;
195 __IO uint32_t counter = 0;
196 uint32_t busystatus = 0;
197 ErrorStatus status = SUCCESS;
198 uint32_t keyaddr = (uint32_t)Key;
199 uint32_t inputaddr = (uint32_t)Input;
200 uint32_t outputaddr = (uint32_t)Output;
201 uint32_t ivaddr = (uint32_t)InitVectors;
204 /* Crypto structures initialisation*/
205 CRYP_KeyStructInit(&DES_CRYP_KeyInitStructure);
207 /* Crypto Init for Encryption process */
208 if(Mode == MODE_ENCRYPT) /* DES encryption */
210 DES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Encrypt;
212 else /*if(Mode == MODE_DECRYPT)*/ /* DES decryption */
214 DES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Decrypt;
217 DES_CRYP_InitStructure.CRYP_AlgoMode = CRYP_AlgoMode_DES_CBC;
218 DES_CRYP_InitStructure.CRYP_DataType = CRYP_DataType_8b;
219 CRYP_Init(&DES_CRYP_InitStructure);
221 /* Key Initialisation */
222 DES_CRYP_KeyInitStructure.CRYP_Key1Left = __REV(*(uint32_t*)(keyaddr));
224 DES_CRYP_KeyInitStructure.CRYP_Key1Right= __REV(*(uint32_t*)(keyaddr));
225 CRYP_KeyInit(& DES_CRYP_KeyInitStructure);
227 /* Initialization Vectors */
228 DES_CRYP_IVInitStructure.CRYP_IV0Left = __REV(*(uint32_t*)(ivaddr));
230 DES_CRYP_IVInitStructure.CRYP_IV0Right= __REV(*(uint32_t*)(ivaddr));
231 CRYP_IVInit(&DES_CRYP_IVInitStructure);
233 /* Flush IN/OUT FIFO */
236 /* Enable Crypto processor */
239 for(i=0; ((i<Ilength) && (status != ERROR)); i+=8)
241 /* Write the Input block in the Input FIFO */
242 CRYP_DataIn(*(uint32_t*)(inputaddr));
244 CRYP_DataIn(*(uint32_t*)(inputaddr));
247 /* Wait until the complete message has been processed */
251 busystatus = CRYP_GetFlagStatus(CRYP_FLAG_BUSY);
253 }while ((counter != DESBUSY_TIMEOUT) && (busystatus != RESET));
255 if (busystatus != RESET)
261 /* Read the Output block from the Output FIFO */
262 *(uint32_t*)(outputaddr) = CRYP_DataOut();
264 *(uint32_t*)(outputaddr) = CRYP_DataOut();
291 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/