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
ror32(uint32_t word,unsigned int shift)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
blake2s_set_lastblock(struct blake2s_state * state)51 static inline void blake2s_set_lastblock(struct blake2s_state *state)
52 {
53 state->f[0] = -1;
54 }
55
blake2s_increment_counter(struct blake2s_state * state,uint32_t inc)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
blake2s_init_param(struct blake2s_state * state,uint32_t param)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
blake2s_init(struct blake2s_state * state,size_t outlen)74 void blake2s_init(struct blake2s_state *state, size_t outlen)
75 {
76 KKASSERT(outlen > 0 && outlen <= BLAKE2S_HASH_SIZE);
77
78 blake2s_init_param(state, 0x01010000 | outlen);
79 state->outlen = outlen;
80 }
81
blake2s_init_key(struct blake2s_state * state,size_t outlen,const void * key,size_t keylen)82 void blake2s_init_key(struct blake2s_state *state, size_t outlen,
83 const void *key, size_t keylen)
84 {
85 uint8_t block[BLAKE2S_BLOCK_SIZE] = { 0 };
86
87 KKASSERT(outlen > 0 && outlen <= BLAKE2S_HASH_SIZE);
88 KKASSERT(key != NULL && keylen > 0 && keylen <= BLAKE2S_KEY_SIZE);
89
90 blake2s_init_param(state, 0x01010000 | keylen << 8 | outlen);
91 state->outlen = outlen;
92 memcpy(block, key, keylen);
93 blake2s_update(state, block, BLAKE2S_BLOCK_SIZE);
94 explicit_bzero(block, BLAKE2S_BLOCK_SIZE);
95 }
96
blake2s_compress(struct blake2s_state * state,const uint8_t * block,size_t nblocks,uint32_t inc)97 static inline void blake2s_compress(struct blake2s_state *state,
98 const uint8_t *block, size_t nblocks,
99 uint32_t inc)
100 {
101 uint32_t m[16];
102 uint32_t v[16];
103 int i;
104
105 KKASSERT(!((nblocks > 1 && inc != BLAKE2S_BLOCK_SIZE)));
106
107 while (nblocks > 0) {
108 blake2s_increment_counter(state, inc);
109 memcpy(m, block, BLAKE2S_BLOCK_SIZE);
110 for (i = 0; i < 16; i++)
111 m[i] = le32toh(m[i]);
112 memcpy(v, state->h, 32);
113 v[ 8] = blake2s_iv[0];
114 v[ 9] = blake2s_iv[1];
115 v[10] = blake2s_iv[2];
116 v[11] = blake2s_iv[3];
117 v[12] = blake2s_iv[4] ^ state->t[0];
118 v[13] = blake2s_iv[5] ^ state->t[1];
119 v[14] = blake2s_iv[6] ^ state->f[0];
120 v[15] = blake2s_iv[7] ^ state->f[1];
121
122 #define G(r, i, a, b, c, d) do { \
123 a += b + m[blake2s_sigma[r][2 * i + 0]]; \
124 d = ror32(d ^ a, 16); \
125 c += d; \
126 b = ror32(b ^ c, 12); \
127 a += b + m[blake2s_sigma[r][2 * i + 1]]; \
128 d = ror32(d ^ a, 8); \
129 c += d; \
130 b = ror32(b ^ c, 7); \
131 } while (0)
132
133 #define ROUND(r) do { \
134 G(r, 0, v[0], v[ 4], v[ 8], v[12]); \
135 G(r, 1, v[1], v[ 5], v[ 9], v[13]); \
136 G(r, 2, v[2], v[ 6], v[10], v[14]); \
137 G(r, 3, v[3], v[ 7], v[11], v[15]); \
138 G(r, 4, v[0], v[ 5], v[10], v[15]); \
139 G(r, 5, v[1], v[ 6], v[11], v[12]); \
140 G(r, 6, v[2], v[ 7], v[ 8], v[13]); \
141 G(r, 7, v[3], v[ 4], v[ 9], v[14]); \
142 } while (0)
143 ROUND(0);
144 ROUND(1);
145 ROUND(2);
146 ROUND(3);
147 ROUND(4);
148 ROUND(5);
149 ROUND(6);
150 ROUND(7);
151 ROUND(8);
152 ROUND(9);
153
154 #undef G
155 #undef ROUND
156
157 for (i = 0; i < 8; ++i)
158 state->h[i] ^= v[i] ^ v[i + 8];
159
160 block += BLAKE2S_BLOCK_SIZE;
161 --nblocks;
162 }
163 }
164
blake2s_update(struct blake2s_state * state,const uint8_t * in,size_t inlen)165 void blake2s_update(struct blake2s_state *state, const uint8_t *in, size_t inlen)
166 {
167 const size_t fill = BLAKE2S_BLOCK_SIZE - state->buflen;
168
169 if (!inlen)
170 return;
171 if (inlen > fill) {
172 memcpy(state->buf + state->buflen, in, fill);
173 blake2s_compress(state, state->buf, 1, BLAKE2S_BLOCK_SIZE);
174 state->buflen = 0;
175 in += fill;
176 inlen -= fill;
177 }
178 if (inlen > BLAKE2S_BLOCK_SIZE) {
179 const size_t nblocks =
180 (inlen + BLAKE2S_BLOCK_SIZE - 1) / BLAKE2S_BLOCK_SIZE;
181 /* Hash one less (full) block than strictly possible */
182 blake2s_compress(state, in, nblocks - 1, BLAKE2S_BLOCK_SIZE);
183 in += BLAKE2S_BLOCK_SIZE * (nblocks - 1);
184 inlen -= BLAKE2S_BLOCK_SIZE * (nblocks - 1);
185 }
186 memcpy(state->buf + state->buflen, in, inlen);
187 state->buflen += inlen;
188 }
189
blake2s_final(struct blake2s_state * state,uint8_t * out)190 void blake2s_final(struct blake2s_state *state, uint8_t *out)
191 {
192 int i;
193
194 blake2s_set_lastblock(state);
195 memset(state->buf + state->buflen, 0,
196 BLAKE2S_BLOCK_SIZE - state->buflen); /* Padding */
197 blake2s_compress(state, state->buf, 1, state->buflen);
198 for (i = 0; i < 8; i++)
199 state->h[i] = htole32(state->h[i]);
200 memcpy(out, state->h, state->outlen);
201 explicit_bzero(state, sizeof(*state));
202 }
203