Files @ 4b7449ab32bd
Branch filter:

Location: therm/libraries/STM32L1xx_StdPeriph_Driver/src/stm32l1xx_aes.c - annotation

Ethan Zonca
Add toggling fun text. Things seem to be working nicely.
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
f23a287bb7c1
/**
  ******************************************************************************
  * @file    stm32l1xx_aes.c
  * @author  MCD Application Team
  * @version V1.2.0
  * @date    22-February-2013
  * @brief   This file provides firmware functions to manage the following 
  *          functionalities of the AES peripheral:           
  *           + Configuration
  *           + Read/Write operations
  *           + DMA transfers management  
  *           + Interrupts and flags management
  * 
  *  @verbatim
 ===============================================================================
                        ##### AES Peripheral features #####
 ===============================================================================
....[..]
   (#) The Advanced Encryption Standard hardware accelerator (AES) can be used
       to both encipher and decipher data using AES algorithm.
   (#) The AES supports 4 operation modes:
       (++) Encryption: It consumes 214 clock cycle when processing one 128-bit block
       (++) Decryption: It consumes 214 clock cycle when processing one 128-bit block
       (++) Key derivation for decryption: It consumes 80 clock cycle when processing one 128-bit block
       (++) Key Derivation and decryption: It consumes 288 clock cycle when processing one 128-bit blobk
   (#) Moreover 3 chaining modes are supported:
       (++) Electronic codebook (ECB): Each plain text is encrypted/decrypted separately
       (++) Cipher block chaining (CBC): Each block is XORed with the previous block
       (++) Counter mode (CTR): A 128-bit counter is encrypted and then XORed with the
          plain text to give the cipher text
  (#) The AES peripheral supports data swapping: 1-bit, 8-bit, 16-bit and 32-bit.
  (#) The AES peripheral supports write/read error handling with interrupt capability.
  (#) Automatic data flow control with support of direct memory access (DMA) using
      2 channels, one for incoming data (DMA2 Channel5), and one for outcoming data
      (DMA2 Channel3).

                      ##### How to use this driver #####
 ===============================================================================
    [..]
        (#) AES AHB clock must be enabled to get write access to AES registers 
            using RCC_AHBPeriphClockCmd(RCC_AHBPeriph_AES, ENABLE).
        (#) Initialize the key using AES_KeyInit().
        (#) Configure the AES operation mode using AES_Init().
        (#) If required, enable interrupt source using AES_ITConfig() and
            enable the AES interrupt vector using NVIC_Init().
        (#) If required, when using the DMA mode.
            (##) Configure the DMA using DMA_Init().
            (##) Enable DMA requests using AES_DMAConfig().
        (#) Enable the AES peripheral using AES_Cmd().
    @endverbatim
  
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT 2013 STMicroelectronics</center></h2>
  *
  * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  * You may not use this file except in compliance with the License.
  * You may obtain a copy of the License at:
  *
  *        http://www.st.com/software_license_agreement_liberty_v2
  *
  * Unless required by applicable law or agreed to in writing, software 
  * distributed under the License is distributed on an "AS IS" BASIS, 
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "stm32l1xx_aes.h"
#include "stm32l1xx_rcc.h"

/** @addtogroup STM32L1xx_StdPeriph_Driver
  * @{
  */

/** @defgroup AES 
  * @brief AES driver modules
  * @{
  */ 

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define CR_CLEAR_MASK  ((uint32_t)0xFFFFFF81)

/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

/** @defgroup AES_Private_Functions
  * @{
  */

/** @defgroup AES_Group1 Initialization and configuration
 *  @brief   Initialization and configuration.
 *
@verbatim
 ===============================================================================
                ##### Initialization and configuration #####
 ===============================================================================

@endverbatim
  * @{
  */  

  /**
  * @brief  Deinitializes AES peripheral registers to their default reset values.
  * @param  None
  * @retval None
  */
void AES_DeInit(void)
{
  /* Enable AES reset state */
  RCC_AHBPeriphResetCmd(RCC_AHBPeriph_AES, ENABLE);
  /* Release AES from reset state */
  RCC_AHBPeriphResetCmd(RCC_AHBPeriph_AES, DISABLE);
}

/**
  * @brief  Initializes the AES peripheral according to the specified parameters
  *         in the AES_InitStruct:
  *           - AES_Operation: specifies the operation mode (encryption, decryption...).
  *           - AES_Chaining: specifies the chaining mode (ECB, CBC or CTR).
  *           - AES_DataType: specifies the data swapping type: 32-bit, 16-bit, 8-bit or 1-bit.
  * @note   If AES is already enabled, use AES_Cmd(DISABLE) before setting the new 
  *         configuration (When AES is enabled, setting configuration is forbidden).
  * @param  AES_InitStruct: pointer to an AES_InitTypeDef structure that contains 
  *         the configuration information for AES peripheral.
  * @retval None
  */
void AES_Init(AES_InitTypeDef* AES_InitStruct)
{
  uint32_t tmpreg = 0;
  
  /* Check the parameters */
  assert_param(IS_AES_MODE(AES_InitStruct->AES_Operation));
  assert_param(IS_AES_CHAINING(AES_InitStruct->AES_Chaining));
  assert_param(IS_AES_DATATYPE(AES_InitStruct->AES_DataType));

  /* Get AES CR register value */
  tmpreg = AES->CR;
  
  /* Clear DATATYPE[1:0], MODE[1:0] and CHMOD[1:0] bits */
  tmpreg &= (uint32_t)CR_CLEAR_MASK;
  
  tmpreg |= (AES_InitStruct->AES_Operation | AES_InitStruct->AES_Chaining | AES_InitStruct->AES_DataType);

  AES->CR = (uint32_t) tmpreg;
}

/**
  * @brief  Initializes the AES Keys according to the specified parameters in the AES_KeyInitStruct.
  * @param  AES_KeyInitStruct: pointer to an AES_KeyInitTypeDef structure that
  *         contains the configuration information for the specified AES Keys.
  * @note   This function must be called while the AES is disabled.
  * @note   In encryption, key derivation and key derivation + decryption modes,
  *         AES_KeyInitStruct must contain the encryption key.
  *         In decryption mode, AES_KeyInitStruct must contain the decryption key.
  * @retval None
  */
void AES_KeyInit(AES_KeyInitTypeDef* AES_KeyInitStruct)
{
  AES->KEYR0 = AES_KeyInitStruct->AES_Key0;
  AES->KEYR1 = AES_KeyInitStruct->AES_Key1;
  AES->KEYR2 = AES_KeyInitStruct->AES_Key2;
  AES->KEYR3 = AES_KeyInitStruct->AES_Key3;
}

/**
  * @brief  Initializes the AES Initialization Vector IV according to 
  *         the specified parameters in the AES_IVInitStruct.
  * @param  AES_KeyInitStruct: pointer to an AES_IVInitTypeDef structure that
  *         contains the configuration information for the specified AES IV.
  * @note   When ECB chaining mode is selected, Initialization Vector IV has no
  *         meaning.
  *         When CTR chaining mode is selected, AES_IV0 contains the CTR value.
  *         AES_IV1, AES_IV2 and AES_IV3 contains nonce value.
  * @retval None
  */
void AES_IVInit(AES_IVInitTypeDef* AES_IVInitStruct)
{
  AES->IVR0 = AES_IVInitStruct->AES_IV0;
  AES->IVR1 = AES_IVInitStruct->AES_IV1;
  AES->IVR2 = AES_IVInitStruct->AES_IV2;
  AES->IVR3 = AES_IVInitStruct->AES_IV3;
}

/**
  * @brief  Enable or disable the AES peripheral.
  * @param  NewState: new state of the AES peripheral.
  *         This parameter can be: ENABLE or DISABLE.
  * @note   The key must be written while AES is disabled.
  * @retval None
  */
void AES_Cmd(FunctionalState NewState)
{
  /* Check the parameter */
  assert_param(IS_FUNCTIONAL_STATE(NewState));

  if (NewState != DISABLE)
  {
    /* Enable the AES peripheral */
    AES->CR |= (uint32_t) AES_CR_EN;   /**< AES Enable */
  }
  else
  {
    /* Disable the AES peripheral */
    AES->CR &= (uint32_t)(~AES_CR_EN);  /**< AES Disable */
  }
}

/**
  * @}
  */

/** @defgroup AES_Group2 Structures initialization functions
 *  @brief   Structures initialization.
 *
@verbatim
 ===============================================================================
              ##### Structures initialization functions #####
 ===============================================================================

@endverbatim
  * @{
  */

/**
  * @brief  Fills each AES_InitStruct member with its default value.
  * @param  AES_InitStruct: pointer to an AES_InitTypeDef structure which will 
  *         be initialized.
  * @retval None
  */
void AES_StructInit(AES_InitTypeDef* AES_InitStruct)
{
  AES_InitStruct->AES_Operation = AES_Operation_Encryp;
  AES_InitStruct->AES_Chaining = AES_Chaining_ECB;
  AES_InitStruct->AES_DataType = AES_DataType_32b;
}

/**
  * @brief  Fills each AES_KeyInitStruct member with its default value.
  * @param  AES_KeyInitStruct: pointer to an AES_KeyInitStruct structure which 
  *         will be initialized.
  * @retval None
  */
void AES_KeyStructInit(AES_KeyInitTypeDef* AES_KeyInitStruct)
{
  AES_KeyInitStruct->AES_Key0 = 0x00000000;
  AES_KeyInitStruct->AES_Key1 = 0x00000000;
  AES_KeyInitStruct->AES_Key2 = 0x00000000;
  AES_KeyInitStruct->AES_Key3 = 0x00000000;
}

/**
  * @brief  Fills each AES_IVInitStruct member with its default value.
  * @param  AES_IVInitStruct: pointer to an AES_IVInitTypeDef structure which
  *         will be initialized.
  * @retval None
  */
void AES_IVStructInit(AES_IVInitTypeDef* AES_IVInitStruct)
{
  AES_IVInitStruct->AES_IV0 = 0x00000000;
  AES_IVInitStruct->AES_IV1 = 0x00000000;
  AES_IVInitStruct->AES_IV2 = 0x00000000;
  AES_IVInitStruct->AES_IV3 = 0x00000000;
}

/**
  * @}
  */

/** @defgroup AES_Group3 AES Read and Write
 *  @brief   AES Read and Write.
 *
@verbatim
 ===============================================================================
                  ##### AES Read and Write functions #####
 ===============================================================================

@endverbatim
  * @{
  */

/**
  * @brief  Write data in DINR register to be processed by AES peripheral.
  * @note   To process 128-bit data (4 * 32-bit), this function must be called
  *         four times to write the 128-bit data in the 32-bit register DINR.
  * @note   When an unexpected write to DOUTR register is detected, WRERR flag is
  *         set.
  * @param  Data: The data to be processed.
  * @retval None
  */
void AES_WriteSubData(uint32_t Data)
{
  /* Write Data */
  AES->DINR = Data;
}

/**
  * @brief  Returns the data in DOUTR register processed by AES peripheral.
  * @note   This function must be called four times to get the 128-bit data.
  * @note   When an unexpected read of DINR register is detected, RDERR flag is
  *         set.
  * @retval The processed data.
  */
uint32_t AES_ReadSubData(void)
{
  /* Read Data */
  return AES->DOUTR;
}

/**
  * @brief  Read the Key value.
  * @param  AES_KeyInitStruct: pointer to an AES_KeyInitTypeDef structure which
  *         will contain the key.
  * @note   When the key derivation mode is selected, AES must be disabled
  *         (AES_Cmd(DISABLE)) before reading the decryption key.
  *         Reading the key while the AES is enabled will return unpredictable
  *         value.
  * @retval None
  */
void AES_ReadKey(AES_KeyInitTypeDef* AES_KeyInitStruct)
{
  AES_KeyInitStruct->AES_Key0 = AES->KEYR0;
  AES_KeyInitStruct->AES_Key1 = AES->KEYR1;
  AES_KeyInitStruct->AES_Key2 = AES->KEYR2;
  AES_KeyInitStruct->AES_Key3 = AES->KEYR3;
}

/**
  * @brief  Read the Initialization Vector IV value.
  * @param  AES_IVInitStruct: pointer to an AES_IVInitTypeDef structure which
  *         will contain the Initialization Vector IV.
  * @note   When the AES is enabled Reading the Initialization Vector IV value
  *         will return 0. The AES must be disabled using AES_Cmd(DISABLE)
  *         to get the right value.
  * @note   When ECB chaining mode is selected, Initialization Vector IV has no
  *         meaning.
  *         When CTR chaining mode is selected, AES_IV0 contains 32-bit Counter value.
  *         AES_IV1, AES_IV2 and AES_IV3 contains nonce value.
  * @retval None
  */
void AES_ReadIV(AES_IVInitTypeDef* AES_IVInitStruct)
{
  AES_IVInitStruct->AES_IV0 = AES->IVR0;
  AES_IVInitStruct->AES_IV1 = AES->IVR1;
  AES_IVInitStruct->AES_IV2 = AES->IVR2;
  AES_IVInitStruct->AES_IV3 = AES->IVR3;
}

/**
  * @}
  */

/** @defgroup AES_Group4 DMA transfers management functions
 *  @brief   DMA transfers management function.
 *
@verbatim
 ===============================================================================
               ##### DMA transfers management functions #####
 ===============================================================================

@endverbatim
  * @{
  */

/**
  * @brief  Configures the AES DMA interface.
  * @param  AES_DMATransfer: Specifies the AES DMA transfer.
  *   This parameter can be one of the following values:
  *     @arg AES_DMATransfer_In: When selected, DMA manages the data input phase.
  *     @arg AES_DMATransfer_Out: When selected, DMA manages the data output phase.
  *     @arg AES_DMATransfer_InOut: When selected, DMA manages both the data input/output phases.
  * @param  NewState Indicates the new state of the AES DMA interface.
  *           This parameter can be: ENABLE or DISABLE.
  * @note   The DMA has no action in key derivation mode.
  * @retval None
  */
void AES_DMAConfig(uint32_t AES_DMATransfer, FunctionalState NewState)
{
  /* Check the parameter */
  assert_param(IS_AES_DMA_TRANSFER(AES_DMATransfer));

  if (NewState != DISABLE)
  {
    /* Enable the DMA transfer */
    AES->CR |= (uint32_t) AES_DMATransfer;
  }
  else
  {
    /* Disable the DMA transfer */
    AES->CR &= (uint32_t)(~AES_DMATransfer);
  }
}

/**
  * @}
  */

/** @defgroup AES_Group5 Interrupts and flags management functions
 *  @brief   Interrupts and flags management functions.
 *
@verbatim

 ===============================================================================
           ##### Interrupts and flags management functions #####
 ===============================================================================
@endverbatim
  * @{
  */

/**
  * @brief  Enables or disables the specified AES interrupt.
  * @param  AES_IT: Specifies the AES interrupt source to enable/disable.
  *     This parameter can be any combinations of the following values:
  *     @arg AES_IT_CC: Computation Complete Interrupt. If enabled, once CCF 
  *                     flag is set an interrupt is generated.
  *     @arg AES_IT_ERR: Error Interrupt. If enabled, once a read error
  *                      flags (RDERR) or write error flag (WRERR) is set,
  *                      an interrupt is generated.
  * @param  NewState: The new state of the AES interrupt source.
  *                   This parameter can be: ENABLE or DISABLE.
  * @retval None
  */
void AES_ITConfig(uint32_t AES_IT, FunctionalState NewState)
{
  /* Check the parameters */
  assert_param(IS_FUNCTIONAL_STATE(NewState));
  assert_param(IS_AES_IT(AES_IT));

  if (NewState != DISABLE)
  {
    AES->CR |= (uint32_t) AES_IT;    /**< AES_IT Enable */
  }
  else
  {
    AES->CR &= (uint32_t)(~AES_IT);  /**< AES_IT Disable */
  }
}

/**
  * @brief  Checks whether the specified AES flag is set or not.
  * @param  AES_FLAG specifies the flag to check.
  *   This parameter can be one of the following values:
  *     @arg AES_FLAG_CCF: Computation Complete Flag is set by hardware when
  *                        he computation phase is completed.
  *     @arg AES_FLAG_RDERR: Read Error Flag is set when an unexpected read
  *                          operation of DOUTR register is detected.
  *     @arg AES_FLAG_WRERR: Write Error Flag  is set when an unexpected write
  *                          operation in DINR is detected.
  * @retval FlagStatus (SET or RESET)
  */
FlagStatus AES_GetFlagStatus(uint32_t AES_FLAG)
{
  FlagStatus bitstatus = RESET;

  /* Check parameters */
  assert_param(IS_AES_FLAG(AES_FLAG));

  if ((AES->SR & AES_FLAG) != (uint32_t)RESET)
  {
    bitstatus = SET;
  }
  else
  {
    bitstatus = RESET;
  }
    
  /* Return the AES_FLAG status */
  return  bitstatus;
}

/**
  * @brief  Clears the AES flags.
  * @param  AES_FLAG: specifies the flag to clear.
  *         This parameter can be:
  *     @arg AES_FLAG_CCF: Computation Complete Flag is cleared by setting CCFC
  *                        bit in CR register.
  *     @arg AES_FLAG_RDERR: Read Error is cleared by setting ERRC bit in 
  *                          CR register.
  *     @arg AES_FLAG_WRERR: Write Error is cleared by setting ERRC bit in
  *                          CR register.
  * @retval None
  */
void AES_ClearFlag(uint32_t AES_FLAG)
{
  /* Check the parameters */
  assert_param(IS_AES_FLAG(AES_FLAG));

  /* Check if AES_FLAG is AES_FLAG_CCF */
  if (AES_FLAG == AES_FLAG_CCF)
  {
    /* Clear CCF flag by setting CCFC bit */
    AES->CR |= (uint32_t) AES_CR_CCFC;
  }
  else /* AES_FLAG is AES_FLAG_RDERR or AES_FLAG_WRERR */
  {
    /* Clear RDERR and WRERR flags by setting ERRC bit */
    AES->CR |= (uint32_t) AES_CR_ERRC;
  }
}

/**
  * @brief  Checks whether the specified AES interrupt has occurred or not.
  * @param  AES_IT: Specifies the AES interrupt pending bit to check.
  *         This parameter can be:
  *     @arg AES_IT_CC: Computation Complete Interrupt.
  *     @arg AES_IT_ERR: Error Interrupt.
  * @retval ITStatus The new state of AES_IT (SET or RESET).
  */
ITStatus AES_GetITStatus(uint32_t AES_IT)
{
  ITStatus itstatus = RESET;
  uint32_t cciebitstatus = RESET, ccfbitstatus = RESET;

  /* Check parameters */
  assert_param(IS_AES_GET_IT(AES_IT));

  cciebitstatus = AES->CR & AES_CR_CCIE;
  ccfbitstatus =  AES->SR & AES_SR_CCF;

  /* Check if AES_IT is AES_IT_CC */
  if (AES_IT == AES_IT_CC)
  {
    /* Check the status of the specified AES interrupt */
    if (((cciebitstatus) != (uint32_t)RESET) && ((ccfbitstatus) != (uint32_t)RESET))
    {
      /* Interrupt occurred */
      itstatus = SET;
    }
    else
    {
      /* Interrupt didn't occur */
      itstatus = RESET;
    }
  }
  else /* AES_IT is AES_IT_ERR */
  {
    /* Check the status of the specified AES interrupt */
    if ((AES->CR & AES_CR_ERRIE) != RESET)
    {
      /* Check if WRERR or RDERR flags are set */
      if ((AES->SR & (uint32_t)(AES_SR_WRERR | AES_SR_RDERR)) != (uint16_t)RESET)
      {
        /* Interrupt occurred */
        itstatus = SET;
      }
      else
      {
        /* Interrupt didn't occur */
        itstatus = RESET;
      }
    }
    else
    {
      /* Interrupt didn't occur */
      itstatus = (ITStatus) RESET;
    }
  }

  /* Return the AES_IT status */
  return itstatus;
}

/**
  * @brief  Clears the AES's interrupt pending bits.
  * @param  AES_IT: specifies the interrupt pending bit to clear.
  *   This parameter can be any combinations of the following values:
  *     @arg AES_IT_CC: Computation Complete Interrupt.
  *     @arg AES_IT_ERR: Error Interrupt.
  * @retval None
  */
void AES_ClearITPendingBit(uint32_t AES_IT)
{
  /* Check the parameters */
  assert_param(IS_AES_IT(AES_IT));

  /* Clear the interrupt pending bit */
  AES->CR |= (uint32_t) (AES_IT >> (uint32_t) 0x00000002);
}

/**
  * @}
  */

/**
  * @}
  */

/**
  * @}
  */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/