blob: e0d55ea2f0ebaee18d3ed148a9168348c45d7c84 [file] [log] [blame]
Stephan Mueller400c40c2015-02-28 20:50:00 +01001/*
2 * algif_aead: User-space interface for AEAD algorithms
3 *
4 * Copyright (C) 2014, Stephan Mueller <smueller@chronox.de>
5 *
6 * This file provides the user-space API for AEAD ciphers.
7 *
8 * This file is derived from algif_skcipher.c.
9 *
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)
13 * any later version.
14 */
15
Tadeusz Struk83094e5e2016-03-11 11:50:33 -080016#include <crypto/internal/aead.h>
Stephan Mueller400c40c2015-02-28 20:50:00 +010017#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>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010022#include <linux/sched/signal.h>
Stephan Mueller400c40c2015-02-28 20:50:00 +010023#include <linux/mm.h>
24#include <linux/module.h>
25#include <linux/net.h>
26#include <net/sock.h>
27
28struct aead_sg_list {
29 unsigned int cur;
30 struct scatterlist sg[ALG_MAX_PAGES];
31};
32
Tadeusz Struk83094e5e2016-03-11 11:50:33 -080033struct aead_async_rsgl {
34 struct af_alg_sgl sgl;
35 struct list_head list;
36};
37
38struct aead_async_req {
39 struct scatterlist *tsgl;
40 struct aead_async_rsgl first_rsgl;
41 struct list_head list;
42 struct kiocb *iocb;
43 unsigned int tsgls;
44 char iv[];
45};
46
Stephan Mueller2a2a2512017-04-24 11:15:23 +020047struct aead_tfm {
48 struct crypto_aead *aead;
49 bool has_key;
50};
51
Stephan Mueller400c40c2015-02-28 20:50:00 +010052struct aead_ctx {
53 struct aead_sg_list tsgl;
Tadeusz Struk83094e5e2016-03-11 11:50:33 -080054 struct aead_async_rsgl first_rsgl;
55 struct list_head list;
Stephan Mueller400c40c2015-02-28 20:50:00 +010056
57 void *iv;
58
59 struct af_alg_completion completion;
60
61 unsigned long used;
62
63 unsigned int len;
64 bool more;
65 bool merge;
66 bool enc;
67
68 size_t aead_assoclen;
69 struct aead_request aead_req;
70};
71
72static inline int aead_sndbuf(struct sock *sk)
73{
74 struct alg_sock *ask = alg_sk(sk);
75 struct aead_ctx *ctx = ask->private;
76
77 return max_t(int, max_t(int, sk->sk_sndbuf & PAGE_MASK, PAGE_SIZE) -
78 ctx->used, 0);
79}
80
81static inline bool aead_writable(struct sock *sk)
82{
83 return PAGE_SIZE <= aead_sndbuf(sk);
84}
85
86static inline bool aead_sufficient_data(struct aead_ctx *ctx)
87{
88 unsigned as = crypto_aead_authsize(crypto_aead_reqtfm(&ctx->aead_req));
89
Stephan Mueller0c1e16c2016-12-05 15:26:19 +010090 /*
91 * The minimum amount of memory needed for an AEAD cipher is
92 * the AAD and in case of decryption the tag.
93 */
94 return ctx->used >= ctx->aead_assoclen + (ctx->enc ? 0 : as);
Stephan Mueller400c40c2015-02-28 20:50:00 +010095}
96
Tadeusz Struk83094e5e2016-03-11 11:50:33 -080097static void aead_reset_ctx(struct aead_ctx *ctx)
98{
99 struct aead_sg_list *sgl = &ctx->tsgl;
100
101 sg_init_table(sgl->sg, ALG_MAX_PAGES);
102 sgl->cur = 0;
103 ctx->used = 0;
104 ctx->more = 0;
105 ctx->merge = 0;
106}
107
Stephan Mueller400c40c2015-02-28 20:50:00 +0100108static void aead_put_sgl(struct sock *sk)
109{
110 struct alg_sock *ask = alg_sk(sk);
111 struct aead_ctx *ctx = ask->private;
112 struct aead_sg_list *sgl = &ctx->tsgl;
113 struct scatterlist *sg = sgl->sg;
114 unsigned int i;
115
116 for (i = 0; i < sgl->cur; i++) {
117 if (!sg_page(sg + i))
118 continue;
119
120 put_page(sg_page(sg + i));
121 sg_assign_page(sg + i, NULL);
122 }
Tadeusz Struk83094e5e2016-03-11 11:50:33 -0800123 aead_reset_ctx(ctx);
Stephan Mueller400c40c2015-02-28 20:50:00 +0100124}
125
126static void aead_wmem_wakeup(struct sock *sk)
127{
128 struct socket_wq *wq;
129
130 if (!aead_writable(sk))
131 return;
132
133 rcu_read_lock();
134 wq = rcu_dereference(sk->sk_wq);
Herbert Xu1ce0bf52015-11-26 13:55:39 +0800135 if (skwq_has_sleeper(wq))
Stephan Mueller400c40c2015-02-28 20:50:00 +0100136 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
137 POLLRDNORM |
138 POLLRDBAND);
139 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
140 rcu_read_unlock();
141}
142
143static int aead_wait_for_data(struct sock *sk, unsigned flags)
144{
WANG Congd9dc8b02016-11-11 10:20:50 -0800145 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Stephan Mueller400c40c2015-02-28 20:50:00 +0100146 struct alg_sock *ask = alg_sk(sk);
147 struct aead_ctx *ctx = ask->private;
148 long timeout;
Stephan Mueller400c40c2015-02-28 20:50:00 +0100149 int err = -ERESTARTSYS;
150
151 if (flags & MSG_DONTWAIT)
152 return -EAGAIN;
153
Eric Dumazet9cd3e072015-11-29 20:03:10 -0800154 sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
WANG Congd9dc8b02016-11-11 10:20:50 -0800155 add_wait_queue(sk_sleep(sk), &wait);
Stephan Mueller400c40c2015-02-28 20:50:00 +0100156 for (;;) {
157 if (signal_pending(current))
158 break;
Stephan Mueller400c40c2015-02-28 20:50:00 +0100159 timeout = MAX_SCHEDULE_TIMEOUT;
WANG Congd9dc8b02016-11-11 10:20:50 -0800160 if (sk_wait_event(sk, &timeout, !ctx->more, &wait)) {
Stephan Mueller400c40c2015-02-28 20:50:00 +0100161 err = 0;
162 break;
163 }
164 }
WANG Congd9dc8b02016-11-11 10:20:50 -0800165 remove_wait_queue(sk_sleep(sk), &wait);
Stephan Mueller400c40c2015-02-28 20:50:00 +0100166
Eric Dumazet9cd3e072015-11-29 20:03:10 -0800167 sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
Stephan Mueller400c40c2015-02-28 20:50:00 +0100168
169 return err;
170}
171
172static void aead_data_wakeup(struct sock *sk)
173{
174 struct alg_sock *ask = alg_sk(sk);
175 struct aead_ctx *ctx = ask->private;
176 struct socket_wq *wq;
177
178 if (ctx->more)
179 return;
180 if (!ctx->used)
181 return;
182
183 rcu_read_lock();
184 wq = rcu_dereference(sk->sk_wq);
Herbert Xu1ce0bf52015-11-26 13:55:39 +0800185 if (skwq_has_sleeper(wq))
Stephan Mueller400c40c2015-02-28 20:50:00 +0100186 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
187 POLLRDNORM |
188 POLLRDBAND);
189 sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
190 rcu_read_unlock();
191}
192
Linus Torvaldseccd02f2015-04-15 14:09:46 -0700193static int aead_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
Stephan Mueller400c40c2015-02-28 20:50:00 +0100194{
195 struct sock *sk = sock->sk;
196 struct alg_sock *ask = alg_sk(sk);
197 struct aead_ctx *ctx = ask->private;
198 unsigned ivsize =
199 crypto_aead_ivsize(crypto_aead_reqtfm(&ctx->aead_req));
200 struct aead_sg_list *sgl = &ctx->tsgl;
201 struct af_alg_control con = {};
202 long copied = 0;
203 bool enc = 0;
204 bool init = 0;
205 int err = -EINVAL;
206
207 if (msg->msg_controllen) {
208 err = af_alg_cmsg_send(msg, &con);
209 if (err)
210 return err;
211
212 init = 1;
213 switch (con.op) {
214 case ALG_OP_ENCRYPT:
215 enc = 1;
216 break;
217 case ALG_OP_DECRYPT:
218 enc = 0;
219 break;
220 default:
221 return -EINVAL;
222 }
223
224 if (con.iv && con.iv->ivlen != ivsize)
225 return -EINVAL;
226 }
227
228 lock_sock(sk);
229 if (!ctx->more && ctx->used)
230 goto unlock;
231
232 if (init) {
233 ctx->enc = enc;
234 if (con.iv)
235 memcpy(ctx->iv, con.iv->iv, ivsize);
236
237 ctx->aead_assoclen = con.aead_assoclen;
238 }
239
240 while (size) {
LABBE Corentin652d5b82015-10-23 14:10:36 +0200241 size_t len = size;
Stephan Mueller400c40c2015-02-28 20:50:00 +0100242 struct scatterlist *sg = NULL;
243
244 /* use the existing memory in an allocated page */
245 if (ctx->merge) {
246 sg = sgl->sg + sgl->cur - 1;
247 len = min_t(unsigned long, len,
248 PAGE_SIZE - sg->offset - sg->length);
249 err = memcpy_from_msg(page_address(sg_page(sg)) +
250 sg->offset + sg->length,
251 msg, len);
252 if (err)
253 goto unlock;
254
255 sg->length += len;
256 ctx->merge = (sg->offset + sg->length) &
257 (PAGE_SIZE - 1);
258
259 ctx->used += len;
260 copied += len;
261 size -= len;
262 continue;
263 }
264
265 if (!aead_writable(sk)) {
266 /* user space sent too much data */
267 aead_put_sgl(sk);
268 err = -EMSGSIZE;
269 goto unlock;
270 }
271
272 /* allocate a new page */
273 len = min_t(unsigned long, size, aead_sndbuf(sk));
274 while (len) {
LABBE Corentin652d5b82015-10-23 14:10:36 +0200275 size_t plen = 0;
Stephan Mueller400c40c2015-02-28 20:50:00 +0100276
277 if (sgl->cur >= ALG_MAX_PAGES) {
278 aead_put_sgl(sk);
279 err = -E2BIG;
280 goto unlock;
281 }
282
283 sg = sgl->sg + sgl->cur;
LABBE Corentin652d5b82015-10-23 14:10:36 +0200284 plen = min_t(size_t, len, PAGE_SIZE);
Stephan Mueller400c40c2015-02-28 20:50:00 +0100285
286 sg_assign_page(sg, alloc_page(GFP_KERNEL));
287 err = -ENOMEM;
288 if (!sg_page(sg))
289 goto unlock;
290
291 err = memcpy_from_msg(page_address(sg_page(sg)),
292 msg, plen);
293 if (err) {
294 __free_page(sg_page(sg));
295 sg_assign_page(sg, NULL);
296 goto unlock;
297 }
298
299 sg->offset = 0;
300 sg->length = plen;
301 len -= plen;
302 ctx->used += plen;
303 copied += plen;
304 sgl->cur++;
305 size -= plen;
306 ctx->merge = plen & (PAGE_SIZE - 1);
307 }
308 }
309
310 err = 0;
311
312 ctx->more = msg->msg_flags & MSG_MORE;
313 if (!ctx->more && !aead_sufficient_data(ctx)) {
314 aead_put_sgl(sk);
315 err = -EMSGSIZE;
316 }
317
318unlock:
319 aead_data_wakeup(sk);
320 release_sock(sk);
321
322 return err ?: copied;
323}
324
325static ssize_t aead_sendpage(struct socket *sock, struct page *page,
326 int offset, size_t size, int flags)
327{
328 struct sock *sk = sock->sk;
329 struct alg_sock *ask = alg_sk(sk);
330 struct aead_ctx *ctx = ask->private;
331 struct aead_sg_list *sgl = &ctx->tsgl;
332 int err = -EINVAL;
333
334 if (flags & MSG_SENDPAGE_NOTLAST)
335 flags |= MSG_MORE;
336
337 if (sgl->cur >= ALG_MAX_PAGES)
338 return -E2BIG;
339
340 lock_sock(sk);
341 if (!ctx->more && ctx->used)
342 goto unlock;
343
344 if (!size)
345 goto done;
346
347 if (!aead_writable(sk)) {
348 /* user space sent too much data */
349 aead_put_sgl(sk);
350 err = -EMSGSIZE;
351 goto unlock;
352 }
353
354 ctx->merge = 0;
355
356 get_page(page);
357 sg_set_page(sgl->sg + sgl->cur, page, size, offset);
358 sgl->cur++;
359 ctx->used += size;
360
361 err = 0;
362
363done:
364 ctx->more = flags & MSG_MORE;
365 if (!ctx->more && !aead_sufficient_data(ctx)) {
366 aead_put_sgl(sk);
367 err = -EMSGSIZE;
368 }
369
370unlock:
371 aead_data_wakeup(sk);
372 release_sock(sk);
373
374 return err ?: size;
375}
376
Tadeusz Struk83094e5e2016-03-11 11:50:33 -0800377#define GET_ASYM_REQ(req, tfm) (struct aead_async_req *) \
378 ((char *)req + sizeof(struct aead_request) + \
379 crypto_aead_reqsize(tfm))
380
381 #define GET_REQ_SIZE(tfm) sizeof(struct aead_async_req) + \
382 crypto_aead_reqsize(tfm) + crypto_aead_ivsize(tfm) + \
383 sizeof(struct aead_request)
384
385static void aead_async_cb(struct crypto_async_request *_req, int err)
386{
387 struct sock *sk = _req->data;
388 struct alg_sock *ask = alg_sk(sk);
389 struct aead_ctx *ctx = ask->private;
390 struct crypto_aead *tfm = crypto_aead_reqtfm(&ctx->aead_req);
391 struct aead_request *req = aead_request_cast(_req);
392 struct aead_async_req *areq = GET_ASYM_REQ(req, tfm);
393 struct scatterlist *sg = areq->tsgl;
394 struct aead_async_rsgl *rsgl;
395 struct kiocb *iocb = areq->iocb;
396 unsigned int i, reqlen = GET_REQ_SIZE(tfm);
397
398 list_for_each_entry(rsgl, &areq->list, list) {
399 af_alg_free_sg(&rsgl->sgl);
400 if (rsgl != &areq->first_rsgl)
401 sock_kfree_s(sk, rsgl, sizeof(*rsgl));
402 }
403
404 for (i = 0; i < areq->tsgls; i++)
405 put_page(sg_page(sg + i));
406
407 sock_kfree_s(sk, areq->tsgl, sizeof(*areq->tsgl) * areq->tsgls);
408 sock_kfree_s(sk, req, reqlen);
409 __sock_put(sk);
410 iocb->ki_complete(iocb, err, err);
411}
412
413static int aead_recvmsg_async(struct socket *sock, struct msghdr *msg,
414 int flags)
415{
416 struct sock *sk = sock->sk;
417 struct alg_sock *ask = alg_sk(sk);
418 struct aead_ctx *ctx = ask->private;
419 struct crypto_aead *tfm = crypto_aead_reqtfm(&ctx->aead_req);
420 struct aead_async_req *areq;
421 struct aead_request *req = NULL;
422 struct aead_sg_list *sgl = &ctx->tsgl;
423 struct aead_async_rsgl *last_rsgl = NULL, *rsgl;
424 unsigned int as = crypto_aead_authsize(tfm);
425 unsigned int i, reqlen = GET_REQ_SIZE(tfm);
426 int err = -ENOMEM;
427 unsigned long used;
Stephan Mueller678b5c62016-12-08 07:09:44 +0100428 size_t outlen = 0;
Tadeusz Struk83094e5e2016-03-11 11:50:33 -0800429 size_t usedpages = 0;
430
431 lock_sock(sk);
432 if (ctx->more) {
433 err = aead_wait_for_data(sk, flags);
434 if (err)
435 goto unlock;
436 }
437
Tadeusz Struk83094e5e2016-03-11 11:50:33 -0800438 if (!aead_sufficient_data(ctx))
439 goto unlock;
440
Stephan Mueller0c1e16c2016-12-05 15:26:19 +0100441 used = ctx->used;
442 if (ctx->enc)
443 outlen = used + as;
444 else
445 outlen = used - as;
446
Tadeusz Struk83094e5e2016-03-11 11:50:33 -0800447 req = sock_kmalloc(sk, reqlen, GFP_KERNEL);
448 if (unlikely(!req))
449 goto unlock;
450
451 areq = GET_ASYM_REQ(req, tfm);
452 memset(&areq->first_rsgl, '\0', sizeof(areq->first_rsgl));
453 INIT_LIST_HEAD(&areq->list);
454 areq->iocb = msg->msg_iocb;
455 memcpy(areq->iv, ctx->iv, crypto_aead_ivsize(tfm));
456 aead_request_set_tfm(req, tfm);
457 aead_request_set_ad(req, ctx->aead_assoclen);
458 aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
459 aead_async_cb, sk);
Stephan Mueller0c1e16c2016-12-05 15:26:19 +0100460 used -= ctx->aead_assoclen;
Tadeusz Struk83094e5e2016-03-11 11:50:33 -0800461
462 /* take over all tx sgls from ctx */
Stephan Mueller5937d812016-12-01 08:22:37 +0100463 areq->tsgl = sock_kmalloc(sk,
464 sizeof(*areq->tsgl) * max_t(u32, sgl->cur, 1),
Tadeusz Struk83094e5e2016-03-11 11:50:33 -0800465 GFP_KERNEL);
466 if (unlikely(!areq->tsgl))
467 goto free;
468
Stephan Mueller5937d812016-12-01 08:22:37 +0100469 sg_init_table(areq->tsgl, max_t(u32, sgl->cur, 1));
Tadeusz Struk83094e5e2016-03-11 11:50:33 -0800470 for (i = 0; i < sgl->cur; i++)
471 sg_set_page(&areq->tsgl[i], sg_page(&sgl->sg[i]),
472 sgl->sg[i].length, sgl->sg[i].offset);
473
474 areq->tsgls = sgl->cur;
475
476 /* create rx sgls */
Stephan Mueller0c1e16c2016-12-05 15:26:19 +0100477 while (outlen > usedpages && iov_iter_count(&msg->msg_iter)) {
Tadeusz Struk83094e5e2016-03-11 11:50:33 -0800478 size_t seglen = min_t(size_t, iov_iter_count(&msg->msg_iter),
479 (outlen - usedpages));
480
481 if (list_empty(&areq->list)) {
482 rsgl = &areq->first_rsgl;
483
484 } else {
485 rsgl = sock_kmalloc(sk, sizeof(*rsgl), GFP_KERNEL);
486 if (unlikely(!rsgl)) {
487 err = -ENOMEM;
488 goto free;
489 }
490 }
491 rsgl->sgl.npages = 0;
492 list_add_tail(&rsgl->list, &areq->list);
493
494 /* make one iovec available as scatterlist */
495 err = af_alg_make_sg(&rsgl->sgl, &msg->msg_iter, seglen);
496 if (err < 0)
497 goto free;
498
499 usedpages += err;
500
501 /* chain the new scatterlist with previous one */
502 if (last_rsgl)
503 af_alg_link_sg(&last_rsgl->sgl, &rsgl->sgl);
504
505 last_rsgl = rsgl;
506
Tadeusz Struk83094e5e2016-03-11 11:50:33 -0800507 iov_iter_advance(&msg->msg_iter, err);
508 }
Stephan Mueller0c1e16c2016-12-05 15:26:19 +0100509
Tadeusz Struk83094e5e2016-03-11 11:50:33 -0800510 /* ensure output buffer is sufficiently large */
Stephan Mueller0c1e16c2016-12-05 15:26:19 +0100511 if (usedpages < outlen) {
512 err = -EINVAL;
513 goto unlock;
514 }
Tadeusz Struk83094e5e2016-03-11 11:50:33 -0800515
516 aead_request_set_crypt(req, areq->tsgl, areq->first_rsgl.sgl.sg, used,
517 areq->iv);
518 err = ctx->enc ? crypto_aead_encrypt(req) : crypto_aead_decrypt(req);
519 if (err) {
520 if (err == -EINPROGRESS) {
521 sock_hold(sk);
522 err = -EIOCBQUEUED;
523 aead_reset_ctx(ctx);
524 goto unlock;
525 } else if (err == -EBADMSG) {
526 aead_put_sgl(sk);
527 }
528 goto free;
529 }
530 aead_put_sgl(sk);
531
532free:
533 list_for_each_entry(rsgl, &areq->list, list) {
534 af_alg_free_sg(&rsgl->sgl);
535 if (rsgl != &areq->first_rsgl)
536 sock_kfree_s(sk, rsgl, sizeof(*rsgl));
537 }
538 if (areq->tsgl)
539 sock_kfree_s(sk, areq->tsgl, sizeof(*areq->tsgl) * areq->tsgls);
540 if (req)
541 sock_kfree_s(sk, req, reqlen);
542unlock:
543 aead_wmem_wakeup(sk);
544 release_sock(sk);
545 return err ? err : outlen;
546}
547
548static int aead_recvmsg_sync(struct socket *sock, struct msghdr *msg, int flags)
Stephan Mueller400c40c2015-02-28 20:50:00 +0100549{
550 struct sock *sk = sock->sk;
551 struct alg_sock *ask = alg_sk(sk);
552 struct aead_ctx *ctx = ask->private;
Stephan Mueller400c40c2015-02-28 20:50:00 +0100553 unsigned as = crypto_aead_authsize(crypto_aead_reqtfm(&ctx->aead_req));
554 struct aead_sg_list *sgl = &ctx->tsgl;
Tadeusz Struk83094e5e2016-03-11 11:50:33 -0800555 struct aead_async_rsgl *last_rsgl = NULL;
556 struct aead_async_rsgl *rsgl, *tmp;
Stephan Mueller400c40c2015-02-28 20:50:00 +0100557 int err = -EINVAL;
558 unsigned long used = 0;
559 size_t outlen = 0;
560 size_t usedpages = 0;
Stephan Mueller400c40c2015-02-28 20:50:00 +0100561
562 lock_sock(sk);
563
564 /*
Stephan Mueller3f692d52016-10-21 04:59:24 +0200565 * Please see documentation of aead_request_set_crypt for the
566 * description of the AEAD memory structure expected from the caller.
Stephan Mueller400c40c2015-02-28 20:50:00 +0100567 */
568
569 if (ctx->more) {
570 err = aead_wait_for_data(sk, flags);
571 if (err)
572 goto unlock;
573 }
574
Stephan Mueller0c1e16c2016-12-05 15:26:19 +0100575 /* data length provided by caller via sendmsg/sendpage */
Stephan Mueller400c40c2015-02-28 20:50:00 +0100576 used = ctx->used;
577
578 /*
579 * Make sure sufficient data is present -- note, the same check is
580 * is also present in sendmsg/sendpage. The checks in sendpage/sendmsg
581 * shall provide an information to the data sender that something is
582 * wrong, but they are irrelevant to maintain the kernel integrity.
583 * We need this check here too in case user space decides to not honor
584 * the error message in sendmsg/sendpage and still call recvmsg. This
585 * check here protects the kernel integrity.
586 */
587 if (!aead_sufficient_data(ctx))
588 goto unlock;
589
Stephan Mueller0c1e16c2016-12-05 15:26:19 +0100590 /*
591 * Calculate the minimum output buffer size holding the result of the
592 * cipher operation. When encrypting data, the receiving buffer is
593 * larger by the tag length compared to the input buffer as the
594 * encryption operation generates the tag. For decryption, the input
595 * buffer provides the tag which is consumed resulting in only the
596 * plaintext without a buffer for the tag returned to the caller.
597 */
598 if (ctx->enc)
599 outlen = used + as;
600 else
601 outlen = used - as;
Herbert Xu19fa7752015-05-27 17:24:41 +0800602
Stephan Mueller400c40c2015-02-28 20:50:00 +0100603 /*
604 * The cipher operation input data is reduced by the associated data
605 * length as this data is processed separately later on.
606 */
Stephan Mueller0c1e16c2016-12-05 15:26:19 +0100607 used -= ctx->aead_assoclen;
Stephan Mueller400c40c2015-02-28 20:50:00 +0100608
609 /* convert iovecs of output buffers into scatterlists */
Stephan Mueller0c1e16c2016-12-05 15:26:19 +0100610 while (outlen > usedpages && iov_iter_count(&msg->msg_iter)) {
Stephan Mueller400c40c2015-02-28 20:50:00 +0100611 size_t seglen = min_t(size_t, iov_iter_count(&msg->msg_iter),
612 (outlen - usedpages));
613
Tadeusz Struk83094e5e2016-03-11 11:50:33 -0800614 if (list_empty(&ctx->list)) {
615 rsgl = &ctx->first_rsgl;
616 } else {
617 rsgl = sock_kmalloc(sk, sizeof(*rsgl), GFP_KERNEL);
618 if (unlikely(!rsgl)) {
619 err = -ENOMEM;
620 goto unlock;
621 }
622 }
623 rsgl->sgl.npages = 0;
624 list_add_tail(&rsgl->list, &ctx->list);
625
Stephan Mueller400c40c2015-02-28 20:50:00 +0100626 /* make one iovec available as scatterlist */
Tadeusz Struk83094e5e2016-03-11 11:50:33 -0800627 err = af_alg_make_sg(&rsgl->sgl, &msg->msg_iter, seglen);
Stephan Mueller400c40c2015-02-28 20:50:00 +0100628 if (err < 0)
629 goto unlock;
630 usedpages += err;
Tadeusz Struk7b2a18e2015-05-15 10:18:37 -0700631 /* chain the new scatterlist with previous one */
Tadeusz Struk83094e5e2016-03-11 11:50:33 -0800632 if (last_rsgl)
633 af_alg_link_sg(&last_rsgl->sgl, &rsgl->sgl);
634
635 last_rsgl = rsgl;
Tadeusz Struk7b2a18e2015-05-15 10:18:37 -0700636
Stephan Mueller400c40c2015-02-28 20:50:00 +0100637 iov_iter_advance(&msg->msg_iter, err);
Stephan Mueller400c40c2015-02-28 20:50:00 +0100638 }
639
Stephan Mueller400c40c2015-02-28 20:50:00 +0100640 /* ensure output buffer is sufficiently large */
Stephan Mueller0c1e16c2016-12-05 15:26:19 +0100641 if (usedpages < outlen) {
642 err = -EINVAL;
Stephan Mueller400c40c2015-02-28 20:50:00 +0100643 goto unlock;
Stephan Mueller0c1e16c2016-12-05 15:26:19 +0100644 }
Stephan Mueller400c40c2015-02-28 20:50:00 +0100645
Herbert Xu19fa7752015-05-27 17:24:41 +0800646 sg_mark_end(sgl->sg + sgl->cur - 1);
Tadeusz Struk83094e5e2016-03-11 11:50:33 -0800647 aead_request_set_crypt(&ctx->aead_req, sgl->sg, ctx->first_rsgl.sgl.sg,
Herbert Xu19fa7752015-05-27 17:24:41 +0800648 used, ctx->iv);
649 aead_request_set_ad(&ctx->aead_req, ctx->aead_assoclen);
Stephan Mueller400c40c2015-02-28 20:50:00 +0100650
651 err = af_alg_wait_for_completion(ctx->enc ?
652 crypto_aead_encrypt(&ctx->aead_req) :
653 crypto_aead_decrypt(&ctx->aead_req),
654 &ctx->completion);
655
656 if (err) {
657 /* EBADMSG implies a valid cipher operation took place */
658 if (err == -EBADMSG)
659 aead_put_sgl(sk);
Tadeusz Struk83094e5e2016-03-11 11:50:33 -0800660
Stephan Mueller400c40c2015-02-28 20:50:00 +0100661 goto unlock;
662 }
663
664 aead_put_sgl(sk);
Stephan Mueller400c40c2015-02-28 20:50:00 +0100665 err = 0;
666
667unlock:
Tadeusz Struk83094e5e2016-03-11 11:50:33 -0800668 list_for_each_entry_safe(rsgl, tmp, &ctx->list, list) {
669 af_alg_free_sg(&rsgl->sgl);
Harsh Jain0b529f12017-02-01 21:10:28 +0530670 list_del(&rsgl->list);
Tadeusz Struk83094e5e2016-03-11 11:50:33 -0800671 if (rsgl != &ctx->first_rsgl)
672 sock_kfree_s(sk, rsgl, sizeof(*rsgl));
Tadeusz Struk83094e5e2016-03-11 11:50:33 -0800673 }
674 INIT_LIST_HEAD(&ctx->list);
Stephan Mueller400c40c2015-02-28 20:50:00 +0100675 aead_wmem_wakeup(sk);
676 release_sock(sk);
677
678 return err ? err : outlen;
679}
680
Tadeusz Struk83094e5e2016-03-11 11:50:33 -0800681static int aead_recvmsg(struct socket *sock, struct msghdr *msg, size_t ignored,
682 int flags)
683{
684 return (msg->msg_iocb && !is_sync_kiocb(msg->msg_iocb)) ?
685 aead_recvmsg_async(sock, msg, flags) :
686 aead_recvmsg_sync(sock, msg, flags);
687}
688
Stephan Mueller400c40c2015-02-28 20:50:00 +0100689static unsigned int aead_poll(struct file *file, struct socket *sock,
690 poll_table *wait)
691{
692 struct sock *sk = sock->sk;
693 struct alg_sock *ask = alg_sk(sk);
694 struct aead_ctx *ctx = ask->private;
695 unsigned int mask;
696
697 sock_poll_wait(file, sk_sleep(sk), wait);
698 mask = 0;
699
700 if (!ctx->more)
701 mask |= POLLIN | POLLRDNORM;
702
703 if (aead_writable(sk))
704 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
705
706 return mask;
707}
708
709static struct proto_ops algif_aead_ops = {
710 .family = PF_ALG,
711
712 .connect = sock_no_connect,
713 .socketpair = sock_no_socketpair,
714 .getname = sock_no_getname,
715 .ioctl = sock_no_ioctl,
716 .listen = sock_no_listen,
717 .shutdown = sock_no_shutdown,
718 .getsockopt = sock_no_getsockopt,
719 .mmap = sock_no_mmap,
720 .bind = sock_no_bind,
721 .accept = sock_no_accept,
722 .setsockopt = sock_no_setsockopt,
723
724 .release = af_alg_release,
725 .sendmsg = aead_sendmsg,
726 .sendpage = aead_sendpage,
727 .recvmsg = aead_recvmsg,
728 .poll = aead_poll,
729};
730
Stephan Mueller2a2a2512017-04-24 11:15:23 +0200731static int aead_check_key(struct socket *sock)
732{
733 int err = 0;
734 struct sock *psk;
735 struct alg_sock *pask;
736 struct aead_tfm *tfm;
737 struct sock *sk = sock->sk;
738 struct alg_sock *ask = alg_sk(sk);
739
740 lock_sock(sk);
741 if (ask->refcnt)
742 goto unlock_child;
743
744 psk = ask->parent;
745 pask = alg_sk(ask->parent);
746 tfm = pask->private;
747
748 err = -ENOKEY;
749 lock_sock_nested(psk, SINGLE_DEPTH_NESTING);
750 if (!tfm->has_key)
751 goto unlock;
752
753 if (!pask->refcnt++)
754 sock_hold(psk);
755
756 ask->refcnt = 1;
757 sock_put(psk);
758
759 err = 0;
760
761unlock:
762 release_sock(psk);
763unlock_child:
764 release_sock(sk);
765
766 return err;
767}
768
769static int aead_sendmsg_nokey(struct socket *sock, struct msghdr *msg,
770 size_t size)
771{
772 int err;
773
774 err = aead_check_key(sock);
775 if (err)
776 return err;
777
778 return aead_sendmsg(sock, msg, size);
779}
780
781static ssize_t aead_sendpage_nokey(struct socket *sock, struct page *page,
782 int offset, size_t size, int flags)
783{
784 int err;
785
786 err = aead_check_key(sock);
787 if (err)
788 return err;
789
790 return aead_sendpage(sock, page, offset, size, flags);
791}
792
793static int aead_recvmsg_nokey(struct socket *sock, struct msghdr *msg,
794 size_t ignored, int flags)
795{
796 int err;
797
798 err = aead_check_key(sock);
799 if (err)
800 return err;
801
802 return aead_recvmsg(sock, msg, ignored, flags);
803}
804
805static struct proto_ops algif_aead_ops_nokey = {
806 .family = PF_ALG,
807
808 .connect = sock_no_connect,
809 .socketpair = sock_no_socketpair,
810 .getname = sock_no_getname,
811 .ioctl = sock_no_ioctl,
812 .listen = sock_no_listen,
813 .shutdown = sock_no_shutdown,
814 .getsockopt = sock_no_getsockopt,
815 .mmap = sock_no_mmap,
816 .bind = sock_no_bind,
817 .accept = sock_no_accept,
818 .setsockopt = sock_no_setsockopt,
819
820 .release = af_alg_release,
821 .sendmsg = aead_sendmsg_nokey,
822 .sendpage = aead_sendpage_nokey,
823 .recvmsg = aead_recvmsg_nokey,
824 .poll = aead_poll,
825};
826
Stephan Mueller400c40c2015-02-28 20:50:00 +0100827static void *aead_bind(const char *name, u32 type, u32 mask)
828{
Stephan Mueller2a2a2512017-04-24 11:15:23 +0200829 struct aead_tfm *tfm;
830 struct crypto_aead *aead;
831
832 tfm = kzalloc(sizeof(*tfm), GFP_KERNEL);
833 if (!tfm)
834 return ERR_PTR(-ENOMEM);
835
836 aead = crypto_alloc_aead(name, type, mask);
837 if (IS_ERR(aead)) {
838 kfree(tfm);
839 return ERR_CAST(aead);
840 }
841
842 tfm->aead = aead;
843
844 return tfm;
Stephan Mueller400c40c2015-02-28 20:50:00 +0100845}
846
847static void aead_release(void *private)
848{
Stephan Mueller2a2a2512017-04-24 11:15:23 +0200849 struct aead_tfm *tfm = private;
850
851 crypto_free_aead(tfm->aead);
852 kfree(tfm);
Stephan Mueller400c40c2015-02-28 20:50:00 +0100853}
854
855static int aead_setauthsize(void *private, unsigned int authsize)
856{
Stephan Mueller2a2a2512017-04-24 11:15:23 +0200857 struct aead_tfm *tfm = private;
858
859 return crypto_aead_setauthsize(tfm->aead, authsize);
Stephan Mueller400c40c2015-02-28 20:50:00 +0100860}
861
862static int aead_setkey(void *private, const u8 *key, unsigned int keylen)
863{
Stephan Mueller2a2a2512017-04-24 11:15:23 +0200864 struct aead_tfm *tfm = private;
865 int err;
866
867 err = crypto_aead_setkey(tfm->aead, key, keylen);
868 tfm->has_key = !err;
869
870 return err;
Stephan Mueller400c40c2015-02-28 20:50:00 +0100871}
872
873static void aead_sock_destruct(struct sock *sk)
874{
875 struct alg_sock *ask = alg_sk(sk);
876 struct aead_ctx *ctx = ask->private;
877 unsigned int ivlen = crypto_aead_ivsize(
878 crypto_aead_reqtfm(&ctx->aead_req));
879
Tadeusz Struk83094e5e2016-03-11 11:50:33 -0800880 WARN_ON(atomic_read(&sk->sk_refcnt) != 0);
Stephan Mueller400c40c2015-02-28 20:50:00 +0100881 aead_put_sgl(sk);
882 sock_kzfree_s(sk, ctx->iv, ivlen);
883 sock_kfree_s(sk, ctx, ctx->len);
884 af_alg_release_parent(sk);
885}
886
Stephan Mueller2a2a2512017-04-24 11:15:23 +0200887static int aead_accept_parent_nokey(void *private, struct sock *sk)
Stephan Mueller400c40c2015-02-28 20:50:00 +0100888{
889 struct aead_ctx *ctx;
890 struct alg_sock *ask = alg_sk(sk);
Stephan Mueller2a2a2512017-04-24 11:15:23 +0200891 struct aead_tfm *tfm = private;
892 struct crypto_aead *aead = tfm->aead;
893 unsigned int len = sizeof(*ctx) + crypto_aead_reqsize(aead);
894 unsigned int ivlen = crypto_aead_ivsize(aead);
Stephan Mueller400c40c2015-02-28 20:50:00 +0100895
896 ctx = sock_kmalloc(sk, len, GFP_KERNEL);
897 if (!ctx)
898 return -ENOMEM;
899 memset(ctx, 0, len);
900
901 ctx->iv = sock_kmalloc(sk, ivlen, GFP_KERNEL);
902 if (!ctx->iv) {
903 sock_kfree_s(sk, ctx, len);
904 return -ENOMEM;
905 }
906 memset(ctx->iv, 0, ivlen);
907
908 ctx->len = len;
909 ctx->used = 0;
910 ctx->more = 0;
911 ctx->merge = 0;
912 ctx->enc = 0;
913 ctx->tsgl.cur = 0;
914 ctx->aead_assoclen = 0;
915 af_alg_init_completion(&ctx->completion);
916 sg_init_table(ctx->tsgl.sg, ALG_MAX_PAGES);
Tadeusz Struk83094e5e2016-03-11 11:50:33 -0800917 INIT_LIST_HEAD(&ctx->list);
Stephan Mueller400c40c2015-02-28 20:50:00 +0100918
919 ask->private = ctx;
920
Stephan Mueller2a2a2512017-04-24 11:15:23 +0200921 aead_request_set_tfm(&ctx->aead_req, aead);
Stephan Mueller400c40c2015-02-28 20:50:00 +0100922 aead_request_set_callback(&ctx->aead_req, CRYPTO_TFM_REQ_MAY_BACKLOG,
923 af_alg_complete, &ctx->completion);
924
925 sk->sk_destruct = aead_sock_destruct;
926
927 return 0;
928}
929
Stephan Mueller2a2a2512017-04-24 11:15:23 +0200930static int aead_accept_parent(void *private, struct sock *sk)
931{
932 struct aead_tfm *tfm = private;
933
934 if (!tfm->has_key)
935 return -ENOKEY;
936
937 return aead_accept_parent_nokey(private, sk);
938}
939
Stephan Mueller400c40c2015-02-28 20:50:00 +0100940static const struct af_alg_type algif_type_aead = {
941 .bind = aead_bind,
942 .release = aead_release,
943 .setkey = aead_setkey,
944 .setauthsize = aead_setauthsize,
945 .accept = aead_accept_parent,
Stephan Mueller2a2a2512017-04-24 11:15:23 +0200946 .accept_nokey = aead_accept_parent_nokey,
Stephan Mueller400c40c2015-02-28 20:50:00 +0100947 .ops = &algif_aead_ops,
Stephan Mueller2a2a2512017-04-24 11:15:23 +0200948 .ops_nokey = &algif_aead_ops_nokey,
Stephan Mueller400c40c2015-02-28 20:50:00 +0100949 .name = "aead",
950 .owner = THIS_MODULE
951};
952
953static int __init algif_aead_init(void)
954{
955 return af_alg_register_type(&algif_type_aead);
956}
957
958static void __exit algif_aead_exit(void)
959{
960 int err = af_alg_unregister_type(&algif_type_aead);
961 BUG_ON(err);
962}
963
964module_init(algif_aead_init);
965module_exit(algif_aead_exit);
966MODULE_LICENSE("GPL");
967MODULE_AUTHOR("Stephan Mueller <smueller@chronox.de>");
968MODULE_DESCRIPTION("AEAD kernel crypto API user space interface");