Coverage Report

Created: 2025-08-25 06:30

/src/openssl/providers/implementations/digests/blake2_prov.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2019-2023 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License 2.0 (the "License").  You may not use
5
 * this file except in compliance with the License.  You can obtain a copy
6
 * in the file LICENSE in the source distribution or at
7
 * https://www.openssl.org/source/license.html
8
 */
9
10
11
#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
0
{
42
0
    const char *s;
43
44
0
    memset(r, 0, sizeof(*r));
45
0
    if (p != NULL)
46
0
        for (; (s = p->key) != NULL; p++)
47
0
            if (ossl_likely(strcmp("size", s + 0) == 0)) {
48
                /* DIGEST_PARAM_SIZE */
49
0
                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
0
                r->size = (OSSL_PARAM *)p;
55
0
            }
56
0
    return 1;
57
0
}
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
0
{
64
0
    return blake_get_ctx_params_list;
65
0
}
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
0
{
85
0
    const char *s;
86
87
0
    memset(r, 0, sizeof(*r));
88
0
    if (p != NULL)
89
0
        for (; (s = p->key) != NULL; p++)
90
0
            if (ossl_likely(strcmp("size", s + 0) == 0)) {
91
                /* 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
0
    return 1;
100
0
}
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
0
int ossl_blake##variant##_get_ctx_params(void *vctx, OSSL_PARAM params[]) \
112
0
{ \
113
0
    struct blake##variant##_md_data_st *mdctx = vctx; \
114
0
    struct blake_get_ctx_params_st p; \
115
0
 \
116
0
    BLAKE##VARIANT##_CTX *ctx = &mdctx->ctx; \
117
0
 \
118
0
    if (ctx == NULL || !blake_get_ctx_params_decoder(params, &p)) \
119
0
        return 0; \
120
0
 \
121
0
    if (p.size != NULL \
122
0
        && !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
0
 \
127
0
    return 1; \
128
0
} \
Unexecuted instantiation: ossl_blake2s_get_ctx_params
Unexecuted instantiation: ossl_blake2b_get_ctx_params
129
 \
130
0
int ossl_blake##variant##_set_ctx_params(void *vctx, const OSSL_PARAM params[]) \
131
0
{ \
132
0
    unsigned int size; \
133
0
    struct blake##variant##_md_data_st *mdctx = vctx; \
134
0
    struct blake_set_ctx_params_st p; \
135
0
 \
136
0
    BLAKE##VARIANT##_CTX *ctx = &mdctx->ctx; \
137
0
 \
138
0
    if (ctx == NULL || !blake_set_ctx_params_decoder(params, &p)) \
139
0
        return 0; \
140
0
 \
141
0
    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
0
 \
153
0
    return 1; \
154
0
} \
Unexecuted instantiation: ossl_blake2s_set_ctx_params
Unexecuted instantiation: ossl_blake2b_set_ctx_params
155
 \
156
0
static int ossl_blake##variantsize##_init(void *ctx) \
157
0
{ \
158
0
    struct blake##variant##_md_data_st *mdctx = ctx; \
159
0
    uint8_t digest_length = mdctx->params.digest_length; \
160
0
 \
161
0
    ossl_blake##variant##_param_init(&mdctx->params); \
162
0
    if (digest_length != 0) \
163
0
        mdctx->params.digest_length = digest_length; \
164
0
    return ossl_blake##variant##_init(&mdctx->ctx, &mdctx->params); \
165
0
} \
Unexecuted instantiation: blake2_prov.c:ossl_blake2s256_init
Unexecuted instantiation: blake2_prov.c:ossl_blake2b512_init
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
0
static int blake##variantsize##_internal_init(void *ctx, const OSSL_PARAM params[]) \
175
0
{ \
176
0
    return ossl_prov_is_running() && ossl_blake##variant##_set_ctx_params(ctx, params) \
177
0
        && ossl_blake##variantsize##_init(ctx); \
178
0
} \
Unexecuted instantiation: blake2_prov.c:blake2s256_internal_init
Unexecuted instantiation: blake2_prov.c:blake2b512_internal_init
179
 \
180
0
static void *blake##variantsize##_newctx(void *prov_ctx) \
181
0
{ \
182
0
    struct blake##variant##_md_data_st *ctx; \
183
0
 \
184
0
    ctx = ossl_prov_is_running() ? OPENSSL_zalloc(sizeof(*ctx)) : NULL; \
185
0
    return ctx; \
186
0
} \
Unexecuted instantiation: blake2_prov.c:blake2s256_newctx
Unexecuted instantiation: blake2_prov.c:blake2b512_newctx
187
 \
188
0
static void blake##variantsize##_freectx(void *vctx) \
189
0
{ \
190
0
    struct blake##variant##_md_data_st *ctx; \
191
0
 \
192
0
    ctx = (struct blake##variant##_md_data_st *)vctx; \
193
0
    OPENSSL_clear_free(ctx, sizeof(*ctx)); \
194
0
} \
Unexecuted instantiation: blake2_prov.c:blake2s256_freectx
Unexecuted instantiation: blake2_prov.c:blake2b512_freectx
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
} \
Unexecuted instantiation: blake2_prov.c:blake2s256_dupctx
Unexecuted instantiation: blake2_prov.c:blake2b512_dupctx
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
0
                                     size_t *outl, size_t outsz) \
218
0
{ \
219
0
    struct blake##variant##_md_data_st *b_ctx; \
220
0
 \
221
0
    b_ctx = (struct blake##variant##_md_data_st *)ctx; \
222
0
 \
223
0
    if (!ossl_prov_is_running()) \
224
0
        return 0; \
225
0
 \
226
0
    *outl = b_ctx->ctx.outlen; \
227
0
 \
228
0
    if (outsz == 0) \
229
0
       return 1; \
230
0
 \
231
0
    if (outsz < *outl) { \
232
0
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_SIZE); \
233
0
        return 0; \
234
0
    } \
235
0
 \
236
0
    return ossl_blake##variant##_final(out, ctx); \
237
0
} \
Unexecuted instantiation: blake2_prov.c:blake2s256_internal_final
Unexecuted instantiation: blake2_prov.c:blake2b512_internal_final
238
 \
239
32
static int blake##variantsize##_get_params(OSSL_PARAM params[]) \
240
32
{ \
241
32
    return ossl_digest_default_get_params(params, BLAKE##VARIANT##_BLOCKBYTES, BLAKE##VARIANT##_OUTBYTES, 0); \
242
32
} \
blake2_prov.c:blake2s256_get_params
Line
Count
Source
239
16
static int blake##variantsize##_get_params(OSSL_PARAM params[]) \
240
16
{ \
241
16
    return ossl_digest_default_get_params(params, BLAKE##VARIANT##_BLOCKBYTES, BLAKE##VARIANT##_OUTBYTES, 0); \
242
16
} \
blake2_prov.c:blake2b512_get_params
Line
Count
Source
239
16
static int blake##variantsize##_get_params(OSSL_PARAM params[]) \
240
16
{ \
241
16
    return ossl_digest_default_get_params(params, BLAKE##VARIANT##_BLOCKBYTES, BLAKE##VARIANT##_OUTBYTES, 0); \
242
16
} \
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
IMPLEMENT_BLAKE_functions(2s, 2S, 2s256)
267
IMPLEMENT_BLAKE_functions(2b, 2B, 2b512)