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