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