xref: /dragonfly/sys/crypto/blake2/blake2s.c (revision 03517d4e)
1 /*	$OpenBSD: blake2s.c,v 1.3 2023/02/03 18:31:16 miod Exp $	*/
2 /*
3  * Copyright (C) 2012 Samuel Neves <sneves@dei.uc.pt>. All Rights Reserved.
4  * Copyright (C) 2015-2020 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
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  * This is an implementation of the BLAKE2s hash and PRF functions.
19  * Information: https://blake2.net/
20  */
21 
22 #include <sys/types.h>
23 #include <sys/systm.h>
24 #include <sys/endian.h>
25 
26 #include <crypto/blake2/blake2s.h>
27 
28 static inline uint32_t ror32(uint32_t word, unsigned int shift)
29 {
30 	return (word >> shift) | (word << (32 - shift));
31 }
32 
33 static const uint32_t blake2s_iv[8] = {
34 	0x6A09E667UL, 0xBB67AE85UL, 0x3C6EF372UL, 0xA54FF53AUL,
35 	0x510E527FUL, 0x9B05688CUL, 0x1F83D9ABUL, 0x5BE0CD19UL
36 };
37 
38 static const uint8_t blake2s_sigma[10][16] = {
39 	{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 },
40 	{ 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 },
41 	{ 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4 },
42 	{ 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8 },
43 	{ 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13 },
44 	{ 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9 },
45 	{ 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11 },
46 	{ 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10 },
47 	{ 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5 },
48 	{ 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0 },
49 };
50 
51 static inline void blake2s_set_lastblock(struct blake2s_state *state)
52 {
53 	state->f[0] = -1;
54 }
55 
56 static inline void blake2s_increment_counter(struct blake2s_state *state,
57 					     uint32_t inc)
58 {
59 	state->t[0] += inc;
60 	state->t[1] += (state->t[0] < inc);
61 }
62 
63 static inline void blake2s_init_param(struct blake2s_state *state,
64 				      uint32_t param)
65 {
66 	int i;
67 
68 	memset(state, 0, sizeof(*state));
69 	for (i = 0; i < 8; ++i)
70 		state->h[i] = blake2s_iv[i];
71 	state->h[0] ^= param;
72 }
73 
74 void blake2s_init(struct blake2s_state *state, size_t outlen)
75 {
76 	KKASSERT(!(!outlen || outlen > BLAKE2S_HASH_SIZE));
77 	blake2s_init_param(state, 0x01010000 | outlen);
78 	state->outlen = outlen;
79 }
80 
81 void blake2s_init_key(struct blake2s_state *state, size_t outlen,
82 		      const void *key, size_t keylen)
83 {
84 	uint8_t block[BLAKE2S_BLOCK_SIZE] = { 0 };
85 
86 	KKASSERT(!(!outlen || outlen > BLAKE2S_HASH_SIZE ||
87 		   !key || !keylen || keylen > BLAKE2S_KEY_SIZE));
88 
89 	blake2s_init_param(state, 0x01010000 | keylen << 8 | outlen);
90 	state->outlen = outlen;
91 	memcpy(block, key, keylen);
92 	blake2s_update(state, block, BLAKE2S_BLOCK_SIZE);
93 	explicit_bzero(block, BLAKE2S_BLOCK_SIZE);
94 }
95 
96 static inline void blake2s_compress(struct blake2s_state *state,
97 				    const uint8_t *block, size_t nblocks,
98 				    uint32_t inc)
99 {
100 	uint32_t m[16];
101 	uint32_t v[16];
102 	int i;
103 
104 	KKASSERT(!((nblocks > 1 && inc != BLAKE2S_BLOCK_SIZE)));
105 
106 	while (nblocks > 0) {
107 		blake2s_increment_counter(state, inc);
108 		memcpy(m, block, BLAKE2S_BLOCK_SIZE);
109 		for (i = 0; i < 16; i++)
110 			m[i] = le32toh(m[i]);
111 		memcpy(v, state->h, 32);
112 		v[ 8] = blake2s_iv[0];
113 		v[ 9] = blake2s_iv[1];
114 		v[10] = blake2s_iv[2];
115 		v[11] = blake2s_iv[3];
116 		v[12] = blake2s_iv[4] ^ state->t[0];
117 		v[13] = blake2s_iv[5] ^ state->t[1];
118 		v[14] = blake2s_iv[6] ^ state->f[0];
119 		v[15] = blake2s_iv[7] ^ state->f[1];
120 
121 #define G(r, i, a, b, c, d) do { \
122 	a += b + m[blake2s_sigma[r][2 * i + 0]]; \
123 	d = ror32(d ^ a, 16); \
124 	c += d; \
125 	b = ror32(b ^ c, 12); \
126 	a += b + m[blake2s_sigma[r][2 * i + 1]]; \
127 	d = ror32(d ^ a, 8); \
128 	c += d; \
129 	b = ror32(b ^ c, 7); \
130 } while (0)
131 
132 #define ROUND(r) do { \
133 	G(r, 0, v[0], v[ 4], v[ 8], v[12]); \
134 	G(r, 1, v[1], v[ 5], v[ 9], v[13]); \
135 	G(r, 2, v[2], v[ 6], v[10], v[14]); \
136 	G(r, 3, v[3], v[ 7], v[11], v[15]); \
137 	G(r, 4, v[0], v[ 5], v[10], v[15]); \
138 	G(r, 5, v[1], v[ 6], v[11], v[12]); \
139 	G(r, 6, v[2], v[ 7], v[ 8], v[13]); \
140 	G(r, 7, v[3], v[ 4], v[ 9], v[14]); \
141 } while (0)
142 		ROUND(0);
143 		ROUND(1);
144 		ROUND(2);
145 		ROUND(3);
146 		ROUND(4);
147 		ROUND(5);
148 		ROUND(6);
149 		ROUND(7);
150 		ROUND(8);
151 		ROUND(9);
152 
153 #undef G
154 #undef ROUND
155 
156 		for (i = 0; i < 8; ++i)
157 			state->h[i] ^= v[i] ^ v[i + 8];
158 
159 		block += BLAKE2S_BLOCK_SIZE;
160 		--nblocks;
161 	}
162 }
163 
164 void blake2s_update(struct blake2s_state *state, const uint8_t *in, size_t inlen)
165 {
166 	const size_t fill = BLAKE2S_BLOCK_SIZE - state->buflen;
167 
168 	if (!inlen)
169 		return;
170 	if (inlen > fill) {
171 		memcpy(state->buf + state->buflen, in, fill);
172 		blake2s_compress(state, state->buf, 1, BLAKE2S_BLOCK_SIZE);
173 		state->buflen = 0;
174 		in += fill;
175 		inlen -= fill;
176 	}
177 	if (inlen > BLAKE2S_BLOCK_SIZE) {
178 		const size_t nblocks =
179 			(inlen + BLAKE2S_BLOCK_SIZE - 1) / BLAKE2S_BLOCK_SIZE;
180 		/* Hash one less (full) block than strictly possible */
181 		blake2s_compress(state, in, nblocks - 1, BLAKE2S_BLOCK_SIZE);
182 		in += BLAKE2S_BLOCK_SIZE * (nblocks - 1);
183 		inlen -= BLAKE2S_BLOCK_SIZE * (nblocks - 1);
184 	}
185 	memcpy(state->buf + state->buflen, in, inlen);
186 	state->buflen += inlen;
187 }
188 
189 void blake2s_final(struct blake2s_state *state, uint8_t *out)
190 {
191 	int i;
192 	blake2s_set_lastblock(state);
193 	memset(state->buf + state->buflen, 0,
194 	       BLAKE2S_BLOCK_SIZE - state->buflen); /* Padding */
195 	blake2s_compress(state, state->buf, 1, state->buflen);
196 	for (i = 0; i < 8; i++)
197 		state->h[i] = htole32(state->h[i]);
198 	memcpy(out, state->h, state->outlen);
199 	explicit_bzero(state, sizeof(*state));
200 }
201 
202 void blake2s_hmac(uint8_t *out, const uint8_t *in, const uint8_t *key,
203 		  size_t outlen, size_t inlen, size_t keylen)
204 {
205 	struct blake2s_state state;
206 	uint8_t x_key[BLAKE2S_BLOCK_SIZE] __aligned(__alignof__(uint32_t)) = { 0 };
207 	uint8_t i_hash[BLAKE2S_HASH_SIZE] __aligned(__alignof__(uint32_t));
208 	int i;
209 
210 	if (keylen > BLAKE2S_BLOCK_SIZE) {
211 		blake2s_init(&state, BLAKE2S_HASH_SIZE);
212 		blake2s_update(&state, key, keylen);
213 		blake2s_final(&state, x_key);
214 	} else
215 		memcpy(x_key, key, keylen);
216 
217 	for (i = 0; i < BLAKE2S_BLOCK_SIZE; ++i)
218 		x_key[i] ^= 0x36;
219 
220 	blake2s_init(&state, BLAKE2S_HASH_SIZE);
221 	blake2s_update(&state, x_key, BLAKE2S_BLOCK_SIZE);
222 	blake2s_update(&state, in, inlen);
223 	blake2s_final(&state, i_hash);
224 
225 	for (i = 0; i < BLAKE2S_BLOCK_SIZE; ++i)
226 		x_key[i] ^= 0x5c ^ 0x36;
227 
228 	blake2s_init(&state, BLAKE2S_HASH_SIZE);
229 	blake2s_update(&state, x_key, BLAKE2S_BLOCK_SIZE);
230 	blake2s_update(&state, i_hash, BLAKE2S_HASH_SIZE);
231 	blake2s_final(&state, i_hash);
232 
233 	memcpy(out, i_hash, outlen);
234 	explicit_bzero(x_key, BLAKE2S_BLOCK_SIZE);
235 	explicit_bzero(i_hash, BLAKE2S_HASH_SIZE);
236 }
237