1b0d17251Schristos /*
2*0e2e28bcSchristos * Copyright 2016-2024 The OpenSSL Project Authors. All Rights Reserved.
3b0d17251Schristos *
4b0d17251Schristos * Licensed under the Apache License 2.0 (the "License"). You may not use
5b0d17251Schristos * this file except in compliance with the License. You can obtain a copy
6b0d17251Schristos * in the file LICENSE in the source distribution or at
7b0d17251Schristos * https://www.openssl.org/source/license.html
8b0d17251Schristos */
9b0d17251Schristos
10b0d17251Schristos /*
11b0d17251Schristos * Derived from the BLAKE2 reference implementation written by Samuel Neves.
12b0d17251Schristos * Copyright 2012, Samuel Neves <sneves@dei.uc.pt>
13b0d17251Schristos * More information about the BLAKE2 hash function and its implementations
14b0d17251Schristos * can be found at https://blake2.net.
15b0d17251Schristos */
16b0d17251Schristos
17b0d17251Schristos #include <assert.h>
18b0d17251Schristos #include <string.h>
19b0d17251Schristos #include <openssl/crypto.h>
20b0d17251Schristos #include "blake2_impl.h"
21b0d17251Schristos #include "prov/blake2.h"
22b0d17251Schristos
23b0d17251Schristos static const uint32_t blake2s_IV[8] =
24b0d17251Schristos {
25b0d17251Schristos 0x6A09E667U, 0xBB67AE85U, 0x3C6EF372U, 0xA54FF53AU,
26b0d17251Schristos 0x510E527FU, 0x9B05688CU, 0x1F83D9ABU, 0x5BE0CD19U
27b0d17251Schristos };
28b0d17251Schristos
29b0d17251Schristos static const uint8_t blake2s_sigma[10][16] =
30b0d17251Schristos {
31b0d17251Schristos { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 } ,
32b0d17251Schristos { 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 } ,
33b0d17251Schristos { 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4 } ,
34b0d17251Schristos { 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8 } ,
35b0d17251Schristos { 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13 } ,
36b0d17251Schristos { 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9 } ,
37b0d17251Schristos { 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11 } ,
38b0d17251Schristos { 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10 } ,
39b0d17251Schristos { 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5 } ,
40b0d17251Schristos { 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13 , 0 } ,
41b0d17251Schristos };
42b0d17251Schristos
43b0d17251Schristos /* Set that it's the last block we'll compress */
blake2s_set_lastblock(BLAKE2S_CTX * S)44b0d17251Schristos static ossl_inline void blake2s_set_lastblock(BLAKE2S_CTX *S)
45b0d17251Schristos {
46b0d17251Schristos S->f[0] = -1;
47b0d17251Schristos }
48b0d17251Schristos
49b0d17251Schristos /* Initialize the hashing state. */
blake2s_init0(BLAKE2S_CTX * S)50b0d17251Schristos static ossl_inline void blake2s_init0(BLAKE2S_CTX *S)
51b0d17251Schristos {
52b0d17251Schristos int i;
53b0d17251Schristos
54b0d17251Schristos memset(S, 0, sizeof(BLAKE2S_CTX));
55b0d17251Schristos for (i = 0; i < 8; ++i) {
56b0d17251Schristos S->h[i] = blake2s_IV[i];
57b0d17251Schristos }
58b0d17251Schristos }
59b0d17251Schristos
60b0d17251Schristos /* init xors IV with input parameter block and sets the output length */
blake2s_init_param(BLAKE2S_CTX * S,const BLAKE2S_PARAM * P)61b0d17251Schristos static void blake2s_init_param(BLAKE2S_CTX *S, const BLAKE2S_PARAM *P)
62b0d17251Schristos {
63b0d17251Schristos size_t i;
64b0d17251Schristos const uint8_t *p = (const uint8_t *)(P);
65b0d17251Schristos
66b0d17251Schristos blake2s_init0(S);
67b0d17251Schristos S->outlen = P->digest_length;
68b0d17251Schristos
69b0d17251Schristos /* The param struct is carefully hand packed, and should be 32 bytes on
70b0d17251Schristos * every platform. */
71b0d17251Schristos assert(sizeof(BLAKE2S_PARAM) == 32);
72b0d17251Schristos /* IV XOR ParamBlock */
73b0d17251Schristos for (i = 0; i < 8; ++i) {
74b0d17251Schristos S->h[i] ^= load32(&p[i*4]);
75b0d17251Schristos }
76b0d17251Schristos }
77b0d17251Schristos
ossl_blake2s_param_init(BLAKE2S_PARAM * P)78b0d17251Schristos void ossl_blake2s_param_init(BLAKE2S_PARAM *P)
79b0d17251Schristos {
80b0d17251Schristos P->digest_length = BLAKE2S_DIGEST_LENGTH;
81b0d17251Schristos P->key_length = 0;
82b0d17251Schristos P->fanout = 1;
83b0d17251Schristos P->depth = 1;
84b0d17251Schristos store32(P->leaf_length, 0);
85b0d17251Schristos store48(P->node_offset, 0);
86b0d17251Schristos P->node_depth = 0;
87b0d17251Schristos P->inner_length = 0;
88b0d17251Schristos memset(P->salt, 0, sizeof(P->salt));
89b0d17251Schristos memset(P->personal, 0, sizeof(P->personal));
90b0d17251Schristos }
91b0d17251Schristos
ossl_blake2s_param_set_digest_length(BLAKE2S_PARAM * P,uint8_t outlen)92b0d17251Schristos void ossl_blake2s_param_set_digest_length(BLAKE2S_PARAM *P, uint8_t outlen)
93b0d17251Schristos {
94b0d17251Schristos P->digest_length = outlen;
95b0d17251Schristos }
96b0d17251Schristos
ossl_blake2s_param_set_key_length(BLAKE2S_PARAM * P,uint8_t keylen)97b0d17251Schristos void ossl_blake2s_param_set_key_length(BLAKE2S_PARAM *P, uint8_t keylen)
98b0d17251Schristos {
99b0d17251Schristos P->key_length = keylen;
100b0d17251Schristos }
101b0d17251Schristos
ossl_blake2s_param_set_personal(BLAKE2S_PARAM * P,const uint8_t * personal,size_t len)102b0d17251Schristos void ossl_blake2s_param_set_personal(BLAKE2S_PARAM *P, const uint8_t *personal,
103b0d17251Schristos size_t len)
104b0d17251Schristos {
105b0d17251Schristos memcpy(P->personal, personal, len);
106b0d17251Schristos memset(P->personal + len, 0, BLAKE2S_PERSONALBYTES - len);
107b0d17251Schristos }
108b0d17251Schristos
ossl_blake2s_param_set_salt(BLAKE2S_PARAM * P,const uint8_t * salt,size_t len)109b0d17251Schristos void ossl_blake2s_param_set_salt(BLAKE2S_PARAM *P, const uint8_t *salt,
110b0d17251Schristos size_t len)
111b0d17251Schristos {
112b0d17251Schristos memcpy(P->salt, salt, len);
113b0d17251Schristos memset(P->salt + len, 0, BLAKE2S_SALTBYTES - len);}
114b0d17251Schristos
115b0d17251Schristos /*
116b0d17251Schristos * Initialize the hashing context with the given parameter block.
117b0d17251Schristos * Always returns 1.
118b0d17251Schristos */
ossl_blake2s_init(BLAKE2S_CTX * c,const BLAKE2S_PARAM * P)119b0d17251Schristos int ossl_blake2s_init(BLAKE2S_CTX *c, const BLAKE2S_PARAM *P)
120b0d17251Schristos {
121b0d17251Schristos blake2s_init_param(c, P);
122b0d17251Schristos return 1;
123b0d17251Schristos }
124b0d17251Schristos
125b0d17251Schristos /*
126b0d17251Schristos * Initialize the hashing context with the given parameter block and key.
127b0d17251Schristos * Always returns 1.
128b0d17251Schristos */
ossl_blake2s_init_key(BLAKE2S_CTX * c,const BLAKE2S_PARAM * P,const void * key)129b0d17251Schristos int ossl_blake2s_init_key(BLAKE2S_CTX *c, const BLAKE2S_PARAM *P,
130b0d17251Schristos const void *key)
131b0d17251Schristos {
132b0d17251Schristos blake2s_init_param(c, P);
133b0d17251Schristos
134b0d17251Schristos /* Pad the key to form first data block */
135b0d17251Schristos {
136b0d17251Schristos uint8_t block[BLAKE2S_BLOCKBYTES] = {0};
137b0d17251Schristos
138b0d17251Schristos memcpy(block, key, P->key_length);
139b0d17251Schristos ossl_blake2s_update(c, block, BLAKE2S_BLOCKBYTES);
140b0d17251Schristos OPENSSL_cleanse(block, BLAKE2S_BLOCKBYTES);
141b0d17251Schristos }
142b0d17251Schristos
143b0d17251Schristos return 1;
144b0d17251Schristos }
145b0d17251Schristos
146b0d17251Schristos /* Permute the state while xoring in the block of data. */
blake2s_compress(BLAKE2S_CTX * S,const uint8_t * blocks,size_t len)147b0d17251Schristos static void blake2s_compress(BLAKE2S_CTX *S,
148b0d17251Schristos const uint8_t *blocks,
149b0d17251Schristos size_t len)
150b0d17251Schristos {
151b0d17251Schristos uint32_t m[16];
152b0d17251Schristos uint32_t v[16];
153b0d17251Schristos size_t i;
154b0d17251Schristos size_t increment;
155b0d17251Schristos
156b0d17251Schristos /*
157b0d17251Schristos * There are two distinct usage vectors for this function:
158b0d17251Schristos *
159b0d17251Schristos * a) BLAKE2s_Update uses it to process complete blocks,
160b0d17251Schristos * possibly more than one at a time;
161b0d17251Schristos *
162b0d17251Schristos * b) BLAK2s_Final uses it to process last block, always
163b0d17251Schristos * single but possibly incomplete, in which case caller
164b0d17251Schristos * pads input with zeros.
165b0d17251Schristos */
166b0d17251Schristos assert(len < BLAKE2S_BLOCKBYTES || len % BLAKE2S_BLOCKBYTES == 0);
167b0d17251Schristos
168b0d17251Schristos /*
169b0d17251Schristos * Since last block is always processed with separate call,
170b0d17251Schristos * |len| not being multiple of complete blocks can be observed
171b0d17251Schristos * only with |len| being less than BLAKE2S_BLOCKBYTES ("less"
172b0d17251Schristos * including even zero), which is why following assignment doesn't
173b0d17251Schristos * have to reside inside the main loop below.
174b0d17251Schristos */
175b0d17251Schristos increment = len < BLAKE2S_BLOCKBYTES ? len : BLAKE2S_BLOCKBYTES;
176b0d17251Schristos
177b0d17251Schristos for (i = 0; i < 8; ++i) {
178b0d17251Schristos v[i] = S->h[i];
179b0d17251Schristos }
180b0d17251Schristos
181b0d17251Schristos do {
182b0d17251Schristos for (i = 0; i < 16; ++i) {
183b0d17251Schristos m[i] = load32(blocks + i * sizeof(m[i]));
184b0d17251Schristos }
185b0d17251Schristos
186b0d17251Schristos /* blake2s_increment_counter */
187b0d17251Schristos S->t[0] += increment;
188b0d17251Schristos S->t[1] += (S->t[0] < increment);
189b0d17251Schristos
190b0d17251Schristos v[ 8] = blake2s_IV[0];
191b0d17251Schristos v[ 9] = blake2s_IV[1];
192b0d17251Schristos v[10] = blake2s_IV[2];
193b0d17251Schristos v[11] = blake2s_IV[3];
194b0d17251Schristos v[12] = S->t[0] ^ blake2s_IV[4];
195b0d17251Schristos v[13] = S->t[1] ^ blake2s_IV[5];
196b0d17251Schristos v[14] = S->f[0] ^ blake2s_IV[6];
197b0d17251Schristos v[15] = S->f[1] ^ blake2s_IV[7];
198b0d17251Schristos #define G(r,i,a,b,c,d) \
199b0d17251Schristos do { \
200b0d17251Schristos a = a + b + m[blake2s_sigma[r][2*i+0]]; \
201b0d17251Schristos d = rotr32(d ^ a, 16); \
202b0d17251Schristos c = c + d; \
203b0d17251Schristos b = rotr32(b ^ c, 12); \
204b0d17251Schristos a = a + b + m[blake2s_sigma[r][2*i+1]]; \
205b0d17251Schristos d = rotr32(d ^ a, 8); \
206b0d17251Schristos c = c + d; \
207b0d17251Schristos b = rotr32(b ^ c, 7); \
208b0d17251Schristos } while (0)
209b0d17251Schristos #define ROUND(r) \
210b0d17251Schristos do { \
211b0d17251Schristos G(r,0,v[ 0],v[ 4],v[ 8],v[12]); \
212b0d17251Schristos G(r,1,v[ 1],v[ 5],v[ 9],v[13]); \
213b0d17251Schristos G(r,2,v[ 2],v[ 6],v[10],v[14]); \
214b0d17251Schristos G(r,3,v[ 3],v[ 7],v[11],v[15]); \
215b0d17251Schristos G(r,4,v[ 0],v[ 5],v[10],v[15]); \
216b0d17251Schristos G(r,5,v[ 1],v[ 6],v[11],v[12]); \
217b0d17251Schristos G(r,6,v[ 2],v[ 7],v[ 8],v[13]); \
218b0d17251Schristos G(r,7,v[ 3],v[ 4],v[ 9],v[14]); \
219b0d17251Schristos } while (0)
220b0d17251Schristos #if defined(OPENSSL_SMALL_FOOTPRINT)
221b0d17251Schristos /* almost 3x reduction on x86_64, 4.5x on ARMv8, 4x on ARMv4 */
222b0d17251Schristos for (i = 0; i < 10; i++) {
223b0d17251Schristos ROUND(i);
224b0d17251Schristos }
225b0d17251Schristos #else
226b0d17251Schristos ROUND(0);
227b0d17251Schristos ROUND(1);
228b0d17251Schristos ROUND(2);
229b0d17251Schristos ROUND(3);
230b0d17251Schristos ROUND(4);
231b0d17251Schristos ROUND(5);
232b0d17251Schristos ROUND(6);
233b0d17251Schristos ROUND(7);
234b0d17251Schristos ROUND(8);
235b0d17251Schristos ROUND(9);
236b0d17251Schristos #endif
237b0d17251Schristos
238b0d17251Schristos for (i = 0; i < 8; ++i) {
239b0d17251Schristos S->h[i] = v[i] ^= v[i + 8] ^ S->h[i];
240b0d17251Schristos }
241b0d17251Schristos #undef G
242b0d17251Schristos #undef ROUND
243b0d17251Schristos blocks += increment;
244b0d17251Schristos len -= increment;
245b0d17251Schristos } while (len);
246b0d17251Schristos }
247b0d17251Schristos
248b0d17251Schristos /* Absorb the input data into the hash state. Always returns 1. */
ossl_blake2s_update(BLAKE2S_CTX * c,const void * data,size_t datalen)249b0d17251Schristos int ossl_blake2s_update(BLAKE2S_CTX *c, const void *data, size_t datalen)
250b0d17251Schristos {
251b0d17251Schristos const uint8_t *in = data;
252b0d17251Schristos size_t fill;
253b0d17251Schristos
254b0d17251Schristos /*
255b0d17251Schristos * Intuitively one would expect intermediate buffer, c->buf, to
256b0d17251Schristos * store incomplete blocks. But in this case we are interested to
257b0d17251Schristos * temporarily stash even complete blocks, because last one in the
258b0d17251Schristos * stream has to be treated in special way, and at this point we
259b0d17251Schristos * don't know if last block in *this* call is last one "ever". This
260b0d17251Schristos * is the reason for why |datalen| is compared as >, and not >=.
261b0d17251Schristos */
262b0d17251Schristos fill = sizeof(c->buf) - c->buflen;
263b0d17251Schristos if (datalen > fill) {
264b0d17251Schristos if (c->buflen) {
265b0d17251Schristos memcpy(c->buf + c->buflen, in, fill); /* Fill buffer */
266b0d17251Schristos blake2s_compress(c, c->buf, BLAKE2S_BLOCKBYTES);
267b0d17251Schristos c->buflen = 0;
268b0d17251Schristos in += fill;
269b0d17251Schristos datalen -= fill;
270b0d17251Schristos }
271b0d17251Schristos if (datalen > BLAKE2S_BLOCKBYTES) {
272b0d17251Schristos size_t stashlen = datalen % BLAKE2S_BLOCKBYTES;
273b0d17251Schristos /*
274b0d17251Schristos * If |datalen| is a multiple of the blocksize, stash
275b0d17251Schristos * last complete block, it can be final one...
276b0d17251Schristos */
277b0d17251Schristos stashlen = stashlen ? stashlen : BLAKE2S_BLOCKBYTES;
278b0d17251Schristos datalen -= stashlen;
279b0d17251Schristos blake2s_compress(c, in, datalen);
280b0d17251Schristos in += datalen;
281b0d17251Schristos datalen = stashlen;
282b0d17251Schristos }
283b0d17251Schristos }
284b0d17251Schristos
285b0d17251Schristos assert(datalen <= BLAKE2S_BLOCKBYTES);
286b0d17251Schristos
287b0d17251Schristos memcpy(c->buf + c->buflen, in, datalen);
288b0d17251Schristos c->buflen += datalen; /* Be lazy, do not compress */
289b0d17251Schristos
290b0d17251Schristos return 1;
291b0d17251Schristos }
292b0d17251Schristos
293b0d17251Schristos /*
294b0d17251Schristos * Calculate the final hash and save it in md.
295b0d17251Schristos * Always returns 1.
296b0d17251Schristos */
ossl_blake2s_final(unsigned char * md,BLAKE2S_CTX * c)297b0d17251Schristos int ossl_blake2s_final(unsigned char *md, BLAKE2S_CTX *c)
298b0d17251Schristos {
299b0d17251Schristos uint8_t outbuffer[BLAKE2S_OUTBYTES] = {0};
300b0d17251Schristos uint8_t *target = outbuffer;
301b0d17251Schristos int iter = (c->outlen + 3) / 4;
302b0d17251Schristos int i;
303b0d17251Schristos
304b0d17251Schristos /* Avoid writing to the temporary buffer if possible */
305b0d17251Schristos if ((c->outlen % sizeof(c->h[0])) == 0)
306b0d17251Schristos target = md;
307b0d17251Schristos
308b0d17251Schristos blake2s_set_lastblock(c);
309b0d17251Schristos /* Padding */
310b0d17251Schristos memset(c->buf + c->buflen, 0, sizeof(c->buf) - c->buflen);
311b0d17251Schristos blake2s_compress(c, c->buf, c->buflen);
312b0d17251Schristos
313b0d17251Schristos /* Output full hash to buffer */
314b0d17251Schristos for (i = 0; i < iter; ++i)
315b0d17251Schristos store32(target + sizeof(c->h[i]) * i, c->h[i]);
316b0d17251Schristos
317*0e2e28bcSchristos if (target != md) {
318b0d17251Schristos memcpy(md, target, c->outlen);
319*0e2e28bcSchristos OPENSSL_cleanse(target, sizeof(outbuffer));
320*0e2e28bcSchristos }
321b0d17251Schristos
322b0d17251Schristos OPENSSL_cleanse(c, sizeof(BLAKE2S_CTX));
323b0d17251Schristos return 1;
324b0d17251Schristos }
325