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