Coverage Report

Created: 2026-04-01 06:39

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl36/providers/implementations/digests/blake2_prov.c
Line
Count
Source
1
/*
2
 * Copyright 2019-2025 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
/* clang-format off */
10
11
/* clang-format on */
12
13
#include <string.h>
14
#include <openssl/crypto.h>
15
#include <openssl/core_names.h>
16
#include <openssl/proverr.h>
17
#include <openssl/err.h>
18
#include "internal/cryptlib.h"
19
#include "prov/blake2.h"
20
#include "prov/digestcommon.h"
21
#include "prov/implementations.h"
22
23
static OSSL_FUNC_digest_gettable_ctx_params_fn blake_gettable_ctx_params;
24
static OSSL_FUNC_digest_settable_ctx_params_fn blake_settable_ctx_params;
25
26
/* clang-format off */
27
/* Machine generated by util/perl/OpenSSL/paramnames.pm */
28
#ifndef blake_get_ctx_params_list
29
static const OSSL_PARAM blake_get_ctx_params_list[] = {
30
    OSSL_PARAM_uint(OSSL_DIGEST_PARAM_SIZE, NULL),
31
    OSSL_PARAM_END
32
};
33
#endif
34
35
#ifndef blake_get_ctx_params_st
36
struct blake_get_ctx_params_st {
37
    OSSL_PARAM *size;
38
};
39
#endif
40
41
#ifndef blake_get_ctx_params_decoder
42
static int blake_get_ctx_params_decoder
43
    (const OSSL_PARAM *p, struct blake_get_ctx_params_st *r)
44
111k
{
45
111k
    const char *s;
46
47
111k
    memset(r, 0, sizeof(*r));
48
111k
    if (p != NULL)
49
223k
        for (; (s = p->key) != NULL; p++)
50
111k
            if (ossl_likely(strcmp("size", s + 0) == 0)) {
51
                /* OSSL_DIGEST_PARAM_SIZE */
52
111k
                if (ossl_unlikely(r->size != NULL)) {
53
0
                    ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
54
0
                                   "param %s is repeated", s);
55
0
                    return 0;
56
0
                }
57
111k
                r->size = (OSSL_PARAM *)p;
58
111k
            }
59
111k
    return 1;
60
111k
}
61
#endif
62
/* End of machine generated */
63
/* clang-format on */
64
65
static const OSSL_PARAM *blake_gettable_ctx_params(ossl_unused void *ctx,
66
    ossl_unused void *pctx)
67
111k
{
68
111k
    return blake_get_ctx_params_list;
69
111k
}
70
71
/* clang-format off */
72
/* Machine generated by util/perl/OpenSSL/paramnames.pm */
73
#ifndef blake_set_ctx_params_list
74
static const OSSL_PARAM blake_set_ctx_params_list[] = {
75
    OSSL_PARAM_uint(OSSL_DIGEST_PARAM_SIZE, NULL),
76
    OSSL_PARAM_END
77
};
78
#endif
79
80
#ifndef blake_set_ctx_params_st
81
struct blake_set_ctx_params_st {
82
    OSSL_PARAM *size;
83
};
84
#endif
85
86
#ifndef blake_set_ctx_params_decoder
87
static int blake_set_ctx_params_decoder
88
    (const OSSL_PARAM *p, struct blake_set_ctx_params_st *r)
89
111k
{
90
111k
    const char *s;
91
92
111k
    memset(r, 0, sizeof(*r));
93
111k
    if (p != NULL)
94
118k
        for (; (s = p->key) != NULL; p++)
95
59.1k
            if (ossl_likely(strcmp("size", s + 0) == 0)) {
96
                /* OSSL_DIGEST_PARAM_SIZE */
97
59.1k
                if (ossl_unlikely(r->size != NULL)) {
98
0
                    ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
99
0
                                   "param %s is repeated", s);
100
0
                    return 0;
101
0
                }
102
59.1k
                r->size = (OSSL_PARAM *)p;
103
59.1k
            }
104
111k
    return 1;
105
111k
}
106
#endif
107
/* End of machine generated */
108
/* clang-format on */
109
110
static const OSSL_PARAM *blake_settable_ctx_params(ossl_unused void *ctx,
111
    ossl_unused void *pctx)
112
35
{
113
35
    return blake_set_ctx_params_list;
114
35
}
115
116
#define IMPLEMENT_BLAKE_functions(variant, VARIANT, variantsize)                                                  \
117
    int ossl_blake##variant##_get_ctx_params(void *vctx, OSSL_PARAM params[])                                     \
118
111k
    {                                                                                                             \
119
111k
        struct blake##variant##_md_data_st *mdctx = vctx;                                                         \
120
111k
        struct blake_get_ctx_params_st p;                                                                         \
121
111k
                                                                                                                  \
122
111k
        BLAKE##VARIANT##_CTX *ctx = &mdctx->ctx;                                                                  \
123
111k
                                                                                                                  \
124
111k
        if (ctx == NULL || !blake_get_ctx_params_decoder(params, &p))                                             \
125
111k
            return 0;                                                                                             \
126
111k
                                                                                                                  \
127
111k
        if (p.size != NULL                                                                                        \
128
111k
            && !OSSL_PARAM_set_uint(p.size, (unsigned int)mdctx->params.digest_length)) {                         \
129
0
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);                                              \
130
0
            return 0;                                                                                             \
131
0
        }                                                                                                         \
132
111k
                                                                                                                  \
133
111k
        return 1;                                                                                                 \
134
111k
    }                                                                                                             \
135
                                                                                                                  \
136
    int ossl_blake##variant##_set_ctx_params(void *vctx, const OSSL_PARAM params[])                               \
137
111k
    {                                                                                                             \
138
111k
        unsigned int size;                                                                                        \
139
111k
        struct blake##variant##_md_data_st *mdctx = vctx;                                                         \
140
111k
        struct blake_set_ctx_params_st p;                                                                         \
141
111k
                                                                                                                  \
142
111k
        BLAKE##VARIANT##_CTX *ctx = &mdctx->ctx;                                                                  \
143
111k
                                                                                                                  \
144
111k
        if (ctx == NULL || !blake_set_ctx_params_decoder(params, &p))                                             \
145
111k
            return 0;                                                                                             \
146
111k
                                                                                                                  \
147
111k
        if (p.size != NULL) {                                                                                     \
148
59.1k
            if (!OSSL_PARAM_get_uint(p.size, &size)) {                                                            \
149
0
                ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);                                          \
150
0
                return 0;                                                                                         \
151
0
            }                                                                                                     \
152
59.1k
            if (size < 1 || size > BLAKE##VARIANT##_OUTBYTES) {                                                   \
153
14
                ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_SIZE);                                              \
154
14
                return 0;                                                                                         \
155
14
            }                                                                                                     \
156
59.1k
            ossl_blake##variant##_param_set_digest_length(&mdctx->params, (uint8_t)size);                         \
157
59.1k
        }                                                                                                         \
158
111k
                                                                                                                  \
159
111k
        return 1;                                                                                                 \
160
111k
    }                                                                                                             \
161
                                                                                                                  \
162
    static int ossl_blake##variantsize##_init(void *ctx)                                                          \
163
209k
    {                                                                                                             \
164
209k
        struct blake##variant##_md_data_st *mdctx = ctx;                                                          \
165
209k
        uint8_t digest_length = mdctx->params.digest_length;                                                      \
166
209k
                                                                                                                  \
167
209k
        ossl_blake##variant##_param_init(&mdctx->params);                                                         \
168
209k
        if (digest_length != 0)                                                                                   \
169
209k
            mdctx->params.digest_length = digest_length;                                                          \
170
209k
        return ossl_blake##variant##_init(&mdctx->ctx, &mdctx->params);                                           \
171
209k
    }                                                                                                             \
blake2_prov.c:ossl_blake2s256_init
Line
Count
Source
163
42.4k
    {                                                                                                             \
164
42.4k
        struct blake##variant##_md_data_st *mdctx = ctx;                                                          \
165
42.4k
        uint8_t digest_length = mdctx->params.digest_length;                                                      \
166
42.4k
                                                                                                                  \
167
42.4k
        ossl_blake##variant##_param_init(&mdctx->params);                                                         \
168
42.4k
        if (digest_length != 0)                                                                                   \
169
42.4k
            mdctx->params.digest_length = digest_length;                                                          \
170
42.4k
        return ossl_blake##variant##_init(&mdctx->ctx, &mdctx->params);                                           \
171
42.4k
    }                                                                                                             \
blake2_prov.c:ossl_blake2b512_init
Line
Count
Source
163
166k
    {                                                                                                             \
164
166k
        struct blake##variant##_md_data_st *mdctx = ctx;                                                          \
165
166k
        uint8_t digest_length = mdctx->params.digest_length;                                                      \
166
166k
                                                                                                                  \
167
166k
        ossl_blake##variant##_param_init(&mdctx->params);                                                         \
168
166k
        if (digest_length != 0)                                                                                   \
169
166k
            mdctx->params.digest_length = digest_length;                                                          \
170
166k
        return ossl_blake##variant##_init(&mdctx->ctx, &mdctx->params);                                           \
171
166k
    }                                                                                                             \
172
                                                                                                                  \
173
    static OSSL_FUNC_digest_init_fn blake##variantsize##_internal_init;                                           \
174
    static OSSL_FUNC_digest_newctx_fn blake##variantsize##_newctx;                                                \
175
    static OSSL_FUNC_digest_freectx_fn blake##variantsize##_freectx;                                              \
176
    static OSSL_FUNC_digest_dupctx_fn blake##variantsize##_dupctx;                                                \
177
    static OSSL_FUNC_digest_final_fn blake##variantsize##_internal_final;                                         \
178
    static OSSL_FUNC_digest_get_params_fn blake##variantsize##_get_params;                                        \
179
                                                                                                                  \
180
    static int blake##variantsize##_internal_init(void *ctx, const OSSL_PARAM params[])                           \
181
209k
    {                                                                                                             \
182
209k
        return ossl_prov_is_running() && ossl_blake##variant##_set_ctx_params(ctx, params)                        \
183
209k
            && ossl_blake##variantsize##_init(ctx);                                                               \
184
209k
    }                                                                                                             \
blake2_prov.c:blake2s256_internal_init
Line
Count
Source
181
42.4k
    {                                                                                                             \
182
42.4k
        return ossl_prov_is_running() && ossl_blake##variant##_set_ctx_params(ctx, params)                        \
183
42.4k
            && ossl_blake##variantsize##_init(ctx);                                                               \
184
42.4k
    }                                                                                                             \
blake2_prov.c:blake2b512_internal_init
Line
Count
Source
181
166k
    {                                                                                                             \
182
166k
        return ossl_prov_is_running() && ossl_blake##variant##_set_ctx_params(ctx, params)                        \
183
166k
            && ossl_blake##variantsize##_init(ctx);                                                               \
184
166k
    }                                                                                                             \
185
                                                                                                                  \
186
    static void *blake##variantsize##_newctx(void *prov_ctx)                                                      \
187
157k
    {                                                                                                             \
188
157k
        struct blake##variant##_md_data_st *ctx;                                                                  \
189
157k
                                                                                                                  \
190
157k
        ctx = ossl_prov_is_running() ? OPENSSL_zalloc(sizeof(*ctx)) : NULL;                                       \
191
157k
        return ctx;                                                                                               \
192
157k
    }                                                                                                             \
193
                                                                                                                  \
194
    static void blake##variantsize##_freectx(void *vctx)                                                          \
195
160k
    {                                                                                                             \
196
160k
        struct blake##variant##_md_data_st *ctx;                                                                  \
197
160k
                                                                                                                  \
198
160k
        ctx = (struct blake##variant##_md_data_st *)vctx;                                                         \
199
160k
        OPENSSL_clear_free(ctx, sizeof(*ctx));                                                                    \
200
160k
    }                                                                                                             \
blake2_prov.c:blake2s256_freectx
Line
Count
Source
195
4.48k
    {                                                                                                             \
196
4.48k
        struct blake##variant##_md_data_st *ctx;                                                                  \
197
4.48k
                                                                                                                  \
198
4.48k
        ctx = (struct blake##variant##_md_data_st *)vctx;                                                         \
199
4.48k
        OPENSSL_clear_free(ctx, sizeof(*ctx));                                                                    \
200
4.48k
    }                                                                                                             \
blake2_prov.c:blake2b512_freectx
Line
Count
Source
195
155k
    {                                                                                                             \
196
155k
        struct blake##variant##_md_data_st *ctx;                                                                  \
197
155k
                                                                                                                  \
198
155k
        ctx = (struct blake##variant##_md_data_st *)vctx;                                                         \
199
155k
        OPENSSL_clear_free(ctx, sizeof(*ctx));                                                                    \
200
155k
    }                                                                                                             \
201
                                                                                                                  \
202
    static void *blake##variantsize##_dupctx(void *ctx)                                                           \
203
2.46k
    {                                                                                                             \
204
2.46k
        struct blake##variant##_md_data_st *in, *ret;                                                             \
205
2.46k
                                                                                                                  \
206
2.46k
        in = (struct blake##variant##_md_data_st *)ctx;                                                           \
207
2.46k
        ret = ossl_prov_is_running() ? OPENSSL_malloc(sizeof(*ret)) : NULL;                                       \
208
2.46k
        if (ret != NULL)                                                                                          \
209
2.46k
            *ret = *in;                                                                                           \
210
2.46k
        return ret;                                                                                               \
211
2.46k
    }                                                                                                             \
212
                                                                                                                  \
213
    static void blake##variantsize##_copyctx(void *voutctx, void *vinctx)                                         \
214
5.47k
    {                                                                                                             \
215
5.47k
        struct blake##variant##_md_data_st *inctx, *outctx;                                                       \
216
5.47k
                                                                                                                  \
217
5.47k
        outctx = (struct blake##variant##_md_data_st *)voutctx;                                                   \
218
5.47k
        inctx = (struct blake##variant##_md_data_st *)vinctx;                                                     \
219
5.47k
        *outctx = *inctx;                                                                                         \
220
5.47k
    }                                                                                                             \
blake2_prov.c:blake2s256_copyctx
Line
Count
Source
214
4.33k
    {                                                                                                             \
215
4.33k
        struct blake##variant##_md_data_st *inctx, *outctx;                                                       \
216
4.33k
                                                                                                                  \
217
4.33k
        outctx = (struct blake##variant##_md_data_st *)voutctx;                                                   \
218
4.33k
        inctx = (struct blake##variant##_md_data_st *)vinctx;                                                     \
219
4.33k
        *outctx = *inctx;                                                                                         \
220
4.33k
    }                                                                                                             \
blake2_prov.c:blake2b512_copyctx
Line
Count
Source
214
1.13k
    {                                                                                                             \
215
1.13k
        struct blake##variant##_md_data_st *inctx, *outctx;                                                       \
216
1.13k
                                                                                                                  \
217
1.13k
        outctx = (struct blake##variant##_md_data_st *)voutctx;                                                   \
218
1.13k
        inctx = (struct blake##variant##_md_data_st *)vinctx;                                                     \
219
1.13k
        *outctx = *inctx;                                                                                         \
220
1.13k
    }                                                                                                             \
221
                                                                                                                  \
222
    static int blake##variantsize##_internal_final(void *ctx, unsigned char *out,                                 \
223
        size_t *outl, size_t outsz)                                                                               \
224
208k
    {                                                                                                             \
225
208k
        struct blake##variant##_md_data_st *b_ctx;                                                                \
226
208k
                                                                                                                  \
227
208k
        b_ctx = (struct blake##variant##_md_data_st *)ctx;                                                        \
228
208k
                                                                                                                  \
229
208k
        if (!ossl_prov_is_running())                                                                              \
230
208k
            return 0;                                                                                             \
231
208k
                                                                                                                  \
232
208k
        *outl = b_ctx->ctx.outlen;                                                                                \
233
208k
                                                                                                                  \
234
208k
        if (outsz == 0)                                                                                           \
235
208k
            return 1;                                                                                             \
236
208k
                                                                                                                  \
237
208k
        if (outsz < *outl) {                                                                                      \
238
12
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_SIZE);                                                  \
239
12
            return 0;                                                                                             \
240
12
        }                                                                                                         \
241
208k
                                                                                                                  \
242
208k
        return ossl_blake##variant##_final(out, ctx);                                                             \
243
208k
    }                                                                                                             \
244
                                                                                                                  \
245
    static int blake##variantsize##_get_params(OSSL_PARAM params[])                                               \
246
182
    {                                                                                                             \
247
182
        return ossl_digest_default_get_params(params, BLAKE##VARIANT##_BLOCKBYTES, BLAKE##VARIANT##_OUTBYTES, 0); \
248
182
    }                                                                                                             \
blake2_prov.c:blake2s256_get_params
Line
Count
Source
246
91
    {                                                                                                             \
247
91
        return ossl_digest_default_get_params(params, BLAKE##VARIANT##_BLOCKBYTES, BLAKE##VARIANT##_OUTBYTES, 0); \
248
91
    }                                                                                                             \
blake2_prov.c:blake2b512_get_params
Line
Count
Source
246
91
    {                                                                                                             \
247
91
        return ossl_digest_default_get_params(params, BLAKE##VARIANT##_BLOCKBYTES, BLAKE##VARIANT##_OUTBYTES, 0); \
248
91
    }                                                                                                             \
249
                                                                                                                  \
250
    const OSSL_DISPATCH ossl_blake##variantsize##_functions[] = {                                                 \
251
        { OSSL_FUNC_DIGEST_NEWCTX, (void (*)(void))blake##variantsize##_newctx },                                 \
252
        { OSSL_FUNC_DIGEST_UPDATE, (void (*)(void))ossl_blake##variant##_update },                                \
253
        { OSSL_FUNC_DIGEST_FINAL, (void (*)(void))blake##variantsize##_internal_final },                          \
254
        { OSSL_FUNC_DIGEST_FREECTX, (void (*)(void))blake##variantsize##_freectx },                               \
255
        { OSSL_FUNC_DIGEST_DUPCTX, (void (*)(void))blake##variantsize##_dupctx },                                 \
256
        { OSSL_FUNC_DIGEST_COPYCTX, (void (*)(void))blake##variantsize##_copyctx },                               \
257
        { OSSL_FUNC_DIGEST_GET_PARAMS, (void (*)(void))blake##variantsize##_get_params },                         \
258
        { OSSL_FUNC_DIGEST_GETTABLE_PARAMS,                                                                       \
259
            (void (*)(void))ossl_digest_default_gettable_params },                                                \
260
        { OSSL_FUNC_DIGEST_INIT, (void (*)(void))blake##variantsize##_internal_init },                            \
261
        { OSSL_FUNC_DIGEST_GETTABLE_CTX_PARAMS,                                                                   \
262
            (void (*)(void))blake_gettable_ctx_params },                                                          \
263
        { OSSL_FUNC_DIGEST_SETTABLE_CTX_PARAMS,                                                                   \
264
            (void (*)(void))blake_settable_ctx_params },                                                          \
265
        { OSSL_FUNC_DIGEST_GET_CTX_PARAMS,                                                                        \
266
            (void (*)(void))ossl_blake##variant##_get_ctx_params },                                               \
267
        { OSSL_FUNC_DIGEST_SET_CTX_PARAMS,                                                                        \
268
            (void (*)(void))ossl_blake##variant##_set_ctx_params },                                               \
269
        { 0, NULL }                                                                                               \
270
    };
271
272
127k
IMPLEMENT_BLAKE_functions(2s, 2S, 2s256)
ossl_blake2s_get_ctx_params
Line
Count
Source
272
IMPLEMENT_BLAKE_functions(2s, 2S, 2s256)
ossl_blake2s_set_ctx_params
Line
Count
Source
272
IMPLEMENT_BLAKE_functions(2s, 2S, 2s256)
Unexecuted instantiation: blake2_prov.c:blake2s256_newctx
blake2_prov.c:blake2s256_internal_final
Line
Count
Source
272
IMPLEMENT_BLAKE_functions(2s, 2S, 2s256)
Unexecuted instantiation: blake2_prov.c:blake2s256_dupctx
273
IMPLEMENT_BLAKE_functions(2b, 2B, 2b512)
ossl_blake2b_get_ctx_params
Line
Count
Source
273
IMPLEMENT_BLAKE_functions(2b, 2B, 2b512)
ossl_blake2b_set_ctx_params
Line
Count
Source
273
IMPLEMENT_BLAKE_functions(2b, 2B, 2b512)
Unexecuted instantiation: blake2_prov.c:blake2b512_newctx
blake2_prov.c:blake2b512_internal_final
Line
Count
Source
273
IMPLEMENT_BLAKE_functions(2b, 2B, 2b512)
Unexecuted instantiation: blake2_prov.c:blake2b512_dupctx