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