xref: /openbmc/linux/crypto/algif_skcipher.c (revision 4161b450)
1 /*
2  * algif_skcipher: User-space interface for skcipher algorithms
3  *
4  * This file provides the user-space API for symmetric key ciphers.
5  *
6  * Copyright (c) 2010 Herbert Xu <herbert@gondor.apana.org.au>
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License as published by the Free
10  * Software Foundation; either version 2 of the License, or (at your option)
11  * any later version.
12  *
13  */
14 
15 #include <crypto/scatterwalk.h>
16 #include <crypto/skcipher.h>
17 #include <crypto/if_alg.h>
18 #include <linux/init.h>
19 #include <linux/list.h>
20 #include <linux/kernel.h>
21 #include <linux/mm.h>
22 #include <linux/module.h>
23 #include <linux/net.h>
24 #include <net/sock.h>
25 
26 struct skcipher_sg_list {
27 	struct list_head list;
28 
29 	int cur;
30 
31 	struct scatterlist sg[0];
32 };
33 
34 struct skcipher_ctx {
35 	struct list_head tsgl;
36 	struct af_alg_sgl rsgl;
37 
38 	void *iv;
39 
40 	struct af_alg_completion completion;
41 
42 	unsigned used;
43 
44 	unsigned int len;
45 	bool more;
46 	bool merge;
47 	bool enc;
48 
49 	struct ablkcipher_request req;
50 };
51 
52 #define MAX_SGL_ENTS ((4096 - sizeof(struct skcipher_sg_list)) / \
53 		      sizeof(struct scatterlist) - 1)
54 
55 static inline int skcipher_sndbuf(struct sock *sk)
56 {
57 	struct alg_sock *ask = alg_sk(sk);
58 	struct skcipher_ctx *ctx = ask->private;
59 
60 	return max_t(int, max_t(int, sk->sk_sndbuf & PAGE_MASK, PAGE_SIZE) -
61 			  ctx->used, 0);
62 }
63 
64 static inline bool skcipher_writable(struct sock *sk)
65 {
66 	return PAGE_SIZE <= skcipher_sndbuf(sk);
67 }
68 
69 static int skcipher_alloc_sgl(struct sock *sk)
70 {
71 	struct alg_sock *ask = alg_sk(sk);
72 	struct skcipher_ctx *ctx = ask->private;
73 	struct skcipher_sg_list *sgl;
74 	struct scatterlist *sg = NULL;
75 
76 	sgl = list_entry(ctx->tsgl.prev, struct skcipher_sg_list, list);
77 	if (!list_empty(&ctx->tsgl))
78 		sg = sgl->sg;
79 
80 	if (!sg || sgl->cur >= MAX_SGL_ENTS) {
81 		sgl = sock_kmalloc(sk, sizeof(*sgl) +
82 				       sizeof(sgl->sg[0]) * (MAX_SGL_ENTS + 1),
83 				   GFP_KERNEL);
84 		if (!sgl)
85 			return -ENOMEM;
86 
87 		sg_init_table(sgl->sg, MAX_SGL_ENTS + 1);
88 		sgl->cur = 0;
89 
90 		if (sg)
91 			scatterwalk_sg_chain(sg, MAX_SGL_ENTS + 1, sgl->sg);
92 
93 		list_add_tail(&sgl->list, &ctx->tsgl);
94 	}
95 
96 	return 0;
97 }
98 
99 static void skcipher_pull_sgl(struct sock *sk, int used)
100 {
101 	struct alg_sock *ask = alg_sk(sk);
102 	struct skcipher_ctx *ctx = ask->private;
103 	struct skcipher_sg_list *sgl;
104 	struct scatterlist *sg;
105 	int i;
106 
107 	while (!list_empty(&ctx->tsgl)) {
108 		sgl = list_first_entry(&ctx->tsgl, struct skcipher_sg_list,
109 				       list);
110 		sg = sgl->sg;
111 
112 		for (i = 0; i < sgl->cur; i++) {
113 			int plen = min_t(int, used, sg[i].length);
114 
115 			if (!sg_page(sg + i))
116 				continue;
117 
118 			sg[i].length -= plen;
119 			sg[i].offset += plen;
120 
121 			used -= plen;
122 			ctx->used -= plen;
123 
124 			if (sg[i].length)
125 				return;
126 
127 			put_page(sg_page(sg + i));
128 			sg_assign_page(sg + i, NULL);
129 		}
130 
131 		list_del(&sgl->list);
132 		sock_kfree_s(sk, sgl,
133 			     sizeof(*sgl) + sizeof(sgl->sg[0]) *
134 					    (MAX_SGL_ENTS + 1));
135 	}
136 
137 	if (!ctx->used)
138 		ctx->merge = 0;
139 }
140 
141 static void skcipher_free_sgl(struct sock *sk)
142 {
143 	struct alg_sock *ask = alg_sk(sk);
144 	struct skcipher_ctx *ctx = ask->private;
145 
146 	skcipher_pull_sgl(sk, ctx->used);
147 }
148 
149 static int skcipher_wait_for_wmem(struct sock *sk, unsigned flags)
150 {
151 	long timeout;
152 	DEFINE_WAIT(wait);
153 	int err = -ERESTARTSYS;
154 
155 	if (flags & MSG_DONTWAIT)
156 		return -EAGAIN;
157 
158 	set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
159 
160 	for (;;) {
161 		if (signal_pending(current))
162 			break;
163 		prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
164 		timeout = MAX_SCHEDULE_TIMEOUT;
165 		if (sk_wait_event(sk, &timeout, skcipher_writable(sk))) {
166 			err = 0;
167 			break;
168 		}
169 	}
170 	finish_wait(sk_sleep(sk), &wait);
171 
172 	return err;
173 }
174 
175 static void skcipher_wmem_wakeup(struct sock *sk)
176 {
177 	struct socket_wq *wq;
178 
179 	if (!skcipher_writable(sk))
180 		return;
181 
182 	rcu_read_lock();
183 	wq = rcu_dereference(sk->sk_wq);
184 	if (wq_has_sleeper(wq))
185 		wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
186 							   POLLRDNORM |
187 							   POLLRDBAND);
188 	sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
189 	rcu_read_unlock();
190 }
191 
192 static int skcipher_wait_for_data(struct sock *sk, unsigned flags)
193 {
194 	struct alg_sock *ask = alg_sk(sk);
195 	struct skcipher_ctx *ctx = ask->private;
196 	long timeout;
197 	DEFINE_WAIT(wait);
198 	int err = -ERESTARTSYS;
199 
200 	if (flags & MSG_DONTWAIT) {
201 		return -EAGAIN;
202 	}
203 
204 	set_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
205 
206 	for (;;) {
207 		if (signal_pending(current))
208 			break;
209 		prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
210 		timeout = MAX_SCHEDULE_TIMEOUT;
211 		if (sk_wait_event(sk, &timeout, ctx->used)) {
212 			err = 0;
213 			break;
214 		}
215 	}
216 	finish_wait(sk_sleep(sk), &wait);
217 
218 	clear_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
219 
220 	return err;
221 }
222 
223 static void skcipher_data_wakeup(struct sock *sk)
224 {
225 	struct alg_sock *ask = alg_sk(sk);
226 	struct skcipher_ctx *ctx = ask->private;
227 	struct socket_wq *wq;
228 
229 	if (!ctx->used)
230 		return;
231 
232 	rcu_read_lock();
233 	wq = rcu_dereference(sk->sk_wq);
234 	if (wq_has_sleeper(wq))
235 		wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
236 							   POLLRDNORM |
237 							   POLLRDBAND);
238 	sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
239 	rcu_read_unlock();
240 }
241 
242 static int skcipher_sendmsg(struct kiocb *unused, struct socket *sock,
243 			    struct msghdr *msg, size_t size)
244 {
245 	struct sock *sk = sock->sk;
246 	struct alg_sock *ask = alg_sk(sk);
247 	struct skcipher_ctx *ctx = ask->private;
248 	struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(&ctx->req);
249 	unsigned ivsize = crypto_ablkcipher_ivsize(tfm);
250 	struct skcipher_sg_list *sgl;
251 	struct af_alg_control con = {};
252 	long copied = 0;
253 	bool enc = 0;
254 	bool init = 0;
255 	int err;
256 	int i;
257 
258 	if (msg->msg_controllen) {
259 		err = af_alg_cmsg_send(msg, &con);
260 		if (err)
261 			return err;
262 
263 		init = 1;
264 		switch (con.op) {
265 		case ALG_OP_ENCRYPT:
266 			enc = 1;
267 			break;
268 		case ALG_OP_DECRYPT:
269 			enc = 0;
270 			break;
271 		default:
272 			return -EINVAL;
273 		}
274 
275 		if (con.iv && con.iv->ivlen != ivsize)
276 			return -EINVAL;
277 	}
278 
279 	err = -EINVAL;
280 
281 	lock_sock(sk);
282 	if (!ctx->more && ctx->used)
283 		goto unlock;
284 
285 	if (init) {
286 		ctx->enc = enc;
287 		if (con.iv)
288 			memcpy(ctx->iv, con.iv->iv, ivsize);
289 	}
290 
291 	while (size) {
292 		struct scatterlist *sg;
293 		unsigned long len = size;
294 		int plen;
295 
296 		if (ctx->merge) {
297 			sgl = list_entry(ctx->tsgl.prev,
298 					 struct skcipher_sg_list, list);
299 			sg = sgl->sg + sgl->cur - 1;
300 			len = min_t(unsigned long, len,
301 				    PAGE_SIZE - sg->offset - sg->length);
302 
303 			err = memcpy_from_msg(page_address(sg_page(sg)) +
304 					      sg->offset + sg->length,
305 					      msg, len);
306 			if (err)
307 				goto unlock;
308 
309 			sg->length += len;
310 			ctx->merge = (sg->offset + sg->length) &
311 				     (PAGE_SIZE - 1);
312 
313 			ctx->used += len;
314 			copied += len;
315 			size -= len;
316 			continue;
317 		}
318 
319 		if (!skcipher_writable(sk)) {
320 			err = skcipher_wait_for_wmem(sk, msg->msg_flags);
321 			if (err)
322 				goto unlock;
323 		}
324 
325 		len = min_t(unsigned long, len, skcipher_sndbuf(sk));
326 
327 		err = skcipher_alloc_sgl(sk);
328 		if (err)
329 			goto unlock;
330 
331 		sgl = list_entry(ctx->tsgl.prev, struct skcipher_sg_list, list);
332 		sg = sgl->sg;
333 		do {
334 			i = sgl->cur;
335 			plen = min_t(int, len, PAGE_SIZE);
336 
337 			sg_assign_page(sg + i, alloc_page(GFP_KERNEL));
338 			err = -ENOMEM;
339 			if (!sg_page(sg + i))
340 				goto unlock;
341 
342 			err = memcpy_from_msg(page_address(sg_page(sg + i)),
343 					      msg, plen);
344 			if (err) {
345 				__free_page(sg_page(sg + i));
346 				sg_assign_page(sg + i, NULL);
347 				goto unlock;
348 			}
349 
350 			sg[i].length = plen;
351 			len -= plen;
352 			ctx->used += plen;
353 			copied += plen;
354 			size -= plen;
355 			sgl->cur++;
356 		} while (len && sgl->cur < MAX_SGL_ENTS);
357 
358 		ctx->merge = plen & (PAGE_SIZE - 1);
359 	}
360 
361 	err = 0;
362 
363 	ctx->more = msg->msg_flags & MSG_MORE;
364 
365 unlock:
366 	skcipher_data_wakeup(sk);
367 	release_sock(sk);
368 
369 	return copied ?: err;
370 }
371 
372 static ssize_t skcipher_sendpage(struct socket *sock, struct page *page,
373 				 int offset, size_t size, int flags)
374 {
375 	struct sock *sk = sock->sk;
376 	struct alg_sock *ask = alg_sk(sk);
377 	struct skcipher_ctx *ctx = ask->private;
378 	struct skcipher_sg_list *sgl;
379 	int err = -EINVAL;
380 
381 	if (flags & MSG_SENDPAGE_NOTLAST)
382 		flags |= MSG_MORE;
383 
384 	lock_sock(sk);
385 	if (!ctx->more && ctx->used)
386 		goto unlock;
387 
388 	if (!size)
389 		goto done;
390 
391 	if (!skcipher_writable(sk)) {
392 		err = skcipher_wait_for_wmem(sk, flags);
393 		if (err)
394 			goto unlock;
395 	}
396 
397 	err = skcipher_alloc_sgl(sk);
398 	if (err)
399 		goto unlock;
400 
401 	ctx->merge = 0;
402 	sgl = list_entry(ctx->tsgl.prev, struct skcipher_sg_list, list);
403 
404 	get_page(page);
405 	sg_set_page(sgl->sg + sgl->cur, page, size, offset);
406 	sgl->cur++;
407 	ctx->used += size;
408 
409 done:
410 	ctx->more = flags & MSG_MORE;
411 
412 unlock:
413 	skcipher_data_wakeup(sk);
414 	release_sock(sk);
415 
416 	return err ?: size;
417 }
418 
419 static int skcipher_recvmsg(struct kiocb *unused, struct socket *sock,
420 			    struct msghdr *msg, size_t ignored, int flags)
421 {
422 	struct sock *sk = sock->sk;
423 	struct alg_sock *ask = alg_sk(sk);
424 	struct skcipher_ctx *ctx = ask->private;
425 	unsigned bs = crypto_ablkcipher_blocksize(crypto_ablkcipher_reqtfm(
426 		&ctx->req));
427 	struct skcipher_sg_list *sgl;
428 	struct scatterlist *sg;
429 	unsigned long iovlen;
430 	const struct iovec *iov;
431 	int err = -EAGAIN;
432 	int used;
433 	long copied = 0;
434 
435 	lock_sock(sk);
436 	for (iov = msg->msg_iter.iov, iovlen = msg->msg_iter.nr_segs; iovlen > 0;
437 	     iovlen--, iov++) {
438 		unsigned long seglen = iov->iov_len;
439 		char __user *from = iov->iov_base;
440 
441 		while (seglen) {
442 			sgl = list_first_entry(&ctx->tsgl,
443 					       struct skcipher_sg_list, list);
444 			sg = sgl->sg;
445 
446 			while (!sg->length)
447 				sg++;
448 
449 			if (!ctx->used) {
450 				err = skcipher_wait_for_data(sk, flags);
451 				if (err)
452 					goto unlock;
453 			}
454 
455 			used = min_t(unsigned long, ctx->used, seglen);
456 
457 			used = af_alg_make_sg(&ctx->rsgl, from, used, 1);
458 			err = used;
459 			if (err < 0)
460 				goto unlock;
461 
462 			if (ctx->more || used < ctx->used)
463 				used -= used % bs;
464 
465 			err = -EINVAL;
466 			if (!used)
467 				goto free;
468 
469 			ablkcipher_request_set_crypt(&ctx->req, sg,
470 						     ctx->rsgl.sg, used,
471 						     ctx->iv);
472 
473 			err = af_alg_wait_for_completion(
474 				ctx->enc ?
475 					crypto_ablkcipher_encrypt(&ctx->req) :
476 					crypto_ablkcipher_decrypt(&ctx->req),
477 				&ctx->completion);
478 
479 free:
480 			af_alg_free_sg(&ctx->rsgl);
481 
482 			if (err)
483 				goto unlock;
484 
485 			copied += used;
486 			from += used;
487 			seglen -= used;
488 			skcipher_pull_sgl(sk, used);
489 		}
490 	}
491 
492 	err = 0;
493 
494 unlock:
495 	skcipher_wmem_wakeup(sk);
496 	release_sock(sk);
497 
498 	return copied ?: err;
499 }
500 
501 
502 static unsigned int skcipher_poll(struct file *file, struct socket *sock,
503 				  poll_table *wait)
504 {
505 	struct sock *sk = sock->sk;
506 	struct alg_sock *ask = alg_sk(sk);
507 	struct skcipher_ctx *ctx = ask->private;
508 	unsigned int mask;
509 
510 	sock_poll_wait(file, sk_sleep(sk), wait);
511 	mask = 0;
512 
513 	if (ctx->used)
514 		mask |= POLLIN | POLLRDNORM;
515 
516 	if (skcipher_writable(sk))
517 		mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
518 
519 	return mask;
520 }
521 
522 static struct proto_ops algif_skcipher_ops = {
523 	.family		=	PF_ALG,
524 
525 	.connect	=	sock_no_connect,
526 	.socketpair	=	sock_no_socketpair,
527 	.getname	=	sock_no_getname,
528 	.ioctl		=	sock_no_ioctl,
529 	.listen		=	sock_no_listen,
530 	.shutdown	=	sock_no_shutdown,
531 	.getsockopt	=	sock_no_getsockopt,
532 	.mmap		=	sock_no_mmap,
533 	.bind		=	sock_no_bind,
534 	.accept		=	sock_no_accept,
535 	.setsockopt	=	sock_no_setsockopt,
536 
537 	.release	=	af_alg_release,
538 	.sendmsg	=	skcipher_sendmsg,
539 	.sendpage	=	skcipher_sendpage,
540 	.recvmsg	=	skcipher_recvmsg,
541 	.poll		=	skcipher_poll,
542 };
543 
544 static void *skcipher_bind(const char *name, u32 type, u32 mask)
545 {
546 	return crypto_alloc_ablkcipher(name, type, mask);
547 }
548 
549 static void skcipher_release(void *private)
550 {
551 	crypto_free_ablkcipher(private);
552 }
553 
554 static int skcipher_setkey(void *private, const u8 *key, unsigned int keylen)
555 {
556 	return crypto_ablkcipher_setkey(private, key, keylen);
557 }
558 
559 static void skcipher_sock_destruct(struct sock *sk)
560 {
561 	struct alg_sock *ask = alg_sk(sk);
562 	struct skcipher_ctx *ctx = ask->private;
563 	struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(&ctx->req);
564 
565 	skcipher_free_sgl(sk);
566 	sock_kzfree_s(sk, ctx->iv, crypto_ablkcipher_ivsize(tfm));
567 	sock_kfree_s(sk, ctx, ctx->len);
568 	af_alg_release_parent(sk);
569 }
570 
571 static int skcipher_accept_parent(void *private, struct sock *sk)
572 {
573 	struct skcipher_ctx *ctx;
574 	struct alg_sock *ask = alg_sk(sk);
575 	unsigned int len = sizeof(*ctx) + crypto_ablkcipher_reqsize(private);
576 
577 	ctx = sock_kmalloc(sk, len, GFP_KERNEL);
578 	if (!ctx)
579 		return -ENOMEM;
580 
581 	ctx->iv = sock_kmalloc(sk, crypto_ablkcipher_ivsize(private),
582 			       GFP_KERNEL);
583 	if (!ctx->iv) {
584 		sock_kfree_s(sk, ctx, len);
585 		return -ENOMEM;
586 	}
587 
588 	memset(ctx->iv, 0, crypto_ablkcipher_ivsize(private));
589 
590 	INIT_LIST_HEAD(&ctx->tsgl);
591 	ctx->len = len;
592 	ctx->used = 0;
593 	ctx->more = 0;
594 	ctx->merge = 0;
595 	ctx->enc = 0;
596 	af_alg_init_completion(&ctx->completion);
597 
598 	ask->private = ctx;
599 
600 	ablkcipher_request_set_tfm(&ctx->req, private);
601 	ablkcipher_request_set_callback(&ctx->req, CRYPTO_TFM_REQ_MAY_BACKLOG,
602 					af_alg_complete, &ctx->completion);
603 
604 	sk->sk_destruct = skcipher_sock_destruct;
605 
606 	return 0;
607 }
608 
609 static const struct af_alg_type algif_type_skcipher = {
610 	.bind		=	skcipher_bind,
611 	.release	=	skcipher_release,
612 	.setkey		=	skcipher_setkey,
613 	.accept		=	skcipher_accept_parent,
614 	.ops		=	&algif_skcipher_ops,
615 	.name		=	"skcipher",
616 	.owner		=	THIS_MODULE
617 };
618 
619 static int __init algif_skcipher_init(void)
620 {
621 	return af_alg_register_type(&algif_type_skcipher);
622 }
623 
624 static void __exit algif_skcipher_exit(void)
625 {
626 	int err = af_alg_unregister_type(&algif_type_skcipher);
627 	BUG_ON(err);
628 }
629 
630 module_init(algif_skcipher_init);
631 module_exit(algif_skcipher_exit);
632 MODULE_LICENSE("GPL");
633