staging: wlan-ng: properly check endpoint types
[linux/fpc-iii.git] / crypto / algif_hash.c
blobe71727c25a7db7c82155e2a605969af7dd92dcc6
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
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>
8 */
10 #include <crypto/hash.h>
11 #include <crypto/if_alg.h>
12 #include <linux/init.h>
13 #include <linux/kernel.h>
14 #include <linux/mm.h>
15 #include <linux/module.h>
16 #include <linux/net.h>
17 #include <net/sock.h>
19 struct hash_ctx {
20 struct af_alg_sgl sgl;
22 u8 *result;
24 struct crypto_wait wait;
26 unsigned int len;
27 bool more;
29 struct ahash_request req;
32 static int hash_alloc_result(struct sock *sk, struct hash_ctx *ctx)
34 unsigned ds;
36 if (ctx->result)
37 return 0;
39 ds = crypto_ahash_digestsize(crypto_ahash_reqtfm(&ctx->req));
41 ctx->result = sock_kmalloc(sk, ds, GFP_KERNEL);
42 if (!ctx->result)
43 return -ENOMEM;
45 memset(ctx->result, 0, ds);
47 return 0;
50 static void hash_free_result(struct sock *sk, struct hash_ctx *ctx)
52 unsigned ds;
54 if (!ctx->result)
55 return;
57 ds = crypto_ahash_digestsize(crypto_ahash_reqtfm(&ctx->req));
59 sock_kzfree_s(sk, ctx->result, ds);
60 ctx->result = NULL;
63 static int hash_sendmsg(struct socket *sock, struct msghdr *msg,
64 size_t ignored)
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;
70 long copied = 0;
71 int err;
73 if (limit > sk->sk_sndbuf)
74 limit = sk->sk_sndbuf;
76 lock_sock(sk);
77 if (!ctx->more) {
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);
82 if (err)
83 goto unlock;
86 ctx->more = false;
88 while (msg_data_left(msg)) {
89 int len = msg_data_left(msg);
91 if (len > limit)
92 len = limit;
94 len = af_alg_make_sg(&ctx->sgl, &msg->msg_iter, len);
95 if (len < 0) {
96 err = copied ? 0 : len;
97 goto unlock;
100 ahash_request_set_crypt(&ctx->req, ctx->sgl.sg, NULL, len);
102 err = crypto_wait_req(crypto_ahash_update(&ctx->req),
103 &ctx->wait);
104 af_alg_free_sg(&ctx->sgl);
105 if (err)
106 goto unlock;
108 copied += len;
109 iov_iter_advance(&msg->msg_iter, len);
112 err = 0;
114 ctx->more = msg->msg_flags & MSG_MORE;
115 if (!ctx->more) {
116 err = hash_alloc_result(sk, ctx);
117 if (err)
118 goto unlock;
120 ahash_request_set_crypt(&ctx->req, NULL, ctx->result, 0);
121 err = crypto_wait_req(crypto_ahash_final(&ctx->req),
122 &ctx->wait);
125 unlock:
126 release_sock(sk);
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;
137 int err;
139 if (flags & MSG_SENDPAGE_NOTLAST)
140 flags |= MSG_MORE;
142 lock_sock(sk);
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);
148 if (err)
149 goto unlock;
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)) {
156 if (ctx->more)
157 err = crypto_ahash_finup(&ctx->req);
158 else
159 err = crypto_ahash_digest(&ctx->req);
160 } else {
161 if (!ctx->more) {
162 err = crypto_ahash_init(&ctx->req);
163 err = crypto_wait_req(err, &ctx->wait);
164 if (err)
165 goto unlock;
168 err = crypto_ahash_update(&ctx->req);
171 err = crypto_wait_req(err, &ctx->wait);
172 if (err)
173 goto unlock;
175 ctx->more = flags & MSG_MORE;
177 unlock:
178 release_sock(sk);
180 return err ?: size;
183 static int hash_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
184 int flags)
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));
190 bool result;
191 int err;
193 if (len > ds)
194 len = ds;
195 else if (len < ds)
196 msg->msg_flags |= MSG_TRUNC;
198 lock_sock(sk);
199 result = ctx->result;
200 err = hash_alloc_result(sk, ctx);
201 if (err)
202 goto unlock;
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),
208 &ctx->wait);
209 if (err)
210 goto unlock;
213 if (!result || ctx->more) {
214 ctx->more = false;
215 err = crypto_wait_req(crypto_ahash_final(&ctx->req),
216 &ctx->wait);
217 if (err)
218 goto unlock;
221 err = memcpy_to_msg(msg, ctx->result, len);
223 unlock:
224 hash_free_result(sk, ctx);
225 release_sock(sk);
227 return err ?: len;
230 static int hash_accept(struct socket *sock, struct socket *newsock, int flags,
231 bool kern)
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];
238 struct sock *sk2;
239 struct alg_sock *ask2;
240 struct hash_ctx *ctx2;
241 bool more;
242 int err;
244 lock_sock(sk);
245 more = ctx->more;
246 err = more ? crypto_ahash_export(req, state) : 0;
247 release_sock(sk);
249 if (err)
250 return err;
252 err = af_alg_accept(ask->parent, newsock, kern);
253 if (err)
254 return err;
256 sk2 = newsock->sk;
257 ask2 = alg_sk(sk2);
258 ctx2 = ask2->private;
259 ctx2->more = more;
261 if (!more)
262 return err;
264 err = crypto_ahash_import(&ctx2->req, state);
265 if (err) {
266 sock_orphan(sk2);
267 sock_put(sk2);
270 return err;
273 static struct proto_ops algif_hash_ops = {
274 .family = PF_ALG,
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)
296 int err = 0;
297 struct sock *psk;
298 struct alg_sock *pask;
299 struct crypto_ahash *tfm;
300 struct sock *sk = sock->sk;
301 struct alg_sock *ask = alg_sk(sk);
303 lock_sock(sk);
304 if (!atomic_read(&ask->nokey_refcnt))
305 goto unlock_child;
307 psk = ask->parent;
308 pask = alg_sk(ask->parent);
309 tfm = pask->private;
311 err = -ENOKEY;
312 lock_sock_nested(psk, SINGLE_DEPTH_NESTING);
313 if (crypto_ahash_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
314 goto unlock;
316 atomic_dec(&pask->nokey_refcnt);
317 atomic_set(&ask->nokey_refcnt, 0);
319 err = 0;
321 unlock:
322 release_sock(psk);
323 unlock_child:
324 release_sock(sk);
326 return err;
329 static int hash_sendmsg_nokey(struct socket *sock, struct msghdr *msg,
330 size_t size)
332 int err;
334 err = hash_check_key(sock);
335 if (err)
336 return err;
338 return hash_sendmsg(sock, msg, size);
341 static ssize_t hash_sendpage_nokey(struct socket *sock, struct page *page,
342 int offset, size_t size, int flags)
344 int err;
346 err = hash_check_key(sock);
347 if (err)
348 return err;
350 return hash_sendpage(sock, page, offset, size, flags);
353 static int hash_recvmsg_nokey(struct socket *sock, struct msghdr *msg,
354 size_t ignored, int flags)
356 int err;
358 err = hash_check_key(sock);
359 if (err)
360 return err;
362 return hash_recvmsg(sock, msg, ignored, flags);
365 static int hash_accept_nokey(struct socket *sock, struct socket *newsock,
366 int flags, bool kern)
368 int err;
370 err = hash_check_key(sock);
371 if (err)
372 return err;
374 return hash_accept(sock, newsock, flags, kern);
377 static struct proto_ops algif_hash_ops_nokey = {
378 .family = PF_ALG,
380 .connect = sock_no_connect,
381 .socketpair = sock_no_socketpair,
382 .getname = sock_no_getname,
383 .ioctl = sock_no_ioctl,
384 .listen = sock_no_listen,
385 .shutdown = sock_no_shutdown,
386 .getsockopt = sock_no_getsockopt,
387 .mmap = sock_no_mmap,
388 .bind = sock_no_bind,
389 .setsockopt = sock_no_setsockopt,
391 .release = af_alg_release,
392 .sendmsg = hash_sendmsg_nokey,
393 .sendpage = hash_sendpage_nokey,
394 .recvmsg = hash_recvmsg_nokey,
395 .accept = hash_accept_nokey,
398 static void *hash_bind(const char *name, u32 type, u32 mask)
400 return crypto_alloc_ahash(name, type, mask);
403 static void hash_release(void *private)
405 crypto_free_ahash(private);
408 static int hash_setkey(void *private, const u8 *key, unsigned int keylen)
410 return crypto_ahash_setkey(private, key, keylen);
413 static void hash_sock_destruct(struct sock *sk)
415 struct alg_sock *ask = alg_sk(sk);
416 struct hash_ctx *ctx = ask->private;
418 hash_free_result(sk, ctx);
419 sock_kfree_s(sk, ctx, ctx->len);
420 af_alg_release_parent(sk);
423 static int hash_accept_parent_nokey(void *private, struct sock *sk)
425 struct crypto_ahash *tfm = private;
426 struct alg_sock *ask = alg_sk(sk);
427 struct hash_ctx *ctx;
428 unsigned int len = sizeof(*ctx) + crypto_ahash_reqsize(tfm);
430 ctx = sock_kmalloc(sk, len, GFP_KERNEL);
431 if (!ctx)
432 return -ENOMEM;
434 ctx->result = NULL;
435 ctx->len = len;
436 ctx->more = false;
437 crypto_init_wait(&ctx->wait);
439 ask->private = ctx;
441 ahash_request_set_tfm(&ctx->req, tfm);
442 ahash_request_set_callback(&ctx->req, CRYPTO_TFM_REQ_MAY_BACKLOG,
443 crypto_req_done, &ctx->wait);
445 sk->sk_destruct = hash_sock_destruct;
447 return 0;
450 static int hash_accept_parent(void *private, struct sock *sk)
452 struct crypto_ahash *tfm = private;
454 if (crypto_ahash_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
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");