/src/openssl35/providers/implementations/digests/blake2_prov.c
Line | Count | Source |
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 | | ossl_unused void *pctx) \ |
26 | 102k | { \ |
27 | 102k | return known_blake##variant##_ctx_params; \ |
28 | 102k | } \ ossl_blake2s_gettable_ctx_params Line | Count | Source | 26 | 1.62k | { \ | 27 | 1.62k | return known_blake##variant##_ctx_params; \ | 28 | 1.62k | } \ |
ossl_blake2b_gettable_ctx_params Line | Count | Source | 26 | 101k | { \ | 27 | 101k | return known_blake##variant##_ctx_params; \ | 28 | 101k | } \ |
|
29 | | \ |
30 | | const OSSL_PARAM *ossl_blake##variant##_settable_ctx_params(ossl_unused void *ctx, \ |
31 | | ossl_unused void *pctx) \ |
32 | 19 | { \ |
33 | 19 | return known_blake##variant##_ctx_params; \ |
34 | 19 | } \ ossl_blake2s_settable_ctx_params Line | Count | Source | 32 | 17 | { \ | 33 | 17 | return known_blake##variant##_ctx_params; \ | 34 | 17 | } \ |
ossl_blake2b_settable_ctx_params Line | Count | Source | 32 | 2 | { \ | 33 | 2 | return known_blake##variant##_ctx_params; \ | 34 | 2 | } \ |
|
35 | | \ |
36 | | int ossl_blake##variant##_get_ctx_params(void *vctx, OSSL_PARAM params[]) \ |
37 | 58.9k | { \ |
38 | 58.9k | struct blake##variant##_md_data_st *mdctx = vctx; \ |
39 | 58.9k | OSSL_PARAM *p; \ |
40 | 58.9k | \ |
41 | 58.9k | BLAKE##VARIANT##_CTX *ctx = &mdctx->ctx; \ |
42 | 58.9k | \ |
43 | 58.9k | if (ctx == NULL) \ |
44 | 58.9k | return 0; \ |
45 | 58.9k | if (ossl_param_is_empty(params)) \ |
46 | 58.9k | return 1; \ |
47 | 58.9k | \ |
48 | 58.9k | p = OSSL_PARAM_locate(params, OSSL_DIGEST_PARAM_SIZE); \ |
49 | 58.9k | if (p != NULL \ |
50 | 58.9k | && !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 | 58.9k | \ |
55 | 58.9k | return 1; \ |
56 | 58.9k | } \ |
57 | | \ |
58 | | int ossl_blake##variant##_set_ctx_params(void *vctx, const OSSL_PARAM params[]) \ |
59 | 58.9k | { \ |
60 | 58.9k | size_t size; \ |
61 | 58.9k | struct blake##variant##_md_data_st *mdctx = vctx; \ |
62 | 58.9k | const OSSL_PARAM *p; \ |
63 | 58.9k | \ |
64 | 58.9k | BLAKE##VARIANT##_CTX *ctx = &mdctx->ctx; \ |
65 | 58.9k | \ |
66 | 58.9k | if (ctx == NULL) \ |
67 | 58.9k | return 0; \ |
68 | 58.9k | if (ossl_param_is_empty(params)) \ |
69 | 58.9k | return 1; \ |
70 | 58.9k | \ |
71 | 58.9k | p = OSSL_PARAM_locate_const(params, OSSL_DIGEST_PARAM_SIZE); \ |
72 | 57.9k | if (p != NULL) { \ |
73 | 57.9k | 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 | 57.9k | 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 | 57.9k | ossl_blake##variant##_param_set_digest_length(&mdctx->params, (uint8_t)size); \ |
82 | 57.9k | } \ |
83 | 57.9k | \ |
84 | 57.9k | return 1; \ |
85 | 57.9k | } \ |
86 | | \ |
87 | | static int ossl_blake##variantsize##_init(void *ctx) \ |
88 | 158k | { \ |
89 | 158k | struct blake##variant##_md_data_st *mdctx = ctx; \ |
90 | 158k | uint8_t digest_length = mdctx->params.digest_length; \ |
91 | 158k | \ |
92 | 158k | ossl_blake##variant##_param_init(&mdctx->params); \ |
93 | 158k | if (digest_length != 0) \ |
94 | 158k | mdctx->params.digest_length = digest_length; \ |
95 | 158k | return ossl_blake##variant##_init(&mdctx->ctx, &mdctx->params); \ |
96 | 158k | } \ blake2_prov.c:ossl_blake2s256_init Line | Count | Source | 88 | 5.21k | { \ | 89 | 5.21k | struct blake##variant##_md_data_st *mdctx = ctx; \ | 90 | 5.21k | uint8_t digest_length = mdctx->params.digest_length; \ | 91 | 5.21k | \ | 92 | 5.21k | ossl_blake##variant##_param_init(&mdctx->params); \ | 93 | 5.21k | if (digest_length != 0) \ | 94 | 5.21k | mdctx->params.digest_length = digest_length; \ | 95 | 5.21k | return ossl_blake##variant##_init(&mdctx->ctx, &mdctx->params); \ | 96 | 5.21k | } \ |
blake2_prov.c:ossl_blake2b512_init Line | Count | Source | 88 | 153k | { \ | 89 | 153k | struct blake##variant##_md_data_st *mdctx = ctx; \ | 90 | 153k | uint8_t digest_length = mdctx->params.digest_length; \ | 91 | 153k | \ | 92 | 153k | ossl_blake##variant##_param_init(&mdctx->params); \ | 93 | 153k | if (digest_length != 0) \ | 94 | 153k | mdctx->params.digest_length = digest_length; \ | 95 | 153k | return ossl_blake##variant##_init(&mdctx->ctx, &mdctx->params); \ | 96 | 153k | } \ |
|
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 | | static int blake##variantsize##_internal_init(void *ctx, const OSSL_PARAM params[]) \ |
106 | 158k | { \ |
107 | 158k | return ossl_prov_is_running() && ossl_blake##variant##_set_ctx_params(ctx, params) \ |
108 | 158k | && ossl_blake##variantsize##_init(ctx); \ |
109 | 158k | } \ blake2_prov.c:blake2s256_internal_init Line | Count | Source | 106 | 5.21k | { \ | 107 | 5.21k | return ossl_prov_is_running() && ossl_blake##variant##_set_ctx_params(ctx, params) \ | 108 | 5.21k | && ossl_blake##variantsize##_init(ctx); \ | 109 | 5.21k | } \ |
blake2_prov.c:blake2b512_internal_init Line | Count | Source | 106 | 153k | { \ | 107 | 153k | return ossl_prov_is_running() && ossl_blake##variant##_set_ctx_params(ctx, params) \ | 108 | 153k | && ossl_blake##variantsize##_init(ctx); \ | 109 | 153k | } \ |
|
110 | | \ |
111 | | static void *blake##variantsize##_newctx(void *prov_ctx) \ |
112 | 154k | { \ |
113 | 154k | struct blake##variant##_md_data_st *ctx; \ |
114 | 154k | \ |
115 | 154k | ctx = ossl_prov_is_running() ? OPENSSL_zalloc(sizeof(*ctx)) : NULL; \ |
116 | 154k | return ctx; \ |
117 | 154k | } \ |
118 | | \ |
119 | | static void blake##variantsize##_freectx(void *vctx) \ |
120 | 156k | { \ |
121 | 156k | struct blake##variant##_md_data_st *ctx; \ |
122 | 156k | \ |
123 | 156k | ctx = (struct blake##variant##_md_data_st *)vctx; \ |
124 | 156k | OPENSSL_clear_free(ctx, sizeof(*ctx)); \ |
125 | 156k | } \ blake2_prov.c:blake2s256_freectx Line | Count | Source | 120 | 2.44k | { \ | 121 | 2.44k | struct blake##variant##_md_data_st *ctx; \ | 122 | 2.44k | \ | 123 | 2.44k | ctx = (struct blake##variant##_md_data_st *)vctx; \ | 124 | 2.44k | OPENSSL_clear_free(ctx, sizeof(*ctx)); \ | 125 | 2.44k | } \ |
blake2_prov.c:blake2b512_freectx Line | Count | Source | 120 | 153k | { \ | 121 | 153k | struct blake##variant##_md_data_st *ctx; \ | 122 | 153k | \ | 123 | 153k | ctx = (struct blake##variant##_md_data_st *)vctx; \ | 124 | 153k | OPENSSL_clear_free(ctx, sizeof(*ctx)); \ | 125 | 153k | } \ |
|
126 | | \ |
127 | | static void *blake##variantsize##_dupctx(void *ctx) \ |
128 | 1.68k | { \ |
129 | 1.68k | struct blake##variant##_md_data_st *in, *ret; \ |
130 | 1.68k | \ |
131 | 1.68k | in = (struct blake##variant##_md_data_st *)ctx; \ |
132 | 1.68k | ret = ossl_prov_is_running() ? OPENSSL_malloc(sizeof(*ret)) : NULL; \ |
133 | 1.68k | if (ret != NULL) \ |
134 | 1.68k | *ret = *in; \ |
135 | 1.68k | return ret; \ |
136 | 1.68k | } \ |
137 | | \ |
138 | | static void blake##variantsize##_copyctx(void *voutctx, void *vinctx) \ |
139 | 3.61k | { \ |
140 | 3.61k | struct blake##variant##_md_data_st *inctx, *outctx; \ |
141 | 3.61k | \ |
142 | 3.61k | outctx = (struct blake##variant##_md_data_st *)voutctx; \ |
143 | 3.61k | inctx = (struct blake##variant##_md_data_st *)vinctx; \ |
144 | 3.61k | *outctx = *inctx; \ |
145 | 3.61k | } \ blake2_prov.c:blake2s256_copyctx Line | Count | Source | 139 | 3.18k | { \ | 140 | 3.18k | struct blake##variant##_md_data_st *inctx, *outctx; \ | 141 | 3.18k | \ | 142 | 3.18k | outctx = (struct blake##variant##_md_data_st *)voutctx; \ | 143 | 3.18k | inctx = (struct blake##variant##_md_data_st *)vinctx; \ | 144 | 3.18k | *outctx = *inctx; \ | 145 | 3.18k | } \ |
blake2_prov.c:blake2b512_copyctx Line | Count | Source | 139 | 421 | { \ | 140 | 421 | struct blake##variant##_md_data_st *inctx, *outctx; \ | 141 | 421 | \ | 142 | 421 | outctx = (struct blake##variant##_md_data_st *)voutctx; \ | 143 | 421 | inctx = (struct blake##variant##_md_data_st *)vinctx; \ | 144 | 421 | *outctx = *inctx; \ | 145 | 421 | } \ |
|
146 | | \ |
147 | | static int blake##variantsize##_internal_final(void *ctx, unsigned char *out, \ |
148 | | size_t *outl, size_t outsz) \ |
149 | 158k | { \ |
150 | 158k | struct blake##variant##_md_data_st *b_ctx; \ |
151 | 158k | \ |
152 | 158k | b_ctx = (struct blake##variant##_md_data_st *)ctx; \ |
153 | 158k | \ |
154 | 158k | if (!ossl_prov_is_running()) \ |
155 | 158k | return 0; \ |
156 | 158k | \ |
157 | 158k | *outl = b_ctx->ctx.outlen; \ |
158 | 158k | \ |
159 | 158k | if (outsz == 0) \ |
160 | 158k | return 1; \ |
161 | 158k | \ |
162 | 158k | if (outsz < *outl) { \ |
163 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_SIZE); \ |
164 | 0 | return 0; \ |
165 | 0 | } \ |
166 | 158k | \ |
167 | 158k | return ossl_blake##variant##_final(out, ctx); \ |
168 | 158k | } \ |
169 | | \ |
170 | | static int blake##variantsize##_get_params(OSSL_PARAM params[]) \ |
171 | 182 | { \ |
172 | 182 | return ossl_digest_default_get_params(params, BLAKE##VARIANT##_BLOCKBYTES, BLAKE##VARIANT##_OUTBYTES, 0); \ |
173 | 182 | } \ blake2_prov.c:blake2s256_get_params Line | Count | Source | 171 | 91 | { \ | 172 | 91 | return ossl_digest_default_get_params(params, BLAKE##VARIANT##_BLOCKBYTES, BLAKE##VARIANT##_OUTBYTES, 0); \ | 173 | 91 | } \ |
blake2_prov.c:blake2b512_get_params Line | Count | Source | 171 | 91 | { \ | 172 | 91 | return ossl_digest_default_get_params(params, BLAKE##VARIANT##_BLOCKBYTES, BLAKE##VARIANT##_OUTBYTES, 0); \ | 173 | 91 | } \ |
|
174 | | \ |
175 | | const OSSL_DISPATCH ossl_blake##variantsize##_functions[] = { \ |
176 | | { OSSL_FUNC_DIGEST_NEWCTX, (void (*)(void))blake##variantsize##_newctx }, \ |
177 | | { OSSL_FUNC_DIGEST_UPDATE, (void (*)(void))ossl_blake##variant##_update }, \ |
178 | | { OSSL_FUNC_DIGEST_FINAL, (void (*)(void))blake##variantsize##_internal_final }, \ |
179 | | { OSSL_FUNC_DIGEST_FREECTX, (void (*)(void))blake##variantsize##_freectx }, \ |
180 | | { OSSL_FUNC_DIGEST_DUPCTX, (void (*)(void))blake##variantsize##_dupctx }, \ |
181 | | { OSSL_FUNC_DIGEST_COPYCTX, (void (*)(void))blake##variantsize##_copyctx }, \ |
182 | | { OSSL_FUNC_DIGEST_GET_PARAMS, (void (*)(void))blake##variantsize##_get_params }, \ |
183 | | { OSSL_FUNC_DIGEST_GETTABLE_PARAMS, \ |
184 | | (void (*)(void))ossl_digest_default_gettable_params }, \ |
185 | | { OSSL_FUNC_DIGEST_INIT, (void (*)(void))blake##variantsize##_internal_init }, \ |
186 | | { OSSL_FUNC_DIGEST_GETTABLE_CTX_PARAMS, \ |
187 | | (void (*)(void))ossl_blake##variant##_gettable_ctx_params }, \ |
188 | | { OSSL_FUNC_DIGEST_SETTABLE_CTX_PARAMS, \ |
189 | | (void (*)(void))ossl_blake##variant##_settable_ctx_params }, \ |
190 | | { OSSL_FUNC_DIGEST_GET_CTX_PARAMS, \ |
191 | | (void (*)(void))ossl_blake##variant##_get_ctx_params }, \ |
192 | | { OSSL_FUNC_DIGEST_SET_CTX_PARAMS, \ |
193 | | (void (*)(void))ossl_blake##variant##_set_ctx_params }, \ |
194 | | { 0, NULL } \ |
195 | | }; |
196 | | |
197 | 8.78k | IMPLEMENT_BLAKE_functions(2s, 2S, 2s256) ossl_blake2s_get_ctx_params Line | Count | Source | 197 | | IMPLEMENT_BLAKE_functions(2s, 2S, 2s256) |
ossl_blake2s_set_ctx_params Line | Count | Source | 197 | | IMPLEMENT_BLAKE_functions(2s, 2S, 2s256) |
Unexecuted instantiation: blake2_prov.c:blake2s256_newctx Unexecuted instantiation: blake2_prov.c:blake2s256_internal_final Unexecuted instantiation: blake2_prov.c:blake2s256_dupctx |
198 | | IMPLEMENT_BLAKE_functions(2b, 2B, 2b512) ossl_blake2b_get_ctx_params Line | Count | Source | 198 | | IMPLEMENT_BLAKE_functions(2b, 2B, 2b512) |
ossl_blake2b_set_ctx_params Line | Count | Source | 198 | | IMPLEMENT_BLAKE_functions(2b, 2B, 2b512) |
Unexecuted instantiation: blake2_prov.c:blake2b512_newctx Unexecuted instantiation: blake2_prov.c:blake2b512_internal_final Unexecuted instantiation: blake2_prov.c:blake2b512_dupctx |