12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2400c40cfSStephan Mueller /*
3400c40cfSStephan Mueller * algif_aead: User-space interface for AEAD algorithms
4400c40cfSStephan Mueller *
5400c40cfSStephan Mueller * Copyright (C) 2014, Stephan Mueller <smueller@chronox.de>
6400c40cfSStephan Mueller *
7400c40cfSStephan Mueller * This file provides the user-space API for AEAD ciphers.
8400c40cfSStephan Mueller *
9d887c52dSStephan Mueller * The following concept of the memory management is used:
10d887c52dSStephan Mueller *
11d887c52dSStephan Mueller * The kernel maintains two SGLs, the TX SGL and the RX SGL. The TX SGL is
12dc97391eSDavid Howells * filled by user space with the data submitted via sendmsg (maybe with
13dc97391eSDavid Howells * MSG_SPLICE_PAGES). Filling up the TX SGL does not cause a crypto operation
14dc97391eSDavid Howells * -- the data will only be tracked by the kernel. Upon receipt of one recvmsg
15dc97391eSDavid Howells * call, the caller must provide a buffer which is tracked with the RX SGL.
16d887c52dSStephan Mueller *
17d887c52dSStephan Mueller * During the processing of the recvmsg operation, the cipher request is
18d887c52dSStephan Mueller * allocated and prepared. As part of the recvmsg operation, the processed
19d887c52dSStephan Mueller * TX buffers are extracted from the TX SGL into a separate SGL.
20d887c52dSStephan Mueller *
21d887c52dSStephan Mueller * After the completion of the crypto operation, the RX SGL and the cipher
22d887c52dSStephan Mueller * request is released. The extracted TX SGL parts are released together with
23d887c52dSStephan Mueller * the RX SGL release.
24400c40cfSStephan Mueller */
25400c40cfSStephan Mueller
2683094e5eSTadeusz Struk #include <crypto/internal/aead.h>
27400c40cfSStephan Mueller #include <crypto/scatterwalk.h>
28400c40cfSStephan Mueller #include <crypto/if_alg.h>
29400c40cfSStephan Mueller #include <linux/init.h>
30400c40cfSStephan Mueller #include <linux/list.h>
31400c40cfSStephan Mueller #include <linux/kernel.h>
32400c40cfSStephan Mueller #include <linux/mm.h>
33400c40cfSStephan Mueller #include <linux/module.h>
34400c40cfSStephan Mueller #include <linux/net.h>
35400c40cfSStephan Mueller #include <net/sock.h>
36400c40cfSStephan Mueller
aead_sufficient_data(struct sock * sk)37d887c52dSStephan Mueller static inline bool aead_sufficient_data(struct sock *sk)
38d887c52dSStephan Mueller {
39d887c52dSStephan Mueller struct alg_sock *ask = alg_sk(sk);
40d887c52dSStephan Mueller struct sock *psk = ask->parent;
41d887c52dSStephan Mueller struct alg_sock *pask = alg_sk(psk);
422d97591eSStephan Mueller struct af_alg_ctx *ctx = ask->private;
43f2804d0eSEric Biggers struct crypto_aead *tfm = pask->private;
44d887c52dSStephan Mueller unsigned int as = crypto_aead_authsize(tfm);
45400c40cfSStephan Mueller
460c1e16cdSStephan Mueller /*
470c1e16cdSStephan Mueller * The minimum amount of memory needed for an AEAD cipher is
480c1e16cdSStephan Mueller * the AAD and in case of decryption the tag.
490c1e16cdSStephan Mueller */
500c1e16cdSStephan Mueller return ctx->used >= ctx->aead_assoclen + (ctx->enc ? 0 : as);
51400c40cfSStephan Mueller }
52400c40cfSStephan Mueller
aead_sendmsg(struct socket * sock,struct msghdr * msg,size_t size)53eccd02f3SLinus Torvalds static int aead_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
54400c40cfSStephan Mueller {
55400c40cfSStephan Mueller struct sock *sk = sock->sk;
56400c40cfSStephan Mueller struct alg_sock *ask = alg_sk(sk);
57d887c52dSStephan Mueller struct sock *psk = ask->parent;
58d887c52dSStephan Mueller struct alg_sock *pask = alg_sk(psk);
59f2804d0eSEric Biggers struct crypto_aead *tfm = pask->private;
60d887c52dSStephan Mueller unsigned int ivsize = crypto_aead_ivsize(tfm);
61400c40cfSStephan Mueller
622d97591eSStephan Mueller return af_alg_sendmsg(sock, msg, size, ivsize);
6383094e5eSTadeusz Struk }
6483094e5eSTadeusz Struk
_aead_recvmsg(struct socket * sock,struct msghdr * msg,size_t ignored,int flags)65d887c52dSStephan Mueller static int _aead_recvmsg(struct socket *sock, struct msghdr *msg,
66d887c52dSStephan Mueller size_t ignored, int flags)
67400c40cfSStephan Mueller {
68400c40cfSStephan Mueller struct sock *sk = sock->sk;
69400c40cfSStephan Mueller struct alg_sock *ask = alg_sk(sk);
70d887c52dSStephan Mueller struct sock *psk = ask->parent;
71d887c52dSStephan Mueller struct alg_sock *pask = alg_sk(psk);
722d97591eSStephan Mueller struct af_alg_ctx *ctx = ask->private;
73f2804d0eSEric Biggers struct crypto_aead *tfm = pask->private;
74a664bf3dSHerbert Xu unsigned int as = crypto_aead_authsize(tfm);
75*5aa58c3aSDouya Le unsigned int ivsize = crypto_aead_ivsize(tfm);
762d97591eSStephan Mueller struct af_alg_async_req *areq;
778e1fa89aSStephan Mueller struct scatterlist *rsgl_src, *tsgl_src = NULL;
78*5aa58c3aSDouya Le void *iv;
79d887c52dSStephan Mueller int err = 0;
80d887c52dSStephan Mueller size_t used = 0; /* [in] TX bufs to be en/decrypted */
81d887c52dSStephan Mueller size_t outlen = 0; /* [out] RX bufs produced by kernel */
82d887c52dSStephan Mueller size_t usedpages = 0; /* [in] RX bufs to be used from user */
83d887c52dSStephan Mueller size_t processed = 0; /* [in] TX bufs to be consumed */
84400c40cfSStephan Mueller
85f3c802a1SHerbert Xu if (!ctx->init || ctx->more) {
86f3c802a1SHerbert Xu err = af_alg_wait_for_data(sk, flags, 0);
8711edb555SStephan Mueller if (err)
8811edb555SStephan Mueller return err;
8911edb555SStephan Mueller }
9011edb555SStephan Mueller
91400c40cfSStephan Mueller /*
92bf63e250SDavid Howells * Data length provided by caller via sendmsg that has not yet been
93bf63e250SDavid Howells * processed.
94400c40cfSStephan Mueller */
95400c40cfSStephan Mueller used = ctx->used;
96400c40cfSStephan Mueller
97400c40cfSStephan Mueller /*
98bf63e250SDavid Howells * Make sure sufficient data is present -- note, the same check is also
99bf63e250SDavid Howells * present in sendmsg. The checks in sendmsg shall provide an
100bf63e250SDavid Howells * information to the data sender that something is wrong, but they are
101bf63e250SDavid Howells * irrelevant to maintain the kernel integrity. We need this check
102bf63e250SDavid Howells * here too in case user space decides to not honor the error message
103bf63e250SDavid Howells * in sendmsg and still call recvmsg. This check here protects the
104bf63e250SDavid Howells * kernel integrity.
105400c40cfSStephan Mueller */
106d887c52dSStephan Mueller if (!aead_sufficient_data(sk))
107d887c52dSStephan Mueller return -EINVAL;
108400c40cfSStephan Mueller
1090c1e16cdSStephan Mueller /*
1100c1e16cdSStephan Mueller * Calculate the minimum output buffer size holding the result of the
1110c1e16cdSStephan Mueller * cipher operation. When encrypting data, the receiving buffer is
1120c1e16cdSStephan Mueller * larger by the tag length compared to the input buffer as the
1130c1e16cdSStephan Mueller * encryption operation generates the tag. For decryption, the input
1140c1e16cdSStephan Mueller * buffer provides the tag which is consumed resulting in only the
1150c1e16cdSStephan Mueller * plaintext without a buffer for the tag returned to the caller.
1160c1e16cdSStephan Mueller */
1170c1e16cdSStephan Mueller if (ctx->enc)
1180c1e16cdSStephan Mueller outlen = used + as;
1190c1e16cdSStephan Mueller else
1200c1e16cdSStephan Mueller outlen = used - as;
12119fa7752SHerbert Xu
122400c40cfSStephan Mueller /*
123400c40cfSStephan Mueller * The cipher operation input data is reduced by the associated data
124400c40cfSStephan Mueller * length as this data is processed separately later on.
125400c40cfSStephan Mueller */
1260c1e16cdSStephan Mueller used -= ctx->aead_assoclen;
127400c40cfSStephan Mueller
128d887c52dSStephan Mueller /* Allocate cipher request for current operation. */
1292d97591eSStephan Mueller areq = af_alg_alloc_areq(sk, sizeof(struct af_alg_async_req) +
130*5aa58c3aSDouya Le crypto_aead_reqsize(tfm) + ivsize);
1312d97591eSStephan Mueller if (IS_ERR(areq))
1322d97591eSStephan Mueller return PTR_ERR(areq);
133400c40cfSStephan Mueller
134*5aa58c3aSDouya Le iv = (u8 *)aead_request_ctx(&areq->cra_u.aead_req) +
135*5aa58c3aSDouya Le crypto_aead_reqsize(tfm);
136*5aa58c3aSDouya Le memcpy(iv, ctx->iv, ivsize);
137*5aa58c3aSDouya Le
138d887c52dSStephan Mueller /* convert iovecs of output buffers into RX SGL */
1392d97591eSStephan Mueller err = af_alg_get_rsgl(sk, msg, flags, areq, outlen, &usedpages);
140d887c52dSStephan Mueller if (err)
141d887c52dSStephan Mueller goto free;
142400c40cfSStephan Mueller
143d887c52dSStephan Mueller /*
144d887c52dSStephan Mueller * Ensure output buffer is sufficiently large. If the caller provides
145d887c52dSStephan Mueller * less buffer space, only use the relative required input size. This
146d887c52dSStephan Mueller * allows AIO operation where the caller sent all data to be processed
147d887c52dSStephan Mueller * and the AIO operation performs the operation on the different chunks
148d887c52dSStephan Mueller * of the input data.
149d887c52dSStephan Mueller */
1500c1e16cdSStephan Mueller if (usedpages < outlen) {
151d887c52dSStephan Mueller size_t less = outlen - usedpages;
152d887c52dSStephan Mueller
1533d14bd48SHerbert Xu if (used < less + (ctx->enc ? 0 : as)) {
1540c1e16cdSStephan Mueller err = -EINVAL;
155d887c52dSStephan Mueller goto free;
156d887c52dSStephan Mueller }
157d887c52dSStephan Mueller used -= less;
158d887c52dSStephan Mueller outlen -= less;
1590c1e16cdSStephan Mueller }
160400c40cfSStephan Mueller
161a664bf3dSHerbert Xu /*
162a664bf3dSHerbert Xu * Create a per request TX SGL for this request which tracks the
163a664bf3dSHerbert Xu * SG entries from the global TX SGL.
164a664bf3dSHerbert Xu */
165d887c52dSStephan Mueller processed = used + ctx->aead_assoclen;
166a664bf3dSHerbert Xu areq->tsgl_entries = af_alg_count_tsgl(sk, processed);
167d887c52dSStephan Mueller if (!areq->tsgl_entries)
168d887c52dSStephan Mueller areq->tsgl_entries = 1;
16976e43e37SKees Cook areq->tsgl = sock_kmalloc(sk, array_size(sizeof(*areq->tsgl),
17076e43e37SKees Cook areq->tsgl_entries),
171d887c52dSStephan Mueller GFP_KERNEL);
172d887c52dSStephan Mueller if (!areq->tsgl) {
173d887c52dSStephan Mueller err = -ENOMEM;
174d887c52dSStephan Mueller goto free;
175d887c52dSStephan Mueller }
176d887c52dSStephan Mueller sg_init_table(areq->tsgl, areq->tsgl_entries);
177a664bf3dSHerbert Xu af_alg_pull_tsgl(sk, processed, areq->tsgl);
178a664bf3dSHerbert Xu tsgl_src = areq->tsgl;
17972548b09SStephan Mueller
180a664bf3dSHerbert Xu /*
181a664bf3dSHerbert Xu * Copy of AAD from source to destination
182a664bf3dSHerbert Xu *
183a664bf3dSHerbert Xu * The AAD is copied to the destination buffer without change. Even
184a664bf3dSHerbert Xu * when user space uses an in-place cipher operation, the kernel
185a664bf3dSHerbert Xu * will copy the data as it does not see whether such in-place operation
186a664bf3dSHerbert Xu * is initiated.
187a664bf3dSHerbert Xu */
18872548b09SStephan Mueller
189a664bf3dSHerbert Xu /* Use the RX SGL as source (and destination) for crypto op. */
190a664bf3dSHerbert Xu rsgl_src = areq->first_rsgl.sgl.sgt.sgl;
19172548b09SStephan Mueller
192a664bf3dSHerbert Xu memcpy_sglist(rsgl_src, tsgl_src, ctx->aead_assoclen);
193400c40cfSStephan Mueller
194d887c52dSStephan Mueller /* Initialize the crypto operation */
195a664bf3dSHerbert Xu aead_request_set_crypt(&areq->cra_u.aead_req, tsgl_src,
196*5aa58c3aSDouya Le areq->first_rsgl.sgl.sgt.sgl, used, iv);
1972d97591eSStephan Mueller aead_request_set_ad(&areq->cra_u.aead_req, ctx->aead_assoclen);
1982d97591eSStephan Mueller aead_request_set_tfm(&areq->cra_u.aead_req, tfm);
199d887c52dSStephan Mueller
200d887c52dSStephan Mueller if (msg->msg_iocb && !is_sync_kiocb(msg->msg_iocb)) {
201d887c52dSStephan Mueller /* AIO operation */
2027d2c3f54SStephan Mueller sock_hold(sk);
203d887c52dSStephan Mueller areq->iocb = msg->msg_iocb;
204d53c5135SStephan Mueller
205d53c5135SStephan Mueller /* Remember output size that will be generated. */
206d53c5135SStephan Mueller areq->outlen = outlen;
207d53c5135SStephan Mueller
2082d97591eSStephan Mueller aead_request_set_callback(&areq->cra_u.aead_req,
209cbdad1f2SHerbert Xu CRYPTO_TFM_REQ_MAY_SLEEP,
2102d97591eSStephan Mueller af_alg_async_cb, areq);
2112d97591eSStephan Mueller err = ctx->enc ? crypto_aead_encrypt(&areq->cra_u.aead_req) :
2122d97591eSStephan Mueller crypto_aead_decrypt(&areq->cra_u.aead_req);
2137d2c3f54SStephan Mueller
2147d2c3f54SStephan Mueller /* AIO operation in progress */
215cbdad1f2SHerbert Xu if (err == -EINPROGRESS)
2167d2c3f54SStephan Mueller return -EIOCBQUEUED;
2177d2c3f54SStephan Mueller
2187d2c3f54SStephan Mueller sock_put(sk);
219d887c52dSStephan Mueller } else {
220d887c52dSStephan Mueller /* Synchronous operation */
2212d97591eSStephan Mueller aead_request_set_callback(&areq->cra_u.aead_req,
222cbdad1f2SHerbert Xu CRYPTO_TFM_REQ_MAY_SLEEP |
223d887c52dSStephan Mueller CRYPTO_TFM_REQ_MAY_BACKLOG,
2242c3f8b16SGilad Ben-Yossef crypto_req_done, &ctx->wait);
2252c3f8b16SGilad Ben-Yossef err = crypto_wait_req(ctx->enc ?
2262d97591eSStephan Mueller crypto_aead_encrypt(&areq->cra_u.aead_req) :
2272d97591eSStephan Mueller crypto_aead_decrypt(&areq->cra_u.aead_req),
2282c3f8b16SGilad Ben-Yossef &ctx->wait);
229400c40cfSStephan Mueller }
230400c40cfSStephan Mueller
231d887c52dSStephan Mueller
232d887c52dSStephan Mueller free:
2337d2c3f54SStephan Mueller af_alg_free_resources(areq);
234400c40cfSStephan Mueller
235400c40cfSStephan Mueller return err ? err : outlen;
236400c40cfSStephan Mueller }
237400c40cfSStephan Mueller
aead_recvmsg(struct socket * sock,struct msghdr * msg,size_t ignored,int flags)238d887c52dSStephan Mueller static int aead_recvmsg(struct socket *sock, struct msghdr *msg,
239d887c52dSStephan Mueller size_t ignored, int flags)
24083094e5eSTadeusz Struk {
241d887c52dSStephan Mueller struct sock *sk = sock->sk;
242d887c52dSStephan Mueller int ret = 0;
243d887c52dSStephan Mueller
244d887c52dSStephan Mueller lock_sock(sk);
245d887c52dSStephan Mueller while (msg_data_left(msg)) {
246d887c52dSStephan Mueller int err = _aead_recvmsg(sock, msg, ignored, flags);
247d887c52dSStephan Mueller
248d887c52dSStephan Mueller /*
249d887c52dSStephan Mueller * This error covers -EIOCBQUEUED which implies that we can
250d887c52dSStephan Mueller * only handle one AIO request. If the caller wants to have
251d887c52dSStephan Mueller * multiple AIO requests in parallel, he must make multiple
252d887c52dSStephan Mueller * separate AIO calls.
2535703c826SStephan Mueller *
2545703c826SStephan Mueller * Also return the error if no data has been processed so far.
255d887c52dSStephan Mueller */
256d887c52dSStephan Mueller if (err <= 0) {
2575703c826SStephan Mueller if (err == -EIOCBQUEUED || err == -EBADMSG || !ret)
258d887c52dSStephan Mueller ret = err;
259d887c52dSStephan Mueller goto out;
260d887c52dSStephan Mueller }
261d887c52dSStephan Mueller
262d887c52dSStephan Mueller ret += err;
263d887c52dSStephan Mueller }
264d887c52dSStephan Mueller
265d887c52dSStephan Mueller out:
2662d97591eSStephan Mueller af_alg_wmem_wakeup(sk);
267d887c52dSStephan Mueller release_sock(sk);
268d887c52dSStephan Mueller return ret;
26983094e5eSTadeusz Struk }
27083094e5eSTadeusz Struk
271400c40cfSStephan Mueller static struct proto_ops algif_aead_ops = {
272400c40cfSStephan Mueller .family = PF_ALG,
273400c40cfSStephan Mueller
274400c40cfSStephan Mueller .connect = sock_no_connect,
275400c40cfSStephan Mueller .socketpair = sock_no_socketpair,
276400c40cfSStephan Mueller .getname = sock_no_getname,
277400c40cfSStephan Mueller .ioctl = sock_no_ioctl,
278400c40cfSStephan Mueller .listen = sock_no_listen,
279400c40cfSStephan Mueller .shutdown = sock_no_shutdown,
280400c40cfSStephan Mueller .mmap = sock_no_mmap,
281400c40cfSStephan Mueller .bind = sock_no_bind,
282400c40cfSStephan Mueller .accept = sock_no_accept,
283400c40cfSStephan Mueller
284400c40cfSStephan Mueller .release = af_alg_release,
285400c40cfSStephan Mueller .sendmsg = aead_sendmsg,
286400c40cfSStephan Mueller .recvmsg = aead_recvmsg,
287a11e1d43SLinus Torvalds .poll = af_alg_poll,
288400c40cfSStephan Mueller };
289400c40cfSStephan Mueller
aead_check_key(struct socket * sock)2902a2a251fSStephan Mueller static int aead_check_key(struct socket *sock)
2912a2a251fSStephan Mueller {
2922a2a251fSStephan Mueller int err = 0;
2932a2a251fSStephan Mueller struct sock *psk;
2942a2a251fSStephan Mueller struct alg_sock *pask;
295f2804d0eSEric Biggers struct crypto_aead *tfm;
2962a2a251fSStephan Mueller struct sock *sk = sock->sk;
2972a2a251fSStephan Mueller struct alg_sock *ask = alg_sk(sk);
2982a2a251fSStephan Mueller
2992a2a251fSStephan Mueller lock_sock(sk);
30034c86f4cSHerbert Xu if (!atomic_read(&ask->nokey_refcnt))
3012a2a251fSStephan Mueller goto unlock_child;
3022a2a251fSStephan Mueller
3032a2a251fSStephan Mueller psk = ask->parent;
3042a2a251fSStephan Mueller pask = alg_sk(ask->parent);
3052a2a251fSStephan Mueller tfm = pask->private;
3062a2a251fSStephan Mueller
3072a2a251fSStephan Mueller err = -ENOKEY;
3082a2a251fSStephan Mueller lock_sock_nested(psk, SINGLE_DEPTH_NESTING);
309f2804d0eSEric Biggers if (crypto_aead_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
3102a2a251fSStephan Mueller goto unlock;
3112a2a251fSStephan Mueller
31234c86f4cSHerbert Xu atomic_dec(&pask->nokey_refcnt);
31334c86f4cSHerbert Xu atomic_set(&ask->nokey_refcnt, 0);
3142a2a251fSStephan Mueller
3152a2a251fSStephan Mueller err = 0;
3162a2a251fSStephan Mueller
3172a2a251fSStephan Mueller unlock:
3182a2a251fSStephan Mueller release_sock(psk);
3192a2a251fSStephan Mueller unlock_child:
3202a2a251fSStephan Mueller release_sock(sk);
3212a2a251fSStephan Mueller
3222a2a251fSStephan Mueller return err;
3232a2a251fSStephan Mueller }
3242a2a251fSStephan Mueller
aead_sendmsg_nokey(struct socket * sock,struct msghdr * msg,size_t size)3252a2a251fSStephan Mueller static int aead_sendmsg_nokey(struct socket *sock, struct msghdr *msg,
3262a2a251fSStephan Mueller size_t size)
3272a2a251fSStephan Mueller {
3282a2a251fSStephan Mueller int err;
3292a2a251fSStephan Mueller
3302a2a251fSStephan Mueller err = aead_check_key(sock);
3312a2a251fSStephan Mueller if (err)
3322a2a251fSStephan Mueller return err;
3332a2a251fSStephan Mueller
3342a2a251fSStephan Mueller return aead_sendmsg(sock, msg, size);
3352a2a251fSStephan Mueller }
3362a2a251fSStephan Mueller
aead_recvmsg_nokey(struct socket * sock,struct msghdr * msg,size_t ignored,int flags)3372a2a251fSStephan Mueller static int aead_recvmsg_nokey(struct socket *sock, struct msghdr *msg,
3382a2a251fSStephan Mueller size_t ignored, int flags)
3392a2a251fSStephan Mueller {
3402a2a251fSStephan Mueller int err;
3412a2a251fSStephan Mueller
3422a2a251fSStephan Mueller err = aead_check_key(sock);
3432a2a251fSStephan Mueller if (err)
3442a2a251fSStephan Mueller return err;
3452a2a251fSStephan Mueller
3462a2a251fSStephan Mueller return aead_recvmsg(sock, msg, ignored, flags);
3472a2a251fSStephan Mueller }
3482a2a251fSStephan Mueller
3492a2a251fSStephan Mueller static struct proto_ops algif_aead_ops_nokey = {
3502a2a251fSStephan Mueller .family = PF_ALG,
3512a2a251fSStephan Mueller
3522a2a251fSStephan Mueller .connect = sock_no_connect,
3532a2a251fSStephan Mueller .socketpair = sock_no_socketpair,
3542a2a251fSStephan Mueller .getname = sock_no_getname,
3552a2a251fSStephan Mueller .ioctl = sock_no_ioctl,
3562a2a251fSStephan Mueller .listen = sock_no_listen,
3572a2a251fSStephan Mueller .shutdown = sock_no_shutdown,
3582a2a251fSStephan Mueller .mmap = sock_no_mmap,
3592a2a251fSStephan Mueller .bind = sock_no_bind,
3602a2a251fSStephan Mueller .accept = sock_no_accept,
3612a2a251fSStephan Mueller
3622a2a251fSStephan Mueller .release = af_alg_release,
3632a2a251fSStephan Mueller .sendmsg = aead_sendmsg_nokey,
3642a2a251fSStephan Mueller .recvmsg = aead_recvmsg_nokey,
365a11e1d43SLinus Torvalds .poll = af_alg_poll,
3662a2a251fSStephan Mueller };
3672a2a251fSStephan Mueller
aead_bind(const char * name,u32 type,u32 mask)368400c40cfSStephan Mueller static void *aead_bind(const char *name, u32 type, u32 mask)
369400c40cfSStephan Mueller {
370f2804d0eSEric Biggers return crypto_alloc_aead(name, type, mask);
371400c40cfSStephan Mueller }
372400c40cfSStephan Mueller
aead_release(void * private)373400c40cfSStephan Mueller static void aead_release(void *private)
374400c40cfSStephan Mueller {
375f2804d0eSEric Biggers crypto_free_aead(private);
376400c40cfSStephan Mueller }
377400c40cfSStephan Mueller
aead_setauthsize(void * private,unsigned int authsize)378400c40cfSStephan Mueller static int aead_setauthsize(void *private, unsigned int authsize)
379400c40cfSStephan Mueller {
380f2804d0eSEric Biggers return crypto_aead_setauthsize(private, authsize);
381400c40cfSStephan Mueller }
382400c40cfSStephan Mueller
aead_setkey(void * private,const u8 * key,unsigned int keylen)383400c40cfSStephan Mueller static int aead_setkey(void *private, const u8 *key, unsigned int keylen)
384400c40cfSStephan Mueller {
385f2804d0eSEric Biggers return crypto_aead_setkey(private, key, keylen);
386400c40cfSStephan Mueller }
387400c40cfSStephan Mueller
aead_sock_destruct(struct sock * sk)388400c40cfSStephan Mueller static void aead_sock_destruct(struct sock *sk)
389400c40cfSStephan Mueller {
390400c40cfSStephan Mueller struct alg_sock *ask = alg_sk(sk);
3912d97591eSStephan Mueller struct af_alg_ctx *ctx = ask->private;
392d887c52dSStephan Mueller struct sock *psk = ask->parent;
393d887c52dSStephan Mueller struct alg_sock *pask = alg_sk(psk);
394f2804d0eSEric Biggers struct crypto_aead *tfm = pask->private;
395d887c52dSStephan Mueller unsigned int ivlen = crypto_aead_ivsize(tfm);
396400c40cfSStephan Mueller
397a664bf3dSHerbert Xu af_alg_pull_tsgl(sk, ctx->used, NULL);
398400c40cfSStephan Mueller sock_kzfree_s(sk, ctx->iv, ivlen);
399400c40cfSStephan Mueller sock_kfree_s(sk, ctx, ctx->len);
400400c40cfSStephan Mueller af_alg_release_parent(sk);
401400c40cfSStephan Mueller }
402400c40cfSStephan Mueller
aead_accept_parent_nokey(void * private,struct sock * sk)4032a2a251fSStephan Mueller static int aead_accept_parent_nokey(void *private, struct sock *sk)
404400c40cfSStephan Mueller {
4052d97591eSStephan Mueller struct af_alg_ctx *ctx;
406400c40cfSStephan Mueller struct alg_sock *ask = alg_sk(sk);
407f2804d0eSEric Biggers struct crypto_aead *tfm = private;
408d887c52dSStephan Mueller unsigned int len = sizeof(*ctx);
409f2804d0eSEric Biggers unsigned int ivlen = crypto_aead_ivsize(tfm);
410400c40cfSStephan Mueller
411400c40cfSStephan Mueller ctx = sock_kmalloc(sk, len, GFP_KERNEL);
412400c40cfSStephan Mueller if (!ctx)
413400c40cfSStephan Mueller return -ENOMEM;
414400c40cfSStephan Mueller memset(ctx, 0, len);
415400c40cfSStephan Mueller
416400c40cfSStephan Mueller ctx->iv = sock_kmalloc(sk, ivlen, GFP_KERNEL);
417400c40cfSStephan Mueller if (!ctx->iv) {
418400c40cfSStephan Mueller sock_kfree_s(sk, ctx, len);
419400c40cfSStephan Mueller return -ENOMEM;
420400c40cfSStephan Mueller }
421400c40cfSStephan Mueller memset(ctx->iv, 0, ivlen);
422400c40cfSStephan Mueller
423d887c52dSStephan Mueller INIT_LIST_HEAD(&ctx->tsgl_list);
424400c40cfSStephan Mueller ctx->len = len;
4252c3f8b16SGilad Ben-Yossef crypto_init_wait(&ctx->wait);
426400c40cfSStephan Mueller
427400c40cfSStephan Mueller ask->private = ctx;
428400c40cfSStephan Mueller
429400c40cfSStephan Mueller sk->sk_destruct = aead_sock_destruct;
430400c40cfSStephan Mueller
431400c40cfSStephan Mueller return 0;
432400c40cfSStephan Mueller }
433400c40cfSStephan Mueller
aead_accept_parent(void * private,struct sock * sk)4342a2a251fSStephan Mueller static int aead_accept_parent(void *private, struct sock *sk)
4352a2a251fSStephan Mueller {
436f2804d0eSEric Biggers struct crypto_aead *tfm = private;
4372a2a251fSStephan Mueller
438f2804d0eSEric Biggers if (crypto_aead_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
4392a2a251fSStephan Mueller return -ENOKEY;
4402a2a251fSStephan Mueller
4412a2a251fSStephan Mueller return aead_accept_parent_nokey(private, sk);
4422a2a251fSStephan Mueller }
4432a2a251fSStephan Mueller
444400c40cfSStephan Mueller static const struct af_alg_type algif_type_aead = {
445400c40cfSStephan Mueller .bind = aead_bind,
446400c40cfSStephan Mueller .release = aead_release,
447400c40cfSStephan Mueller .setkey = aead_setkey,
448400c40cfSStephan Mueller .setauthsize = aead_setauthsize,
449400c40cfSStephan Mueller .accept = aead_accept_parent,
4502a2a251fSStephan Mueller .accept_nokey = aead_accept_parent_nokey,
451400c40cfSStephan Mueller .ops = &algif_aead_ops,
4522a2a251fSStephan Mueller .ops_nokey = &algif_aead_ops_nokey,
453400c40cfSStephan Mueller .name = "aead",
454400c40cfSStephan Mueller .owner = THIS_MODULE
455400c40cfSStephan Mueller };
456400c40cfSStephan Mueller
algif_aead_init(void)457400c40cfSStephan Mueller static int __init algif_aead_init(void)
458400c40cfSStephan Mueller {
459400c40cfSStephan Mueller return af_alg_register_type(&algif_type_aead);
460400c40cfSStephan Mueller }
461400c40cfSStephan Mueller
algif_aead_exit(void)462400c40cfSStephan Mueller static void __exit algif_aead_exit(void)
463400c40cfSStephan Mueller {
464400c40cfSStephan Mueller int err = af_alg_unregister_type(&algif_type_aead);
465400c40cfSStephan Mueller BUG_ON(err);
466400c40cfSStephan Mueller }
467400c40cfSStephan Mueller
468400c40cfSStephan Mueller module_init(algif_aead_init);
469400c40cfSStephan Mueller module_exit(algif_aead_exit);
470400c40cfSStephan Mueller MODULE_LICENSE("GPL");
471400c40cfSStephan Mueller MODULE_AUTHOR("Stephan Mueller <smueller@chronox.de>");
472400c40cfSStephan Mueller MODULE_DESCRIPTION("AEAD kernel crypto API user space interface");
473