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
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
|
// SPDX-License-Identifier: GPL-2.0
/*
* StarFive AES acceleration driver
*
* Copyright (c) 2022 StarFive Technology
*/
#include <crypto/engine.h>
#include <crypto/gcm.h>
#include <crypto/internal/aead.h>
#include <crypto/internal/skcipher.h>
#include <crypto/scatterwalk.h>
#include "jh7110-cryp.h"
#include <linux/err.h>
#include <linux/iopoll.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/string.h>
#define STARFIVE_AES_REGS_OFFSET 0x100
#define STARFIVE_AES_AESDIO0R (STARFIVE_AES_REGS_OFFSET + 0x0)
#define STARFIVE_AES_KEY0 (STARFIVE_AES_REGS_OFFSET + 0x4)
#define STARFIVE_AES_KEY1 (STARFIVE_AES_REGS_OFFSET + 0x8)
#define STARFIVE_AES_KEY2 (STARFIVE_AES_REGS_OFFSET + 0xC)
#define STARFIVE_AES_KEY3 (STARFIVE_AES_REGS_OFFSET + 0x10)
#define STARFIVE_AES_KEY4 (STARFIVE_AES_REGS_OFFSET + 0x14)
#define STARFIVE_AES_KEY5 (STARFIVE_AES_REGS_OFFSET + 0x18)
#define STARFIVE_AES_KEY6 (STARFIVE_AES_REGS_OFFSET + 0x1C)
#define STARFIVE_AES_KEY7 (STARFIVE_AES_REGS_OFFSET + 0x20)
#define STARFIVE_AES_CSR (STARFIVE_AES_REGS_OFFSET + 0x24)
#define STARFIVE_AES_IV0 (STARFIVE_AES_REGS_OFFSET + 0x28)
#define STARFIVE_AES_IV1 (STARFIVE_AES_REGS_OFFSET + 0x2C)
#define STARFIVE_AES_IV2 (STARFIVE_AES_REGS_OFFSET + 0x30)
#define STARFIVE_AES_IV3 (STARFIVE_AES_REGS_OFFSET + 0x34)
#define STARFIVE_AES_NONCE0 (STARFIVE_AES_REGS_OFFSET + 0x3C)
#define STARFIVE_AES_NONCE1 (STARFIVE_AES_REGS_OFFSET + 0x40)
#define STARFIVE_AES_NONCE2 (STARFIVE_AES_REGS_OFFSET + 0x44)
#define STARFIVE_AES_NONCE3 (STARFIVE_AES_REGS_OFFSET + 0x48)
#define STARFIVE_AES_ALEN0 (STARFIVE_AES_REGS_OFFSET + 0x4C)
#define STARFIVE_AES_ALEN1 (STARFIVE_AES_REGS_OFFSET + 0x50)
#define STARFIVE_AES_MLEN0 (STARFIVE_AES_REGS_OFFSET + 0x54)
#define STARFIVE_AES_MLEN1 (STARFIVE_AES_REGS_OFFSET + 0x58)
#define STARFIVE_AES_IVLEN (STARFIVE_AES_REGS_OFFSET + 0x5C)
#define FLG_MODE_MASK GENMASK(2, 0)
#define FLG_ENCRYPT BIT(4)
/* Misc */
#define CCM_B0_ADATA 0x40
#define AES_BLOCK_32 (AES_BLOCK_SIZE / sizeof(u32))
static inline int starfive_aes_wait_busy(struct starfive_cryp_dev *cryp)
{
u32 status;
return readl_relaxed_poll_timeout(cryp->base + STARFIVE_AES_CSR, status,
!(status & STARFIVE_AES_BUSY), 10, 100000);
}
static inline int starfive_aes_wait_keydone(struct starfive_cryp_dev *cryp)
{
u32 status;
return readl_relaxed_poll_timeout(cryp->base + STARFIVE_AES_CSR, status,
(status & STARFIVE_AES_KEY_DONE), 10, 100000);
}
static inline int starfive_aes_wait_gcmdone(struct starfive_cryp_dev *cryp)
{
u32 status;
return readl_relaxed_poll_timeout(cryp->base + STARFIVE_AES_CSR, status,
(status & STARFIVE_AES_GCM_DONE), 10, 100000);
}
static inline int is_gcm(struct starfive_cryp_dev *cryp)
{
return (cryp->flags & FLG_MODE_MASK) == STARFIVE_AES_MODE_GCM;
}
static inline bool is_encrypt(struct starfive_cryp_dev *cryp)
{
return cryp->flags & FLG_ENCRYPT;
}
static void starfive_aes_aead_hw_start(struct starfive_cryp_ctx *ctx, u32 hw_mode)
{
struct starfive_cryp_dev *cryp = ctx->cryp;
unsigned int value;
switch (hw_mode) {
case STARFIVE_AES_MODE_GCM:
value = readl(ctx->cryp->base + STARFIVE_AES_CSR);
value |= STARFIVE_AES_GCM_START;
writel(value, cryp->base + STARFIVE_AES_CSR);
starfive_aes_wait_gcmdone(cryp);
break;
case STARFIVE_AES_MODE_CCM:
value = readl(ctx->cryp->base + STARFIVE_AES_CSR);
value |= STARFIVE_AES_CCM_START;
writel(value, cryp->base + STARFIVE_AES_CSR);
break;
}
}
static inline void starfive_aes_set_alen(struct starfive_cryp_ctx *ctx)
{
struct starfive_cryp_dev *cryp = ctx->cryp;
writel(upper_32_bits(cryp->assoclen), cryp->base + STARFIVE_AES_ALEN0);
writel(lower_32_bits(cryp->assoclen), cryp->base + STARFIVE_AES_ALEN1);
}
static inline void starfive_aes_set_mlen(struct starfive_cryp_ctx *ctx)
{
struct starfive_cryp_dev *cryp = ctx->cryp;
writel(upper_32_bits(cryp->total_in), cryp->base + STARFIVE_AES_MLEN0);
writel(lower_32_bits(cryp->total_in), cryp->base + STARFIVE_AES_MLEN1);
}
static inline int starfive_aes_ccm_check_iv(const u8 *iv)
{
/* 2 <= L <= 8, so 1 <= L' <= 7. */
if (iv[0] < 1 || iv[0] > 7)
return -EINVAL;
return 0;
}
static int starfive_aes_write_iv(struct starfive_cryp_ctx *ctx, u32 *iv)
{
struct starfive_cryp_dev *cryp = ctx->cryp;
writel(iv[0], cryp->base + STARFIVE_AES_IV0);
writel(iv[1], cryp->base + STARFIVE_AES_IV1);
writel(iv[2], cryp->base + STARFIVE_AES_IV2);
if (is_gcm(cryp)) {
if (starfive_aes_wait_gcmdone(cryp))
return -ETIMEDOUT;
return 0;
}
writel(iv[3], cryp->base + STARFIVE_AES_IV3);
return 0;
}
static inline void starfive_aes_get_iv(struct starfive_cryp_dev *cryp, u32 *iv)
{
iv[0] = readl(cryp->base + STARFIVE_AES_IV0);
iv[1] = readl(cryp->base + STARFIVE_AES_IV1);
iv[2] = readl(cryp->base + STARFIVE_AES_IV2);
iv[3] = readl(cryp->base + STARFIVE_AES_IV3);
}
static inline void starfive_aes_write_nonce(struct starfive_cryp_ctx *ctx, u32 *nonce)
{
struct starfive_cryp_dev *cryp = ctx->cryp;
writel(nonce[0], cryp->base + STARFIVE_AES_NONCE0);
writel(nonce[1], cryp->base + STARFIVE_AES_NONCE1);
writel(nonce[2], cryp->base + STARFIVE_AES_NONCE2);
writel(nonce[3], cryp->base + STARFIVE_AES_NONCE3);
}
static int starfive_aes_write_key(struct starfive_cryp_ctx *ctx)
{
struct starfive_cryp_dev *cryp = ctx->cryp;
u32 *key = (u32 *)ctx->key;
if (ctx->keylen >= AES_KEYSIZE_128) {
writel(key[0], cryp->base + STARFIVE_AES_KEY0);
writel(key[1], cryp->base + STARFIVE_AES_KEY1);
writel(key[2], cryp->base + STARFIVE_AES_KEY2);
writel(key[3], cryp->base + STARFIVE_AES_KEY3);
}
if (ctx->keylen >= AES_KEYSIZE_192) {
writel(key[4], cryp->base + STARFIVE_AES_KEY4);
writel(key[5], cryp->base + STARFIVE_AES_KEY5);
}
if (ctx->keylen >= AES_KEYSIZE_256) {
writel(key[6], cryp->base + STARFIVE_AES_KEY6);
writel(key[7], cryp->base + STARFIVE_AES_KEY7);
}
if (starfive_aes_wait_keydone(cryp))
return -ETIMEDOUT;
return 0;
}
static int starfive_aes_ccm_init(struct starfive_cryp_ctx *ctx)
{
struct starfive_cryp_dev *cryp = ctx->cryp;
u8 iv[AES_BLOCK_SIZE], b0[AES_BLOCK_SIZE];
unsigned int textlen;
memcpy(iv, cryp->req.areq->iv, AES_BLOCK_SIZE);
memset(iv + AES_BLOCK_SIZE - 1 - iv[0], 0, iv[0] + 1);
/* Build B0 */
memcpy(b0, iv, AES_BLOCK_SIZE);
b0[0] |= (8 * ((cryp->authsize - 2) / 2));
if (cryp->assoclen)
b0[0] |= CCM_B0_ADATA;
textlen = cryp->total_in;
b0[AES_BLOCK_SIZE - 2] = textlen >> 8;
b0[AES_BLOCK_SIZE - 1] = textlen & 0xFF;
starfive_aes_write_nonce(ctx, (u32 *)b0);
return 0;
}
static int starfive_aes_hw_init(struct starfive_cryp_ctx *ctx)
{
struct starfive_cryp_request_ctx *rctx = ctx->rctx;
struct starfive_cryp_dev *cryp = ctx->cryp;
u32 hw_mode;
/* reset */
rctx->csr.aes.v = 0;
rctx->csr.aes.aesrst = 1;
writel(rctx->csr.aes.v, cryp->base + STARFIVE_AES_CSR);
/* csr setup */
hw_mode = cryp->flags & FLG_MODE_MASK;
rctx->csr.aes.v = 0;
switch (ctx->keylen) {
case AES_KEYSIZE_128:
rctx->csr.aes.keymode = STARFIVE_AES_KEYMODE_128;
break;
case AES_KEYSIZE_192:
rctx->csr.aes.keymode = STARFIVE_AES_KEYMODE_192;
break;
case AES_KEYSIZE_256:
rctx->csr.aes.keymode = STARFIVE_AES_KEYMODE_256;
break;
}
rctx->csr.aes.mode = hw_mode;
rctx->csr.aes.cmode = !is_encrypt(cryp);
rctx->csr.aes.stmode = STARFIVE_AES_MODE_XFB_1;
if (cryp->side_chan) {
rctx->csr.aes.delay_aes = 1;
rctx->csr.aes.vaes_start = 1;
}
writel(rctx->csr.aes.v, cryp->base + STARFIVE_AES_CSR);
cryp->err = starfive_aes_write_key(ctx);
if (cryp->err)
return cryp->err;
switch (hw_mode) {
case STARFIVE_AES_MODE_GCM:
starfive_aes_set_alen(ctx);
starfive_aes_set_mlen(ctx);
writel(GCM_AES_IV_SIZE, cryp->base + STARFIVE_AES_IVLEN);
starfive_aes_aead_hw_start(ctx, hw_mode);
starfive_aes_write_iv(ctx, (void *)cryp->req.areq->iv);
break;
case STARFIVE_AES_MODE_CCM:
starfive_aes_set_alen(ctx);
starfive_aes_set_mlen(ctx);
starfive_aes_ccm_init(ctx);
starfive_aes_aead_hw_start(ctx, hw_mode);
break;
case STARFIVE_AES_MODE_CBC:
case STARFIVE_AES_MODE_CTR:
starfive_aes_write_iv(ctx, (void *)cryp->req.sreq->iv);
break;
default:
break;
}
return cryp->err;
}
static int starfive_aes_read_authtag(struct starfive_cryp_ctx *ctx)
{
struct starfive_cryp_dev *cryp = ctx->cryp;
struct starfive_cryp_request_ctx *rctx = ctx->rctx;
int i;
if (starfive_aes_wait_busy(cryp))
return dev_err_probe(cryp->dev, -ETIMEDOUT,
"Timeout waiting for tag generation.");
if ((cryp->flags & FLG_MODE_MASK) == STARFIVE_AES_MODE_GCM) {
cryp->tag_out[0] = readl(cryp->base + STARFIVE_AES_NONCE0);
cryp->tag_out[1] = readl(cryp->base + STARFIVE_AES_NONCE1);
cryp->tag_out[2] = readl(cryp->base + STARFIVE_AES_NONCE2);
cryp->tag_out[3] = readl(cryp->base + STARFIVE_AES_NONCE3);
} else {
for (i = 0; i < AES_BLOCK_32; i++)
cryp->tag_out[i] = readl(cryp->base + STARFIVE_AES_AESDIO0R);
}
if (is_encrypt(cryp)) {
scatterwalk_map_and_copy(cryp->tag_out, rctx->out_sg,
cryp->total_in, cryp->authsize, 1);
} else {
if (crypto_memneq(cryp->tag_in, cryp->tag_out, cryp->authsize))
return -EBADMSG;
}
return 0;
}
static void starfive_aes_finish_req(struct starfive_cryp_ctx *ctx)
{
struct starfive_cryp_dev *cryp = ctx->cryp;
int err = cryp->err;
if (!err && cryp->authsize)
err = starfive_aes_read_authtag(ctx);
if (!err && ((cryp->flags & FLG_MODE_MASK) == STARFIVE_AES_MODE_CBC ||
(cryp->flags & FLG_MODE_MASK) == STARFIVE_AES_MODE_CTR))
starfive_aes_get_iv(cryp, (void *)cryp->req.sreq->iv);
if (cryp->authsize)
crypto_finalize_aead_request(cryp->engine, cryp->req.areq, err);
else
crypto_finalize_skcipher_request(cryp->engine, cryp->req.sreq,
err);
}
static int starfive_aes_gcm_write_adata(struct starfive_cryp_ctx *ctx)
{
struct starfive_cryp_dev *cryp = ctx->cryp;
struct starfive_cryp_request_ctx *rctx = ctx->rctx;
u32 *buffer;
int total_len, loop;
total_len = ALIGN(cryp->assoclen, AES_BLOCK_SIZE) / sizeof(unsigned int);
buffer = (u32 *)rctx->adata;
for (loop = 0; loop < total_len; loop += 4) {
writel(*buffer, cryp->base + STARFIVE_AES_NONCE0);
buffer++;
writel(*buffer, cryp->base + STARFIVE_AES_NONCE1);
buffer++;
writel(*buffer, cryp->base + STARFIVE_AES_NONCE2);
buffer++;
writel(*buffer, cryp->base + STARFIVE_AES_NONCE3);
buffer++;
}
if (starfive_aes_wait_gcmdone(cryp))
return dev_err_probe(cryp->dev, -ETIMEDOUT,
"Timeout processing gcm aad block");
return 0;
}
static int starfive_aes_ccm_write_adata(struct starfive_cryp_ctx *ctx)
{
struct starfive_cryp_dev *cryp = ctx->cryp;
struct starfive_cryp_request_ctx *rctx = ctx->rctx;
u32 *buffer;
u8 *ci;
int total_len, loop;
total_len = cryp->assoclen;
ci = rctx->adata;
writeb(*ci, cryp->base + STARFIVE_AES_AESDIO0R);
ci++;
writeb(*ci, cryp->base + STARFIVE_AES_AESDIO0R);
ci++;
total_len -= 2;
buffer = (u32 *)ci;
for (loop = 0; loop < 3; loop++, buffer++)
writel(*buffer, cryp->base + STARFIVE_AES_AESDIO0R);
total_len -= 12;
while (total_len > 0) {
for (loop = 0; loop < AES_BLOCK_32; loop++, buffer++)
writel(*buffer, cryp->base + STARFIVE_AES_AESDIO0R);
total_len -= AES_BLOCK_SIZE;
}
if (starfive_aes_wait_busy(cryp))
return dev_err_probe(cryp->dev, -ETIMEDOUT,
"Timeout processing ccm aad block");
return 0;
}
static void starfive_aes_dma_done(void *param)
{
struct starfive_cryp_dev *cryp = param;
complete(&cryp->dma_done);
}
static void starfive_aes_dma_init(struct starfive_cryp_dev *cryp)
{
cryp->cfg_in.direction = DMA_MEM_TO_DEV;
cryp->cfg_in.src_addr_width = DMA_SLAVE_BUSWIDTH_16_BYTES;
cryp->cfg_in.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
cryp->cfg_in.src_maxburst = cryp->dma_maxburst;
cryp->cfg_in.dst_maxburst = cryp->dma_maxburst;
cryp->cfg_in.dst_addr = cryp->phys_base + STARFIVE_ALG_FIFO_OFFSET;
dmaengine_slave_config(cryp->tx, &cryp->cfg_in);
cryp->cfg_out.direction = DMA_DEV_TO_MEM;
cryp->cfg_out.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
cryp->cfg_out.dst_addr_width = DMA_SLAVE_BUSWIDTH_16_BYTES;
cryp->cfg_out.src_maxburst = 4;
cryp->cfg_out.dst_maxburst = 4;
cryp->cfg_out.src_addr = cryp->phys_base + STARFIVE_ALG_FIFO_OFFSET;
dmaengine_slave_config(cryp->rx, &cryp->cfg_out);
init_completion(&cryp->dma_done);
}
static int starfive_aes_dma_xfer(struct starfive_cryp_dev *cryp,
struct scatterlist *src,
struct scatterlist *dst,
int len)
{
struct dma_async_tx_descriptor *in_desc, *out_desc;
union starfive_alg_cr alg_cr;
int ret = 0, in_save, out_save;
alg_cr.v = 0;
alg_cr.start = 1;
alg_cr.aes_dma_en = 1;
writel(alg_cr.v, cryp->base + STARFIVE_ALG_CR_OFFSET);
in_save = sg_dma_len(src);
out_save = sg_dma_len(dst);
writel(ALIGN(len, AES_BLOCK_SIZE), cryp->base + STARFIVE_DMA_IN_LEN_OFFSET);
writel(ALIGN(len, AES_BLOCK_SIZE), cryp->base + STARFIVE_DMA_OUT_LEN_OFFSET);
sg_dma_len(src) = ALIGN(len, AES_BLOCK_SIZE);
sg_dma_len(dst) = ALIGN(len, AES_BLOCK_SIZE);
out_desc = dmaengine_prep_slave_sg(cryp->rx, dst, 1, DMA_DEV_TO_MEM,
DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
if (!out_desc) {
ret = -EINVAL;
goto dma_err;
}
out_desc->callback = starfive_aes_dma_done;
out_desc->callback_param = cryp;
reinit_completion(&cryp->dma_done);
dmaengine_submit(out_desc);
dma_async_issue_pending(cryp->rx);
in_desc = dmaengine_prep_slave_sg(cryp->tx, src, 1, DMA_MEM_TO_DEV,
DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
if (!in_desc) {
ret = -EINVAL;
goto dma_err;
}
dmaengine_submit(in_desc);
dma_async_issue_pending(cryp->tx);
if (!wait_for_completion_timeout(&cryp->dma_done,
msecs_to_jiffies(1000)))
ret = -ETIMEDOUT;
dma_err:
sg_dma_len(src) = in_save;
sg_dma_len(dst) = out_save;
alg_cr.v = 0;
alg_cr.clear = 1;
writel(alg_cr.v, cryp->base + STARFIVE_ALG_CR_OFFSET);
return ret;
}
static int starfive_aes_map_sg(struct starfive_cryp_dev *cryp,
struct scatterlist *src,
struct scatterlist *dst)
{
struct scatterlist *stsg, *dtsg;
struct scatterlist _src[2], _dst[2];
unsigned int remain = cryp->total_in;
unsigned int len, src_nents, dst_nents;
int ret;
if (src == dst) {
for (stsg = src, dtsg = dst; remain > 0;
stsg = sg_next(stsg), dtsg = sg_next(dtsg)) {
src_nents = dma_map_sg(cryp->dev, stsg, 1, DMA_BIDIRECTIONAL);
if (src_nents == 0)
return dev_err_probe(cryp->dev, -ENOMEM,
"dma_map_sg error\n");
dst_nents = src_nents;
len = min(sg_dma_len(stsg), remain);
ret = starfive_aes_dma_xfer(cryp, stsg, dtsg, len);
dma_unmap_sg(cryp->dev, stsg, 1, DMA_BIDIRECTIONAL);
if (ret)
return ret;
remain -= len;
}
} else {
for (stsg = src, dtsg = dst;;) {
src_nents = dma_map_sg(cryp->dev, stsg, 1, DMA_TO_DEVICE);
if (src_nents == 0)
return dev_err_probe(cryp->dev, -ENOMEM,
"dma_map_sg src error\n");
dst_nents = dma_map_sg(cryp->dev, dtsg, 1, DMA_FROM_DEVICE);
if (dst_nents == 0)
return dev_err_probe(cryp->dev, -ENOMEM,
"dma_map_sg dst error\n");
len = min(sg_dma_len(stsg), sg_dma_len(dtsg));
len = min(len, remain);
ret = starfive_aes_dma_xfer(cryp, stsg, dtsg, len);
dma_unmap_sg(cryp->dev, stsg, 1, DMA_TO_DEVICE);
dma_unmap_sg(cryp->dev, dtsg, 1, DMA_FROM_DEVICE);
if (ret)
return ret;
remain -= len;
if (remain == 0)
break;
if (sg_dma_len(stsg) - len) {
stsg = scatterwalk_ffwd(_src, stsg, len);
dtsg = sg_next(dtsg);
} else if (sg_dma_len(dtsg) - len) {
dtsg = scatterwalk_ffwd(_dst, dtsg, len);
stsg = sg_next(stsg);
} else {
stsg = sg_next(stsg);
dtsg = sg_next(dtsg);
}
}
}
return 0;
}
static int starfive_aes_do_one_req(struct crypto_engine *engine, void *areq)
{
struct skcipher_request *req =
container_of(areq, struct skcipher_request, base);
struct starfive_cryp_ctx *ctx =
crypto_skcipher_ctx(crypto_skcipher_reqtfm(req));
struct starfive_cryp_request_ctx *rctx = skcipher_request_ctx(req);
struct starfive_cryp_dev *cryp = ctx->cryp;
int ret;
cryp->req.sreq = req;
cryp->total_in = req->cryptlen;
cryp->total_out = req->cryptlen;
cryp->assoclen = 0;
cryp->authsize = 0;
rctx->in_sg = req->src;
rctx->out_sg = req->dst;
ctx->rctx = rctx;
ret = starfive_aes_hw_init(ctx);
if (ret)
return ret;
if (!cryp->total_in)
goto finish_req;
starfive_aes_dma_init(cryp);
ret = starfive_aes_map_sg(cryp, rctx->in_sg, rctx->out_sg);
if (ret)
return ret;
finish_req:
starfive_aes_finish_req(ctx);
return 0;
}
static int starfive_aes_init_tfm(struct crypto_skcipher *tfm,
const char *alg_name)
{
struct starfive_cryp_ctx *ctx = crypto_skcipher_ctx(tfm);
ctx->cryp = starfive_cryp_find_dev(ctx);
if (!ctx->cryp)
return -ENODEV;
ctx->skcipher_fbk = crypto_alloc_skcipher(alg_name, 0,
CRYPTO_ALG_NEED_FALLBACK);
if (IS_ERR(ctx->skcipher_fbk))
return dev_err_probe(ctx->cryp->dev, PTR_ERR(ctx->skcipher_fbk),
"%s() failed to allocate fallback for %s\n",
__func__, alg_name);
crypto_skcipher_set_reqsize(tfm, sizeof(struct starfive_cryp_request_ctx) +
crypto_skcipher_reqsize(ctx->skcipher_fbk));
return 0;
}
static void starfive_aes_exit_tfm(struct crypto_skcipher *tfm)
{
struct starfive_cryp_ctx *ctx = crypto_skcipher_ctx(tfm);
crypto_free_skcipher(ctx->skcipher_fbk);
}
static int starfive_aes_aead_do_one_req(struct crypto_engine *engine, void *areq)
{
struct aead_request *req =
container_of(areq, struct aead_request, base);
struct starfive_cryp_ctx *ctx =
crypto_aead_ctx(crypto_aead_reqtfm(req));
struct starfive_cryp_dev *cryp = ctx->cryp;
struct starfive_cryp_request_ctx *rctx = aead_request_ctx(req);
struct scatterlist _src[2], _dst[2];
int ret;
cryp->req.areq = req;
cryp->assoclen = req->assoclen;
cryp->authsize = crypto_aead_authsize(crypto_aead_reqtfm(req));
rctx->in_sg = scatterwalk_ffwd(_src, req->src, cryp->assoclen);
if (req->src == req->dst)
rctx->out_sg = rctx->in_sg;
else
rctx->out_sg = scatterwalk_ffwd(_dst, req->dst, cryp->assoclen);
if (is_encrypt(cryp)) {
cryp->total_in = req->cryptlen;
cryp->total_out = req->cryptlen;
} else {
cryp->total_in = req->cryptlen - cryp->authsize;
cryp->total_out = cryp->total_in;
scatterwalk_map_and_copy(cryp->tag_in, req->src,
cryp->total_in + cryp->assoclen,
cryp->authsize, 0);
}
if (cryp->assoclen) {
rctx->adata = kzalloc(cryp->assoclen + AES_BLOCK_SIZE, GFP_KERNEL);
if (!rctx->adata)
return dev_err_probe(cryp->dev, -ENOMEM,
"Failed to alloc memory for adata");
if (sg_copy_to_buffer(req->src, sg_nents_for_len(req->src, cryp->assoclen),
rctx->adata, cryp->assoclen) != cryp->assoclen)
return -EINVAL;
}
if (cryp->total_in)
sg_zero_buffer(rctx->in_sg, sg_nents(rctx->in_sg),
sg_dma_len(rctx->in_sg) - cryp->total_in,
cryp->total_in);
ctx->rctx = rctx;
ret = starfive_aes_hw_init(ctx);
if (ret)
return ret;
if (!cryp->assoclen)
goto write_text;
if ((cryp->flags & FLG_MODE_MASK) == STARFIVE_AES_MODE_CCM)
ret = starfive_aes_ccm_write_adata(ctx);
else
ret = starfive_aes_gcm_write_adata(ctx);
kfree(rctx->adata);
if (ret)
return ret;
write_text:
if (!cryp->total_in)
goto finish_req;
starfive_aes_dma_init(cryp);
ret = starfive_aes_map_sg(cryp, rctx->in_sg, rctx->out_sg);
if (ret)
return ret;
finish_req:
starfive_aes_finish_req(ctx);
return 0;
}
static int starfive_aes_aead_init_tfm(struct crypto_aead *tfm,
const char *alg_name)
{
struct starfive_cryp_ctx *ctx = crypto_aead_ctx(tfm);
ctx->cryp = starfive_cryp_find_dev(ctx);
if (!ctx->cryp)
return -ENODEV;
ctx->aead_fbk = crypto_alloc_aead(alg_name, 0,
CRYPTO_ALG_NEED_FALLBACK);
if (IS_ERR(ctx->aead_fbk))
return dev_err_probe(ctx->cryp->dev, PTR_ERR(ctx->aead_fbk),
"%s() failed to allocate fallback for %s\n",
__func__, alg_name);
crypto_aead_set_reqsize(tfm, sizeof(struct starfive_cryp_request_ctx) +
crypto_aead_reqsize(ctx->aead_fbk));
return 0;
}
static void starfive_aes_aead_exit_tfm(struct crypto_aead *tfm)
{
struct starfive_cryp_ctx *ctx = crypto_aead_ctx(tfm);
crypto_free_aead(ctx->aead_fbk);
}
static bool starfive_aes_check_unaligned(struct starfive_cryp_dev *cryp,
struct scatterlist *src,
struct scatterlist *dst)
{
struct scatterlist *tsg;
int i;
for_each_sg(src, tsg, sg_nents(src), i)
if (!IS_ALIGNED(tsg->offset, sizeof(u32)) ||
(!IS_ALIGNED(tsg->length, AES_BLOCK_SIZE) &&
!sg_is_last(tsg)))
return true;
if (src != dst)
for_each_sg(dst, tsg, sg_nents(dst), i)
if (!IS_ALIGNED(tsg->offset, sizeof(u32)) ||
(!IS_ALIGNED(tsg->length, AES_BLOCK_SIZE) &&
!sg_is_last(tsg)))
return true;
return false;
}
static int starfive_aes_do_fallback(struct skcipher_request *req, bool enc)
{
struct starfive_cryp_ctx *ctx =
crypto_skcipher_ctx(crypto_skcipher_reqtfm(req));
struct skcipher_request *subreq = skcipher_request_ctx(req);
skcipher_request_set_tfm(subreq, ctx->skcipher_fbk);
skcipher_request_set_callback(subreq, req->base.flags,
req->base.complete,
req->base.data);
skcipher_request_set_crypt(subreq, req->src, req->dst,
req->cryptlen, req->iv);
return enc ? crypto_skcipher_encrypt(subreq) :
crypto_skcipher_decrypt(subreq);
}
static int starfive_aes_crypt(struct skcipher_request *req, unsigned long flags)
{
struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
struct starfive_cryp_ctx *ctx = crypto_skcipher_ctx(tfm);
struct starfive_cryp_dev *cryp = ctx->cryp;
unsigned int blocksize_align = crypto_skcipher_blocksize(tfm) - 1;
cryp->flags = flags;
if ((cryp->flags & FLG_MODE_MASK) == STARFIVE_AES_MODE_ECB ||
(cryp->flags & FLG_MODE_MASK) == STARFIVE_AES_MODE_CBC)
if (req->cryptlen & blocksize_align)
return -EINVAL;
if (starfive_aes_check_unaligned(cryp, req->src, req->dst))
return starfive_aes_do_fallback(req, is_encrypt(cryp));
return crypto_transfer_skcipher_request_to_engine(cryp->engine, req);
}
static int starfive_aes_aead_do_fallback(struct aead_request *req, bool enc)
{
struct starfive_cryp_ctx *ctx =
crypto_aead_ctx(crypto_aead_reqtfm(req));
struct aead_request *subreq = aead_request_ctx(req);
aead_request_set_tfm(subreq, ctx->aead_fbk);
aead_request_set_callback(subreq, req->base.flags,
req->base.complete,
req->base.data);
aead_request_set_crypt(subreq, req->src, req->dst,
req->cryptlen, req->iv);
aead_request_set_ad(subreq, req->assoclen);
return enc ? crypto_aead_encrypt(subreq) :
crypto_aead_decrypt(subreq);
}
static int starfive_aes_aead_crypt(struct aead_request *req, unsigned long flags)
{
struct starfive_cryp_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
struct starfive_cryp_dev *cryp = ctx->cryp;
struct scatterlist *src, *dst, _src[2], _dst[2];
cryp->flags = flags;
/* aes-ccm does not support tag verification for non-aligned text,
* use fallback for ccm decryption instead.
*/
if (((cryp->flags & FLG_MODE_MASK) == STARFIVE_AES_MODE_CCM) &&
!is_encrypt(cryp))
return starfive_aes_aead_do_fallback(req, 0);
src = scatterwalk_ffwd(_src, req->src, req->assoclen);
if (req->src == req->dst)
dst = src;
else
dst = scatterwalk_ffwd(_dst, req->dst, req->assoclen);
if (starfive_aes_check_unaligned(cryp, src, dst))
return starfive_aes_aead_do_fallback(req, is_encrypt(cryp));
return crypto_transfer_aead_request_to_engine(cryp->engine, req);
}
static int starfive_aes_setkey(struct crypto_skcipher *tfm, const u8 *key,
unsigned int keylen)
{
struct starfive_cryp_ctx *ctx = crypto_skcipher_ctx(tfm);
if (!key || !keylen)
return -EINVAL;
if (keylen != AES_KEYSIZE_128 &&
keylen != AES_KEYSIZE_192 &&
keylen != AES_KEYSIZE_256)
return -EINVAL;
memcpy(ctx->key, key, keylen);
ctx->keylen = keylen;
return crypto_skcipher_setkey(ctx->skcipher_fbk, key, keylen);
}
static int starfive_aes_aead_setkey(struct crypto_aead *tfm, const u8 *key,
unsigned int keylen)
{
struct starfive_cryp_ctx *ctx = crypto_aead_ctx(tfm);
if (!key || !keylen)
return -EINVAL;
if (keylen != AES_KEYSIZE_128 &&
keylen != AES_KEYSIZE_192 &&
keylen != AES_KEYSIZE_256)
return -EINVAL;
memcpy(ctx->key, key, keylen);
ctx->keylen = keylen;
return crypto_aead_setkey(ctx->aead_fbk, key, keylen);
}
static int starfive_aes_gcm_setauthsize(struct crypto_aead *tfm,
unsigned int authsize)
{
struct starfive_cryp_ctx *ctx = crypto_aead_ctx(tfm);
int ret;
ret = crypto_gcm_check_authsize(authsize);
if (ret)
return ret;
return crypto_aead_setauthsize(ctx->aead_fbk, authsize);
}
static int starfive_aes_ccm_setauthsize(struct crypto_aead *tfm,
unsigned int authsize)
{
struct starfive_cryp_ctx *ctx = crypto_aead_ctx(tfm);
switch (authsize) {
case 4:
case 6:
case 8:
case 10:
case 12:
case 14:
case 16:
break;
default:
return -EINVAL;
}
return crypto_aead_setauthsize(ctx->aead_fbk, authsize);
}
static int starfive_aes_ecb_encrypt(struct skcipher_request *req)
{
return starfive_aes_crypt(req, STARFIVE_AES_MODE_ECB | FLG_ENCRYPT);
}
static int starfive_aes_ecb_decrypt(struct skcipher_request *req)
{
return starfive_aes_crypt(req, STARFIVE_AES_MODE_ECB);
}
static int starfive_aes_cbc_encrypt(struct skcipher_request *req)
{
return starfive_aes_crypt(req, STARFIVE_AES_MODE_CBC | FLG_ENCRYPT);
}
static int starfive_aes_cbc_decrypt(struct skcipher_request *req)
{
return starfive_aes_crypt(req, STARFIVE_AES_MODE_CBC);
}
static int starfive_aes_ctr_encrypt(struct skcipher_request *req)
{
return starfive_aes_crypt(req, STARFIVE_AES_MODE_CTR | FLG_ENCRYPT);
}
static int starfive_aes_ctr_decrypt(struct skcipher_request *req)
{
return starfive_aes_crypt(req, STARFIVE_AES_MODE_CTR);
}
static int starfive_aes_gcm_encrypt(struct aead_request *req)
{
return starfive_aes_aead_crypt(req, STARFIVE_AES_MODE_GCM | FLG_ENCRYPT);
}
static int starfive_aes_gcm_decrypt(struct aead_request *req)
{
return starfive_aes_aead_crypt(req, STARFIVE_AES_MODE_GCM);
}
static int starfive_aes_ccm_encrypt(struct aead_request *req)
{
int ret;
ret = starfive_aes_ccm_check_iv(req->iv);
if (ret)
return ret;
return starfive_aes_aead_crypt(req, STARFIVE_AES_MODE_CCM | FLG_ENCRYPT);
}
static int starfive_aes_ccm_decrypt(struct aead_request *req)
{
int ret;
ret = starfive_aes_ccm_check_iv(req->iv);
if (ret)
return ret;
return starfive_aes_aead_crypt(req, STARFIVE_AES_MODE_CCM);
}
static int starfive_aes_ecb_init_tfm(struct crypto_skcipher *tfm)
{
return starfive_aes_init_tfm(tfm, "ecb(aes-generic)");
}
static int starfive_aes_cbc_init_tfm(struct crypto_skcipher *tfm)
{
return starfive_aes_init_tfm(tfm, "cbc(aes-generic)");
}
static int starfive_aes_ctr_init_tfm(struct crypto_skcipher *tfm)
{
return starfive_aes_init_tfm(tfm, "ctr(aes-generic)");
}
static int starfive_aes_ccm_init_tfm(struct crypto_aead *tfm)
{
return starfive_aes_aead_init_tfm(tfm, "ccm_base(ctr(aes-generic),cbcmac(aes-generic))");
}
static int starfive_aes_gcm_init_tfm(struct crypto_aead *tfm)
{
return starfive_aes_aead_init_tfm(tfm, "gcm_base(ctr(aes-generic),ghash-generic)");
}
static struct skcipher_engine_alg skcipher_algs[] = {
{
.base.init = starfive_aes_ecb_init_tfm,
.base.exit = starfive_aes_exit_tfm,
.base.setkey = starfive_aes_setkey,
.base.encrypt = starfive_aes_ecb_encrypt,
.base.decrypt = starfive_aes_ecb_decrypt,
.base.min_keysize = AES_MIN_KEY_SIZE,
.base.max_keysize = AES_MAX_KEY_SIZE,
.base.base = {
.cra_name = "ecb(aes)",
.cra_driver_name = "starfive-ecb-aes",
.cra_priority = 200,
.cra_flags = CRYPTO_ALG_ASYNC |
CRYPTO_ALG_NEED_FALLBACK,
.cra_blocksize = AES_BLOCK_SIZE,
.cra_ctxsize = sizeof(struct starfive_cryp_ctx),
.cra_alignmask = 0xf,
.cra_module = THIS_MODULE,
},
.op = {
.do_one_request = starfive_aes_do_one_req,
},
}, {
.base.init = starfive_aes_cbc_init_tfm,
.base.exit = starfive_aes_exit_tfm,
.base.setkey = starfive_aes_setkey,
.base.encrypt = starfive_aes_cbc_encrypt,
.base.decrypt = starfive_aes_cbc_decrypt,
.base.min_keysize = AES_MIN_KEY_SIZE,
.base.max_keysize = AES_MAX_KEY_SIZE,
.base.ivsize = AES_BLOCK_SIZE,
.base.base = {
.cra_name = "cbc(aes)",
.cra_driver_name = "starfive-cbc-aes",
.cra_priority = 200,
.cra_flags = CRYPTO_ALG_ASYNC |
CRYPTO_ALG_NEED_FALLBACK,
.cra_blocksize = AES_BLOCK_SIZE,
.cra_ctxsize = sizeof(struct starfive_cryp_ctx),
.cra_alignmask = 0xf,
.cra_module = THIS_MODULE,
},
.op = {
.do_one_request = starfive_aes_do_one_req,
},
}, {
.base.init = starfive_aes_ctr_init_tfm,
.base.exit = starfive_aes_exit_tfm,
.base.setkey = starfive_aes_setkey,
.base.encrypt = starfive_aes_ctr_encrypt,
.base.decrypt = starfive_aes_ctr_decrypt,
.base.min_keysize = AES_MIN_KEY_SIZE,
.base.max_keysize = AES_MAX_KEY_SIZE,
.base.ivsize = AES_BLOCK_SIZE,
.base.base = {
.cra_name = "ctr(aes)",
.cra_driver_name = "starfive-ctr-aes",
.cra_priority = 200,
.cra_flags = CRYPTO_ALG_ASYNC |
CRYPTO_ALG_NEED_FALLBACK,
.cra_blocksize = 1,
.cra_ctxsize = sizeof(struct starfive_cryp_ctx),
.cra_alignmask = 0xf,
.cra_module = THIS_MODULE,
},
.op = {
.do_one_request = starfive_aes_do_one_req,
},
},
};
static struct aead_engine_alg aead_algs[] = {
{
.base.setkey = starfive_aes_aead_setkey,
.base.setauthsize = starfive_aes_gcm_setauthsize,
.base.encrypt = starfive_aes_gcm_encrypt,
.base.decrypt = starfive_aes_gcm_decrypt,
.base.init = starfive_aes_gcm_init_tfm,
.base.exit = starfive_aes_aead_exit_tfm,
.base.ivsize = GCM_AES_IV_SIZE,
.base.maxauthsize = AES_BLOCK_SIZE,
.base.base = {
.cra_name = "gcm(aes)",
.cra_driver_name = "starfive-gcm-aes",
.cra_priority = 200,
.cra_flags = CRYPTO_ALG_ASYNC |
CRYPTO_ALG_NEED_FALLBACK,
.cra_blocksize = 1,
.cra_ctxsize = sizeof(struct starfive_cryp_ctx),
.cra_alignmask = 0xf,
.cra_module = THIS_MODULE,
},
.op = {
.do_one_request = starfive_aes_aead_do_one_req,
},
}, {
.base.setkey = starfive_aes_aead_setkey,
.base.setauthsize = starfive_aes_ccm_setauthsize,
.base.encrypt = starfive_aes_ccm_encrypt,
.base.decrypt = starfive_aes_ccm_decrypt,
.base.init = starfive_aes_ccm_init_tfm,
.base.exit = starfive_aes_aead_exit_tfm,
.base.ivsize = AES_BLOCK_SIZE,
.base.maxauthsize = AES_BLOCK_SIZE,
.base.base = {
.cra_name = "ccm(aes)",
.cra_driver_name = "starfive-ccm-aes",
.cra_priority = 200,
.cra_flags = CRYPTO_ALG_ASYNC |
CRYPTO_ALG_NEED_FALLBACK,
.cra_blocksize = 1,
.cra_ctxsize = sizeof(struct starfive_cryp_ctx),
.cra_alignmask = 0xf,
.cra_module = THIS_MODULE,
},
.op = {
.do_one_request = starfive_aes_aead_do_one_req,
},
},
};
int starfive_aes_register_algs(void)
{
int ret;
ret = crypto_engine_register_skciphers(skcipher_algs, ARRAY_SIZE(skcipher_algs));
if (ret)
return ret;
ret = crypto_engine_register_aeads(aead_algs, ARRAY_SIZE(aead_algs));
if (ret)
crypto_engine_unregister_skciphers(skcipher_algs, ARRAY_SIZE(skcipher_algs));
return ret;
}
void starfive_aes_unregister_algs(void)
{
crypto_engine_unregister_aeads(aead_algs, ARRAY_SIZE(aead_algs));
crypto_engine_unregister_skciphers(skcipher_algs, ARRAY_SIZE(skcipher_algs));
}
|