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