Commit | Line | Data |
---|---|---|
fe869cdb HX |
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 | ||
85529752 HX |
37 | struct algif_hash_tfm { |
38 | struct crypto_ahash *hash; | |
39 | bool has_key; | |
40 | }; | |
41 | ||
fe869cdb HX |
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) { | |
9250afa4 WR |
59 | err = af_alg_wait_for_completion(crypto_ahash_init(&ctx->req), |
60 | &ctx->completion); | |
fe869cdb HX |
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); | |
269230e7 HX |
77 | if (newlen < 0) { |
78 | err = copied ? 0 : newlen; | |
fe869cdb | 79 | goto unlock; |
269230e7 | 80 | } |
fe869cdb HX |
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 | ||
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 | ||
86a24344 SL |
123 | if (flags & MSG_SENDPAGE_NOTLAST) |
124 | flags |= MSG_MORE; | |
125 | ||
fe869cdb HX |
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); | |
9250afa4 | 140 | err = af_alg_wait_for_completion(err, &ctx->completion); |
fe869cdb HX |
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 | ||
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 | ||
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; | |
0907dbed | 198 | char state[crypto_ahash_statesize(crypto_ahash_reqtfm(req)) ? : 1]; |
fe869cdb HX |
199 | struct sock *sk2; |
200 | struct alg_sock *ask2; | |
201 | struct hash_ctx *ctx2; | |
daaf3fd9 | 202 | bool more; |
fe869cdb HX |
203 | int err; |
204 | ||
daaf3fd9 HX |
205 | lock_sock(sk); |
206 | more = ctx->more; | |
207 | err = more ? crypto_ahash_export(req, state) : 0; | |
208 | release_sock(sk); | |
209 | ||
fe869cdb HX |
210 | if (err) |
211 | return err; | |
212 | ||
213 | err = af_alg_accept(ask->parent, newsock); | |
214 | if (err) | |
215 | return err; | |
216 | ||
217 | sk2 = newsock->sk; | |
218 | ask2 = alg_sk(sk2); | |
219 | ctx2 = ask2->private; | |
daaf3fd9 HX |
220 | ctx2->more = more; |
221 | ||
222 | if (!more) | |
223 | return err; | |
fe869cdb HX |
224 | |
225 | err = crypto_ahash_import(&ctx2->req, state); | |
226 | if (err) { | |
227 | sock_orphan(sk2); | |
228 | sock_put(sk2); | |
229 | } | |
230 | ||
231 | return err; | |
232 | } | |
233 | ||
234 | static struct proto_ops algif_hash_ops = { | |
235 | .family = PF_ALG, | |
236 | ||
237 | .connect = sock_no_connect, | |
238 | .socketpair = sock_no_socketpair, | |
239 | .getname = sock_no_getname, | |
240 | .ioctl = sock_no_ioctl, | |
241 | .listen = sock_no_listen, | |
242 | .shutdown = sock_no_shutdown, | |
243 | .getsockopt = sock_no_getsockopt, | |
244 | .mmap = sock_no_mmap, | |
245 | .bind = sock_no_bind, | |
246 | .setsockopt = sock_no_setsockopt, | |
247 | .poll = sock_no_poll, | |
248 | ||
249 | .release = af_alg_release, | |
250 | .sendmsg = hash_sendmsg, | |
251 | .sendpage = hash_sendpage, | |
252 | .recvmsg = hash_recvmsg, | |
253 | .accept = hash_accept, | |
254 | }; | |
255 | ||
85529752 HX |
256 | static int hash_check_key(struct socket *sock) |
257 | { | |
54541dc3 | 258 | int err = 0; |
85529752 HX |
259 | struct sock *psk; |
260 | struct alg_sock *pask; | |
261 | struct algif_hash_tfm *tfm; | |
262 | struct sock *sk = sock->sk; | |
263 | struct alg_sock *ask = alg_sk(sk); | |
264 | ||
54541dc3 | 265 | lock_sock(sk); |
85529752 | 266 | if (ask->refcnt) |
54541dc3 | 267 | goto unlock_child; |
85529752 HX |
268 | |
269 | psk = ask->parent; | |
270 | pask = alg_sk(ask->parent); | |
271 | tfm = pask->private; | |
272 | ||
273 | err = -ENOKEY; | |
54541dc3 | 274 | lock_sock_nested(psk, SINGLE_DEPTH_NESTING); |
85529752 HX |
275 | if (!tfm->has_key) |
276 | goto unlock; | |
277 | ||
278 | if (!pask->refcnt++) | |
279 | sock_hold(psk); | |
280 | ||
281 | ask->refcnt = 1; | |
282 | sock_put(psk); | |
283 | ||
284 | err = 0; | |
285 | ||
286 | unlock: | |
287 | release_sock(psk); | |
54541dc3 HX |
288 | unlock_child: |
289 | release_sock(sk); | |
85529752 HX |
290 | |
291 | return err; | |
292 | } | |
293 | ||
294 | static int hash_sendmsg_nokey(struct kiocb *unused, struct socket *sock, | |
295 | struct msghdr *msg, size_t size) | |
296 | { | |
297 | int err; | |
298 | ||
299 | err = hash_check_key(sock); | |
300 | if (err) | |
301 | return err; | |
302 | ||
303 | return hash_sendmsg(unused, sock, msg, size); | |
304 | } | |
305 | ||
306 | static ssize_t hash_sendpage_nokey(struct socket *sock, struct page *page, | |
307 | int offset, size_t size, int flags) | |
308 | { | |
309 | int err; | |
310 | ||
311 | err = hash_check_key(sock); | |
312 | if (err) | |
313 | return err; | |
314 | ||
315 | return hash_sendpage(sock, page, offset, size, flags); | |
316 | } | |
317 | ||
318 | static int hash_recvmsg_nokey(struct kiocb *unused, struct socket *sock, | |
319 | struct msghdr *msg, size_t ignored, int flags) | |
320 | { | |
321 | int err; | |
322 | ||
323 | err = hash_check_key(sock); | |
324 | if (err) | |
325 | return err; | |
326 | ||
327 | return hash_recvmsg(unused, sock, msg, ignored, flags); | |
328 | } | |
329 | ||
330 | static int hash_accept_nokey(struct socket *sock, struct socket *newsock, | |
331 | int flags) | |
332 | { | |
333 | int err; | |
334 | ||
335 | err = hash_check_key(sock); | |
336 | if (err) | |
337 | return err; | |
338 | ||
339 | return hash_accept(sock, newsock, flags); | |
340 | } | |
341 | ||
342 | static struct proto_ops algif_hash_ops_nokey = { | |
343 | .family = PF_ALG, | |
344 | ||
345 | .connect = sock_no_connect, | |
346 | .socketpair = sock_no_socketpair, | |
347 | .getname = sock_no_getname, | |
348 | .ioctl = sock_no_ioctl, | |
349 | .listen = sock_no_listen, | |
350 | .shutdown = sock_no_shutdown, | |
351 | .getsockopt = sock_no_getsockopt, | |
352 | .mmap = sock_no_mmap, | |
353 | .bind = sock_no_bind, | |
354 | .setsockopt = sock_no_setsockopt, | |
355 | .poll = sock_no_poll, | |
356 | ||
357 | .release = af_alg_release, | |
358 | .sendmsg = hash_sendmsg_nokey, | |
359 | .sendpage = hash_sendpage_nokey, | |
360 | .recvmsg = hash_recvmsg_nokey, | |
361 | .accept = hash_accept_nokey, | |
362 | }; | |
363 | ||
fe869cdb HX |
364 | static void *hash_bind(const char *name, u32 type, u32 mask) |
365 | { | |
85529752 HX |
366 | struct algif_hash_tfm *tfm; |
367 | struct crypto_ahash *hash; | |
368 | ||
369 | tfm = kzalloc(sizeof(*tfm), GFP_KERNEL); | |
370 | if (!tfm) | |
371 | return ERR_PTR(-ENOMEM); | |
372 | ||
373 | hash = crypto_alloc_ahash(name, type, mask); | |
374 | if (IS_ERR(hash)) { | |
375 | kfree(tfm); | |
376 | return ERR_CAST(hash); | |
377 | } | |
378 | ||
379 | tfm->hash = hash; | |
380 | ||
381 | return tfm; | |
fe869cdb HX |
382 | } |
383 | ||
384 | static void hash_release(void *private) | |
385 | { | |
85529752 HX |
386 | struct algif_hash_tfm *tfm = private; |
387 | ||
388 | crypto_free_ahash(tfm->hash); | |
389 | kfree(tfm); | |
fe869cdb HX |
390 | } |
391 | ||
392 | static int hash_setkey(void *private, const u8 *key, unsigned int keylen) | |
393 | { | |
85529752 HX |
394 | struct algif_hash_tfm *tfm = private; |
395 | int err; | |
396 | ||
397 | err = crypto_ahash_setkey(tfm->hash, key, keylen); | |
398 | tfm->has_key = !err; | |
399 | ||
400 | return err; | |
fe869cdb HX |
401 | } |
402 | ||
023fe7ba | 403 | static void hash_sock_destruct(struct sock *sk) |
fe869cdb HX |
404 | { |
405 | struct alg_sock *ask = alg_sk(sk); | |
406 | struct hash_ctx *ctx = ask->private; | |
407 | ||
408 | sock_kfree_s(sk, ctx->result, | |
409 | crypto_ahash_digestsize(crypto_ahash_reqtfm(&ctx->req))); | |
410 | sock_kfree_s(sk, ctx, ctx->len); | |
85529752 HX |
411 | af_alg_release_parent(sk); |
412 | } | |
413 | ||
023fe7ba | 414 | static int hash_accept_parent_nokey(void *private, struct sock *sk) |
fe869cdb HX |
415 | { |
416 | struct hash_ctx *ctx; | |
417 | struct alg_sock *ask = alg_sk(sk); | |
85529752 HX |
418 | struct algif_hash_tfm *tfm = private; |
419 | struct crypto_ahash *hash = tfm->hash; | |
420 | unsigned len = sizeof(*ctx) + crypto_ahash_reqsize(hash); | |
421 | unsigned ds = crypto_ahash_digestsize(hash); | |
fe869cdb HX |
422 | |
423 | ctx = sock_kmalloc(sk, len, GFP_KERNEL); | |
424 | if (!ctx) | |
425 | return -ENOMEM; | |
426 | ||
427 | ctx->result = sock_kmalloc(sk, ds, GFP_KERNEL); | |
428 | if (!ctx->result) { | |
429 | sock_kfree_s(sk, ctx, len); | |
430 | return -ENOMEM; | |
431 | } | |
432 | ||
433 | memset(ctx->result, 0, ds); | |
434 | ||
435 | ctx->len = len; | |
436 | ctx->more = 0; | |
437 | af_alg_init_completion(&ctx->completion); | |
438 | ||
439 | ask->private = ctx; | |
440 | ||
85529752 | 441 | ahash_request_set_tfm(&ctx->req, hash); |
fe869cdb HX |
442 | ahash_request_set_callback(&ctx->req, CRYPTO_TFM_REQ_MAY_BACKLOG, |
443 | af_alg_complete, &ctx->completion); | |
444 | ||
445 | sk->sk_destruct = hash_sock_destruct; | |
446 | ||
447 | return 0; | |
448 | } | |
449 | ||
85529752 HX |
450 | static int hash_accept_parent(void *private, struct sock *sk) |
451 | { | |
452 | struct algif_hash_tfm *tfm = private; | |
453 | ||
454 | if (!tfm->has_key && crypto_ahash_has_setkey(tfm->hash)) | |
455 | return -ENOKEY; | |
456 | ||
023fe7ba | 457 | return hash_accept_parent_nokey(private, sk); |
85529752 HX |
458 | } |
459 | ||
fe869cdb HX |
460 | static const struct af_alg_type algif_type_hash = { |
461 | .bind = hash_bind, | |
462 | .release = hash_release, | |
463 | .setkey = hash_setkey, | |
464 | .accept = hash_accept_parent, | |
85529752 | 465 | .accept_nokey = hash_accept_parent_nokey, |
fe869cdb | 466 | .ops = &algif_hash_ops, |
85529752 | 467 | .ops_nokey = &algif_hash_ops_nokey, |
fe869cdb HX |
468 | .name = "hash", |
469 | .owner = THIS_MODULE | |
470 | }; | |
471 | ||
472 | static int __init algif_hash_init(void) | |
473 | { | |
474 | return af_alg_register_type(&algif_type_hash); | |
475 | } | |
476 | ||
477 | static void __exit algif_hash_exit(void) | |
478 | { | |
479 | int err = af_alg_unregister_type(&algif_type_hash); | |
480 | BUG_ON(err); | |
481 | } | |
482 | ||
483 | module_init(algif_hash_init); | |
484 | module_exit(algif_hash_exit); | |
485 | MODULE_LICENSE("GPL"); |