/src/openssl32/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/sha3.h" |
18 | | #include "prov/digestcommon.h" |
19 | | #include "prov/implementations.h" |
20 | | |
21 | | #define SHA3_FLAGS PROV_DIGEST_FLAG_ALGID_ABSENT |
22 | | #define SHAKE_FLAGS (PROV_DIGEST_FLAG_XOF | PROV_DIGEST_FLAG_ALGID_ABSENT) |
23 | | #define KMAC_FLAGS PROV_DIGEST_FLAG_XOF |
24 | | |
25 | | /* |
26 | | * Forward declaration of any unique methods implemented here. This is not strictly |
27 | | * necessary for the compiler, but provides an assurance that the signatures |
28 | | * of the functions in the dispatch table are correct. |
29 | | */ |
30 | | static OSSL_FUNC_digest_init_fn keccak_init; |
31 | | static OSSL_FUNC_digest_init_fn keccak_init_params; |
32 | | static OSSL_FUNC_digest_update_fn keccak_update; |
33 | | static OSSL_FUNC_digest_final_fn keccak_final; |
34 | | static OSSL_FUNC_digest_freectx_fn keccak_freectx; |
35 | | static OSSL_FUNC_digest_dupctx_fn keccak_dupctx; |
36 | | static OSSL_FUNC_digest_set_ctx_params_fn shake_set_ctx_params; |
37 | | static OSSL_FUNC_digest_settable_ctx_params_fn shake_settable_ctx_params; |
38 | | static sha3_absorb_fn generic_sha3_absorb; |
39 | | static sha3_final_fn generic_sha3_final; |
40 | | |
41 | | #if defined(OPENSSL_CPUID_OBJ) && defined(__s390__) && defined(KECCAK1600_ASM) |
42 | | /* |
43 | | * IBM S390X support |
44 | | */ |
45 | | # include "s390x_arch.h" |
46 | | # define S390_SHA3 1 |
47 | | # define S390_SHA3_CAPABLE(name) \ |
48 | | ((OPENSSL_s390xcap_P.kimd[0] & S390X_CAPBIT(S390X_##name)) && \ |
49 | | (OPENSSL_s390xcap_P.klmd[0] & S390X_CAPBIT(S390X_##name))) |
50 | | |
51 | | #endif |
52 | | |
53 | | static int keccak_init(void *vctx, ossl_unused const OSSL_PARAM params[]) |
54 | 161M | { |
55 | 161M | if (!ossl_prov_is_running()) |
56 | 0 | return 0; |
57 | | /* The newctx() handles most of the ctx fixed setup. */ |
58 | 161M | ossl_sha3_reset((KECCAK1600_CTX *)vctx); |
59 | 161M | return 1; |
60 | 161M | } |
61 | | |
62 | | static int keccak_init_params(void *vctx, const OSSL_PARAM params[]) |
63 | 161M | { |
64 | 161M | return keccak_init(vctx, NULL) |
65 | 161M | && shake_set_ctx_params(vctx, params); |
66 | 161M | } |
67 | | |
68 | | static int keccak_update(void *vctx, const unsigned char *inp, size_t len) |
69 | 494M | { |
70 | 494M | KECCAK1600_CTX *ctx = vctx; |
71 | 494M | const size_t bsz = ctx->block_size; |
72 | 494M | size_t num, rem; |
73 | | |
74 | 494M | if (len == 0) |
75 | 0 | return 1; |
76 | | |
77 | | /* Is there anything in the buffer already ? */ |
78 | 494M | if ((num = ctx->bufsz) != 0) { |
79 | | /* Calculate how much space is left in the buffer */ |
80 | 333M | rem = bsz - num; |
81 | | /* If the new input does not fill the buffer then just add it */ |
82 | 333M | if (len < rem) { |
83 | 333M | memcpy(ctx->buf + num, inp, len); |
84 | 333M | ctx->bufsz += len; |
85 | 333M | return 1; |
86 | 333M | } |
87 | | /* otherwise fill up the buffer and absorb the buffer */ |
88 | 180k | memcpy(ctx->buf + num, inp, rem); |
89 | | /* Update the input pointer */ |
90 | 180k | inp += rem; |
91 | 180k | len -= rem; |
92 | 180k | ctx->meth.absorb(ctx, ctx->buf, bsz); |
93 | 180k | ctx->bufsz = 0; |
94 | 180k | } |
95 | | /* Absorb the input - rem = leftover part of the input < blocksize) */ |
96 | 161M | rem = ctx->meth.absorb(ctx, inp, len); |
97 | | /* Copy the leftover bit of the input into the buffer */ |
98 | 161M | if (rem) { |
99 | 161M | memcpy(ctx->buf, inp + len - rem, rem); |
100 | 161M | ctx->bufsz = rem; |
101 | 161M | } |
102 | 161M | return 1; |
103 | 494M | } |
104 | | |
105 | | static int keccak_final(void *vctx, unsigned char *out, size_t *outl, |
106 | | size_t outsz) |
107 | 221k | { |
108 | 221k | int ret = 1; |
109 | 221k | KECCAK1600_CTX *ctx = vctx; |
110 | | |
111 | 221k | if (!ossl_prov_is_running()) |
112 | 0 | return 0; |
113 | 221k | if (outsz > 0) |
114 | 221k | ret = ctx->meth.final(out, ctx); |
115 | | |
116 | 221k | *outl = ctx->md_size; |
117 | 221k | return ret; |
118 | 221k | } |
119 | | |
120 | | /*- |
121 | | * Generic software version of the absorb() and final(). |
122 | | */ |
123 | | static size_t generic_sha3_absorb(void *vctx, const void *inp, size_t len) |
124 | 221k | { |
125 | 221k | KECCAK1600_CTX *ctx = vctx; |
126 | | |
127 | 221k | return SHA3_absorb(ctx->A, inp, len, ctx->block_size); |
128 | 221k | } |
129 | | |
130 | | static int generic_sha3_final(unsigned char *md, void *vctx) |
131 | 161M | { |
132 | 161M | return ossl_sha3_final(md, (KECCAK1600_CTX *)vctx); |
133 | 161M | } |
134 | | |
135 | | static PROV_SHA3_METHOD sha3_generic_md = |
136 | | { |
137 | | generic_sha3_absorb, |
138 | | generic_sha3_final |
139 | | }; |
140 | | |
141 | | #if defined(S390_SHA3) |
142 | | |
143 | | static sha3_absorb_fn s390x_sha3_absorb; |
144 | | static sha3_final_fn s390x_sha3_final; |
145 | | static sha3_final_fn s390x_shake_final; |
146 | | |
147 | | /*- |
148 | | * The platform specific parts of the absorb() and final() for S390X. |
149 | | */ |
150 | | static size_t s390x_sha3_absorb(void *vctx, const void *inp, size_t len) |
151 | | { |
152 | | KECCAK1600_CTX *ctx = vctx; |
153 | | size_t rem = len % ctx->block_size; |
154 | | |
155 | | s390x_kimd(inp, len - rem, ctx->pad, ctx->A); |
156 | | return rem; |
157 | | } |
158 | | |
159 | | static int s390x_sha3_final(unsigned char *md, void *vctx) |
160 | | { |
161 | | KECCAK1600_CTX *ctx = vctx; |
162 | | |
163 | | if (!ossl_prov_is_running()) |
164 | | return 0; |
165 | | s390x_klmd(ctx->buf, ctx->bufsz, NULL, 0, ctx->pad, ctx->A); |
166 | | memcpy(md, ctx->A, ctx->md_size); |
167 | | return 1; |
168 | | } |
169 | | |
170 | | static int s390x_shake_final(unsigned char *md, void *vctx) |
171 | | { |
172 | | KECCAK1600_CTX *ctx = vctx; |
173 | | |
174 | | if (!ossl_prov_is_running()) |
175 | | return 0; |
176 | | s390x_klmd(ctx->buf, ctx->bufsz, md, ctx->md_size, ctx->pad, ctx->A); |
177 | | return 1; |
178 | | } |
179 | | |
180 | | static int s390x_keccakc_final(unsigned char *md, void *vctx, int padding) |
181 | | { |
182 | | KECCAK1600_CTX *ctx = vctx; |
183 | | size_t bsz = ctx->block_size; |
184 | | size_t num = ctx->bufsz; |
185 | | size_t needed = ctx->md_size; |
186 | | |
187 | | if (!ossl_prov_is_running()) |
188 | | return 0; |
189 | | if (ctx->md_size == 0) |
190 | | return 1; |
191 | | memset(ctx->buf + num, 0, bsz - num); |
192 | | ctx->buf[num] = padding; |
193 | | ctx->buf[bsz - 1] |= 0x80; |
194 | | s390x_kimd(ctx->buf, bsz, ctx->pad, ctx->A); |
195 | | num = needed > bsz ? bsz : needed; |
196 | | memcpy(md, ctx->A, num); |
197 | | needed -= num; |
198 | | if (needed > 0) |
199 | | s390x_klmd(NULL, 0, md + bsz, needed, ctx->pad | S390X_KLMD_PS, ctx->A); |
200 | | |
201 | | return 1; |
202 | | } |
203 | | |
204 | | static int s390x_keccak_final(unsigned char *md, void *vctx) |
205 | | { |
206 | | return s390x_keccakc_final(md, vctx, 0x01); |
207 | | } |
208 | | |
209 | | static int s390x_kmac_final(unsigned char *md, void *vctx) |
210 | | { |
211 | | return s390x_keccakc_final(md, vctx, 0x04); |
212 | | } |
213 | | |
214 | | static PROV_SHA3_METHOD sha3_s390x_md = |
215 | | { |
216 | | s390x_sha3_absorb, |
217 | | s390x_sha3_final |
218 | | }; |
219 | | |
220 | | static PROV_SHA3_METHOD keccak_s390x_md = |
221 | | { |
222 | | s390x_sha3_absorb, |
223 | | s390x_keccak_final |
224 | | }; |
225 | | |
226 | | static PROV_SHA3_METHOD shake_s390x_md = |
227 | | { |
228 | | s390x_sha3_absorb, |
229 | | s390x_shake_final |
230 | | }; |
231 | | |
232 | | static PROV_SHA3_METHOD kmac_s390x_md = |
233 | | { |
234 | | s390x_sha3_absorb, |
235 | | s390x_kmac_final |
236 | | }; |
237 | | |
238 | | # define SHA3_SET_MD(uname, typ) \ |
239 | | if (S390_SHA3_CAPABLE(uname)) { \ |
240 | | ctx->pad = S390X_##uname; \ |
241 | | ctx->meth = typ##_s390x_md; \ |
242 | | } else { \ |
243 | | ctx->meth = sha3_generic_md; \ |
244 | | } |
245 | | # define KMAC_SET_MD(bitlen) \ |
246 | | if (S390_SHA3_CAPABLE(SHAKE_##bitlen)) { \ |
247 | | ctx->pad = S390X_SHAKE_##bitlen; \ |
248 | | ctx->meth = kmac_s390x_md; \ |
249 | | } else { \ |
250 | | ctx->meth = sha3_generic_md; \ |
251 | | } |
252 | | #elif defined(__aarch64__) && defined(KECCAK1600_ASM) |
253 | | # include "arm_arch.h" |
254 | | |
255 | | static sha3_absorb_fn armsha3_sha3_absorb; |
256 | | |
257 | | size_t SHA3_absorb_cext(uint64_t A[5][5], const unsigned char *inp, size_t len, |
258 | | size_t r); |
259 | | /*- |
260 | | * Hardware-assisted ARMv8.2 SHA3 extension version of the absorb() |
261 | | */ |
262 | | static size_t armsha3_sha3_absorb(void *vctx, const void *inp, size_t len) |
263 | | { |
264 | | KECCAK1600_CTX *ctx = vctx; |
265 | | |
266 | | return SHA3_absorb_cext(ctx->A, inp, len, ctx->block_size); |
267 | | } |
268 | | |
269 | | static PROV_SHA3_METHOD sha3_ARMSHA3_md = |
270 | | { |
271 | | armsha3_sha3_absorb, |
272 | | generic_sha3_final |
273 | | }; |
274 | | # define SHA3_SET_MD(uname, typ) \ |
275 | | if (OPENSSL_armcap_P & ARMV8_HAVE_SHA3_AND_WORTH_USING) { \ |
276 | | ctx->meth = sha3_ARMSHA3_md; \ |
277 | | } else { \ |
278 | | ctx->meth = sha3_generic_md; \ |
279 | | } |
280 | | # define KMAC_SET_MD(bitlen) \ |
281 | | if (OPENSSL_armcap_P & ARMV8_HAVE_SHA3_AND_WORTH_USING) { \ |
282 | | ctx->meth = sha3_ARMSHA3_md; \ |
283 | | } else { \ |
284 | | ctx->meth = sha3_generic_md; \ |
285 | | } |
286 | | #else |
287 | 203k | # define SHA3_SET_MD(uname, typ) ctx->meth = sha3_generic_md; |
288 | 88 | # define KMAC_SET_MD(bitlen) ctx->meth = sha3_generic_md; |
289 | | #endif /* S390_SHA3 */ |
290 | | |
291 | | #define SHA3_newctx(typ, uname, name, bitlen, pad) \ |
292 | | static OSSL_FUNC_digest_newctx_fn name##_newctx; \ |
293 | 203k | static void *name##_newctx(void *provctx) \ |
294 | 203k | { \ |
295 | 203k | KECCAK1600_CTX *ctx = ossl_prov_is_running() ? OPENSSL_zalloc(sizeof(*ctx)) \ |
296 | 203k | : NULL; \ |
297 | 203k | \ |
298 | 203k | if (ctx == NULL) \ |
299 | 203k | return NULL; \ |
300 | 203k | ossl_sha3_init(ctx, pad, bitlen); \ |
301 | 203k | SHA3_SET_MD(uname, typ) \ |
302 | 203k | return ctx; \ |
303 | 203k | } sha3_prov.c:sha3_224_newctx Line | Count | Source | 293 | 51.2k | static void *name##_newctx(void *provctx) \ | 294 | 51.2k | { \ | 295 | 51.2k | KECCAK1600_CTX *ctx = ossl_prov_is_running() ? OPENSSL_zalloc(sizeof(*ctx)) \ | 296 | 51.2k | : NULL; \ | 297 | 51.2k | \ | 298 | 51.2k | if (ctx == NULL) \ | 299 | 51.2k | return NULL; \ | 300 | 51.2k | ossl_sha3_init(ctx, pad, bitlen); \ | 301 | 51.2k | SHA3_SET_MD(uname, typ) \ | 302 | 51.2k | return ctx; \ | 303 | 51.2k | } |
sha3_prov.c:sha3_256_newctx Line | Count | Source | 293 | 47.1k | static void *name##_newctx(void *provctx) \ | 294 | 47.1k | { \ | 295 | 47.1k | KECCAK1600_CTX *ctx = ossl_prov_is_running() ? OPENSSL_zalloc(sizeof(*ctx)) \ | 296 | 47.1k | : NULL; \ | 297 | 47.1k | \ | 298 | 47.1k | if (ctx == NULL) \ | 299 | 47.1k | return NULL; \ | 300 | 47.1k | ossl_sha3_init(ctx, pad, bitlen); \ | 301 | 47.1k | SHA3_SET_MD(uname, typ) \ | 302 | 47.1k | return ctx; \ | 303 | 47.1k | } |
sha3_prov.c:sha3_384_newctx Line | Count | Source | 293 | 8.97k | static void *name##_newctx(void *provctx) \ | 294 | 8.97k | { \ | 295 | 8.97k | KECCAK1600_CTX *ctx = ossl_prov_is_running() ? OPENSSL_zalloc(sizeof(*ctx)) \ | 296 | 8.97k | : NULL; \ | 297 | 8.97k | \ | 298 | 8.97k | if (ctx == NULL) \ | 299 | 8.97k | return NULL; \ | 300 | 8.97k | ossl_sha3_init(ctx, pad, bitlen); \ | 301 | 8.97k | SHA3_SET_MD(uname, typ) \ | 302 | 8.97k | return ctx; \ | 303 | 8.97k | } |
sha3_prov.c:sha3_512_newctx Line | Count | Source | 293 | 23.5k | static void *name##_newctx(void *provctx) \ | 294 | 23.5k | { \ | 295 | 23.5k | KECCAK1600_CTX *ctx = ossl_prov_is_running() ? OPENSSL_zalloc(sizeof(*ctx)) \ | 296 | 23.5k | : NULL; \ | 297 | 23.5k | \ | 298 | 23.5k | if (ctx == NULL) \ | 299 | 23.5k | return NULL; \ | 300 | 23.5k | ossl_sha3_init(ctx, pad, bitlen); \ | 301 | 23.5k | SHA3_SET_MD(uname, typ) \ | 302 | 23.5k | return ctx; \ | 303 | 23.5k | } |
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 sha3_prov.c:shake_128_newctx Line | Count | Source | 293 | 43.7k | static void *name##_newctx(void *provctx) \ | 294 | 43.7k | { \ | 295 | 43.7k | KECCAK1600_CTX *ctx = ossl_prov_is_running() ? OPENSSL_zalloc(sizeof(*ctx)) \ | 296 | 43.7k | : NULL; \ | 297 | 43.7k | \ | 298 | 43.7k | if (ctx == NULL) \ | 299 | 43.7k | return NULL; \ | 300 | 43.7k | ossl_sha3_init(ctx, pad, bitlen); \ | 301 | 43.7k | SHA3_SET_MD(uname, typ) \ | 302 | 43.7k | return ctx; \ | 303 | 43.7k | } |
sha3_prov.c:shake_256_newctx Line | Count | Source | 293 | 29.1k | static void *name##_newctx(void *provctx) \ | 294 | 29.1k | { \ | 295 | 29.1k | KECCAK1600_CTX *ctx = ossl_prov_is_running() ? OPENSSL_zalloc(sizeof(*ctx)) \ | 296 | 29.1k | : NULL; \ | 297 | 29.1k | \ | 298 | 29.1k | if (ctx == NULL) \ | 299 | 29.1k | return NULL; \ | 300 | 29.1k | ossl_sha3_init(ctx, pad, bitlen); \ | 301 | 29.1k | SHA3_SET_MD(uname, typ) \ | 302 | 29.1k | return ctx; \ | 303 | 29.1k | } |
|
304 | | |
305 | | #define KMAC_newctx(uname, bitlen, pad) \ |
306 | | static OSSL_FUNC_digest_newctx_fn uname##_newctx; \ |
307 | 88 | static void *uname##_newctx(void *provctx) \ |
308 | 88 | { \ |
309 | 88 | KECCAK1600_CTX *ctx = ossl_prov_is_running() ? OPENSSL_zalloc(sizeof(*ctx)) \ |
310 | 88 | : NULL; \ |
311 | 88 | \ |
312 | 88 | if (ctx == NULL) \ |
313 | 88 | return NULL; \ |
314 | 88 | ossl_keccak_kmac_init(ctx, pad, bitlen); \ |
315 | 88 | KMAC_SET_MD(bitlen) \ |
316 | 88 | return ctx; \ |
317 | 88 | } sha3_prov.c:keccak_kmac_128_newctx Line | Count | Source | 307 | 17 | static void *uname##_newctx(void *provctx) \ | 308 | 17 | { \ | 309 | 17 | KECCAK1600_CTX *ctx = ossl_prov_is_running() ? OPENSSL_zalloc(sizeof(*ctx)) \ | 310 | 17 | : NULL; \ | 311 | 17 | \ | 312 | 17 | if (ctx == NULL) \ | 313 | 17 | return NULL; \ | 314 | 17 | ossl_keccak_kmac_init(ctx, pad, bitlen); \ | 315 | 17 | KMAC_SET_MD(bitlen) \ | 316 | 17 | return ctx; \ | 317 | 17 | } |
sha3_prov.c:keccak_kmac_256_newctx Line | Count | Source | 307 | 71 | static void *uname##_newctx(void *provctx) \ | 308 | 71 | { \ | 309 | 71 | KECCAK1600_CTX *ctx = ossl_prov_is_running() ? OPENSSL_zalloc(sizeof(*ctx)) \ | 310 | 71 | : NULL; \ | 311 | 71 | \ | 312 | 71 | if (ctx == NULL) \ | 313 | 71 | return NULL; \ | 314 | 71 | ossl_keccak_kmac_init(ctx, pad, bitlen); \ | 315 | 71 | KMAC_SET_MD(bitlen) \ | 316 | 71 | return ctx; \ | 317 | 71 | } |
|
318 | | |
319 | | #define PROV_FUNC_SHA3_DIGEST_COMMON(name, bitlen, blksize, dgstsize, flags) \ |
320 | | PROV_FUNC_DIGEST_GET_PARAM(name, blksize, dgstsize, flags) \ |
321 | | const OSSL_DISPATCH ossl_##name##_functions[] = { \ |
322 | | { OSSL_FUNC_DIGEST_NEWCTX, (void (*)(void))name##_newctx }, \ |
323 | | { OSSL_FUNC_DIGEST_UPDATE, (void (*)(void))keccak_update }, \ |
324 | | { OSSL_FUNC_DIGEST_FINAL, (void (*)(void))keccak_final }, \ |
325 | | { OSSL_FUNC_DIGEST_FREECTX, (void (*)(void))keccak_freectx }, \ |
326 | | { OSSL_FUNC_DIGEST_DUPCTX, (void (*)(void))keccak_dupctx }, \ |
327 | | PROV_DISPATCH_FUNC_DIGEST_GET_PARAMS(name) |
328 | | |
329 | | #define PROV_FUNC_SHA3_DIGEST(name, bitlen, blksize, dgstsize, flags) \ |
330 | | PROV_FUNC_SHA3_DIGEST_COMMON(name, bitlen, blksize, dgstsize, flags), \ |
331 | | { OSSL_FUNC_DIGEST_INIT, (void (*)(void))keccak_init }, \ |
332 | | PROV_DISPATCH_FUNC_DIGEST_CONSTRUCT_END |
333 | | |
334 | | #define PROV_FUNC_SHAKE_DIGEST(name, bitlen, blksize, dgstsize, flags) \ |
335 | | PROV_FUNC_SHA3_DIGEST_COMMON(name, bitlen, blksize, dgstsize, flags), \ |
336 | | { OSSL_FUNC_DIGEST_INIT, (void (*)(void))keccak_init_params }, \ |
337 | | { OSSL_FUNC_DIGEST_SET_CTX_PARAMS, (void (*)(void))shake_set_ctx_params }, \ |
338 | | { OSSL_FUNC_DIGEST_SETTABLE_CTX_PARAMS, \ |
339 | | (void (*)(void))shake_settable_ctx_params }, \ |
340 | | PROV_DISPATCH_FUNC_DIGEST_CONSTRUCT_END |
341 | | |
342 | | static void keccak_freectx(void *vctx) |
343 | 243k | { |
344 | 243k | KECCAK1600_CTX *ctx = (KECCAK1600_CTX *)vctx; |
345 | | |
346 | 243k | OPENSSL_clear_free(ctx, sizeof(*ctx)); |
347 | 243k | } |
348 | | |
349 | | static void *keccak_dupctx(void *ctx) |
350 | 301 | { |
351 | 301 | KECCAK1600_CTX *in = (KECCAK1600_CTX *)ctx; |
352 | 301 | KECCAK1600_CTX *ret = ossl_prov_is_running() ? OPENSSL_malloc(sizeof(*ret)) |
353 | 301 | : NULL; |
354 | | |
355 | 301 | if (ret != NULL) |
356 | 301 | *ret = *in; |
357 | 301 | return ret; |
358 | 301 | } |
359 | | |
360 | | static const OSSL_PARAM known_shake_settable_ctx_params[] = { |
361 | | {OSSL_DIGEST_PARAM_XOFLEN, OSSL_PARAM_UNSIGNED_INTEGER, NULL, 0, 0}, |
362 | | OSSL_PARAM_END |
363 | | }; |
364 | | static const OSSL_PARAM *shake_settable_ctx_params(ossl_unused void *ctx, |
365 | | ossl_unused void *provctx) |
366 | 3 | { |
367 | 3 | return known_shake_settable_ctx_params; |
368 | 3 | } |
369 | | |
370 | | static int shake_set_ctx_params(void *vctx, const OSSL_PARAM params[]) |
371 | 96.0k | { |
372 | 96.0k | const OSSL_PARAM *p; |
373 | 96.0k | KECCAK1600_CTX *ctx = (KECCAK1600_CTX *)vctx; |
374 | | |
375 | 96.0k | if (ctx == NULL) |
376 | 0 | return 0; |
377 | 96.0k | if (params == NULL) |
378 | 96.0k | return 1; |
379 | | |
380 | 78 | p = OSSL_PARAM_locate_const(params, OSSL_DIGEST_PARAM_XOFLEN); |
381 | 78 | if (p != NULL && !OSSL_PARAM_get_size_t(p, &ctx->md_size)) { |
382 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); |
383 | 0 | return 0; |
384 | 0 | } |
385 | 78 | return 1; |
386 | 78 | } |
387 | | |
388 | | #define IMPLEMENT_SHA3_functions(bitlen) \ |
389 | | SHA3_newctx(sha3, SHA3_##bitlen, sha3_##bitlen, bitlen, '\x06') \ |
390 | | PROV_FUNC_SHA3_DIGEST(sha3_##bitlen, bitlen, \ |
391 | | SHA3_BLOCKSIZE(bitlen), SHA3_MDSIZE(bitlen), \ |
392 | | SHA3_FLAGS) |
393 | | |
394 | | #define IMPLEMENT_KECCAK_functions(bitlen) \ |
395 | | SHA3_newctx(keccak, KECCAK_##bitlen, keccak_##bitlen, bitlen, '\x01') \ |
396 | | PROV_FUNC_SHA3_DIGEST(keccak_##bitlen, bitlen, \ |
397 | | SHA3_BLOCKSIZE(bitlen), SHA3_MDSIZE(bitlen), \ |
398 | | SHA3_FLAGS) |
399 | | |
400 | | #define IMPLEMENT_SHAKE_functions(bitlen) \ |
401 | | SHA3_newctx(shake, SHAKE_##bitlen, shake_##bitlen, bitlen, '\x1f') \ |
402 | | PROV_FUNC_SHAKE_DIGEST(shake_##bitlen, bitlen, \ |
403 | | SHA3_BLOCKSIZE(bitlen), SHA3_MDSIZE(bitlen), \ |
404 | | SHAKE_FLAGS) |
405 | | #define IMPLEMENT_KMAC_functions(bitlen) \ |
406 | | KMAC_newctx(keccak_kmac_##bitlen, bitlen, '\x04') \ |
407 | | PROV_FUNC_SHAKE_DIGEST(keccak_kmac_##bitlen, bitlen, \ |
408 | | SHA3_BLOCKSIZE(bitlen), KMAC_MDSIZE(bitlen), \ |
409 | | KMAC_FLAGS) |
410 | | |
411 | | /* ossl_sha3_224_functions */ |
412 | | IMPLEMENT_SHA3_functions(224) |
413 | | /* ossl_sha3_256_functions */ |
414 | | IMPLEMENT_SHA3_functions(256) |
415 | | /* ossl_sha3_384_functions */ |
416 | | IMPLEMENT_SHA3_functions(384) |
417 | | /* ossl_sha3_512_functions */ |
418 | | IMPLEMENT_SHA3_functions(512) |
419 | | /* ossl_keccak_224_functions */ |
420 | | IMPLEMENT_KECCAK_functions(224) |
421 | | /* ossl_keccak_256_functions */ |
422 | | IMPLEMENT_KECCAK_functions(256) |
423 | | /* ossl_keccak_384_functions */ |
424 | | IMPLEMENT_KECCAK_functions(384) |
425 | | /* ossl_keccak_512_functions */ |
426 | | IMPLEMENT_KECCAK_functions(512) |
427 | | /* ossl_shake_128_functions */ |
428 | | IMPLEMENT_SHAKE_functions(128) |
429 | | /* ossl_shake_256_functions */ |
430 | | IMPLEMENT_SHAKE_functions(256) |
431 | | /* ossl_keccak_kmac_128_functions */ |
432 | | IMPLEMENT_KMAC_functions(128) |
433 | | /* ossl_keccak_kmac_256_functions */ |
434 | | IMPLEMENT_KMAC_functions(256) |