2 ******************************************************************************
3 * @file stm32f4xx_cryp_tdes.c
4 * @author MCD Application Team
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
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 TDES in ECB Mode using CRYP_TDES_ECB()
23 * 3. Encrypt and decrypt using TDES in CBC Mode using CRYP_TDES_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 TDESBUSY_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_Group7 High Level TDES functions
70 * @brief High Level TDES functions
73 ===============================================================================
74 High Level TDES functions
75 ===============================================================================
83 * @brief Encrypt and decrypt using TDES in ECB Mode
84 * @param Mode: encryption or decryption Mode.
85 * This parameter can be one of the following values:
86 * @arg MODE_ENCRYPT: Encryption
87 * @arg MODE_DECRYPT: Decryption
88 * @param Key: Key used for TDES algorithm.
89 * @param Ilength: length of the Input buffer, must be a multiple of 8.
90 * @param Input: pointer to the Input buffer.
91 * @param Output: pointer to the returned buffer.
92 * @retval An ErrorStatus enumeration value:
93 * - SUCCESS: Operation done
94 * - ERROR: Operation failed
96 ErrorStatus CRYP_TDES_ECB(uint8_t Mode, uint8_t Key[24], uint8_t *Input,
97 uint32_t Ilength, uint8_t *Output)
99 CRYP_InitTypeDef TDES_CRYP_InitStructure;
100 CRYP_KeyInitTypeDef TDES_CRYP_KeyInitStructure;
101 __IO uint32_t counter = 0;
102 uint32_t busystatus = 0;
103 ErrorStatus status = SUCCESS;
104 uint32_t keyaddr = (uint32_t)Key;
105 uint32_t inputaddr = (uint32_t)Input;
106 uint32_t outputaddr = (uint32_t)Output;
109 /* Crypto structures initialisation*/
110 CRYP_KeyStructInit(&TDES_CRYP_KeyInitStructure);
112 /* Crypto Init for Encryption process */
113 if(Mode == MODE_ENCRYPT) /* TDES encryption */
115 TDES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Encrypt;
117 else /*if(Mode == MODE_DECRYPT)*/ /* TDES decryption */
119 TDES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Decrypt;
122 TDES_CRYP_InitStructure.CRYP_AlgoMode = CRYP_AlgoMode_TDES_ECB;
123 TDES_CRYP_InitStructure.CRYP_DataType = CRYP_DataType_8b;
124 CRYP_Init(&TDES_CRYP_InitStructure);
126 /* Key Initialisation */
127 TDES_CRYP_KeyInitStructure.CRYP_Key1Left = __REV(*(uint32_t*)(keyaddr));
129 TDES_CRYP_KeyInitStructure.CRYP_Key1Right= __REV(*(uint32_t*)(keyaddr));
131 TDES_CRYP_KeyInitStructure.CRYP_Key2Left = __REV(*(uint32_t*)(keyaddr));
133 TDES_CRYP_KeyInitStructure.CRYP_Key2Right= __REV(*(uint32_t*)(keyaddr));
135 TDES_CRYP_KeyInitStructure.CRYP_Key3Left = __REV(*(uint32_t*)(keyaddr));
137 TDES_CRYP_KeyInitStructure.CRYP_Key3Right= __REV(*(uint32_t*)(keyaddr));
138 CRYP_KeyInit(& TDES_CRYP_KeyInitStructure);
140 /* Flush IN/OUT FIFO */
143 /* Enable Crypto processor */
146 for(i=0; ((i<Ilength) && (status != ERROR)); i+=8)
148 /* Write the Input block in the Input FIFO */
149 CRYP_DataIn(*(uint32_t*)(inputaddr));
151 CRYP_DataIn(*(uint32_t*)(inputaddr));
154 /* Wait until the complete message has been processed */
158 busystatus = CRYP_GetFlagStatus(CRYP_FLAG_BUSY);
160 }while ((counter != TDESBUSY_TIMEOUT) && (busystatus != RESET));
162 if (busystatus != RESET)
169 /* Read the Output block from the Output FIFO */
170 *(uint32_t*)(outputaddr) = CRYP_DataOut();
172 *(uint32_t*)(outputaddr) = CRYP_DataOut();
184 * @brief Encrypt and decrypt using TDES in CBC Mode
185 * @param Mode: encryption or decryption Mode.
186 * This parameter can be one of the following values:
187 * @arg MODE_ENCRYPT: Encryption
188 * @arg MODE_DECRYPT: Decryption
189 * @param Key: Key used for TDES algorithm.
190 * @param InitVectors: Initialisation Vectors used for TDES algorithm.
191 * @param Input: pointer to the Input buffer.
192 * @param Ilength: length of the Input buffer, must be a multiple of 8.
193 * @param Output: pointer to the returned buffer.
194 * @retval An ErrorStatus enumeration value:
195 * - SUCCESS: Operation done
196 * - ERROR: Operation failed
198 ErrorStatus CRYP_TDES_CBC(uint8_t Mode, uint8_t Key[24], uint8_t InitVectors[8],
199 uint8_t *Input, uint32_t Ilength, uint8_t *Output)
201 CRYP_InitTypeDef TDES_CRYP_InitStructure;
202 CRYP_KeyInitTypeDef TDES_CRYP_KeyInitStructure;
203 CRYP_IVInitTypeDef TDES_CRYP_IVInitStructure;
204 __IO uint32_t counter = 0;
205 uint32_t busystatus = 0;
206 ErrorStatus status = SUCCESS;
207 uint32_t keyaddr = (uint32_t)Key;
208 uint32_t inputaddr = (uint32_t)Input;
209 uint32_t outputaddr = (uint32_t)Output;
210 uint32_t ivaddr = (uint32_t)InitVectors;
213 /* Crypto structures initialisation*/
214 CRYP_KeyStructInit(&TDES_CRYP_KeyInitStructure);
216 /* Crypto Init for Encryption process */
217 if(Mode == MODE_ENCRYPT) /* TDES encryption */
219 TDES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Encrypt;
223 TDES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Decrypt;
225 TDES_CRYP_InitStructure.CRYP_AlgoMode = CRYP_AlgoMode_TDES_CBC;
226 TDES_CRYP_InitStructure.CRYP_DataType = CRYP_DataType_8b;
228 CRYP_Init(&TDES_CRYP_InitStructure);
230 /* Key Initialisation */
231 TDES_CRYP_KeyInitStructure.CRYP_Key1Left = __REV(*(uint32_t*)(keyaddr));
233 TDES_CRYP_KeyInitStructure.CRYP_Key1Right= __REV(*(uint32_t*)(keyaddr));
235 TDES_CRYP_KeyInitStructure.CRYP_Key2Left = __REV(*(uint32_t*)(keyaddr));
237 TDES_CRYP_KeyInitStructure.CRYP_Key2Right= __REV(*(uint32_t*)(keyaddr));
239 TDES_CRYP_KeyInitStructure.CRYP_Key3Left = __REV(*(uint32_t*)(keyaddr));
241 TDES_CRYP_KeyInitStructure.CRYP_Key3Right= __REV(*(uint32_t*)(keyaddr));
242 CRYP_KeyInit(& TDES_CRYP_KeyInitStructure);
244 /* Initialization Vectors */
245 TDES_CRYP_IVInitStructure.CRYP_IV0Left = __REV(*(uint32_t*)(ivaddr));
247 TDES_CRYP_IVInitStructure.CRYP_IV0Right= __REV(*(uint32_t*)(ivaddr));
248 CRYP_IVInit(&TDES_CRYP_IVInitStructure);
250 /* Flush IN/OUT FIFO */
253 /* Enable Crypto processor */
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 != TDESBUSY_TIMEOUT) && (busystatus != RESET));
272 if (busystatus != RESET)
279 /* Read the Output block from the Output FIFO */
280 *(uint32_t*)(outputaddr) = CRYP_DataOut();
282 *(uint32_t*)(outputaddr) = CRYP_DataOut();
308 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/