/src/openssl36/providers/implementations/digests/sha3_prov.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2019-2025 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 | | /* clang-format off */ |
10 | | |
11 | | /* clang-format on */ |
12 | | |
13 | | #include <string.h> |
14 | | #include <openssl/core_names.h> |
15 | | #include <openssl/crypto.h> |
16 | | #include <openssl/evp.h> |
17 | | #include <openssl/params.h> |
18 | | #include <openssl/err.h> |
19 | | #include <openssl/proverr.h> |
20 | | #include "internal/cryptlib.h" |
21 | | #include "internal/numbers.h" |
22 | | #include "internal/sha3.h" |
23 | | #include "prov/digestcommon.h" |
24 | | #include "prov/implementations.h" |
25 | | #include "internal/common.h" |
26 | | |
27 | | #define SHA3_FLAGS PROV_DIGEST_FLAG_ALGID_ABSENT |
28 | | #define SHAKE_FLAGS (PROV_DIGEST_FLAG_XOF | PROV_DIGEST_FLAG_ALGID_ABSENT) |
29 | | #define KMAC_FLAGS PROV_DIGEST_FLAG_XOF |
30 | | |
31 | | /* |
32 | | * Forward declaration of any unique methods implemented here. This is not strictly |
33 | | * necessary for the compiler, but provides an assurance that the signatures |
34 | | * of the functions in the dispatch table are correct. |
35 | | */ |
36 | | static OSSL_FUNC_digest_init_fn keccak_init; |
37 | | static OSSL_FUNC_digest_init_fn keccak_init_params; |
38 | | static OSSL_FUNC_digest_update_fn keccak_update; |
39 | | static OSSL_FUNC_digest_final_fn keccak_final; |
40 | | static OSSL_FUNC_digest_freectx_fn keccak_freectx; |
41 | | static OSSL_FUNC_digest_copyctx_fn keccak_copyctx; |
42 | | static OSSL_FUNC_digest_dupctx_fn keccak_dupctx; |
43 | | static OSSL_FUNC_digest_squeeze_fn shake_squeeze; |
44 | | static OSSL_FUNC_digest_get_ctx_params_fn shake_get_ctx_params; |
45 | | static OSSL_FUNC_digest_gettable_ctx_params_fn shake_gettable_ctx_params; |
46 | | static OSSL_FUNC_digest_set_ctx_params_fn shake_set_ctx_params; |
47 | | static OSSL_FUNC_digest_settable_ctx_params_fn shake_settable_ctx_params; |
48 | | static sha3_absorb_fn generic_sha3_absorb; |
49 | | static sha3_final_fn generic_sha3_final; |
50 | | static sha3_squeeze_fn generic_sha3_squeeze; |
51 | | |
52 | | #if defined(OPENSSL_CPUID_OBJ) && defined(__s390__) && defined(KECCAK1600_ASM) |
53 | | /* |
54 | | * IBM S390X support |
55 | | */ |
56 | | #include "s390x_arch.h" |
57 | | #define S390_SHA3 1 |
58 | | #define S390_SHA3_CAPABLE(name) \ |
59 | | ((OPENSSL_s390xcap_P.kimd[0] & S390X_CAPBIT(S390X_##name)) && (OPENSSL_s390xcap_P.klmd[0] & S390X_CAPBIT(S390X_##name))) |
60 | | |
61 | | #endif |
62 | | |
63 | | static int keccak_init(void *vctx, ossl_unused const OSSL_PARAM params[]) |
64 | 329M | { |
65 | 329M | if (ossl_unlikely(!ossl_prov_is_running())) |
66 | 0 | return 0; |
67 | | /* The newctx() handles most of the ctx fixed setup. */ |
68 | 329M | ossl_sha3_reset((KECCAK1600_CTX *)vctx); |
69 | 329M | return 1; |
70 | 329M | } |
71 | | |
72 | | static int keccak_init_params(void *vctx, const OSSL_PARAM params[]) |
73 | 467M | { |
74 | 467M | return keccak_init(vctx, NULL) |
75 | 467M | && shake_set_ctx_params(vctx, params); |
76 | 467M | } |
77 | | |
78 | | static int keccak_update(void *vctx, const unsigned char *inp, size_t len) |
79 | 1.01G | { |
80 | 1.01G | KECCAK1600_CTX *ctx = vctx; |
81 | 1.01G | const size_t bsz = ctx->block_size; |
82 | 1.01G | size_t num, rem; |
83 | | |
84 | 1.01G | if (ossl_unlikely(len == 0)) |
85 | 0 | return 1; |
86 | | |
87 | | /* Is there anything in the buffer already ? */ |
88 | 1.01G | if (ossl_likely((num = ctx->bufsz) != 0)) { |
89 | | /* Calculate how much space is left in the buffer */ |
90 | 682M | rem = bsz - num; |
91 | | /* If the new input does not fill the buffer then just add it */ |
92 | 682M | if (len < rem) { |
93 | 682M | memcpy(ctx->buf + num, inp, len); |
94 | 682M | ctx->bufsz += len; |
95 | 682M | return 1; |
96 | 682M | } |
97 | | /* otherwise fill up the buffer and absorb the buffer */ |
98 | 449k | memcpy(ctx->buf + num, inp, rem); |
99 | | /* Update the input pointer */ |
100 | 449k | inp += rem; |
101 | 449k | len -= rem; |
102 | 449k | ctx->meth.absorb(ctx, ctx->buf, bsz); |
103 | 449k | ctx->bufsz = 0; |
104 | 449k | } |
105 | | /* Absorb the input - rem = leftover part of the input < blocksize) */ |
106 | 330M | rem = ctx->meth.absorb(ctx, inp, len); |
107 | | /* Copy the leftover bit of the input into the buffer */ |
108 | 330M | if (ossl_likely(rem)) { |
109 | 330M | memcpy(ctx->buf, inp + len - rem, rem); |
110 | 330M | ctx->bufsz = rem; |
111 | 330M | } |
112 | 330M | return 1; |
113 | 1.01G | } |
114 | | |
115 | | static int keccak_final(void *vctx, unsigned char *out, size_t *outl, |
116 | | size_t outlen) |
117 | 329M | { |
118 | 329M | int ret = 1; |
119 | 329M | KECCAK1600_CTX *ctx = vctx; |
120 | | |
121 | 329M | if (ossl_unlikely(!ossl_prov_is_running())) |
122 | 0 | return 0; |
123 | 329M | if (ossl_unlikely(ctx->md_size == SIZE_MAX)) { |
124 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_LENGTH); |
125 | 0 | return 0; |
126 | 0 | } |
127 | 329M | if (ossl_likely(outlen > 0)) |
128 | 329M | ret = ctx->meth.final(ctx, out, ctx->md_size); |
129 | | |
130 | 329M | *outl = ctx->md_size; |
131 | 329M | return ret; |
132 | 329M | } |
133 | | |
134 | | static int shake_squeeze(void *vctx, unsigned char *out, size_t *outl, |
135 | | size_t outlen) |
136 | 1.82M | { |
137 | 1.82M | int ret = 1; |
138 | 1.82M | KECCAK1600_CTX *ctx = vctx; |
139 | | |
140 | 1.82M | if (!ossl_prov_is_running()) |
141 | 0 | return 0; |
142 | 1.82M | if (ctx->meth.squeeze == NULL) |
143 | 0 | return 0; |
144 | 1.82M | if (outlen > 0) |
145 | 1.82M | ret = ctx->meth.squeeze(ctx, out, outlen); |
146 | | |
147 | 1.82M | *outl = outlen; |
148 | 1.82M | return ret; |
149 | 1.82M | } |
150 | | |
151 | | /*- |
152 | | * Generic software version of the absorb() and final(). |
153 | | */ |
154 | | static size_t generic_sha3_absorb(void *vctx, const void *inp, size_t len) |
155 | 468M | { |
156 | 468M | KECCAK1600_CTX *ctx = vctx; |
157 | | |
158 | 468M | if (!(ctx->xof_state == XOF_STATE_INIT || ctx->xof_state == XOF_STATE_ABSORB)) |
159 | 0 | return 0; |
160 | 468M | ctx->xof_state = XOF_STATE_ABSORB; |
161 | 468M | return SHA3_absorb(ctx->A, inp, len, ctx->block_size); |
162 | 468M | } |
163 | | |
164 | | static int generic_sha3_final(void *vctx, unsigned char *out, size_t outlen) |
165 | 466M | { |
166 | 466M | return ossl_sha3_final((KECCAK1600_CTX *)vctx, out, outlen); |
167 | 466M | } |
168 | | |
169 | | static int generic_sha3_squeeze(void *vctx, unsigned char *out, size_t outlen) |
170 | 1.82M | { |
171 | 1.82M | return ossl_sha3_squeeze((KECCAK1600_CTX *)vctx, out, outlen); |
172 | 1.82M | } |
173 | | |
174 | | static PROV_SHA3_METHOD sha3_generic_md = { |
175 | | generic_sha3_absorb, |
176 | | generic_sha3_final, |
177 | | NULL |
178 | | }; |
179 | | |
180 | | static PROV_SHA3_METHOD shake_generic_md = { |
181 | | generic_sha3_absorb, |
182 | | generic_sha3_final, |
183 | | generic_sha3_squeeze |
184 | | }; |
185 | | |
186 | | #if defined(S390_SHA3) |
187 | | |
188 | | static sha3_absorb_fn s390x_sha3_absorb; |
189 | | static sha3_final_fn s390x_sha3_final; |
190 | | static sha3_final_fn s390x_shake_final; |
191 | | |
192 | | /*- |
193 | | * The platform specific parts of the absorb() and final() for S390X. |
194 | | */ |
195 | | static size_t s390x_sha3_absorb(void *vctx, const void *inp, size_t len) |
196 | | { |
197 | | KECCAK1600_CTX *ctx = vctx; |
198 | | size_t rem = len % ctx->block_size; |
199 | | unsigned int fc; |
200 | | |
201 | | if (!(ctx->xof_state == XOF_STATE_INIT || ctx->xof_state == XOF_STATE_ABSORB)) |
202 | | return 0; |
203 | | if (len - rem > 0) { |
204 | | fc = ctx->pad; |
205 | | fc |= ctx->xof_state == XOF_STATE_INIT ? S390X_KIMD_NIP : 0; |
206 | | ctx->xof_state = XOF_STATE_ABSORB; |
207 | | s390x_kimd(inp, len - rem, fc, ctx->A); |
208 | | } |
209 | | return rem; |
210 | | } |
211 | | |
212 | | static int s390x_sha3_final(void *vctx, unsigned char *out, size_t outlen) |
213 | | { |
214 | | KECCAK1600_CTX *ctx = vctx; |
215 | | unsigned int fc; |
216 | | |
217 | | if (!ossl_prov_is_running()) |
218 | | return 0; |
219 | | if (!(ctx->xof_state == XOF_STATE_INIT || ctx->xof_state == XOF_STATE_ABSORB)) |
220 | | return 0; |
221 | | fc = ctx->pad | S390X_KLMD_DUFOP; |
222 | | fc |= ctx->xof_state == XOF_STATE_INIT ? S390X_KLMD_NIP : 0; |
223 | | ctx->xof_state = XOF_STATE_FINAL; |
224 | | s390x_klmd(ctx->buf, ctx->bufsz, NULL, 0, fc, ctx->A); |
225 | | memcpy(out, ctx->A, outlen); |
226 | | return 1; |
227 | | } |
228 | | |
229 | | static int s390x_shake_final(void *vctx, unsigned char *out, size_t outlen) |
230 | | { |
231 | | KECCAK1600_CTX *ctx = vctx; |
232 | | unsigned int fc; |
233 | | |
234 | | if (!ossl_prov_is_running()) |
235 | | return 0; |
236 | | if (!(ctx->xof_state == XOF_STATE_INIT || ctx->xof_state == XOF_STATE_ABSORB)) |
237 | | return 0; |
238 | | fc = ctx->pad | S390X_KLMD_DUFOP; |
239 | | fc |= ctx->xof_state == XOF_STATE_INIT ? S390X_KLMD_NIP : 0; |
240 | | ctx->xof_state = XOF_STATE_FINAL; |
241 | | s390x_klmd(ctx->buf, ctx->bufsz, out, outlen, fc, ctx->A); |
242 | | return 1; |
243 | | } |
244 | | |
245 | | static int s390x_shake_squeeze(void *vctx, unsigned char *out, size_t outlen) |
246 | | { |
247 | | KECCAK1600_CTX *ctx = vctx; |
248 | | unsigned int fc; |
249 | | size_t len; |
250 | | |
251 | | if (!ossl_prov_is_running()) |
252 | | return 0; |
253 | | if (ctx->xof_state == XOF_STATE_FINAL) |
254 | | return 0; |
255 | | /* |
256 | | * On the first squeeze call, finish the absorb process (incl. padding). |
257 | | */ |
258 | | if (ctx->xof_state != XOF_STATE_SQUEEZE) { |
259 | | fc = ctx->pad; |
260 | | fc |= ctx->xof_state == XOF_STATE_INIT ? S390X_KLMD_NIP : 0; |
261 | | ctx->xof_state = XOF_STATE_SQUEEZE; |
262 | | s390x_klmd(ctx->buf, ctx->bufsz, out, outlen, fc, ctx->A); |
263 | | ctx->bufsz = outlen % ctx->block_size; |
264 | | /* reuse ctx->bufsz to count bytes squeezed from current sponge */ |
265 | | return 1; |
266 | | } |
267 | | ctx->xof_state = XOF_STATE_SQUEEZE; |
268 | | if (ctx->bufsz != 0) { |
269 | | len = ctx->block_size - ctx->bufsz; |
270 | | if (outlen < len) |
271 | | len = outlen; |
272 | | memcpy(out, (char *)ctx->A + ctx->bufsz, len); |
273 | | out += len; |
274 | | outlen -= len; |
275 | | ctx->bufsz += len; |
276 | | if (ctx->bufsz == ctx->block_size) |
277 | | ctx->bufsz = 0; |
278 | | } |
279 | | if (outlen == 0) |
280 | | return 1; |
281 | | s390x_klmd(NULL, 0, out, outlen, ctx->pad | S390X_KLMD_PS, ctx->A); |
282 | | ctx->bufsz = outlen % ctx->block_size; |
283 | | |
284 | | return 1; |
285 | | } |
286 | | |
287 | | static int s390x_keccakc_final(void *vctx, unsigned char *out, size_t outlen, |
288 | | int padding) |
289 | | { |
290 | | KECCAK1600_CTX *ctx = vctx; |
291 | | size_t bsz = ctx->block_size; |
292 | | size_t num = ctx->bufsz; |
293 | | size_t needed = outlen; |
294 | | unsigned int fc; |
295 | | |
296 | | if (!ossl_prov_is_running()) |
297 | | return 0; |
298 | | if (!(ctx->xof_state == XOF_STATE_INIT || ctx->xof_state == XOF_STATE_ABSORB)) |
299 | | return 0; |
300 | | fc = ctx->pad; |
301 | | fc |= ctx->xof_state == XOF_STATE_INIT ? S390X_KIMD_NIP : 0; |
302 | | ctx->xof_state = XOF_STATE_FINAL; |
303 | | if (outlen == 0) |
304 | | return 1; |
305 | | memset(ctx->buf + num, 0, bsz - num); |
306 | | ctx->buf[num] = padding; |
307 | | ctx->buf[bsz - 1] |= 0x80; |
308 | | s390x_kimd(ctx->buf, bsz, fc, ctx->A); |
309 | | num = needed > bsz ? bsz : needed; |
310 | | memcpy(out, ctx->A, num); |
311 | | needed -= num; |
312 | | if (needed > 0) |
313 | | s390x_klmd(NULL, 0, out + bsz, needed, |
314 | | ctx->pad | S390X_KLMD_PS | S390X_KLMD_DUFOP, ctx->A); |
315 | | |
316 | | return 1; |
317 | | } |
318 | | |
319 | | static int s390x_keccak_final(void *vctx, unsigned char *out, size_t outlen) |
320 | | { |
321 | | return s390x_keccakc_final(vctx, out, outlen, 0x01); |
322 | | } |
323 | | |
324 | | static int s390x_kmac_final(void *vctx, unsigned char *out, size_t outlen) |
325 | | { |
326 | | return s390x_keccakc_final(vctx, out, outlen, 0x04); |
327 | | } |
328 | | |
329 | | static int s390x_keccakc_squeeze(void *vctx, unsigned char *out, size_t outlen, |
330 | | int padding) |
331 | | { |
332 | | KECCAK1600_CTX *ctx = vctx; |
333 | | size_t len; |
334 | | unsigned int fc; |
335 | | |
336 | | if (!ossl_prov_is_running()) |
337 | | return 0; |
338 | | if (ctx->xof_state == XOF_STATE_FINAL) |
339 | | return 0; |
340 | | /* |
341 | | * On the first squeeze call, finish the absorb process |
342 | | * by adding the trailing padding and then doing |
343 | | * a final absorb. |
344 | | */ |
345 | | if (ctx->xof_state != XOF_STATE_SQUEEZE) { |
346 | | len = ctx->block_size - ctx->bufsz; |
347 | | memset(ctx->buf + ctx->bufsz, 0, len); |
348 | | ctx->buf[ctx->bufsz] = padding; |
349 | | ctx->buf[ctx->block_size - 1] |= 0x80; |
350 | | fc = ctx->pad; |
351 | | fc |= ctx->xof_state == XOF_STATE_INIT ? S390X_KIMD_NIP : 0; |
352 | | s390x_kimd(ctx->buf, ctx->block_size, fc, ctx->A); |
353 | | ctx->bufsz = 0; |
354 | | /* reuse ctx->bufsz to count bytes squeezed from current sponge */ |
355 | | } |
356 | | if (ctx->bufsz != 0 || ctx->xof_state != XOF_STATE_SQUEEZE) { |
357 | | len = ctx->block_size - ctx->bufsz; |
358 | | if (outlen < len) |
359 | | len = outlen; |
360 | | memcpy(out, (char *)ctx->A + ctx->bufsz, len); |
361 | | out += len; |
362 | | outlen -= len; |
363 | | ctx->bufsz += len; |
364 | | if (ctx->bufsz == ctx->block_size) |
365 | | ctx->bufsz = 0; |
366 | | } |
367 | | ctx->xof_state = XOF_STATE_SQUEEZE; |
368 | | if (outlen == 0) |
369 | | return 1; |
370 | | s390x_klmd(NULL, 0, out, outlen, ctx->pad | S390X_KLMD_PS, ctx->A); |
371 | | ctx->bufsz = outlen % ctx->block_size; |
372 | | |
373 | | return 1; |
374 | | } |
375 | | |
376 | | static int s390x_keccak_squeeze(void *vctx, unsigned char *out, size_t outlen) |
377 | | { |
378 | | return s390x_keccakc_squeeze(vctx, out, outlen, 0x01); |
379 | | } |
380 | | |
381 | | static int s390x_kmac_squeeze(void *vctx, unsigned char *out, size_t outlen) |
382 | | { |
383 | | return s390x_keccakc_squeeze(vctx, out, outlen, 0x04); |
384 | | } |
385 | | |
386 | | static PROV_SHA3_METHOD sha3_s390x_md = { |
387 | | s390x_sha3_absorb, |
388 | | s390x_sha3_final, |
389 | | NULL, |
390 | | }; |
391 | | |
392 | | static PROV_SHA3_METHOD keccak_s390x_md = { |
393 | | s390x_sha3_absorb, |
394 | | s390x_keccak_final, |
395 | | s390x_keccak_squeeze, |
396 | | }; |
397 | | |
398 | | static PROV_SHA3_METHOD shake_s390x_md = { |
399 | | s390x_sha3_absorb, |
400 | | s390x_shake_final, |
401 | | s390x_shake_squeeze, |
402 | | }; |
403 | | |
404 | | static PROV_SHA3_METHOD kmac_s390x_md = { |
405 | | s390x_sha3_absorb, |
406 | | s390x_kmac_final, |
407 | | s390x_kmac_squeeze, |
408 | | }; |
409 | | |
410 | | #define SHAKE_SET_MD(uname, typ) \ |
411 | | if (S390_SHA3_CAPABLE(uname)) { \ |
412 | | ctx->pad = S390X_##uname; \ |
413 | | ctx->meth = typ##_s390x_md; \ |
414 | | } else { \ |
415 | | ctx->meth = shake_generic_md; \ |
416 | | } |
417 | | |
418 | | #define SHA3_SET_MD(uname, typ) \ |
419 | | if (S390_SHA3_CAPABLE(uname)) { \ |
420 | | ctx->pad = S390X_##uname; \ |
421 | | ctx->meth = typ##_s390x_md; \ |
422 | | } else { \ |
423 | | ctx->meth = sha3_generic_md; \ |
424 | | } |
425 | | #define KMAC_SET_MD(bitlen) \ |
426 | | if (S390_SHA3_CAPABLE(SHAKE_##bitlen)) { \ |
427 | | ctx->pad = S390X_SHAKE_##bitlen; \ |
428 | | ctx->meth = kmac_s390x_md; \ |
429 | | } else { \ |
430 | | ctx->meth = sha3_generic_md; \ |
431 | | } |
432 | | #elif defined(__aarch64__) && defined(KECCAK1600_ASM) |
433 | | #include "arm_arch.h" |
434 | | |
435 | | static sha3_absorb_fn armsha3_sha3_absorb; |
436 | | |
437 | | size_t SHA3_absorb_cext(uint64_t A[5][5], const unsigned char *inp, size_t len, |
438 | | size_t r); |
439 | | /*- |
440 | | * Hardware-assisted ARMv8.2 SHA3 extension version of the absorb() |
441 | | */ |
442 | | static size_t armsha3_sha3_absorb(void *vctx, const void *inp, size_t len) |
443 | | { |
444 | | KECCAK1600_CTX *ctx = vctx; |
445 | | |
446 | | return SHA3_absorb_cext(ctx->A, inp, len, ctx->block_size); |
447 | | } |
448 | | |
449 | | static PROV_SHA3_METHOD sha3_ARMSHA3_md = { |
450 | | armsha3_sha3_absorb, |
451 | | generic_sha3_final |
452 | | }; |
453 | | static PROV_SHA3_METHOD shake_ARMSHA3_md = { |
454 | | armsha3_sha3_absorb, |
455 | | generic_sha3_final, |
456 | | generic_sha3_squeeze |
457 | | }; |
458 | | #define SHAKE_SET_MD(uname, typ) \ |
459 | | if (OPENSSL_armcap_P & ARMV8_HAVE_SHA3_AND_WORTH_USING) { \ |
460 | | ctx->meth = shake_ARMSHA3_md; \ |
461 | | } else { \ |
462 | | ctx->meth = shake_generic_md; \ |
463 | | } |
464 | | |
465 | | #define SHA3_SET_MD(uname, typ) \ |
466 | | if (OPENSSL_armcap_P & ARMV8_HAVE_SHA3_AND_WORTH_USING) { \ |
467 | | ctx->meth = sha3_ARMSHA3_md; \ |
468 | | } else { \ |
469 | | ctx->meth = sha3_generic_md; \ |
470 | | } |
471 | | #define KMAC_SET_MD(bitlen) \ |
472 | | if (OPENSSL_armcap_P & ARMV8_HAVE_SHA3_AND_WORTH_USING) { \ |
473 | | ctx->meth = sha3_ARMSHA3_md; \ |
474 | | } else { \ |
475 | | ctx->meth = sha3_generic_md; \ |
476 | | } |
477 | | #else |
478 | 176k | #define SHA3_SET_MD(uname, typ) ctx->meth = sha3_generic_md; |
479 | 379 | #define KMAC_SET_MD(bitlen) ctx->meth = sha3_generic_md; |
480 | 109k | #define SHAKE_SET_MD(uname, typ) ctx->meth = shake_generic_md; |
481 | | #endif /* S390_SHA3 */ |
482 | | |
483 | | #define SHA3_newctx(typ, uname, name, bitlen, pad) \ |
484 | | static OSSL_FUNC_digest_newctx_fn name##_newctx; \ |
485 | | static void *name##_newctx(void *provctx) \ |
486 | 176k | { \ |
487 | 176k | KECCAK1600_CTX *ctx = ossl_prov_is_running() ? OPENSSL_zalloc(sizeof(*ctx)) \ |
488 | 176k | : NULL; \ |
489 | 176k | \ |
490 | 176k | if (ctx == NULL) \ |
491 | 176k | return NULL; \ |
492 | 176k | ossl_sha3_init(ctx, pad, bitlen); \ |
493 | 176k | SHA3_SET_MD(uname, typ) \ |
494 | 176k | return ctx; \ |
495 | 176k | } |
496 | | |
497 | | #define SHAKE_newctx(typ, uname, name, bitlen, mdlen, pad) \ |
498 | | static OSSL_FUNC_digest_newctx_fn name##_newctx; \ |
499 | | static void *name##_newctx(void *provctx) \ |
500 | 109k | { \ |
501 | 109k | KECCAK1600_CTX *ctx = ossl_prov_is_running() ? OPENSSL_zalloc(sizeof(*ctx)) \ |
502 | 109k | : NULL; \ |
503 | 109k | \ |
504 | 109k | if (ctx == NULL) \ |
505 | 109k | return NULL; \ |
506 | 109k | ossl_keccak_init(ctx, pad, bitlen, mdlen); \ |
507 | 109k | if (mdlen == 0) \ |
508 | 109k | ctx->md_size = SIZE_MAX; \ |
509 | 109k | SHAKE_SET_MD(uname, typ) \ |
510 | 109k | return ctx; \ |
511 | 109k | } |
512 | | |
513 | | #define KMAC_newctx(uname, bitlen, pad) \ |
514 | | static OSSL_FUNC_digest_newctx_fn uname##_newctx; \ |
515 | | static void *uname##_newctx(void *provctx) \ |
516 | 379 | { \ |
517 | 379 | KECCAK1600_CTX *ctx = ossl_prov_is_running() ? OPENSSL_zalloc(sizeof(*ctx)) \ |
518 | 379 | : NULL; \ |
519 | 379 | \ |
520 | 379 | if (ctx == NULL) \ |
521 | 379 | return NULL; \ |
522 | 379 | ossl_keccak_init(ctx, pad, bitlen, 2 * bitlen); \ |
523 | 379 | KMAC_SET_MD(bitlen) \ |
524 | 379 | return ctx; \ |
525 | 379 | } |
526 | | |
527 | | #define PROV_FUNC_SHA3_DIGEST_COMMON(name, bitlen, blksize, dgstsize, flags) \ |
528 | | PROV_FUNC_DIGEST_GET_PARAM(name, blksize, dgstsize, flags) \ |
529 | | const OSSL_DISPATCH ossl_##name##_functions[] = { \ |
530 | | { OSSL_FUNC_DIGEST_NEWCTX, (void (*)(void))name##_newctx }, \ |
531 | | { OSSL_FUNC_DIGEST_UPDATE, (void (*)(void))keccak_update }, \ |
532 | | { OSSL_FUNC_DIGEST_FINAL, (void (*)(void))keccak_final }, \ |
533 | | { OSSL_FUNC_DIGEST_FREECTX, (void (*)(void))keccak_freectx }, \ |
534 | | { OSSL_FUNC_DIGEST_DUPCTX, (void (*)(void))keccak_dupctx }, \ |
535 | | { OSSL_FUNC_DIGEST_COPYCTX, (void (*)(void))keccak_copyctx }, \ |
536 | | PROV_DISPATCH_FUNC_DIGEST_GET_PARAMS(name) |
537 | | |
538 | | #define PROV_FUNC_SHA3_DIGEST(name, bitlen, blksize, dgstsize, flags) \ |
539 | | PROV_FUNC_SHA3_DIGEST_COMMON(name, bitlen, blksize, dgstsize, flags), \ |
540 | | { OSSL_FUNC_DIGEST_INIT, (void (*)(void))keccak_init }, \ |
541 | | PROV_DISPATCH_FUNC_DIGEST_CONSTRUCT_END |
542 | | |
543 | | #define PROV_FUNC_SHAKE_DIGEST(name, bitlen, blksize, dgstsize, flags) \ |
544 | | PROV_FUNC_SHA3_DIGEST_COMMON(name, bitlen, blksize, dgstsize, flags), \ |
545 | | { OSSL_FUNC_DIGEST_SQUEEZE, (void (*)(void))shake_squeeze }, \ |
546 | | { OSSL_FUNC_DIGEST_INIT, (void (*)(void))keccak_init_params }, \ |
547 | | { OSSL_FUNC_DIGEST_SET_CTX_PARAMS, (void (*)(void))shake_set_ctx_params }, \ |
548 | | { OSSL_FUNC_DIGEST_SETTABLE_CTX_PARAMS, \ |
549 | | (void (*)(void))shake_settable_ctx_params }, \ |
550 | | { OSSL_FUNC_DIGEST_GET_CTX_PARAMS, (void (*)(void))shake_get_ctx_params }, \ |
551 | | { OSSL_FUNC_DIGEST_GETTABLE_CTX_PARAMS, \ |
552 | | (void (*)(void))shake_gettable_ctx_params }, \ |
553 | | PROV_DISPATCH_FUNC_DIGEST_CONSTRUCT_END |
554 | | |
555 | | static void keccak_freectx(void *vctx) |
556 | 392k | { |
557 | 392k | KECCAK1600_CTX *ctx = (KECCAK1600_CTX *)vctx; |
558 | | |
559 | 392k | OPENSSL_clear_free(ctx, sizeof(*ctx)); |
560 | 392k | } |
561 | | |
562 | | static void keccak_copyctx(void *voutctx, void *vinctx) |
563 | 2.92k | { |
564 | 2.92k | KECCAK1600_CTX *outctx = (KECCAK1600_CTX *)voutctx; |
565 | 2.92k | KECCAK1600_CTX *inctx = (KECCAK1600_CTX *)vinctx; |
566 | | |
567 | 2.92k | *outctx = *inctx; |
568 | 2.92k | } |
569 | | |
570 | | static void *keccak_dupctx(void *ctx) |
571 | 1.82k | { |
572 | 1.82k | KECCAK1600_CTX *in = (KECCAK1600_CTX *)ctx; |
573 | 1.82k | KECCAK1600_CTX *ret = ossl_prov_is_running() ? OPENSSL_malloc(sizeof(*ret)) |
574 | 1.82k | : NULL; |
575 | | |
576 | 1.82k | if (ret != NULL) |
577 | 1.82k | *ret = *in; |
578 | 1.82k | return ret; |
579 | 1.82k | } |
580 | | |
581 | | /* clang-format off */ |
582 | | /* Machine generated by util/perl/OpenSSL/paramnames.pm */ |
583 | | #ifndef shake_get_ctx_params_list |
584 | | static const OSSL_PARAM shake_get_ctx_params_list[] = { |
585 | | OSSL_PARAM_size_t(OSSL_DIGEST_PARAM_XOFLEN, NULL), |
586 | | OSSL_PARAM_size_t(OSSL_DIGEST_PARAM_SIZE, NULL), |
587 | | OSSL_PARAM_END |
588 | | }; |
589 | | #endif |
590 | | |
591 | | #ifndef shake_get_ctx_params_st |
592 | | struct shake_get_ctx_params_st { |
593 | | OSSL_PARAM *size; |
594 | | OSSL_PARAM *xoflen; |
595 | | }; |
596 | | #endif |
597 | | |
598 | | #ifndef shake_get_ctx_params_decoder |
599 | | static int shake_get_ctx_params_decoder |
600 | | (const OSSL_PARAM *p, struct shake_get_ctx_params_st *r) |
601 | 18 | { |
602 | 18 | const char *s; |
603 | | |
604 | 18 | memset(r, 0, sizeof(*r)); |
605 | 18 | if (p != NULL) |
606 | 36 | for (; (s = p->key) != NULL; p++) |
607 | 18 | switch(s[0]) { |
608 | 0 | default: |
609 | 0 | break; |
610 | 18 | case 's': |
611 | 18 | if (ossl_likely(strcmp("ize", s + 1) == 0)) { |
612 | | /* OSSL_DIGEST_PARAM_SIZE */ |
613 | 18 | if (ossl_unlikely(r->size != NULL)) { |
614 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
615 | 0 | "param %s is repeated", s); |
616 | 0 | return 0; |
617 | 0 | } |
618 | 18 | r->size = (OSSL_PARAM *)p; |
619 | 18 | } |
620 | 18 | break; |
621 | 18 | case 'x': |
622 | 0 | if (ossl_likely(strcmp("oflen", s + 1) == 0)) { |
623 | | /* OSSL_DIGEST_PARAM_XOFLEN */ |
624 | 0 | if (ossl_unlikely(r->xoflen != NULL)) { |
625 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
626 | 0 | "param %s is repeated", s); |
627 | 0 | return 0; |
628 | 0 | } |
629 | 0 | r->xoflen = (OSSL_PARAM *)p; |
630 | 0 | } |
631 | 18 | } |
632 | 18 | return 1; |
633 | 18 | } |
634 | | #endif |
635 | | /* End of machine generated */ |
636 | | /* clang-format on */ |
637 | | |
638 | | static const OSSL_PARAM *shake_gettable_ctx_params(ossl_unused void *ctx, |
639 | | ossl_unused void *provctx) |
640 | 33 | { |
641 | 33 | return shake_get_ctx_params_list; |
642 | 33 | } |
643 | | |
644 | | static int shake_get_ctx_params(void *vctx, OSSL_PARAM params[]) |
645 | 18 | { |
646 | 18 | struct shake_get_ctx_params_st p; |
647 | 18 | KECCAK1600_CTX *ctx = (KECCAK1600_CTX *)vctx; |
648 | | |
649 | 18 | if (ctx == NULL || !shake_get_ctx_params_decoder(params, &p)) |
650 | 0 | return 0; |
651 | | |
652 | 18 | if (p.xoflen != NULL && !OSSL_PARAM_set_size_t(p.xoflen, ctx->md_size)) { |
653 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); |
654 | 0 | return 0; |
655 | 0 | } |
656 | | /* Size is an alias of xoflen but separate them for compatibility */ |
657 | 18 | if (p.size != NULL && !OSSL_PARAM_set_size_t(p.size, ctx->md_size)) { |
658 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); |
659 | 0 | return 0; |
660 | 0 | } |
661 | 18 | return 1; |
662 | 18 | } |
663 | | |
664 | | /* clang-format off */ |
665 | | /* Machine generated by util/perl/OpenSSL/paramnames.pm */ |
666 | | #ifndef shake_set_ctx_params_list |
667 | | static const OSSL_PARAM shake_set_ctx_params_list[] = { |
668 | | OSSL_PARAM_size_t(OSSL_DIGEST_PARAM_XOFLEN, NULL), |
669 | | OSSL_PARAM_size_t(OSSL_DIGEST_PARAM_SIZE, NULL), |
670 | | OSSL_PARAM_END |
671 | | }; |
672 | | #endif |
673 | | |
674 | | #ifndef shake_set_ctx_params_st |
675 | | struct shake_set_ctx_params_st { |
676 | | OSSL_PARAM *xoflen; |
677 | | }; |
678 | | #endif |
679 | | |
680 | | #ifndef shake_set_ctx_params_decoder |
681 | | static int shake_set_ctx_params_decoder |
682 | | (const OSSL_PARAM *p, struct shake_set_ctx_params_st *r) |
683 | 658M | { |
684 | 658M | const char *s; |
685 | | |
686 | 658M | memset(r, 0, sizeof(*r)); |
687 | 658M | if (p != NULL) |
688 | 658M | for (; (s = p->key) != NULL; p++) |
689 | 329M | switch(s[0]) { |
690 | 0 | default: |
691 | 0 | break; |
692 | 0 | case 's': |
693 | 0 | if (ossl_likely(strcmp("ize", s + 1) == 0)) { |
694 | | /* OSSL_DIGEST_PARAM_SIZE */ |
695 | 0 | if (ossl_unlikely(r->xoflen != NULL)) { |
696 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
697 | 0 | "param %s is repeated", s); |
698 | 0 | return 0; |
699 | 0 | } |
700 | 0 | r->xoflen = (OSSL_PARAM *)p; |
701 | 0 | } |
702 | 0 | break; |
703 | 329M | case 'x': |
704 | 329M | if (ossl_likely(strcmp("oflen", s + 1) == 0)) { |
705 | | /* OSSL_DIGEST_PARAM_XOFLEN */ |
706 | 329M | if (ossl_unlikely(r->xoflen != NULL)) { |
707 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
708 | 0 | "param %s is repeated", s); |
709 | 0 | return 0; |
710 | 0 | } |
711 | 329M | r->xoflen = (OSSL_PARAM *)p; |
712 | 329M | } |
713 | 329M | } |
714 | 658M | return 1; |
715 | 658M | } |
716 | | #endif |
717 | | /* End of machine generated */ |
718 | | /* clang-format on */ |
719 | | |
720 | | static const OSSL_PARAM *shake_settable_ctx_params(ossl_unused void *ctx, |
721 | | ossl_unused void *provctx) |
722 | 28 | { |
723 | 28 | return shake_set_ctx_params_list; |
724 | 28 | } |
725 | | |
726 | | static int shake_set_ctx_params(void *vctx, const OSSL_PARAM params[]) |
727 | 658M | { |
728 | 658M | struct shake_set_ctx_params_st p; |
729 | 658M | KECCAK1600_CTX *ctx = (KECCAK1600_CTX *)vctx; |
730 | | |
731 | 658M | if (ossl_unlikely(ctx == NULL || !shake_set_ctx_params_decoder(params, &p))) |
732 | 0 | return 0; |
733 | | |
734 | 658M | if (ossl_unlikely(p.xoflen != NULL |
735 | 658M | && !OSSL_PARAM_get_size_t(p.xoflen, &ctx->md_size))) { |
736 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); |
737 | 0 | return 0; |
738 | 0 | } |
739 | 658M | return 1; |
740 | 658M | } |
741 | | |
742 | | #define IMPLEMENT_SHA3_functions(bitlen) \ |
743 | | SHA3_newctx(sha3, SHA3_##bitlen, sha3_##bitlen, bitlen, '\x06') \ |
744 | | PROV_FUNC_SHA3_DIGEST(sha3_##bitlen, bitlen, \ |
745 | | SHA3_BLOCKSIZE(bitlen), SHA3_MDSIZE(bitlen), \ |
746 | | SHA3_FLAGS) |
747 | | |
748 | | #define IMPLEMENT_KECCAK_functions(bitlen) \ |
749 | | SHA3_newctx(keccak, KECCAK_##bitlen, keccak_##bitlen, bitlen, '\x01') \ |
750 | | PROV_FUNC_SHA3_DIGEST(keccak_##bitlen, bitlen, \ |
751 | | SHA3_BLOCKSIZE(bitlen), SHA3_MDSIZE(bitlen), \ |
752 | | SHA3_FLAGS) |
753 | | |
754 | | #define IMPLEMENT_SHAKE_functions(bitlen) \ |
755 | | SHAKE_newctx(shake, SHAKE_##bitlen, shake_##bitlen, bitlen, \ |
756 | | 0 /* no default md length */, '\x1f') \ |
757 | | PROV_FUNC_SHAKE_DIGEST(shake_##bitlen, bitlen, \ |
758 | | SHA3_BLOCKSIZE(bitlen), 0, \ |
759 | | SHAKE_FLAGS) |
760 | | |
761 | | #define IMPLEMENT_KMAC_functions(bitlen) \ |
762 | | KMAC_newctx(keccak_kmac_##bitlen, bitlen, '\x04') \ |
763 | | PROV_FUNC_SHAKE_DIGEST(keccak_kmac_##bitlen, bitlen, \ |
764 | | SHA3_BLOCKSIZE(bitlen), KMAC_MDSIZE(bitlen), \ |
765 | | KMAC_FLAGS) |
766 | | |
767 | | /* ossl_sha3_224_functions */ |
768 | 57.7k | IMPLEMENT_SHA3_functions(224) |
769 | 57.7k | /* ossl_sha3_256_functions */ |
770 | 159k | IMPLEMENT_SHA3_functions(256) |
771 | 159k | /* ossl_sha3_384_functions */ |
772 | 159k | IMPLEMENT_SHA3_functions(384) |
773 | 20.8k | /* ossl_sha3_512_functions */ |
774 | 109k | IMPLEMENT_SHA3_functions(512) |
775 | 109k | /* ossl_keccak_224_functions */ |
776 | 109k | IMPLEMENT_KECCAK_functions(224) |
777 | 2.64k | /* ossl_keccak_256_functions */ |
778 | 2.64k | IMPLEMENT_KECCAK_functions(256) |
779 | 1.43k | /* ossl_keccak_384_functions */ |
780 | 1.43k | IMPLEMENT_KECCAK_functions(384) |
781 | 856 | /* ossl_keccak_512_functions */ |
782 | 1.02k | IMPLEMENT_KECCAK_functions(512) |
783 | 1.02k | /* ossl_shake_128_functions */ |
784 | 158k | IMPLEMENT_SHAKE_functions(128) |
785 | 158k | /* ossl_shake_256_functions */ |
786 | 169k | IMPLEMENT_SHAKE_functions(256) |
787 | 169k | /* ossl_keccak_kmac_128_functions */ |
788 | 169k | IMPLEMENT_KMAC_functions(128) |
789 | 336 | /* ossl_keccak_kmac_256_functions */ |
790 | | IMPLEMENT_KMAC_functions(256) |