CARME-M4 BSP  V1.5
stm32f4xx_cryp_tdes.c
Go to the documentation of this file.
1 
47 /* Includes ------------------------------------------------------------------*/
48 #include "stm32f4xx_cryp.h"
49 
50 
60 /* Private typedef -----------------------------------------------------------*/
61 /* Private define ------------------------------------------------------------*/
62 #define TDESBUSY_TIMEOUT ((uint32_t) 0x00010000)
63 
64 /* Private macro -------------------------------------------------------------*/
65 /* Private variables ---------------------------------------------------------*/
66 /* Private function prototypes -----------------------------------------------*/
67 /* Private functions ---------------------------------------------------------*/
68 
69 
100 ErrorStatus CRYP_TDES_ECB(uint8_t Mode, uint8_t Key[24], uint8_t *Input,
101  uint32_t Ilength, uint8_t *Output)
102 {
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;
112 
113  /* Crypto structures initialisation*/
114  CRYP_KeyStructInit(&TDES_CRYP_KeyInitStructure);
115 
116  /* Crypto Init for Encryption process */
117  if(Mode == MODE_ENCRYPT) /* TDES encryption */
118  {
119  TDES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Encrypt;
120  }
121  else /*if(Mode == MODE_DECRYPT)*/ /* TDES decryption */
122  {
123  TDES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Decrypt;
124  }
125 
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);
129 
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);
143 
144  /* Flush IN/OUT FIFO */
145  CRYP_FIFOFlush();
146 
147  /* Enable Crypto processor */
148  CRYP_Cmd(ENABLE);
149 
150  if(CRYP_GetCmdStatus() == DISABLE)
151  {
152  /* The CRYP peripheral clock is not enabled or the device doesn't embedd
153  the CRYP peripheral (please check the device sales type. */
154  return(ERROR);
155  }
156  for(i=0; ((i<Ilength) && (status != ERROR)); i+=8)
157  {
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;
163 
164  /* Wait until the complete message has been processed */
165  counter = 0;
166  do
167  {
168  busystatus = CRYP_GetFlagStatus(CRYP_FLAG_BUSY);
169  counter++;
170  }while ((counter != TDESBUSY_TIMEOUT) && (busystatus != RESET));
171 
172  if (busystatus != RESET)
173  {
174  status = ERROR;
175  }
176  else
177  {
178 
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;
184  }
185  }
186 
187  /* Disable Crypto */
188  CRYP_Cmd(DISABLE);
189 
190  return status;
191 }
192 
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)
210 {
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;
222 
223  /* Crypto structures initialisation*/
224  CRYP_KeyStructInit(&TDES_CRYP_KeyInitStructure);
225 
226  /* Crypto Init for Encryption process */
227  if(Mode == MODE_ENCRYPT) /* TDES encryption */
228  {
229  TDES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Encrypt;
230  }
231  else
232  {
233  TDES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Decrypt;
234  }
235  TDES_CRYP_InitStructure.CRYP_AlgoMode = CRYP_AlgoMode_TDES_CBC;
236  TDES_CRYP_InitStructure.CRYP_DataType = CRYP_DataType_8b;
237 
238  CRYP_Init(&TDES_CRYP_InitStructure);
239 
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);
253 
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);
259 
260  /* Flush IN/OUT FIFO */
261  CRYP_FIFOFlush();
262 
263  /* Enable Crypto processor */
264  CRYP_Cmd(ENABLE);
265 
266  if(CRYP_GetCmdStatus() == DISABLE)
267  {
268  /* The CRYP peripheral clock is not enabled or the device doesn't embedd
269  the CRYP peripheral (please check the device sales type. */
270  return(ERROR);
271  }
272 
273  for(i=0; ((i<Ilength) && (status != ERROR)); i+=8)
274  {
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;
280 
281  /* Wait until the complete message has been processed */
282  counter = 0;
283  do
284  {
285  busystatus = CRYP_GetFlagStatus(CRYP_FLAG_BUSY);
286  counter++;
287  }while ((counter != TDESBUSY_TIMEOUT) && (busystatus != RESET));
288 
289  if (busystatus != RESET)
290  {
291  status = ERROR;
292  }
293  else
294  {
295 
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;
301  }
302  }
303 
304  /* Disable Crypto */
305  CRYP_Cmd(DISABLE);
306 
307  return status;
308 }
325 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
CRYP Key(s) structure definition.
void CRYP_KeyInit(CRYP_KeyInitTypeDef *CRYP_KeyInitStruct)
Initializes the CRYP Keys according to the specified parameters in the CRYP_KeyInitStruct.
#define CRYP_FLAG_BUSY
uint32_t CRYP_AlgoDir
ErrorStatus CRYP_TDES_ECB(uint8_t Mode, uint8_t Key[24], uint8_t *Input, uint32_t Ilength, uint8_t *Output)
Encrypt and decrypt using TDES in ECB Mode.
uint32_t CRYP_DataType
void CRYP_Cmd(FunctionalState NewState)
Enables or disables the CRYP peripheral.
uint32_t CRYP_DataOut(void)
Returns the last data entered into the output FIFO.
void CRYP_IVInit(CRYP_IVInitTypeDef *CRYP_IVInitStruct)
Initializes the CRYP Initialization Vectors(IV) according to the specified parameters in the CRYP_IVI...
uint32_t CRYP_AlgoMode
#define CRYP_AlgoMode_TDES_ECB
void CRYP_KeyStructInit(CRYP_KeyInitTypeDef *CRYP_KeyInitStruct)
Fills each CRYP_KeyInitStruct member with its default value.
void CRYP_DataIn(uint32_t Data)
Writes data in the Data Input register (DIN).
FlagStatus CRYP_GetFlagStatus(uint8_t CRYP_FLAG)
Checks whether the specified CRYP flag is set or not.
void CRYP_Init(CRYP_InitTypeDef *CRYP_InitStruct)
Initializes the CRYP peripheral according to the specified parameters in the CRYP_InitStruct.
CRYP Initialization Vectors (IV) structure definition.
#define CRYP_AlgoMode_TDES_CBC
This file contains all the functions prototypes for the Cryptographic processor(CRYP) firmware librar...
void CRYP_FIFOFlush(void)
Flushes the IN and OUT FIFOs (that is read and write pointers of the FIFOs are reset) ...
ErrorStatus CRYP_TDES_CBC(uint8_t Mode, uint8_t Key[24], uint8_t InitVectors[8], uint8_t *Input, uint32_t Ilength, uint8_t *Output)
Encrypt and decrypt using TDES in CBC Mode.
CRYP Init structure definition.
FunctionalState CRYP_GetCmdStatus(void)
Returns whether CRYP peripheral is enabled or disabled.