/src/openssl/providers/implementations/digests/turboshake_prov.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2026 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 <string.h> |
11 | | #include <openssl/core_dispatch.h> |
12 | | #include <openssl/core_names.h> |
13 | | #include <openssl/crypto.h> |
14 | | #include <openssl/err.h> |
15 | | #include <openssl/params.h> |
16 | | #include <openssl/proverr.h> |
17 | | #include "internal/common.h" |
18 | | #include "internal/sha3.h" |
19 | | #include "prov/digestcommon.h" |
20 | | #include "prov/implementations.h" |
21 | | #include "turboshake_prov.h" |
22 | | |
23 | | #define TURBOSHAKE_FLAGS (PROV_DIGEST_FLAG_XOF | PROV_DIGEST_FLAG_ALGID_ABSENT) |
24 | | |
25 | 0 | #define TURBOSHAKE_DOMAIN_DEFAULT 0x1f |
26 | 0 | #define TURBOSHAKE_DOMAIN_MIN 0x01 |
27 | 0 | #define TURBOSHAKE_DOMAIN_MAX 0x7f |
28 | | |
29 | | #include "providers/implementations/digests/turboshake_prov.inc" |
30 | | |
31 | | typedef struct turboshake_ctx_st { |
32 | | KECCAK1600_CTX kctx; |
33 | | size_t bitlen; |
34 | | } TURBOSHAKE_CTX; |
35 | | |
36 | | static OSSL_FUNC_digest_init_fn turboshake_init; |
37 | | static OSSL_FUNC_digest_update_fn turboshake_update; |
38 | | static OSSL_FUNC_digest_final_fn turboshake_final; |
39 | | static OSSL_FUNC_digest_squeeze_fn turboshake_squeeze; |
40 | | static OSSL_FUNC_digest_freectx_fn turboshake_freectx; |
41 | | static OSSL_FUNC_digest_dupctx_fn turboshake_dupctx; |
42 | | static OSSL_FUNC_digest_copyctx_fn turboshake_copyctx; |
43 | | static OSSL_FUNC_digest_get_ctx_params_fn turboshake_get_ctx_params; |
44 | | static OSSL_FUNC_digest_gettable_ctx_params_fn turboshake_gettable_ctx_params; |
45 | | static OSSL_FUNC_digest_set_ctx_params_fn turboshake_set_ctx_params; |
46 | | static OSSL_FUNC_digest_settable_ctx_params_fn turboshake_settable_ctx_params; |
47 | | |
48 | | static size_t turboshake_absorb_p12(KECCAK1600_CTX *ctx, |
49 | | const unsigned char *inp, size_t len) |
50 | 0 | { |
51 | 0 | return ossl_keccak1600_p12_absorb(ctx->A, inp, len, ctx->block_size); |
52 | 0 | } |
53 | | |
54 | | static int turboshake_final_p12(KECCAK1600_CTX *ctx, unsigned char *out, |
55 | | size_t outlen) |
56 | 0 | { |
57 | 0 | size_t bsz = ctx->block_size; |
58 | 0 | size_t num = ctx->bufsz; |
59 | |
|
60 | 0 | memset(ctx->buf + num, 0, bsz - num); |
61 | 0 | ctx->buf[num] = ctx->pad; |
62 | 0 | ctx->buf[bsz - 1] |= 0x80; |
63 | |
|
64 | 0 | (void)ossl_keccak1600_p12_absorb(ctx->A, ctx->buf, bsz, bsz); |
65 | 0 | ossl_keccak1600_p12_squeeze(ctx->A, out, outlen, bsz, 0); |
66 | 0 | return 1; |
67 | 0 | } |
68 | | |
69 | | static int turboshake_squeeze_p12(KECCAK1600_CTX *ctx, unsigned char *out, |
70 | | size_t outlen) |
71 | 0 | { |
72 | 0 | return ossl_keccak_block_squeeze(ctx, out, outlen, |
73 | 0 | ossl_keccak1600_p12_absorb, ossl_keccak1600_p12_squeeze); |
74 | 0 | } |
75 | | |
76 | | static PROV_SHA3_METHOD turboshake_p12_md = { |
77 | | turboshake_absorb_p12, |
78 | | turboshake_final_p12, |
79 | | turboshake_squeeze_p12 |
80 | | }; |
81 | | |
82 | | int ossl_turboshake_init_keccak(KECCAK1600_CTX *ctx, size_t bitlen, |
83 | | unsigned int domain, size_t xoflen) |
84 | 0 | { |
85 | 0 | if (domain < TURBOSHAKE_DOMAIN_MIN || domain > TURBOSHAKE_DOMAIN_MAX) |
86 | 0 | return 0; |
87 | 0 | if (!ossl_sha3_init(ctx, (unsigned char)domain, bitlen)) |
88 | 0 | return 0; |
89 | 0 | memset(ctx->A, 0, sizeof(ctx->A)); |
90 | 0 | ctx->md_size = xoflen; |
91 | 0 | ctx->meth = turboshake_p12_md; |
92 | 0 | return 1; |
93 | 0 | } |
94 | | |
95 | | static void *turboshake_newctx(ossl_unused void *provctx, size_t bitlen) |
96 | 0 | { |
97 | 0 | TURBOSHAKE_CTX *ctx; |
98 | |
|
99 | 0 | if (ossl_unlikely(!ossl_prov_is_running())) |
100 | 0 | return NULL; |
101 | 0 | ctx = OPENSSL_zalloc(sizeof(*ctx)); |
102 | 0 | if (ctx == NULL) |
103 | 0 | return NULL; |
104 | 0 | ctx->bitlen = bitlen; |
105 | 0 | if (!ossl_turboshake_init_keccak(&ctx->kctx, bitlen, |
106 | 0 | TURBOSHAKE_DOMAIN_DEFAULT, bitlen == 128 ? 32 : 64)) { |
107 | 0 | OPENSSL_clear_free(ctx, sizeof(*ctx)); |
108 | 0 | return NULL; |
109 | 0 | } |
110 | 0 | return ctx; |
111 | 0 | } |
112 | | |
113 | | static int turboshake_init(void *vctx, const OSSL_PARAM params[]) |
114 | 0 | { |
115 | 0 | TURBOSHAKE_CTX *ctx = vctx; |
116 | |
|
117 | 0 | if (ossl_unlikely(!ossl_prov_is_running())) |
118 | 0 | return 0; |
119 | 0 | return ossl_turboshake_init_keccak(&ctx->kctx, ctx->bitlen, |
120 | 0 | TURBOSHAKE_DOMAIN_DEFAULT, ctx->bitlen == 128 ? 32 : 64) |
121 | 0 | && turboshake_set_ctx_params(vctx, params); |
122 | 0 | } |
123 | | |
124 | | static int turboshake_update(void *vctx, const unsigned char *inp, size_t len) |
125 | 0 | { |
126 | 0 | TURBOSHAKE_CTX *ctx = vctx; |
127 | |
|
128 | 0 | return ossl_sha3_absorb(&ctx->kctx, inp, len); |
129 | 0 | } |
130 | | |
131 | | static int turboshake_final(void *vctx, unsigned char *out, size_t *outl, |
132 | | size_t outsz) |
133 | 0 | { |
134 | 0 | TURBOSHAKE_CTX *ctx = vctx; |
135 | 0 | size_t xoflen = ctx->kctx.md_size; |
136 | |
|
137 | 0 | if (ossl_unlikely(!ossl_prov_is_running())) |
138 | 0 | return 0; |
139 | 0 | if (outsz < xoflen) |
140 | 0 | return 0; |
141 | 0 | if (xoflen > 0 && !ossl_sha3_final(&ctx->kctx, out, xoflen)) |
142 | 0 | return 0; |
143 | 0 | if (outl != NULL) |
144 | 0 | *outl = xoflen; |
145 | 0 | return 1; |
146 | 0 | } |
147 | | |
148 | | static int turboshake_squeeze(void *vctx, unsigned char *out, size_t *outl, |
149 | | size_t outsz) |
150 | 0 | { |
151 | 0 | TURBOSHAKE_CTX *ctx = vctx; |
152 | |
|
153 | 0 | if (ossl_unlikely(!ossl_prov_is_running())) |
154 | 0 | return 0; |
155 | 0 | if (outsz > 0 && !ossl_sha3_squeeze(&ctx->kctx, out, outsz)) |
156 | 0 | return 0; |
157 | 0 | if (outl != NULL) |
158 | 0 | *outl = outsz; |
159 | 0 | return 1; |
160 | 0 | } |
161 | | |
162 | | static void turboshake_freectx(void *vctx) |
163 | 0 | { |
164 | 0 | TURBOSHAKE_CTX *ctx = vctx; |
165 | |
|
166 | 0 | OPENSSL_clear_free(ctx, sizeof(*ctx)); |
167 | 0 | } |
168 | | |
169 | | static void *turboshake_dupctx(void *vctx) |
170 | 0 | { |
171 | 0 | TURBOSHAKE_CTX *in = vctx; |
172 | 0 | TURBOSHAKE_CTX *ret = ossl_prov_is_running() ? OPENSSL_malloc(sizeof(*ret)) |
173 | 0 | : NULL; |
174 | |
|
175 | 0 | if (ret != NULL) |
176 | 0 | *ret = *in; |
177 | 0 | return ret; |
178 | 0 | } |
179 | | |
180 | | static void turboshake_copyctx(void *voutctx, void *vinctx) |
181 | 0 | { |
182 | 0 | TURBOSHAKE_CTX *outctx = voutctx; |
183 | 0 | TURBOSHAKE_CTX *inctx = vinctx; |
184 | |
|
185 | 0 | *outctx = *inctx; |
186 | 0 | } |
187 | | |
188 | | static const OSSL_PARAM *turboshake_gettable_ctx_params( |
189 | | ossl_unused void *ctx, ossl_unused void *provctx) |
190 | 0 | { |
191 | 0 | return turboshake_get_ctx_params_list; |
192 | 0 | } |
193 | | |
194 | | static int turboshake_get_ctx_params(void *vctx, OSSL_PARAM params[]) |
195 | 0 | { |
196 | 0 | TURBOSHAKE_CTX *ctx = vctx; |
197 | 0 | struct turboshake_get_ctx_params_st p; |
198 | 0 | size_t xoflen; |
199 | 0 | unsigned int domain; |
200 | |
|
201 | 0 | if (ctx == NULL || !turboshake_get_ctx_params_decoder(params, &p)) |
202 | 0 | return 0; |
203 | | |
204 | 0 | xoflen = ctx->kctx.md_size; |
205 | 0 | if (p.size != NULL && !OSSL_PARAM_set_size_t(p.size, xoflen)) { |
206 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); |
207 | 0 | return 0; |
208 | 0 | } |
209 | 0 | if (p.xoflen != NULL && !OSSL_PARAM_set_size_t(p.xoflen, xoflen)) { |
210 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); |
211 | 0 | return 0; |
212 | 0 | } |
213 | 0 | domain = ctx->kctx.pad; |
214 | 0 | if (p.domain != NULL && !OSSL_PARAM_set_uint(p.domain, domain)) { |
215 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); |
216 | 0 | return 0; |
217 | 0 | } |
218 | 0 | return 1; |
219 | 0 | } |
220 | | |
221 | | static const OSSL_PARAM *turboshake_settable_ctx_params( |
222 | | ossl_unused void *ctx, ossl_unused void *provctx) |
223 | 0 | { |
224 | 0 | return turboshake_set_ctx_params_list; |
225 | 0 | } |
226 | | |
227 | | static int turboshake_set_ctx_params(void *vctx, const OSSL_PARAM params[]) |
228 | 0 | { |
229 | 0 | TURBOSHAKE_CTX *ctx = vctx; |
230 | 0 | struct turboshake_set_ctx_params_st p; |
231 | 0 | int has_change = 0; |
232 | |
|
233 | 0 | if (ctx == NULL || !turboshake_set_ctx_params_decoder(params, &p)) |
234 | 0 | return 0; |
235 | | |
236 | 0 | has_change = p.xoflen != NULL || p.domain != NULL; |
237 | 0 | if (has_change && (ctx->kctx.xof_state == XOF_STATE_FINAL || ctx->kctx.xof_state == XOF_STATE_SQUEEZE)) |
238 | 0 | return 0; |
239 | | |
240 | 0 | if (p.xoflen != NULL |
241 | 0 | && !OSSL_PARAM_get_size_t(p.xoflen, &ctx->kctx.md_size)) { |
242 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); |
243 | 0 | return 0; |
244 | 0 | } |
245 | 0 | if (p.domain != NULL) { |
246 | 0 | unsigned int domain; |
247 | |
|
248 | 0 | if (!OSSL_PARAM_get_uint(p.domain, &domain)) { |
249 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); |
250 | 0 | return 0; |
251 | 0 | } |
252 | 0 | if (domain < TURBOSHAKE_DOMAIN_MIN || domain > TURBOSHAKE_DOMAIN_MAX) { |
253 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_VALUE_ERROR); |
254 | 0 | return 0; |
255 | 0 | } |
256 | 0 | ctx->kctx.pad = (unsigned char)domain; |
257 | 0 | } |
258 | 0 | return 1; |
259 | 0 | } |
260 | | |
261 | | #define IMPLEMENT_TURBOSHAKE(bitlen) \ |
262 | | static OSSL_FUNC_digest_newctx_fn turboshake_##bitlen##_newctx; \ |
263 | | static void *turboshake_##bitlen##_newctx(void *provctx) \ |
264 | 0 | { \ |
265 | 0 | return turboshake_newctx(provctx, bitlen); \ |
266 | 0 | } \ Unexecuted instantiation: turboshake_prov.c:turboshake_128_newctx Unexecuted instantiation: turboshake_prov.c:turboshake_256_newctx |
267 | | PROV_FUNC_DIGEST_GET_PARAM(turboshake_##bitlen, SHA3_BLOCKSIZE(bitlen), \ |
268 | | bitlen == 128 ? 32 : 64, TURBOSHAKE_FLAGS) \ |
269 | | const OSSL_DISPATCH ossl_turboshake_##bitlen##_functions[] = { \ |
270 | | { OSSL_FUNC_DIGEST_NEWCTX, (void (*)(void))turboshake_##bitlen##_newctx }, \ |
271 | | { OSSL_FUNC_DIGEST_INIT, (void (*)(void))turboshake_init }, \ |
272 | | { OSSL_FUNC_DIGEST_UPDATE, (void (*)(void))turboshake_update }, \ |
273 | | { OSSL_FUNC_DIGEST_FINAL, (void (*)(void))turboshake_final }, \ |
274 | | { OSSL_FUNC_DIGEST_SQUEEZE, (void (*)(void))turboshake_squeeze }, \ |
275 | | { OSSL_FUNC_DIGEST_FREECTX, (void (*)(void))turboshake_freectx }, \ |
276 | | { OSSL_FUNC_DIGEST_DUPCTX, (void (*)(void))turboshake_dupctx }, \ |
277 | | { OSSL_FUNC_DIGEST_COPYCTX, (void (*)(void))turboshake_copyctx }, \ |
278 | | { OSSL_FUNC_DIGEST_SET_CTX_PARAMS, (void (*)(void))turboshake_set_ctx_params }, \ |
279 | | { OSSL_FUNC_DIGEST_SETTABLE_CTX_PARAMS, \ |
280 | | (void (*)(void))turboshake_settable_ctx_params }, \ |
281 | | { OSSL_FUNC_DIGEST_GET_CTX_PARAMS, (void (*)(void))turboshake_get_ctx_params }, \ |
282 | | { OSSL_FUNC_DIGEST_GETTABLE_CTX_PARAMS, \ |
283 | | (void (*)(void))turboshake_gettable_ctx_params }, \ |
284 | | PROV_DISPATCH_FUNC_DIGEST_GET_PARAMS(turboshake_##bitlen), \ |
285 | | PROV_DISPATCH_FUNC_DIGEST_CONSTRUCT_END |
286 | | |
287 | | /* ossl_turboshake_128_functions */ |
288 | | IMPLEMENT_TURBOSHAKE(128) |
289 | | /* ossl_turboshake_256_functions */ |
290 | | IMPLEMENT_TURBOSHAKE(256) |