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