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);
106 iov_iter_revert(&msg->msg_iter, len);
115 ctx->more = msg->msg_flags & MSG_MORE;
117 err = hash_alloc_result(sk, ctx);
121 ahash_request_set_crypt(&ctx->req, NULL, ctx->result, 0);
122 err = crypto_wait_req(crypto_ahash_final(&ctx->req),
129 return err ?: copied;
132 static ssize_t hash_sendpage(struct socket *sock, struct page *page,
133 int offset, size_t size, int flags)
135 struct sock *sk = sock->sk;
136 struct alg_sock *ask = alg_sk(sk);
137 struct hash_ctx *ctx = ask->private;
140 if (flags & MSG_SENDPAGE_NOTLAST)
144 sg_init_table(ctx->sgl.sg, 1);
145 sg_set_page(ctx->sgl.sg, page, size, offset);
147 if (!(flags & MSG_MORE)) {
148 err = hash_alloc_result(sk, ctx);
151 } else if (!ctx->more)
152 hash_free_result(sk, ctx);
154 ahash_request_set_crypt(&ctx->req, ctx->sgl.sg, ctx->result, size);
156 if (!(flags & MSG_MORE)) {
158 err = crypto_ahash_finup(&ctx->req);
160 err = crypto_ahash_digest(&ctx->req);
163 err = crypto_ahash_init(&ctx->req);
164 err = crypto_wait_req(err, &ctx->wait);
169 err = crypto_ahash_update(&ctx->req);
172 err = crypto_wait_req(err, &ctx->wait);
176 ctx->more = flags & MSG_MORE;
184 static int hash_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
187 struct sock *sk = sock->sk;
188 struct alg_sock *ask = alg_sk(sk);
189 struct hash_ctx *ctx = ask->private;
190 unsigned ds = crypto_ahash_digestsize(crypto_ahash_reqtfm(&ctx->req));
197 msg->msg_flags |= MSG_TRUNC;
200 result = ctx->result;
201 err = hash_alloc_result(sk, ctx);
205 ahash_request_set_crypt(&ctx->req, NULL, ctx->result, 0);
207 if (!result && !ctx->more) {
208 err = crypto_wait_req(crypto_ahash_init(&ctx->req),
214 if (!result || ctx->more) {
216 err = crypto_wait_req(crypto_ahash_final(&ctx->req),
222 err = memcpy_to_msg(msg, ctx->result, len);
225 hash_free_result(sk, ctx);
231 static int hash_accept(struct socket *sock, struct socket *newsock, int flags,
234 struct sock *sk = sock->sk;
235 struct alg_sock *ask = alg_sk(sk);
236 struct hash_ctx *ctx = ask->private;
237 struct ahash_request *req = &ctx->req;
238 struct crypto_ahash *tfm;
240 struct alg_sock *ask2;
241 struct hash_ctx *ctx2;
246 tfm = crypto_ahash_reqtfm(req);
247 state = kmalloc(crypto_ahash_statesize(tfm), GFP_KERNEL);
254 err = more ? crypto_ahash_export(req, state) : 0;
260 err = af_alg_accept(ask->parent, newsock, kern);
266 ctx2 = ask2->private;
272 err = crypto_ahash_import(&ctx2->req, state);
279 kfree_sensitive(state);
285 static struct proto_ops algif_hash_ops = {
288 .connect = sock_no_connect,
289 .socketpair = sock_no_socketpair,
290 .getname = sock_no_getname,
291 .ioctl = sock_no_ioctl,
292 .listen = sock_no_listen,
293 .shutdown = sock_no_shutdown,
294 .mmap = sock_no_mmap,
295 .bind = sock_no_bind,
297 .release = af_alg_release,
298 .sendmsg = hash_sendmsg,
299 .sendpage = hash_sendpage,
300 .recvmsg = hash_recvmsg,
301 .accept = hash_accept,
304 static int hash_check_key(struct socket *sock)
308 struct alg_sock *pask;
309 struct crypto_ahash *tfm;
310 struct sock *sk = sock->sk;
311 struct alg_sock *ask = alg_sk(sk);
314 if (!atomic_read(&ask->nokey_refcnt))
318 pask = alg_sk(ask->parent);
322 lock_sock_nested(psk, SINGLE_DEPTH_NESTING);
323 if (crypto_ahash_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
326 atomic_dec(&pask->nokey_refcnt);
327 atomic_set(&ask->nokey_refcnt, 0);
339 static int hash_sendmsg_nokey(struct socket *sock, struct msghdr *msg,
344 err = hash_check_key(sock);
348 return hash_sendmsg(sock, msg, size);
351 static ssize_t hash_sendpage_nokey(struct socket *sock, struct page *page,
352 int offset, size_t size, int flags)
356 err = hash_check_key(sock);
360 return hash_sendpage(sock, page, offset, size, flags);
363 static int hash_recvmsg_nokey(struct socket *sock, struct msghdr *msg,
364 size_t ignored, int flags)
368 err = hash_check_key(sock);
372 return hash_recvmsg(sock, msg, ignored, flags);
375 static int hash_accept_nokey(struct socket *sock, struct socket *newsock,
376 int flags, bool kern)
380 err = hash_check_key(sock);
384 return hash_accept(sock, newsock, flags, kern);
387 static struct proto_ops algif_hash_ops_nokey = {
390 .connect = sock_no_connect,
391 .socketpair = sock_no_socketpair,
392 .getname = sock_no_getname,
393 .ioctl = sock_no_ioctl,
394 .listen = sock_no_listen,
395 .shutdown = sock_no_shutdown,
396 .mmap = sock_no_mmap,
397 .bind = sock_no_bind,
399 .release = af_alg_release,
400 .sendmsg = hash_sendmsg_nokey,
401 .sendpage = hash_sendpage_nokey,
402 .recvmsg = hash_recvmsg_nokey,
403 .accept = hash_accept_nokey,
406 static void *hash_bind(const char *name, u32 type, u32 mask)
408 return crypto_alloc_ahash(name, type, mask);
411 static void hash_release(void *private)
413 crypto_free_ahash(private);
416 static int hash_setkey(void *private, const u8 *key, unsigned int keylen)
418 return crypto_ahash_setkey(private, key, keylen);
421 static void hash_sock_destruct(struct sock *sk)
423 struct alg_sock *ask = alg_sk(sk);
424 struct hash_ctx *ctx = ask->private;
426 hash_free_result(sk, ctx);
427 sock_kfree_s(sk, ctx, ctx->len);
428 af_alg_release_parent(sk);
431 static int hash_accept_parent_nokey(void *private, struct sock *sk)
433 struct crypto_ahash *tfm = private;
434 struct alg_sock *ask = alg_sk(sk);
435 struct hash_ctx *ctx;
436 unsigned int len = sizeof(*ctx) + crypto_ahash_reqsize(tfm);
438 ctx = sock_kmalloc(sk, len, GFP_KERNEL);
445 crypto_init_wait(&ctx->wait);
449 ahash_request_set_tfm(&ctx->req, tfm);
450 ahash_request_set_callback(&ctx->req, CRYPTO_TFM_REQ_MAY_BACKLOG,
451 crypto_req_done, &ctx->wait);
453 sk->sk_destruct = hash_sock_destruct;
458 static int hash_accept_parent(void *private, struct sock *sk)
460 struct crypto_ahash *tfm = private;
462 if (crypto_ahash_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
465 return hash_accept_parent_nokey(private, sk);
468 static const struct af_alg_type algif_type_hash = {
470 .release = hash_release,
471 .setkey = hash_setkey,
472 .accept = hash_accept_parent,
473 .accept_nokey = hash_accept_parent_nokey,
474 .ops = &algif_hash_ops,
475 .ops_nokey = &algif_hash_ops_nokey,
480 static int __init algif_hash_init(void)
482 return af_alg_register_type(&algif_type_hash);
485 static void __exit algif_hash_exit(void)
487 int err = af_alg_unregister_type(&algif_type_hash);
491 module_init(algif_hash_init);
492 module_exit(algif_hash_exit);
493 MODULE_LICENSE("GPL");