Coverage Report

Created: 2025-06-22 06:56

/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
153
                                                   ossl_unused void *pctx) \
26
153
{ \
27
153
    return known_blake##variant##_ctx_params; \
28
153
} \
ossl_blake2s_gettable_ctx_params
Line
Count
Source
25
77
                                                   ossl_unused void *pctx) \
26
77
{ \
27
77
    return known_blake##variant##_ctx_params; \
28
77
} \
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
153
int ossl_blake##variant##_get_ctx_params(void *vctx, OSSL_PARAM params[]) \
37
153
{ \
38
153
    struct blake##variant##_md_data_st *mdctx = vctx; \
39
153
    OSSL_PARAM *p; \
40
153
 \
41
153
    BLAKE##VARIANT##_CTX *ctx = &mdctx->ctx; \
42
153
 \
43
153
    if (ctx == NULL) \
44
153
        return 0; \
45
153
    if (ossl_param_is_empty(params)) \
46
153
        return 1; \
47
153
 \
48
153
    p = OSSL_PARAM_locate(params, OSSL_DIGEST_PARAM_SIZE); \
49
153
    if (p != NULL \
50
153
        && !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
153
 \
55
153
    return 1; \
56
153
} \
ossl_blake2s_get_ctx_params
Line
Count
Source
36
77
int ossl_blake##variant##_get_ctx_params(void *vctx, OSSL_PARAM params[]) \
37
77
{ \
38
77
    struct blake##variant##_md_data_st *mdctx = vctx; \
39
77
    OSSL_PARAM *p; \
40
77
 \
41
77
    BLAKE##VARIANT##_CTX *ctx = &mdctx->ctx; \
42
77
 \
43
77
    if (ctx == NULL) \
44
77
        return 0; \
45
77
    if (ossl_param_is_empty(params)) \
46
77
        return 1; \
47
77
 \
48
77
    p = OSSL_PARAM_locate(params, OSSL_DIGEST_PARAM_SIZE); \
49
77
    if (p != NULL \
50
77
        && !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
77
 \
55
77
    return 1; \
56
77
} \
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
306
int ossl_blake##variant##_set_ctx_params(void *vctx, const OSSL_PARAM params[]) \
59
306
{ \
60
306
    size_t size; \
61
306
    struct blake##variant##_md_data_st *mdctx = vctx; \
62
306
    const OSSL_PARAM *p; \
63
306
 \
64
306
    BLAKE##VARIANT##_CTX *ctx = &mdctx->ctx; \
65
306
 \
66
306
    if (ctx == NULL) \
67
306
        return 0; \
68
306
    if (ossl_param_is_empty(params)) \
69
306
        return 1; \
70
306
 \
71
306
    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
154
int ossl_blake##variant##_set_ctx_params(void *vctx, const OSSL_PARAM params[]) \
59
154
{ \
60
154
    size_t size; \
61
154
    struct blake##variant##_md_data_st *mdctx = vctx; \
62
154
    const OSSL_PARAM *p; \
63
154
 \
64
154
    BLAKE##VARIANT##_CTX *ctx = &mdctx->ctx; \
65
154
 \
66
154
    if (ctx == NULL) \
67
154
        return 0; \
68
154
    if (ossl_param_is_empty(params)) \
69
154
        return 1; \
70
154
 \
71
154
    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
306
static int ossl_blake##variantsize##_init(void *ctx) \
88
306
{ \
89
306
    struct blake##variant##_md_data_st *mdctx = ctx; \
90
306
    uint8_t digest_length = mdctx->params.digest_length; \
91
306
 \
92
306
    ossl_blake##variant##_param_init(&mdctx->params); \
93
306
    if (digest_length != 0) \
94
306
        mdctx->params.digest_length = digest_length; \
95
306
    return ossl_blake##variant##_init(&mdctx->ctx, &mdctx->params); \
96
306
} \
blake2_prov.c:ossl_blake2s256_init
Line
Count
Source
87
154
static int ossl_blake##variantsize##_init(void *ctx) \
88
154
{ \
89
154
    struct blake##variant##_md_data_st *mdctx = ctx; \
90
154
    uint8_t digest_length = mdctx->params.digest_length; \
91
154
 \
92
154
    ossl_blake##variant##_param_init(&mdctx->params); \
93
154
    if (digest_length != 0) \
94
154
        mdctx->params.digest_length = digest_length; \
95
154
    return ossl_blake##variant##_init(&mdctx->ctx, &mdctx->params); \
96
154
} \
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
306
static int blake##variantsize##_internal_init(void *ctx, const OSSL_PARAM params[]) \
106
306
{ \
107
306
    return ossl_prov_is_running() && ossl_blake##variant##_set_ctx_params(ctx, params) \
108
306
        && ossl_blake##variantsize##_init(ctx); \
109
306
} \
blake2_prov.c:blake2s256_internal_init
Line
Count
Source
105
154
static int blake##variantsize##_internal_init(void *ctx, const OSSL_PARAM params[]) \
106
154
{ \
107
154
    return ossl_prov_is_running() && ossl_blake##variant##_set_ctx_params(ctx, params) \
108
154
        && ossl_blake##variantsize##_init(ctx); \
109
154
} \
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
153
static void *blake##variantsize##_newctx(void *prov_ctx) \
112
153
{ \
113
153
    struct blake##variant##_md_data_st *ctx; \
114
153
 \
115
153
    ctx = ossl_prov_is_running() ? OPENSSL_zalloc(sizeof(*ctx)) : NULL; \
116
153
    return ctx; \
117
153
} \
blake2_prov.c:blake2s256_newctx
Line
Count
Source
111
77
static void *blake##variantsize##_newctx(void *prov_ctx) \
112
77
{ \
113
77
    struct blake##variant##_md_data_st *ctx; \
114
77
 \
115
77
    ctx = ossl_prov_is_running() ? OPENSSL_zalloc(sizeof(*ctx)) : NULL; \
116
77
    return ctx; \
117
77
} \
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
153
static void blake##variantsize##_freectx(void *vctx) \
120
153
{ \
121
153
    struct blake##variant##_md_data_st *ctx; \
122
153
 \
123
153
    ctx = (struct blake##variant##_md_data_st *)vctx; \
124
153
    OPENSSL_clear_free(ctx, sizeof(*ctx)); \
125
153
} \
blake2_prov.c:blake2s256_freectx
Line
Count
Source
119
77
static void blake##variantsize##_freectx(void *vctx) \
120
77
{ \
121
77
    struct blake##variant##_md_data_st *ctx; \
122
77
 \
123
77
    ctx = (struct blake##variant##_md_data_st *)vctx; \
124
77
    OPENSSL_clear_free(ctx, sizeof(*ctx)); \
125
77
} \
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
153
                                     size_t *outl, size_t outsz) \
149
153
{ \
150
153
    struct blake##variant##_md_data_st *b_ctx; \
151
153
 \
152
153
    b_ctx = (struct blake##variant##_md_data_st *)ctx; \
153
153
 \
154
153
    if (!ossl_prov_is_running()) \
155
153
        return 0; \
156
153
 \
157
153
    *outl = b_ctx->ctx.outlen; \
158
153
 \
159
153
    if (outsz == 0) \
160
153
       return 1; \
161
153
 \
162
153
    if (outsz < *outl) { \
163
0
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_SIZE); \
164
0
        return 0; \
165
0
    } \
166
153
 \
167
153
    return ossl_blake##variant##_final(out, ctx); \
168
153
} \
blake2_prov.c:blake2s256_internal_final
Line
Count
Source
148
77
                                     size_t *outl, size_t outsz) \
149
77
{ \
150
77
    struct blake##variant##_md_data_st *b_ctx; \
151
77
 \
152
77
    b_ctx = (struct blake##variant##_md_data_st *)ctx; \
153
77
 \
154
77
    if (!ossl_prov_is_running()) \
155
77
        return 0; \
156
77
 \
157
77
    *outl = b_ctx->ctx.outlen; \
158
77
 \
159
77
    if (outsz == 0) \
160
77
       return 1; \
161
77
 \
162
77
    if (outsz < *outl) { \
163
0
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_SIZE); \
164
0
        return 0; \
165
0
    } \
166
77
 \
167
77
    return ossl_blake##variant##_final(out, ctx); \
168
77
} \
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)