scsi: ufs: fix race between clock gating and devfreq scaling work
[linux/fpc-iii.git] / crypto / algif_aead.c
blob6c11537ca404673fb576c615786e8aa009d06015
1 /*
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)
13 * any later version.
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/mm.h>
23 #include <linux/module.h>
24 #include <linux/net.h>
25 #include <net/sock.h>
27 struct aead_sg_list {
28 unsigned int cur;
29 struct scatterlist sg[ALG_MAX_PAGES];
32 struct aead_async_rsgl {
33 struct af_alg_sgl sgl;
34 struct list_head list;
37 struct aead_async_req {
38 struct scatterlist *tsgl;
39 struct aead_async_rsgl first_rsgl;
40 struct list_head list;
41 struct kiocb *iocb;
42 struct sock *sk;
43 unsigned int tsgls;
44 char iv[];
47 struct aead_tfm {
48 struct crypto_aead *aead;
49 bool has_key;
52 struct aead_ctx {
53 struct aead_sg_list tsgl;
54 struct aead_async_rsgl first_rsgl;
55 struct list_head list;
57 void *iv;
59 struct af_alg_completion completion;
61 unsigned long used;
63 unsigned int len;
64 bool more;
65 bool merge;
66 bool enc;
68 size_t aead_assoclen;
69 struct aead_request aead_req;
72 static inline int aead_sndbuf(struct sock *sk)
74 struct alg_sock *ask = alg_sk(sk);
75 struct aead_ctx *ctx = ask->private;
77 return max_t(int, max_t(int, sk->sk_sndbuf & PAGE_MASK, PAGE_SIZE) -
78 ctx->used, 0);
81 static inline bool aead_writable(struct sock *sk)
83 return PAGE_SIZE <= aead_sndbuf(sk);
86 static inline bool aead_sufficient_data(struct aead_ctx *ctx)
88 unsigned as = crypto_aead_authsize(crypto_aead_reqtfm(&ctx->aead_req));
91 * The minimum amount of memory needed for an AEAD cipher is
92 * the AAD and in case of decryption the tag.
94 return ctx->used >= ctx->aead_assoclen + (ctx->enc ? 0 : as);
97 static void aead_reset_ctx(struct aead_ctx *ctx)
99 struct aead_sg_list *sgl = &ctx->tsgl;
101 sg_init_table(sgl->sg, ALG_MAX_PAGES);
102 sgl->cur = 0;
103 ctx->used = 0;
104 ctx->more = 0;
105 ctx->merge = 0;
108 static void aead_put_sgl(struct sock *sk)
110 struct alg_sock *ask = alg_sk(sk);
111 struct aead_ctx *ctx = ask->private;
112 struct aead_sg_list *sgl = &ctx->tsgl;
113 struct scatterlist *sg = sgl->sg;
114 unsigned int i;
116 for (i = 0; i < sgl->cur; i++) {
117 if (!sg_page(sg + i))
118 continue;
120 put_page(sg_page(sg + i));
121 sg_assign_page(sg + i, NULL);
123 aead_reset_ctx(ctx);
126 static void aead_wmem_wakeup(struct sock *sk)
128 struct socket_wq *wq;
130 if (!aead_writable(sk))
131 return;
133 rcu_read_lock();
134 wq = rcu_dereference(sk->sk_wq);
135 if (skwq_has_sleeper(wq))
136 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
137 POLLRDNORM |
138 POLLRDBAND);
139 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
140 rcu_read_unlock();
143 static int aead_wait_for_data(struct sock *sk, unsigned flags)
145 struct alg_sock *ask = alg_sk(sk);
146 struct aead_ctx *ctx = ask->private;
147 long timeout;
148 DEFINE_WAIT(wait);
149 int err = -ERESTARTSYS;
151 if (flags & MSG_DONTWAIT)
152 return -EAGAIN;
154 sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
156 for (;;) {
157 if (signal_pending(current))
158 break;
159 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
160 timeout = MAX_SCHEDULE_TIMEOUT;
161 if (sk_wait_event(sk, &timeout, !ctx->more)) {
162 err = 0;
163 break;
166 finish_wait(sk_sleep(sk), &wait);
168 sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
170 return err;
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;
179 if (ctx->more)
180 return;
181 if (!ctx->used)
182 return;
184 rcu_read_lock();
185 wq = rcu_dereference(sk->sk_wq);
186 if (skwq_has_sleeper(wq))
187 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
188 POLLRDNORM |
189 POLLRDBAND);
190 sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
191 rcu_read_unlock();
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;
199 unsigned ivsize =
200 crypto_aead_ivsize(crypto_aead_reqtfm(&ctx->aead_req));
201 struct aead_sg_list *sgl = &ctx->tsgl;
202 struct af_alg_control con = {};
203 long copied = 0;
204 bool enc = 0;
205 bool init = 0;
206 int err = -EINVAL;
208 if (msg->msg_controllen) {
209 err = af_alg_cmsg_send(msg, &con);
210 if (err)
211 return err;
213 init = 1;
214 switch (con.op) {
215 case ALG_OP_ENCRYPT:
216 enc = 1;
217 break;
218 case ALG_OP_DECRYPT:
219 enc = 0;
220 break;
221 default:
222 return -EINVAL;
225 if (con.iv && con.iv->ivlen != ivsize)
226 return -EINVAL;
229 lock_sock(sk);
230 if (!ctx->more && ctx->used)
231 goto unlock;
233 if (init) {
234 ctx->enc = enc;
235 if (con.iv)
236 memcpy(ctx->iv, con.iv->iv, ivsize);
238 ctx->aead_assoclen = con.aead_assoclen;
241 while (size) {
242 size_t len = size;
243 struct scatterlist *sg = NULL;
245 /* use the existing memory in an allocated page */
246 if (ctx->merge) {
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,
252 msg, len);
253 if (err)
254 goto unlock;
256 sg->length += len;
257 ctx->merge = (sg->offset + sg->length) &
258 (PAGE_SIZE - 1);
260 ctx->used += len;
261 copied += len;
262 size -= len;
263 continue;
266 if (!aead_writable(sk)) {
267 /* user space sent too much data */
268 aead_put_sgl(sk);
269 err = -EMSGSIZE;
270 goto unlock;
273 /* allocate a new page */
274 len = min_t(unsigned long, size, aead_sndbuf(sk));
275 while (len) {
276 size_t plen = 0;
278 if (sgl->cur >= ALG_MAX_PAGES) {
279 aead_put_sgl(sk);
280 err = -E2BIG;
281 goto unlock;
284 sg = sgl->sg + sgl->cur;
285 plen = min_t(size_t, len, PAGE_SIZE);
287 sg_assign_page(sg, alloc_page(GFP_KERNEL));
288 err = -ENOMEM;
289 if (!sg_page(sg))
290 goto unlock;
292 err = memcpy_from_msg(page_address(sg_page(sg)),
293 msg, plen);
294 if (err) {
295 __free_page(sg_page(sg));
296 sg_assign_page(sg, NULL);
297 goto unlock;
300 sg->offset = 0;
301 sg->length = plen;
302 len -= plen;
303 ctx->used += plen;
304 copied += plen;
305 sgl->cur++;
306 size -= plen;
307 ctx->merge = plen & (PAGE_SIZE - 1);
311 err = 0;
313 ctx->more = msg->msg_flags & MSG_MORE;
314 if (!ctx->more && !aead_sufficient_data(ctx)) {
315 aead_put_sgl(sk);
316 err = -EMSGSIZE;
319 unlock:
320 aead_data_wakeup(sk);
321 release_sock(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;
333 int err = -EINVAL;
335 if (flags & MSG_SENDPAGE_NOTLAST)
336 flags |= MSG_MORE;
338 if (sgl->cur >= ALG_MAX_PAGES)
339 return -E2BIG;
341 lock_sock(sk);
342 if (!ctx->more && ctx->used)
343 goto unlock;
345 if (!size)
346 goto done;
348 if (!aead_writable(sk)) {
349 /* user space sent too much data */
350 aead_put_sgl(sk);
351 err = -EMSGSIZE;
352 goto unlock;
355 ctx->merge = 0;
357 get_page(page);
358 sg_set_page(sgl->sg + sgl->cur, page, size, offset);
359 sgl->cur++;
360 ctx->used += size;
362 err = 0;
364 done:
365 ctx->more = flags & MSG_MORE;
366 if (!ctx->more && !aead_sufficient_data(ctx)) {
367 aead_put_sgl(sk);
368 err = -EMSGSIZE;
371 unlock:
372 aead_data_wakeup(sk);
373 release_sock(sk);
375 return err ?: size;
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);
408 __sock_put(sk);
409 iocb->ki_complete(iocb, err, err);
412 static int aead_recvmsg_async(struct socket *sock, struct msghdr *msg,
413 int flags)
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);
425 int err = -ENOMEM;
426 unsigned long used;
427 size_t outlen = 0;
428 size_t usedpages = 0;
430 lock_sock(sk);
431 if (ctx->more) {
432 err = aead_wait_for_data(sk, flags);
433 if (err)
434 goto unlock;
437 if (!aead_sufficient_data(ctx))
438 goto unlock;
440 used = ctx->used;
441 if (ctx->enc)
442 outlen = used + as;
443 else
444 outlen = used - as;
446 req = sock_kmalloc(sk, reqlen, GFP_KERNEL);
447 if (unlikely(!req))
448 goto unlock;
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;
454 areq->sk = sk;
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,
459 aead_async_cb, req);
460 used -= ctx->aead_assoclen;
462 /* take over all tx sgls from ctx */
463 areq->tsgl = sock_kmalloc(sk, sizeof(*areq->tsgl) * sgl->cur,
464 GFP_KERNEL);
465 if (unlikely(!areq->tsgl))
466 goto free;
468 sg_init_table(areq->tsgl, sgl->cur);
469 for (i = 0; i < sgl->cur; i++)
470 sg_set_page(&areq->tsgl[i], sg_page(&sgl->sg[i]),
471 sgl->sg[i].length, sgl->sg[i].offset);
473 areq->tsgls = sgl->cur;
475 /* create rx sgls */
476 while (outlen > usedpages && iov_iter_count(&msg->msg_iter)) {
477 size_t seglen = min_t(size_t, iov_iter_count(&msg->msg_iter),
478 (outlen - usedpages));
480 if (list_empty(&areq->list)) {
481 rsgl = &areq->first_rsgl;
483 } else {
484 rsgl = sock_kmalloc(sk, sizeof(*rsgl), GFP_KERNEL);
485 if (unlikely(!rsgl)) {
486 err = -ENOMEM;
487 goto free;
490 rsgl->sgl.npages = 0;
491 list_add_tail(&rsgl->list, &areq->list);
493 /* make one iovec available as scatterlist */
494 err = af_alg_make_sg(&rsgl->sgl, &msg->msg_iter, seglen);
495 if (err < 0)
496 goto free;
498 usedpages += err;
500 /* chain the new scatterlist with previous one */
501 if (last_rsgl)
502 af_alg_link_sg(&last_rsgl->sgl, &rsgl->sgl);
504 last_rsgl = rsgl;
506 iov_iter_advance(&msg->msg_iter, err);
509 /* ensure output buffer is sufficiently large */
510 if (usedpages < outlen) {
511 err = -EINVAL;
512 goto unlock;
515 aead_request_set_crypt(req, areq->tsgl, areq->first_rsgl.sgl.sg, used,
516 areq->iv);
517 err = ctx->enc ? crypto_aead_encrypt(req) : crypto_aead_decrypt(req);
518 if (err) {
519 if (err == -EINPROGRESS) {
520 sock_hold(sk);
521 err = -EIOCBQUEUED;
522 aead_reset_ctx(ctx);
523 goto unlock;
524 } else if (err == -EBADMSG) {
525 aead_put_sgl(sk);
527 goto free;
529 aead_put_sgl(sk);
531 free:
532 list_for_each_entry(rsgl, &areq->list, list) {
533 af_alg_free_sg(&rsgl->sgl);
534 if (rsgl != &areq->first_rsgl)
535 sock_kfree_s(sk, rsgl, sizeof(*rsgl));
537 if (areq->tsgl)
538 sock_kfree_s(sk, areq->tsgl, sizeof(*areq->tsgl) * areq->tsgls);
539 if (req)
540 sock_kfree_s(sk, req, reqlen);
541 unlock:
542 aead_wmem_wakeup(sk);
543 release_sock(sk);
544 return err ? err : outlen;
547 static int aead_recvmsg_sync(struct socket *sock, struct msghdr *msg, int flags)
549 struct sock *sk = sock->sk;
550 struct alg_sock *ask = alg_sk(sk);
551 struct aead_ctx *ctx = ask->private;
552 unsigned as = crypto_aead_authsize(crypto_aead_reqtfm(&ctx->aead_req));
553 struct aead_sg_list *sgl = &ctx->tsgl;
554 struct aead_async_rsgl *last_rsgl = NULL;
555 struct aead_async_rsgl *rsgl, *tmp;
556 int err = -EINVAL;
557 unsigned long used = 0;
558 size_t outlen = 0;
559 size_t usedpages = 0;
561 lock_sock(sk);
564 * AEAD memory structure: For encryption, the tag is appended to the
565 * ciphertext which implies that the memory allocated for the ciphertext
566 * must be increased by the tag length. For decryption, the tag
567 * is expected to be concatenated to the ciphertext. The plaintext
568 * therefore has a memory size of the ciphertext minus the tag length.
570 * The memory structure for cipher operation has the following
571 * structure:
572 * AEAD encryption input: assoc data || plaintext
573 * AEAD encryption output: cipherntext || auth tag
574 * AEAD decryption input: assoc data || ciphertext || auth tag
575 * AEAD decryption output: plaintext
578 if (ctx->more) {
579 err = aead_wait_for_data(sk, flags);
580 if (err)
581 goto unlock;
584 /* data length provided by caller via sendmsg/sendpage */
585 used = ctx->used;
588 * Make sure sufficient data is present -- note, the same check is
589 * is also present in sendmsg/sendpage. The checks in sendpage/sendmsg
590 * shall provide an information to the data sender that something is
591 * wrong, but they are irrelevant to maintain the kernel integrity.
592 * We need this check here too in case user space decides to not honor
593 * the error message in sendmsg/sendpage and still call recvmsg. This
594 * check here protects the kernel integrity.
596 if (!aead_sufficient_data(ctx))
597 goto unlock;
600 * Calculate the minimum output buffer size holding the result of the
601 * cipher operation. When encrypting data, the receiving buffer is
602 * larger by the tag length compared to the input buffer as the
603 * encryption operation generates the tag. For decryption, the input
604 * buffer provides the tag which is consumed resulting in only the
605 * plaintext without a buffer for the tag returned to the caller.
607 if (ctx->enc)
608 outlen = used + as;
609 else
610 outlen = used - as;
613 * The cipher operation input data is reduced by the associated data
614 * length as this data is processed separately later on.
616 used -= ctx->aead_assoclen;
618 /* convert iovecs of output buffers into scatterlists */
619 while (outlen > usedpages && iov_iter_count(&msg->msg_iter)) {
620 size_t seglen = min_t(size_t, iov_iter_count(&msg->msg_iter),
621 (outlen - usedpages));
623 if (list_empty(&ctx->list)) {
624 rsgl = &ctx->first_rsgl;
625 } else {
626 rsgl = sock_kmalloc(sk, sizeof(*rsgl), GFP_KERNEL);
627 if (unlikely(!rsgl)) {
628 err = -ENOMEM;
629 goto unlock;
632 rsgl->sgl.npages = 0;
633 list_add_tail(&rsgl->list, &ctx->list);
635 /* make one iovec available as scatterlist */
636 err = af_alg_make_sg(&rsgl->sgl, &msg->msg_iter, seglen);
637 if (err < 0)
638 goto unlock;
639 usedpages += err;
640 /* chain the new scatterlist with previous one */
641 if (last_rsgl)
642 af_alg_link_sg(&last_rsgl->sgl, &rsgl->sgl);
644 last_rsgl = rsgl;
646 iov_iter_advance(&msg->msg_iter, err);
649 /* ensure output buffer is sufficiently large */
650 if (usedpages < outlen) {
651 err = -EINVAL;
652 goto unlock;
655 sg_mark_end(sgl->sg + sgl->cur - 1);
656 aead_request_set_crypt(&ctx->aead_req, sgl->sg, ctx->first_rsgl.sgl.sg,
657 used, ctx->iv);
658 aead_request_set_ad(&ctx->aead_req, ctx->aead_assoclen);
660 err = af_alg_wait_for_completion(ctx->enc ?
661 crypto_aead_encrypt(&ctx->aead_req) :
662 crypto_aead_decrypt(&ctx->aead_req),
663 &ctx->completion);
665 if (err) {
666 /* EBADMSG implies a valid cipher operation took place */
667 if (err == -EBADMSG)
668 aead_put_sgl(sk);
670 goto unlock;
673 aead_put_sgl(sk);
674 err = 0;
676 unlock:
677 list_for_each_entry_safe(rsgl, tmp, &ctx->list, list) {
678 af_alg_free_sg(&rsgl->sgl);
679 list_del(&rsgl->list);
680 if (rsgl != &ctx->first_rsgl)
681 sock_kfree_s(sk, rsgl, sizeof(*rsgl));
683 INIT_LIST_HEAD(&ctx->list);
684 aead_wmem_wakeup(sk);
685 release_sock(sk);
687 return err ? err : outlen;
690 static int aead_recvmsg(struct socket *sock, struct msghdr *msg, size_t ignored,
691 int flags)
693 return (msg->msg_iocb && !is_sync_kiocb(msg->msg_iocb)) ?
694 aead_recvmsg_async(sock, msg, flags) :
695 aead_recvmsg_sync(sock, msg, flags);
698 static unsigned int aead_poll(struct file *file, struct socket *sock,
699 poll_table *wait)
701 struct sock *sk = sock->sk;
702 struct alg_sock *ask = alg_sk(sk);
703 struct aead_ctx *ctx = ask->private;
704 unsigned int mask;
706 sock_poll_wait(file, sk_sleep(sk), wait);
707 mask = 0;
709 if (!ctx->more)
710 mask |= POLLIN | POLLRDNORM;
712 if (aead_writable(sk))
713 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
715 return mask;
718 static struct proto_ops algif_aead_ops = {
719 .family = PF_ALG,
721 .connect = sock_no_connect,
722 .socketpair = sock_no_socketpair,
723 .getname = sock_no_getname,
724 .ioctl = sock_no_ioctl,
725 .listen = sock_no_listen,
726 .shutdown = sock_no_shutdown,
727 .getsockopt = sock_no_getsockopt,
728 .mmap = sock_no_mmap,
729 .bind = sock_no_bind,
730 .accept = sock_no_accept,
731 .setsockopt = sock_no_setsockopt,
733 .release = af_alg_release,
734 .sendmsg = aead_sendmsg,
735 .sendpage = aead_sendpage,
736 .recvmsg = aead_recvmsg,
737 .poll = aead_poll,
740 static int aead_check_key(struct socket *sock)
742 int err = 0;
743 struct sock *psk;
744 struct alg_sock *pask;
745 struct aead_tfm *tfm;
746 struct sock *sk = sock->sk;
747 struct alg_sock *ask = alg_sk(sk);
749 lock_sock(sk);
750 if (ask->refcnt)
751 goto unlock_child;
753 psk = ask->parent;
754 pask = alg_sk(ask->parent);
755 tfm = pask->private;
757 err = -ENOKEY;
758 lock_sock_nested(psk, SINGLE_DEPTH_NESTING);
759 if (!tfm->has_key)
760 goto unlock;
762 if (!pask->refcnt++)
763 sock_hold(psk);
765 ask->refcnt = 1;
766 sock_put(psk);
768 err = 0;
770 unlock:
771 release_sock(psk);
772 unlock_child:
773 release_sock(sk);
775 return err;
778 static int aead_sendmsg_nokey(struct socket *sock, struct msghdr *msg,
779 size_t size)
781 int err;
783 err = aead_check_key(sock);
784 if (err)
785 return err;
787 return aead_sendmsg(sock, msg, size);
790 static ssize_t aead_sendpage_nokey(struct socket *sock, struct page *page,
791 int offset, size_t size, int flags)
793 int err;
795 err = aead_check_key(sock);
796 if (err)
797 return err;
799 return aead_sendpage(sock, page, offset, size, flags);
802 static int aead_recvmsg_nokey(struct socket *sock, struct msghdr *msg,
803 size_t ignored, int flags)
805 int err;
807 err = aead_check_key(sock);
808 if (err)
809 return err;
811 return aead_recvmsg(sock, msg, ignored, flags);
814 static struct proto_ops algif_aead_ops_nokey = {
815 .family = PF_ALG,
817 .connect = sock_no_connect,
818 .socketpair = sock_no_socketpair,
819 .getname = sock_no_getname,
820 .ioctl = sock_no_ioctl,
821 .listen = sock_no_listen,
822 .shutdown = sock_no_shutdown,
823 .getsockopt = sock_no_getsockopt,
824 .mmap = sock_no_mmap,
825 .bind = sock_no_bind,
826 .accept = sock_no_accept,
827 .setsockopt = sock_no_setsockopt,
829 .release = af_alg_release,
830 .sendmsg = aead_sendmsg_nokey,
831 .sendpage = aead_sendpage_nokey,
832 .recvmsg = aead_recvmsg_nokey,
833 .poll = aead_poll,
836 static void *aead_bind(const char *name, u32 type, u32 mask)
838 struct aead_tfm *tfm;
839 struct crypto_aead *aead;
841 tfm = kzalloc(sizeof(*tfm), GFP_KERNEL);
842 if (!tfm)
843 return ERR_PTR(-ENOMEM);
845 aead = crypto_alloc_aead(name, type, mask);
846 if (IS_ERR(aead)) {
847 kfree(tfm);
848 return ERR_CAST(aead);
851 tfm->aead = aead;
853 return tfm;
856 static void aead_release(void *private)
858 struct aead_tfm *tfm = private;
860 crypto_free_aead(tfm->aead);
861 kfree(tfm);
864 static int aead_setauthsize(void *private, unsigned int authsize)
866 struct aead_tfm *tfm = private;
868 return crypto_aead_setauthsize(tfm->aead, authsize);
871 static int aead_setkey(void *private, const u8 *key, unsigned int keylen)
873 struct aead_tfm *tfm = private;
874 int err;
876 err = crypto_aead_setkey(tfm->aead, key, keylen);
877 tfm->has_key = !err;
879 return err;
882 static void aead_sock_destruct(struct sock *sk)
884 struct alg_sock *ask = alg_sk(sk);
885 struct aead_ctx *ctx = ask->private;
886 unsigned int ivlen = crypto_aead_ivsize(
887 crypto_aead_reqtfm(&ctx->aead_req));
889 WARN_ON(atomic_read(&sk->sk_refcnt) != 0);
890 aead_put_sgl(sk);
891 sock_kzfree_s(sk, ctx->iv, ivlen);
892 sock_kfree_s(sk, ctx, ctx->len);
893 af_alg_release_parent(sk);
896 static int aead_accept_parent_nokey(void *private, struct sock *sk)
898 struct aead_ctx *ctx;
899 struct alg_sock *ask = alg_sk(sk);
900 struct aead_tfm *tfm = private;
901 struct crypto_aead *aead = tfm->aead;
902 unsigned int len = sizeof(*ctx) + crypto_aead_reqsize(aead);
903 unsigned int ivlen = crypto_aead_ivsize(aead);
905 ctx = sock_kmalloc(sk, len, GFP_KERNEL);
906 if (!ctx)
907 return -ENOMEM;
908 memset(ctx, 0, len);
910 ctx->iv = sock_kmalloc(sk, ivlen, GFP_KERNEL);
911 if (!ctx->iv) {
912 sock_kfree_s(sk, ctx, len);
913 return -ENOMEM;
915 memset(ctx->iv, 0, ivlen);
917 ctx->len = len;
918 ctx->used = 0;
919 ctx->more = 0;
920 ctx->merge = 0;
921 ctx->enc = 0;
922 ctx->tsgl.cur = 0;
923 ctx->aead_assoclen = 0;
924 af_alg_init_completion(&ctx->completion);
925 sg_init_table(ctx->tsgl.sg, ALG_MAX_PAGES);
926 INIT_LIST_HEAD(&ctx->list);
928 ask->private = ctx;
930 aead_request_set_tfm(&ctx->aead_req, aead);
931 aead_request_set_callback(&ctx->aead_req, CRYPTO_TFM_REQ_MAY_BACKLOG,
932 af_alg_complete, &ctx->completion);
934 sk->sk_destruct = aead_sock_destruct;
936 return 0;
939 static int aead_accept_parent(void *private, struct sock *sk)
941 struct aead_tfm *tfm = private;
943 if (!tfm->has_key)
944 return -ENOKEY;
946 return aead_accept_parent_nokey(private, sk);
949 static const struct af_alg_type algif_type_aead = {
950 .bind = aead_bind,
951 .release = aead_release,
952 .setkey = aead_setkey,
953 .setauthsize = aead_setauthsize,
954 .accept = aead_accept_parent,
955 .accept_nokey = aead_accept_parent_nokey,
956 .ops = &algif_aead_ops,
957 .ops_nokey = &algif_aead_ops_nokey,
958 .name = "aead",
959 .owner = THIS_MODULE
962 static int __init algif_aead_init(void)
964 return af_alg_register_type(&algif_type_aead);
967 static void __exit algif_aead_exit(void)
969 int err = af_alg_unregister_type(&algif_type_aead);
970 BUG_ON(err);
973 module_init(algif_aead_init);
974 module_exit(algif_aead_exit);
975 MODULE_LICENSE("GPL");
976 MODULE_AUTHOR("Stephan Mueller <smueller@chronox.de>");
977 MODULE_DESCRIPTION("AEAD kernel crypto API user space interface");