Coverage Report

Created: 2025-12-31 06:58

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
55.9k
{
45
55.9k
    const char *s;
46
47
55.9k
    memset(r, 0, sizeof(*r));
48
55.9k
    if (p != NULL)
49
111k
        for (; (s = p->key) != NULL; p++)
50
55.9k
            if (ossl_likely(strcmp("size", s + 0) == 0)) {
51
                /* OSSL_DIGEST_PARAM_SIZE */
52
55.9k
                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
55.9k
                r->size = (OSSL_PARAM *)p;
58
55.9k
            }
59
55.9k
    return 1;
60
55.9k
}
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
55.9k
{
68
55.9k
    return blake_get_ctx_params_list;
69
55.9k
}
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
56.0k
{
90
56.0k
    const char *s;
91
92
56.0k
    memset(r, 0, sizeof(*r));
93
56.0k
    if (p != NULL)
94
103k
        for (; (s = p->key) != NULL; p++)
95
51.8k
            if (ossl_likely(strcmp("size", s + 0) == 0)) {
96
                /* OSSL_DIGEST_PARAM_SIZE */
97
51.8k
                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
51.8k
                r->size = (OSSL_PARAM *)p;
103
51.8k
            }
104
56.0k
    return 1;
105
56.0k
}
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
13
{
113
13
    return blake_set_ctx_params_list;
114
13
}
115
116
#define IMPLEMENT_BLAKE_functions(variant, VARIANT, variantsize)                                                  \
117
    int ossl_blake##variant##_get_ctx_params(void *vctx, OSSL_PARAM params[])                                     \
118
55.9k
    {                                                                                                             \
119
55.9k
        struct blake##variant##_md_data_st *mdctx = vctx;                                                         \
120
55.9k
        struct blake_get_ctx_params_st p;                                                                         \
121
55.9k
                                                                                                                  \
122
55.9k
        BLAKE##VARIANT##_CTX *ctx = &mdctx->ctx;                                                                  \
123
55.9k
                                                                                                                  \
124
55.9k
        if (ctx == NULL || !blake_get_ctx_params_decoder(params, &p))                                             \
125
55.9k
            return 0;                                                                                             \
126
55.9k
                                                                                                                  \
127
55.9k
        if (p.size != NULL                                                                                        \
128
55.9k
            && !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
55.9k
                                                                                                                  \
133
55.9k
        return 1;                                                                                                 \
134
55.9k
    }                                                                                                             \
135
                                                                                                                  \
136
    int ossl_blake##variant##_set_ctx_params(void *vctx, const OSSL_PARAM params[])                               \
137
56.0k
    {                                                                                                             \
138
56.0k
        unsigned int size;                                                                                        \
139
56.0k
        struct blake##variant##_md_data_st *mdctx = vctx;                                                         \
140
56.0k
        struct blake_set_ctx_params_st p;                                                                         \
141
56.0k
                                                                                                                  \
142
56.0k
        BLAKE##VARIANT##_CTX *ctx = &mdctx->ctx;                                                                  \
143
56.0k
                                                                                                                  \
144
56.0k
        if (ctx == NULL || !blake_set_ctx_params_decoder(params, &p))                                             \
145
56.0k
            return 0;                                                                                             \
146
56.0k
                                                                                                                  \
147
56.0k
        if (p.size != NULL) {                                                                                     \
148
51.8k
            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
51.8k
            if (size < 1 || size > BLAKE##VARIANT##_OUTBYTES) {                                                   \
153
0
                ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_SIZE);                                              \
154
0
                return 0;                                                                                         \
155
0
            }                                                                                                     \
156
51.8k
            ossl_blake##variant##_param_set_digest_length(&mdctx->params, (uint8_t)size);                         \
157
51.8k
        }                                                                                                         \
158
56.0k
                                                                                                                  \
159
56.0k
        return 1;                                                                                                 \
160
56.0k
    }                                                                                                             \
161
                                                                                                                  \
162
    static int ossl_blake##variantsize##_init(void *ctx)                                                          \
163
158k
    {                                                                                                             \
164
158k
        struct blake##variant##_md_data_st *mdctx = ctx;                                                          \
165
158k
        uint8_t digest_length = mdctx->params.digest_length;                                                      \
166
158k
                                                                                                                  \
167
158k
        ossl_blake##variant##_param_init(&mdctx->params);                                                         \
168
158k
        if (digest_length != 0)                                                                                   \
169
158k
            mdctx->params.digest_length = digest_length;                                                          \
170
158k
        return ossl_blake##variant##_init(&mdctx->ctx, &mdctx->params);                                           \
171
158k
    }                                                                                                             \
blake2_prov.c:ossl_blake2s256_init
Line
Count
Source
163
5.21k
    {                                                                                                             \
164
5.21k
        struct blake##variant##_md_data_st *mdctx = ctx;                                                          \
165
5.21k
        uint8_t digest_length = mdctx->params.digest_length;                                                      \
166
5.21k
                                                                                                                  \
167
5.21k
        ossl_blake##variant##_param_init(&mdctx->params);                                                         \
168
5.21k
        if (digest_length != 0)                                                                                   \
169
5.21k
            mdctx->params.digest_length = digest_length;                                                          \
170
5.21k
        return ossl_blake##variant##_init(&mdctx->ctx, &mdctx->params);                                           \
171
5.21k
    }                                                                                                             \
blake2_prov.c:ossl_blake2b512_init
Line
Count
Source
163
153k
    {                                                                                                             \
164
153k
        struct blake##variant##_md_data_st *mdctx = ctx;                                                          \
165
153k
        uint8_t digest_length = mdctx->params.digest_length;                                                      \
166
153k
                                                                                                                  \
167
153k
        ossl_blake##variant##_param_init(&mdctx->params);                                                         \
168
153k
        if (digest_length != 0)                                                                                   \
169
153k
            mdctx->params.digest_length = digest_length;                                                          \
170
153k
        return ossl_blake##variant##_init(&mdctx->ctx, &mdctx->params);                                           \
171
153k
    }                                                                                                             \
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
158k
    {                                                                                                             \
182
158k
        return ossl_prov_is_running() && ossl_blake##variant##_set_ctx_params(ctx, params)                        \
183
158k
            && ossl_blake##variantsize##_init(ctx);                                                               \
184
158k
    }                                                                                                             \
blake2_prov.c:blake2s256_internal_init
Line
Count
Source
181
5.21k
    {                                                                                                             \
182
5.21k
        return ossl_prov_is_running() && ossl_blake##variant##_set_ctx_params(ctx, params)                        \
183
5.21k
            && ossl_blake##variantsize##_init(ctx);                                                               \
184
5.21k
    }                                                                                                             \
blake2_prov.c:blake2b512_internal_init
Line
Count
Source
181
153k
    {                                                                                                             \
182
153k
        return ossl_prov_is_running() && ossl_blake##variant##_set_ctx_params(ctx, params)                        \
183
153k
            && ossl_blake##variantsize##_init(ctx);                                                               \
184
153k
    }                                                                                                             \
185
                                                                                                                  \
186
    static void *blake##variantsize##_newctx(void *prov_ctx)                                                      \
187
154k
    {                                                                                                             \
188
154k
        struct blake##variant##_md_data_st *ctx;                                                                  \
189
154k
                                                                                                                  \
190
154k
        ctx = ossl_prov_is_running() ? OPENSSL_zalloc(sizeof(*ctx)) : NULL;                                       \
191
154k
        return ctx;                                                                                               \
192
154k
    }                                                                                                             \
193
                                                                                                                  \
194
    static void blake##variantsize##_freectx(void *vctx)                                                          \
195
156k
    {                                                                                                             \
196
156k
        struct blake##variant##_md_data_st *ctx;                                                                  \
197
156k
                                                                                                                  \
198
156k
        ctx = (struct blake##variant##_md_data_st *)vctx;                                                         \
199
156k
        OPENSSL_clear_free(ctx, sizeof(*ctx));                                                                    \
200
156k
    }                                                                                                             \
blake2_prov.c:blake2s256_freectx
Line
Count
Source
195
2.44k
    {                                                                                                             \
196
2.44k
        struct blake##variant##_md_data_st *ctx;                                                                  \
197
2.44k
                                                                                                                  \
198
2.44k
        ctx = (struct blake##variant##_md_data_st *)vctx;                                                         \
199
2.44k
        OPENSSL_clear_free(ctx, sizeof(*ctx));                                                                    \
200
2.44k
    }                                                                                                             \
blake2_prov.c:blake2b512_freectx
Line
Count
Source
195
153k
    {                                                                                                             \
196
153k
        struct blake##variant##_md_data_st *ctx;                                                                  \
197
153k
                                                                                                                  \
198
153k
        ctx = (struct blake##variant##_md_data_st *)vctx;                                                         \
199
153k
        OPENSSL_clear_free(ctx, sizeof(*ctx));                                                                    \
200
153k
    }                                                                                                             \
201
                                                                                                                  \
202
    static void *blake##variantsize##_dupctx(void *ctx)                                                           \
203
1.68k
    {                                                                                                             \
204
1.68k
        struct blake##variant##_md_data_st *in, *ret;                                                             \
205
1.68k
                                                                                                                  \
206
1.68k
        in = (struct blake##variant##_md_data_st *)ctx;                                                           \
207
1.68k
        ret = ossl_prov_is_running() ? OPENSSL_malloc(sizeof(*ret)) : NULL;                                       \
208
1.68k
        if (ret != NULL)                                                                                          \
209
1.68k
            *ret = *in;                                                                                           \
210
1.68k
        return ret;                                                                                               \
211
1.68k
    }                                                                                                             \
212
                                                                                                                  \
213
    static void blake##variantsize##_copyctx(void *voutctx, void *vinctx)                                         \
214
3.61k
    {                                                                                                             \
215
3.61k
        struct blake##variant##_md_data_st *inctx, *outctx;                                                       \
216
3.61k
                                                                                                                  \
217
3.61k
        outctx = (struct blake##variant##_md_data_st *)voutctx;                                                   \
218
3.61k
        inctx = (struct blake##variant##_md_data_st *)vinctx;                                                     \
219
3.61k
        *outctx = *inctx;                                                                                         \
220
3.61k
    }                                                                                                             \
blake2_prov.c:blake2s256_copyctx
Line
Count
Source
214
3.18k
    {                                                                                                             \
215
3.18k
        struct blake##variant##_md_data_st *inctx, *outctx;                                                       \
216
3.18k
                                                                                                                  \
217
3.18k
        outctx = (struct blake##variant##_md_data_st *)voutctx;                                                   \
218
3.18k
        inctx = (struct blake##variant##_md_data_st *)vinctx;                                                     \
219
3.18k
        *outctx = *inctx;                                                                                         \
220
3.18k
    }                                                                                                             \
blake2_prov.c:blake2b512_copyctx
Line
Count
Source
214
421
    {                                                                                                             \
215
421
        struct blake##variant##_md_data_st *inctx, *outctx;                                                       \
216
421
                                                                                                                  \
217
421
        outctx = (struct blake##variant##_md_data_st *)voutctx;                                                   \
218
421
        inctx = (struct blake##variant##_md_data_st *)vinctx;                                                     \
219
421
        *outctx = *inctx;                                                                                         \
220
421
    }                                                                                                             \
221
                                                                                                                  \
222
    static int blake##variantsize##_internal_final(void *ctx, unsigned char *out,                                 \
223
        size_t *outl, size_t outsz)                                                                               \
224
158k
    {                                                                                                             \
225
158k
        struct blake##variant##_md_data_st *b_ctx;                                                                \
226
158k
                                                                                                                  \
227
158k
        b_ctx = (struct blake##variant##_md_data_st *)ctx;                                                        \
228
158k
                                                                                                                  \
229
158k
        if (!ossl_prov_is_running())                                                                              \
230
158k
            return 0;                                                                                             \
231
158k
                                                                                                                  \
232
158k
        *outl = b_ctx->ctx.outlen;                                                                                \
233
158k
                                                                                                                  \
234
158k
        if (outsz == 0)                                                                                           \
235
158k
            return 1;                                                                                             \
236
158k
                                                                                                                  \
237
158k
        if (outsz < *outl) {                                                                                      \
238
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_SIZE);                                                  \
239
0
            return 0;                                                                                             \
240
0
        }                                                                                                         \
241
158k
                                                                                                                  \
242
158k
        return ossl_blake##variant##_final(out, ctx);                                                             \
243
158k
    }                                                                                                             \
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
14.2k
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
Unexecuted instantiation: blake2_prov.c:blake2s256_internal_final
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
Unexecuted instantiation: blake2_prov.c:blake2b512_internal_final
Unexecuted instantiation: blake2_prov.c:blake2b512_dupctx