Coverage Report

Created: 2026-09-12 06:55

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