Implement SHAKE in sha3.c#648
Conversation
d383661 to
b777b99
Compare
55b4391 to
54a0ef2
Compare
bjwtaylor
left a comment
There was a problem hiding this comment.
One quick question, though still need to review in more detail when I get a chance.
| break; | ||
| #endif | ||
|
|
||
| #if defined(MBEDTLS_SHA3_WANT_SHAKE128) |
There was a problem hiding this comment.
Should the SHAKE variants be setting the ctx->olen to 0, to clear it to prevent any prior length being retained?
There was a problem hiding this comment.
Yes, that's a good idea. And that shows we're missing a context reuse test case for SHAKE.
There was a problem hiding this comment.
More precisely, the case where it can go wrong is if there's a non-SHAKE operation without finish, then a SHAKE operation. The SHAKE operation will be limited to a single finish() call, which will output exactly the length of the hash from the first operation. We aren't currently testing a context reuse without finish (i.e. init, starts, update, starts, update, finish).
| /* | ||
| * SHA-3 process buffer | ||
| */ | ||
| int mbedtls_sha3_update(mbedtls_sha3_context *ctx, |
There was a problem hiding this comment.
Should this function check for the presence of ctx->finished to prevent an API misuse of using an update after a finish and produce an error if it is set?
There was a problem hiding this comment.
Ideally yes, but this is now a private interface.
It would be good to do that in 3.6 though, since sha3.h is public there.
There was a problem hiding this comment.
As discussed on Thursday, this is an internal interface, we don't particularly need to protect against misuse.
| #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_SOME_HASH) */ | ||
|
|
||
| #if defined(MBEDTLS_SHA3_WANT_SHAKE128) | ||
| static const unsigned char shake128_test_input[2][16] = |
There was a problem hiding this comment.
Do we document anywhere where this test data comes from?
There was a problem hiding this comment.
Should, yes. Do, unfortunately, no. But we don't actually rely on it for correctness (we have the unit tests for that), so I can live with it.
There was a problem hiding this comment.
Yeah, I was more thinking if anybody has to modify it or wants to use it for other tests it's useful to know where it comes from.
valeriosetti
left a comment
There was a problem hiding this comment.
I found only a minor typo in a comment, but the rest LGTM.
This is in preparation for adding SHAKE support: if e.g. SHAKE128 is enabled but no SHA3 hash, then psa_crypto_hash.c should not include SHA3 support. Conversely, if e.g. SHAKE128/256 is enabled but neither SHAKE128 nor SHAKE256, then psa_crypto_xof.c should not include SHAKE support. Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
We don't have a XOF API yet in PSA. To provide a way to enable built-in SHAKE support, for now, add options to `crypto_config.h` that are enabled in the `full` config. Define the MBEDTLS_PSA_BUILTIN_xxx symbols as macros with no parameter, rather than with the value 1 as usual, so that `config.py full` enables them. Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
Signed-off-by: Pol Henarejos <pol.henarejos@cttc.es> Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
SHAKE, as it is a XOF function, it allows arbitrary output lengths that can be called multiple times and concatenated. The property SHAKE_finish(olen1+olen2)=SHAKE_finish(olen1)||SHAKE_finish(olen2) always holds. Signed-off-by: Pol Henarejos <pol.henarejos@cttc.es> Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
Signed-off-by: Pol Henarejos <pol.henarejos@cttc.es> Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
Choose some representative lengths: 0, a few short inputs,
one block -1/+0/+1, two blocks -1/+0/+1.
Test cases generated by the following Python code:
```
from hashlib import shake_128, shake_256;
def tc(f, shake, v, n): msg = shake(bytes(str(n), encoding="ascii")).digest(n); output = shake(msg).digest(300); return f"SHAKE{v} {f} {n}\ndepends_on:MBEDTLS_SHA3_WANT_SHAKE{v}\nshake_{f}:MBEDTLS_SHA3_SHAKE{v}:\"{msg.hex()}\":\"{output.hex()}\":\n"
for n in [0, 1, 2, 4, 8, 16, 42, 55, 100, 167, 168, 169, 335, 336, 337]: print(tc("input", shake_128, 128, n))
for n in [0, 1, 2, 4, 8, 16, 42, 55, 100, 135, 136, 137, 271, 272, 273]: print(tc("input", shake_256, 256, n))
for n in [0, 1, 200]: print(tc("output", shake_128, 128, n))
for n in [0, 1, 200]: print(tc("output", shake_256, 256, n))
```
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
Test data from NIST: https://github.com/usnistgov/ACVP-Server/blob/v1.1.0.41/gen-val/json-files/SHAKE-128-FIPS202/internalProjection.json https://github.com/usnistgov/ACVP-Server/blob/v1.1.0.41/gen-val/json-files/SHAKE-256-FIPS202/internalProjection.json Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
4c973cf
9ff5c39 to
4c973cf
Compare
|
I rewrote the history to remove the framework update commit, since it isn't needed after all. I also fixed the typo that Valerio pointed out while I was at it. The previous version is at https://github.com/gilles-peskine-arm/TF-PSA-Crypto/tree/sha3-shake-builtin-4 |
Implement SHAKE in the SHA3 module. Resolves #621
An earlier version of this pull request also implemented the SHAKE128/256 and SHAKE256/512 hash algorithms. We don't need those urgently: they'll be needed for HashML-DSA and for Ed448ph respectively. That part is at https://github.com/gilles-peskine-arm/TF-PSA-Crypto/tree/sha3-shake-builtin-2 (with unresolved problems with some test driver builds) and needs preceding PR: Mbed-TLS/mbedtls-framework#267.
PR checklist