diff --git a/src/dsa/api/algorithm.rs b/src/dsa/api/algorithm.rs index c9ef429..831fae7 100644 --- a/src/dsa/api/algorithm.rs +++ b/src/dsa/api/algorithm.rs @@ -71,21 +71,7 @@ impl DsaAlgorithm { /// Get the corresponding `DsaType` for the algorithm pub(crate) fn get_dsa_type(&self) -> Option { - match self { - DsaAlgorithm::SlhDsaSha2_128s => Some(DsaType::SlhDsaSha2_128s), - DsaAlgorithm::SlhDsaSha2_128f => Some(DsaType::SlhDsaSha2_128f), - DsaAlgorithm::SlhDsaSha2_192s => Some(DsaType::SlhDsaSha2_192s), - DsaAlgorithm::SlhDsaSha2_192f => Some(DsaType::SlhDsaSha2_192f), - DsaAlgorithm::SlhDsaSha2_256s => Some(DsaType::SlhDsaSha2_256s), - DsaAlgorithm::SlhDsaSha2_256f => Some(DsaType::SlhDsaSha2_256f), - DsaAlgorithm::SlhDsaShake128s => Some(DsaType::SlhDsaShake128s), - DsaAlgorithm::SlhDsaShake128f => Some(DsaType::SlhDsaShake128f), - DsaAlgorithm::SlhDsaShake192s => Some(DsaType::SlhDsaShake192s), - DsaAlgorithm::SlhDsaShake192f => Some(DsaType::SlhDsaShake192f), - DsaAlgorithm::SlhDsaShake256s => Some(DsaType::SlhDsaShake256s), - DsaAlgorithm::SlhDsaShake256f => Some(DsaType::SlhDsaShake256f), - _ => None, - } + None } /// Get the corresponding `PrehashDsaType` for the algorithm @@ -162,7 +148,19 @@ impl DsaAlgorithm { Some(PrehashDsaType::HashMlDsa87EcdsaBrainpoolP384r1Sha512) } DsaAlgorithm::HashMlDsa87Ed448Sha512 => Some(PrehashDsaType::HashMlDsa87Ed448Sha512), - _ => None, + + DsaAlgorithm::SlhDsaSha2_128s => Some(PrehashDsaType::SlhDsaSha2_128s), + DsaAlgorithm::SlhDsaSha2_128f => Some(PrehashDsaType::SlhDsaSha2_128f), + DsaAlgorithm::SlhDsaSha2_192s => Some(PrehashDsaType::SlhDsaSha2_192s), + DsaAlgorithm::SlhDsaSha2_192f => Some(PrehashDsaType::SlhDsaSha2_192f), + DsaAlgorithm::SlhDsaSha2_256s => Some(PrehashDsaType::SlhDsaSha2_256s), + DsaAlgorithm::SlhDsaSha2_256f => Some(PrehashDsaType::SlhDsaSha2_256f), + DsaAlgorithm::SlhDsaShake128s => Some(PrehashDsaType::SlhDsaShake128s), + DsaAlgorithm::SlhDsaShake128f => Some(PrehashDsaType::SlhDsaShake128f), + DsaAlgorithm::SlhDsaShake192s => Some(PrehashDsaType::SlhDsaShake192s), + DsaAlgorithm::SlhDsaShake192f => Some(PrehashDsaType::SlhDsaShake192f), + DsaAlgorithm::SlhDsaShake256s => Some(PrehashDsaType::SlhDsaShake256s), + DsaAlgorithm::SlhDsaShake256f => Some(PrehashDsaType::SlhDsaShake256f), } } diff --git a/src/dsa/common/config/hash_type.rs b/src/dsa/common/config/hash_type.rs index 5f974ab..3cc1b77 100644 --- a/src/dsa/common/config/hash_type.rs +++ b/src/dsa/common/config/hash_type.rs @@ -59,6 +59,19 @@ impl HashTypeConfig for PrehashDsaType { PrehashDsaType::HashMlDsa87EcdsaP384Sha512 => Some(HashType::Sha512), PrehashDsaType::HashMlDsa87EcdsaBrainpoolP384r1Sha512 => Some(HashType::Sha512), PrehashDsaType::HashMlDsa87Ed448Sha512 => Some(HashType::Sha512), + + PrehashDsaType::SlhDsaSha2_128s => None, + PrehashDsaType::SlhDsaSha2_128f => None, + PrehashDsaType::SlhDsaSha2_192s => None, + PrehashDsaType::SlhDsaSha2_192f => None, + PrehashDsaType::SlhDsaSha2_256s => None, + PrehashDsaType::SlhDsaSha2_256f => None, + PrehashDsaType::SlhDsaShake128s => None, + PrehashDsaType::SlhDsaShake128f => None, + PrehashDsaType::SlhDsaShake192s => None, + PrehashDsaType::SlhDsaShake192f => None, + PrehashDsaType::SlhDsaShake256s => None, + PrehashDsaType::SlhDsaShake256f => None, } } } diff --git a/src/dsa/common/config/oids.rs b/src/dsa/common/config/oids.rs index d677761..f0b1ec8 100644 --- a/src/dsa/common/config/oids.rs +++ b/src/dsa/common/config/oids.rs @@ -34,19 +34,6 @@ impl Oid for DsaType { DsaType::EcdsaP384SHA384 | DsaType::EcdsaBrainpoolP384r1SHA384 => "1.2.840.10045.4.3.3", DsaType::Ed25519 => "1.3.101.112", DsaType::Ed448 => "1.3.101.113", - - DsaType::SlhDsaSha2_128s => "2.16.840.1.101.3.4.3.20", - DsaType::SlhDsaSha2_128f => "2.16.840.1.101.3.4.3.21", - DsaType::SlhDsaSha2_192s => "2.16.840.1.101.3.4.3.22", - DsaType::SlhDsaSha2_192f => "2.16.840.1.101.3.4.3.23", - DsaType::SlhDsaSha2_256s => "2.16.840.1.101.3.4.3.24", - DsaType::SlhDsaSha2_256f => "2.16.840.1.101.3.4.3.25", - DsaType::SlhDsaShake128s => "2.16.840.1.101.3.4.3.26", - DsaType::SlhDsaShake128f => "2.16.840.1.101.3.4.3.27", - DsaType::SlhDsaShake192s => "2.16.840.1.101.3.4.3.28", - DsaType::SlhDsaShake192f => "2.16.840.1.101.3.4.3.29", - DsaType::SlhDsaShake256s => "2.16.840.1.101.3.4.3.30", - DsaType::SlhDsaShake256f => "2.16.840.1.101.3.4.3.31", } .to_string() } @@ -100,6 +87,19 @@ impl Oid for PrehashDsaType { PrehashDsaType::HashMlDsa87EcdsaP384Sha512 => "2.16.840.1.114027.80.8.1.51", PrehashDsaType::HashMlDsa87EcdsaBrainpoolP384r1Sha512 => "2.16.840.1.114027.80.8.1.52", PrehashDsaType::HashMlDsa87Ed448Sha512 => "2.16.840.1.114027.80.8.1.53", + + PrehashDsaType::SlhDsaSha2_128s => "2.16.840.1.101.3.4.3.20", + PrehashDsaType::SlhDsaSha2_128f => "2.16.840.1.101.3.4.3.21", + PrehashDsaType::SlhDsaSha2_192s => "2.16.840.1.101.3.4.3.22", + PrehashDsaType::SlhDsaSha2_192f => "2.16.840.1.101.3.4.3.23", + PrehashDsaType::SlhDsaSha2_256s => "2.16.840.1.101.3.4.3.24", + PrehashDsaType::SlhDsaSha2_256f => "2.16.840.1.101.3.4.3.25", + PrehashDsaType::SlhDsaShake128s => "2.16.840.1.101.3.4.3.26", + PrehashDsaType::SlhDsaShake128f => "2.16.840.1.101.3.4.3.27", + PrehashDsaType::SlhDsaShake192s => "2.16.840.1.101.3.4.3.28", + 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", } .to_string() } diff --git a/src/dsa/common/config/pk_len.rs b/src/dsa/common/config/pk_len.rs index 886bfa5..ca01389 100644 --- a/src/dsa/common/config/pk_len.rs +++ b/src/dsa/common/config/pk_len.rs @@ -24,19 +24,6 @@ impl PKLen for DsaType { DsaType::EcdsaP256SHA256 => Some(65), DsaType::EcdsaBrainpoolP256r1SHA256 => Some(65), - DsaType::SlhDsaSha2_128s => Some(32), - DsaType::SlhDsaSha2_128f => Some(32), - DsaType::SlhDsaSha2_192s => Some(48), - DsaType::SlhDsaSha2_192f => Some(48), - DsaType::SlhDsaSha2_256s => Some(64), - DsaType::SlhDsaSha2_256f => Some(64), - DsaType::SlhDsaShake128s => Some(32), - DsaType::SlhDsaShake128f => Some(32), - DsaType::SlhDsaShake192s => Some(48), - DsaType::SlhDsaShake192f => Some(48), - DsaType::SlhDsaShake256s => Some(64), - DsaType::SlhDsaShake256f => Some(64), - DsaType::EcdsaP384SHA384 => Some(97), DsaType::EcdsaBrainpoolP384r1SHA384 => Some(97), DsaType::Ed25519 => Some(32), @@ -91,6 +78,19 @@ impl PKLen for PrehashDsaType { PrehashDsaType::HashMlDsa87EcdsaP384Sha512 => Some(2592 + 97 + 12), // 2701 PrehashDsaType::HashMlDsa87EcdsaBrainpoolP384r1Sha512 => Some(2592 + 97 + 12), // 2701 PrehashDsaType::HashMlDsa87Ed448Sha512 => Some(2592 + 57 + 12), // 2523 + + PrehashDsaType::SlhDsaSha2_128s => Some(32), + PrehashDsaType::SlhDsaSha2_128f => Some(32), + PrehashDsaType::SlhDsaSha2_192s => Some(48), + PrehashDsaType::SlhDsaSha2_192f => Some(48), + PrehashDsaType::SlhDsaSha2_256s => Some(64), + PrehashDsaType::SlhDsaSha2_256f => Some(64), + PrehashDsaType::SlhDsaShake128s => Some(32), + PrehashDsaType::SlhDsaShake128f => Some(32), + PrehashDsaType::SlhDsaShake192s => Some(48), + PrehashDsaType::SlhDsaShake192f => Some(48), + PrehashDsaType::SlhDsaShake256s => Some(64), + PrehashDsaType::SlhDsaShake256f => Some(64), } } } diff --git a/src/dsa/common/config/sig_len.rs b/src/dsa/common/config/sig_len.rs index 90c87b4..69f914d 100644 --- a/src/dsa/common/config/sig_len.rs +++ b/src/dsa/common/config/sig_len.rs @@ -25,19 +25,6 @@ impl SigLen for DsaType { DsaType::EcdsaP256SHA256 => None, DsaType::EcdsaBrainpoolP256r1SHA256 => None, - DsaType::SlhDsaSha2_128s => Some(7856), - DsaType::SlhDsaSha2_128f => Some(17088), - DsaType::SlhDsaSha2_192s => Some(16224), - DsaType::SlhDsaSha2_192f => Some(35664), - DsaType::SlhDsaSha2_256s => Some(29792), - DsaType::SlhDsaSha2_256f => Some(49856), - DsaType::SlhDsaShake128s => Some(7856), - DsaType::SlhDsaShake128f => Some(17088), - DsaType::SlhDsaShake192s => Some(16224), - DsaType::SlhDsaShake192f => Some(35664), - DsaType::SlhDsaShake256s => Some(29792), - DsaType::SlhDsaShake256f => Some(49856), - // P256 and P384 variations do not have a fixed sig_len DsaType::EcdsaP384SHA384 => None, DsaType::EcdsaBrainpoolP384r1SHA384 => None, @@ -94,6 +81,19 @@ impl SigLen for PrehashDsaType { PrehashDsaType::HashMlDsa87EcdsaP384Sha512 => None, // None PrehashDsaType::HashMlDsa87EcdsaBrainpoolP384r1Sha512 => None, // None PrehashDsaType::HashMlDsa87Ed448Sha512 => Some(4627 + 114 + 12), // 4753 + + PrehashDsaType::SlhDsaSha2_128s => Some(7856), + PrehashDsaType::SlhDsaSha2_128f => Some(17088), + PrehashDsaType::SlhDsaSha2_192s => Some(16224), + PrehashDsaType::SlhDsaSha2_192f => Some(35664), + PrehashDsaType::SlhDsaSha2_256s => Some(29792), + PrehashDsaType::SlhDsaSha2_256f => Some(49856), + PrehashDsaType::SlhDsaShake128s => Some(7856), + PrehashDsaType::SlhDsaShake128f => Some(17088), + PrehashDsaType::SlhDsaShake192s => Some(16224), + PrehashDsaType::SlhDsaShake192f => Some(35664), + PrehashDsaType::SlhDsaShake256s => Some(29792), + PrehashDsaType::SlhDsaShake256f => Some(49856), } } } diff --git a/src/dsa/common/config/sk_len.rs b/src/dsa/common/config/sk_len.rs index e80ad27..c747bb9 100644 --- a/src/dsa/common/config/sk_len.rs +++ b/src/dsa/common/config/sk_len.rs @@ -25,19 +25,6 @@ impl SKLen for DsaType { DsaType::EcdsaP256SHA256 => Some(32), DsaType::EcdsaBrainpoolP256r1SHA256 => Some(32), - DsaType::SlhDsaSha2_128s => Some(32 * 2), - DsaType::SlhDsaSha2_128f => Some(32 * 2), - DsaType::SlhDsaSha2_192s => Some(48 * 2), - DsaType::SlhDsaSha2_192f => Some(48 * 2), - DsaType::SlhDsaSha2_256s => Some(64 * 2), - DsaType::SlhDsaSha2_256f => Some(64 * 2), - DsaType::SlhDsaShake128s => Some(32 * 2), - DsaType::SlhDsaShake128f => Some(32 * 2), - DsaType::SlhDsaShake192s => Some(48 * 2), - DsaType::SlhDsaShake192f => Some(48 * 2), - DsaType::SlhDsaShake256s => Some(64 * 2), - DsaType::SlhDsaShake256f => Some(64 * 2), - DsaType::EcdsaP384SHA384 => Some(48), DsaType::EcdsaBrainpoolP384r1SHA384 => Some(48), DsaType::Ed25519 => Some(32), @@ -92,6 +79,19 @@ impl SKLen for PrehashDsaType { PrehashDsaType::HashMlDsa87EcdsaP384Sha512 => Some(4896 + 48 + 10), // 4954 PrehashDsaType::HashMlDsa87EcdsaBrainpoolP384r1Sha512 => Some(4896 + 48 + 10), // 4954 PrehashDsaType::HashMlDsa87Ed448Sha512 => Some(4896 + 57 + 10), // 4963 + + PrehashDsaType::SlhDsaSha2_128s => Some(32 * 2), + PrehashDsaType::SlhDsaSha2_128f => Some(32 * 2), + PrehashDsaType::SlhDsaSha2_192s => Some(48 * 2), + PrehashDsaType::SlhDsaSha2_192f => Some(48 * 2), + PrehashDsaType::SlhDsaSha2_256s => Some(64 * 2), + PrehashDsaType::SlhDsaSha2_256f => Some(64 * 2), + PrehashDsaType::SlhDsaShake128s => Some(32 * 2), + PrehashDsaType::SlhDsaShake128f => Some(32 * 2), + PrehashDsaType::SlhDsaShake192s => Some(48 * 2), + PrehashDsaType::SlhDsaShake192f => Some(48 * 2), + PrehashDsaType::SlhDsaShake256s => Some(64 * 2), + PrehashDsaType::SlhDsaShake256f => Some(64 * 2), } } } diff --git a/src/dsa/common/dsa_type.rs b/src/dsa/common/dsa_type.rs index c2989a0..39436b0 100644 --- a/src/dsa/common/dsa_type.rs +++ b/src/dsa/common/dsa_type.rs @@ -20,20 +20,6 @@ pub enum DsaType { Ed25519, Ed448, EcdsaBrainpoolP384r1SHA384, - - // SLH DSA - SlhDsaSha2_128s, - SlhDsaSha2_128f, - SlhDsaSha2_192s, - SlhDsaSha2_192f, - SlhDsaSha2_256s, - SlhDsaSha2_256f, - SlhDsaShake128s, - SlhDsaShake128f, - SlhDsaShake192s, - SlhDsaShake192f, - SlhDsaShake256s, - SlhDsaShake256f, } impl DsaType { diff --git a/src/dsa/common/prehash_dsa_type.rs b/src/dsa/common/prehash_dsa_type.rs index 08c0011..4039563 100644 --- a/src/dsa/common/prehash_dsa_type.rs +++ b/src/dsa/common/prehash_dsa_type.rs @@ -46,6 +46,21 @@ pub enum PrehashDsaType { HashMlDsa87EcdsaP384Sha512, HashMlDsa87EcdsaBrainpoolP384r1Sha512, HashMlDsa87Ed448Sha512, + + // Pure SLH-DSA + // SLH DSA + SlhDsaSha2_128s, + SlhDsaSha2_128f, + SlhDsaSha2_192s, + SlhDsaSha2_192f, + SlhDsaSha2_256s, + SlhDsaSha2_256f, + SlhDsaShake128s, + SlhDsaShake128f, + SlhDsaShake192s, + SlhDsaShake192f, + SlhDsaShake256s, + SlhDsaShake256f, } impl PrehashDsaType { diff --git a/src/dsa/dsa_manager.rs b/src/dsa/dsa_manager.rs index 3abf4ee..54b8020 100644 --- a/src/dsa/dsa_manager.rs +++ b/src/dsa/dsa_manager.rs @@ -72,19 +72,19 @@ const COMPOSITE_DSA_TYPES: [PrehashDsaType; 28] = [ PrehashDsaType::HashMlDsa65Rsa4096Pkcs15Sha512, ]; -const SLH_DSA_TYPES: [DsaType; 12] = [ - DsaType::SlhDsaSha2_128s, - DsaType::SlhDsaSha2_128f, - DsaType::SlhDsaSha2_192s, - DsaType::SlhDsaSha2_192f, - DsaType::SlhDsaSha2_256s, - DsaType::SlhDsaSha2_256f, - DsaType::SlhDsaShake128s, - DsaType::SlhDsaShake128f, - DsaType::SlhDsaShake192s, - DsaType::SlhDsaShake192f, - DsaType::SlhDsaShake256s, - DsaType::SlhDsaShake256f, +const SLH_DSA_TYPES: [PrehashDsaType; 12] = [ + PrehashDsaType::SlhDsaSha2_128s, + PrehashDsaType::SlhDsaSha2_128f, + PrehashDsaType::SlhDsaSha2_192s, + PrehashDsaType::SlhDsaSha2_192f, + PrehashDsaType::SlhDsaSha2_256s, + PrehashDsaType::SlhDsaSha2_256f, + PrehashDsaType::SlhDsaShake128s, + PrehashDsaType::SlhDsaShake128f, + PrehashDsaType::SlhDsaShake192s, + PrehashDsaType::SlhDsaShake192f, + PrehashDsaType::SlhDsaShake256s, + PrehashDsaType::SlhDsaShake256f, ]; // Implement clone @@ -95,14 +95,14 @@ pub enum DsaManager { Rsa(RsaDsaManager), /// EC DSA manager Ec(EcDsaManager), - /// SLH DSA manager - Slh(SlhDsaManager), } // Implement clone #[derive(Clone)] /// Enum to represent the different types of pre-hash DSA managers pub enum PrehashDsaManager { + /// SLH DSA manager + Slh(SlhDsaManager), /// ML DSA manager Ml(MlDsaManager), /// Composite DSA manager @@ -118,9 +118,6 @@ impl Dsa for DsaManager { _ if RSA_DSA_TYPES.contains(&dsa_type) => { DsaManager::Rsa(RsaDsaManager::new(dsa_type)?) } - _ if SLH_DSA_TYPES.contains(&dsa_type) => { - DsaManager::Slh(SlhDsaManager::new(dsa_type)?) - } _ if EC_DSA_TYPES.contains(&dsa_type) => DsaManager::Ec(EcDsaManager::new(dsa_type)?), _ => { panic!("Not implemented"); @@ -133,7 +130,6 @@ impl Dsa for DsaManager { match self { DsaManager::Rsa(rsa) => rsa.key_gen(), DsaManager::Ec(ec) => ec.key_gen(), - DsaManager::Slh(slh) => slh.key_gen(), } } @@ -141,7 +137,6 @@ impl Dsa for DsaManager { match self { DsaManager::Rsa(rsa) => rsa.key_gen_with_rng(rng), DsaManager::Ec(ec) => ec.key_gen_with_rng(rng), - DsaManager::Slh(slh) => slh.key_gen_with_rng(rng), } } @@ -149,7 +144,6 @@ impl Dsa for DsaManager { match self { DsaManager::Rsa(rsa) => rsa.sign(sk, msg), DsaManager::Ec(ec) => ec.sign(sk, msg), - DsaManager::Slh(slh) => slh.sign(sk, msg), } } @@ -157,7 +151,6 @@ impl Dsa for DsaManager { match self { DsaManager::Rsa(rsa) => rsa.verify(pk, msg, sig), DsaManager::Ec(ec) => ec.verify(pk, msg, sig), - DsaManager::Slh(slh) => slh.verify(pk, msg, sig), } } @@ -165,7 +158,6 @@ impl Dsa for DsaManager { match self { DsaManager::Rsa(rsa) => rsa.get_dsa_info(), DsaManager::Ec(ec) => ec.get_dsa_info(), - DsaManager::Slh(slh) => slh.get_dsa_info(), } } @@ -173,7 +165,6 @@ impl Dsa for DsaManager { match self { DsaManager::Rsa(rsa) => rsa.get_public_key(sk), DsaManager::Ec(ec) => ec.get_public_key(sk), - DsaManager::Slh(slh) => slh.get_public_key(sk), } } } @@ -190,6 +181,9 @@ impl PrehashDsa for PrehashDsaManager { _ if COMPOSITE_DSA_TYPES.contains(&dsa_type) => { PrehashDsaManager::Composite(CompositeDsaManager::new(dsa_type)?) } + _ if SLH_DSA_TYPES.contains(&dsa_type) => { + PrehashDsaManager::Slh(SlhDsaManager::new(dsa_type)?) + } _ => { panic!("Not implemented"); } @@ -201,6 +195,7 @@ impl PrehashDsa for PrehashDsaManager { match self { PrehashDsaManager::Ml(ml) => ml.key_gen(), PrehashDsaManager::Composite(composite) => composite.key_gen(), + PrehashDsaManager::Slh(slh) => slh.key_gen(), } } @@ -208,6 +203,7 @@ impl PrehashDsa for PrehashDsaManager { match self { PrehashDsaManager::Ml(ml) => ml.key_gen_with_rng(rng), PrehashDsaManager::Composite(composite) => composite.key_gen_with_rng(rng), + PrehashDsaManager::Slh(slh) => slh.key_gen_with_rng(rng), } } @@ -215,6 +211,7 @@ impl PrehashDsa for PrehashDsaManager { match self { PrehashDsaManager::Ml(ml) => ml.sign_with_ctx(sk, msg, ctx), PrehashDsaManager::Composite(composite) => composite.sign_with_ctx(sk, msg, ctx), + PrehashDsaManager::Slh(slh) => slh.sign_with_ctx(sk, msg, ctx), } } @@ -228,6 +225,7 @@ impl PrehashDsa for PrehashDsaManager { match self { PrehashDsaManager::Ml(ml) => ml.verify_with_ctx(pk, msg, sig, ctx), PrehashDsaManager::Composite(composite) => composite.verify_with_ctx(pk, msg, sig, ctx), + PrehashDsaManager::Slh(slh) => slh.verify_with_ctx(pk, msg, sig, ctx), } } @@ -235,6 +233,7 @@ impl PrehashDsa for PrehashDsaManager { match self { PrehashDsaManager::Ml(ml) => ml.get_dsa_info(), PrehashDsaManager::Composite(composite) => composite.get_dsa_info(), + PrehashDsaManager::Slh(slh) => slh.get_dsa_info(), } } @@ -242,6 +241,7 @@ impl PrehashDsa for PrehashDsaManager { match self { PrehashDsaManager::Ml(ml) => ml.get_public_key(sk), PrehashDsaManager::Composite(composite) => composite.get_public_key(sk), + PrehashDsaManager::Slh(slh) => slh.get_public_key(sk), } } } @@ -256,7 +256,6 @@ mod tests { let mut all_dsas: Vec = Vec::new(); all_dsas.extend_from_slice(&RSA_DSA_TYPES); all_dsas.extend_from_slice(&EC_DSA_TYPES); - all_dsas.extend_from_slice(&SLH_DSA_TYPES); // This is just to test that the manager can create all DSA types for dsa_type in all_dsas { @@ -270,6 +269,7 @@ mod tests { let mut all_dsas: Vec = Vec::new(); all_dsas.extend_from_slice(&ML_DSA_TYPES); all_dsas.extend_from_slice(&COMPOSITE_DSA_TYPES); + all_dsas.extend_from_slice(&SLH_DSA_TYPES); // This is just to test that the manager can create all DSA types for dsa_type in all_dsas { diff --git a/src/dsa/slh_dsa.rs b/src/dsa/slh_dsa.rs index 5f9c8a9..88717c1 100644 --- a/src/dsa/slh_dsa.rs +++ b/src/dsa/slh_dsa.rs @@ -1,6 +1,3 @@ -use crate::dsa::common::dsa_info::DsaInfo; -use crate::dsa::common::dsa_trait::Dsa; -use crate::dsa::common::dsa_type::DsaType; use crate::QuantCryptError; use rand_core::SeedableRng; @@ -20,10 +17,14 @@ use fips205::slh_dsa_shake_256f; use fips205::slh_dsa_shake_256s; use fips205::traits::{SerDes, Signer, Verifier}; +use super::common::prehash_dsa_info::PrehashDsaInfo; +use super::common::prehash_dsa_trait::PrehashDsa; +use super::common::prehash_dsa_type::PrehashDsaType; + type Result = std::result::Result; macro_rules! sign_slh { - ($sig_type:ident, $sk:expr, $msg:expr) => {{ + ($sig_type:ident, $sk:expr, $msg:expr, $ctx:expr) => {{ if $sk.len() != $sig_type::SK_LEN { return Err(QuantCryptError::InvalidPrivateKey); } @@ -38,7 +39,7 @@ macro_rules! sign_slh { // Try signing the message let sig = sk - .try_sign($msg, &[], true) // Empty context + .try_sign($msg, $ctx, true) .map_err(|_| QuantCryptError::SignatureFailed)?; // Convert the signature to a Vec and return it @@ -48,7 +49,7 @@ macro_rules! sign_slh { } macro_rules! verify_slh { - ($sig_type:ident, $pk: expr, $msg: expr, $signature: expr) => {{ + ($sig_type:ident, $pk: expr, $msg: expr, $signature: expr, $ctx: expr) => {{ if $pk.len() != $sig_type::PK_LEN { return Err(QuantCryptError::InvalidPublicKey); } @@ -67,7 +68,7 @@ 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, &[])); + let result = Ok(pk.verify($msg, &sig_buf, $ctx)); result }}; @@ -99,17 +100,17 @@ macro_rules! get_public_key { #[derive(Clone)] pub struct SlhDsaManager { - pub dsa_info: DsaInfo, + pub dsa_info: PrehashDsaInfo, } -impl Dsa for SlhDsaManager { +impl PrehashDsa for SlhDsaManager { /// Create a new DSA instance /// /// # Arguments /// /// * `dsa_type` - The type of DSA to create - fn new(dsa_type: DsaType) -> Result { - let dsa_info = DsaInfo::new(dsa_type); + fn new(dsa_type: PrehashDsaType) -> Result { + let dsa_info = PrehashDsaInfo::new(dsa_type); Ok(Self { dsa_info }) } @@ -127,18 +128,18 @@ impl Dsa for SlhDsaManager { rng: &mut impl rand_core::CryptoRngCore, ) -> Result<(Vec, Vec)> { match self.dsa_info.dsa_type { - DsaType::SlhDsaSha2_128s => keygen_slh!(slh_dsa_sha2_128s, rng), - DsaType::SlhDsaSha2_128f => keygen_slh!(slh_dsa_sha2_128f, rng), - DsaType::SlhDsaSha2_192s => keygen_slh!(slh_dsa_sha2_192s, rng), - DsaType::SlhDsaSha2_192f => keygen_slh!(slh_dsa_sha2_192f, rng), - DsaType::SlhDsaSha2_256s => keygen_slh!(slh_dsa_sha2_256s, rng), - DsaType::SlhDsaSha2_256f => keygen_slh!(slh_dsa_sha2_256f, rng), - DsaType::SlhDsaShake128s => keygen_slh!(slh_dsa_shake_128s, rng), - DsaType::SlhDsaShake128f => keygen_slh!(slh_dsa_shake_128f, rng), - DsaType::SlhDsaShake192s => keygen_slh!(slh_dsa_shake_192s, rng), - DsaType::SlhDsaShake192f => keygen_slh!(slh_dsa_shake_192f, rng), - DsaType::SlhDsaShake256s => keygen_slh!(slh_dsa_shake_256s, rng), - DsaType::SlhDsaShake256f => keygen_slh!(slh_dsa_shake_256f, rng), + PrehashDsaType::SlhDsaSha2_128s => keygen_slh!(slh_dsa_sha2_128s, rng), + PrehashDsaType::SlhDsaSha2_128f => keygen_slh!(slh_dsa_sha2_128f, rng), + PrehashDsaType::SlhDsaSha2_192s => keygen_slh!(slh_dsa_sha2_192s, rng), + PrehashDsaType::SlhDsaSha2_192f => keygen_slh!(slh_dsa_sha2_192f, rng), + PrehashDsaType::SlhDsaSha2_256s => keygen_slh!(slh_dsa_sha2_256s, rng), + PrehashDsaType::SlhDsaSha2_256f => keygen_slh!(slh_dsa_sha2_256f, rng), + PrehashDsaType::SlhDsaShake128s => keygen_slh!(slh_dsa_shake_128s, rng), + PrehashDsaType::SlhDsaShake128f => keygen_slh!(slh_dsa_shake_128f, rng), + PrehashDsaType::SlhDsaShake192s => keygen_slh!(slh_dsa_shake_192s, rng), + 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), _ => Err(QuantCryptError::NotImplemented), } } @@ -153,30 +154,56 @@ impl Dsa for SlhDsaManager { self.key_gen_with_rng(&mut rng) } + /// Get DSA metadata information such as the key lengths, + /// size of signature, etc. + fn get_dsa_info(&self) -> PrehashDsaInfo { + self.dsa_info.clone() + } + + fn get_public_key(&self, sk: &[u8]) -> Result> { + match self.dsa_info.dsa_type { + PrehashDsaType::SlhDsaSha2_128f => get_public_key!(slh_dsa_sha2_128f, sk), + PrehashDsaType::SlhDsaSha2_128s => get_public_key!(slh_dsa_sha2_128s, sk), + PrehashDsaType::SlhDsaSha2_192f => get_public_key!(slh_dsa_sha2_192f, sk), + PrehashDsaType::SlhDsaSha2_192s => get_public_key!(slh_dsa_sha2_192s, sk), + PrehashDsaType::SlhDsaSha2_256f => get_public_key!(slh_dsa_sha2_256f, sk), + PrehashDsaType::SlhDsaSha2_256s => get_public_key!(slh_dsa_sha2_256s, sk), + PrehashDsaType::SlhDsaShake128f => get_public_key!(slh_dsa_shake_128f, sk), + PrehashDsaType::SlhDsaShake128s => get_public_key!(slh_dsa_shake_128s, sk), + PrehashDsaType::SlhDsaShake192f => get_public_key!(slh_dsa_shake_192f, sk), + 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), + _ => Err(QuantCryptError::NotImplemented), + } + } + /// Sign a message /// /// # Arguments /// /// * `msg` - The message to sign /// * `sk` - The secret key + /// * `ctx` - The context /// /// # Returns /// /// The signature - fn sign(&self, sk: &[u8], msg: &[u8]) -> Result> { + fn sign_with_ctx(&self, sk: &[u8], msg: &[u8], ctx: Option<&[u8]>) -> Result> { + let ctx = ctx.unwrap_or(&[]); match self.dsa_info.dsa_type { - DsaType::SlhDsaSha2_128s => sign_slh!(slh_dsa_sha2_128s, &sk, msg), - DsaType::SlhDsaSha2_128f => sign_slh!(slh_dsa_sha2_128f, &sk, msg), - DsaType::SlhDsaSha2_192s => sign_slh!(slh_dsa_sha2_192s, &sk, msg), - DsaType::SlhDsaSha2_192f => sign_slh!(slh_dsa_sha2_192f, &sk, msg), - DsaType::SlhDsaSha2_256s => sign_slh!(slh_dsa_sha2_256s, &sk, msg), - DsaType::SlhDsaSha2_256f => sign_slh!(slh_dsa_sha2_256f, &sk, msg), - DsaType::SlhDsaShake128s => sign_slh!(slh_dsa_shake_128s, &sk, msg), - DsaType::SlhDsaShake128f => sign_slh!(slh_dsa_shake_128f, &sk, msg), - DsaType::SlhDsaShake192s => sign_slh!(slh_dsa_shake_192s, &sk, msg), - DsaType::SlhDsaShake192f => sign_slh!(slh_dsa_shake_192f, &sk, msg), - DsaType::SlhDsaShake256s => sign_slh!(slh_dsa_shake_256s, &sk, msg), - DsaType::SlhDsaShake256f => sign_slh!(slh_dsa_shake_256f, &sk, msg), + 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), _ => Err(QuantCryptError::NotImplemented), } } @@ -188,48 +215,56 @@ impl Dsa for SlhDsaManager { /// * `msg` - The message to verify /// * `pk` - The public key /// * `sig` - The signature + /// * `ctx` - The context /// /// # Returns /// /// A boolean indicating if the signature is valid - fn verify(&self, pk: &[u8], msg: &[u8], signature: &[u8]) -> Result { - match self.dsa_info.dsa_type { - DsaType::SlhDsaSha2_128f => verify_slh!(slh_dsa_sha2_128f, pk, msg, signature), - DsaType::SlhDsaSha2_128s => verify_slh!(slh_dsa_sha2_128s, pk, msg, signature), - DsaType::SlhDsaSha2_192f => verify_slh!(slh_dsa_sha2_192f, pk, msg, signature), - DsaType::SlhDsaSha2_192s => verify_slh!(slh_dsa_sha2_192s, pk, msg, signature), - DsaType::SlhDsaSha2_256f => verify_slh!(slh_dsa_sha2_256f, pk, msg, signature), - DsaType::SlhDsaSha2_256s => verify_slh!(slh_dsa_sha2_256s, pk, msg, signature), - DsaType::SlhDsaShake128f => verify_slh!(slh_dsa_shake_128f, pk, msg, signature), - DsaType::SlhDsaShake128s => verify_slh!(slh_dsa_shake_128s, pk, msg, signature), - DsaType::SlhDsaShake192f => verify_slh!(slh_dsa_shake_192f, pk, msg, signature), - DsaType::SlhDsaShake192s => verify_slh!(slh_dsa_shake_192s, pk, msg, signature), - DsaType::SlhDsaShake256f => verify_slh!(slh_dsa_shake_256f, pk, msg, signature), - DsaType::SlhDsaShake256s => verify_slh!(slh_dsa_shake_256s, pk, msg, signature), - _ => Err(QuantCryptError::NotImplemented), - } - } - - /// Get DSA metadata information such as the key lengths, - /// size of signature, etc. - fn get_dsa_info(&self) -> DsaInfo { - self.dsa_info.clone() - } - - fn get_public_key(&self, sk: &[u8]) -> Result> { + fn verify_with_ctx( + &self, + pk: &[u8], + msg: &[u8], + signature: &[u8], + ctx: Option<&[u8]>, + ) -> Result { + let ctx = ctx.unwrap_or(&[]); match self.dsa_info.dsa_type { - DsaType::SlhDsaSha2_128f => get_public_key!(slh_dsa_sha2_128f, sk), - DsaType::SlhDsaSha2_128s => get_public_key!(slh_dsa_sha2_128s, sk), - DsaType::SlhDsaSha2_192f => get_public_key!(slh_dsa_sha2_192f, sk), - DsaType::SlhDsaSha2_192s => get_public_key!(slh_dsa_sha2_192s, sk), - DsaType::SlhDsaSha2_256f => get_public_key!(slh_dsa_sha2_256f, sk), - DsaType::SlhDsaSha2_256s => get_public_key!(slh_dsa_sha2_256s, sk), - DsaType::SlhDsaShake128f => get_public_key!(slh_dsa_shake_128f, sk), - DsaType::SlhDsaShake128s => get_public_key!(slh_dsa_shake_128s, sk), - DsaType::SlhDsaShake192f => get_public_key!(slh_dsa_shake_192f, sk), - DsaType::SlhDsaShake192s => get_public_key!(slh_dsa_shake_192s, sk), - DsaType::SlhDsaShake256f => get_public_key!(slh_dsa_shake_256f, sk), - DsaType::SlhDsaShake256s => get_public_key!(slh_dsa_shake_256s, sk), + PrehashDsaType::SlhDsaSha2_128f => { + verify_slh!(slh_dsa_sha2_128f, pk, msg, signature, ctx) + } + PrehashDsaType::SlhDsaSha2_128s => { + verify_slh!(slh_dsa_sha2_128s, pk, msg, signature, ctx) + } + PrehashDsaType::SlhDsaSha2_192f => { + verify_slh!(slh_dsa_sha2_192f, pk, msg, signature, ctx) + } + PrehashDsaType::SlhDsaSha2_192s => { + verify_slh!(slh_dsa_sha2_192s, pk, msg, signature, ctx) + } + PrehashDsaType::SlhDsaSha2_256f => { + verify_slh!(slh_dsa_sha2_256f, pk, msg, signature, ctx) + } + PrehashDsaType::SlhDsaSha2_256s => { + verify_slh!(slh_dsa_sha2_256s, pk, msg, signature, ctx) + } + PrehashDsaType::SlhDsaShake128f => { + verify_slh!(slh_dsa_shake_128f, pk, msg, signature, ctx) + } + PrehashDsaType::SlhDsaShake128s => { + verify_slh!(slh_dsa_shake_128s, pk, msg, signature, ctx) + } + PrehashDsaType::SlhDsaShake192f => { + verify_slh!(slh_dsa_shake_192f, pk, msg, signature, ctx) + } + PrehashDsaType::SlhDsaShake192s => { + verify_slh!(slh_dsa_shake_192s, pk, msg, signature, ctx) + } + PrehashDsaType::SlhDsaShake256f => { + verify_slh!(slh_dsa_shake_256f, pk, msg, signature, ctx) + } + PrehashDsaType::SlhDsaShake256s => { + verify_slh!(slh_dsa_shake_256s, pk, msg, signature, ctx) + } _ => Err(QuantCryptError::NotImplemented), } } @@ -239,12 +274,11 @@ impl Dsa for SlhDsaManager { mod tests { use super::*; use crate::certificates::Certificate; - use crate::dsa::common::dsa_type::DsaType; use crate::dsa::common::macros::test_dsa; #[test] fn test_slh_dsa_sha2_128s() { - let dsa = SlhDsaManager::new(DsaType::SlhDsaSha2_128s); + let dsa = SlhDsaManager::new(PrehashDsaType::SlhDsaSha2_128s); test_dsa!(dsa); let cert_bytes = @@ -255,7 +289,7 @@ mod tests { #[test] fn test_slh_dsa_sha2_128f() { - let dsa = SlhDsaManager::new(DsaType::SlhDsaSha2_128f); + let dsa = SlhDsaManager::new(PrehashDsaType::SlhDsaSha2_128f); test_dsa!(dsa); let cert_bytes = @@ -266,7 +300,7 @@ mod tests { #[test] fn test_slh_dsa_sha2_192s() { - let dsa = SlhDsaManager::new(DsaType::SlhDsaSha2_192s); + let dsa = SlhDsaManager::new(PrehashDsaType::SlhDsaSha2_192s); test_dsa!(dsa); let cert_bytes = @@ -277,7 +311,7 @@ mod tests { #[test] fn test_slh_dsa_sha2_192f() { - let dsa = SlhDsaManager::new(DsaType::SlhDsaSha2_192f); + let dsa = SlhDsaManager::new(PrehashDsaType::SlhDsaSha2_192f); test_dsa!(dsa); let cert_bytes = @@ -288,7 +322,7 @@ mod tests { #[test] fn test_slh_dsa_sha2_256s() { - let dsa = SlhDsaManager::new(DsaType::SlhDsaSha2_256s); + let dsa = SlhDsaManager::new(PrehashDsaType::SlhDsaSha2_256s); test_dsa!(dsa); let cert_bytes = @@ -299,7 +333,7 @@ mod tests { #[test] fn test_slh_dsa_sha2_256f() { - let dsa = SlhDsaManager::new(DsaType::SlhDsaSha2_256f); + let dsa = SlhDsaManager::new(PrehashDsaType::SlhDsaSha2_256f); test_dsa!(dsa); let cert_bytes = @@ -310,7 +344,7 @@ mod tests { #[test] fn test_slh_dsa_shake_128s() { - let dsa = SlhDsaManager::new(DsaType::SlhDsaShake128s); + let dsa = SlhDsaManager::new(PrehashDsaType::SlhDsaShake128s); test_dsa!(dsa); let cert_bytes = @@ -321,7 +355,7 @@ mod tests { #[test] fn test_slh_dsa_shake_128f() { - let dsa = SlhDsaManager::new(DsaType::SlhDsaShake128f); + let dsa = SlhDsaManager::new(PrehashDsaType::SlhDsaShake128f); test_dsa!(dsa); let cert_bytes = @@ -332,7 +366,7 @@ mod tests { #[test] fn test_slh_dsa_shake_192s() { - let dsa = SlhDsaManager::new(DsaType::SlhDsaShake192s); + let dsa = SlhDsaManager::new(PrehashDsaType::SlhDsaShake192s); test_dsa!(dsa); let cert_bytes = @@ -343,7 +377,7 @@ mod tests { #[test] fn test_slh_dsa_shake_192f() { - let dsa = SlhDsaManager::new(DsaType::SlhDsaShake192f); + let dsa = SlhDsaManager::new(PrehashDsaType::SlhDsaShake192f); test_dsa!(dsa); let cert_bytes = @@ -354,7 +388,7 @@ mod tests { #[test] fn test_slh_dsa_shake_256s() { - let dsa = SlhDsaManager::new(DsaType::SlhDsaShake256s); + let dsa = SlhDsaManager::new(PrehashDsaType::SlhDsaShake256s); test_dsa!(dsa); let cert_bytes = @@ -365,7 +399,7 @@ mod tests { #[test] fn test_slh_dsa_shake_256f() { - let dsa = SlhDsaManager::new(DsaType::SlhDsaShake256f); + let dsa = SlhDsaManager::new(PrehashDsaType::SlhDsaShake256f); test_dsa!(dsa); let cert_bytes =