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)
15 #include <crypto/hash.h>
16 #include <crypto/if_alg.h>
17 #include <linux/init.h>
18 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/net.h>
25 struct af_alg_sgl sgl
;
29 struct af_alg_completion completion
;
34 struct ahash_request req
;
37 struct algif_hash_tfm
{
38 struct crypto_ahash
*hash
;
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;
54 if (limit
> sk
->sk_sndbuf
)
55 limit
= sk
->sk_sndbuf
;
59 err
= af_alg_wait_for_completion(crypto_ahash_init(&ctx
->req
),
67 for (iov
= msg
->msg_iov
, iovlen
= msg
->msg_iovlen
; iovlen
> 0;
69 unsigned long seglen
= iov
->iov_len
;
70 char __user
*from
= iov
->iov_base
;
73 int len
= min_t(unsigned long, seglen
, limit
);
76 newlen
= af_alg_make_sg(&ctx
->sgl
, from
, len
, 0);
78 err
= copied
? 0 : newlen
;
82 ahash_request_set_crypt(&ctx
->req
, ctx
->sgl
.sg
, NULL
,
85 err
= af_alg_wait_for_completion(
86 crypto_ahash_update(&ctx
->req
),
89 af_alg_free_sg(&ctx
->sgl
);
102 ctx
->more
= msg
->msg_flags
& MSG_MORE
;
104 ahash_request_set_crypt(&ctx
->req
, NULL
, ctx
->result
, 0);
105 err
= af_alg_wait_for_completion(crypto_ahash_final(&ctx
->req
),
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;
123 if (flags
& MSG_SENDPAGE_NOTLAST
)
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
)) {
134 err
= crypto_ahash_finup(&ctx
->req
);
136 err
= crypto_ahash_digest(&ctx
->req
);
139 err
= crypto_ahash_init(&ctx
->req
);
140 err
= af_alg_wait_for_completion(err
, &ctx
->completion
);
145 err
= crypto_ahash_update(&ctx
->req
);
148 err
= af_alg_wait_for_completion(err
, &ctx
->completion
);
152 ctx
->more
= flags
& MSG_MORE
;
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
));
172 msg
->msg_flags
|= MSG_TRUNC
;
177 ahash_request_set_crypt(&ctx
->req
, NULL
, ctx
->result
, 0);
178 err
= af_alg_wait_for_completion(crypto_ahash_final(&ctx
->req
),
184 err
= memcpy_toiovec(msg
->msg_iov
, ctx
->result
, 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
))];
200 struct alg_sock
*ask2
;
201 struct hash_ctx
*ctx2
;
207 err
= more
? crypto_ahash_export(req
, state
) : 0;
213 err
= af_alg_accept(ask
->parent
, newsock
);
219 ctx2
= ask2
->private;
225 err
= crypto_ahash_import(&ctx2
->req
, state
);
234 static struct proto_ops algif_hash_ops
= {
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
)
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
);
270 pask
= alg_sk(ask
->parent
);
274 lock_sock_nested(psk
, SINGLE_DEPTH_NESTING
);
294 static int hash_sendmsg_nokey(struct kiocb
*unused
, struct socket
*sock
,
295 struct msghdr
*msg
, size_t size
)
299 err
= hash_check_key(sock
);
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
)
311 err
= hash_check_key(sock
);
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
)
323 err
= hash_check_key(sock
);
327 return hash_recvmsg(unused
, sock
, msg
, ignored
, flags
);
330 static int hash_accept_nokey(struct socket
*sock
, struct socket
*newsock
,
335 err
= hash_check_key(sock
);
339 return hash_accept(sock
, newsock
, flags
);
342 static struct proto_ops algif_hash_ops_nokey
= {
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
);
371 return ERR_PTR(-ENOMEM
);
373 hash
= crypto_alloc_ahash(name
, type
, mask
);
376 return ERR_CAST(hash
);
384 static void hash_release(void *private)
386 struct algif_hash_tfm
*tfm
= private;
388 crypto_free_ahash(tfm
->hash
);
392 static int hash_setkey(void *private, const u8
*key
, unsigned int keylen
)
394 struct algif_hash_tfm
*tfm
= private;
397 err
= crypto_ahash_setkey(tfm
->hash
, key
, keylen
);
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
);
427 ctx
->result
= sock_kmalloc(sk
, ds
, GFP_KERNEL
);
429 sock_kfree_s(sk
, ctx
, len
);
433 memset(ctx
->result
, 0, ds
);
437 af_alg_init_completion(&ctx
->completion
);
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
;
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
))
457 return hash_accept_parent_nokey(private, sk
);
460 static const struct af_alg_type algif_type_hash
= {
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
,
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
);
483 module_init(algif_hash_init
);
484 module_exit(algif_hash_exit
);
485 MODULE_LICENSE("GPL");