1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * algif_aead: User-space interface for AEAD algorithms
5 * Copyright (C) 2014, Stephan Mueller <smueller@chronox.de>
7 * This file provides the user-space API for AEAD ciphers.
9 * The following concept of the memory management is used:
11 * The kernel maintains two SGLs, the TX SGL and the RX SGL. The TX SGL is
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.
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.
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
26 #include <crypto/internal/aead.h>
27 #include <crypto/scatterwalk.h>
28 #include <crypto/if_alg.h>
29 #include <crypto/skcipher.h>
30 #include <linux/init.h>
31 #include <linux/list.h>
32 #include <linux/kernel.h>
34 #include <linux/module.h>
35 #include <linux/net.h>
38 static inline bool aead_sufficient_data(struct sock *sk)
40 struct alg_sock *ask = alg_sk(sk);
41 struct sock *psk = ask->parent;
42 struct alg_sock *pask = alg_sk(psk);
43 struct af_alg_ctx *ctx = ask->private;
44 struct crypto_aead *tfm = pask->private;
45 unsigned int as = crypto_aead_authsize(tfm);
48 * The minimum amount of memory needed for an AEAD cipher is
49 * the AAD and in case of decryption the tag.
51 return ctx->used >= ctx->aead_assoclen + (ctx->enc ? 0 : as);
54 static int aead_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
56 struct sock *sk = sock->sk;
57 struct alg_sock *ask = alg_sk(sk);
58 struct sock *psk = ask->parent;
59 struct alg_sock *pask = alg_sk(psk);
60 struct crypto_aead *tfm = pask->private;
61 unsigned int ivsize = crypto_aead_ivsize(tfm);
63 return af_alg_sendmsg(sock, msg, size, ivsize);
66 static int _aead_recvmsg(struct socket *sock, struct msghdr *msg,
67 size_t ignored, int flags)
69 struct sock *sk = sock->sk;
70 struct alg_sock *ask = alg_sk(sk);
71 struct sock *psk = ask->parent;
72 struct alg_sock *pask = alg_sk(psk);
73 struct af_alg_ctx *ctx = ask->private;
74 struct crypto_aead *tfm = pask->private;
75 unsigned int i, as = crypto_aead_authsize(tfm);
76 struct af_alg_async_req *areq;
77 struct af_alg_tsgl *tsgl, *tmp;
78 struct scatterlist *rsgl_src, *tsgl_src = NULL;
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 */
85 if (!ctx->init || ctx->more) {
86 err = af_alg_wait_for_data(sk, flags, 0);
92 * Data length provided by caller via sendmsg that has not yet been
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
106 if (!aead_sufficient_data(sk))
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.
123 * The cipher operation input data is reduced by the associated data
124 * length as this data is processed separately later on.
126 used -= ctx->aead_assoclen;
128 /* Allocate cipher request for current operation. */
129 areq = af_alg_alloc_areq(sk, sizeof(struct af_alg_async_req) +
130 crypto_aead_reqsize(tfm));
132 return PTR_ERR(areq);
134 /* convert iovecs of output buffers into RX SGL */
135 err = af_alg_get_rsgl(sk, msg, flags, areq, outlen, &usedpages);
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
146 if (usedpages < outlen) {
147 size_t less = outlen - usedpages;
157 processed = used + ctx->aead_assoclen;
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;
162 if (!(process_sg->length) || !sg_page(process_sg))
164 tsgl_src = process_sg;
170 if (processed && !tsgl_src) {
176 * Copy of AAD from source to destination
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
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.
188 /* Use the RX SGL as source (and destination) for crypto op. */
189 rsgl_src = areq->first_rsgl.sgl.sgt.sgl;
193 * Encryption operation - The in-place cipher operation is
194 * achieved by the following operation:
200 * RX SGL: AAD || PT || Tag
202 memcpy_sglist(areq->first_rsgl.sgl.sgt.sgl, tsgl_src,
204 af_alg_pull_tsgl(sk, processed, NULL, 0);
207 * Decryption operation - To achieve an in-place cipher
208 * operation, the following SGL structure is used:
210 * TX SGL: AAD || CT || Tag
212 * | copy | | Create SGL link.
214 * RX SGL: AAD || CT ----+
217 /* Copy AAD || CT to RX SGL buffer for in-place operation. */
218 memcpy_sglist(areq->first_rsgl.sgl.sgt.sgl, tsgl_src, outlen);
220 /* Create TX SGL for tag and chain it to RX SGL. */
221 areq->tsgl_entries = af_alg_count_tsgl(sk, processed,
223 if (!areq->tsgl_entries)
224 areq->tsgl_entries = 1;
225 areq->tsgl = sock_kmalloc(sk, array_size(sizeof(*areq->tsgl),
232 sg_init_table(areq->tsgl, areq->tsgl_entries);
234 /* Release TX SGL, except for tag data and reassign tag data. */
235 af_alg_pull_tsgl(sk, processed, areq->tsgl, processed - as);
237 /* chain the areq TX SGL holding the tag with RX SGL */
240 struct af_alg_sgl *sgl_prev = &areq->last_rsgl->sgl;
241 struct scatterlist *sg = sgl_prev->sgt.sgl;
243 sg_unmark_end(sg + sgl_prev->sgt.nents - 1);
244 sg_chain(sg, sgl_prev->sgt.nents + 1, areq->tsgl);
246 /* no RX SGL present (e.g. authentication only) */
247 rsgl_src = areq->tsgl;
250 /* Initialize the crypto operation */
251 aead_request_set_crypt(&areq->cra_u.aead_req, rsgl_src,
252 areq->first_rsgl.sgl.sgt.sgl, used, ctx->iv);
253 aead_request_set_ad(&areq->cra_u.aead_req, ctx->aead_assoclen);
254 aead_request_set_tfm(&areq->cra_u.aead_req, tfm);
256 if (msg->msg_iocb && !is_sync_kiocb(msg->msg_iocb)) {
259 areq->iocb = msg->msg_iocb;
261 /* Remember output size that will be generated. */
262 areq->outlen = outlen;
264 aead_request_set_callback(&areq->cra_u.aead_req,
265 CRYPTO_TFM_REQ_MAY_SLEEP,
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);
270 /* AIO operation in progress */
271 if (err == -EINPROGRESS)
276 /* Synchronous operation */
277 aead_request_set_callback(&areq->cra_u.aead_req,
278 CRYPTO_TFM_REQ_MAY_SLEEP |
279 CRYPTO_TFM_REQ_MAY_BACKLOG,
280 crypto_req_done, &ctx->wait);
281 err = crypto_wait_req(ctx->enc ?
282 crypto_aead_encrypt(&areq->cra_u.aead_req) :
283 crypto_aead_decrypt(&areq->cra_u.aead_req),
289 af_alg_free_resources(areq);
291 return err ? err : outlen;
294 static int aead_recvmsg(struct socket *sock, struct msghdr *msg,
295 size_t ignored, int flags)
297 struct sock *sk = sock->sk;
301 while (msg_data_left(msg)) {
302 int err = _aead_recvmsg(sock, msg, ignored, flags);
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.
310 * Also return the error if no data has been processed so far.
313 if (err == -EIOCBQUEUED || err == -EBADMSG || !ret)
322 af_alg_wmem_wakeup(sk);
327 static struct proto_ops algif_aead_ops = {
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,
336 .mmap = sock_no_mmap,
337 .bind = sock_no_bind,
338 .accept = sock_no_accept,
340 .release = af_alg_release,
341 .sendmsg = aead_sendmsg,
342 .recvmsg = aead_recvmsg,
346 static int aead_check_key(struct socket *sock)
350 struct alg_sock *pask;
351 struct crypto_aead *tfm;
352 struct sock *sk = sock->sk;
353 struct alg_sock *ask = alg_sk(sk);
356 if (!atomic_read(&ask->nokey_refcnt))
360 pask = alg_sk(ask->parent);
364 lock_sock_nested(psk, SINGLE_DEPTH_NESTING);
365 if (crypto_aead_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
368 atomic_dec(&pask->nokey_refcnt);
369 atomic_set(&ask->nokey_refcnt, 0);
381 static int aead_sendmsg_nokey(struct socket *sock, struct msghdr *msg,
386 err = aead_check_key(sock);
390 return aead_sendmsg(sock, msg, size);
393 static int aead_recvmsg_nokey(struct socket *sock, struct msghdr *msg,
394 size_t ignored, int flags)
398 err = aead_check_key(sock);
402 return aead_recvmsg(sock, msg, ignored, flags);
405 static struct proto_ops algif_aead_ops_nokey = {
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,
414 .mmap = sock_no_mmap,
415 .bind = sock_no_bind,
416 .accept = sock_no_accept,
418 .release = af_alg_release,
419 .sendmsg = aead_sendmsg_nokey,
420 .recvmsg = aead_recvmsg_nokey,
424 static void *aead_bind(const char *name, u32 type, u32 mask)
426 return crypto_alloc_aead(name, type, mask);
429 static void aead_release(void *private)
431 crypto_free_aead(private);
434 static int aead_setauthsize(void *private, unsigned int authsize)
436 return crypto_aead_setauthsize(private, authsize);
439 static int aead_setkey(void *private, const u8 *key, unsigned int keylen)
441 return crypto_aead_setkey(private, key, keylen);
444 static void aead_sock_destruct(struct sock *sk)
446 struct alg_sock *ask = alg_sk(sk);
447 struct af_alg_ctx *ctx = ask->private;
448 struct sock *psk = ask->parent;
449 struct alg_sock *pask = alg_sk(psk);
450 struct crypto_aead *tfm = pask->private;
451 unsigned int ivlen = crypto_aead_ivsize(tfm);
453 af_alg_pull_tsgl(sk, ctx->used, NULL, 0);
454 sock_kzfree_s(sk, ctx->iv, ivlen);
455 sock_kfree_s(sk, ctx, ctx->len);
456 af_alg_release_parent(sk);
459 static int aead_accept_parent_nokey(void *private, struct sock *sk)
461 struct af_alg_ctx *ctx;
462 struct alg_sock *ask = alg_sk(sk);
463 struct crypto_aead *tfm = private;
464 unsigned int len = sizeof(*ctx);
465 unsigned int ivlen = crypto_aead_ivsize(tfm);
467 ctx = sock_kmalloc(sk, len, GFP_KERNEL);
472 ctx->iv = sock_kmalloc(sk, ivlen, GFP_KERNEL);
474 sock_kfree_s(sk, ctx, len);
477 memset(ctx->iv, 0, ivlen);
479 INIT_LIST_HEAD(&ctx->tsgl_list);
481 crypto_init_wait(&ctx->wait);
485 sk->sk_destruct = aead_sock_destruct;
490 static int aead_accept_parent(void *private, struct sock *sk)
492 struct crypto_aead *tfm = private;
494 if (crypto_aead_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
497 return aead_accept_parent_nokey(private, sk);
500 static const struct af_alg_type algif_type_aead = {
502 .release = aead_release,
503 .setkey = aead_setkey,
504 .setauthsize = aead_setauthsize,
505 .accept = aead_accept_parent,
506 .accept_nokey = aead_accept_parent_nokey,
507 .ops = &algif_aead_ops,
508 .ops_nokey = &algif_aead_ops_nokey,
513 static int __init algif_aead_init(void)
515 return af_alg_register_type(&algif_type_aead);
518 static void __exit algif_aead_exit(void)
520 int err = af_alg_unregister_type(&algif_type_aead);
524 module_init(algif_aead_init);
525 module_exit(algif_aead_exit);
526 MODULE_LICENSE("GPL");
527 MODULE_AUTHOR("Stephan Mueller <smueller@chronox.de>");
528 MODULE_DESCRIPTION("AEAD kernel crypto API user space interface");