nvme: add tracepoint for nvme_setup_cmd
[linux-2.6-block.git] / crypto / algif_aead.c
CommitLineData
400c40cf
SM
1/*
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 *
400c40cf
SM
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.
d887c52d
SM
12 *
13 * The following concept of the memory management is used:
14 *
15 * The kernel maintains two SGLs, the TX SGL and the RX SGL. The TX SGL is
16 * filled by user space with the data submitted via sendpage/sendmsg. Filling
17 * up the TX SGL does not cause a crypto operation -- the data will only be
18 * tracked by the kernel. Upon receipt of one recvmsg call, the caller must
19 * provide a buffer which is tracked with the RX SGL.
20 *
21 * During the processing of the recvmsg operation, the cipher request is
22 * allocated and prepared. As part of the recvmsg operation, the processed
23 * TX buffers are extracted from the TX SGL into a separate SGL.
24 *
25 * After the completion of the crypto operation, the RX SGL and the cipher
26 * request is released. The extracted TX SGL parts are released together with
27 * the RX SGL release.
400c40cf
SM
28 */
29
83094e5e 30#include <crypto/internal/aead.h>
400c40cf
SM
31#include <crypto/scatterwalk.h>
32#include <crypto/if_alg.h>
72548b09
SM
33#include <crypto/skcipher.h>
34#include <crypto/null.h>
400c40cf
SM
35#include <linux/init.h>
36#include <linux/list.h>
37#include <linux/kernel.h>
38#include <linux/mm.h>
39#include <linux/module.h>
40#include <linux/net.h>
41#include <net/sock.h>
42
2a2a251f
SM
43struct aead_tfm {
44 struct crypto_aead *aead;
45 bool has_key;
72548b09 46 struct crypto_skcipher *null_tfm;
2a2a251f
SM
47};
48
d887c52d
SM
49static inline bool aead_sufficient_data(struct sock *sk)
50{
51 struct alg_sock *ask = alg_sk(sk);
52 struct sock *psk = ask->parent;
53 struct alg_sock *pask = alg_sk(psk);
2d97591e 54 struct af_alg_ctx *ctx = ask->private;
d887c52d
SM
55 struct aead_tfm *aeadc = pask->private;
56 struct crypto_aead *tfm = aeadc->aead;
57 unsigned int as = crypto_aead_authsize(tfm);
400c40cf 58
0c1e16cd
SM
59 /*
60 * The minimum amount of memory needed for an AEAD cipher is
61 * the AAD and in case of decryption the tag.
62 */
63 return ctx->used >= ctx->aead_assoclen + (ctx->enc ? 0 : as);
400c40cf
SM
64}
65
eccd02f3 66static int aead_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
400c40cf
SM
67{
68 struct sock *sk = sock->sk;
69 struct alg_sock *ask = alg_sk(sk);
d887c52d
SM
70 struct sock *psk = ask->parent;
71 struct alg_sock *pask = alg_sk(psk);
d887c52d
SM
72 struct aead_tfm *aeadc = pask->private;
73 struct crypto_aead *tfm = aeadc->aead;
74 unsigned int ivsize = crypto_aead_ivsize(tfm);
400c40cf 75
2d97591e 76 return af_alg_sendmsg(sock, msg, size, ivsize);
83094e5e
TS
77}
78
72548b09
SM
79static int crypto_aead_copy_sgl(struct crypto_skcipher *null_tfm,
80 struct scatterlist *src,
81 struct scatterlist *dst, unsigned int len)
82{
83 SKCIPHER_REQUEST_ON_STACK(skreq, null_tfm);
84
85 skcipher_request_set_tfm(skreq, null_tfm);
86 skcipher_request_set_callback(skreq, CRYPTO_TFM_REQ_MAY_BACKLOG,
87 NULL, NULL);
88 skcipher_request_set_crypt(skreq, src, dst, len, NULL);
89
90 return crypto_skcipher_encrypt(skreq);
91}
92
d887c52d
SM
93static int _aead_recvmsg(struct socket *sock, struct msghdr *msg,
94 size_t ignored, int flags)
400c40cf
SM
95{
96 struct sock *sk = sock->sk;
97 struct alg_sock *ask = alg_sk(sk);
d887c52d
SM
98 struct sock *psk = ask->parent;
99 struct alg_sock *pask = alg_sk(psk);
2d97591e 100 struct af_alg_ctx *ctx = ask->private;
d887c52d
SM
101 struct aead_tfm *aeadc = pask->private;
102 struct crypto_aead *tfm = aeadc->aead;
72548b09 103 struct crypto_skcipher *null_tfm = aeadc->null_tfm;
8e1fa89a 104 unsigned int i, as = crypto_aead_authsize(tfm);
2d97591e 105 struct af_alg_async_req *areq;
8e1fa89a
SM
106 struct af_alg_tsgl *tsgl, *tmp;
107 struct scatterlist *rsgl_src, *tsgl_src = NULL;
d887c52d
SM
108 int err = 0;
109 size_t used = 0; /* [in] TX bufs to be en/decrypted */
110 size_t outlen = 0; /* [out] RX bufs produced by kernel */
111 size_t usedpages = 0; /* [in] RX bufs to be used from user */
112 size_t processed = 0; /* [in] TX bufs to be consumed */
400c40cf
SM
113
114 /*
d887c52d
SM
115 * Data length provided by caller via sendmsg/sendpage that has not
116 * yet been processed.
400c40cf 117 */
400c40cf
SM
118 used = ctx->used;
119
120 /*
121 * Make sure sufficient data is present -- note, the same check is
122 * is also present in sendmsg/sendpage. The checks in sendpage/sendmsg
123 * shall provide an information to the data sender that something is
124 * wrong, but they are irrelevant to maintain the kernel integrity.
125 * We need this check here too in case user space decides to not honor
126 * the error message in sendmsg/sendpage and still call recvmsg. This
127 * check here protects the kernel integrity.
128 */
d887c52d
SM
129 if (!aead_sufficient_data(sk))
130 return -EINVAL;
400c40cf 131
0c1e16cd
SM
132 /*
133 * Calculate the minimum output buffer size holding the result of the
134 * cipher operation. When encrypting data, the receiving buffer is
135 * larger by the tag length compared to the input buffer as the
136 * encryption operation generates the tag. For decryption, the input
137 * buffer provides the tag which is consumed resulting in only the
138 * plaintext without a buffer for the tag returned to the caller.
139 */
140 if (ctx->enc)
141 outlen = used + as;
142 else
143 outlen = used - as;
19fa7752 144
400c40cf
SM
145 /*
146 * The cipher operation input data is reduced by the associated data
147 * length as this data is processed separately later on.
148 */
0c1e16cd 149 used -= ctx->aead_assoclen;
400c40cf 150
d887c52d 151 /* Allocate cipher request for current operation. */
2d97591e
SM
152 areq = af_alg_alloc_areq(sk, sizeof(struct af_alg_async_req) +
153 crypto_aead_reqsize(tfm));
154 if (IS_ERR(areq))
155 return PTR_ERR(areq);
d887c52d
SM
156
157 /* convert iovecs of output buffers into RX SGL */
2d97591e
SM
158 err = af_alg_get_rsgl(sk, msg, flags, areq, outlen, &usedpages);
159 if (err)
160 goto free;
400c40cf 161
d887c52d
SM
162 /*
163 * Ensure output buffer is sufficiently large. If the caller provides
164 * less buffer space, only use the relative required input size. This
165 * allows AIO operation where the caller sent all data to be processed
166 * and the AIO operation performs the operation on the different chunks
167 * of the input data.
168 */
0c1e16cd 169 if (usedpages < outlen) {
d887c52d 170 size_t less = outlen - usedpages;
400c40cf 171
d887c52d
SM
172 if (used < less) {
173 err = -EINVAL;
174 goto free;
175 }
176 used -= less;
177 outlen -= less;
178 }
400c40cf 179
72548b09 180 processed = used + ctx->aead_assoclen;
8e1fa89a
SM
181 list_for_each_entry_safe(tsgl, tmp, &ctx->tsgl_list, list) {
182 for (i = 0; i < tsgl->cur; i++) {
183 struct scatterlist *process_sg = tsgl->sg + i;
184
185 if (!(process_sg->length) || !sg_page(process_sg))
186 continue;
187 tsgl_src = process_sg;
188 break;
189 }
190 if (tsgl_src)
191 break;
192 }
193 if (processed && !tsgl_src) {
194 err = -EFAULT;
195 goto free;
196 }
72548b09 197
d887c52d 198 /*
72548b09
SM
199 * Copy of AAD from source to destination
200 *
201 * The AAD is copied to the destination buffer without change. Even
202 * when user space uses an in-place cipher operation, the kernel
203 * will copy the data as it does not see whether such in-place operation
204 * is initiated.
205 *
206 * To ensure efficiency, the following implementation ensure that the
207 * ciphers are invoked to perform a crypto operation in-place. This
208 * is achieved by memory management specified as follows.
d887c52d 209 */
72548b09
SM
210
211 /* Use the RX SGL as source (and destination) for crypto op. */
8e1fa89a 212 rsgl_src = areq->first_rsgl.sgl.sg;
72548b09
SM
213
214 if (ctx->enc) {
215 /*
216 * Encryption operation - The in-place cipher operation is
217 * achieved by the following operation:
218 *
75d11e75 219 * TX SGL: AAD || PT
72548b09
SM
220 * | |
221 * | copy |
222 * v v
75d11e75 223 * RX SGL: AAD || PT || Tag
72548b09 224 */
8e1fa89a 225 err = crypto_aead_copy_sgl(null_tfm, tsgl_src,
72548b09
SM
226 areq->first_rsgl.sgl.sg, processed);
227 if (err)
228 goto free;
2d97591e 229 af_alg_pull_tsgl(sk, processed, NULL, 0);
72548b09
SM
230 } else {
231 /*
232 * Decryption operation - To achieve an in-place cipher
233 * operation, the following SGL structure is used:
234 *
235 * TX SGL: AAD || CT || Tag
236 * | | ^
237 * | copy | | Create SGL link.
238 * v v |
239 * RX SGL: AAD || CT ----+
240 */
241
242 /* Copy AAD || CT to RX SGL buffer for in-place operation. */
8e1fa89a 243 err = crypto_aead_copy_sgl(null_tfm, tsgl_src,
72548b09
SM
244 areq->first_rsgl.sgl.sg, outlen);
245 if (err)
246 goto free;
247
248 /* Create TX SGL for tag and chain it to RX SGL. */
2d97591e
SM
249 areq->tsgl_entries = af_alg_count_tsgl(sk, processed,
250 processed - as);
72548b09
SM
251 if (!areq->tsgl_entries)
252 areq->tsgl_entries = 1;
253 areq->tsgl = sock_kmalloc(sk, sizeof(*areq->tsgl) *
254 areq->tsgl_entries,
255 GFP_KERNEL);
256 if (!areq->tsgl) {
257 err = -ENOMEM;
258 goto free;
259 }
260 sg_init_table(areq->tsgl, areq->tsgl_entries);
261
262 /* Release TX SGL, except for tag data and reassign tag data. */
2d97591e 263 af_alg_pull_tsgl(sk, processed, areq->tsgl, processed - as);
72548b09
SM
264
265 /* chain the areq TX SGL holding the tag with RX SGL */
2d97591e 266 if (usedpages) {
72548b09 267 /* RX SGL present */
2d97591e 268 struct af_alg_sgl *sgl_prev = &areq->last_rsgl->sgl;
72548b09
SM
269
270 sg_unmark_end(sgl_prev->sg + sgl_prev->npages - 1);
271 sg_chain(sgl_prev->sg, sgl_prev->npages + 1,
272 areq->tsgl);
273 } else
274 /* no RX SGL present (e.g. authentication only) */
8e1fa89a 275 rsgl_src = areq->tsgl;
d887c52d 276 }
d887c52d
SM
277
278 /* Initialize the crypto operation */
8e1fa89a 279 aead_request_set_crypt(&areq->cra_u.aead_req, rsgl_src,
d887c52d 280 areq->first_rsgl.sgl.sg, used, ctx->iv);
2d97591e
SM
281 aead_request_set_ad(&areq->cra_u.aead_req, ctx->aead_assoclen);
282 aead_request_set_tfm(&areq->cra_u.aead_req, tfm);
d887c52d
SM
283
284 if (msg->msg_iocb && !is_sync_kiocb(msg->msg_iocb)) {
285 /* AIO operation */
7d2c3f54 286 sock_hold(sk);
d887c52d 287 areq->iocb = msg->msg_iocb;
2d97591e 288 aead_request_set_callback(&areq->cra_u.aead_req,
d887c52d 289 CRYPTO_TFM_REQ_MAY_BACKLOG,
2d97591e
SM
290 af_alg_async_cb, areq);
291 err = ctx->enc ? crypto_aead_encrypt(&areq->cra_u.aead_req) :
292 crypto_aead_decrypt(&areq->cra_u.aead_req);
7d2c3f54
SM
293
294 /* AIO operation in progress */
295 if (err == -EINPROGRESS || err == -EBUSY) {
296 /* Remember output size that will be generated. */
297 areq->outlen = outlen;
298
299 return -EIOCBQUEUED;
300 }
301
302 sock_put(sk);
d887c52d
SM
303 } else {
304 /* Synchronous operation */
2d97591e 305 aead_request_set_callback(&areq->cra_u.aead_req,
d887c52d 306 CRYPTO_TFM_REQ_MAY_BACKLOG,
2c3f8b16
GBY
307 crypto_req_done, &ctx->wait);
308 err = crypto_wait_req(ctx->enc ?
2d97591e
SM
309 crypto_aead_encrypt(&areq->cra_u.aead_req) :
310 crypto_aead_decrypt(&areq->cra_u.aead_req),
2c3f8b16 311 &ctx->wait);
400c40cf
SM
312 }
313
d887c52d
SM
314
315free:
7d2c3f54 316 af_alg_free_resources(areq);
400c40cf
SM
317
318 return err ? err : outlen;
319}
320
d887c52d
SM
321static int aead_recvmsg(struct socket *sock, struct msghdr *msg,
322 size_t ignored, int flags)
83094e5e 323{
d887c52d
SM
324 struct sock *sk = sock->sk;
325 int ret = 0;
326
327 lock_sock(sk);
328 while (msg_data_left(msg)) {
329 int err = _aead_recvmsg(sock, msg, ignored, flags);
330
331 /*
332 * This error covers -EIOCBQUEUED which implies that we can
333 * only handle one AIO request. If the caller wants to have
334 * multiple AIO requests in parallel, he must make multiple
335 * separate AIO calls.
5703c826
SM
336 *
337 * Also return the error if no data has been processed so far.
d887c52d
SM
338 */
339 if (err <= 0) {
5703c826 340 if (err == -EIOCBQUEUED || err == -EBADMSG || !ret)
d887c52d
SM
341 ret = err;
342 goto out;
343 }
344
345 ret += err;
346 }
347
348out:
2d97591e 349 af_alg_wmem_wakeup(sk);
d887c52d
SM
350 release_sock(sk);
351 return ret;
83094e5e
TS
352}
353
400c40cf
SM
354static struct proto_ops algif_aead_ops = {
355 .family = PF_ALG,
356
357 .connect = sock_no_connect,
358 .socketpair = sock_no_socketpair,
359 .getname = sock_no_getname,
360 .ioctl = sock_no_ioctl,
361 .listen = sock_no_listen,
362 .shutdown = sock_no_shutdown,
363 .getsockopt = sock_no_getsockopt,
364 .mmap = sock_no_mmap,
365 .bind = sock_no_bind,
366 .accept = sock_no_accept,
367 .setsockopt = sock_no_setsockopt,
368
369 .release = af_alg_release,
370 .sendmsg = aead_sendmsg,
2d97591e 371 .sendpage = af_alg_sendpage,
400c40cf 372 .recvmsg = aead_recvmsg,
2d97591e 373 .poll = af_alg_poll,
400c40cf
SM
374};
375
2a2a251f
SM
376static int aead_check_key(struct socket *sock)
377{
378 int err = 0;
379 struct sock *psk;
380 struct alg_sock *pask;
381 struct aead_tfm *tfm;
382 struct sock *sk = sock->sk;
383 struct alg_sock *ask = alg_sk(sk);
384
385 lock_sock(sk);
386 if (ask->refcnt)
387 goto unlock_child;
388
389 psk = ask->parent;
390 pask = alg_sk(ask->parent);
391 tfm = pask->private;
392
393 err = -ENOKEY;
394 lock_sock_nested(psk, SINGLE_DEPTH_NESTING);
395 if (!tfm->has_key)
396 goto unlock;
397
398 if (!pask->refcnt++)
399 sock_hold(psk);
400
401 ask->refcnt = 1;
402 sock_put(psk);
403
404 err = 0;
405
406unlock:
407 release_sock(psk);
408unlock_child:
409 release_sock(sk);
410
411 return err;
412}
413
414static int aead_sendmsg_nokey(struct socket *sock, struct msghdr *msg,
415 size_t size)
416{
417 int err;
418
419 err = aead_check_key(sock);
420 if (err)
421 return err;
422
423 return aead_sendmsg(sock, msg, size);
424}
425
426static ssize_t aead_sendpage_nokey(struct socket *sock, struct page *page,
427 int offset, size_t size, int flags)
428{
429 int err;
430
431 err = aead_check_key(sock);
432 if (err)
433 return err;
434
2d97591e 435 return af_alg_sendpage(sock, page, offset, size, flags);
2a2a251f
SM
436}
437
438static int aead_recvmsg_nokey(struct socket *sock, struct msghdr *msg,
439 size_t ignored, int flags)
440{
441 int err;
442
443 err = aead_check_key(sock);
444 if (err)
445 return err;
446
447 return aead_recvmsg(sock, msg, ignored, flags);
448}
449
450static struct proto_ops algif_aead_ops_nokey = {
451 .family = PF_ALG,
452
453 .connect = sock_no_connect,
454 .socketpair = sock_no_socketpair,
455 .getname = sock_no_getname,
456 .ioctl = sock_no_ioctl,
457 .listen = sock_no_listen,
458 .shutdown = sock_no_shutdown,
459 .getsockopt = sock_no_getsockopt,
460 .mmap = sock_no_mmap,
461 .bind = sock_no_bind,
462 .accept = sock_no_accept,
463 .setsockopt = sock_no_setsockopt,
464
465 .release = af_alg_release,
466 .sendmsg = aead_sendmsg_nokey,
467 .sendpage = aead_sendpage_nokey,
468 .recvmsg = aead_recvmsg_nokey,
2d97591e 469 .poll = af_alg_poll,
2a2a251f
SM
470};
471
400c40cf
SM
472static void *aead_bind(const char *name, u32 type, u32 mask)
473{
2a2a251f
SM
474 struct aead_tfm *tfm;
475 struct crypto_aead *aead;
72548b09 476 struct crypto_skcipher *null_tfm;
2a2a251f
SM
477
478 tfm = kzalloc(sizeof(*tfm), GFP_KERNEL);
479 if (!tfm)
480 return ERR_PTR(-ENOMEM);
481
482 aead = crypto_alloc_aead(name, type, mask);
483 if (IS_ERR(aead)) {
484 kfree(tfm);
485 return ERR_CAST(aead);
486 }
487
72548b09
SM
488 null_tfm = crypto_get_default_null_skcipher2();
489 if (IS_ERR(null_tfm)) {
490 crypto_free_aead(aead);
491 kfree(tfm);
492 return ERR_CAST(null_tfm);
493 }
494
2a2a251f 495 tfm->aead = aead;
72548b09 496 tfm->null_tfm = null_tfm;
2a2a251f
SM
497
498 return tfm;
400c40cf
SM
499}
500
501static void aead_release(void *private)
502{
2a2a251f
SM
503 struct aead_tfm *tfm = private;
504
505 crypto_free_aead(tfm->aead);
b32a7dc8 506 crypto_put_default_null_skcipher2();
2a2a251f 507 kfree(tfm);
400c40cf
SM
508}
509
510static int aead_setauthsize(void *private, unsigned int authsize)
511{
2a2a251f
SM
512 struct aead_tfm *tfm = private;
513
514 return crypto_aead_setauthsize(tfm->aead, authsize);
400c40cf
SM
515}
516
517static int aead_setkey(void *private, const u8 *key, unsigned int keylen)
518{
2a2a251f
SM
519 struct aead_tfm *tfm = private;
520 int err;
521
522 err = crypto_aead_setkey(tfm->aead, key, keylen);
523 tfm->has_key = !err;
524
525 return err;
400c40cf
SM
526}
527
528static void aead_sock_destruct(struct sock *sk)
529{
530 struct alg_sock *ask = alg_sk(sk);
2d97591e 531 struct af_alg_ctx *ctx = ask->private;
d887c52d
SM
532 struct sock *psk = ask->parent;
533 struct alg_sock *pask = alg_sk(psk);
534 struct aead_tfm *aeadc = pask->private;
535 struct crypto_aead *tfm = aeadc->aead;
536 unsigned int ivlen = crypto_aead_ivsize(tfm);
400c40cf 537
2d97591e 538 af_alg_pull_tsgl(sk, ctx->used, NULL, 0);
400c40cf
SM
539 sock_kzfree_s(sk, ctx->iv, ivlen);
540 sock_kfree_s(sk, ctx, ctx->len);
541 af_alg_release_parent(sk);
542}
543
2a2a251f 544static int aead_accept_parent_nokey(void *private, struct sock *sk)
400c40cf 545{
2d97591e 546 struct af_alg_ctx *ctx;
400c40cf 547 struct alg_sock *ask = alg_sk(sk);
2a2a251f
SM
548 struct aead_tfm *tfm = private;
549 struct crypto_aead *aead = tfm->aead;
d887c52d 550 unsigned int len = sizeof(*ctx);
2a2a251f 551 unsigned int ivlen = crypto_aead_ivsize(aead);
400c40cf
SM
552
553 ctx = sock_kmalloc(sk, len, GFP_KERNEL);
554 if (!ctx)
555 return -ENOMEM;
556 memset(ctx, 0, len);
557
558 ctx->iv = sock_kmalloc(sk, ivlen, GFP_KERNEL);
559 if (!ctx->iv) {
560 sock_kfree_s(sk, ctx, len);
561 return -ENOMEM;
562 }
563 memset(ctx->iv, 0, ivlen);
564
d887c52d 565 INIT_LIST_HEAD(&ctx->tsgl_list);
400c40cf
SM
566 ctx->len = len;
567 ctx->used = 0;
d887c52d 568 ctx->rcvused = 0;
400c40cf
SM
569 ctx->more = 0;
570 ctx->merge = 0;
571 ctx->enc = 0;
400c40cf 572 ctx->aead_assoclen = 0;
2c3f8b16 573 crypto_init_wait(&ctx->wait);
400c40cf
SM
574
575 ask->private = ctx;
576
400c40cf
SM
577 sk->sk_destruct = aead_sock_destruct;
578
579 return 0;
580}
581
2a2a251f
SM
582static int aead_accept_parent(void *private, struct sock *sk)
583{
584 struct aead_tfm *tfm = private;
585
586 if (!tfm->has_key)
587 return -ENOKEY;
588
589 return aead_accept_parent_nokey(private, sk);
590}
591
400c40cf
SM
592static const struct af_alg_type algif_type_aead = {
593 .bind = aead_bind,
594 .release = aead_release,
595 .setkey = aead_setkey,
596 .setauthsize = aead_setauthsize,
597 .accept = aead_accept_parent,
2a2a251f 598 .accept_nokey = aead_accept_parent_nokey,
400c40cf 599 .ops = &algif_aead_ops,
2a2a251f 600 .ops_nokey = &algif_aead_ops_nokey,
400c40cf
SM
601 .name = "aead",
602 .owner = THIS_MODULE
603};
604
605static int __init algif_aead_init(void)
606{
607 return af_alg_register_type(&algif_type_aead);
608}
609
610static void __exit algif_aead_exit(void)
611{
612 int err = af_alg_unregister_type(&algif_type_aead);
613 BUG_ON(err);
614}
615
616module_init(algif_aead_init);
617module_exit(algif_aead_exit);
618MODULE_LICENSE("GPL");
619MODULE_AUTHOR("Stephan Mueller <smueller@chronox.de>");
620MODULE_DESCRIPTION("AEAD kernel crypto API user space interface");