Coverage Report

Created: 2025-06-13 06:36

/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
#include <openssl/crypto.h>
11
#include <openssl/core_names.h>
12
#include <openssl/proverr.h>
13
#include <openssl/err.h>
14
#include "prov/blake2.h"
15
#include "prov/digestcommon.h"
16
#include "prov/implementations.h"
17
18
#define IMPLEMENT_BLAKE_functions(variant, VARIANT, variantsize) \
19
static const OSSL_PARAM known_blake##variant##_ctx_params[] = { \
20
    {OSSL_DIGEST_PARAM_SIZE, OSSL_PARAM_UNSIGNED_INTEGER, NULL, 0, 0}, \
21
    OSSL_PARAM_END \
22
}; \
23
 \
24
const OSSL_PARAM *ossl_blake##variant##_gettable_ctx_params(ossl_unused void *ctx, \
25
139
                                                   ossl_unused void *pctx) \
26
139
{ \
27
139
    return known_blake##variant##_ctx_params; \
28
139
} \
ossl_blake2s_gettable_ctx_params
Line
Count
Source
25
63
                                                   ossl_unused void *pctx) \
26
63
{ \
27
63
    return known_blake##variant##_ctx_params; \
28
63
} \
ossl_blake2b_gettable_ctx_params
Line
Count
Source
25
76
                                                   ossl_unused void *pctx) \
26
76
{ \
27
76
    return known_blake##variant##_ctx_params; \
28
76
} \
29
 \
30
const OSSL_PARAM *ossl_blake##variant##_settable_ctx_params(ossl_unused void *ctx, \
31
0
                                                   ossl_unused void *pctx) \
32
0
{ \
33
0
    return known_blake##variant##_ctx_params; \
34
0
} \
Unexecuted instantiation: ossl_blake2s_settable_ctx_params
Unexecuted instantiation: ossl_blake2b_settable_ctx_params
35
 \
36
139
int ossl_blake##variant##_get_ctx_params(void *vctx, OSSL_PARAM params[]) \
37
139
{ \
38
139
    struct blake##variant##_md_data_st *mdctx = vctx; \
39
139
    OSSL_PARAM *p; \
40
139
 \
41
139
    BLAKE##VARIANT##_CTX *ctx = &mdctx->ctx; \
42
139
 \
43
139
    if (ctx == NULL) \
44
139
        return 0; \
45
139
    if (ossl_param_is_empty(params)) \
46
139
        return 1; \
47
139
 \
48
139
    p = OSSL_PARAM_locate(params, OSSL_DIGEST_PARAM_SIZE); \
49
139
    if (p != NULL \
50
139
        && !OSSL_PARAM_set_uint(p, (unsigned int)mdctx->params.digest_length)) { \
51
0
        ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); \
52
0
        return 0; \
53
0
    } \
54
139
 \
55
139
    return 1; \
56
139
} \
ossl_blake2s_get_ctx_params
Line
Count
Source
36
63
int ossl_blake##variant##_get_ctx_params(void *vctx, OSSL_PARAM params[]) \
37
63
{ \
38
63
    struct blake##variant##_md_data_st *mdctx = vctx; \
39
63
    OSSL_PARAM *p; \
40
63
 \
41
63
    BLAKE##VARIANT##_CTX *ctx = &mdctx->ctx; \
42
63
 \
43
63
    if (ctx == NULL) \
44
63
        return 0; \
45
63
    if (ossl_param_is_empty(params)) \
46
63
        return 1; \
47
63
 \
48
63
    p = OSSL_PARAM_locate(params, OSSL_DIGEST_PARAM_SIZE); \
49
63
    if (p != NULL \
50
63
        && !OSSL_PARAM_set_uint(p, (unsigned int)mdctx->params.digest_length)) { \
51
0
        ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); \
52
0
        return 0; \
53
0
    } \
54
63
 \
55
63
    return 1; \
56
63
} \
ossl_blake2b_get_ctx_params
Line
Count
Source
36
76
int ossl_blake##variant##_get_ctx_params(void *vctx, OSSL_PARAM params[]) \
37
76
{ \
38
76
    struct blake##variant##_md_data_st *mdctx = vctx; \
39
76
    OSSL_PARAM *p; \
40
76
 \
41
76
    BLAKE##VARIANT##_CTX *ctx = &mdctx->ctx; \
42
76
 \
43
76
    if (ctx == NULL) \
44
76
        return 0; \
45
76
    if (ossl_param_is_empty(params)) \
46
76
        return 1; \
47
76
 \
48
76
    p = OSSL_PARAM_locate(params, OSSL_DIGEST_PARAM_SIZE); \
49
76
    if (p != NULL \
50
76
        && !OSSL_PARAM_set_uint(p, (unsigned int)mdctx->params.digest_length)) { \
51
0
        ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); \
52
0
        return 0; \
53
0
    } \
54
76
 \
55
76
    return 1; \
56
76
} \
57
 \
58
278
int ossl_blake##variant##_set_ctx_params(void *vctx, const OSSL_PARAM params[]) \
59
278
{ \
60
278
    size_t size; \
61
278
    struct blake##variant##_md_data_st *mdctx = vctx; \
62
278
    const OSSL_PARAM *p; \
63
278
 \
64
278
    BLAKE##VARIANT##_CTX *ctx = &mdctx->ctx; \
65
278
 \
66
278
    if (ctx == NULL) \
67
278
        return 0; \
68
278
    if (ossl_param_is_empty(params)) \
69
278
        return 1; \
70
278
 \
71
278
    p = OSSL_PARAM_locate_const(params, OSSL_DIGEST_PARAM_SIZE); \
72
0
    if (p != NULL) { \
73
0
        if (!OSSL_PARAM_get_size_t(p, &size)) { \
74
0
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); \
75
0
            return 0; \
76
0
        } \
77
0
        if (size < 1 || size > BLAKE##VARIANT##_OUTBYTES) { \
78
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_SIZE); \
79
0
            return 0; \
80
0
        } \
81
0
        ossl_blake##variant##_param_set_digest_length(&mdctx->params, (uint8_t)size); \
82
0
    } \
83
0
 \
84
0
    return 1; \
85
0
} \
ossl_blake2s_set_ctx_params
Line
Count
Source
58
126
int ossl_blake##variant##_set_ctx_params(void *vctx, const OSSL_PARAM params[]) \
59
126
{ \
60
126
    size_t size; \
61
126
    struct blake##variant##_md_data_st *mdctx = vctx; \
62
126
    const OSSL_PARAM *p; \
63
126
 \
64
126
    BLAKE##VARIANT##_CTX *ctx = &mdctx->ctx; \
65
126
 \
66
126
    if (ctx == NULL) \
67
126
        return 0; \
68
126
    if (ossl_param_is_empty(params)) \
69
126
        return 1; \
70
126
 \
71
126
    p = OSSL_PARAM_locate_const(params, OSSL_DIGEST_PARAM_SIZE); \
72
0
    if (p != NULL) { \
73
0
        if (!OSSL_PARAM_get_size_t(p, &size)) { \
74
0
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); \
75
0
            return 0; \
76
0
        } \
77
0
        if (size < 1 || size > BLAKE##VARIANT##_OUTBYTES) { \
78
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_SIZE); \
79
0
            return 0; \
80
0
        } \
81
0
        ossl_blake##variant##_param_set_digest_length(&mdctx->params, (uint8_t)size); \
82
0
    } \
83
0
 \
84
0
    return 1; \
85
0
} \
ossl_blake2b_set_ctx_params
Line
Count
Source
58
152
int ossl_blake##variant##_set_ctx_params(void *vctx, const OSSL_PARAM params[]) \
59
152
{ \
60
152
    size_t size; \
61
152
    struct blake##variant##_md_data_st *mdctx = vctx; \
62
152
    const OSSL_PARAM *p; \
63
152
 \
64
152
    BLAKE##VARIANT##_CTX *ctx = &mdctx->ctx; \
65
152
 \
66
152
    if (ctx == NULL) \
67
152
        return 0; \
68
152
    if (ossl_param_is_empty(params)) \
69
152
        return 1; \
70
152
 \
71
152
    p = OSSL_PARAM_locate_const(params, OSSL_DIGEST_PARAM_SIZE); \
72
0
    if (p != NULL) { \
73
0
        if (!OSSL_PARAM_get_size_t(p, &size)) { \
74
0
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); \
75
0
            return 0; \
76
0
        } \
77
0
        if (size < 1 || size > BLAKE##VARIANT##_OUTBYTES) { \
78
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_SIZE); \
79
0
            return 0; \
80
0
        } \
81
0
        ossl_blake##variant##_param_set_digest_length(&mdctx->params, (uint8_t)size); \
82
0
    } \
83
0
 \
84
0
    return 1; \
85
0
} \
86
 \
87
278
static int ossl_blake##variantsize##_init(void *ctx) \
88
278
{ \
89
278
    struct blake##variant##_md_data_st *mdctx = ctx; \
90
278
    uint8_t digest_length = mdctx->params.digest_length; \
91
278
 \
92
278
    ossl_blake##variant##_param_init(&mdctx->params); \
93
278
    if (digest_length != 0) \
94
278
        mdctx->params.digest_length = digest_length; \
95
278
    return ossl_blake##variant##_init(&mdctx->ctx, &mdctx->params); \
96
278
} \
blake2_prov.c:ossl_blake2s256_init
Line
Count
Source
87
126
static int ossl_blake##variantsize##_init(void *ctx) \
88
126
{ \
89
126
    struct blake##variant##_md_data_st *mdctx = ctx; \
90
126
    uint8_t digest_length = mdctx->params.digest_length; \
91
126
 \
92
126
    ossl_blake##variant##_param_init(&mdctx->params); \
93
126
    if (digest_length != 0) \
94
126
        mdctx->params.digest_length = digest_length; \
95
126
    return ossl_blake##variant##_init(&mdctx->ctx, &mdctx->params); \
96
126
} \
blake2_prov.c:ossl_blake2b512_init
Line
Count
Source
87
152
static int ossl_blake##variantsize##_init(void *ctx) \
88
152
{ \
89
152
    struct blake##variant##_md_data_st *mdctx = ctx; \
90
152
    uint8_t digest_length = mdctx->params.digest_length; \
91
152
 \
92
152
    ossl_blake##variant##_param_init(&mdctx->params); \
93
152
    if (digest_length != 0) \
94
152
        mdctx->params.digest_length = digest_length; \
95
152
    return ossl_blake##variant##_init(&mdctx->ctx, &mdctx->params); \
96
152
} \
97
 \
98
static OSSL_FUNC_digest_init_fn blake##variantsize##_internal_init; \
99
static OSSL_FUNC_digest_newctx_fn blake##variantsize##_newctx; \
100
static OSSL_FUNC_digest_freectx_fn blake##variantsize##_freectx; \
101
static OSSL_FUNC_digest_dupctx_fn blake##variantsize##_dupctx; \
102
static OSSL_FUNC_digest_final_fn blake##variantsize##_internal_final; \
103
static OSSL_FUNC_digest_get_params_fn blake##variantsize##_get_params; \
104
 \
105
278
static int blake##variantsize##_internal_init(void *ctx, const OSSL_PARAM params[]) \
106
278
{ \
107
278
    return ossl_prov_is_running() && ossl_blake##variant##_set_ctx_params(ctx, params) \
108
278
        && ossl_blake##variantsize##_init(ctx); \
109
278
} \
blake2_prov.c:blake2s256_internal_init
Line
Count
Source
105
126
static int blake##variantsize##_internal_init(void *ctx, const OSSL_PARAM params[]) \
106
126
{ \
107
126
    return ossl_prov_is_running() && ossl_blake##variant##_set_ctx_params(ctx, params) \
108
126
        && ossl_blake##variantsize##_init(ctx); \
109
126
} \
blake2_prov.c:blake2b512_internal_init
Line
Count
Source
105
152
static int blake##variantsize##_internal_init(void *ctx, const OSSL_PARAM params[]) \
106
152
{ \
107
152
    return ossl_prov_is_running() && ossl_blake##variant##_set_ctx_params(ctx, params) \
108
152
        && ossl_blake##variantsize##_init(ctx); \
109
152
} \
110
 \
111
139
static void *blake##variantsize##_newctx(void *prov_ctx) \
112
139
{ \
113
139
    struct blake##variant##_md_data_st *ctx; \
114
139
 \
115
139
    ctx = ossl_prov_is_running() ? OPENSSL_zalloc(sizeof(*ctx)) : NULL; \
116
139
    return ctx; \
117
139
} \
blake2_prov.c:blake2s256_newctx
Line
Count
Source
111
63
static void *blake##variantsize##_newctx(void *prov_ctx) \
112
63
{ \
113
63
    struct blake##variant##_md_data_st *ctx; \
114
63
 \
115
63
    ctx = ossl_prov_is_running() ? OPENSSL_zalloc(sizeof(*ctx)) : NULL; \
116
63
    return ctx; \
117
63
} \
blake2_prov.c:blake2b512_newctx
Line
Count
Source
111
76
static void *blake##variantsize##_newctx(void *prov_ctx) \
112
76
{ \
113
76
    struct blake##variant##_md_data_st *ctx; \
114
76
 \
115
76
    ctx = ossl_prov_is_running() ? OPENSSL_zalloc(sizeof(*ctx)) : NULL; \
116
76
    return ctx; \
117
76
} \
118
 \
119
139
static void blake##variantsize##_freectx(void *vctx) \
120
139
{ \
121
139
    struct blake##variant##_md_data_st *ctx; \
122
139
 \
123
139
    ctx = (struct blake##variant##_md_data_st *)vctx; \
124
139
    OPENSSL_clear_free(ctx, sizeof(*ctx)); \
125
139
} \
blake2_prov.c:blake2s256_freectx
Line
Count
Source
119
63
static void blake##variantsize##_freectx(void *vctx) \
120
63
{ \
121
63
    struct blake##variant##_md_data_st *ctx; \
122
63
 \
123
63
    ctx = (struct blake##variant##_md_data_st *)vctx; \
124
63
    OPENSSL_clear_free(ctx, sizeof(*ctx)); \
125
63
} \
blake2_prov.c:blake2b512_freectx
Line
Count
Source
119
76
static void blake##variantsize##_freectx(void *vctx) \
120
76
{ \
121
76
    struct blake##variant##_md_data_st *ctx; \
122
76
 \
123
76
    ctx = (struct blake##variant##_md_data_st *)vctx; \
124
76
    OPENSSL_clear_free(ctx, sizeof(*ctx)); \
125
76
} \
126
 \
127
0
static void *blake##variantsize##_dupctx(void *ctx) \
128
0
{ \
129
0
    struct blake##variant##_md_data_st *in, *ret; \
130
0
 \
131
0
    in = (struct blake##variant##_md_data_st *)ctx; \
132
0
    ret = ossl_prov_is_running()? OPENSSL_malloc(sizeof(*ret)) : NULL; \
133
0
    if (ret != NULL) \
134
0
        *ret = *in; \
135
0
    return ret; \
136
0
} \
Unexecuted instantiation: blake2_prov.c:blake2s256_dupctx
Unexecuted instantiation: blake2_prov.c:blake2b512_dupctx
137
\
138
0
static void blake##variantsize##_copyctx(void *voutctx, void *vinctx) \
139
0
{ \
140
0
    struct blake##variant##_md_data_st *inctx, *outctx; \
141
0
 \
142
0
    outctx = (struct blake##variant##_md_data_st *)voutctx; \
143
0
    inctx = (struct blake##variant##_md_data_st *)vinctx; \
144
0
    *outctx = *inctx; \
145
0
} \
Unexecuted instantiation: blake2_prov.c:blake2s256_copyctx
Unexecuted instantiation: blake2_prov.c:blake2b512_copyctx
146
 \
147
static int blake##variantsize##_internal_final(void *ctx, unsigned char *out, \
148
139
                                     size_t *outl, size_t outsz) \
149
139
{ \
150
139
    struct blake##variant##_md_data_st *b_ctx; \
151
139
 \
152
139
    b_ctx = (struct blake##variant##_md_data_st *)ctx; \
153
139
 \
154
139
    if (!ossl_prov_is_running()) \
155
139
        return 0; \
156
139
 \
157
139
    *outl = b_ctx->ctx.outlen; \
158
139
 \
159
139
    if (outsz == 0) \
160
139
       return 1; \
161
139
 \
162
139
    if (outsz < *outl) { \
163
0
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_SIZE); \
164
0
        return 0; \
165
0
    } \
166
139
 \
167
139
    return ossl_blake##variant##_final(out, ctx); \
168
139
} \
blake2_prov.c:blake2s256_internal_final
Line
Count
Source
148
63
                                     size_t *outl, size_t outsz) \
149
63
{ \
150
63
    struct blake##variant##_md_data_st *b_ctx; \
151
63
 \
152
63
    b_ctx = (struct blake##variant##_md_data_st *)ctx; \
153
63
 \
154
63
    if (!ossl_prov_is_running()) \
155
63
        return 0; \
156
63
 \
157
63
    *outl = b_ctx->ctx.outlen; \
158
63
 \
159
63
    if (outsz == 0) \
160
63
       return 1; \
161
63
 \
162
63
    if (outsz < *outl) { \
163
0
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_SIZE); \
164
0
        return 0; \
165
0
    } \
166
63
 \
167
63
    return ossl_blake##variant##_final(out, ctx); \
168
63
} \
blake2_prov.c:blake2b512_internal_final
Line
Count
Source
148
76
                                     size_t *outl, size_t outsz) \
149
76
{ \
150
76
    struct blake##variant##_md_data_st *b_ctx; \
151
76
 \
152
76
    b_ctx = (struct blake##variant##_md_data_st *)ctx; \
153
76
 \
154
76
    if (!ossl_prov_is_running()) \
155
76
        return 0; \
156
76
 \
157
76
    *outl = b_ctx->ctx.outlen; \
158
76
 \
159
76
    if (outsz == 0) \
160
76
       return 1; \
161
76
 \
162
76
    if (outsz < *outl) { \
163
0
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_SIZE); \
164
0
        return 0; \
165
0
    } \
166
76
 \
167
76
    return ossl_blake##variant##_final(out, ctx); \
168
76
} \
169
 \
170
8
static int blake##variantsize##_get_params(OSSL_PARAM params[]) \
171
8
{ \
172
8
    return ossl_digest_default_get_params(params, BLAKE##VARIANT##_BLOCKBYTES, BLAKE##VARIANT##_OUTBYTES, 0); \
173
8
} \
blake2_prov.c:blake2s256_get_params
Line
Count
Source
170
4
static int blake##variantsize##_get_params(OSSL_PARAM params[]) \
171
4
{ \
172
4
    return ossl_digest_default_get_params(params, BLAKE##VARIANT##_BLOCKBYTES, BLAKE##VARIANT##_OUTBYTES, 0); \
173
4
} \
blake2_prov.c:blake2b512_get_params
Line
Count
Source
170
4
static int blake##variantsize##_get_params(OSSL_PARAM params[]) \
171
4
{ \
172
4
    return ossl_digest_default_get_params(params, BLAKE##VARIANT##_BLOCKBYTES, BLAKE##VARIANT##_OUTBYTES, 0); \
173
4
} \
174
 \
175
const OSSL_DISPATCH ossl_blake##variantsize##_functions[] = { \
176
    {OSSL_FUNC_DIGEST_NEWCTX, (void (*)(void))blake##variantsize##_newctx}, \
177
    {OSSL_FUNC_DIGEST_UPDATE, (void (*)(void))ossl_blake##variant##_update}, \
178
    {OSSL_FUNC_DIGEST_FINAL, (void (*)(void))blake##variantsize##_internal_final}, \
179
    {OSSL_FUNC_DIGEST_FREECTX, (void (*)(void))blake##variantsize##_freectx}, \
180
    {OSSL_FUNC_DIGEST_DUPCTX, (void (*)(void))blake##variantsize##_dupctx}, \
181
    {OSSL_FUNC_DIGEST_COPYCTX, (void (*)(void))blake##variantsize##_copyctx}, \
182
    {OSSL_FUNC_DIGEST_GET_PARAMS, (void (*)(void))blake##variantsize##_get_params}, \
183
    {OSSL_FUNC_DIGEST_GETTABLE_PARAMS, \
184
     (void (*)(void))ossl_digest_default_gettable_params}, \
185
    {OSSL_FUNC_DIGEST_INIT, (void (*)(void))blake##variantsize##_internal_init}, \
186
    {OSSL_FUNC_DIGEST_GETTABLE_CTX_PARAMS, \
187
     (void (*)(void))ossl_blake##variant##_gettable_ctx_params}, \
188
    {OSSL_FUNC_DIGEST_SETTABLE_CTX_PARAMS, \
189
     (void (*)(void))ossl_blake##variant##_settable_ctx_params}, \
190
    {OSSL_FUNC_DIGEST_GET_CTX_PARAMS, \
191
     (void (*)(void))ossl_blake##variant##_get_ctx_params}, \
192
    {OSSL_FUNC_DIGEST_SET_CTX_PARAMS, \
193
     (void (*)(void))ossl_blake##variant##_set_ctx_params}, \
194
    {0, NULL} \
195
};
196
197
IMPLEMENT_BLAKE_functions(2s, 2S, 2s256)
198
IMPLEMENT_BLAKE_functions(2b, 2B, 2b512)