xref: /linux/crypto/algif_skcipher.c (revision c32dd3367b975ac2c59e0fec6a8c100522f51c1c)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * algif_skcipher: User-space interface for skcipher algorithms
4  *
5  * This file provides the user-space API for symmetric key ciphers.
6  *
7  * Copyright (c) 2010 Herbert Xu <herbert@gondor.apana.org.au>
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
14  * the kernel. Upon receipt of one recvmsg call, the caller must provide a
15  * buffer 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/scatterwalk.h>
27 #include <crypto/skcipher.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 const struct af_alg_allowlist_entry skcipher_allowlist[] = {
38 	{ "adiantum(xchacha12,aes)", false }, /* cryptsetup */
39 	{ "adiantum(xchacha20,aes)", false }, /* cryptsetup */
40 	{ "cbc(aes)", true }, /* iwd */
41 	{ "cbc(des)", true }, /* iwd */
42 	{ "cbc(des3_ede)", true }, /* iwd */
43 	{ "ctr(aes)", true }, /* iwd */
44 	{ "ecb(aes)", true }, /* iwd, bluez */
45 	{ "ecb(des)", true }, /* iwd */
46 	{ "hctr2(aes)", false }, /* cryptsetup */
47 	{ "xts(aes)", false }, /* cryptsetup benchmark */
48 	{ "xts(camellia)", false }, /* cryptsetup */
49 	{ "xts(serpent)", false }, /* cryptsetup */
50 	{ "xts(twofish)", false }, /* cryptsetup */
51 	{},
52 };
53 
54 static int skcipher_sendmsg(struct socket *sock, struct msghdr *msg,
55 			    size_t size)
56 {
57 	struct sock *sk = sock->sk;
58 	struct alg_sock *ask = alg_sk(sk);
59 	struct sock *psk = ask->parent;
60 	struct alg_sock *pask = alg_sk(psk);
61 	struct crypto_skcipher *tfm = pask->private;
62 	unsigned ivsize = crypto_skcipher_ivsize(tfm);
63 
64 	return af_alg_sendmsg(sock, msg, size, ivsize);
65 }
66 
67 static int algif_skcipher_export(struct sock *sk, struct skcipher_request *req)
68 {
69 	struct alg_sock *ask = alg_sk(sk);
70 	struct crypto_skcipher *tfm;
71 	struct af_alg_ctx *ctx;
72 	struct alg_sock *pask;
73 	unsigned statesize;
74 	struct sock *psk;
75 	int err;
76 
77 	if (!(req->base.flags & CRYPTO_SKCIPHER_REQ_NOTFINAL))
78 		return 0;
79 
80 	ctx = ask->private;
81 	psk = ask->parent;
82 	pask = alg_sk(psk);
83 	tfm = pask->private;
84 
85 	statesize = crypto_skcipher_statesize(tfm);
86 	ctx->state = sock_kmalloc(sk, statesize, GFP_ATOMIC);
87 	if (!ctx->state)
88 		return -ENOMEM;
89 
90 	err = crypto_skcipher_export(req, ctx->state);
91 	if (err) {
92 		sock_kzfree_s(sk, ctx->state, statesize);
93 		ctx->state = NULL;
94 	}
95 
96 	return err;
97 }
98 
99 static int _skcipher_recvmsg(struct socket *sock, struct msghdr *msg,
100 			     size_t ignored, int flags)
101 {
102 	struct sock *sk = sock->sk;
103 	struct alg_sock *ask = alg_sk(sk);
104 	struct sock *psk = ask->parent;
105 	struct alg_sock *pask = alg_sk(psk);
106 	struct af_alg_ctx *ctx = ask->private;
107 	struct crypto_skcipher *tfm = pask->private;
108 	unsigned int bs = crypto_skcipher_chunksize(tfm);
109 	struct af_alg_async_req *areq;
110 	unsigned cflags = 0;
111 	int err = 0;
112 	size_t len = 0;
113 
114 	if (!ctx->init || (ctx->more && ctx->used < bs)) {
115 		err = af_alg_wait_for_data(sk, flags, bs);
116 		if (err)
117 			return err;
118 	}
119 
120 	/* Allocate cipher request for current operation. */
121 	areq = af_alg_alloc_areq(sk, sizeof(struct af_alg_async_req) +
122 				     crypto_skcipher_reqsize(tfm));
123 	if (IS_ERR(areq))
124 		return PTR_ERR(areq);
125 
126 	/* convert iovecs of output buffers into RX SGL */
127 	err = af_alg_get_rsgl(sk, msg, flags, areq, ctx->used, &len);
128 	if (err)
129 		goto free;
130 
131 	/*
132 	 * If more buffers are to be expected to be processed, process only
133 	 * full block size buffers.
134 	 */
135 	if (ctx->more || len < ctx->used) {
136 		if (len < bs) {
137 			err = -EINVAL;
138 			goto free;
139 		}
140 
141 		len -= len % bs;
142 		cflags |= CRYPTO_SKCIPHER_REQ_NOTFINAL;
143 	}
144 
145 	/*
146 	 * Create a per request TX SGL for this request which tracks the
147 	 * SG entries from the global TX SGL.
148 	 */
149 	areq->tsgl_entries = af_alg_count_tsgl(sk, len);
150 	if (!areq->tsgl_entries)
151 		areq->tsgl_entries = 1;
152 	areq->tsgl = sock_kmalloc(sk, array_size(sizeof(*areq->tsgl),
153 						 areq->tsgl_entries),
154 				  GFP_KERNEL);
155 	if (!areq->tsgl) {
156 		err = -ENOMEM;
157 		goto free;
158 	}
159 	sg_init_table(areq->tsgl, areq->tsgl_entries);
160 	af_alg_pull_tsgl(sk, len, areq->tsgl);
161 
162 	/* Initialize the crypto operation */
163 	skcipher_request_set_tfm(&areq->cra_u.skcipher_req, tfm);
164 	skcipher_request_set_crypt(&areq->cra_u.skcipher_req, areq->tsgl,
165 				   areq->first_rsgl.sgl.sgt.sgl, len, ctx->iv);
166 
167 	if (ctx->state) {
168 		err = crypto_skcipher_import(&areq->cra_u.skcipher_req,
169 					     ctx->state);
170 		sock_kzfree_s(sk, ctx->state, crypto_skcipher_statesize(tfm));
171 		ctx->state = NULL;
172 		if (err)
173 			goto free;
174 		cflags |= CRYPTO_SKCIPHER_REQ_CONT;
175 	}
176 
177 	skcipher_request_set_callback(&areq->cra_u.skcipher_req,
178 				      cflags |
179 				      CRYPTO_TFM_REQ_MAY_SLEEP |
180 				      CRYPTO_TFM_REQ_MAY_BACKLOG,
181 				      crypto_req_done, &ctx->wait);
182 	err = crypto_wait_req(ctx->enc ?
183 		crypto_skcipher_encrypt(&areq->cra_u.skcipher_req) :
184 		crypto_skcipher_decrypt(&areq->cra_u.skcipher_req),
185 					 &ctx->wait);
186 
187 	if (!err)
188 		err = algif_skcipher_export(
189 			sk, &areq->cra_u.skcipher_req);
190 
191 free:
192 	af_alg_free_resources(areq);
193 
194 	return err ? err : len;
195 }
196 
197 static int skcipher_recvmsg(struct socket *sock, struct msghdr *msg,
198 			    size_t ignored, int flags)
199 {
200 	struct sock *sk = sock->sk;
201 	int ret = 0;
202 
203 	lock_sock(sk);
204 	while (msg_data_left(msg)) {
205 		int err = _skcipher_recvmsg(sock, msg, ignored, flags);
206 
207 		/*
208 		 * This error covers -EIOCBQUEUED which implies that we can
209 		 * only handle one AIO request. If the caller wants to have
210 		 * multiple AIO requests in parallel, he must make multiple
211 		 * separate AIO calls.
212 		 *
213 		 * Also return the error if no data has been processed so far.
214 		 */
215 		if (err <= 0) {
216 			if (err == -EIOCBQUEUED || !ret)
217 				ret = err;
218 			goto out;
219 		}
220 
221 		ret += err;
222 	}
223 
224 out:
225 	af_alg_wmem_wakeup(sk);
226 	release_sock(sk);
227 	return ret;
228 }
229 
230 static struct proto_ops algif_skcipher_ops = {
231 	.family		=	PF_ALG,
232 
233 	.connect	=	sock_no_connect,
234 	.socketpair	=	sock_no_socketpair,
235 	.getname	=	sock_no_getname,
236 	.ioctl		=	sock_no_ioctl,
237 	.listen		=	sock_no_listen,
238 	.shutdown	=	sock_no_shutdown,
239 	.mmap		=	sock_no_mmap,
240 	.bind		=	sock_no_bind,
241 	.accept		=	sock_no_accept,
242 
243 	.release	=	af_alg_release,
244 	.sendmsg	=	skcipher_sendmsg,
245 	.recvmsg	=	skcipher_recvmsg,
246 	.poll		=	af_alg_poll,
247 };
248 
249 static int skcipher_check_key(struct socket *sock)
250 {
251 	int err = 0;
252 	struct sock *psk;
253 	struct alg_sock *pask;
254 	struct crypto_skcipher *tfm;
255 	struct sock *sk = sock->sk;
256 	struct alg_sock *ask = alg_sk(sk);
257 
258 	lock_sock(sk);
259 	if (!atomic_read(&ask->nokey_refcnt))
260 		goto unlock_child;
261 
262 	psk = ask->parent;
263 	pask = alg_sk(ask->parent);
264 	tfm = pask->private;
265 
266 	err = -ENOKEY;
267 	lock_sock_nested(psk, SINGLE_DEPTH_NESTING);
268 	if (crypto_skcipher_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
269 		goto unlock;
270 
271 	atomic_dec(&pask->nokey_refcnt);
272 	atomic_set(&ask->nokey_refcnt, 0);
273 
274 	err = 0;
275 
276 unlock:
277 	release_sock(psk);
278 unlock_child:
279 	release_sock(sk);
280 
281 	return err;
282 }
283 
284 static int skcipher_sendmsg_nokey(struct socket *sock, struct msghdr *msg,
285 				  size_t size)
286 {
287 	int err;
288 
289 	err = skcipher_check_key(sock);
290 	if (err)
291 		return err;
292 
293 	return skcipher_sendmsg(sock, msg, size);
294 }
295 
296 static int skcipher_recvmsg_nokey(struct socket *sock, struct msghdr *msg,
297 				  size_t ignored, int flags)
298 {
299 	int err;
300 
301 	err = skcipher_check_key(sock);
302 	if (err)
303 		return err;
304 
305 	return skcipher_recvmsg(sock, msg, ignored, flags);
306 }
307 
308 static struct proto_ops algif_skcipher_ops_nokey = {
309 	.family		=	PF_ALG,
310 
311 	.connect	=	sock_no_connect,
312 	.socketpair	=	sock_no_socketpair,
313 	.getname	=	sock_no_getname,
314 	.ioctl		=	sock_no_ioctl,
315 	.listen		=	sock_no_listen,
316 	.shutdown	=	sock_no_shutdown,
317 	.mmap		=	sock_no_mmap,
318 	.bind		=	sock_no_bind,
319 	.accept		=	sock_no_accept,
320 
321 	.release	=	af_alg_release,
322 	.sendmsg	=	skcipher_sendmsg_nokey,
323 	.recvmsg	=	skcipher_recvmsg_nokey,
324 	.poll		=	af_alg_poll,
325 };
326 
327 static void *skcipher_bind(const char *name)
328 {
329 	int err;
330 
331 	err = af_alg_check_restriction(name, skcipher_allowlist);
332 	if (err)
333 		return ERR_PTR(err);
334 
335 	return crypto_alloc_skcipher(name, 0, AF_ALG_CRYPTOAPI_MASK);
336 }
337 
338 static void skcipher_release(void *private)
339 {
340 	crypto_free_skcipher(private);
341 }
342 
343 static int skcipher_setkey(void *private, const u8 *key, unsigned int keylen)
344 {
345 	return crypto_skcipher_setkey(private, key, keylen);
346 }
347 
348 static void skcipher_sock_destruct(struct sock *sk)
349 {
350 	struct alg_sock *ask = alg_sk(sk);
351 	struct af_alg_ctx *ctx = ask->private;
352 	struct sock *psk = ask->parent;
353 	struct alg_sock *pask = alg_sk(psk);
354 	struct crypto_skcipher *tfm = pask->private;
355 
356 	af_alg_pull_tsgl(sk, ctx->used, NULL);
357 	sock_kzfree_s(sk, ctx->iv, crypto_skcipher_ivsize(tfm));
358 	if (ctx->state)
359 		sock_kzfree_s(sk, ctx->state, crypto_skcipher_statesize(tfm));
360 	sock_kfree_s(sk, ctx, ctx->len);
361 	af_alg_release_parent(sk);
362 }
363 
364 static int skcipher_accept_parent_nokey(void *private, struct sock *sk)
365 {
366 	struct af_alg_ctx *ctx;
367 	struct alg_sock *ask = alg_sk(sk);
368 	struct crypto_skcipher *tfm = private;
369 	unsigned int len = sizeof(*ctx);
370 
371 	ctx = sock_kmalloc(sk, len, GFP_KERNEL);
372 	if (!ctx)
373 		return -ENOMEM;
374 	memset(ctx, 0, len);
375 
376 	ctx->iv = sock_kmalloc(sk, crypto_skcipher_ivsize(tfm),
377 			       GFP_KERNEL);
378 	if (!ctx->iv) {
379 		sock_kfree_s(sk, ctx, len);
380 		return -ENOMEM;
381 	}
382 	memset(ctx->iv, 0, crypto_skcipher_ivsize(tfm));
383 
384 	INIT_LIST_HEAD(&ctx->tsgl_list);
385 	ctx->len = len;
386 	crypto_init_wait(&ctx->wait);
387 
388 	ask->private = ctx;
389 
390 	sk->sk_destruct = skcipher_sock_destruct;
391 
392 	return 0;
393 }
394 
395 static int skcipher_accept_parent(void *private, struct sock *sk)
396 {
397 	struct crypto_skcipher *tfm = private;
398 
399 	if (crypto_skcipher_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
400 		return -ENOKEY;
401 
402 	return skcipher_accept_parent_nokey(private, sk);
403 }
404 
405 static const struct af_alg_type algif_type_skcipher = {
406 	.bind		=	skcipher_bind,
407 	.release	=	skcipher_release,
408 	.setkey		=	skcipher_setkey,
409 	.accept		=	skcipher_accept_parent,
410 	.accept_nokey	=	skcipher_accept_parent_nokey,
411 	.ops		=	&algif_skcipher_ops,
412 	.ops_nokey	=	&algif_skcipher_ops_nokey,
413 	.name		=	"skcipher",
414 	.owner		=	THIS_MODULE
415 };
416 
417 static int __init algif_skcipher_init(void)
418 {
419 	return af_alg_register_type(&algif_type_skcipher);
420 }
421 
422 static void __exit algif_skcipher_exit(void)
423 {
424 	int err = af_alg_unregister_type(&algif_type_skcipher);
425 	BUG_ON(err);
426 }
427 
428 module_init(algif_skcipher_init);
429 module_exit(algif_skcipher_exit);
430 MODULE_DESCRIPTION("Userspace interface for skcipher algorithms");
431 MODULE_LICENSE("GPL");
432