-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathast_crypto_engine.c
More file actions
2643 lines (2263 loc) · 72.9 KB
/
ast_crypto_engine.c
File metadata and controls
2643 lines (2263 loc) · 72.9 KB
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
/*
* Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved.
* Copyright (c) 2025 ASPEED Technology Inc.
*
* Licensed under the Apache License 2.0 (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
* in the file LICENSE in the source distribution or at
* https://www.openssl.org/source/license.html
*/
/* We need to use some deprecated APIs */
#define OPENSSL_SUPPRESS_DEPRECATED
/* #define AFALG_NO_FALLBACK */
/* #define AFALG_ZERO_COPY */
#ifdef AFALG_ZERO_COPY
/* Required for vmsplice */
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <sys/uio.h>
static size_t zc_maxsize, pagemask;
#endif /* AFALG_ZERO_COPY */
#include <asm/byteorder.h>
#include <asm/types.h>
#include <assert.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#ifndef AFALG_NO_CRYPTOUSER
#include <linux/cryptouser.h>
#elif !defined(CRYPTO_MAX_NAME)
#define CRYPTO_MAX_NAME 64
#endif
#include <linux/if_alg.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <openssl/rsa.h>
#include <openssl/engine.h>
#include <openssl/err.h>
#include <openssl/evp.h>
#include <openssl/objects.h>
#include <linux/aio_abi.h>
#include "ast_crypto_engine.h"
/* linux/crypto.h is not public, so we must define the type and masks here,
* and hope they are still valid. */
#ifndef CRYPTO_ALG_TYPE_MASK
#define CRYPTO_ALG_TYPE_MASK 0x0000000f
#define CRYPTO_ALG_TYPE_BLKCIPHER 0x00000004
#define CRYPTO_ALG_TYPE_SKCIPHER 0x00000005
#define CRYPTO_ALG_TYPE_AKCIPHER 0x00000006
#define CRYPTO_ALG_TYPE_SHASH 0x0000000e
#define CRYPTO_ALG_TYPE_AHASH 0x0000000f
#define CRYPTO_ALG_KERN_DRIVER_ONLY 0x00001000
#define CRYPTO_ALG_INTERNAL 0x00002000
#endif
#ifndef OSSL_NELEM
#define OSSL_NELEM(x) (sizeof(x)/sizeof((x)[0]))
#endif
/* SHA3_BLOCKSIZE is not exported, so we copy the definition here */
#ifndef SHA3_BLOCKSIZE
#define KECCAK1600_WIDTH 1600
#define SHA3_BLOCKSIZE(bitlen) (KECCAK1600_WIDTH - bitlen * 2) / 8
#endif
#define engine_afalg_id "ast_crypto_engine"
#define AFALG_REQUIRE_ACCELERATED 0 /* require confirmation of acceleration */
#define AFALG_USE_SOFTWARE 1 /* allow software drivers */
#define AFALG_REJECT_SOFTWARE 2 /* only disallow confirmed software drivers */
#define AFALG_DEFAULT_USE_SOFTDRIVERS AFALG_USE_SOFTWARE
#ifndef AFALG_DEFAULT_USE_SOFTDRIVERS
#define AFALG_DEFAULT_USE_SOFTDRIVERS AFALG_REJECT_SOFTWARE
#endif
static int use_softdrivers = AFALG_DEFAULT_USE_SOFTDRIVERS;
/*
* cipher/digest status & acceleration definitions
* Make sure the defaults are set to 0
*/
struct driver_info_st {
enum afalg_status_t {
AFALG_STATUS_FAILURE = -3, /* unusable for other reason */
AFALG_STATUS_NO_COPY = -2, /* hash state copy not supported */
AFALG_STATUS_NO_OPEN = -1, /* bind call failed */
AFALG_STATUS_UNKNOWN = 0, /* not tested yet */
AFALG_STATUS_USABLE = 1 /* algo can be used */
} status;
enum afalg_accelerated_t {
AFALG_NOT_ACCELERATED = -1, /* software implemented */
AFALG_ACCELERATION_UNKNOWN = 0, /* acceleration support unkown */
AFALG_ACCELERATED = 1 /* hardware accelerated */
} accelerated;
};
void engine_load_afalg_int(void);
void engine_load_afalg_int(void)
{
}
static int get_afalg_socket(const char *salg_name, const char *salg_type,
const __u32 feat, const __u32 mask)
{
struct sockaddr_alg sa;
int fd = -1;
memset(&sa, 0, sizeof(sa));
sa.salg_family = AF_ALG;
OPENSSL_strlcpy((char *)sa.salg_type, salg_type, sizeof(sa.salg_type));
OPENSSL_strlcpy((char *)sa.salg_name, salg_name, sizeof(sa.salg_name));
sa.salg_feat = feat;
sa.salg_mask = mask;
if ((fd = socket(AF_ALG, SOCK_SEQPACKET, 0)) < 0) {
SYSerr(SYS_F_SOCKET, errno);
return -1;
}
if (bind(fd, (struct sockaddr *)&sa, sizeof(sa)) == 0)
return fd;
close(fd);
return -1;
}
static int afalg_closefd(int fd)
{
int ret;
if (fd < 0 || (ret = close(fd)) == 0)
return 0;
/* For compatibility with openssl 1.1.0 */
#ifdef SYS_F_CLOSE
SYSerr(SYS_F_CLOSE, errno);
#endif
return ret;
}
static int max_sendbuf_size = 0;
static int set_sendbuf_size(int fd)
{
int ret = -1, snd_len, rlen;
socklen_t optlen = sizeof(snd_len);
FILE *fp;
char buf[32], *end;
memset(buf, 0, sizeof(buf));
fp = fopen("/proc/sys/net/core/wmem_max", "r");
if (fp == NULL) {
fprintf(stderr, "Failed to open /proc/sys/net/core/wmem_max\n");
return ret;
}
rlen = fread(buf, 1, sizeof(buf)-1, fp);
fclose(fp);
if (rlen <= 0) {
fprintf(stderr, "Failed to read /proc/sys/net/core/wmem_max\n");
return ret;
}
snd_len = strtol(buf, &end, 10);
if ((errno == ERANGE && (snd_len == LONG_MAX || snd_len == LONG_MIN))
|| (errno != 0 && snd_len == 0)) {
fprintf(stderr, "Invalid wmem_max value (%s)\n", buf);
return ret;
}
/* max send buffer size is twice wmem_max */
snd_len *= 2;
ret = setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &snd_len, optlen);
if (ret) {
fprintf(stderr, "setsockopt is failed, errno = %d\n", errno);
return ret;
}
max_sendbuf_size = snd_len;
return 0;
}
struct afalg_alg_info {
char alg_name[CRYPTO_MAX_NAME];
char driver_name[CRYPTO_MAX_NAME];
__u32 priority;
__u32 flags;
};
static struct afalg_alg_info *afalg_alg_list = NULL;
static int afalg_alg_list_count = -1; /* no info available */
#ifndef AFALG_NO_CRYPTOUSER
static int prepare_afalg_alg_list(void)
{
int ret = -EFAULT;
/* NETLINK_CRYPTO specific */
void *buf = NULL;
struct nlmsghdr *res_n;
int buf_size;
struct {
struct nlmsghdr n;
struct crypto_user_alg cru;
} req;
struct crypto_user_alg *cru_res = NULL;
struct afalg_alg_info *list;
/* AF_NETLINK specific */
struct sockaddr_nl nl;
struct iovec iov;
struct msghdr msg;
struct rtattr *rta;
int nlfd, msg_len, rta_len, list_count;
__u32 alg_type;
memset(&req, 0, sizeof(req));
memset(&msg, 0, sizeof(msg));
list = afalg_alg_list = NULL;
afalg_alg_list_count = -1;
req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.cru));
req.n.nlmsg_flags = NLM_F_REQUEST | NLM_F_MATCH;
req.n.nlmsg_type = CRYPTO_MSG_GETALG;
/* open netlink socket */
nlfd = socket(AF_NETLINK, SOCK_RAW, NETLINK_CRYPTO);
if (nlfd < 0) {
if (errno != EPROTONOSUPPORT) /* crypto_user module not available */
printf("Netlink error: cannot open netlink socket");
return -errno;
}
memset(&nl, 0, sizeof(nl));
nl.nl_family = AF_NETLINK;
if (bind(nlfd, (struct sockaddr*)&nl, sizeof(nl)) < 0) {
printf("Netlink error: cannot bind netlink socket");
ret = -errno;
goto out;
}
/* sending data */
memset(&nl, 0, sizeof(nl));
nl.nl_family = AF_NETLINK;
iov.iov_base = (void*) &req.n;
iov.iov_len = req.n.nlmsg_len;
msg.msg_name = &nl;
msg.msg_namelen = sizeof(nl);
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
if (sendmsg(nlfd, &msg, 0) < 0) {
printf("Netlink error: sendmsg failed");
ret = -errno;
goto out;
}
/* get the msg size */
iov.iov_base = NULL;
iov.iov_len = 0;
buf_size = recvmsg(nlfd, &msg, MSG_PEEK | MSG_TRUNC);
if (buf_size <= 0) {
printf("Failed to get afalg_alg_list size\n");
ret = -errno;
goto out;
}
buf = OPENSSL_zalloc(buf_size);
if (buf == NULL) {
printf("Failed to get allocate memory: %d\n", buf_size);
ret = -errno;
goto out;
}
iov.iov_base = buf;
iov.iov_len = buf_size;
while (1) {
if ((msg_len = recvmsg(nlfd, &msg, 0)) <= 0) {
if (errno == EINTR || errno == EAGAIN)
continue;
if (msg_len == 0)
printf("Nelink error: no data");
else
printf("Nelink error: netlink receive error");
ret = -errno;
goto out;
}
if ((u_int32_t)msg_len > buf_size) {
printf("Netlink error: received too much data");
ret = -errno;
goto out;
}
break;
}
ret = -EFAULT;
list_count = 0;
for (res_n = (struct nlmsghdr *)buf; (ret = NLMSG_OK(res_n, (__u32)msg_len));
res_n = NLMSG_NEXT(res_n, msg_len)) {
if (res_n->nlmsg_type == NLMSG_ERROR) {
ret = 0;
goto out;
}
cru_res = NLMSG_DATA(res_n);
if (res_n->nlmsg_type != CRYPTO_MSG_GETALG
|| !cru_res || res_n->nlmsg_len < NLMSG_SPACE(sizeof(*cru_res)))
continue;
alg_type = cru_res->cru_flags & CRYPTO_ALG_TYPE_MASK;
if ((alg_type != CRYPTO_ALG_TYPE_SKCIPHER && alg_type != CRYPTO_ALG_TYPE_BLKCIPHER
&& alg_type != CRYPTO_ALG_TYPE_SHASH && alg_type != CRYPTO_ALG_TYPE_AHASH
&& alg_type != CRYPTO_ALG_TYPE_AKCIPHER)
|| cru_res->cru_flags & CRYPTO_ALG_INTERNAL)
continue;
list = OPENSSL_realloc(afalg_alg_list, (list_count + 1) * sizeof(struct afalg_alg_info));
if (list == NULL) {
OPENSSL_free(afalg_alg_list);
afalg_alg_list = NULL;
ret = -ENOMEM;
goto out;
}
memset(&list[list_count], 0, sizeof(struct afalg_alg_info));
afalg_alg_list = list;
rta_len=msg_len;
list[list_count].priority = 0;
for (rta = (struct rtattr *)(((char *) cru_res)
+ NLMSG_ALIGN(sizeof(struct crypto_user_alg)));
(ret = RTA_OK (rta, rta_len)); rta = RTA_NEXT(rta, rta_len)) {
if (rta->rta_type == CRYPTOCFGA_PRIORITY_VAL) {
list[list_count].priority = *((__u32 *)RTA_DATA(rta));
break;
}
}
OPENSSL_strlcpy(list[list_count].alg_name, cru_res->cru_name,
sizeof(list->alg_name));
OPENSSL_strlcpy(list[list_count].driver_name, cru_res->cru_driver_name,
sizeof(list->driver_name));
list[list_count].flags = cru_res->cru_flags;
list_count++;
}
ret = afalg_alg_list_count = list_count;
out:
close(nlfd);
OPENSSL_free(buf);
return ret;
}
#endif
#ifndef AFALG_NO_CRYPTOUSER
static const char *
afalg_get_driver_name(const char *alg_name,
enum afalg_accelerated_t expected_accel)
{
int i;
__u32 priority = 0;
int found = 0;
enum afalg_accelerated_t accel;
const char *driver_name = "unknown";
for (i = 0; i < afalg_alg_list_count; i++) {
if (strcmp(afalg_alg_list[i].alg_name, alg_name) ||
priority > afalg_alg_list[i].priority)
continue;
if (afalg_alg_list[i].flags & CRYPTO_ALG_KERN_DRIVER_ONLY)
accel = AFALG_ACCELERATED;
else
accel = AFALG_NOT_ACCELERATED;
if ((found && priority == afalg_alg_list[i].priority)
|| accel != expected_accel) {
driver_name = "**unreliable info**";
} else {
found = 1;
priority = afalg_alg_list[i].priority;
driver_name = afalg_alg_list[i].driver_name;
}
}
return driver_name;
}
#endif
/******************************************************************************
*
* Ciphers
*
*****************************************************************************/
struct cipher_data_st {
int nid;
int blocksize;
int keylen;
int ivlen;
int flags;
const char *name;
#ifndef AFALG_NO_FALLBACK
const EVP_CIPHER *((*fallback) (void));
int fb_threshold;
#endif
};
struct cipher_ctx {
int bfd, sfd;
#ifdef AFALG_ZERO_COPY
int pipes[2];
#endif
#ifndef AFALG_NO_FALLBACK
EVP_CIPHER_CTX *fallback;
int fb_threshold;
#endif
int control_is_set;
const struct cipher_data_st *cipher_d;
unsigned int blocksize, num;
unsigned char partial[EVP_MAX_BLOCK_LENGTH];
};
#ifndef OPENSSL_NO_DES
#define OPENSSL_NO_DES
#endif
static const struct cipher_data_st cipher_data[] = {
#ifndef OPENSSL_NO_DES
{ NID_des_cbc, 8, 8, 8, EVP_CIPH_CBC_MODE, "cbc(des)",
#ifndef AFALG_NO_FALLBACK
EVP_des_cbc, 320
#endif
},
{ NID_des_ede3_cbc, 8, 24, 8, EVP_CIPH_CBC_MODE, "cbc(des3_ede)",
#ifndef AFALG_NO_FALLBACK
EVP_des_ede3_cbc, 96
#endif
},
#endif
{ NID_aes_128_cbc, 16, 128 / 8, 16, EVP_CIPH_CBC_MODE, "cbc(aes)",
#ifndef AFALG_NO_FALLBACK
EVP_aes_128_cbc, 1536
#endif
},
{ NID_aes_192_cbc, 16, 192 / 8, 16, EVP_CIPH_CBC_MODE, "cbc(aes)",
#ifndef AFALG_NO_FALLBACK
EVP_aes_192_cbc, 1152
#endif
},
{ NID_aes_256_cbc, 16, 256 / 8, 16, EVP_CIPH_CBC_MODE, "cbc(aes)",
#ifndef AFALG_NO_FALLBACK
EVP_aes_256_cbc, 960
#endif
},
{ NID_aes_128_ctr, 16, 128 / 8, 16, EVP_CIPH_CTR_MODE, "ctr(aes)",
#ifndef AFALG_NO_FALLBACK
EVP_aes_128_ctr, 1360
#endif
},
{ NID_aes_192_ctr, 16, 192 / 8, 16, EVP_CIPH_CTR_MODE, "ctr(aes)",
#ifndef AFALG_NO_FALLBACK
EVP_aes_192_ctr, 1152
#endif
},
{ NID_aes_256_ctr, 16, 256 / 8, 16, EVP_CIPH_CTR_MODE, "ctr(aes)",
#ifndef AFALG_NO_FALLBACK
EVP_aes_256_ctr, 960
#endif
},
{ NID_aes_128_ecb, 16, 128 / 8, 0, EVP_CIPH_ECB_MODE, "ecb(aes)",
#ifndef AFALG_NO_FALLBACK
EVP_aes_128_ecb, 2048
#endif
},
{ NID_aes_192_ecb, 16, 192 / 8, 0, EVP_CIPH_ECB_MODE, "ecb(aes)",
#ifndef AFALG_NO_FALLBACK
EVP_aes_192_ecb, 1440
#endif
},
{ NID_aes_256_ecb, 16, 256 / 8, 0, EVP_CIPH_ECB_MODE, "ecb(aes)",
#ifndef AFALG_NO_FALLBACK
EVP_aes_256_ecb, 1152
#endif
},
{ NID_aes_128_cfb128, 16, 128 / 8, 16, EVP_CIPH_CFB_MODE, "cfb(aes)",
#ifndef AFALG_NO_FALLBACK
EVP_aes_128_cfb, 2048
#endif
},
{ NID_aes_192_cfb128, 16, 192 / 8, 16, EVP_CIPH_CFB_MODE, "cfb(aes)",
#ifndef AFALG_NO_FALLBACK
EVP_aes_192_cfb, 1440
#endif
},
{ NID_aes_256_cfb128, 16, 256 / 8, 16, EVP_CIPH_CFB_MODE, "cfb(aes)",
#ifndef AFALG_NO_FALLBACK
EVP_aes_256_cfb, 1152
#endif
},
{ NID_aes_128_ofb128, 16, 128 / 8, 16, EVP_CIPH_OFB_MODE, "ofb(aes)",
#ifndef AFALG_NO_FALLBACK
EVP_aes_128_ofb, 2048
#endif
},
{ NID_aes_192_ofb128, 16, 192 / 8, 16, EVP_CIPH_OFB_MODE, "ofb(aes)",
#ifndef AFALG_NO_FALLBACK
EVP_aes_192_ofb, 1440
#endif
},
{ NID_aes_256_ofb128, 16, 256 / 8, 16, EVP_CIPH_OFB_MODE, "ofb(aes)",
#ifndef AFALG_NO_FALLBACK
EVP_aes_256_ofb, 1152
#endif
},
};
static size_t find_cipher_data_index(int nid)
{
size_t i;
for (i = 0; i < OSSL_NELEM(cipher_data); i++)
if (nid == cipher_data[i].nid)
return i;
return (size_t)-1;
}
static size_t get_cipher_data_index(int nid)
{
size_t i = find_cipher_data_index(nid);
if (i != (size_t)-1)
return i;
/*
* Code further down must make sure that only NIDs in the table above
* are used. If any other NID reaches this function, there's a grave
* coding error further down.
*/
assert("Code that never should be reached" == NULL);
return -1;
}
static int afalg_set_key(int sfd, const void *key, int keylen, int sockopt)
{
if (setsockopt(sfd, SOL_ALG, sockopt, key, keylen) >= 0)
return 1;
return 0;
}
static int afalg_set_control(struct msghdr *msg, int op,
const unsigned char *iv, unsigned int ivlen)
{
size_t set_op_len = sizeof(op);
size_t set_iv_len;
struct cmsghdr *cmsg;
struct af_alg_iv *aiv;
if (!iv)
ivlen = 0;
set_iv_len = offsetof(struct af_alg_iv, iv) + ivlen;
msg->msg_controllen = CMSG_SPACE(set_op_len)
+ (ivlen > 0 ? CMSG_SPACE(set_iv_len) : 0);
msg->msg_control = OPENSSL_zalloc(msg->msg_controllen);
if (msg->msg_control == NULL) {
printf("afalg_set_control: OPENSSL_zalloc failed, %zu\n", msg->msg_controllen);
return 0;
}
cmsg = CMSG_FIRSTHDR(msg);
if (cmsg == NULL) {
ALG_WARN("%s: CMSG_FIRSTHDR error setting op.\n", __func__);
goto err;
}
cmsg->cmsg_level = SOL_ALG;
cmsg->cmsg_type = ALG_SET_OP;
cmsg->cmsg_len = CMSG_LEN(sizeof(op));
*(CMSG_DATA(cmsg)) = op;
if (ivlen == 0)
return 1;
cmsg = CMSG_NXTHDR(msg, cmsg);
if (cmsg == NULL) {
ALG_WARN("%s: CMSG_NXTHDR error setting iv.\n", __func__);
goto err;
}
cmsg->cmsg_level = SOL_ALG;
cmsg->cmsg_type = ALG_SET_IV;
cmsg->cmsg_len = CMSG_LEN(offsetof(struct af_alg_iv, iv) + ivlen);
aiv = (void *)CMSG_DATA(cmsg);
aiv->ivlen = ivlen;
memcpy(aiv->iv, iv, ivlen);
return 1;
err:
OPENSSL_free(msg->msg_control);
msg->msg_control = NULL;
msg->msg_controllen = 0;
return 0;
}
#ifndef AFALG_NO_FALLBACK
static EVP_CIPHER_CTX *cipher_fb_ctx[OSSL_NELEM(cipher_data)][2] = { { NULL, }, };
static int cipher_fb_threshold[OSSL_NELEM(cipher_data)] = { 0, };
static int prepare_cipher_fallback(int i, int enc)
{
int ret;
cipher_fb_ctx[i][enc] = EVP_CIPHER_CTX_new();
if (!cipher_fb_ctx[i][enc])
return 0;
if ((ret = EVP_CipherInit_ex2(cipher_fb_ctx[i][enc], cipher_data[i].fallback(),
NULL, NULL, enc, NULL))) {
return 1;
}
ALG_WARN("%s: cipher init error\n", __func__);
EVP_CIPHER_CTX_free(cipher_fb_ctx[i][enc]);
cipher_fb_ctx[i][enc] = NULL;
return 0;
}
static int cipher_fb_init(struct cipher_ctx *cipher_ctx,
EVP_CIPHER_CTX *source_ctx,
const unsigned char *key,
const unsigned char *iv, int enc)
{
/* Now we can set key and IV */
if (!EVP_CipherInit_ex2(source_ctx, NULL, key, iv, enc, NULL)) {
/* Error */
ALG_WARN("%s: cipher_init() error\n", __func__);
EVP_CIPHER_CTX_free(source_ctx);
return 0;
}
return 1;
}
#endif
static int cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
const unsigned char *iv, int enc)
{
struct cipher_ctx *cipher_ctx =
(struct cipher_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
size_t i = get_cipher_data_index(EVP_CIPHER_CTX_nid(ctx));
const struct cipher_data_st *cipher_d = &cipher_data[i];
int mode = EVP_CIPHER_CTX_mode(ctx);
__u32 afalg_mask;
int keylen;
if (cipher_ctx->bfd == -1) {
if (mode == EVP_CIPH_CTR_MODE)
cipher_ctx->blocksize = cipher_d->blocksize;
if (use_softdrivers == AFALG_REQUIRE_ACCELERATED)
afalg_mask = CRYPTO_ALG_KERN_DRIVER_ONLY;
else
afalg_mask = 0;
cipher_ctx->bfd = get_afalg_socket(cipher_d->name, "skcipher",
afalg_mask, afalg_mask);
if (cipher_ctx->bfd < 0) {
SYSerr(SYS_F_BIND, errno);
return 0;
}
}
if (cipher_ctx->sfd != -1) {
close(cipher_ctx->sfd);
cipher_ctx->sfd = -1;
}
if (key != NULL) {
if ((keylen = EVP_CIPHER_CTX_key_length(ctx)) > 0
&& !afalg_set_key(cipher_ctx->bfd, key, keylen, ALG_SET_KEY)) {
printf("cipher_init: Error setting key.\n");
goto err;
}
#ifndef AFALG_NO_FALLBACK
if (cipher_fb_ctx[i][enc]) {
if (!cipher_fb_init(cipher_ctx, cipher_fb_ctx[i][enc], key, iv,
enc)) {
printf("cipher_init: Warning: Cannot set fallback key."
" Fallback will not be used!\n");
} else {
cipher_ctx->fb_threshold = cipher_fb_threshold[i];
}
}
#endif
}
if ((cipher_ctx->sfd = accept(cipher_ctx->bfd, NULL, 0)) < 0) {
printf("cipher_init: accept");
goto err;
}
if (set_sendbuf_size(cipher_ctx->sfd))
goto err;
#ifdef AFALG_ZERO_COPY
if (pipe(cipher_ctx->pipes) < 0) {
printf("cipher_init: pipes");
goto err;
}
#endif
cipher_ctx->cipher_d = cipher_d;
return 1;
err:
close(cipher_ctx->bfd);
if (cipher_ctx->sfd >= 0) {
close(cipher_ctx->sfd);
cipher_ctx->sfd = -1;
}
return 0;
}
static int afalg_do_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
const unsigned char *in, size_t inl)
{
struct cipher_ctx *cipher_ctx =
(struct cipher_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
struct msghdr msg = { 0 };
struct iovec iov;
int res = -1;
int ret = 0;
int op = EVP_CIPHER_CTX_encrypting(ctx) ? ALG_OP_ENCRYPT : ALG_OP_DECRYPT;
int ivlen = EVP_CIPHER_CTX_iv_length(ctx);
unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
#ifndef AFALG_NO_FALLBACK
const EVP_CIPHER *fb_cipher;
int (*fb_do_cipher) (EVP_CIPHER_CTX *ctx, unsigned char *out,
const unsigned char *in, size_t inl);
#endif
if (ivlen < 0) {
fprintf(stderr, "Get IV length failed.\n");
return -1;
}
if (max_sendbuf_size < inl) {
fprintf(stderr, "Input data size (%zu) is too big to send to Kernel driver.\n", inl);
fprintf(stderr, "Please enlarge the wmem_max (/proc/sys/net/core/wmem_max) for further test\n");
return -1;
}
#ifndef AFALG_NO_FALLBACK
if (inl < (size_t)cipher_ctx->fb_threshold) {
ALG_DBG("%s: inl(%zu) < fb_threshold(%d), do_fb_cipher()\n",
__func__, inl, cipher_ctx->fb_threshold);
if ((fb_cipher = EVP_CIPHER_CTX_cipher(cipher_ctx->fallback))
&& (fb_do_cipher = EVP_CIPHER_meth_get_do_cipher(fb_cipher))) {
if (ivlen) {
memcpy(EVP_CIPHER_CTX_iv_noconst(cipher_ctx->fallback), iv, ivlen);
cipher_ctx->control_is_set = 0;
}
return fb_do_cipher(cipher_ctx->fallback, out, in, inl);
}
}
#endif
if (!cipher_ctx->control_is_set) {
afalg_set_control(&msg, op, iv, ivlen);
cipher_ctx->control_is_set = 1;
}
iov.iov_base = (void *)in;
iov.iov_len = inl;
#ifdef AFALG_ZERO_COPY
if (inl <= zc_maxsize && ((size_t)in & pagemask) == 0) {
if (msg.msg_control && sendmsg(cipher_ctx->sfd, &msg, 0) < 0) {
printf ("afalg_do_cipher: sendmsg");
goto out;
}
res = vmsplice(cipher_ctx->pipes[1], &iov, 1,
SPLICE_F_GIFT & SPLICE_F_MORE);
if (res < 0) {
printf("afalg_do_cipher: vmsplice");
goto out;
} else if (res != (ssize_t) inl) {
fprintf(stderr,
"afalg_do_cipher: vmsplice: sent %zd bytes != len %zd\n",
res, inl);
goto out;
}
res = splice(cipher_ctx->pipes[0], NULL, cipher_ctx->sfd, NULL, inl, 0);
if (res < 0) {
printf("afalg_do_cipher: splice");
goto out;
} else if (res != (ssize_t) inl) {
fprintf(stderr,
"afalg_do_cipher: splice: spliced %zd bytes != len %zd\n",
res, inl);
goto out;
}
} else
#endif
{
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
if ((res = sendmsg(cipher_ctx->sfd, &msg, 0)) < 0) {
printf("afalg_do_cipher: sendmsg");
goto out;
} else if (res != (size_t) inl) {
ALG_ERR("afalg_do_cipher: sent 0x%x bytes != len 0x%x\n",
(__u32)res, (__u32)inl);
goto out;
}
}
if ((res = read(cipher_ctx->sfd, out, inl)) == (size_t) inl)
ret = 1;
else
ALG_ERR("afalg_do_cipher: read 0x%x bytes != len 0x%x\n",
(__u32)res, (__u32)inl);
out:
if (msg.msg_control)
OPENSSL_free(msg.msg_control);
return ret;
}
static int cbc_do_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
const unsigned char *in, size_t inl)
{
#ifndef AFALG_NO_FALLBACK
int enc = EVP_CIPHER_CTX_encrypting(ctx);
int ivlen = EVP_CIPHER_CTX_iv_length(ctx);
unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
unsigned char saved_iv[EVP_MAX_IV_LENGTH];
int ret;
if (ivlen < 0) {
fprintf(stderr, "Get IV length failed.\n");
return -1;
}
assert(inl >= ivlen);
if (!enc)
memcpy(saved_iv, in + inl - ivlen, ivlen);
if ((ret = afalg_do_cipher(ctx, out, in, inl)))
memcpy(iv, enc ? out + inl - ivlen : saved_iv, ivlen);
return ret;
#else
return afalg_do_cipher(ctx, out, in, inl);
#endif
}
#if !defined(AFALG_KERNEL_UPDATES_CTR_IV) || !defined(AFALG_NO_FALLBACK)
static void ctr_update_iv(unsigned char *iv, size_t ivlen, __u64 nblocks)
{
__be64 *a = (__be64 *)(iv + ivlen);
__u64 b;
for (; ivlen >= 8; ivlen -= 8) {
b = nblocks + __be64_to_cpu(*--a);
*a = __cpu_to_be64(b);
if (nblocks < b)
return;
nblocks = 1;
}
}
#endif
static int ctr_do_blocks(EVP_CIPHER_CTX *ctx, struct cipher_ctx *cipher_ctx,
unsigned char *out, const unsigned char *in,
size_t inl, size_t nblocks)
{
#if !defined(AFALG_KERNEL_UPDATES_CTR_IV) || !defined(AFALG_NO_FALLBACK)
int ret;
int ivlen = EVP_CIPHER_CTX_iv_length(ctx);
unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
if (ivlen < 0) {
fprintf(stderr, "Get IV length failed.\n");
return -1;
}
ret = afalg_do_cipher(ctx, out, in, inl);
if (ret) {
if (cipher_ctx->control_is_set) {
ctr_update_iv(iv, ivlen, nblocks);
# ifndef AFALG_KERNEL_UPDATES_CTR_IV
cipher_ctx->control_is_set = 0;
# endif
} else {
# ifndef AFALG_NO_FALLBACK
memcpy(iv, EVP_CIPHER_CTX_iv(cipher_ctx->fallback), ivlen);
# endif
}
}
return ret;
#else
(void)cipher_ctx;
(void)nblocks;
return afalg_do_cipher(ctx, out, in, inl);
#endif
}
static int ctr_do_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
const unsigned char *in, size_t inl)
{
struct cipher_ctx *cipher_ctx =
(struct cipher_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
size_t nblocks, len;
/* handle initial partial block */
while (cipher_ctx->num && inl) {
(*out++) = *(in++) ^ cipher_ctx->partial[cipher_ctx->num];
--inl;
cipher_ctx->num = (cipher_ctx->num + 1) % cipher_ctx->blocksize;
}
/* process full blocks */
if (inl >= (unsigned int) cipher_ctx->blocksize) {
nblocks = inl / cipher_ctx->blocksize;
len = nblocks * cipher_ctx->blocksize;
if (!ctr_do_blocks(ctx, cipher_ctx, out, in, len, nblocks))
return 0;
inl -= len;
out += len;
in += len;
}
/* process final partial block */
if (inl) {
memset(cipher_ctx->partial, 0, cipher_ctx->blocksize);
if (!ctr_do_blocks(ctx, cipher_ctx, cipher_ctx->partial,
cipher_ctx->partial, cipher_ctx->blocksize, 1))
return 0;
while (inl--) {
out[cipher_ctx->num] = in[cipher_ctx->num]
^ cipher_ctx->partial[cipher_ctx->num];
cipher_ctx->num++;
}
}
return 1;
}
static int cipher_ctrl(EVP_CIPHER_CTX *ctx, int type, int p1, void* p2)
{
struct cipher_ctx *cipher_ctx =
(struct cipher_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
struct cipher_ctx *to_cipher_ctx;
(void)p1;
switch (type) {
case EVP_CTRL_COPY:
if (cipher_ctx == NULL)
return 1;
/* when copying the context, a new session needs to be initialized */
to_cipher_ctx = (struct cipher_ctx *)EVP_CIPHER_CTX_get_cipher_data(
(EVP_CIPHER_CTX *)p2);
to_cipher_ctx->bfd = to_cipher_ctx->sfd = -1;
to_cipher_ctx->control_is_set = 0;
#ifdef AFALG_ZERO_COPY
if (pipe(to_cipher_ctx->pipes) != 0)
return 0;
#endif
#ifndef AFALG_NO_FALLBACK
if (cipher_ctx->fallback) {
if (!(to_cipher_ctx->fallback = EVP_CIPHER_CTX_new()))
return 0;
if (!EVP_CIPHER_CTX_copy(to_cipher_ctx->fallback,
cipher_ctx->fallback)) {
EVP_CIPHER_CTX_free(to_cipher_ctx->fallback);
to_cipher_ctx->fallback = NULL;
return 0;