mirrored from https://www.bouncycastle.org/repositories/bc-rust
-
Notifications
You must be signed in to change notification settings - Fork 17
Feature/sha3 final patches #87
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dghgit
wants to merge
9
commits into
release/0.1.3alpha
Choose a base branch
from
feature/sha3-final-patches
base: release/0.1.3alpha
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
133ea1b
sha3: fix partial-byte squeeze, validate partial-bit inputs, docs and…
dghgit 5afe03d
sha3: share finalize() between do_final_out and do_final_partial_bits…
dghgit 989e339
sha3: rustfmt
dghgit b8beef8
sha3: address PR #87 review
dghgit 77b5d86
sha3: read NIST FIPS 202 example vectors from bc-test-data
dghgit b57febb
sha3: add NIST CAVP SHA3VS harness; fix double SHAKE suffix on 4-bit …
dghgit 6153bbf
core: document the partial-byte bit-ordering convention on Hash
dghgit 8496d7e
Add 0.1.3 release notes for the SHA-3 changes (PR #87)
dghgit c940a5d
Add bench_sha3_mem_usage: struct sizes and massif entry points for SH…
dghgit File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -89,7 +89,7 @@ | |
| //! [`KDF`] acts on [`KeyMaterial`] objects as both the input and output values. | ||
| //! In the case of SHA3, the [`KDF`] interfaces are simple wrapper functions around the underlying SHA3 or SHAKE | ||
| //! primitive that correctly maintains the length and entropy metadata of the key material that it is acting on. | ||
| //! This is intended to act as a developer ait to prevent some classes of developer mistakes, such as | ||
| //! This is intended to act as a developer aid to prevent some classes of developer mistakes, such as | ||
| //! deriving a cryptographic key from uninitialized (aka zeroized) input key material, or using low-entropy | ||
| //! input key material to derive a MAC, symmetric, or asymmetric key. | ||
| //! | ||
|
|
@@ -108,6 +108,31 @@ | |
| //! [`KeyType::CryptographicRandom`] since the input [`KeyMaterial`] is 16 bytes but [`SHA3_256`] needs at least 32 bytes of | ||
| //! full-entropy input key material in order to be able to produce full entropy output key material. | ||
| //! | ||
| //! # Memory Usage | ||
| //! | ||
| //! All SHA3 and SHAKE variants share the same Keccak-f\[1600\] sponge and so have identical memory | ||
| //! footprints. No heap memory is used by the algorithms themselves; the `Vec<u8>`-returning | ||
| //! convenience methods allocate only the output buffer, and the `*_out` variants allocate nothing. | ||
| //! | ||
| //! | Object | Size (bytes) | | ||
| //! |-----------------------------------------|--------------| | ||
| //! | `SHA3_224` .. `SHA3_512`, `SHAKE128/256` | 440 | | ||
| //! | Suspended state ([`Suspendable`]) | 415 | | ||
| //! | ||
| //! Sizes are `core::mem::size_of` values reported by `mem_usage_benches/bench_sha3_mem_usage.rs` | ||
| //! (`cargo run --release -p mem_usage_benches --bin bench_sha3_mem_usage`), which also has valgrind | ||
| //! massif entry points for measuring peak stack usage of the hash, XOF and suspend/resume paths. | ||
| //! | ||
| //! # Security Considerations | ||
| //! | ||
| //! * SHA3-224/256/384/512 offer 112/128/192/256 bits of collision resistance respectively; SHAKE128 | ||
| //! and SHAKE256 offer 128 and 256 bits of security for output lengths at least twice that size | ||
| //! (FIPS 202 Appendix A.1). | ||
| //! * SHAKE is an XOF, not a hash: `SHAKE128(m, 32)` is a prefix of `SHAKE128(m, 64)`. If the output | ||
| //! length must be bound to the digest, include it in the message (FIPS 202 Appendix A.2). | ||
| //! * The sponge state and queue are held in [`bouncycastle_utils::secret::Secret`] and zeroized on | ||
| //! drop. | ||
| //! | ||
| //! # Suspending and resuming execution | ||
| //! | ||
| //! When hashing a large message, it can be advantageous to be able to suspend the operation | ||
|
|
@@ -160,17 +185,17 @@ mod sha3; | |
| mod shake; | ||
|
|
||
| /*** String constants ***/ | ||
| /// | ||
| /// Algorithm name string for SHA3-224, as used by the factories and CLI. | ||
| pub const SHA3_224_NAME: &str = "SHA3-224"; | ||
| /// | ||
| /// Algorithm name string for SHA3-256, as used by the factories and CLI. | ||
| pub const SHA3_256_NAME: &str = "SHA3-256"; | ||
| /// | ||
| /// Algorithm name string for SHA3-384, as used by the factories and CLI. | ||
| pub const SHA3_384_NAME: &str = "SHA3-384"; | ||
| /// | ||
| /// Algorithm name string for SHA3-512, as used by the factories and CLI. | ||
| pub const SHA3_512_NAME: &str = "SHA3-512"; | ||
| /// | ||
| /// Algorithm name string for SHAKE128, as used by the factories and CLI. | ||
| pub const SHAKE128_NAME: &str = "SHAKE128"; | ||
| /// | ||
| /// Algorithm name string for SHAKE256, as used by the factories and CLI. | ||
| pub const SHAKE256_NAME: &str = "SHAKE256"; | ||
|
|
||
| /*** pub types ***/ | ||
|
|
@@ -205,11 +230,13 @@ trait SHA3Params: HashAlgParams { | |
|
|
||
| // TODO: it would probably be more elegant to macro these. | ||
|
|
||
| impl HashAlgParams for SHA3_224 { | ||
| const OUTPUT_LEN: usize = 28; | ||
| // const BLOCK_LEN: usize = 64; | ||
| const BLOCK_LEN: usize = 144; // FIPS 202 Table 3 | ||
| /// The public hash types expose the same parameters as their `*Params` marker, so the constants | ||
| /// are defined exactly once (on the params struct) and forwarded here. | ||
| impl<PARAMS: SHA3Params> HashAlgParams for SHA3Internal<PARAMS> { | ||
| const OUTPUT_LEN: usize = PARAMS::OUTPUT_LEN; | ||
| const BLOCK_LEN: usize = PARAMS::BLOCK_LEN; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh sweet! That's an excellent use of generics!! 👍
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Honestly, I cannot take the full credit for that one. |
||
| } | ||
|
|
||
| /// The parameters for SHA3_224. | ||
| #[derive(Clone)] | ||
| pub struct SHA3_224Params; | ||
|
|
@@ -219,7 +246,6 @@ impl Algorithm for SHA3_224Params { | |
| } | ||
| impl HashAlgParams for SHA3_224Params { | ||
| const OUTPUT_LEN: usize = 28; | ||
| // const BLOCK_LEN: usize = 64; | ||
| const BLOCK_LEN: usize = 144; // FIPS 202 Table 3 | ||
| } | ||
| impl SHA3Params for SHA3_224Params { | ||
|
|
@@ -233,11 +259,6 @@ impl AlgorithmOID for SHA3_224 { | |
| &[0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x07]; | ||
| } | ||
|
|
||
| impl HashAlgParams for SHA3_256 { | ||
| const OUTPUT_LEN: usize = 32; | ||
| // const BLOCK_LEN: usize = 64; | ||
| const BLOCK_LEN: usize = 136; // FIPS 202 Table 3 | ||
| } | ||
| /// The parameters for SHA3_256. | ||
| #[derive(Clone)] | ||
| pub struct SHA3_256Params; | ||
|
|
@@ -247,7 +268,6 @@ impl Algorithm for SHA3_256Params { | |
| } | ||
| impl HashAlgParams for SHA3_256Params { | ||
| const OUTPUT_LEN: usize = 32; | ||
| // const BLOCK_LEN: usize = 64; | ||
| const BLOCK_LEN: usize = 136; // FIPS 202 Table 3 | ||
| } | ||
| impl SHA3Params for SHA3_256Params { | ||
|
|
@@ -263,18 +283,12 @@ impl AlgorithmOID for SHA3_256 { | |
| /// The parameters for SHA3_384. | ||
| #[derive(Clone)] | ||
| pub struct SHA3_384Params; | ||
| impl HashAlgParams for SHA3_384 { | ||
| const OUTPUT_LEN: usize = 48; | ||
| // const BLOCK_LEN: usize = 128; | ||
| const BLOCK_LEN: usize = 104; // FIPS 202 Table 3 | ||
| } | ||
| impl Algorithm for SHA3_384Params { | ||
| const ALG_NAME: &'static str = SHA3_384_NAME; | ||
| const MAX_SECURITY_STRENGTH: SecurityStrength = SecurityStrength::_192bit; | ||
| } | ||
| impl HashAlgParams for SHA3_384Params { | ||
| const OUTPUT_LEN: usize = 48; | ||
| // const BLOCK_LEN: usize = 128; | ||
| const BLOCK_LEN: usize = 104; // FIPS 202 Table 3 | ||
| } | ||
| impl SHA3Params for SHA3_384Params { | ||
|
|
@@ -290,18 +304,12 @@ impl AlgorithmOID for SHA3_384 { | |
| /// The parameters for SHA3_512. | ||
| #[derive(Clone)] | ||
| pub struct SHA3_512Params; | ||
| impl HashAlgParams for SHA3_512 { | ||
| const OUTPUT_LEN: usize = 64; | ||
| // const BLOCK_LEN: usize = 128; | ||
| const BLOCK_LEN: usize = 72; // FIPS 202 Table 3 | ||
| } | ||
| impl Algorithm for SHA3_512Params { | ||
| const ALG_NAME: &'static str = SHA3_512_NAME; | ||
| const MAX_SECURITY_STRENGTH: SecurityStrength = SecurityStrength::_256bit; | ||
| } | ||
| impl HashAlgParams for SHA3_512Params { | ||
| const OUTPUT_LEN: usize = 64; | ||
| // const BLOCK_LEN: usize = 128; | ||
| const BLOCK_LEN: usize = 72; // FIPS 202 Table 3 | ||
| } | ||
| impl SHA3Params for SHA3_512Params { | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For my curiosity, where did these numbers come from?
Where I've done this on other crates, I have, for example, a
/mem_usage_benches/bench_mldsa_mem_usage.rsthat prints struct sizes using rust'ssize_of::<>operator.This PR has not added equivalent mem benches for SHA3, so how did you measure these numbers?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As you guessed, it was a throw away though, as I was just focusing on the review issues. Did you want me to commit in something equivalent (would probably suggest a separate branch but can do here).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added mem_usage_benches/bench_sha3_mem_usage.rs as well.