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