1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
* Copyright 2019-2025 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
* in the file LICENSE in the source distribution or at
* https://www.openssl.org/source/license.html
*/
#include <string.h>
#include <openssl/crypto.h>
#include <openssl/core_names.h>
#include <openssl/proverr.h>
#include <openssl/err.h>
#include "internal/cryptlib.h"
#include "prov/blake2.h"
#include "prov/digestcommon.h"
#include "prov/implementations.h"
#include "providers/implementations/digests/blake2_prov.inc"
static OSSL_FUNC_digest_gettable_ctx_params_fn blake_gettable_ctx_params;
static OSSL_FUNC_digest_settable_ctx_params_fn blake_settable_ctx_params;
static const OSSL_PARAM *blake_gettable_ctx_params(ossl_unused void *ctx,
ossl_unused void *pctx)
{
return blake_get_ctx_params_list;
}
static const OSSL_PARAM *blake_settable_ctx_params(ossl_unused void *ctx,
ossl_unused void *pctx)
{
return blake_set_ctx_params_list;
}
#define IMPLEMENT_BLAKE_functions(variant, VARIANT, variantsize) \
int ossl_blake##variant##_get_ctx_params(void *vctx, OSSL_PARAM params[]) \
{ \
struct blake##variant##_md_data_st *mdctx = vctx; \
struct blake_get_ctx_params_st p; \
\
BLAKE##VARIANT##_CTX *ctx = &mdctx->ctx; \
\
if (ctx == NULL || !blake_get_ctx_params_decoder(params, &p)) \
return 0; \
\
if (p.size != NULL \
&& !OSSL_PARAM_set_uint(p.size, (unsigned int)mdctx->params.digest_length)) { \
ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); \
return 0; \
} \
\
return 1; \
} \
\
int ossl_blake##variant##_set_ctx_params(void *vctx, const OSSL_PARAM params[]) \
{ \
unsigned int size; \
struct blake##variant##_md_data_st *mdctx = vctx; \
struct blake_set_ctx_params_st p; \
\
BLAKE##VARIANT##_CTX *ctx = &mdctx->ctx; \
\
if (ctx == NULL || !blake_set_ctx_params_decoder(params, &p)) \
return 0; \
\
if (p.size != NULL) { \
if (!OSSL_PARAM_get_uint(p.size, &size)) { \
ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); \
return 0; \
} \
if (size < 1 || size > BLAKE##VARIANT##_OUTBYTES) { \
ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_SIZE); \
return 0; \
} \
ossl_blake##variant##_param_set_digest_length(&mdctx->params, (uint8_t)size); \
} \
\
return 1; \
} \
\
static int ossl_blake##variantsize##_init(void *ctx) \
{ \
struct blake##variant##_md_data_st *mdctx = ctx; \
uint8_t digest_length = mdctx->params.digest_length; \
\
ossl_blake##variant##_param_init(&mdctx->params); \
if (digest_length != 0) \
mdctx->params.digest_length = digest_length; \
return ossl_blake##variant##_init(&mdctx->ctx, &mdctx->params); \
} \
\
static OSSL_FUNC_digest_init_fn blake##variantsize##_internal_init; \
static OSSL_FUNC_digest_newctx_fn blake##variantsize##_newctx; \
static OSSL_FUNC_digest_freectx_fn blake##variantsize##_freectx; \
static OSSL_FUNC_digest_dupctx_fn blake##variantsize##_dupctx; \
static OSSL_FUNC_digest_final_fn blake##variantsize##_internal_final; \
static OSSL_FUNC_digest_get_params_fn blake##variantsize##_get_params; \
\
static int blake##variantsize##_internal_init(void *ctx, const OSSL_PARAM params[]) \
{ \
return ossl_prov_is_running() && ossl_blake##variant##_set_ctx_params(ctx, params) \
&& ossl_blake##variantsize##_init(ctx); \
} \
\
static void *blake##variantsize##_newctx(void *prov_ctx) \
{ \
struct blake##variant##_md_data_st *ctx; \
\
ctx = ossl_prov_is_running() ? OPENSSL_zalloc(sizeof(*ctx)) : NULL; \
return ctx; \
} \
\
static void blake##variantsize##_freectx(void *vctx) \
{ \
struct blake##variant##_md_data_st *ctx; \
\
ctx = (struct blake##variant##_md_data_st *)vctx; \
OPENSSL_clear_free(ctx, sizeof(*ctx)); \
} \
\
static void *blake##variantsize##_dupctx(void *ctx) \
{ \
struct blake##variant##_md_data_st *in, *ret; \
\
in = (struct blake##variant##_md_data_st *)ctx; \
ret = ossl_prov_is_running() ? OPENSSL_malloc(sizeof(*ret)) : NULL; \
if (ret != NULL) \
*ret = *in; \
return ret; \
} \
\
static void blake##variantsize##_copyctx(void *voutctx, void *vinctx) \
{ \
struct blake##variant##_md_data_st *inctx, *outctx; \
\
outctx = (struct blake##variant##_md_data_st *)voutctx; \
inctx = (struct blake##variant##_md_data_st *)vinctx; \
*outctx = *inctx; \
} \
\
static int blake##variantsize##_internal_final(void *ctx, unsigned char *out, \
size_t *outl, size_t outsz) \
{ \
struct blake##variant##_md_data_st *b_ctx; \
\
b_ctx = (struct blake##variant##_md_data_st *)ctx; \
\
if (!ossl_prov_is_running()) \
return 0; \
\
*outl = b_ctx->ctx.outlen; \
\
if (outsz == 0) \
return 1; \
\
if (outsz < *outl) { \
ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_SIZE); \
return 0; \
} \
\
return ossl_blake##variant##_final(out, ctx); \
} \
\
static int blake##variantsize##_get_params(OSSL_PARAM params[]) \
{ \
return ossl_digest_default_get_params(params, BLAKE##VARIANT##_BLOCKBYTES, BLAKE##VARIANT##_OUTBYTES, 0); \
} \
\
const OSSL_DISPATCH ossl_blake##variantsize##_functions[] = { \
{ OSSL_FUNC_DIGEST_NEWCTX, (void (*)(void))blake##variantsize##_newctx }, \
{ OSSL_FUNC_DIGEST_UPDATE, (void (*)(void))ossl_blake##variant##_update }, \
{ OSSL_FUNC_DIGEST_FINAL, (void (*)(void))blake##variantsize##_internal_final }, \
{ OSSL_FUNC_DIGEST_FREECTX, (void (*)(void))blake##variantsize##_freectx }, \
{ OSSL_FUNC_DIGEST_DUPCTX, (void (*)(void))blake##variantsize##_dupctx }, \
{ OSSL_FUNC_DIGEST_COPYCTX, (void (*)(void))blake##variantsize##_copyctx }, \
{ OSSL_FUNC_DIGEST_GET_PARAMS, (void (*)(void))blake##variantsize##_get_params }, \
{ OSSL_FUNC_DIGEST_GETTABLE_PARAMS, \
(void (*)(void))ossl_digest_default_gettable_params }, \
{ OSSL_FUNC_DIGEST_INIT, (void (*)(void))blake##variantsize##_internal_init }, \
{ OSSL_FUNC_DIGEST_GETTABLE_CTX_PARAMS, \
(void (*)(void))blake_gettable_ctx_params }, \
{ OSSL_FUNC_DIGEST_SETTABLE_CTX_PARAMS, \
(void (*)(void))blake_settable_ctx_params }, \
{ OSSL_FUNC_DIGEST_GET_CTX_PARAMS, \
(void (*)(void))ossl_blake##variant##_get_ctx_params }, \
{ OSSL_FUNC_DIGEST_SET_CTX_PARAMS, \
(void (*)(void))ossl_blake##variant##_set_ctx_params }, \
{ 0, NULL } \
};
IMPLEMENT_BLAKE_functions(2s, 2S, 2s256)
IMPLEMENT_BLAKE_functions(2b, 2B, 2b512)