of: fdt: Fix return with value in void function
[linux/fpc-iii.git] / crypto / algif_hash.c
blobd11d431251f79b6d8d516cc5d593ee229ae45279
1 /*
2 * algif_hash: User-space interface for hash algorithms
4 * This file provides the user-space API for hash algorithms.
6 * Copyright (c) 2010 Herbert Xu <herbert@gondor.apana.org.au>
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.
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>
24 struct hash_ctx {
25 struct af_alg_sgl sgl;
27 u8 *result;
29 struct af_alg_completion completion;
31 unsigned int len;
32 bool more;
34 struct ahash_request req;
37 struct algif_hash_tfm {
38 struct crypto_ahash *hash;
39 bool has_key;
42 static int hash_sendmsg(struct kiocb *unused, struct socket *sock,
43 struct msghdr *msg, size_t ignored)
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;
54 if (limit > sk->sk_sndbuf)
55 limit = sk->sk_sndbuf;
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;
65 ctx->more = 0;
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;
72 while (seglen) {
73 int len = min_t(unsigned long, seglen, limit);
74 int newlen;
76 newlen = af_alg_make_sg(&ctx->sgl, from, len, 0);
77 if (newlen < 0) {
78 err = copied ? 0 : newlen;
79 goto unlock;
82 ahash_request_set_crypt(&ctx->req, ctx->sgl.sg, NULL,
83 newlen);
85 err = af_alg_wait_for_completion(
86 crypto_ahash_update(&ctx->req),
87 &ctx->completion);
89 af_alg_free_sg(&ctx->sgl);
91 if (err)
92 goto unlock;
94 seglen -= newlen;
95 from += newlen;
96 copied += newlen;
100 err = 0;
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);
109 unlock:
110 release_sock(sk);
112 return err ?: copied;
115 static ssize_t hash_sendpage(struct socket *sock, struct page *page,
116 int offset, size_t size, int flags)
118 struct sock *sk = sock->sk;
119 struct alg_sock *ask = alg_sk(sk);
120 struct hash_ctx *ctx = ask->private;
121 int err;
123 if (flags & MSG_SENDPAGE_NOTLAST)
124 flags |= MSG_MORE;
126 lock_sock(sk);
127 sg_init_table(ctx->sgl.sg, 1);
128 sg_set_page(ctx->sgl.sg, page, size, offset);
130 ahash_request_set_crypt(&ctx->req, ctx->sgl.sg, ctx->result, size);
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;
145 err = crypto_ahash_update(&ctx->req);
148 err = af_alg_wait_for_completion(err, &ctx->completion);
149 if (err)
150 goto unlock;
152 ctx->more = flags & MSG_MORE;
154 unlock:
155 release_sock(sk);
157 return err ?: size;
160 static int hash_recvmsg(struct kiocb *unused, struct socket *sock,
161 struct msghdr *msg, size_t len, int flags)
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;
169 if (len > ds)
170 len = ds;
171 else if (len < ds)
172 msg->msg_flags |= MSG_TRUNC;
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;
184 err = memcpy_toiovec(msg->msg_iov, ctx->result, len);
186 unlock:
187 release_sock(sk);
189 return err ?: len;
192 static int hash_accept(struct socket *sock, struct socket *newsock, int flags)
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))];
199 struct sock *sk2;
200 struct alg_sock *ask2;
201 struct hash_ctx *ctx2;
202 bool more;
203 int err;
205 lock_sock(sk);
206 more = ctx->more;
207 err = more ? crypto_ahash_export(req, state) : 0;
208 release_sock(sk);
210 if (err)
211 return err;
213 err = af_alg_accept(ask->parent, newsock);
214 if (err)
215 return err;
217 sk2 = newsock->sk;
218 ask2 = alg_sk(sk2);
219 ctx2 = ask2->private;
220 ctx2->more = more;
222 if (!more)
223 return err;
225 err = crypto_ahash_import(&ctx2->req, state);
226 if (err) {
227 sock_orphan(sk2);
228 sock_put(sk2);
231 return err;
234 static struct proto_ops algif_hash_ops = {
235 .family = PF_ALG,
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,
249 .release = af_alg_release,
250 .sendmsg = hash_sendmsg,
251 .sendpage = hash_sendpage,
252 .recvmsg = hash_recvmsg,
253 .accept = hash_accept,
256 static int hash_check_key(struct socket *sock)
258 int err = 0;
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);
265 lock_sock(sk);
266 if (ask->refcnt)
267 goto unlock_child;
269 psk = ask->parent;
270 pask = alg_sk(ask->parent);
271 tfm = pask->private;
273 err = -ENOKEY;
274 lock_sock_nested(psk, SINGLE_DEPTH_NESTING);
275 if (!tfm->has_key)
276 goto unlock;
278 if (!pask->refcnt++)
279 sock_hold(psk);
281 ask->refcnt = 1;
282 sock_put(psk);
284 err = 0;
286 unlock:
287 release_sock(psk);
288 unlock_child:
289 release_sock(sk);
291 return err;
294 static int hash_sendmsg_nokey(struct kiocb *unused, struct socket *sock,
295 struct msghdr *msg, size_t size)
297 int err;
299 err = hash_check_key(sock);
300 if (err)
301 return err;
303 return hash_sendmsg(unused, sock, msg, size);
306 static ssize_t hash_sendpage_nokey(struct socket *sock, struct page *page,
307 int offset, size_t size, int flags)
309 int err;
311 err = hash_check_key(sock);
312 if (err)
313 return err;
315 return hash_sendpage(sock, page, offset, size, flags);
318 static int hash_recvmsg_nokey(struct kiocb *unused, struct socket *sock,
319 struct msghdr *msg, size_t ignored, int flags)
321 int err;
323 err = hash_check_key(sock);
324 if (err)
325 return err;
327 return hash_recvmsg(unused, sock, msg, ignored, flags);
330 static int hash_accept_nokey(struct socket *sock, struct socket *newsock,
331 int flags)
333 int err;
335 err = hash_check_key(sock);
336 if (err)
337 return err;
339 return hash_accept(sock, newsock, flags);
342 static struct proto_ops algif_hash_ops_nokey = {
343 .family = PF_ALG,
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,
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,
364 static void *hash_bind(const char *name, u32 type, u32 mask)
366 struct algif_hash_tfm *tfm;
367 struct crypto_ahash *hash;
369 tfm = kzalloc(sizeof(*tfm), GFP_KERNEL);
370 if (!tfm)
371 return ERR_PTR(-ENOMEM);
373 hash = crypto_alloc_ahash(name, type, mask);
374 if (IS_ERR(hash)) {
375 kfree(tfm);
376 return ERR_CAST(hash);
379 tfm->hash = hash;
381 return tfm;
384 static void hash_release(void *private)
386 struct algif_hash_tfm *tfm = private;
388 crypto_free_ahash(tfm->hash);
389 kfree(tfm);
392 static int hash_setkey(void *private, const u8 *key, unsigned int keylen)
394 struct algif_hash_tfm *tfm = private;
395 int err;
397 err = crypto_ahash_setkey(tfm->hash, key, keylen);
398 tfm->has_key = !err;
400 return err;
403 static void hash_sock_destruct(struct sock *sk)
405 struct alg_sock *ask = alg_sk(sk);
406 struct hash_ctx *ctx = ask->private;
408 sock_kfree_s(sk, ctx->result,
409 crypto_ahash_digestsize(crypto_ahash_reqtfm(&ctx->req)));
410 sock_kfree_s(sk, ctx, ctx->len);
411 af_alg_release_parent(sk);
414 static int hash_accept_parent_nokey(void *private, struct sock *sk)
416 struct hash_ctx *ctx;
417 struct alg_sock *ask = alg_sk(sk);
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);
423 ctx = sock_kmalloc(sk, len, GFP_KERNEL);
424 if (!ctx)
425 return -ENOMEM;
427 ctx->result = sock_kmalloc(sk, ds, GFP_KERNEL);
428 if (!ctx->result) {
429 sock_kfree_s(sk, ctx, len);
430 return -ENOMEM;
433 memset(ctx->result, 0, ds);
435 ctx->len = len;
436 ctx->more = 0;
437 af_alg_init_completion(&ctx->completion);
439 ask->private = ctx;
441 ahash_request_set_tfm(&ctx->req, hash);
442 ahash_request_set_callback(&ctx->req, CRYPTO_TFM_REQ_MAY_BACKLOG,
443 af_alg_complete, &ctx->completion);
445 sk->sk_destruct = hash_sock_destruct;
447 return 0;
450 static int hash_accept_parent(void *private, struct sock *sk)
452 struct algif_hash_tfm *tfm = private;
454 if (!tfm->has_key && crypto_ahash_has_setkey(tfm->hash))
455 return -ENOKEY;
457 return hash_accept_parent_nokey(private, sk);
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,
465 .accept_nokey = hash_accept_parent_nokey,
466 .ops = &algif_hash_ops,
467 .ops_nokey = &algif_hash_ops_nokey,
468 .name = "hash",
469 .owner = THIS_MODULE
472 static int __init algif_hash_init(void)
474 return af_alg_register_type(&algif_type_hash);
477 static void __exit algif_hash_exit(void)
479 int err = af_alg_unregister_type(&algif_type_hash);
480 BUG_ON(err);
483 module_init(algif_hash_init);
484 module_exit(algif_hash_exit);
485 MODULE_LICENSE("GPL");