f80d307321199d7a45f5005b243ee8dd3deeb4eb
[openssl.git] / providers / implementations / digests / blake2_prov.c
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 static int ossl_blake2s256_init(void *ctx)
19 {
20     BLAKE2S_PARAM P;
21
22     ossl_blake2s_param_init(&P);
23     return ossl_blake2s_init((BLAKE2S_CTX *)ctx, &P);
24 }
25
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,
30                            ossl_blake2s_final)
31
32 /* ossl_blake2b512_functions */
33
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}, \
37     OSSL_PARAM_END \
38 }; \
39  \
40 const OSSL_PARAM *ossl_blake##variant##_gettable_ctx_params(ossl_unused void *ctx, \
41                                                    ossl_unused void *pctx) \
42 { \
43     return known_blake##variant##_ctx_params; \
44 } \
45  \
46 const OSSL_PARAM *ossl_blake##variant##_settable_ctx_params(ossl_unused void *ctx, \
47                                                    ossl_unused void *pctx) \
48 { \
49     return known_blake##variant##_ctx_params; \
50 } \
51  \
52 int ossl_blake##variant##_get_ctx_params(void *vctx, OSSL_PARAM params[]) \
53 { \
54     struct blake##variant##_md_data_st *mdctx = vctx; \
55     OSSL_PARAM *p; \
56  \
57     BLAKE##VARIANT##_CTX *ctx = &mdctx->ctx; \
58  \
59     if (ctx == NULL) \
60         return 0; \
61     if (params == NULL) \
62         return 1; \
63  \
64     p = OSSL_PARAM_locate(params, OSSL_DIGEST_PARAM_SIZE); \
65     if (p != NULL \
66         && !OSSL_PARAM_set_uint(p, (unsigned int)mdctx->params.digest_length)) { \
67         ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); \
68         return 0; \
69     } \
70  \
71     return 1; \
72 } \
73  \
74 int ossl_blake##variant##_set_ctx_params(void *vctx, const OSSL_PARAM params[]) \
75 { \
76     size_t size; \
77     struct blake##variant##_md_data_st *mdctx = vctx; \
78     const OSSL_PARAM *p; \
79  \
80     BLAKE##VARIANT##_CTX *ctx = &mdctx->ctx; \
81  \
82     if (ctx == NULL) \
83         return 0; \
84     if (params == NULL) \
85         return 1; \
86  \
87     p = OSSL_PARAM_locate_const(params, OSSL_DIGEST_PARAM_SIZE); \
88     if (p != NULL) { \
89         if (!OSSL_PARAM_get_size_t(p, &size)) { \
90             ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); \
91             return 0; \
92         } \
93         if (size < 1 || size > UINT8_MAX) { \
94             ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_SIZE); \
95             return 0; \
96         } \
97         ossl_blake##variant##_param_set_digest_length(&mdctx->params, (uint8_t)size); \
98     } \
99  \
100     return 1; \
101 } \
102  \
103 static int ossl_blake##variantsize##_init(void *ctx) \
104 { \
105     struct blake##variant##_md_data_st *mdctx = ctx; \
106     uint8_t digest_length = mdctx->params.digest_length; \
107  \
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); \
112 } \
113  \
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; \
120  \
121 static int blake##variantsize##_internal_init(void *ctx, const OSSL_PARAM params[]) \
122 { \
123     return ossl_prov_is_running() && ossl_blake##variant##_set_ctx_params(ctx, params) \
124         && ossl_blake##variantsize##_init(ctx); \
125 } \
126  \
127 static void *blake##variantsize##_newctx(void *prov_ctx) \
128 { \
129     struct blake##variant##_md_data_st *ctx; \
130  \
131     ctx = ossl_prov_is_running() ? OPENSSL_zalloc(sizeof(*ctx)) : NULL; \
132     return ctx; \
133 } \
134  \
135 static void blake##variantsize##_freectx(void *vctx) \
136 { \
137     struct blake##variant##_md_data_st *ctx; \
138  \
139     ctx = (struct blake##variant##_md_data_st *)vctx; \
140     OPENSSL_clear_free(ctx, sizeof(*ctx)); \
141 } \
142  \
143 static void *blake##variantsize##_dupctx(void *ctx) \
144 { \
145     struct blake##variant##_md_data_st *in, *ret; \
146  \
147     in = (struct blake##variant##_md_data_st *)ctx; \
148     ret = ossl_prov_is_running()? OPENSSL_malloc(sizeof(*ret)) : NULL; \
149     if (ret != NULL) \
150         *ret = *in; \
151     return ret; \
152 } \
153  \
154 static int blake##variantsize##_internal_final(void *ctx, unsigned char *out, \
155                                      size_t *outl, size_t outsz) \
156 { \
157     struct blake##variant##_md_data_st *b_ctx; \
158  \
159     b_ctx = (struct blake##variant##_md_data_st *)ctx; \
160  \
161     if (!ossl_prov_is_running()) \
162         return 0; \
163  \
164     *outl = b_ctx->ctx.outlen; \
165  \
166     if (outsz == 0) \
167        return 1; \
168  \
169     if (outsz < *outl) { \
170         ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_SIZE); \
171         return 0; \
172     } \
173  \
174     return ossl_blake##variant##_final(out, ctx); \
175 } \
176  \
177 static int blake##variantsize##_get_params(OSSL_PARAM params[]) \
178 { \
179     return ossl_digest_default_get_params(params, BLAKE##VARIANT##_BLOCKBYTES, 64, 0); \
180 } \
181  \
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}, \
200     {0, NULL} \
201 };
202
203 IMPLEMENT_BLAKE_functions(2b, 2B, 2b512)