blob: ec07a864b9c51d319a435380fe2686360a921af6 [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>
21#include <linux/mm.h>
22#include <linux/module.h>
23#include <linux/net.h>
24#include <net/sock.h>
25
26struct skcipher_sg_list {
27 struct list_head list;
28
29 int cur;
30
31 struct scatterlist sg[0];
32};
33
Herbert Xudd504582015-12-25 15:40:05 +080034struct skcipher_tfm {
35 struct crypto_skcipher *skcipher;
36 bool has_key;
37};
38
Herbert Xu8ff59092010-10-19 21:31:55 +080039struct skcipher_ctx {
40 struct list_head tsgl;
41 struct af_alg_sgl rsgl;
42
43 void *iv;
44
45 struct af_alg_completion completion;
46
Tadeusz Struka5969992015-03-19 12:31:40 -070047 atomic_t inflight;
LABBE Corentin652d5b82015-10-23 14:10:36 +020048 size_t used;
Herbert Xu8ff59092010-10-19 21:31:55 +080049
50 unsigned int len;
51 bool more;
52 bool merge;
53 bool enc;
54
Herbert Xu0d96e4b2015-12-18 19:16:57 +080055 struct skcipher_request req;
Herbert Xu8ff59092010-10-19 21:31:55 +080056};
57
Tadeusz Struka5969992015-03-19 12:31:40 -070058struct skcipher_async_rsgl {
59 struct af_alg_sgl sgl;
60 struct list_head list;
61};
62
63struct skcipher_async_req {
64 struct kiocb *iocb;
65 struct skcipher_async_rsgl first_sgl;
66 struct list_head list;
67 struct scatterlist *tsg;
Herbert Xuec69bbf2016-02-03 21:39:24 +080068 atomic_t *inflight;
69 struct skcipher_request req;
Tadeusz Struka5969992015-03-19 12:31:40 -070070};
71
Ondrej Kozinae2cffb52014-08-25 11:49:54 +020072#define MAX_SGL_ENTS ((4096 - sizeof(struct skcipher_sg_list)) / \
Herbert Xu8ff59092010-10-19 21:31:55 +080073 sizeof(struct scatterlist) - 1)
74
Tadeusz Struka5969992015-03-19 12:31:40 -070075static void skcipher_free_async_sgls(struct skcipher_async_req *sreq)
76{
77 struct skcipher_async_rsgl *rsgl, *tmp;
78 struct scatterlist *sgl;
79 struct scatterlist *sg;
80 int i, n;
81
82 list_for_each_entry_safe(rsgl, tmp, &sreq->list, list) {
83 af_alg_free_sg(&rsgl->sgl);
84 if (rsgl != &sreq->first_sgl)
85 kfree(rsgl);
86 }
87 sgl = sreq->tsg;
88 n = sg_nents(sgl);
89 for_each_sg(sgl, sg, n, i)
90 put_page(sg_page(sg));
91
92 kfree(sreq->tsg);
93}
94
95static void skcipher_async_cb(struct crypto_async_request *req, int err)
96{
Herbert Xuec69bbf2016-02-03 21:39:24 +080097 struct skcipher_async_req *sreq = req->data;
Tadeusz Struka5969992015-03-19 12:31:40 -070098 struct kiocb *iocb = sreq->iocb;
99
Herbert Xuec69bbf2016-02-03 21:39:24 +0800100 atomic_dec(sreq->inflight);
Tadeusz Struka5969992015-03-19 12:31:40 -0700101 skcipher_free_async_sgls(sreq);
Herbert Xuec69bbf2016-02-03 21:39:24 +0800102 kzfree(sreq);
Al Viro237dae82015-04-09 00:00:30 -0400103 iocb->ki_complete(iocb, err, err);
Tadeusz Struka5969992015-03-19 12:31:40 -0700104}
105
Herbert Xu0f6bb832010-11-30 16:49:02 +0800106static inline int skcipher_sndbuf(struct sock *sk)
Herbert Xu8ff59092010-10-19 21:31:55 +0800107{
108 struct alg_sock *ask = alg_sk(sk);
109 struct skcipher_ctx *ctx = ask->private;
110
Herbert Xu0f6bb832010-11-30 16:49:02 +0800111 return max_t(int, max_t(int, sk->sk_sndbuf & PAGE_MASK, PAGE_SIZE) -
112 ctx->used, 0);
113}
114
115static inline bool skcipher_writable(struct sock *sk)
116{
117 return PAGE_SIZE <= skcipher_sndbuf(sk);
Herbert Xu8ff59092010-10-19 21:31:55 +0800118}
119
120static int skcipher_alloc_sgl(struct sock *sk)
121{
122 struct alg_sock *ask = alg_sk(sk);
123 struct skcipher_ctx *ctx = ask->private;
124 struct skcipher_sg_list *sgl;
125 struct scatterlist *sg = NULL;
126
127 sgl = list_entry(ctx->tsgl.prev, struct skcipher_sg_list, list);
128 if (!list_empty(&ctx->tsgl))
129 sg = sgl->sg;
130
131 if (!sg || sgl->cur >= MAX_SGL_ENTS) {
132 sgl = sock_kmalloc(sk, sizeof(*sgl) +
133 sizeof(sgl->sg[0]) * (MAX_SGL_ENTS + 1),
134 GFP_KERNEL);
135 if (!sgl)
136 return -ENOMEM;
137
138 sg_init_table(sgl->sg, MAX_SGL_ENTS + 1);
139 sgl->cur = 0;
140
141 if (sg)
Dan Williamsc56f6d12015-08-07 18:15:13 +0200142 sg_chain(sg, MAX_SGL_ENTS + 1, sgl->sg);
Herbert Xu8ff59092010-10-19 21:31:55 +0800143
144 list_add_tail(&sgl->list, &ctx->tsgl);
145 }
146
147 return 0;
148}
149
LABBE Corentin652d5b82015-10-23 14:10:36 +0200150static void skcipher_pull_sgl(struct sock *sk, size_t used, int put)
Herbert Xu8ff59092010-10-19 21:31:55 +0800151{
152 struct alg_sock *ask = alg_sk(sk);
153 struct skcipher_ctx *ctx = ask->private;
154 struct skcipher_sg_list *sgl;
155 struct scatterlist *sg;
156 int i;
157
158 while (!list_empty(&ctx->tsgl)) {
159 sgl = list_first_entry(&ctx->tsgl, struct skcipher_sg_list,
160 list);
161 sg = sgl->sg;
162
163 for (i = 0; i < sgl->cur; i++) {
LABBE Corentin652d5b82015-10-23 14:10:36 +0200164 size_t plen = min_t(size_t, used, sg[i].length);
Herbert Xu8ff59092010-10-19 21:31:55 +0800165
166 if (!sg_page(sg + i))
167 continue;
168
169 sg[i].length -= plen;
170 sg[i].offset += plen;
171
172 used -= plen;
173 ctx->used -= plen;
174
175 if (sg[i].length)
176 return;
Tadeusz Struka5969992015-03-19 12:31:40 -0700177 if (put)
178 put_page(sg_page(sg + i));
Herbert Xu8ff59092010-10-19 21:31:55 +0800179 sg_assign_page(sg + i, NULL);
180 }
181
182 list_del(&sgl->list);
183 sock_kfree_s(sk, sgl,
184 sizeof(*sgl) + sizeof(sgl->sg[0]) *
185 (MAX_SGL_ENTS + 1));
186 }
187
188 if (!ctx->used)
189 ctx->merge = 0;
190}
191
192static void skcipher_free_sgl(struct sock *sk)
193{
194 struct alg_sock *ask = alg_sk(sk);
195 struct skcipher_ctx *ctx = ask->private;
196
Tadeusz Struka5969992015-03-19 12:31:40 -0700197 skcipher_pull_sgl(sk, ctx->used, 1);
Herbert Xu8ff59092010-10-19 21:31:55 +0800198}
199
200static int skcipher_wait_for_wmem(struct sock *sk, unsigned flags)
201{
202 long timeout;
203 DEFINE_WAIT(wait);
204 int err = -ERESTARTSYS;
205
206 if (flags & MSG_DONTWAIT)
207 return -EAGAIN;
208
Eric Dumazet9cd3e072015-11-29 20:03:10 -0800209 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
Herbert Xu8ff59092010-10-19 21:31:55 +0800210
211 for (;;) {
212 if (signal_pending(current))
213 break;
214 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
215 timeout = MAX_SCHEDULE_TIMEOUT;
216 if (sk_wait_event(sk, &timeout, skcipher_writable(sk))) {
217 err = 0;
218 break;
219 }
220 }
221 finish_wait(sk_sleep(sk), &wait);
222
223 return err;
224}
225
226static void skcipher_wmem_wakeup(struct sock *sk)
227{
228 struct socket_wq *wq;
229
230 if (!skcipher_writable(sk))
231 return;
232
233 rcu_read_lock();
234 wq = rcu_dereference(sk->sk_wq);
Herbert Xu1ce0bf52015-11-26 13:55:39 +0800235 if (skwq_has_sleeper(wq))
Herbert Xu8ff59092010-10-19 21:31:55 +0800236 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
237 POLLRDNORM |
238 POLLRDBAND);
239 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
240 rcu_read_unlock();
241}
242
243static int skcipher_wait_for_data(struct sock *sk, unsigned flags)
244{
245 struct alg_sock *ask = alg_sk(sk);
246 struct skcipher_ctx *ctx = ask->private;
247 long timeout;
248 DEFINE_WAIT(wait);
249 int err = -ERESTARTSYS;
250
251 if (flags & MSG_DONTWAIT) {
252 return -EAGAIN;
253 }
254
Eric Dumazet9cd3e072015-11-29 20:03:10 -0800255 sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
Herbert Xu8ff59092010-10-19 21:31:55 +0800256
257 for (;;) {
258 if (signal_pending(current))
259 break;
260 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
261 timeout = MAX_SCHEDULE_TIMEOUT;
262 if (sk_wait_event(sk, &timeout, ctx->used)) {
263 err = 0;
264 break;
265 }
266 }
267 finish_wait(sk_sleep(sk), &wait);
268
Eric Dumazet9cd3e072015-11-29 20:03:10 -0800269 sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
Herbert Xu8ff59092010-10-19 21:31:55 +0800270
271 return err;
272}
273
274static void skcipher_data_wakeup(struct sock *sk)
275{
276 struct alg_sock *ask = alg_sk(sk);
277 struct skcipher_ctx *ctx = ask->private;
278 struct socket_wq *wq;
279
280 if (!ctx->used)
281 return;
282
283 rcu_read_lock();
284 wq = rcu_dereference(sk->sk_wq);
Herbert Xu1ce0bf52015-11-26 13:55:39 +0800285 if (skwq_has_sleeper(wq))
Herbert Xu8ff59092010-10-19 21:31:55 +0800286 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
287 POLLRDNORM |
288 POLLRDBAND);
289 sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
290 rcu_read_unlock();
291}
292
Ying Xue1b784142015-03-02 15:37:48 +0800293static int skcipher_sendmsg(struct socket *sock, struct msghdr *msg,
294 size_t size)
Herbert Xu8ff59092010-10-19 21:31:55 +0800295{
296 struct sock *sk = sock->sk;
297 struct alg_sock *ask = alg_sk(sk);
298 struct skcipher_ctx *ctx = ask->private;
Herbert Xu0d96e4b2015-12-18 19:16:57 +0800299 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(&ctx->req);
300 unsigned ivsize = crypto_skcipher_ivsize(tfm);
Herbert Xu8ff59092010-10-19 21:31:55 +0800301 struct skcipher_sg_list *sgl;
302 struct af_alg_control con = {};
303 long copied = 0;
304 bool enc = 0;
Stephan Muellerf26b7b82014-11-30 10:55:26 +0100305 bool init = 0;
Herbert Xu8ff59092010-10-19 21:31:55 +0800306 int err;
307 int i;
308
309 if (msg->msg_controllen) {
310 err = af_alg_cmsg_send(msg, &con);
311 if (err)
312 return err;
313
Stephan Muellerf26b7b82014-11-30 10:55:26 +0100314 init = 1;
Herbert Xu8ff59092010-10-19 21:31:55 +0800315 switch (con.op) {
316 case ALG_OP_ENCRYPT:
317 enc = 1;
318 break;
319 case ALG_OP_DECRYPT:
320 enc = 0;
321 break;
322 default:
323 return -EINVAL;
324 }
325
326 if (con.iv && con.iv->ivlen != ivsize)
327 return -EINVAL;
328 }
329
330 err = -EINVAL;
331
332 lock_sock(sk);
333 if (!ctx->more && ctx->used)
334 goto unlock;
335
Stephan Muellerf26b7b82014-11-30 10:55:26 +0100336 if (init) {
Herbert Xu8ff59092010-10-19 21:31:55 +0800337 ctx->enc = enc;
338 if (con.iv)
339 memcpy(ctx->iv, con.iv->iv, ivsize);
340 }
341
Herbert Xu8ff59092010-10-19 21:31:55 +0800342 while (size) {
343 struct scatterlist *sg;
344 unsigned long len = size;
LABBE Corentin652d5b82015-10-23 14:10:36 +0200345 size_t plen;
Herbert Xu8ff59092010-10-19 21:31:55 +0800346
347 if (ctx->merge) {
348 sgl = list_entry(ctx->tsgl.prev,
349 struct skcipher_sg_list, list);
350 sg = sgl->sg + sgl->cur - 1;
351 len = min_t(unsigned long, len,
352 PAGE_SIZE - sg->offset - sg->length);
353
Al Viro6ce8e9c2014-04-06 21:25:44 -0400354 err = memcpy_from_msg(page_address(sg_page(sg)) +
355 sg->offset + sg->length,
356 msg, len);
Herbert Xu8ff59092010-10-19 21:31:55 +0800357 if (err)
358 goto unlock;
359
360 sg->length += len;
361 ctx->merge = (sg->offset + sg->length) &
362 (PAGE_SIZE - 1);
363
364 ctx->used += len;
365 copied += len;
366 size -= len;
Herbert Xu8ff59092010-10-19 21:31:55 +0800367 continue;
368 }
369
Herbert Xu0f6bb832010-11-30 16:49:02 +0800370 if (!skcipher_writable(sk)) {
Herbert Xu8ff59092010-10-19 21:31:55 +0800371 err = skcipher_wait_for_wmem(sk, msg->msg_flags);
372 if (err)
373 goto unlock;
Herbert Xu8ff59092010-10-19 21:31:55 +0800374 }
375
Herbert Xu0f6bb832010-11-30 16:49:02 +0800376 len = min_t(unsigned long, len, skcipher_sndbuf(sk));
Herbert Xu8ff59092010-10-19 21:31:55 +0800377
378 err = skcipher_alloc_sgl(sk);
379 if (err)
380 goto unlock;
381
382 sgl = list_entry(ctx->tsgl.prev, struct skcipher_sg_list, list);
383 sg = sgl->sg;
Herbert Xu202736d2016-01-19 21:23:57 +0800384 if (sgl->cur)
385 sg_unmark_end(sg + sgl->cur - 1);
Herbert Xu8ff59092010-10-19 21:31:55 +0800386 do {
387 i = sgl->cur;
LABBE Corentin652d5b82015-10-23 14:10:36 +0200388 plen = min_t(size_t, len, PAGE_SIZE);
Herbert Xu8ff59092010-10-19 21:31:55 +0800389
390 sg_assign_page(sg + i, alloc_page(GFP_KERNEL));
391 err = -ENOMEM;
392 if (!sg_page(sg + i))
393 goto unlock;
394
Al Viro6ce8e9c2014-04-06 21:25:44 -0400395 err = memcpy_from_msg(page_address(sg_page(sg + i)),
396 msg, plen);
Herbert Xu8ff59092010-10-19 21:31:55 +0800397 if (err) {
398 __free_page(sg_page(sg + i));
399 sg_assign_page(sg + i, NULL);
400 goto unlock;
401 }
402
403 sg[i].length = plen;
404 len -= plen;
405 ctx->used += plen;
406 copied += plen;
407 size -= plen;
Herbert Xu8ff59092010-10-19 21:31:55 +0800408 sgl->cur++;
409 } while (len && sgl->cur < MAX_SGL_ENTS);
410
Tadeusz Struk0f477b62014-12-08 12:03:42 -0800411 if (!size)
412 sg_mark_end(sg + sgl->cur - 1);
413
Herbert Xu8ff59092010-10-19 21:31:55 +0800414 ctx->merge = plen & (PAGE_SIZE - 1);
415 }
416
417 err = 0;
418
419 ctx->more = msg->msg_flags & MSG_MORE;
Herbert Xu8ff59092010-10-19 21:31:55 +0800420
421unlock:
422 skcipher_data_wakeup(sk);
423 release_sock(sk);
424
425 return copied ?: err;
426}
427
428static ssize_t skcipher_sendpage(struct socket *sock, struct page *page,
429 int offset, size_t size, int flags)
430{
431 struct sock *sk = sock->sk;
432 struct alg_sock *ask = alg_sk(sk);
433 struct skcipher_ctx *ctx = ask->private;
434 struct skcipher_sg_list *sgl;
435 int err = -EINVAL;
Herbert Xu8ff59092010-10-19 21:31:55 +0800436
Shawn Landdend3f7d562013-11-24 22:36:28 -0800437 if (flags & MSG_SENDPAGE_NOTLAST)
438 flags |= MSG_MORE;
439
Herbert Xu8ff59092010-10-19 21:31:55 +0800440 lock_sock(sk);
441 if (!ctx->more && ctx->used)
442 goto unlock;
443
444 if (!size)
445 goto done;
446
Herbert Xu0f6bb832010-11-30 16:49:02 +0800447 if (!skcipher_writable(sk)) {
Herbert Xu8ff59092010-10-19 21:31:55 +0800448 err = skcipher_wait_for_wmem(sk, flags);
449 if (err)
450 goto unlock;
Herbert Xu8ff59092010-10-19 21:31:55 +0800451 }
452
453 err = skcipher_alloc_sgl(sk);
454 if (err)
455 goto unlock;
456
457 ctx->merge = 0;
458 sgl = list_entry(ctx->tsgl.prev, struct skcipher_sg_list, list);
459
Tadeusz Struk0f477b62014-12-08 12:03:42 -0800460 if (sgl->cur)
461 sg_unmark_end(sgl->sg + sgl->cur - 1);
462
463 sg_mark_end(sgl->sg + sgl->cur);
Herbert Xu8ff59092010-10-19 21:31:55 +0800464 get_page(page);
465 sg_set_page(sgl->sg + sgl->cur, page, size, offset);
466 sgl->cur++;
467 ctx->used += size;
468
469done:
470 ctx->more = flags & MSG_MORE;
Herbert Xu8ff59092010-10-19 21:31:55 +0800471
472unlock:
473 skcipher_data_wakeup(sk);
474 release_sock(sk);
475
476 return err ?: size;
477}
478
Tadeusz Struka5969992015-03-19 12:31:40 -0700479static int skcipher_all_sg_nents(struct skcipher_ctx *ctx)
480{
481 struct skcipher_sg_list *sgl;
482 struct scatterlist *sg;
483 int nents = 0;
484
485 list_for_each_entry(sgl, &ctx->tsgl, list) {
486 sg = sgl->sg;
487
488 while (!sg->length)
489 sg++;
490
491 nents += sg_nents(sg);
492 }
493 return nents;
494}
495
496static int skcipher_recvmsg_async(struct socket *sock, struct msghdr *msg,
497 int flags)
498{
499 struct sock *sk = sock->sk;
500 struct alg_sock *ask = alg_sk(sk);
Herbert Xuec69bbf2016-02-03 21:39:24 +0800501 struct sock *psk = ask->parent;
502 struct alg_sock *pask = alg_sk(psk);
Tadeusz Struka5969992015-03-19 12:31:40 -0700503 struct skcipher_ctx *ctx = ask->private;
Herbert Xuec69bbf2016-02-03 21:39:24 +0800504 struct skcipher_tfm *skc = pask->private;
505 struct crypto_skcipher *tfm = skc->skcipher;
Tadeusz Struka5969992015-03-19 12:31:40 -0700506 struct skcipher_sg_list *sgl;
507 struct scatterlist *sg;
508 struct skcipher_async_req *sreq;
Herbert Xu0d96e4b2015-12-18 19:16:57 +0800509 struct skcipher_request *req;
Tadeusz Struka5969992015-03-19 12:31:40 -0700510 struct skcipher_async_rsgl *last_rsgl = NULL;
tadeusz.struk@intel.com033f46b2015-04-01 13:53:06 -0700511 unsigned int txbufs = 0, len = 0, tx_nents = skcipher_all_sg_nents(ctx);
Herbert Xuec69bbf2016-02-03 21:39:24 +0800512 unsigned int reqsize = crypto_skcipher_reqsize(tfm);
513 unsigned int ivsize = crypto_skcipher_ivsize(tfm);
Tadeusz Struka5969992015-03-19 12:31:40 -0700514 int err = -ENOMEM;
tadeusz.struk@intel.com033f46b2015-04-01 13:53:06 -0700515 bool mark = false;
Herbert Xuec69bbf2016-02-03 21:39:24 +0800516 char *iv;
517
518 sreq = kzalloc(sizeof(*sreq) + reqsize + ivsize, GFP_KERNEL);
519 if (unlikely(!sreq))
520 goto out;
521
522 req = &sreq->req;
523 iv = (char *)(req + 1) + reqsize;
524 sreq->iocb = msg->msg_iocb;
525 INIT_LIST_HEAD(&sreq->list);
526 sreq->inflight = &ctx->inflight;
Tadeusz Struka5969992015-03-19 12:31:40 -0700527
528 lock_sock(sk);
Tadeusz Struka5969992015-03-19 12:31:40 -0700529 sreq->tsg = kcalloc(tx_nents, sizeof(*sg), GFP_KERNEL);
Herbert Xuec69bbf2016-02-03 21:39:24 +0800530 if (unlikely(!sreq->tsg))
Tadeusz Struka5969992015-03-19 12:31:40 -0700531 goto unlock;
Tadeusz Struka5969992015-03-19 12:31:40 -0700532 sg_init_table(sreq->tsg, tx_nents);
Herbert Xuec69bbf2016-02-03 21:39:24 +0800533 memcpy(iv, ctx->iv, ivsize);
534 skcipher_request_set_tfm(req, tfm);
Herbert Xu0d96e4b2015-12-18 19:16:57 +0800535 skcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Herbert Xuec69bbf2016-02-03 21:39:24 +0800536 skcipher_async_cb, sreq);
Tadeusz Struka5969992015-03-19 12:31:40 -0700537
538 while (iov_iter_count(&msg->msg_iter)) {
539 struct skcipher_async_rsgl *rsgl;
tadeusz.struk@intel.comac110f42015-03-25 07:29:19 -0700540 int used;
Tadeusz Struka5969992015-03-19 12:31:40 -0700541
542 if (!ctx->used) {
543 err = skcipher_wait_for_data(sk, flags);
544 if (err)
545 goto free;
546 }
547 sgl = list_first_entry(&ctx->tsgl,
548 struct skcipher_sg_list, list);
549 sg = sgl->sg;
550
551 while (!sg->length)
552 sg++;
553
554 used = min_t(unsigned long, ctx->used,
555 iov_iter_count(&msg->msg_iter));
556 used = min_t(unsigned long, used, sg->length);
557
tadeusz.struk@intel.com033f46b2015-04-01 13:53:06 -0700558 if (txbufs == tx_nents) {
Tadeusz Struka5969992015-03-19 12:31:40 -0700559 struct scatterlist *tmp;
560 int x;
561 /* Ran out of tx slots in async request
562 * need to expand */
563 tmp = kcalloc(tx_nents * 2, sizeof(*tmp),
564 GFP_KERNEL);
565 if (!tmp)
566 goto free;
567
568 sg_init_table(tmp, tx_nents * 2);
569 for (x = 0; x < tx_nents; x++)
570 sg_set_page(&tmp[x], sg_page(&sreq->tsg[x]),
571 sreq->tsg[x].length,
572 sreq->tsg[x].offset);
573 kfree(sreq->tsg);
574 sreq->tsg = tmp;
575 tx_nents *= 2;
tadeusz.struk@intel.com033f46b2015-04-01 13:53:06 -0700576 mark = true;
Tadeusz Struka5969992015-03-19 12:31:40 -0700577 }
578 /* Need to take over the tx sgl from ctx
579 * to the asynch req - these sgls will be freed later */
tadeusz.struk@intel.com033f46b2015-04-01 13:53:06 -0700580 sg_set_page(sreq->tsg + txbufs++, sg_page(sg), sg->length,
Tadeusz Struka5969992015-03-19 12:31:40 -0700581 sg->offset);
582
583 if (list_empty(&sreq->list)) {
584 rsgl = &sreq->first_sgl;
585 list_add_tail(&rsgl->list, &sreq->list);
586 } else {
Tadeusz Struk82d92922015-03-30 14:25:44 -0700587 rsgl = kmalloc(sizeof(*rsgl), GFP_KERNEL);
Tadeusz Struka5969992015-03-19 12:31:40 -0700588 if (!rsgl) {
589 err = -ENOMEM;
590 goto free;
591 }
592 list_add_tail(&rsgl->list, &sreq->list);
593 }
594
595 used = af_alg_make_sg(&rsgl->sgl, &msg->msg_iter, used);
596 err = used;
597 if (used < 0)
598 goto free;
599 if (last_rsgl)
600 af_alg_link_sg(&last_rsgl->sgl, &rsgl->sgl);
601
602 last_rsgl = rsgl;
603 len += used;
604 skcipher_pull_sgl(sk, used, 0);
605 iov_iter_advance(&msg->msg_iter, used);
606 }
607
tadeusz.struk@intel.com033f46b2015-04-01 13:53:06 -0700608 if (mark)
609 sg_mark_end(sreq->tsg + txbufs - 1);
610
Herbert Xu0d96e4b2015-12-18 19:16:57 +0800611 skcipher_request_set_crypt(req, sreq->tsg, sreq->first_sgl.sgl.sg,
Herbert Xuec69bbf2016-02-03 21:39:24 +0800612 len, iv);
Herbert Xu0d96e4b2015-12-18 19:16:57 +0800613 err = ctx->enc ? crypto_skcipher_encrypt(req) :
614 crypto_skcipher_decrypt(req);
Tadeusz Struka5969992015-03-19 12:31:40 -0700615 if (err == -EINPROGRESS) {
616 atomic_inc(&ctx->inflight);
617 err = -EIOCBQUEUED;
Herbert Xuec69bbf2016-02-03 21:39:24 +0800618 sreq = NULL;
Tadeusz Struka5969992015-03-19 12:31:40 -0700619 goto unlock;
620 }
621free:
622 skcipher_free_async_sgls(sreq);
Tadeusz Struka5969992015-03-19 12:31:40 -0700623unlock:
624 skcipher_wmem_wakeup(sk);
625 release_sock(sk);
Herbert Xuec69bbf2016-02-03 21:39:24 +0800626 kzfree(sreq);
627out:
Tadeusz Struka5969992015-03-19 12:31:40 -0700628 return err;
629}
630
631static int skcipher_recvmsg_sync(struct socket *sock, struct msghdr *msg,
632 int flags)
Herbert Xu8ff59092010-10-19 21:31:55 +0800633{
634 struct sock *sk = sock->sk;
635 struct alg_sock *ask = alg_sk(sk);
636 struct skcipher_ctx *ctx = ask->private;
Herbert Xu0d96e4b2015-12-18 19:16:57 +0800637 unsigned bs = crypto_skcipher_blocksize(crypto_skcipher_reqtfm(
Herbert Xu8ff59092010-10-19 21:31:55 +0800638 &ctx->req));
639 struct skcipher_sg_list *sgl;
640 struct scatterlist *sg;
Herbert Xu8ff59092010-10-19 21:31:55 +0800641 int err = -EAGAIN;
642 int used;
643 long copied = 0;
644
645 lock_sock(sk);
Al Viro01e97e62014-12-15 21:39:31 -0500646 while (msg_data_left(msg)) {
Linus Torvalds9399f0c2015-02-10 19:55:45 -0800647 if (!ctx->used) {
Al Viro1d10eb22014-11-28 16:39:25 -0500648 err = skcipher_wait_for_data(sk, flags);
649 if (err)
Herbert Xubc97e572010-11-30 17:04:31 +0800650 goto unlock;
Al Viro1d10eb22014-11-28 16:39:25 -0500651 }
Herbert Xubc97e572010-11-30 17:04:31 +0800652
Al Viro01e97e62014-12-15 21:39:31 -0500653 used = min_t(unsigned long, ctx->used, msg_data_left(msg));
Herbert Xu8ff59092010-10-19 21:31:55 +0800654
Al Viro1d10eb22014-11-28 16:39:25 -0500655 used = af_alg_make_sg(&ctx->rsgl, &msg->msg_iter, used);
656 err = used;
657 if (err < 0)
658 goto unlock;
Herbert Xu8ff59092010-10-19 21:31:55 +0800659
Al Viro1d10eb22014-11-28 16:39:25 -0500660 if (ctx->more || used < ctx->used)
661 used -= used % bs;
Herbert Xu8ff59092010-10-19 21:31:55 +0800662
Al Viro1d10eb22014-11-28 16:39:25 -0500663 err = -EINVAL;
664 if (!used)
665 goto free;
666
Herbert Xu4f0414e2016-01-18 18:46:10 +0800667 sgl = list_first_entry(&ctx->tsgl,
668 struct skcipher_sg_list, list);
669 sg = sgl->sg;
670
671 while (!sg->length)
672 sg++;
673
Herbert Xu0d96e4b2015-12-18 19:16:57 +0800674 skcipher_request_set_crypt(&ctx->req, sg, ctx->rsgl.sg, used,
675 ctx->iv);
Al Viro1d10eb22014-11-28 16:39:25 -0500676
677 err = af_alg_wait_for_completion(
Herbert Xu8ff59092010-10-19 21:31:55 +0800678 ctx->enc ?
Herbert Xu0d96e4b2015-12-18 19:16:57 +0800679 crypto_skcipher_encrypt(&ctx->req) :
680 crypto_skcipher_decrypt(&ctx->req),
Herbert Xu8ff59092010-10-19 21:31:55 +0800681 &ctx->completion);
682
Herbert Xubc97e572010-11-30 17:04:31 +0800683free:
Al Viro1d10eb22014-11-28 16:39:25 -0500684 af_alg_free_sg(&ctx->rsgl);
Herbert Xu8ff59092010-10-19 21:31:55 +0800685
Al Viro1d10eb22014-11-28 16:39:25 -0500686 if (err)
687 goto unlock;
Herbert Xu8ff59092010-10-19 21:31:55 +0800688
Al Viro1d10eb22014-11-28 16:39:25 -0500689 copied += used;
Tadeusz Struka5969992015-03-19 12:31:40 -0700690 skcipher_pull_sgl(sk, used, 1);
Al Viro1d10eb22014-11-28 16:39:25 -0500691 iov_iter_advance(&msg->msg_iter, used);
Herbert Xu8ff59092010-10-19 21:31:55 +0800692 }
693
694 err = 0;
695
696unlock:
697 skcipher_wmem_wakeup(sk);
698 release_sock(sk);
699
700 return copied ?: err;
701}
702
Tadeusz Struka5969992015-03-19 12:31:40 -0700703static int skcipher_recvmsg(struct socket *sock, struct msghdr *msg,
704 size_t ignored, int flags)
705{
706 return (msg->msg_iocb && !is_sync_kiocb(msg->msg_iocb)) ?
707 skcipher_recvmsg_async(sock, msg, flags) :
708 skcipher_recvmsg_sync(sock, msg, flags);
709}
Herbert Xu8ff59092010-10-19 21:31:55 +0800710
711static unsigned int skcipher_poll(struct file *file, struct socket *sock,
712 poll_table *wait)
713{
714 struct sock *sk = sock->sk;
715 struct alg_sock *ask = alg_sk(sk);
716 struct skcipher_ctx *ctx = ask->private;
717 unsigned int mask;
718
719 sock_poll_wait(file, sk_sleep(sk), wait);
720 mask = 0;
721
722 if (ctx->used)
723 mask |= POLLIN | POLLRDNORM;
724
725 if (skcipher_writable(sk))
726 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
727
728 return mask;
729}
730
731static struct proto_ops algif_skcipher_ops = {
732 .family = PF_ALG,
733
734 .connect = sock_no_connect,
735 .socketpair = sock_no_socketpair,
736 .getname = sock_no_getname,
737 .ioctl = sock_no_ioctl,
738 .listen = sock_no_listen,
739 .shutdown = sock_no_shutdown,
740 .getsockopt = sock_no_getsockopt,
741 .mmap = sock_no_mmap,
742 .bind = sock_no_bind,
743 .accept = sock_no_accept,
744 .setsockopt = sock_no_setsockopt,
745
746 .release = af_alg_release,
747 .sendmsg = skcipher_sendmsg,
748 .sendpage = skcipher_sendpage,
749 .recvmsg = skcipher_recvmsg,
750 .poll = skcipher_poll,
751};
752
Herbert Xua0fa2d02016-01-04 13:36:12 +0900753static int skcipher_check_key(struct socket *sock)
754{
Herbert Xu18227932016-01-15 22:02:20 +0800755 int err = 0;
Herbert Xua0fa2d02016-01-04 13:36:12 +0900756 struct sock *psk;
757 struct alg_sock *pask;
758 struct skcipher_tfm *tfm;
759 struct sock *sk = sock->sk;
760 struct alg_sock *ask = alg_sk(sk);
761
Herbert Xu18227932016-01-15 22:02:20 +0800762 lock_sock(sk);
Herbert Xua0fa2d02016-01-04 13:36:12 +0900763 if (ask->refcnt)
Herbert Xu18227932016-01-15 22:02:20 +0800764 goto unlock_child;
Herbert Xua0fa2d02016-01-04 13:36:12 +0900765
766 psk = ask->parent;
767 pask = alg_sk(ask->parent);
768 tfm = pask->private;
769
770 err = -ENOKEY;
Herbert Xu18227932016-01-15 22:02:20 +0800771 lock_sock_nested(psk, SINGLE_DEPTH_NESTING);
Herbert Xua0fa2d02016-01-04 13:36:12 +0900772 if (!tfm->has_key)
773 goto unlock;
774
775 if (!pask->refcnt++)
776 sock_hold(psk);
777
778 ask->refcnt = 1;
779 sock_put(psk);
780
781 err = 0;
782
783unlock:
784 release_sock(psk);
Herbert Xu18227932016-01-15 22:02:20 +0800785unlock_child:
786 release_sock(sk);
Herbert Xua0fa2d02016-01-04 13:36:12 +0900787
788 return err;
789}
790
791static int skcipher_sendmsg_nokey(struct socket *sock, struct msghdr *msg,
792 size_t size)
793{
794 int err;
795
796 err = skcipher_check_key(sock);
797 if (err)
798 return err;
799
800 return skcipher_sendmsg(sock, msg, size);
801}
802
803static ssize_t skcipher_sendpage_nokey(struct socket *sock, struct page *page,
804 int offset, size_t size, int flags)
805{
806 int err;
807
808 err = skcipher_check_key(sock);
809 if (err)
810 return err;
811
812 return skcipher_sendpage(sock, page, offset, size, flags);
813}
814
815static int skcipher_recvmsg_nokey(struct socket *sock, struct msghdr *msg,
816 size_t ignored, int flags)
817{
818 int err;
819
820 err = skcipher_check_key(sock);
821 if (err)
822 return err;
823
824 return skcipher_recvmsg(sock, msg, ignored, flags);
825}
826
827static struct proto_ops algif_skcipher_ops_nokey = {
828 .family = PF_ALG,
829
830 .connect = sock_no_connect,
831 .socketpair = sock_no_socketpair,
832 .getname = sock_no_getname,
833 .ioctl = sock_no_ioctl,
834 .listen = sock_no_listen,
835 .shutdown = sock_no_shutdown,
836 .getsockopt = sock_no_getsockopt,
837 .mmap = sock_no_mmap,
838 .bind = sock_no_bind,
839 .accept = sock_no_accept,
840 .setsockopt = sock_no_setsockopt,
841
842 .release = af_alg_release,
843 .sendmsg = skcipher_sendmsg_nokey,
844 .sendpage = skcipher_sendpage_nokey,
845 .recvmsg = skcipher_recvmsg_nokey,
846 .poll = skcipher_poll,
847};
848
Herbert Xu8ff59092010-10-19 21:31:55 +0800849static void *skcipher_bind(const char *name, u32 type, u32 mask)
850{
Herbert Xudd504582015-12-25 15:40:05 +0800851 struct skcipher_tfm *tfm;
852 struct crypto_skcipher *skcipher;
853
854 tfm = kzalloc(sizeof(*tfm), GFP_KERNEL);
855 if (!tfm)
856 return ERR_PTR(-ENOMEM);
857
858 skcipher = crypto_alloc_skcipher(name, type, mask);
859 if (IS_ERR(skcipher)) {
860 kfree(tfm);
861 return ERR_CAST(skcipher);
862 }
863
864 tfm->skcipher = skcipher;
865
866 return tfm;
Herbert Xu8ff59092010-10-19 21:31:55 +0800867}
868
869static void skcipher_release(void *private)
870{
Herbert Xudd504582015-12-25 15:40:05 +0800871 struct skcipher_tfm *tfm = private;
872
873 crypto_free_skcipher(tfm->skcipher);
874 kfree(tfm);
Herbert Xu8ff59092010-10-19 21:31:55 +0800875}
876
877static int skcipher_setkey(void *private, const u8 *key, unsigned int keylen)
878{
Herbert Xudd504582015-12-25 15:40:05 +0800879 struct skcipher_tfm *tfm = private;
880 int err;
881
882 err = crypto_skcipher_setkey(tfm->skcipher, key, keylen);
883 tfm->has_key = !err;
884
885 return err;
Herbert Xu8ff59092010-10-19 21:31:55 +0800886}
887
Tadeusz Struka5969992015-03-19 12:31:40 -0700888static void skcipher_wait(struct sock *sk)
889{
890 struct alg_sock *ask = alg_sk(sk);
891 struct skcipher_ctx *ctx = ask->private;
892 int ctr = 0;
893
894 while (atomic_read(&ctx->inflight) && ctr++ < 100)
895 msleep(100);
896}
897
Herbert Xu8ff59092010-10-19 21:31:55 +0800898static void skcipher_sock_destruct(struct sock *sk)
899{
900 struct alg_sock *ask = alg_sk(sk);
901 struct skcipher_ctx *ctx = ask->private;
Herbert Xu0d96e4b2015-12-18 19:16:57 +0800902 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(&ctx->req);
Herbert Xu8ff59092010-10-19 21:31:55 +0800903
Tadeusz Struka5969992015-03-19 12:31:40 -0700904 if (atomic_read(&ctx->inflight))
905 skcipher_wait(sk);
906
Herbert Xu8ff59092010-10-19 21:31:55 +0800907 skcipher_free_sgl(sk);
Herbert Xu0d96e4b2015-12-18 19:16:57 +0800908 sock_kzfree_s(sk, ctx->iv, crypto_skcipher_ivsize(tfm));
Herbert Xu8ff59092010-10-19 21:31:55 +0800909 sock_kfree_s(sk, ctx, ctx->len);
910 af_alg_release_parent(sk);
911}
912
Herbert Xud7b65ae2016-01-13 15:01:06 +0800913static int skcipher_accept_parent_nokey(void *private, struct sock *sk)
Herbert Xu8ff59092010-10-19 21:31:55 +0800914{
915 struct skcipher_ctx *ctx;
916 struct alg_sock *ask = alg_sk(sk);
Herbert Xudd504582015-12-25 15:40:05 +0800917 struct skcipher_tfm *tfm = private;
918 struct crypto_skcipher *skcipher = tfm->skcipher;
919 unsigned int len = sizeof(*ctx) + crypto_skcipher_reqsize(skcipher);
Herbert Xu8ff59092010-10-19 21:31:55 +0800920
921 ctx = sock_kmalloc(sk, len, GFP_KERNEL);
922 if (!ctx)
923 return -ENOMEM;
924
Herbert Xudd504582015-12-25 15:40:05 +0800925 ctx->iv = sock_kmalloc(sk, crypto_skcipher_ivsize(skcipher),
Herbert Xu8ff59092010-10-19 21:31:55 +0800926 GFP_KERNEL);
927 if (!ctx->iv) {
928 sock_kfree_s(sk, ctx, len);
929 return -ENOMEM;
930 }
931
Herbert Xudd504582015-12-25 15:40:05 +0800932 memset(ctx->iv, 0, crypto_skcipher_ivsize(skcipher));
Herbert Xu8ff59092010-10-19 21:31:55 +0800933
934 INIT_LIST_HEAD(&ctx->tsgl);
935 ctx->len = len;
936 ctx->used = 0;
937 ctx->more = 0;
938 ctx->merge = 0;
939 ctx->enc = 0;
Tadeusz Struka5969992015-03-19 12:31:40 -0700940 atomic_set(&ctx->inflight, 0);
Herbert Xu8ff59092010-10-19 21:31:55 +0800941 af_alg_init_completion(&ctx->completion);
942
943 ask->private = ctx;
944
Herbert Xudd504582015-12-25 15:40:05 +0800945 skcipher_request_set_tfm(&ctx->req, skcipher);
Herbert Xu0d96e4b2015-12-18 19:16:57 +0800946 skcipher_request_set_callback(&ctx->req, CRYPTO_TFM_REQ_MAY_BACKLOG,
947 af_alg_complete, &ctx->completion);
Herbert Xu8ff59092010-10-19 21:31:55 +0800948
949 sk->sk_destruct = skcipher_sock_destruct;
950
951 return 0;
952}
953
Herbert Xua0fa2d02016-01-04 13:36:12 +0900954static int skcipher_accept_parent(void *private, struct sock *sk)
955{
956 struct skcipher_tfm *tfm = private;
957
Herbert Xu6e8d8ec2016-01-11 21:29:41 +0800958 if (!tfm->has_key && crypto_skcipher_has_setkey(tfm->skcipher))
Herbert Xua0fa2d02016-01-04 13:36:12 +0900959 return -ENOKEY;
960
Herbert Xud7b65ae2016-01-13 15:01:06 +0800961 return skcipher_accept_parent_nokey(private, sk);
Herbert Xua0fa2d02016-01-04 13:36:12 +0900962}
963
Herbert Xu8ff59092010-10-19 21:31:55 +0800964static const struct af_alg_type algif_type_skcipher = {
965 .bind = skcipher_bind,
966 .release = skcipher_release,
967 .setkey = skcipher_setkey,
968 .accept = skcipher_accept_parent,
Herbert Xua0fa2d02016-01-04 13:36:12 +0900969 .accept_nokey = skcipher_accept_parent_nokey,
Herbert Xu8ff59092010-10-19 21:31:55 +0800970 .ops = &algif_skcipher_ops,
Herbert Xua0fa2d02016-01-04 13:36:12 +0900971 .ops_nokey = &algif_skcipher_ops_nokey,
Herbert Xu8ff59092010-10-19 21:31:55 +0800972 .name = "skcipher",
973 .owner = THIS_MODULE
974};
975
976static int __init algif_skcipher_init(void)
977{
978 return af_alg_register_type(&algif_type_skcipher);
979}
980
981static void __exit algif_skcipher_exit(void)
982{
983 int err = af_alg_unregister_type(&algif_type_skcipher);
984 BUG_ON(err);
985}
986
987module_init(algif_skcipher_init);
988module_exit(algif_skcipher_exit);
989MODULE_LICENSE("GPL");