xref: /linux/crypto/algif_aead.c (revision d639d9fa162aadec1ae9980c4dcf6e50bd2f8290)
1 // SPDX-License-Identifier: GPL-2.0-or-later
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  *
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
12  * filled by user space with the data submitted via sendmsg.  Filling up the TX
13  * SGL does not cause a crypto operation -- the data will only be tracked by the
14  * kernel. Upon receipt of one recvmsg call, the caller must provide a buffer
15  * which is tracked with the RX SGL.
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.
24  */
25 
26 #include <crypto/internal/aead.h>
27 #include <crypto/scatterwalk.h>
28 #include <crypto/if_alg.h>
29 #include <linux/init.h>
30 #include <linux/list.h>
31 #include <linux/kernel.h>
32 #include <linux/mm.h>
33 #include <linux/module.h>
34 #include <linux/net.h>
35 #include <net/sock.h>
36 
37 static inline bool aead_sufficient_data(struct sock *sk)
38 {
39 	struct alg_sock *ask = alg_sk(sk);
40 	struct sock *psk = ask->parent;
41 	struct alg_sock *pask = alg_sk(psk);
42 	struct af_alg_ctx *ctx = ask->private;
43 	struct crypto_aead *tfm = pask->private;
44 	unsigned int as = crypto_aead_authsize(tfm);
45 
46 	/*
47 	 * The minimum amount of memory needed for an AEAD cipher is
48 	 * the AAD and in case of decryption the tag.
49 	 */
50 	return ctx->used >= ctx->aead_assoclen + (ctx->enc ? 0 : as);
51 }
52 
53 static int aead_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
54 {
55 	struct sock *sk = sock->sk;
56 	struct alg_sock *ask = alg_sk(sk);
57 	struct sock *psk = ask->parent;
58 	struct alg_sock *pask = alg_sk(psk);
59 	struct crypto_aead *tfm = pask->private;
60 	unsigned int ivsize = crypto_aead_ivsize(tfm);
61 
62 	return af_alg_sendmsg(sock, msg, size, ivsize);
63 }
64 
65 static int _aead_recvmsg(struct socket *sock, struct msghdr *msg,
66 			 size_t ignored, int flags)
67 {
68 	struct sock *sk = sock->sk;
69 	struct alg_sock *ask = alg_sk(sk);
70 	struct sock *psk = ask->parent;
71 	struct alg_sock *pask = alg_sk(psk);
72 	struct af_alg_ctx *ctx = ask->private;
73 	struct crypto_aead *tfm = pask->private;
74 	unsigned int as = crypto_aead_authsize(tfm);
75 	unsigned int ivsize = crypto_aead_ivsize(tfm);
76 	struct af_alg_async_req *areq;
77 	struct scatterlist *rsgl_src, *tsgl_src = NULL;
78 	void *iv;
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 */
84 
85 	if (!ctx->init || ctx->more) {
86 		err = af_alg_wait_for_data(sk, flags, 0);
87 		if (err)
88 			return err;
89 	}
90 
91 	/*
92 	 * Data length provided by caller via sendmsg that has not yet been
93 	 * processed.
94 	 */
95 	used = ctx->used;
96 
97 	/*
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.
105 	 */
106 	if (!aead_sufficient_data(sk))
107 		return -EINVAL;
108 
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;
121 
122 	/*
123 	 * The cipher operation input data is reduced by the associated data
124 	 * length as this data is processed separately later on.
125 	 */
126 	used -= ctx->aead_assoclen;
127 
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) + ivsize);
131 	if (IS_ERR(areq))
132 		return PTR_ERR(areq);
133 
134 	iv = (u8 *)aead_request_ctx(&areq->cra_u.aead_req) +
135 	     crypto_aead_reqsize(tfm);
136 	memcpy(iv, ctx->iv, ivsize);
137 
138 	/* convert iovecs of output buffers into RX SGL */
139 	err = af_alg_get_rsgl(sk, msg, flags, areq, outlen, &usedpages);
140 	if (err)
141 		goto free;
142 
143 	/*
144 	 * Ensure output buffer is sufficiently large. If the caller provides
145 	 * less buffer space, only use the relative required input size. This
146 	 * allows AIO operation where the caller sent all data to be processed
147 	 * and the AIO operation performs the operation on the different chunks
148 	 * of the input data.
149 	 */
150 	if (usedpages < outlen) {
151 		size_t less = outlen - usedpages;
152 
153 		if (used < less + (ctx->enc ? 0 : as)) {
154 			err = -EINVAL;
155 			goto free;
156 		}
157 		used -= less;
158 		outlen -= less;
159 	}
160 
161 	/*
162 	 * Create a per request TX SGL for this request which tracks the
163 	 * SG entries from the global TX SGL.
164 	 */
165 	processed = used + ctx->aead_assoclen;
166 	areq->tsgl_entries = af_alg_count_tsgl(sk, processed);
167 	if (!areq->tsgl_entries)
168 		areq->tsgl_entries = 1;
169 	areq->tsgl = sock_kmalloc(sk, array_size(sizeof(*areq->tsgl),
170 					         areq->tsgl_entries),
171 				  GFP_KERNEL);
172 	if (!areq->tsgl) {
173 		err = -ENOMEM;
174 		goto free;
175 	}
176 	sg_init_table(areq->tsgl, areq->tsgl_entries);
177 	af_alg_pull_tsgl(sk, processed, areq->tsgl);
178 	tsgl_src = areq->tsgl;
179 
180 	/*
181 	 * Copy of AAD from source to destination
182 	 *
183 	 * The AAD is copied to the destination buffer without change. Even
184 	 * when user space uses an in-place cipher operation, the kernel
185 	 * will copy the data as it does not see whether such in-place operation
186 	 * is initiated.
187 	 */
188 
189 	/* Use the RX SGL as source (and destination) for crypto op. */
190 	rsgl_src = areq->first_rsgl.sgl.sgt.sgl;
191 
192 	memcpy_sglist(rsgl_src, tsgl_src, ctx->aead_assoclen);
193 
194 	/* Initialize the crypto operation */
195 	aead_request_set_crypt(&areq->cra_u.aead_req, tsgl_src,
196 			       areq->first_rsgl.sgl.sgt.sgl, used, iv);
197 	aead_request_set_ad(&areq->cra_u.aead_req, ctx->aead_assoclen);
198 	aead_request_set_tfm(&areq->cra_u.aead_req, tfm);
199 
200 	aead_request_set_callback(&areq->cra_u.aead_req,
201 				  CRYPTO_TFM_REQ_MAY_SLEEP |
202 				  CRYPTO_TFM_REQ_MAY_BACKLOG,
203 				  crypto_req_done, &ctx->wait);
204 	err = crypto_wait_req(ctx->enc ?
205 			crypto_aead_encrypt(&areq->cra_u.aead_req) :
206 			crypto_aead_decrypt(&areq->cra_u.aead_req),
207 			&ctx->wait);
208 
209 free:
210 	af_alg_free_resources(areq);
211 
212 	return err ? err : outlen;
213 }
214 
215 static int aead_recvmsg(struct socket *sock, struct msghdr *msg,
216 			size_t ignored, int flags)
217 {
218 	struct sock *sk = sock->sk;
219 	int ret = 0;
220 
221 	lock_sock(sk);
222 	while (msg_data_left(msg)) {
223 		int err = _aead_recvmsg(sock, msg, ignored, flags);
224 
225 		/*
226 		 * This error covers -EIOCBQUEUED which implies that we can
227 		 * only handle one AIO request. If the caller wants to have
228 		 * multiple AIO requests in parallel, he must make multiple
229 		 * separate AIO calls.
230 		 *
231 		 * Also return the error if no data has been processed so far.
232 		 */
233 		if (err <= 0) {
234 			if (err == -EIOCBQUEUED || err == -EBADMSG || !ret)
235 				ret = err;
236 			goto out;
237 		}
238 
239 		ret += err;
240 	}
241 
242 out:
243 	af_alg_wmem_wakeup(sk);
244 	release_sock(sk);
245 	return ret;
246 }
247 
248 static struct proto_ops algif_aead_ops = {
249 	.family		=	PF_ALG,
250 
251 	.connect	=	sock_no_connect,
252 	.socketpair	=	sock_no_socketpair,
253 	.getname	=	sock_no_getname,
254 	.ioctl		=	sock_no_ioctl,
255 	.listen		=	sock_no_listen,
256 	.shutdown	=	sock_no_shutdown,
257 	.mmap		=	sock_no_mmap,
258 	.bind		=	sock_no_bind,
259 	.accept		=	sock_no_accept,
260 
261 	.release	=	af_alg_release,
262 	.sendmsg	=	aead_sendmsg,
263 	.recvmsg	=	aead_recvmsg,
264 	.poll		=	af_alg_poll,
265 };
266 
267 static int aead_check_key(struct socket *sock)
268 {
269 	int err = 0;
270 	struct sock *psk;
271 	struct alg_sock *pask;
272 	struct crypto_aead *tfm;
273 	struct sock *sk = sock->sk;
274 	struct alg_sock *ask = alg_sk(sk);
275 
276 	lock_sock(sk);
277 	if (!atomic_read(&ask->nokey_refcnt))
278 		goto unlock_child;
279 
280 	psk = ask->parent;
281 	pask = alg_sk(ask->parent);
282 	tfm = pask->private;
283 
284 	err = -ENOKEY;
285 	lock_sock_nested(psk, SINGLE_DEPTH_NESTING);
286 	if (crypto_aead_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
287 		goto unlock;
288 
289 	atomic_dec(&pask->nokey_refcnt);
290 	atomic_set(&ask->nokey_refcnt, 0);
291 
292 	err = 0;
293 
294 unlock:
295 	release_sock(psk);
296 unlock_child:
297 	release_sock(sk);
298 
299 	return err;
300 }
301 
302 static int aead_sendmsg_nokey(struct socket *sock, struct msghdr *msg,
303 				  size_t size)
304 {
305 	int err;
306 
307 	err = aead_check_key(sock);
308 	if (err)
309 		return err;
310 
311 	return aead_sendmsg(sock, msg, size);
312 }
313 
314 static int aead_recvmsg_nokey(struct socket *sock, struct msghdr *msg,
315 				  size_t ignored, int flags)
316 {
317 	int err;
318 
319 	err = aead_check_key(sock);
320 	if (err)
321 		return err;
322 
323 	return aead_recvmsg(sock, msg, ignored, flags);
324 }
325 
326 static struct proto_ops algif_aead_ops_nokey = {
327 	.family		=	PF_ALG,
328 
329 	.connect	=	sock_no_connect,
330 	.socketpair	=	sock_no_socketpair,
331 	.getname	=	sock_no_getname,
332 	.ioctl		=	sock_no_ioctl,
333 	.listen		=	sock_no_listen,
334 	.shutdown	=	sock_no_shutdown,
335 	.mmap		=	sock_no_mmap,
336 	.bind		=	sock_no_bind,
337 	.accept		=	sock_no_accept,
338 
339 	.release	=	af_alg_release,
340 	.sendmsg	=	aead_sendmsg_nokey,
341 	.recvmsg	=	aead_recvmsg_nokey,
342 	.poll		=	af_alg_poll,
343 };
344 
345 static void *aead_bind(const char *name)
346 {
347 	return crypto_alloc_aead(name, 0, AF_ALG_CRYPTOAPI_MASK);
348 }
349 
350 static void aead_release(void *private)
351 {
352 	crypto_free_aead(private);
353 }
354 
355 static int aead_setauthsize(void *private, unsigned int authsize)
356 {
357 	return crypto_aead_setauthsize(private, authsize);
358 }
359 
360 static int aead_setkey(void *private, const u8 *key, unsigned int keylen)
361 {
362 	return crypto_aead_setkey(private, key, keylen);
363 }
364 
365 static void aead_sock_destruct(struct sock *sk)
366 {
367 	struct alg_sock *ask = alg_sk(sk);
368 	struct af_alg_ctx *ctx = ask->private;
369 	struct sock *psk = ask->parent;
370 	struct alg_sock *pask = alg_sk(psk);
371 	struct crypto_aead *tfm = pask->private;
372 	unsigned int ivlen = crypto_aead_ivsize(tfm);
373 
374 	af_alg_pull_tsgl(sk, ctx->used, NULL);
375 	sock_kzfree_s(sk, ctx->iv, ivlen);
376 	sock_kfree_s(sk, ctx, ctx->len);
377 	af_alg_release_parent(sk);
378 }
379 
380 static int aead_accept_parent_nokey(void *private, struct sock *sk)
381 {
382 	struct af_alg_ctx *ctx;
383 	struct alg_sock *ask = alg_sk(sk);
384 	struct crypto_aead *tfm = private;
385 	unsigned int len = sizeof(*ctx);
386 	unsigned int ivlen = crypto_aead_ivsize(tfm);
387 
388 	ctx = sock_kmalloc(sk, len, GFP_KERNEL);
389 	if (!ctx)
390 		return -ENOMEM;
391 	memset(ctx, 0, len);
392 
393 	ctx->iv = sock_kmalloc(sk, ivlen, GFP_KERNEL);
394 	if (!ctx->iv) {
395 		sock_kfree_s(sk, ctx, len);
396 		return -ENOMEM;
397 	}
398 	memset(ctx->iv, 0, ivlen);
399 
400 	INIT_LIST_HEAD(&ctx->tsgl_list);
401 	ctx->len = len;
402 	crypto_init_wait(&ctx->wait);
403 
404 	ask->private = ctx;
405 
406 	sk->sk_destruct = aead_sock_destruct;
407 
408 	return 0;
409 }
410 
411 static int aead_accept_parent(void *private, struct sock *sk)
412 {
413 	struct crypto_aead *tfm = private;
414 
415 	if (crypto_aead_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
416 		return -ENOKEY;
417 
418 	return aead_accept_parent_nokey(private, sk);
419 }
420 
421 static const struct af_alg_type algif_type_aead = {
422 	.bind		=	aead_bind,
423 	.release	=	aead_release,
424 	.setkey		=	aead_setkey,
425 	.setauthsize	=	aead_setauthsize,
426 	.accept		=	aead_accept_parent,
427 	.accept_nokey	=	aead_accept_parent_nokey,
428 	.ops		=	&algif_aead_ops,
429 	.ops_nokey	=	&algif_aead_ops_nokey,
430 	.name		=	"aead",
431 	.owner		=	THIS_MODULE
432 };
433 
434 static int __init algif_aead_init(void)
435 {
436 	return af_alg_register_type(&algif_type_aead);
437 }
438 
439 static void __exit algif_aead_exit(void)
440 {
441 	int err = af_alg_unregister_type(&algif_type_aead);
442 	BUG_ON(err);
443 }
444 
445 module_init(algif_aead_init);
446 module_exit(algif_aead_exit);
447 MODULE_LICENSE("GPL");
448 MODULE_AUTHOR("Stephan Mueller <smueller@chronox.de>");
449 MODULE_DESCRIPTION("AEAD kernel crypto API user space interface");
450