Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SHA3 and SHAKE - New API Design (#2098)
### Issues: Resolves #CryptoAlg-2810 ### Description of changes: AWS-LC supports SHA3 and SHAKE algorithms though low level SHA3_Init, SHA3_Update, SHA3_Final and SHAKE_init, SHAKE_Final APIs. Currently, there are two issues with the implementation and usage of SHA3 and SHAKE: - There is no support for SHAKE_Update function. SHAKE is implemented by calling SHAKE_Init, SHA3_Update and SHAKE_Final. - SHAKE_Final allows multiple consecutive calls to enable incremental XOF output generation. This PR addresses both of them as follows: - Introduce new API layers - FIPS202, SHA3 and SHAKE. - _Keccak1600_ layer (#2097) implements KeccakF1600 Absorb and Squeeze functions; Keccak1600 layer does _not_ manage internal input/output buffers. - _FIPS202_ layer implements Reset, Init, Update, and Finalize functionalities; FIPS202 layer manages the internal input/output buffers, allowing incremental requests (not necessarily multiple of block size) to Update (Absorb) and Squeeze for input/output processing. (Other functionalities, such as zero-ing of bitstate, block size checks, etc. are also handled by FIPS202 API layer). - _FIPS202_ layer implements all common behavior between SHA3 and SHAKE algorithms. - _FIPS202_ layer checks/updates the |ctx->state| flag when handling a common behavior between SHA3 and SHAKE algorithms. |ctx->state| is updated in the higher level SHA3_ SHAKE_ API layer when the behavior of both algorithms diverges (SHAKE _can_ allow incremental squeezes). - _SHA3_ layer implements Init, Update, and Final functionalities; SHA3 layer only implements SHA3 algorithm, thus, offers a single-call SHA3_Final function. SHA3_Final will update internal |ctx->state| flag to prevent any sequential calls. - _SHAKE_ layer implements XOF SHAKE algorithm, therefore, offers Init, Absorb, Squeeze, and Final functionalities; - _SHAKE_ layer implements Init, and Absorb, Squeeze with incremental call support for absorb (byte-wise) and squeeze (block-wise). - _SHAKE_ layer implements a single-call SHAKE_Final function that generates an arbitrary length output and finalizes SHAKE. Incremental XOF output generation is handled by |SHAKE_Squeeze|. |SHAKE_Squeeze| can be called multiple times. SHAKE_Final should be called only once. - KECCAK600_CTX struct updates: - Remove |padded| field - Introduce |state| field - |state| can be |KECCAK1600_STATE_ABSORB|, |KECCAK1600_STATE_SQUEEZE|, |KECCAK1600_STATE_FINAL| - |KECCAK1600_STATE_ABSORB| - allows incremental absorbs until the state is changed - |KECCAK1600_STATE_SQUEEZE| - allows incremental squeezes for |SHAKE_Squeeze| - |KECCAK1600_STATE_Final| - prevents from incremental squeezes via |SHAKE_Final| and prevents from consecutive calls to |SHA3_Final| (Final functions are single-shot functions). SHA3 vs SHAKE algorithms (APIs usage): >- SHA3 digest generation: SHA3_Init; SHA3_Update; SHA3_Final; >- SHAKE (single-shot-output) output generation: SHAKE_Init; SHAKE_Absorb; SHAKE_Final; >- SHAKE (incremental) output generation: SHAKE_Init; SHAKE_Absorb; SHAKE_Squeeze<sup>+</sup>; ### Call-outs: Service indicator is updated: - Inside SHA3 and SHAKE single shot APIs (as previously in AWS-LC); - Inside SHA3_Final (as previously in AWS-LC); - Inside SHAKE_Final (Single-Shot XOF Final output generation as previously in AWS-LC); - Inside SHAKE_Squeeze (Streaming XOF Squeezes output generation updates the service indicator after each extendable output update); All other algorithms that use SHA3/SHAKE APIs are updated: - ML-KEM (SHA3/SHAKE calls will be inlined later) - ML-DSA (SHAKE_Squeeze (incremental XOF output functionality) inside ML-DSA is never invoked with the KAT test vectors and gtests) ### Testing: _./crypto/crypto_test --gtest_filter="KeccakInternalTest.*"_ _./crypto/crypto_test --gtest_filter="SHA3Test.*"_ _./crypto/crypto_test --gtest_filter="SHAKETest.*"_ _./crypto/crypto_test --gtest_filter="All/PerKEMTest.*"_ _./crypto/crypto_test --gtest_filter="All/PQDSAParameterTest.*"_ By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license and the ISC license. --------- Co-authored-by: Jake Massimo <[email protected]> Co-authored-by: Will Childs-Klein <[email protected]> Co-authored-by: Justin W Smith <[email protected]> Co-authored-by: Shubham Mittal <[email protected]> Co-authored-by: Samuel Chiang <[email protected]> Co-authored-by: David Benjamin <[email protected]> Co-authored-by: Theo Buehler <[email protected]> Co-authored-by: Adam Langley <[email protected]> Co-authored-by: Brian Ledger <[email protected]> Co-authored-by: Nick Harper <[email protected]> Co-authored-by: Andrew Hopkins <[email protected]> Co-authored-by: torben-hansen <[email protected]> Co-authored-by: Sean McGrail <[email protected]> Co-authored-by: olivergillespie <[email protected]>
- Loading branch information