Coverage Report

Created: 2024-05-21 06:33

/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
0
                                                   ossl_unused void *pctx) \
26
0
{ \
27
0
    return known_blake##variant##_ctx_params; \
28
0
} \
Unexecuted instantiation: ossl_blake2s_gettable_ctx_params
Unexecuted instantiation: ossl_blake2b_gettable_ctx_params
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
173
int ossl_blake##variant##_get_ctx_params(void *vctx, OSSL_PARAM params[]) \
37
173
{ \
38
173
    struct blake##variant##_md_data_st *mdctx = vctx; \
39
173
    OSSL_PARAM *p; \
40
173
 \
41
173
    BLAKE##VARIANT##_CTX *ctx = &mdctx->ctx; \
42
173
 \
43
173
    if (ctx == NULL) \
44
173
        return 0; \
45
173
    if (params == NULL) \
46
173
        return 1; \
47
173
 \
48
173
    p = OSSL_PARAM_locate(params, OSSL_DIGEST_PARAM_SIZE); \
49
173
    if (p != NULL \
50
173
        && !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
173
 \
55
173
    return 1; \
56
173
} \
ossl_blake2s_get_ctx_params
Line
Count
Source
36
84
int ossl_blake##variant##_get_ctx_params(void *vctx, OSSL_PARAM params[]) \
37
84
{ \
38
84
    struct blake##variant##_md_data_st *mdctx = vctx; \
39
84
    OSSL_PARAM *p; \
40
84
 \
41
84
    BLAKE##VARIANT##_CTX *ctx = &mdctx->ctx; \
42
84
 \
43
84
    if (ctx == NULL) \
44
84
        return 0; \
45
84
    if (params == NULL) \
46
84
        return 1; \
47
84
 \
48
84
    p = OSSL_PARAM_locate(params, OSSL_DIGEST_PARAM_SIZE); \
49
84
    if (p != NULL \
50
84
        && !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
84
 \
55
84
    return 1; \
56
84
} \
ossl_blake2b_get_ctx_params
Line
Count
Source
36
89
int ossl_blake##variant##_get_ctx_params(void *vctx, OSSL_PARAM params[]) \
37
89
{ \
38
89
    struct blake##variant##_md_data_st *mdctx = vctx; \
39
89
    OSSL_PARAM *p; \
40
89
 \
41
89
    BLAKE##VARIANT##_CTX *ctx = &mdctx->ctx; \
42
89
 \
43
89
    if (ctx == NULL) \
44
89
        return 0; \
45
89
    if (params == NULL) \
46
89
        return 1; \
47
89
 \
48
89
    p = OSSL_PARAM_locate(params, OSSL_DIGEST_PARAM_SIZE); \
49
89
    if (p != NULL \
50
89
        && !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
89
 \
55
89
    return 1; \
56
89
} \
57
 \
58
346
int ossl_blake##variant##_set_ctx_params(void *vctx, const OSSL_PARAM params[]) \
59
346
{ \
60
346
    size_t size; \
61
346
    struct blake##variant##_md_data_st *mdctx = vctx; \
62
346
    const OSSL_PARAM *p; \
63
346
 \
64
346
    BLAKE##VARIANT##_CTX *ctx = &mdctx->ctx; \
65
346
 \
66
346
    if (ctx == NULL) \
67
346
        return 0; \
68
346
    if (params == NULL) \
69
346
        return 1; \
70
346
 \
71
346
    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
168
int ossl_blake##variant##_set_ctx_params(void *vctx, const OSSL_PARAM params[]) \
59
168
{ \
60
168
    size_t size; \
61
168
    struct blake##variant##_md_data_st *mdctx = vctx; \
62
168
    const OSSL_PARAM *p; \
63
168
 \
64
168
    BLAKE##VARIANT##_CTX *ctx = &mdctx->ctx; \
65
168
 \
66
168
    if (ctx == NULL) \
67
168
        return 0; \
68
168
    if (params == NULL) \
69
168
        return 1; \
70
168
 \
71
168
    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
178
int ossl_blake##variant##_set_ctx_params(void *vctx, const OSSL_PARAM params[]) \
59
178
{ \
60
178
    size_t size; \
61
178
    struct blake##variant##_md_data_st *mdctx = vctx; \
62
178
    const OSSL_PARAM *p; \
63
178
 \
64
178
    BLAKE##VARIANT##_CTX *ctx = &mdctx->ctx; \
65
178
 \
66
178
    if (ctx == NULL) \
67
178
        return 0; \
68
178
    if (params == NULL) \
69
178
        return 1; \
70
178
 \
71
178
    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
346
static int ossl_blake##variantsize##_init(void *ctx) \
88
346
{ \
89
346
    struct blake##variant##_md_data_st *mdctx = ctx; \
90
346
    uint8_t digest_length = mdctx->params.digest_length; \
91
346
 \
92
346
    ossl_blake##variant##_param_init(&mdctx->params); \
93
346
    if (digest_length != 0) \
94
346
        mdctx->params.digest_length = digest_length; \
95
346
    return ossl_blake##variant##_init(&mdctx->ctx, &mdctx->params); \
96
346
} \
blake2_prov.c:ossl_blake2s256_init
Line
Count
Source
87
168
static int ossl_blake##variantsize##_init(void *ctx) \
88
168
{ \
89
168
    struct blake##variant##_md_data_st *mdctx = ctx; \
90
168
    uint8_t digest_length = mdctx->params.digest_length; \
91
168
 \
92
168
    ossl_blake##variant##_param_init(&mdctx->params); \
93
168
    if (digest_length != 0) \
94
168
        mdctx->params.digest_length = digest_length; \
95
168
    return ossl_blake##variant##_init(&mdctx->ctx, &mdctx->params); \
96
168
} \
blake2_prov.c:ossl_blake2b512_init
Line
Count
Source
87
178
static int ossl_blake##variantsize##_init(void *ctx) \
88
178
{ \
89
178
    struct blake##variant##_md_data_st *mdctx = ctx; \
90
178
    uint8_t digest_length = mdctx->params.digest_length; \
91
178
 \
92
178
    ossl_blake##variant##_param_init(&mdctx->params); \
93
178
    if (digest_length != 0) \
94
178
        mdctx->params.digest_length = digest_length; \
95
178
    return ossl_blake##variant##_init(&mdctx->ctx, &mdctx->params); \
96
178
} \
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
346
static int blake##variantsize##_internal_init(void *ctx, const OSSL_PARAM params[]) \
106
346
{ \
107
346
    return ossl_prov_is_running() && ossl_blake##variant##_set_ctx_params(ctx, params) \
108
346
        && ossl_blake##variantsize##_init(ctx); \
109
346
} \
blake2_prov.c:blake2s256_internal_init
Line
Count
Source
105
168
static int blake##variantsize##_internal_init(void *ctx, const OSSL_PARAM params[]) \
106
168
{ \
107
168
    return ossl_prov_is_running() && ossl_blake##variant##_set_ctx_params(ctx, params) \
108
168
        && ossl_blake##variantsize##_init(ctx); \
109
168
} \
blake2_prov.c:blake2b512_internal_init
Line
Count
Source
105
178
static int blake##variantsize##_internal_init(void *ctx, const OSSL_PARAM params[]) \
106
178
{ \
107
178
    return ossl_prov_is_running() && ossl_blake##variant##_set_ctx_params(ctx, params) \
108
178
        && ossl_blake##variantsize##_init(ctx); \
109
178
} \
110
 \
111
173
static void *blake##variantsize##_newctx(void *prov_ctx) \
112
173
{ \
113
173
    struct blake##variant##_md_data_st *ctx; \
114
173
 \
115
173
    ctx = ossl_prov_is_running() ? OPENSSL_zalloc(sizeof(*ctx)) : NULL; \
116
173
    return ctx; \
117
173
} \
blake2_prov.c:blake2s256_newctx
Line
Count
Source
111
84
static void *blake##variantsize##_newctx(void *prov_ctx) \
112
84
{ \
113
84
    struct blake##variant##_md_data_st *ctx; \
114
84
 \
115
84
    ctx = ossl_prov_is_running() ? OPENSSL_zalloc(sizeof(*ctx)) : NULL; \
116
84
    return ctx; \
117
84
} \
blake2_prov.c:blake2b512_newctx
Line
Count
Source
111
89
static void *blake##variantsize##_newctx(void *prov_ctx) \
112
89
{ \
113
89
    struct blake##variant##_md_data_st *ctx; \
114
89
 \
115
89
    ctx = ossl_prov_is_running() ? OPENSSL_zalloc(sizeof(*ctx)) : NULL; \
116
89
    return ctx; \
117
89
} \
118
 \
119
173
static void blake##variantsize##_freectx(void *vctx) \
120
173
{ \
121
173
    struct blake##variant##_md_data_st *ctx; \
122
173
 \
123
173
    ctx = (struct blake##variant##_md_data_st *)vctx; \
124
173
    OPENSSL_clear_free(ctx, sizeof(*ctx)); \
125
173
} \
blake2_prov.c:blake2s256_freectx
Line
Count
Source
119
84
static void blake##variantsize##_freectx(void *vctx) \
120
84
{ \
121
84
    struct blake##variant##_md_data_st *ctx; \
122
84
 \
123
84
    ctx = (struct blake##variant##_md_data_st *)vctx; \
124
84
    OPENSSL_clear_free(ctx, sizeof(*ctx)); \
125
84
} \
blake2_prov.c:blake2b512_freectx
Line
Count
Source
119
89
static void blake##variantsize##_freectx(void *vctx) \
120
89
{ \
121
89
    struct blake##variant##_md_data_st *ctx; \
122
89
 \
123
89
    ctx = (struct blake##variant##_md_data_st *)vctx; \
124
89
    OPENSSL_clear_free(ctx, sizeof(*ctx)); \
125
89
} \
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
static int blake##variantsize##_internal_final(void *ctx, unsigned char *out, \
139
173
                                     size_t *outl, size_t outsz) \
140
173
{ \
141
173
    struct blake##variant##_md_data_st *b_ctx; \
142
173
 \
143
173
    b_ctx = (struct blake##variant##_md_data_st *)ctx; \
144
173
 \
145
173
    if (!ossl_prov_is_running()) \
146
173
        return 0; \
147
173
 \
148
173
    *outl = b_ctx->ctx.outlen; \
149
173
 \
150
173
    if (outsz == 0) \
151
173
       return 1; \
152
173
 \
153
173
    if (outsz < *outl) { \
154
0
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_SIZE); \
155
0
        return 0; \
156
0
    } \
157
173
 \
158
173
    return ossl_blake##variant##_final(out, ctx); \
159
173
} \
blake2_prov.c:blake2s256_internal_final
Line
Count
Source
139
84
                                     size_t *outl, size_t outsz) \
140
84
{ \
141
84
    struct blake##variant##_md_data_st *b_ctx; \
142
84
 \
143
84
    b_ctx = (struct blake##variant##_md_data_st *)ctx; \
144
84
 \
145
84
    if (!ossl_prov_is_running()) \
146
84
        return 0; \
147
84
 \
148
84
    *outl = b_ctx->ctx.outlen; \
149
84
 \
150
84
    if (outsz == 0) \
151
84
       return 1; \
152
84
 \
153
84
    if (outsz < *outl) { \
154
0
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_SIZE); \
155
0
        return 0; \
156
0
    } \
157
84
 \
158
84
    return ossl_blake##variant##_final(out, ctx); \
159
84
} \
blake2_prov.c:blake2b512_internal_final
Line
Count
Source
139
89
                                     size_t *outl, size_t outsz) \
140
89
{ \
141
89
    struct blake##variant##_md_data_st *b_ctx; \
142
89
 \
143
89
    b_ctx = (struct blake##variant##_md_data_st *)ctx; \
144
89
 \
145
89
    if (!ossl_prov_is_running()) \
146
89
        return 0; \
147
89
 \
148
89
    *outl = b_ctx->ctx.outlen; \
149
89
 \
150
89
    if (outsz == 0) \
151
89
       return 1; \
152
89
 \
153
89
    if (outsz < *outl) { \
154
0
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_SIZE); \
155
0
        return 0; \
156
0
    } \
157
89
 \
158
89
    return ossl_blake##variant##_final(out, ctx); \
159
89
} \
160
 \
161
8
static int blake##variantsize##_get_params(OSSL_PARAM params[]) \
162
8
{ \
163
8
    return ossl_digest_default_get_params(params, BLAKE##VARIANT##_BLOCKBYTES, BLAKE##VARIANT##_OUTBYTES, 0); \
164
8
} \
blake2_prov.c:blake2s256_get_params
Line
Count
Source
161
4
static int blake##variantsize##_get_params(OSSL_PARAM params[]) \
162
4
{ \
163
4
    return ossl_digest_default_get_params(params, BLAKE##VARIANT##_BLOCKBYTES, BLAKE##VARIANT##_OUTBYTES, 0); \
164
4
} \
blake2_prov.c:blake2b512_get_params
Line
Count
Source
161
4
static int blake##variantsize##_get_params(OSSL_PARAM params[]) \
162
4
{ \
163
4
    return ossl_digest_default_get_params(params, BLAKE##VARIANT##_BLOCKBYTES, BLAKE##VARIANT##_OUTBYTES, 0); \
164
4
} \
165
 \
166
const OSSL_DISPATCH ossl_blake##variantsize##_functions[] = { \
167
    {OSSL_FUNC_DIGEST_NEWCTX, (void (*)(void))blake##variantsize##_newctx}, \
168
    {OSSL_FUNC_DIGEST_UPDATE, (void (*)(void))ossl_blake##variant##_update}, \
169
    {OSSL_FUNC_DIGEST_FINAL, (void (*)(void))blake##variantsize##_internal_final}, \
170
    {OSSL_FUNC_DIGEST_FREECTX, (void (*)(void))blake##variantsize##_freectx}, \
171
    {OSSL_FUNC_DIGEST_DUPCTX, (void (*)(void))blake##variantsize##_dupctx}, \
172
    {OSSL_FUNC_DIGEST_GET_PARAMS, (void (*)(void))blake##variantsize##_get_params}, \
173
    {OSSL_FUNC_DIGEST_GETTABLE_PARAMS, \
174
     (void (*)(void))ossl_digest_default_gettable_params}, \
175
    {OSSL_FUNC_DIGEST_INIT, (void (*)(void))blake##variantsize##_internal_init}, \
176
    {OSSL_FUNC_DIGEST_GETTABLE_CTX_PARAMS, \
177
     (void (*)(void))ossl_blake##variant##_gettable_ctx_params}, \
178
    {OSSL_FUNC_DIGEST_SETTABLE_CTX_PARAMS, \
179
     (void (*)(void))ossl_blake##variant##_settable_ctx_params}, \
180
    {OSSL_FUNC_DIGEST_GET_CTX_PARAMS, \
181
     (void (*)(void))ossl_blake##variant##_get_ctx_params}, \
182
    {OSSL_FUNC_DIGEST_SET_CTX_PARAMS, \
183
     (void (*)(void))ossl_blake##variant##_set_ctx_params}, \
184
    {0, NULL} \
185
};
186
187
IMPLEMENT_BLAKE_functions(2s, 2S, 2s256)
188
IMPLEMENT_BLAKE_functions(2b, 2B, 2b512)