2 * Copyright 2019-2023 The OpenSSL Project Authors. All Rights Reserved.
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
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"
18 static int ossl_blake2s256_init(void *ctx)
22 ossl_blake2s_param_init(&P);
23 return ossl_blake2s_init((BLAKE2S_CTX *)ctx, &P);
26 /* ossl_blake2s256_functions */
27 IMPLEMENT_digest_functions(blake2s256, BLAKE2S_CTX,
28 BLAKE2S_BLOCKBYTES, BLAKE2S_DIGEST_LENGTH, 0,
29 ossl_blake2s256_init, ossl_blake2s_update,
32 /* ossl_blake2b512_functions */
34 #define IMPLEMENT_BLAKE_functions(variant, VARIANT, variantsize) \
35 static const OSSL_PARAM known_blake##variant##_ctx_params[] = { \
36 {OSSL_DIGEST_PARAM_SIZE, OSSL_PARAM_UNSIGNED_INTEGER, NULL, 0, 0}, \
40 const OSSL_PARAM *ossl_blake##variant##_gettable_ctx_params(ossl_unused void *ctx, \
41 ossl_unused void *pctx) \
43 return known_blake##variant##_ctx_params; \
46 const OSSL_PARAM *ossl_blake##variant##_settable_ctx_params(ossl_unused void *ctx, \
47 ossl_unused void *pctx) \
49 return known_blake##variant##_ctx_params; \
52 int ossl_blake##variant##_get_ctx_params(void *vctx, OSSL_PARAM params[]) \
54 struct blake##variant##_md_data_st *mdctx = vctx; \
57 BLAKE##VARIANT##_CTX *ctx = &mdctx->ctx; \
64 p = OSSL_PARAM_locate(params, OSSL_DIGEST_PARAM_SIZE); \
66 && !OSSL_PARAM_set_uint(p, (unsigned int)mdctx->params.digest_length)) { \
67 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); \
74 int ossl_blake##variant##_set_ctx_params(void *vctx, const OSSL_PARAM params[]) \
77 struct blake##variant##_md_data_st *mdctx = vctx; \
78 const OSSL_PARAM *p; \
80 BLAKE##VARIANT##_CTX *ctx = &mdctx->ctx; \
87 p = OSSL_PARAM_locate_const(params, OSSL_DIGEST_PARAM_SIZE); \
89 if (!OSSL_PARAM_get_size_t(p, &size)) { \
90 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); \
93 if (size < 1 || size > UINT8_MAX) { \
94 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_SIZE); \
97 ossl_blake##variant##_param_set_digest_length(&mdctx->params, (uint8_t)size); \
103 static int ossl_blake##variantsize##_init(void *ctx) \
105 struct blake##variant##_md_data_st *mdctx = ctx; \
106 uint8_t digest_length = mdctx->params.digest_length; \
108 ossl_blake##variant##_param_init(&mdctx->params); \
109 if (digest_length != 0) \
110 mdctx->params.digest_length = digest_length; \
111 return ossl_blake##variant##_init(&mdctx->ctx, &mdctx->params); \
114 static OSSL_FUNC_digest_init_fn blake##variantsize##_internal_init; \
115 static OSSL_FUNC_digest_newctx_fn blake##variantsize##_newctx; \
116 static OSSL_FUNC_digest_freectx_fn blake##variantsize##_freectx; \
117 static OSSL_FUNC_digest_dupctx_fn blake##variantsize##_dupctx; \
118 static OSSL_FUNC_digest_final_fn blake##variantsize##_internal_final; \
119 static OSSL_FUNC_digest_get_params_fn blake##variantsize##_get_params; \
121 static int blake##variantsize##_internal_init(void *ctx, const OSSL_PARAM params[]) \
123 return ossl_prov_is_running() && ossl_blake##variant##_set_ctx_params(ctx, params) \
124 && ossl_blake##variantsize##_init(ctx); \
127 static void *blake##variantsize##_newctx(void *prov_ctx) \
129 struct blake##variant##_md_data_st *ctx; \
131 ctx = ossl_prov_is_running() ? OPENSSL_zalloc(sizeof(*ctx)) : NULL; \
135 static void blake##variantsize##_freectx(void *vctx) \
137 struct blake##variant##_md_data_st *ctx; \
139 ctx = (struct blake##variant##_md_data_st *)vctx; \
140 OPENSSL_clear_free(ctx, sizeof(*ctx)); \
143 static void *blake##variantsize##_dupctx(void *ctx) \
145 struct blake##variant##_md_data_st *in, *ret; \
147 in = (struct blake##variant##_md_data_st *)ctx; \
148 ret = ossl_prov_is_running()? OPENSSL_malloc(sizeof(*ret)) : NULL; \
154 static int blake##variantsize##_internal_final(void *ctx, unsigned char *out, \
155 size_t *outl, size_t outsz) \
157 struct blake##variant##_md_data_st *b_ctx; \
159 b_ctx = (struct blake##variant##_md_data_st *)ctx; \
161 if (!ossl_prov_is_running()) \
164 *outl = b_ctx->ctx.outlen; \
169 if (outsz < *outl) { \
170 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_SIZE); \
174 return ossl_blake##variant##_final(out, ctx); \
177 static int blake##variantsize##_get_params(OSSL_PARAM params[]) \
179 return ossl_digest_default_get_params(params, BLAKE##VARIANT##_BLOCKBYTES, 64, 0); \
182 const OSSL_DISPATCH ossl_blake##variantsize##_functions[] = { \
183 {OSSL_FUNC_DIGEST_NEWCTX, (void (*)(void))blake##variantsize##_newctx}, \
184 {OSSL_FUNC_DIGEST_UPDATE, (void (*)(void))ossl_blake##variant##_update}, \
185 {OSSL_FUNC_DIGEST_FINAL, (void (*)(void))blake##variantsize##_internal_final}, \
186 {OSSL_FUNC_DIGEST_FREECTX, (void (*)(void))blake##variantsize##_freectx}, \
187 {OSSL_FUNC_DIGEST_DUPCTX, (void (*)(void))blake##variantsize##_dupctx}, \
188 {OSSL_FUNC_DIGEST_GET_PARAMS, (void (*)(void))blake##variantsize##_get_params}, \
189 {OSSL_FUNC_DIGEST_GETTABLE_PARAMS, \
190 (void (*)(void))ossl_digest_default_gettable_params}, \
191 {OSSL_FUNC_DIGEST_INIT, (void (*)(void))blake##variantsize##_internal_init}, \
192 {OSSL_FUNC_DIGEST_GETTABLE_CTX_PARAMS, \
193 (void (*)(void))ossl_blake##variant##_gettable_ctx_params}, \
194 {OSSL_FUNC_DIGEST_SETTABLE_CTX_PARAMS, \
195 (void (*)(void))ossl_blake##variant##_settable_ctx_params}, \
196 {OSSL_FUNC_DIGEST_GET_CTX_PARAMS, \
197 (void (*)(void))ossl_blake##variant##_get_ctx_params}, \
198 {OSSL_FUNC_DIGEST_SET_CTX_PARAMS, \
199 (void (*)(void))ossl_blake##variant##_set_ctx_params}, \
203 IMPLEMENT_BLAKE_functions(2b, 2B, 2b512)