CARME-M4 BSP  V1.5
stm32f4xx_cryp_des.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 DESBUSY_TIMEOUT ((uint32_t) 0x00010000)
63 
64 /* Private macro -------------------------------------------------------------*/
65 /* Private variables ---------------------------------------------------------*/
66 /* Private function prototypes -----------------------------------------------*/
67 /* Private functions ---------------------------------------------------------*/
68 
69 
99 ErrorStatus CRYP_DES_ECB(uint8_t Mode, uint8_t Key[8], uint8_t *Input,
100  uint32_t Ilength, uint8_t *Output)
101 {
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;
111 
112  /* Crypto structures initialisation*/
113  CRYP_KeyStructInit(&DES_CRYP_KeyInitStructure);
114 
115  /* Crypto Init for Encryption process */
116  if( Mode == MODE_ENCRYPT ) /* DES encryption */
117  {
118  DES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Encrypt;
119  }
120  else/* if( Mode == MODE_DECRYPT )*/ /* DES decryption */
121  {
122  DES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Decrypt;
123  }
124 
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);
128 
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);
134 
135  /* Flush IN/OUT FIFO */
136  CRYP_FIFOFlush();
137 
138  /* Enable Crypto processor */
139  CRYP_Cmd(ENABLE);
140 
141  if(CRYP_GetCmdStatus() == DISABLE)
142  {
143  /* The CRYP peripheral clock is not enabled or the device doesn't embedd
144  the CRYP peripheral (please check the device sales type. */
145  return(ERROR);
146  }
147  for(i=0; ((i<Ilength) && (status != ERROR)); i+=8)
148  {
149 
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;
155 
156 /* Wait until the complete message has been processed */
157  counter = 0;
158  do
159  {
160  busystatus = CRYP_GetFlagStatus(CRYP_FLAG_BUSY);
161  counter++;
162  }while ((counter != DESBUSY_TIMEOUT) && (busystatus != RESET));
163 
164  if (busystatus != RESET)
165  {
166  status = ERROR;
167  }
168  else
169  {
170 
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;
176  }
177  }
178 
179  /* Disable Crypto */
180  CRYP_Cmd(DISABLE);
181 
182  return status;
183 }
184 
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)
202 {
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;
214 
215  /* Crypto structures initialisation*/
216  CRYP_KeyStructInit(&DES_CRYP_KeyInitStructure);
217 
218  /* Crypto Init for Encryption process */
219  if(Mode == MODE_ENCRYPT) /* DES encryption */
220  {
221  DES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Encrypt;
222  }
223  else /*if(Mode == MODE_DECRYPT)*/ /* DES decryption */
224  {
225  DES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Decrypt;
226  }
227 
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);
231 
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);
237 
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);
243 
244  /* Flush IN/OUT FIFO */
245  CRYP_FIFOFlush();
246 
247  /* Enable Crypto processor */
248  CRYP_Cmd(ENABLE);
249 
250  if(CRYP_GetCmdStatus() == DISABLE)
251  {
252  /* The CRYP peripheral clock is not enabled or the device doesn't embedd
253  the CRYP peripheral (please check the device sales type. */
254  return(ERROR);
255  }
256  for(i=0; ((i<Ilength) && (status != ERROR)); i+=8)
257  {
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;
263 
264  /* Wait until the complete message has been processed */
265  counter = 0;
266  do
267  {
268  busystatus = CRYP_GetFlagStatus(CRYP_FLAG_BUSY);
269  counter++;
270  }while ((counter != DESBUSY_TIMEOUT) && (busystatus != RESET));
271 
272  if (busystatus != RESET)
273  {
274  status = ERROR;
275  }
276  else
277  {
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;
283  }
284  }
285 
286  /* Disable Crypto */
287  CRYP_Cmd(DISABLE);
288 
289  return status;
290 }
291 
308 /************************ (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
ErrorStatus CRYP_DES_CBC(uint8_t Mode, uint8_t Key[8], uint8_t InitVectors[8], uint8_t *Input, uint32_t Ilength, uint8_t *Output)
Encrypt and decrypt using DES in CBC Mode.
uint32_t CRYP_AlgoDir
uint32_t CRYP_DataType
#define CRYP_AlgoMode_DES_CBC
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
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.
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_DES_ECB(uint8_t Mode, uint8_t Key[8], uint8_t *Input, uint32_t Ilength, uint8_t *Output)
Encrypt and decrypt using DES in ECB Mode.
CRYP Init structure definition.
FunctionalState CRYP_GetCmdStatus(void)
Returns whether CRYP peripheral is enabled or disabled.