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
;
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 struct alg_sock
*ask
= alg_sk(sk
);
141 struct aead_ctx
*ctx
= ask
->private;
144 int err
= -ERESTARTSYS
;
146 if (flags
& MSG_DONTWAIT
)
149 sk_set_bit(SOCKWQ_ASYNC_WAITDATA
, sk
);
152 if (signal_pending(current
))
154 prepare_to_wait(sk_sleep(sk
), &wait
, TASK_INTERRUPTIBLE
);
155 timeout
= MAX_SCHEDULE_TIMEOUT
;
156 if (sk_wait_event(sk
, &timeout
, !ctx
->more
)) {
161 finish_wait(sk_sleep(sk
), &wait
);
163 sk_clear_bit(SOCKWQ_ASYNC_WAITDATA
, sk
);
168 static void aead_data_wakeup(struct sock
*sk
)
170 struct alg_sock
*ask
= alg_sk(sk
);
171 struct aead_ctx
*ctx
= ask
->private;
172 struct socket_wq
*wq
;
180 wq
= rcu_dereference(sk
->sk_wq
);
181 if (skwq_has_sleeper(wq
))
182 wake_up_interruptible_sync_poll(&wq
->wait
, POLLOUT
|
185 sk_wake_async(sk
, SOCK_WAKE_SPACE
, POLL_OUT
);
189 static int aead_sendmsg(struct socket
*sock
, struct msghdr
*msg
, size_t size
)
191 struct sock
*sk
= sock
->sk
;
192 struct alg_sock
*ask
= alg_sk(sk
);
193 struct aead_ctx
*ctx
= ask
->private;
195 crypto_aead_ivsize(crypto_aead_reqtfm(&ctx
->aead_req
));
196 struct aead_sg_list
*sgl
= &ctx
->tsgl
;
197 struct af_alg_control con
= {};
203 if (msg
->msg_controllen
) {
204 err
= af_alg_cmsg_send(msg
, &con
);
220 if (con
.iv
&& con
.iv
->ivlen
!= ivsize
)
225 if (!ctx
->more
&& ctx
->used
)
231 memcpy(ctx
->iv
, con
.iv
->iv
, ivsize
);
233 ctx
->aead_assoclen
= con
.aead_assoclen
;
238 struct scatterlist
*sg
= NULL
;
240 /* use the existing memory in an allocated page */
242 sg
= sgl
->sg
+ sgl
->cur
- 1;
243 len
= min_t(unsigned long, len
,
244 PAGE_SIZE
- sg
->offset
- sg
->length
);
245 err
= memcpy_from_msg(page_address(sg_page(sg
)) +
246 sg
->offset
+ sg
->length
,
252 ctx
->merge
= (sg
->offset
+ sg
->length
) &
261 if (!aead_writable(sk
)) {
262 /* user space sent too much data */
268 /* allocate a new page */
269 len
= min_t(unsigned long, size
, aead_sndbuf(sk
));
273 if (sgl
->cur
>= ALG_MAX_PAGES
) {
279 sg
= sgl
->sg
+ sgl
->cur
;
280 plen
= min_t(size_t, len
, PAGE_SIZE
);
282 sg_assign_page(sg
, alloc_page(GFP_KERNEL
));
287 err
= memcpy_from_msg(page_address(sg_page(sg
)),
290 __free_page(sg_page(sg
));
291 sg_assign_page(sg
, NULL
);
302 ctx
->merge
= plen
& (PAGE_SIZE
- 1);
308 ctx
->more
= msg
->msg_flags
& MSG_MORE
;
309 if (!ctx
->more
&& !aead_sufficient_data(ctx
)) {
315 aead_data_wakeup(sk
);
318 return err
?: copied
;
321 static ssize_t
aead_sendpage(struct socket
*sock
, struct page
*page
,
322 int offset
, size_t size
, int flags
)
324 struct sock
*sk
= sock
->sk
;
325 struct alg_sock
*ask
= alg_sk(sk
);
326 struct aead_ctx
*ctx
= ask
->private;
327 struct aead_sg_list
*sgl
= &ctx
->tsgl
;
330 if (flags
& MSG_SENDPAGE_NOTLAST
)
333 if (sgl
->cur
>= ALG_MAX_PAGES
)
337 if (!ctx
->more
&& ctx
->used
)
343 if (!aead_writable(sk
)) {
344 /* user space sent too much data */
353 sg_set_page(sgl
->sg
+ sgl
->cur
, page
, size
, offset
);
360 ctx
->more
= flags
& MSG_MORE
;
361 if (!ctx
->more
&& !aead_sufficient_data(ctx
)) {
367 aead_data_wakeup(sk
);
373 #define GET_ASYM_REQ(req, tfm) (struct aead_async_req *) \
374 ((char *)req + sizeof(struct aead_request) + \
375 crypto_aead_reqsize(tfm))
377 #define GET_REQ_SIZE(tfm) sizeof(struct aead_async_req) + \
378 crypto_aead_reqsize(tfm) + crypto_aead_ivsize(tfm) + \
379 sizeof(struct aead_request)
381 static void aead_async_cb(struct crypto_async_request
*_req
, int err
)
383 struct aead_request
*req
= _req
->data
;
384 struct crypto_aead
*tfm
= crypto_aead_reqtfm(req
);
385 struct aead_async_req
*areq
= GET_ASYM_REQ(req
, tfm
);
386 struct sock
*sk
= areq
->sk
;
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
;
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
, sizeof(*areq
->tsgl
) * sgl
->cur
,
460 if (unlikely(!areq
->tsgl
))
463 sg_init_table(areq
->tsgl
, sgl
->cur
);
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 * AEAD memory structure: For encryption, the tag is appended to the
560 * ciphertext which implies that the memory allocated for the ciphertext
561 * must be increased by the tag length. For decryption, the tag
562 * is expected to be concatenated to the ciphertext. The plaintext
563 * therefore has a memory size of the ciphertext minus the tag length.
565 * The memory structure for cipher operation has the following
567 * AEAD encryption input: assoc data || plaintext
568 * AEAD encryption output: cipherntext || auth tag
569 * AEAD decryption input: assoc data || ciphertext || auth tag
570 * AEAD decryption output: plaintext
574 err
= aead_wait_for_data(sk
, flags
);
579 /* data length provided by caller via sendmsg/sendpage */
583 * Make sure sufficient data is present -- note, the same check is
584 * is also present in sendmsg/sendpage. The checks in sendpage/sendmsg
585 * shall provide an information to the data sender that something is
586 * wrong, but they are irrelevant to maintain the kernel integrity.
587 * We need this check here too in case user space decides to not honor
588 * the error message in sendmsg/sendpage and still call recvmsg. This
589 * check here protects the kernel integrity.
591 if (!aead_sufficient_data(ctx
))
595 * Calculate the minimum output buffer size holding the result of the
596 * cipher operation. When encrypting data, the receiving buffer is
597 * larger by the tag length compared to the input buffer as the
598 * encryption operation generates the tag. For decryption, the input
599 * buffer provides the tag which is consumed resulting in only the
600 * plaintext without a buffer for the tag returned to the caller.
608 * The cipher operation input data is reduced by the associated data
609 * length as this data is processed separately later on.
611 used
-= ctx
->aead_assoclen
;
613 /* convert iovecs of output buffers into scatterlists */
614 while (outlen
> usedpages
&& iov_iter_count(&msg
->msg_iter
)) {
615 size_t seglen
= min_t(size_t, iov_iter_count(&msg
->msg_iter
),
616 (outlen
- usedpages
));
618 if (list_empty(&ctx
->list
)) {
619 rsgl
= &ctx
->first_rsgl
;
621 rsgl
= sock_kmalloc(sk
, sizeof(*rsgl
), GFP_KERNEL
);
622 if (unlikely(!rsgl
)) {
627 rsgl
->sgl
.npages
= 0;
628 list_add_tail(&rsgl
->list
, &ctx
->list
);
630 /* make one iovec available as scatterlist */
631 err
= af_alg_make_sg(&rsgl
->sgl
, &msg
->msg_iter
, seglen
);
635 /* chain the new scatterlist with previous one */
637 af_alg_link_sg(&last_rsgl
->sgl
, &rsgl
->sgl
);
641 iov_iter_advance(&msg
->msg_iter
, err
);
644 /* ensure output buffer is sufficiently large */
645 if (usedpages
< outlen
) {
650 sg_mark_end(sgl
->sg
+ sgl
->cur
- 1);
651 aead_request_set_crypt(&ctx
->aead_req
, sgl
->sg
, ctx
->first_rsgl
.sgl
.sg
,
653 aead_request_set_ad(&ctx
->aead_req
, ctx
->aead_assoclen
);
655 err
= af_alg_wait_for_completion(ctx
->enc
?
656 crypto_aead_encrypt(&ctx
->aead_req
) :
657 crypto_aead_decrypt(&ctx
->aead_req
),
661 /* EBADMSG implies a valid cipher operation took place */
672 list_for_each_entry_safe(rsgl
, tmp
, &ctx
->list
, list
) {
673 af_alg_free_sg(&rsgl
->sgl
);
674 list_del(&rsgl
->list
);
675 if (rsgl
!= &ctx
->first_rsgl
)
676 sock_kfree_s(sk
, rsgl
, sizeof(*rsgl
));
678 INIT_LIST_HEAD(&ctx
->list
);
679 aead_wmem_wakeup(sk
);
682 return err
? err
: outlen
;
685 static int aead_recvmsg(struct socket
*sock
, struct msghdr
*msg
, size_t ignored
,
688 return (msg
->msg_iocb
&& !is_sync_kiocb(msg
->msg_iocb
)) ?
689 aead_recvmsg_async(sock
, msg
, flags
) :
690 aead_recvmsg_sync(sock
, msg
, flags
);
693 static unsigned int aead_poll(struct file
*file
, struct socket
*sock
,
696 struct sock
*sk
= sock
->sk
;
697 struct alg_sock
*ask
= alg_sk(sk
);
698 struct aead_ctx
*ctx
= ask
->private;
701 sock_poll_wait(file
, sk_sleep(sk
), wait
);
705 mask
|= POLLIN
| POLLRDNORM
;
707 if (aead_writable(sk
))
708 mask
|= POLLOUT
| POLLWRNORM
| POLLWRBAND
;
713 static struct proto_ops algif_aead_ops
= {
716 .connect
= sock_no_connect
,
717 .socketpair
= sock_no_socketpair
,
718 .getname
= sock_no_getname
,
719 .ioctl
= sock_no_ioctl
,
720 .listen
= sock_no_listen
,
721 .shutdown
= sock_no_shutdown
,
722 .getsockopt
= sock_no_getsockopt
,
723 .mmap
= sock_no_mmap
,
724 .bind
= sock_no_bind
,
725 .accept
= sock_no_accept
,
726 .setsockopt
= sock_no_setsockopt
,
728 .release
= af_alg_release
,
729 .sendmsg
= aead_sendmsg
,
730 .sendpage
= aead_sendpage
,
731 .recvmsg
= aead_recvmsg
,
735 static void *aead_bind(const char *name
, u32 type
, u32 mask
)
737 return crypto_alloc_aead(name
, type
, mask
);
740 static void aead_release(void *private)
742 crypto_free_aead(private);
745 static int aead_setauthsize(void *private, unsigned int authsize
)
747 return crypto_aead_setauthsize(private, authsize
);
750 static int aead_setkey(void *private, const u8
*key
, unsigned int keylen
)
752 return crypto_aead_setkey(private, key
, keylen
);
755 static void aead_sock_destruct(struct sock
*sk
)
757 struct alg_sock
*ask
= alg_sk(sk
);
758 struct aead_ctx
*ctx
= ask
->private;
759 unsigned int ivlen
= crypto_aead_ivsize(
760 crypto_aead_reqtfm(&ctx
->aead_req
));
762 WARN_ON(atomic_read(&sk
->sk_refcnt
) != 0);
764 sock_kzfree_s(sk
, ctx
->iv
, ivlen
);
765 sock_kfree_s(sk
, ctx
, ctx
->len
);
766 af_alg_release_parent(sk
);
769 static int aead_accept_parent(void *private, struct sock
*sk
)
771 struct aead_ctx
*ctx
;
772 struct alg_sock
*ask
= alg_sk(sk
);
773 unsigned int len
= sizeof(*ctx
) + crypto_aead_reqsize(private);
774 unsigned int ivlen
= crypto_aead_ivsize(private);
776 ctx
= sock_kmalloc(sk
, len
, GFP_KERNEL
);
781 ctx
->iv
= sock_kmalloc(sk
, ivlen
, GFP_KERNEL
);
783 sock_kfree_s(sk
, ctx
, len
);
786 memset(ctx
->iv
, 0, ivlen
);
794 ctx
->aead_assoclen
= 0;
795 af_alg_init_completion(&ctx
->completion
);
796 sg_init_table(ctx
->tsgl
.sg
, ALG_MAX_PAGES
);
797 INIT_LIST_HEAD(&ctx
->list
);
801 aead_request_set_tfm(&ctx
->aead_req
, private);
802 aead_request_set_callback(&ctx
->aead_req
, CRYPTO_TFM_REQ_MAY_BACKLOG
,
803 af_alg_complete
, &ctx
->completion
);
805 sk
->sk_destruct
= aead_sock_destruct
;
810 static const struct af_alg_type algif_type_aead
= {
812 .release
= aead_release
,
813 .setkey
= aead_setkey
,
814 .setauthsize
= aead_setauthsize
,
815 .accept
= aead_accept_parent
,
816 .ops
= &algif_aead_ops
,
821 static int __init
algif_aead_init(void)
823 return af_alg_register_type(&algif_type_aead
);
826 static void __exit
algif_aead_exit(void)
828 int err
= af_alg_unregister_type(&algif_type_aead
);
832 module_init(algif_aead_init
);
833 module_exit(algif_aead_exit
);
834 MODULE_LICENSE("GPL");
835 MODULE_AUTHOR("Stephan Mueller <smueller@chronox.de>");
836 MODULE_DESCRIPTION("AEAD kernel crypto API user space interface");