Coverage Report

Created: 2025-10-12 06:56

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/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
111
{
42
111
    const char *s;
43
44
111
    memset(r, 0, sizeof(*r));
45
111
    if (p != NULL)
46
222
        for (; (s = p->key) != NULL; p++)
47
111
            if (ossl_likely(strcmp("size", s + 0) == 0)) {
48
                /* OSSL_DIGEST_PARAM_SIZE */
49
111
                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
111
                r->size = (OSSL_PARAM *)p;
55
111
            }
56
111
    return 1;
57
111
}
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
111
{
64
111
    return blake_get_ctx_params_list;
65
111
}
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
222
{
85
222
    const char *s;
86
87
222
    memset(r, 0, sizeof(*r));
88
222
    if (p != NULL)
89
0
        for (; (s = p->key) != NULL; p++)
90
0
            if (ossl_likely(strcmp("size", s + 0) == 0)) {
91
                /* OSSL_DIGEST_PARAM_SIZE */
92
0
                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
0
                r->size = (OSSL_PARAM *)p;
98
0
            }
99
222
    return 1;
100
222
}
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
0
{
107
0
    return blake_set_ctx_params_list;
108
0
}
109
110
#define IMPLEMENT_BLAKE_functions(variant, VARIANT, variantsize) \
111
111
int ossl_blake##variant##_get_ctx_params(void *vctx, OSSL_PARAM params[]) \
112
111
{ \
113
111
    struct blake##variant##_md_data_st *mdctx = vctx; \
114
111
    struct blake_get_ctx_params_st p; \
115
111
 \
116
111
    BLAKE##VARIANT##_CTX *ctx = &mdctx->ctx; \
117
111
 \
118
111
    if (ctx == NULL || !blake_get_ctx_params_decoder(params, &p)) \
119
111
        return 0; \
120
111
 \
121
111
    if (p.size != NULL \
122
111
        && !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
111
 \
127
111
    return 1; \
128
111
} \
129
 \
130
222
int ossl_blake##variant##_set_ctx_params(void *vctx, const OSSL_PARAM params[]) \
131
222
{ \
132
222
    unsigned int size; \
133
222
    struct blake##variant##_md_data_st *mdctx = vctx; \
134
222
    struct blake_set_ctx_params_st p; \
135
222
 \
136
222
    BLAKE##VARIANT##_CTX *ctx = &mdctx->ctx; \
137
222
 \
138
222
    if (ctx == NULL || !blake_set_ctx_params_decoder(params, &p)) \
139
222
        return 0; \
140
222
 \
141
222
    if (p.size != NULL) { \
142
0
        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
0
        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
0
        ossl_blake##variant##_param_set_digest_length(&mdctx->params, (uint8_t)size); \
151
0
    } \
152
222
 \
153
222
    return 1; \
154
222
} \
155
 \
156
222
static int ossl_blake##variantsize##_init(void *ctx) \
157
222
{ \
158
222
    struct blake##variant##_md_data_st *mdctx = ctx; \
159
222
    uint8_t digest_length = mdctx->params.digest_length; \
160
222
 \
161
222
    ossl_blake##variant##_param_init(&mdctx->params); \
162
222
    if (digest_length != 0) \
163
222
        mdctx->params.digest_length = digest_length; \
164
222
    return ossl_blake##variant##_init(&mdctx->ctx, &mdctx->params); \
165
222
} \
blake2_prov.c:ossl_blake2s256_init
Line
Count
Source
156
112
static int ossl_blake##variantsize##_init(void *ctx) \
157
112
{ \
158
112
    struct blake##variant##_md_data_st *mdctx = ctx; \
159
112
    uint8_t digest_length = mdctx->params.digest_length; \
160
112
 \
161
112
    ossl_blake##variant##_param_init(&mdctx->params); \
162
112
    if (digest_length != 0) \
163
112
        mdctx->params.digest_length = digest_length; \
164
112
    return ossl_blake##variant##_init(&mdctx->ctx, &mdctx->params); \
165
112
} \
blake2_prov.c:ossl_blake2b512_init
Line
Count
Source
156
110
static int ossl_blake##variantsize##_init(void *ctx) \
157
110
{ \
158
110
    struct blake##variant##_md_data_st *mdctx = ctx; \
159
110
    uint8_t digest_length = mdctx->params.digest_length; \
160
110
 \
161
110
    ossl_blake##variant##_param_init(&mdctx->params); \
162
110
    if (digest_length != 0) \
163
110
        mdctx->params.digest_length = digest_length; \
164
110
    return ossl_blake##variant##_init(&mdctx->ctx, &mdctx->params); \
165
110
} \
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
222
static int blake##variantsize##_internal_init(void *ctx, const OSSL_PARAM params[]) \
175
222
{ \
176
222
    return ossl_prov_is_running() && ossl_blake##variant##_set_ctx_params(ctx, params) \
177
222
        && ossl_blake##variantsize##_init(ctx); \
178
222
} \
blake2_prov.c:blake2s256_internal_init
Line
Count
Source
174
112
static int blake##variantsize##_internal_init(void *ctx, const OSSL_PARAM params[]) \
175
112
{ \
176
112
    return ossl_prov_is_running() && ossl_blake##variant##_set_ctx_params(ctx, params) \
177
112
        && ossl_blake##variantsize##_init(ctx); \
178
112
} \
blake2_prov.c:blake2b512_internal_init
Line
Count
Source
174
110
static int blake##variantsize##_internal_init(void *ctx, const OSSL_PARAM params[]) \
175
110
{ \
176
110
    return ossl_prov_is_running() && ossl_blake##variant##_set_ctx_params(ctx, params) \
177
110
        && ossl_blake##variantsize##_init(ctx); \
178
110
} \
179
 \
180
111
static void *blake##variantsize##_newctx(void *prov_ctx) \
181
111
{ \
182
111
    struct blake##variant##_md_data_st *ctx; \
183
111
 \
184
111
    ctx = ossl_prov_is_running() ? OPENSSL_zalloc(sizeof(*ctx)) : NULL; \
185
111
    return ctx; \
186
111
} \
187
 \
188
111
static void blake##variantsize##_freectx(void *vctx) \
189
111
{ \
190
111
    struct blake##variant##_md_data_st *ctx; \
191
111
 \
192
111
    ctx = (struct blake##variant##_md_data_st *)vctx; \
193
111
    OPENSSL_clear_free(ctx, sizeof(*ctx)); \
194
111
} \
blake2_prov.c:blake2s256_freectx
Line
Count
Source
188
56
static void blake##variantsize##_freectx(void *vctx) \
189
56
{ \
190
56
    struct blake##variant##_md_data_st *ctx; \
191
56
 \
192
56
    ctx = (struct blake##variant##_md_data_st *)vctx; \
193
56
    OPENSSL_clear_free(ctx, sizeof(*ctx)); \
194
56
} \
blake2_prov.c:blake2b512_freectx
Line
Count
Source
188
55
static void blake##variantsize##_freectx(void *vctx) \
189
55
{ \
190
55
    struct blake##variant##_md_data_st *ctx; \
191
55
 \
192
55
    ctx = (struct blake##variant##_md_data_st *)vctx; \
193
55
    OPENSSL_clear_free(ctx, sizeof(*ctx)); \
194
55
} \
195
 \
196
0
static void *blake##variantsize##_dupctx(void *ctx) \
197
0
{ \
198
0
    struct blake##variant##_md_data_st *in, *ret; \
199
0
 \
200
0
    in = (struct blake##variant##_md_data_st *)ctx; \
201
0
    ret = ossl_prov_is_running()? OPENSSL_malloc(sizeof(*ret)) : NULL; \
202
0
    if (ret != NULL) \
203
0
        *ret = *in; \
204
0
    return ret; \
205
0
} \
206
\
207
0
static void blake##variantsize##_copyctx(void *voutctx, void *vinctx) \
208
0
{ \
209
0
    struct blake##variant##_md_data_st *inctx, *outctx; \
210
0
 \
211
0
    outctx = (struct blake##variant##_md_data_st *)voutctx; \
212
0
    inctx = (struct blake##variant##_md_data_st *)vinctx; \
213
0
    *outctx = *inctx; \
214
0
} \
Unexecuted instantiation: blake2_prov.c:blake2s256_copyctx
Unexecuted instantiation: blake2_prov.c:blake2b512_copyctx
215
 \
216
static int blake##variantsize##_internal_final(void *ctx, unsigned char *out, \
217
111
                                     size_t *outl, size_t outsz) \
218
111
{ \
219
111
    struct blake##variant##_md_data_st *b_ctx; \
220
111
 \
221
111
    b_ctx = (struct blake##variant##_md_data_st *)ctx; \
222
111
 \
223
111
    if (!ossl_prov_is_running()) \
224
111
        return 0; \
225
111
 \
226
111
    *outl = b_ctx->ctx.outlen; \
227
111
 \
228
111
    if (outsz == 0) \
229
111
       return 1; \
230
111
 \
231
111
    if (outsz < *outl) { \
232
0
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_SIZE); \
233
0
        return 0; \
234
0
    } \
235
111
 \
236
111
    return ossl_blake##variant##_final(out, ctx); \
237
111
} \
238
 \
239
8
static int blake##variantsize##_get_params(OSSL_PARAM params[]) \
240
8
{ \
241
8
    return ossl_digest_default_get_params(params, BLAKE##VARIANT##_BLOCKBYTES, BLAKE##VARIANT##_OUTBYTES, 0); \
242
8
} \
blake2_prov.c:blake2s256_get_params
Line
Count
Source
239
4
static int blake##variantsize##_get_params(OSSL_PARAM params[]) \
240
4
{ \
241
4
    return ossl_digest_default_get_params(params, BLAKE##VARIANT##_BLOCKBYTES, BLAKE##VARIANT##_OUTBYTES, 0); \
242
4
} \
blake2_prov.c:blake2b512_get_params
Line
Count
Source
239
4
static int blake##variantsize##_get_params(OSSL_PARAM params[]) \
240
4
{ \
241
4
    return ossl_digest_default_get_params(params, BLAKE##VARIANT##_BLOCKBYTES, BLAKE##VARIANT##_OUTBYTES, 0); \
242
4
} \
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
280
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