1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * algif_hash: User-space interface for hash algorithms
5 * This file provides the user-space API for hash algorithms.
7 * Copyright (c) 2010 Herbert Xu <herbert@gondor.apana.org.au>
10 #include <crypto/hash.h>
11 #include <crypto/if_alg.h>
12 #include <linux/init.h>
13 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/net.h>
20 struct af_alg_sgl sgl
;
24 struct crypto_wait wait
;
29 struct ahash_request req
;
32 static int hash_alloc_result(struct sock
*sk
, struct hash_ctx
*ctx
)
39 ds
= crypto_ahash_digestsize(crypto_ahash_reqtfm(&ctx
->req
));
41 ctx
->result
= sock_kmalloc(sk
, ds
, GFP_KERNEL
);
45 memset(ctx
->result
, 0, ds
);
50 static void hash_free_result(struct sock
*sk
, struct hash_ctx
*ctx
)
57 ds
= crypto_ahash_digestsize(crypto_ahash_reqtfm(&ctx
->req
));
59 sock_kzfree_s(sk
, ctx
->result
, ds
);
63 static int hash_sendmsg(struct socket
*sock
, struct msghdr
*msg
,
66 int limit
= ALG_MAX_PAGES
* PAGE_SIZE
;
67 struct sock
*sk
= sock
->sk
;
68 struct alg_sock
*ask
= alg_sk(sk
);
69 struct hash_ctx
*ctx
= ask
->private;
73 if (limit
> sk
->sk_sndbuf
)
74 limit
= sk
->sk_sndbuf
;
78 if ((msg
->msg_flags
& MSG_MORE
))
79 hash_free_result(sk
, ctx
);
81 err
= crypto_wait_req(crypto_ahash_init(&ctx
->req
), &ctx
->wait
);
88 while (msg_data_left(msg
)) {
89 int len
= msg_data_left(msg
);
94 len
= af_alg_make_sg(&ctx
->sgl
, &msg
->msg_iter
, len
);
96 err
= copied
? 0 : len
;
100 ahash_request_set_crypt(&ctx
->req
, ctx
->sgl
.sg
, NULL
, len
);
102 err
= crypto_wait_req(crypto_ahash_update(&ctx
->req
),
104 af_alg_free_sg(&ctx
->sgl
);
109 iov_iter_advance(&msg
->msg_iter
, len
);
114 ctx
->more
= msg
->msg_flags
& MSG_MORE
;
116 err
= hash_alloc_result(sk
, ctx
);
120 ahash_request_set_crypt(&ctx
->req
, NULL
, ctx
->result
, 0);
121 err
= crypto_wait_req(crypto_ahash_final(&ctx
->req
),
128 return err
?: copied
;
131 static ssize_t
hash_sendpage(struct socket
*sock
, struct page
*page
,
132 int offset
, size_t size
, int flags
)
134 struct sock
*sk
= sock
->sk
;
135 struct alg_sock
*ask
= alg_sk(sk
);
136 struct hash_ctx
*ctx
= ask
->private;
139 if (flags
& MSG_SENDPAGE_NOTLAST
)
143 sg_init_table(ctx
->sgl
.sg
, 1);
144 sg_set_page(ctx
->sgl
.sg
, page
, size
, offset
);
146 if (!(flags
& MSG_MORE
)) {
147 err
= hash_alloc_result(sk
, ctx
);
150 } else if (!ctx
->more
)
151 hash_free_result(sk
, ctx
);
153 ahash_request_set_crypt(&ctx
->req
, ctx
->sgl
.sg
, ctx
->result
, size
);
155 if (!(flags
& MSG_MORE
)) {
157 err
= crypto_ahash_finup(&ctx
->req
);
159 err
= crypto_ahash_digest(&ctx
->req
);
162 err
= crypto_ahash_init(&ctx
->req
);
163 err
= crypto_wait_req(err
, &ctx
->wait
);
168 err
= crypto_ahash_update(&ctx
->req
);
171 err
= crypto_wait_req(err
, &ctx
->wait
);
175 ctx
->more
= flags
& MSG_MORE
;
183 static int hash_recvmsg(struct socket
*sock
, struct msghdr
*msg
, size_t len
,
186 struct sock
*sk
= sock
->sk
;
187 struct alg_sock
*ask
= alg_sk(sk
);
188 struct hash_ctx
*ctx
= ask
->private;
189 unsigned ds
= crypto_ahash_digestsize(crypto_ahash_reqtfm(&ctx
->req
));
196 msg
->msg_flags
|= MSG_TRUNC
;
199 result
= ctx
->result
;
200 err
= hash_alloc_result(sk
, ctx
);
204 ahash_request_set_crypt(&ctx
->req
, NULL
, ctx
->result
, 0);
206 if (!result
&& !ctx
->more
) {
207 err
= crypto_wait_req(crypto_ahash_init(&ctx
->req
),
213 if (!result
|| ctx
->more
) {
215 err
= crypto_wait_req(crypto_ahash_final(&ctx
->req
),
221 err
= memcpy_to_msg(msg
, ctx
->result
, len
);
224 hash_free_result(sk
, ctx
);
230 static int hash_accept(struct socket
*sock
, struct socket
*newsock
, int flags
,
233 struct sock
*sk
= sock
->sk
;
234 struct alg_sock
*ask
= alg_sk(sk
);
235 struct hash_ctx
*ctx
= ask
->private;
236 struct ahash_request
*req
= &ctx
->req
;
237 char state
[HASH_MAX_STATESIZE
];
239 struct alg_sock
*ask2
;
240 struct hash_ctx
*ctx2
;
246 err
= more
? crypto_ahash_export(req
, state
) : 0;
252 err
= af_alg_accept(ask
->parent
, newsock
, kern
);
258 ctx2
= ask2
->private;
264 err
= crypto_ahash_import(&ctx2
->req
, state
);
273 static struct proto_ops algif_hash_ops
= {
276 .connect
= sock_no_connect
,
277 .socketpair
= sock_no_socketpair
,
278 .getname
= sock_no_getname
,
279 .ioctl
= sock_no_ioctl
,
280 .listen
= sock_no_listen
,
281 .shutdown
= sock_no_shutdown
,
282 .getsockopt
= sock_no_getsockopt
,
283 .mmap
= sock_no_mmap
,
284 .bind
= sock_no_bind
,
285 .setsockopt
= sock_no_setsockopt
,
287 .release
= af_alg_release
,
288 .sendmsg
= hash_sendmsg
,
289 .sendpage
= hash_sendpage
,
290 .recvmsg
= hash_recvmsg
,
291 .accept
= hash_accept
,
294 static int hash_check_key(struct socket
*sock
)
298 struct alg_sock
*pask
;
299 struct crypto_ahash
*tfm
;
300 struct sock
*sk
= sock
->sk
;
301 struct alg_sock
*ask
= alg_sk(sk
);
308 pask
= alg_sk(ask
->parent
);
312 lock_sock_nested(psk
, SINGLE_DEPTH_NESTING
);
313 if (crypto_ahash_get_flags(tfm
) & CRYPTO_TFM_NEED_KEY
)
332 static int hash_sendmsg_nokey(struct socket
*sock
, struct msghdr
*msg
,
337 err
= hash_check_key(sock
);
341 return hash_sendmsg(sock
, msg
, size
);
344 static ssize_t
hash_sendpage_nokey(struct socket
*sock
, struct page
*page
,
345 int offset
, size_t size
, int flags
)
349 err
= hash_check_key(sock
);
353 return hash_sendpage(sock
, page
, offset
, size
, flags
);
356 static int hash_recvmsg_nokey(struct socket
*sock
, struct msghdr
*msg
,
357 size_t ignored
, int flags
)
361 err
= hash_check_key(sock
);
365 return hash_recvmsg(sock
, msg
, ignored
, flags
);
368 static int hash_accept_nokey(struct socket
*sock
, struct socket
*newsock
,
369 int flags
, bool kern
)
373 err
= hash_check_key(sock
);
377 return hash_accept(sock
, newsock
, flags
, kern
);
380 static struct proto_ops algif_hash_ops_nokey
= {
383 .connect
= sock_no_connect
,
384 .socketpair
= sock_no_socketpair
,
385 .getname
= sock_no_getname
,
386 .ioctl
= sock_no_ioctl
,
387 .listen
= sock_no_listen
,
388 .shutdown
= sock_no_shutdown
,
389 .getsockopt
= sock_no_getsockopt
,
390 .mmap
= sock_no_mmap
,
391 .bind
= sock_no_bind
,
392 .setsockopt
= sock_no_setsockopt
,
394 .release
= af_alg_release
,
395 .sendmsg
= hash_sendmsg_nokey
,
396 .sendpage
= hash_sendpage_nokey
,
397 .recvmsg
= hash_recvmsg_nokey
,
398 .accept
= hash_accept_nokey
,
401 static void *hash_bind(const char *name
, u32 type
, u32 mask
)
403 return crypto_alloc_ahash(name
, type
, mask
);
406 static void hash_release(void *private)
408 crypto_free_ahash(private);
411 static int hash_setkey(void *private, const u8
*key
, unsigned int keylen
)
413 return crypto_ahash_setkey(private, key
, keylen
);
416 static void hash_sock_destruct(struct sock
*sk
)
418 struct alg_sock
*ask
= alg_sk(sk
);
419 struct hash_ctx
*ctx
= ask
->private;
421 hash_free_result(sk
, ctx
);
422 sock_kfree_s(sk
, ctx
, ctx
->len
);
423 af_alg_release_parent(sk
);
426 static int hash_accept_parent_nokey(void *private, struct sock
*sk
)
428 struct crypto_ahash
*tfm
= private;
429 struct alg_sock
*ask
= alg_sk(sk
);
430 struct hash_ctx
*ctx
;
431 unsigned int len
= sizeof(*ctx
) + crypto_ahash_reqsize(tfm
);
433 ctx
= sock_kmalloc(sk
, len
, GFP_KERNEL
);
440 crypto_init_wait(&ctx
->wait
);
444 ahash_request_set_tfm(&ctx
->req
, tfm
);
445 ahash_request_set_callback(&ctx
->req
, CRYPTO_TFM_REQ_MAY_BACKLOG
,
446 crypto_req_done
, &ctx
->wait
);
448 sk
->sk_destruct
= hash_sock_destruct
;
453 static int hash_accept_parent(void *private, struct sock
*sk
)
455 struct crypto_ahash
*tfm
= private;
457 if (crypto_ahash_get_flags(tfm
) & CRYPTO_TFM_NEED_KEY
)
460 return hash_accept_parent_nokey(private, sk
);
463 static const struct af_alg_type algif_type_hash
= {
465 .release
= hash_release
,
466 .setkey
= hash_setkey
,
467 .accept
= hash_accept_parent
,
468 .accept_nokey
= hash_accept_parent_nokey
,
469 .ops
= &algif_hash_ops
,
470 .ops_nokey
= &algif_hash_ops_nokey
,
475 static int __init
algif_hash_init(void)
477 return af_alg_register_type(&algif_type_hash
);
480 static void __exit
algif_hash_exit(void)
482 int err
= af_alg_unregister_type(&algif_type_hash
);
486 module_init(algif_hash_init
);
487 module_exit(algif_hash_exit
);
488 MODULE_LICENSE("GPL");