Debian Codegraph @

lib/kcapi-kernel-if.c

raw symbols SCIP
1 /* Kernel crypto API AF_ALG interface code
2 *
3 * Copyright (C) 2015 - 2022, Stephan Mueller <[email protected]>
4 *
5 * License: see COPYING file in root directory
6 *
7 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
8 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
9 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
10 * WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
11 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
12 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
13 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
14 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
15 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
16 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
17 * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
18 * DAMAGE.
19 */
20
21 #define _GNU_SOURCE
22 #include <stdarg.h>
23 #include <unistd.h>
24 #include <asm/types.h>
25 #include <linux/netlink.h>
26 #include <linux/rtnetlink.h>
27 #include <unistd.h>
28 #include <sys/user.h>
29 #include <sys/eventfd.h>
30 #include <time.h>
31 #include <sys/select.h>
32 #include <sys/utsname.h>
33 #include <sys/prctl.h>
34
35 #include "cryptouser.h"
36 #include "kcapi.h"
37 #include "internal.h"
38
39 /************************************************************
40 * Common helper used within the lib and as an API
41 ************************************************************/
42 IMPL_SYMVER(memset_secure, "1.3.1")
43 void impl_memset_secure(void *s, int c, size_t n)
44 {
45 memset(s, c, n);
46 __asm__ __volatile__("" : : "r" (s) : "memory");
47 }
48
49 ORIG_SYMVER(memset_secure, "0.12.0")
50 void orig_memset_secure(void *s, int c, uint32_t n)
51 {
52 impl_memset_secure(s, c, n);
53 }
54
55 /************************************************************
56 * Logging logic
57 ************************************************************/
58 enum kcapi_verbosity kcapi_verbosity_level = KCAPI_LOG_ERR;
59
60 void kcapi_dolog(enum kcapi_verbosity severity, const char *fmt, ...)
61 {
62 va_list args;
63 char msg[128];
64 char sev[16];
65
66 if (severity > kcapi_verbosity_level)
67 return;
68
69 memset(sev, 0, sizeof(sev));
70 memset(msg, 0, sizeof(msg));
71
72 va_start(args, fmt);
73 vsnprintf(msg, sizeof(msg) - 1, fmt, args);
74 va_end(args);
75
76 switch (severity) {
77 case KCAPI_LOG_DEBUG:
78 snprintf(sev, sizeof(sev), "Debug");
79 break;
80 case KCAPI_LOG_VERBOSE:
81 snprintf(sev, sizeof(sev), "Verbose");
82 break;
83 case KCAPI_LOG_WARN:
84 snprintf(sev, sizeof(sev), "Warning");
85 break;
86 case KCAPI_LOG_ERR:
87 snprintf(sev, sizeof(sev), "Error");
88 break;
89 default:
90 snprintf(sev, sizeof(sev), "Unknown");
91 }
92 fprintf(stderr, "libkcapi - %s: %s\n", sev, msg);
93 }
94
95 /************************************************************
96 * Internal logic
97 ************************************************************/
98
99 int _kcapi_common_accept(struct kcapi_handle *handle)
100 {
101 struct kcapi_handle_tfm *tfm = handle->tfm;
102 int fd;
103
104 if (*_kcapi_get_opfd(handle) != -1)
105 return 0;
106
107 fd = accept(tfm->tfmfd, NULL, 0);
108 if (fd == -1) {
109 int errsv;
110
111 errsv = errno;
112 kcapi_dolog(KCAPI_LOG_ERR, "AF_ALG: accept failed");
113 return -errsv;
114 }
115 kcapi_dolog(KCAPI_LOG_DEBUG, "AF_ALG: accept syscall successful");
116
117 *_kcapi_get_opfd(handle) = fd;
118
119 return 0;
120 }
121
122 #ifdef __GLIBC__
123 static inline size_t kcapi_downcast_int(size_t in)
124 {
125 return in;
126 }
127
128 static inline size_t kcapi_downcast_socklen_t(size_t in)
129 {
130 return in;
131 }
132 #else
133 static inline int kcapi_downcast_int(size_t in)
134 {
135 if (in > INT_MAX)
136 return INT_MAX;
137 return (int)in;
138 }
139
140 static inline socklen_t kcapi_downcast_socklen_t(size_t in)
141 {
142 return (socklen_t)in;
143 }
144 #endif
145
146 ssize_t _kcapi_common_send_meta(struct kcapi_handle *handle,
147 struct iovec *iov, size_t iovlen,
148 unsigned int enc, uint32_t flags)
149 {
150 struct kcapi_handle_tfm *tfm = handle->tfm;
151 ssize_t ret;
152 char buffer_static[80] = { 0 };
153 char *buffer_p = buffer_static;
154 char *buffer_alloc = NULL;
155
156 /* plaintext / ciphertext data */
157 struct cmsghdr *header = NULL;
158 uint32_t *type = NULL;
159 struct msghdr msg;
160
161 /* IV data */
162 struct af_alg_iv *alg_iv = NULL;
163 size_t iv_msg_size = handle->cipher.iv ?
164 CMSG_SPACE(sizeof(*alg_iv) + tfm->info.ivsize) :
165 0;
166
167 /* AEAD data */
168 uint32_t *assoclen = NULL;
169 size_t assoc_msg_size = handle->aead.assoclen ?
170 CMSG_SPACE(sizeof(*assoclen)) : 0;
171
172 size_t bufferlen =
173 CMSG_SPACE(sizeof(*type)) + /* Encryption / Decryption */
174 iv_msg_size + /* IV */
175 assoc_msg_size; /* AEAD associated data size */
176
177 ret = _kcapi_common_accept(handle);
178 if (ret)
179 return ret;
180
181 memset(&msg, 0, sizeof(msg));
182
183 /* allocate buffer, if static buffer is too small */
184 if (bufferlen > sizeof(buffer_static)) {
185 buffer_alloc = calloc(1, bufferlen);
186 if (!buffer_alloc)
187 return -ENOMEM;
188 buffer_p = buffer_alloc;
189 kcapi_dolog(KCAPI_LOG_VERBOSE,
190 "_kcapi_common_send_meta_fd: submission buffer of size %zu allocated",
191 bufferlen);
192 }
193
194 msg.msg_control = buffer_p;
195 msg.msg_iov = iov;
196 msg.msg_iovlen = kcapi_downcast_int(iovlen);
197 msg.msg_controllen = kcapi_downcast_socklen_t(bufferlen);
198 /* encrypt/decrypt operation */
199 header = CMSG_FIRSTHDR(&msg);
200 if (!header) {
201 ret = -EFAULT;
202 goto out;
203 }
204 header->cmsg_level = SOL_ALG;
205 header->cmsg_type = ALG_SET_OP;
206 header->cmsg_len = CMSG_LEN(sizeof(*type));
207 type = (void*)CMSG_DATA(header);
208 *type = enc;
209
210 /* set IV */
211 if (handle->cipher.iv) {
212 header = CMSG_NXTHDR(&msg, header);
213 if (!header) {
214 ret = -EFAULT;
215 goto out;
216 }
217 header->cmsg_level = SOL_ALG;
218 header->cmsg_type = ALG_SET_IV;
219 header->cmsg_len = kcapi_downcast_socklen_t(iv_msg_size);
220 alg_iv = (void*)CMSG_DATA(header);
221 alg_iv->ivlen = tfm->info.ivsize;
222 memcpy(alg_iv->iv, handle->cipher.iv, tfm->info.ivsize);
223 }
224
225 /* set AEAD information */
226 if (handle->aead.assoclen) {
227 /* Set associated data length */
228 header = CMSG_NXTHDR(&msg, header);
229 if (!header) {
230 ret = -EFAULT;
231 goto out;
232 }
233 header->cmsg_level = SOL_ALG;
234 header->cmsg_type = ALG_SET_AEAD_ASSOCLEN;
235 header->cmsg_len = CMSG_LEN(sizeof(*assoclen));
236 assoclen = (void*)CMSG_DATA(header);
237 *assoclen = (uint32_t)handle->aead.assoclen;
238 }
239
240 ret = sendmsg(*_kcapi_get_opfd(handle), &msg, (int)flags);
241 if (ret < 0)
242 ret = -errno;
243 kcapi_dolog(KCAPI_LOG_DEBUG,
244 "AF_ALG: sendmsg syscall returned %zd", ret);
245
246 out:
247 kcapi_memset_secure(buffer_p, 0, bufferlen);
248 if (buffer_alloc)
249 free(buffer_alloc);
250 return ret;
251 }
252
253 ssize_t _kcapi_common_send_data(struct kcapi_handle *handle,
254 struct iovec *iov, size_t iovlen,
255 uint32_t flags)
256 {
257 struct msghdr msg;
258 ssize_t ret;
259
260 ret = _kcapi_common_accept(handle);
261 if (ret)
262 return ret;
263
264 msg.msg_name = NULL;
265 msg.msg_namelen = 0;
266 msg.msg_control = NULL;
267 msg.msg_controllen = 0;
268 msg.msg_flags = 0;
269 msg.msg_iov = iov;
270 msg.msg_iovlen = kcapi_downcast_int(iovlen);
271 ret = sendmsg(*_kcapi_get_opfd(handle), &msg, (int)flags);
272 if (ret < 0)
273 ret = -errno;
274 kcapi_dolog(KCAPI_LOG_DEBUG, "AF_ALG: sendmsg syscall returned %zd",
275 ret);
276
277 return ret;
278 }
279
280 ssize_t _kcapi_common_vmsplice_iov(struct kcapi_handle *handle,
281 struct iovec *iov, size_t iovlen,
282 uint32_t flags)
283 {
284 ssize_t ret = 0;
285 size_t i, inlen = 0;
286
287 if (iovlen && !iov)
288 return -EINVAL;
289
290 for (i = 0; i < iovlen; i++)
291 inlen += iov[i].iov_len;
292
293 /*
294 * The kernel has the following limits for vmsplice / splice:
295 *
296 * 1. At most only 16 pages are allowed to be sent to the kernel -
297 * this is a hard limit in the vmsplice system call.
298 * 2. At most, the entire buffer size must not be larger than the
299 * buffer size of the pipe used for the splice. If desired, this
300 * buffer size can be enlarged with kcapi_set_maxsplicesize().
301 * On the other side, the maximum buffer size can be obtained via
302 * kcapi_get_maxsplicesize().
303 * 3. vmsplice cannot be used for a zero buffer.
304 *
305 * If any of the aforementioned conditions is not satisfied, the
306 * sendmsg() system call is used which copies the data into the
307 * kernel.
308 */
309 handle->processed_sg += iovlen;
310 if (handle->processed_sg > (size_t)handle->flags.alg_max_pages ||
311 !inlen ||
312 inlen > handle->pipesize) {
313 kcapi_dolog(KCAPI_LOG_DEBUG,
314 "AF_ALG: Using fallback of sendmsg instead of splice");
315 return _kcapi_common_send_data(handle, iov, iovlen,
316 (flags & SPLICE_F_MORE) ?
317 MSG_MORE : 0);
318 }
319
320 ret = _kcapi_common_accept(handle);
321 if (ret)
322 return ret;
323
324 ret = vmsplice(handle->pipes[1], iov, iovlen, SPLICE_F_GIFT|flags);
325 if (ret < 0) {
326 ret = -errno;
327 kcapi_dolog(KCAPI_LOG_DEBUG,
328 "AF_ALG: vmsplice syscall returned %zd", ret);
329 return ret;
330 }
331 kcapi_dolog(KCAPI_LOG_DEBUG, "AF_ALG: vmsplice syscall returned %zd",
332 ret);
333
334 if ((uint32_t)ret != inlen) {
335 kcapi_dolog(KCAPI_LOG_ERR, "vmsplice: not all data received by kernel (data received: %ld -- data sent: %lu)",
336 (long)ret, (unsigned long)inlen);
337 return -EFAULT;
338 }
339
340 ret = splice(handle->pipes[0], NULL, *_kcapi_get_opfd(handle), NULL,
341 (size_t)ret, flags);
342 if (ret < 0)
343 ret = -errno;
344 kcapi_dolog(KCAPI_LOG_DEBUG, "AF_ALG: splice syscall returned %zd",
345 ret);
346
347 return ret;
348 }
349
350 ssize_t _kcapi_common_vmsplice_chunk(struct kcapi_handle *handle,
351 const uint8_t *in, size_t inlen,
352 uint32_t flags)
353 {
354 struct iovec iov;
355 ssize_t processed = 0;
356 ssize_t ret = 0;
357 uint32_t sflags = (flags & SPLICE_F_MORE) ? MSG_MORE : 0;
358
359 if (inlen > INT_MAX)
360 return -EMSGSIZE;
361
362 if (!inlen)
363 return _kcapi_common_send_data(handle, NULL, 0, sflags);
364
365 ret = _kcapi_common_accept(handle);
366 if (ret)
367 return ret;
368
369 while (inlen) {
370 iov.iov_base = (void*)(uintptr_t)(in + processed);
371 iov.iov_len = inlen;
372
373 /*
374 * See comment in _kcapi_common_vmsplice_iov for the explanation
375 * of this check.
376 */
377 if ((handle->processed_sg++) > handle->flags.alg_max_pages ||
378 !inlen ||
379 inlen > handle->pipesize) {
380 kcapi_dolog(KCAPI_LOG_DEBUG,
381 "AF_ALG: Using fallback of sendmsg instead of splice");
382 ret = _kcapi_common_send_data(handle, &iov, 1, sflags);
383 if (ret < 0)
384 return ret;
385 } else {
386 ret = vmsplice(handle->pipes[1], &iov, 1,
387 SPLICE_F_GIFT|flags);
388 if (ret < 0) {
389 ret = -errno;
390 kcapi_dolog(KCAPI_LOG_DEBUG,
391 "AF_ALG: vmsplice syscall returned %zd",
392 ret);
393 return ret;
394 }
395 kcapi_dolog(KCAPI_LOG_DEBUG,
396 "AF_ALG: vmsplice syscall returned %zd",
397 ret);
398
399 ret = splice(handle->pipes[0], NULL,
400 *_kcapi_get_opfd(handle), NULL,
401 (size_t)ret, flags);
402 if (ret < 0) {
403 ret = -errno;
404 kcapi_dolog(KCAPI_LOG_DEBUG,
405 "AF_ALG: splice syscall returned %zd",
406 ret);
407 return ret;
408 }
409 kcapi_dolog(KCAPI_LOG_DEBUG,
410 "AF_ALG: splice syscall returned %zd", ret);
411 }
412
413 processed += ret;
414 inlen -= (uint32_t)ret;
415 }
416
417 return processed;
418 }
419
420 /* Wrapper for io_getevents -- returns < 0 on error, or processed bytes */
421 int _kcapi_aio_read_all(struct kcapi_handle *handle, size_t toread,
422 struct timespec *timeout)
423 {
424 if (toread > KCAPI_AIO_CONCURRENT)
425 return -EINVAL;
426
427 while (toread) {
428 int i;
429 struct io_event events[KCAPI_AIO_CONCURRENT];
430 int rc = io_getevents(handle->aio.aio_ctx, 1, (long)toread,
431 events, timeout);
432
433 if (rc < 0)
434 return rc;
435
436 for (i = 0; i < rc; i++) {
437 struct iocb *cb;
438
439 /*
440 * If one cipher operation fails, so will the entire
441 * AIO operation
442 */
443 if (events[i].res < 0) {
444 handle->aio.iocb_ret[events[i].data] =
445 events[i].res;
446 return (int)events[i].res;
447 }
448
449 cb = (struct iocb *)(uintptr_t)events[i].obj;
450
451 /*
452 * Older symmetric AIO implementations used a wrong
453 * return code.
454 */
455 if (events[i].res > 0) {
456 handle->aio.iocb_ret[events[i].data] =
457 events[i].res;
458 } else {
459 handle->aio.iocb_ret[events[i].data] =
460 (__s64)cb->aio_nbytes;
461 }
462
463 cb->aio_fildes = 0;
464 }
465 toread -= (uint32_t)rc;
466 }
467
468 return 0;
469 }
470
471 int _kcapi_aio_send_iov(struct kcapi_handle *handle, struct iovec *iov,
472 size_t iovlen, int access, unsigned int enc)
473 {
474 ssize_t ret;
475 size_t len = iov ? iov->iov_len : 0;
476
477 /*
478 * Using two syscalls with memcpy is faster than four syscalls
479 * without memcpy below the given threshold.
480 */
481 if ((access == KCAPI_ACCESS_HEURISTIC && len <= (1<<13)) ||
482 access == KCAPI_ACCESS_SENDMSG) {
483 ret = _kcapi_common_send_meta(handle, iov, iovlen, enc, 0);
484 if (0 > ret)
485 return (int)ret;
486 } else {
487 ret = _kcapi_common_send_meta(handle, NULL, 0, enc, MSG_MORE);
488 if (0 > ret)
489 return (int)ret;
490 ret = _kcapi_common_vmsplice_iov(handle, iov, iovlen, 0);
491 if (0 > ret)
492 return (int)ret;
493 }
494
495 return 0;
496 }
497
498 int _kcapi_aio_read_iov(struct kcapi_handle *handle,
499 struct iovec *iov, size_t iovlen)
500 {
501 struct iocb *cb = handle->aio.cio;
502 size_t i;
503 int32_t ret;
504
505 if (iovlen > KCAPI_AIO_CONCURRENT)
506 return -EFAULT;
507
508 for (i = 0; i < iovlen; i++) {
509 while (cb->aio_fildes) {
510 struct timespec timeout;
511
512 timeout.tv_sec = 0;
513 timeout.tv_nsec = 10000;
514 ret = _kcapi_aio_read_all(handle, iovlen, &timeout);
515 if (ret < 0)
516 return ret;
517 }
518
519 memset(cb, 0, sizeof(*cb));
520 cb->aio_fildes = (__u32)*_kcapi_get_opfd(handle);
521 cb->aio_lio_opcode = IOCB_CMD_PREAD;
522 cb->aio_buf = (unsigned long)iov->iov_base;
523 cb->aio_offset = 0;
524 cb->aio_data = i;
525 cb->aio_nbytes = iov->iov_len;
526 cb->aio_flags = IOCB_FLAG_RESFD;
527 cb->aio_resfd = (__u32)handle->aio.efd;
528
529 handle->aio.iocb_ret[i] = AIO_OUTSTANDING;
530
531 cb++;
532 iov++;
533 }
534
535 ret = io_submit(handle->aio.aio_ctx, (long)iovlen, handle->aio.ciopp);
536 if ((uint32_t)ret != iovlen) {
537 if (ret < 0) {
538 ret = -errno;
539 kcapi_dolog(KCAPI_LOG_ERR, "io_read Error: %d\n", ret);
540 return ret;
541 } else {
542 kcapi_dolog(KCAPI_LOG_ERR,
543 "Could not sumbit AIO read\n");
544 return -EIO;
545 }
546 }
547
548 return _kcapi_aio_read_all(handle, iovlen, NULL);
549 }
550
551 ssize_t _kcapi_common_recv_data(struct kcapi_handle *handle,
552 struct iovec *iov, size_t iovlen)
553 {
554 struct msghdr msg;
555 ssize_t ret;
556
557 ret = _kcapi_common_accept(handle);
558 if (ret)
559 return ret;
560
561 msg.msg_name = NULL;
562 msg.msg_namelen = 0;
563 msg.msg_control = NULL;
564 msg.msg_controllen = 0;
565 msg.msg_flags = 0;
566 msg.msg_iov = iov;
567 #ifdef __GLIBC__
568 msg.msg_iovlen = iovlen;
569 #else
570 msg.msg_iovlen = (int)iovlen;
571 #endif
572 ret = recvmsg(*_kcapi_get_opfd(handle), &msg, 0);
573 if (ret < 0)
574 ret = -errno;
575 kcapi_dolog(KCAPI_LOG_DEBUG, "AF_ALG: recvmsg syscall returned %zd",
576 ret);
577
578 /*
579 * As the iovecs are processed and removed from the list in the kernel
580 * we can also reset the list of processed iovecs here.
581 *
582 * Note, if there is an error, the kernel keeps the list unless it is
583 * a "valid" error of EBADMSG indicating an integrity error of the
584 * crypto operation.
585 */
586 if (ret >= 0 || ret == -EBADMSG)
587 handle->processed_sg = 0;
588
589 #if 0
590 /*
591 * Truncated message digests can be identified with this check.
592 */
593 if (msg.msg_flags & MSG_TRUNC) {
594 fprintf(stderr, "recvmsg: processed data was truncated by kernel (only %lu bytes processed)\n", (unsigned long)ret);
595 return -EMSGSIZE;
596 }
597 #endif
598
599 return ret;
600 }
601
602 ssize_t _kcapi_common_read_data(struct kcapi_handle *handle,
603 uint8_t *out, size_t outlen)
604 {
605 ssize_t ret;
606 ssize_t totallen = 0;
607
608 if (outlen > INT_MAX)
609 return -EMSGSIZE;
610
611 ret = _kcapi_common_accept(handle);
612 if (ret)
613 return ret;
614
615
616 if (outlen) {
617 do {
618 ret = read(*_kcapi_get_opfd(handle), out, outlen);
619 if (ret > 0) {
620 out += ret;
621 outlen -= (uint32_t)ret;
622 totallen += ret;
623 }
624 kcapi_dolog(KCAPI_LOG_DEBUG,
625 "AF_ALG: read syscall returned %zd", ret);
626 } while ((ret > 0 || errno == EINTR) && outlen);
627
628 if (ret < 0)
629 return -errno;
630 }
631
632 return totallen;
633 }
634
635 int _kcapi_common_setkey(struct kcapi_handle *handle,
636 const uint8_t *key, uint32_t keylen)
637 {
638 struct kcapi_handle_tfm *tfm = handle->tfm;
639 int ret;
640
641 ret = setsockopt(tfm->tfmfd, SOL_ALG, ALG_SET_KEY, key, keylen);
642 if (ret < 0)
643 ret = -errno;
644 kcapi_dolog(KCAPI_LOG_DEBUG,
645 "AF_ALG setkey: setsockopt syscall returned %d", ret);
646
647 return ret;
648 }
649
650 int _kcapi_common_setentropy(struct kcapi_handle *handle,
651 const uint8_t *ent, uint32_t entlen)
652 {
653 struct kcapi_handle_tfm *tfm = handle->tfm;
654 int ret;
655
656 ret = setsockopt(tfm->tfmfd, SOL_ALG, ALG_SET_DRBG_ENTROPY, ent,
657 entlen);
658 if (ret < 0)
659 ret = -errno;
660 kcapi_dolog(KCAPI_LOG_DEBUG,
661 "AF_ALG setentropy: setsockopt syscall returned %d", ret);
662
663 return ret;
664 }
665
666 static int __kcapi_common_getinfo(struct kcapi_handle *handle,
667 const char *ciphername,
668 int drivername)
669 {
670 struct kcapi_handle_tfm *tfm = handle->tfm;
671 int ret = -EFAULT;
672
673 /* NETLINK_CRYPTO specific */
674 char buf[4096];
675 struct nlmsghdr *res_n = (struct nlmsghdr *)buf;
676 struct {
677 struct nlmsghdr n;
678 struct crypto_user_alg cru;
679 } req;
680 struct crypto_user_alg *cru_res = NULL;
681 unsigned long res_len = 0;
682 struct rtattr *tb[CRYPTOCFGA_MAX+1];
683 struct rtattr *rta;
684
685 /* AF_NETLINK specific */
686 struct sockaddr_nl nl;
687 int sd = 0;
688 socklen_t addr_len;
689 struct iovec iov;
690 struct msghdr msg;
691
692 memset(&req, 0, sizeof(req));
693 memset(&buf, 0, sizeof(buf));
694 memset(&msg, 0, sizeof(msg));
695
696 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.cru));
697 req.n.nlmsg_flags = NLM_F_REQUEST;
698 req.n.nlmsg_type = CRYPTO_MSG_GETALG;
699 req.n.nlmsg_seq = (__u32)time(NULL);
700
701 if (drivername)
702 strncpy(req.cru.cru_driver_name, ciphername,
703 sizeof(req.cru.cru_driver_name) - 1);
704 else
705 strncpy(req.cru.cru_name, ciphername, sizeof(req.cru.cru_name) - 1);
706
707 /* talk to netlink socket */
708 sd = socket(AF_NETLINK, SOCK_RAW, NETLINK_CRYPTO);
709 if (sd < 0) {
710 kcapi_dolog(KCAPI_LOG_ERR,
711 "Netlink error: cannot open netlink socket");
712 return -errno;
713 }
714 memset(&nl, 0, sizeof(nl));
715 nl.nl_family = AF_NETLINK;
716 if (bind(sd, (struct sockaddr*)&nl, sizeof(nl)) < 0) {
717 ret = -errno;
718 kcapi_dolog(KCAPI_LOG_ERR,
719 "Netlink error: cannot bind netlink socket");
720 goto out;
721 }
722 /* sanity check that netlink socket was successfully opened */
723 addr_len = sizeof(nl);
724 if (getsockname(sd, (struct sockaddr*)&nl, &addr_len) < 0) {
725 ret = -errno;
726 kcapi_dolog(KCAPI_LOG_ERR, "Netlink error: cannot getsockname");
727 goto out;
728 }
729 if (addr_len != sizeof(nl)) {
730 ret = -errno;
731 kcapi_dolog(KCAPI_LOG_ERR,
732 "Netlink error: wrong address length %d", addr_len);
733 goto out;
734 }
735 if (nl.nl_family != AF_NETLINK) {
736 ret = -errno;
737 kcapi_dolog(KCAPI_LOG_ERR,
738 "Netlink error: wrong address family %d",
739 nl.nl_family);
740 goto out;
741 }
742
743 /* sending data */
744 memset(&nl, 0, sizeof(nl));
745 nl.nl_family = AF_NETLINK;
746 iov.iov_base = (void*) &req.n;
747 iov.iov_len = req.n.nlmsg_len;
748 msg.msg_name = &nl;
749 msg.msg_namelen = sizeof(nl);
750 msg.msg_iov = &iov;
751 msg.msg_iovlen = 1;
752 if (sendmsg(sd, &msg, 0) < 0) {
753 ret = -errno;
754 kcapi_dolog(KCAPI_LOG_ERR, "Netlink error: sendmsg failed");
755 goto out;
756 }
757 memset(buf,0,sizeof(buf));
758 iov.iov_base = buf;
759 while (1) {
760 ssize_t rc;
761 iov.iov_len = sizeof(buf);
762 rc = recvmsg(sd, &msg, 0);
763 if (rc < 0) {
764 if (errno == EINTR || errno == EAGAIN)
765 continue;
766 ret = -errno;
767 kcapi_dolog(KCAPI_LOG_ERR,
768 "Netlink error: netlink receive error");
769 goto out;
770 }
771 if (rc == 0) {
772 ret = -errno;
773 kcapi_dolog(KCAPI_LOG_ERR, "Netlink error: no data");
774 goto out;
775 }
776 if (rc > (ssize_t)sizeof(buf)) {
777 ret = -errno;
778 kcapi_dolog(KCAPI_LOG_ERR,
779 "Netlink error: received too much data");
780 goto out;
781 }
782 break;
783 }
784
785 ret = -EFAULT;
786 res_len = res_n->nlmsg_len;
787 if (res_n->nlmsg_type == NLMSG_ERROR) {
788 /*
789 * return -EAGAIN -- this error will occur if we received a
790 * driver name, but used it for a generic name. Allow caller
791 * to invoke function again where driver name is looked up
792 */
793 ret = -EAGAIN;
794 goto out;
795 }
796
797 if (res_n->nlmsg_type == CRYPTO_MSG_GETALG) {
798 cru_res = NLMSG_DATA(res_n);
799 if (res_len < NLMSG_SPACE(sizeof(*cru_res))) {
800 kcapi_dolog(KCAPI_LOG_ERR, "Netlink error: nlmsg len %lu",
801 res_len);
802 goto out;
803 }
804 res_len -= NLMSG_SPACE(sizeof(*cru_res));
805 }
806
807 /* parse data */
808 if (!cru_res) {
809 ret = -EFAULT;
810 goto out;
811 }
812 rta = CR_RTA(cru_res);
813 memset(tb, 0, sizeof(struct rtattr *) * (CRYPTOCFGA_MAX + 1));
814 while (RTA_OK(rta, res_len)) {
815 if ((rta->rta_type <= CRYPTOCFGA_MAX) && (!tb[rta->rta_type]))
816 tb[rta->rta_type] = rta;
817 rta = RTA_NEXT(rta, res_len);
818 }
819 if (res_len) {
820 kcapi_dolog(KCAPI_LOG_ERR, "Netlink error: unprocessed data %lu",
821 res_len);
822 goto out;
823 }
824
825 if (tb[CRYPTOCFGA_REPORT_HASH]) {
826 struct rtattr *rta = tb[CRYPTOCFGA_REPORT_HASH];
827 struct crypto_report_hash *rsh =
828 (struct crypto_report_hash *) RTA_DATA(rta);
829 tfm->info.hash_digestsize = rsh->digestsize;
830 tfm->info.blocksize = rsh->blocksize;
831 kcapi_dolog(KCAPI_LOG_DEBUG,
832 "Get cipher info: hash with digestsize %u, blocksize %u",
833 rsh->digestsize, rsh->blocksize);
834
835 tfm->info.cipher_type = KCAPI_CIPHER_MD;
836 }
837 if (tb[CRYPTOCFGA_REPORT_BLKCIPHER]) {
838 struct rtattr *rta = tb[CRYPTOCFGA_REPORT_BLKCIPHER];
839 struct crypto_report_blkcipher *rblk =
840 (struct crypto_report_blkcipher *) RTA_DATA(rta);
841 tfm->info.blocksize = rblk->blocksize;
842 tfm->info.ivsize = rblk->ivsize;
843 tfm->info.blk_min_keysize = rblk->min_keysize;
844 tfm->info.blk_max_keysize = rblk->max_keysize;
845 kcapi_dolog(KCAPI_LOG_DEBUG, "Get cipher info: block cipher with blocksize %u, ivsize %u, minimum keysize %u, maximum keysize %u",
846 rblk->blocksize, rblk->ivsize, rblk->min_keysize,
847 rblk->max_keysize);
848
849 tfm->info.cipher_type = KCAPI_CIPHER_SKCIPHER;
850 }
851 if (tb[CRYPTOCFGA_REPORT_AEAD]) {
852 struct rtattr *rta = tb[CRYPTOCFGA_REPORT_AEAD];
853 struct crypto_report_aead *raead =
854 (struct crypto_report_aead *) RTA_DATA(rta);
855 tfm->info.blocksize = raead->blocksize;
856 tfm->info.ivsize = raead->ivsize;
857 tfm->info.aead_maxauthsize = raead->maxauthsize;
858 kcapi_dolog(KCAPI_LOG_DEBUG, "Get cipher info: AEAD block cipher with blocksize %u, ivsize %u, maximum authentication size %u",
859 raead->blocksize, raead->ivsize,
860 raead->maxauthsize);
861
862 tfm->info.cipher_type = KCAPI_CIPHER_AEAD;
863 }
864 if (tb[CRYPTOCFGA_REPORT_RNG]) {
865 struct rtattr *rta = tb[CRYPTOCFGA_REPORT_RNG];
866 struct crypto_report_rng *rrng =
867 (struct crypto_report_rng *) RTA_DATA(rta);
868 tfm->info.rng_seedsize = rrng->seedsize;
869 kcapi_dolog(KCAPI_LOG_DEBUG,
870 "Get cipher info: RNG cipher with seedsize %u",
871 rrng->seedsize);
872
873 tfm->info.cipher_type = KCAPI_CIPHER_RNG;
874 }
875 if (tb[CRYPTOCFGA_UNSPEC])
876 kcapi_dolog(KCAPI_LOG_DEBUG,
877 "Get cipher info: unspecified data received");
878 if (tb[CRYPTOCFGA_PRIORITY_VAL])
879 kcapi_dolog(KCAPI_LOG_DEBUG,
880 "Get cipher info: u32 value received");
881 if (tb[CRYPTOCFGA_REPORT_LARVAL])
882 kcapi_dolog(KCAPI_LOG_DEBUG,
883 "Get cipher info: larval value received");
884 if (tb[CRYPTOCFGA_REPORT_COMPRESS])
885 kcapi_dolog(KCAPI_LOG_DEBUG,
886 "Get cipher info: compression algorithm type received");
887 if (tb[CRYPTOCFGA_REPORT_CIPHER])
888 kcapi_dolog(KCAPI_LOG_DEBUG,
889 "Get cipher info: simple cipher algorithm type received");
890 if (tb[CRYPTOCFGA_REPORT_AKCIPHER]) {
891 kcapi_dolog(KCAPI_LOG_DEBUG,
892 "Get cipher info: asymmetric cipher algorithm type received");
893
894 tfm->info.cipher_type = KCAPI_CIPHER_AKCIPHER;
895 }
896 if (tb[CRYPTOCFGA_REPORT_KPP]) {
897 kcapi_dolog(KCAPI_LOG_DEBUG,
898 "Get cipher info: kpp cipher algorithm type received");
899
900 tfm->info.cipher_type = KCAPI_CIPHER_KPP;
901 }
902 if (tb[CRYPTOCFGA_REPORT_ACOMP])
903 kcapi_dolog(KCAPI_LOG_DEBUG,
904 "Get cipher info: asymmetric compression algorithm type received");
905 kcapi_dolog(KCAPI_LOG_VERBOSE,
906 "Get cipher info: all information for %s received from kernel",
907 ciphername);
908
909 ret = 0;
910
911 out:
912 close(sd);
913 return ret;
914 }
915
916 static int _kcapi_common_getinfo(struct kcapi_handle *handle,
917 const char *ciphername)
918 {
919 int ret = __kcapi_common_getinfo(handle, ciphername, 0);
920 if (ret)
921 return __kcapi_common_getinfo(handle, ciphername, 1);
922 return 0;
923 }
924
925 static inline void _kcapi_aio_destroy(struct kcapi_handle *handle)
926 {
927 if (handle->aio.efd != -1)
928 close(handle->aio.efd);
929 handle->aio.efd = -1;
930 if (handle->aio.aio_ctx)
931 io_destroy(handle->aio.aio_ctx);
932 handle->aio.aio_ctx = 0;
933 if (handle->aio.cio)
934 free(handle->aio.cio);
935 handle->aio.cio = NULL;
936 if (handle->aio.ciopp)
937 free(handle->aio.ciopp);
938 handle->aio.ciopp = NULL;
939 if (handle->aio.iocb_ret)
940 free(handle->aio.iocb_ret);
941 handle->aio.iocb_ret = NULL;
942 }
943
944 static void _kcapi_handle_destroy_tfm(struct kcapi_handle *handle)
945 {
946 struct kcapi_handle_tfm *tfm;
947 if (!handle || !handle->tfm)
948 return;
949
950 tfm = handle->tfm;
951 if (atomic_dec_and_test(&tfm->refcnt)) {
952 if (tfm->tfmfd != -1)
953 close(tfm->tfmfd);
954 kcapi_memset_secure(tfm, 0, sizeof(*tfm));
955 free(tfm);
956 handle->tfm = NULL;
957 }
958 }
959
960 void _kcapi_handle_destroy(struct kcapi_handle *handle)
961 {
962 if (!handle)
963 return;
964 if (*_kcapi_get_opfd(handle) != -1)
965 close(*_kcapi_get_opfd(handle));
966 if (handle->pipes[0] != -1)
967 close(handle->pipes[0]);
968 if (handle->pipes[1] != -1)
969 close(handle->pipes[1]);
970 _kcapi_aio_destroy(handle);
971 _kcapi_handle_destroy_tfm(handle);
972 kcapi_memset_secure(handle, 0, sizeof(struct kcapi_handle));
973 free(handle);
974 }
975
976 static int _kcapi_get_kernver(struct kcapi_handle *handle)
977 {
978 struct kcapi_handle_tfm *tfm = handle->tfm;
979 struct utsname kernel;
980 char *saveptr = NULL;
981 char *res = NULL;
982
983 if (uname(&kernel))
984 return -errno;
985
986 /* 3.15.0 */
987 res = strtok_r(kernel.release, ".", &saveptr);
988 if (!res) {
989 printf("Could not parse kernel version");
990 return -EFAULT;
991 }
992 tfm->sysinfo.kernel_maj = strtoul(res, NULL, 10);
993 res = strtok_r(NULL, ".", &saveptr);
994 if (!res) {
995 printf("Could not parse kernel version");
996 return -EFAULT;
997 }
998 tfm->sysinfo.kernel_minor = strtoul(res, NULL, 10);
999 res = strtok_r(NULL, ".", &saveptr);
1000 if (!res) {
1001 printf("Could not parse kernel version");
1002 return -EFAULT;
1003 }
1004 tfm->sysinfo.kernel_patchlevel = strtoul(res, NULL, 10);
1005
1006 return 0;
1007 }
1008
1009 /* return true if kernel is greater or equal to given values, otherwise false */
1010 static bool _kcapi_kernver_ge(struct kcapi_handle *handle, unsigned int maj,
1011 unsigned int minor, unsigned int patchlevel)
1012 {
1013 struct kcapi_handle_tfm *tfm = handle->tfm;
1014
1015 if (maj < tfm->sysinfo.kernel_maj)
1016 return true;
1017 if (maj == tfm->sysinfo.kernel_maj) {
1018 if (minor < tfm->sysinfo.kernel_minor)
1019 return true;
1020 if (minor == tfm->sysinfo.kernel_minor) {
1021 if (patchlevel <= tfm->sysinfo.kernel_patchlevel)
1022 return true;
1023 }
1024 }
1025 return false;
1026 }
1027
1028 static int _kcapi_aio_init(struct kcapi_handle *handle)
1029 {
1030 struct kcapi_handle_tfm *tfm = handle->tfm;
1031 uint32_t i;
1032 int err;
1033
1034 if (tfm->info.cipher_type == KCAPI_CIPHER_AEAD) {
1035 if (!_kcapi_kernver_ge(handle, 4, 7, 0)) {
1036 kcapi_dolog(KCAPI_LOG_VERBOSE, "AIO support for AEAD cipher not present on current kernel");
1037 err = -EOPNOTSUPP;
1038 goto err;
1039 }
1040 }
1041 if (tfm->info.cipher_type == KCAPI_CIPHER_SKCIPHER) {
1042 if (!_kcapi_kernver_ge(handle, 4, 1, 0)) {
1043 kcapi_dolog(KCAPI_LOG_VERBOSE, "AIO support for symmetric ciphers not present on current kernel");
1044 err = -EOPNOTSUPP;
1045 goto err;
1046 }
1047 }
1048
1049 handle->aio.cio = calloc(KCAPI_AIO_CONCURRENT, sizeof(struct iocb));
1050 if (!handle->aio.cio) {
1051 err = -ENOMEM;
1052 goto err;
1053 }
1054
1055 handle->aio.ciopp = calloc(KCAPI_AIO_CONCURRENT, sizeof(void *));
1056 if (!handle->aio.ciopp) {
1057 err = -ENOMEM;
1058 goto err;
1059 }
1060
1061 handle->aio.iocb_ret = calloc(KCAPI_AIO_CONCURRENT, sizeof(__s64));
1062 if (!handle->aio.iocb_ret) {
1063 err = -ENOMEM;
1064 goto err;
1065 }
1066
1067 /*
1068 * Set up the pointers to pointers array that is required by
1069 * io_submit. Please do not ask me why the kernel wants this. :-)
1070 */
1071 for (i = 0; i < KCAPI_AIO_CONCURRENT; i++)
1072 *(handle->aio.ciopp + i) = handle->aio.cio + i;
1073
1074 handle->aio.efd = eventfd(0, EFD_CLOEXEC);
1075 if (handle->aio.efd < 0) {
1076 err = -errno;
1077 kcapi_dolog(KCAPI_LOG_ERR, "Event FD cannot be initialized: %d\n",
1078 err);
1079 goto err;
1080 }
1081
1082 err = io_setup(KCAPI_AIO_CONCURRENT, &handle->aio.aio_ctx);
1083 if (err < 0) {
1084 err = -errno;
1085 kcapi_dolog(KCAPI_LOG_ERR, "io_setup error %d\n", err);
1086 goto err;
1087 }
1088
1089 handle->aio.disable = false;
1090 kcapi_dolog(KCAPI_LOG_VERBOSE, "asynchronous I/O initialized");
1091
1092 return 0;
1093
1094 err:
1095 _kcapi_aio_destroy(handle);
1096
1097 return err;
1098 }
1099
1100 static void _kcapi_handle_flags(struct kcapi_handle *handle)
1101 {
1102 /* new memory structure for AF_ALG AEAD interface + fixed hashing empty input */
1103 handle->flags.ge_v4_9 = _kcapi_kernver_ge(handle, 4, 9, 0);
1104
1105 /* older interfaces only processed 16 pages in a row */
1106 handle->flags.alg_max_pages = ALG_MAX_PAGES;
1107 }
1108
1109 static int _kcapi_handle_alloc(struct kcapi_handle **caller)
1110 {
1111 struct kcapi_handle *handle = calloc(1, sizeof(struct kcapi_handle));
1112
1113 if (!handle)
1114 return -ENOMEM;
1115
1116 *_kcapi_get_opfd(handle) = -1;
1117 handle->pipes[0] = -1;
1118 handle->pipes[1] = -1;
1119 handle->aio.efd = -1;
1120 handle->pagesize = (unsigned int)sysconf(_SC_PAGESIZE);
1121
1122 *caller = handle;
1123
1124 return 0;
1125 }
1126
1127 static int _kcapi_handle_init_op(struct kcapi_handle *handle, uint32_t flags)
1128 {
1129 int ret;
1130
1131 ret = pipe(handle->pipes);
1132 if (ret) {
1133 ret = -errno;
1134 kcapi_dolog(KCAPI_LOG_ERR,
1135 "AF_ALG: pipe syscall failed (errno: %d)",
1136 ret);
1137 return ret;
1138 }
1139
1140 /*
1141 * For vmsplice to allow the maximum number of 16 pages, we need to
1142 * increase the pipe buffer by one more page - it seems the kernel
1143 * uses some parts of the pipe for some house-keeping?!
1144 */
1145 ret = kcapi_set_maxsplicesize(handle,
1146 handle->pagesize * (ALG_MAX_PAGES + 1));
1147 if (ret < 0) {
1148 kcapi_dolog(KCAPI_LOG_WARN,
1149 "AF_ALG: setting maximum buffer size failed: %d)",
1150 ret);
1151 ret = kcapi_get_maxsplicesize(handle);
1152 if (ret < 0)
1153 return ret;
1154
1155 }
1156 ret = 0;
1157 kcapi_dolog(KCAPI_LOG_DEBUG, "AF_ALG: pipe syscall passed");
1158
1159 if (flags & KCAPI_INIT_AIO) {
1160 ret = _kcapi_aio_init(handle);
1161
1162 /*
1163 * We complain about kernels without AIO support, but allow
1164 * the allocation nonetheless as we have synchronous fallbacks
1165 * for all AIO implementations. This allows the use of
1166 * libkcapi AIO API even though the kernel does not support it.
1167 * This allows compatibility for applications.
1168 */
1169 if (ret && ret != -EOPNOTSUPP)
1170 return ret;
1171 ret = 0;
1172 } else
1173 handle->aio.disable = true;
1174
1175 _kcapi_handle_flags(handle);
1176
1177 return ret;
1178 }
1179
1180 static int _kcapi_handle_init_tfm(struct kcapi_handle *handle, const char *type,
1181 const char *ciphername)
1182 {
1183 struct kcapi_handle_tfm *tfm = handle->tfm;
1184 struct sockaddr_alg sa;
1185 int ret;
1186 char versionbuffer[50];
1187
1188 kcapi_versionstring(versionbuffer, sizeof(versionbuffer));
1189 kcapi_dolog(KCAPI_LOG_VERBOSE,
1190 "%s - initializing cipher operation with kernel",
1191 versionbuffer);
1192
1193 tfm->tfmfd = -1;
1194 atomic_set(1, &tfm->refcnt);
1195
1196 ret = _kcapi_get_kernver(handle);
1197 if (ret)
1198 return ret;
1199
1200 memset(&sa, 0, sizeof(sa));
1201 sa.salg_family = AF_ALG;
1202 snprintf((char *)sa.salg_type, sizeof(sa.salg_type),"%s", type);
1203 snprintf((char *)sa.salg_name, sizeof(sa.salg_name),"%s", ciphername);
1204
1205 tfm->tfmfd = socket(AF_ALG, SOCK_SEQPACKET, 0);
1206 if (tfm->tfmfd == -1) {
1207 ret = -errno;
1208 kcapi_dolog(KCAPI_LOG_ERR,
1209 "AF_ALG: socket syscall failed (errno: %d)",
1210 ret);
1211 return ret;
1212 }
1213 kcapi_dolog(KCAPI_LOG_DEBUG, "AF_ALG: socket syscall passed");
1214
1215 if (bind(tfm->tfmfd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
1216 ret = -errno;
1217 kcapi_dolog(KCAPI_LOG_ERR, "AF_ALG: bind failed (errno: %d)",
1218 ret);
1219 return ret;
1220 }
1221 kcapi_dolog(KCAPI_LOG_DEBUG, "AF_ALG: bind syscall passed");
1222
1223 ret = _kcapi_common_getinfo(handle, ciphername);
1224 if (ret) {
1225 ret = -errno;
1226 kcapi_dolog(KCAPI_LOG_ERR, "NETLINK_CRYPTO: cannot obtain cipher information for %s (is required crypto_user.c patch missing? see documentation)",
1227 ciphername);
1228 return ret;
1229 }
1230
1231 return 0;
1232 }
1233
1234 int _kcapi_handle_init(struct kcapi_handle **caller, const char *type,
1235 const char *ciphername, uint32_t flags)
1236 {
1237 struct kcapi_handle *handle;
1238 struct kcapi_handle_tfm *tfm;
1239 int ret;
1240
1241 ret = _kcapi_handle_alloc(&handle);
1242 if (ret)
1243 return ret;
1244
1245 tfm = calloc(1, sizeof(struct kcapi_handle_tfm));
1246 if (!tfm) {
1247 free(handle);
1248 return -ENOMEM;
1249 }
1250
1251 handle->tfm = tfm;
1252
1253 ret = _kcapi_handle_init_tfm(handle, type, ciphername);
1254 if (ret)
1255 goto err;
1256
1257 ret = _kcapi_handle_init_op(handle, flags);
1258 if (ret)
1259 goto err;
1260
1261 *caller = handle;
1262
1263 kcapi_dolog(KCAPI_LOG_VERBOSE,
1264 "communication for %s with kernel initialized",
1265 ciphername);
1266
1267 return 0;
1268
1269 err:
1270 _kcapi_handle_destroy(handle);
1271 return ret;
1272 }
1273
1274 int kcapi_handle_reinit(struct kcapi_handle **newhandle,
1275 struct kcapi_handle *existing, uint32_t flags)
1276 {
1277 struct kcapi_handle *handle;
1278 int ret;
1279
1280 ret = _kcapi_handle_alloc(&handle);
1281 if (ret)
1282 return ret;
1283
1284 if (!existing || !existing->tfm) {
1285 ret = -EINVAL;
1286 goto err;
1287 }
1288
1289 atomic_inc(&existing->tfm->refcnt);
1290 handle->tfm = existing->tfm;
1291
1292 ret = _kcapi_handle_init_op(handle, flags);
1293 if (ret)
1294 goto err;
1295
1296 *newhandle = handle;
1297
1298 kcapi_dolog(KCAPI_LOG_VERBOSE,
1299 "new cipher handle from existing handle initialized");
1300
1301 return 0;
1302
1303 err:
1304 _kcapi_handle_destroy(handle);
1305 return ret;
1306 }
1307
1308 ssize_t _kcapi_cipher_crypt(struct kcapi_handle *handle, const uint8_t *in,
1309 size_t inlen, uint8_t *out, size_t outlen,
1310 int access, unsigned int enc)
1311 {
1312 struct iovec iov;
1313 ssize_t ret = 0;
1314
1315 if (outlen > INT_MAX)
1316 return -EMSGSIZE;
1317
1318 /*
1319 * Using two syscalls with memcpy is faster than four syscalls
1320 * without memcpy below the given threshold.
1321 */
1322 if ((access == KCAPI_ACCESS_HEURISTIC && inlen <= (1<<13)) ||
1323 access == KCAPI_ACCESS_SENDMSG) {
1324 iov.iov_base = (void*)(uintptr_t)in;
1325 iov.iov_len = inlen;
1326 ret = _kcapi_common_send_meta(handle, &iov, 1, enc, 0);
1327 if (0 > ret)
1328 return ret;
1329 } else {
1330 ret = _kcapi_common_send_meta(handle, NULL, 0, enc, MSG_MORE);
1331 if (0 > ret)
1332 return ret;
1333 ret = _kcapi_common_vmsplice_chunk(handle, in, inlen, 0);
1334 if (0 > ret)
1335 return ret;
1336 }
1337
1338 return _kcapi_common_read_data(handle, out, outlen);
1339 }
1340
1341 ssize_t _kcapi_cipher_crypt_chunk(struct kcapi_handle *handle,
1342 const uint8_t *in, size_t inlen,
1343 uint8_t *out, size_t outlen,
1344 int access, unsigned int enc)
1345 {
1346 ssize_t totallen = 0;
1347 size_t maxprocess = handle->pagesize * ALG_MAX_PAGES;
1348 ssize_t ret;
1349
1350 if (outlen > INT_MAX)
1351 return -EMSGSIZE;
1352
1353 while (inlen && outlen) {
1354 size_t inprocess = inlen;
1355 size_t outprocess = outlen;
1356
1357 /*
1358 * We do not check that sysconf(_SC_PAGESIZE) * ALG_MAX_PAGES is
1359 * a multiple of blocksize, because we assume that this is
1360 * always the case.
1361 */
1362 if (inlen > maxprocess)
1363 inprocess = maxprocess;
1364 if (outlen > maxprocess)
1365 outprocess = maxprocess;
1366
1367 ret = _kcapi_cipher_crypt(handle, in, inprocess, out,
1368 outprocess, access, enc);
1369 if (ret < 0)
1370 return ret;
1371
1372 totallen += (ssize_t)inprocess;
1373 in += inprocess;
1374 inlen -= inprocess;
1375 out += ret;
1376 outlen -= (uint32_t)ret;
1377 }
1378
1379 return totallen;
1380 }
1381
1382 ssize_t _kcapi_cipher_crypt_aio(struct kcapi_handle *handle,
1383 struct iovec *iniov, struct iovec *outiov,
1384 size_t iovlen, int access, unsigned int enc)
1385 {
1386 size_t i, outstanding = 0, processed = 0, inflight = 0;
1387 ssize_t ret;
1388
1389 if (handle->aio.disable == true) {
1390 kcapi_dolog(KCAPI_LOG_WARN, "AIO support disabled\n");
1391 return -EOPNOTSUPP;
1392 }
1393
1394 /* Every IOVEC is processed as its individual cipher operation. */
1395 while (iovlen) {
1396 size_t max_process = KCAPI_AIO_CONCURRENT - inflight;
1397 size_t process = (max_process < iovlen) ?
1398 max_process : iovlen;
1399
1400 ret = _kcapi_aio_send_iov(handle, iniov, process, access, enc);
1401 if (ret < 0)
1402 return ret;
1403
1404 ret = _kcapi_aio_read_iov(handle, outiov, process);
1405 if (ret < 0)
1406 return ret;
1407
1408 iniov += process;
1409 outiov += process;
1410 iovlen -= process;
1411
1412 inflight = 0;
1413 for (i = 0; i < KCAPI_AIO_CONCURRENT; i++) {
1414 if (handle->aio.iocb_ret[i] == AIO_OUTSTANDING) {
1415 inflight++;
1416 } else if (handle->aio.iocb_ret[i] < 0) {
1417 return (int32_t)handle->aio.iocb_ret[i];
1418 } else {
1419 if (handle->aio.iocb_ret[i] > INT_MAX)
1420 return -EOVERFLOW;
1421
1422 processed += (uint32_t)handle->aio.iocb_ret[i];
1423 if (processed > INT_MAX)
1424 return -EOVERFLOW;
1425 handle->aio.iocb_ret[i] = 0;
1426 }
1427 }
1428 }
1429
1430 /*
1431 * If a multi-staged AIO operation shall be designed, the following
1432 * loop needs to be moved to a closing API call. If done so, the
1433 * current function could be invoked multiple times to send more data
1434 * to the kernel before the closing call requires that all outstanding
1435 * requests are to be completed.
1436 *
1437 * If a multi-staged AIO operation is to be implemented, the issue
1438 * is that when submitting a number of requests, the caller is not
1439 * able to detect which particular request is completed. Thus, an
1440 * "open-ended" multi-staged AIO operation could not be implemented.
1441 */
1442 for (i = 0; i < KCAPI_AIO_CONCURRENT; i++) {
1443 if (handle->aio.iocb_ret[i] == AIO_OUTSTANDING)
1444 outstanding++;
1445 }
1446
1447 ret = _kcapi_aio_read_all(handle, outstanding, NULL);
1448 if (ret < 0)
1449 return ret;
1450
1451 for (i = 0; i < KCAPI_AIO_CONCURRENT; i++) {
1452 if (handle->aio.iocb_ret[i] == AIO_OUTSTANDING) {
1453 return -EBADMSG;
1454 } else if (handle->aio.iocb_ret[i] < 0) {
1455 return (int32_t)handle->aio.iocb_ret[i];
1456 } else {
1457 if (handle->aio.iocb_ret[i] > INT_MAX)
1458 return -EOVERFLOW;
1459
1460 processed += (uint32_t)handle->aio.iocb_ret[i];
1461 if (processed > INT_MAX)
1462 return -EOVERFLOW;
1463 handle->aio.iocb_ret[i] = 0;
1464 }
1465 }
1466 return (ssize_t)processed;
1467 }