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>
23 #include <linux/module.h>
24 #include <linux/net.h>
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
;
47 struct aead_sg_list tsgl
;
48 struct aead_async_rsgl first_rsgl
;
49 struct list_head list
;
53 struct af_alg_completion completion
;
63 struct aead_request aead_req
;
66 static inline int aead_sndbuf(struct sock
*sk
)
68 struct alg_sock
*ask
= alg_sk(sk
);
69 struct aead_ctx
*ctx
= ask
->private;
71 return max_t(int, max_t(int, sk
->sk_sndbuf
& PAGE_MASK
, PAGE_SIZE
) -
75 static inline bool aead_writable(struct sock
*sk
)
77 return PAGE_SIZE
<= aead_sndbuf(sk
);
80 static inline bool aead_sufficient_data(struct aead_ctx
*ctx
)
82 unsigned as
= crypto_aead_authsize(crypto_aead_reqtfm(&ctx
->aead_req
));
85 * The minimum amount of memory needed for an AEAD cipher is
86 * the AAD and in case of decryption the tag.
88 return ctx
->used
>= ctx
->aead_assoclen
+ (ctx
->enc
? 0 : as
);
91 static void aead_reset_ctx(struct aead_ctx
*ctx
)
93 struct aead_sg_list
*sgl
= &ctx
->tsgl
;
95 sg_init_table(sgl
->sg
, ALG_MAX_PAGES
);
102 static void aead_put_sgl(struct sock
*sk
)
104 struct alg_sock
*ask
= alg_sk(sk
);
105 struct aead_ctx
*ctx
= ask
->private;
106 struct aead_sg_list
*sgl
= &ctx
->tsgl
;
107 struct scatterlist
*sg
= sgl
->sg
;
110 for (i
= 0; i
< sgl
->cur
; i
++) {
111 if (!sg_page(sg
+ i
))
114 put_page(sg_page(sg
+ i
));
115 sg_assign_page(sg
+ i
, NULL
);
120 static void aead_wmem_wakeup(struct sock
*sk
)
122 struct socket_wq
*wq
;
124 if (!aead_writable(sk
))
128 wq
= rcu_dereference(sk
->sk_wq
);
129 if (skwq_has_sleeper(wq
))
130 wake_up_interruptible_sync_poll(&wq
->wait
, POLLIN
|
133 sk_wake_async(sk
, SOCK_WAKE_WAITD
, POLL_IN
);
137 static int aead_wait_for_data(struct sock
*sk
, unsigned flags
)
139 DEFINE_WAIT_FUNC(wait
, woken_wake_function
);
140 struct alg_sock
*ask
= alg_sk(sk
);
141 struct aead_ctx
*ctx
= ask
->private;
143 int err
= -ERESTARTSYS
;
145 if (flags
& MSG_DONTWAIT
)
148 sk_set_bit(SOCKWQ_ASYNC_WAITDATA
, sk
);
149 add_wait_queue(sk_sleep(sk
), &wait
);
151 if (signal_pending(current
))
153 timeout
= MAX_SCHEDULE_TIMEOUT
;
154 if (sk_wait_event(sk
, &timeout
, !ctx
->more
, &wait
)) {
159 remove_wait_queue(sk_sleep(sk
), &wait
);
161 sk_clear_bit(SOCKWQ_ASYNC_WAITDATA
, sk
);
166 static void aead_data_wakeup(struct sock
*sk
)
168 struct alg_sock
*ask
= alg_sk(sk
);
169 struct aead_ctx
*ctx
= ask
->private;
170 struct socket_wq
*wq
;
178 wq
= rcu_dereference(sk
->sk_wq
);
179 if (skwq_has_sleeper(wq
))
180 wake_up_interruptible_sync_poll(&wq
->wait
, POLLOUT
|
183 sk_wake_async(sk
, SOCK_WAKE_SPACE
, POLL_OUT
);
187 static int aead_sendmsg(struct socket
*sock
, struct msghdr
*msg
, size_t size
)
189 struct sock
*sk
= sock
->sk
;
190 struct alg_sock
*ask
= alg_sk(sk
);
191 struct aead_ctx
*ctx
= ask
->private;
193 crypto_aead_ivsize(crypto_aead_reqtfm(&ctx
->aead_req
));
194 struct aead_sg_list
*sgl
= &ctx
->tsgl
;
195 struct af_alg_control con
= {};
201 if (msg
->msg_controllen
) {
202 err
= af_alg_cmsg_send(msg
, &con
);
218 if (con
.iv
&& con
.iv
->ivlen
!= ivsize
)
223 if (!ctx
->more
&& ctx
->used
)
229 memcpy(ctx
->iv
, con
.iv
->iv
, ivsize
);
231 ctx
->aead_assoclen
= con
.aead_assoclen
;
236 struct scatterlist
*sg
= NULL
;
238 /* use the existing memory in an allocated page */
240 sg
= sgl
->sg
+ sgl
->cur
- 1;
241 len
= min_t(unsigned long, len
,
242 PAGE_SIZE
- sg
->offset
- sg
->length
);
243 err
= memcpy_from_msg(page_address(sg_page(sg
)) +
244 sg
->offset
+ sg
->length
,
250 ctx
->merge
= (sg
->offset
+ sg
->length
) &
259 if (!aead_writable(sk
)) {
260 /* user space sent too much data */
266 /* allocate a new page */
267 len
= min_t(unsigned long, size
, aead_sndbuf(sk
));
271 if (sgl
->cur
>= ALG_MAX_PAGES
) {
277 sg
= sgl
->sg
+ sgl
->cur
;
278 plen
= min_t(size_t, len
, PAGE_SIZE
);
280 sg_assign_page(sg
, alloc_page(GFP_KERNEL
));
285 err
= memcpy_from_msg(page_address(sg_page(sg
)),
288 __free_page(sg_page(sg
));
289 sg_assign_page(sg
, NULL
);
300 ctx
->merge
= plen
& (PAGE_SIZE
- 1);
306 ctx
->more
= msg
->msg_flags
& MSG_MORE
;
307 if (!ctx
->more
&& !aead_sufficient_data(ctx
)) {
313 aead_data_wakeup(sk
);
316 return err
?: copied
;
319 static ssize_t
aead_sendpage(struct socket
*sock
, struct page
*page
,
320 int offset
, size_t size
, int flags
)
322 struct sock
*sk
= sock
->sk
;
323 struct alg_sock
*ask
= alg_sk(sk
);
324 struct aead_ctx
*ctx
= ask
->private;
325 struct aead_sg_list
*sgl
= &ctx
->tsgl
;
328 if (flags
& MSG_SENDPAGE_NOTLAST
)
331 if (sgl
->cur
>= ALG_MAX_PAGES
)
335 if (!ctx
->more
&& ctx
->used
)
341 if (!aead_writable(sk
)) {
342 /* user space sent too much data */
351 sg_set_page(sgl
->sg
+ sgl
->cur
, page
, size
, offset
);
358 ctx
->more
= flags
& MSG_MORE
;
359 if (!ctx
->more
&& !aead_sufficient_data(ctx
)) {
365 aead_data_wakeup(sk
);
371 #define GET_ASYM_REQ(req, tfm) (struct aead_async_req *) \
372 ((char *)req + sizeof(struct aead_request) + \
373 crypto_aead_reqsize(tfm))
375 #define GET_REQ_SIZE(tfm) sizeof(struct aead_async_req) + \
376 crypto_aead_reqsize(tfm) + crypto_aead_ivsize(tfm) + \
377 sizeof(struct aead_request)
379 static void aead_async_cb(struct crypto_async_request
*_req
, int err
)
381 struct sock
*sk
= _req
->data
;
382 struct alg_sock
*ask
= alg_sk(sk
);
383 struct aead_ctx
*ctx
= ask
->private;
384 struct crypto_aead
*tfm
= crypto_aead_reqtfm(&ctx
->aead_req
);
385 struct aead_request
*req
= aead_request_cast(_req
);
386 struct aead_async_req
*areq
= GET_ASYM_REQ(req
, tfm
);
387 struct scatterlist
*sg
= areq
->tsgl
;
388 struct aead_async_rsgl
*rsgl
;
389 struct kiocb
*iocb
= areq
->iocb
;
390 unsigned int i
, reqlen
= GET_REQ_SIZE(tfm
);
392 list_for_each_entry(rsgl
, &areq
->list
, list
) {
393 af_alg_free_sg(&rsgl
->sgl
);
394 if (rsgl
!= &areq
->first_rsgl
)
395 sock_kfree_s(sk
, rsgl
, sizeof(*rsgl
));
398 for (i
= 0; i
< areq
->tsgls
; i
++)
399 put_page(sg_page(sg
+ i
));
401 sock_kfree_s(sk
, areq
->tsgl
, sizeof(*areq
->tsgl
) * areq
->tsgls
);
402 sock_kfree_s(sk
, req
, reqlen
);
404 iocb
->ki_complete(iocb
, err
, err
);
407 static int aead_recvmsg_async(struct socket
*sock
, struct msghdr
*msg
,
410 struct sock
*sk
= sock
->sk
;
411 struct alg_sock
*ask
= alg_sk(sk
);
412 struct aead_ctx
*ctx
= ask
->private;
413 struct crypto_aead
*tfm
= crypto_aead_reqtfm(&ctx
->aead_req
);
414 struct aead_async_req
*areq
;
415 struct aead_request
*req
= NULL
;
416 struct aead_sg_list
*sgl
= &ctx
->tsgl
;
417 struct aead_async_rsgl
*last_rsgl
= NULL
, *rsgl
;
418 unsigned int as
= crypto_aead_authsize(tfm
);
419 unsigned int i
, reqlen
= GET_REQ_SIZE(tfm
);
423 size_t usedpages
= 0;
427 err
= aead_wait_for_data(sk
, flags
);
432 if (!aead_sufficient_data(ctx
))
441 req
= sock_kmalloc(sk
, reqlen
, GFP_KERNEL
);
445 areq
= GET_ASYM_REQ(req
, tfm
);
446 memset(&areq
->first_rsgl
, '\0', sizeof(areq
->first_rsgl
));
447 INIT_LIST_HEAD(&areq
->list
);
448 areq
->iocb
= msg
->msg_iocb
;
449 memcpy(areq
->iv
, ctx
->iv
, crypto_aead_ivsize(tfm
));
450 aead_request_set_tfm(req
, tfm
);
451 aead_request_set_ad(req
, ctx
->aead_assoclen
);
452 aead_request_set_callback(req
, CRYPTO_TFM_REQ_MAY_BACKLOG
,
454 used
-= ctx
->aead_assoclen
;
456 /* take over all tx sgls from ctx */
457 areq
->tsgl
= sock_kmalloc(sk
,
458 sizeof(*areq
->tsgl
) * max_t(u32
, sgl
->cur
, 1),
460 if (unlikely(!areq
->tsgl
))
463 sg_init_table(areq
->tsgl
, max_t(u32
, sgl
->cur
, 1));
464 for (i
= 0; i
< sgl
->cur
; i
++)
465 sg_set_page(&areq
->tsgl
[i
], sg_page(&sgl
->sg
[i
]),
466 sgl
->sg
[i
].length
, sgl
->sg
[i
].offset
);
468 areq
->tsgls
= sgl
->cur
;
471 while (outlen
> usedpages
&& iov_iter_count(&msg
->msg_iter
)) {
472 size_t seglen
= min_t(size_t, iov_iter_count(&msg
->msg_iter
),
473 (outlen
- usedpages
));
475 if (list_empty(&areq
->list
)) {
476 rsgl
= &areq
->first_rsgl
;
479 rsgl
= sock_kmalloc(sk
, sizeof(*rsgl
), GFP_KERNEL
);
480 if (unlikely(!rsgl
)) {
485 rsgl
->sgl
.npages
= 0;
486 list_add_tail(&rsgl
->list
, &areq
->list
);
488 /* make one iovec available as scatterlist */
489 err
= af_alg_make_sg(&rsgl
->sgl
, &msg
->msg_iter
, seglen
);
495 /* chain the new scatterlist with previous one */
497 af_alg_link_sg(&last_rsgl
->sgl
, &rsgl
->sgl
);
501 iov_iter_advance(&msg
->msg_iter
, err
);
504 /* ensure output buffer is sufficiently large */
505 if (usedpages
< outlen
) {
510 aead_request_set_crypt(req
, areq
->tsgl
, areq
->first_rsgl
.sgl
.sg
, used
,
512 err
= ctx
->enc
? crypto_aead_encrypt(req
) : crypto_aead_decrypt(req
);
514 if (err
== -EINPROGRESS
) {
519 } else if (err
== -EBADMSG
) {
527 list_for_each_entry(rsgl
, &areq
->list
, list
) {
528 af_alg_free_sg(&rsgl
->sgl
);
529 if (rsgl
!= &areq
->first_rsgl
)
530 sock_kfree_s(sk
, rsgl
, sizeof(*rsgl
));
533 sock_kfree_s(sk
, areq
->tsgl
, sizeof(*areq
->tsgl
) * areq
->tsgls
);
535 sock_kfree_s(sk
, req
, reqlen
);
537 aead_wmem_wakeup(sk
);
539 return err
? err
: outlen
;
542 static int aead_recvmsg_sync(struct socket
*sock
, struct msghdr
*msg
, int flags
)
544 struct sock
*sk
= sock
->sk
;
545 struct alg_sock
*ask
= alg_sk(sk
);
546 struct aead_ctx
*ctx
= ask
->private;
547 unsigned as
= crypto_aead_authsize(crypto_aead_reqtfm(&ctx
->aead_req
));
548 struct aead_sg_list
*sgl
= &ctx
->tsgl
;
549 struct aead_async_rsgl
*last_rsgl
= NULL
;
550 struct aead_async_rsgl
*rsgl
, *tmp
;
552 unsigned long used
= 0;
554 size_t usedpages
= 0;
559 * Please see documentation of aead_request_set_crypt for the
560 * description of the AEAD memory structure expected from the caller.
564 err
= aead_wait_for_data(sk
, flags
);
569 /* data length provided by caller via sendmsg/sendpage */
573 * Make sure sufficient data is present -- note, the same check is
574 * is also present in sendmsg/sendpage. The checks in sendpage/sendmsg
575 * shall provide an information to the data sender that something is
576 * wrong, but they are irrelevant to maintain the kernel integrity.
577 * We need this check here too in case user space decides to not honor
578 * the error message in sendmsg/sendpage and still call recvmsg. This
579 * check here protects the kernel integrity.
581 if (!aead_sufficient_data(ctx
))
585 * Calculate the minimum output buffer size holding the result of the
586 * cipher operation. When encrypting data, the receiving buffer is
587 * larger by the tag length compared to the input buffer as the
588 * encryption operation generates the tag. For decryption, the input
589 * buffer provides the tag which is consumed resulting in only the
590 * plaintext without a buffer for the tag returned to the caller.
598 * The cipher operation input data is reduced by the associated data
599 * length as this data is processed separately later on.
601 used
-= ctx
->aead_assoclen
;
603 /* convert iovecs of output buffers into scatterlists */
604 while (outlen
> usedpages
&& iov_iter_count(&msg
->msg_iter
)) {
605 size_t seglen
= min_t(size_t, iov_iter_count(&msg
->msg_iter
),
606 (outlen
- usedpages
));
608 if (list_empty(&ctx
->list
)) {
609 rsgl
= &ctx
->first_rsgl
;
611 rsgl
= sock_kmalloc(sk
, sizeof(*rsgl
), GFP_KERNEL
);
612 if (unlikely(!rsgl
)) {
617 rsgl
->sgl
.npages
= 0;
618 list_add_tail(&rsgl
->list
, &ctx
->list
);
620 /* make one iovec available as scatterlist */
621 err
= af_alg_make_sg(&rsgl
->sgl
, &msg
->msg_iter
, seglen
);
625 /* chain the new scatterlist with previous one */
627 af_alg_link_sg(&last_rsgl
->sgl
, &rsgl
->sgl
);
631 iov_iter_advance(&msg
->msg_iter
, err
);
634 /* ensure output buffer is sufficiently large */
635 if (usedpages
< outlen
) {
640 sg_mark_end(sgl
->sg
+ sgl
->cur
- 1);
641 aead_request_set_crypt(&ctx
->aead_req
, sgl
->sg
, ctx
->first_rsgl
.sgl
.sg
,
643 aead_request_set_ad(&ctx
->aead_req
, ctx
->aead_assoclen
);
645 err
= af_alg_wait_for_completion(ctx
->enc
?
646 crypto_aead_encrypt(&ctx
->aead_req
) :
647 crypto_aead_decrypt(&ctx
->aead_req
),
651 /* EBADMSG implies a valid cipher operation took place */
662 list_for_each_entry_safe(rsgl
, tmp
, &ctx
->list
, list
) {
663 af_alg_free_sg(&rsgl
->sgl
);
664 if (rsgl
!= &ctx
->first_rsgl
)
665 sock_kfree_s(sk
, rsgl
, sizeof(*rsgl
));
666 list_del(&rsgl
->list
);
668 INIT_LIST_HEAD(&ctx
->list
);
669 aead_wmem_wakeup(sk
);
672 return err
? err
: outlen
;
675 static int aead_recvmsg(struct socket
*sock
, struct msghdr
*msg
, size_t ignored
,
678 return (msg
->msg_iocb
&& !is_sync_kiocb(msg
->msg_iocb
)) ?
679 aead_recvmsg_async(sock
, msg
, flags
) :
680 aead_recvmsg_sync(sock
, msg
, flags
);
683 static unsigned int aead_poll(struct file
*file
, struct socket
*sock
,
686 struct sock
*sk
= sock
->sk
;
687 struct alg_sock
*ask
= alg_sk(sk
);
688 struct aead_ctx
*ctx
= ask
->private;
691 sock_poll_wait(file
, sk_sleep(sk
), wait
);
695 mask
|= POLLIN
| POLLRDNORM
;
697 if (aead_writable(sk
))
698 mask
|= POLLOUT
| POLLWRNORM
| POLLWRBAND
;
703 static struct proto_ops algif_aead_ops
= {
706 .connect
= sock_no_connect
,
707 .socketpair
= sock_no_socketpair
,
708 .getname
= sock_no_getname
,
709 .ioctl
= sock_no_ioctl
,
710 .listen
= sock_no_listen
,
711 .shutdown
= sock_no_shutdown
,
712 .getsockopt
= sock_no_getsockopt
,
713 .mmap
= sock_no_mmap
,
714 .bind
= sock_no_bind
,
715 .accept
= sock_no_accept
,
716 .setsockopt
= sock_no_setsockopt
,
718 .release
= af_alg_release
,
719 .sendmsg
= aead_sendmsg
,
720 .sendpage
= aead_sendpage
,
721 .recvmsg
= aead_recvmsg
,
725 static void *aead_bind(const char *name
, u32 type
, u32 mask
)
727 return crypto_alloc_aead(name
, type
, mask
);
730 static void aead_release(void *private)
732 crypto_free_aead(private);
735 static int aead_setauthsize(void *private, unsigned int authsize
)
737 return crypto_aead_setauthsize(private, authsize
);
740 static int aead_setkey(void *private, const u8
*key
, unsigned int keylen
)
742 return crypto_aead_setkey(private, key
, keylen
);
745 static void aead_sock_destruct(struct sock
*sk
)
747 struct alg_sock
*ask
= alg_sk(sk
);
748 struct aead_ctx
*ctx
= ask
->private;
749 unsigned int ivlen
= crypto_aead_ivsize(
750 crypto_aead_reqtfm(&ctx
->aead_req
));
752 WARN_ON(atomic_read(&sk
->sk_refcnt
) != 0);
754 sock_kzfree_s(sk
, ctx
->iv
, ivlen
);
755 sock_kfree_s(sk
, ctx
, ctx
->len
);
756 af_alg_release_parent(sk
);
759 static int aead_accept_parent(void *private, struct sock
*sk
)
761 struct aead_ctx
*ctx
;
762 struct alg_sock
*ask
= alg_sk(sk
);
763 unsigned int len
= sizeof(*ctx
) + crypto_aead_reqsize(private);
764 unsigned int ivlen
= crypto_aead_ivsize(private);
766 ctx
= sock_kmalloc(sk
, len
, GFP_KERNEL
);
771 ctx
->iv
= sock_kmalloc(sk
, ivlen
, GFP_KERNEL
);
773 sock_kfree_s(sk
, ctx
, len
);
776 memset(ctx
->iv
, 0, ivlen
);
784 ctx
->aead_assoclen
= 0;
785 af_alg_init_completion(&ctx
->completion
);
786 sg_init_table(ctx
->tsgl
.sg
, ALG_MAX_PAGES
);
787 INIT_LIST_HEAD(&ctx
->list
);
791 aead_request_set_tfm(&ctx
->aead_req
, private);
792 aead_request_set_callback(&ctx
->aead_req
, CRYPTO_TFM_REQ_MAY_BACKLOG
,
793 af_alg_complete
, &ctx
->completion
);
795 sk
->sk_destruct
= aead_sock_destruct
;
800 static const struct af_alg_type algif_type_aead
= {
802 .release
= aead_release
,
803 .setkey
= aead_setkey
,
804 .setauthsize
= aead_setauthsize
,
805 .accept
= aead_accept_parent
,
806 .ops
= &algif_aead_ops
,
811 static int __init
algif_aead_init(void)
813 return af_alg_register_type(&algif_type_aead
);
816 static void __exit
algif_aead_exit(void)
818 int err
= af_alg_unregister_type(&algif_type_aead
);
822 module_init(algif_aead_init
);
823 module_exit(algif_aead_exit
);
824 MODULE_LICENSE("GPL");
825 MODULE_AUTHOR("Stephan Mueller <smueller@chronox.de>");
826 MODULE_DESCRIPTION("AEAD kernel crypto API user space interface");