Coverage Report

Created: 2024-05-21 06:52

/src/openssl/providers/implementations/digests/blake2b_prov.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2016-2023 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
0
{
35
0
    return known_blake2b_ctx_params;
36
0
}
37
38
const OSSL_PARAM *ossl_blake2b_settable_ctx_params(ossl_unused void *ctx,
39
                                                   ossl_unused void *pctx)
40
0
{
41
0
    return known_blake2b_ctx_params;
42
0
}
43
44
int ossl_blake2b_get_ctx_params(void *vctx, OSSL_PARAM params[])
45
0
{
46
0
    struct blake2b_md_data_st *mdctx = vctx;
47
0
    OSSL_PARAM *p;
48
49
0
    BLAKE2B_CTX *ctx = &mdctx->ctx;
50
51
0
    if (ctx == NULL)
52
0
        return 0;
53
0
    if (params == NULL)
54
0
        return 1;
55
56
0
    p = OSSL_PARAM_locate(params, OSSL_DIGEST_PARAM_SIZE);
57
0
    if (p != NULL
58
0
        && !OSSL_PARAM_set_uint(p, (unsigned int)mdctx->params.digest_length)) {
59
0
        ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
60
0
        return 0;
61
0
    }
62
63
0
    return 1;
64
0
}
65
66
int ossl_blake2b_set_ctx_params(void *vctx, const OSSL_PARAM params[])
67
0
{
68
0
    size_t size;
69
0
    struct blake2b_md_data_st *mdctx = vctx;
70
0
    const OSSL_PARAM *p;
71
72
0
    BLAKE2B_CTX *ctx = &mdctx->ctx;
73
74
0
    if (ctx == NULL)
75
0
        return 0;
76
0
    if (params == NULL)
77
0
        return 1;
78
79
0
    p = OSSL_PARAM_locate_const(params, OSSL_DIGEST_PARAM_SIZE);
80
0
    if (p != NULL) {
81
0
        if (!OSSL_PARAM_get_size_t(p, &size)) {
82
0
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
83
0
            return 0;
84
0
        }
85
0
        if (size < 1 || size > BLAKE2B_OUTBYTES) {
86
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_SIZE);
87
0
            return 0;
88
0
        }
89
0
        ossl_blake2b_param_set_digest_length(&mdctx->params, (uint8_t)size);
90
0
    }
91
92
0
    return 1;
93
0
}
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
0
{
122
0
    S->f[0] = -1;
123
0
}
124
125
/* Initialize the hashing state. */
126
static ossl_inline void blake2b_init0(BLAKE2B_CTX *S)
127
0
{
128
0
    int i;
129
130
0
    memset(S, 0, sizeof(BLAKE2B_CTX));
131
0
    for (i = 0; i < 8; ++i) {
132
0
        S->h[i] = blake2b_IV[i];
133
0
    }
134
0
}
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
0
{
139
0
    size_t i;
140
0
    const uint8_t *p = (const uint8_t *)(P);
141
142
0
    blake2b_init0(S);
143
0
    S->outlen = P->digest_length;
144
145
    /* The param struct is carefully hand packed, and should be 64 bytes on
146
     * every platform. */
147
0
    assert(sizeof(BLAKE2B_PARAM) == 64);
148
    /* IV XOR ParamBlock */
149
0
    for (i = 0; i < 8; ++i) {
150
0
        S->h[i] ^= load64(p + sizeof(S->h[i]) * i);
151
0
    }
152
0
}
153
154
/* Initialize the parameter block with default values */
155
void ossl_blake2b_param_init(BLAKE2B_PARAM *P)
156
0
{
157
0
    P->digest_length = BLAKE2B_DIGEST_LENGTH;
158
0
    P->key_length    = 0;
159
0
    P->fanout        = 1;
160
0
    P->depth         = 1;
161
0
    store32(P->leaf_length, 0);
162
0
    store64(P->node_offset, 0);
163
0
    P->node_depth    = 0;
164
0
    P->inner_length  = 0;
165
0
    memset(P->reserved, 0, sizeof(P->reserved));
166
0
    memset(P->salt,     0, sizeof(P->salt));
167
0
    memset(P->personal, 0, sizeof(P->personal));
168
0
}
169
170
void ossl_blake2b_param_set_digest_length(BLAKE2B_PARAM *P, uint8_t outlen)
171
0
{
172
0
    P->digest_length = outlen;
173
0
}
174
175
void ossl_blake2b_param_set_key_length(BLAKE2B_PARAM *P, uint8_t keylen)
176
0
{
177
0
    P->key_length = keylen;
178
0
}
179
180
void ossl_blake2b_param_set_personal(BLAKE2B_PARAM *P, const uint8_t *personal,
181
                                     size_t len)
182
0
{
183
0
    memcpy(P->personal, personal, len);
184
0
    memset(P->personal + len, 0, BLAKE2B_PERSONALBYTES - len);
185
0
}
186
187
void ossl_blake2b_param_set_salt(BLAKE2B_PARAM *P, const uint8_t *salt,
188
                                 size_t len)
189
0
{
190
0
    memcpy(P->salt, salt, len);
191
0
    memset(P->salt + len, 0, BLAKE2B_SALTBYTES - len);
192
0
}
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
0
{
200
0
    blake2b_init_param(c, P);
201
0
    return 1;
202
0
}
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
0
{
211
0
    blake2b_init_param(c, P);
212
213
    /* Pad the key to form first data block */
214
0
    {
215
0
        uint8_t block[BLAKE2B_BLOCKBYTES] = {0};
216
217
0
        memcpy(block, key, P->key_length);
218
0
        ossl_blake2b_update(c, block, BLAKE2B_BLOCKBYTES);
219
0
        OPENSSL_cleanse(block, BLAKE2B_BLOCKBYTES);
220
0
    }
221
222
0
    return 1;
223
0
}
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
0
{
230
0
    uint64_t m[16];
231
0
    uint64_t v[16];
232
0
    int i;
233
0
    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
0
    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
0
    increment = len < BLAKE2B_BLOCKBYTES ? len : BLAKE2B_BLOCKBYTES;
255
256
0
    for (i = 0; i < 8; ++i) {
257
0
        v[i] = S->h[i];
258
0
    }
259
260
0
    do {
261
0
        for (i = 0; i < 16; ++i) {
262
0
            m[i] = load64(blocks + i * sizeof(m[i]));
263
0
        }
264
265
        /* blake2b_increment_counter */
266
0
        S->t[0] += increment;
267
0
        S->t[1] += (S->t[0] < increment);
268
269
0
        v[8]  = blake2b_IV[0];
270
0
        v[9]  = blake2b_IV[1];
271
0
        v[10] = blake2b_IV[2];
272
0
        v[11] = blake2b_IV[3];
273
0
        v[12] = S->t[0] ^ blake2b_IV[4];
274
0
        v[13] = S->t[1] ^ blake2b_IV[5];
275
0
        v[14] = S->f[0] ^ blake2b_IV[6];
276
0
        v[15] = S->f[1] ^ blake2b_IV[7];
277
0
#define G(r,i,a,b,c,d) \
278
0
        do { \
279
0
            a = a + b + m[blake2b_sigma[r][2*i+0]]; \
280
0
            d = rotr64(d ^ a, 32); \
281
0
            c = c + d; \
282
0
            b = rotr64(b ^ c, 24); \
283
0
            a = a + b + m[blake2b_sigma[r][2*i+1]]; \
284
0
            d = rotr64(d ^ a, 16); \
285
0
            c = c + d; \
286
0
            b = rotr64(b ^ c, 63); \
287
0
        } while (0)
288
0
#define ROUND(r)  \
289
0
        do { \
290
0
            G(r,0,v[ 0],v[ 4],v[ 8],v[12]); \
291
0
            G(r,1,v[ 1],v[ 5],v[ 9],v[13]); \
292
0
            G(r,2,v[ 2],v[ 6],v[10],v[14]); \
293
0
            G(r,3,v[ 3],v[ 7],v[11],v[15]); \
294
0
            G(r,4,v[ 0],v[ 5],v[10],v[15]); \
295
0
            G(r,5,v[ 1],v[ 6],v[11],v[12]); \
296
0
            G(r,6,v[ 2],v[ 7],v[ 8],v[13]); \
297
0
            G(r,7,v[ 3],v[ 4],v[ 9],v[14]); \
298
0
        } 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
0
        ROUND(0);
306
0
        ROUND(1);
307
0
        ROUND(2);
308
0
        ROUND(3);
309
0
        ROUND(4);
310
0
        ROUND(5);
311
0
        ROUND(6);
312
0
        ROUND(7);
313
0
        ROUND(8);
314
0
        ROUND(9);
315
0
        ROUND(10);
316
0
        ROUND(11);
317
0
#endif
318
319
0
        for (i = 0; i < 8; ++i) {
320
0
            S->h[i] = v[i] ^= v[i + 8] ^ S->h[i];
321
0
        }
322
0
#undef G
323
0
#undef ROUND
324
0
        blocks += increment;
325
0
        len -= increment;
326
0
    } while (len);
327
0
}
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
0
{
332
0
    const uint8_t *in = data;
333
0
    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
0
    fill = sizeof(c->buf) - c->buflen;
344
0
    if (datalen > fill) {
345
0
        if (c->buflen) {
346
0
            memcpy(c->buf + c->buflen, in, fill); /* Fill buffer */
347
0
            blake2b_compress(c, c->buf, BLAKE2B_BLOCKBYTES);
348
0
            c->buflen = 0;
349
0
            in += fill;
350
0
            datalen -= fill;
351
0
        }
352
0
        if (datalen > BLAKE2B_BLOCKBYTES) {
353
0
            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
0
            stashlen = stashlen ? stashlen : BLAKE2B_BLOCKBYTES;
359
0
            datalen -= stashlen;
360
0
            blake2b_compress(c, in, datalen);
361
0
            in += datalen;
362
0
            datalen = stashlen;
363
0
        }
364
0
    }
365
366
0
    assert(datalen <= BLAKE2B_BLOCKBYTES);
367
368
0
    memcpy(c->buf + c->buflen, in, datalen);
369
0
    c->buflen += datalen; /* Be lazy, do not compress */
370
371
0
    return 1;
372
0
}
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
0
{
380
0
    uint8_t outbuffer[BLAKE2B_OUTBYTES] = {0};
381
0
    uint8_t *target = outbuffer;
382
0
    int iter = (c->outlen + 7) / 8;
383
0
    int i;
384
385
    /* Avoid writing to the temporary buffer if possible */
386
0
    if ((c->outlen % sizeof(c->h[0])) == 0)
387
0
        target = md;
388
389
0
    blake2b_set_lastblock(c);
390
    /* Padding */
391
0
    memset(c->buf + c->buflen, 0, sizeof(c->buf) - c->buflen);
392
0
    blake2b_compress(c, c->buf, c->buflen);
393
394
    /* Output full hash to buffer */
395
0
    for (i = 0; i < iter; ++i)
396
0
        store64(target + sizeof(c->h[i]) * i, c->h[i]);
397
398
0
    if (target != md)
399
0
        memcpy(md, target, c->outlen);
400
401
0
    OPENSSL_cleanse(c, sizeof(BLAKE2B_CTX));
402
0
    return 1;
403
0
}