1 /* $OpenBSD: blake2s.h,v 1.3 2023/02/03 18:31:16 miod Exp $ */
2 /*
3 * Copyright (C) 2015-2020 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
4 * Copyright (C) 2019-2020 Matt Dunwoodie <ncon@noconroy.net>.
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #include <sys/types.h>
20
21 #ifndef _BLAKE2S_H_
22 #define _BLAKE2S_H_
23
24 enum blake2s_lengths {
25 BLAKE2S_BLOCK_SIZE = 64,
26 BLAKE2S_HASH_SIZE = 32,
27 BLAKE2S_KEY_SIZE = 32
28 };
29
30 struct blake2s_state {
31 uint32_t h[8];
32 uint32_t t[2];
33 uint32_t f[2];
34 uint8_t buf[BLAKE2S_BLOCK_SIZE];
35 unsigned int buflen;
36 unsigned int outlen;
37 };
38
39 void blake2s_init(struct blake2s_state *state, const size_t outlen);
40 void blake2s_init_key(struct blake2s_state *state, const size_t outlen,
41 const void *key, const size_t keylen);
42 void blake2s_update(struct blake2s_state *state, const uint8_t *in, size_t inlen);
43 void blake2s_final(struct blake2s_state *state, uint8_t *out);
44
blake2s(uint8_t * out,const uint8_t * in,const uint8_t * key,const size_t outlen,const size_t inlen,const size_t keylen)45 static inline void blake2s(
46 uint8_t *out, const uint8_t *in, const uint8_t *key,
47 const size_t outlen, const size_t inlen, const size_t keylen)
48 {
49 struct blake2s_state state;
50
51 KASSERT((in != NULL || inlen == 0) &&
52 out != NULL && outlen <= BLAKE2S_HASH_SIZE &&
53 (key != NULL || keylen == 0) && keylen <= BLAKE2S_KEY_SIZE);
54
55 if (keylen)
56 blake2s_init_key(&state, outlen, key, keylen);
57 else
58 blake2s_init(&state, outlen);
59
60 blake2s_update(&state, in, inlen);
61 blake2s_final(&state, out);
62 }
63
64 void blake2s_hmac(uint8_t *out, const uint8_t *in, const uint8_t *key,
65 const size_t outlen, const size_t inlen, const size_t keylen);
66
67 #endif /* _BLAKE2S_H_ */
68