xref: /openbsd-src/sys/crypto/blake2s.c (revision 505ee9ea3b177e2387d907a91ca7da069f3f14d8)
1 /*
2  * Copyright (C) 2012 Samuel Neves <sneves@dei.uc.pt>. All Rights Reserved.
3  * Copyright (C) 2015-2020 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  *
17  * This is an implementation of the BLAKE2s hash and PRF functions.
18  * Information: https://blake2.net/
19  */
20 
21 #include <sys/types.h>
22 #include <sys/systm.h>
23 
24 #include <crypto/blake2s.h>
25 
26 static inline uint32_t
27 ror32(uint32_t word, unsigned int shift)
28 {
29 	return (word >> shift) | (word << (32 - shift));
30 }
31 
32 static const uint32_t blake2s_iv[8] = {
33 	0x6A09E667UL, 0xBB67AE85UL, 0x3C6EF372UL, 0xA54FF53AUL,
34 	0x510E527FUL, 0x9B05688CUL, 0x1F83D9ABUL, 0x5BE0CD19UL
35 };
36 
37 static const uint8_t blake2s_sigma[10][16] = {
38 	{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 },
39 	{ 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 },
40 	{ 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4 },
41 	{ 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8 },
42 	{ 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13 },
43 	{ 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9 },
44 	{ 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11 },
45 	{ 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10 },
46 	{ 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5 },
47 	{ 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0 },
48 };
49 
50 static inline void blake2s_set_lastblock(struct blake2s_state *state)
51 {
52 	state->f[0] = -1;
53 }
54 
55 static inline void blake2s_increment_counter(struct blake2s_state *state,
56 					     const uint32_t inc)
57 {
58 	state->t[0] += inc;
59 	state->t[1] += (state->t[0] < inc);
60 }
61 
62 static inline void blake2s_init_param(struct blake2s_state *state,
63 				      const uint32_t param)
64 {
65 	int i;
66 
67 	memset(state, 0, sizeof(*state));
68 	for (i = 0; i < 8; ++i)
69 		state->h[i] = blake2s_iv[i];
70 	state->h[0] ^= param;
71 }
72 
73 void blake2s_init(struct blake2s_state *state, const size_t outlen)
74 {
75 #ifdef DIAGNOSTIC
76 	KASSERT(!(!outlen || outlen > BLAKE2S_HASH_SIZE));
77 #endif
78 	blake2s_init_param(state, 0x01010000 | outlen);
79 	state->outlen = outlen;
80 }
81 
82 void blake2s_init_key(struct blake2s_state *state, const size_t outlen,
83 		      const void *key, const size_t keylen)
84 {
85 	uint8_t block[BLAKE2S_BLOCK_SIZE] = { 0 };
86 
87 #ifdef DIAGNOSTIC
88 	KASSERT(!(!outlen || outlen > BLAKE2S_HASH_SIZE ||
89 		  !key || !keylen || keylen > BLAKE2S_KEY_SIZE));
90 #endif
91 
92 	blake2s_init_param(state, 0x01010000 | keylen << 8 | outlen);
93 	state->outlen = outlen;
94 	memcpy(block, key, keylen);
95 	blake2s_update(state, block, BLAKE2S_BLOCK_SIZE);
96 	explicit_bzero(block, BLAKE2S_BLOCK_SIZE);
97 }
98 
99 static inline void blake2s_compress(struct blake2s_state *state,
100 				    const uint8_t *block, size_t nblocks,
101 				    const uint32_t inc)
102 {
103 	uint32_t m[16];
104 	uint32_t v[16];
105 	int i;
106 
107 #ifdef DIAGNOSTIC
108 	KASSERT(!((nblocks > 1 && inc != BLAKE2S_BLOCK_SIZE)));
109 #endif
110 
111 	while (nblocks > 0) {
112 		blake2s_increment_counter(state, inc);
113 		memcpy(m, block, BLAKE2S_BLOCK_SIZE);
114 		for (i = 0; i < 16; i++)
115 			m[i] = le32toh(m[i]);
116 		memcpy(v, state->h, 32);
117 		v[ 8] = blake2s_iv[0];
118 		v[ 9] = blake2s_iv[1];
119 		v[10] = blake2s_iv[2];
120 		v[11] = blake2s_iv[3];
121 		v[12] = blake2s_iv[4] ^ state->t[0];
122 		v[13] = blake2s_iv[5] ^ state->t[1];
123 		v[14] = blake2s_iv[6] ^ state->f[0];
124 		v[15] = blake2s_iv[7] ^ state->f[1];
125 
126 #define G(r, i, a, b, c, d) do { \
127 	a += b + m[blake2s_sigma[r][2 * i + 0]]; \
128 	d = ror32(d ^ a, 16); \
129 	c += d; \
130 	b = ror32(b ^ c, 12); \
131 	a += b + m[blake2s_sigma[r][2 * i + 1]]; \
132 	d = ror32(d ^ a, 8); \
133 	c += d; \
134 	b = ror32(b ^ c, 7); \
135 } while (0)
136 
137 #define ROUND(r) do { \
138 	G(r, 0, v[0], v[ 4], v[ 8], v[12]); \
139 	G(r, 1, v[1], v[ 5], v[ 9], v[13]); \
140 	G(r, 2, v[2], v[ 6], v[10], v[14]); \
141 	G(r, 3, v[3], v[ 7], v[11], v[15]); \
142 	G(r, 4, v[0], v[ 5], v[10], v[15]); \
143 	G(r, 5, v[1], v[ 6], v[11], v[12]); \
144 	G(r, 6, v[2], v[ 7], v[ 8], v[13]); \
145 	G(r, 7, v[3], v[ 4], v[ 9], v[14]); \
146 } while (0)
147 		ROUND(0);
148 		ROUND(1);
149 		ROUND(2);
150 		ROUND(3);
151 		ROUND(4);
152 		ROUND(5);
153 		ROUND(6);
154 		ROUND(7);
155 		ROUND(8);
156 		ROUND(9);
157 
158 #undef G
159 #undef ROUND
160 
161 		for (i = 0; i < 8; ++i)
162 			state->h[i] ^= v[i] ^ v[i + 8];
163 
164 		block += BLAKE2S_BLOCK_SIZE;
165 		--nblocks;
166 	}
167 }
168 
169 void blake2s_update(struct blake2s_state *state, const uint8_t *in, size_t inlen)
170 {
171 	const size_t fill = BLAKE2S_BLOCK_SIZE - state->buflen;
172 
173 	if (!inlen)
174 		return;
175 	if (inlen > fill) {
176 		memcpy(state->buf + state->buflen, in, fill);
177 		blake2s_compress(state, state->buf, 1, BLAKE2S_BLOCK_SIZE);
178 		state->buflen = 0;
179 		in += fill;
180 		inlen -= fill;
181 	}
182 	if (inlen > BLAKE2S_BLOCK_SIZE) {
183 		const size_t nblocks =
184 			(inlen + BLAKE2S_BLOCK_SIZE - 1) / BLAKE2S_BLOCK_SIZE;
185 		/* Hash one less (full) block than strictly possible */
186 		blake2s_compress(state, in, nblocks - 1, BLAKE2S_BLOCK_SIZE);
187 		in += BLAKE2S_BLOCK_SIZE * (nblocks - 1);
188 		inlen -= BLAKE2S_BLOCK_SIZE * (nblocks - 1);
189 	}
190 	memcpy(state->buf + state->buflen, in, inlen);
191 	state->buflen += inlen;
192 }
193 
194 void blake2s_final(struct blake2s_state *state, uint8_t *out)
195 {
196 	int i;
197 	blake2s_set_lastblock(state);
198 	memset(state->buf + state->buflen, 0,
199 	       BLAKE2S_BLOCK_SIZE - state->buflen); /* Padding */
200 	blake2s_compress(state, state->buf, 1, state->buflen);
201 	for (i = 0; i < 8; i++)
202 		state->h[i] = htole32(state->h[i]);
203 	memcpy(out, state->h, state->outlen);
204 	explicit_bzero(state, sizeof(*state));
205 }
206 
207 void blake2s_hmac(uint8_t *out, const uint8_t *in, const uint8_t *key, const size_t outlen,
208 		  const size_t inlen, const size_t keylen)
209 {
210 	struct blake2s_state state;
211 	uint8_t x_key[BLAKE2S_BLOCK_SIZE] __aligned(__alignof__(uint32_t)) = { 0 };
212 	uint8_t i_hash[BLAKE2S_HASH_SIZE] __aligned(__alignof__(uint32_t));
213 	int i;
214 
215 	if (keylen > BLAKE2S_BLOCK_SIZE) {
216 		blake2s_init(&state, BLAKE2S_HASH_SIZE);
217 		blake2s_update(&state, key, keylen);
218 		blake2s_final(&state, x_key);
219 	} else
220 		memcpy(x_key, key, keylen);
221 
222 	for (i = 0; i < BLAKE2S_BLOCK_SIZE; ++i)
223 		x_key[i] ^= 0x36;
224 
225 	blake2s_init(&state, BLAKE2S_HASH_SIZE);
226 	blake2s_update(&state, x_key, BLAKE2S_BLOCK_SIZE);
227 	blake2s_update(&state, in, inlen);
228 	blake2s_final(&state, i_hash);
229 
230 	for (i = 0; i < BLAKE2S_BLOCK_SIZE; ++i)
231 		x_key[i] ^= 0x5c ^ 0x36;
232 
233 	blake2s_init(&state, BLAKE2S_HASH_SIZE);
234 	blake2s_update(&state, x_key, BLAKE2S_BLOCK_SIZE);
235 	blake2s_update(&state, i_hash, BLAKE2S_HASH_SIZE);
236 	blake2s_final(&state, i_hash);
237 
238 	memcpy(out, i_hash, outlen);
239 	explicit_bzero(x_key, BLAKE2S_BLOCK_SIZE);
240 	explicit_bzero(i_hash, BLAKE2S_HASH_SIZE);
241 }
242