xref: /netbsd-src/crypto/external/bsd/openssl/dist/providers/implementations/digests/blake2b_prov.c (revision 0e2e28bced52bda3788c857106bde6c44d2df3b8)
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 uint64_t blake2b_IV[8] =
24b0d17251Schristos {
25b0d17251Schristos     0x6a09e667f3bcc908ULL, 0xbb67ae8584caa73bULL,
26b0d17251Schristos     0x3c6ef372fe94f82bULL, 0xa54ff53a5f1d36f1ULL,
27b0d17251Schristos     0x510e527fade682d1ULL, 0x9b05688c2b3e6c1fULL,
28b0d17251Schristos     0x1f83d9abfb41bd6bULL, 0x5be0cd19137e2179ULL
29b0d17251Schristos };
30b0d17251Schristos 
31b0d17251Schristos static const uint8_t blake2b_sigma[12][16] =
32b0d17251Schristos {
33b0d17251Schristos     {  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15 } ,
34b0d17251Schristos     { 14, 10,  4,  8,  9, 15, 13,  6,  1, 12,  0,  2, 11,  7,  5,  3 } ,
35b0d17251Schristos     { 11,  8, 12,  0,  5,  2, 15, 13, 10, 14,  3,  6,  7,  1,  9,  4 } ,
36b0d17251Schristos     {  7,  9,  3,  1, 13, 12, 11, 14,  2,  6,  5, 10,  4,  0, 15,  8 } ,
37b0d17251Schristos     {  9,  0,  5,  7,  2,  4, 10, 15, 14,  1, 11, 12,  6,  8,  3, 13 } ,
38b0d17251Schristos     {  2, 12,  6, 10,  0, 11,  8,  3,  4, 13,  7,  5, 15, 14,  1,  9 } ,
39b0d17251Schristos     { 12,  5,  1, 15, 14, 13,  4, 10,  0,  7,  6,  3,  9,  2,  8, 11 } ,
40b0d17251Schristos     { 13, 11,  7, 14, 12,  1,  3,  9,  5,  0, 15,  4,  8,  6,  2, 10 } ,
41b0d17251Schristos     {  6, 15, 14,  9, 11,  3,  0,  8, 12,  2, 13,  7,  1,  4, 10,  5 } ,
42b0d17251Schristos     { 10,  2,  8,  4,  7,  6,  1,  5, 15, 11,  9, 14,  3, 12, 13 , 0 } ,
43b0d17251Schristos     {  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15 } ,
44b0d17251Schristos     { 14, 10,  4,  8,  9, 15, 13,  6,  1, 12,  0,  2, 11,  7,  5,  3 }
45b0d17251Schristos };
46b0d17251Schristos 
47b0d17251Schristos /* Set that it's the last block we'll compress */
blake2b_set_lastblock(BLAKE2B_CTX * S)48b0d17251Schristos static ossl_inline void blake2b_set_lastblock(BLAKE2B_CTX *S)
49b0d17251Schristos {
50b0d17251Schristos     S->f[0] = -1;
51b0d17251Schristos }
52b0d17251Schristos 
53b0d17251Schristos /* Initialize the hashing state. */
blake2b_init0(BLAKE2B_CTX * S)54b0d17251Schristos static ossl_inline void blake2b_init0(BLAKE2B_CTX *S)
55b0d17251Schristos {
56b0d17251Schristos     int i;
57b0d17251Schristos 
58b0d17251Schristos     memset(S, 0, sizeof(BLAKE2B_CTX));
59b0d17251Schristos     for (i = 0; i < 8; ++i) {
60b0d17251Schristos         S->h[i] = blake2b_IV[i];
61b0d17251Schristos     }
62b0d17251Schristos }
63b0d17251Schristos 
64b0d17251Schristos /* init xors IV with input parameter block and sets the output length */
blake2b_init_param(BLAKE2B_CTX * S,const BLAKE2B_PARAM * P)65b0d17251Schristos static void blake2b_init_param(BLAKE2B_CTX *S, const BLAKE2B_PARAM *P)
66b0d17251Schristos {
67b0d17251Schristos     size_t i;
68b0d17251Schristos     const uint8_t *p = (const uint8_t *)(P);
69b0d17251Schristos 
70b0d17251Schristos     blake2b_init0(S);
71b0d17251Schristos     S->outlen = P->digest_length;
72b0d17251Schristos 
73b0d17251Schristos     /* The param struct is carefully hand packed, and should be 64 bytes on
74b0d17251Schristos      * every platform. */
75b0d17251Schristos     assert(sizeof(BLAKE2B_PARAM) == 64);
76b0d17251Schristos     /* IV XOR ParamBlock */
77b0d17251Schristos     for (i = 0; i < 8; ++i) {
78b0d17251Schristos         S->h[i] ^= load64(p + sizeof(S->h[i]) * i);
79b0d17251Schristos     }
80b0d17251Schristos }
81b0d17251Schristos 
82b0d17251Schristos /* Initialize the parameter block with default values */
ossl_blake2b_param_init(BLAKE2B_PARAM * P)83b0d17251Schristos void ossl_blake2b_param_init(BLAKE2B_PARAM *P)
84b0d17251Schristos {
85b0d17251Schristos     P->digest_length = BLAKE2B_DIGEST_LENGTH;
86b0d17251Schristos     P->key_length    = 0;
87b0d17251Schristos     P->fanout        = 1;
88b0d17251Schristos     P->depth         = 1;
89b0d17251Schristos     store32(P->leaf_length, 0);
90b0d17251Schristos     store64(P->node_offset, 0);
91b0d17251Schristos     P->node_depth    = 0;
92b0d17251Schristos     P->inner_length  = 0;
93b0d17251Schristos     memset(P->reserved, 0, sizeof(P->reserved));
94b0d17251Schristos     memset(P->salt,     0, sizeof(P->salt));
95b0d17251Schristos     memset(P->personal, 0, sizeof(P->personal));
96b0d17251Schristos }
97b0d17251Schristos 
ossl_blake2b_param_set_digest_length(BLAKE2B_PARAM * P,uint8_t outlen)98b0d17251Schristos void ossl_blake2b_param_set_digest_length(BLAKE2B_PARAM *P, uint8_t outlen)
99b0d17251Schristos {
100b0d17251Schristos     P->digest_length = outlen;
101b0d17251Schristos }
102b0d17251Schristos 
ossl_blake2b_param_set_key_length(BLAKE2B_PARAM * P,uint8_t keylen)103b0d17251Schristos void ossl_blake2b_param_set_key_length(BLAKE2B_PARAM *P, uint8_t keylen)
104b0d17251Schristos {
105b0d17251Schristos     P->key_length = keylen;
106b0d17251Schristos }
107b0d17251Schristos 
ossl_blake2b_param_set_personal(BLAKE2B_PARAM * P,const uint8_t * personal,size_t len)108b0d17251Schristos void ossl_blake2b_param_set_personal(BLAKE2B_PARAM *P, const uint8_t *personal,
109b0d17251Schristos                                      size_t len)
110b0d17251Schristos {
111b0d17251Schristos     memcpy(P->personal, personal, len);
112b0d17251Schristos     memset(P->personal + len, 0, BLAKE2B_PERSONALBYTES - len);
113b0d17251Schristos }
114b0d17251Schristos 
ossl_blake2b_param_set_salt(BLAKE2B_PARAM * P,const uint8_t * salt,size_t len)115b0d17251Schristos void ossl_blake2b_param_set_salt(BLAKE2B_PARAM *P, const uint8_t *salt,
116b0d17251Schristos                                  size_t len)
117b0d17251Schristos {
118b0d17251Schristos     memcpy(P->salt, salt, len);
119b0d17251Schristos     memset(P->salt + len, 0, BLAKE2B_SALTBYTES - len);
120b0d17251Schristos }
121b0d17251Schristos 
122b0d17251Schristos /*
123b0d17251Schristos  * Initialize the hashing context with the given parameter block.
124b0d17251Schristos  * Always returns 1.
125b0d17251Schristos  */
ossl_blake2b_init(BLAKE2B_CTX * c,const BLAKE2B_PARAM * P)126b0d17251Schristos int ossl_blake2b_init(BLAKE2B_CTX *c, const BLAKE2B_PARAM *P)
127b0d17251Schristos {
128b0d17251Schristos     blake2b_init_param(c, P);
129b0d17251Schristos     return 1;
130b0d17251Schristos }
131b0d17251Schristos 
132b0d17251Schristos /*
133b0d17251Schristos  * Initialize the hashing context with the given parameter block and key.
134b0d17251Schristos  * Always returns 1.
135b0d17251Schristos  */
ossl_blake2b_init_key(BLAKE2B_CTX * c,const BLAKE2B_PARAM * P,const void * key)136b0d17251Schristos int ossl_blake2b_init_key(BLAKE2B_CTX *c, const BLAKE2B_PARAM *P,
137b0d17251Schristos                           const void *key)
138b0d17251Schristos {
139b0d17251Schristos     blake2b_init_param(c, P);
140b0d17251Schristos 
141b0d17251Schristos     /* Pad the key to form first data block */
142b0d17251Schristos     {
143b0d17251Schristos         uint8_t block[BLAKE2B_BLOCKBYTES] = {0};
144b0d17251Schristos 
145b0d17251Schristos         memcpy(block, key, P->key_length);
146b0d17251Schristos         ossl_blake2b_update(c, block, BLAKE2B_BLOCKBYTES);
147b0d17251Schristos         OPENSSL_cleanse(block, BLAKE2B_BLOCKBYTES);
148b0d17251Schristos     }
149b0d17251Schristos 
150b0d17251Schristos     return 1;
151b0d17251Schristos }
152b0d17251Schristos 
153b0d17251Schristos /* Permute the state while xoring in the block of data. */
blake2b_compress(BLAKE2B_CTX * S,const uint8_t * blocks,size_t len)154b0d17251Schristos static void blake2b_compress(BLAKE2B_CTX *S,
155b0d17251Schristos                             const uint8_t *blocks,
156b0d17251Schristos                             size_t len)
157b0d17251Schristos {
158b0d17251Schristos     uint64_t m[16];
159b0d17251Schristos     uint64_t v[16];
160b0d17251Schristos     int i;
161b0d17251Schristos     size_t increment;
162b0d17251Schristos 
163b0d17251Schristos     /*
164b0d17251Schristos      * There are two distinct usage vectors for this function:
165b0d17251Schristos      *
166b0d17251Schristos      * a) BLAKE2b_Update uses it to process complete blocks,
167b0d17251Schristos      *    possibly more than one at a time;
168b0d17251Schristos      *
169b0d17251Schristos      * b) BLAK2b_Final uses it to process last block, always
170b0d17251Schristos      *    single but possibly incomplete, in which case caller
171b0d17251Schristos      *    pads input with zeros.
172b0d17251Schristos      */
173b0d17251Schristos     assert(len < BLAKE2B_BLOCKBYTES || len % BLAKE2B_BLOCKBYTES == 0);
174b0d17251Schristos 
175b0d17251Schristos     /*
176b0d17251Schristos      * Since last block is always processed with separate call,
177b0d17251Schristos      * |len| not being multiple of complete blocks can be observed
178b0d17251Schristos      * only with |len| being less than BLAKE2B_BLOCKBYTES ("less"
179b0d17251Schristos      * including even zero), which is why following assignment doesn't
180b0d17251Schristos      * have to reside inside the main loop below.
181b0d17251Schristos      */
182b0d17251Schristos     increment = len < BLAKE2B_BLOCKBYTES ? len : BLAKE2B_BLOCKBYTES;
183b0d17251Schristos 
184b0d17251Schristos     for (i = 0; i < 8; ++i) {
185b0d17251Schristos         v[i] = S->h[i];
186b0d17251Schristos     }
187b0d17251Schristos 
188b0d17251Schristos     do {
189b0d17251Schristos         for (i = 0; i < 16; ++i) {
190b0d17251Schristos             m[i] = load64(blocks + i * sizeof(m[i]));
191b0d17251Schristos         }
192b0d17251Schristos 
193b0d17251Schristos         /* blake2b_increment_counter */
194b0d17251Schristos         S->t[0] += increment;
195b0d17251Schristos         S->t[1] += (S->t[0] < increment);
196b0d17251Schristos 
197b0d17251Schristos         v[8]  = blake2b_IV[0];
198b0d17251Schristos         v[9]  = blake2b_IV[1];
199b0d17251Schristos         v[10] = blake2b_IV[2];
200b0d17251Schristos         v[11] = blake2b_IV[3];
201b0d17251Schristos         v[12] = S->t[0] ^ blake2b_IV[4];
202b0d17251Schristos         v[13] = S->t[1] ^ blake2b_IV[5];
203b0d17251Schristos         v[14] = S->f[0] ^ blake2b_IV[6];
204b0d17251Schristos         v[15] = S->f[1] ^ blake2b_IV[7];
205b0d17251Schristos #define G(r,i,a,b,c,d) \
206b0d17251Schristos         do { \
207b0d17251Schristos             a = a + b + m[blake2b_sigma[r][2*i+0]]; \
208b0d17251Schristos             d = rotr64(d ^ a, 32); \
209b0d17251Schristos             c = c + d; \
210b0d17251Schristos             b = rotr64(b ^ c, 24); \
211b0d17251Schristos             a = a + b + m[blake2b_sigma[r][2*i+1]]; \
212b0d17251Schristos             d = rotr64(d ^ a, 16); \
213b0d17251Schristos             c = c + d; \
214b0d17251Schristos             b = rotr64(b ^ c, 63); \
215b0d17251Schristos         } while (0)
216b0d17251Schristos #define ROUND(r)  \
217b0d17251Schristos         do { \
218b0d17251Schristos             G(r,0,v[ 0],v[ 4],v[ 8],v[12]); \
219b0d17251Schristos             G(r,1,v[ 1],v[ 5],v[ 9],v[13]); \
220b0d17251Schristos             G(r,2,v[ 2],v[ 6],v[10],v[14]); \
221b0d17251Schristos             G(r,3,v[ 3],v[ 7],v[11],v[15]); \
222b0d17251Schristos             G(r,4,v[ 0],v[ 5],v[10],v[15]); \
223b0d17251Schristos             G(r,5,v[ 1],v[ 6],v[11],v[12]); \
224b0d17251Schristos             G(r,6,v[ 2],v[ 7],v[ 8],v[13]); \
225b0d17251Schristos             G(r,7,v[ 3],v[ 4],v[ 9],v[14]); \
226b0d17251Schristos         } while (0)
227b0d17251Schristos #if defined(OPENSSL_SMALL_FOOTPRINT)
228b0d17251Schristos         /* 3x size reduction on x86_64, almost 7x on ARMv8, 9x on ARMv4 */
229b0d17251Schristos         for (i = 0; i < 12; i++) {
230b0d17251Schristos             ROUND(i);
231b0d17251Schristos         }
232b0d17251Schristos #else
233b0d17251Schristos         ROUND(0);
234b0d17251Schristos         ROUND(1);
235b0d17251Schristos         ROUND(2);
236b0d17251Schristos         ROUND(3);
237b0d17251Schristos         ROUND(4);
238b0d17251Schristos         ROUND(5);
239b0d17251Schristos         ROUND(6);
240b0d17251Schristos         ROUND(7);
241b0d17251Schristos         ROUND(8);
242b0d17251Schristos         ROUND(9);
243b0d17251Schristos         ROUND(10);
244b0d17251Schristos         ROUND(11);
245b0d17251Schristos #endif
246b0d17251Schristos 
247b0d17251Schristos         for (i = 0; i < 8; ++i) {
248b0d17251Schristos             S->h[i] = v[i] ^= v[i + 8] ^ S->h[i];
249b0d17251Schristos         }
250b0d17251Schristos #undef G
251b0d17251Schristos #undef ROUND
252b0d17251Schristos         blocks += increment;
253b0d17251Schristos         len -= increment;
254b0d17251Schristos     } while (len);
255b0d17251Schristos }
256b0d17251Schristos 
257b0d17251Schristos /* Absorb the input data into the hash state.  Always returns 1. */
ossl_blake2b_update(BLAKE2B_CTX * c,const void * data,size_t datalen)258b0d17251Schristos int ossl_blake2b_update(BLAKE2B_CTX *c, const void *data, size_t datalen)
259b0d17251Schristos {
260b0d17251Schristos     const uint8_t *in = data;
261b0d17251Schristos     size_t fill;
262b0d17251Schristos 
263b0d17251Schristos     /*
264b0d17251Schristos      * Intuitively one would expect intermediate buffer, c->buf, to
265b0d17251Schristos      * store incomplete blocks. But in this case we are interested to
266b0d17251Schristos      * temporarily stash even complete blocks, because last one in the
267b0d17251Schristos      * stream has to be treated in special way, and at this point we
268b0d17251Schristos      * don't know if last block in *this* call is last one "ever". This
269b0d17251Schristos      * is the reason for why |datalen| is compared as >, and not >=.
270b0d17251Schristos      */
271b0d17251Schristos     fill = sizeof(c->buf) - c->buflen;
272b0d17251Schristos     if (datalen > fill) {
273b0d17251Schristos         if (c->buflen) {
274b0d17251Schristos             memcpy(c->buf + c->buflen, in, fill); /* Fill buffer */
275b0d17251Schristos             blake2b_compress(c, c->buf, BLAKE2B_BLOCKBYTES);
276b0d17251Schristos             c->buflen = 0;
277b0d17251Schristos             in += fill;
278b0d17251Schristos             datalen -= fill;
279b0d17251Schristos         }
280b0d17251Schristos         if (datalen > BLAKE2B_BLOCKBYTES) {
281b0d17251Schristos             size_t stashlen = datalen % BLAKE2B_BLOCKBYTES;
282b0d17251Schristos             /*
283b0d17251Schristos              * If |datalen| is a multiple of the blocksize, stash
284b0d17251Schristos              * last complete block, it can be final one...
285b0d17251Schristos              */
286b0d17251Schristos             stashlen = stashlen ? stashlen : BLAKE2B_BLOCKBYTES;
287b0d17251Schristos             datalen -= stashlen;
288b0d17251Schristos             blake2b_compress(c, in, datalen);
289b0d17251Schristos             in += datalen;
290b0d17251Schristos             datalen = stashlen;
291b0d17251Schristos         }
292b0d17251Schristos     }
293b0d17251Schristos 
294b0d17251Schristos     assert(datalen <= BLAKE2B_BLOCKBYTES);
295b0d17251Schristos 
296b0d17251Schristos     memcpy(c->buf + c->buflen, in, datalen);
297b0d17251Schristos     c->buflen += datalen; /* Be lazy, do not compress */
298b0d17251Schristos 
299b0d17251Schristos     return 1;
300b0d17251Schristos }
301b0d17251Schristos 
302b0d17251Schristos /*
303b0d17251Schristos  * Calculate the final hash and save it in md.
304b0d17251Schristos  * Always returns 1.
305b0d17251Schristos  */
ossl_blake2b_final(unsigned char * md,BLAKE2B_CTX * c)306b0d17251Schristos int ossl_blake2b_final(unsigned char *md, BLAKE2B_CTX *c)
307b0d17251Schristos {
308b0d17251Schristos     uint8_t outbuffer[BLAKE2B_OUTBYTES] = {0};
309b0d17251Schristos     uint8_t *target = outbuffer;
310b0d17251Schristos     int iter = (c->outlen + 7) / 8;
311b0d17251Schristos     int i;
312b0d17251Schristos 
313b0d17251Schristos     /* Avoid writing to the temporary buffer if possible */
314b0d17251Schristos     if ((c->outlen % sizeof(c->h[0])) == 0)
315b0d17251Schristos         target = md;
316b0d17251Schristos 
317b0d17251Schristos     blake2b_set_lastblock(c);
318b0d17251Schristos     /* Padding */
319b0d17251Schristos     memset(c->buf + c->buflen, 0, sizeof(c->buf) - c->buflen);
320b0d17251Schristos     blake2b_compress(c, c->buf, c->buflen);
321b0d17251Schristos 
322b0d17251Schristos     /* Output full hash to buffer */
323b0d17251Schristos     for (i = 0; i < iter; ++i)
324b0d17251Schristos         store64(target + sizeof(c->h[i]) * i, c->h[i]);
325b0d17251Schristos 
326*0e2e28bcSchristos     if (target != md) {
327b0d17251Schristos         memcpy(md, target, c->outlen);
328*0e2e28bcSchristos         OPENSSL_cleanse(target, sizeof(outbuffer));
329*0e2e28bcSchristos     }
330b0d17251Schristos 
331b0d17251Schristos     OPENSSL_cleanse(c, sizeof(BLAKE2B_CTX));
332b0d17251Schristos     return 1;
333b0d17251Schristos }
334