• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * algif_hash: User-space interface for hash algorithms
3  *
4  * This file provides the user-space API for hash algorithms.
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/hash.h>
16 #include <crypto/if_alg.h>
17 #include <linux/init.h>
18 #include <linux/kernel.h>
19 #include <linux/mm.h>
20 #include <linux/module.h>
21 #include <linux/net.h>
22 #include <net/sock.h>
23 
24 struct hash_ctx {
25 	struct af_alg_sgl sgl;
26 
27 	u8 *result;
28 
29 	struct af_alg_completion completion;
30 
31 	unsigned int len;
32 	bool more;
33 
34 	struct ahash_request req;
35 };
36 
37 struct algif_hash_tfm {
38 	struct crypto_ahash *hash;
39 	bool has_key;
40 };
41 
hash_sendmsg(struct kiocb * unused,struct socket * sock,struct msghdr * msg,size_t ignored)42 static int hash_sendmsg(struct kiocb *unused, struct socket *sock,
43 			struct msghdr *msg, size_t ignored)
44 {
45 	int limit = ALG_MAX_PAGES * PAGE_SIZE;
46 	struct sock *sk = sock->sk;
47 	struct alg_sock *ask = alg_sk(sk);
48 	struct hash_ctx *ctx = ask->private;
49 	unsigned long iovlen;
50 	struct iovec *iov;
51 	long copied = 0;
52 	int err;
53 
54 	if (limit > sk->sk_sndbuf)
55 		limit = sk->sk_sndbuf;
56 
57 	lock_sock(sk);
58 	if (!ctx->more) {
59 		err = af_alg_wait_for_completion(crypto_ahash_init(&ctx->req),
60 						&ctx->completion);
61 		if (err)
62 			goto unlock;
63 	}
64 
65 	ctx->more = 0;
66 
67 	for (iov = msg->msg_iov, iovlen = msg->msg_iovlen; iovlen > 0;
68 	     iovlen--, iov++) {
69 		unsigned long seglen = iov->iov_len;
70 		char __user *from = iov->iov_base;
71 
72 		while (seglen) {
73 			int len = min_t(unsigned long, seglen, limit);
74 			int newlen;
75 
76 			newlen = af_alg_make_sg(&ctx->sgl, from, len, 0);
77 			if (newlen < 0) {
78 				err = copied ? 0 : newlen;
79 				goto unlock;
80 			}
81 
82 			ahash_request_set_crypt(&ctx->req, ctx->sgl.sg, NULL,
83 						newlen);
84 
85 			err = af_alg_wait_for_completion(
86 				crypto_ahash_update(&ctx->req),
87 				&ctx->completion);
88 
89 			af_alg_free_sg(&ctx->sgl);
90 
91 			if (err)
92 				goto unlock;
93 
94 			seglen -= newlen;
95 			from += newlen;
96 			copied += newlen;
97 		}
98 	}
99 
100 	err = 0;
101 
102 	ctx->more = msg->msg_flags & MSG_MORE;
103 	if (!ctx->more) {
104 		ahash_request_set_crypt(&ctx->req, NULL, ctx->result, 0);
105 		err = af_alg_wait_for_completion(crypto_ahash_final(&ctx->req),
106 						 &ctx->completion);
107 	}
108 
109 unlock:
110 	release_sock(sk);
111 
112 	return err ?: copied;
113 }
114 
hash_sendpage(struct socket * sock,struct page * page,int offset,size_t size,int flags)115 static ssize_t hash_sendpage(struct socket *sock, struct page *page,
116 			     int offset, size_t size, int flags)
117 {
118 	struct sock *sk = sock->sk;
119 	struct alg_sock *ask = alg_sk(sk);
120 	struct hash_ctx *ctx = ask->private;
121 	int err;
122 
123 	if (flags & MSG_SENDPAGE_NOTLAST)
124 		flags |= MSG_MORE;
125 
126 	lock_sock(sk);
127 	sg_init_table(ctx->sgl.sg, 1);
128 	sg_set_page(ctx->sgl.sg, page, size, offset);
129 
130 	ahash_request_set_crypt(&ctx->req, ctx->sgl.sg, ctx->result, size);
131 
132 	if (!(flags & MSG_MORE)) {
133 		if (ctx->more)
134 			err = crypto_ahash_finup(&ctx->req);
135 		else
136 			err = crypto_ahash_digest(&ctx->req);
137 	} else {
138 		if (!ctx->more) {
139 			err = crypto_ahash_init(&ctx->req);
140 			err = af_alg_wait_for_completion(err, &ctx->completion);
141 			if (err)
142 				goto unlock;
143 		}
144 
145 		err = crypto_ahash_update(&ctx->req);
146 	}
147 
148 	err = af_alg_wait_for_completion(err, &ctx->completion);
149 	if (err)
150 		goto unlock;
151 
152 	ctx->more = flags & MSG_MORE;
153 
154 unlock:
155 	release_sock(sk);
156 
157 	return err ?: size;
158 }
159 
hash_recvmsg(struct kiocb * unused,struct socket * sock,struct msghdr * msg,size_t len,int flags)160 static int hash_recvmsg(struct kiocb *unused, struct socket *sock,
161 			struct msghdr *msg, size_t len, int flags)
162 {
163 	struct sock *sk = sock->sk;
164 	struct alg_sock *ask = alg_sk(sk);
165 	struct hash_ctx *ctx = ask->private;
166 	unsigned ds = crypto_ahash_digestsize(crypto_ahash_reqtfm(&ctx->req));
167 	int err;
168 
169 	if (len > ds)
170 		len = ds;
171 	else if (len < ds)
172 		msg->msg_flags |= MSG_TRUNC;
173 
174 	lock_sock(sk);
175 	if (ctx->more) {
176 		ctx->more = 0;
177 		ahash_request_set_crypt(&ctx->req, NULL, ctx->result, 0);
178 		err = af_alg_wait_for_completion(crypto_ahash_final(&ctx->req),
179 						 &ctx->completion);
180 		if (err)
181 			goto unlock;
182 	}
183 
184 	err = memcpy_toiovec(msg->msg_iov, ctx->result, len);
185 
186 unlock:
187 	release_sock(sk);
188 
189 	return err ?: len;
190 }
191 
hash_accept(struct socket * sock,struct socket * newsock,int flags)192 static int hash_accept(struct socket *sock, struct socket *newsock, int flags)
193 {
194 	struct sock *sk = sock->sk;
195 	struct alg_sock *ask = alg_sk(sk);
196 	struct hash_ctx *ctx = ask->private;
197 	struct ahash_request *req = &ctx->req;
198 	char state[crypto_ahash_statesize(crypto_ahash_reqtfm(req)) ? : 1];
199 	struct sock *sk2;
200 	struct alg_sock *ask2;
201 	struct hash_ctx *ctx2;
202 	int err;
203 
204 	err = crypto_ahash_export(req, state);
205 	if (err)
206 		return err;
207 
208 	err = af_alg_accept(ask->parent, newsock);
209 	if (err)
210 		return err;
211 
212 	sk2 = newsock->sk;
213 	ask2 = alg_sk(sk2);
214 	ctx2 = ask2->private;
215 	ctx2->more = 1;
216 
217 	err = crypto_ahash_import(&ctx2->req, state);
218 	if (err) {
219 		sock_orphan(sk2);
220 		sock_put(sk2);
221 	}
222 
223 	return err;
224 }
225 
226 static struct proto_ops algif_hash_ops = {
227 	.family		=	PF_ALG,
228 
229 	.connect	=	sock_no_connect,
230 	.socketpair	=	sock_no_socketpair,
231 	.getname	=	sock_no_getname,
232 	.ioctl		=	sock_no_ioctl,
233 	.listen		=	sock_no_listen,
234 	.shutdown	=	sock_no_shutdown,
235 	.getsockopt	=	sock_no_getsockopt,
236 	.mmap		=	sock_no_mmap,
237 	.bind		=	sock_no_bind,
238 	.setsockopt	=	sock_no_setsockopt,
239 	.poll		=	sock_no_poll,
240 
241 	.release	=	af_alg_release,
242 	.sendmsg	=	hash_sendmsg,
243 	.sendpage	=	hash_sendpage,
244 	.recvmsg	=	hash_recvmsg,
245 	.accept		=	hash_accept,
246 };
247 
hash_check_key(struct socket * sock)248 static int hash_check_key(struct socket *sock)
249 {
250 	int err = 0;
251 	struct sock *psk;
252 	struct alg_sock *pask;
253 	struct algif_hash_tfm *tfm;
254 	struct sock *sk = sock->sk;
255 	struct alg_sock *ask = alg_sk(sk);
256 
257 	lock_sock(sk);
258 	if (ask->refcnt)
259 		goto unlock_child;
260 
261 	psk = ask->parent;
262 	pask = alg_sk(ask->parent);
263 	tfm = pask->private;
264 
265 	err = -ENOKEY;
266 	lock_sock_nested(psk, SINGLE_DEPTH_NESTING);
267 	if (!tfm->has_key)
268 		goto unlock;
269 
270 	if (!pask->refcnt++)
271 		sock_hold(psk);
272 
273 	ask->refcnt = 1;
274 	sock_put(psk);
275 
276 	err = 0;
277 
278 unlock:
279 	release_sock(psk);
280 unlock_child:
281 	release_sock(sk);
282 
283 	return err;
284 }
285 
hash_sendmsg_nokey(struct kiocb * unused,struct socket * sock,struct msghdr * msg,size_t size)286 static int hash_sendmsg_nokey(struct kiocb *unused, struct socket *sock,
287 			      struct msghdr *msg, size_t size)
288 {
289 	int err;
290 
291 	err = hash_check_key(sock);
292 	if (err)
293 		return err;
294 
295 	return hash_sendmsg(NULL, sock, msg, size);
296 }
297 
hash_sendpage_nokey(struct socket * sock,struct page * page,int offset,size_t size,int flags)298 static ssize_t hash_sendpage_nokey(struct socket *sock, struct page *page,
299 				   int offset, size_t size, int flags)
300 {
301 	int err;
302 
303 	err = hash_check_key(sock);
304 	if (err)
305 		return err;
306 
307 	return hash_sendpage(sock, page, offset, size, flags);
308 }
309 
hash_recvmsg_nokey(struct kiocb * unused,struct socket * sock,struct msghdr * msg,size_t ignored,int flags)310 static int hash_recvmsg_nokey(struct kiocb *unused, struct socket *sock,
311 			      struct msghdr *msg, size_t ignored, int flags)
312 {
313 	int err;
314 
315 	err = hash_check_key(sock);
316 	if (err)
317 		return err;
318 
319 	return hash_recvmsg(NULL, sock, msg, ignored, flags);
320 }
321 
hash_accept_nokey(struct socket * sock,struct socket * newsock,int flags)322 static int hash_accept_nokey(struct socket *sock, struct socket *newsock,
323 			     int flags)
324 {
325 	int err;
326 
327 	err = hash_check_key(sock);
328 	if (err)
329 		return err;
330 
331 	return hash_accept(sock, newsock, flags);
332 }
333 
334 static struct proto_ops algif_hash_ops_nokey = {
335 	.family		=	PF_ALG,
336 
337 	.connect	=	sock_no_connect,
338 	.socketpair	=	sock_no_socketpair,
339 	.getname	=	sock_no_getname,
340 	.ioctl		=	sock_no_ioctl,
341 	.listen		=	sock_no_listen,
342 	.shutdown	=	sock_no_shutdown,
343 	.getsockopt	=	sock_no_getsockopt,
344 	.mmap		=	sock_no_mmap,
345 	.bind		=	sock_no_bind,
346 	.setsockopt	=	sock_no_setsockopt,
347 	.poll		=	sock_no_poll,
348 
349 	.release	=	af_alg_release,
350 	.sendmsg	=	hash_sendmsg_nokey,
351 	.sendpage	=	hash_sendpage_nokey,
352 	.recvmsg	=	hash_recvmsg_nokey,
353 	.accept		=	hash_accept_nokey,
354 };
355 
hash_bind(const char * name,u32 type,u32 mask)356 static void *hash_bind(const char *name, u32 type, u32 mask)
357 {
358 	struct algif_hash_tfm *tfm;
359 	struct crypto_ahash *hash;
360 
361 	tfm = kzalloc(sizeof(*tfm), GFP_KERNEL);
362 	if (!tfm)
363 		return ERR_PTR(-ENOMEM);
364 
365 	hash = crypto_alloc_ahash(name, type, mask);
366 	if (IS_ERR(hash)) {
367 		kfree(tfm);
368 		return ERR_CAST(hash);
369 	}
370 
371 	tfm->hash = hash;
372 
373 	return tfm;
374 }
375 
hash_release(void * private)376 static void hash_release(void *private)
377 {
378 	struct algif_hash_tfm *tfm = private;
379 
380 	crypto_free_ahash(tfm->hash);
381 	kfree(tfm);
382 }
383 
hash_setkey(void * private,const u8 * key,unsigned int keylen)384 static int hash_setkey(void *private, const u8 *key, unsigned int keylen)
385 {
386 	struct algif_hash_tfm *tfm = private;
387 	int err;
388 
389 	err = crypto_ahash_setkey(tfm->hash, key, keylen);
390 	tfm->has_key = !err;
391 
392 	return err;
393 }
394 
hash_sock_destruct(struct sock * sk)395 static void hash_sock_destruct(struct sock *sk)
396 {
397 	struct alg_sock *ask = alg_sk(sk);
398 	struct hash_ctx *ctx = ask->private;
399 
400 	sock_kfree_s(sk, ctx->result,
401 		     crypto_ahash_digestsize(crypto_ahash_reqtfm(&ctx->req)));
402 	sock_kfree_s(sk, ctx, ctx->len);
403 	af_alg_release_parent(sk);
404 }
405 
hash_accept_parent_nokey(void * private,struct sock * sk)406 static int hash_accept_parent_nokey(void *private, struct sock *sk)
407 {
408 	struct hash_ctx *ctx;
409 	struct alg_sock *ask = alg_sk(sk);
410 	struct algif_hash_tfm *tfm = private;
411 	struct crypto_ahash *hash = tfm->hash;
412 	unsigned len = sizeof(*ctx) + crypto_ahash_reqsize(hash);
413 	unsigned ds = crypto_ahash_digestsize(hash);
414 
415 	ctx = sock_kmalloc(sk, len, GFP_KERNEL);
416 	if (!ctx)
417 		return -ENOMEM;
418 
419 	ctx->result = sock_kmalloc(sk, ds, GFP_KERNEL);
420 	if (!ctx->result) {
421 		sock_kfree_s(sk, ctx, len);
422 		return -ENOMEM;
423 	}
424 
425 	memset(ctx->result, 0, ds);
426 
427 	ctx->len = len;
428 	ctx->more = 0;
429 	af_alg_init_completion(&ctx->completion);
430 
431 	ask->private = ctx;
432 
433 	ahash_request_set_tfm(&ctx->req, hash);
434 	ahash_request_set_callback(&ctx->req, CRYPTO_TFM_REQ_MAY_BACKLOG,
435 				   af_alg_complete, &ctx->completion);
436 
437 	sk->sk_destruct = hash_sock_destruct;
438 
439 	return 0;
440 }
441 
hash_accept_parent(void * private,struct sock * sk)442 static int hash_accept_parent(void *private, struct sock *sk)
443 {
444 	struct algif_hash_tfm *tfm = private;
445 
446 	if (!tfm->has_key && crypto_ahash_has_setkey(tfm->hash))
447 		return -ENOKEY;
448 
449 	return hash_accept_parent_nokey(private, sk);
450 }
451 
452 static const struct af_alg_type algif_type_hash = {
453 	.bind		=	hash_bind,
454 	.release	=	hash_release,
455 	.setkey		=	hash_setkey,
456 	.accept		=	hash_accept_parent,
457 	.accept_nokey	=	hash_accept_parent_nokey,
458 	.ops		=	&algif_hash_ops,
459 	.ops_nokey	=	&algif_hash_ops_nokey,
460 	.name		=	"hash",
461 	.owner		=	THIS_MODULE
462 };
463 
algif_hash_init(void)464 static int __init algif_hash_init(void)
465 {
466 	return af_alg_register_type(&algif_type_hash);
467 }
468 
algif_hash_exit(void)469 static void __exit algif_hash_exit(void)
470 {
471 	int err = af_alg_unregister_type(&algif_type_hash);
472 	BUG_ON(err);
473 }
474 
475 module_init(algif_hash_init);
476 module_exit(algif_hash_exit);
477 MODULE_LICENSE("GPL");
478