File: kcapi-kernel-if.c

package info (click to toggle)
libkcapi 1.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 2,992 kB
  • sloc: ansic: 13,808; sh: 2,422; perl: 1,949; makefile: 287
file content (1467 lines) | stat: -rw-r--r-- 37,277 bytes parent folder | download
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
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
/* Kernel crypto API AF_ALG interface code
 *
 * Copyright (C) 2015 - 2022, Stephan Mueller <smueller@chronox.de>
 *
 * License: see COPYING file in root directory
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
 * WHICH ARE HEREBY DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
 * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 */

#define _GNU_SOURCE
#include <stdarg.h>
#include <unistd.h>
#include <asm/types.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <unistd.h>
#include <sys/user.h>
#include <sys/eventfd.h>
#include <time.h>
#include <sys/select.h>
#include <sys/utsname.h>
#include <sys/prctl.h>

#include "cryptouser.h"
#include "kcapi.h"
#include "internal.h"

/************************************************************
 * Common helper used within the lib and as an API
 ************************************************************/
IMPL_SYMVER(memset_secure, "1.3.1")
void impl_memset_secure(void *s, int c, size_t n)
{
	memset(s, c, n);
	__asm__ __volatile__("" : : "r" (s) : "memory");
}

ORIG_SYMVER(memset_secure, "0.12.0")
void orig_memset_secure(void *s, int c, uint32_t n)
{
        impl_memset_secure(s, c, n);
}

/************************************************************
 * Logging logic
 ************************************************************/
enum kcapi_verbosity kcapi_verbosity_level = KCAPI_LOG_ERR;

void kcapi_dolog(enum kcapi_verbosity severity, const char *fmt, ...)
{
	va_list args;
	char msg[128];
	char sev[16];

	if (severity > kcapi_verbosity_level)
		return;

	memset(sev, 0, sizeof(sev));
	memset(msg, 0, sizeof(msg));

	va_start(args, fmt);
	vsnprintf(msg, sizeof(msg) - 1, fmt, args);
	va_end(args);

	switch (severity) {
	case KCAPI_LOG_DEBUG:
		snprintf(sev, sizeof(sev), "Debug");
		break;
	case KCAPI_LOG_VERBOSE:
		snprintf(sev, sizeof(sev), "Verbose");
		break;
	case KCAPI_LOG_WARN:
		snprintf(sev, sizeof(sev), "Warning");
		break;
	case KCAPI_LOG_ERR:
		snprintf(sev, sizeof(sev), "Error");
		break;
	default:
		snprintf(sev, sizeof(sev), "Unknown");
	}
	fprintf(stderr, "libkcapi - %s: %s\n", sev, msg);
}

/************************************************************
 * Internal logic
 ************************************************************/

int _kcapi_common_accept(struct kcapi_handle *handle)
{
	struct kcapi_handle_tfm *tfm = handle->tfm;
	int fd;

	if (*_kcapi_get_opfd(handle) != -1)
		return 0;

	fd = accept(tfm->tfmfd, NULL, 0);
	if (fd == -1) {
		int errsv;

		errsv = errno;
		kcapi_dolog(KCAPI_LOG_ERR, "AF_ALG: accept failed");
		return -errsv;
	}
	kcapi_dolog(KCAPI_LOG_DEBUG, "AF_ALG: accept syscall successful");

	*_kcapi_get_opfd(handle) = fd;

	return 0;
}

#ifdef __GLIBC__
static inline size_t kcapi_downcast_int(size_t in)
{
	return in;
}

static inline size_t kcapi_downcast_socklen_t(size_t in)
{
	return in;
}
#else
static inline int kcapi_downcast_int(size_t in)
{
	if (in > INT_MAX)
		return INT_MAX;
	return (int)in;
}

static inline socklen_t kcapi_downcast_socklen_t(size_t in)
{
	return (socklen_t)in;
}
#endif

ssize_t _kcapi_common_send_meta(struct kcapi_handle *handle,
				struct iovec *iov, size_t iovlen,
				unsigned int enc, uint32_t flags)
{
	struct kcapi_handle_tfm *tfm = handle->tfm;
	ssize_t ret;
	char buffer_static[80] = { 0 };
	char *buffer_p = buffer_static;
	char *buffer_alloc = NULL;

	/* plaintext / ciphertext data */
	struct cmsghdr *header = NULL;
	uint32_t *type = NULL;
	struct msghdr msg;

	/* IV data */
	struct af_alg_iv *alg_iv = NULL;
	size_t iv_msg_size = handle->cipher.iv ?
			  CMSG_SPACE(sizeof(*alg_iv) + tfm->info.ivsize) :
			  0;

	/* AEAD data */
	uint32_t *assoclen = NULL;
	size_t assoc_msg_size = handle->aead.assoclen ?
				CMSG_SPACE(sizeof(*assoclen)) : 0;

	size_t bufferlen =
		CMSG_SPACE(sizeof(*type)) + 	/* Encryption / Decryption */
		iv_msg_size +			/* IV */
		assoc_msg_size;			/* AEAD associated data size */

	ret = _kcapi_common_accept(handle);
	if (ret)
		return ret;

	memset(&msg, 0, sizeof(msg));

	/* allocate buffer, if static buffer is too small */
	if (bufferlen > sizeof(buffer_static)) {
		buffer_alloc = calloc(1, bufferlen);
		if (!buffer_alloc)
			return -ENOMEM;
		buffer_p = buffer_alloc;
		kcapi_dolog(KCAPI_LOG_VERBOSE,
			    "_kcapi_common_send_meta_fd: submission buffer of size %zu allocated",
			    bufferlen);
	}

	msg.msg_control = buffer_p;
	msg.msg_iov = iov;
	msg.msg_iovlen = kcapi_downcast_int(iovlen);
	msg.msg_controllen = kcapi_downcast_socklen_t(bufferlen);
	/* encrypt/decrypt operation */
	header = CMSG_FIRSTHDR(&msg);
	if (!header) {
		ret = -EFAULT;
		goto out;
	}
	header->cmsg_level = SOL_ALG;
	header->cmsg_type = ALG_SET_OP;
	header->cmsg_len = CMSG_LEN(sizeof(*type));
	type = (void*)CMSG_DATA(header);
	*type = enc;

	/* set IV */
	if (handle->cipher.iv) {
		header = CMSG_NXTHDR(&msg, header);
		if (!header) {
			ret = -EFAULT;
			goto out;
		}
		header->cmsg_level = SOL_ALG;
		header->cmsg_type = ALG_SET_IV;
		header->cmsg_len = kcapi_downcast_socklen_t(iv_msg_size);
		alg_iv = (void*)CMSG_DATA(header);
		alg_iv->ivlen = tfm->info.ivsize;
		memcpy(alg_iv->iv, handle->cipher.iv, tfm->info.ivsize);
	}

	/* set AEAD information */
	if (handle->aead.assoclen) {
		/* Set associated data length */
		header = CMSG_NXTHDR(&msg, header);
		if (!header) {
			ret = -EFAULT;
			goto out;
		}
		header->cmsg_level = SOL_ALG;
		header->cmsg_type = ALG_SET_AEAD_ASSOCLEN;
		header->cmsg_len = CMSG_LEN(sizeof(*assoclen));
		assoclen = (void*)CMSG_DATA(header);
		*assoclen = (uint32_t)handle->aead.assoclen;
	}

	ret = sendmsg(*_kcapi_get_opfd(handle), &msg, (int)flags);
	if (ret < 0)
		ret = -errno;
	kcapi_dolog(KCAPI_LOG_DEBUG,
		    "AF_ALG: sendmsg syscall returned %zd", ret);

out:
	kcapi_memset_secure(buffer_p, 0, bufferlen);
	if (buffer_alloc)
		free(buffer_alloc);
	return ret;
}

ssize_t _kcapi_common_send_data(struct kcapi_handle *handle,
				struct iovec *iov, size_t iovlen,
				uint32_t flags)
{
	struct msghdr msg;
	ssize_t ret;

	ret = _kcapi_common_accept(handle);
	if (ret)
		return ret;

	msg.msg_name = NULL;
	msg.msg_namelen = 0;
	msg.msg_control = NULL;
	msg.msg_controllen = 0;
	msg.msg_flags = 0;
	msg.msg_iov = iov;
	msg.msg_iovlen = kcapi_downcast_int(iovlen);
	ret = sendmsg(*_kcapi_get_opfd(handle), &msg, (int)flags);
	if (ret < 0)
		ret = -errno;
	kcapi_dolog(KCAPI_LOG_DEBUG, "AF_ALG: sendmsg syscall returned %zd",
		    ret);

	return ret;
}

ssize_t _kcapi_common_vmsplice_iov(struct kcapi_handle *handle,
				   struct iovec *iov, size_t iovlen,
				   uint32_t flags)
{
	ssize_t ret = 0;
	size_t i, inlen = 0;

	if (iovlen && !iov)
		return -EINVAL;

	for (i = 0; i < iovlen; i++)
		inlen += iov[i].iov_len;

	/*
	 * The kernel has the following limits for vmsplice / splice:
	 *
	 * 1. At most only 16 pages are allowed to be sent to the kernel -
	 *    this is a hard limit in the vmsplice system call.
	 * 2. At most, the entire buffer size must not be larger than the
	 *    buffer size of the pipe used for the splice. If desired, this
	 *    buffer size can be enlarged with kcapi_set_maxsplicesize().
	 *    On the other side, the maximum buffer size can be obtained via
	 *    kcapi_get_maxsplicesize().
	 * 3. vmsplice cannot be used for a zero buffer.
	 *
	 * If any of the aforementioned conditions is not satisfied, the
	 * sendmsg() system call is used which copies the data into the
	 * kernel.
	 */
	handle->processed_sg += iovlen;
	if (handle->processed_sg > (size_t)handle->flags.alg_max_pages ||
	    !inlen ||
	    inlen > handle->pipesize) {
		kcapi_dolog(KCAPI_LOG_DEBUG,
			    "AF_ALG: Using fallback of sendmsg instead of splice");
		return _kcapi_common_send_data(handle, iov, iovlen,
					       (flags & SPLICE_F_MORE) ?
					        MSG_MORE : 0);
	}

	ret = _kcapi_common_accept(handle);
	if (ret)
		return ret;

	ret = vmsplice(handle->pipes[1], iov, iovlen, SPLICE_F_GIFT|flags);
	if (ret < 0) {
		ret = -errno;
		kcapi_dolog(KCAPI_LOG_DEBUG,
			    "AF_ALG: vmsplice syscall returned %zd", ret);
		return ret;
	}
	kcapi_dolog(KCAPI_LOG_DEBUG, "AF_ALG: vmsplice syscall returned %zd",
		    ret);

	if ((uint32_t)ret != inlen) {
		kcapi_dolog(KCAPI_LOG_ERR, "vmsplice: not all data received by kernel (data received: %ld -- data sent: %lu)",
			(long)ret, (unsigned long)inlen);
		return -EFAULT;
	}

	ret = splice(handle->pipes[0], NULL, *_kcapi_get_opfd(handle), NULL,
		     (size_t)ret, flags);
	if (ret < 0)
		ret = -errno;
	kcapi_dolog(KCAPI_LOG_DEBUG, "AF_ALG: splice syscall returned %zd",
		    ret);

	return ret;
}

ssize_t _kcapi_common_vmsplice_chunk(struct kcapi_handle *handle,
				     const uint8_t *in, size_t inlen,
				     uint32_t flags)
{
	struct iovec iov;
	ssize_t processed = 0;
	ssize_t ret = 0;
	uint32_t sflags = (flags & SPLICE_F_MORE) ? MSG_MORE : 0;

	if (inlen > INT_MAX)
		return -EMSGSIZE;

	if (!inlen)
		return _kcapi_common_send_data(handle, NULL, 0, sflags);

	ret = _kcapi_common_accept(handle);
	if (ret)
		return ret;

	while (inlen) {
		iov.iov_base = (void*)(uintptr_t)(in + processed);
		iov.iov_len = inlen;

		/*
		 * See comment in _kcapi_common_vmsplice_iov for the explanation
		 * of this check.
		 */
		if ((handle->processed_sg++) > handle->flags.alg_max_pages ||
		    !inlen ||
		    inlen > handle->pipesize) {
			kcapi_dolog(KCAPI_LOG_DEBUG,
				    "AF_ALG: Using fallback of sendmsg instead of splice");
			ret = _kcapi_common_send_data(handle, &iov, 1, sflags);
			if (ret < 0)
				return ret;
		} else {
			ret = vmsplice(handle->pipes[1], &iov, 1,
				       SPLICE_F_GIFT|flags);
			if (ret < 0) {
				ret = -errno;
				kcapi_dolog(KCAPI_LOG_DEBUG,
					    "AF_ALG: vmsplice syscall returned %zd",
					    ret);
				return ret;
			}
			kcapi_dolog(KCAPI_LOG_DEBUG,
				    "AF_ALG: vmsplice syscall returned %zd",
				    ret);

			ret = splice(handle->pipes[0], NULL,
				     *_kcapi_get_opfd(handle), NULL,
				     (size_t)ret, flags);
			if (ret < 0) {
				ret = -errno;
				kcapi_dolog(KCAPI_LOG_DEBUG,
					    "AF_ALG: splice syscall returned %zd",
					    ret);
				return ret;
			}
			kcapi_dolog(KCAPI_LOG_DEBUG,
				    "AF_ALG: splice syscall returned %zd", ret);
		}

		processed += ret;
		inlen -= (uint32_t)ret;
	}

	return processed;
}

/* Wrapper for io_getevents -- returns < 0 on error, or processed bytes */
int _kcapi_aio_read_all(struct kcapi_handle *handle, size_t toread,
			struct timespec *timeout)
{
	if (toread > KCAPI_AIO_CONCURRENT)
		return -EINVAL;

	while (toread) {
		int i;
		struct io_event events[KCAPI_AIO_CONCURRENT];
		int rc = io_getevents(handle->aio.aio_ctx, 1, (long)toread,
				      events, timeout);

		if (rc < 0)
			return rc;

		for (i = 0; i < rc; i++) {
			struct iocb *cb;

			/*
			 * If one cipher operation fails, so will the entire
			 * AIO operation
			 */
			if (events[i].res < 0) {
				handle->aio.iocb_ret[events[i].data] =
							events[i].res;
				return (int)events[i].res;
			}

			cb = (struct iocb *)(uintptr_t)events[i].obj;

			/*
			 * Older symmetric AIO implementations used a wrong
			 * return code.
			 */
			if (events[i].res > 0) {
				handle->aio.iocb_ret[events[i].data] =
								events[i].res;
			} else {
				handle->aio.iocb_ret[events[i].data] =
							(__s64)cb->aio_nbytes;
			}

			cb->aio_fildes = 0;
		}
		toread -= (uint32_t)rc;
	}

	return 0;
}

int _kcapi_aio_send_iov(struct kcapi_handle *handle, struct iovec *iov,
			size_t iovlen, int access, unsigned int enc)
{
	ssize_t ret;
	size_t len = iov ? iov->iov_len : 0;

	/*
	 * Using two syscalls with memcpy is faster than four syscalls
	 * without memcpy below the given threshold.
	 */
	if ((access == KCAPI_ACCESS_HEURISTIC && len <= (1<<13)) ||
	    access == KCAPI_ACCESS_SENDMSG) {
		ret = _kcapi_common_send_meta(handle, iov, iovlen, enc, 0);
		if (0 > ret)
			return (int)ret;
	} else {
		ret = _kcapi_common_send_meta(handle, NULL, 0, enc, MSG_MORE);
		if (0 > ret)
			return (int)ret;
		ret = _kcapi_common_vmsplice_iov(handle, iov, iovlen, 0);
		if (0 > ret)
			return (int)ret;
	}

	return 0;
}

int _kcapi_aio_read_iov(struct kcapi_handle *handle,
			struct iovec *iov, size_t iovlen)
{
	struct iocb *cb = handle->aio.cio;
	size_t i;
	int32_t ret;

	if (iovlen > KCAPI_AIO_CONCURRENT)
		return -EFAULT;

	for (i = 0; i < iovlen; i++) {
		while (cb->aio_fildes) {
			struct timespec timeout;

			timeout.tv_sec = 0;
			timeout.tv_nsec = 10000;
			ret = _kcapi_aio_read_all(handle, iovlen, &timeout);
			if (ret < 0)
				return ret;
		}

		memset(cb, 0, sizeof(*cb));
		cb->aio_fildes = (__u32)*_kcapi_get_opfd(handle);
		cb->aio_lio_opcode = IOCB_CMD_PREAD;
		cb->aio_buf = (unsigned long)iov->iov_base;
		cb->aio_offset = 0;
		cb->aio_data = i;
		cb->aio_nbytes = iov->iov_len;
		cb->aio_flags = IOCB_FLAG_RESFD;
		cb->aio_resfd = (__u32)handle->aio.efd;

		handle->aio.iocb_ret[i] = AIO_OUTSTANDING;

		cb++;
		iov++;
	}

	ret = io_submit(handle->aio.aio_ctx, (long)iovlen, handle->aio.ciopp);
	if ((uint32_t)ret != iovlen) {
		if (ret < 0) {
			ret = -errno;
			kcapi_dolog(KCAPI_LOG_ERR, "io_read Error: %d\n", ret);
			return ret;
		} else {
			kcapi_dolog(KCAPI_LOG_ERR,
				    "Could not sumbit AIO read\n");
			return -EIO;
		}
	}

	return _kcapi_aio_read_all(handle, iovlen, NULL);
}

ssize_t _kcapi_common_recv_data(struct kcapi_handle *handle,
				struct iovec *iov, size_t iovlen)
{
	struct msghdr msg;
	ssize_t ret;

	ret = _kcapi_common_accept(handle);
	if (ret)
		return ret;

	msg.msg_name = NULL;
	msg.msg_namelen = 0;
	msg.msg_control = NULL;
	msg.msg_controllen = 0;
	msg.msg_flags = 0;
	msg.msg_iov = iov;
#ifdef __GLIBC__
	msg.msg_iovlen = iovlen;
#else
	msg.msg_iovlen = (int)iovlen;
#endif
	ret = recvmsg(*_kcapi_get_opfd(handle), &msg, 0);
	if (ret < 0)
		ret = -errno;
	kcapi_dolog(KCAPI_LOG_DEBUG, "AF_ALG: recvmsg syscall returned %zd",
		    ret);

	/*
	 * As the iovecs are processed and removed from the list in the kernel
	 * we can also reset the list of processed iovecs here.
	 *
	 * Note, if there is an error, the kernel keeps the list unless it is
	 * a "valid" error of EBADMSG indicating an integrity error of the
	 * crypto operation.
	 */
	if (ret >= 0 || ret == -EBADMSG)
		handle->processed_sg = 0;

#if 0
	/*
	 * Truncated message digests can be identified with this check.
	 */
	if (msg.msg_flags & MSG_TRUNC) {
		fprintf(stderr, "recvmsg: processed data was truncated by kernel (only %lu bytes processed)\n", (unsigned long)ret);
		return -EMSGSIZE;
	}
#endif

	return ret;
}

ssize_t _kcapi_common_read_data(struct kcapi_handle *handle,
				   uint8_t *out, size_t outlen)
{
	ssize_t ret;
	ssize_t totallen = 0;

	if (outlen > INT_MAX)
		return -EMSGSIZE;

	ret = _kcapi_common_accept(handle);
	if (ret)
		return ret;


	if (outlen) {
		do {
			ret = read(*_kcapi_get_opfd(handle), out, outlen);
			if (ret > 0) {
				out += ret;
				outlen -= (uint32_t)ret;
				totallen += ret;
			}
			kcapi_dolog(KCAPI_LOG_DEBUG,
				    "AF_ALG: read syscall returned %zd", ret);
		} while ((ret > 0 || errno == EINTR) && outlen);

		if (ret < 0)
			return -errno;
	}

	return totallen;
}

int _kcapi_common_setkey(struct kcapi_handle *handle,
			 const uint8_t *key, uint32_t keylen)
{
	struct kcapi_handle_tfm *tfm = handle->tfm;
	int ret;

	ret = setsockopt(tfm->tfmfd, SOL_ALG, ALG_SET_KEY, key, keylen);
	if (ret < 0)
		ret = -errno;
	kcapi_dolog(KCAPI_LOG_DEBUG,
		    "AF_ALG setkey: setsockopt syscall returned %d", ret);

	return ret;
}

int _kcapi_common_setentropy(struct kcapi_handle *handle,
			     const uint8_t *ent, uint32_t entlen)
{
	struct kcapi_handle_tfm *tfm = handle->tfm;
	int ret;

	ret = setsockopt(tfm->tfmfd, SOL_ALG, ALG_SET_DRBG_ENTROPY, ent,
			 entlen);
	if (ret < 0)
		ret = -errno;
	kcapi_dolog(KCAPI_LOG_DEBUG,
		    "AF_ALG setentropy: setsockopt syscall returned %d", ret);

	return ret;
}

static int __kcapi_common_getinfo(struct kcapi_handle *handle,
				  const char *ciphername,
				  int drivername)
{
	struct kcapi_handle_tfm *tfm = handle->tfm;
	int ret = -EFAULT;

	/* NETLINK_CRYPTO specific */
	char buf[4096];
	struct nlmsghdr *res_n = (struct nlmsghdr *)buf;
	struct {
		struct nlmsghdr n;
		struct crypto_user_alg cru;
	} req;
	struct crypto_user_alg *cru_res = NULL;
	unsigned long res_len = 0;
	struct rtattr *tb[CRYPTOCFGA_MAX+1];
	struct rtattr *rta;

	/* AF_NETLINK specific */
	struct sockaddr_nl nl;
	int sd = 0;
	socklen_t addr_len;
	struct iovec iov;
	struct msghdr msg;

	memset(&req, 0, sizeof(req));
	memset(&buf, 0, sizeof(buf));
	memset(&msg, 0, sizeof(msg));

	req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.cru));
	req.n.nlmsg_flags = NLM_F_REQUEST;
	req.n.nlmsg_type = CRYPTO_MSG_GETALG;
	req.n.nlmsg_seq = (__u32)time(NULL);

	if (drivername)
		strncpy(req.cru.cru_driver_name, ciphername,
			sizeof(req.cru.cru_driver_name) - 1);
	else
		strncpy(req.cru.cru_name, ciphername, sizeof(req.cru.cru_name) - 1);

	/* talk to netlink socket */
	sd =  socket(AF_NETLINK, SOCK_RAW, NETLINK_CRYPTO);
	if (sd < 0) {
		kcapi_dolog(KCAPI_LOG_ERR,
			    "Netlink error: cannot open netlink socket");
		return -errno;
	}
	memset(&nl, 0, sizeof(nl));
	nl.nl_family = AF_NETLINK;
	if (bind(sd, (struct sockaddr*)&nl, sizeof(nl)) < 0) {
		ret = -errno;
		kcapi_dolog(KCAPI_LOG_ERR,
			    "Netlink error: cannot bind netlink socket");
		goto out;
	}
	/* sanity check that netlink socket was successfully opened */
	addr_len = sizeof(nl);
	if (getsockname(sd, (struct sockaddr*)&nl, &addr_len) < 0) {
		ret = -errno;
		kcapi_dolog(KCAPI_LOG_ERR, "Netlink error: cannot getsockname");
		goto out;
	}
	if (addr_len != sizeof(nl)) {
		ret = -errno;
		kcapi_dolog(KCAPI_LOG_ERR,
			    "Netlink error: wrong address length %d", addr_len);
		goto out;
	}
	if (nl.nl_family != AF_NETLINK) {
		ret = -errno;
		kcapi_dolog(KCAPI_LOG_ERR,
			    "Netlink error: wrong address family %d",
			    nl.nl_family);
		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(sd, &msg, 0) < 0) {
		ret = -errno;
		kcapi_dolog(KCAPI_LOG_ERR, "Netlink error: sendmsg failed");
		goto out;
	}
	memset(buf,0,sizeof(buf));
	iov.iov_base = buf;
	while (1) {
		ssize_t rc;
		iov.iov_len = sizeof(buf);
		rc = recvmsg(sd, &msg, 0);
		if (rc < 0) {
			if (errno == EINTR || errno == EAGAIN)
				continue;
			ret = -errno;
			kcapi_dolog(KCAPI_LOG_ERR,
				    "Netlink error: netlink receive error");
			goto out;
		}
		if (rc == 0) {
			ret = -errno;
			kcapi_dolog(KCAPI_LOG_ERR, "Netlink error: no data");
			goto out;
		}
		if (rc > (ssize_t)sizeof(buf)) {
			ret = -errno;
			kcapi_dolog(KCAPI_LOG_ERR,
				    "Netlink error: received too much data");
			goto out;
		}
		break;
	}

	ret = -EFAULT;
	res_len = res_n->nlmsg_len;
	if (res_n->nlmsg_type == NLMSG_ERROR) {
		/*
		 * return -EAGAIN -- this error will occur if we received a
		 * driver name, but used it for a generic name. Allow caller
		 * to invoke function again where driver name is looked up
		 */
		ret = -EAGAIN;
		goto out;
	}

	if (res_n->nlmsg_type == CRYPTO_MSG_GETALG) {
		cru_res = NLMSG_DATA(res_n);
		if (res_len < NLMSG_SPACE(sizeof(*cru_res))) {
			kcapi_dolog(KCAPI_LOG_ERR, "Netlink error: nlmsg len %lu",
				    res_len);
			goto out;
		}
		res_len -= NLMSG_SPACE(sizeof(*cru_res));
	}

	/* parse data */
	if (!cru_res) {
		ret = -EFAULT;
		goto out;
	}
	rta = CR_RTA(cru_res);
	memset(tb, 0, sizeof(struct rtattr *) * (CRYPTOCFGA_MAX + 1));
	while (RTA_OK(rta, res_len)) {
		if ((rta->rta_type <= CRYPTOCFGA_MAX) && (!tb[rta->rta_type]))
			tb[rta->rta_type] = rta;
		rta = RTA_NEXT(rta, res_len);
	}
	if (res_len) {
		kcapi_dolog(KCAPI_LOG_ERR, "Netlink error: unprocessed data %lu",
			    res_len);
		goto out;
	}

	if (tb[CRYPTOCFGA_REPORT_HASH]) {
		struct rtattr *rta = tb[CRYPTOCFGA_REPORT_HASH];
		struct crypto_report_hash *rsh =
			(struct crypto_report_hash *) RTA_DATA(rta);
		tfm->info.hash_digestsize = rsh->digestsize;
		tfm->info.blocksize = rsh->blocksize;
		kcapi_dolog(KCAPI_LOG_DEBUG,
			    "Get cipher info: hash with digestsize %u,  blocksize %u",
			    rsh->digestsize, rsh->blocksize);

		tfm->info.cipher_type = KCAPI_CIPHER_MD;
	}
	if (tb[CRYPTOCFGA_REPORT_BLKCIPHER]) {
		struct rtattr *rta = tb[CRYPTOCFGA_REPORT_BLKCIPHER];
		struct crypto_report_blkcipher *rblk =
			(struct crypto_report_blkcipher *) RTA_DATA(rta);
		tfm->info.blocksize = rblk->blocksize;
		tfm->info.ivsize = rblk->ivsize;
		tfm->info.blk_min_keysize = rblk->min_keysize;
		tfm->info.blk_max_keysize = rblk->max_keysize;
		kcapi_dolog(KCAPI_LOG_DEBUG, "Get cipher info: block cipher with blocksize %u, ivsize %u, minimum keysize %u, maximum keysize %u",
			    rblk->blocksize, rblk->ivsize, rblk->min_keysize,
			    rblk->max_keysize);

		tfm->info.cipher_type = KCAPI_CIPHER_SKCIPHER;
	}
	if (tb[CRYPTOCFGA_REPORT_AEAD]) {
		struct rtattr *rta = tb[CRYPTOCFGA_REPORT_AEAD];
		struct crypto_report_aead *raead =
			(struct crypto_report_aead *) RTA_DATA(rta);
		tfm->info.blocksize = raead->blocksize;
		tfm->info.ivsize = raead->ivsize;
		tfm->info.aead_maxauthsize = raead->maxauthsize;
		kcapi_dolog(KCAPI_LOG_DEBUG, "Get cipher info: AEAD block cipher with blocksize %u, ivsize %u, maximum authentication size %u",
			    raead->blocksize, raead->ivsize,
			    raead->maxauthsize);

		tfm->info.cipher_type = KCAPI_CIPHER_AEAD;
	}
	if (tb[CRYPTOCFGA_REPORT_RNG]) {
		struct rtattr *rta = tb[CRYPTOCFGA_REPORT_RNG];
		struct crypto_report_rng *rrng =
			(struct crypto_report_rng *) RTA_DATA(rta);
		tfm->info.rng_seedsize = rrng->seedsize;
		kcapi_dolog(KCAPI_LOG_DEBUG,
			    "Get cipher info: RNG cipher with seedsize %u",
			    rrng->seedsize);

		tfm->info.cipher_type = KCAPI_CIPHER_RNG;
	}
	if (tb[CRYPTOCFGA_UNSPEC])
		kcapi_dolog(KCAPI_LOG_DEBUG,
			    "Get cipher info: unspecified data received");
	if (tb[CRYPTOCFGA_PRIORITY_VAL])
		kcapi_dolog(KCAPI_LOG_DEBUG,
			    "Get cipher info: u32 value received");
	if (tb[CRYPTOCFGA_REPORT_LARVAL])
		kcapi_dolog(KCAPI_LOG_DEBUG,
			    "Get cipher info: larval value received");
	if (tb[CRYPTOCFGA_REPORT_COMPRESS])
		kcapi_dolog(KCAPI_LOG_DEBUG,
			    "Get cipher info: compression algorithm type received");
	if (tb[CRYPTOCFGA_REPORT_CIPHER])
		kcapi_dolog(KCAPI_LOG_DEBUG,
			    "Get cipher info: simple cipher algorithm type received");
	if (tb[CRYPTOCFGA_REPORT_AKCIPHER]) {
		kcapi_dolog(KCAPI_LOG_DEBUG,
			    "Get cipher info: asymmetric cipher algorithm type received");

		tfm->info.cipher_type = KCAPI_CIPHER_AKCIPHER;
	}
	if (tb[CRYPTOCFGA_REPORT_KPP]) {
		kcapi_dolog(KCAPI_LOG_DEBUG,
			    "Get cipher info: kpp cipher algorithm type received");

		tfm->info.cipher_type = KCAPI_CIPHER_KPP;
	}
	if (tb[CRYPTOCFGA_REPORT_ACOMP])
		kcapi_dolog(KCAPI_LOG_DEBUG,
			    "Get cipher info: asymmetric compression algorithm type received");
	kcapi_dolog(KCAPI_LOG_VERBOSE,
		    "Get cipher info: all information for %s received from kernel",
		    ciphername);

	ret = 0;

out:
	close(sd);
	return ret;
}

static int _kcapi_common_getinfo(struct kcapi_handle *handle,
				 const char *ciphername)
{
	int ret = __kcapi_common_getinfo(handle, ciphername, 0);
	if (ret)
		return __kcapi_common_getinfo(handle, ciphername, 1);
	return 0;
}

static inline void _kcapi_aio_destroy(struct kcapi_handle *handle)
{
	if (handle->aio.efd != -1)
		close(handle->aio.efd);
	handle->aio.efd = -1;
	if (handle->aio.aio_ctx)
		io_destroy(handle->aio.aio_ctx);
	handle->aio.aio_ctx = 0;
	if (handle->aio.cio)
		free(handle->aio.cio);
	handle->aio.cio = NULL;
	if (handle->aio.ciopp)
		free(handle->aio.ciopp);
	handle->aio.ciopp = NULL;
	if (handle->aio.iocb_ret)
		free(handle->aio.iocb_ret);
	handle->aio.iocb_ret = NULL;
}

static void _kcapi_handle_destroy_tfm(struct kcapi_handle *handle)
{
	struct kcapi_handle_tfm *tfm;
	if (!handle || !handle->tfm)
		return;

	tfm = handle->tfm;
	if (atomic_dec_and_test(&tfm->refcnt)) {
		if (tfm->tfmfd != -1)
			close(tfm->tfmfd);
		kcapi_memset_secure(tfm, 0, sizeof(*tfm));
		free(tfm);
		handle->tfm = NULL;
	}
}

void _kcapi_handle_destroy(struct kcapi_handle *handle)
{
	if (!handle)
		return;
	if (*_kcapi_get_opfd(handle) != -1)
		close(*_kcapi_get_opfd(handle));
	if (handle->pipes[0] != -1)
		close(handle->pipes[0]);
	if (handle->pipes[1] != -1)
		close(handle->pipes[1]);
	_kcapi_aio_destroy(handle);
	_kcapi_handle_destroy_tfm(handle);
	kcapi_memset_secure(handle, 0, sizeof(struct kcapi_handle));
	free(handle);
}

static int _kcapi_get_kernver(struct kcapi_handle *handle)
{
	struct kcapi_handle_tfm *tfm = handle->tfm;
	struct utsname kernel;
	char *saveptr = NULL;
	char *res = NULL;

	if (uname(&kernel))
		return -errno;

	/* 3.15.0 */
	res = strtok_r(kernel.release, ".", &saveptr);
	if (!res) {
		printf("Could not parse kernel version");
		return -EFAULT;
	}
	tfm->sysinfo.kernel_maj = strtoul(res, NULL, 10);
	res = strtok_r(NULL, ".", &saveptr);
	if (!res) {
		printf("Could not parse kernel version");
		return -EFAULT;
	}
	tfm->sysinfo.kernel_minor = strtoul(res, NULL, 10);
	res = strtok_r(NULL, ".", &saveptr);
	if (!res) {
		printf("Could not parse kernel version");
		return -EFAULT;
	}
	tfm->sysinfo.kernel_patchlevel = strtoul(res, NULL, 10);

	return 0;
}

/* return true if kernel is greater or equal to given values, otherwise false */
static bool _kcapi_kernver_ge(struct kcapi_handle *handle, unsigned int maj,
			      unsigned int minor, unsigned int patchlevel)
{
	struct kcapi_handle_tfm *tfm = handle->tfm;

	if (maj < tfm->sysinfo.kernel_maj)
		return true;
	if (maj == tfm->sysinfo.kernel_maj) {
		if (minor < tfm->sysinfo.kernel_minor)
			return true;
		if (minor == tfm->sysinfo.kernel_minor) {
			if (patchlevel <= tfm->sysinfo.kernel_patchlevel)
				return true;
		}
	}
	return false;
}

static int _kcapi_aio_init(struct kcapi_handle *handle)
{
	struct kcapi_handle_tfm *tfm = handle->tfm;
	uint32_t i;
	int err;

	if (tfm->info.cipher_type == KCAPI_CIPHER_AEAD) {
		if (!_kcapi_kernver_ge(handle, 4, 7, 0)) {
			kcapi_dolog(KCAPI_LOG_VERBOSE, "AIO support for AEAD cipher not present on current kernel");
			err = -EOPNOTSUPP;
			goto err;
		}
	}
	if (tfm->info.cipher_type == KCAPI_CIPHER_SKCIPHER) {
		if (!_kcapi_kernver_ge(handle, 4, 1, 0)) {
			kcapi_dolog(KCAPI_LOG_VERBOSE, "AIO support for symmetric ciphers not present on current kernel");
			err = -EOPNOTSUPP;
			goto err;
		}
	}

	handle->aio.cio = calloc(KCAPI_AIO_CONCURRENT, sizeof(struct iocb));
	if (!handle->aio.cio) {
		err = -ENOMEM;
		goto err;
	}

	handle->aio.ciopp = calloc(KCAPI_AIO_CONCURRENT, sizeof(void *));
	if (!handle->aio.ciopp) {
		err = -ENOMEM;
		goto err;
	}

	handle->aio.iocb_ret = calloc(KCAPI_AIO_CONCURRENT, sizeof(__s64));
	if (!handle->aio.iocb_ret) {
		err = -ENOMEM;
		goto err;
	}

	/*
	 * Set up the pointers to pointers array that is required by
	 * io_submit. Please do not ask me why the kernel wants this. :-)
	 */
	for (i = 0; i < KCAPI_AIO_CONCURRENT; i++)
		*(handle->aio.ciopp + i) = handle->aio.cio + i;

	handle->aio.efd = eventfd(0, EFD_CLOEXEC);
	if (handle->aio.efd < 0) {
		err = -errno;
		kcapi_dolog(KCAPI_LOG_ERR, "Event FD cannot be initialized: %d\n",
			    err);
		goto err;
	}

	err = io_setup(KCAPI_AIO_CONCURRENT, &handle->aio.aio_ctx);
	if (err < 0) {
		err = -errno;
		kcapi_dolog(KCAPI_LOG_ERR, "io_setup error %d\n", err);
		goto err;
	}

	handle->aio.disable = false;
	kcapi_dolog(KCAPI_LOG_VERBOSE, "asynchronous I/O initialized");

	return 0;

err:
	_kcapi_aio_destroy(handle);

	return err;
}

static void _kcapi_handle_flags(struct kcapi_handle *handle)
{
	/* new memory structure for AF_ALG AEAD interface + fixed hashing empty input */
	handle->flags.ge_v4_9 = _kcapi_kernver_ge(handle, 4, 9, 0);

	/* older interfaces only processed 16 pages in a row */
	handle->flags.alg_max_pages = ALG_MAX_PAGES;
}

static int _kcapi_handle_alloc(struct kcapi_handle **caller)
{
	struct kcapi_handle *handle = calloc(1, sizeof(struct kcapi_handle));

	if (!handle)
		return -ENOMEM;

	*_kcapi_get_opfd(handle) = -1;
	handle->pipes[0] = -1;
	handle->pipes[1] = -1;
	handle->aio.efd = -1;
	handle->pagesize = (unsigned int)sysconf(_SC_PAGESIZE);

	*caller = handle;

	return 0;
}

static int _kcapi_handle_init_op(struct kcapi_handle *handle, uint32_t flags)
{
	int ret;

	ret = pipe(handle->pipes);
	if (ret) {
		ret = -errno;
		kcapi_dolog(KCAPI_LOG_ERR,
			    "AF_ALG: pipe syscall failed (errno: %d)",
			    ret);
		return ret;
	}

	/*
	 * For vmsplice to allow the maximum number of 16 pages, we need to
	 * increase the pipe buffer by one more page - it seems the kernel
	 * uses some parts of the pipe for some house-keeping?!
	 */
	ret = kcapi_set_maxsplicesize(handle,
				      handle->pagesize * (ALG_MAX_PAGES + 1));
	if (ret < 0) {
		kcapi_dolog(KCAPI_LOG_WARN,
			    "AF_ALG: setting maximum buffer size failed: %d)",
			    ret);
		ret = kcapi_get_maxsplicesize(handle);
		if (ret < 0)
			return ret;

	}
	ret = 0;
	kcapi_dolog(KCAPI_LOG_DEBUG, "AF_ALG: pipe syscall passed");

	if (flags & KCAPI_INIT_AIO) {
		ret = _kcapi_aio_init(handle);

		/*
		 * We complain about kernels without AIO support, but allow
		 * the allocation nonetheless as we have synchronous fallbacks
		 * for all AIO implementations. This allows the use of
		 * libkcapi AIO API even though the kernel does not support it.
		 * This allows compatibility for applications.
		 */
		if (ret && ret != -EOPNOTSUPP)
			return ret;
		ret = 0;
	} else
		handle->aio.disable = true;

	_kcapi_handle_flags(handle);

	return ret;
}

static int _kcapi_handle_init_tfm(struct kcapi_handle *handle, const char *type,
				  const char *ciphername)
{
	struct kcapi_handle_tfm *tfm = handle->tfm;
	struct sockaddr_alg sa;
	int ret;
	char versionbuffer[50];

	kcapi_versionstring(versionbuffer, sizeof(versionbuffer));
	kcapi_dolog(KCAPI_LOG_VERBOSE,
		    "%s - initializing cipher operation with kernel",
		    versionbuffer);

	tfm->tfmfd = -1;
	atomic_set(1, &tfm->refcnt);

	ret = _kcapi_get_kernver(handle);
	if (ret)
		return ret;

	memset(&sa, 0, sizeof(sa));
	sa.salg_family = AF_ALG;
	snprintf((char *)sa.salg_type, sizeof(sa.salg_type),"%s", type);
	snprintf((char *)sa.salg_name, sizeof(sa.salg_name),"%s", ciphername);

	tfm->tfmfd = socket(AF_ALG, SOCK_SEQPACKET, 0);
	if (tfm->tfmfd == -1) {
		ret = -errno;
		kcapi_dolog(KCAPI_LOG_ERR,
			    "AF_ALG: socket syscall failed (errno: %d)",
			    ret);
		return ret;
	}
	kcapi_dolog(KCAPI_LOG_DEBUG, "AF_ALG: socket syscall passed");

	if (bind(tfm->tfmfd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
		ret = -errno;
		kcapi_dolog(KCAPI_LOG_ERR, "AF_ALG: bind failed (errno: %d)",
			    ret);
		return ret;
	}
	kcapi_dolog(KCAPI_LOG_DEBUG, "AF_ALG: bind syscall passed");

	ret = _kcapi_common_getinfo(handle, ciphername);
	if (ret) {
		ret = -errno;
		kcapi_dolog(KCAPI_LOG_ERR, "NETLINK_CRYPTO: cannot obtain cipher information for %s (is required crypto_user.c patch missing? see documentation)",
			    ciphername);
		return ret;
	}

	return 0;
}

int _kcapi_handle_init(struct kcapi_handle **caller, const char *type,
		       const char *ciphername, uint32_t flags)
{
	struct kcapi_handle *handle;
	struct kcapi_handle_tfm *tfm;
	int ret;

	ret = _kcapi_handle_alloc(&handle);
	if (ret)
		return ret;

	tfm = calloc(1, sizeof(struct kcapi_handle_tfm));
	if (!tfm) {
		free(handle);
		return -ENOMEM;
	}

	handle->tfm = tfm;

	ret = _kcapi_handle_init_tfm(handle, type, ciphername);
	if (ret)
		goto err;

	ret = _kcapi_handle_init_op(handle, flags);
	if (ret)
		goto err;

	*caller = handle;

	kcapi_dolog(KCAPI_LOG_VERBOSE,
		    "communication for %s with kernel initialized",
		    ciphername);

	return 0;

err:
	_kcapi_handle_destroy(handle);
	return ret;
}

int kcapi_handle_reinit(struct kcapi_handle **newhandle,
			struct kcapi_handle *existing, uint32_t flags)
{
	struct kcapi_handle *handle;
	int ret;

	ret = _kcapi_handle_alloc(&handle);
	if (ret)
		return ret;

	if (!existing || !existing->tfm) {
		ret = -EINVAL;
		goto err;
	}

	atomic_inc(&existing->tfm->refcnt);
	handle->tfm = existing->tfm;

	ret = _kcapi_handle_init_op(handle, flags);
	if (ret)
		goto err;

	*newhandle = handle;

	kcapi_dolog(KCAPI_LOG_VERBOSE,
		    "new cipher handle from existing handle initialized");

	return 0;

err:
	_kcapi_handle_destroy(handle);
	return ret;
}

ssize_t _kcapi_cipher_crypt(struct kcapi_handle *handle, const uint8_t *in,
			    size_t inlen, uint8_t *out, size_t outlen,
			    int access, unsigned int enc)
{
	struct iovec iov;
	ssize_t ret = 0;

	if (outlen > INT_MAX)
		return -EMSGSIZE;

	/*
	 * Using two syscalls with memcpy is faster than four syscalls
	 * without memcpy below the given threshold.
	 */
	if ((access == KCAPI_ACCESS_HEURISTIC && inlen <= (1<<13)) ||
	    access == KCAPI_ACCESS_SENDMSG) {
		iov.iov_base = (void*)(uintptr_t)in;
		iov.iov_len = inlen;
		ret = _kcapi_common_send_meta(handle, &iov, 1, enc, 0);
		if (0 > ret)
			return ret;
	} else {
		ret = _kcapi_common_send_meta(handle, NULL, 0, enc, MSG_MORE);
		if (0 > ret)
			return ret;
		ret = _kcapi_common_vmsplice_chunk(handle, in, inlen, 0);
		if (0 > ret)
			return ret;
	}

	return _kcapi_common_read_data(handle, out, outlen);
}

ssize_t _kcapi_cipher_crypt_chunk(struct kcapi_handle *handle,
				  const uint8_t *in, size_t inlen,
				  uint8_t *out, size_t outlen,
				  int access, unsigned int enc)
{
	ssize_t totallen = 0;
	size_t maxprocess = handle->pagesize * ALG_MAX_PAGES;
	ssize_t ret;

	if (outlen > INT_MAX)
		return -EMSGSIZE;

	while (inlen && outlen) {
		size_t inprocess = inlen;
		size_t outprocess = outlen;

		/*
		 * We do not check that sysconf(_SC_PAGESIZE) * ALG_MAX_PAGES is
		 * a multiple of blocksize, because we assume that this is
		 * always the case.
		 */
		if (inlen > maxprocess)
			inprocess = maxprocess;
		if (outlen > maxprocess)
			outprocess = maxprocess;

		ret = _kcapi_cipher_crypt(handle, in, inprocess, out,
					  outprocess, access, enc);
		if (ret < 0)
			return ret;

		totallen += (ssize_t)inprocess;
		in += inprocess;
		inlen -= inprocess;
		out += ret;
		outlen -= (uint32_t)ret;
	}

	return totallen;
}

ssize_t _kcapi_cipher_crypt_aio(struct kcapi_handle *handle,
				struct iovec *iniov, struct iovec *outiov,
				size_t iovlen, int access, unsigned int enc)
{
	size_t i, outstanding = 0, processed = 0, inflight = 0;
	ssize_t ret;

	if (handle->aio.disable == true) {
		kcapi_dolog(KCAPI_LOG_WARN, "AIO support disabled\n");
		return -EOPNOTSUPP;
	}

	/* Every IOVEC is processed as its individual cipher operation. */
	while (iovlen) {
		size_t max_process = KCAPI_AIO_CONCURRENT - inflight;
		size_t process = (max_process < iovlen) ?
							max_process : iovlen;

		ret = _kcapi_aio_send_iov(handle, iniov, process, access, enc);
		if (ret < 0)
			return ret;

		ret = _kcapi_aio_read_iov(handle, outiov, process);
		if (ret < 0)
			return ret;

		iniov += process;
		outiov += process;
		iovlen -= process;

		inflight = 0;
		for (i = 0; i < KCAPI_AIO_CONCURRENT; i++) {
			if (handle->aio.iocb_ret[i] == AIO_OUTSTANDING) {
				inflight++;
			} else if (handle->aio.iocb_ret[i] < 0) {
				return (int32_t)handle->aio.iocb_ret[i];
			} else {
				if (handle->aio.iocb_ret[i] > INT_MAX)
					return -EOVERFLOW;

				processed += (uint32_t)handle->aio.iocb_ret[i];
				if (processed > INT_MAX)
					return -EOVERFLOW;
				handle->aio.iocb_ret[i] = 0;
			}
		}
	}

	/*
	 * If a multi-staged AIO operation shall be designed, the following
	 * loop needs to be moved to a closing API call. If done so, the
	 * current function could be invoked multiple times to send more data
	 * to the kernel before the closing call requires that all outstanding
	 * requests are to be completed.
	 *
	 * If a multi-staged AIO operation is to be implemented, the issue
	 * is that when submitting a number of requests, the caller is not
	 * able to detect which particular request is completed. Thus, an
	 * "open-ended" multi-staged AIO operation could not be implemented.
	 */
	for (i = 0; i < KCAPI_AIO_CONCURRENT; i++) {
		if (handle->aio.iocb_ret[i] == AIO_OUTSTANDING)
			outstanding++;
	}

	ret = _kcapi_aio_read_all(handle, outstanding, NULL);
	if (ret < 0)
		return ret;

	for (i = 0; i < KCAPI_AIO_CONCURRENT; i++) {
		if (handle->aio.iocb_ret[i] == AIO_OUTSTANDING) {
			return -EBADMSG;
		} else if (handle->aio.iocb_ret[i] < 0) {
			return (int32_t)handle->aio.iocb_ret[i];
		} else {
			if (handle->aio.iocb_ret[i] > INT_MAX)
				return -EOVERFLOW;

			processed += (uint32_t)handle->aio.iocb_ret[i];
			if (processed > INT_MAX)
				return -EOVERFLOW;
			handle->aio.iocb_ret[i] = 0;
		}
	}
	return (ssize_t)processed;
}