Coverage Report

Created: 2025-12-04 06:33

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