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 socket
*sock
, struct msghdr
*msg
,
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;
52 if (limit
> sk
->sk_sndbuf
)
53 limit
= sk
->sk_sndbuf
;
57 err
= crypto_ahash_init(&ctx
->req
);
64 while (msg_data_left(msg
)) {
65 int len
= msg_data_left(msg
);
70 len
= af_alg_make_sg(&ctx
->sgl
, &msg
->msg_iter
, len
);
72 err
= copied
? 0 : len
;
76 ahash_request_set_crypt(&ctx
->req
, ctx
->sgl
.sg
, NULL
, len
);
78 err
= af_alg_wait_for_completion(crypto_ahash_update(&ctx
->req
),
80 af_alg_free_sg(&ctx
->sgl
);
85 iov_iter_advance(&msg
->msg_iter
, len
);
90 ctx
->more
= msg
->msg_flags
& MSG_MORE
;
92 ahash_request_set_crypt(&ctx
->req
, NULL
, ctx
->result
, 0);
93 err
= af_alg_wait_for_completion(crypto_ahash_final(&ctx
->req
),
100 return err
?: copied
;
103 static ssize_t
hash_sendpage(struct socket
*sock
, struct page
*page
,
104 int offset
, size_t size
, int flags
)
106 struct sock
*sk
= sock
->sk
;
107 struct alg_sock
*ask
= alg_sk(sk
);
108 struct hash_ctx
*ctx
= ask
->private;
111 if (flags
& MSG_SENDPAGE_NOTLAST
)
115 sg_init_table(ctx
->sgl
.sg
, 1);
116 sg_set_page(ctx
->sgl
.sg
, page
, size
, offset
);
118 ahash_request_set_crypt(&ctx
->req
, ctx
->sgl
.sg
, ctx
->result
, size
);
120 if (!(flags
& MSG_MORE
)) {
122 err
= crypto_ahash_finup(&ctx
->req
);
124 err
= crypto_ahash_digest(&ctx
->req
);
127 err
= crypto_ahash_init(&ctx
->req
);
132 err
= crypto_ahash_update(&ctx
->req
);
135 err
= af_alg_wait_for_completion(err
, &ctx
->completion
);
139 ctx
->more
= flags
& MSG_MORE
;
147 static int hash_recvmsg(struct socket
*sock
, struct msghdr
*msg
, size_t len
,
150 struct sock
*sk
= sock
->sk
;
151 struct alg_sock
*ask
= alg_sk(sk
);
152 struct hash_ctx
*ctx
= ask
->private;
153 unsigned ds
= crypto_ahash_digestsize(crypto_ahash_reqtfm(&ctx
->req
));
159 msg
->msg_flags
|= MSG_TRUNC
;
164 ahash_request_set_crypt(&ctx
->req
, NULL
, ctx
->result
, 0);
165 err
= af_alg_wait_for_completion(crypto_ahash_final(&ctx
->req
),
171 err
= memcpy_to_msg(msg
, ctx
->result
, len
);
179 static int hash_accept(struct socket
*sock
, struct socket
*newsock
, int flags
)
181 struct sock
*sk
= sock
->sk
;
182 struct alg_sock
*ask
= alg_sk(sk
);
183 struct hash_ctx
*ctx
= ask
->private;
184 struct ahash_request
*req
= &ctx
->req
;
185 char state
[crypto_ahash_statesize(crypto_ahash_reqtfm(req
))];
187 struct alg_sock
*ask2
;
188 struct hash_ctx
*ctx2
;
194 err
= more
? crypto_ahash_export(req
, state
) : 0;
200 err
= af_alg_accept(ask
->parent
, newsock
);
206 ctx2
= ask2
->private;
212 err
= crypto_ahash_import(&ctx2
->req
, state
);
221 static struct proto_ops algif_hash_ops
= {
224 .connect
= sock_no_connect
,
225 .socketpair
= sock_no_socketpair
,
226 .getname
= sock_no_getname
,
227 .ioctl
= sock_no_ioctl
,
228 .listen
= sock_no_listen
,
229 .shutdown
= sock_no_shutdown
,
230 .getsockopt
= sock_no_getsockopt
,
231 .mmap
= sock_no_mmap
,
232 .bind
= sock_no_bind
,
233 .setsockopt
= sock_no_setsockopt
,
234 .poll
= sock_no_poll
,
236 .release
= af_alg_release
,
237 .sendmsg
= hash_sendmsg
,
238 .sendpage
= hash_sendpage
,
239 .recvmsg
= hash_recvmsg
,
240 .accept
= hash_accept
,
243 static int hash_check_key(struct socket
*sock
)
247 struct alg_sock
*pask
;
248 struct algif_hash_tfm
*tfm
;
249 struct sock
*sk
= sock
->sk
;
250 struct alg_sock
*ask
= alg_sk(sk
);
257 pask
= alg_sk(ask
->parent
);
261 lock_sock_nested(psk
, SINGLE_DEPTH_NESTING
);
281 static int hash_sendmsg_nokey(struct socket
*sock
, struct msghdr
*msg
,
286 err
= hash_check_key(sock
);
290 return hash_sendmsg(sock
, msg
, size
);
293 static ssize_t
hash_sendpage_nokey(struct socket
*sock
, struct page
*page
,
294 int offset
, size_t size
, int flags
)
298 err
= hash_check_key(sock
);
302 return hash_sendpage(sock
, page
, offset
, size
, flags
);
305 static int hash_recvmsg_nokey(struct socket
*sock
, struct msghdr
*msg
,
306 size_t ignored
, int flags
)
310 err
= hash_check_key(sock
);
314 return hash_recvmsg(sock
, msg
, ignored
, flags
);
317 static int hash_accept_nokey(struct socket
*sock
, struct socket
*newsock
,
322 err
= hash_check_key(sock
);
326 return hash_accept(sock
, newsock
, flags
);
329 static struct proto_ops algif_hash_ops_nokey
= {
332 .connect
= sock_no_connect
,
333 .socketpair
= sock_no_socketpair
,
334 .getname
= sock_no_getname
,
335 .ioctl
= sock_no_ioctl
,
336 .listen
= sock_no_listen
,
337 .shutdown
= sock_no_shutdown
,
338 .getsockopt
= sock_no_getsockopt
,
339 .mmap
= sock_no_mmap
,
340 .bind
= sock_no_bind
,
341 .setsockopt
= sock_no_setsockopt
,
342 .poll
= sock_no_poll
,
344 .release
= af_alg_release
,
345 .sendmsg
= hash_sendmsg_nokey
,
346 .sendpage
= hash_sendpage_nokey
,
347 .recvmsg
= hash_recvmsg_nokey
,
348 .accept
= hash_accept_nokey
,
351 static void *hash_bind(const char *name
, u32 type
, u32 mask
)
353 struct algif_hash_tfm
*tfm
;
354 struct crypto_ahash
*hash
;
356 tfm
= kzalloc(sizeof(*tfm
), GFP_KERNEL
);
358 return ERR_PTR(-ENOMEM
);
360 hash
= crypto_alloc_ahash(name
, type
, mask
);
363 return ERR_CAST(hash
);
371 static void hash_release(void *private)
373 struct algif_hash_tfm
*tfm
= private;
375 crypto_free_ahash(tfm
->hash
);
379 static int hash_setkey(void *private, const u8
*key
, unsigned int keylen
)
381 struct algif_hash_tfm
*tfm
= private;
384 err
= crypto_ahash_setkey(tfm
->hash
, key
, keylen
);
390 static void hash_sock_destruct(struct sock
*sk
)
392 struct alg_sock
*ask
= alg_sk(sk
);
393 struct hash_ctx
*ctx
= ask
->private;
395 sock_kzfree_s(sk
, ctx
->result
,
396 crypto_ahash_digestsize(crypto_ahash_reqtfm(&ctx
->req
)));
397 sock_kfree_s(sk
, ctx
, ctx
->len
);
398 af_alg_release_parent(sk
);
401 static int hash_accept_parent_nokey(void *private, struct sock
*sk
)
403 struct hash_ctx
*ctx
;
404 struct alg_sock
*ask
= alg_sk(sk
);
405 struct algif_hash_tfm
*tfm
= private;
406 struct crypto_ahash
*hash
= tfm
->hash
;
407 unsigned len
= sizeof(*ctx
) + crypto_ahash_reqsize(hash
);
408 unsigned ds
= crypto_ahash_digestsize(hash
);
410 ctx
= sock_kmalloc(sk
, len
, GFP_KERNEL
);
414 ctx
->result
= sock_kmalloc(sk
, ds
, GFP_KERNEL
);
416 sock_kfree_s(sk
, ctx
, len
);
420 memset(ctx
->result
, 0, ds
);
424 af_alg_init_completion(&ctx
->completion
);
428 ahash_request_set_tfm(&ctx
->req
, hash
);
429 ahash_request_set_callback(&ctx
->req
, CRYPTO_TFM_REQ_MAY_BACKLOG
,
430 af_alg_complete
, &ctx
->completion
);
432 sk
->sk_destruct
= hash_sock_destruct
;
437 static int hash_accept_parent(void *private, struct sock
*sk
)
439 struct algif_hash_tfm
*tfm
= private;
441 if (!tfm
->has_key
&& crypto_ahash_has_setkey(tfm
->hash
))
444 return hash_accept_parent_nokey(private, sk
);
447 static const struct af_alg_type algif_type_hash
= {
449 .release
= hash_release
,
450 .setkey
= hash_setkey
,
451 .accept
= hash_accept_parent
,
452 .accept_nokey
= hash_accept_parent_nokey
,
453 .ops
= &algif_hash_ops
,
454 .ops_nokey
= &algif_hash_ops_nokey
,
459 static int __init
algif_hash_init(void)
461 return af_alg_register_type(&algif_type_hash
);
464 static void __exit
algif_hash_exit(void)
466 int err
= af_alg_unregister_type(&algif_type_hash
);
470 module_init(algif_hash_init
);
471 module_exit(algif_hash_exit
);
472 MODULE_LICENSE("GPL");