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
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
|
/**
*********************************************************************************
*
* @file ald_crypt.c
* @brief CRYPT module driver.
* This is the common part of the CRYPT initialization
*
* @version V1.0
* @date 7 Dec 2017
* @author AE Team
* @note
*
* Copyright (C) Shanghai Eastsoft Microelectronics Co. Ltd. All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the License); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* www.apache.org/licenses/LICENSE-2.0
*
* 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.
*
*********************************************************************************
*/
#include "ald_crypt.h"
/** @addtogroup ES32FXXX_ALD
* @{
*/
/** @defgroup CRYPT CRYPT
* @brief CRYPT module driver
* @{
*/
#ifdef ALD_CRYPT
/** @addtogroup CRYPT_Private_Functions CRYPT Private Functions
* @{
*/
void crypt_reset(crypt_handle_t *hperh);
#ifdef ALD_DMA
static void crypt_dma_crypt_cplt(void *arg);
static void crypt_dma_error(void *arg);
#endif
/**
* @}
*/
/** @defgroup CRYPT_Public_Functions CRYPT Public Functions
* @{
*/
/** @defgroup CRYPT_Public_Functions_Group1 Initialization functions
* @brief Initialization and Configuration functions
* @{
*/
/**
* @brief Initializes the CRYPT mode according to the specified parameters in
* the crypt_init_t and create the associated handle.
* @param hperh: Pointer to a crypt_handle_t structure that contains
* the configuration information for the specified CRYPT module.
* @retval Status, see @ref ald_status_t.
*/
ald_status_t ald_crypt_init(crypt_handle_t *hperh)
{
uint32_t tmp = 0;
if (hperh == NULL)
return ERROR;
assert_param(IS_CRYPT(hperh->perh));
assert_param(IS_CRYPT_MODE(hperh->init.mode));
__LOCK(hperh);
crypt_reset(hperh);
if (hperh->state == CRYPT_STATE_RESET)
__UNLOCK(hperh);
tmp = hperh->perh->CON;
hperh->step = 4;
tmp |= ((1 << CRYPT_CON_FIFOODR_POS) | (hperh->init.mode << CRYPT_CON_MODE_POSS) | \
(hperh->init.type << CRYPT_CON_TYPE_POSS) | (1 << CRYPT_CON_FIFOEN_POS));
WRITE_REG(hperh->perh->CON, tmp);
hperh->state = CRYPT_STATE_READY;
__UNLOCK(hperh);
return OK;
}
/**
* @brief Write the Content of KEY.
* @param hperh: Pointer to a crypt_handle_t structure that contains
* the configuration information for the specified CRYPT module.
* @param key: Pointer to key data buffer
* @retval Status, see @ref ald_status_t.
*/
ald_status_t ald_crypt_write_key(crypt_handle_t *hperh, uint32_t *key)
{
uint32_t *temp = key;
uint32_t i;
if ((hperh == NULL) || (key == NULL))
return ERROR;
if (hperh->state == CRYPT_STATE_BUSY)
return BUSY;
assert_param(IS_CRYPT(hperh->perh));
hperh->perh->KEY[3] = *temp++;
hperh->perh->KEY[2] = *temp++;
hperh->perh->KEY[1] = *temp++;
hperh->perh->KEY[0] = *temp;
for (i = 0; i < 4; i++)
hperh->key[i] = *key++;
return OK;
}
/**
* @brief Read the Content of KEY.
* @param hperh: Pointer to a crypt_handle_t structure that contains
* the configuration information for the specified CRYPT module.
* @param key: The pointer to the key
* @retval Status, see @ref ald_status_t.
*/
ald_status_t ald_crypt_read_key(crypt_handle_t *hperh, uint32_t *key)
{
uint32_t *temp = key;
if ((hperh == NULL) || (key == NULL))
return ERROR;
if (hperh->state == CRYPT_STATE_BUSY)
return BUSY;
assert_param(IS_CRYPT(hperh->perh));
*temp++ = hperh->perh->KEY[3];
*temp++ = hperh->perh->KEY[2];
*temp++ = hperh->perh->KEY[1];
*temp = hperh->perh->KEY[0];
return OK;
}
/**
* @brief Write the Content of IV if you use CBC mode
* @param hperh: Pointer to a crypt_handle_t structure that contains
* the configuration information for the specified CRYPT module.
* @param iv: Pointer to iv data buffer
* @retval Status, see @ref ald_status_t.
*/
ald_status_t ald_crypt_write_ivr(crypt_handle_t *hperh, uint32_t *iv)
{
uint32_t *temp = iv;
uint32_t i;
if ((hperh == NULL) || (iv == NULL))
return ERROR;
if (hperh->state == CRYPT_STATE_BUSY)
return BUSY;
assert_param(IS_CRYPT(hperh->perh));
hperh->perh->IV[3] = *temp++;
hperh->perh->IV[2] = *temp++;
hperh->perh->IV[1] = *temp++;
hperh->perh->IV[0] = *temp;
for (i = 0; i < 4; i++)
hperh->iv[i] = *iv++;
CRYPT_IVEN_ENABLE(hperh);
return OK;
}
/**
* @brief Read the Content of IV.
* @param hperh: Pointer to a crypt_handle_t structure that contains
* the configuration information for the specified CRYPT module.
* @param iv: Pointer to iv data buffer
* @retval Status, see @ref ald_status_t.
*/
ald_status_t ald_crypt_read_ivr(crypt_handle_t *hperh, uint32_t *iv)
{
uint32_t *temp = iv;
if ((hperh == NULL) || (iv == NULL))
return ERROR;
if (hperh->state == CRYPT_STATE_BUSY)
return BUSY;
assert_param(IS_CRYPT(hperh->perh));
*temp++ = hperh->perh->IV[3];
*temp++ = hperh->perh->IV[2];
*temp++ = hperh->perh->IV[1];
*temp = hperh->perh->IV[0];
return OK;
}
/**
* @}
*/
/** @defgroup CRYPT_Public_Functions_Group2 Encrypt or Decrypt functions
* @brief Encrypt or Decrypt functions
* @{
*/
/**
* @brief Encrypt an amount of data in blocking mode.
* @param hperh: Pointer to a crypt_handle_t structure that contains
* the configuration information for the specified CRYPT module.
* @param plain_text: Pointer to plain data buffer
* @param cipher_text: Pointer to cipher data buffer
* @param size: Amount of plain data
* @retval Status, see @ref ald_status_t.
* @note the size is multiple of 16(ase)
*/
ald_status_t ald_crypt_encrypt(crypt_handle_t *hperh, uint8_t *plain_text, uint8_t *cipher_text, uint32_t size)
{
uint32_t count = 0;
uint32_t i;
uint32_t *plain_buf = (uint32_t *)plain_text;
uint32_t *cipher_buf = (uint32_t *)cipher_text;
if (hperh->state != CRYPT_STATE_READY)
return ERROR;
if ((plain_buf == NULL) || (cipher_buf == NULL) || (size == 0))
return ERROR;
assert_param(IS_CRYPT(hperh->perh));
__LOCK(hperh);
hperh->state = CRYPT_STATE_BUSY;
CRYPT_SETDIR(hperh, CRYPT_ENCRYPT);
count = size / (4 * hperh->step);
while (count--) {
for (i = 0; i < hperh->step; i++) {
CRYPT_WRITE_FIFO(hperh, *plain_buf);
plain_buf++;
}
while (ald_crypt_get_flag_status(hperh, CRYPT_FLAG_DONE) == SET);
for (i = 0; i < hperh->step; i++) {
*cipher_buf = CRYPT_READ_FIFO(hperh);
cipher_buf++;
}
}
hperh->state = CRYPT_STATE_READY;
__UNLOCK(hperh);
return OK;
}
/**
* @brief Decrypt an amount of data in blocking mode.
* @param hperh: Pointer to a crypt_handle_t structure that contains
* the configuration information for the specified CRYPT module.
* @param cipher_text: Pointer to cipher data buffer
* @param plain_text: Pointer to plain data buffer
* @param size: Amount of cipher data
* @retval Status, see @ref ald_status_t.
* @note the size is multiple of 16(ase)
*/
ald_status_t ald_crypt_decrypt(crypt_handle_t *hperh, uint8_t *cipher_text, uint8_t *plain_text, uint32_t size)
{
uint32_t count = 0;
uint32_t i;
uint32_t *plain_buf = (uint32_t*)plain_text;
uint32_t *cipher_buf = (uint32_t*)cipher_text;
if (hperh->init.mode == CRYPT_MODE_CTR) {
return ald_crypt_encrypt(hperh, cipher_text, plain_text, size);
}
if (hperh->state != CRYPT_STATE_READY)
return ERROR;
if ((plain_buf == NULL) || (cipher_buf == NULL) || (size == 0))
return ERROR;
assert_param(IS_CRYPT(hperh->perh));
__LOCK(hperh);
hperh->state = CRYPT_STATE_BUSY;
CRYPT_SETDIR(hperh, CRYPT_DECRYPT);
count = size / (4 * hperh->step);
while (count--) {
for (i = 0; i < hperh->step; i++) {
CRYPT_WRITE_FIFO(hperh, *cipher_buf);
cipher_buf++;
}
while (ald_crypt_get_flag_status(hperh, CRYPT_FLAG_DONE) == SET);
for (i = 0; i < hperh->step; i++) {
*plain_buf = CRYPT_READ_FIFO(hperh);
plain_buf++;
}
}
hperh->state = CRYPT_STATE_READY;
__UNLOCK(hperh);
return OK;
}
void gcm_mul(uint32_t *res, uint32_t *data, uint32_t *iv)
{
CRYPT->CON = 0;
CRYPT->DATA[0] = data[3];
CRYPT->DATA[1] = data[2];
CRYPT->DATA[2] = data[1];
CRYPT->DATA[3] = data[0];
CRYPT->IV[0] = iv[3];
CRYPT->IV[1] = iv[2];
CRYPT->IV[2] = iv[1];
CRYPT->IV[3] = iv[0];
CRYPT->CON |= ((1 << CRYPT_CON_RESCLR_POS) | (3 << CRYPT_CON_MODE_POSS) | \
(1 << CRYPT_CON_GO_POS));
while (READ_BIT(CRYPT->IF, CRYPT_IF_MULTHIF_MSK) == 0);
res[3] = CRYPT->RES[0];
res[2] = CRYPT->RES[1];
res[1] = CRYPT->RES[2];
res[0] = CRYPT->RES[3];
WRITE_REG(CRYPT->IFC, CRYPT_IFC_MULTHIFC_MSK);
return;
}
/**
* @brief verify an amount of data in gcm mode.
* @param hperh: Pointer to a crypt_handle_t structure that contains
* the configuration information for the specified CRYPT module.
* @param cipher_text: Pointer to cipher data buffer
* @param size: Amount of cipher data
* @param aadata: Pointer to additional authenticated data buffer
* @param alen: Amount of additional authenticated data
* @param tag: Pointer to authentication tag buffer
* @retval Status, see @ref ald_status_t.
*/
ald_status_t ald_crypt_gcm_verify(crypt_handle_t *hperh, uint8_t *cipher_text, uint32_t size, uint8_t *aadata, uint32_t alen, uint8_t *tag)
{
uint8_t GCM_HASH_in[0x60] = {0};
uint8_t ecb[16] = {0};
uint32_t x_temp[4];
uint64_t u, v;
uint32_t len = 0;
uint32_t j, i, k;
uint32_t *tag_temp, *cipher_text_temp;
/* calculate u and v */
u = 128 * ((size % 16) ? (size / 16 + 1) : size / 16) - size * 8;
v = 128 * ((alen % 16) ? (alen / 16 + 1): alen / 16) - alen * 8;
/* get the input of GHASH algorithm,the input:A||0^v||C||0^u||[len(A)]_64||[len(C)]_64 */
for (i = 0; i < alen; i++) {
GCM_HASH_in [i] = * (aadata + i);
}
len += alen;
for (i = 0; i < v / 8; i++) {
GCM_HASH_in[i + len] = 0;
}
len += v / 8;
for (i = 0; i < size; i++) {
GCM_HASH_in[i + len] = * (cipher_text + i);
}
len += size;
for (i = 0; i < u / 8; i++) {
GCM_HASH_in[i + len] = 0;
}
len += u / 8;
for (i = 0; i < 4; i++) {
GCM_HASH_in[i + len] = 0;
}
len += 4;
for (i = 0; i < 4; i++) {
GCM_HASH_in[i + len] = ((alen * 8) >> (8 * i)) & 0xFF;
}
len += 4;
for (i = 0; i < 4; i++) {
GCM_HASH_in[i + len] = 0;
}
len += 4;
for (i = 0; i < 4; i++) {
GCM_HASH_in[i + len] = ((size * 8) >> (8 * i)) & 0xFF;
}
len += 4;
CRYPT->CON &= ~(3U << CRYPT_CON_MODE_POSS);
CRYPT->CON |= (CRYPT_MODE_ECB << CRYPT_CON_MODE_POSS);
ald_crypt_encrypt(hperh, ecb, ecb, 16);
k = len / 16;
for (i = 0; i < 16; i++) {
tag[i] = 0;
}
cipher_text_temp = (uint32_t *)GCM_HASH_in;
tag_temp = (uint32_t *)tag;
for (i = 0; i < k; i++) {
for (j = 0; j < 4; j++) {
x_temp[j] = (*cipher_text_temp) ^ tag_temp[j];
++cipher_text_temp;
}
gcm_mul((uint32_t *)tag_temp, x_temp, (uint32_t *)ecb);
}
/* calculate the authentication tag T,
* T = CIPH_K(J0)^S,J0=IV||0^31||1,CIPH_K is the algorithm of AES in ECB mode
*/
tag_temp = (uint32_t *)tag;
ald_crypt_init(hperh);
CRYPT->CON &= ~(3U << CRYPT_CON_MODE_POSS);
CRYPT->CON |= (CRYPT_MODE_CTR << CRYPT_CON_MODE_POSS);
ald_crypt_write_key(hperh, hperh->key);
hperh->iv[3] = 1;
ald_crypt_write_ivr(hperh, hperh->iv);
ald_crypt_encrypt(hperh, tag, tag, 16);
return OK;
}
/**
* @brief Encrypt an amount of data in non-blocking mode.
* @param hperh: Pointer to a crypt_handle_t structure that contains
* the configuration information for the specified CRYPT module.
* @param plain_text: Pointer to plain data buffer
* @param cipher_text: Pointer to cipher data buffer
* @param size: Amount of plain data
* @retval Status, see @ref ald_status_t.
* @note the size is multiple of 16(ase)
*/
ald_status_t ald_crypt_encrypt_by_it(crypt_handle_t *hperh, uint8_t * plain_text, uint8_t *cipher_text, uint32_t size)
{
uint32_t i;
uint32_t *plain_buf = (uint32_t *)plain_text;
if (hperh->state != CRYPT_STATE_READY)
return ERROR;
if ((plain_text == NULL) || (cipher_text == NULL) || (size == 0))
return ERROR;
assert_param(IS_CRYPT(hperh->perh));
__LOCK(hperh);
hperh->state = CRYPT_STATE_BUSY;
CRYPT_SETDIR(hperh, CRYPT_ENCRYPT);
hperh->count = hperh->step;
hperh->plain_text = plain_text;
hperh->cipher_text = cipher_text;
hperh->size = size;
ald_crypt_interrupt_config(hperh, CRYPT_IT_IT, ENABLE);
for (i = 0; i < hperh->step; i++) {
CRYPT_WRITE_FIFO(hperh, *plain_buf);
++plain_buf;
}
__UNLOCK(hperh);
return OK;
}
/**
* @brief Decrypt an amount of data in non-blocking mode.
* @param hperh: Pointer to a crypt_handle_t structure that contains
* the configuration information for the specified CRYPT module.
* @param plain_text: Pointer to plain data buffer
* @param cipher_text: Pointer to cipher data buffer
* @param size: Amount of cipher data
* @retval Status, see @ref ald_status_t.
* @note the size is multiple of 16(ase)
*/
ald_status_t ald_crypt_decrypt_by_it(crypt_handle_t *hperh, uint8_t *cipher_text, uint8_t *plain_text, uint32_t size)
{
uint32_t i;
uint32_t *cipher_buf = (uint32_t*)cipher_text;
if (hperh->init.mode == CRYPT_MODE_CTR) {
return ald_crypt_decrypt_by_it(hperh, cipher_text, plain_text, size);
}
if (hperh->state != CRYPT_STATE_READY)
return ERROR;
if ((plain_text == NULL) || (cipher_text == NULL) || (size == 0))
return ERROR;
assert_param(IS_CRYPT(hperh->perh));
__LOCK(hperh);
hperh->state = CRYPT_STATE_BUSY;
CRYPT_SETDIR(hperh, CRYPT_DECRYPT);
hperh->count = hperh->step;
hperh->plain_text = plain_text;
hperh->cipher_text = cipher_text;
hperh->size = size;
ald_crypt_interrupt_config(hperh, CRYPT_IT_IT, ENABLE);
for (i = 0; i < hperh->step; i++) {
CRYPT_WRITE_FIFO(hperh, *cipher_buf);
cipher_buf++;
}
__UNLOCK(hperh);
return OK;
}
#ifdef ALD_DMA
/**
* @brief Encrypt an amount of data in non-blocking mode.
* @param hperh: Pointer to a crypt_handle_t structure that contains
* the configuration information for the specified CRYPT module.
* @param plain_text: Pointer to plain data buffer
* @param cipher_text: Pointer to cipher data buffer
* @param size: Amount of plain data
* @param channel_m2p: Memory to Crypt module DMA channel
* @param channel_p2m: Crypt module to Memory DMA channel
* @retval Status, see @ref ald_status_t.
* @note the size is multiple of 16(ase)
*/
ald_status_t ald_crypt_encrypt_by_dma(crypt_handle_t *hperh, uint8_t * plain_text,
uint8_t *cipher_text, uint32_t size, uint8_t channel_m2p, uint8_t channel_p2m)
{
if (hperh->state != CRYPT_STATE_READY)
return ERROR;
if (plain_text == NULL || cipher_text == NULL || size == 0)
return ERROR;
assert_param(IS_CRYPT(hperh->perh));
__LOCK(hperh);
hperh->state = CRYPT_STATE_BUSY;
hperh->plain_text = plain_text;
hperh->cipher_text = cipher_text;
hperh->size = size;
hperh->count = size;
if (hperh->hdma_m2p.perh == NULL)
hperh->hdma_m2p.perh = DMA0;
if (hperh->hdma_p2m.perh == NULL)
hperh->hdma_p2m.perh = DMA0;
hperh->hdma_m2p.cplt_arg = NULL;
hperh->hdma_m2p.cplt_cbk = NULL;
hperh->hdma_m2p.err_arg = NULL;
hperh->hdma_m2p.err_cbk = NULL;
hperh->hdma_p2m.cplt_arg = (void *)hperh;
hperh->hdma_p2m.cplt_cbk = &crypt_dma_crypt_cplt;
hperh->hdma_p2m.err_arg = (void *)hperh;
hperh->hdma_p2m.err_cbk = &crypt_dma_error;
CRYPT_SETDIR(hperh, CRYPT_ENCRYPT);
ald_dma_config_struct(&hperh->hdma_m2p.config);
hperh->hdma_m2p.config.data_width = DMA_DATA_SIZE_WORD;
hperh->hdma_m2p.config.src = (void *)hperh->plain_text;
hperh->hdma_m2p.config.dst = (void *)&hperh->perh->FIFO;
hperh->hdma_m2p.config.size = size / 4;
hperh->hdma_m2p.config.src_inc = DMA_DATA_INC_WORD;
hperh->hdma_m2p.config.dst_inc = DMA_DATA_INC_NONE;
hperh->hdma_m2p.config.msel = DMA_MSEL_CRYPT;
hperh->hdma_m2p.config.msigsel = DMA_MSIGSEL_CRYPT_WRITE;
hperh->hdma_m2p.config.channel = channel_m2p;
ald_dma_config_basic(&(hperh->hdma_m2p));
ald_dma_config_struct(&hperh->hdma_p2m.config);
hperh->hdma_p2m.config.data_width = DMA_DATA_SIZE_WORD;
hperh->hdma_p2m.config.src = (void *)&hperh->perh->FIFO;
hperh->hdma_p2m.config.dst = (void *)hperh->cipher_text;
hperh->hdma_p2m.config.size = size / 4;
hperh->hdma_p2m.config.src_inc = DMA_DATA_INC_NONE;
hperh->hdma_p2m.config.dst_inc = DMA_DATA_INC_WORD;
hperh->hdma_p2m.config.msel = DMA_MSEL_CRYPT;
hperh->hdma_p2m.config.msigsel = DMA_MSIGSEL_CRYPT_READ;
hperh->hdma_p2m.config.channel = channel_p2m;
ald_dma_config_basic(&(hperh->hdma_p2m));
CRYPT_DMA_ENABLE(hperh);
__UNLOCK(hperh);
return OK;
}
/**
* @brief Decrypt an amount of data in non-blocking mode.
* @param hperh: Pointer to a crypt_handle_t structure that contains
* the configuration information for the specified CRYPT module.
* @param plain_text: Pointer to plain data buffer
* @param cipher_text: Pointer to cipher data buffer
* @param size: Amount of cipher data
* @param channel_m2p: Memory to Crypt module DMA channel
* @param channel_p2m: Crypt module to Memory DMA channel
* @retval Status, see @ref ald_status_t.
* @note the size is multiple of 16(ase)
*/
ald_status_t ald_crypt_decrypt_by_dma(crypt_handle_t *hperh, uint8_t * cipher_text,
uint8_t *plain_text, uint32_t size, uint8_t channel_m2p, uint8_t channel_p2m)
{
if (hperh->init.mode == CRYPT_MODE_CTR)
return ald_crypt_decrypt_by_dma(hperh, cipher_text, plain_text, size, channel_m2p, channel_p2m);
if (hperh->state != CRYPT_STATE_READY)
return ERROR;
if (plain_text == NULL || cipher_text == NULL || size == 0)
return ERROR;
__LOCK(hperh);
hperh->state = CRYPT_STATE_BUSY;
hperh->plain_text = plain_text;
hperh->cipher_text = cipher_text;
hperh->size = size;
hperh->count = size;
if (hperh->hdma_m2p.perh == NULL)
hperh->hdma_m2p.perh = DMA0;
if (hperh->hdma_p2m.perh == NULL)
hperh->hdma_p2m.perh = DMA0;
hperh->hdma_m2p.cplt_arg = NULL;
hperh->hdma_m2p.cplt_cbk = NULL;
hperh->hdma_m2p.err_arg = NULL;
hperh->hdma_m2p.err_cbk = NULL;
hperh->hdma_p2m.cplt_arg = (void *)hperh;
hperh->hdma_p2m.cplt_cbk = &crypt_dma_crypt_cplt;
hperh->hdma_p2m.err_arg = (void *)hperh;
hperh->hdma_p2m.err_cbk = &crypt_dma_error;
CRYPT_SETDIR(hperh, CRYPT_DECRYPT);
ald_dma_config_struct(&hperh->hdma_m2p.config);
hperh->hdma_m2p.config.data_width = DMA_DATA_SIZE_WORD;
hperh->hdma_m2p.config.src = (void *)hperh->cipher_text;
hperh->hdma_m2p.config.dst = (void *)&hperh->perh->FIFO;
hperh->hdma_m2p.config.size = size / 4;
hperh->hdma_m2p.config.src_inc = DMA_DATA_INC_WORD;
hperh->hdma_m2p.config.dst_inc = DMA_DATA_INC_NONE;
hperh->hdma_m2p.config.msel = DMA_MSEL_CRYPT;
hperh->hdma_m2p.config.msigsel = DMA_MSIGSEL_CRYPT_WRITE;
hperh->hdma_m2p.config.channel = channel_m2p;
ald_dma_config_basic(&(hperh->hdma_m2p));
ald_dma_config_struct(&hperh->hdma_p2m.config);
hperh->hdma_p2m.config.data_width = DMA_DATA_SIZE_WORD;
hperh->hdma_p2m.config.src = (void *)&hperh->perh->FIFO;
hperh->hdma_p2m.config.dst = (void *)hperh->plain_text;
hperh->hdma_p2m.config.size = size / 4;
hperh->hdma_p2m.config.src_inc = DMA_DATA_INC_NONE;
hperh->hdma_p2m.config.dst_inc = DMA_DATA_INC_WORD;
hperh->hdma_p2m.config.msel = DMA_MSEL_CRYPT;
hperh->hdma_p2m.config.msigsel = DMA_MSIGSEL_CRYPT_READ;
hperh->hdma_p2m.config.channel = channel_p2m;
ald_dma_config_basic(&(hperh->hdma_p2m));
CRYPT_DMA_ENABLE(hperh);
__UNLOCK(hperh);
return OK;
}
/**
* @}
*/
/** @defgroup CRYPT_Public_Functions_Group3 DMA operation functions
* @brief DMA operation functions
* @{
*/
/**
* @brief Pauses the DMA Transfer.
* @param hperh: Pointer to a crypt_handle_t structure that contains
* the configuration information for the specified CRYPT module.
* @retval Status, see @ref ald_status_t.
*/
ald_status_t ald_crypt_dma_pause(crypt_handle_t *hperh)
{
__LOCK(hperh);
CRYPT_DMA_DISABLE(hperh);
__UNLOCK(hperh);
return OK;
}
/**
* @brief Resumes the DMA Transfer.
* @param hperh: Pointer to a crypt_handle_t structure that contains
* the configuration information for the specified CRYPT module.
* @retval Status, see @ref ald_status_t.
*/
ald_status_t ald_crypt_dma_resume(crypt_handle_t *hperh)
{
__LOCK(hperh);
CRYPT_DMA_ENABLE(hperh);
__UNLOCK(hperh);
return OK;
}
/**
* @brief Stops the DMA Transfer.
* @param hperh: Pointer to a crypt_handle_t structure that contains
* the configuration information for the specified CRYPT module.
* @retval Status, see @ref ald_status_t.
*/
ald_status_t ald_crypt_dma_stop(crypt_handle_t *hperh)
{
__LOCK(hperh);
CRYPT_DMA_DISABLE(hperh);
__UNLOCK(hperh);
hperh->state = CRYPT_STATE_READY;
return OK;
}
#endif
/**
* @brief This function handles CRYPT interrupt request.
* @param hperh: Pointer to a crypt_handle_t structure that contains
* the configuration information for the specified CRYPT module.
* @retval None
*/
void ald_crypt_irq_handler(crypt_handle_t *hperh)
{
uint32_t i;
uint32_t *in_buf;
uint32_t *out_buf;
if (READ_BIT(hperh->perh->CON, CRYPT_CON_ENCS_MSK)) {
in_buf = (uint32_t *)hperh->plain_text + hperh->count;
out_buf = (uint32_t *)hperh->cipher_text + hperh->count - hperh->step;
}
else {
in_buf = (uint32_t *)hperh->cipher_text + hperh->count;
out_buf = (uint32_t *)hperh->plain_text + hperh->count - hperh->step;
}
if (ald_crypt_get_flag_status(hperh, CRYPT_FLAG_AESIF) == SET) {
ald_crypt_clear_flag_status(hperh, CRYPT_FLAG_AESIF);
}
for (i = 0; i < hperh->step; i++)
*out_buf++ = CRYPT_READ_FIFO(hperh);
hperh->count += hperh->step;
if (hperh->count > (hperh->size / 4)) {
hperh->count = 0;
hperh->state = CRYPT_STATE_READY;
if (hperh->crypt_cplt_cbk)
hperh->crypt_cplt_cbk(hperh);
}
else {
for (i = 0; i < hperh->step; i++) {
CRYPT_WRITE_FIFO(hperh, *in_buf++);
}
}
}
/**
* @}
*/
/** @defgroup CRYPT_Public_Functions_Group4 Peripheral Control functions
* @brief CRYPT control functions
* @{
*/
/**
* @brief Enables or disables the specified CRYPT interrupts.
* @param hperh: Pointer to a crypt_handle_t structure that contains
* the configuration information for the specified CRYPT module.
* @param it: Specifies the CRYPT interrupt sources to be enabled or disabled.
* This parameter can be one of the following values:
* @arg crypt_it_t: CRYPT interrupt
* @param state: New status
* - ENABLE
* - DISABLE
* @retval None
*/
void ald_crypt_interrupt_config(crypt_handle_t *hperh, crypt_it_t it, type_func_t state)
{
assert_param(IS_CRYPT(hperh->perh));
if (it == CRYPT_IT_IT) {
CLEAR_BIT(CRYPT->CON, CRYPT_CON_IE_MSK);
CRYPT->CON |= (state << CRYPT_CON_IE_POS);
}
return;
}
/** @brief Check whether the specified CRYPT flag is set or not.
* @param hperh: Pointer to a crypt_handle_t structure that contains
* the configuration information for the specified CRYPT module.
* @param flag: specifies the flag to check.
* This parameter can be one of the @ref crypt_flag_t.
* @retval Status
* - SET
* - RESET
*/
flag_status_t ald_crypt_get_flag_status(crypt_handle_t *hperh, crypt_flag_t flag)
{
assert_param(IS_CRYPT(hperh->perh));
assert_param(IS_CRYPT_FLAG(flag));
if (CRYPT->IF & flag)
return SET;
return RESET;
}
/** @brief Clear the specified CRYPT pending flags.
* @param hperh: Pointer to a crypt_handle_t structure that contains
* the configuration information for the specified CRYPT module.
* @param flag: specifies the flag to check.
* This parameter can be any combination of the following values:
* @arg CRYPT_FLAG_AESIF: AES encrypt or decrypt Complete flag.
* @arg CRYPT_FLAG_DONE: encrypt or decrypt Complete flag.
* @retval None
*/
void ald_crypt_clear_flag_status(crypt_handle_t *hperh, crypt_flag_t flag)
{
assert_param(IS_CRYPT(hperh->perh));
assert_param(IS_CRYPT_FLAG(flag));
WRITE_REG(CRYPT->IFC, flag);
return;
}
/**
* @brief Checks whether the specified CRYPT interrupt has occurred or not.
* @param hperh: Pointer to a crypt_handle_t structure that contains
* the configuration information for the specified CRYPT module.
* @param it: Specifies the CRYPT interrupt source to check.
* This parameter can be one of the following values:
* @arg crypt_it_t: CRYPT interrupt
* @retval Status
* - SET
* - RESET
*/
it_status_t ald_crypt_get_it_status(crypt_handle_t *hperh, crypt_it_t it)
{
assert_param(IS_CRYPT_IT(it));
if (READ_BIT(CRYPT->CON, CRYPT_CON_IE_MSK))
return SET;
return RESET;
}
/**
* @}
*/
/** @defgroup CRYPT_Public_Functions_Group5 Peripheral State and Errors functions
* @brief State and Errors functions
* @{
*/
/**
* @brief Returns the CRYPT state.
* @param hperh: Pointer to a crypt_handle_t structure that contains
* the configuration information for the specified CRYPT module.
* @retval CRYPT state
*/
crypt_state_t ald_crypt_get_state(crypt_handle_t *hperh)
{
assert_param(IS_CRYPT(hperh->perh));
return hperh->state;
}
/**
* @}
*/
/**
* @}
*/
/** @defgroup CRYPT_Private_Functions CRYPT Private Functions
* @brief CRYPT Private functions
* @{
*/
/**
* @brief Reset the CRYPT peripheral.
* @param hperh: Pointer to a crypt_handle_t structure that contains
* the configuration information for the specified CRYPT module.
* @retval None
*/
void crypt_reset(crypt_handle_t *hperh)
{
hperh->perh->DATA[0] = 0x0;
hperh->perh->DATA[1] = 0x0;
hperh->perh->DATA[2] = 0x0;
hperh->perh->DATA[3] = 0x0;
hperh->perh->KEY[0] = 0x0;
hperh->perh->KEY[1] = 0x0;
hperh->perh->KEY[2] = 0x0;
hperh->perh->KEY[3] = 0x0;
hperh->perh->KEY[4] = 0x0;
hperh->perh->KEY[5] = 0x0;
hperh->perh->KEY[6] = 0x0;
hperh->perh->KEY[7] = 0x0;
hperh->perh->IV[0] = 0x0;
hperh->perh->IV[1] = 0x0;
hperh->perh->IV[2] = 0x0;
hperh->perh->IV[3] = 0x0;
hperh->perh->CON = 0x0;
hperh->state = CRYPT_STATE_READY;
__UNLOCK(hperh);
}
#ifdef ALD_DMA
/**
* @brief DMA CRYPT encrypt or decrypt process complete callback.
* @param arg: Pointer to a crypt_handle_t structure that contains
* the configuration information for the specified CRYPT module.
* @retval None
*/
static void crypt_dma_crypt_cplt(void *arg)
{
crypt_handle_t *hperh = (crypt_handle_t *)arg;
CRYPT_DMA_DISABLE(hperh);
hperh->count = 0;
hperh->plain_text = NULL;
hperh->cipher_text = NULL;
hperh->size = 0;
hperh->state = CRYPT_STATE_READY;
if (hperh->crypt_cplt_cbk)
hperh->crypt_cplt_cbk(hperh);
}
/**
* @brief DMA CRYPT communication error callback.
* @param arg: Pointer to a crypt_handle_t structure that contains
* the configuration information for the specified CRYPT module.
* @retval None
*/
static void crypt_dma_error(void *arg)
{
crypt_handle_t *hperh = (crypt_handle_t *)arg;
CRYPT_DMA_DISABLE(hperh);
hperh->count = 0;
hperh->plain_text = NULL;
hperh->cipher_text = NULL;
hperh->size = 0;
hperh->state = CRYPT_STATE_READY;
if (hperh->err_cplt_cbk)
hperh->err_cplt_cbk(hperh);
}
#endif
/**
* @}
*/
/**
* @}
*/
#endif /* ALD_CRYPT */
/**
* @}
*/
|