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