xref: /openbmc/linux/crypto/algif_aead.c (revision dc97391e)
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
12*dc97391eSDavid Howells  * filled by user space with the data submitted via sendmsg (maybe with
13*dc97391eSDavid Howells  * MSG_SPLICE_PAGES).  Filling up the TX SGL does not cause a crypto operation
14*dc97391eSDavid Howells  * -- the data will only be tracked by the kernel. Upon receipt of one recvmsg
15*dc97391eSDavid 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>
2972548b09SStephan Mueller #include <crypto/skcipher.h>
3072548b09SStephan Mueller #include <crypto/null.h>
31400c40cfSStephan Mueller #include <linux/init.h>
32400c40cfSStephan Mueller #include <linux/list.h>
33400c40cfSStephan Mueller #include <linux/kernel.h>
34400c40cfSStephan Mueller #include <linux/mm.h>
35400c40cfSStephan Mueller #include <linux/module.h>
36400c40cfSStephan Mueller #include <linux/net.h>
37400c40cfSStephan Mueller #include <net/sock.h>
38400c40cfSStephan Mueller 
392a2a251fSStephan Mueller struct aead_tfm {
402a2a251fSStephan Mueller 	struct crypto_aead *aead;
418d605398SKees Cook 	struct crypto_sync_skcipher *null_tfm;
422a2a251fSStephan Mueller };
432a2a251fSStephan Mueller 
aead_sufficient_data(struct sock * sk)44d887c52dSStephan Mueller static inline bool aead_sufficient_data(struct sock *sk)
45d887c52dSStephan Mueller {
46d887c52dSStephan Mueller 	struct alg_sock *ask = alg_sk(sk);
47d887c52dSStephan Mueller 	struct sock *psk = ask->parent;
48d887c52dSStephan Mueller 	struct alg_sock *pask = alg_sk(psk);
492d97591eSStephan Mueller 	struct af_alg_ctx *ctx = ask->private;
50d887c52dSStephan Mueller 	struct aead_tfm *aeadc = pask->private;
51d887c52dSStephan Mueller 	struct crypto_aead *tfm = aeadc->aead;
52d887c52dSStephan Mueller 	unsigned int as = crypto_aead_authsize(tfm);
53400c40cfSStephan Mueller 
540c1e16cdSStephan Mueller 	/*
550c1e16cdSStephan Mueller 	 * The minimum amount of memory needed for an AEAD cipher is
560c1e16cdSStephan Mueller 	 * the AAD and in case of decryption the tag.
570c1e16cdSStephan Mueller 	 */
580c1e16cdSStephan Mueller 	return ctx->used >= ctx->aead_assoclen + (ctx->enc ? 0 : as);
59400c40cfSStephan Mueller }
60400c40cfSStephan Mueller 
aead_sendmsg(struct socket * sock,struct msghdr * msg,size_t size)61eccd02f3SLinus Torvalds static int aead_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
62400c40cfSStephan Mueller {
63400c40cfSStephan Mueller 	struct sock *sk = sock->sk;
64400c40cfSStephan Mueller 	struct alg_sock *ask = alg_sk(sk);
65d887c52dSStephan Mueller 	struct sock *psk = ask->parent;
66d887c52dSStephan Mueller 	struct alg_sock *pask = alg_sk(psk);
67d887c52dSStephan Mueller 	struct aead_tfm *aeadc = pask->private;
68d887c52dSStephan Mueller 	struct crypto_aead *tfm = aeadc->aead;
69d887c52dSStephan Mueller 	unsigned int ivsize = crypto_aead_ivsize(tfm);
70400c40cfSStephan Mueller 
712d97591eSStephan Mueller 	return af_alg_sendmsg(sock, msg, size, ivsize);
7283094e5eSTadeusz Struk }
7383094e5eSTadeusz Struk 
crypto_aead_copy_sgl(struct crypto_sync_skcipher * null_tfm,struct scatterlist * src,struct scatterlist * dst,unsigned int len)748d605398SKees Cook static int crypto_aead_copy_sgl(struct crypto_sync_skcipher *null_tfm,
7572548b09SStephan Mueller 				struct scatterlist *src,
7672548b09SStephan Mueller 				struct scatterlist *dst, unsigned int len)
7772548b09SStephan Mueller {
788d605398SKees Cook 	SYNC_SKCIPHER_REQUEST_ON_STACK(skreq, null_tfm);
7972548b09SStephan Mueller 
808d605398SKees Cook 	skcipher_request_set_sync_tfm(skreq, null_tfm);
81cbdad1f2SHerbert Xu 	skcipher_request_set_callback(skreq, CRYPTO_TFM_REQ_MAY_SLEEP,
8272548b09SStephan Mueller 				      NULL, NULL);
8372548b09SStephan Mueller 	skcipher_request_set_crypt(skreq, src, dst, len, NULL);
8472548b09SStephan Mueller 
8572548b09SStephan Mueller 	return crypto_skcipher_encrypt(skreq);
8672548b09SStephan Mueller }
8772548b09SStephan Mueller 
_aead_recvmsg(struct socket * sock,struct msghdr * msg,size_t ignored,int flags)88d887c52dSStephan Mueller static int _aead_recvmsg(struct socket *sock, struct msghdr *msg,
89d887c52dSStephan Mueller 			 size_t ignored, int flags)
90400c40cfSStephan Mueller {
91400c40cfSStephan Mueller 	struct sock *sk = sock->sk;
92400c40cfSStephan Mueller 	struct alg_sock *ask = alg_sk(sk);
93d887c52dSStephan Mueller 	struct sock *psk = ask->parent;
94d887c52dSStephan Mueller 	struct alg_sock *pask = alg_sk(psk);
952d97591eSStephan Mueller 	struct af_alg_ctx *ctx = ask->private;
96d887c52dSStephan Mueller 	struct aead_tfm *aeadc = pask->private;
97d887c52dSStephan Mueller 	struct crypto_aead *tfm = aeadc->aead;
988d605398SKees Cook 	struct crypto_sync_skcipher *null_tfm = aeadc->null_tfm;
998e1fa89aSStephan Mueller 	unsigned int i, as = crypto_aead_authsize(tfm);
1002d97591eSStephan Mueller 	struct af_alg_async_req *areq;
1018e1fa89aSStephan Mueller 	struct af_alg_tsgl *tsgl, *tmp;
1028e1fa89aSStephan Mueller 	struct scatterlist *rsgl_src, *tsgl_src = NULL;
103d887c52dSStephan Mueller 	int err = 0;
104d887c52dSStephan Mueller 	size_t used = 0;		/* [in]  TX bufs to be en/decrypted */
105d887c52dSStephan Mueller 	size_t outlen = 0;		/* [out] RX bufs produced by kernel */
106d887c52dSStephan Mueller 	size_t usedpages = 0;		/* [in]  RX bufs to be used from user */
107d887c52dSStephan Mueller 	size_t processed = 0;		/* [in]  TX bufs to be consumed */
108400c40cfSStephan Mueller 
109f3c802a1SHerbert Xu 	if (!ctx->init || ctx->more) {
110f3c802a1SHerbert Xu 		err = af_alg_wait_for_data(sk, flags, 0);
11111edb555SStephan Mueller 		if (err)
11211edb555SStephan Mueller 			return err;
11311edb555SStephan Mueller 	}
11411edb555SStephan Mueller 
115400c40cfSStephan Mueller 	/*
116bf63e250SDavid Howells 	 * Data length provided by caller via sendmsg that has not yet been
117bf63e250SDavid Howells 	 * processed.
118400c40cfSStephan Mueller 	 */
119400c40cfSStephan Mueller 	used = ctx->used;
120400c40cfSStephan Mueller 
121400c40cfSStephan Mueller 	/*
122bf63e250SDavid Howells 	 * Make sure sufficient data is present -- note, the same check is also
123bf63e250SDavid Howells 	 * present in sendmsg. The checks in sendmsg shall provide an
124bf63e250SDavid Howells 	 * information to the data sender that something is wrong, but they are
125bf63e250SDavid Howells 	 * irrelevant to maintain the kernel integrity.  We need this check
126bf63e250SDavid Howells 	 * here too in case user space decides to not honor the error message
127bf63e250SDavid Howells 	 * in sendmsg and still call recvmsg. This check here protects the
128bf63e250SDavid Howells 	 * kernel integrity.
129400c40cfSStephan Mueller 	 */
130d887c52dSStephan Mueller 	if (!aead_sufficient_data(sk))
131d887c52dSStephan Mueller 		return -EINVAL;
132400c40cfSStephan Mueller 
1330c1e16cdSStephan Mueller 	/*
1340c1e16cdSStephan Mueller 	 * Calculate the minimum output buffer size holding the result of the
1350c1e16cdSStephan Mueller 	 * cipher operation. When encrypting data, the receiving buffer is
1360c1e16cdSStephan Mueller 	 * larger by the tag length compared to the input buffer as the
1370c1e16cdSStephan Mueller 	 * encryption operation generates the tag. For decryption, the input
1380c1e16cdSStephan Mueller 	 * buffer provides the tag which is consumed resulting in only the
1390c1e16cdSStephan Mueller 	 * plaintext without a buffer for the tag returned to the caller.
1400c1e16cdSStephan Mueller 	 */
1410c1e16cdSStephan Mueller 	if (ctx->enc)
1420c1e16cdSStephan Mueller 		outlen = used + as;
1430c1e16cdSStephan Mueller 	else
1440c1e16cdSStephan Mueller 		outlen = used - as;
14519fa7752SHerbert Xu 
146400c40cfSStephan Mueller 	/*
147400c40cfSStephan Mueller 	 * The cipher operation input data is reduced by the associated data
148400c40cfSStephan Mueller 	 * length as this data is processed separately later on.
149400c40cfSStephan Mueller 	 */
1500c1e16cdSStephan Mueller 	used -= ctx->aead_assoclen;
151400c40cfSStephan Mueller 
152d887c52dSStephan Mueller 	/* Allocate cipher request for current operation. */
1532d97591eSStephan Mueller 	areq = af_alg_alloc_areq(sk, sizeof(struct af_alg_async_req) +
1542d97591eSStephan Mueller 				     crypto_aead_reqsize(tfm));
1552d97591eSStephan Mueller 	if (IS_ERR(areq))
1562d97591eSStephan Mueller 		return PTR_ERR(areq);
157400c40cfSStephan Mueller 
158d887c52dSStephan Mueller 	/* convert iovecs of output buffers into RX SGL */
1592d97591eSStephan Mueller 	err = af_alg_get_rsgl(sk, msg, flags, areq, outlen, &usedpages);
160d887c52dSStephan Mueller 	if (err)
161d887c52dSStephan Mueller 		goto free;
162400c40cfSStephan Mueller 
163d887c52dSStephan Mueller 	/*
164d887c52dSStephan Mueller 	 * Ensure output buffer is sufficiently large. If the caller provides
165d887c52dSStephan Mueller 	 * less buffer space, only use the relative required input size. This
166d887c52dSStephan Mueller 	 * allows AIO operation where the caller sent all data to be processed
167d887c52dSStephan Mueller 	 * and the AIO operation performs the operation on the different chunks
168d887c52dSStephan Mueller 	 * of the input data.
169d887c52dSStephan Mueller 	 */
1700c1e16cdSStephan Mueller 	if (usedpages < outlen) {
171d887c52dSStephan Mueller 		size_t less = outlen - usedpages;
172d887c52dSStephan Mueller 
173d887c52dSStephan Mueller 		if (used < less) {
1740c1e16cdSStephan Mueller 			err = -EINVAL;
175d887c52dSStephan Mueller 			goto free;
176d887c52dSStephan Mueller 		}
177d887c52dSStephan Mueller 		used -= less;
178d887c52dSStephan Mueller 		outlen -= less;
1790c1e16cdSStephan Mueller 	}
180400c40cfSStephan Mueller 
181d887c52dSStephan Mueller 	processed = used + ctx->aead_assoclen;
1828e1fa89aSStephan Mueller 	list_for_each_entry_safe(tsgl, tmp, &ctx->tsgl_list, list) {
1838e1fa89aSStephan Mueller 		for (i = 0; i < tsgl->cur; i++) {
1848e1fa89aSStephan Mueller 			struct scatterlist *process_sg = tsgl->sg + i;
1858e1fa89aSStephan Mueller 
1868e1fa89aSStephan Mueller 			if (!(process_sg->length) || !sg_page(process_sg))
1878e1fa89aSStephan Mueller 				continue;
1888e1fa89aSStephan Mueller 			tsgl_src = process_sg;
1898e1fa89aSStephan Mueller 			break;
1908e1fa89aSStephan Mueller 		}
1918e1fa89aSStephan Mueller 		if (tsgl_src)
1928e1fa89aSStephan Mueller 			break;
1938e1fa89aSStephan Mueller 	}
1948e1fa89aSStephan Mueller 	if (processed && !tsgl_src) {
1958e1fa89aSStephan Mueller 		err = -EFAULT;
1968e1fa89aSStephan Mueller 		goto free;
1978e1fa89aSStephan Mueller 	}
19872548b09SStephan Mueller 
19972548b09SStephan Mueller 	/*
20072548b09SStephan Mueller 	 * Copy of AAD from source to destination
20172548b09SStephan Mueller 	 *
20272548b09SStephan Mueller 	 * The AAD is copied to the destination buffer without change. Even
20372548b09SStephan Mueller 	 * when user space uses an in-place cipher operation, the kernel
20472548b09SStephan Mueller 	 * will copy the data as it does not see whether such in-place operation
20572548b09SStephan Mueller 	 * is initiated.
20672548b09SStephan Mueller 	 *
20772548b09SStephan Mueller 	 * To ensure efficiency, the following implementation ensure that the
20872548b09SStephan Mueller 	 * ciphers are invoked to perform a crypto operation in-place. This
20972548b09SStephan Mueller 	 * is achieved by memory management specified as follows.
21072548b09SStephan Mueller 	 */
21172548b09SStephan Mueller 
21272548b09SStephan Mueller 	/* Use the RX SGL as source (and destination) for crypto op. */
213c1abe6f5SDavid Howells 	rsgl_src = areq->first_rsgl.sgl.sgt.sgl;
21472548b09SStephan Mueller 
21572548b09SStephan Mueller 	if (ctx->enc) {
21672548b09SStephan Mueller 		/*
21772548b09SStephan Mueller 		 * Encryption operation - The in-place cipher operation is
21872548b09SStephan Mueller 		 * achieved by the following operation:
21972548b09SStephan Mueller 		 *
22075d11e75SStephan Mueller 		 * TX SGL: AAD || PT
22172548b09SStephan Mueller 		 *	    |	   |
22272548b09SStephan Mueller 		 *	    | copy |
22372548b09SStephan Mueller 		 *	    v	   v
22475d11e75SStephan Mueller 		 * RX SGL: AAD || PT || Tag
22572548b09SStephan Mueller 		 */
2268e1fa89aSStephan Mueller 		err = crypto_aead_copy_sgl(null_tfm, tsgl_src,
227c1abe6f5SDavid Howells 					   areq->first_rsgl.sgl.sgt.sgl,
228c1abe6f5SDavid Howells 					   processed);
22972548b09SStephan Mueller 		if (err)
23072548b09SStephan Mueller 			goto free;
2312d97591eSStephan Mueller 		af_alg_pull_tsgl(sk, processed, NULL, 0);
23272548b09SStephan Mueller 	} else {
23372548b09SStephan Mueller 		/*
23472548b09SStephan Mueller 		 * Decryption operation - To achieve an in-place cipher
23572548b09SStephan Mueller 		 * operation, the following  SGL structure is used:
23672548b09SStephan Mueller 		 *
23772548b09SStephan Mueller 		 * TX SGL: AAD || CT || Tag
23872548b09SStephan Mueller 		 *	    |	   |	 ^
23972548b09SStephan Mueller 		 *	    | copy |	 | Create SGL link.
24072548b09SStephan Mueller 		 *	    v	   v	 |
24172548b09SStephan Mueller 		 * RX SGL: AAD || CT ----+
24272548b09SStephan Mueller 		 */
24372548b09SStephan Mueller 
24472548b09SStephan Mueller 		 /* Copy AAD || CT to RX SGL buffer for in-place operation. */
2458e1fa89aSStephan Mueller 		err = crypto_aead_copy_sgl(null_tfm, tsgl_src,
246c1abe6f5SDavid Howells 					   areq->first_rsgl.sgl.sgt.sgl,
247c1abe6f5SDavid Howells 					   outlen);
24872548b09SStephan Mueller 		if (err)
24972548b09SStephan Mueller 			goto free;
25072548b09SStephan Mueller 
25172548b09SStephan Mueller 		/* Create TX SGL for tag and chain it to RX SGL. */
2522d97591eSStephan Mueller 		areq->tsgl_entries = af_alg_count_tsgl(sk, processed,
25372548b09SStephan Mueller 						       processed - as);
254d887c52dSStephan Mueller 		if (!areq->tsgl_entries)
255d887c52dSStephan Mueller 			areq->tsgl_entries = 1;
25676e43e37SKees Cook 		areq->tsgl = sock_kmalloc(sk, array_size(sizeof(*areq->tsgl),
25776e43e37SKees Cook 							 areq->tsgl_entries),
258d887c52dSStephan Mueller 					  GFP_KERNEL);
259d887c52dSStephan Mueller 		if (!areq->tsgl) {
260d887c52dSStephan Mueller 			err = -ENOMEM;
261d887c52dSStephan Mueller 			goto free;
262d887c52dSStephan Mueller 		}
263d887c52dSStephan Mueller 		sg_init_table(areq->tsgl, areq->tsgl_entries);
26472548b09SStephan Mueller 
26572548b09SStephan Mueller 		/* Release TX SGL, except for tag data and reassign tag data. */
2662d97591eSStephan Mueller 		af_alg_pull_tsgl(sk, processed, areq->tsgl, processed - as);
26772548b09SStephan Mueller 
26872548b09SStephan Mueller 		/* chain the areq TX SGL holding the tag with RX SGL */
2692d97591eSStephan Mueller 		if (usedpages) {
27072548b09SStephan Mueller 			/* RX SGL present */
2712d97591eSStephan Mueller 			struct af_alg_sgl *sgl_prev = &areq->last_rsgl->sgl;
272c1abe6f5SDavid Howells 			struct scatterlist *sg = sgl_prev->sgt.sgl;
27372548b09SStephan Mueller 
274c1abe6f5SDavid Howells 			sg_unmark_end(sg + sgl_prev->sgt.nents - 1);
275c1abe6f5SDavid Howells 			sg_chain(sg, sgl_prev->sgt.nents + 1, areq->tsgl);
27672548b09SStephan Mueller 		} else
27772548b09SStephan Mueller 			/* no RX SGL present (e.g. authentication only) */
2788e1fa89aSStephan Mueller 			rsgl_src = areq->tsgl;
27972548b09SStephan Mueller 	}
280400c40cfSStephan Mueller 
281d887c52dSStephan Mueller 	/* Initialize the crypto operation */
2828e1fa89aSStephan Mueller 	aead_request_set_crypt(&areq->cra_u.aead_req, rsgl_src,
283c1abe6f5SDavid Howells 			       areq->first_rsgl.sgl.sgt.sgl, used, ctx->iv);
2842d97591eSStephan Mueller 	aead_request_set_ad(&areq->cra_u.aead_req, ctx->aead_assoclen);
2852d97591eSStephan Mueller 	aead_request_set_tfm(&areq->cra_u.aead_req, tfm);
286d887c52dSStephan Mueller 
287d887c52dSStephan Mueller 	if (msg->msg_iocb && !is_sync_kiocb(msg->msg_iocb)) {
288d887c52dSStephan Mueller 		/* AIO operation */
2897d2c3f54SStephan Mueller 		sock_hold(sk);
290d887c52dSStephan Mueller 		areq->iocb = msg->msg_iocb;
291d53c5135SStephan Mueller 
292d53c5135SStephan Mueller 		/* Remember output size that will be generated. */
293d53c5135SStephan Mueller 		areq->outlen = outlen;
294d53c5135SStephan Mueller 
2952d97591eSStephan Mueller 		aead_request_set_callback(&areq->cra_u.aead_req,
296cbdad1f2SHerbert Xu 					  CRYPTO_TFM_REQ_MAY_SLEEP,
2972d97591eSStephan Mueller 					  af_alg_async_cb, areq);
2982d97591eSStephan Mueller 		err = ctx->enc ? crypto_aead_encrypt(&areq->cra_u.aead_req) :
2992d97591eSStephan Mueller 				 crypto_aead_decrypt(&areq->cra_u.aead_req);
3007d2c3f54SStephan Mueller 
3017d2c3f54SStephan Mueller 		/* AIO operation in progress */
302cbdad1f2SHerbert Xu 		if (err == -EINPROGRESS)
3037d2c3f54SStephan Mueller 			return -EIOCBQUEUED;
3047d2c3f54SStephan Mueller 
3057d2c3f54SStephan Mueller 		sock_put(sk);
306d887c52dSStephan Mueller 	} else {
307d887c52dSStephan Mueller 		/* Synchronous operation */
3082d97591eSStephan Mueller 		aead_request_set_callback(&areq->cra_u.aead_req,
309cbdad1f2SHerbert Xu 					  CRYPTO_TFM_REQ_MAY_SLEEP |
310d887c52dSStephan Mueller 					  CRYPTO_TFM_REQ_MAY_BACKLOG,
3112c3f8b16SGilad Ben-Yossef 					  crypto_req_done, &ctx->wait);
3122c3f8b16SGilad Ben-Yossef 		err = crypto_wait_req(ctx->enc ?
3132d97591eSStephan Mueller 				crypto_aead_encrypt(&areq->cra_u.aead_req) :
3142d97591eSStephan Mueller 				crypto_aead_decrypt(&areq->cra_u.aead_req),
3152c3f8b16SGilad Ben-Yossef 				&ctx->wait);
316400c40cfSStephan Mueller 	}
317400c40cfSStephan Mueller 
318d887c52dSStephan Mueller 
319d887c52dSStephan Mueller free:
3207d2c3f54SStephan Mueller 	af_alg_free_resources(areq);
321400c40cfSStephan Mueller 
322400c40cfSStephan Mueller 	return err ? err : outlen;
323400c40cfSStephan Mueller }
324400c40cfSStephan Mueller 
aead_recvmsg(struct socket * sock,struct msghdr * msg,size_t ignored,int flags)325d887c52dSStephan Mueller static int aead_recvmsg(struct socket *sock, struct msghdr *msg,
326d887c52dSStephan Mueller 			size_t ignored, int flags)
32783094e5eSTadeusz Struk {
328d887c52dSStephan Mueller 	struct sock *sk = sock->sk;
329d887c52dSStephan Mueller 	int ret = 0;
330d887c52dSStephan Mueller 
331d887c52dSStephan Mueller 	lock_sock(sk);
332d887c52dSStephan Mueller 	while (msg_data_left(msg)) {
333d887c52dSStephan Mueller 		int err = _aead_recvmsg(sock, msg, ignored, flags);
334d887c52dSStephan Mueller 
335d887c52dSStephan Mueller 		/*
336d887c52dSStephan Mueller 		 * This error covers -EIOCBQUEUED which implies that we can
337d887c52dSStephan Mueller 		 * only handle one AIO request. If the caller wants to have
338d887c52dSStephan Mueller 		 * multiple AIO requests in parallel, he must make multiple
339d887c52dSStephan Mueller 		 * separate AIO calls.
3405703c826SStephan Mueller 		 *
3415703c826SStephan Mueller 		 * Also return the error if no data has been processed so far.
342d887c52dSStephan Mueller 		 */
343d887c52dSStephan Mueller 		if (err <= 0) {
3445703c826SStephan Mueller 			if (err == -EIOCBQUEUED || err == -EBADMSG || !ret)
345d887c52dSStephan Mueller 				ret = err;
346d887c52dSStephan Mueller 			goto out;
347d887c52dSStephan Mueller 		}
348d887c52dSStephan Mueller 
349d887c52dSStephan Mueller 		ret += err;
350d887c52dSStephan Mueller 	}
351d887c52dSStephan Mueller 
352d887c52dSStephan Mueller out:
3532d97591eSStephan Mueller 	af_alg_wmem_wakeup(sk);
354d887c52dSStephan Mueller 	release_sock(sk);
355d887c52dSStephan Mueller 	return ret;
35683094e5eSTadeusz Struk }
35783094e5eSTadeusz Struk 
358400c40cfSStephan Mueller static struct proto_ops algif_aead_ops = {
359400c40cfSStephan Mueller 	.family		=	PF_ALG,
360400c40cfSStephan Mueller 
361400c40cfSStephan Mueller 	.connect	=	sock_no_connect,
362400c40cfSStephan Mueller 	.socketpair	=	sock_no_socketpair,
363400c40cfSStephan Mueller 	.getname	=	sock_no_getname,
364400c40cfSStephan Mueller 	.ioctl		=	sock_no_ioctl,
365400c40cfSStephan Mueller 	.listen		=	sock_no_listen,
366400c40cfSStephan Mueller 	.shutdown	=	sock_no_shutdown,
367400c40cfSStephan Mueller 	.mmap		=	sock_no_mmap,
368400c40cfSStephan Mueller 	.bind		=	sock_no_bind,
369400c40cfSStephan Mueller 	.accept		=	sock_no_accept,
370400c40cfSStephan Mueller 
371400c40cfSStephan Mueller 	.release	=	af_alg_release,
372400c40cfSStephan Mueller 	.sendmsg	=	aead_sendmsg,
373400c40cfSStephan Mueller 	.recvmsg	=	aead_recvmsg,
374a11e1d43SLinus Torvalds 	.poll		=	af_alg_poll,
375400c40cfSStephan Mueller };
376400c40cfSStephan Mueller 
aead_check_key(struct socket * sock)3772a2a251fSStephan Mueller static int aead_check_key(struct socket *sock)
3782a2a251fSStephan Mueller {
3792a2a251fSStephan Mueller 	int err = 0;
3802a2a251fSStephan Mueller 	struct sock *psk;
3812a2a251fSStephan Mueller 	struct alg_sock *pask;
3822a2a251fSStephan Mueller 	struct aead_tfm *tfm;
3832a2a251fSStephan Mueller 	struct sock *sk = sock->sk;
3842a2a251fSStephan Mueller 	struct alg_sock *ask = alg_sk(sk);
3852a2a251fSStephan Mueller 
3862a2a251fSStephan Mueller 	lock_sock(sk);
38734c86f4cSHerbert Xu 	if (!atomic_read(&ask->nokey_refcnt))
3882a2a251fSStephan Mueller 		goto unlock_child;
3892a2a251fSStephan Mueller 
3902a2a251fSStephan Mueller 	psk = ask->parent;
3912a2a251fSStephan Mueller 	pask = alg_sk(ask->parent);
3922a2a251fSStephan Mueller 	tfm = pask->private;
3932a2a251fSStephan Mueller 
3942a2a251fSStephan Mueller 	err = -ENOKEY;
3952a2a251fSStephan Mueller 	lock_sock_nested(psk, SINGLE_DEPTH_NESTING);
396dc26c17fSEric Biggers 	if (crypto_aead_get_flags(tfm->aead) & CRYPTO_TFM_NEED_KEY)
3972a2a251fSStephan Mueller 		goto unlock;
3982a2a251fSStephan Mueller 
39934c86f4cSHerbert Xu 	atomic_dec(&pask->nokey_refcnt);
40034c86f4cSHerbert Xu 	atomic_set(&ask->nokey_refcnt, 0);
4012a2a251fSStephan Mueller 
4022a2a251fSStephan Mueller 	err = 0;
4032a2a251fSStephan Mueller 
4042a2a251fSStephan Mueller unlock:
4052a2a251fSStephan Mueller 	release_sock(psk);
4062a2a251fSStephan Mueller unlock_child:
4072a2a251fSStephan Mueller 	release_sock(sk);
4082a2a251fSStephan Mueller 
4092a2a251fSStephan Mueller 	return err;
4102a2a251fSStephan Mueller }
4112a2a251fSStephan Mueller 
aead_sendmsg_nokey(struct socket * sock,struct msghdr * msg,size_t size)4122a2a251fSStephan Mueller static int aead_sendmsg_nokey(struct socket *sock, struct msghdr *msg,
4132a2a251fSStephan Mueller 				  size_t size)
4142a2a251fSStephan Mueller {
4152a2a251fSStephan Mueller 	int err;
4162a2a251fSStephan Mueller 
4172a2a251fSStephan Mueller 	err = aead_check_key(sock);
4182a2a251fSStephan Mueller 	if (err)
4192a2a251fSStephan Mueller 		return err;
4202a2a251fSStephan Mueller 
4212a2a251fSStephan Mueller 	return aead_sendmsg(sock, msg, size);
4222a2a251fSStephan Mueller }
4232a2a251fSStephan Mueller 
aead_recvmsg_nokey(struct socket * sock,struct msghdr * msg,size_t ignored,int flags)4242a2a251fSStephan Mueller static int aead_recvmsg_nokey(struct socket *sock, struct msghdr *msg,
4252a2a251fSStephan Mueller 				  size_t ignored, int flags)
4262a2a251fSStephan Mueller {
4272a2a251fSStephan Mueller 	int err;
4282a2a251fSStephan Mueller 
4292a2a251fSStephan Mueller 	err = aead_check_key(sock);
4302a2a251fSStephan Mueller 	if (err)
4312a2a251fSStephan Mueller 		return err;
4322a2a251fSStephan Mueller 
4332a2a251fSStephan Mueller 	return aead_recvmsg(sock, msg, ignored, flags);
4342a2a251fSStephan Mueller }
4352a2a251fSStephan Mueller 
4362a2a251fSStephan Mueller static struct proto_ops algif_aead_ops_nokey = {
4372a2a251fSStephan Mueller 	.family		=	PF_ALG,
4382a2a251fSStephan Mueller 
4392a2a251fSStephan Mueller 	.connect	=	sock_no_connect,
4402a2a251fSStephan Mueller 	.socketpair	=	sock_no_socketpair,
4412a2a251fSStephan Mueller 	.getname	=	sock_no_getname,
4422a2a251fSStephan Mueller 	.ioctl		=	sock_no_ioctl,
4432a2a251fSStephan Mueller 	.listen		=	sock_no_listen,
4442a2a251fSStephan Mueller 	.shutdown	=	sock_no_shutdown,
4452a2a251fSStephan Mueller 	.mmap		=	sock_no_mmap,
4462a2a251fSStephan Mueller 	.bind		=	sock_no_bind,
4472a2a251fSStephan Mueller 	.accept		=	sock_no_accept,
4482a2a251fSStephan Mueller 
4492a2a251fSStephan Mueller 	.release	=	af_alg_release,
4502a2a251fSStephan Mueller 	.sendmsg	=	aead_sendmsg_nokey,
4512a2a251fSStephan Mueller 	.recvmsg	=	aead_recvmsg_nokey,
452a11e1d43SLinus Torvalds 	.poll		=	af_alg_poll,
4532a2a251fSStephan Mueller };
4542a2a251fSStephan Mueller 
aead_bind(const char * name,u32 type,u32 mask)455400c40cfSStephan Mueller static void *aead_bind(const char *name, u32 type, u32 mask)
456400c40cfSStephan Mueller {
4572a2a251fSStephan Mueller 	struct aead_tfm *tfm;
4582a2a251fSStephan Mueller 	struct crypto_aead *aead;
4598d605398SKees Cook 	struct crypto_sync_skcipher *null_tfm;
4602a2a251fSStephan Mueller 
4612a2a251fSStephan Mueller 	tfm = kzalloc(sizeof(*tfm), GFP_KERNEL);
4622a2a251fSStephan Mueller 	if (!tfm)
4632a2a251fSStephan Mueller 		return ERR_PTR(-ENOMEM);
4642a2a251fSStephan Mueller 
4652a2a251fSStephan Mueller 	aead = crypto_alloc_aead(name, type, mask);
4662a2a251fSStephan Mueller 	if (IS_ERR(aead)) {
4672a2a251fSStephan Mueller 		kfree(tfm);
4682a2a251fSStephan Mueller 		return ERR_CAST(aead);
4692a2a251fSStephan Mueller 	}
4702a2a251fSStephan Mueller 
4713a2d4fb5SEric Biggers 	null_tfm = crypto_get_default_null_skcipher();
47272548b09SStephan Mueller 	if (IS_ERR(null_tfm)) {
47372548b09SStephan Mueller 		crypto_free_aead(aead);
47472548b09SStephan Mueller 		kfree(tfm);
47572548b09SStephan Mueller 		return ERR_CAST(null_tfm);
47672548b09SStephan Mueller 	}
47772548b09SStephan Mueller 
4782a2a251fSStephan Mueller 	tfm->aead = aead;
47972548b09SStephan Mueller 	tfm->null_tfm = null_tfm;
4802a2a251fSStephan Mueller 
4812a2a251fSStephan Mueller 	return tfm;
482400c40cfSStephan Mueller }
483400c40cfSStephan Mueller 
aead_release(void * private)484400c40cfSStephan Mueller static void aead_release(void *private)
485400c40cfSStephan Mueller {
4862a2a251fSStephan Mueller 	struct aead_tfm *tfm = private;
4872a2a251fSStephan Mueller 
4882a2a251fSStephan Mueller 	crypto_free_aead(tfm->aead);
4893a2d4fb5SEric Biggers 	crypto_put_default_null_skcipher();
4902a2a251fSStephan Mueller 	kfree(tfm);
491400c40cfSStephan Mueller }
492400c40cfSStephan Mueller 
aead_setauthsize(void * private,unsigned int authsize)493400c40cfSStephan Mueller static int aead_setauthsize(void *private, unsigned int authsize)
494400c40cfSStephan Mueller {
4952a2a251fSStephan Mueller 	struct aead_tfm *tfm = private;
4962a2a251fSStephan Mueller 
4972a2a251fSStephan Mueller 	return crypto_aead_setauthsize(tfm->aead, authsize);
498400c40cfSStephan Mueller }
499400c40cfSStephan Mueller 
aead_setkey(void * private,const u8 * key,unsigned int keylen)500400c40cfSStephan Mueller static int aead_setkey(void *private, const u8 *key, unsigned int keylen)
501400c40cfSStephan Mueller {
5022a2a251fSStephan Mueller 	struct aead_tfm *tfm = private;
5032a2a251fSStephan Mueller 
504dc26c17fSEric Biggers 	return crypto_aead_setkey(tfm->aead, key, keylen);
505400c40cfSStephan Mueller }
506400c40cfSStephan Mueller 
aead_sock_destruct(struct sock * sk)507400c40cfSStephan Mueller static void aead_sock_destruct(struct sock *sk)
508400c40cfSStephan Mueller {
509400c40cfSStephan Mueller 	struct alg_sock *ask = alg_sk(sk);
5102d97591eSStephan Mueller 	struct af_alg_ctx *ctx = ask->private;
511d887c52dSStephan Mueller 	struct sock *psk = ask->parent;
512d887c52dSStephan Mueller 	struct alg_sock *pask = alg_sk(psk);
513d887c52dSStephan Mueller 	struct aead_tfm *aeadc = pask->private;
514d887c52dSStephan Mueller 	struct crypto_aead *tfm = aeadc->aead;
515d887c52dSStephan Mueller 	unsigned int ivlen = crypto_aead_ivsize(tfm);
516400c40cfSStephan Mueller 
5172d97591eSStephan Mueller 	af_alg_pull_tsgl(sk, ctx->used, NULL, 0);
518400c40cfSStephan Mueller 	sock_kzfree_s(sk, ctx->iv, ivlen);
519400c40cfSStephan Mueller 	sock_kfree_s(sk, ctx, ctx->len);
520400c40cfSStephan Mueller 	af_alg_release_parent(sk);
521400c40cfSStephan Mueller }
522400c40cfSStephan Mueller 
aead_accept_parent_nokey(void * private,struct sock * sk)5232a2a251fSStephan Mueller static int aead_accept_parent_nokey(void *private, struct sock *sk)
524400c40cfSStephan Mueller {
5252d97591eSStephan Mueller 	struct af_alg_ctx *ctx;
526400c40cfSStephan Mueller 	struct alg_sock *ask = alg_sk(sk);
5272a2a251fSStephan Mueller 	struct aead_tfm *tfm = private;
5282a2a251fSStephan Mueller 	struct crypto_aead *aead = tfm->aead;
529d887c52dSStephan Mueller 	unsigned int len = sizeof(*ctx);
5302a2a251fSStephan Mueller 	unsigned int ivlen = crypto_aead_ivsize(aead);
531400c40cfSStephan Mueller 
532400c40cfSStephan Mueller 	ctx = sock_kmalloc(sk, len, GFP_KERNEL);
533400c40cfSStephan Mueller 	if (!ctx)
534400c40cfSStephan Mueller 		return -ENOMEM;
535400c40cfSStephan Mueller 	memset(ctx, 0, len);
536400c40cfSStephan Mueller 
537400c40cfSStephan Mueller 	ctx->iv = sock_kmalloc(sk, ivlen, GFP_KERNEL);
538400c40cfSStephan Mueller 	if (!ctx->iv) {
539400c40cfSStephan Mueller 		sock_kfree_s(sk, ctx, len);
540400c40cfSStephan Mueller 		return -ENOMEM;
541400c40cfSStephan Mueller 	}
542400c40cfSStephan Mueller 	memset(ctx->iv, 0, ivlen);
543400c40cfSStephan Mueller 
544d887c52dSStephan Mueller 	INIT_LIST_HEAD(&ctx->tsgl_list);
545400c40cfSStephan Mueller 	ctx->len = len;
5462c3f8b16SGilad Ben-Yossef 	crypto_init_wait(&ctx->wait);
547400c40cfSStephan Mueller 
548400c40cfSStephan Mueller 	ask->private = ctx;
549400c40cfSStephan Mueller 
550400c40cfSStephan Mueller 	sk->sk_destruct = aead_sock_destruct;
551400c40cfSStephan Mueller 
552400c40cfSStephan Mueller 	return 0;
553400c40cfSStephan Mueller }
554400c40cfSStephan Mueller 
aead_accept_parent(void * private,struct sock * sk)5552a2a251fSStephan Mueller static int aead_accept_parent(void *private, struct sock *sk)
5562a2a251fSStephan Mueller {
5572a2a251fSStephan Mueller 	struct aead_tfm *tfm = private;
5582a2a251fSStephan Mueller 
559dc26c17fSEric Biggers 	if (crypto_aead_get_flags(tfm->aead) & CRYPTO_TFM_NEED_KEY)
5602a2a251fSStephan Mueller 		return -ENOKEY;
5612a2a251fSStephan Mueller 
5622a2a251fSStephan Mueller 	return aead_accept_parent_nokey(private, sk);
5632a2a251fSStephan Mueller }
5642a2a251fSStephan Mueller 
565400c40cfSStephan Mueller static const struct af_alg_type algif_type_aead = {
566400c40cfSStephan Mueller 	.bind		=	aead_bind,
567400c40cfSStephan Mueller 	.release	=	aead_release,
568400c40cfSStephan Mueller 	.setkey		=	aead_setkey,
569400c40cfSStephan Mueller 	.setauthsize	=	aead_setauthsize,
570400c40cfSStephan Mueller 	.accept		=	aead_accept_parent,
5712a2a251fSStephan Mueller 	.accept_nokey	=	aead_accept_parent_nokey,
572400c40cfSStephan Mueller 	.ops		=	&algif_aead_ops,
5732a2a251fSStephan Mueller 	.ops_nokey	=	&algif_aead_ops_nokey,
574400c40cfSStephan Mueller 	.name		=	"aead",
575400c40cfSStephan Mueller 	.owner		=	THIS_MODULE
576400c40cfSStephan Mueller };
577400c40cfSStephan Mueller 
algif_aead_init(void)578400c40cfSStephan Mueller static int __init algif_aead_init(void)
579400c40cfSStephan Mueller {
580400c40cfSStephan Mueller 	return af_alg_register_type(&algif_type_aead);
581400c40cfSStephan Mueller }
582400c40cfSStephan Mueller 
algif_aead_exit(void)583400c40cfSStephan Mueller static void __exit algif_aead_exit(void)
584400c40cfSStephan Mueller {
585400c40cfSStephan Mueller 	int err = af_alg_unregister_type(&algif_type_aead);
586400c40cfSStephan Mueller 	BUG_ON(err);
587400c40cfSStephan Mueller }
588400c40cfSStephan Mueller 
589400c40cfSStephan Mueller module_init(algif_aead_init);
590400c40cfSStephan Mueller module_exit(algif_aead_exit);
591400c40cfSStephan Mueller MODULE_LICENSE("GPL");
592400c40cfSStephan Mueller MODULE_AUTHOR("Stephan Mueller <smueller@chronox.de>");
593400c40cfSStephan Mueller MODULE_DESCRIPTION("AEAD kernel crypto API user space interface");
594