1/*
2 * algif_aead: User-space interface for AEAD algorithms
3 *
4 * Copyright (C) 2014, Stephan Mueller <smueller@chronox.de>
5 *
6 * This file provides the user-space API for AEAD ciphers.
7 *
8 * This file is derived from algif_skcipher.c.
9 *
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the Free
12 * Software Foundation; either version 2 of the License, or (at your option)
13 * any later version.
14 */
15
16#include <crypto/scatterwalk.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
26struct aead_sg_list {
27	unsigned int cur;
28	struct scatterlist sg[ALG_MAX_PAGES];
29};
30
31struct aead_ctx {
32	struct aead_sg_list tsgl;
33	/*
34	 * RSGL_MAX_ENTRIES is an artificial limit where user space at maximum
35	 * can cause the kernel to allocate RSGL_MAX_ENTRIES * ALG_MAX_PAGES
36	 * pages
37	 */
38#define RSGL_MAX_ENTRIES ALG_MAX_PAGES
39	struct af_alg_sgl rsgl[RSGL_MAX_ENTRIES];
40
41	void *iv;
42
43	struct af_alg_completion completion;
44
45	unsigned long used;
46
47	unsigned int len;
48	bool more;
49	bool merge;
50	bool enc;
51
52	size_t aead_assoclen;
53	struct aead_request aead_req;
54};
55
56static inline int aead_sndbuf(struct sock *sk)
57{
58	struct alg_sock *ask = alg_sk(sk);
59	struct aead_ctx *ctx = ask->private;
60
61	return max_t(int, max_t(int, sk->sk_sndbuf & PAGE_MASK, PAGE_SIZE) -
62			  ctx->used, 0);
63}
64
65static inline bool aead_writable(struct sock *sk)
66{
67	return PAGE_SIZE <= aead_sndbuf(sk);
68}
69
70static inline bool aead_sufficient_data(struct aead_ctx *ctx)
71{
72	unsigned as = crypto_aead_authsize(crypto_aead_reqtfm(&ctx->aead_req));
73
74	return (ctx->used >= (ctx->aead_assoclen + (ctx->enc ? 0 : as)));
75}
76
77static void aead_put_sgl(struct sock *sk)
78{
79	struct alg_sock *ask = alg_sk(sk);
80	struct aead_ctx *ctx = ask->private;
81	struct aead_sg_list *sgl = &ctx->tsgl;
82	struct scatterlist *sg = sgl->sg;
83	unsigned int i;
84
85	for (i = 0; i < sgl->cur; i++) {
86		if (!sg_page(sg + i))
87			continue;
88
89		put_page(sg_page(sg + i));
90		sg_assign_page(sg + i, NULL);
91	}
92	sgl->cur = 0;
93	ctx->used = 0;
94	ctx->more = 0;
95	ctx->merge = 0;
96}
97
98static void aead_wmem_wakeup(struct sock *sk)
99{
100	struct socket_wq *wq;
101
102	if (!aead_writable(sk))
103		return;
104
105	rcu_read_lock();
106	wq = rcu_dereference(sk->sk_wq);
107	if (wq_has_sleeper(wq))
108		wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
109							   POLLRDNORM |
110							   POLLRDBAND);
111	sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
112	rcu_read_unlock();
113}
114
115static int aead_wait_for_data(struct sock *sk, unsigned flags)
116{
117	struct alg_sock *ask = alg_sk(sk);
118	struct aead_ctx *ctx = ask->private;
119	long timeout;
120	DEFINE_WAIT(wait);
121	int err = -ERESTARTSYS;
122
123	if (flags & MSG_DONTWAIT)
124		return -EAGAIN;
125
126	set_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
127
128	for (;;) {
129		if (signal_pending(current))
130			break;
131		prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
132		timeout = MAX_SCHEDULE_TIMEOUT;
133		if (sk_wait_event(sk, &timeout, !ctx->more)) {
134			err = 0;
135			break;
136		}
137	}
138	finish_wait(sk_sleep(sk), &wait);
139
140	clear_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
141
142	return err;
143}
144
145static void aead_data_wakeup(struct sock *sk)
146{
147	struct alg_sock *ask = alg_sk(sk);
148	struct aead_ctx *ctx = ask->private;
149	struct socket_wq *wq;
150
151	if (ctx->more)
152		return;
153	if (!ctx->used)
154		return;
155
156	rcu_read_lock();
157	wq = rcu_dereference(sk->sk_wq);
158	if (wq_has_sleeper(wq))
159		wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
160							   POLLRDNORM |
161							   POLLRDBAND);
162	sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
163	rcu_read_unlock();
164}
165
166static int aead_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
167{
168	struct sock *sk = sock->sk;
169	struct alg_sock *ask = alg_sk(sk);
170	struct aead_ctx *ctx = ask->private;
171	unsigned ivsize =
172		crypto_aead_ivsize(crypto_aead_reqtfm(&ctx->aead_req));
173	struct aead_sg_list *sgl = &ctx->tsgl;
174	struct af_alg_control con = {};
175	long copied = 0;
176	bool enc = 0;
177	bool init = 0;
178	int err = -EINVAL;
179
180	if (msg->msg_controllen) {
181		err = af_alg_cmsg_send(msg, &con);
182		if (err)
183			return err;
184
185		init = 1;
186		switch (con.op) {
187		case ALG_OP_ENCRYPT:
188			enc = 1;
189			break;
190		case ALG_OP_DECRYPT:
191			enc = 0;
192			break;
193		default:
194			return -EINVAL;
195		}
196
197		if (con.iv && con.iv->ivlen != ivsize)
198			return -EINVAL;
199	}
200
201	lock_sock(sk);
202	if (!ctx->more && ctx->used)
203		goto unlock;
204
205	if (init) {
206		ctx->enc = enc;
207		if (con.iv)
208			memcpy(ctx->iv, con.iv->iv, ivsize);
209
210		ctx->aead_assoclen = con.aead_assoclen;
211	}
212
213	while (size) {
214		unsigned long len = size;
215		struct scatterlist *sg = NULL;
216
217		/* use the existing memory in an allocated page */
218		if (ctx->merge) {
219			sg = sgl->sg + sgl->cur - 1;
220			len = min_t(unsigned long, len,
221				    PAGE_SIZE - sg->offset - sg->length);
222			err = memcpy_from_msg(page_address(sg_page(sg)) +
223					      sg->offset + sg->length,
224					      msg, len);
225			if (err)
226				goto unlock;
227
228			sg->length += len;
229			ctx->merge = (sg->offset + sg->length) &
230				     (PAGE_SIZE - 1);
231
232			ctx->used += len;
233			copied += len;
234			size -= len;
235			continue;
236		}
237
238		if (!aead_writable(sk)) {
239			/* user space sent too much data */
240			aead_put_sgl(sk);
241			err = -EMSGSIZE;
242			goto unlock;
243		}
244
245		/* allocate a new page */
246		len = min_t(unsigned long, size, aead_sndbuf(sk));
247		while (len) {
248			int plen = 0;
249
250			if (sgl->cur >= ALG_MAX_PAGES) {
251				aead_put_sgl(sk);
252				err = -E2BIG;
253				goto unlock;
254			}
255
256			sg = sgl->sg + sgl->cur;
257			plen = min_t(int, len, PAGE_SIZE);
258
259			sg_assign_page(sg, alloc_page(GFP_KERNEL));
260			err = -ENOMEM;
261			if (!sg_page(sg))
262				goto unlock;
263
264			err = memcpy_from_msg(page_address(sg_page(sg)),
265					      msg, plen);
266			if (err) {
267				__free_page(sg_page(sg));
268				sg_assign_page(sg, NULL);
269				goto unlock;
270			}
271
272			sg->offset = 0;
273			sg->length = plen;
274			len -= plen;
275			ctx->used += plen;
276			copied += plen;
277			sgl->cur++;
278			size -= plen;
279			ctx->merge = plen & (PAGE_SIZE - 1);
280		}
281	}
282
283	err = 0;
284
285	ctx->more = msg->msg_flags & MSG_MORE;
286	if (!ctx->more && !aead_sufficient_data(ctx)) {
287		aead_put_sgl(sk);
288		err = -EMSGSIZE;
289	}
290
291unlock:
292	aead_data_wakeup(sk);
293	release_sock(sk);
294
295	return err ?: copied;
296}
297
298static ssize_t aead_sendpage(struct socket *sock, struct page *page,
299			     int offset, size_t size, int flags)
300{
301	struct sock *sk = sock->sk;
302	struct alg_sock *ask = alg_sk(sk);
303	struct aead_ctx *ctx = ask->private;
304	struct aead_sg_list *sgl = &ctx->tsgl;
305	int err = -EINVAL;
306
307	if (flags & MSG_SENDPAGE_NOTLAST)
308		flags |= MSG_MORE;
309
310	if (sgl->cur >= ALG_MAX_PAGES)
311		return -E2BIG;
312
313	lock_sock(sk);
314	if (!ctx->more && ctx->used)
315		goto unlock;
316
317	if (!size)
318		goto done;
319
320	if (!aead_writable(sk)) {
321		/* user space sent too much data */
322		aead_put_sgl(sk);
323		err = -EMSGSIZE;
324		goto unlock;
325	}
326
327	ctx->merge = 0;
328
329	get_page(page);
330	sg_set_page(sgl->sg + sgl->cur, page, size, offset);
331	sgl->cur++;
332	ctx->used += size;
333
334	err = 0;
335
336done:
337	ctx->more = flags & MSG_MORE;
338	if (!ctx->more && !aead_sufficient_data(ctx)) {
339		aead_put_sgl(sk);
340		err = -EMSGSIZE;
341	}
342
343unlock:
344	aead_data_wakeup(sk);
345	release_sock(sk);
346
347	return err ?: size;
348}
349
350static int aead_recvmsg(struct socket *sock, struct msghdr *msg, size_t ignored, int flags)
351{
352	struct sock *sk = sock->sk;
353	struct alg_sock *ask = alg_sk(sk);
354	struct aead_ctx *ctx = ask->private;
355	unsigned bs = crypto_aead_blocksize(crypto_aead_reqtfm(&ctx->aead_req));
356	unsigned as = crypto_aead_authsize(crypto_aead_reqtfm(&ctx->aead_req));
357	struct aead_sg_list *sgl = &ctx->tsgl;
358	struct scatterlist *sg = NULL;
359	struct scatterlist assoc[ALG_MAX_PAGES];
360	size_t assoclen = 0;
361	unsigned int i = 0;
362	int err = -EINVAL;
363	unsigned long used = 0;
364	size_t outlen = 0;
365	size_t usedpages = 0;
366	unsigned int cnt = 0;
367
368	/* Limit number of IOV blocks to be accessed below */
369	if (msg->msg_iter.nr_segs > RSGL_MAX_ENTRIES)
370		return -ENOMSG;
371
372	lock_sock(sk);
373
374	/*
375	 * AEAD memory structure: For encryption, the tag is appended to the
376	 * ciphertext which implies that the memory allocated for the ciphertext
377	 * must be increased by the tag length. For decryption, the tag
378	 * is expected to be concatenated to the ciphertext. The plaintext
379	 * therefore has a memory size of the ciphertext minus the tag length.
380	 *
381	 * The memory structure for cipher operation has the following
382	 * structure:
383	 *	AEAD encryption input:  assoc data || plaintext
384	 *	AEAD encryption output: cipherntext || auth tag
385	 *	AEAD decryption input:  assoc data || ciphertext || auth tag
386	 *	AEAD decryption output: plaintext
387	 */
388
389	if (ctx->more) {
390		err = aead_wait_for_data(sk, flags);
391		if (err)
392			goto unlock;
393	}
394
395	used = ctx->used;
396
397	/*
398	 * Make sure sufficient data is present -- note, the same check is
399	 * is also present in sendmsg/sendpage. The checks in sendpage/sendmsg
400	 * shall provide an information to the data sender that something is
401	 * wrong, but they are irrelevant to maintain the kernel integrity.
402	 * We need this check here too in case user space decides to not honor
403	 * the error message in sendmsg/sendpage and still call recvmsg. This
404	 * check here protects the kernel integrity.
405	 */
406	if (!aead_sufficient_data(ctx))
407		goto unlock;
408
409	/*
410	 * The cipher operation input data is reduced by the associated data
411	 * length as this data is processed separately later on.
412	 */
413	used -= ctx->aead_assoclen;
414
415	if (ctx->enc) {
416		/* round up output buffer to multiple of block size */
417		outlen = ((used + bs - 1) / bs * bs);
418		/* add the size needed for the auth tag to be created */
419		outlen += as;
420	} else {
421		/* output data size is input without the authentication tag */
422		outlen = used - as;
423		/* round up output buffer to multiple of block size */
424		outlen = ((outlen + bs - 1) / bs * bs);
425	}
426
427	/* convert iovecs of output buffers into scatterlists */
428	while (iov_iter_count(&msg->msg_iter)) {
429		size_t seglen = min_t(size_t, iov_iter_count(&msg->msg_iter),
430				      (outlen - usedpages));
431
432		/* make one iovec available as scatterlist */
433		err = af_alg_make_sg(&ctx->rsgl[cnt], &msg->msg_iter,
434				     seglen);
435		if (err < 0)
436			goto unlock;
437		usedpages += err;
438		/* chain the new scatterlist with previous one */
439		if (cnt)
440			af_alg_link_sg(&ctx->rsgl[cnt-1], &ctx->rsgl[cnt]);
441
442		/* we do not need more iovecs as we have sufficient memory */
443		if (outlen <= usedpages)
444			break;
445		iov_iter_advance(&msg->msg_iter, err);
446		cnt++;
447	}
448
449	err = -EINVAL;
450	/* ensure output buffer is sufficiently large */
451	if (usedpages < outlen)
452		goto unlock;
453
454	sg_init_table(assoc, ALG_MAX_PAGES);
455	assoclen = ctx->aead_assoclen;
456	/*
457	 * Split scatterlist into two: first part becomes AD, second part
458	 * is plaintext / ciphertext. The first part is assigned to assoc
459	 * scatterlist. When this loop finishes, sg points to the start of the
460	 * plaintext / ciphertext.
461	 */
462	for (i = 0; i < ctx->tsgl.cur; i++) {
463		sg = sgl->sg + i;
464		if (sg->length <= assoclen) {
465			/* AD is larger than one page */
466			sg_set_page(assoc + i, sg_page(sg),
467				    sg->length, sg->offset);
468			assoclen -= sg->length;
469			if (i >= ctx->tsgl.cur)
470				goto unlock;
471		} else if (!assoclen) {
472			/* current page is to start of plaintext / ciphertext */
473			if (i)
474				/* AD terminates at page boundary */
475				sg_mark_end(assoc + i - 1);
476			else
477				/* AD size is zero */
478				sg_mark_end(assoc);
479			break;
480		} else {
481			/* AD does not terminate at page boundary */
482			sg_set_page(assoc + i, sg_page(sg),
483				    assoclen, sg->offset);
484			sg_mark_end(assoc + i);
485			/* plaintext / ciphertext starts after AD */
486			sg->length -= assoclen;
487			sg->offset += assoclen;
488			break;
489		}
490	}
491
492	aead_request_set_assoc(&ctx->aead_req, assoc, ctx->aead_assoclen);
493	aead_request_set_crypt(&ctx->aead_req, sg, ctx->rsgl[0].sg, used,
494			       ctx->iv);
495
496	err = af_alg_wait_for_completion(ctx->enc ?
497					 crypto_aead_encrypt(&ctx->aead_req) :
498					 crypto_aead_decrypt(&ctx->aead_req),
499					 &ctx->completion);
500
501	if (err) {
502		/* EBADMSG implies a valid cipher operation took place */
503		if (err == -EBADMSG)
504			aead_put_sgl(sk);
505		goto unlock;
506	}
507
508	aead_put_sgl(sk);
509
510	err = 0;
511
512unlock:
513	for (i = 0; i < cnt; i++)
514		af_alg_free_sg(&ctx->rsgl[i]);
515
516	aead_wmem_wakeup(sk);
517	release_sock(sk);
518
519	return err ? err : outlen;
520}
521
522static unsigned int aead_poll(struct file *file, struct socket *sock,
523			      poll_table *wait)
524{
525	struct sock *sk = sock->sk;
526	struct alg_sock *ask = alg_sk(sk);
527	struct aead_ctx *ctx = ask->private;
528	unsigned int mask;
529
530	sock_poll_wait(file, sk_sleep(sk), wait);
531	mask = 0;
532
533	if (!ctx->more)
534		mask |= POLLIN | POLLRDNORM;
535
536	if (aead_writable(sk))
537		mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
538
539	return mask;
540}
541
542static struct proto_ops algif_aead_ops = {
543	.family		=	PF_ALG,
544
545	.connect	=	sock_no_connect,
546	.socketpair	=	sock_no_socketpair,
547	.getname	=	sock_no_getname,
548	.ioctl		=	sock_no_ioctl,
549	.listen		=	sock_no_listen,
550	.shutdown	=	sock_no_shutdown,
551	.getsockopt	=	sock_no_getsockopt,
552	.mmap		=	sock_no_mmap,
553	.bind		=	sock_no_bind,
554	.accept		=	sock_no_accept,
555	.setsockopt	=	sock_no_setsockopt,
556
557	.release	=	af_alg_release,
558	.sendmsg	=	aead_sendmsg,
559	.sendpage	=	aead_sendpage,
560	.recvmsg	=	aead_recvmsg,
561	.poll		=	aead_poll,
562};
563
564static void *aead_bind(const char *name, u32 type, u32 mask)
565{
566	return crypto_alloc_aead(name, type, mask);
567}
568
569static void aead_release(void *private)
570{
571	crypto_free_aead(private);
572}
573
574static int aead_setauthsize(void *private, unsigned int authsize)
575{
576	return crypto_aead_setauthsize(private, authsize);
577}
578
579static int aead_setkey(void *private, const u8 *key, unsigned int keylen)
580{
581	return crypto_aead_setkey(private, key, keylen);
582}
583
584static void aead_sock_destruct(struct sock *sk)
585{
586	struct alg_sock *ask = alg_sk(sk);
587	struct aead_ctx *ctx = ask->private;
588	unsigned int ivlen = crypto_aead_ivsize(
589				crypto_aead_reqtfm(&ctx->aead_req));
590
591	aead_put_sgl(sk);
592	sock_kzfree_s(sk, ctx->iv, ivlen);
593	sock_kfree_s(sk, ctx, ctx->len);
594	af_alg_release_parent(sk);
595}
596
597static int aead_accept_parent(void *private, struct sock *sk)
598{
599	struct aead_ctx *ctx;
600	struct alg_sock *ask = alg_sk(sk);
601	unsigned int len = sizeof(*ctx) + crypto_aead_reqsize(private);
602	unsigned int ivlen = crypto_aead_ivsize(private);
603
604	ctx = sock_kmalloc(sk, len, GFP_KERNEL);
605	if (!ctx)
606		return -ENOMEM;
607	memset(ctx, 0, len);
608
609	ctx->iv = sock_kmalloc(sk, ivlen, GFP_KERNEL);
610	if (!ctx->iv) {
611		sock_kfree_s(sk, ctx, len);
612		return -ENOMEM;
613	}
614	memset(ctx->iv, 0, ivlen);
615
616	ctx->len = len;
617	ctx->used = 0;
618	ctx->more = 0;
619	ctx->merge = 0;
620	ctx->enc = 0;
621	ctx->tsgl.cur = 0;
622	ctx->aead_assoclen = 0;
623	af_alg_init_completion(&ctx->completion);
624	sg_init_table(ctx->tsgl.sg, ALG_MAX_PAGES);
625
626	ask->private = ctx;
627
628	aead_request_set_tfm(&ctx->aead_req, private);
629	aead_request_set_callback(&ctx->aead_req, CRYPTO_TFM_REQ_MAY_BACKLOG,
630				  af_alg_complete, &ctx->completion);
631
632	sk->sk_destruct = aead_sock_destruct;
633
634	return 0;
635}
636
637static const struct af_alg_type algif_type_aead = {
638	.bind		=	aead_bind,
639	.release	=	aead_release,
640	.setkey		=	aead_setkey,
641	.setauthsize	=	aead_setauthsize,
642	.accept		=	aead_accept_parent,
643	.ops		=	&algif_aead_ops,
644	.name		=	"aead",
645	.owner		=	THIS_MODULE
646};
647
648static int __init algif_aead_init(void)
649{
650	return af_alg_register_type(&algif_type_aead);
651}
652
653static void __exit algif_aead_exit(void)
654{
655	int err = af_alg_unregister_type(&algif_type_aead);
656	BUG_ON(err);
657}
658
659module_init(algif_aead_init);
660module_exit(algif_aead_exit);
661MODULE_LICENSE("GPL");
662MODULE_AUTHOR("Stephan Mueller <smueller@chronox.de>");
663MODULE_DESCRIPTION("AEAD kernel crypto API user space interface");
664