forked from InboraStudio/Game-engine-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main-SLL..c
802 lines (732 loc) · 23.3 KB
/
main-SLL..c
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
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
#include "gd32l23x.h"
#include <stdio.h>
#include "gd32l233r_eval.h"
#include "main.h"
__attribute__((aligned(4)))
uint8_t encrypt_result[TEXT_SIZE];
__attribute__((aligned(4)))
uint8_t decrypt_result[TEXT_SIZE];
uint16_t algorithm_mode_used = 0;
uint32_t i = 0;
/* DES algorithm ECB mode demo */
static void des_ecb_demo(void);
/* DES algorithm CBC mode demo */
static void des_cbc_demo(void);
/* TDES algorithm ECB mode demo */
static void tdes_ecb_demo(void);
/* TDES algorithm CBC mode demo */
static void tdes_cbc_demo(void);
/* AES algorithm ECB mode demo */
static void aes_ecb_demo(void);
/* AES algorithm CBC mode demo */
static void aes_cbc_demo(void);
/* AES algorithm CTR mode demo */
static void aes_ctr_demo(void);
/* AES algorithm GCM mode demo */
static void aes_gcm_demo(void);
/* AES algorithm CCM mode demo */
static void aes_ccm_demo(void);
/* AES algorithm CFB mode demo */
static void aes_cfb_demo(void);
/* AES algorithm OFB mode demo */
static void aes_ofb_demo(void);
/* printf data by a block of 16 bytes */
static void data_display(uint32_t datalength, uint8_t *data);
/* display algorithm selection */
static void algorithm_choose_display(void);
/* display mode selection */
static void mode_choose_display(void);
/* algorithm selection */
static void algorithm_choose(void);
/* mode selection */
static void mode_choose(void);
/*!
\brief main function
\param[in] none
\param[out] none
\retval none
*/
int main(void)
{
gd_eval_com_init(EVAL_COM);
/* enable CAU clock */
rcu_periph_clock_enable(RCU_CAU);
while(1) {
printf("\r\nPlain data :\r\n");
data_display(TEXT_SIZE, plaintext);
/* select algorithm */
algorithm_choose_display();
algorithm_choose();
/* select mode */
mode_choose_display();
mode_choose();
/* go to the corresponding encryption/decryption */
switch(algorithm_mode_used) {
/* when DES ECB mode is selected */
case ALGORITHM_USE_DES | MODE_USE_ECB:
des_ecb_demo();
break;
/* when DES CBC mode is selected */
case ALGORITHM_USE_DES | MODE_USE_CBC:
des_cbc_demo();
break;
/* when TDES ECB mode is selected */
case ALGORITHM_USE_TDES | MODE_USE_ECB:
tdes_ecb_demo();
break;
/* when TDES CBC mode is selected */
case ALGORITHM_USE_TDES | MODE_USE_CBC:
tdes_cbc_demo();
break;
/* when AES ECB mode is selected */
case ALGORITHM_USE_AES | MODE_USE_ECB:
aes_ecb_demo();
break;
/* when AES CBC mode is selected */
case ALGORITHM_USE_AES | MODE_USE_CBC:
aes_cbc_demo();
break;
/* when AES CTR mode is selected */
case ALGORITHM_USE_AES | MODE_USE_CTR:
aes_ctr_demo();
break;
/* when AES GCM mode is selected */
case ALGORITHM_USE_AES | MODE_USE_GCM:
aes_gcm_demo();
break;
/* when AES CCM mode is selected */
case ALGORITHM_USE_AES | MODE_USE_CCM:
aes_ccm_demo();
break;
/* when AES CFB mode is selected */
case ALGORITHM_USE_AES | MODE_USE_CFB:
aes_cfb_demo();
break;
/* when AES OFB mode is selected */
case ALGORITHM_USE_AES | MODE_USE_OFB:
aes_ofb_demo();
break;
default:
printf("algorithm or mode error! \r\n");
break;
}
printf("\r\n Example restarted...\n ");
algorithm_mode_used = 0;
}
}
/*!
\brief DES algorithm ECB mode demo
\param[in] none
\param[out] none
\retval none
*/
static void des_ecb_demo(void)
{
cau_parameter_struct text;
/* reset the CAU peripheral */
cau_deinit();
cau_struct_para_init(&text);
/* encryption in DES ECB mode */
text.alg_dir = CAU_ENCRYPT;
text.key = des_key;
text.input = plaintext;
text.in_length = TEXT_SIZE;
if(SUCCESS == cau_des_ecb(&text, encrypt_result)) {
printf(" \r\n Encrypted data with DES Mode ECB :\r\n\r\n");
data_display(TEXT_SIZE, encrypt_result);
}
/* decryption in DES ECB mode */
text.alg_dir = CAU_DECRYPT;
text.key = des_key;
text.input = encrypt_result;
text.in_length = TEXT_SIZE;
if(SUCCESS == cau_des_ecb(&text, decrypt_result)) {
printf(" \r\n Decrypted data with DES Mode ECB :\r\n\r\n");
data_display(TEXT_SIZE, decrypt_result);
}
}
/*!
\brief DES algorithm CBC mode demo
\param[in] none
\param[out] none
\retval none
*/
static void des_cbc_demo(void)
{
cau_parameter_struct text;
/* reset the CAU peripheral */
cau_deinit();
cau_struct_para_init(&text);
/* encryption in DES CBC mode */
text.alg_dir = CAU_ENCRYPT;
text.key = des_key;
text.iv = vectors;
text.input = plaintext;
text.in_length = TEXT_SIZE;
if(SUCCESS == cau_des_cbc(&text, encrypt_result)) {
printf(" \r\n Encrypted data with DES Mode CBC :\r\n\r\n");
data_display(TEXT_SIZE, encrypt_result);
}
/* decryption in DES CBC mode */
text.alg_dir = CAU_DECRYPT;
text.key = des_key;
text.iv = vectors;
text.input = encrypt_result;
text.in_length = TEXT_SIZE;
if(SUCCESS == cau_des_cbc(&text, decrypt_result)) {
printf(" \r\n Decrypted data with DES Mode CBC :\r\n\r\n");
data_display(TEXT_SIZE, decrypt_result);
}
}
/*!
\brief TDES algorithm ECB mode demo
\param[in] none
\param[out] none
\retval none
*/
static void tdes_ecb_demo(void)
{
cau_parameter_struct text;
/* reset the CAU peripheral */
cau_deinit();
cau_struct_para_init(&text);
/* encryption in TDES ECB mode */
text.alg_dir = CAU_ENCRYPT;
text.key = tdes_key;
text.input = plaintext;
text.in_length = TEXT_SIZE;
if(SUCCESS == cau_tdes_ecb(&text, encrypt_result)) {
printf(" \r\n Encrypted data with TDES Mode ECB :\r\n\r\n");
data_display(TEXT_SIZE, encrypt_result);
}
/* decryption in TDES ECB mode */
text.alg_dir = CAU_DECRYPT;
text.key = tdes_key;
text.input = encrypt_result;
text.in_length = TEXT_SIZE;
if(SUCCESS == cau_tdes_ecb(&text, decrypt_result)) {
printf(" \r\n Decrypted data with TDES Mode ECB :\r\n\r\n");
data_display(TEXT_SIZE, decrypt_result);
}
}
/*!
\brief TDES algorithm CBC mode demo
\param[in] none
\param[out] none
\retval none
*/
static void tdes_cbc_demo(void)
{
cau_parameter_struct text;
/* reset the CAU peripheral */
cau_deinit();
cau_struct_para_init(&text);
/* encryption in TDES CBC mode */
text.alg_dir = CAU_ENCRYPT;
text.key = tdes_key;
text.iv = vectors;
text.input = plaintext;
text.in_length = TEXT_SIZE;
if(SUCCESS == cau_tdes_cbc(&text, encrypt_result)) {
printf(" \r\n Encrypted data with TDES Mode CBC :\r\n\r\n");
data_display(TEXT_SIZE, encrypt_result);
}
/* decryption in TDES CBC mode */
text.alg_dir = CAU_DECRYPT;
text.key = tdes_key;
text.iv = vectors;
text.input = encrypt_result;
text.in_length = TEXT_SIZE;
if(SUCCESS == cau_tdes_cbc(&text, decrypt_result)) {
printf(" \r\n Decrypted data with TDES Mode CBC :\r\n\r\n");
data_display(TEXT_SIZE, decrypt_result);
}
}
/*!
\brief AES algorithm ECB mode demo
\param[in] none
\param[out] none
\retval none
*/
static void aes_ecb_demo(void)
{
cau_parameter_struct text;
uint8_t *key_addr;
uint32_t key_size;
/* reset the CAU peripheral */
cau_deinit();
cau_struct_para_init(&text);
/* ECB mode: AES 128 -> AES 192 -> AES 256 */
for(i = 0; i < 3; i++) {
/* encryption in ECB mode */
key_addr = key_select[i];
key_size = keysize[i];
text.alg_dir = CAU_ENCRYPT;
text.key = key_addr;
text.key_size = key_size;
text.input = plaintext;
text.in_length = TEXT_SIZE;
if(SUCCESS == cau_aes_ecb(&text, encrypt_result)) {
printf(" \r\n\r\nEncrypted Data with AES %d Mode ECB :\r\n\r\n", keysize[i]);
data_display(TEXT_SIZE, encrypt_result);
}
/* decryption in ECB mode */
text.alg_dir = CAU_DECRYPT;
text.key = key_addr;
text.key_size = key_size;
text.input = encrypt_result;
text.in_length = TEXT_SIZE;
if(SUCCESS == cau_aes_ecb(&text, decrypt_result)) {
printf(" \r\n\r\nDecrypted Data with AES %d Mode ECB :\r\n\r\n", keysize[i]);
data_display(TEXT_SIZE, decrypt_result);
}
}
}
/*!
\brief AES algorithm CBC mode demo
\param[in] none
\param[out] none
\retval none
*/
static void aes_cbc_demo(void)
{
cau_parameter_struct text;
uint8_t *key_addr;
uint32_t key_size;
/* reset the CAU peripheral */
cau_deinit();
cau_struct_para_init(&text);
/* CBC mode: AES 128 -> AES 192 -> AES 256 */
for(i = 0; i < 3; i++) {
/* encryption in CBC mode */
key_addr = key_select[i];
key_size = keysize[i];
text.alg_dir = CAU_ENCRYPT;
text.key = key_addr;
text.key_size = key_size;
text.iv = vectors;
text.input = plaintext;
text.in_length = TEXT_SIZE;
if(SUCCESS == cau_aes_cbc(&text, encrypt_result)) {
printf(" \r\n\r\nEncrypted Data with AES %d Mode CBC :\r\n\r\n", keysize[i]);
data_display(TEXT_SIZE, encrypt_result);
}
/* decryption in CBC mode */
text.alg_dir = CAU_DECRYPT;
text.key = key_addr;
text.key_size = key_size;
text.iv = vectors;
text.input = encrypt_result;
text.in_length = TEXT_SIZE;
if(SUCCESS == cau_aes_cbc(&text, decrypt_result)) {
printf(" \r\n\r\nDecrypted Data with AES %d Mode CBC :\r\n\r\n", keysize[i]);
data_display(TEXT_SIZE, decrypt_result);
}
}
}
/*!
\brief AES algorithm CTR mode demo
\param[in] none
\param[out] none
\retval none
*/
static void aes_ctr_demo(void)
{
cau_parameter_struct text;
uint8_t *key_addr;
uint32_t key_size;
/* reset the CAU peripheral */
cau_deinit();
cau_struct_para_init(&text);
/* CTR mode: AES 128 -> AES 192 -> AES 256 */
for(i = 0; i < 3; i++) {
/* encryption in CTR mode */
key_addr = key_select[i];
key_size = keysize[i];
text.alg_dir = CAU_ENCRYPT;
text.key = key_addr;
text.key_size = key_size;
text.iv = vectors;
text.input = plaintext;
text.in_length = TEXT_SIZE;
if(SUCCESS == cau_aes_ctr(&text, encrypt_result)) {
printf(" \r\n\r\nEncrypted Data with AES %d Mode CTR :\r\n\r\n", keysize[i]);
data_display(TEXT_SIZE, encrypt_result);
}
/* decryption in CTR mode */
text.alg_dir = CAU_DECRYPT;
text.key = key_addr;
text.key_size = key_size;
text.iv = vectors;
text.input = encrypt_result;
text.in_length = TEXT_SIZE;
if(SUCCESS == cau_aes_ctr(&text, decrypt_result)) {
printf(" \r\n\r\nDecrypted Data with AES %d Mode CTR :\r\n\r\n", keysize[i]);
data_display(TEXT_SIZE, decrypt_result);
}
}
}
/*!
\brief AES algorithm GCM mode demo
\param[in] none
\param[out] none
\retval none
*/
static void aes_gcm_demo(void)
{
cau_parameter_struct text;
uint8_t *key_addr;
uint32_t key_size;
uint8_t gcm_tag[GCM_TAG_SIZE];
/* reset the CAU peripheral */
cau_deinit();
cau_struct_para_init(&text);
/* GCM mode: AES 128 -> AES 192 -> AES 256 */
for(i = 0; i < 3; i++) {
/* encryption in GCM mode */
key_addr = key_select[i];
key_size = keysize[i];
text.alg_dir = CAU_ENCRYPT;
text.key = key_addr;
text.key_size = key_size;
text.iv = vectors;
text.iv_size = IV_SIZE;
text.input = plaintext;
text.in_length = TEXT_SIZE;
text.aad = aadmessage;
text.aad_size = AAD_SIZE;
if(SUCCESS == cau_aes_gcm(&text, encrypt_result, gcm_tag)) {
printf(" \r\n\r\nEncrypted Data with AES %d Mode GCM :\r\n\r\n", keysize[i]);
printf("encrypted data:\r\n");
data_display(TEXT_SIZE, encrypt_result);
printf("tag:\r\n");
data_display(GCM_TAG_SIZE, gcm_tag);
}
/* decryption in GCM mode */
text.alg_dir = CAU_DECRYPT;
text.key = key_addr;
text.key_size = key_size;
text.iv = vectors;
text.iv_size = IV_SIZE;
text.input = encrypt_result;
text.in_length = TEXT_SIZE;
text.aad = aadmessage;
text.aad_size = AAD_SIZE;
if(SUCCESS == cau_aes_gcm(&text, decrypt_result, gcm_tag)) {
printf(" \r\n\r\nDecrypted Data with AES %d Mode GCM :\r\n\r\n", keysize[i]);
printf("decrypted data:\r\n");
data_display(TEXT_SIZE, decrypt_result);
printf("tag:\r\n");
data_display(GCM_TAG_SIZE, gcm_tag);
}
}
}
/*!
\brief AES algorithm CCM mode demo
\param[in] none
\param[out] none
\retval none
*/
static void aes_ccm_demo(void)
{
cau_parameter_struct text;
uint8_t *key_addr;
uint32_t key_size;
uint8_t ccm_tag[CCM_TAG_SIZE];
uint8_t aad_buf[AAD_SIZE + 21];
/* reset the CAU peripheral */
cau_deinit();
cau_struct_para_init(&text);
/* CCM mode: AES 128 -> AES 192 -> AES 256 */
for(i = 0; i < 3; i++) {
/* encryption in CCM mode */
key_addr = key_select[i];
key_size = keysize[i];
text.alg_dir = CAU_ENCRYPT;
text.key = key_addr;
text.key_size = key_size;
text.iv = ccm_vectors;
text.iv_size = CCM_IV_SIZE;
text.input = plaintext;
text.in_length = TEXT_SIZE;
text.aad = aadmessage;
text.aad_size = AAD_SIZE;
if(SUCCESS == cau_aes_ccm(&text, encrypt_result, ccm_tag, CCM_TAG_SIZE, (uint8_t *)aad_buf)) {
printf(" \r\n\r\nEncrypted Data with AES %d Mode CCM :\r\n\r\n", keysize[i]);
printf("encrypted data:\r\n");
data_display(TEXT_SIZE, encrypt_result);
printf("tag:\r\n");
data_display(CCM_TAG_SIZE, ccm_tag);
}
/* decryption in CCM mode */
text.alg_dir = CAU_DECRYPT;
text.key = key_addr;
text.key_size = key_size;
text.iv = ccm_vectors;
text.iv_size = CCM_IV_SIZE;
text.input = encrypt_result;
text.in_length = TEXT_SIZE;
text.aad = aadmessage;
text.aad_size = AAD_SIZE;
if(SUCCESS == cau_aes_ccm(&text, decrypt_result, ccm_tag, CCM_TAG_SIZE, (uint8_t *)aad_buf)) {
printf(" \r\n\r\nDecrypted Data with AES %d Mode CCM :\r\n\r\n", keysize[i]);
printf("decrypted data:\r\n");
data_display(TEXT_SIZE, decrypt_result);
printf("tag:\r\n");
data_display(CCM_TAG_SIZE, ccm_tag);
}
}
}
/*!
\brief AES algorithm CFB mode demo
\param[in] none
\param[out] none
\retval none
*/
static void aes_cfb_demo(void)
{
cau_parameter_struct text;
uint8_t *key_addr;
uint32_t key_size;
/* reset the CAU peripheral */
cau_deinit();
cau_struct_para_init(&text);
/* CFB mode: AES 128 -> AES 192 -> AES 256 */
for(i = 0; i < 3; i++) {
/* encryption in CFB mode */
key_addr = key_select[i];
key_size = keysize[i];
text.alg_dir = CAU_ENCRYPT;
text.key = key_addr;
text.key_size = key_size;
text.iv = vectors;
text.iv_size = IV_SIZE;
text.input = plaintext;
text.in_length = TEXT_SIZE;
if(SUCCESS == cau_aes_cfb(&text, encrypt_result)) {
printf(" \r\n\r\nEncrypted Data with AES %d Mode CFB :\r\n\r\n", keysize[i]);
data_display(TEXT_SIZE, encrypt_result);
}
/* decryption in CFB mode */
text.alg_dir = CAU_DECRYPT;
text.key = key_addr;
text.key_size = key_size;
text.iv = vectors;
text.iv_size = IV_SIZE;
text.input = encrypt_result;
text.in_length = TEXT_SIZE;
if(SUCCESS == cau_aes_cfb(&text, decrypt_result)) {
printf(" \r\n\r\nDecrypted Data with AES %d Mode CFB :\r\n\r\n", keysize[i]);
data_display(TEXT_SIZE, decrypt_result);
}
}
}
/*!
\brief AES algorithm OFB mode demo
\param[in] none
\param[out] none
\retval none
*/
static void aes_ofb_demo(void)
{
cau_parameter_struct text;
uint8_t *key_addr;
uint32_t key_size;
/* reset the CAU peripheral */
cau_deinit();
cau_struct_para_init(&text);
/* OFB mode: AES 128 -> AES 192 -> AES 256 */
for(i = 0; i < 3; i++) {
/* encryption in OFB mode */
key_addr = key_select[i];
key_size = keysize[i];
text.alg_dir = CAU_ENCRYPT;
text.key = key_addr;
text.key_size = key_size;
text.iv = vectors;
text.iv_size = IV_SIZE;
text.input = plaintext;
text.in_length = TEXT_SIZE;
if(SUCCESS == cau_aes_ofb(&text, encrypt_result)) {
printf(" \r\n\r\nEncrypted Data with AES %d Mode OFB :\r\n\r\n", keysize[i]);
data_display(TEXT_SIZE, encrypt_result);
}
/* decryption in OFB mode */
text.alg_dir = CAU_DECRYPT;
text.key = key_addr;
text.key_size = key_size;
text.iv = vectors;
text.iv_size = IV_SIZE;
text.input = encrypt_result;
text.in_length = TEXT_SIZE;
if(SUCCESS == cau_aes_ofb(&text, decrypt_result)) {
printf(" \r\n\r\nDecrypted Data with AES %d Mode OFB :\r\n\r\n", keysize[i]);
data_display(TEXT_SIZE, decrypt_result);
}
}
}
/*!
\brief printf data by a block of 16 bytes
\param[in] datalength: length of the data to display
\param[in] data: pointer to the data to display
\param[out] none
\retval none
*/
static void data_display(uint32_t datalength, uint8_t *data)
{
uint32_t i = 0, count = 0;
for(i = 0; i < datalength; i++) {
printf("0x%02X ", data[i]);
count++;
/* display every 16 bytes in a block */
if(16 == count) {
count = 0;
printf(" [Block %d] \r\n", i / 16);
}
}
}
/*!
\brief display algorithm selection
\param[in] none
\param[out] none
\retval none
*/
static void algorithm_choose_display(void)
{
printf("========Choose CAU algorithm=======\r\n");
printf("1: DES algorithm \r\n");
printf("2: TDES algorithm \r\n");
printf("3: AES algorithm \r\n");
printf(" \r\n");
}
/*!
\brief display mode selection
\param[in] none
\param[out] none
\retval none
*/
static void mode_choose_display(void)
{
printf("========Choose CAU mode=======\r\n");
printf("1: ECB mode \r\n");
printf("2: CBC mode \r\n");
printf("3: CTR mode only when choose AES algorithm \r\n");
printf("4: GCM mode only when choose AES algorithm \r\n");
printf("5: CCM mode only when choose AES algorithm \r\n");
printf("6: CFB mode only when choose AES algorithm \r\n");
printf("7: OFB mode only when choose AES algorithm \r\n");
printf(" \r\n");
}
/*!
\brief algorithm selection
\param[in] none
\param[out] none
\retval none
*/
static void algorithm_choose(void)
{
uint8_t temp;
uint8_t flag;
flag = 1U;
do {
/* wait until an algorithm is chosen */
while(RESET == usart_flag_get(EVAL_COM, USART_FLAG_RBNE)) {
}
temp = usart_data_receive(EVAL_COM);
switch(temp) {
/* choose '1' to use DES algorithm */
case '1':
printf("You choose to use DES algorithm \r\n");
flag = 1U;
algorithm_mode_used = algorithm_mode_used | ALGORITHM_USE_DES;
break;
/* choose '2' to use TDES algorithm */
case '2':
printf("You choose to use TDES algorithm \r\n");
flag = 1U;
algorithm_mode_used = algorithm_mode_used | ALGORITHM_USE_TDES;
break;
/* choose '3' to use AES algorithm */
case '3':
printf("You choose to use AES algorithm \r\n");
flag = 1U;
algorithm_mode_used = algorithm_mode_used | ALGORITHM_USE_AES;
break;
default:
printf("Choose error: please choose again! \r\n");
flag = 0U;
}
} while(0U == flag);
}
/*!
\brief mode selection
\param[in] none
\param[out] none
\retval none
*/
static void mode_choose(void)
{
uint8_t temp;
uint8_t flag;
flag = 1U;
do {
/* wait until a mode is chosen */
while(RESET == usart_flag_get(EVAL_COM, USART_FLAG_RBNE)) {
}
temp = usart_data_receive(EVAL_COM);
switch(temp) {
/* choose '1' to use ECB mode */
case '1':
printf("You choose to use ECB mode \r\n");
flag = 1U;
algorithm_mode_used = algorithm_mode_used | MODE_USE_ECB;
break;
/* choose '2' to use CBC mode */
case '2':
printf("You choose to use CBC mode \r\n");
flag = 1U;
algorithm_mode_used = algorithm_mode_used | MODE_USE_CBC;
break;
/* choose '3' to use CTR mode */
case '3':
printf("You choose to use CTR mode \r\n");
flag = 1U;
algorithm_mode_used = algorithm_mode_used | MODE_USE_CTR;
break;
/* choose '4' to use GCM mode */
case '4':
printf("You choose to use GCM mode \r\n");
flag = 1U;
algorithm_mode_used = algorithm_mode_used | MODE_USE_GCM;
break;
/* choose '5' to use CCM mode */
case '5':
printf("You choose to use CCM mode \r\n");
flag = 1U;
algorithm_mode_used = algorithm_mode_used | MODE_USE_CCM;
break;
/* choose '6' to use CFB mode */
case '6':
printf("You choose to use CFB mode \r\n");
flag = 1U;
algorithm_mode_used = algorithm_mode_used | MODE_USE_CFB;
break;
/* choose '7' to use OFB mode */
case '7':
printf("You choose to use OFB mode \r\n");
flag = 1U;
algorithm_mode_used = algorithm_mode_used | MODE_USE_OFB;
break;
default:
printf("Choose error: please choose again! \r\n");
flag = 0U;
}
} while(0U == flag);
}
/* retarget the C library printf function to the USART */
int fputc(int ch, FILE *f)
{
usart_data_transmit(EVAL_COM, (uint8_t)ch);
while(RESET == usart_flag_get(EVAL_COM, USART_FLAG_TBE));
return ch;
}