2 * algif_skcipher: User-space interface for skcipher algorithms
4 * This file provides the user-space API for symmetric key ciphers.
6 * Copyright (c) 2010 Herbert Xu <herbert@gondor.apana.org.au>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the Free
10 * Software Foundation; either version 2 of the License, or (at your option)
15 #include <crypto/scatterwalk.h>
16 #include <crypto/skcipher.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>
26 struct skcipher_sg_list {
27 struct list_head list;
31 struct scatterlist sg[0];
35 struct list_head tsgl;
36 struct af_alg_sgl rsgl;
40 struct af_alg_completion completion;
49 struct ablkcipher_request req;
52 #define MAX_SGL_ENTS ((PAGE_SIZE - sizeof(struct skcipher_sg_list)) / \
53 sizeof(struct scatterlist) - 1)
55 static inline bool skcipher_writable(struct sock *sk)
57 struct alg_sock *ask = alg_sk(sk);
58 struct skcipher_ctx *ctx = ask->private;
60 return ctx->used + PAGE_SIZE <= max_t(int, sk->sk_sndbuf, PAGE_SIZE);
63 static int skcipher_alloc_sgl(struct sock *sk)
65 struct alg_sock *ask = alg_sk(sk);
66 struct skcipher_ctx *ctx = ask->private;
67 struct skcipher_sg_list *sgl;
68 struct scatterlist *sg = NULL;
70 sgl = list_entry(ctx->tsgl.prev, struct skcipher_sg_list, list);
71 if (!list_empty(&ctx->tsgl))
74 if (!sg || sgl->cur >= MAX_SGL_ENTS) {
75 sgl = sock_kmalloc(sk, sizeof(*sgl) +
76 sizeof(sgl->sg[0]) * (MAX_SGL_ENTS + 1),
81 sg_init_table(sgl->sg, MAX_SGL_ENTS + 1);
85 scatterwalk_sg_chain(sg, MAX_SGL_ENTS + 1, sgl->sg);
87 list_add_tail(&sgl->list, &ctx->tsgl);
93 static void skcipher_pull_sgl(struct sock *sk, int used)
95 struct alg_sock *ask = alg_sk(sk);
96 struct skcipher_ctx *ctx = ask->private;
97 struct skcipher_sg_list *sgl;
98 struct scatterlist *sg;
101 while (!list_empty(&ctx->tsgl)) {
102 sgl = list_first_entry(&ctx->tsgl, struct skcipher_sg_list,
106 for (i = 0; i < sgl->cur; i++) {
107 int plen = min_t(int, used, sg[i].length);
109 if (!sg_page(sg + i))
112 sg[i].length -= plen;
113 sg[i].offset += plen;
121 put_page(sg_page(sg + i));
122 sg_assign_page(sg + i, NULL);
125 list_del(&sgl->list);
126 sock_kfree_s(sk, sgl,
127 sizeof(*sgl) + sizeof(sgl->sg[0]) *
135 static void skcipher_free_sgl(struct sock *sk)
137 struct alg_sock *ask = alg_sk(sk);
138 struct skcipher_ctx *ctx = ask->private;
140 skcipher_pull_sgl(sk, ctx->used);
143 static int skcipher_wait_for_wmem(struct sock *sk, unsigned flags)
147 int err = -ERESTARTSYS;
149 if (flags & MSG_DONTWAIT)
152 set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
155 if (signal_pending(current))
157 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
158 timeout = MAX_SCHEDULE_TIMEOUT;
159 if (sk_wait_event(sk, &timeout, skcipher_writable(sk))) {
164 finish_wait(sk_sleep(sk), &wait);
169 static void skcipher_wmem_wakeup(struct sock *sk)
171 struct socket_wq *wq;
173 if (!skcipher_writable(sk))
177 wq = rcu_dereference(sk->sk_wq);
178 if (wq_has_sleeper(wq))
179 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
182 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
186 static int skcipher_wait_for_data(struct sock *sk, unsigned flags)
188 struct alg_sock *ask = alg_sk(sk);
189 struct skcipher_ctx *ctx = ask->private;
192 int err = -ERESTARTSYS;
194 if (flags & MSG_DONTWAIT) {
198 set_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
201 if (signal_pending(current))
203 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
204 timeout = MAX_SCHEDULE_TIMEOUT;
205 if (sk_wait_event(sk, &timeout, ctx->used)) {
210 finish_wait(sk_sleep(sk), &wait);
212 clear_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
217 static void skcipher_data_wakeup(struct sock *sk)
219 struct alg_sock *ask = alg_sk(sk);
220 struct skcipher_ctx *ctx = ask->private;
221 struct socket_wq *wq;
227 wq = rcu_dereference(sk->sk_wq);
228 if (wq_has_sleeper(wq))
229 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
232 sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
236 static int skcipher_sendmsg(struct kiocb *unused, struct socket *sock,
237 struct msghdr *msg, size_t size)
239 struct sock *sk = sock->sk;
240 struct alg_sock *ask = alg_sk(sk);
241 struct skcipher_ctx *ctx = ask->private;
242 struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(&ctx->req);
243 unsigned ivsize = crypto_ablkcipher_ivsize(tfm);
244 struct skcipher_sg_list *sgl;
245 struct af_alg_control con = {};
252 if (msg->msg_controllen) {
253 err = af_alg_cmsg_send(msg, &con);
268 if (con.iv && con.iv->ivlen != ivsize)
275 if (!ctx->more && ctx->used)
281 memcpy(ctx->iv, con.iv->iv, ivsize);
284 limit = max_t(int, sk->sk_sndbuf, PAGE_SIZE);
288 struct scatterlist *sg;
289 unsigned long len = size;
293 sgl = list_entry(ctx->tsgl.prev,
294 struct skcipher_sg_list, list);
295 sg = sgl->sg + sgl->cur - 1;
296 len = min_t(unsigned long, len,
297 PAGE_SIZE - sg->offset - sg->length);
299 err = memcpy_fromiovec(page_address(sg_page(sg)) +
300 sg->offset + sg->length,
306 ctx->merge = (sg->offset + sg->length) &
316 if (limit < PAGE_SIZE) {
317 err = skcipher_wait_for_wmem(sk, msg->msg_flags);
321 limit = max_t(int, sk->sk_sndbuf, PAGE_SIZE);
325 len = min_t(unsigned long, len, limit);
327 err = skcipher_alloc_sgl(sk);
331 sgl = list_entry(ctx->tsgl.prev, struct skcipher_sg_list, list);
335 plen = min_t(int, len, PAGE_SIZE);
337 sg_assign_page(sg + i, alloc_page(GFP_KERNEL));
339 if (!sg_page(sg + i))
342 err = memcpy_fromiovec(page_address(sg_page(sg + i)),
345 __free_page(sg_page(sg + i));
346 sg_assign_page(sg + i, NULL);
357 } while (len && sgl->cur < MAX_SGL_ENTS);
359 ctx->merge = plen & (PAGE_SIZE - 1);
364 ctx->more = msg->msg_flags & MSG_MORE;
365 if (!ctx->more && !list_empty(&ctx->tsgl))
366 sgl = list_entry(ctx->tsgl.prev, struct skcipher_sg_list, list);
369 skcipher_data_wakeup(sk);
372 return copied ?: err;
375 static ssize_t skcipher_sendpage(struct socket *sock, struct page *page,
376 int offset, size_t size, int flags)
378 struct sock *sk = sock->sk;
379 struct alg_sock *ask = alg_sk(sk);
380 struct skcipher_ctx *ctx = ask->private;
381 struct skcipher_sg_list *sgl;
386 if (!ctx->more && ctx->used)
392 limit = max_t(int, sk->sk_sndbuf, PAGE_SIZE);
395 if (limit < PAGE_SIZE) {
396 err = skcipher_wait_for_wmem(sk, flags);
400 limit = max_t(int, sk->sk_sndbuf, PAGE_SIZE);
404 err = skcipher_alloc_sgl(sk);
409 sgl = list_entry(ctx->tsgl.prev, struct skcipher_sg_list, list);
412 sg_set_page(sgl->sg + sgl->cur, page, size, offset);
417 ctx->more = flags & MSG_MORE;
418 if (!ctx->more && !list_empty(&ctx->tsgl))
419 sgl = list_entry(ctx->tsgl.prev, struct skcipher_sg_list, list);
422 skcipher_data_wakeup(sk);
428 static int skcipher_recvmsg(struct kiocb *unused, struct socket *sock,
429 struct msghdr *msg, size_t ignored, int flags)
431 struct sock *sk = sock->sk;
432 struct alg_sock *ask = alg_sk(sk);
433 struct skcipher_ctx *ctx = ask->private;
434 unsigned bs = crypto_ablkcipher_blocksize(crypto_ablkcipher_reqtfm(
436 struct skcipher_sg_list *sgl;
437 struct scatterlist *sg;
438 unsigned long iovlen;
445 for (iov = msg->msg_iov, iovlen = msg->msg_iovlen; iovlen > 0;
447 unsigned long seglen = iov->iov_len;
448 char __user *from = iov->iov_base;
451 sgl = list_first_entry(&ctx->tsgl,
452 struct skcipher_sg_list, list);
460 err = skcipher_wait_for_data(sk, flags);
465 used = min_t(unsigned long, used, seglen);
467 if (ctx->more || used < ctx->used)
474 used = af_alg_make_sg(&ctx->rsgl, from, used, 1);
479 ablkcipher_request_set_crypt(&ctx->req, sg,
483 err = af_alg_wait_for_completion(
485 crypto_ablkcipher_encrypt(&ctx->req) :
486 crypto_ablkcipher_decrypt(&ctx->req),
489 af_alg_free_sg(&ctx->rsgl);
497 skcipher_pull_sgl(sk, used);
504 skcipher_wmem_wakeup(sk);
507 return copied ?: err;
511 static unsigned int skcipher_poll(struct file *file, struct socket *sock,
514 struct sock *sk = sock->sk;
515 struct alg_sock *ask = alg_sk(sk);
516 struct skcipher_ctx *ctx = ask->private;
519 sock_poll_wait(file, sk_sleep(sk), wait);
523 mask |= POLLIN | POLLRDNORM;
525 if (skcipher_writable(sk))
526 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
531 static struct proto_ops algif_skcipher_ops = {
534 .connect = sock_no_connect,
535 .socketpair = sock_no_socketpair,
536 .getname = sock_no_getname,
537 .ioctl = sock_no_ioctl,
538 .listen = sock_no_listen,
539 .shutdown = sock_no_shutdown,
540 .getsockopt = sock_no_getsockopt,
541 .mmap = sock_no_mmap,
542 .bind = sock_no_bind,
543 .accept = sock_no_accept,
544 .setsockopt = sock_no_setsockopt,
546 .release = af_alg_release,
547 .sendmsg = skcipher_sendmsg,
548 .sendpage = skcipher_sendpage,
549 .recvmsg = skcipher_recvmsg,
550 .poll = skcipher_poll,
553 static void *skcipher_bind(const char *name, u32 type, u32 mask)
555 return crypto_alloc_ablkcipher(name, type, mask);
558 static void skcipher_release(void *private)
560 crypto_free_ablkcipher(private);
563 static int skcipher_setkey(void *private, const u8 *key, unsigned int keylen)
565 return crypto_ablkcipher_setkey(private, key, keylen);
568 static void skcipher_sock_destruct(struct sock *sk)
570 struct alg_sock *ask = alg_sk(sk);
571 struct skcipher_ctx *ctx = ask->private;
572 struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(&ctx->req);
574 skcipher_free_sgl(sk);
575 sock_kfree_s(sk, ctx->iv, crypto_ablkcipher_ivsize(tfm));
576 sock_kfree_s(sk, ctx, ctx->len);
577 af_alg_release_parent(sk);
580 static int skcipher_accept_parent(void *private, struct sock *sk)
582 struct skcipher_ctx *ctx;
583 struct alg_sock *ask = alg_sk(sk);
584 unsigned int len = sizeof(*ctx) + crypto_ablkcipher_reqsize(private);
586 ctx = sock_kmalloc(sk, len, GFP_KERNEL);
590 ctx->iv = sock_kmalloc(sk, crypto_ablkcipher_ivsize(private),
593 sock_kfree_s(sk, ctx, len);
597 memset(ctx->iv, 0, crypto_ablkcipher_ivsize(private));
599 INIT_LIST_HEAD(&ctx->tsgl);
605 af_alg_init_completion(&ctx->completion);
609 ablkcipher_request_set_tfm(&ctx->req, private);
610 ablkcipher_request_set_callback(&ctx->req, CRYPTO_TFM_REQ_MAY_BACKLOG,
611 af_alg_complete, &ctx->completion);
613 sk->sk_destruct = skcipher_sock_destruct;
618 static const struct af_alg_type algif_type_skcipher = {
619 .bind = skcipher_bind,
620 .release = skcipher_release,
621 .setkey = skcipher_setkey,
622 .accept = skcipher_accept_parent,
623 .ops = &algif_skcipher_ops,
628 static int __init algif_skcipher_init(void)
630 return af_alg_register_type(&algif_type_skcipher);
633 static void __exit algif_skcipher_exit(void)
635 int err = af_alg_unregister_type(&algif_type_skcipher);
639 module_init(algif_skcipher_init);
640 module_exit(algif_skcipher_exit);
641 MODULE_LICENSE("GPL");