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