Coverage Report

Created: 2026-02-22 06:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/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
131
{
27
131
    return blake_get_ctx_params_list;
28
131
}
29
30
static const OSSL_PARAM *blake_settable_ctx_params(ossl_unused void *ctx,
31
    ossl_unused void *pctx)
32
0
{
33
0
    return blake_set_ctx_params_list;
34
0
}
35
36
#define IMPLEMENT_BLAKE_functions(variant, VARIANT, variantsize)                                                  \
37
    int ossl_blake##variant##_get_ctx_params(void *vctx, OSSL_PARAM params[])                                     \
38
131
    {                                                                                                             \
39
131
        struct blake##variant##_md_data_st *mdctx = vctx;                                                         \
40
131
        struct blake_get_ctx_params_st p;                                                                         \
41
131
                                                                                                                  \
42
131
        BLAKE##VARIANT##_CTX *ctx = &mdctx->ctx;                                                                  \
43
131
                                                                                                                  \
44
131
        if (ctx == NULL || !blake_get_ctx_params_decoder(params, &p))                                             \
45
131
            return 0;                                                                                             \
46
131
                                                                                                                  \
47
131
        if (p.size != NULL                                                                                        \
48
131
            && !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
131
                                                                                                                  \
53
131
        return 1;                                                                                                 \
54
131
    }                                                                                                             \
55
                                                                                                                  \
56
    int ossl_blake##variant##_set_ctx_params(void *vctx, const OSSL_PARAM params[])                               \
57
262
    {                                                                                                             \
58
262
        unsigned int size;                                                                                        \
59
262
        struct blake##variant##_md_data_st *mdctx = vctx;                                                         \
60
262
        struct blake_set_ctx_params_st p;                                                                         \
61
262
                                                                                                                  \
62
262
        BLAKE##VARIANT##_CTX *ctx = &mdctx->ctx;                                                                  \
63
262
                                                                                                                  \
64
262
        if (ctx == NULL || !blake_set_ctx_params_decoder(params, &p))                                             \
65
262
            return 0;                                                                                             \
66
262
                                                                                                                  \
67
262
        if (p.size != NULL) {                                                                                     \
68
0
            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
0
            if (size < 1 || size > BLAKE##VARIANT##_OUTBYTES) {                                                   \
73
0
                ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_SIZE);                                              \
74
0
                return 0;                                                                                         \
75
0
            }                                                                                                     \
76
0
            ossl_blake##variant##_param_set_digest_length(&mdctx->params, (uint8_t)size);                         \
77
0
        }                                                                                                         \
78
262
                                                                                                                  \
79
262
        return 1;                                                                                                 \
80
262
    }                                                                                                             \
81
                                                                                                                  \
82
    static int ossl_blake##variantsize##_init(void *ctx)                                                          \
83
262
    {                                                                                                             \
84
262
        struct blake##variant##_md_data_st *mdctx = ctx;                                                          \
85
262
        uint8_t digest_length = mdctx->params.digest_length;                                                      \
86
262
                                                                                                                  \
87
262
        ossl_blake##variant##_param_init(&mdctx->params);                                                         \
88
262
        if (digest_length != 0)                                                                                   \
89
262
            mdctx->params.digest_length = digest_length;                                                          \
90
262
        return ossl_blake##variant##_init(&mdctx->ctx, &mdctx->params);                                           \
91
262
    }                                                                                                             \
blake2_prov.c:ossl_blake2s256_init
Line
Count
Source
83
110
    {                                                                                                             \
84
110
        struct blake##variant##_md_data_st *mdctx = ctx;                                                          \
85
110
        uint8_t digest_length = mdctx->params.digest_length;                                                      \
86
110
                                                                                                                  \
87
110
        ossl_blake##variant##_param_init(&mdctx->params);                                                         \
88
110
        if (digest_length != 0)                                                                                   \
89
110
            mdctx->params.digest_length = digest_length;                                                          \
90
110
        return ossl_blake##variant##_init(&mdctx->ctx, &mdctx->params);                                           \
91
110
    }                                                                                                             \
blake2_prov.c:ossl_blake2b512_init
Line
Count
Source
83
152
    {                                                                                                             \
84
152
        struct blake##variant##_md_data_st *mdctx = ctx;                                                          \
85
152
        uint8_t digest_length = mdctx->params.digest_length;                                                      \
86
152
                                                                                                                  \
87
152
        ossl_blake##variant##_param_init(&mdctx->params);                                                         \
88
152
        if (digest_length != 0)                                                                                   \
89
152
            mdctx->params.digest_length = digest_length;                                                          \
90
152
        return ossl_blake##variant##_init(&mdctx->ctx, &mdctx->params);                                           \
91
152
    }                                                                                                             \
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
262
    {                                                                                                             \
102
262
        return ossl_prov_is_running() && ossl_blake##variant##_set_ctx_params(ctx, params)                        \
103
262
            && ossl_blake##variantsize##_init(ctx);                                                               \
104
262
    }                                                                                                             \
blake2_prov.c:blake2s256_internal_init
Line
Count
Source
101
110
    {                                                                                                             \
102
110
        return ossl_prov_is_running() && ossl_blake##variant##_set_ctx_params(ctx, params)                        \
103
110
            && ossl_blake##variantsize##_init(ctx);                                                               \
104
110
    }                                                                                                             \
blake2_prov.c:blake2b512_internal_init
Line
Count
Source
101
152
    {                                                                                                             \
102
152
        return ossl_prov_is_running() && ossl_blake##variant##_set_ctx_params(ctx, params)                        \
103
152
            && ossl_blake##variantsize##_init(ctx);                                                               \
104
152
    }                                                                                                             \
105
                                                                                                                  \
106
    static void *blake##variantsize##_newctx(void *prov_ctx)                                                      \
107
131
    {                                                                                                             \
108
131
        struct blake##variant##_md_data_st *ctx;                                                                  \
109
131
                                                                                                                  \
110
131
        ctx = ossl_prov_is_running() ? OPENSSL_zalloc(sizeof(*ctx)) : NULL;                                       \
111
131
        return ctx;                                                                                               \
112
131
    }                                                                                                             \
113
                                                                                                                  \
114
    static void blake##variantsize##_freectx(void *vctx)                                                          \
115
131
    {                                                                                                             \
116
131
        struct blake##variant##_md_data_st *ctx;                                                                  \
117
131
                                                                                                                  \
118
131
        ctx = (struct blake##variant##_md_data_st *)vctx;                                                         \
119
131
        OPENSSL_clear_free(ctx, sizeof(*ctx));                                                                    \
120
131
    }                                                                                                             \
blake2_prov.c:blake2s256_freectx
Line
Count
Source
115
55
    {                                                                                                             \
116
55
        struct blake##variant##_md_data_st *ctx;                                                                  \
117
55
                                                                                                                  \
118
55
        ctx = (struct blake##variant##_md_data_st *)vctx;                                                         \
119
55
        OPENSSL_clear_free(ctx, sizeof(*ctx));                                                                    \
120
55
    }                                                                                                             \
blake2_prov.c:blake2b512_freectx
Line
Count
Source
115
76
    {                                                                                                             \
116
76
        struct blake##variant##_md_data_st *ctx;                                                                  \
117
76
                                                                                                                  \
118
76
        ctx = (struct blake##variant##_md_data_st *)vctx;                                                         \
119
76
        OPENSSL_clear_free(ctx, sizeof(*ctx));                                                                    \
120
76
    }                                                                                                             \
121
                                                                                                                  \
122
    static void *blake##variantsize##_dupctx(void *ctx)                                                           \
123
0
    {                                                                                                             \
124
0
        struct blake##variant##_md_data_st *in, *ret;                                                             \
125
0
                                                                                                                  \
126
0
        in = (struct blake##variant##_md_data_st *)ctx;                                                           \
127
0
        ret = ossl_prov_is_running() ? OPENSSL_malloc(sizeof(*ret)) : NULL;                                       \
128
0
        if (ret != NULL)                                                                                          \
129
0
            *ret = *in;                                                                                           \
130
0
        return ret;                                                                                               \
131
0
    }                                                                                                             \
132
                                                                                                                  \
133
    static void blake##variantsize##_copyctx(void *voutctx, void *vinctx)                                         \
134
0
    {                                                                                                             \
135
0
        struct blake##variant##_md_data_st *inctx, *outctx;                                                       \
136
0
                                                                                                                  \
137
0
        outctx = (struct blake##variant##_md_data_st *)voutctx;                                                   \
138
0
        inctx = (struct blake##variant##_md_data_st *)vinctx;                                                     \
139
0
        *outctx = *inctx;                                                                                         \
140
0
    }                                                                                                             \
Unexecuted instantiation: blake2_prov.c:blake2s256_copyctx
Unexecuted instantiation: blake2_prov.c:blake2b512_copyctx
141
                                                                                                                  \
142
    static int blake##variantsize##_internal_final(void *ctx, unsigned char *out,                                 \
143
        size_t *outl, size_t outsz)                                                                               \
144
131
    {                                                                                                             \
145
131
        struct blake##variant##_md_data_st *b_ctx;                                                                \
146
131
                                                                                                                  \
147
131
        b_ctx = (struct blake##variant##_md_data_st *)ctx;                                                        \
148
131
                                                                                                                  \
149
131
        if (!ossl_prov_is_running())                                                                              \
150
131
            return 0;                                                                                             \
151
131
                                                                                                                  \
152
131
        *outl = b_ctx->ctx.outlen;                                                                                \
153
131
                                                                                                                  \
154
131
        if (outsz == 0)                                                                                           \
155
131
            return 1;                                                                                             \
156
131
                                                                                                                  \
157
131
        if (outsz < *outl) {                                                                                      \
158
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_SIZE);                                                  \
159
0
            return 0;                                                                                             \
160
0
        }                                                                                                         \
161
131
                                                                                                                  \
162
131
        return ossl_blake##variant##_final(out, ctx);                                                             \
163
131
    }                                                                                                             \
164
                                                                                                                  \
165
    static int blake##variantsize##_get_params(OSSL_PARAM params[])                                               \
166
8
    {                                                                                                             \
167
8
        return ossl_digest_default_get_params(params, BLAKE##VARIANT##_BLOCKBYTES, BLAKE##VARIANT##_OUTBYTES, 0); \
168
8
    }                                                                                                             \
blake2_prov.c:blake2s256_get_params
Line
Count
Source
166
4
    {                                                                                                             \
167
4
        return ossl_digest_default_get_params(params, BLAKE##VARIANT##_BLOCKBYTES, BLAKE##VARIANT##_OUTBYTES, 0); \
168
4
    }                                                                                                             \
blake2_prov.c:blake2b512_get_params
Line
Count
Source
166
4
    {                                                                                                             \
167
4
        return ossl_digest_default_get_params(params, BLAKE##VARIANT##_BLOCKBYTES, BLAKE##VARIANT##_OUTBYTES, 0); \
168
4
    }                                                                                                             \
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
275
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
Unexecuted instantiation: blake2_prov.c:blake2s256_internal_final
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
Unexecuted instantiation: blake2_prov.c:blake2b512_internal_final
Unexecuted instantiation: blake2_prov.c:blake2b512_dupctx