/src/openssl/providers/implementations/digests/sha3_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 <string.h> |
11 | | #include <openssl/core_names.h> |
12 | | #include <openssl/crypto.h> |
13 | | #include <openssl/evp.h> |
14 | | #include <openssl/params.h> |
15 | | #include <openssl/err.h> |
16 | | #include <openssl/proverr.h> |
17 | | #include "internal/numbers.h" |
18 | | #include "internal/sha3.h" |
19 | | #include "prov/digestcommon.h" |
20 | | #include "prov/implementations.h" |
21 | | |
22 | | #define SHA3_FLAGS PROV_DIGEST_FLAG_ALGID_ABSENT |
23 | | #define SHAKE_FLAGS PROV_DIGEST_FLAG_XOF |
24 | | #define KMAC_FLAGS PROV_DIGEST_FLAG_XOF |
25 | | |
26 | | /* |
27 | | * Forward declaration of any unique methods implemented here. This is not strictly |
28 | | * necessary for the compiler, but provides an assurance that the signatures |
29 | | * of the functions in the dispatch table are correct. |
30 | | */ |
31 | | static OSSL_FUNC_digest_init_fn keccak_init; |
32 | | static OSSL_FUNC_digest_init_fn keccak_init_params; |
33 | | static OSSL_FUNC_digest_update_fn keccak_update; |
34 | | static OSSL_FUNC_digest_final_fn keccak_final; |
35 | | static OSSL_FUNC_digest_freectx_fn keccak_freectx; |
36 | | static OSSL_FUNC_digest_dupctx_fn keccak_dupctx; |
37 | | static OSSL_FUNC_digest_squeeze_fn shake_squeeze; |
38 | | static OSSL_FUNC_digest_set_ctx_params_fn shake_set_ctx_params; |
39 | | static OSSL_FUNC_digest_settable_ctx_params_fn shake_settable_ctx_params; |
40 | | static sha3_absorb_fn generic_sha3_absorb; |
41 | | static sha3_final_fn generic_sha3_final; |
42 | | static sha3_squeeze_fn generic_sha3_squeeze; |
43 | | |
44 | | #if defined(OPENSSL_CPUID_OBJ) && defined(__s390__) && defined(KECCAK1600_ASM) |
45 | | /* |
46 | | * IBM S390X support |
47 | | */ |
48 | | # include "s390x_arch.h" |
49 | | # define S390_SHA3 1 |
50 | | # define S390_SHA3_CAPABLE(name) \ |
51 | | ((OPENSSL_s390xcap_P.kimd[0] & S390X_CAPBIT(S390X_##name)) && \ |
52 | | (OPENSSL_s390xcap_P.klmd[0] & S390X_CAPBIT(S390X_##name))) |
53 | | |
54 | | #endif |
55 | | |
56 | | static int keccak_init(void *vctx, ossl_unused const OSSL_PARAM params[]) |
57 | 114 | { |
58 | 114 | if (!ossl_prov_is_running()) |
59 | 0 | return 0; |
60 | | /* The newctx() handles most of the ctx fixed setup. */ |
61 | 114 | ossl_sha3_reset((KECCAK1600_CTX *)vctx); |
62 | 114 | return 1; |
63 | 114 | } |
64 | | |
65 | | static int keccak_init_params(void *vctx, const OSSL_PARAM params[]) |
66 | 0 | { |
67 | 0 | return keccak_init(vctx, NULL) |
68 | 0 | && shake_set_ctx_params(vctx, params); |
69 | 0 | } |
70 | | |
71 | | static int keccak_update(void *vctx, const unsigned char *inp, size_t len) |
72 | 114 | { |
73 | 114 | KECCAK1600_CTX *ctx = vctx; |
74 | 114 | const size_t bsz = ctx->block_size; |
75 | 114 | size_t num, rem; |
76 | | |
77 | 114 | if (len == 0) |
78 | 0 | return 1; |
79 | | |
80 | | /* Is there anything in the buffer already ? */ |
81 | 114 | if ((num = ctx->bufsz) != 0) { |
82 | | /* Calculate how much space is left in the buffer */ |
83 | 57 | rem = bsz - num; |
84 | | /* If the new input does not fill the buffer then just add it */ |
85 | 57 | if (len < rem) { |
86 | 0 | memcpy(ctx->buf + num, inp, len); |
87 | 0 | ctx->bufsz += len; |
88 | 0 | return 1; |
89 | 0 | } |
90 | | /* otherwise fill up the buffer and absorb the buffer */ |
91 | 57 | memcpy(ctx->buf + num, inp, rem); |
92 | | /* Update the input pointer */ |
93 | 57 | inp += rem; |
94 | 57 | len -= rem; |
95 | 57 | ctx->meth.absorb(ctx, ctx->buf, bsz); |
96 | 57 | ctx->bufsz = 0; |
97 | 57 | } |
98 | | /* Absorb the input - rem = leftover part of the input < blocksize) */ |
99 | 114 | rem = ctx->meth.absorb(ctx, inp, len); |
100 | | /* Copy the leftover bit of the input into the buffer */ |
101 | 114 | if (rem) { |
102 | 108 | memcpy(ctx->buf, inp + len - rem, rem); |
103 | 108 | ctx->bufsz = rem; |
104 | 108 | } |
105 | 114 | return 1; |
106 | 114 | } |
107 | | |
108 | | static int keccak_final(void *vctx, unsigned char *out, size_t *outl, |
109 | | size_t outlen) |
110 | 57 | { |
111 | 57 | int ret = 1; |
112 | 57 | KECCAK1600_CTX *ctx = vctx; |
113 | | |
114 | 57 | if (!ossl_prov_is_running()) |
115 | 0 | return 0; |
116 | 57 | if (ctx->md_size == SIZE_MAX) { |
117 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_LENGTH); |
118 | 0 | return 0; |
119 | 0 | } |
120 | 57 | if (outlen > 0) |
121 | 57 | ret = ctx->meth.final(ctx, out, ctx->md_size); |
122 | | |
123 | 57 | *outl = ctx->md_size; |
124 | 57 | return ret; |
125 | 57 | } |
126 | | |
127 | | static int shake_squeeze(void *vctx, unsigned char *out, size_t *outl, |
128 | | size_t outlen) |
129 | 0 | { |
130 | 0 | int ret = 1; |
131 | 0 | KECCAK1600_CTX *ctx = vctx; |
132 | |
|
133 | 0 | if (!ossl_prov_is_running()) |
134 | 0 | return 0; |
135 | 0 | if (ctx->meth.squeeze == NULL) |
136 | 0 | return 0; |
137 | 0 | if (outlen > 0) |
138 | 0 | ret = ctx->meth.squeeze(ctx, out, outlen); |
139 | |
|
140 | 0 | *outl = outlen; |
141 | 0 | return ret; |
142 | 0 | } |
143 | | |
144 | | /*- |
145 | | * Generic software version of the absorb() and final(). |
146 | | */ |
147 | | static size_t generic_sha3_absorb(void *vctx, const void *inp, size_t len) |
148 | 171 | { |
149 | 171 | KECCAK1600_CTX *ctx = vctx; |
150 | | |
151 | 171 | if (!(ctx->xof_state == XOF_STATE_INIT || |
152 | 171 | ctx->xof_state == XOF_STATE_ABSORB)) |
153 | 0 | return 0; |
154 | 171 | ctx->xof_state = XOF_STATE_ABSORB; |
155 | 171 | return SHA3_absorb(ctx->A, inp, len, ctx->block_size); |
156 | 171 | } |
157 | | |
158 | | static int generic_sha3_final(void *vctx, unsigned char *out, size_t outlen) |
159 | 57 | { |
160 | 57 | return ossl_sha3_final((KECCAK1600_CTX *)vctx, out, outlen); |
161 | 57 | } |
162 | | |
163 | | static int generic_sha3_squeeze(void *vctx, unsigned char *out, size_t outlen) |
164 | 0 | { |
165 | 0 | return ossl_sha3_squeeze((KECCAK1600_CTX *)vctx, out, outlen); |
166 | 0 | } |
167 | | |
168 | | static PROV_SHA3_METHOD sha3_generic_md = |
169 | | { |
170 | | generic_sha3_absorb, |
171 | | generic_sha3_final, |
172 | | NULL |
173 | | }; |
174 | | |
175 | | static PROV_SHA3_METHOD shake_generic_md = |
176 | | { |
177 | | generic_sha3_absorb, |
178 | | generic_sha3_final, |
179 | | generic_sha3_squeeze |
180 | | }; |
181 | | |
182 | | #if defined(S390_SHA3) |
183 | | |
184 | | static sha3_absorb_fn s390x_sha3_absorb; |
185 | | static sha3_final_fn s390x_sha3_final; |
186 | | static sha3_final_fn s390x_shake_final; |
187 | | |
188 | | /*- |
189 | | * The platform specific parts of the absorb() and final() for S390X. |
190 | | */ |
191 | | static size_t s390x_sha3_absorb(void *vctx, const void *inp, size_t len) |
192 | | { |
193 | | KECCAK1600_CTX *ctx = vctx; |
194 | | size_t rem = len % ctx->block_size; |
195 | | |
196 | | if (!(ctx->xof_state == XOF_STATE_INIT || |
197 | | ctx->xof_state == XOF_STATE_ABSORB)) |
198 | | return 0; |
199 | | ctx->xof_state = XOF_STATE_ABSORB; |
200 | | s390x_kimd(inp, len - rem, ctx->pad, ctx->A); |
201 | | return rem; |
202 | | } |
203 | | |
204 | | static int s390x_sha3_final(void *vctx, unsigned char *out, size_t outlen) |
205 | | { |
206 | | KECCAK1600_CTX *ctx = vctx; |
207 | | |
208 | | if (!ossl_prov_is_running()) |
209 | | return 0; |
210 | | if (!(ctx->xof_state == XOF_STATE_INIT || |
211 | | ctx->xof_state == XOF_STATE_ABSORB)) |
212 | | return 0; |
213 | | ctx->xof_state = XOF_STATE_FINAL; |
214 | | s390x_klmd(ctx->buf, ctx->bufsz, NULL, 0, ctx->pad, ctx->A); |
215 | | memcpy(out, ctx->A, outlen); |
216 | | return 1; |
217 | | } |
218 | | |
219 | | static int s390x_shake_final(void *vctx, unsigned char *out, size_t outlen) |
220 | | { |
221 | | KECCAK1600_CTX *ctx = vctx; |
222 | | |
223 | | if (!ossl_prov_is_running()) |
224 | | return 0; |
225 | | if (!(ctx->xof_state == XOF_STATE_INIT || |
226 | | ctx->xof_state == XOF_STATE_ABSORB)) |
227 | | return 0; |
228 | | ctx->xof_state = XOF_STATE_FINAL; |
229 | | s390x_klmd(ctx->buf, ctx->bufsz, out, outlen, ctx->pad, ctx->A); |
230 | | return 1; |
231 | | } |
232 | | |
233 | | static int s390x_shake_squeeze(void *vctx, unsigned char *out, size_t outlen) |
234 | | { |
235 | | KECCAK1600_CTX *ctx = vctx; |
236 | | size_t len; |
237 | | |
238 | | if (!ossl_prov_is_running()) |
239 | | return 0; |
240 | | if (ctx->xof_state == XOF_STATE_FINAL) |
241 | | return 0; |
242 | | /* |
243 | | * On the first squeeze call, finish the absorb process (incl. padding). |
244 | | */ |
245 | | if (ctx->xof_state != XOF_STATE_SQUEEZE) { |
246 | | ctx->xof_state = XOF_STATE_SQUEEZE; |
247 | | s390x_klmd(ctx->buf, ctx->bufsz, out, outlen, ctx->pad, ctx->A); |
248 | | ctx->bufsz = outlen % ctx->block_size; |
249 | | /* reuse ctx->bufsz to count bytes squeezed from current sponge */ |
250 | | return 1; |
251 | | } |
252 | | ctx->xof_state = XOF_STATE_SQUEEZE; |
253 | | if (ctx->bufsz != 0) { |
254 | | len = ctx->block_size - ctx->bufsz; |
255 | | if (outlen < len) |
256 | | len = outlen; |
257 | | memcpy(out, (char *)ctx->A + ctx->bufsz, len); |
258 | | out += len; |
259 | | outlen -= len; |
260 | | ctx->bufsz += len; |
261 | | if (ctx->bufsz == ctx->block_size) |
262 | | ctx->bufsz = 0; |
263 | | } |
264 | | if (outlen == 0) |
265 | | return 1; |
266 | | s390x_klmd(NULL, 0, out, outlen, ctx->pad | S390X_KLMD_PS, ctx->A); |
267 | | ctx->bufsz = outlen % ctx->block_size; |
268 | | |
269 | | return 1; |
270 | | } |
271 | | |
272 | | static int s390x_keccakc_final(void *vctx, unsigned char *out, size_t outlen, |
273 | | int padding) |
274 | | { |
275 | | KECCAK1600_CTX *ctx = vctx; |
276 | | size_t bsz = ctx->block_size; |
277 | | size_t num = ctx->bufsz; |
278 | | size_t needed = outlen; |
279 | | |
280 | | if (!ossl_prov_is_running()) |
281 | | return 0; |
282 | | if (!(ctx->xof_state == XOF_STATE_INIT || |
283 | | ctx->xof_state == XOF_STATE_ABSORB)) |
284 | | return 0; |
285 | | ctx->xof_state = XOF_STATE_FINAL; |
286 | | if (outlen == 0) |
287 | | return 1; |
288 | | memset(ctx->buf + num, 0, bsz - num); |
289 | | ctx->buf[num] = padding; |
290 | | ctx->buf[bsz - 1] |= 0x80; |
291 | | s390x_kimd(ctx->buf, bsz, ctx->pad, ctx->A); |
292 | | num = needed > bsz ? bsz : needed; |
293 | | memcpy(out, ctx->A, num); |
294 | | needed -= num; |
295 | | if (needed > 0) |
296 | | s390x_klmd(NULL, 0, out + bsz, needed, ctx->pad | S390X_KLMD_PS, ctx->A); |
297 | | |
298 | | return 1; |
299 | | } |
300 | | |
301 | | static int s390x_keccak_final(void *vctx, unsigned char *out, size_t outlen) |
302 | | { |
303 | | return s390x_keccakc_final(vctx, out, outlen, 0x01); |
304 | | } |
305 | | |
306 | | static int s390x_kmac_final(void *vctx, unsigned char *out, size_t outlen) |
307 | | { |
308 | | return s390x_keccakc_final(vctx, out, outlen, 0x04); |
309 | | } |
310 | | |
311 | | static int s390x_keccakc_squeeze(void *vctx, unsigned char *out, size_t outlen, |
312 | | int padding) |
313 | | { |
314 | | KECCAK1600_CTX *ctx = vctx; |
315 | | size_t len; |
316 | | |
317 | | if (!ossl_prov_is_running()) |
318 | | return 0; |
319 | | if (ctx->xof_state == XOF_STATE_FINAL) |
320 | | return 0; |
321 | | /* |
322 | | * On the first squeeze call, finish the absorb process |
323 | | * by adding the trailing padding and then doing |
324 | | * a final absorb. |
325 | | */ |
326 | | if (ctx->xof_state != XOF_STATE_SQUEEZE) { |
327 | | len = ctx->block_size - ctx->bufsz; |
328 | | memset(ctx->buf + ctx->bufsz, 0, len); |
329 | | ctx->buf[ctx->bufsz] = padding; |
330 | | ctx->buf[ctx->block_size - 1] |= 0x80; |
331 | | s390x_kimd(ctx->buf, ctx->block_size, ctx->pad, ctx->A); |
332 | | ctx->bufsz = 0; |
333 | | /* reuse ctx->bufsz to count bytes squeezed from current sponge */ |
334 | | } |
335 | | if (ctx->bufsz != 0 || ctx->xof_state != XOF_STATE_SQUEEZE) { |
336 | | len = ctx->block_size - ctx->bufsz; |
337 | | if (outlen < len) |
338 | | len = outlen; |
339 | | memcpy(out, (char *)ctx->A + ctx->bufsz, len); |
340 | | out += len; |
341 | | outlen -= len; |
342 | | ctx->bufsz += len; |
343 | | if (ctx->bufsz == ctx->block_size) |
344 | | ctx->bufsz = 0; |
345 | | } |
346 | | ctx->xof_state = XOF_STATE_SQUEEZE; |
347 | | if (outlen == 0) |
348 | | return 1; |
349 | | s390x_klmd(NULL, 0, out, outlen, ctx->pad | S390X_KLMD_PS, ctx->A); |
350 | | ctx->bufsz = outlen % ctx->block_size; |
351 | | |
352 | | return 1; |
353 | | } |
354 | | |
355 | | static int s390x_keccak_squeeze(void *vctx, unsigned char *out, size_t outlen) |
356 | | { |
357 | | return s390x_keccakc_squeeze(vctx, out, outlen, 0x01); |
358 | | } |
359 | | |
360 | | static int s390x_kmac_squeeze(void *vctx, unsigned char *out, size_t outlen) |
361 | | { |
362 | | return s390x_keccakc_squeeze(vctx, out, outlen, 0x04); |
363 | | } |
364 | | |
365 | | static PROV_SHA3_METHOD sha3_s390x_md = |
366 | | { |
367 | | s390x_sha3_absorb, |
368 | | s390x_sha3_final, |
369 | | NULL, |
370 | | }; |
371 | | |
372 | | static PROV_SHA3_METHOD keccak_s390x_md = |
373 | | { |
374 | | s390x_sha3_absorb, |
375 | | s390x_keccak_final, |
376 | | s390x_keccak_squeeze, |
377 | | }; |
378 | | |
379 | | static PROV_SHA3_METHOD shake_s390x_md = |
380 | | { |
381 | | s390x_sha3_absorb, |
382 | | s390x_shake_final, |
383 | | s390x_shake_squeeze, |
384 | | }; |
385 | | |
386 | | static PROV_SHA3_METHOD kmac_s390x_md = |
387 | | { |
388 | | s390x_sha3_absorb, |
389 | | s390x_kmac_final, |
390 | | s390x_kmac_squeeze, |
391 | | }; |
392 | | |
393 | | # define SHAKE_SET_MD(uname, typ) \ |
394 | | if (S390_SHA3_CAPABLE(uname)) { \ |
395 | | ctx->pad = S390X_##uname; \ |
396 | | ctx->meth = typ##_s390x_md; \ |
397 | | } else { \ |
398 | | ctx->meth = shake_generic_md; \ |
399 | | } |
400 | | |
401 | | # define SHA3_SET_MD(uname, typ) \ |
402 | | if (S390_SHA3_CAPABLE(uname)) { \ |
403 | | ctx->pad = S390X_##uname; \ |
404 | | ctx->meth = typ##_s390x_md; \ |
405 | | } else { \ |
406 | | ctx->meth = sha3_generic_md; \ |
407 | | } |
408 | | # define KMAC_SET_MD(bitlen) \ |
409 | | if (S390_SHA3_CAPABLE(SHAKE_##bitlen)) { \ |
410 | | ctx->pad = S390X_SHAKE_##bitlen; \ |
411 | | ctx->meth = kmac_s390x_md; \ |
412 | | } else { \ |
413 | | ctx->meth = sha3_generic_md; \ |
414 | | } |
415 | | #elif defined(__aarch64__) && defined(KECCAK1600_ASM) |
416 | | # include "arm_arch.h" |
417 | | |
418 | | static sha3_absorb_fn armsha3_sha3_absorb; |
419 | | |
420 | | size_t SHA3_absorb_cext(uint64_t A[5][5], const unsigned char *inp, size_t len, |
421 | | size_t r); |
422 | | /*- |
423 | | * Hardware-assisted ARMv8.2 SHA3 extension version of the absorb() |
424 | | */ |
425 | | static size_t armsha3_sha3_absorb(void *vctx, const void *inp, size_t len) |
426 | | { |
427 | | KECCAK1600_CTX *ctx = vctx; |
428 | | |
429 | | return SHA3_absorb_cext(ctx->A, inp, len, ctx->block_size); |
430 | | } |
431 | | |
432 | | static PROV_SHA3_METHOD sha3_ARMSHA3_md = |
433 | | { |
434 | | armsha3_sha3_absorb, |
435 | | generic_sha3_final |
436 | | }; |
437 | | static PROV_SHA3_METHOD shake_ARMSHA3_md = |
438 | | { |
439 | | armsha3_sha3_absorb, |
440 | | generic_sha3_final, |
441 | | generic_sha3_squeeze |
442 | | }; |
443 | | # define SHAKE_SET_MD(uname, typ) \ |
444 | | if (OPENSSL_armcap_P & ARMV8_HAVE_SHA3_AND_WORTH_USING) { \ |
445 | | ctx->meth = shake_ARMSHA3_md; \ |
446 | | } else { \ |
447 | | ctx->meth = shake_generic_md; \ |
448 | | } |
449 | | |
450 | | # define SHA3_SET_MD(uname, typ) \ |
451 | | if (OPENSSL_armcap_P & ARMV8_HAVE_SHA3_AND_WORTH_USING) { \ |
452 | | ctx->meth = sha3_ARMSHA3_md; \ |
453 | | } else { \ |
454 | | ctx->meth = sha3_generic_md; \ |
455 | | } |
456 | | # define KMAC_SET_MD(bitlen) \ |
457 | | if (OPENSSL_armcap_P & ARMV8_HAVE_SHA3_AND_WORTH_USING) { \ |
458 | | ctx->meth = sha3_ARMSHA3_md; \ |
459 | | } else { \ |
460 | | ctx->meth = sha3_generic_md; \ |
461 | | } |
462 | | #else |
463 | 57 | # define SHA3_SET_MD(uname, typ) ctx->meth = sha3_generic_md; |
464 | 0 | # define KMAC_SET_MD(bitlen) ctx->meth = sha3_generic_md; |
465 | 0 | # define SHAKE_SET_MD(uname, typ) ctx->meth = shake_generic_md; |
466 | | #endif /* S390_SHA3 */ |
467 | | |
468 | | #define SHA3_newctx(typ, uname, name, bitlen, pad) \ |
469 | | static OSSL_FUNC_digest_newctx_fn name##_newctx; \ |
470 | 57 | static void *name##_newctx(void *provctx) \ |
471 | 57 | { \ |
472 | 57 | KECCAK1600_CTX *ctx = ossl_prov_is_running() ? OPENSSL_zalloc(sizeof(*ctx)) \ |
473 | 57 | : NULL; \ |
474 | 57 | \ |
475 | 57 | if (ctx == NULL) \ |
476 | 57 | return NULL; \ |
477 | 57 | ossl_sha3_init(ctx, pad, bitlen); \ |
478 | 57 | SHA3_SET_MD(uname, typ) \ |
479 | 57 | return ctx; \ |
480 | 57 | } sha3_prov.c:sha3_224_newctx Line | Count | Source | 470 | 23 | static void *name##_newctx(void *provctx) \ | 471 | 23 | { \ | 472 | 23 | KECCAK1600_CTX *ctx = ossl_prov_is_running() ? OPENSSL_zalloc(sizeof(*ctx)) \ | 473 | 23 | : NULL; \ | 474 | 23 | \ | 475 | 23 | if (ctx == NULL) \ | 476 | 23 | return NULL; \ | 477 | 23 | ossl_sha3_init(ctx, pad, bitlen); \ | 478 | 23 | SHA3_SET_MD(uname, typ) \ | 479 | 23 | return ctx; \ | 480 | 23 | } |
sha3_prov.c:sha3_256_newctx Line | Count | Source | 470 | 8 | static void *name##_newctx(void *provctx) \ | 471 | 8 | { \ | 472 | 8 | KECCAK1600_CTX *ctx = ossl_prov_is_running() ? OPENSSL_zalloc(sizeof(*ctx)) \ | 473 | 8 | : NULL; \ | 474 | 8 | \ | 475 | 8 | if (ctx == NULL) \ | 476 | 8 | return NULL; \ | 477 | 8 | ossl_sha3_init(ctx, pad, bitlen); \ | 478 | 8 | SHA3_SET_MD(uname, typ) \ | 479 | 8 | return ctx; \ | 480 | 8 | } |
sha3_prov.c:sha3_384_newctx Line | Count | Source | 470 | 6 | static void *name##_newctx(void *provctx) \ | 471 | 6 | { \ | 472 | 6 | KECCAK1600_CTX *ctx = ossl_prov_is_running() ? OPENSSL_zalloc(sizeof(*ctx)) \ | 473 | 6 | : NULL; \ | 474 | 6 | \ | 475 | 6 | if (ctx == NULL) \ | 476 | 6 | return NULL; \ | 477 | 6 | ossl_sha3_init(ctx, pad, bitlen); \ | 478 | 6 | SHA3_SET_MD(uname, typ) \ | 479 | 6 | return ctx; \ | 480 | 6 | } |
sha3_prov.c:sha3_512_newctx Line | Count | Source | 470 | 20 | static void *name##_newctx(void *provctx) \ | 471 | 20 | { \ | 472 | 20 | KECCAK1600_CTX *ctx = ossl_prov_is_running() ? OPENSSL_zalloc(sizeof(*ctx)) \ | 473 | 20 | : NULL; \ | 474 | 20 | \ | 475 | 20 | if (ctx == NULL) \ | 476 | 20 | return NULL; \ | 477 | 20 | ossl_sha3_init(ctx, pad, bitlen); \ | 478 | 20 | SHA3_SET_MD(uname, typ) \ | 479 | 20 | return ctx; \ | 480 | 20 | } |
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 |
481 | | |
482 | | #define SHAKE_newctx(typ, uname, name, bitlen, mdlen, pad) \ |
483 | | static OSSL_FUNC_digest_newctx_fn name##_newctx; \ |
484 | 0 | static void *name##_newctx(void *provctx) \ |
485 | 0 | { \ |
486 | 0 | KECCAK1600_CTX *ctx = ossl_prov_is_running() ? OPENSSL_zalloc(sizeof(*ctx))\ |
487 | 0 | : NULL; \ |
488 | 0 | \ |
489 | 0 | if (ctx == NULL) \ |
490 | 0 | return NULL; \ |
491 | 0 | ossl_keccak_init(ctx, pad, bitlen, mdlen); \ |
492 | 0 | if (mdlen == 0) \ |
493 | 0 | ctx->md_size = SIZE_MAX; \ |
494 | 0 | SHAKE_SET_MD(uname, typ) \ |
495 | 0 | return ctx; \ |
496 | 0 | } Unexecuted instantiation: sha3_prov.c:shake_128_newctx Unexecuted instantiation: sha3_prov.c:shake_256_newctx |
497 | | |
498 | | #define KMAC_newctx(uname, bitlen, pad) \ |
499 | | static OSSL_FUNC_digest_newctx_fn uname##_newctx; \ |
500 | 0 | static void *uname##_newctx(void *provctx) \ |
501 | 0 | { \ |
502 | 0 | KECCAK1600_CTX *ctx = ossl_prov_is_running() ? OPENSSL_zalloc(sizeof(*ctx)) \ |
503 | 0 | : NULL; \ |
504 | 0 | \ |
505 | 0 | if (ctx == NULL) \ |
506 | 0 | return NULL; \ |
507 | 0 | ossl_keccak_init(ctx, pad, bitlen, 2 * bitlen); \ |
508 | 0 | KMAC_SET_MD(bitlen) \ |
509 | 0 | return ctx; \ |
510 | 0 | } Unexecuted instantiation: sha3_prov.c:keccak_kmac_128_newctx Unexecuted instantiation: sha3_prov.c:keccak_kmac_256_newctx |
511 | | |
512 | | #define PROV_FUNC_SHA3_DIGEST_COMMON(name, bitlen, blksize, dgstsize, flags) \ |
513 | | PROV_FUNC_DIGEST_GET_PARAM(name, blksize, dgstsize, flags) \ |
514 | | const OSSL_DISPATCH ossl_##name##_functions[] = { \ |
515 | | { OSSL_FUNC_DIGEST_NEWCTX, (void (*)(void))name##_newctx }, \ |
516 | | { OSSL_FUNC_DIGEST_UPDATE, (void (*)(void))keccak_update }, \ |
517 | | { OSSL_FUNC_DIGEST_FINAL, (void (*)(void))keccak_final }, \ |
518 | | { OSSL_FUNC_DIGEST_FREECTX, (void (*)(void))keccak_freectx }, \ |
519 | | { OSSL_FUNC_DIGEST_DUPCTX, (void (*)(void))keccak_dupctx }, \ |
520 | | PROV_DISPATCH_FUNC_DIGEST_GET_PARAMS(name) |
521 | | |
522 | | #define PROV_FUNC_SHA3_DIGEST(name, bitlen, blksize, dgstsize, flags) \ |
523 | | PROV_FUNC_SHA3_DIGEST_COMMON(name, bitlen, blksize, dgstsize, flags), \ |
524 | | { OSSL_FUNC_DIGEST_INIT, (void (*)(void))keccak_init }, \ |
525 | | PROV_DISPATCH_FUNC_DIGEST_CONSTRUCT_END |
526 | | |
527 | | #define PROV_FUNC_SHAKE_DIGEST(name, bitlen, blksize, dgstsize, flags) \ |
528 | | PROV_FUNC_SHA3_DIGEST_COMMON(name, bitlen, blksize, dgstsize, flags), \ |
529 | | { OSSL_FUNC_DIGEST_SQUEEZE, (void (*)(void))shake_squeeze }, \ |
530 | | { OSSL_FUNC_DIGEST_INIT, (void (*)(void))keccak_init_params }, \ |
531 | | { OSSL_FUNC_DIGEST_SET_CTX_PARAMS, (void (*)(void))shake_set_ctx_params }, \ |
532 | | { OSSL_FUNC_DIGEST_SETTABLE_CTX_PARAMS, \ |
533 | | (void (*)(void))shake_settable_ctx_params }, \ |
534 | | PROV_DISPATCH_FUNC_DIGEST_CONSTRUCT_END |
535 | | |
536 | | static void keccak_freectx(void *vctx) |
537 | 57 | { |
538 | 57 | KECCAK1600_CTX *ctx = (KECCAK1600_CTX *)vctx; |
539 | | |
540 | 57 | OPENSSL_clear_free(ctx, sizeof(*ctx)); |
541 | 57 | } |
542 | | |
543 | | static void *keccak_dupctx(void *ctx) |
544 | 0 | { |
545 | 0 | KECCAK1600_CTX *in = (KECCAK1600_CTX *)ctx; |
546 | 0 | KECCAK1600_CTX *ret = ossl_prov_is_running() ? OPENSSL_malloc(sizeof(*ret)) |
547 | 0 | : NULL; |
548 | |
|
549 | 0 | if (ret != NULL) |
550 | 0 | *ret = *in; |
551 | 0 | return ret; |
552 | 0 | } |
553 | | |
554 | | static const OSSL_PARAM known_shake_settable_ctx_params[] = { |
555 | | {OSSL_DIGEST_PARAM_XOFLEN, OSSL_PARAM_UNSIGNED_INTEGER, NULL, 0, 0}, |
556 | | OSSL_PARAM_END |
557 | | }; |
558 | | static const OSSL_PARAM *shake_settable_ctx_params(ossl_unused void *ctx, |
559 | | ossl_unused void *provctx) |
560 | 0 | { |
561 | 0 | return known_shake_settable_ctx_params; |
562 | 0 | } |
563 | | |
564 | | static int shake_set_ctx_params(void *vctx, const OSSL_PARAM params[]) |
565 | 0 | { |
566 | 0 | const OSSL_PARAM *p; |
567 | 0 | KECCAK1600_CTX *ctx = (KECCAK1600_CTX *)vctx; |
568 | |
|
569 | 0 | if (ctx == NULL) |
570 | 0 | return 0; |
571 | 0 | if (params == NULL) |
572 | 0 | return 1; |
573 | | |
574 | 0 | p = OSSL_PARAM_locate_const(params, OSSL_DIGEST_PARAM_XOFLEN); |
575 | 0 | if (p != NULL && !OSSL_PARAM_get_size_t(p, &ctx->md_size)) { |
576 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); |
577 | 0 | return 0; |
578 | 0 | } |
579 | 0 | return 1; |
580 | 0 | } |
581 | | |
582 | | #define IMPLEMENT_SHA3_functions(bitlen) \ |
583 | | SHA3_newctx(sha3, SHA3_##bitlen, sha3_##bitlen, bitlen, '\x06') \ |
584 | | PROV_FUNC_SHA3_DIGEST(sha3_##bitlen, bitlen, \ |
585 | | SHA3_BLOCKSIZE(bitlen), SHA3_MDSIZE(bitlen), \ |
586 | | SHA3_FLAGS) |
587 | | |
588 | | #define IMPLEMENT_KECCAK_functions(bitlen) \ |
589 | | SHA3_newctx(keccak, KECCAK_##bitlen, keccak_##bitlen, bitlen, '\x01') \ |
590 | | PROV_FUNC_SHA3_DIGEST(keccak_##bitlen, bitlen, \ |
591 | | SHA3_BLOCKSIZE(bitlen), SHA3_MDSIZE(bitlen), \ |
592 | | SHA3_FLAGS) |
593 | | |
594 | | #define IMPLEMENT_SHAKE_functions(bitlen) \ |
595 | | SHAKE_newctx(shake, SHAKE_##bitlen, shake_##bitlen, bitlen, \ |
596 | | 0 /* no default md length */, '\x1f') \ |
597 | | PROV_FUNC_SHAKE_DIGEST(shake_##bitlen, bitlen, \ |
598 | | SHA3_BLOCKSIZE(bitlen), 0, \ |
599 | | SHAKE_FLAGS) |
600 | | |
601 | | #define IMPLEMENT_KMAC_functions(bitlen) \ |
602 | | KMAC_newctx(keccak_kmac_##bitlen, bitlen, '\x04') \ |
603 | | PROV_FUNC_SHAKE_DIGEST(keccak_kmac_##bitlen, bitlen, \ |
604 | | SHA3_BLOCKSIZE(bitlen), KMAC_MDSIZE(bitlen), \ |
605 | | KMAC_FLAGS) |
606 | | |
607 | | /* ossl_sha3_224_functions */ |
608 | | IMPLEMENT_SHA3_functions(224) |
609 | | /* ossl_sha3_256_functions */ |
610 | | IMPLEMENT_SHA3_functions(256) |
611 | | /* ossl_sha3_384_functions */ |
612 | | IMPLEMENT_SHA3_functions(384) |
613 | | /* ossl_sha3_512_functions */ |
614 | | IMPLEMENT_SHA3_functions(512) |
615 | | /* ossl_keccak_224_functions */ |
616 | | IMPLEMENT_KECCAK_functions(224) |
617 | | /* ossl_keccak_256_functions */ |
618 | | IMPLEMENT_KECCAK_functions(256) |
619 | | /* ossl_keccak_384_functions */ |
620 | | IMPLEMENT_KECCAK_functions(384) |
621 | | /* ossl_keccak_512_functions */ |
622 | | IMPLEMENT_KECCAK_functions(512) |
623 | | /* ossl_shake_128_functions */ |
624 | | IMPLEMENT_SHAKE_functions(128) |
625 | | /* ossl_shake_256_functions */ |
626 | | IMPLEMENT_SHAKE_functions(256) |
627 | | /* ossl_keccak_kmac_128_functions */ |
628 | | IMPLEMENT_KMAC_functions(128) |
629 | | /* ossl_keccak_kmac_256_functions */ |
630 | | IMPLEMENT_KMAC_functions(256) |