2 * algif_aead: User-space interface for AEAD algorithms
4 * Copyright (C) 2014, Stephan Mueller <smueller@chronox.de>
6 * This file provides the user-space API for AEAD ciphers.
8 * This file is derived from algif_skcipher.c.
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the Free
12 * Software Foundation; either version 2 of the License, or (at your option)
16 #include <crypto/internal/aead.h>
17 #include <crypto/scatterwalk.h>
18 #include <crypto/if_alg.h>
19 #include <linux/init.h>
20 #include <linux/list.h>
21 #include <linux/kernel.h>
22 #include <linux/sched/signal.h>
24 #include <linux/module.h>
25 #include <linux/net.h>
30 struct scatterlist sg[ALG_MAX_PAGES];
33 struct aead_async_rsgl {
34 struct af_alg_sgl sgl;
35 struct list_head list;
38 struct aead_async_req {
39 struct scatterlist *tsgl;
40 struct aead_async_rsgl first_rsgl;
41 struct list_head list;
49 struct crypto_aead *aead;
54 struct aead_sg_list tsgl;
55 struct aead_async_rsgl first_rsgl;
56 struct list_head list;
60 struct af_alg_completion completion;
70 struct aead_request aead_req;
73 static inline int aead_sndbuf(struct sock *sk)
75 struct alg_sock *ask = alg_sk(sk);
76 struct aead_ctx *ctx = ask->private;
78 return max_t(int, max_t(int, sk->sk_sndbuf & PAGE_MASK, PAGE_SIZE) -
82 static inline bool aead_writable(struct sock *sk)
84 return PAGE_SIZE <= aead_sndbuf(sk);
87 static inline bool aead_sufficient_data(struct aead_ctx *ctx)
89 unsigned as = crypto_aead_authsize(crypto_aead_reqtfm(&ctx->aead_req));
92 * The minimum amount of memory needed for an AEAD cipher is
93 * the AAD and in case of decryption the tag.
95 return ctx->used >= ctx->aead_assoclen + (ctx->enc ? 0 : as);
98 static void aead_reset_ctx(struct aead_ctx *ctx)
100 struct aead_sg_list *sgl = &ctx->tsgl;
102 sg_init_table(sgl->sg, ALG_MAX_PAGES);
109 static void aead_put_sgl(struct sock *sk)
111 struct alg_sock *ask = alg_sk(sk);
112 struct aead_ctx *ctx = ask->private;
113 struct aead_sg_list *sgl = &ctx->tsgl;
114 struct scatterlist *sg = sgl->sg;
117 for (i = 0; i < sgl->cur; i++) {
118 if (!sg_page(sg + i))
121 put_page(sg_page(sg + i));
122 sg_assign_page(sg + i, NULL);
127 static void aead_wmem_wakeup(struct sock *sk)
129 struct socket_wq *wq;
131 if (!aead_writable(sk))
135 wq = rcu_dereference(sk->sk_wq);
136 if (skwq_has_sleeper(wq))
137 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
140 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
144 static int aead_wait_for_data(struct sock *sk, unsigned flags)
146 DEFINE_WAIT_FUNC(wait, woken_wake_function);
147 struct alg_sock *ask = alg_sk(sk);
148 struct aead_ctx *ctx = ask->private;
150 int err = -ERESTARTSYS;
152 if (flags & MSG_DONTWAIT)
155 sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
156 add_wait_queue(sk_sleep(sk), &wait);
158 if (signal_pending(current))
160 timeout = MAX_SCHEDULE_TIMEOUT;
161 if (sk_wait_event(sk, &timeout, !ctx->more, &wait)) {
166 remove_wait_queue(sk_sleep(sk), &wait);
168 sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
173 static void aead_data_wakeup(struct sock *sk)
175 struct alg_sock *ask = alg_sk(sk);
176 struct aead_ctx *ctx = ask->private;
177 struct socket_wq *wq;
185 wq = rcu_dereference(sk->sk_wq);
186 if (skwq_has_sleeper(wq))
187 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
190 sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
194 static int aead_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
196 struct sock *sk = sock->sk;
197 struct alg_sock *ask = alg_sk(sk);
198 struct aead_ctx *ctx = ask->private;
200 crypto_aead_ivsize(crypto_aead_reqtfm(&ctx->aead_req));
201 struct aead_sg_list *sgl = &ctx->tsgl;
202 struct af_alg_control con = {};
208 if (msg->msg_controllen) {
209 err = af_alg_cmsg_send(msg, &con);
225 if (con.iv && con.iv->ivlen != ivsize)
230 if (!ctx->more && ctx->used)
236 memcpy(ctx->iv, con.iv->iv, ivsize);
238 ctx->aead_assoclen = con.aead_assoclen;
243 struct scatterlist *sg = NULL;
245 /* use the existing memory in an allocated page */
247 sg = sgl->sg + sgl->cur - 1;
248 len = min_t(unsigned long, len,
249 PAGE_SIZE - sg->offset - sg->length);
250 err = memcpy_from_msg(page_address(sg_page(sg)) +
251 sg->offset + sg->length,
257 ctx->merge = (sg->offset + sg->length) &
266 if (!aead_writable(sk)) {
267 /* user space sent too much data */
273 /* allocate a new page */
274 len = min_t(unsigned long, size, aead_sndbuf(sk));
278 if (sgl->cur >= ALG_MAX_PAGES) {
284 sg = sgl->sg + sgl->cur;
285 plen = min_t(size_t, len, PAGE_SIZE);
287 sg_assign_page(sg, alloc_page(GFP_KERNEL));
292 err = memcpy_from_msg(page_address(sg_page(sg)),
295 __free_page(sg_page(sg));
296 sg_assign_page(sg, NULL);
307 ctx->merge = plen & (PAGE_SIZE - 1);
313 ctx->more = msg->msg_flags & MSG_MORE;
314 if (!ctx->more && !aead_sufficient_data(ctx)) {
320 aead_data_wakeup(sk);
323 return err ?: copied;
326 static ssize_t aead_sendpage(struct socket *sock, struct page *page,
327 int offset, size_t size, int flags)
329 struct sock *sk = sock->sk;
330 struct alg_sock *ask = alg_sk(sk);
331 struct aead_ctx *ctx = ask->private;
332 struct aead_sg_list *sgl = &ctx->tsgl;
335 if (flags & MSG_SENDPAGE_NOTLAST)
338 if (sgl->cur >= ALG_MAX_PAGES)
342 if (!ctx->more && ctx->used)
348 if (!aead_writable(sk)) {
349 /* user space sent too much data */
358 sg_set_page(sgl->sg + sgl->cur, page, size, offset);
365 ctx->more = flags & MSG_MORE;
366 if (!ctx->more && !aead_sufficient_data(ctx)) {
372 aead_data_wakeup(sk);
378 #define GET_ASYM_REQ(req, tfm) (struct aead_async_req *) \
379 ((char *)req + sizeof(struct aead_request) + \
380 crypto_aead_reqsize(tfm))
382 #define GET_REQ_SIZE(tfm) sizeof(struct aead_async_req) + \
383 crypto_aead_reqsize(tfm) + crypto_aead_ivsize(tfm) + \
384 sizeof(struct aead_request)
386 static void aead_async_cb(struct crypto_async_request *_req, int err)
388 struct aead_request *req = _req->data;
389 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
390 struct aead_async_req *areq = GET_ASYM_REQ(req, tfm);
391 struct sock *sk = areq->sk;
392 struct scatterlist *sg = areq->tsgl;
393 struct aead_async_rsgl *rsgl;
394 struct kiocb *iocb = areq->iocb;
395 unsigned int i, reqlen = GET_REQ_SIZE(tfm);
397 list_for_each_entry(rsgl, &areq->list, list) {
398 af_alg_free_sg(&rsgl->sgl);
399 if (rsgl != &areq->first_rsgl)
400 sock_kfree_s(sk, rsgl, sizeof(*rsgl));
403 for (i = 0; i < areq->tsgls; i++)
404 put_page(sg_page(sg + i));
406 sock_kfree_s(sk, areq->tsgl, sizeof(*areq->tsgl) * areq->tsgls);
407 sock_kfree_s(sk, req, reqlen);
409 iocb->ki_complete(iocb, err, err);
412 static int aead_recvmsg_async(struct socket *sock, struct msghdr *msg,
415 struct sock *sk = sock->sk;
416 struct alg_sock *ask = alg_sk(sk);
417 struct aead_ctx *ctx = ask->private;
418 struct crypto_aead *tfm = crypto_aead_reqtfm(&ctx->aead_req);
419 struct aead_async_req *areq;
420 struct aead_request *req = NULL;
421 struct aead_sg_list *sgl = &ctx->tsgl;
422 struct aead_async_rsgl *last_rsgl = NULL, *rsgl;
423 unsigned int as = crypto_aead_authsize(tfm);
424 unsigned int i, reqlen = GET_REQ_SIZE(tfm);
428 size_t usedpages = 0;
432 err = aead_wait_for_data(sk, flags);
437 if (!aead_sufficient_data(ctx))
446 req = sock_kmalloc(sk, reqlen, GFP_KERNEL);
450 areq = GET_ASYM_REQ(req, tfm);
451 memset(&areq->first_rsgl, '\0', sizeof(areq->first_rsgl));
452 INIT_LIST_HEAD(&areq->list);
453 areq->iocb = msg->msg_iocb;
455 memcpy(areq->iv, ctx->iv, crypto_aead_ivsize(tfm));
456 aead_request_set_tfm(req, tfm);
457 aead_request_set_ad(req, ctx->aead_assoclen);
458 aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
460 used -= ctx->aead_assoclen;
462 /* take over all tx sgls from ctx */
463 areq->tsgl = sock_kmalloc(sk,
464 sizeof(*areq->tsgl) * max_t(u32, sgl->cur, 1),
466 if (unlikely(!areq->tsgl))
469 sg_init_table(areq->tsgl, max_t(u32, sgl->cur, 1));
470 for (i = 0; i < sgl->cur; i++)
471 sg_set_page(&areq->tsgl[i], sg_page(&sgl->sg[i]),
472 sgl->sg[i].length, sgl->sg[i].offset);
474 areq->tsgls = sgl->cur;
477 while (outlen > usedpages && iov_iter_count(&msg->msg_iter)) {
478 size_t seglen = min_t(size_t, iov_iter_count(&msg->msg_iter),
479 (outlen - usedpages));
481 if (list_empty(&areq->list)) {
482 rsgl = &areq->first_rsgl;
485 rsgl = sock_kmalloc(sk, sizeof(*rsgl), GFP_KERNEL);
486 if (unlikely(!rsgl)) {
491 rsgl->sgl.npages = 0;
492 list_add_tail(&rsgl->list, &areq->list);
494 /* make one iovec available as scatterlist */
495 err = af_alg_make_sg(&rsgl->sgl, &msg->msg_iter, seglen);
501 /* chain the new scatterlist with previous one */
503 af_alg_link_sg(&last_rsgl->sgl, &rsgl->sgl);
507 iov_iter_advance(&msg->msg_iter, err);
510 /* ensure output buffer is sufficiently large */
511 if (usedpages < outlen) {
516 aead_request_set_crypt(req, areq->tsgl, areq->first_rsgl.sgl.sg, used,
518 err = ctx->enc ? crypto_aead_encrypt(req) : crypto_aead_decrypt(req);
520 if (err == -EINPROGRESS) {
525 } else if (err == -EBADMSG) {
533 list_for_each_entry(rsgl, &areq->list, list) {
534 af_alg_free_sg(&rsgl->sgl);
535 if (rsgl != &areq->first_rsgl)
536 sock_kfree_s(sk, rsgl, sizeof(*rsgl));
539 sock_kfree_s(sk, areq->tsgl, sizeof(*areq->tsgl) * areq->tsgls);
541 sock_kfree_s(sk, req, reqlen);
543 aead_wmem_wakeup(sk);
545 return err ? err : outlen;
548 static int aead_recvmsg_sync(struct socket *sock, struct msghdr *msg, int flags)
550 struct sock *sk = sock->sk;
551 struct alg_sock *ask = alg_sk(sk);
552 struct aead_ctx *ctx = ask->private;
553 unsigned as = crypto_aead_authsize(crypto_aead_reqtfm(&ctx->aead_req));
554 struct aead_sg_list *sgl = &ctx->tsgl;
555 struct aead_async_rsgl *last_rsgl = NULL;
556 struct aead_async_rsgl *rsgl, *tmp;
558 unsigned long used = 0;
560 size_t usedpages = 0;
565 * Please see documentation of aead_request_set_crypt for the
566 * description of the AEAD memory structure expected from the caller.
570 err = aead_wait_for_data(sk, flags);
575 /* data length provided by caller via sendmsg/sendpage */
579 * Make sure sufficient data is present -- note, the same check is
580 * is also present in sendmsg/sendpage. The checks in sendpage/sendmsg
581 * shall provide an information to the data sender that something is
582 * wrong, but they are irrelevant to maintain the kernel integrity.
583 * We need this check here too in case user space decides to not honor
584 * the error message in sendmsg/sendpage and still call recvmsg. This
585 * check here protects the kernel integrity.
587 if (!aead_sufficient_data(ctx))
591 * Calculate the minimum output buffer size holding the result of the
592 * cipher operation. When encrypting data, the receiving buffer is
593 * larger by the tag length compared to the input buffer as the
594 * encryption operation generates the tag. For decryption, the input
595 * buffer provides the tag which is consumed resulting in only the
596 * plaintext without a buffer for the tag returned to the caller.
604 * The cipher operation input data is reduced by the associated data
605 * length as this data is processed separately later on.
607 used -= ctx->aead_assoclen;
609 /* convert iovecs of output buffers into scatterlists */
610 while (outlen > usedpages && iov_iter_count(&msg->msg_iter)) {
611 size_t seglen = min_t(size_t, iov_iter_count(&msg->msg_iter),
612 (outlen - usedpages));
614 if (list_empty(&ctx->list)) {
615 rsgl = &ctx->first_rsgl;
617 rsgl = sock_kmalloc(sk, sizeof(*rsgl), GFP_KERNEL);
618 if (unlikely(!rsgl)) {
623 rsgl->sgl.npages = 0;
624 list_add_tail(&rsgl->list, &ctx->list);
626 /* make one iovec available as scatterlist */
627 err = af_alg_make_sg(&rsgl->sgl, &msg->msg_iter, seglen);
631 /* chain the new scatterlist with previous one */
633 af_alg_link_sg(&last_rsgl->sgl, &rsgl->sgl);
637 iov_iter_advance(&msg->msg_iter, err);
640 /* ensure output buffer is sufficiently large */
641 if (usedpages < outlen) {
646 sg_mark_end(sgl->sg + sgl->cur - 1);
647 aead_request_set_crypt(&ctx->aead_req, sgl->sg, ctx->first_rsgl.sgl.sg,
649 aead_request_set_ad(&ctx->aead_req, ctx->aead_assoclen);
651 err = af_alg_wait_for_completion(ctx->enc ?
652 crypto_aead_encrypt(&ctx->aead_req) :
653 crypto_aead_decrypt(&ctx->aead_req),
657 /* EBADMSG implies a valid cipher operation took place */
668 list_for_each_entry_safe(rsgl, tmp, &ctx->list, list) {
669 af_alg_free_sg(&rsgl->sgl);
670 list_del(&rsgl->list);
671 if (rsgl != &ctx->first_rsgl)
672 sock_kfree_s(sk, rsgl, sizeof(*rsgl));
674 INIT_LIST_HEAD(&ctx->list);
675 aead_wmem_wakeup(sk);
678 return err ? err : outlen;
681 static int aead_recvmsg(struct socket *sock, struct msghdr *msg, size_t ignored,
684 return (msg->msg_iocb && !is_sync_kiocb(msg->msg_iocb)) ?
685 aead_recvmsg_async(sock, msg, flags) :
686 aead_recvmsg_sync(sock, msg, flags);
689 static unsigned int aead_poll(struct file *file, struct socket *sock,
692 struct sock *sk = sock->sk;
693 struct alg_sock *ask = alg_sk(sk);
694 struct aead_ctx *ctx = ask->private;
697 sock_poll_wait(file, sk_sleep(sk), wait);
701 mask |= POLLIN | POLLRDNORM;
703 if (aead_writable(sk))
704 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
709 static struct proto_ops algif_aead_ops = {
712 .connect = sock_no_connect,
713 .socketpair = sock_no_socketpair,
714 .getname = sock_no_getname,
715 .ioctl = sock_no_ioctl,
716 .listen = sock_no_listen,
717 .shutdown = sock_no_shutdown,
718 .getsockopt = sock_no_getsockopt,
719 .mmap = sock_no_mmap,
720 .bind = sock_no_bind,
721 .accept = sock_no_accept,
722 .setsockopt = sock_no_setsockopt,
724 .release = af_alg_release,
725 .sendmsg = aead_sendmsg,
726 .sendpage = aead_sendpage,
727 .recvmsg = aead_recvmsg,
731 static int aead_check_key(struct socket *sock)
735 struct alg_sock *pask;
736 struct aead_tfm *tfm;
737 struct sock *sk = sock->sk;
738 struct alg_sock *ask = alg_sk(sk);
745 pask = alg_sk(ask->parent);
749 lock_sock_nested(psk, SINGLE_DEPTH_NESTING);
769 static int aead_sendmsg_nokey(struct socket *sock, struct msghdr *msg,
774 err = aead_check_key(sock);
778 return aead_sendmsg(sock, msg, size);
781 static ssize_t aead_sendpage_nokey(struct socket *sock, struct page *page,
782 int offset, size_t size, int flags)
786 err = aead_check_key(sock);
790 return aead_sendpage(sock, page, offset, size, flags);
793 static int aead_recvmsg_nokey(struct socket *sock, struct msghdr *msg,
794 size_t ignored, int flags)
798 err = aead_check_key(sock);
802 return aead_recvmsg(sock, msg, ignored, flags);
805 static struct proto_ops algif_aead_ops_nokey = {
808 .connect = sock_no_connect,
809 .socketpair = sock_no_socketpair,
810 .getname = sock_no_getname,
811 .ioctl = sock_no_ioctl,
812 .listen = sock_no_listen,
813 .shutdown = sock_no_shutdown,
814 .getsockopt = sock_no_getsockopt,
815 .mmap = sock_no_mmap,
816 .bind = sock_no_bind,
817 .accept = sock_no_accept,
818 .setsockopt = sock_no_setsockopt,
820 .release = af_alg_release,
821 .sendmsg = aead_sendmsg_nokey,
822 .sendpage = aead_sendpage_nokey,
823 .recvmsg = aead_recvmsg_nokey,
827 static void *aead_bind(const char *name, u32 type, u32 mask)
829 struct aead_tfm *tfm;
830 struct crypto_aead *aead;
832 tfm = kzalloc(sizeof(*tfm), GFP_KERNEL);
834 return ERR_PTR(-ENOMEM);
836 aead = crypto_alloc_aead(name, type, mask);
839 return ERR_CAST(aead);
847 static void aead_release(void *private)
849 struct aead_tfm *tfm = private;
851 crypto_free_aead(tfm->aead);
855 static int aead_setauthsize(void *private, unsigned int authsize)
857 struct aead_tfm *tfm = private;
859 return crypto_aead_setauthsize(tfm->aead, authsize);
862 static int aead_setkey(void *private, const u8 *key, unsigned int keylen)
864 struct aead_tfm *tfm = private;
867 err = crypto_aead_setkey(tfm->aead, key, keylen);
873 static void aead_sock_destruct(struct sock *sk)
875 struct alg_sock *ask = alg_sk(sk);
876 struct aead_ctx *ctx = ask->private;
877 unsigned int ivlen = crypto_aead_ivsize(
878 crypto_aead_reqtfm(&ctx->aead_req));
880 WARN_ON(refcount_read(&sk->sk_refcnt) != 0);
882 sock_kzfree_s(sk, ctx->iv, ivlen);
883 sock_kfree_s(sk, ctx, ctx->len);
884 af_alg_release_parent(sk);
887 static int aead_accept_parent_nokey(void *private, struct sock *sk)
889 struct aead_ctx *ctx;
890 struct alg_sock *ask = alg_sk(sk);
891 struct aead_tfm *tfm = private;
892 struct crypto_aead *aead = tfm->aead;
893 unsigned int len = sizeof(*ctx) + crypto_aead_reqsize(aead);
894 unsigned int ivlen = crypto_aead_ivsize(aead);
896 ctx = sock_kmalloc(sk, len, GFP_KERNEL);
901 ctx->iv = sock_kmalloc(sk, ivlen, GFP_KERNEL);
903 sock_kfree_s(sk, ctx, len);
906 memset(ctx->iv, 0, ivlen);
914 ctx->aead_assoclen = 0;
915 af_alg_init_completion(&ctx->completion);
916 sg_init_table(ctx->tsgl.sg, ALG_MAX_PAGES);
917 INIT_LIST_HEAD(&ctx->list);
921 aead_request_set_tfm(&ctx->aead_req, aead);
922 aead_request_set_callback(&ctx->aead_req, CRYPTO_TFM_REQ_MAY_BACKLOG,
923 af_alg_complete, &ctx->completion);
925 sk->sk_destruct = aead_sock_destruct;
930 static int aead_accept_parent(void *private, struct sock *sk)
932 struct aead_tfm *tfm = private;
937 return aead_accept_parent_nokey(private, sk);
940 static const struct af_alg_type algif_type_aead = {
942 .release = aead_release,
943 .setkey = aead_setkey,
944 .setauthsize = aead_setauthsize,
945 .accept = aead_accept_parent,
946 .accept_nokey = aead_accept_parent_nokey,
947 .ops = &algif_aead_ops,
948 .ops_nokey = &algif_aead_ops_nokey,
953 static int __init algif_aead_init(void)
955 return af_alg_register_type(&algif_type_aead);
958 static void __exit algif_aead_exit(void)
960 int err = af_alg_unregister_type(&algif_type_aead);
964 module_init(algif_aead_init);
965 module_exit(algif_aead_exit);
966 MODULE_LICENSE("GPL");
967 MODULE_AUTHOR("Stephan Mueller <smueller@chronox.de>");
968 MODULE_DESCRIPTION("AEAD kernel crypto API user space interface");