/src/openssl/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 | | |
10 | | #include <string.h> |
11 | | #include <openssl/byteorder.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 | | #include "providers/implementations/digests/sha3_prov.inc" |
25 | | |
26 | | #define SHA3_FLAGS PROV_DIGEST_FLAG_ALGID_ABSENT |
27 | | #define SHAKE_FLAGS (PROV_DIGEST_FLAG_XOF | PROV_DIGEST_FLAG_ALGID_ABSENT) |
28 | | #define CSHAKE_KECCAK_FLAGS PROV_DIGEST_FLAG_XOF |
29 | | |
30 | | /* |
31 | | * FIPS 202 Section 5.1 Specifies a padding mode that is added to the last |
32 | | * block that consists of a 1 bit followed by padding zero bits and a trailing |
33 | | * 1 bit (where the bits are in LSB order) |
34 | | * |
35 | | * For a given input message special algorithm context bits are appended: |
36 | | * i.e. |
37 | | * KECCAK[c] = (No tag is used) |
38 | | * SHA3 = 01 |
39 | | * SHAKE = 1111 |
40 | | * CSHAKE_KECCAK = 00 (See NIST SP800-185 3.3 : i.e. it has 2 trailing zero bits) |
41 | | * Note that KMAC and TupleHash use CSHAKE_KECCAK. |
42 | | * The OpenSSL implementation only allows input messages that are in bytes, |
43 | | * so the above concatenated bits will start on a byte boundary. |
44 | | * Following these bits will be a 1 bit then the padding zeros which gives |
45 | | * |
46 | | * KECCAK[c] = 1000 |
47 | | * SHA3 = 0110 |
48 | | * SHAKE = 11111000 |
49 | | * CSHAKE_KECCAK = 0010 (See NIST SP800-185 3.3 : i.e. KMAC uses cSHAKE with a fixed string) |
50 | | * |
51 | | * Which gives the following padding values as bytes. |
52 | | */ |
53 | | #define KECCAK_PADDING 0x01 |
54 | | #define SHA3_PADDING 0x06 |
55 | | #define SHAKE_PADDING 0x1f |
56 | | #define CSHAKE_KECCAK_PADDING 0x04 |
57 | | |
58 | | #if defined(OPENSSL_CPUID_OBJ) && defined(__s390__) && defined(KECCAK1600_ASM) |
59 | | /* |
60 | | * IBM S390X support |
61 | | */ |
62 | | #include "s390x_arch.h" |
63 | | #define S390_SHA3 1 |
64 | | #define S390_SHA3_CAPABLE(name) \ |
65 | | ((OPENSSL_s390xcap_P.kimd[0] & S390X_CAPBIT(S390X_##name)) && (OPENSSL_s390xcap_P.klmd[0] & S390X_CAPBIT(S390X_##name))) |
66 | | #endif |
67 | | |
68 | | /* |
69 | | * Forward declaration of any unique methods implemented here. This is not strictly |
70 | | * necessary for the compiler, but provides an assurance that the signatures |
71 | | * of the functions in the dispatch table are correct. |
72 | | */ |
73 | | static OSSL_FUNC_digest_init_fn keccak_init; |
74 | | static OSSL_FUNC_digest_init_fn keccak_init_params; |
75 | | static OSSL_FUNC_digest_update_fn keccak_update; |
76 | | static OSSL_FUNC_digest_final_fn keccak_final; |
77 | | static OSSL_FUNC_digest_freectx_fn keccak_freectx; |
78 | | static OSSL_FUNC_digest_copyctx_fn keccak_copyctx; |
79 | | static OSSL_FUNC_digest_dupctx_fn keccak_dupctx; |
80 | | static OSSL_FUNC_digest_squeeze_fn shake_squeeze; |
81 | | |
82 | | static OSSL_FUNC_digest_get_ctx_params_fn shake_get_ctx_params; |
83 | | static OSSL_FUNC_digest_gettable_ctx_params_fn shake_gettable_ctx_params; |
84 | | static OSSL_FUNC_digest_set_ctx_params_fn shake_set_ctx_params; |
85 | | static OSSL_FUNC_digest_settable_ctx_params_fn shake_settable_ctx_params; |
86 | | |
87 | | static PROV_SHA3_METHOD sha3_generic_md = { |
88 | | ossl_sha3_absorb_default, |
89 | | ossl_sha3_final_default, |
90 | | NULL |
91 | | }; |
92 | | |
93 | | static PROV_SHA3_METHOD shake_generic_md = { |
94 | | ossl_sha3_absorb_default, |
95 | | ossl_sha3_final_default, |
96 | | ossl_shake_squeeze_default |
97 | | }; |
98 | | |
99 | | static int keccak_init(void *vctx, ossl_unused const OSSL_PARAM params[]) |
100 | 0 | { |
101 | 0 | if (ossl_unlikely(!ossl_prov_is_running())) |
102 | 0 | return 0; |
103 | | /* The newctx() handles most of the ctx fixed setup. */ |
104 | 0 | ossl_sha3_reset((KECCAK1600_CTX *)vctx); |
105 | 0 | return 1; |
106 | 0 | } |
107 | | |
108 | | static int keccak_init_params(void *vctx, const OSSL_PARAM params[]) |
109 | 0 | { |
110 | 0 | return keccak_init(vctx, NULL) |
111 | 0 | && shake_set_ctx_params(vctx, params); |
112 | 0 | } |
113 | | |
114 | | static int keccak_update(void *vctx, const unsigned char *inp, size_t len) |
115 | 0 | { |
116 | 0 | return ossl_sha3_absorb((KECCAK1600_CTX *)vctx, inp, len); |
117 | 0 | } |
118 | | |
119 | | static int keccak_final(void *vctx, unsigned char *out, size_t *outl, |
120 | | size_t outlen) |
121 | 0 | { |
122 | 0 | int ret = 1; |
123 | 0 | KECCAK1600_CTX *ctx = vctx; |
124 | |
|
125 | 0 | if (ossl_unlikely(!ossl_prov_is_running())) |
126 | 0 | return 0; |
127 | 0 | if (ossl_unlikely(ctx->md_size == SIZE_MAX)) { |
128 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_LENGTH); |
129 | 0 | return 0; |
130 | 0 | } |
131 | 0 | ret = ossl_sha3_final(ctx, out, ctx->md_size); |
132 | 0 | *outl = ctx->md_size; |
133 | 0 | return ret; |
134 | 0 | } |
135 | | |
136 | | static int shake_squeeze(void *vctx, unsigned char *out, size_t *outl, |
137 | | size_t outlen) |
138 | 0 | { |
139 | 0 | int ret = 1; |
140 | 0 | KECCAK1600_CTX *ctx = vctx; |
141 | |
|
142 | 0 | if (!ossl_prov_is_running()) |
143 | 0 | return 0; |
144 | 0 | if (ctx->meth.squeeze == NULL) |
145 | 0 | return 0; |
146 | 0 | if (outlen > 0) |
147 | 0 | ret = ossl_sha3_squeeze(ctx, out, outlen); |
148 | 0 | if (outl != NULL) |
149 | 0 | *outl = outlen; |
150 | 0 | return ret; |
151 | 0 | } |
152 | | |
153 | | #if defined(S390_SHA3) |
154 | | |
155 | | static sha3_absorb_fn s390x_sha3_absorb; |
156 | | static sha3_final_fn s390x_sha3_final; |
157 | | static sha3_final_fn s390x_shake_final; |
158 | | |
159 | | /*- |
160 | | * The platform specific parts of the absorb() and final() for S390X. |
161 | | */ |
162 | | static size_t s390x_sha3_absorb(KECCAK1600_CTX *ctx, const unsigned char *inp, size_t len) |
163 | | { |
164 | | size_t rem = len % ctx->block_size; |
165 | | unsigned int fc; |
166 | | |
167 | | if (len - rem > 0) { |
168 | | fc = ctx->pad; |
169 | | fc |= ctx->xof_state == XOF_STATE_INIT ? S390X_KIMD_NIP : 0; |
170 | | s390x_kimd(inp, len - rem, fc, ctx->A); |
171 | | } |
172 | | return rem; |
173 | | } |
174 | | |
175 | | static int s390x_sha3_final(KECCAK1600_CTX *ctx, unsigned char *out, size_t outlen) |
176 | | { |
177 | | unsigned int fc; |
178 | | |
179 | | fc = ctx->pad | S390X_KLMD_DUFOP; |
180 | | fc |= ctx->xof_state == XOF_STATE_INIT ? S390X_KLMD_NIP : 0; |
181 | | s390x_klmd(ctx->buf, ctx->bufsz, NULL, 0, fc, ctx->A); |
182 | | memcpy(out, ctx->A, outlen); |
183 | | return 1; |
184 | | } |
185 | | |
186 | | static int s390x_shake_final(KECCAK1600_CTX *ctx, unsigned char *out, size_t outlen) |
187 | | { |
188 | | unsigned int fc; |
189 | | |
190 | | fc = ctx->pad | S390X_KLMD_DUFOP; |
191 | | fc |= ctx->xof_state == XOF_STATE_INIT ? S390X_KLMD_NIP : 0; |
192 | | s390x_klmd(ctx->buf, ctx->bufsz, out, outlen, fc, ctx->A); |
193 | | return 1; |
194 | | } |
195 | | |
196 | | static int s390x_shake_squeeze(KECCAK1600_CTX *ctx, unsigned char *out, size_t outlen) |
197 | | { |
198 | | unsigned int fc; |
199 | | size_t len; |
200 | | |
201 | | /* |
202 | | * On the first squeeze call, finish the absorb process (incl. padding). |
203 | | */ |
204 | | if (ctx->xof_state != XOF_STATE_SQUEEZE) { |
205 | | fc = ctx->pad; |
206 | | fc |= ctx->xof_state == XOF_STATE_INIT ? S390X_KLMD_NIP : 0; |
207 | | s390x_klmd(ctx->buf, ctx->bufsz, out, outlen, fc, ctx->A); |
208 | | ctx->bufsz = outlen % ctx->block_size; |
209 | | /* reuse ctx->bufsz to count bytes squeezed from current sponge */ |
210 | | return 1; |
211 | | } |
212 | | if (ctx->bufsz != 0) { |
213 | | len = ctx->block_size - ctx->bufsz; |
214 | | if (outlen < len) |
215 | | len = outlen; |
216 | | memcpy(out, (char *)ctx->A + ctx->bufsz, len); |
217 | | out += len; |
218 | | outlen -= len; |
219 | | ctx->bufsz += len; |
220 | | if (ctx->bufsz == ctx->block_size) |
221 | | ctx->bufsz = 0; |
222 | | } |
223 | | if (outlen == 0) |
224 | | return 1; |
225 | | s390x_klmd(NULL, 0, out, outlen, ctx->pad | S390X_KLMD_PS, ctx->A); |
226 | | ctx->bufsz = outlen % ctx->block_size; |
227 | | |
228 | | return 1; |
229 | | } |
230 | | |
231 | | static int s390x_keccakc_final(KECCAK1600_CTX *ctx, unsigned char *out, size_t outlen, |
232 | | int padding) |
233 | | { |
234 | | size_t bsz = ctx->block_size; |
235 | | size_t num = ctx->bufsz; |
236 | | size_t needed = outlen; |
237 | | unsigned int fc; |
238 | | |
239 | | fc = ctx->pad; |
240 | | fc |= ctx->xof_state == XOF_STATE_INIT ? S390X_KIMD_NIP : 0; |
241 | | if (outlen == 0) |
242 | | return 1; |
243 | | memset(ctx->buf + num, 0, bsz - num); |
244 | | ctx->buf[num] = padding; |
245 | | ctx->buf[bsz - 1] |= 0x80; |
246 | | s390x_kimd(ctx->buf, bsz, fc, ctx->A); |
247 | | num = needed > bsz ? bsz : needed; |
248 | | memcpy(out, ctx->A, num); |
249 | | needed -= num; |
250 | | if (needed > 0) |
251 | | s390x_klmd(NULL, 0, out + bsz, needed, |
252 | | ctx->pad | S390X_KLMD_PS | S390X_KLMD_DUFOP, ctx->A); |
253 | | |
254 | | return 1; |
255 | | } |
256 | | |
257 | | static int s390x_keccak_final(KECCAK1600_CTX *ctx, unsigned char *out, size_t outlen) |
258 | | { |
259 | | return s390x_keccakc_final(ctx, out, outlen, 0x01); |
260 | | } |
261 | | |
262 | | static int s390x_cshake_keccak_final(KECCAK1600_CTX *ctx, unsigned char *out, size_t outlen) |
263 | | { |
264 | | return s390x_keccakc_final(ctx, out, outlen, 0x04); |
265 | | } |
266 | | |
267 | | static int s390x_keccakc_squeeze(KECCAK1600_CTX *ctx, unsigned char *out, size_t outlen, |
268 | | int padding) |
269 | | { |
270 | | size_t len; |
271 | | unsigned int fc; |
272 | | |
273 | | /* |
274 | | * On the first squeeze call, finish the absorb process |
275 | | * by adding the trailing padding and then doing |
276 | | * a final absorb. |
277 | | */ |
278 | | if (ctx->xof_state != XOF_STATE_SQUEEZE) { |
279 | | len = ctx->block_size - ctx->bufsz; |
280 | | memset(ctx->buf + ctx->bufsz, 0, len); |
281 | | ctx->buf[ctx->bufsz] = padding; |
282 | | ctx->buf[ctx->block_size - 1] |= 0x80; |
283 | | fc = ctx->pad; |
284 | | fc |= ctx->xof_state == XOF_STATE_INIT ? S390X_KIMD_NIP : 0; |
285 | | s390x_kimd(ctx->buf, ctx->block_size, fc, ctx->A); |
286 | | ctx->bufsz = 0; |
287 | | /* reuse ctx->bufsz to count bytes squeezed from current sponge */ |
288 | | } |
289 | | if (ctx->bufsz != 0 || ctx->xof_state != XOF_STATE_SQUEEZE) { |
290 | | len = ctx->block_size - ctx->bufsz; |
291 | | if (outlen < len) |
292 | | len = outlen; |
293 | | memcpy(out, (char *)ctx->A + ctx->bufsz, len); |
294 | | out += len; |
295 | | outlen -= len; |
296 | | ctx->bufsz += len; |
297 | | if (ctx->bufsz == ctx->block_size) |
298 | | ctx->bufsz = 0; |
299 | | } |
300 | | if (outlen == 0) |
301 | | return 1; |
302 | | s390x_klmd(NULL, 0, out, outlen, ctx->pad | S390X_KLMD_PS, ctx->A); |
303 | | ctx->bufsz = outlen % ctx->block_size; |
304 | | |
305 | | return 1; |
306 | | } |
307 | | |
308 | | static int s390x_keccak_squeeze(KECCAK1600_CTX *ctx, unsigned char *out, size_t outlen) |
309 | | { |
310 | | return s390x_keccakc_squeeze(ctx, out, outlen, KECCAK_PADDING); |
311 | | } |
312 | | |
313 | | static int s390x_cshake_keccak_squeeze(KECCAK1600_CTX *ctx, unsigned char *out, size_t outlen) |
314 | | { |
315 | | return s390x_keccakc_squeeze(ctx, out, outlen, CSHAKE_KECCAK_PADDING); |
316 | | } |
317 | | |
318 | | static PROV_SHA3_METHOD sha3_s390x_md = { |
319 | | s390x_sha3_absorb, |
320 | | s390x_sha3_final, |
321 | | NULL, |
322 | | }; |
323 | | |
324 | | static PROV_SHA3_METHOD keccak_s390x_md = { |
325 | | s390x_sha3_absorb, |
326 | | s390x_keccak_final, |
327 | | s390x_keccak_squeeze, |
328 | | }; |
329 | | |
330 | | static PROV_SHA3_METHOD shake_s390x_md = { |
331 | | s390x_sha3_absorb, |
332 | | s390x_shake_final, |
333 | | s390x_shake_squeeze, |
334 | | }; |
335 | | |
336 | | static PROV_SHA3_METHOD cshake_keccak_s390x_md = { |
337 | | s390x_sha3_absorb, |
338 | | s390x_cshake_keccak_final, |
339 | | s390x_cshake_keccak_squeeze, |
340 | | }; |
341 | | |
342 | | #define SHAKE_SET_MD(uname, typ) \ |
343 | | if (S390_SHA3_CAPABLE(uname)) { \ |
344 | | ctx->pad = S390X_##uname; \ |
345 | | ctx->meth = typ##_s390x_md; \ |
346 | | } else { \ |
347 | | ctx->meth = shake_generic_md; \ |
348 | | } |
349 | | |
350 | | #define SHA3_SET_MD(uname, typ) \ |
351 | | if (S390_SHA3_CAPABLE(uname)) { \ |
352 | | ctx->pad = S390X_##uname; \ |
353 | | ctx->meth = typ##_s390x_md; \ |
354 | | } else { \ |
355 | | ctx->meth = sha3_generic_md; \ |
356 | | } |
357 | | #define CSHAKE_KECCAK_SET_MD(bitlen) \ |
358 | | if (S390_SHA3_CAPABLE(SHAKE_##bitlen)) { \ |
359 | | ctx->pad = S390X_SHAKE_##bitlen; \ |
360 | | ctx->meth = cshake_keccak_s390x_md; \ |
361 | | } else { \ |
362 | | ctx->meth = shake_generic_md; \ |
363 | | } |
364 | | #elif defined(__aarch64__) && defined(KECCAK1600_ASM) |
365 | | #include "arm_arch.h" |
366 | | |
367 | | static sha3_absorb_fn armsha3_sha3_absorb; |
368 | | |
369 | | size_t SHA3_absorb_cext(uint64_t A[5][5], const unsigned char *inp, size_t len, |
370 | | size_t r); |
371 | | /*- |
372 | | * Hardware-assisted ARMv8.2 SHA3 extension version of the absorb() |
373 | | */ |
374 | | static size_t armsha3_sha3_absorb(KECCAK1600_CTX *ctx, const unsigned char *inp, size_t len) |
375 | | { |
376 | | return SHA3_absorb_cext(ctx->A, inp, len, ctx->block_size); |
377 | | } |
378 | | |
379 | | static PROV_SHA3_METHOD sha3_ARMSHA3_md = { |
380 | | armsha3_sha3_absorb, |
381 | | ossl_sha3_final_default, |
382 | | NULL |
383 | | }; |
384 | | static PROV_SHA3_METHOD shake_ARMSHA3_md = { |
385 | | armsha3_sha3_absorb, |
386 | | ossl_sha3_final_default, |
387 | | ossl_shake_squeeze_default |
388 | | }; |
389 | | #define SHAKE_SET_MD(uname, typ) \ |
390 | | if (OPENSSL_armcap_P & ARMV8_HAVE_SHA3_AND_WORTH_USING) { \ |
391 | | ctx->meth = shake_ARMSHA3_md; \ |
392 | | } else { \ |
393 | | ctx->meth = shake_generic_md; \ |
394 | | } |
395 | | |
396 | | #define SHA3_SET_MD(uname, typ) \ |
397 | | if (OPENSSL_armcap_P & ARMV8_HAVE_SHA3_AND_WORTH_USING) { \ |
398 | | ctx->meth = sha3_ARMSHA3_md; \ |
399 | | } else { \ |
400 | | ctx->meth = sha3_generic_md; \ |
401 | | } |
402 | | #define CSHAKE_KECCAK_SET_MD(bitlen) \ |
403 | | if (OPENSSL_armcap_P & ARMV8_HAVE_SHA3_AND_WORTH_USING) { \ |
404 | | ctx->meth = shake_ARMSHA3_md; \ |
405 | | } else { \ |
406 | | ctx->meth = shake_generic_md; \ |
407 | | } |
408 | | #else |
409 | 0 | #define SHA3_SET_MD(uname, typ) ctx->meth = sha3_generic_md; |
410 | 0 | #define CSHAKE_KECCAK_SET_MD(bitlen) ctx->meth = shake_generic_md; |
411 | 0 | #define SHAKE_SET_MD(uname, typ) ctx->meth = shake_generic_md; |
412 | | #endif /* S390_SHA3 */ |
413 | | |
414 | | #define SHA3_newctx(typ, uname, name, bitlen, pad) \ |
415 | | static OSSL_FUNC_digest_newctx_fn name##_newctx; \ |
416 | | static void *name##_newctx(void *provctx) \ |
417 | 0 | { \ |
418 | 0 | KECCAK1600_CTX *ctx; \ |
419 | 0 | \ |
420 | 0 | DIGEST_PROV_CHECK(provctx, SHA3_256); \ |
421 | 0 | if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL) \ |
422 | 0 | return NULL; \ |
423 | 0 | ossl_sha3_init(ctx, pad, bitlen); \ |
424 | 0 | SHA3_SET_MD(uname, typ) \ |
425 | 0 | return ctx; \ |
426 | 0 | } |
427 | | |
428 | | #define SHAKE_newctx(typ, uname, name, bitlen, mdlen, pad) \ |
429 | | static OSSL_FUNC_digest_newctx_fn name##_newctx; \ |
430 | | static void *name##_newctx(void *provctx) \ |
431 | 0 | { \ |
432 | 0 | KECCAK1600_CTX *ctx; \ |
433 | 0 | \ |
434 | 0 | DIGEST_PROV_CHECK(provctx, SHA3_256); \ |
435 | 0 | if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL) \ |
436 | 0 | return NULL; \ |
437 | 0 | ossl_keccak_init(ctx, pad, bitlen, mdlen); \ |
438 | 0 | if (mdlen == 0) \ |
439 | 0 | ctx->md_size = SIZE_MAX; \ |
440 | 0 | SHAKE_SET_MD(uname, typ) \ |
441 | 0 | return ctx; \ |
442 | 0 | } |
443 | | |
444 | | #define CSHAKE_KECCAK_newctx(uname, bitlen, pad) \ |
445 | | static OSSL_FUNC_digest_newctx_fn uname##_newctx; \ |
446 | | static void *uname##_newctx(void *provctx) \ |
447 | 0 | { \ |
448 | 0 | KECCAK1600_CTX *ctx; \ |
449 | 0 | \ |
450 | 0 | DIGEST_PROV_CHECK(provctx, SHA3_256); \ |
451 | 0 | if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL) \ |
452 | 0 | return NULL; \ |
453 | 0 | ossl_keccak_init(ctx, pad, bitlen, 2 * bitlen); \ |
454 | 0 | CSHAKE_KECCAK_SET_MD(bitlen) \ |
455 | 0 | return ctx; \ |
456 | 0 | } |
457 | | |
458 | | #define KMAC_newctx(uname, bitlen, pad) \ |
459 | | static OSSL_FUNC_digest_newctx_fn uname##_newctx; \ |
460 | | static void *uname##_newctx(void *provctx) \ |
461 | | { \ |
462 | | KECCAK1600_CTX *ctx; \ |
463 | | \ |
464 | | DIGEST_PROV_CHECK(provctx, SHA3_256); \ |
465 | | if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL) \ |
466 | | return NULL; \ |
467 | | ossl_keccak_init(ctx, pad, bitlen, 2 * bitlen); \ |
468 | | KMAC_SET_MD(bitlen) \ |
469 | | return ctx; \ |
470 | | } |
471 | | |
472 | | #define PROV_FUNC_SHA3_DIGEST_COMMON(name, bitlen, blksize, dgstsize, flags) \ |
473 | | PROV_FUNC_DIGEST_GET_PARAM(name, blksize, dgstsize, flags) \ |
474 | | const OSSL_DISPATCH ossl_##name##_functions[] = { \ |
475 | | { OSSL_FUNC_DIGEST_NEWCTX, (void (*)(void))name##_newctx }, \ |
476 | | { OSSL_FUNC_DIGEST_UPDATE, (void (*)(void))keccak_update }, \ |
477 | | { OSSL_FUNC_DIGEST_FINAL, (void (*)(void))keccak_final }, \ |
478 | | { OSSL_FUNC_DIGEST_FREECTX, (void (*)(void))keccak_freectx }, \ |
479 | | { OSSL_FUNC_DIGEST_DUPCTX, (void (*)(void))keccak_dupctx }, \ |
480 | | { OSSL_FUNC_DIGEST_COPYCTX, (void (*)(void))keccak_copyctx }, \ |
481 | | { OSSL_FUNC_DIGEST_SERIALIZE, (void (*)(void))name##_serialize }, \ |
482 | | { OSSL_FUNC_DIGEST_DESERIALIZE, (void (*)(void))name##_deserialize }, \ |
483 | | PROV_DISPATCH_FUNC_DIGEST_GET_PARAMS(name) |
484 | | |
485 | | #define PROV_FUNC_SHA3_DIGEST(name, bitlen, blksize, dgstsize, flags) \ |
486 | | PROV_FUNC_SHA3_DIGEST_COMMON(name, bitlen, blksize, dgstsize, flags), \ |
487 | | { OSSL_FUNC_DIGEST_INIT, (void (*)(void))keccak_init }, \ |
488 | | PROV_DISPATCH_FUNC_DIGEST_CONSTRUCT_END |
489 | | |
490 | | #define PROV_FUNC_SHAKE_DIGEST(name, bitlen, blksize, dgstsize, flags) \ |
491 | | PROV_FUNC_SHA3_DIGEST_COMMON(name, bitlen, blksize, dgstsize, flags), \ |
492 | | { OSSL_FUNC_DIGEST_SQUEEZE, (void (*)(void))shake_squeeze }, \ |
493 | | { OSSL_FUNC_DIGEST_INIT, (void (*)(void))keccak_init_params }, \ |
494 | | { OSSL_FUNC_DIGEST_SET_CTX_PARAMS, (void (*)(void))shake_set_ctx_params }, \ |
495 | | { OSSL_FUNC_DIGEST_SETTABLE_CTX_PARAMS, \ |
496 | | (void (*)(void))shake_settable_ctx_params }, \ |
497 | | { OSSL_FUNC_DIGEST_GET_CTX_PARAMS, (void (*)(void))shake_get_ctx_params }, \ |
498 | | { OSSL_FUNC_DIGEST_GETTABLE_CTX_PARAMS, \ |
499 | | (void (*)(void))shake_gettable_ctx_params }, \ |
500 | | PROV_DISPATCH_FUNC_DIGEST_CONSTRUCT_END |
501 | | |
502 | | static void keccak_freectx(void *vctx) |
503 | 0 | { |
504 | 0 | KECCAK1600_CTX *ctx = (KECCAK1600_CTX *)vctx; |
505 | |
|
506 | 0 | OPENSSL_clear_free(ctx, sizeof(*ctx)); |
507 | 0 | } |
508 | | |
509 | | static void keccak_copyctx(void *voutctx, void *vinctx) |
510 | 0 | { |
511 | 0 | KECCAK1600_CTX *outctx = (KECCAK1600_CTX *)voutctx; |
512 | 0 | KECCAK1600_CTX *inctx = (KECCAK1600_CTX *)vinctx; |
513 | |
|
514 | 0 | *outctx = *inctx; |
515 | 0 | } |
516 | | |
517 | | static void *keccak_dupctx(void *ctx) |
518 | 0 | { |
519 | 0 | KECCAK1600_CTX *in = (KECCAK1600_CTX *)ctx; |
520 | 0 | KECCAK1600_CTX *ret = ossl_prov_is_running() ? OPENSSL_malloc(sizeof(*ret)) |
521 | 0 | : NULL; |
522 | |
|
523 | 0 | if (ret != NULL) |
524 | 0 | *ret = *in; |
525 | 0 | return ret; |
526 | 0 | } |
527 | | |
528 | | static const unsigned char keccakmagic[] = "KECCAKv1"; |
529 | 0 | #define KECCAKMAGIC_LEN (sizeof(keccakmagic) - 1) |
530 | | #define KECCAK_SERIALIZATION_LEN \ |
531 | 0 | ( \ |
532 | 0 | KECCAKMAGIC_LEN /* magic string */ \ |
533 | 0 | + sizeof(uint64_t) /* impl-ID */ \ |
534 | 0 | + sizeof(uint64_t) /* c->md_size */ \ |
535 | 0 | + (sizeof(uint64_t) * 4) /* c->block_size, c->bufsz, c->pad, c->xof_state */ \ |
536 | 0 | + (sizeof(uint64_t) * 5 * 5) /* c->A */ \ |
537 | 0 | + (KECCAK1600_WIDTH / 8 - 32) /* c->buf */ \ |
538 | 0 | ) |
539 | | |
540 | | static int KECCAK_Serialize(KECCAK1600_CTX *c, int impl_id, |
541 | | unsigned char *output, size_t *outlen) |
542 | 0 | { |
543 | 0 | unsigned char *p; |
544 | 0 | int i, j; |
545 | |
|
546 | 0 | if (output == NULL) { |
547 | 0 | if (outlen == NULL) |
548 | 0 | return 0; |
549 | | |
550 | 0 | *outlen = KECCAK_SERIALIZATION_LEN; |
551 | 0 | return 1; |
552 | 0 | } |
553 | | |
554 | 0 | if (outlen != NULL && *outlen < KECCAK_SERIALIZATION_LEN) |
555 | 0 | return 0; |
556 | | |
557 | 0 | p = output; |
558 | | |
559 | | /* Magic code */ |
560 | 0 | memcpy(p, keccakmagic, KECCAKMAGIC_LEN); |
561 | 0 | p += KECCAKMAGIC_LEN; |
562 | | |
563 | | /* Additional check data */ |
564 | 0 | p = OPENSSL_store_u64_le(p, impl_id); |
565 | 0 | p = OPENSSL_store_u64_le(p, c->md_size); |
566 | |
|
567 | 0 | p = OPENSSL_store_u64_le(p, c->block_size); |
568 | 0 | p = OPENSSL_store_u64_le(p, c->bufsz); |
569 | 0 | p = OPENSSL_store_u64_le(p, c->pad); |
570 | 0 | p = OPENSSL_store_u64_le(p, c->xof_state); |
571 | | |
572 | | /* A matrix */ |
573 | 0 | for (i = 0; i < 5; i++) { |
574 | 0 | for (j = 0; j < 5; j++) |
575 | 0 | p = OPENSSL_store_u64_le(p, c->A[i][j]); |
576 | 0 | } |
577 | |
|
578 | 0 | if (outlen != NULL) |
579 | 0 | *outlen = KECCAK_SERIALIZATION_LEN; |
580 | | |
581 | | /* buf */ |
582 | 0 | memcpy(p, c->buf, sizeof(c->buf)); |
583 | |
|
584 | 0 | return 1; |
585 | 0 | } |
586 | | |
587 | | /* |
588 | | * This function only performs basic input sanity checks and is not |
589 | | * built to handle malicious input data. Only trusted input should be |
590 | | * fed to this function |
591 | | */ |
592 | | static int KECCAK_Deserialize(KECCAK1600_CTX *c, int impl_id, |
593 | | const unsigned char *input, size_t len) |
594 | 0 | { |
595 | 0 | const unsigned char *p; |
596 | 0 | uint64_t val; |
597 | 0 | int i, j; |
598 | |
|
599 | 0 | if (c == NULL || input == NULL || len != KECCAK_SERIALIZATION_LEN) |
600 | 0 | return 0; |
601 | | |
602 | | /* Magic code */ |
603 | 0 | if (memcmp(input, keccakmagic, KECCAKMAGIC_LEN) != 0) |
604 | 0 | return 0; |
605 | | |
606 | 0 | p = input + KECCAKMAGIC_LEN; |
607 | | |
608 | | /* Check for matching Impl ID */ |
609 | 0 | p = OPENSSL_load_u64_le(&val, p); |
610 | 0 | if (val != (uint64_t)impl_id) |
611 | 0 | return 0; |
612 | | |
613 | | /* Check for matching md_size */ |
614 | 0 | p = OPENSSL_load_u64_le(&val, p); |
615 | 0 | if (val != (uint64_t)c->md_size) |
616 | 0 | return 0; |
617 | | |
618 | | /* check that block_size is congruent with the initialized value */ |
619 | 0 | p = OPENSSL_load_u64_le(&val, p); |
620 | 0 | if (val != c->block_size) |
621 | 0 | return 0; |
622 | | /* check that bufsz does not exceed block_size */ |
623 | 0 | p = OPENSSL_load_u64_le(&val, p); |
624 | 0 | if (val > c->block_size) |
625 | 0 | return 0; |
626 | 0 | c->bufsz = (size_t)val; |
627 | 0 | p = OPENSSL_load_u64_le(&val, p); |
628 | 0 | if (val != c->pad) |
629 | 0 | return 0; |
630 | 0 | p = OPENSSL_load_u64_le(&val, p); |
631 | 0 | c->xof_state = (int)val; |
632 | | |
633 | | /* A matrix */ |
634 | 0 | for (i = 0; i < 5; i++) { |
635 | 0 | for (j = 0; j < 5; j++) { |
636 | 0 | p = OPENSSL_load_u64_le(&val, p); |
637 | 0 | c->A[i][j] = val; |
638 | 0 | } |
639 | 0 | } |
640 | | |
641 | | /* buf */ |
642 | 0 | memcpy(c->buf, p, sizeof(c->buf)); |
643 | |
|
644 | 0 | return 1; |
645 | 0 | } |
646 | | |
647 | | #define IMPLEMENT_SERIALIZE_FNS(name, id) \ |
648 | | static int name##_serialize(void *vctx, unsigned char *out, \ |
649 | | size_t *outlen) \ |
650 | 0 | { \ |
651 | 0 | return KECCAK_Serialize(vctx, id, out, outlen); \ |
652 | 0 | } \ Unexecuted instantiation: sha3_prov.c:sha3_224_serialize Unexecuted instantiation: sha3_prov.c:sha3_256_serialize Unexecuted instantiation: sha3_prov.c:sha3_384_serialize Unexecuted instantiation: sha3_prov.c:sha3_512_serialize Unexecuted instantiation: sha3_prov.c:keccak_224_serialize Unexecuted instantiation: sha3_prov.c:keccak_256_serialize Unexecuted instantiation: sha3_prov.c:keccak_384_serialize Unexecuted instantiation: sha3_prov.c:keccak_512_serialize Unexecuted instantiation: sha3_prov.c:shake_128_serialize Unexecuted instantiation: sha3_prov.c:shake_256_serialize Unexecuted instantiation: sha3_prov.c:cshake_keccak_128_serialize Unexecuted instantiation: sha3_prov.c:cshake_keccak_256_serialize |
653 | | static int name##_deserialize(void *vctx, const unsigned char *in, \ |
654 | | size_t inlen) \ |
655 | 0 | { \ |
656 | 0 | return KECCAK_Deserialize(vctx, id, in, inlen); \ |
657 | 0 | } Unexecuted instantiation: sha3_prov.c:sha3_224_deserialize Unexecuted instantiation: sha3_prov.c:sha3_256_deserialize Unexecuted instantiation: sha3_prov.c:sha3_384_deserialize Unexecuted instantiation: sha3_prov.c:sha3_512_deserialize Unexecuted instantiation: sha3_prov.c:keccak_224_deserialize Unexecuted instantiation: sha3_prov.c:keccak_256_deserialize Unexecuted instantiation: sha3_prov.c:keccak_384_deserialize Unexecuted instantiation: sha3_prov.c:keccak_512_deserialize Unexecuted instantiation: sha3_prov.c:shake_128_deserialize Unexecuted instantiation: sha3_prov.c:shake_256_deserialize Unexecuted instantiation: sha3_prov.c:cshake_keccak_128_deserialize Unexecuted instantiation: sha3_prov.c:cshake_keccak_256_deserialize |
658 | | |
659 | | static const OSSL_PARAM *shake_gettable_ctx_params(ossl_unused void *ctx, |
660 | | ossl_unused void *provctx) |
661 | 0 | { |
662 | 0 | return shake_get_ctx_params_list; |
663 | 0 | } |
664 | | |
665 | | static int shake_get_ctx_params(void *vctx, OSSL_PARAM params[]) |
666 | 0 | { |
667 | 0 | struct shake_get_ctx_params_st p; |
668 | 0 | KECCAK1600_CTX *ctx = (KECCAK1600_CTX *)vctx; |
669 | |
|
670 | 0 | if (ctx == NULL || !shake_get_ctx_params_decoder(params, &p)) |
671 | 0 | return 0; |
672 | | |
673 | 0 | if (p.xoflen != NULL && !OSSL_PARAM_set_size_t(p.xoflen, ctx->md_size)) { |
674 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); |
675 | 0 | return 0; |
676 | 0 | } |
677 | | /* Size is an alias of xoflen but separate them for compatibility */ |
678 | 0 | if (p.size != NULL && !OSSL_PARAM_set_size_t(p.size, ctx->md_size)) { |
679 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); |
680 | 0 | return 0; |
681 | 0 | } |
682 | 0 | return 1; |
683 | 0 | } |
684 | | |
685 | | static const OSSL_PARAM *shake_settable_ctx_params(ossl_unused void *ctx, |
686 | | ossl_unused void *provctx) |
687 | 0 | { |
688 | 0 | return shake_set_ctx_params_list; |
689 | 0 | } |
690 | | |
691 | | static int shake_set_ctx_params(void *vctx, const OSSL_PARAM params[]) |
692 | 0 | { |
693 | 0 | struct shake_set_ctx_params_st p; |
694 | 0 | KECCAK1600_CTX *ctx = (KECCAK1600_CTX *)vctx; |
695 | |
|
696 | 0 | if (ossl_unlikely(ctx == NULL || !shake_set_ctx_params_decoder(params, &p))) |
697 | 0 | return 0; |
698 | | |
699 | 0 | if (ossl_unlikely(p.xoflen != NULL |
700 | 0 | && !OSSL_PARAM_get_size_t(p.xoflen, &ctx->md_size))) { |
701 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); |
702 | 0 | return 0; |
703 | 0 | } |
704 | 0 | return 1; |
705 | 0 | } |
706 | | |
707 | | #define KECCAK_SER_ID 0x010000 |
708 | | #define SHAKE_SER_ID 0x020000 |
709 | | #define SHA3_SER_ID 0x040000 |
710 | | #define CSHAKE_KECCAK_SER_ID 0x080000 |
711 | | |
712 | | #define IMPLEMENT_SHA3_functions(bitlen) \ |
713 | | SHA3_newctx(sha3, SHA3_##bitlen, sha3_##bitlen, bitlen, (uint8_t)SHA3_PADDING) \ |
714 | | IMPLEMENT_SERIALIZE_FNS(sha3_##bitlen, SHA3_SER_ID + bitlen) \ |
715 | | PROV_FUNC_SHA3_DIGEST(sha3_##bitlen, bitlen, \ |
716 | | SHA3_BLOCKSIZE(bitlen), SHA3_MDSIZE(bitlen), \ |
717 | | SHA3_FLAGS) |
718 | | |
719 | | #define IMPLEMENT_KECCAK_functions(bitlen) \ |
720 | | SHA3_newctx(keccak, KECCAK_##bitlen, keccak_##bitlen, bitlen, (uint8_t)KECCAK_PADDING) \ |
721 | | IMPLEMENT_SERIALIZE_FNS(keccak_##bitlen, KECCAK_SER_ID + bitlen) \ |
722 | | PROV_FUNC_SHA3_DIGEST(keccak_##bitlen, bitlen, \ |
723 | | SHA3_BLOCKSIZE(bitlen), SHA3_MDSIZE(bitlen), \ |
724 | | SHA3_FLAGS) |
725 | | |
726 | | #define IMPLEMENT_SHAKE_functions(bitlen) \ |
727 | | SHAKE_newctx(shake, SHAKE_##bitlen, shake_##bitlen, bitlen, \ |
728 | | 0 /* no default md length */, (uint8_t)SHAKE_PADDING) \ |
729 | | IMPLEMENT_SERIALIZE_FNS(shake_##bitlen, SHAKE_SER_ID + bitlen) \ |
730 | | PROV_FUNC_SHAKE_DIGEST(shake_##bitlen, bitlen, \ |
731 | | SHA3_BLOCKSIZE(bitlen), 0, \ |
732 | | SHAKE_FLAGS) |
733 | | |
734 | | #define IMPLEMENT_CSHAKE_KECCAK_functions(bitlen) \ |
735 | | CSHAKE_KECCAK_newctx(cshake_keccak_##bitlen, bitlen, (uint8_t)CSHAKE_KECCAK_PADDING) \ |
736 | | IMPLEMENT_SERIALIZE_FNS(cshake_keccak_##bitlen, CSHAKE_KECCAK_SER_ID + bitlen) \ |
737 | | PROV_FUNC_SHAKE_DIGEST(cshake_keccak_##bitlen, bitlen, \ |
738 | | SHA3_BLOCKSIZE(bitlen), \ |
739 | | CSHAKE_KECCAK_MDSIZE(bitlen), \ |
740 | | CSHAKE_KECCAK_FLAGS) |
741 | | |
742 | | /* ossl_sha3_224_functions */ |
743 | 0 | IMPLEMENT_SHA3_functions(224) |
744 | 0 | /* ossl_sha3_256_functions */ |
745 | 0 | IMPLEMENT_SHA3_functions(256) |
746 | 0 | /* ossl_sha3_384_functions */ |
747 | 0 | IMPLEMENT_SHA3_functions(384) |
748 | 0 | /* ossl_sha3_512_functions */ |
749 | 0 | IMPLEMENT_SHA3_functions(512) |
750 | 0 | /* ossl_keccak_224_functions */ |
751 | 0 | IMPLEMENT_KECCAK_functions(224) |
752 | 0 | /* ossl_keccak_256_functions */ |
753 | 0 | IMPLEMENT_KECCAK_functions(256) |
754 | 0 | /* ossl_keccak_384_functions */ |
755 | 0 | IMPLEMENT_KECCAK_functions(384) |
756 | 0 | /* ossl_keccak_512_functions */ |
757 | 0 | IMPLEMENT_KECCAK_functions(512) |
758 | 0 | /* ossl_shake_128_functions */ |
759 | 0 | IMPLEMENT_SHAKE_functions(128) |
760 | 0 | /* ossl_shake_256_functions */ |
761 | 0 | IMPLEMENT_SHAKE_functions(256) |
762 | 0 | /* ossl_cshake_keccak_128_functions */ |
763 | 0 | IMPLEMENT_CSHAKE_KECCAK_functions(128) |
764 | 0 | /* ossl_cshake_keccak_256_functions */ |
765 | | IMPLEMENT_CSHAKE_KECCAK_functions(256) |