-
Couldn't load subscription status.
- Fork 163
slh-dsa: implement DigestSigner/Verifier
#1075
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
daxpedda
wants to merge
1
commit into
RustCrypto:master
Choose a base branch
from
daxpedda:slh-dsa-digest-signer-verifier
base: master
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,17 +3,18 @@ | |
|
|
||
| use core::fmt::Debug; | ||
|
|
||
| use crate::hashes::HashSuite; | ||
| use crate::hashes::{HashDigest, HashSuite}; | ||
| use crate::{ | ||
| ParameterSet, address::Address, fors::ForsParams, hypertree::HypertreeParams, wots::WotsParams, | ||
| xmss::XmssParams, | ||
| }; | ||
| use crate::{PkSeed, SkPrf, SkSeed}; | ||
| use const_oid::db::fips205; | ||
| use digest::{Digest, KeyInit, Mac}; | ||
| use hmac::Hmac; | ||
| use digest::{Digest, KeyInit, Mac, Output, OutputSizeUser, Update}; | ||
| use hmac::{EagerHash, Hmac}; | ||
| use hybrid_array::{Array, ArraySize}; | ||
| use sha2::{Sha256, Sha512}; | ||
| use signature::Result; | ||
| use typenum::{Diff, Sum, U, U16, U24, U30, U32, U34, U39, U42, U47, U49, U64, U128}; | ||
|
|
||
| /// Implementation of the MGF1 XOF | ||
|
|
@@ -44,6 +45,36 @@ pub struct Sha2L1<N, M> { | |
| _m: core::marker::PhantomData<M>, | ||
| } | ||
|
|
||
| /// `Digest` implementation [`Sha2L1`] and [`Sha2L35`]. | ||
| pub struct Sha2Digest<H: EagerHash>(Inner<H>); | ||
|
|
||
| enum Inner<H: EagerHash> { | ||
| Hmac(Hmac<H>), | ||
| Hash(H), | ||
| } | ||
|
|
||
| impl<H: EagerHash> Update for Sha2Digest<H> { | ||
| fn update(&mut self, data: &[u8]) { | ||
| match &mut self.0 { | ||
| Inner::Hmac(hmac) => Mac::update(hmac, data), | ||
| Inner::Hash(hash) => Digest::update(hash, data), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl<H: EagerHash<Core: OutputSizeUser<OutputSize = H::OutputSize>>> Sha2Digest<H> { | ||
| fn finalize(self) -> Output<H> { | ||
| match self.0 { | ||
| Inner::Hmac(hmac) => hmac.finalize().into_bytes(), | ||
| Inner::Hash(hash) => hash.finalize(), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl<N: ArraySize, M: ArraySize> HashDigest for Sha2L1<N, M> { | ||
| type Digest = Sha2Digest<Sha256>; | ||
| } | ||
|
|
||
| impl<N: ArraySize, M: ArraySize> HashSuite for Sha2L1<N, M> | ||
| where | ||
| N: core::ops::Add<N>, | ||
|
|
@@ -61,35 +92,31 @@ where | |
| fn prf_msg( | ||
| sk_prf: &SkPrf<Self::N>, | ||
| opt_rand: &Array<u8, Self::N>, | ||
| msg: &[&[impl AsRef<[u8]>]], | ||
| ) -> Array<u8, Self::N> { | ||
| let mut mac = Hmac::<Sha256>::new_from_slice(sk_prf.as_ref()).unwrap(); | ||
| msg: &impl Fn(&mut Self::Digest) -> Result<()>, | ||
| ) -> Result<Array<u8, Self::N>> { | ||
| let mut mac = Sha2Digest(Inner::Hmac( | ||
| Hmac::<Sha256>::new_from_slice(sk_prf.as_ref()).unwrap(), | ||
| )); | ||
| mac.update(opt_rand.as_slice()); | ||
| msg.iter() | ||
| .copied() | ||
| .flatten() | ||
| .for_each(|msg_part| mac.update(msg_part.as_ref())); | ||
| let result = mac.finalize().into_bytes(); | ||
| Array::clone_from_slice(&result[..Self::N::USIZE]) | ||
| msg(&mut mac)?; | ||
| let result = mac.finalize(); | ||
| Ok(Array::clone_from_slice(&result[..Self::N::USIZE])) | ||
| } | ||
|
|
||
| fn h_msg( | ||
| rand: &Array<u8, Self::N>, | ||
| pk_seed: &PkSeed<Self::N>, | ||
| pk_root: &Array<u8, Self::N>, | ||
| msg: &[&[impl AsRef<[u8]>]], | ||
| ) -> Array<u8, Self::M> { | ||
| let mut h = Sha256::new(); | ||
| msg: &impl Fn(&mut Self::Digest) -> Result<()>, | ||
| ) -> Result<Array<u8, Self::M>> { | ||
| let mut h = Sha2Digest(Inner::Hash(Sha256::new())); | ||
| h.update(rand); | ||
| h.update(pk_seed); | ||
| h.update(pk_seed.as_ref()); | ||
| h.update(pk_root); | ||
| msg.iter() | ||
| .copied() | ||
| .flatten() | ||
| .for_each(|msg_part| h.update(msg_part.as_ref())); | ||
| msg(&mut h)?; | ||
| let result = Array(h.finalize().into()); | ||
| let seed = rand.clone().concat(pk_seed.0.clone()).concat(result); | ||
| mgf1::<Sha256, Self::M>(&seed) | ||
| Ok(mgf1::<Sha256, Self::M>(&seed)) | ||
| } | ||
|
|
||
| fn prf_sk( | ||
|
|
@@ -117,7 +144,8 @@ where | |
| .chain_update(pk_seed) | ||
| .chain_update(&zeroes) | ||
| .chain_update(adrs.compressed()); | ||
| m.iter().for_each(|x| sha.update(x.as_slice())); | ||
| m.iter() | ||
| .for_each(|x| Update::update(&mut sha, x.as_slice())); | ||
|
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. Hitting the same problem as usual. This time really unclear how to avoid it without loosing access to all the QoL methods of |
||
| let hash = sha.finalize(); | ||
| Array::clone_from_slice(&hash[..Self::N::USIZE]) | ||
| } | ||
|
|
@@ -210,6 +238,10 @@ pub struct Sha2L35<N, M> { | |
| _m: core::marker::PhantomData<M>, | ||
| } | ||
|
|
||
| impl<N: ArraySize, M: ArraySize> HashDigest for Sha2L35<N, M> { | ||
| type Digest = Sha2Digest<Sha512>; | ||
| } | ||
|
|
||
| impl<N: ArraySize, M: ArraySize> HashSuite for Sha2L35<N, M> | ||
| where | ||
| N: core::ops::Add<N>, | ||
|
|
@@ -229,35 +261,31 @@ where | |
| fn prf_msg( | ||
| sk_prf: &SkPrf<Self::N>, | ||
| opt_rand: &Array<u8, Self::N>, | ||
| msg: &[&[impl AsRef<[u8]>]], | ||
| ) -> Array<u8, Self::N> { | ||
| let mut mac = Hmac::<Sha512>::new_from_slice(sk_prf.as_ref()).unwrap(); | ||
| msg: &impl Fn(&mut Self::Digest) -> Result<()>, | ||
| ) -> Result<Array<u8, Self::N>> { | ||
| let mut mac = Sha2Digest(Inner::Hmac( | ||
| Hmac::<Sha512>::new_from_slice(sk_prf.as_ref()).unwrap(), | ||
| )); | ||
| mac.update(opt_rand.as_slice()); | ||
| msg.iter() | ||
| .copied() | ||
| .flatten() | ||
| .for_each(|msg_part| mac.update(msg_part.as_ref())); | ||
| let result = mac.finalize().into_bytes(); | ||
| Array::clone_from_slice(&result[..Self::N::USIZE]) | ||
| msg(&mut mac)?; | ||
| let result = mac.finalize(); | ||
| Ok(Array::clone_from_slice(&result[..Self::N::USIZE])) | ||
| } | ||
|
|
||
| fn h_msg( | ||
| rand: &Array<u8, Self::N>, | ||
| pk_seed: &PkSeed<Self::N>, | ||
| pk_root: &Array<u8, Self::N>, | ||
| msg: &[&[impl AsRef<[u8]>]], | ||
| ) -> Array<u8, Self::M> { | ||
| let mut h = Sha512::new(); | ||
| msg: &impl Fn(&mut Self::Digest) -> Result<()>, | ||
| ) -> Result<Array<u8, Self::M>> { | ||
| let mut h = Sha2Digest(Inner::Hash(Sha512::new())); | ||
| h.update(rand); | ||
| h.update(pk_seed); | ||
| h.update(pk_seed.as_ref()); | ||
| h.update(pk_root); | ||
| msg.iter() | ||
| .copied() | ||
| .flatten() | ||
| .for_each(|msg_part| h.update(msg_part.as_ref())); | ||
| msg(&mut h)?; | ||
| let result = Array(h.finalize().into()); | ||
| let seed = rand.clone().concat(pk_seed.0.clone()).concat(result); | ||
| mgf1::<Sha512, Self::M>(&seed) | ||
| Ok(mgf1::<Sha512, Self::M>(&seed)) | ||
| } | ||
|
|
||
| fn prf_sk( | ||
|
|
@@ -285,7 +313,8 @@ where | |
| .chain_update(pk_seed) | ||
| .chain_update(&zeroes) | ||
| .chain_update(adrs.compressed()); | ||
| m.iter().for_each(|x| sha.update(x.as_slice())); | ||
| m.iter() | ||
| .for_each(|x| Update::update(&mut sha, x.as_slice())); | ||
| let hash = sha.finalize(); | ||
| Array::clone_from_slice(&hash[..Self::N::USIZE]) | ||
| } | ||
|
|
||
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
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.
I originally had
type DigestonHashSuitedirectly, but that made it publicly not accessible.So I introduced
HashDigestjust to make it publicly accessible if users ever need to access the type.