blob: 903605dbc1a50282e8e3d7ced0bba148ec7ebe9c [file] [log] [blame]
Herbert Xu8ff59092010-10-19 21:31:55 +08001/*
2 * algif_skcipher: User-space interface for skcipher algorithms
3 *
4 * This file provides the user-space API for symmetric key ciphers.
5 *
6 * Copyright (c) 2010 Herbert Xu <herbert@gondor.apana.org.au>
7 *
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)
11 * any later version.
12 *
13 */
14
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>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010021#include <linux/sched/signal.h>
Herbert Xu8ff59092010-10-19 21:31:55 +080022#include <linux/mm.h>
23#include <linux/module.h>
24#include <linux/net.h>
25#include <net/sock.h>
26
27struct skcipher_sg_list {
28 struct list_head list;
29
30 int cur;
31
32 struct scatterlist sg[0];
33};
34
Herbert Xudd504582015-12-25 15:40:05 +080035struct skcipher_tfm {
36 struct crypto_skcipher *skcipher;
37 bool has_key;
38};
39
Herbert Xu8ff59092010-10-19 21:31:55 +080040struct skcipher_ctx {
41 struct list_head tsgl;
42 struct af_alg_sgl rsgl;
43
44 void *iv;
45
46 struct af_alg_completion completion;
47
Tadeusz Struka5969992015-03-19 12:31:40 -070048 atomic_t inflight;
LABBE Corentin652d5b82015-10-23 14:10:36 +020049 size_t used;
Herbert Xu8ff59092010-10-19 21:31:55 +080050
51 unsigned int len;
52 bool more;
53 bool merge;
54 bool enc;
55
Herbert Xu0d96e4b2015-12-18 19:16:57 +080056 struct skcipher_request req;
Herbert Xu8ff59092010-10-19 21:31:55 +080057};
58
Tadeusz Struka5969992015-03-19 12:31:40 -070059struct skcipher_async_rsgl {
60 struct af_alg_sgl sgl;
61 struct list_head list;
62};
63
64struct skcipher_async_req {
65 struct kiocb *iocb;
66 struct skcipher_async_rsgl first_sgl;
67 struct list_head list;
68 struct scatterlist *tsg;
Herbert Xuec69bbf2016-02-03 21:39:24 +080069 atomic_t *inflight;
70 struct skcipher_request req;
Tadeusz Struka5969992015-03-19 12:31:40 -070071};
72
Ondrej Kozinae2cffb52014-08-25 11:49:54 +020073#define MAX_SGL_ENTS ((4096 - sizeof(struct skcipher_sg_list)) / \
Herbert Xu8ff59092010-10-19 21:31:55 +080074 sizeof(struct scatterlist) - 1)
75
Tadeusz Struka5969992015-03-19 12:31:40 -070076static void skcipher_free_async_sgls(struct skcipher_async_req *sreq)
77{
78 struct skcipher_async_rsgl *rsgl, *tmp;
79 struct scatterlist *sgl;
80 struct scatterlist *sg;
81 int i, n;
82
83 list_for_each_entry_safe(rsgl, tmp, &sreq->list, list) {
84 af_alg_free_sg(&rsgl->sgl);
85 if (rsgl != &sreq->first_sgl)
86 kfree(rsgl);
87 }
88 sgl = sreq->tsg;
89 n = sg_nents(sgl);
Stephan Mueller445a5822017-08-16 11:56:24 +020090 for_each_sg(sgl, sg, n, i) {
91 struct page *page = sg_page(sg);
92
93 /* some SGs may not have a page mapped */
94 if (page && page_ref_count(page))
95 put_page(page);
96 }
Tadeusz Struka5969992015-03-19 12:31:40 -070097
98 kfree(sreq->tsg);
99}
100
101static void skcipher_async_cb(struct crypto_async_request *req, int err)
102{
Herbert Xuec69bbf2016-02-03 21:39:24 +0800103 struct skcipher_async_req *sreq = req->data;
Tadeusz Struka5969992015-03-19 12:31:40 -0700104 struct kiocb *iocb = sreq->iocb;
105
Herbert Xuec69bbf2016-02-03 21:39:24 +0800106 atomic_dec(sreq->inflight);
Tadeusz Struka5969992015-03-19 12:31:40 -0700107 skcipher_free_async_sgls(sreq);
Herbert Xuec69bbf2016-02-03 21:39:24 +0800108 kzfree(sreq);
Al Viro237dae82015-04-09 00:00:30 -0400109 iocb->ki_complete(iocb, err, err);
Tadeusz Struka5969992015-03-19 12:31:40 -0700110}
111
Herbert Xu0f6bb832010-11-30 16:49:02 +0800112static inline int skcipher_sndbuf(struct sock *sk)
Herbert Xu8ff59092010-10-19 21:31:55 +0800113{
114 struct alg_sock *ask = alg_sk(sk);
115 struct skcipher_ctx *ctx = ask->private;
116
Herbert Xu0f6bb832010-11-30 16:49:02 +0800117 return max_t(int, max_t(int, sk->sk_sndbuf & PAGE_MASK, PAGE_SIZE) -
118 ctx->used, 0);
119}
120
121static inline bool skcipher_writable(struct sock *sk)
122{
123 return PAGE_SIZE <= skcipher_sndbuf(sk);
Herbert Xu8ff59092010-10-19 21:31:55 +0800124}
125
126static int skcipher_alloc_sgl(struct sock *sk)
127{
128 struct alg_sock *ask = alg_sk(sk);
129 struct skcipher_ctx *ctx = ask->private;
130 struct skcipher_sg_list *sgl;
131 struct scatterlist *sg = NULL;
132
133 sgl = list_entry(ctx->tsgl.prev, struct skcipher_sg_list, list);
134 if (!list_empty(&ctx->tsgl))
135 sg = sgl->sg;
136
137 if (!sg || sgl->cur >= MAX_SGL_ENTS) {
138 sgl = sock_kmalloc(sk, sizeof(*sgl) +
139 sizeof(sgl->sg[0]) * (MAX_SGL_ENTS + 1),
140 GFP_KERNEL);
141 if (!sgl)
142 return -ENOMEM;
143
144 sg_init_table(sgl->sg, MAX_SGL_ENTS + 1);
145 sgl->cur = 0;
146
147 if (sg)
Dan Williamsc56f6d12015-08-07 18:15:13 +0200148 sg_chain(sg, MAX_SGL_ENTS + 1, sgl->sg);
Herbert Xu8ff59092010-10-19 21:31:55 +0800149
150 list_add_tail(&sgl->list, &ctx->tsgl);
151 }
152
153 return 0;
154}
155
LABBE Corentin652d5b82015-10-23 14:10:36 +0200156static void skcipher_pull_sgl(struct sock *sk, size_t used, int put)
Herbert Xu8ff59092010-10-19 21:31:55 +0800157{
158 struct alg_sock *ask = alg_sk(sk);
159 struct skcipher_ctx *ctx = ask->private;
160 struct skcipher_sg_list *sgl;
161 struct scatterlist *sg;
162 int i;
163
164 while (!list_empty(&ctx->tsgl)) {
165 sgl = list_first_entry(&ctx->tsgl, struct skcipher_sg_list,
166 list);
167 sg = sgl->sg;
168
169 for (i = 0; i < sgl->cur; i++) {
LABBE Corentin652d5b82015-10-23 14:10:36 +0200170 size_t plen = min_t(size_t, used, sg[i].length);
Herbert Xu8ff59092010-10-19 21:31:55 +0800171
172 if (!sg_page(sg + i))
173 continue;
174
175 sg[i].length -= plen;
176 sg[i].offset += plen;
177
178 used -= plen;
179 ctx->used -= plen;
180
181 if (sg[i].length)
182 return;
Tadeusz Struka5969992015-03-19 12:31:40 -0700183 if (put)
184 put_page(sg_page(sg + i));
Herbert Xu8ff59092010-10-19 21:31:55 +0800185 sg_assign_page(sg + i, NULL);
186 }
187
188 list_del(&sgl->list);
189 sock_kfree_s(sk, sgl,
190 sizeof(*sgl) + sizeof(sgl->sg[0]) *
191 (MAX_SGL_ENTS + 1));
192 }
193
194 if (!ctx->used)
195 ctx->merge = 0;
196}
197
198static void skcipher_free_sgl(struct sock *sk)
199{
200 struct alg_sock *ask = alg_sk(sk);
201 struct skcipher_ctx *ctx = ask->private;
202
Tadeusz Struka5969992015-03-19 12:31:40 -0700203 skcipher_pull_sgl(sk, ctx->used, 1);
Herbert Xu8ff59092010-10-19 21:31:55 +0800204}
205
206static int skcipher_wait_for_wmem(struct sock *sk, unsigned flags)
207{
WANG Congd9dc8b02016-11-11 10:20:50 -0800208 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Herbert Xu8ff59092010-10-19 21:31:55 +0800209 int err = -ERESTARTSYS;
WANG Congd9dc8b02016-11-11 10:20:50 -0800210 long timeout;
Herbert Xu8ff59092010-10-19 21:31:55 +0800211
212 if (flags & MSG_DONTWAIT)
213 return -EAGAIN;
214
Eric Dumazet9cd3e072015-11-29 20:03:10 -0800215 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
Herbert Xu8ff59092010-10-19 21:31:55 +0800216
WANG Congd9dc8b02016-11-11 10:20:50 -0800217 add_wait_queue(sk_sleep(sk), &wait);
Herbert Xu8ff59092010-10-19 21:31:55 +0800218 for (;;) {
219 if (signal_pending(current))
220 break;
Herbert Xu8ff59092010-10-19 21:31:55 +0800221 timeout = MAX_SCHEDULE_TIMEOUT;
WANG Congd9dc8b02016-11-11 10:20:50 -0800222 if (sk_wait_event(sk, &timeout, skcipher_writable(sk), &wait)) {
Herbert Xu8ff59092010-10-19 21:31:55 +0800223 err = 0;
224 break;
225 }
226 }
WANG Congd9dc8b02016-11-11 10:20:50 -0800227 remove_wait_queue(sk_sleep(sk), &wait);
Herbert Xu8ff59092010-10-19 21:31:55 +0800228
229 return err;
230}
231
232static void skcipher_wmem_wakeup(struct sock *sk)
233{
234 struct socket_wq *wq;
235
236 if (!skcipher_writable(sk))
237 return;
238
239 rcu_read_lock();
240 wq = rcu_dereference(sk->sk_wq);
Herbert Xu1ce0bf52015-11-26 13:55:39 +0800241 if (skwq_has_sleeper(wq))
Herbert Xu8ff59092010-10-19 21:31:55 +0800242 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
243 POLLRDNORM |
244 POLLRDBAND);
245 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
246 rcu_read_unlock();
247}
248
249static int skcipher_wait_for_data(struct sock *sk, unsigned flags)
250{
WANG Congd9dc8b02016-11-11 10:20:50 -0800251 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Herbert Xu8ff59092010-10-19 21:31:55 +0800252 struct alg_sock *ask = alg_sk(sk);
253 struct skcipher_ctx *ctx = ask->private;
254 long timeout;
Herbert Xu8ff59092010-10-19 21:31:55 +0800255 int err = -ERESTARTSYS;
256
257 if (flags & MSG_DONTWAIT) {
258 return -EAGAIN;
259 }
260
Eric Dumazet9cd3e072015-11-29 20:03:10 -0800261 sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
Herbert Xu8ff59092010-10-19 21:31:55 +0800262
WANG Congd9dc8b02016-11-11 10:20:50 -0800263 add_wait_queue(sk_sleep(sk), &wait);
Herbert Xu8ff59092010-10-19 21:31:55 +0800264 for (;;) {
265 if (signal_pending(current))
266 break;
Herbert Xu8ff59092010-10-19 21:31:55 +0800267 timeout = MAX_SCHEDULE_TIMEOUT;
WANG Congd9dc8b02016-11-11 10:20:50 -0800268 if (sk_wait_event(sk, &timeout, ctx->used, &wait)) {
Herbert Xu8ff59092010-10-19 21:31:55 +0800269 err = 0;
270 break;
271 }
272 }
WANG Congd9dc8b02016-11-11 10:20:50 -0800273 remove_wait_queue(sk_sleep(sk), &wait);
Herbert Xu8ff59092010-10-19 21:31:55 +0800274
Eric Dumazet9cd3e072015-11-29 20:03:10 -0800275 sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
Herbert Xu8ff59092010-10-19 21:31:55 +0800276
277 return err;
278}
279
280static void skcipher_data_wakeup(struct sock *sk)
281{
282 struct alg_sock *ask = alg_sk(sk);
283 struct skcipher_ctx *ctx = ask->private;
284 struct socket_wq *wq;
285
286 if (!ctx->used)
287 return;
288
289 rcu_read_lock();
290 wq = rcu_dereference(sk->sk_wq);
Herbert Xu1ce0bf52015-11-26 13:55:39 +0800291 if (skwq_has_sleeper(wq))
Herbert Xu8ff59092010-10-19 21:31:55 +0800292 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
293 POLLRDNORM |
294 POLLRDBAND);
295 sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
296 rcu_read_unlock();
297}
298
Ying Xue1b784142015-03-02 15:37:48 +0800299static int skcipher_sendmsg(struct socket *sock, struct msghdr *msg,
300 size_t size)
Herbert Xu8ff59092010-10-19 21:31:55 +0800301{
302 struct sock *sk = sock->sk;
303 struct alg_sock *ask = alg_sk(sk);
Herbert Xu6454c2b2016-02-03 21:39:26 +0800304 struct sock *psk = ask->parent;
305 struct alg_sock *pask = alg_sk(psk);
Herbert Xu8ff59092010-10-19 21:31:55 +0800306 struct skcipher_ctx *ctx = ask->private;
Herbert Xu6454c2b2016-02-03 21:39:26 +0800307 struct skcipher_tfm *skc = pask->private;
308 struct crypto_skcipher *tfm = skc->skcipher;
Herbert Xu0d96e4b2015-12-18 19:16:57 +0800309 unsigned ivsize = crypto_skcipher_ivsize(tfm);
Herbert Xu8ff59092010-10-19 21:31:55 +0800310 struct skcipher_sg_list *sgl;
311 struct af_alg_control con = {};
312 long copied = 0;
313 bool enc = 0;
Stephan Muellerf26b7b82014-11-30 10:55:26 +0100314 bool init = 0;
Herbert Xu8ff59092010-10-19 21:31:55 +0800315 int err;
316 int i;
317
318 if (msg->msg_controllen) {
319 err = af_alg_cmsg_send(msg, &con);
320 if (err)
321 return err;
322
Stephan Muellerf26b7b82014-11-30 10:55:26 +0100323 init = 1;
Herbert Xu8ff59092010-10-19 21:31:55 +0800324 switch (con.op) {
325 case ALG_OP_ENCRYPT:
326 enc = 1;
327 break;
328 case ALG_OP_DECRYPT:
329 enc = 0;
330 break;
331 default:
332 return -EINVAL;
333 }
334
335 if (con.iv && con.iv->ivlen != ivsize)
336 return -EINVAL;
337 }
338
339 err = -EINVAL;
340
341 lock_sock(sk);
342 if (!ctx->more && ctx->used)
343 goto unlock;
344
Stephan Muellerf26b7b82014-11-30 10:55:26 +0100345 if (init) {
Herbert Xu8ff59092010-10-19 21:31:55 +0800346 ctx->enc = enc;
347 if (con.iv)
348 memcpy(ctx->iv, con.iv->iv, ivsize);
349 }
350
Herbert Xu8ff59092010-10-19 21:31:55 +0800351 while (size) {
352 struct scatterlist *sg;
353 unsigned long len = size;
LABBE Corentin652d5b82015-10-23 14:10:36 +0200354 size_t plen;
Herbert Xu8ff59092010-10-19 21:31:55 +0800355
356 if (ctx->merge) {
357 sgl = list_entry(ctx->tsgl.prev,
358 struct skcipher_sg_list, list);
359 sg = sgl->sg + sgl->cur - 1;
360 len = min_t(unsigned long, len,
361 PAGE_SIZE - sg->offset - sg->length);
362
Al Viro6ce8e9c2014-04-06 21:25:44 -0400363 err = memcpy_from_msg(page_address(sg_page(sg)) +
364 sg->offset + sg->length,
365 msg, len);
Herbert Xu8ff59092010-10-19 21:31:55 +0800366 if (err)
367 goto unlock;
368
369 sg->length += len;
370 ctx->merge = (sg->offset + sg->length) &
371 (PAGE_SIZE - 1);
372
373 ctx->used += len;
374 copied += len;
375 size -= len;
Herbert Xu8ff59092010-10-19 21:31:55 +0800376 continue;
377 }
378
Herbert Xu0f6bb832010-11-30 16:49:02 +0800379 if (!skcipher_writable(sk)) {
Herbert Xu8ff59092010-10-19 21:31:55 +0800380 err = skcipher_wait_for_wmem(sk, msg->msg_flags);
381 if (err)
382 goto unlock;
Herbert Xu8ff59092010-10-19 21:31:55 +0800383 }
384
Herbert Xu0f6bb832010-11-30 16:49:02 +0800385 len = min_t(unsigned long, len, skcipher_sndbuf(sk));
Herbert Xu8ff59092010-10-19 21:31:55 +0800386
387 err = skcipher_alloc_sgl(sk);
388 if (err)
389 goto unlock;
390
391 sgl = list_entry(ctx->tsgl.prev, struct skcipher_sg_list, list);
392 sg = sgl->sg;
Herbert Xu202736d2016-01-19 21:23:57 +0800393 if (sgl->cur)
394 sg_unmark_end(sg + sgl->cur - 1);
Herbert Xu8ff59092010-10-19 21:31:55 +0800395 do {
396 i = sgl->cur;
LABBE Corentin652d5b82015-10-23 14:10:36 +0200397 plen = min_t(size_t, len, PAGE_SIZE);
Herbert Xu8ff59092010-10-19 21:31:55 +0800398
399 sg_assign_page(sg + i, alloc_page(GFP_KERNEL));
400 err = -ENOMEM;
401 if (!sg_page(sg + i))
402 goto unlock;
403
Al Viro6ce8e9c2014-04-06 21:25:44 -0400404 err = memcpy_from_msg(page_address(sg_page(sg + i)),
405 msg, plen);
Herbert Xu8ff59092010-10-19 21:31:55 +0800406 if (err) {
407 __free_page(sg_page(sg + i));
408 sg_assign_page(sg + i, NULL);
409 goto unlock;
410 }
411
412 sg[i].length = plen;
413 len -= plen;
414 ctx->used += plen;
415 copied += plen;
416 size -= plen;
Herbert Xu8ff59092010-10-19 21:31:55 +0800417 sgl->cur++;
418 } while (len && sgl->cur < MAX_SGL_ENTS);
419
Tadeusz Struk0f477b62014-12-08 12:03:42 -0800420 if (!size)
421 sg_mark_end(sg + sgl->cur - 1);
422
Herbert Xu8ff59092010-10-19 21:31:55 +0800423 ctx->merge = plen & (PAGE_SIZE - 1);
424 }
425
426 err = 0;
427
428 ctx->more = msg->msg_flags & MSG_MORE;
Herbert Xu8ff59092010-10-19 21:31:55 +0800429
430unlock:
431 skcipher_data_wakeup(sk);
432 release_sock(sk);
433
434 return copied ?: err;
435}
436
437static ssize_t skcipher_sendpage(struct socket *sock, struct page *page,
438 int offset, size_t size, int flags)
439{
440 struct sock *sk = sock->sk;
441 struct alg_sock *ask = alg_sk(sk);
442 struct skcipher_ctx *ctx = ask->private;
443 struct skcipher_sg_list *sgl;
444 int err = -EINVAL;
Herbert Xu8ff59092010-10-19 21:31:55 +0800445
Shawn Landdend3f7d562013-11-24 22:36:28 -0800446 if (flags & MSG_SENDPAGE_NOTLAST)
447 flags |= MSG_MORE;
448
Herbert Xu8ff59092010-10-19 21:31:55 +0800449 lock_sock(sk);
450 if (!ctx->more && ctx->used)
451 goto unlock;
452
453 if (!size)
454 goto done;
455
Herbert Xu0f6bb832010-11-30 16:49:02 +0800456 if (!skcipher_writable(sk)) {
Herbert Xu8ff59092010-10-19 21:31:55 +0800457 err = skcipher_wait_for_wmem(sk, flags);
458 if (err)
459 goto unlock;
Herbert Xu8ff59092010-10-19 21:31:55 +0800460 }
461
462 err = skcipher_alloc_sgl(sk);
463 if (err)
464 goto unlock;
465
466 ctx->merge = 0;
467 sgl = list_entry(ctx->tsgl.prev, struct skcipher_sg_list, list);
468
Tadeusz Struk0f477b62014-12-08 12:03:42 -0800469 if (sgl->cur)
470 sg_unmark_end(sgl->sg + sgl->cur - 1);
471
472 sg_mark_end(sgl->sg + sgl->cur);
Herbert Xu8ff59092010-10-19 21:31:55 +0800473 get_page(page);
474 sg_set_page(sgl->sg + sgl->cur, page, size, offset);
475 sgl->cur++;
476 ctx->used += size;
477
478done:
479 ctx->more = flags & MSG_MORE;
Herbert Xu8ff59092010-10-19 21:31:55 +0800480
481unlock:
482 skcipher_data_wakeup(sk);
483 release_sock(sk);
484
485 return err ?: size;
486}
487
Tadeusz Struka5969992015-03-19 12:31:40 -0700488static int skcipher_all_sg_nents(struct skcipher_ctx *ctx)
489{
490 struct skcipher_sg_list *sgl;
491 struct scatterlist *sg;
492 int nents = 0;
493
494 list_for_each_entry(sgl, &ctx->tsgl, list) {
495 sg = sgl->sg;
496
497 while (!sg->length)
498 sg++;
499
500 nents += sg_nents(sg);
501 }
502 return nents;
503}
504
505static int skcipher_recvmsg_async(struct socket *sock, struct msghdr *msg,
506 int flags)
507{
508 struct sock *sk = sock->sk;
509 struct alg_sock *ask = alg_sk(sk);
Herbert Xuec69bbf2016-02-03 21:39:24 +0800510 struct sock *psk = ask->parent;
511 struct alg_sock *pask = alg_sk(psk);
Tadeusz Struka5969992015-03-19 12:31:40 -0700512 struct skcipher_ctx *ctx = ask->private;
Herbert Xuec69bbf2016-02-03 21:39:24 +0800513 struct skcipher_tfm *skc = pask->private;
514 struct crypto_skcipher *tfm = skc->skcipher;
Tadeusz Struka5969992015-03-19 12:31:40 -0700515 struct skcipher_sg_list *sgl;
516 struct scatterlist *sg;
517 struct skcipher_async_req *sreq;
Herbert Xu0d96e4b2015-12-18 19:16:57 +0800518 struct skcipher_request *req;
Tadeusz Struka5969992015-03-19 12:31:40 -0700519 struct skcipher_async_rsgl *last_rsgl = NULL;
Herbert Xu6454c2b2016-02-03 21:39:26 +0800520 unsigned int txbufs = 0, len = 0, tx_nents;
Herbert Xuec69bbf2016-02-03 21:39:24 +0800521 unsigned int reqsize = crypto_skcipher_reqsize(tfm);
522 unsigned int ivsize = crypto_skcipher_ivsize(tfm);
Tadeusz Struka5969992015-03-19 12:31:40 -0700523 int err = -ENOMEM;
tadeusz.struk@intel.com033f46b2015-04-01 13:53:06 -0700524 bool mark = false;
Herbert Xuec69bbf2016-02-03 21:39:24 +0800525 char *iv;
526
527 sreq = kzalloc(sizeof(*sreq) + reqsize + ivsize, GFP_KERNEL);
528 if (unlikely(!sreq))
529 goto out;
530
531 req = &sreq->req;
532 iv = (char *)(req + 1) + reqsize;
533 sreq->iocb = msg->msg_iocb;
534 INIT_LIST_HEAD(&sreq->list);
535 sreq->inflight = &ctx->inflight;
Tadeusz Struka5969992015-03-19 12:31:40 -0700536
537 lock_sock(sk);
Herbert Xu6454c2b2016-02-03 21:39:26 +0800538 tx_nents = skcipher_all_sg_nents(ctx);
Tadeusz Struka5969992015-03-19 12:31:40 -0700539 sreq->tsg = kcalloc(tx_nents, sizeof(*sg), GFP_KERNEL);
Herbert Xuec69bbf2016-02-03 21:39:24 +0800540 if (unlikely(!sreq->tsg))
Tadeusz Struka5969992015-03-19 12:31:40 -0700541 goto unlock;
Tadeusz Struka5969992015-03-19 12:31:40 -0700542 sg_init_table(sreq->tsg, tx_nents);
Herbert Xuec69bbf2016-02-03 21:39:24 +0800543 memcpy(iv, ctx->iv, ivsize);
544 skcipher_request_set_tfm(req, tfm);
Herbert Xudad41992016-02-03 21:39:27 +0800545 skcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_SLEEP,
Herbert Xuec69bbf2016-02-03 21:39:24 +0800546 skcipher_async_cb, sreq);
Tadeusz Struka5969992015-03-19 12:31:40 -0700547
548 while (iov_iter_count(&msg->msg_iter)) {
549 struct skcipher_async_rsgl *rsgl;
tadeusz.struk@intel.comac110f42015-03-25 07:29:19 -0700550 int used;
Tadeusz Struka5969992015-03-19 12:31:40 -0700551
552 if (!ctx->used) {
553 err = skcipher_wait_for_data(sk, flags);
554 if (err)
555 goto free;
556 }
557 sgl = list_first_entry(&ctx->tsgl,
558 struct skcipher_sg_list, list);
559 sg = sgl->sg;
560
561 while (!sg->length)
562 sg++;
563
564 used = min_t(unsigned long, ctx->used,
565 iov_iter_count(&msg->msg_iter));
566 used = min_t(unsigned long, used, sg->length);
567
tadeusz.struk@intel.com033f46b2015-04-01 13:53:06 -0700568 if (txbufs == tx_nents) {
Tadeusz Struka5969992015-03-19 12:31:40 -0700569 struct scatterlist *tmp;
570 int x;
571 /* Ran out of tx slots in async request
572 * need to expand */
573 tmp = kcalloc(tx_nents * 2, sizeof(*tmp),
574 GFP_KERNEL);
Pan Biane2c1b822016-12-01 10:04:43 +0800575 if (!tmp) {
576 err = -ENOMEM;
Tadeusz Struka5969992015-03-19 12:31:40 -0700577 goto free;
Pan Biane2c1b822016-12-01 10:04:43 +0800578 }
Tadeusz Struka5969992015-03-19 12:31:40 -0700579
580 sg_init_table(tmp, tx_nents * 2);
581 for (x = 0; x < tx_nents; x++)
582 sg_set_page(&tmp[x], sg_page(&sreq->tsg[x]),
583 sreq->tsg[x].length,
584 sreq->tsg[x].offset);
585 kfree(sreq->tsg);
586 sreq->tsg = tmp;
587 tx_nents *= 2;
tadeusz.struk@intel.com033f46b2015-04-01 13:53:06 -0700588 mark = true;
Tadeusz Struka5969992015-03-19 12:31:40 -0700589 }
590 /* Need to take over the tx sgl from ctx
591 * to the asynch req - these sgls will be freed later */
tadeusz.struk@intel.com033f46b2015-04-01 13:53:06 -0700592 sg_set_page(sreq->tsg + txbufs++, sg_page(sg), sg->length,
Tadeusz Struka5969992015-03-19 12:31:40 -0700593 sg->offset);
594
595 if (list_empty(&sreq->list)) {
596 rsgl = &sreq->first_sgl;
597 list_add_tail(&rsgl->list, &sreq->list);
598 } else {
Tadeusz Struk82d92922015-03-30 14:25:44 -0700599 rsgl = kmalloc(sizeof(*rsgl), GFP_KERNEL);
Tadeusz Struka5969992015-03-19 12:31:40 -0700600 if (!rsgl) {
601 err = -ENOMEM;
602 goto free;
603 }
604 list_add_tail(&rsgl->list, &sreq->list);
605 }
606
607 used = af_alg_make_sg(&rsgl->sgl, &msg->msg_iter, used);
608 err = used;
609 if (used < 0)
610 goto free;
611 if (last_rsgl)
612 af_alg_link_sg(&last_rsgl->sgl, &rsgl->sgl);
613
614 last_rsgl = rsgl;
615 len += used;
616 skcipher_pull_sgl(sk, used, 0);
617 iov_iter_advance(&msg->msg_iter, used);
618 }
619
tadeusz.struk@intel.com033f46b2015-04-01 13:53:06 -0700620 if (mark)
621 sg_mark_end(sreq->tsg + txbufs - 1);
622
Herbert Xu0d96e4b2015-12-18 19:16:57 +0800623 skcipher_request_set_crypt(req, sreq->tsg, sreq->first_sgl.sgl.sg,
Herbert Xuec69bbf2016-02-03 21:39:24 +0800624 len, iv);
Herbert Xu0d96e4b2015-12-18 19:16:57 +0800625 err = ctx->enc ? crypto_skcipher_encrypt(req) :
626 crypto_skcipher_decrypt(req);
Tadeusz Struka5969992015-03-19 12:31:40 -0700627 if (err == -EINPROGRESS) {
628 atomic_inc(&ctx->inflight);
629 err = -EIOCBQUEUED;
Herbert Xuec69bbf2016-02-03 21:39:24 +0800630 sreq = NULL;
Tadeusz Struka5969992015-03-19 12:31:40 -0700631 goto unlock;
632 }
633free:
634 skcipher_free_async_sgls(sreq);
Tadeusz Struka5969992015-03-19 12:31:40 -0700635unlock:
636 skcipher_wmem_wakeup(sk);
637 release_sock(sk);
Herbert Xuec69bbf2016-02-03 21:39:24 +0800638 kzfree(sreq);
639out:
Tadeusz Struka5969992015-03-19 12:31:40 -0700640 return err;
641}
642
643static int skcipher_recvmsg_sync(struct socket *sock, struct msghdr *msg,
644 int flags)
Herbert Xu8ff59092010-10-19 21:31:55 +0800645{
646 struct sock *sk = sock->sk;
647 struct alg_sock *ask = alg_sk(sk);
Herbert Xu6454c2b2016-02-03 21:39:26 +0800648 struct sock *psk = ask->parent;
649 struct alg_sock *pask = alg_sk(psk);
Herbert Xu8ff59092010-10-19 21:31:55 +0800650 struct skcipher_ctx *ctx = ask->private;
Herbert Xu6454c2b2016-02-03 21:39:26 +0800651 struct skcipher_tfm *skc = pask->private;
652 struct crypto_skcipher *tfm = skc->skcipher;
653 unsigned bs = crypto_skcipher_blocksize(tfm);
Herbert Xu8ff59092010-10-19 21:31:55 +0800654 struct skcipher_sg_list *sgl;
655 struct scatterlist *sg;
Herbert Xu8ff59092010-10-19 21:31:55 +0800656 int err = -EAGAIN;
657 int used;
658 long copied = 0;
659
660 lock_sock(sk);
Al Viro01e97e62014-12-15 21:39:31 -0500661 while (msg_data_left(msg)) {
Linus Torvalds9399f0c2015-02-10 19:55:45 -0800662 if (!ctx->used) {
Al Viro1d10eb22014-11-28 16:39:25 -0500663 err = skcipher_wait_for_data(sk, flags);
664 if (err)
Herbert Xubc97e572010-11-30 17:04:31 +0800665 goto unlock;
Al Viro1d10eb22014-11-28 16:39:25 -0500666 }
Herbert Xubc97e572010-11-30 17:04:31 +0800667
Al Viro01e97e62014-12-15 21:39:31 -0500668 used = min_t(unsigned long, ctx->used, msg_data_left(msg));
Herbert Xu8ff59092010-10-19 21:31:55 +0800669
Al Viro1d10eb22014-11-28 16:39:25 -0500670 used = af_alg_make_sg(&ctx->rsgl, &msg->msg_iter, used);
671 err = used;
672 if (err < 0)
673 goto unlock;
Herbert Xu8ff59092010-10-19 21:31:55 +0800674
Al Viro1d10eb22014-11-28 16:39:25 -0500675 if (ctx->more || used < ctx->used)
676 used -= used % bs;
Herbert Xu8ff59092010-10-19 21:31:55 +0800677
Al Viro1d10eb22014-11-28 16:39:25 -0500678 err = -EINVAL;
679 if (!used)
680 goto free;
681
Herbert Xu4f0414e2016-01-18 18:46:10 +0800682 sgl = list_first_entry(&ctx->tsgl,
683 struct skcipher_sg_list, list);
684 sg = sgl->sg;
685
686 while (!sg->length)
687 sg++;
688
Herbert Xu0d96e4b2015-12-18 19:16:57 +0800689 skcipher_request_set_crypt(&ctx->req, sg, ctx->rsgl.sg, used,
690 ctx->iv);
Al Viro1d10eb22014-11-28 16:39:25 -0500691
692 err = af_alg_wait_for_completion(
Herbert Xu8ff59092010-10-19 21:31:55 +0800693 ctx->enc ?
Herbert Xu0d96e4b2015-12-18 19:16:57 +0800694 crypto_skcipher_encrypt(&ctx->req) :
695 crypto_skcipher_decrypt(&ctx->req),
Herbert Xu8ff59092010-10-19 21:31:55 +0800696 &ctx->completion);
697
Herbert Xubc97e572010-11-30 17:04:31 +0800698free:
Al Viro1d10eb22014-11-28 16:39:25 -0500699 af_alg_free_sg(&ctx->rsgl);
Herbert Xu8ff59092010-10-19 21:31:55 +0800700
Al Viro1d10eb22014-11-28 16:39:25 -0500701 if (err)
702 goto unlock;
Herbert Xu8ff59092010-10-19 21:31:55 +0800703
Al Viro1d10eb22014-11-28 16:39:25 -0500704 copied += used;
Tadeusz Struka5969992015-03-19 12:31:40 -0700705 skcipher_pull_sgl(sk, used, 1);
Al Viro1d10eb22014-11-28 16:39:25 -0500706 iov_iter_advance(&msg->msg_iter, used);
Herbert Xu8ff59092010-10-19 21:31:55 +0800707 }
708
709 err = 0;
710
711unlock:
712 skcipher_wmem_wakeup(sk);
713 release_sock(sk);
714
715 return copied ?: err;
716}
717
Tadeusz Struka5969992015-03-19 12:31:40 -0700718static int skcipher_recvmsg(struct socket *sock, struct msghdr *msg,
719 size_t ignored, int flags)
720{
721 return (msg->msg_iocb && !is_sync_kiocb(msg->msg_iocb)) ?
722 skcipher_recvmsg_async(sock, msg, flags) :
723 skcipher_recvmsg_sync(sock, msg, flags);
724}
Herbert Xu8ff59092010-10-19 21:31:55 +0800725
726static unsigned int skcipher_poll(struct file *file, struct socket *sock,
727 poll_table *wait)
728{
729 struct sock *sk = sock->sk;
730 struct alg_sock *ask = alg_sk(sk);
731 struct skcipher_ctx *ctx = ask->private;
732 unsigned int mask;
733
734 sock_poll_wait(file, sk_sleep(sk), wait);
735 mask = 0;
736
737 if (ctx->used)
738 mask |= POLLIN | POLLRDNORM;
739
740 if (skcipher_writable(sk))
741 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
742
743 return mask;
744}
745
746static struct proto_ops algif_skcipher_ops = {
747 .family = PF_ALG,
748
749 .connect = sock_no_connect,
750 .socketpair = sock_no_socketpair,
751 .getname = sock_no_getname,
752 .ioctl = sock_no_ioctl,
753 .listen = sock_no_listen,
754 .shutdown = sock_no_shutdown,
755 .getsockopt = sock_no_getsockopt,
756 .mmap = sock_no_mmap,
757 .bind = sock_no_bind,
758 .accept = sock_no_accept,
759 .setsockopt = sock_no_setsockopt,
760
761 .release = af_alg_release,
762 .sendmsg = skcipher_sendmsg,
763 .sendpage = skcipher_sendpage,
764 .recvmsg = skcipher_recvmsg,
765 .poll = skcipher_poll,
766};
767
Herbert Xua0fa2d02016-01-04 13:36:12 +0900768static int skcipher_check_key(struct socket *sock)
769{
Herbert Xu18227932016-01-15 22:02:20 +0800770 int err = 0;
Herbert Xua0fa2d02016-01-04 13:36:12 +0900771 struct sock *psk;
772 struct alg_sock *pask;
773 struct skcipher_tfm *tfm;
774 struct sock *sk = sock->sk;
775 struct alg_sock *ask = alg_sk(sk);
776
Herbert Xu18227932016-01-15 22:02:20 +0800777 lock_sock(sk);
Herbert Xua0fa2d02016-01-04 13:36:12 +0900778 if (ask->refcnt)
Herbert Xu18227932016-01-15 22:02:20 +0800779 goto unlock_child;
Herbert Xua0fa2d02016-01-04 13:36:12 +0900780
781 psk = ask->parent;
782 pask = alg_sk(ask->parent);
783 tfm = pask->private;
784
785 err = -ENOKEY;
Herbert Xu18227932016-01-15 22:02:20 +0800786 lock_sock_nested(psk, SINGLE_DEPTH_NESTING);
Herbert Xua0fa2d02016-01-04 13:36:12 +0900787 if (!tfm->has_key)
788 goto unlock;
789
790 if (!pask->refcnt++)
791 sock_hold(psk);
792
793 ask->refcnt = 1;
794 sock_put(psk);
795
796 err = 0;
797
798unlock:
799 release_sock(psk);
Herbert Xu18227932016-01-15 22:02:20 +0800800unlock_child:
801 release_sock(sk);
Herbert Xua0fa2d02016-01-04 13:36:12 +0900802
803 return err;
804}
805
806static int skcipher_sendmsg_nokey(struct socket *sock, struct msghdr *msg,
807 size_t size)
808{
809 int err;
810
811 err = skcipher_check_key(sock);
812 if (err)
813 return err;
814
815 return skcipher_sendmsg(sock, msg, size);
816}
817
818static ssize_t skcipher_sendpage_nokey(struct socket *sock, struct page *page,
819 int offset, size_t size, int flags)
820{
821 int err;
822
823 err = skcipher_check_key(sock);
824 if (err)
825 return err;
826
827 return skcipher_sendpage(sock, page, offset, size, flags);
828}
829
830static int skcipher_recvmsg_nokey(struct socket *sock, struct msghdr *msg,
831 size_t ignored, int flags)
832{
833 int err;
834
835 err = skcipher_check_key(sock);
836 if (err)
837 return err;
838
839 return skcipher_recvmsg(sock, msg, ignored, flags);
840}
841
842static struct proto_ops algif_skcipher_ops_nokey = {
843 .family = PF_ALG,
844
845 .connect = sock_no_connect,
846 .socketpair = sock_no_socketpair,
847 .getname = sock_no_getname,
848 .ioctl = sock_no_ioctl,
849 .listen = sock_no_listen,
850 .shutdown = sock_no_shutdown,
851 .getsockopt = sock_no_getsockopt,
852 .mmap = sock_no_mmap,
853 .bind = sock_no_bind,
854 .accept = sock_no_accept,
855 .setsockopt = sock_no_setsockopt,
856
857 .release = af_alg_release,
858 .sendmsg = skcipher_sendmsg_nokey,
859 .sendpage = skcipher_sendpage_nokey,
860 .recvmsg = skcipher_recvmsg_nokey,
861 .poll = skcipher_poll,
862};
863
Herbert Xu8ff59092010-10-19 21:31:55 +0800864static void *skcipher_bind(const char *name, u32 type, u32 mask)
865{
Herbert Xudd504582015-12-25 15:40:05 +0800866 struct skcipher_tfm *tfm;
867 struct crypto_skcipher *skcipher;
868
869 tfm = kzalloc(sizeof(*tfm), GFP_KERNEL);
870 if (!tfm)
871 return ERR_PTR(-ENOMEM);
872
873 skcipher = crypto_alloc_skcipher(name, type, mask);
874 if (IS_ERR(skcipher)) {
875 kfree(tfm);
876 return ERR_CAST(skcipher);
877 }
878
879 tfm->skcipher = skcipher;
880
881 return tfm;
Herbert Xu8ff59092010-10-19 21:31:55 +0800882}
883
884static void skcipher_release(void *private)
885{
Herbert Xudd504582015-12-25 15:40:05 +0800886 struct skcipher_tfm *tfm = private;
887
888 crypto_free_skcipher(tfm->skcipher);
889 kfree(tfm);
Herbert Xu8ff59092010-10-19 21:31:55 +0800890}
891
892static int skcipher_setkey(void *private, const u8 *key, unsigned int keylen)
893{
Herbert Xudd504582015-12-25 15:40:05 +0800894 struct skcipher_tfm *tfm = private;
895 int err;
896
897 err = crypto_skcipher_setkey(tfm->skcipher, key, keylen);
898 tfm->has_key = !err;
899
900 return err;
Herbert Xu8ff59092010-10-19 21:31:55 +0800901}
902
Tadeusz Struka5969992015-03-19 12:31:40 -0700903static void skcipher_wait(struct sock *sk)
904{
905 struct alg_sock *ask = alg_sk(sk);
906 struct skcipher_ctx *ctx = ask->private;
907 int ctr = 0;
908
909 while (atomic_read(&ctx->inflight) && ctr++ < 100)
910 msleep(100);
911}
912
Herbert Xu8ff59092010-10-19 21:31:55 +0800913static void skcipher_sock_destruct(struct sock *sk)
914{
915 struct alg_sock *ask = alg_sk(sk);
916 struct skcipher_ctx *ctx = ask->private;
Herbert Xu0d96e4b2015-12-18 19:16:57 +0800917 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(&ctx->req);
Herbert Xu8ff59092010-10-19 21:31:55 +0800918
Tadeusz Struka5969992015-03-19 12:31:40 -0700919 if (atomic_read(&ctx->inflight))
920 skcipher_wait(sk);
921
Herbert Xu8ff59092010-10-19 21:31:55 +0800922 skcipher_free_sgl(sk);
Herbert Xu0d96e4b2015-12-18 19:16:57 +0800923 sock_kzfree_s(sk, ctx->iv, crypto_skcipher_ivsize(tfm));
Herbert Xu8ff59092010-10-19 21:31:55 +0800924 sock_kfree_s(sk, ctx, ctx->len);
925 af_alg_release_parent(sk);
926}
927
Herbert Xud7b65ae2016-01-13 15:01:06 +0800928static int skcipher_accept_parent_nokey(void *private, struct sock *sk)
Herbert Xu8ff59092010-10-19 21:31:55 +0800929{
930 struct skcipher_ctx *ctx;
931 struct alg_sock *ask = alg_sk(sk);
Herbert Xudd504582015-12-25 15:40:05 +0800932 struct skcipher_tfm *tfm = private;
933 struct crypto_skcipher *skcipher = tfm->skcipher;
934 unsigned int len = sizeof(*ctx) + crypto_skcipher_reqsize(skcipher);
Herbert Xu8ff59092010-10-19 21:31:55 +0800935
936 ctx = sock_kmalloc(sk, len, GFP_KERNEL);
937 if (!ctx)
938 return -ENOMEM;
939
Herbert Xudd504582015-12-25 15:40:05 +0800940 ctx->iv = sock_kmalloc(sk, crypto_skcipher_ivsize(skcipher),
Herbert Xu8ff59092010-10-19 21:31:55 +0800941 GFP_KERNEL);
942 if (!ctx->iv) {
943 sock_kfree_s(sk, ctx, len);
944 return -ENOMEM;
945 }
946
Herbert Xudd504582015-12-25 15:40:05 +0800947 memset(ctx->iv, 0, crypto_skcipher_ivsize(skcipher));
Herbert Xu8ff59092010-10-19 21:31:55 +0800948
949 INIT_LIST_HEAD(&ctx->tsgl);
950 ctx->len = len;
951 ctx->used = 0;
952 ctx->more = 0;
953 ctx->merge = 0;
954 ctx->enc = 0;
Tadeusz Struka5969992015-03-19 12:31:40 -0700955 atomic_set(&ctx->inflight, 0);
Herbert Xu8ff59092010-10-19 21:31:55 +0800956 af_alg_init_completion(&ctx->completion);
957
958 ask->private = ctx;
959
Herbert Xudd504582015-12-25 15:40:05 +0800960 skcipher_request_set_tfm(&ctx->req, skcipher);
Herbert Xudad41992016-02-03 21:39:27 +0800961 skcipher_request_set_callback(&ctx->req, CRYPTO_TFM_REQ_MAY_SLEEP |
962 CRYPTO_TFM_REQ_MAY_BACKLOG,
Herbert Xu0d96e4b2015-12-18 19:16:57 +0800963 af_alg_complete, &ctx->completion);
Herbert Xu8ff59092010-10-19 21:31:55 +0800964
965 sk->sk_destruct = skcipher_sock_destruct;
966
967 return 0;
968}
969
Herbert Xua0fa2d02016-01-04 13:36:12 +0900970static int skcipher_accept_parent(void *private, struct sock *sk)
971{
972 struct skcipher_tfm *tfm = private;
973
Herbert Xu6e8d8ec2016-01-11 21:29:41 +0800974 if (!tfm->has_key && crypto_skcipher_has_setkey(tfm->skcipher))
Herbert Xua0fa2d02016-01-04 13:36:12 +0900975 return -ENOKEY;
976
Herbert Xud7b65ae2016-01-13 15:01:06 +0800977 return skcipher_accept_parent_nokey(private, sk);
Herbert Xua0fa2d02016-01-04 13:36:12 +0900978}
979
Herbert Xu8ff59092010-10-19 21:31:55 +0800980static const struct af_alg_type algif_type_skcipher = {
981 .bind = skcipher_bind,
982 .release = skcipher_release,
983 .setkey = skcipher_setkey,
984 .accept = skcipher_accept_parent,
Herbert Xua0fa2d02016-01-04 13:36:12 +0900985 .accept_nokey = skcipher_accept_parent_nokey,
Herbert Xu8ff59092010-10-19 21:31:55 +0800986 .ops = &algif_skcipher_ops,
Herbert Xua0fa2d02016-01-04 13:36:12 +0900987 .ops_nokey = &algif_skcipher_ops_nokey,
Herbert Xu8ff59092010-10-19 21:31:55 +0800988 .name = "skcipher",
989 .owner = THIS_MODULE
990};
991
992static int __init algif_skcipher_init(void)
993{
994 return af_alg_register_type(&algif_type_skcipher);
995}
996
997static void __exit algif_skcipher_exit(void)
998{
999 int err = af_alg_unregister_type(&algif_type_skcipher);
1000 BUG_ON(err);
1001}
1002
1003module_init(algif_skcipher_init);
1004module_exit(algif_skcipher_exit);
1005MODULE_LICENSE("GPL");