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/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];
33 struct aead_sg_list tsgl;
35 * RSGL_MAX_ENTRIES is an artificial limit where user space at maximum
36 * can cause the kernel to allocate RSGL_MAX_ENTRIES * ALG_MAX_PAGES
39 #define RSGL_MAX_ENTRIES ALG_MAX_PAGES
40 struct af_alg_sgl rsgl[RSGL_MAX_ENTRIES];
44 struct af_alg_completion completion;
54 struct aead_request aead_req;
57 static inline int aead_sndbuf(struct sock *sk)
59 struct alg_sock *ask = alg_sk(sk);
60 struct aead_ctx *ctx = ask->private;
62 return max_t(int, max_t(int, sk->sk_sndbuf & PAGE_MASK, PAGE_SIZE) -
66 static inline bool aead_writable(struct sock *sk)
68 return PAGE_SIZE <= aead_sndbuf(sk);
71 static inline bool aead_sufficient_data(struct aead_ctx *ctx)
73 unsigned as = crypto_aead_authsize(crypto_aead_reqtfm(&ctx->aead_req));
75 return ctx->used >= ctx->aead_assoclen + as;
78 static void aead_put_sgl(struct sock *sk)
80 struct alg_sock *ask = alg_sk(sk);
81 struct aead_ctx *ctx = ask->private;
82 struct aead_sg_list *sgl = &ctx->tsgl;
83 struct scatterlist *sg = sgl->sg;
86 for (i = 0; i < sgl->cur; i++) {
90 put_page(sg_page(sg + i));
91 sg_assign_page(sg + i, NULL);
93 sg_init_table(sg, ALG_MAX_PAGES);
100 static void aead_wmem_wakeup(struct sock *sk)
102 struct socket_wq *wq;
104 if (!aead_writable(sk))
108 wq = rcu_dereference(sk->sk_wq);
109 if (wq_has_sleeper(wq))
110 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
113 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
117 static int aead_wait_for_data(struct sock *sk, unsigned flags)
119 struct alg_sock *ask = alg_sk(sk);
120 struct aead_ctx *ctx = ask->private;
123 int err = -ERESTARTSYS;
125 if (flags & MSG_DONTWAIT)
128 set_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
131 if (signal_pending(current))
133 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
134 timeout = MAX_SCHEDULE_TIMEOUT;
135 if (sk_wait_event(sk, &timeout, !ctx->more)) {
140 finish_wait(sk_sleep(sk), &wait);
142 clear_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
147 static void aead_data_wakeup(struct sock *sk)
149 struct alg_sock *ask = alg_sk(sk);
150 struct aead_ctx *ctx = ask->private;
151 struct socket_wq *wq;
159 wq = rcu_dereference(sk->sk_wq);
160 if (wq_has_sleeper(wq))
161 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
164 sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
168 static int aead_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
170 struct sock *sk = sock->sk;
171 struct alg_sock *ask = alg_sk(sk);
172 struct aead_ctx *ctx = ask->private;
174 crypto_aead_ivsize(crypto_aead_reqtfm(&ctx->aead_req));
175 struct aead_sg_list *sgl = &ctx->tsgl;
176 struct af_alg_control con = {};
182 if (msg->msg_controllen) {
183 err = af_alg_cmsg_send(msg, &con);
199 if (con.iv && con.iv->ivlen != ivsize)
204 if (!ctx->more && ctx->used)
210 memcpy(ctx->iv, con.iv->iv, ivsize);
212 ctx->aead_assoclen = con.aead_assoclen;
216 unsigned long len = size;
217 struct scatterlist *sg = NULL;
219 /* use the existing memory in an allocated page */
221 sg = sgl->sg + sgl->cur - 1;
222 len = min_t(unsigned long, len,
223 PAGE_SIZE - sg->offset - sg->length);
224 err = memcpy_from_msg(page_address(sg_page(sg)) +
225 sg->offset + sg->length,
231 ctx->merge = (sg->offset + sg->length) &
240 if (!aead_writable(sk)) {
241 /* user space sent too much data */
247 /* allocate a new page */
248 len = min_t(unsigned long, size, aead_sndbuf(sk));
252 if (sgl->cur >= ALG_MAX_PAGES) {
258 sg = sgl->sg + sgl->cur;
259 plen = min_t(int, len, PAGE_SIZE);
261 sg_assign_page(sg, alloc_page(GFP_KERNEL));
266 err = memcpy_from_msg(page_address(sg_page(sg)),
269 __free_page(sg_page(sg));
270 sg_assign_page(sg, NULL);
281 ctx->merge = plen & (PAGE_SIZE - 1);
287 ctx->more = msg->msg_flags & MSG_MORE;
288 if (!ctx->more && !aead_sufficient_data(ctx)) {
294 aead_data_wakeup(sk);
297 return err ?: copied;
300 static ssize_t aead_sendpage(struct socket *sock, struct page *page,
301 int offset, size_t size, int flags)
303 struct sock *sk = sock->sk;
304 struct alg_sock *ask = alg_sk(sk);
305 struct aead_ctx *ctx = ask->private;
306 struct aead_sg_list *sgl = &ctx->tsgl;
309 if (flags & MSG_SENDPAGE_NOTLAST)
312 if (sgl->cur >= ALG_MAX_PAGES)
316 if (!ctx->more && ctx->used)
322 if (!aead_writable(sk)) {
323 /* user space sent too much data */
332 sg_set_page(sgl->sg + sgl->cur, page, size, offset);
339 ctx->more = flags & MSG_MORE;
340 if (!ctx->more && !aead_sufficient_data(ctx)) {
346 aead_data_wakeup(sk);
352 static int aead_recvmsg(struct socket *sock, struct msghdr *msg, size_t ignored, int flags)
354 struct sock *sk = sock->sk;
355 struct alg_sock *ask = alg_sk(sk);
356 struct aead_ctx *ctx = ask->private;
357 unsigned as = crypto_aead_authsize(crypto_aead_reqtfm(&ctx->aead_req));
358 struct aead_sg_list *sgl = &ctx->tsgl;
361 unsigned long used = 0;
363 size_t usedpages = 0;
364 unsigned int cnt = 0;
366 /* Limit number of IOV blocks to be accessed below */
367 if (msg->msg_iter.nr_segs > RSGL_MAX_ENTRIES)
373 * AEAD memory structure: For encryption, the tag is appended to the
374 * ciphertext which implies that the memory allocated for the ciphertext
375 * must be increased by the tag length. For decryption, the tag
376 * is expected to be concatenated to the ciphertext. The plaintext
377 * therefore has a memory size of the ciphertext minus the tag length.
379 * The memory structure for cipher operation has the following
381 * AEAD encryption input: assoc data || plaintext
382 * AEAD encryption output: cipherntext || auth tag
383 * AEAD decryption input: assoc data || ciphertext || auth tag
384 * AEAD decryption output: plaintext
388 err = aead_wait_for_data(sk, flags);
396 * Make sure sufficient data is present -- note, the same check is
397 * is also present in sendmsg/sendpage. The checks in sendpage/sendmsg
398 * shall provide an information to the data sender that something is
399 * wrong, but they are irrelevant to maintain the kernel integrity.
400 * We need this check here too in case user space decides to not honor
401 * the error message in sendmsg/sendpage and still call recvmsg. This
402 * check here protects the kernel integrity.
404 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 + (ctx->enc ? as : 0);
415 /* convert iovecs of output buffers into scatterlists */
416 while (iov_iter_count(&msg->msg_iter)) {
417 size_t seglen = min_t(size_t, iov_iter_count(&msg->msg_iter),
418 (outlen - usedpages));
420 /* make one iovec available as scatterlist */
421 err = af_alg_make_sg(&ctx->rsgl[cnt], &msg->msg_iter,
426 /* chain the new scatterlist with previous one */
428 af_alg_link_sg(&ctx->rsgl[cnt-1], &ctx->rsgl[cnt]);
430 /* we do not need more iovecs as we have sufficient memory */
431 if (outlen <= usedpages)
433 iov_iter_advance(&msg->msg_iter, err);
438 /* ensure output buffer is sufficiently large */
439 if (usedpages < outlen)
442 sg_mark_end(sgl->sg + sgl->cur - 1);
444 aead_request_set_crypt(&ctx->aead_req, sgl->sg, ctx->rsgl[0].sg,
446 aead_request_set_ad(&ctx->aead_req, ctx->aead_assoclen);
448 err = af_alg_wait_for_completion(ctx->enc ?
449 crypto_aead_encrypt(&ctx->aead_req) :
450 crypto_aead_decrypt(&ctx->aead_req),
454 /* EBADMSG implies a valid cipher operation took place */
465 for (i = 0; i < cnt; i++)
466 af_alg_free_sg(&ctx->rsgl[i]);
468 aead_wmem_wakeup(sk);
471 return err ? err : outlen;
474 static unsigned int aead_poll(struct file *file, struct socket *sock,
477 struct sock *sk = sock->sk;
478 struct alg_sock *ask = alg_sk(sk);
479 struct aead_ctx *ctx = ask->private;
482 sock_poll_wait(file, sk_sleep(sk), wait);
486 mask |= POLLIN | POLLRDNORM;
488 if (aead_writable(sk))
489 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
494 static struct proto_ops algif_aead_ops = {
497 .connect = sock_no_connect,
498 .socketpair = sock_no_socketpair,
499 .getname = sock_no_getname,
500 .ioctl = sock_no_ioctl,
501 .listen = sock_no_listen,
502 .shutdown = sock_no_shutdown,
503 .getsockopt = sock_no_getsockopt,
504 .mmap = sock_no_mmap,
505 .bind = sock_no_bind,
506 .accept = sock_no_accept,
507 .setsockopt = sock_no_setsockopt,
509 .release = af_alg_release,
510 .sendmsg = aead_sendmsg,
511 .sendpage = aead_sendpage,
512 .recvmsg = aead_recvmsg,
516 static void *aead_bind(const char *name, u32 type, u32 mask)
518 return crypto_alloc_aead(name, type, mask);
521 static void aead_release(void *private)
523 crypto_free_aead(private);
526 static int aead_setauthsize(void *private, unsigned int authsize)
528 return crypto_aead_setauthsize(private, authsize);
531 static int aead_setkey(void *private, const u8 *key, unsigned int keylen)
533 return crypto_aead_setkey(private, key, keylen);
536 static void aead_sock_destruct(struct sock *sk)
538 struct alg_sock *ask = alg_sk(sk);
539 struct aead_ctx *ctx = ask->private;
540 unsigned int ivlen = crypto_aead_ivsize(
541 crypto_aead_reqtfm(&ctx->aead_req));
544 sock_kzfree_s(sk, ctx->iv, ivlen);
545 sock_kfree_s(sk, ctx, ctx->len);
546 af_alg_release_parent(sk);
549 static int aead_accept_parent(void *private, struct sock *sk)
551 struct aead_ctx *ctx;
552 struct alg_sock *ask = alg_sk(sk);
553 unsigned int len = sizeof(*ctx) + crypto_aead_reqsize(private);
554 unsigned int ivlen = crypto_aead_ivsize(private);
556 ctx = sock_kmalloc(sk, len, GFP_KERNEL);
561 ctx->iv = sock_kmalloc(sk, ivlen, GFP_KERNEL);
563 sock_kfree_s(sk, ctx, len);
566 memset(ctx->iv, 0, ivlen);
574 ctx->aead_assoclen = 0;
575 af_alg_init_completion(&ctx->completion);
576 sg_init_table(ctx->tsgl.sg, ALG_MAX_PAGES);
580 aead_request_set_tfm(&ctx->aead_req, private);
581 aead_request_set_callback(&ctx->aead_req, CRYPTO_TFM_REQ_MAY_BACKLOG,
582 af_alg_complete, &ctx->completion);
584 sk->sk_destruct = aead_sock_destruct;
589 static const struct af_alg_type algif_type_aead = {
591 .release = aead_release,
592 .setkey = aead_setkey,
593 .setauthsize = aead_setauthsize,
594 .accept = aead_accept_parent,
595 .ops = &algif_aead_ops,
600 static int __init algif_aead_init(void)
602 return af_alg_register_type(&algif_type_aead);
605 static void __exit algif_aead_exit(void)
607 int err = af_alg_unregister_type(&algif_type_aead);
611 module_init(algif_aead_init);
612 module_exit(algif_aead_exit);
613 MODULE_LICENSE("GPL");
614 MODULE_AUTHOR("Stephan Mueller <smueller@chronox.de>");
615 MODULE_DESCRIPTION("AEAD kernel crypto API user space interface");