1a3c41f8bSConrad Meyer /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
3a3c41f8bSConrad Meyer *
4a3c41f8bSConrad Meyer * Copyright (c) 2019 Conrad Meyer <cem@FreeBSD.org>
5a3c41f8bSConrad Meyer *
6a3c41f8bSConrad Meyer * Redistribution and use in source and binary forms, with or without
7a3c41f8bSConrad Meyer * modification, are permitted provided that the following conditions
8a3c41f8bSConrad Meyer * are met:
9a3c41f8bSConrad Meyer * 1. Redistributions of source code must retain the above copyright
10a3c41f8bSConrad Meyer * notice, this list of conditions and the following disclaimer.
11a3c41f8bSConrad Meyer * 2. Redistributions in binary form must reproduce the above copyright
12a3c41f8bSConrad Meyer * notice, this list of conditions and the following disclaimer in the
13a3c41f8bSConrad Meyer * documentation and/or other materials provided with the distribution.
14a3c41f8bSConrad Meyer *
15a3c41f8bSConrad Meyer * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16a3c41f8bSConrad Meyer * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17a3c41f8bSConrad Meyer * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18a3c41f8bSConrad Meyer * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19a3c41f8bSConrad Meyer * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20a3c41f8bSConrad Meyer * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21a3c41f8bSConrad Meyer * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22a3c41f8bSConrad Meyer * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23a3c41f8bSConrad Meyer * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24a3c41f8bSConrad Meyer * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25a3c41f8bSConrad Meyer * SUCH DAMAGE.
26a3c41f8bSConrad Meyer */
27a3c41f8bSConrad Meyer #pragma once
28a3c41f8bSConrad Meyer
29a3c41f8bSConrad Meyer #include <dev/random/fenestrasX/fx_priv.h>
30a3c41f8bSConrad Meyer #define blake2b_init blake2b_init_ref
31a3c41f8bSConrad Meyer #define blake2b_update blake2b_update_ref
32a3c41f8bSConrad Meyer #define blake2b_final blake2b_final_ref
33a3c41f8bSConrad Meyer #include <contrib/libb2/blake2.h>
34a3c41f8bSConrad Meyer
35a3c41f8bSConrad Meyer #define FXRNG_HASH_SZ BLAKE2B_OUTBYTES /* 64 */
36a3c41f8bSConrad Meyer
37a3c41f8bSConrad Meyer /*
38a3c41f8bSConrad Meyer * Wrappers for hash function abstraction.
39a3c41f8bSConrad Meyer */
40a3c41f8bSConrad Meyer struct fxrng_hash {
41a3c41f8bSConrad Meyer blake2b_state state;
42a3c41f8bSConrad Meyer };
43a3c41f8bSConrad Meyer
44a3c41f8bSConrad Meyer static inline void
fxrng_hash_init(struct fxrng_hash * h)45a3c41f8bSConrad Meyer fxrng_hash_init(struct fxrng_hash *h)
46a3c41f8bSConrad Meyer {
47a3c41f8bSConrad Meyer int rc;
48a3c41f8bSConrad Meyer
49a3c41f8bSConrad Meyer rc = blake2b_init(&h->state, FXRNG_HASH_SZ);
50a3c41f8bSConrad Meyer ASSERT(rc == 0, "blake2b_init");
51a3c41f8bSConrad Meyer }
52a3c41f8bSConrad Meyer
53a3c41f8bSConrad Meyer static inline void
fxrng_hash_update(struct fxrng_hash * h,const void * buf,size_t sz)54a3c41f8bSConrad Meyer fxrng_hash_update(struct fxrng_hash *h, const void *buf, size_t sz)
55a3c41f8bSConrad Meyer {
56a3c41f8bSConrad Meyer int rc;
57a3c41f8bSConrad Meyer
58a3c41f8bSConrad Meyer rc = blake2b_update(&h->state, buf, sz);
59a3c41f8bSConrad Meyer ASSERT(rc == 0, "blake2b_update");
60a3c41f8bSConrad Meyer }
61a3c41f8bSConrad Meyer
62a3c41f8bSConrad Meyer static inline void
fxrng_hash_finish(struct fxrng_hash * h,uint8_t buf[static FXRNG_HASH_SZ],size_t sz)63a3c41f8bSConrad Meyer fxrng_hash_finish(struct fxrng_hash *h, uint8_t buf[static FXRNG_HASH_SZ], size_t sz)
64a3c41f8bSConrad Meyer {
65a3c41f8bSConrad Meyer int rc;
66a3c41f8bSConrad Meyer
67a3c41f8bSConrad Meyer rc = blake2b_final(&h->state, buf, sz);
68a3c41f8bSConrad Meyer ASSERT(rc == 0, "blake2b_final(sz=%zu)", sz);
69a3c41f8bSConrad Meyer explicit_bzero(h, sizeof(*h));
70a3c41f8bSConrad Meyer }
71