2 * algif_skcipher: User-space interface for skcipher algorithms
4 * This file provides the user-space API for symmetric key ciphers.
6 * Copyright (c) 2010 Herbert Xu <herbert@gondor.apana.org.au>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the Free
10 * Software Foundation; either version 2 of the License, or (at your option)
15 #include <crypto/scatterwalk.h>
16 #include <crypto/skcipher.h>
17 #include <crypto/if_alg.h>
18 #include <linux/init.h>
19 #include <linux/list.h>
20 #include <linux/kernel.h>
21 #include <linux/sched/signal.h>
23 #include <linux/module.h>
24 #include <linux/net.h>
27 struct skcipher_sg_list {
28 struct list_head list;
32 struct scatterlist sg[0];
36 struct crypto_skcipher *skcipher;
41 struct list_head tsgl;
42 struct af_alg_sgl rsgl;
46 struct af_alg_completion completion;
56 struct skcipher_request req;
59 struct skcipher_async_rsgl {
60 struct af_alg_sgl sgl;
61 struct list_head list;
64 struct skcipher_async_req {
66 struct skcipher_async_rsgl first_sgl;
67 struct list_head list;
68 struct scatterlist *tsg;
70 struct skcipher_request req;
73 #define MAX_SGL_ENTS ((4096 - sizeof(struct skcipher_sg_list)) / \
74 sizeof(struct scatterlist) - 1)
76 static void skcipher_free_async_sgls(struct skcipher_async_req *sreq)
78 struct skcipher_async_rsgl *rsgl, *tmp;
79 struct scatterlist *sgl;
80 struct scatterlist *sg;
83 list_for_each_entry_safe(rsgl, tmp, &sreq->list, list) {
84 af_alg_free_sg(&rsgl->sgl);
85 if (rsgl != &sreq->first_sgl)
90 for_each_sg(sgl, sg, n, i)
91 put_page(sg_page(sg));
96 static void skcipher_async_cb(struct crypto_async_request *req, int err)
98 struct skcipher_async_req *sreq = req->data;
99 struct kiocb *iocb = sreq->iocb;
101 atomic_dec(sreq->inflight);
102 skcipher_free_async_sgls(sreq);
104 iocb->ki_complete(iocb, err, err);
107 static inline int skcipher_sndbuf(struct sock *sk)
109 struct alg_sock *ask = alg_sk(sk);
110 struct skcipher_ctx *ctx = ask->private;
112 return max_t(int, max_t(int, sk->sk_sndbuf & PAGE_MASK, PAGE_SIZE) -
116 static inline bool skcipher_writable(struct sock *sk)
118 return PAGE_SIZE <= skcipher_sndbuf(sk);
121 static int skcipher_alloc_sgl(struct sock *sk)
123 struct alg_sock *ask = alg_sk(sk);
124 struct skcipher_ctx *ctx = ask->private;
125 struct skcipher_sg_list *sgl;
126 struct scatterlist *sg = NULL;
128 sgl = list_entry(ctx->tsgl.prev, struct skcipher_sg_list, list);
129 if (!list_empty(&ctx->tsgl))
132 if (!sg || sgl->cur >= MAX_SGL_ENTS) {
133 sgl = sock_kmalloc(sk, sizeof(*sgl) +
134 sizeof(sgl->sg[0]) * (MAX_SGL_ENTS + 1),
139 sg_init_table(sgl->sg, MAX_SGL_ENTS + 1);
143 sg_chain(sg, MAX_SGL_ENTS + 1, sgl->sg);
145 list_add_tail(&sgl->list, &ctx->tsgl);
151 static void skcipher_pull_sgl(struct sock *sk, size_t used, int put)
153 struct alg_sock *ask = alg_sk(sk);
154 struct skcipher_ctx *ctx = ask->private;
155 struct skcipher_sg_list *sgl;
156 struct scatterlist *sg;
159 while (!list_empty(&ctx->tsgl)) {
160 sgl = list_first_entry(&ctx->tsgl, struct skcipher_sg_list,
164 for (i = 0; i < sgl->cur; i++) {
165 size_t plen = min_t(size_t, used, sg[i].length);
167 if (!sg_page(sg + i))
170 sg[i].length -= plen;
171 sg[i].offset += plen;
179 put_page(sg_page(sg + i));
180 sg_assign_page(sg + i, NULL);
183 list_del(&sgl->list);
184 sock_kfree_s(sk, sgl,
185 sizeof(*sgl) + sizeof(sgl->sg[0]) *
193 static void skcipher_free_sgl(struct sock *sk)
195 struct alg_sock *ask = alg_sk(sk);
196 struct skcipher_ctx *ctx = ask->private;
198 skcipher_pull_sgl(sk, ctx->used, 1);
201 static int skcipher_wait_for_wmem(struct sock *sk, unsigned flags)
203 DEFINE_WAIT_FUNC(wait, woken_wake_function);
204 int err = -ERESTARTSYS;
207 if (flags & MSG_DONTWAIT)
210 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
212 add_wait_queue(sk_sleep(sk), &wait);
214 if (signal_pending(current))
216 timeout = MAX_SCHEDULE_TIMEOUT;
217 if (sk_wait_event(sk, &timeout, skcipher_writable(sk), &wait)) {
222 remove_wait_queue(sk_sleep(sk), &wait);
227 static void skcipher_wmem_wakeup(struct sock *sk)
229 struct socket_wq *wq;
231 if (!skcipher_writable(sk))
235 wq = rcu_dereference(sk->sk_wq);
236 if (skwq_has_sleeper(wq))
237 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
240 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
244 static int skcipher_wait_for_data(struct sock *sk, unsigned flags)
246 DEFINE_WAIT_FUNC(wait, woken_wake_function);
247 struct alg_sock *ask = alg_sk(sk);
248 struct skcipher_ctx *ctx = ask->private;
250 int err = -ERESTARTSYS;
252 if (flags & MSG_DONTWAIT) {
256 sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
258 add_wait_queue(sk_sleep(sk), &wait);
260 if (signal_pending(current))
262 timeout = MAX_SCHEDULE_TIMEOUT;
263 if (sk_wait_event(sk, &timeout, ctx->used, &wait)) {
268 remove_wait_queue(sk_sleep(sk), &wait);
270 sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
275 static void skcipher_data_wakeup(struct sock *sk)
277 struct alg_sock *ask = alg_sk(sk);
278 struct skcipher_ctx *ctx = ask->private;
279 struct socket_wq *wq;
285 wq = rcu_dereference(sk->sk_wq);
286 if (skwq_has_sleeper(wq))
287 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
290 sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
294 static int skcipher_sendmsg(struct socket *sock, struct msghdr *msg,
297 struct sock *sk = sock->sk;
298 struct alg_sock *ask = alg_sk(sk);
299 struct sock *psk = ask->parent;
300 struct alg_sock *pask = alg_sk(psk);
301 struct skcipher_ctx *ctx = ask->private;
302 struct skcipher_tfm *skc = pask->private;
303 struct crypto_skcipher *tfm = skc->skcipher;
304 unsigned ivsize = crypto_skcipher_ivsize(tfm);
305 struct skcipher_sg_list *sgl;
306 struct af_alg_control con = {};
313 if (msg->msg_controllen) {
314 err = af_alg_cmsg_send(msg, &con);
330 if (con.iv && con.iv->ivlen != ivsize)
337 if (!ctx->more && ctx->used)
343 memcpy(ctx->iv, con.iv->iv, ivsize);
347 struct scatterlist *sg;
348 unsigned long len = size;
352 sgl = list_entry(ctx->tsgl.prev,
353 struct skcipher_sg_list, list);
354 sg = sgl->sg + sgl->cur - 1;
355 len = min_t(unsigned long, len,
356 PAGE_SIZE - sg->offset - sg->length);
358 err = memcpy_from_msg(page_address(sg_page(sg)) +
359 sg->offset + sg->length,
365 ctx->merge = (sg->offset + sg->length) &
374 if (!skcipher_writable(sk)) {
375 err = skcipher_wait_for_wmem(sk, msg->msg_flags);
380 len = min_t(unsigned long, len, skcipher_sndbuf(sk));
382 err = skcipher_alloc_sgl(sk);
386 sgl = list_entry(ctx->tsgl.prev, struct skcipher_sg_list, list);
389 sg_unmark_end(sg + sgl->cur - 1);
392 plen = min_t(size_t, len, PAGE_SIZE);
394 sg_assign_page(sg + i, alloc_page(GFP_KERNEL));
396 if (!sg_page(sg + i))
399 err = memcpy_from_msg(page_address(sg_page(sg + i)),
402 __free_page(sg_page(sg + i));
403 sg_assign_page(sg + i, NULL);
413 } while (len && sgl->cur < MAX_SGL_ENTS);
416 sg_mark_end(sg + sgl->cur - 1);
418 ctx->merge = plen & (PAGE_SIZE - 1);
423 ctx->more = msg->msg_flags & MSG_MORE;
426 skcipher_data_wakeup(sk);
429 return copied ?: err;
432 static ssize_t skcipher_sendpage(struct socket *sock, struct page *page,
433 int offset, size_t size, int flags)
435 struct sock *sk = sock->sk;
436 struct alg_sock *ask = alg_sk(sk);
437 struct skcipher_ctx *ctx = ask->private;
438 struct skcipher_sg_list *sgl;
441 if (flags & MSG_SENDPAGE_NOTLAST)
445 if (!ctx->more && ctx->used)
451 if (!skcipher_writable(sk)) {
452 err = skcipher_wait_for_wmem(sk, flags);
457 err = skcipher_alloc_sgl(sk);
462 sgl = list_entry(ctx->tsgl.prev, struct skcipher_sg_list, list);
465 sg_unmark_end(sgl->sg + sgl->cur - 1);
467 sg_mark_end(sgl->sg + sgl->cur);
469 sg_set_page(sgl->sg + sgl->cur, page, size, offset);
474 ctx->more = flags & MSG_MORE;
477 skcipher_data_wakeup(sk);
483 static int skcipher_all_sg_nents(struct skcipher_ctx *ctx)
485 struct skcipher_sg_list *sgl;
486 struct scatterlist *sg;
489 list_for_each_entry(sgl, &ctx->tsgl, list) {
495 nents += sg_nents(sg);
500 static int skcipher_recvmsg_async(struct socket *sock, struct msghdr *msg,
503 struct sock *sk = sock->sk;
504 struct alg_sock *ask = alg_sk(sk);
505 struct sock *psk = ask->parent;
506 struct alg_sock *pask = alg_sk(psk);
507 struct skcipher_ctx *ctx = ask->private;
508 struct skcipher_tfm *skc = pask->private;
509 struct crypto_skcipher *tfm = skc->skcipher;
510 struct skcipher_sg_list *sgl;
511 struct scatterlist *sg;
512 struct skcipher_async_req *sreq;
513 struct skcipher_request *req;
514 struct skcipher_async_rsgl *last_rsgl = NULL;
515 unsigned int txbufs = 0, len = 0, tx_nents;
516 unsigned int reqsize = crypto_skcipher_reqsize(tfm);
517 unsigned int ivsize = crypto_skcipher_ivsize(tfm);
522 sreq = kzalloc(sizeof(*sreq) + reqsize + ivsize, GFP_KERNEL);
527 iv = (char *)(req + 1) + reqsize;
528 sreq->iocb = msg->msg_iocb;
529 INIT_LIST_HEAD(&sreq->list);
530 sreq->inflight = &ctx->inflight;
533 tx_nents = skcipher_all_sg_nents(ctx);
534 sreq->tsg = kcalloc(tx_nents, sizeof(*sg), GFP_KERNEL);
535 if (unlikely(!sreq->tsg))
537 sg_init_table(sreq->tsg, tx_nents);
538 memcpy(iv, ctx->iv, ivsize);
539 skcipher_request_set_tfm(req, tfm);
540 skcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_SLEEP,
541 skcipher_async_cb, sreq);
543 while (iov_iter_count(&msg->msg_iter)) {
544 struct skcipher_async_rsgl *rsgl;
548 err = skcipher_wait_for_data(sk, flags);
552 sgl = list_first_entry(&ctx->tsgl,
553 struct skcipher_sg_list, list);
559 used = min_t(unsigned long, ctx->used,
560 iov_iter_count(&msg->msg_iter));
561 used = min_t(unsigned long, used, sg->length);
563 if (txbufs == tx_nents) {
564 struct scatterlist *tmp;
566 /* Ran out of tx slots in async request
568 tmp = kcalloc(tx_nents * 2, sizeof(*tmp),
575 sg_init_table(tmp, tx_nents * 2);
576 for (x = 0; x < tx_nents; x++)
577 sg_set_page(&tmp[x], sg_page(&sreq->tsg[x]),
579 sreq->tsg[x].offset);
585 /* Need to take over the tx sgl from ctx
586 * to the asynch req - these sgls will be freed later */
587 sg_set_page(sreq->tsg + txbufs++, sg_page(sg), sg->length,
590 if (list_empty(&sreq->list)) {
591 rsgl = &sreq->first_sgl;
592 list_add_tail(&rsgl->list, &sreq->list);
594 rsgl = kmalloc(sizeof(*rsgl), GFP_KERNEL);
599 list_add_tail(&rsgl->list, &sreq->list);
602 used = af_alg_make_sg(&rsgl->sgl, &msg->msg_iter, used);
607 af_alg_link_sg(&last_rsgl->sgl, &rsgl->sgl);
611 skcipher_pull_sgl(sk, used, 0);
612 iov_iter_advance(&msg->msg_iter, used);
616 sg_mark_end(sreq->tsg + txbufs - 1);
618 skcipher_request_set_crypt(req, sreq->tsg, sreq->first_sgl.sgl.sg,
620 err = ctx->enc ? crypto_skcipher_encrypt(req) :
621 crypto_skcipher_decrypt(req);
622 if (err == -EINPROGRESS) {
623 atomic_inc(&ctx->inflight);
629 skcipher_free_async_sgls(sreq);
631 skcipher_wmem_wakeup(sk);
638 static int skcipher_recvmsg_sync(struct socket *sock, struct msghdr *msg,
641 struct sock *sk = sock->sk;
642 struct alg_sock *ask = alg_sk(sk);
643 struct sock *psk = ask->parent;
644 struct alg_sock *pask = alg_sk(psk);
645 struct skcipher_ctx *ctx = ask->private;
646 struct skcipher_tfm *skc = pask->private;
647 struct crypto_skcipher *tfm = skc->skcipher;
648 unsigned bs = crypto_skcipher_blocksize(tfm);
649 struct skcipher_sg_list *sgl;
650 struct scatterlist *sg;
656 while (msg_data_left(msg)) {
658 err = skcipher_wait_for_data(sk, flags);
663 used = min_t(unsigned long, ctx->used, msg_data_left(msg));
665 used = af_alg_make_sg(&ctx->rsgl, &msg->msg_iter, used);
670 if (ctx->more || used < ctx->used)
677 sgl = list_first_entry(&ctx->tsgl,
678 struct skcipher_sg_list, list);
684 skcipher_request_set_crypt(&ctx->req, sg, ctx->rsgl.sg, used,
687 err = af_alg_wait_for_completion(
689 crypto_skcipher_encrypt(&ctx->req) :
690 crypto_skcipher_decrypt(&ctx->req),
694 af_alg_free_sg(&ctx->rsgl);
700 skcipher_pull_sgl(sk, used, 1);
701 iov_iter_advance(&msg->msg_iter, used);
707 skcipher_wmem_wakeup(sk);
710 return copied ?: err;
713 static int skcipher_recvmsg(struct socket *sock, struct msghdr *msg,
714 size_t ignored, int flags)
716 return (msg->msg_iocb && !is_sync_kiocb(msg->msg_iocb)) ?
717 skcipher_recvmsg_async(sock, msg, flags) :
718 skcipher_recvmsg_sync(sock, msg, flags);
721 static unsigned int skcipher_poll(struct file *file, struct socket *sock,
724 struct sock *sk = sock->sk;
725 struct alg_sock *ask = alg_sk(sk);
726 struct skcipher_ctx *ctx = ask->private;
729 sock_poll_wait(file, sk_sleep(sk), wait);
733 mask |= POLLIN | POLLRDNORM;
735 if (skcipher_writable(sk))
736 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
741 static struct proto_ops algif_skcipher_ops = {
744 .connect = sock_no_connect,
745 .socketpair = sock_no_socketpair,
746 .getname = sock_no_getname,
747 .ioctl = sock_no_ioctl,
748 .listen = sock_no_listen,
749 .shutdown = sock_no_shutdown,
750 .getsockopt = sock_no_getsockopt,
751 .mmap = sock_no_mmap,
752 .bind = sock_no_bind,
753 .accept = sock_no_accept,
754 .setsockopt = sock_no_setsockopt,
756 .release = af_alg_release,
757 .sendmsg = skcipher_sendmsg,
758 .sendpage = skcipher_sendpage,
759 .recvmsg = skcipher_recvmsg,
760 .poll = skcipher_poll,
763 static int skcipher_check_key(struct socket *sock)
767 struct alg_sock *pask;
768 struct skcipher_tfm *tfm;
769 struct sock *sk = sock->sk;
770 struct alg_sock *ask = alg_sk(sk);
777 pask = alg_sk(ask->parent);
781 lock_sock_nested(psk, SINGLE_DEPTH_NESTING);
801 static int skcipher_sendmsg_nokey(struct socket *sock, struct msghdr *msg,
806 err = skcipher_check_key(sock);
810 return skcipher_sendmsg(sock, msg, size);
813 static ssize_t skcipher_sendpage_nokey(struct socket *sock, struct page *page,
814 int offset, size_t size, int flags)
818 err = skcipher_check_key(sock);
822 return skcipher_sendpage(sock, page, offset, size, flags);
825 static int skcipher_recvmsg_nokey(struct socket *sock, struct msghdr *msg,
826 size_t ignored, int flags)
830 err = skcipher_check_key(sock);
834 return skcipher_recvmsg(sock, msg, ignored, flags);
837 static struct proto_ops algif_skcipher_ops_nokey = {
840 .connect = sock_no_connect,
841 .socketpair = sock_no_socketpair,
842 .getname = sock_no_getname,
843 .ioctl = sock_no_ioctl,
844 .listen = sock_no_listen,
845 .shutdown = sock_no_shutdown,
846 .getsockopt = sock_no_getsockopt,
847 .mmap = sock_no_mmap,
848 .bind = sock_no_bind,
849 .accept = sock_no_accept,
850 .setsockopt = sock_no_setsockopt,
852 .release = af_alg_release,
853 .sendmsg = skcipher_sendmsg_nokey,
854 .sendpage = skcipher_sendpage_nokey,
855 .recvmsg = skcipher_recvmsg_nokey,
856 .poll = skcipher_poll,
859 static void *skcipher_bind(const char *name, u32 type, u32 mask)
861 struct skcipher_tfm *tfm;
862 struct crypto_skcipher *skcipher;
864 tfm = kzalloc(sizeof(*tfm), GFP_KERNEL);
866 return ERR_PTR(-ENOMEM);
868 skcipher = crypto_alloc_skcipher(name, type, mask);
869 if (IS_ERR(skcipher)) {
871 return ERR_CAST(skcipher);
874 tfm->skcipher = skcipher;
879 static void skcipher_release(void *private)
881 struct skcipher_tfm *tfm = private;
883 crypto_free_skcipher(tfm->skcipher);
887 static int skcipher_setkey(void *private, const u8 *key, unsigned int keylen)
889 struct skcipher_tfm *tfm = private;
892 err = crypto_skcipher_setkey(tfm->skcipher, key, keylen);
898 static void skcipher_wait(struct sock *sk)
900 struct alg_sock *ask = alg_sk(sk);
901 struct skcipher_ctx *ctx = ask->private;
904 while (atomic_read(&ctx->inflight) && ctr++ < 100)
908 static void skcipher_sock_destruct(struct sock *sk)
910 struct alg_sock *ask = alg_sk(sk);
911 struct skcipher_ctx *ctx = ask->private;
912 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(&ctx->req);
914 if (atomic_read(&ctx->inflight))
917 skcipher_free_sgl(sk);
918 sock_kzfree_s(sk, ctx->iv, crypto_skcipher_ivsize(tfm));
919 sock_kfree_s(sk, ctx, ctx->len);
920 af_alg_release_parent(sk);
923 static int skcipher_accept_parent_nokey(void *private, struct sock *sk)
925 struct skcipher_ctx *ctx;
926 struct alg_sock *ask = alg_sk(sk);
927 struct skcipher_tfm *tfm = private;
928 struct crypto_skcipher *skcipher = tfm->skcipher;
929 unsigned int len = sizeof(*ctx) + crypto_skcipher_reqsize(skcipher);
931 ctx = sock_kmalloc(sk, len, GFP_KERNEL);
935 ctx->iv = sock_kmalloc(sk, crypto_skcipher_ivsize(skcipher),
938 sock_kfree_s(sk, ctx, len);
942 memset(ctx->iv, 0, crypto_skcipher_ivsize(skcipher));
944 INIT_LIST_HEAD(&ctx->tsgl);
950 atomic_set(&ctx->inflight, 0);
951 af_alg_init_completion(&ctx->completion);
955 skcipher_request_set_tfm(&ctx->req, skcipher);
956 skcipher_request_set_callback(&ctx->req, CRYPTO_TFM_REQ_MAY_SLEEP |
957 CRYPTO_TFM_REQ_MAY_BACKLOG,
958 af_alg_complete, &ctx->completion);
960 sk->sk_destruct = skcipher_sock_destruct;
965 static int skcipher_accept_parent(void *private, struct sock *sk)
967 struct skcipher_tfm *tfm = private;
969 if (!tfm->has_key && crypto_skcipher_has_setkey(tfm->skcipher))
972 return skcipher_accept_parent_nokey(private, sk);
975 static const struct af_alg_type algif_type_skcipher = {
976 .bind = skcipher_bind,
977 .release = skcipher_release,
978 .setkey = skcipher_setkey,
979 .accept = skcipher_accept_parent,
980 .accept_nokey = skcipher_accept_parent_nokey,
981 .ops = &algif_skcipher_ops,
982 .ops_nokey = &algif_skcipher_ops_nokey,
987 static int __init algif_skcipher_init(void)
989 return af_alg_register_type(&algif_type_skcipher);
992 static void __exit algif_skcipher_exit(void)
994 int err = af_alg_unregister_type(&algif_type_skcipher);
998 module_init(algif_skcipher_init);
999 module_exit(algif_skcipher_exit);
1000 MODULE_LICENSE("GPL");