#![no_std]
#![deny(clippy::pedantic, warnings, missing_docs, unsafe_code)]
#![deny(absolute_paths_not_starting_with_crate, dead_code)]
#![deny(elided_lifetimes_in_paths, explicit_outlives_requirements, keyword_idents)]
#![deny(let_underscore_drop, macro_use_extern_crate, meta_variable_misuse, missing_abi)]
#![deny(non_ascii_idents, rust_2021_incompatible_closure_captures)]
#![deny(rust_2021_incompatible_or_patterns, rust_2021_prefixes_incompatible_syntax)]
#![deny(rust_2021_prelude_collisions, single_use_lifetimes, trivial_casts)]
#![deny(trivial_numeric_casts, unreachable_pub, unsafe_op_in_unsafe_fn, unstable_features)]
#![deny(unused_extern_crates, unused_import_braces, unused_lifetimes, unused_macro_rules)]
#![deny(unused_qualifications, unused_results, variant_size_differences)]
#![doc = include_str!("../README.md")]
pub mod traits;
pub use types::Ph;
mod fors;
mod hashers;
mod helpers;
mod hypertree;
mod slh;
mod types;
mod wots;
mod xmss;
const LGW: u32 = 4;
const W: u32 = 16;
const LEN2: u32 = 3;
macro_rules! functionality {
() => {
use crate::hashers::hash_message;
use crate::traits::{KeyGen, SerDes, Signer, Verifier};
use crate::types::{Ph, SlhDsaSig, SlhPrivateKey, SlhPublicKey};
use rand_core::CryptoRngCore;
use zeroize::{Zeroize, ZeroizeOnDrop};
#[derive(Clone, Zeroize, ZeroizeOnDrop)]
pub struct PrivateKey(SlhPrivateKey<N>);
#[derive(Clone, Zeroize, ZeroizeOnDrop)]
pub struct PublicKey(SlhPublicKey<N>);
#[derive(Zeroize, ZeroizeOnDrop)]
pub struct KG();
#[cfg(feature = "default-rng")]
pub fn try_keygen() -> Result<(PublicKey, PrivateKey), &'static str> { KG::try_keygen() }
pub fn try_keygen_with_rng(
rng: &mut impl CryptoRngCore,
) -> Result<(PublicKey, PrivateKey), &'static str> {
KG::try_keygen_with_rng(rng)
}
impl KeyGen for KG {
type PrivateKey = PrivateKey;
type PublicKey = PublicKey;
fn try_keygen_with_rng(
rng: &mut impl CryptoRngCore,
) -> Result<(PublicKey, PrivateKey), &'static str> {
let res = crate::slh::slh_keygen_with_rng::<D, H, HP, K, LEN, M, N>(rng, &HASHERS);
res.map(|(sk, pk)| (PublicKey(pk), PrivateKey(sk)))
}
}
impl Signer for PrivateKey {
type Signature = [u8; SIG_LEN];
type PublicKey = PublicKey;
fn try_sign_with_rng(
&self, rng: &mut impl CryptoRngCore, m: &[u8], ctx: &[u8], hedged: bool,
) -> Result<[u8; SIG_LEN], &'static str> {
if ctx.len() > 255 {
return Err("ctx must be less than 256 bytes");
};
let mp: &[&[u8]] = &[&[0u8], &[ctx.len().to_le_bytes()[0]], ctx, m];
let sig = crate::slh::slh_sign_with_rng::<A, D, H, HP, K, LEN, M, N>(
rng, &HASHERS, &mp, &self.0, hedged,
);
sig.map(|s| s.serialize())
}
fn try_hash_sign_with_rng(
&self, rng: &mut impl CryptoRngCore, message: &[u8], ctx: &[u8], ph: &Ph,
hedged: bool,
) -> Result<Self::Signature, &'static str> {
if ctx.len() > 255 {
return Err("ctx must be less than 256 bytes");
};
let mut phm = [0u8; 64]; let (oid, phm_len) = hash_message(message, ph, &mut phm);
let mp: &[&[u8]] = &[
&[1u8],
&[ctx.len().to_le_bytes()[0]],
ctx,
&oid,
&phm[0..phm_len],
];
let sig = crate::slh::slh_sign_with_rng::<A, D, H, HP, K, LEN, M, N>(
rng, &HASHERS, &mp, &self.0, hedged, );
sig.map(|s| s.serialize())
}
fn get_public_key(&self) -> Self::PublicKey {
PublicKey(SlhPublicKey{pk_seed: self.0.pk_seed, pk_root: self.0.pk_root})
}
fn _test_only_raw_sign(
&self, rng: &mut impl CryptoRngCore, m: &[u8], hedged: bool,
) -> Result<[u8; SIG_LEN], &'static str> {
let mut opt_rand = (self.0).pk_seed;
if hedged {
rng.try_fill_bytes(&mut opt_rand).map_err(|_| "Alg17: rng failed")?;
}
let sig = crate::slh::slh_sign_internal::<A, D, H, HP, K, LEN, M, N>(
&HASHERS,
&[m],
&self.0,
opt_rand,
);
sig.map(|s| s.serialize())
}
}
impl Verifier for PublicKey {
type Signature = [u8; SIG_LEN];
fn verify(&self, m: &[u8], sig_bytes: &[u8; SIG_LEN], ctx: &[u8]) -> bool {
if ctx.len() > 255 {
return false;
};
let sig = SlhDsaSig::<A, D, HP, K, LEN, N>::deserialize(sig_bytes);
let mp: &[&[u8]] = &[&[0u8], &[ctx.len().to_le_bytes()[0]], ctx, m];
let res = crate::slh::slh_verify::<A, D, H, HP, K, LEN, M, N>(
&HASHERS, &mp, &sig, &self.0,
);
res
}
fn hash_verify(
&self, m: &[u8], sig_bytes: &[u8; SIG_LEN], ctx: &[u8], ph: &Ph,
) -> bool {
if ctx.len() > 255 {
return false;
};
let sig = SlhDsaSig::<A, D, HP, K, LEN, N>::deserialize(sig_bytes);
let mut phm = [0u8; 64]; let (oid, phm_len) = hash_message(m, ph, &mut phm);
let mp: &[&[u8]] = &[
&[1u8],
&[ctx.len().to_le_bytes()[0]],
ctx,
&oid,
&phm[0..phm_len],
];
let res = crate::slh::slh_verify::<A, D, H, HP, K, LEN, M, N>(
&HASHERS, &mp, &sig, &self.0,
);
res
}
fn _test_only_raw_verify(
&self, m: &[u8], sig_bytes: &[u8; SIG_LEN],
) -> Result<bool, &'static str> {
let sig = SlhDsaSig::<A, D, HP, K, LEN, N>::deserialize(sig_bytes);
let res = crate::slh::slh_verify_internal::<A, D, H, HP, K, LEN, M, N>(
&HASHERS,
&[m],
&sig,
&self.0,
);
Ok(res)
}
}
impl SerDes for PublicKey {
type ByteArray = [u8; PK_LEN];
fn into_bytes(self) -> Self::ByteArray {
let mut out = [0u8; PK_LEN];
out[0..(PK_LEN / 2)].copy_from_slice(&self.0.pk_seed);
out[(PK_LEN / 2)..].copy_from_slice(&self.0.pk_root);
out
}
fn try_from_bytes(bytes: &Self::ByteArray) -> Result<Self, &'static str> {
let mut pk = SlhPublicKey { pk_seed: [0u8; N], pk_root: [0u8; N] };
pk.pk_seed.copy_from_slice(&bytes[..(PK_LEN / 2)]);
pk.pk_root.copy_from_slice(&bytes[(PK_LEN / 2)..]);
Ok(PublicKey(pk))
}
}
impl SerDes for PrivateKey {
type ByteArray = [u8; SK_LEN];
fn into_bytes(self) -> Self::ByteArray {
let mut bytes = [0u8; SK_LEN];
bytes[0..(SK_LEN / 4)].copy_from_slice(&self.0.sk_seed);
bytes[(SK_LEN / 4)..(SK_LEN / 2)].copy_from_slice(&self.0.sk_prf);
bytes[(SK_LEN / 2)..(3 * SK_LEN / 4)].copy_from_slice(&self.0.pk_seed);
bytes[(3 * SK_LEN / 4)..].copy_from_slice(&self.0.pk_root);
bytes
}
fn try_from_bytes(bytes: &Self::ByteArray) -> Result<Self, &'static str> {
let mut sk = SlhPrivateKey {
sk_seed: [0u8; N],
sk_prf: [0u8; N],
pk_seed: [0u8; N],
pk_root: [0u8; N],
};
sk.sk_seed.copy_from_slice(&bytes[0..(SK_LEN / 4)]);
sk.sk_prf.copy_from_slice(&bytes[(SK_LEN / 4)..(SK_LEN / 2)]);
sk.pk_seed.copy_from_slice(&bytes[(SK_LEN / 2)..(3 * SK_LEN / 4)]);
sk.pk_root.copy_from_slice(&bytes[(3 * SK_LEN / 4)..]);
let (sk_test, _) = crate::slh::slh_keygen_internal::<D, H, HP, K, LEN, M, N>(&HASHERS, sk.sk_seed, sk.sk_prf, sk.pk_seed);
if sk_test.pk_root != sk.pk_root { return Err("Corrupted key")}
Ok(PrivateKey(sk))
}
}
#[cfg(test)]
mod tests {
use super::*;
use rand_chacha::rand_core::SeedableRng;
#[test]
fn simple_round_trips() {
let message = [0u8, 1, 2, 3];
let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(123);
let (pk1, sk1) = KG::try_keygen_with_rng(&mut rng).unwrap();
let pk1_bytes = pk1.into_bytes();
let sk1_bytes = sk1.into_bytes();
let pk2 = PublicKey::try_from_bytes(&pk1_bytes).unwrap();
let sk2 = PrivateKey::try_from_bytes(&sk1_bytes).unwrap();
let sig = sk2.try_sign_with_rng(&mut rng, &message, b"context", true).unwrap();
let result = pk2.verify(&message, &sig, b"context");
assert!(result, "Signature failed to verify");
let (pk3, sk3) = KG::keygen_with_seeds(&[0u8; N], &[1u8; N], &[2u8; N]);
let sig = sk3.try_sign_with_rng(&mut rng, &message, b"context", true).unwrap();
let result = pk3.verify(&message, &sig, b"context");
assert!(result, "Signature failed to verify");
let result = pk2.verify(&message, &sig, b"some other context");
assert!(!result, "Signature should not have verified");
for ph in [Ph::SHA256, Ph::SHA512, Ph::SHAKE128, Ph::SHAKE256] {
let sig = sk2
.try_hash_sign_with_rng(&mut rng, &message, b"context", &ph, true)
.unwrap();
let result = pk2.hash_verify(&message, &sig, b"context", &ph);
assert!(result, "Signature failed to verify");
let result = pk2.hash_verify(&message, &sig, b"some other context", &ph);
assert!(!result, "Signature should not have verified");
}
}
}
};
}
#[cfg(feature = "slh_dsa_sha2_128s")]
pub mod slh_dsa_sha2_128s {
use crate::hashers::sha2_cat_1::{f, h, h_msg, prf, prf_msg, t_l};
use crate::hashers::Hashers;
pub const N: usize = 16;
const H: usize = 63;
const D: usize = 7;
const HP: usize = 9;
const A: usize = 12;
const K: usize = 14;
const M: usize = 30;
const LEN: usize = 2 * N + 3;
pub const PK_LEN: usize = 32;
pub const SIG_LEN: usize = 7856;
pub const SK_LEN: usize = PK_LEN * 2;
static HASHERS: Hashers<K, LEN, M, N> =
Hashers::<K, LEN, M, N> { h_msg, prf, prf_msg, f, h, t_l, t_len: t_l };
functionality!();
}
#[cfg(feature = "slh_dsa_shake_128s")]
pub mod slh_dsa_shake_128s {
use crate::hashers::shake::{f, h, h_msg, prf, prf_msg, t_l};
use crate::hashers::Hashers;
pub const N: usize = 16;
const H: usize = 63;
const D: usize = 7;
const HP: usize = 9;
const A: usize = 12;
const K: usize = 14;
const M: usize = 30;
const LEN: usize = 2 * N + 3;
pub const PK_LEN: usize = 32;
pub const SIG_LEN: usize = 7856;
pub const SK_LEN: usize = PK_LEN * 2;
static HASHERS: Hashers<K, LEN, M, N> =
Hashers::<K, LEN, M, N> { h_msg, prf, prf_msg, f, h, t_l, t_len: t_l };
functionality!();
}
#[cfg(feature = "slh_dsa_sha2_128f")]
pub mod slh_dsa_sha2_128f {
use crate::hashers::sha2_cat_1::{f, h, h_msg, prf, prf_msg, t_l};
use crate::hashers::Hashers;
pub const N: usize = 16;
const H: usize = 66;
const D: usize = 22;
const HP: usize = 3;
const A: usize = 6;
const K: usize = 33;
const M: usize = 34;
const LEN: usize = 2 * N + 3;
pub const PK_LEN: usize = 32;
pub const SIG_LEN: usize = 17088;
pub const SK_LEN: usize = PK_LEN * 2;
static HASHERS: Hashers<K, LEN, M, N> =
Hashers::<K, LEN, M, N> { h_msg, prf, prf_msg, f, h, t_l, t_len: t_l };
functionality!();
}
#[cfg(feature = "slh_dsa_shake_128f")]
pub mod slh_dsa_shake_128f {
use crate::hashers::shake::{f, h, h_msg, prf, prf_msg, t_l};
use crate::hashers::Hashers;
pub const N: usize = 16;
const H: usize = 66;
const D: usize = 22;
const HP: usize = 3;
const A: usize = 6;
const K: usize = 33;
const M: usize = 34;
const LEN: usize = 2 * N + 3;
pub const PK_LEN: usize = 32;
pub const SIG_LEN: usize = 17088;
pub const SK_LEN: usize = PK_LEN * 2;
static HASHERS: Hashers<K, LEN, M, N> =
Hashers::<K, LEN, M, N> { h_msg, prf, prf_msg, f, h, t_l, t_len: t_l };
functionality!();
}
#[cfg(feature = "slh_dsa_sha2_192s")]
pub mod slh_dsa_sha2_192s {
use crate::hashers::sha2_cat_3_5::{f, h, h_msg, prf, prf_msg, t_l};
use crate::hashers::Hashers;
pub const N: usize = 24;
const H: usize = 63;
const D: usize = 7;
const HP: usize = 9;
const A: usize = 14;
const K: usize = 17;
const M: usize = 39;
const LEN: usize = 2 * N + 3;
pub const PK_LEN: usize = 48;
pub const SIG_LEN: usize = 16224;
pub const SK_LEN: usize = PK_LEN * 2;
static HASHERS: Hashers<K, LEN, M, N> =
Hashers::<K, LEN, M, N> { h_msg, prf, prf_msg, f, h, t_l, t_len: t_l };
functionality!();
}
#[cfg(feature = "slh_dsa_shake_192s")]
pub mod slh_dsa_shake_192s {
use crate::hashers::shake::{f, h, h_msg, prf, prf_msg, t_l};
use crate::hashers::Hashers;
pub const N: usize = 24;
const H: usize = 63;
const D: usize = 7;
const HP: usize = 9;
const A: usize = 14;
const K: usize = 17;
const M: usize = 39;
const LEN: usize = 2 * N + 3;
pub const PK_LEN: usize = 48;
pub const SIG_LEN: usize = 16224;
pub const SK_LEN: usize = PK_LEN * 2;
static HASHERS: Hashers<K, LEN, M, N> =
Hashers::<K, LEN, M, N> { h_msg, prf, prf_msg, f, h, t_l, t_len: t_l };
functionality!();
}
#[cfg(feature = "slh_dsa_sha2_192f")]
pub mod slh_dsa_sha2_192f {
use crate::hashers::sha2_cat_3_5::{f, h, h_msg, prf, prf_msg, t_l};
use crate::hashers::Hashers;
pub const N: usize = 24;
const H: usize = 66;
const D: usize = 22;
const HP: usize = 3;
const A: usize = 8;
const K: usize = 33;
const M: usize = 42;
const LEN: usize = 2 * N + 3;
pub const PK_LEN: usize = 48;
pub const SIG_LEN: usize = 35664;
pub const SK_LEN: usize = PK_LEN * 2;
static HASHERS: Hashers<K, LEN, M, N> =
Hashers::<K, LEN, M, N> { h_msg, prf, prf_msg, f, h, t_l, t_len: t_l };
functionality!();
}
#[cfg(feature = "slh_dsa_shake_192f")]
pub mod slh_dsa_shake_192f {
use crate::hashers::shake::{f, h, h_msg, prf, prf_msg, t_l};
use crate::hashers::Hashers;
pub const N: usize = 24;
const H: usize = 66;
const D: usize = 22;
const HP: usize = 3;
const A: usize = 8;
const K: usize = 33;
const M: usize = 42;
const LEN: usize = 2 * N + 3;
pub const PK_LEN: usize = 48;
pub const SIG_LEN: usize = 35664;
pub const SK_LEN: usize = PK_LEN * 2;
static HASHERS: Hashers<K, LEN, M, N> =
Hashers::<K, LEN, M, N> { h_msg, prf, prf_msg, f, h, t_l, t_len: t_l };
functionality!();
}
#[cfg(feature = "slh_dsa_sha2_256s")]
pub mod slh_dsa_sha2_256s {
use crate::hashers::sha2_cat_3_5::{f, h, h_msg, prf, prf_msg, t_l};
use crate::hashers::Hashers;
pub const N: usize = 32;
const H: usize = 64;
const D: usize = 8;
const HP: usize = 8;
const A: usize = 14;
const K: usize = 22;
const M: usize = 47;
const LEN: usize = 2 * N + 3;
pub const PK_LEN: usize = 64;
pub const SIG_LEN: usize = 29792;
pub const SK_LEN: usize = PK_LEN * 2;
static HASHERS: Hashers<K, LEN, M, N> =
Hashers::<K, LEN, M, N> { h_msg, prf, prf_msg, f, h, t_l, t_len: t_l };
functionality!();
}
#[cfg(feature = "slh_dsa_shake_256s")]
pub mod slh_dsa_shake_256s {
use crate::hashers::shake::{f, h, h_msg, prf, prf_msg, t_l};
use crate::hashers::Hashers;
pub const N: usize = 32;
const H: usize = 64;
const D: usize = 8;
const HP: usize = 8;
const A: usize = 14;
const K: usize = 22;
const M: usize = 47;
const LEN: usize = 2 * N + 3;
pub const PK_LEN: usize = 64;
pub const SIG_LEN: usize = 29792;
pub const SK_LEN: usize = PK_LEN * 2;
static HASHERS: Hashers<K, LEN, M, N> =
Hashers::<K, LEN, M, N> { h_msg, prf, prf_msg, f, h, t_l, t_len: t_l };
functionality!();
}
#[cfg(feature = "slh_dsa_sha2_256f")]
pub mod slh_dsa_sha2_256f {
use crate::hashers::sha2_cat_3_5::{f, h, h_msg, prf, prf_msg, t_l};
use crate::hashers::Hashers;
pub const N: usize = 32;
const H: usize = 68;
const D: usize = 17;
const HP: usize = 4;
const A: usize = 9;
const K: usize = 35;
const M: usize = 49;
const LEN: usize = 2 * N + 3;
pub const PK_LEN: usize = 64;
pub const SIG_LEN: usize = 49856;
pub const SK_LEN: usize = PK_LEN * 2;
static HASHERS: Hashers<K, LEN, M, N> =
Hashers::<K, LEN, M, N> { h_msg, prf, prf_msg, f, h, t_l, t_len: t_l };
functionality!();
}
#[cfg(feature = "slh_dsa_shake_256f")]
pub mod slh_dsa_shake_256f {
use crate::hashers::shake::{f, h, h_msg, prf, prf_msg, t_l};
use crate::hashers::Hashers;
pub const N: usize = 32;
const H: usize = 68;
const D: usize = 17;
const HP: usize = 4;
const A: usize = 9;
const K: usize = 35;
const M: usize = 49;
const LEN: usize = 2 * N + 3;
pub const PK_LEN: usize = 64;
pub const SIG_LEN: usize = 49856;
pub const SK_LEN: usize = PK_LEN * 2;
static HASHERS: Hashers<K, LEN, M, N> =
Hashers::<K, LEN, M, N> { h_msg, prf, prf_msg, f, h, t_l, t_len: t_l };
functionality!();
}