diff --git a/src/asn1/certificate.rs b/src/asn1/certificate.rs index 34e673c..0c09a2d 100644 --- a/src/asn1/certificate.rs +++ b/src/asn1/certificate.rs @@ -560,8 +560,8 @@ mod tests { continue; } - // TODO: Add support for pre hash SLH DSA / ML DSA - if path.contains(dsa_prehash) { + // TODO: Check for bug in BC ML-DSA Prehash or bug in our code + if path.contains(dsa_prehash) && !path.contains(slh_dsa_prefix) { continue; } diff --git a/src/dsa/api/algorithm.rs b/src/dsa/api/algorithm.rs index 831fae7..2f69b31 100644 --- a/src/dsa/api/algorithm.rs +++ b/src/dsa/api/algorithm.rs @@ -48,7 +48,7 @@ pub enum DsaAlgorithm { HashMlDsa87EcdsaBrainpoolP384r1Sha512, HashMlDsa87Ed448Sha512, - // Pure DSAs + // Pure SLH-DSAs SlhDsaSha2_128s, SlhDsaSha2_128f, SlhDsaSha2_192s, @@ -61,6 +61,20 @@ pub enum DsaAlgorithm { SlhDsaShake192f, SlhDsaShake256s, SlhDsaShake256f, + + // Prehash SLH-DSAs + HashSlhDsaSha2_128s, + HashSlhDsaSha2_128f, + HashSlhDsaSha2_192s, + HashSlhDsaSha2_192f, + HashSlhDsaSha2_256s, + HashSlhDsaSha2_256f, + HashSlhDsaShake128s, + HashSlhDsaShake128f, + HashSlhDsaShake192s, + HashSlhDsaShake192f, + HashSlhDsaShake256s, + HashSlhDsaShake256f, } impl DsaAlgorithm { @@ -161,6 +175,19 @@ impl DsaAlgorithm { DsaAlgorithm::SlhDsaShake192f => Some(PrehashDsaType::SlhDsaShake192f), DsaAlgorithm::SlhDsaShake256s => Some(PrehashDsaType::SlhDsaShake256s), DsaAlgorithm::SlhDsaShake256f => Some(PrehashDsaType::SlhDsaShake256f), + + DsaAlgorithm::HashSlhDsaSha2_128s => Some(PrehashDsaType::HashSlhDsaSha2_128s), + DsaAlgorithm::HashSlhDsaSha2_128f => Some(PrehashDsaType::HashSlhDsaSha2_128f), + DsaAlgorithm::HashSlhDsaSha2_192s => Some(PrehashDsaType::HashSlhDsaSha2_192s), + DsaAlgorithm::HashSlhDsaSha2_192f => Some(PrehashDsaType::HashSlhDsaSha2_192f), + DsaAlgorithm::HashSlhDsaSha2_256s => Some(PrehashDsaType::HashSlhDsaSha2_256s), + DsaAlgorithm::HashSlhDsaSha2_256f => Some(PrehashDsaType::HashSlhDsaSha2_256f), + DsaAlgorithm::HashSlhDsaShake128s => Some(PrehashDsaType::HashSlhDsaShake128s), + DsaAlgorithm::HashSlhDsaShake128f => Some(PrehashDsaType::HashSlhDsaShake128f), + DsaAlgorithm::HashSlhDsaShake192s => Some(PrehashDsaType::HashSlhDsaShake192s), + DsaAlgorithm::HashSlhDsaShake192f => Some(PrehashDsaType::HashSlhDsaShake192f), + DsaAlgorithm::HashSlhDsaShake256s => Some(PrehashDsaType::HashSlhDsaShake256s), + DsaAlgorithm::HashSlhDsaShake256f => Some(PrehashDsaType::HashSlhDsaShake256f), } } @@ -178,6 +205,30 @@ impl DsaAlgorithm { | DsaAlgorithm::HashMlDsa44 | DsaAlgorithm::HashMlDsa65 | DsaAlgorithm::HashMlDsa87 + | DsaAlgorithm::SlhDsaSha2_128s + | DsaAlgorithm::SlhDsaSha2_128f + | DsaAlgorithm::SlhDsaSha2_192s + | DsaAlgorithm::SlhDsaSha2_192f + | DsaAlgorithm::SlhDsaSha2_256s + | DsaAlgorithm::SlhDsaSha2_256f + | DsaAlgorithm::SlhDsaShake128s + | DsaAlgorithm::SlhDsaShake128f + | DsaAlgorithm::SlhDsaShake192s + | DsaAlgorithm::SlhDsaShake192f + | DsaAlgorithm::SlhDsaShake256s + | DsaAlgorithm::SlhDsaShake256f + | DsaAlgorithm::HashSlhDsaSha2_128s + | DsaAlgorithm::HashSlhDsaSha2_128f + | DsaAlgorithm::HashSlhDsaSha2_192s + | DsaAlgorithm::HashSlhDsaSha2_192f + | DsaAlgorithm::HashSlhDsaSha2_256s + | DsaAlgorithm::HashSlhDsaSha2_256f + | DsaAlgorithm::HashSlhDsaShake128s + | DsaAlgorithm::HashSlhDsaShake128f + | DsaAlgorithm::HashSlhDsaShake192s + | DsaAlgorithm::HashSlhDsaShake192f + | DsaAlgorithm::HashSlhDsaShake256s + | DsaAlgorithm::HashSlhDsaShake256f ) } diff --git a/src/dsa/common/config/hash_type.rs b/src/dsa/common/config/hash_type.rs index 3cc1b77..f900013 100644 --- a/src/dsa/common/config/hash_type.rs +++ b/src/dsa/common/config/hash_type.rs @@ -72,6 +72,20 @@ impl HashTypeConfig for PrehashDsaType { PrehashDsaType::SlhDsaShake192f => None, PrehashDsaType::SlhDsaShake256s => None, PrehashDsaType::SlhDsaShake256f => None, + + // Prehash SLH-DSA + PrehashDsaType::HashSlhDsaSha2_128s => Some(HashType::Sha256), + PrehashDsaType::HashSlhDsaSha2_128f => Some(HashType::Sha256), + PrehashDsaType::HashSlhDsaSha2_192s => Some(HashType::Sha512), + PrehashDsaType::HashSlhDsaSha2_192f => Some(HashType::Sha512), + PrehashDsaType::HashSlhDsaSha2_256s => Some(HashType::Sha512), + PrehashDsaType::HashSlhDsaSha2_256f => Some(HashType::Sha512), + PrehashDsaType::HashSlhDsaShake128s => Some(HashType::Shake128), + PrehashDsaType::HashSlhDsaShake128f => Some(HashType::Shake128), + PrehashDsaType::HashSlhDsaShake192s => Some(HashType::Shake256), + PrehashDsaType::HashSlhDsaShake192f => Some(HashType::Shake256), + PrehashDsaType::HashSlhDsaShake256s => Some(HashType::Shake256), + PrehashDsaType::HashSlhDsaShake256f => Some(HashType::Shake256), } } } diff --git a/src/dsa/common/config/oids.rs b/src/dsa/common/config/oids.rs index f0b1ec8..fcb9472 100644 --- a/src/dsa/common/config/oids.rs +++ b/src/dsa/common/config/oids.rs @@ -100,6 +100,20 @@ impl Oid for PrehashDsaType { PrehashDsaType::SlhDsaShake192f => "2.16.840.1.101.3.4.3.29", PrehashDsaType::SlhDsaShake256s => "2.16.840.1.101.3.4.3.30", PrehashDsaType::SlhDsaShake256f => "2.16.840.1.101.3.4.3.31", + + // Prehash SLH-DSA + PrehashDsaType::HashSlhDsaSha2_128s => "2.16.840.1.101.3.4.3.35", + PrehashDsaType::HashSlhDsaSha2_128f => "2.16.840.1.101.3.4.3.36", + PrehashDsaType::HashSlhDsaSha2_192s => "2.16.840.1.101.3.4.3.37", + PrehashDsaType::HashSlhDsaSha2_192f => "2.16.840.1.101.3.4.3.38", + PrehashDsaType::HashSlhDsaSha2_256s => "2.16.840.1.101.3.4.3.39", + PrehashDsaType::HashSlhDsaSha2_256f => "2.16.840.1.101.3.4.3.40", + PrehashDsaType::HashSlhDsaShake128s => "2.16.840.1.101.3.4.3.41", + PrehashDsaType::HashSlhDsaShake128f => "2.16.840.1.101.3.4.3.42", + PrehashDsaType::HashSlhDsaShake192s => "2.16.840.1.101.3.4.3.43", + PrehashDsaType::HashSlhDsaShake192f => "2.16.840.1.101.3.4.3.44", + PrehashDsaType::HashSlhDsaShake256s => "2.16.840.1.101.3.4.3.45", + PrehashDsaType::HashSlhDsaShake256f => "2.16.840.1.101.3.4.3.46", } .to_string() } diff --git a/src/dsa/common/config/pk_len.rs b/src/dsa/common/config/pk_len.rs index ca01389..f620848 100644 --- a/src/dsa/common/config/pk_len.rs +++ b/src/dsa/common/config/pk_len.rs @@ -79,6 +79,7 @@ impl PKLen for PrehashDsaType { PrehashDsaType::HashMlDsa87EcdsaBrainpoolP384r1Sha512 => Some(2592 + 97 + 12), // 2701 PrehashDsaType::HashMlDsa87Ed448Sha512 => Some(2592 + 57 + 12), // 2523 + // Pure SLH-DSA PrehashDsaType::SlhDsaSha2_128s => Some(32), PrehashDsaType::SlhDsaSha2_128f => Some(32), PrehashDsaType::SlhDsaSha2_192s => Some(48), @@ -91,6 +92,20 @@ impl PKLen for PrehashDsaType { PrehashDsaType::SlhDsaShake192f => Some(48), PrehashDsaType::SlhDsaShake256s => Some(64), PrehashDsaType::SlhDsaShake256f => Some(64), + + // Prehash SLH-DSA + PrehashDsaType::HashSlhDsaSha2_128s => None, + PrehashDsaType::HashSlhDsaSha2_128f => None, + PrehashDsaType::HashSlhDsaSha2_192s => None, + PrehashDsaType::HashSlhDsaSha2_192f => None, + PrehashDsaType::HashSlhDsaSha2_256s => None, + PrehashDsaType::HashSlhDsaSha2_256f => None, + PrehashDsaType::HashSlhDsaShake128s => None, + PrehashDsaType::HashSlhDsaShake128f => None, + PrehashDsaType::HashSlhDsaShake192s => None, + PrehashDsaType::HashSlhDsaShake192f => None, + PrehashDsaType::HashSlhDsaShake256s => None, + PrehashDsaType::HashSlhDsaShake256f => None, } } } diff --git a/src/dsa/common/config/sig_len.rs b/src/dsa/common/config/sig_len.rs index 69f914d..739a65b 100644 --- a/src/dsa/common/config/sig_len.rs +++ b/src/dsa/common/config/sig_len.rs @@ -94,6 +94,20 @@ impl SigLen for PrehashDsaType { PrehashDsaType::SlhDsaShake192f => Some(35664), PrehashDsaType::SlhDsaShake256s => Some(29792), PrehashDsaType::SlhDsaShake256f => Some(49856), + + // Prehash SLH-DSA + PrehashDsaType::HashSlhDsaSha2_128s => None, + PrehashDsaType::HashSlhDsaSha2_128f => None, + PrehashDsaType::HashSlhDsaSha2_192s => None, + PrehashDsaType::HashSlhDsaSha2_192f => None, + PrehashDsaType::HashSlhDsaSha2_256s => None, + PrehashDsaType::HashSlhDsaSha2_256f => None, + PrehashDsaType::HashSlhDsaShake128s => None, + PrehashDsaType::HashSlhDsaShake128f => None, + PrehashDsaType::HashSlhDsaShake192s => None, + PrehashDsaType::HashSlhDsaShake192f => None, + PrehashDsaType::HashSlhDsaShake256s => None, + PrehashDsaType::HashSlhDsaShake256f => None, } } } diff --git a/src/dsa/common/config/sk_len.rs b/src/dsa/common/config/sk_len.rs index c747bb9..3af3d04 100644 --- a/src/dsa/common/config/sk_len.rs +++ b/src/dsa/common/config/sk_len.rs @@ -92,6 +92,20 @@ impl SKLen for PrehashDsaType { PrehashDsaType::SlhDsaShake192f => Some(48 * 2), PrehashDsaType::SlhDsaShake256s => Some(64 * 2), PrehashDsaType::SlhDsaShake256f => Some(64 * 2), + + // Prehash SLH-DSA + PrehashDsaType::HashSlhDsaSha2_128s => None, + PrehashDsaType::HashSlhDsaSha2_128f => None, + PrehashDsaType::HashSlhDsaSha2_192s => None, + PrehashDsaType::HashSlhDsaSha2_192f => None, + PrehashDsaType::HashSlhDsaSha2_256s => None, + PrehashDsaType::HashSlhDsaSha2_256f => None, + PrehashDsaType::HashSlhDsaShake128s => None, + PrehashDsaType::HashSlhDsaShake128f => None, + PrehashDsaType::HashSlhDsaShake192s => None, + PrehashDsaType::HashSlhDsaShake192f => None, + PrehashDsaType::HashSlhDsaShake256s => None, + PrehashDsaType::HashSlhDsaShake256f => None, } } } diff --git a/src/dsa/common/prehash_dsa_type.rs b/src/dsa/common/prehash_dsa_type.rs index 4039563..9df9b7d 100644 --- a/src/dsa/common/prehash_dsa_type.rs +++ b/src/dsa/common/prehash_dsa_type.rs @@ -48,7 +48,6 @@ pub enum PrehashDsaType { HashMlDsa87Ed448Sha512, // Pure SLH-DSA - // SLH DSA SlhDsaSha2_128s, SlhDsaSha2_128f, SlhDsaSha2_192s, @@ -61,6 +60,20 @@ pub enum PrehashDsaType { SlhDsaShake192f, SlhDsaShake256s, SlhDsaShake256f, + + // Prehash SLH-DSA + HashSlhDsaSha2_128s, + HashSlhDsaSha2_128f, + HashSlhDsaSha2_192s, + HashSlhDsaSha2_192f, + HashSlhDsaSha2_256s, + HashSlhDsaSha2_256f, + HashSlhDsaShake128s, + HashSlhDsaShake128f, + HashSlhDsaShake192s, + HashSlhDsaShake192f, + HashSlhDsaShake256s, + HashSlhDsaShake256f, } impl PrehashDsaType { diff --git a/src/dsa/dsa_manager.rs b/src/dsa/dsa_manager.rs index 54b8020..f170298 100644 --- a/src/dsa/dsa_manager.rs +++ b/src/dsa/dsa_manager.rs @@ -72,7 +72,7 @@ const COMPOSITE_DSA_TYPES: [PrehashDsaType; 28] = [ PrehashDsaType::HashMlDsa65Rsa4096Pkcs15Sha512, ]; -const SLH_DSA_TYPES: [PrehashDsaType; 12] = [ +const SLH_DSA_TYPES: [PrehashDsaType; 24] = [ PrehashDsaType::SlhDsaSha2_128s, PrehashDsaType::SlhDsaSha2_128f, PrehashDsaType::SlhDsaSha2_192s, @@ -85,6 +85,18 @@ const SLH_DSA_TYPES: [PrehashDsaType; 12] = [ PrehashDsaType::SlhDsaShake192f, PrehashDsaType::SlhDsaShake256s, PrehashDsaType::SlhDsaShake256f, + PrehashDsaType::HashSlhDsaSha2_128s, + PrehashDsaType::HashSlhDsaSha2_128f, + PrehashDsaType::HashSlhDsaSha2_192s, + PrehashDsaType::HashSlhDsaSha2_192f, + PrehashDsaType::HashSlhDsaSha2_256s, + PrehashDsaType::HashSlhDsaSha2_256f, + PrehashDsaType::HashSlhDsaShake128s, + PrehashDsaType::HashSlhDsaShake128f, + PrehashDsaType::HashSlhDsaShake192s, + PrehashDsaType::HashSlhDsaShake192f, + PrehashDsaType::HashSlhDsaShake256s, + PrehashDsaType::HashSlhDsaShake256f, ]; // Implement clone diff --git a/src/dsa/ml_dsa.rs b/src/dsa/ml_dsa.rs index cdbffe4..39efce7 100644 --- a/src/dsa/ml_dsa.rs +++ b/src/dsa/ml_dsa.rs @@ -33,6 +33,7 @@ macro_rules! sign_ml { let ph = match (hash_type) { HashType::Sha256 => Ph::SHA256, HashType::Sha512 => Ph::SHA512, + _ => return Err(QuantCryptError::NotImplemented), }; // Try signing the message sk.try_hash_sign($msg, $ctx, &ph) @@ -72,6 +73,7 @@ macro_rules! verify_ml { let ph = match (hash_type) { HashType::Sha256 => Ph::SHA256, HashType::Sha512 => Ph::SHA512, + _ => return Err(QuantCryptError::NotImplemented), }; Ok(pk.hash_verify($msg, &sig_buf, $ctx, &ph)) diff --git a/src/dsa/slh_dsa.rs b/src/dsa/slh_dsa.rs index 88717c1..0081d9a 100644 --- a/src/dsa/slh_dsa.rs +++ b/src/dsa/slh_dsa.rs @@ -16,15 +16,17 @@ use fips205::slh_dsa_shake_192s; use fips205::slh_dsa_shake_256f; use fips205::slh_dsa_shake_256s; use fips205::traits::{SerDes, Signer, Verifier}; +use fips205::Ph; use super::common::prehash_dsa_info::PrehashDsaInfo; use super::common::prehash_dsa_trait::PrehashDsa; use super::common::prehash_dsa_type::PrehashDsaType; +use crate::hash::common::hash_type::HashType; type Result = std::result::Result; macro_rules! sign_slh { - ($sig_type:ident, $sk:expr, $msg:expr, $ctx:expr) => {{ + ($self:expr, $sig_type:ident, $sk:expr, $msg:expr, $ctx:expr) => {{ if $sk.len() != $sig_type::SK_LEN { return Err(QuantCryptError::InvalidPrivateKey); } @@ -38,10 +40,21 @@ macro_rules! sign_slh { .map_err(|_| QuantCryptError::SignatureFailed)?; // Try signing the message - let sig = sk - .try_sign($msg, $ctx, true) - .map_err(|_| QuantCryptError::SignatureFailed)?; - + let sig = if let Some(hash_type) = $self.dsa_info.hash_type.clone() { + let ph = match (hash_type) { + HashType::Sha256 => Ph::SHA256, + HashType::Sha512 => Ph::SHA512, + HashType::Shake128 => Ph::SHAKE128, + HashType::Shake256 => Ph::SHAKE256, + }; + // Try signing the message + sk.try_hash_sign($msg, $ctx, &ph, true) + .map_err(|_| QuantCryptError::SignatureFailed)? + } else { + // Try signing the message + sk.try_sign($msg, $ctx, true) + .map_err(|_| QuantCryptError::SignatureFailed)? + }; // Convert the signature to a Vec and return it let sig: Vec = sig.to_vec(); Ok(sig) @@ -49,7 +62,7 @@ macro_rules! sign_slh { } macro_rules! verify_slh { - ($sig_type:ident, $pk: expr, $msg: expr, $signature: expr, $ctx: expr) => {{ + ($self:expr, $sig_type:ident, $pk: expr, $msg: expr, $signature: expr, $ctx: expr) => {{ if $pk.len() != $sig_type::PK_LEN { return Err(QuantCryptError::InvalidPublicKey); } @@ -68,9 +81,18 @@ macro_rules! verify_slh { let pk = $sig_type::PublicKey::try_from_bytes(&pk_buf) .map_err(|_| QuantCryptError::InvalidPublicKey)?; - let result = Ok(pk.verify($msg, &sig_buf, $ctx)); - - result + if let Some(hash_type) = $self.dsa_info.hash_type.clone() { + let ph = match (hash_type) { + HashType::Sha256 => Ph::SHA256, + HashType::Sha512 => Ph::SHA512, + HashType::Shake128 => Ph::SHAKE128, + HashType::Shake256 => Ph::SHAKE256, + }; + + Ok(pk.hash_verify($msg, &sig_buf, $ctx, &ph)) + } else { + Ok(pk.verify($msg, &sig_buf, $ctx)) + } }}; } @@ -140,6 +162,19 @@ impl PrehashDsa for SlhDsaManager { PrehashDsaType::SlhDsaShake192f => keygen_slh!(slh_dsa_shake_192f, rng), PrehashDsaType::SlhDsaShake256s => keygen_slh!(slh_dsa_shake_256s, rng), PrehashDsaType::SlhDsaShake256f => keygen_slh!(slh_dsa_shake_256f, rng), + + PrehashDsaType::HashSlhDsaSha2_128s => keygen_slh!(slh_dsa_sha2_128s, rng), + PrehashDsaType::HashSlhDsaSha2_128f => keygen_slh!(slh_dsa_sha2_128f, rng), + PrehashDsaType::HashSlhDsaSha2_192s => keygen_slh!(slh_dsa_sha2_192s, rng), + PrehashDsaType::HashSlhDsaSha2_192f => keygen_slh!(slh_dsa_sha2_192f, rng), + PrehashDsaType::HashSlhDsaSha2_256s => keygen_slh!(slh_dsa_sha2_256s, rng), + PrehashDsaType::HashSlhDsaSha2_256f => keygen_slh!(slh_dsa_sha2_256f, rng), + PrehashDsaType::HashSlhDsaShake128s => keygen_slh!(slh_dsa_shake_128s, rng), + PrehashDsaType::HashSlhDsaShake128f => keygen_slh!(slh_dsa_shake_128f, rng), + PrehashDsaType::HashSlhDsaShake192s => keygen_slh!(slh_dsa_shake_192s, rng), + PrehashDsaType::HashSlhDsaShake192f => keygen_slh!(slh_dsa_shake_192f, rng), + PrehashDsaType::HashSlhDsaShake256s => keygen_slh!(slh_dsa_shake_256s, rng), + PrehashDsaType::HashSlhDsaShake256f => keygen_slh!(slh_dsa_shake_256f, rng), _ => Err(QuantCryptError::NotImplemented), } } @@ -174,6 +209,19 @@ impl PrehashDsa for SlhDsaManager { PrehashDsaType::SlhDsaShake192s => get_public_key!(slh_dsa_shake_192s, sk), PrehashDsaType::SlhDsaShake256f => get_public_key!(slh_dsa_shake_256f, sk), PrehashDsaType::SlhDsaShake256s => get_public_key!(slh_dsa_shake_256s, sk), + + PrehashDsaType::HashSlhDsaSha2_128f => get_public_key!(slh_dsa_sha2_128f, sk), + PrehashDsaType::HashSlhDsaSha2_128s => get_public_key!(slh_dsa_sha2_128s, sk), + PrehashDsaType::HashSlhDsaSha2_192f => get_public_key!(slh_dsa_sha2_192f, sk), + PrehashDsaType::HashSlhDsaSha2_192s => get_public_key!(slh_dsa_sha2_192s, sk), + PrehashDsaType::HashSlhDsaSha2_256f => get_public_key!(slh_dsa_sha2_256f, sk), + PrehashDsaType::HashSlhDsaSha2_256s => get_public_key!(slh_dsa_sha2_256s, sk), + PrehashDsaType::HashSlhDsaShake128f => get_public_key!(slh_dsa_shake_128f, sk), + PrehashDsaType::HashSlhDsaShake128s => get_public_key!(slh_dsa_shake_128s, sk), + PrehashDsaType::HashSlhDsaShake192f => get_public_key!(slh_dsa_shake_192f, sk), + PrehashDsaType::HashSlhDsaShake192s => get_public_key!(slh_dsa_shake_192s, sk), + PrehashDsaType::HashSlhDsaShake256f => get_public_key!(slh_dsa_shake_256f, sk), + PrehashDsaType::HashSlhDsaShake256s => get_public_key!(slh_dsa_shake_256s, sk), _ => Err(QuantCryptError::NotImplemented), } } @@ -192,18 +240,55 @@ impl PrehashDsa for SlhDsaManager { fn sign_with_ctx(&self, sk: &[u8], msg: &[u8], ctx: Option<&[u8]>) -> Result> { let ctx = ctx.unwrap_or(&[]); match self.dsa_info.dsa_type { - PrehashDsaType::SlhDsaSha2_128s => sign_slh!(slh_dsa_sha2_128s, &sk, msg, ctx), - PrehashDsaType::SlhDsaSha2_128f => sign_slh!(slh_dsa_sha2_128f, &sk, msg, ctx), - PrehashDsaType::SlhDsaSha2_192s => sign_slh!(slh_dsa_sha2_192s, &sk, msg, ctx), - PrehashDsaType::SlhDsaSha2_192f => sign_slh!(slh_dsa_sha2_192f, &sk, msg, ctx), - PrehashDsaType::SlhDsaSha2_256s => sign_slh!(slh_dsa_sha2_256s, &sk, msg, ctx), - PrehashDsaType::SlhDsaSha2_256f => sign_slh!(slh_dsa_sha2_256f, &sk, msg, ctx), - PrehashDsaType::SlhDsaShake128s => sign_slh!(slh_dsa_shake_128s, &sk, msg, ctx), - PrehashDsaType::SlhDsaShake128f => sign_slh!(slh_dsa_shake_128f, &sk, msg, ctx), - PrehashDsaType::SlhDsaShake192s => sign_slh!(slh_dsa_shake_192s, &sk, msg, ctx), - PrehashDsaType::SlhDsaShake192f => sign_slh!(slh_dsa_shake_192f, &sk, msg, ctx), - PrehashDsaType::SlhDsaShake256s => sign_slh!(slh_dsa_shake_256s, &sk, msg, ctx), - PrehashDsaType::SlhDsaShake256f => sign_slh!(slh_dsa_shake_256f, &sk, msg, ctx), + PrehashDsaType::SlhDsaSha2_128s => sign_slh!(self, slh_dsa_sha2_128s, &sk, msg, ctx), + PrehashDsaType::SlhDsaSha2_128f => sign_slh!(self, slh_dsa_sha2_128f, &sk, msg, ctx), + PrehashDsaType::SlhDsaSha2_192s => sign_slh!(self, slh_dsa_sha2_192s, &sk, msg, ctx), + PrehashDsaType::SlhDsaSha2_192f => sign_slh!(self, slh_dsa_sha2_192f, &sk, msg, ctx), + PrehashDsaType::SlhDsaSha2_256s => sign_slh!(self, slh_dsa_sha2_256s, &sk, msg, ctx), + PrehashDsaType::SlhDsaSha2_256f => sign_slh!(self, slh_dsa_sha2_256f, &sk, msg, ctx), + PrehashDsaType::SlhDsaShake128s => sign_slh!(self, slh_dsa_shake_128s, &sk, msg, ctx), + PrehashDsaType::SlhDsaShake128f => sign_slh!(self, slh_dsa_shake_128f, &sk, msg, ctx), + PrehashDsaType::SlhDsaShake192s => sign_slh!(self, slh_dsa_shake_192s, &sk, msg, ctx), + PrehashDsaType::SlhDsaShake192f => sign_slh!(self, slh_dsa_shake_192f, &sk, msg, ctx), + PrehashDsaType::SlhDsaShake256s => sign_slh!(self, slh_dsa_shake_256s, &sk, msg, ctx), + PrehashDsaType::SlhDsaShake256f => sign_slh!(self, slh_dsa_shake_256f, &sk, msg, ctx), + + PrehashDsaType::HashSlhDsaSha2_128s => { + sign_slh!(self, slh_dsa_sha2_128s, &sk, msg, ctx) + } + PrehashDsaType::HashSlhDsaSha2_128f => { + sign_slh!(self, slh_dsa_sha2_128f, &sk, msg, ctx) + } + PrehashDsaType::HashSlhDsaSha2_192s => { + sign_slh!(self, slh_dsa_sha2_192s, &sk, msg, ctx) + } + PrehashDsaType::HashSlhDsaSha2_192f => { + sign_slh!(self, slh_dsa_sha2_192f, &sk, msg, ctx) + } + PrehashDsaType::HashSlhDsaSha2_256s => { + sign_slh!(self, slh_dsa_sha2_256s, &sk, msg, ctx) + } + PrehashDsaType::HashSlhDsaSha2_256f => { + sign_slh!(self, slh_dsa_sha2_256f, &sk, msg, ctx) + } + PrehashDsaType::HashSlhDsaShake128s => { + sign_slh!(self, slh_dsa_shake_128s, &sk, msg, ctx) + } + PrehashDsaType::HashSlhDsaShake128f => { + sign_slh!(self, slh_dsa_shake_128f, &sk, msg, ctx) + } + PrehashDsaType::HashSlhDsaShake192s => { + sign_slh!(self, slh_dsa_shake_192s, &sk, msg, ctx) + } + PrehashDsaType::HashSlhDsaShake192f => { + sign_slh!(self, slh_dsa_shake_192f, &sk, msg, ctx) + } + PrehashDsaType::HashSlhDsaShake256s => { + sign_slh!(self, slh_dsa_shake_256s, &sk, msg, ctx) + } + PrehashDsaType::HashSlhDsaShake256f => { + sign_slh!(self, slh_dsa_shake_256f, &sk, msg, ctx) + } _ => Err(QuantCryptError::NotImplemented), } } @@ -230,40 +315,77 @@ impl PrehashDsa for SlhDsaManager { let ctx = ctx.unwrap_or(&[]); match self.dsa_info.dsa_type { PrehashDsaType::SlhDsaSha2_128f => { - verify_slh!(slh_dsa_sha2_128f, pk, msg, signature, ctx) + verify_slh!(self, slh_dsa_sha2_128f, pk, msg, signature, ctx) } PrehashDsaType::SlhDsaSha2_128s => { - verify_slh!(slh_dsa_sha2_128s, pk, msg, signature, ctx) + verify_slh!(self, slh_dsa_sha2_128s, pk, msg, signature, ctx) } PrehashDsaType::SlhDsaSha2_192f => { - verify_slh!(slh_dsa_sha2_192f, pk, msg, signature, ctx) + verify_slh!(self, slh_dsa_sha2_192f, pk, msg, signature, ctx) } PrehashDsaType::SlhDsaSha2_192s => { - verify_slh!(slh_dsa_sha2_192s, pk, msg, signature, ctx) + verify_slh!(self, slh_dsa_sha2_192s, pk, msg, signature, ctx) } PrehashDsaType::SlhDsaSha2_256f => { - verify_slh!(slh_dsa_sha2_256f, pk, msg, signature, ctx) + verify_slh!(self, slh_dsa_sha2_256f, pk, msg, signature, ctx) } PrehashDsaType::SlhDsaSha2_256s => { - verify_slh!(slh_dsa_sha2_256s, pk, msg, signature, ctx) + verify_slh!(self, slh_dsa_sha2_256s, pk, msg, signature, ctx) } PrehashDsaType::SlhDsaShake128f => { - verify_slh!(slh_dsa_shake_128f, pk, msg, signature, ctx) + verify_slh!(self, slh_dsa_shake_128f, pk, msg, signature, ctx) } PrehashDsaType::SlhDsaShake128s => { - verify_slh!(slh_dsa_shake_128s, pk, msg, signature, ctx) + verify_slh!(self, slh_dsa_shake_128s, pk, msg, signature, ctx) } PrehashDsaType::SlhDsaShake192f => { - verify_slh!(slh_dsa_shake_192f, pk, msg, signature, ctx) + verify_slh!(self, slh_dsa_shake_192f, pk, msg, signature, ctx) } PrehashDsaType::SlhDsaShake192s => { - verify_slh!(slh_dsa_shake_192s, pk, msg, signature, ctx) + verify_slh!(self, slh_dsa_shake_192s, pk, msg, signature, ctx) } PrehashDsaType::SlhDsaShake256f => { - verify_slh!(slh_dsa_shake_256f, pk, msg, signature, ctx) + verify_slh!(self, slh_dsa_shake_256f, pk, msg, signature, ctx) } PrehashDsaType::SlhDsaShake256s => { - verify_slh!(slh_dsa_shake_256s, pk, msg, signature, ctx) + verify_slh!(self, slh_dsa_shake_256s, pk, msg, signature, ctx) + } + // Prehash SLH-DSA + PrehashDsaType::HashSlhDsaSha2_128f => { + verify_slh!(self, slh_dsa_sha2_128f, pk, msg, signature, ctx) + } + PrehashDsaType::HashSlhDsaSha2_128s => { + verify_slh!(self, slh_dsa_sha2_128s, pk, msg, signature, ctx) + } + PrehashDsaType::HashSlhDsaSha2_192f => { + verify_slh!(self, slh_dsa_sha2_192f, pk, msg, signature, ctx) + } + PrehashDsaType::HashSlhDsaSha2_192s => { + verify_slh!(self, slh_dsa_sha2_192s, pk, msg, signature, ctx) + } + PrehashDsaType::HashSlhDsaSha2_256f => { + verify_slh!(self, slh_dsa_sha2_256f, pk, msg, signature, ctx) + } + PrehashDsaType::HashSlhDsaSha2_256s => { + verify_slh!(self, slh_dsa_sha2_256s, pk, msg, signature, ctx) + } + PrehashDsaType::HashSlhDsaShake128f => { + verify_slh!(self, slh_dsa_shake_128f, pk, msg, signature, ctx) + } + PrehashDsaType::HashSlhDsaShake128s => { + verify_slh!(self, slh_dsa_shake_128s, pk, msg, signature, ctx) + } + PrehashDsaType::HashSlhDsaShake192f => { + verify_slh!(self, slh_dsa_shake_192f, pk, msg, signature, ctx) + } + PrehashDsaType::HashSlhDsaShake192s => { + verify_slh!(self, slh_dsa_shake_192s, pk, msg, signature, ctx) + } + PrehashDsaType::HashSlhDsaShake256f => { + verify_slh!(self, slh_dsa_shake_256f, pk, msg, signature, ctx) + } + PrehashDsaType::HashSlhDsaShake256s => { + verify_slh!(self, slh_dsa_shake_256s, pk, msg, signature, ctx) } _ => Err(QuantCryptError::NotImplemented), } @@ -274,12 +396,12 @@ impl PrehashDsa for SlhDsaManager { mod tests { use super::*; use crate::certificates::Certificate; - use crate::dsa::common::macros::test_dsa; + use crate::dsa::common::macros::test_prehash_dsa; #[test] fn test_slh_dsa_sha2_128s() { let dsa = SlhDsaManager::new(PrehashDsaType::SlhDsaSha2_128s); - test_dsa!(dsa); + test_prehash_dsa!(dsa); let cert_bytes = include_bytes!("../../test/data/slh/slh-dsa-sha2-128s-2.16.840.1.101.3.4.3.20_ta.der"); @@ -290,7 +412,7 @@ mod tests { #[test] fn test_slh_dsa_sha2_128f() { let dsa = SlhDsaManager::new(PrehashDsaType::SlhDsaSha2_128f); - test_dsa!(dsa); + test_prehash_dsa!(dsa); let cert_bytes = include_bytes!("../../test/data/slh/slh-dsa-sha2-128f-2.16.840.1.101.3.4.3.21_ta.der"); @@ -301,7 +423,7 @@ mod tests { #[test] fn test_slh_dsa_sha2_192s() { let dsa = SlhDsaManager::new(PrehashDsaType::SlhDsaSha2_192s); - test_dsa!(dsa); + test_prehash_dsa!(dsa); let cert_bytes = include_bytes!("../../test/data/slh/slh-dsa-sha2-192s-2.16.840.1.101.3.4.3.22_ta.der"); @@ -312,7 +434,7 @@ mod tests { #[test] fn test_slh_dsa_sha2_192f() { let dsa = SlhDsaManager::new(PrehashDsaType::SlhDsaSha2_192f); - test_dsa!(dsa); + test_prehash_dsa!(dsa); let cert_bytes = include_bytes!("../../test/data/slh/slh-dsa-sha2-192f-2.16.840.1.101.3.4.3.23_ta.der"); @@ -323,7 +445,7 @@ mod tests { #[test] fn test_slh_dsa_sha2_256s() { let dsa = SlhDsaManager::new(PrehashDsaType::SlhDsaSha2_256s); - test_dsa!(dsa); + test_prehash_dsa!(dsa); let cert_bytes = include_bytes!("../../test/data/slh/slh-dsa-sha2-256s-2.16.840.1.101.3.4.3.24_ta.der"); @@ -334,7 +456,7 @@ mod tests { #[test] fn test_slh_dsa_sha2_256f() { let dsa = SlhDsaManager::new(PrehashDsaType::SlhDsaSha2_256f); - test_dsa!(dsa); + test_prehash_dsa!(dsa); let cert_bytes = include_bytes!("../../test/data/slh/slh-dsa-sha2-256f-2.16.840.1.101.3.4.3.25_ta.der"); @@ -345,7 +467,7 @@ mod tests { #[test] fn test_slh_dsa_shake_128s() { let dsa = SlhDsaManager::new(PrehashDsaType::SlhDsaShake128s); - test_dsa!(dsa); + test_prehash_dsa!(dsa); let cert_bytes = include_bytes!("../../test/data/slh/slh-dsa-shake-128s-2.16.840.1.101.3.4.3.26_ta.der"); @@ -356,7 +478,7 @@ mod tests { #[test] fn test_slh_dsa_shake_128f() { let dsa = SlhDsaManager::new(PrehashDsaType::SlhDsaShake128f); - test_dsa!(dsa); + test_prehash_dsa!(dsa); let cert_bytes = include_bytes!("../../test/data/slh/slh-dsa-shake-128f-2.16.840.1.101.3.4.3.27_ta.der"); @@ -367,7 +489,7 @@ mod tests { #[test] fn test_slh_dsa_shake_192s() { let dsa = SlhDsaManager::new(PrehashDsaType::SlhDsaShake192s); - test_dsa!(dsa); + test_prehash_dsa!(dsa); let cert_bytes = include_bytes!("../../test/data/slh/slh-dsa-shake-192s-2.16.840.1.101.3.4.3.28_ta.der"); @@ -378,7 +500,7 @@ mod tests { #[test] fn test_slh_dsa_shake_192f() { let dsa = SlhDsaManager::new(PrehashDsaType::SlhDsaShake192f); - test_dsa!(dsa); + test_prehash_dsa!(dsa); let cert_bytes = include_bytes!("../../test/data/slh/slh-dsa-shake-192f-2.16.840.1.101.3.4.3.29_ta.der"); @@ -389,7 +511,7 @@ mod tests { #[test] fn test_slh_dsa_shake_256s() { let dsa = SlhDsaManager::new(PrehashDsaType::SlhDsaShake256s); - test_dsa!(dsa); + test_prehash_dsa!(dsa); let cert_bytes = include_bytes!("../../test/data/slh/slh-dsa-shake-256s-2.16.840.1.101.3.4.3.30_ta.der"); @@ -400,11 +522,85 @@ mod tests { #[test] fn test_slh_dsa_shake_256f() { let dsa = SlhDsaManager::new(PrehashDsaType::SlhDsaShake256f); - test_dsa!(dsa); + test_prehash_dsa!(dsa); let cert_bytes = include_bytes!("../../test/data/slh/slh-dsa-shake-256f-2.16.840.1.101.3.4.3.31_ta.der"); let cert = Certificate::from_der(cert_bytes).unwrap(); assert!(cert.verify_self_signed().unwrap()); } + + //////////////////////////////////////// + + #[test] + fn test_slh_dsa_sha2_128s_ph() { + let dsa = SlhDsaManager::new(PrehashDsaType::HashSlhDsaSha2_128s); + test_prehash_dsa!(dsa); + } + + #[test] + fn test_slh_dsa_sha2_128f_ph() { + let dsa = SlhDsaManager::new(PrehashDsaType::HashSlhDsaSha2_128f); + test_prehash_dsa!(dsa); + } + + #[test] + fn test_slh_dsa_sha2_192s_ph() { + let dsa = SlhDsaManager::new(PrehashDsaType::HashSlhDsaSha2_192s); + test_prehash_dsa!(dsa); + } + + #[test] + fn test_slh_dsa_sha2_192f_ph() { + let dsa = SlhDsaManager::new(PrehashDsaType::HashSlhDsaSha2_192f); + test_prehash_dsa!(dsa); + } + + #[test] + fn test_slh_dsa_sha2_256s_ph() { + let dsa = SlhDsaManager::new(PrehashDsaType::HashSlhDsaSha2_256s); + test_prehash_dsa!(dsa); + } + + #[test] + fn test_slh_dsa_sha2_256f_ph() { + let dsa = SlhDsaManager::new(PrehashDsaType::HashSlhDsaSha2_256f); + test_prehash_dsa!(dsa); + } + + #[test] + fn test_slh_dsa_shake_128s_ph() { + let dsa = SlhDsaManager::new(PrehashDsaType::HashSlhDsaShake128s); + test_prehash_dsa!(dsa); + } + + #[test] + fn test_slh_dsa_shake_128f_ph() { + let dsa = SlhDsaManager::new(PrehashDsaType::HashSlhDsaShake128f); + test_prehash_dsa!(dsa); + } + + #[test] + fn test_slh_dsa_shake_192s_ph() { + let dsa = SlhDsaManager::new(PrehashDsaType::HashSlhDsaShake192s); + test_prehash_dsa!(dsa); + } + + #[test] + fn test_slh_dsa_shake_192f_ph() { + let dsa = SlhDsaManager::new(PrehashDsaType::HashSlhDsaShake192f); + test_prehash_dsa!(dsa); + } + + #[test] + fn test_slh_dsa_shake_256s_ph() { + let dsa = SlhDsaManager::new(PrehashDsaType::HashSlhDsaShake256s); + test_prehash_dsa!(dsa); + } + + #[test] + fn test_slh_dsa_shake_256f_ph() { + let dsa = SlhDsaManager::new(PrehashDsaType::HashSlhDsaShake256f); + test_prehash_dsa!(dsa); + } } diff --git a/src/hash/common/config/oids.rs b/src/hash/common/config/oids.rs index 9a88ed9..a0e811d 100644 --- a/src/hash/common/config/oids.rs +++ b/src/hash/common/config/oids.rs @@ -20,6 +20,8 @@ impl Oid for HashType { match self { HashType::Sha256 => "2.16.840.1.101.3.4.2.1", HashType::Sha512 => "2.16.840.1.101.3.4.2.3", + HashType::Shake128 => "2.16.840.1.101.3.4.2.11", + HashType::Shake256 => "2.16.840.1.101.3.4.2.12", } .to_string() } diff --git a/src/hash/common/hash_type.rs b/src/hash/common/hash_type.rs index 5f5266a..de0b4d4 100644 --- a/src/hash/common/hash_type.rs +++ b/src/hash/common/hash_type.rs @@ -7,4 +7,8 @@ pub enum HashType { Sha256, /// SHA512 Sha512, + /// Shake 128 + Shake128, + /// Shake 256 + Shake256, } diff --git a/src/hash/hash_manager.rs b/src/hash/hash_manager.rs index 2521821..ddc4f3d 100644 --- a/src/hash/hash_manager.rs +++ b/src/hash/hash_manager.rs @@ -21,7 +21,9 @@ impl Hash for HashManager { Self: Sized, { match hash_type { - HashType::Sha256 | HashType::Sha512 => Ok(HashManager::Sha(ShaHash::new(hash_type)?)), + HashType::Sha256 | HashType::Sha512 | HashType::Shake128 | HashType::Shake256 => { + Ok(HashManager::Sha(ShaHash::new(hash_type)?)) + } } } diff --git a/src/hash/sha_hash.rs b/src/hash/sha_hash.rs index 20c531c..0f9c0dc 100644 --- a/src/hash/sha_hash.rs +++ b/src/hash/sha_hash.rs @@ -22,6 +22,8 @@ impl Hash for ShaHash { let digest = match hash_type { HashType::Sha256 => MessageDigest::sha256(), HashType::Sha512 => MessageDigest::sha512(), + HashType::Shake128 => MessageDigest::shake_128(), + HashType::Shake256 => MessageDigest::shake_256(), }; Ok(ShaHash { hash_type, digest })