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/scatterwalk.h>
17 #include <crypto/if_alg.h>
18 #include <linux/init.h>
19 #include <linux/list.h>
20 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/net.h>
28 struct scatterlist sg[ALG_MAX_PAGES];
32 struct aead_sg_list tsgl;
34 * RSGL_MAX_ENTRIES is an artificial limit where user space at maximum
35 * can cause the kernel to allocate RSGL_MAX_ENTRIES * ALG_MAX_PAGES
38 #define RSGL_MAX_ENTRIES ALG_MAX_PAGES
39 struct af_alg_sgl rsgl[RSGL_MAX_ENTRIES];
43 struct af_alg_completion completion;
53 struct aead_request aead_req;
56 static inline int aead_sndbuf(struct sock *sk)
58 struct alg_sock *ask = alg_sk(sk);
59 struct aead_ctx *ctx = ask->private;
61 return max_t(int, max_t(int, sk->sk_sndbuf & PAGE_MASK, PAGE_SIZE) -
65 static inline bool aead_writable(struct sock *sk)
67 return PAGE_SIZE <= aead_sndbuf(sk);
70 static inline bool aead_sufficient_data(struct aead_ctx *ctx)
72 unsigned as = crypto_aead_authsize(crypto_aead_reqtfm(&ctx->aead_req));
74 return (ctx->used >= (ctx->aead_assoclen + (ctx->enc ? 0 : as)));
77 static void aead_put_sgl(struct sock *sk)
79 struct alg_sock *ask = alg_sk(sk);
80 struct aead_ctx *ctx = ask->private;
81 struct aead_sg_list *sgl = &ctx->tsgl;
82 struct scatterlist *sg = sgl->sg;
85 for (i = 0; i < sgl->cur; i++) {
89 put_page(sg_page(sg + i));
90 sg_assign_page(sg + i, NULL);
98 static void aead_wmem_wakeup(struct sock *sk)
100 struct socket_wq *wq;
102 if (!aead_writable(sk))
106 wq = rcu_dereference(sk->sk_wq);
107 if (wq_has_sleeper(wq))
108 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
111 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
115 static int aead_wait_for_data(struct sock *sk, unsigned flags)
117 struct alg_sock *ask = alg_sk(sk);
118 struct aead_ctx *ctx = ask->private;
121 int err = -ERESTARTSYS;
123 if (flags & MSG_DONTWAIT)
126 set_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
129 if (signal_pending(current))
131 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
132 timeout = MAX_SCHEDULE_TIMEOUT;
133 if (sk_wait_event(sk, &timeout, !ctx->more)) {
138 finish_wait(sk_sleep(sk), &wait);
140 clear_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
145 static void aead_data_wakeup(struct sock *sk)
147 struct alg_sock *ask = alg_sk(sk);
148 struct aead_ctx *ctx = ask->private;
149 struct socket_wq *wq;
157 wq = rcu_dereference(sk->sk_wq);
158 if (wq_has_sleeper(wq))
159 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
162 sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
166 static int aead_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
168 struct sock *sk = sock->sk;
169 struct alg_sock *ask = alg_sk(sk);
170 struct aead_ctx *ctx = ask->private;
172 crypto_aead_ivsize(crypto_aead_reqtfm(&ctx->aead_req));
173 struct aead_sg_list *sgl = &ctx->tsgl;
174 struct af_alg_control con = {};
180 if (msg->msg_controllen) {
181 err = af_alg_cmsg_send(msg, &con);
197 if (con.iv && con.iv->ivlen != ivsize)
202 if (!ctx->more && ctx->used)
208 memcpy(ctx->iv, con.iv->iv, ivsize);
210 ctx->aead_assoclen = con.aead_assoclen;
214 unsigned long len = size;
215 struct scatterlist *sg = NULL;
217 /* use the existing memory in an allocated page */
219 sg = sgl->sg + sgl->cur - 1;
220 len = min_t(unsigned long, len,
221 PAGE_SIZE - sg->offset - sg->length);
222 err = memcpy_from_msg(page_address(sg_page(sg)) +
223 sg->offset + sg->length,
229 ctx->merge = (sg->offset + sg->length) &
238 if (!aead_writable(sk)) {
239 /* user space sent too much data */
245 /* allocate a new page */
246 len = min_t(unsigned long, size, aead_sndbuf(sk));
250 if (sgl->cur >= ALG_MAX_PAGES) {
256 sg = sgl->sg + sgl->cur;
257 plen = min_t(int, len, PAGE_SIZE);
259 sg_assign_page(sg, alloc_page(GFP_KERNEL));
264 err = memcpy_from_msg(page_address(sg_page(sg)),
267 __free_page(sg_page(sg));
268 sg_assign_page(sg, NULL);
279 ctx->merge = plen & (PAGE_SIZE - 1);
285 ctx->more = msg->msg_flags & MSG_MORE;
286 if (!ctx->more && !aead_sufficient_data(ctx)) {
292 aead_data_wakeup(sk);
295 return err ?: copied;
298 static ssize_t aead_sendpage(struct socket *sock, struct page *page,
299 int offset, size_t size, int flags)
301 struct sock *sk = sock->sk;
302 struct alg_sock *ask = alg_sk(sk);
303 struct aead_ctx *ctx = ask->private;
304 struct aead_sg_list *sgl = &ctx->tsgl;
307 if (flags & MSG_SENDPAGE_NOTLAST)
310 if (sgl->cur >= ALG_MAX_PAGES)
314 if (!ctx->more && ctx->used)
320 if (!aead_writable(sk)) {
321 /* user space sent too much data */
330 sg_set_page(sgl->sg + sgl->cur, page, size, offset);
337 ctx->more = flags & MSG_MORE;
338 if (!ctx->more && !aead_sufficient_data(ctx)) {
344 aead_data_wakeup(sk);
350 static int aead_recvmsg(struct socket *sock, struct msghdr *msg, size_t ignored, int flags)
352 struct sock *sk = sock->sk;
353 struct alg_sock *ask = alg_sk(sk);
354 struct aead_ctx *ctx = ask->private;
355 unsigned bs = crypto_aead_blocksize(crypto_aead_reqtfm(&ctx->aead_req));
356 unsigned as = crypto_aead_authsize(crypto_aead_reqtfm(&ctx->aead_req));
357 struct aead_sg_list *sgl = &ctx->tsgl;
358 struct scatterlist *sg = NULL;
359 struct scatterlist assoc[ALG_MAX_PAGES];
363 unsigned long used = 0;
365 size_t usedpages = 0;
366 unsigned int cnt = 0;
368 /* Limit number of IOV blocks to be accessed below */
369 if (msg->msg_iter.nr_segs > RSGL_MAX_ENTRIES)
375 * AEAD memory structure: For encryption, the tag is appended to the
376 * ciphertext which implies that the memory allocated for the ciphertext
377 * must be increased by the tag length. For decryption, the tag
378 * is expected to be concatenated to the ciphertext. The plaintext
379 * therefore has a memory size of the ciphertext minus the tag length.
381 * The memory structure for cipher operation has the following
383 * AEAD encryption input: assoc data || plaintext
384 * AEAD encryption output: cipherntext || auth tag
385 * AEAD decryption input: assoc data || ciphertext || auth tag
386 * AEAD decryption output: plaintext
390 err = aead_wait_for_data(sk, flags);
398 * Make sure sufficient data is present -- note, the same check is
399 * is also present in sendmsg/sendpage. The checks in sendpage/sendmsg
400 * shall provide an information to the data sender that something is
401 * wrong, but they are irrelevant to maintain the kernel integrity.
402 * We need this check here too in case user space decides to not honor
403 * the error message in sendmsg/sendpage and still call recvmsg. This
404 * check here protects the kernel integrity.
406 if (!aead_sufficient_data(ctx))
410 * The cipher operation input data is reduced by the associated data
411 * length as this data is processed separately later on.
413 used -= ctx->aead_assoclen;
416 /* round up output buffer to multiple of block size */
417 outlen = ((used + bs - 1) / bs * bs);
418 /* add the size needed for the auth tag to be created */
421 /* output data size is input without the authentication tag */
423 /* round up output buffer to multiple of block size */
424 outlen = ((outlen + bs - 1) / bs * bs);
427 /* convert iovecs of output buffers into scatterlists */
428 while (iov_iter_count(&msg->msg_iter)) {
429 size_t seglen = min_t(size_t, iov_iter_count(&msg->msg_iter),
430 (outlen - usedpages));
432 /* make one iovec available as scatterlist */
433 err = af_alg_make_sg(&ctx->rsgl[cnt], &msg->msg_iter,
438 /* chain the new scatterlist with initial list */
440 scatterwalk_crypto_chain(ctx->rsgl[0].sg,
441 ctx->rsgl[cnt].sg, 1,
442 sg_nents(ctx->rsgl[cnt-1].sg));
443 /* we do not need more iovecs as we have sufficient memory */
444 if (outlen <= usedpages)
446 iov_iter_advance(&msg->msg_iter, err);
451 /* ensure output buffer is sufficiently large */
452 if (usedpages < outlen)
455 sg_init_table(assoc, ALG_MAX_PAGES);
456 assoclen = ctx->aead_assoclen;
458 * Split scatterlist into two: first part becomes AD, second part
459 * is plaintext / ciphertext. The first part is assigned to assoc
460 * scatterlist. When this loop finishes, sg points to the start of the
461 * plaintext / ciphertext.
463 for (i = 0; i < ctx->tsgl.cur; i++) {
465 if (sg->length <= assoclen) {
466 /* AD is larger than one page */
467 sg_set_page(assoc + i, sg_page(sg),
468 sg->length, sg->offset);
469 assoclen -= sg->length;
470 if (i >= ctx->tsgl.cur)
472 } else if (!assoclen) {
473 /* current page is to start of plaintext / ciphertext */
475 /* AD terminates at page boundary */
476 sg_mark_end(assoc + i - 1);
478 /* AD size is zero */
482 /* AD does not terminate at page boundary */
483 sg_set_page(assoc + i, sg_page(sg),
484 assoclen, sg->offset);
485 sg_mark_end(assoc + i);
486 /* plaintext / ciphertext starts after AD */
487 sg->length -= assoclen;
488 sg->offset += assoclen;
493 aead_request_set_assoc(&ctx->aead_req, assoc, ctx->aead_assoclen);
494 aead_request_set_crypt(&ctx->aead_req, sg, ctx->rsgl[0].sg, used,
497 err = af_alg_wait_for_completion(ctx->enc ?
498 crypto_aead_encrypt(&ctx->aead_req) :
499 crypto_aead_decrypt(&ctx->aead_req),
503 /* EBADMSG implies a valid cipher operation took place */
514 for (i = 0; i < cnt; i++)
515 af_alg_free_sg(&ctx->rsgl[i]);
517 aead_wmem_wakeup(sk);
520 return err ? err : outlen;
523 static unsigned int aead_poll(struct file *file, struct socket *sock,
526 struct sock *sk = sock->sk;
527 struct alg_sock *ask = alg_sk(sk);
528 struct aead_ctx *ctx = ask->private;
531 sock_poll_wait(file, sk_sleep(sk), wait);
535 mask |= POLLIN | POLLRDNORM;
537 if (aead_writable(sk))
538 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
543 static struct proto_ops algif_aead_ops = {
546 .connect = sock_no_connect,
547 .socketpair = sock_no_socketpair,
548 .getname = sock_no_getname,
549 .ioctl = sock_no_ioctl,
550 .listen = sock_no_listen,
551 .shutdown = sock_no_shutdown,
552 .getsockopt = sock_no_getsockopt,
553 .mmap = sock_no_mmap,
554 .bind = sock_no_bind,
555 .accept = sock_no_accept,
556 .setsockopt = sock_no_setsockopt,
558 .release = af_alg_release,
559 .sendmsg = aead_sendmsg,
560 .sendpage = aead_sendpage,
561 .recvmsg = aead_recvmsg,
565 static void *aead_bind(const char *name, u32 type, u32 mask)
567 return crypto_alloc_aead(name, type, mask);
570 static void aead_release(void *private)
572 crypto_free_aead(private);
575 static int aead_setauthsize(void *private, unsigned int authsize)
577 return crypto_aead_setauthsize(private, authsize);
580 static int aead_setkey(void *private, const u8 *key, unsigned int keylen)
582 return crypto_aead_setkey(private, key, keylen);
585 static void aead_sock_destruct(struct sock *sk)
587 struct alg_sock *ask = alg_sk(sk);
588 struct aead_ctx *ctx = ask->private;
589 unsigned int ivlen = crypto_aead_ivsize(
590 crypto_aead_reqtfm(&ctx->aead_req));
593 sock_kzfree_s(sk, ctx->iv, ivlen);
594 sock_kfree_s(sk, ctx, ctx->len);
595 af_alg_release_parent(sk);
598 static int aead_accept_parent(void *private, struct sock *sk)
600 struct aead_ctx *ctx;
601 struct alg_sock *ask = alg_sk(sk);
602 unsigned int len = sizeof(*ctx) + crypto_aead_reqsize(private);
603 unsigned int ivlen = crypto_aead_ivsize(private);
605 ctx = sock_kmalloc(sk, len, GFP_KERNEL);
610 ctx->iv = sock_kmalloc(sk, ivlen, GFP_KERNEL);
612 sock_kfree_s(sk, ctx, len);
615 memset(ctx->iv, 0, ivlen);
623 ctx->aead_assoclen = 0;
624 af_alg_init_completion(&ctx->completion);
625 sg_init_table(ctx->tsgl.sg, ALG_MAX_PAGES);
629 aead_request_set_tfm(&ctx->aead_req, private);
630 aead_request_set_callback(&ctx->aead_req, CRYPTO_TFM_REQ_MAY_BACKLOG,
631 af_alg_complete, &ctx->completion);
633 sk->sk_destruct = aead_sock_destruct;
638 static const struct af_alg_type algif_type_aead = {
640 .release = aead_release,
641 .setkey = aead_setkey,
642 .setauthsize = aead_setauthsize,
643 .accept = aead_accept_parent,
644 .ops = &algif_aead_ops,
649 static int __init algif_aead_init(void)
651 return af_alg_register_type(&algif_type_aead);
654 static void __exit algif_aead_exit(void)
656 int err = af_alg_unregister_type(&algif_type_aead);
660 module_init(algif_aead_init);
661 module_exit(algif_aead_exit);
662 MODULE_LICENSE("GPL");
663 MODULE_AUTHOR("Stephan Mueller <smueller@chronox.de>");
664 MODULE_DESCRIPTION("AEAD kernel crypto API user space interface");