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
;
48 struct aead_sg_list tsgl
;
49 struct aead_async_rsgl first_rsgl
;
50 struct list_head list
;
54 struct af_alg_completion completion
;
64 struct aead_request aead_req
;
67 static inline int aead_sndbuf(struct sock
*sk
)
69 struct alg_sock
*ask
= alg_sk(sk
);
70 struct aead_ctx
*ctx
= ask
->private;
72 return max_t(int, max_t(int, sk
->sk_sndbuf
& PAGE_MASK
, PAGE_SIZE
) -
76 static inline bool aead_writable(struct sock
*sk
)
78 return PAGE_SIZE
<= aead_sndbuf(sk
);
81 static inline bool aead_sufficient_data(struct aead_ctx
*ctx
)
83 unsigned as
= crypto_aead_authsize(crypto_aead_reqtfm(&ctx
->aead_req
));
86 * The minimum amount of memory needed for an AEAD cipher is
87 * the AAD and in case of decryption the tag.
89 return ctx
->used
>= ctx
->aead_assoclen
+ (ctx
->enc
? 0 : as
);
92 static void aead_reset_ctx(struct aead_ctx
*ctx
)
94 struct aead_sg_list
*sgl
= &ctx
->tsgl
;
96 sg_init_table(sgl
->sg
, ALG_MAX_PAGES
);
103 static void aead_put_sgl(struct sock
*sk
)
105 struct alg_sock
*ask
= alg_sk(sk
);
106 struct aead_ctx
*ctx
= ask
->private;
107 struct aead_sg_list
*sgl
= &ctx
->tsgl
;
108 struct scatterlist
*sg
= sgl
->sg
;
111 for (i
= 0; i
< sgl
->cur
; i
++) {
112 if (!sg_page(sg
+ i
))
115 put_page(sg_page(sg
+ i
));
116 sg_assign_page(sg
+ i
, NULL
);
121 static void aead_wmem_wakeup(struct sock
*sk
)
123 struct socket_wq
*wq
;
125 if (!aead_writable(sk
))
129 wq
= rcu_dereference(sk
->sk_wq
);
130 if (skwq_has_sleeper(wq
))
131 wake_up_interruptible_sync_poll(&wq
->wait
, POLLIN
|
134 sk_wake_async(sk
, SOCK_WAKE_WAITD
, POLL_IN
);
138 static int aead_wait_for_data(struct sock
*sk
, unsigned flags
)
140 DEFINE_WAIT_FUNC(wait
, woken_wake_function
);
141 struct alg_sock
*ask
= alg_sk(sk
);
142 struct aead_ctx
*ctx
= ask
->private;
144 int err
= -ERESTARTSYS
;
146 if (flags
& MSG_DONTWAIT
)
149 sk_set_bit(SOCKWQ_ASYNC_WAITDATA
, sk
);
150 add_wait_queue(sk_sleep(sk
), &wait
);
152 if (signal_pending(current
))
154 timeout
= MAX_SCHEDULE_TIMEOUT
;
155 if (sk_wait_event(sk
, &timeout
, !ctx
->more
, &wait
)) {
160 remove_wait_queue(sk_sleep(sk
), &wait
);
162 sk_clear_bit(SOCKWQ_ASYNC_WAITDATA
, sk
);
167 static void aead_data_wakeup(struct sock
*sk
)
169 struct alg_sock
*ask
= alg_sk(sk
);
170 struct aead_ctx
*ctx
= ask
->private;
171 struct socket_wq
*wq
;
179 wq
= rcu_dereference(sk
->sk_wq
);
180 if (skwq_has_sleeper(wq
))
181 wake_up_interruptible_sync_poll(&wq
->wait
, POLLOUT
|
184 sk_wake_async(sk
, SOCK_WAKE_SPACE
, POLL_OUT
);
188 static int aead_sendmsg(struct socket
*sock
, struct msghdr
*msg
, size_t size
)
190 struct sock
*sk
= sock
->sk
;
191 struct alg_sock
*ask
= alg_sk(sk
);
192 struct aead_ctx
*ctx
= ask
->private;
194 crypto_aead_ivsize(crypto_aead_reqtfm(&ctx
->aead_req
));
195 struct aead_sg_list
*sgl
= &ctx
->tsgl
;
196 struct af_alg_control con
= {};
202 if (msg
->msg_controllen
) {
203 err
= af_alg_cmsg_send(msg
, &con
);
219 if (con
.iv
&& con
.iv
->ivlen
!= ivsize
)
224 if (!ctx
->more
&& ctx
->used
)
230 memcpy(ctx
->iv
, con
.iv
->iv
, ivsize
);
232 ctx
->aead_assoclen
= con
.aead_assoclen
;
237 struct scatterlist
*sg
= NULL
;
239 /* use the existing memory in an allocated page */
241 sg
= sgl
->sg
+ sgl
->cur
- 1;
242 len
= min_t(unsigned long, len
,
243 PAGE_SIZE
- sg
->offset
- sg
->length
);
244 err
= memcpy_from_msg(page_address(sg_page(sg
)) +
245 sg
->offset
+ sg
->length
,
251 ctx
->merge
= (sg
->offset
+ sg
->length
) &
260 if (!aead_writable(sk
)) {
261 /* user space sent too much data */
267 /* allocate a new page */
268 len
= min_t(unsigned long, size
, aead_sndbuf(sk
));
272 if (sgl
->cur
>= ALG_MAX_PAGES
) {
278 sg
= sgl
->sg
+ sgl
->cur
;
279 plen
= min_t(size_t, len
, PAGE_SIZE
);
281 sg_assign_page(sg
, alloc_page(GFP_KERNEL
));
286 err
= memcpy_from_msg(page_address(sg_page(sg
)),
289 __free_page(sg_page(sg
));
290 sg_assign_page(sg
, NULL
);
301 ctx
->merge
= plen
& (PAGE_SIZE
- 1);
307 ctx
->more
= msg
->msg_flags
& MSG_MORE
;
308 if (!ctx
->more
&& !aead_sufficient_data(ctx
)) {
314 aead_data_wakeup(sk
);
317 return err
?: copied
;
320 static ssize_t
aead_sendpage(struct socket
*sock
, struct page
*page
,
321 int offset
, size_t size
, int flags
)
323 struct sock
*sk
= sock
->sk
;
324 struct alg_sock
*ask
= alg_sk(sk
);
325 struct aead_ctx
*ctx
= ask
->private;
326 struct aead_sg_list
*sgl
= &ctx
->tsgl
;
329 if (flags
& MSG_SENDPAGE_NOTLAST
)
332 if (sgl
->cur
>= ALG_MAX_PAGES
)
336 if (!ctx
->more
&& ctx
->used
)
342 if (!aead_writable(sk
)) {
343 /* user space sent too much data */
352 sg_set_page(sgl
->sg
+ sgl
->cur
, page
, size
, offset
);
359 ctx
->more
= flags
& MSG_MORE
;
360 if (!ctx
->more
&& !aead_sufficient_data(ctx
)) {
366 aead_data_wakeup(sk
);
372 #define GET_ASYM_REQ(req, tfm) (struct aead_async_req *) \
373 ((char *)req + sizeof(struct aead_request) + \
374 crypto_aead_reqsize(tfm))
376 #define GET_REQ_SIZE(tfm) sizeof(struct aead_async_req) + \
377 crypto_aead_reqsize(tfm) + crypto_aead_ivsize(tfm) + \
378 sizeof(struct aead_request)
380 static void aead_async_cb(struct crypto_async_request
*_req
, int err
)
382 struct sock
*sk
= _req
->data
;
383 struct alg_sock
*ask
= alg_sk(sk
);
384 struct aead_ctx
*ctx
= ask
->private;
385 struct crypto_aead
*tfm
= crypto_aead_reqtfm(&ctx
->aead_req
);
386 struct aead_request
*req
= aead_request_cast(_req
);
387 struct aead_async_req
*areq
= GET_ASYM_REQ(req
, tfm
);
388 struct scatterlist
*sg
= areq
->tsgl
;
389 struct aead_async_rsgl
*rsgl
;
390 struct kiocb
*iocb
= areq
->iocb
;
391 unsigned int i
, reqlen
= GET_REQ_SIZE(tfm
);
393 list_for_each_entry(rsgl
, &areq
->list
, list
) {
394 af_alg_free_sg(&rsgl
->sgl
);
395 if (rsgl
!= &areq
->first_rsgl
)
396 sock_kfree_s(sk
, rsgl
, sizeof(*rsgl
));
399 for (i
= 0; i
< areq
->tsgls
; i
++)
400 put_page(sg_page(sg
+ i
));
402 sock_kfree_s(sk
, areq
->tsgl
, sizeof(*areq
->tsgl
) * areq
->tsgls
);
403 sock_kfree_s(sk
, req
, reqlen
);
405 iocb
->ki_complete(iocb
, err
, err
);
408 static int aead_recvmsg_async(struct socket
*sock
, struct msghdr
*msg
,
411 struct sock
*sk
= sock
->sk
;
412 struct alg_sock
*ask
= alg_sk(sk
);
413 struct aead_ctx
*ctx
= ask
->private;
414 struct crypto_aead
*tfm
= crypto_aead_reqtfm(&ctx
->aead_req
);
415 struct aead_async_req
*areq
;
416 struct aead_request
*req
= NULL
;
417 struct aead_sg_list
*sgl
= &ctx
->tsgl
;
418 struct aead_async_rsgl
*last_rsgl
= NULL
, *rsgl
;
419 unsigned int as
= crypto_aead_authsize(tfm
);
420 unsigned int i
, reqlen
= GET_REQ_SIZE(tfm
);
424 size_t usedpages
= 0;
428 err
= aead_wait_for_data(sk
, flags
);
433 if (!aead_sufficient_data(ctx
))
442 req
= sock_kmalloc(sk
, reqlen
, GFP_KERNEL
);
446 areq
= GET_ASYM_REQ(req
, tfm
);
447 memset(&areq
->first_rsgl
, '\0', sizeof(areq
->first_rsgl
));
448 INIT_LIST_HEAD(&areq
->list
);
449 areq
->iocb
= msg
->msg_iocb
;
450 memcpy(areq
->iv
, ctx
->iv
, crypto_aead_ivsize(tfm
));
451 aead_request_set_tfm(req
, tfm
);
452 aead_request_set_ad(req
, ctx
->aead_assoclen
);
453 aead_request_set_callback(req
, CRYPTO_TFM_REQ_MAY_BACKLOG
,
455 used
-= ctx
->aead_assoclen
;
457 /* take over all tx sgls from ctx */
458 areq
->tsgl
= sock_kmalloc(sk
,
459 sizeof(*areq
->tsgl
) * max_t(u32
, sgl
->cur
, 1),
461 if (unlikely(!areq
->tsgl
))
464 sg_init_table(areq
->tsgl
, max_t(u32
, sgl
->cur
, 1));
465 for (i
= 0; i
< sgl
->cur
; i
++)
466 sg_set_page(&areq
->tsgl
[i
], sg_page(&sgl
->sg
[i
]),
467 sgl
->sg
[i
].length
, sgl
->sg
[i
].offset
);
469 areq
->tsgls
= sgl
->cur
;
472 while (outlen
> usedpages
&& iov_iter_count(&msg
->msg_iter
)) {
473 size_t seglen
= min_t(size_t, iov_iter_count(&msg
->msg_iter
),
474 (outlen
- usedpages
));
476 if (list_empty(&areq
->list
)) {
477 rsgl
= &areq
->first_rsgl
;
480 rsgl
= sock_kmalloc(sk
, sizeof(*rsgl
), GFP_KERNEL
);
481 if (unlikely(!rsgl
)) {
486 rsgl
->sgl
.npages
= 0;
487 list_add_tail(&rsgl
->list
, &areq
->list
);
489 /* make one iovec available as scatterlist */
490 err
= af_alg_make_sg(&rsgl
->sgl
, &msg
->msg_iter
, seglen
);
496 /* chain the new scatterlist with previous one */
498 af_alg_link_sg(&last_rsgl
->sgl
, &rsgl
->sgl
);
502 iov_iter_advance(&msg
->msg_iter
, err
);
505 /* ensure output buffer is sufficiently large */
506 if (usedpages
< outlen
) {
511 aead_request_set_crypt(req
, areq
->tsgl
, areq
->first_rsgl
.sgl
.sg
, used
,
513 err
= ctx
->enc
? crypto_aead_encrypt(req
) : crypto_aead_decrypt(req
);
515 if (err
== -EINPROGRESS
) {
520 } else if (err
== -EBADMSG
) {
528 list_for_each_entry(rsgl
, &areq
->list
, list
) {
529 af_alg_free_sg(&rsgl
->sgl
);
530 if (rsgl
!= &areq
->first_rsgl
)
531 sock_kfree_s(sk
, rsgl
, sizeof(*rsgl
));
534 sock_kfree_s(sk
, areq
->tsgl
, sizeof(*areq
->tsgl
) * areq
->tsgls
);
536 sock_kfree_s(sk
, req
, reqlen
);
538 aead_wmem_wakeup(sk
);
540 return err
? err
: outlen
;
543 static int aead_recvmsg_sync(struct socket
*sock
, struct msghdr
*msg
, int flags
)
545 struct sock
*sk
= sock
->sk
;
546 struct alg_sock
*ask
= alg_sk(sk
);
547 struct aead_ctx
*ctx
= ask
->private;
548 unsigned as
= crypto_aead_authsize(crypto_aead_reqtfm(&ctx
->aead_req
));
549 struct aead_sg_list
*sgl
= &ctx
->tsgl
;
550 struct aead_async_rsgl
*last_rsgl
= NULL
;
551 struct aead_async_rsgl
*rsgl
, *tmp
;
553 unsigned long used
= 0;
555 size_t usedpages
= 0;
560 * Please see documentation of aead_request_set_crypt for the
561 * description of the AEAD memory structure expected from the caller.
565 err
= aead_wait_for_data(sk
, flags
);
570 /* data length provided by caller via sendmsg/sendpage */
574 * Make sure sufficient data is present -- note, the same check is
575 * is also present in sendmsg/sendpage. The checks in sendpage/sendmsg
576 * shall provide an information to the data sender that something is
577 * wrong, but they are irrelevant to maintain the kernel integrity.
578 * We need this check here too in case user space decides to not honor
579 * the error message in sendmsg/sendpage and still call recvmsg. This
580 * check here protects the kernel integrity.
582 if (!aead_sufficient_data(ctx
))
586 * Calculate the minimum output buffer size holding the result of the
587 * cipher operation. When encrypting data, the receiving buffer is
588 * larger by the tag length compared to the input buffer as the
589 * encryption operation generates the tag. For decryption, the input
590 * buffer provides the tag which is consumed resulting in only the
591 * plaintext without a buffer for the tag returned to the caller.
599 * The cipher operation input data is reduced by the associated data
600 * length as this data is processed separately later on.
602 used
-= ctx
->aead_assoclen
;
604 /* convert iovecs of output buffers into scatterlists */
605 while (outlen
> usedpages
&& iov_iter_count(&msg
->msg_iter
)) {
606 size_t seglen
= min_t(size_t, iov_iter_count(&msg
->msg_iter
),
607 (outlen
- usedpages
));
609 if (list_empty(&ctx
->list
)) {
610 rsgl
= &ctx
->first_rsgl
;
612 rsgl
= sock_kmalloc(sk
, sizeof(*rsgl
), GFP_KERNEL
);
613 if (unlikely(!rsgl
)) {
618 rsgl
->sgl
.npages
= 0;
619 list_add_tail(&rsgl
->list
, &ctx
->list
);
621 /* make one iovec available as scatterlist */
622 err
= af_alg_make_sg(&rsgl
->sgl
, &msg
->msg_iter
, seglen
);
626 /* chain the new scatterlist with previous one */
628 af_alg_link_sg(&last_rsgl
->sgl
, &rsgl
->sgl
);
632 iov_iter_advance(&msg
->msg_iter
, err
);
635 /* ensure output buffer is sufficiently large */
636 if (usedpages
< outlen
) {
641 sg_mark_end(sgl
->sg
+ sgl
->cur
- 1);
642 aead_request_set_crypt(&ctx
->aead_req
, sgl
->sg
, ctx
->first_rsgl
.sgl
.sg
,
644 aead_request_set_ad(&ctx
->aead_req
, ctx
->aead_assoclen
);
646 err
= af_alg_wait_for_completion(ctx
->enc
?
647 crypto_aead_encrypt(&ctx
->aead_req
) :
648 crypto_aead_decrypt(&ctx
->aead_req
),
652 /* EBADMSG implies a valid cipher operation took place */
663 list_for_each_entry_safe(rsgl
, tmp
, &ctx
->list
, list
) {
664 af_alg_free_sg(&rsgl
->sgl
);
665 list_del(&rsgl
->list
);
666 if (rsgl
!= &ctx
->first_rsgl
)
667 sock_kfree_s(sk
, rsgl
, sizeof(*rsgl
));
669 INIT_LIST_HEAD(&ctx
->list
);
670 aead_wmem_wakeup(sk
);
673 return err
? err
: outlen
;
676 static int aead_recvmsg(struct socket
*sock
, struct msghdr
*msg
, size_t ignored
,
679 return (msg
->msg_iocb
&& !is_sync_kiocb(msg
->msg_iocb
)) ?
680 aead_recvmsg_async(sock
, msg
, flags
) :
681 aead_recvmsg_sync(sock
, msg
, flags
);
684 static unsigned int aead_poll(struct file
*file
, struct socket
*sock
,
687 struct sock
*sk
= sock
->sk
;
688 struct alg_sock
*ask
= alg_sk(sk
);
689 struct aead_ctx
*ctx
= ask
->private;
692 sock_poll_wait(file
, sk_sleep(sk
), wait
);
696 mask
|= POLLIN
| POLLRDNORM
;
698 if (aead_writable(sk
))
699 mask
|= POLLOUT
| POLLWRNORM
| POLLWRBAND
;
704 static struct proto_ops algif_aead_ops
= {
707 .connect
= sock_no_connect
,
708 .socketpair
= sock_no_socketpair
,
709 .getname
= sock_no_getname
,
710 .ioctl
= sock_no_ioctl
,
711 .listen
= sock_no_listen
,
712 .shutdown
= sock_no_shutdown
,
713 .getsockopt
= sock_no_getsockopt
,
714 .mmap
= sock_no_mmap
,
715 .bind
= sock_no_bind
,
716 .accept
= sock_no_accept
,
717 .setsockopt
= sock_no_setsockopt
,
719 .release
= af_alg_release
,
720 .sendmsg
= aead_sendmsg
,
721 .sendpage
= aead_sendpage
,
722 .recvmsg
= aead_recvmsg
,
726 static void *aead_bind(const char *name
, u32 type
, u32 mask
)
728 return crypto_alloc_aead(name
, type
, mask
);
731 static void aead_release(void *private)
733 crypto_free_aead(private);
736 static int aead_setauthsize(void *private, unsigned int authsize
)
738 return crypto_aead_setauthsize(private, authsize
);
741 static int aead_setkey(void *private, const u8
*key
, unsigned int keylen
)
743 return crypto_aead_setkey(private, key
, keylen
);
746 static void aead_sock_destruct(struct sock
*sk
)
748 struct alg_sock
*ask
= alg_sk(sk
);
749 struct aead_ctx
*ctx
= ask
->private;
750 unsigned int ivlen
= crypto_aead_ivsize(
751 crypto_aead_reqtfm(&ctx
->aead_req
));
753 WARN_ON(atomic_read(&sk
->sk_refcnt
) != 0);
755 sock_kzfree_s(sk
, ctx
->iv
, ivlen
);
756 sock_kfree_s(sk
, ctx
, ctx
->len
);
757 af_alg_release_parent(sk
);
760 static int aead_accept_parent(void *private, struct sock
*sk
)
762 struct aead_ctx
*ctx
;
763 struct alg_sock
*ask
= alg_sk(sk
);
764 unsigned int len
= sizeof(*ctx
) + crypto_aead_reqsize(private);
765 unsigned int ivlen
= crypto_aead_ivsize(private);
767 ctx
= sock_kmalloc(sk
, len
, GFP_KERNEL
);
772 ctx
->iv
= sock_kmalloc(sk
, ivlen
, GFP_KERNEL
);
774 sock_kfree_s(sk
, ctx
, len
);
777 memset(ctx
->iv
, 0, ivlen
);
785 ctx
->aead_assoclen
= 0;
786 af_alg_init_completion(&ctx
->completion
);
787 sg_init_table(ctx
->tsgl
.sg
, ALG_MAX_PAGES
);
788 INIT_LIST_HEAD(&ctx
->list
);
792 aead_request_set_tfm(&ctx
->aead_req
, private);
793 aead_request_set_callback(&ctx
->aead_req
, CRYPTO_TFM_REQ_MAY_BACKLOG
,
794 af_alg_complete
, &ctx
->completion
);
796 sk
->sk_destruct
= aead_sock_destruct
;
801 static const struct af_alg_type algif_type_aead
= {
803 .release
= aead_release
,
804 .setkey
= aead_setkey
,
805 .setauthsize
= aead_setauthsize
,
806 .accept
= aead_accept_parent
,
807 .ops
= &algif_aead_ops
,
812 static int __init
algif_aead_init(void)
814 return af_alg_register_type(&algif_type_aead
);
817 static void __exit
algif_aead_exit(void)
819 int err
= af_alg_unregister_type(&algif_type_aead
);
823 module_init(algif_aead_init
);
824 module_exit(algif_aead_exit
);
825 MODULE_LICENSE("GPL");
826 MODULE_AUTHOR("Stephan Mueller <smueller@chronox.de>");
827 MODULE_DESCRIPTION("AEAD kernel crypto API user space interface");