Coverage Report

Created: 2025-06-13 06:58

/src/openssl32/providers/implementations/digests/blake2b_prov.c
Line
Count
Source
1
/*
2
 * Copyright 2016-2024 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License 2.0 (the "License").  You may not use
5
 * this file except in compliance with the License.  You can obtain a copy
6
 * in the file LICENSE in the source distribution or at
7
 * https://www.openssl.org/source/license.html
8
 */
9
10
/*
11
 * Derived from the BLAKE2 reference implementation written by Samuel Neves.
12
 * Copyright 2012, Samuel Neves <sneves@dei.uc.pt>
13
 * More information about the BLAKE2 hash function and its implementations
14
 * can be found at https://blake2.net.
15
 */
16
17
#include <assert.h>
18
#include <string.h>
19
#include <openssl/crypto.h>
20
#include <openssl/core_names.h>
21
#include <openssl/proverr.h>
22
#include <openssl/err.h>
23
#include "internal/numbers.h"
24
#include "blake2_impl.h"
25
#include "prov/blake2.h"
26
27
static const OSSL_PARAM known_blake2b_ctx_params[] = {
28
    {OSSL_DIGEST_PARAM_SIZE, OSSL_PARAM_UNSIGNED_INTEGER, NULL, 0, 0},
29
    OSSL_PARAM_END
30
};
31
32
const OSSL_PARAM *ossl_blake2b_gettable_ctx_params(ossl_unused void *ctx,
33
                                                   ossl_unused void *pctx)
34
27.6k
{
35
27.6k
    return known_blake2b_ctx_params;
36
27.6k
}
37
38
const OSSL_PARAM *ossl_blake2b_settable_ctx_params(ossl_unused void *ctx,
39
                                                   ossl_unused void *pctx)
40
1
{
41
1
    return known_blake2b_ctx_params;
42
1
}
43
44
int ossl_blake2b_get_ctx_params(void *vctx, OSSL_PARAM params[])
45
{
46
    struct blake2b_md_data_st *mdctx = vctx;
47
    OSSL_PARAM *p;
48
49
    BLAKE2B_CTX *ctx = &mdctx->ctx;
50
51
    if (ctx == NULL)
52
        return 0;
53
    if (params == NULL)
54
        return 1;
55
56
    p = OSSL_PARAM_locate(params, OSSL_DIGEST_PARAM_SIZE);
57
    if (p != NULL
58
        && !OSSL_PARAM_set_uint(p, (unsigned int)mdctx->params.digest_length)) {
59
        ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
60
        return 0;
61
    }
62
63
    return 1;
64
}
65
66
int ossl_blake2b_set_ctx_params(void *vctx, const OSSL_PARAM params[])
67
{
68
    size_t size;
69
    struct blake2b_md_data_st *mdctx = vctx;
70
    const OSSL_PARAM *p;
71
72
    BLAKE2B_CTX *ctx = &mdctx->ctx;
73
74
    if (ctx == NULL)
75
        return 0;
76
    if (params == NULL)
77
        return 1;
78
79
    p = OSSL_PARAM_locate_const(params, OSSL_DIGEST_PARAM_SIZE);
80
    if (p != NULL) {
81
        if (!OSSL_PARAM_get_size_t(p, &size)) {
82
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
83
            return 0;
84
        }
85
        if (size < 1 || size > BLAKE2B_OUTBYTES) {
86
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_SIZE);
87
            return 0;
88
        }
89
        ossl_blake2b_param_set_digest_length(&mdctx->params, (uint8_t)size);
90
    }
91
92
    return 1;
93
}
94
95
static const uint64_t blake2b_IV[8] =
96
{
97
    0x6a09e667f3bcc908ULL, 0xbb67ae8584caa73bULL,
98
    0x3c6ef372fe94f82bULL, 0xa54ff53a5f1d36f1ULL,
99
    0x510e527fade682d1ULL, 0x9b05688c2b3e6c1fULL,
100
    0x1f83d9abfb41bd6bULL, 0x5be0cd19137e2179ULL
101
};
102
103
static const uint8_t blake2b_sigma[12][16] =
104
{
105
    {  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15 } ,
106
    { 14, 10,  4,  8,  9, 15, 13,  6,  1, 12,  0,  2, 11,  7,  5,  3 } ,
107
    { 11,  8, 12,  0,  5,  2, 15, 13, 10, 14,  3,  6,  7,  1,  9,  4 } ,
108
    {  7,  9,  3,  1, 13, 12, 11, 14,  2,  6,  5, 10,  4,  0, 15,  8 } ,
109
    {  9,  0,  5,  7,  2,  4, 10, 15, 14,  1, 11, 12,  6,  8,  3, 13 } ,
110
    {  2, 12,  6, 10,  0, 11,  8,  3,  4, 13,  7,  5, 15, 14,  1,  9 } ,
111
    { 12,  5,  1, 15, 14, 13,  4, 10,  0,  7,  6,  3,  9,  2,  8, 11 } ,
112
    { 13, 11,  7, 14, 12,  1,  3,  9,  5,  0, 15,  4,  8,  6,  2, 10 } ,
113
    {  6, 15, 14,  9, 11,  3,  0,  8, 12,  2, 13,  7,  1,  4, 10,  5 } ,
114
    { 10,  2,  8,  4,  7,  6,  1,  5, 15, 11,  9, 14,  3, 12, 13 , 0 } ,
115
    {  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15 } ,
116
    { 14, 10,  4,  8,  9, 15, 13,  6,  1, 12,  0,  2, 11,  7,  5,  3 }
117
};
118
119
/* Set that it's the last block we'll compress */
120
static ossl_inline void blake2b_set_lastblock(BLAKE2B_CTX *S)
121
28.5k
{
122
28.5k
    S->f[0] = -1;
123
28.5k
}
124
125
/* Initialize the hashing state. */
126
static ossl_inline void blake2b_init0(BLAKE2B_CTX *S)
127
28.6k
{
128
28.6k
    int i;
129
130
28.6k
    memset(S, 0, sizeof(BLAKE2B_CTX));
131
257k
    for (i = 0; i < 8; ++i) {
132
228k
        S->h[i] = blake2b_IV[i];
133
228k
    }
134
28.6k
}
135
136
/* init xors IV with input parameter block and sets the output length */
137
static void blake2b_init_param(BLAKE2B_CTX *S, const BLAKE2B_PARAM *P)
138
28.6k
{
139
28.6k
    size_t i;
140
28.6k
    const uint8_t *p = (const uint8_t *)(P);
141
142
28.6k
    blake2b_init0(S);
143
28.6k
    S->outlen = P->digest_length;
144
145
    /* The param struct is carefully hand packed, and should be 64 bytes on
146
     * every platform. */
147
28.6k
    assert(sizeof(BLAKE2B_PARAM) == 64);
148
    /* IV XOR ParamBlock */
149
257k
    for (i = 0; i < 8; ++i) {
150
228k
        S->h[i] ^= load64(p + sizeof(S->h[i]) * i);
151
228k
    }
152
28.6k
}
153
154
/* Initialize the parameter block with default values */
155
void ossl_blake2b_param_init(BLAKE2B_PARAM *P)
156
27.7k
{
157
27.7k
    P->digest_length = BLAKE2B_DIGEST_LENGTH;
158
27.7k
    P->key_length    = 0;
159
27.7k
    P->fanout        = 1;
160
27.7k
    P->depth         = 1;
161
27.7k
    store32(P->leaf_length, 0);
162
27.7k
    store64(P->node_offset, 0);
163
27.7k
    P->node_depth    = 0;
164
27.7k
    P->inner_length  = 0;
165
27.7k
    memset(P->reserved, 0, sizeof(P->reserved));
166
27.7k
    memset(P->salt,     0, sizeof(P->salt));
167
27.7k
    memset(P->personal, 0, sizeof(P->personal));
168
27.7k
}
169
170
void ossl_blake2b_param_set_digest_length(BLAKE2B_PARAM *P, uint8_t outlen)
171
27.1k
{
172
27.1k
    P->digest_length = outlen;
173
27.1k
}
174
175
void ossl_blake2b_param_set_key_length(BLAKE2B_PARAM *P, uint8_t keylen)
176
983
{
177
983
    P->key_length = keylen;
178
983
}
179
180
void ossl_blake2b_param_set_personal(BLAKE2B_PARAM *P, const uint8_t *personal,
181
                                     size_t len)
182
53
{
183
53
    memcpy(P->personal, personal, len);
184
53
    memset(P->personal + len, 0, BLAKE2B_PERSONALBYTES - len);
185
53
}
186
187
void ossl_blake2b_param_set_salt(BLAKE2B_PARAM *P, const uint8_t *salt,
188
                                 size_t len)
189
44
{
190
44
    memcpy(P->salt, salt, len);
191
44
    memset(P->salt + len, 0, BLAKE2B_SALTBYTES - len);
192
44
}
193
194
/*
195
 * Initialize the hashing context with the given parameter block.
196
 * Always returns 1.
197
 */
198
int ossl_blake2b_init(BLAKE2B_CTX *c, const BLAKE2B_PARAM *P)
199
27.6k
{
200
27.6k
    blake2b_init_param(c, P);
201
27.6k
    return 1;
202
27.6k
}
203
204
/*
205
 * Initialize the hashing context with the given parameter block and key.
206
 * Always returns 1.
207
 */
208
int ossl_blake2b_init_key(BLAKE2B_CTX *c, const BLAKE2B_PARAM *P,
209
                          const void *key)
210
919
{
211
919
    blake2b_init_param(c, P);
212
213
    /* Pad the key to form first data block */
214
919
    {
215
919
        uint8_t block[BLAKE2B_BLOCKBYTES] = {0};
216
217
919
        memcpy(block, key, P->key_length);
218
919
        ossl_blake2b_update(c, block, BLAKE2B_BLOCKBYTES);
219
919
        OPENSSL_cleanse(block, BLAKE2B_BLOCKBYTES);
220
919
    }
221
222
919
    return 1;
223
919
}
224
225
/* Permute the state while xoring in the block of data. */
226
static void blake2b_compress(BLAKE2B_CTX *S,
227
                            const uint8_t *blocks,
228
                            size_t len)
229
30.3k
{
230
30.3k
    uint64_t m[16];
231
30.3k
    uint64_t v[16];
232
30.3k
    int i;
233
30.3k
    size_t increment;
234
235
    /*
236
     * There are two distinct usage vectors for this function:
237
     *
238
     * a) BLAKE2b_Update uses it to process complete blocks,
239
     *    possibly more than one at a time;
240
     *
241
     * b) BLAK2b_Final uses it to process last block, always
242
     *    single but possibly incomplete, in which case caller
243
     *    pads input with zeros.
244
     */
245
30.3k
    assert(len < BLAKE2B_BLOCKBYTES || len % BLAKE2B_BLOCKBYTES == 0);
246
247
    /*
248
     * Since last block is always processed with separate call,
249
     * |len| not being multiple of complete blocks can be observed
250
     * only with |len| being less than BLAKE2B_BLOCKBYTES ("less"
251
     * including even zero), which is why following assignment doesn't
252
     * have to reside inside the main loop below.
253
     */
254
30.3k
    increment = len < BLAKE2B_BLOCKBYTES ? len : BLAKE2B_BLOCKBYTES;
255
256
272k
    for (i = 0; i < 8; ++i) {
257
242k
        v[i] = S->h[i];
258
242k
    }
259
260
80.5k
    do {
261
1.36M
        for (i = 0; i < 16; ++i) {
262
1.28M
            m[i] = load64(blocks + i * sizeof(m[i]));
263
1.28M
        }
264
265
        /* blake2b_increment_counter */
266
80.5k
        S->t[0] += increment;
267
80.5k
        S->t[1] += (S->t[0] < increment);
268
269
80.5k
        v[8]  = blake2b_IV[0];
270
80.5k
        v[9]  = blake2b_IV[1];
271
80.5k
        v[10] = blake2b_IV[2];
272
80.5k
        v[11] = blake2b_IV[3];
273
80.5k
        v[12] = S->t[0] ^ blake2b_IV[4];
274
80.5k
        v[13] = S->t[1] ^ blake2b_IV[5];
275
80.5k
        v[14] = S->f[0] ^ blake2b_IV[6];
276
80.5k
        v[15] = S->f[1] ^ blake2b_IV[7];
277
80.5k
#define G(r,i,a,b,c,d) \
278
7.73M
        do { \
279
7.73M
            a = a + b + m[blake2b_sigma[r][2*i+0]]; \
280
7.73M
            d = rotr64(d ^ a, 32); \
281
7.73M
            c = c + d; \
282
7.73M
            b = rotr64(b ^ c, 24); \
283
7.73M
            a = a + b + m[blake2b_sigma[r][2*i+1]]; \
284
7.73M
            d = rotr64(d ^ a, 16); \
285
7.73M
            c = c + d; \
286
7.73M
            b = rotr64(b ^ c, 63); \
287
7.73M
        } while (0)
288
80.5k
#define ROUND(r)  \
289
966k
        do { \
290
966k
            G(r,0,v[ 0],v[ 4],v[ 8],v[12]); \
291
966k
            G(r,1,v[ 1],v[ 5],v[ 9],v[13]); \
292
966k
            G(r,2,v[ 2],v[ 6],v[10],v[14]); \
293
966k
            G(r,3,v[ 3],v[ 7],v[11],v[15]); \
294
966k
            G(r,4,v[ 0],v[ 5],v[10],v[15]); \
295
966k
            G(r,5,v[ 1],v[ 6],v[11],v[12]); \
296
966k
            G(r,6,v[ 2],v[ 7],v[ 8],v[13]); \
297
966k
            G(r,7,v[ 3],v[ 4],v[ 9],v[14]); \
298
966k
        } while (0)
299
#if defined(OPENSSL_SMALL_FOOTPRINT)
300
        /* 3x size reduction on x86_64, almost 7x on ARMv8, 9x on ARMv4 */
301
        for (i = 0; i < 12; i++) {
302
            ROUND(i);
303
        }
304
#else
305
80.5k
        ROUND(0);
306
80.5k
        ROUND(1);
307
80.5k
        ROUND(2);
308
80.5k
        ROUND(3);
309
80.5k
        ROUND(4);
310
80.5k
        ROUND(5);
311
80.5k
        ROUND(6);
312
80.5k
        ROUND(7);
313
80.5k
        ROUND(8);
314
80.5k
        ROUND(9);
315
80.5k
        ROUND(10);
316
80.5k
        ROUND(11);
317
80.5k
#endif
318
319
724k
        for (i = 0; i < 8; ++i) {
320
644k
            S->h[i] = v[i] ^= v[i + 8] ^ S->h[i];
321
644k
        }
322
80.5k
#undef G
323
80.5k
#undef ROUND
324
80.5k
        blocks += increment;
325
80.5k
        len -= increment;
326
80.5k
    } while (len);
327
30.3k
}
328
329
/* Absorb the input data into the hash state.  Always returns 1. */
330
int ossl_blake2b_update(BLAKE2B_CTX *c, const void *data, size_t datalen)
331
32.1k
{
332
32.1k
    const uint8_t *in = data;
333
32.1k
    size_t fill;
334
335
    /*
336
     * Intuitively one would expect intermediate buffer, c->buf, to
337
     * store incomplete blocks. But in this case we are interested to
338
     * temporarily stash even complete blocks, because last one in the
339
     * stream has to be treated in special way, and at this point we
340
     * don't know if last block in *this* call is last one "ever". This
341
     * is the reason for why |datalen| is compared as >, and not >=.
342
     */
343
32.1k
    fill = sizeof(c->buf) - c->buflen;
344
32.1k
    if (datalen > fill) {
345
1.57k
        if (c->buflen) {
346
1.55k
            memcpy(c->buf + c->buflen, in, fill); /* Fill buffer */
347
1.55k
            blake2b_compress(c, c->buf, BLAKE2B_BLOCKBYTES);
348
1.55k
            c->buflen = 0;
349
1.55k
            in += fill;
350
1.55k
            datalen -= fill;
351
1.55k
        }
352
1.57k
        if (datalen > BLAKE2B_BLOCKBYTES) {
353
176
            size_t stashlen = datalen % BLAKE2B_BLOCKBYTES;
354
            /*
355
             * If |datalen| is a multiple of the blocksize, stash
356
             * last complete block, it can be final one...
357
             */
358
176
            stashlen = stashlen ? stashlen : BLAKE2B_BLOCKBYTES;
359
176
            datalen -= stashlen;
360
176
            blake2b_compress(c, in, datalen);
361
176
            in += datalen;
362
176
            datalen = stashlen;
363
176
        }
364
1.57k
    }
365
366
32.1k
    assert(datalen <= BLAKE2B_BLOCKBYTES);
367
368
32.1k
    memcpy(c->buf + c->buflen, in, datalen);
369
32.1k
    c->buflen += datalen; /* Be lazy, do not compress */
370
371
32.1k
    return 1;
372
32.1k
}
373
374
/*
375
 * Calculate the final hash and save it in md.
376
 * Always returns 1.
377
 */
378
int ossl_blake2b_final(unsigned char *md, BLAKE2B_CTX *c)
379
28.5k
{
380
28.5k
    uint8_t outbuffer[BLAKE2B_OUTBYTES] = {0};
381
28.5k
    uint8_t *target = outbuffer;
382
28.5k
    int iter = (c->outlen + 7) / 8;
383
28.5k
    int i;
384
385
    /* Avoid writing to the temporary buffer if possible */
386
28.5k
    if ((c->outlen % sizeof(c->h[0])) == 0)
387
28.5k
        target = md;
388
389
28.5k
    blake2b_set_lastblock(c);
390
    /* Padding */
391
28.5k
    memset(c->buf + c->buflen, 0, sizeof(c->buf) - c->buflen);
392
28.5k
    blake2b_compress(c, c->buf, c->buflen);
393
394
    /* Output full hash to buffer */
395
256k
    for (i = 0; i < iter; ++i)
396
228k
        store64(target + sizeof(c->h[i]) * i, c->h[i]);
397
398
28.5k
    if (target != md) {
399
21
        memcpy(md, target, c->outlen);
400
21
        OPENSSL_cleanse(target, sizeof(outbuffer));
401
21
    }
402
403
28.5k
    OPENSSL_cleanse(c, sizeof(BLAKE2B_CTX));
404
28.5k
    return 1;
405
28.5k
}