Skip to content

Commit

Permalink
Add SLH-DSA CVP known answer tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tjade273 committed Aug 15, 2024
1 parent 78829ac commit 82bd850
Show file tree
Hide file tree
Showing 12 changed files with 2,233 additions and 23 deletions.
38 changes: 22 additions & 16 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion slh-dsa/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ digest = "0.10.7"

[dev-dependencies]
hex-literal = "0.4.1"
hex = "0.4.1"
hex = { version = "0.4.1", features = ["serde"] }
num-bigint = "0.4.4"
quickcheck = "1"
quickcheck_macros = "1"
Expand All @@ -40,6 +40,8 @@ ctr = "0.9.2"
rand_core = "0.6.4"
paste = "1.0.15"
rand = "0.8.5"
serde_json = "1.0.124"
serde = { version = "1.0.207", features = ["derive"] }

[lib]
bench = false
Expand Down
2 changes: 1 addition & 1 deletion slh-dsa/src/hashes/sha2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ impl ForsParams for Sha2_192f {
type MD = U<{ (33 * 8 + 7) / 8 }>;
}
impl ParameterSet for Sha2_192f {
const NAME: &'static str = "SLH-DSA-SHA2-128f";
const NAME: &'static str = "SLH-DSA-SHA2-192f";
}

/// SHA2 at L5 security with small signatures
Expand Down
60 changes: 57 additions & 3 deletions slh-dsa/src/signing_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use hybrid_array::{Array, ArraySize};
use typenum::{Unsigned, U, U16, U24, U32};

// NewTypes for ensuring hash argument order correctness
#[derive(Clone)]
#[derive(Clone, PartialEq, Eq, Debug)]
pub(crate) struct SkSeed<N: ArraySize>(pub(crate) Array<u8, N>);
impl<N: ArraySize> AsRef<[u8]> for SkSeed<N> {
fn as_ref(&self) -> &[u8] {
Expand All @@ -29,7 +29,7 @@ impl<N: ArraySize> SkSeed<N> {
}
}

#[derive(Clone)]
#[derive(Clone, PartialEq, Eq, Debug)]
pub(crate) struct SkPrf<N: ArraySize>(pub(crate) Array<u8, N>);
impl<N: ArraySize> AsRef<[u8]> for SkPrf<N> {
fn as_ref(&self) -> &[u8] {
Expand All @@ -51,7 +51,7 @@ impl<N: ArraySize> SkPrf<N> {
}

/// A `SigningKey` allows signing messages with a fixed parameter set
#[derive(Clone)]
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct SigningKey<P: ParameterSet> {
pub(crate) sk_seed: SkSeed<P::N>,
pub(crate) sk_prf: SkPrf<P::N>,
Expand Down Expand Up @@ -168,6 +168,26 @@ impl<P: ParameterSet> SigningKey<P> {
}
}

impl<P: ParameterSet> TryFrom<&[u8]> for SigningKey<P> {
type Error = Error;

fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
if bytes.len() != P::SkLen::USIZE {
return Err(Error::new());
}

let (sk_seed_bytes, rest) = bytes.split_at(P::N::USIZE);
let (sk_prf_bytes, verifying_key_bytes) = rest.split_at(P::N::USIZE);
let verifying_key = VerifyingKey::try_from(verifying_key_bytes)?;

Ok(SigningKey {
sk_seed: SkSeed::from(sk_seed_bytes),
sk_prf: SkPrf::from(sk_prf_bytes),
verifying_key,
})
}
}

impl<P: ParameterSet> Signer<Signature<P>> for SigningKey<P> {
fn try_sign(&self, msg: &[u8]) -> Result<Signature<P>, Error> {
self.try_sign_with_context(msg, &[], None)
Expand Down Expand Up @@ -216,3 +236,37 @@ impl<M> SigningKeyLen for Shake<U24, M> {
impl<M> SigningKeyLen for Shake<U32, M> {
type SkLen = U<{ 4 * 32 }>;
}

#[cfg(test)]
mod tests {
use crate::{util::macros::test_parameter_sets, ParameterSet, SigningKey};

fn test_serialize_deserialize<P: ParameterSet>() {
let mut rng: rand::prelude::ThreadRng = rand::thread_rng();
let sk = SigningKey::<P>::new(&mut rng);
let bytes = sk.to_bytes();
let sk2 = SigningKey::<P>::try_from(bytes.as_slice()).unwrap();
assert_eq!(sk, sk2);
}
test_parameter_sets!(test_serialize_deserialize);

#[cfg(feature = "alloc")]
fn test_serialize_deserialize_vec<P: ParameterSet>() {
let mut rng: rand::prelude::ThreadRng = rand::thread_rng();
let sk = SigningKey::<P>::new(&mut rng);
let vec = sk.to_vec();
let sk2 = SigningKey::<P>::try_from(vec.as_slice()).unwrap();
assert_eq!(sk, sk2);
}
#[cfg(feature = "alloc")]
test_parameter_sets!(test_serialize_deserialize_vec);

#[test]
fn test_deserialize_fail_on_incorrect_length() {
let mut rng: rand::prelude::ThreadRng = rand::thread_rng();
let sk = SigningKey::<Shake128f>::new(&mut rng);
let bytes = sk.to_bytes();
let incorrect_bytes = &bytes[..bytes.len() - 1];
assert!(SigningKey::<Shake128f>::try_from(incorrect_bytes).is_err());
}
}
8 changes: 8 additions & 0 deletions slh-dsa/tests/acvp/COPYRIGHT
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
https://github.com/usnistgov/ACVP-Server

NIST-developed software is provided by NIST as a public service. You may use, copy, and distribute copies of the software in any medium, provided that you keep intact this entire notice. You may improve, modify, and create derivative works of the software or any portion of the software, and you may copy and distribute such modifications or works. Modified works should carry a notice stating that you changed the software and should note the date and nature of any such change. Please explicitly acknowledge the National Institute of Standards and Technology as the source of the software.

NIST-developed software is expressly provided "AS IS." NIST MAKES NO WARRANTY OF ANY KIND, EXPRESS, IMPLIED, IN FACT, OR ARISING BY OPERATION OF LAW, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND DATA ACCURACY. NIST NEITHER REPRESENTS NOR WARRANTS THAT THE OPERATION OF THE SOFTWARE WILL BE UNINTERRUPTED OR ERROR-FREE, OR THAT ANY DEFECTS WILL BE CORRECTED. NIST DOES NOT WARRANT OR MAKE ANY REPRESENTATIONS REGARDING THE USE OF THE SOFTWARE OR THE RESULTS THEREOF, INCLUDING BUT NOT LIMITED TO THE CORRECTNESS, ACCURACY, RELIABILITY, OR USEFULNESS OF THE SOFTWARE.

You are solely responsible for determining the appropriateness of using and distributing the software and you assume all risks associated with its use, including but not limited to the risks and costs of program errors, compliance with applicable laws, damage to or loss of data, programs or equipment, and the unavailability or interruption of operation. This software is not intended to be used in any situation where a failure could cause risk of injury or damage to property. The software developed by NIST employees is not subject to copyright protection within the United States.

Loading

0 comments on commit 82bd850

Please sign in to comment.