Coverage Report

Created: 2025-08-28 06:41

/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
145
{
42
145
    const char *s;
43
44
145
    memset(r, 0, sizeof(*r));
45
145
    if (p != NULL)
46
290
        for (; (s = p->key) != NULL; p++)
47
145
            if (ossl_likely(strcmp("size", s + 0) == 0)) {
48
                /* DIGEST_PARAM_SIZE */
49
145
                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
145
                r->size = (OSSL_PARAM *)p;
55
145
            }
56
145
    return 1;
57
145
}
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
145
{
64
145
    return blake_get_ctx_params_list;
65
145
}
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
290
{
85
290
    const char *s;
86
87
290
    memset(r, 0, sizeof(*r));
88
290
    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
290
    return 1;
100
290
}
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
145
int ossl_blake##variant##_get_ctx_params(void *vctx, OSSL_PARAM params[]) \
112
145
{ \
113
145
    struct blake##variant##_md_data_st *mdctx = vctx; \
114
145
    struct blake_get_ctx_params_st p; \
115
145
 \
116
145
    BLAKE##VARIANT##_CTX *ctx = &mdctx->ctx; \
117
145
 \
118
145
    if (ctx == NULL || !blake_get_ctx_params_decoder(params, &p)) \
119
145
        return 0; \
120
145
 \
121
145
    if (p.size != NULL \
122
145
        && !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
145
 \
127
145
    return 1; \
128
145
} \
ossl_blake2s_get_ctx_params
Line
Count
Source
111
80
int ossl_blake##variant##_get_ctx_params(void *vctx, OSSL_PARAM params[]) \
112
80
{ \
113
80
    struct blake##variant##_md_data_st *mdctx = vctx; \
114
80
    struct blake_get_ctx_params_st p; \
115
80
 \
116
80
    BLAKE##VARIANT##_CTX *ctx = &mdctx->ctx; \
117
80
 \
118
80
    if (ctx == NULL || !blake_get_ctx_params_decoder(params, &p)) \
119
80
        return 0; \
120
80
 \
121
80
    if (p.size != NULL \
122
80
        && !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
80
 \
127
80
    return 1; \
128
80
} \
ossl_blake2b_get_ctx_params
Line
Count
Source
111
65
int ossl_blake##variant##_get_ctx_params(void *vctx, OSSL_PARAM params[]) \
112
65
{ \
113
65
    struct blake##variant##_md_data_st *mdctx = vctx; \
114
65
    struct blake_get_ctx_params_st p; \
115
65
 \
116
65
    BLAKE##VARIANT##_CTX *ctx = &mdctx->ctx; \
117
65
 \
118
65
    if (ctx == NULL || !blake_get_ctx_params_decoder(params, &p)) \
119
65
        return 0; \
120
65
 \
121
65
    if (p.size != NULL \
122
65
        && !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
65
 \
127
65
    return 1; \
128
65
} \
129
 \
130
290
int ossl_blake##variant##_set_ctx_params(void *vctx, const OSSL_PARAM params[]) \
131
290
{ \
132
290
    unsigned int size; \
133
290
    struct blake##variant##_md_data_st *mdctx = vctx; \
134
290
    struct blake_set_ctx_params_st p; \
135
290
 \
136
290
    BLAKE##VARIANT##_CTX *ctx = &mdctx->ctx; \
137
290
 \
138
290
    if (ctx == NULL || !blake_set_ctx_params_decoder(params, &p)) \
139
290
        return 0; \
140
290
 \
141
290
    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
290
 \
153
290
    return 1; \
154
290
} \
ossl_blake2s_set_ctx_params
Line
Count
Source
130
160
int ossl_blake##variant##_set_ctx_params(void *vctx, const OSSL_PARAM params[]) \
131
160
{ \
132
160
    unsigned int size; \
133
160
    struct blake##variant##_md_data_st *mdctx = vctx; \
134
160
    struct blake_set_ctx_params_st p; \
135
160
 \
136
160
    BLAKE##VARIANT##_CTX *ctx = &mdctx->ctx; \
137
160
 \
138
160
    if (ctx == NULL || !blake_set_ctx_params_decoder(params, &p)) \
139
160
        return 0; \
140
160
 \
141
160
    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
160
 \
153
160
    return 1; \
154
160
} \
ossl_blake2b_set_ctx_params
Line
Count
Source
130
130
int ossl_blake##variant##_set_ctx_params(void *vctx, const OSSL_PARAM params[]) \
131
130
{ \
132
130
    unsigned int size; \
133
130
    struct blake##variant##_md_data_st *mdctx = vctx; \
134
130
    struct blake_set_ctx_params_st p; \
135
130
 \
136
130
    BLAKE##VARIANT##_CTX *ctx = &mdctx->ctx; \
137
130
 \
138
130
    if (ctx == NULL || !blake_set_ctx_params_decoder(params, &p)) \
139
130
        return 0; \
140
130
 \
141
130
    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
130
 \
153
130
    return 1; \
154
130
} \
155
 \
156
290
static int ossl_blake##variantsize##_init(void *ctx) \
157
290
{ \
158
290
    struct blake##variant##_md_data_st *mdctx = ctx; \
159
290
    uint8_t digest_length = mdctx->params.digest_length; \
160
290
 \
161
290
    ossl_blake##variant##_param_init(&mdctx->params); \
162
290
    if (digest_length != 0) \
163
290
        mdctx->params.digest_length = digest_length; \
164
290
    return ossl_blake##variant##_init(&mdctx->ctx, &mdctx->params); \
165
290
} \
blake2_prov.c:ossl_blake2s256_init
Line
Count
Source
156
160
static int ossl_blake##variantsize##_init(void *ctx) \
157
160
{ \
158
160
    struct blake##variant##_md_data_st *mdctx = ctx; \
159
160
    uint8_t digest_length = mdctx->params.digest_length; \
160
160
 \
161
160
    ossl_blake##variant##_param_init(&mdctx->params); \
162
160
    if (digest_length != 0) \
163
160
        mdctx->params.digest_length = digest_length; \
164
160
    return ossl_blake##variant##_init(&mdctx->ctx, &mdctx->params); \
165
160
} \
blake2_prov.c:ossl_blake2b512_init
Line
Count
Source
156
130
static int ossl_blake##variantsize##_init(void *ctx) \
157
130
{ \
158
130
    struct blake##variant##_md_data_st *mdctx = ctx; \
159
130
    uint8_t digest_length = mdctx->params.digest_length; \
160
130
 \
161
130
    ossl_blake##variant##_param_init(&mdctx->params); \
162
130
    if (digest_length != 0) \
163
130
        mdctx->params.digest_length = digest_length; \
164
130
    return ossl_blake##variant##_init(&mdctx->ctx, &mdctx->params); \
165
130
} \
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
290
static int blake##variantsize##_internal_init(void *ctx, const OSSL_PARAM params[]) \
175
290
{ \
176
290
    return ossl_prov_is_running() && ossl_blake##variant##_set_ctx_params(ctx, params) \
177
290
        && ossl_blake##variantsize##_init(ctx); \
178
290
} \
blake2_prov.c:blake2s256_internal_init
Line
Count
Source
174
160
static int blake##variantsize##_internal_init(void *ctx, const OSSL_PARAM params[]) \
175
160
{ \
176
160
    return ossl_prov_is_running() && ossl_blake##variant##_set_ctx_params(ctx, params) \
177
160
        && ossl_blake##variantsize##_init(ctx); \
178
160
} \
blake2_prov.c:blake2b512_internal_init
Line
Count
Source
174
130
static int blake##variantsize##_internal_init(void *ctx, const OSSL_PARAM params[]) \
175
130
{ \
176
130
    return ossl_prov_is_running() && ossl_blake##variant##_set_ctx_params(ctx, params) \
177
130
        && ossl_blake##variantsize##_init(ctx); \
178
130
} \
179
 \
180
145
static void *blake##variantsize##_newctx(void *prov_ctx) \
181
145
{ \
182
145
    struct blake##variant##_md_data_st *ctx; \
183
145
 \
184
145
    ctx = ossl_prov_is_running() ? OPENSSL_zalloc(sizeof(*ctx)) : NULL; \
185
145
    return ctx; \
186
145
} \
blake2_prov.c:blake2s256_newctx
Line
Count
Source
180
80
static void *blake##variantsize##_newctx(void *prov_ctx) \
181
80
{ \
182
80
    struct blake##variant##_md_data_st *ctx; \
183
80
 \
184
80
    ctx = ossl_prov_is_running() ? OPENSSL_zalloc(sizeof(*ctx)) : NULL; \
185
80
    return ctx; \
186
80
} \
blake2_prov.c:blake2b512_newctx
Line
Count
Source
180
65
static void *blake##variantsize##_newctx(void *prov_ctx) \
181
65
{ \
182
65
    struct blake##variant##_md_data_st *ctx; \
183
65
 \
184
65
    ctx = ossl_prov_is_running() ? OPENSSL_zalloc(sizeof(*ctx)) : NULL; \
185
65
    return ctx; \
186
65
} \
187
 \
188
145
static void blake##variantsize##_freectx(void *vctx) \
189
145
{ \
190
145
    struct blake##variant##_md_data_st *ctx; \
191
145
 \
192
145
    ctx = (struct blake##variant##_md_data_st *)vctx; \
193
145
    OPENSSL_clear_free(ctx, sizeof(*ctx)); \
194
145
} \
blake2_prov.c:blake2s256_freectx
Line
Count
Source
188
80
static void blake##variantsize##_freectx(void *vctx) \
189
80
{ \
190
80
    struct blake##variant##_md_data_st *ctx; \
191
80
 \
192
80
    ctx = (struct blake##variant##_md_data_st *)vctx; \
193
80
    OPENSSL_clear_free(ctx, sizeof(*ctx)); \
194
80
} \
blake2_prov.c:blake2b512_freectx
Line
Count
Source
188
65
static void blake##variantsize##_freectx(void *vctx) \
189
65
{ \
190
65
    struct blake##variant##_md_data_st *ctx; \
191
65
 \
192
65
    ctx = (struct blake##variant##_md_data_st *)vctx; \
193
65
    OPENSSL_clear_free(ctx, sizeof(*ctx)); \
194
65
} \
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
145
                                     size_t *outl, size_t outsz) \
218
145
{ \
219
145
    struct blake##variant##_md_data_st *b_ctx; \
220
145
 \
221
145
    b_ctx = (struct blake##variant##_md_data_st *)ctx; \
222
145
 \
223
145
    if (!ossl_prov_is_running()) \
224
145
        return 0; \
225
145
 \
226
145
    *outl = b_ctx->ctx.outlen; \
227
145
 \
228
145
    if (outsz == 0) \
229
145
       return 1; \
230
145
 \
231
145
    if (outsz < *outl) { \
232
0
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_SIZE); \
233
0
        return 0; \
234
0
    } \
235
145
 \
236
145
    return ossl_blake##variant##_final(out, ctx); \
237
145
} \
blake2_prov.c:blake2s256_internal_final
Line
Count
Source
217
80
                                     size_t *outl, size_t outsz) \
218
80
{ \
219
80
    struct blake##variant##_md_data_st *b_ctx; \
220
80
 \
221
80
    b_ctx = (struct blake##variant##_md_data_st *)ctx; \
222
80
 \
223
80
    if (!ossl_prov_is_running()) \
224
80
        return 0; \
225
80
 \
226
80
    *outl = b_ctx->ctx.outlen; \
227
80
 \
228
80
    if (outsz == 0) \
229
80
       return 1; \
230
80
 \
231
80
    if (outsz < *outl) { \
232
0
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_SIZE); \
233
0
        return 0; \
234
0
    } \
235
80
 \
236
80
    return ossl_blake##variant##_final(out, ctx); \
237
80
} \
blake2_prov.c:blake2b512_internal_final
Line
Count
Source
217
65
                                     size_t *outl, size_t outsz) \
218
65
{ \
219
65
    struct blake##variant##_md_data_st *b_ctx; \
220
65
 \
221
65
    b_ctx = (struct blake##variant##_md_data_st *)ctx; \
222
65
 \
223
65
    if (!ossl_prov_is_running()) \
224
65
        return 0; \
225
65
 \
226
65
    *outl = b_ctx->ctx.outlen; \
227
65
 \
228
65
    if (outsz == 0) \
229
65
       return 1; \
230
65
 \
231
65
    if (outsz < *outl) { \
232
0
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_SIZE); \
233
0
        return 0; \
234
0
    } \
235
65
 \
236
65
    return ossl_blake##variant##_final(out, ctx); \
237
65
} \
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
IMPLEMENT_BLAKE_functions(2s, 2S, 2s256)
267
IMPLEMENT_BLAKE_functions(2b, 2B, 2b512)