fips205-source/src/lib.rs

492 lines
14 KiB
Rust
Raw Normal View History

2024-02-07 21:31:17 +00:00
#![no_std]
2024-01-11 23:49:25 +00:00
#![deny(clippy::pedantic)]
#![deny(warnings)]
2024-01-27 20:43:14 +00:00
//#![deny(missing_docs)]
2024-02-09 23:43:59 +00:00
#![doc = include_str!("../README.md")]
/// Implements FIPS 205 draft Stateless Hash-Based Digital Signature Standard.
/// See <https://csrc.nist.gov/pubs/fips/205/ipd>
2024-01-11 23:49:25 +00:00
2024-01-28 00:58:56 +00:00
/// TKTK crate doc
// TODO
// 1. General clean-up
2024-02-08 12:42:28 +00:00
// 7. Doc, of course!
2024-02-09 23:43:59 +00:00
2024-02-09 22:31:05 +00:00
mod fors;
2024-01-27 20:43:14 +00:00
mod hashers;
2024-02-09 22:31:05 +00:00
mod helpers;
mod hypertree;
mod slh;
2024-02-09 23:43:59 +00:00
pub mod traits;
2024-01-12 23:22:21 +00:00
mod types;
2024-02-09 22:31:05 +00:00
mod wots;
mod xmss;
2024-01-11 23:49:25 +00:00
2024-02-07 21:31:17 +00:00
// Per eqns 5.1-4 on page 16, LGW=4, W=16 and LEN2=3 are constant across all security parameter sets.
2024-01-18 18:55:20 +00:00
const LGW: u32 = 4;
const W: u32 = 16;
2024-01-25 21:17:18 +00:00
const LEN2: u32 = 3;
2024-01-18 18:55:20 +00:00
2024-02-07 21:31:17 +00:00
2024-01-28 00:58:56 +00:00
/// blah
2024-01-11 23:49:25 +00:00
macro_rules! functionality {
() => {
2024-02-09 21:28:59 +00:00
use crate::traits::{KeyGen, SerDes, Signer, Verifier};
2024-01-25 21:17:18 +00:00
use crate::types::{SlhDsaSig, SlhPrivateKey, SlhPublicKey};
2024-01-22 00:15:52 +00:00
use rand_core::CryptoRngCore;
2024-02-09 21:28:59 +00:00
use zeroize::{Zeroize, ZeroizeOnDrop};
2024-02-09 22:31:05 +00:00
2024-02-09 21:28:59 +00:00
#[derive(Zeroize, ZeroizeOnDrop)]
pub struct PublicKey(SlhPublicKey<N>);
#[derive(Zeroize, ZeroizeOnDrop)]
pub struct PrivateKey(SlhPrivateKey<N>);
#[derive(Zeroize, ZeroizeOnDrop)]
pub struct KG(); // Arguable how useful an empty struct+trait is...
2024-01-22 00:15:52 +00:00
2024-02-09 22:31:05 +00:00
2024-01-22 00:15:52 +00:00
/// blah
2024-01-25 21:17:18 +00:00
/// # Errors
2024-02-09 21:28:59 +00:00
impl KeyGen for KG {
type PrivateKey = PrivateKey;
type PublicKey = PublicKey;
fn try_keygen_with_rng_vt(
rng: &mut impl CryptoRngCore,
) -> Result<(PublicKey, PrivateKey), &'static str> {
2024-02-09 22:31:05 +00:00
let res = crate::slh::slh_keygen_with_rng::<D, H, HP, K, Len, M, N>(rng, &HASHERS);
2024-02-09 21:28:59 +00:00
res.map(|(sk, pk)| (PublicKey(pk), PrivateKey(sk)))
}
2024-01-22 00:15:52 +00:00
}
2024-02-09 22:31:05 +00:00
/// blah
/// # Errors
2024-02-09 21:28:59 +00:00
#[cfg(feature = "default-rng")]
pub fn try_keygen_vt() -> Result<(PublicKey, PrivateKey), &'static str> {
KG::try_keygen_vt()
2024-01-22 00:15:52 +00:00
}
2024-02-09 22:31:05 +00:00
2024-02-09 21:28:59 +00:00
impl Signer for PrivateKey {
type Signature = [u8; SIG_LEN];
/// blah
/// # Errors
fn try_sign_with_rng_ct(
&self, rng: &mut impl CryptoRngCore, m: &[u8], randomize: bool,
) -> Result<[u8; SIG_LEN], &'static str> {
2024-02-09 22:31:05 +00:00
let sig = crate::slh::slh_sign_with_rng::<A, D, H, HP, K, Len, M, N>(
2024-02-09 21:28:59 +00:00
rng, &HASHERS, &m, &self.0, randomize,
);
sig.map(|s| s.deserialize())
}
}
2024-02-09 22:31:05 +00:00
2024-02-09 21:28:59 +00:00
impl Verifier for PublicKey {
type Signature = [u8; SIG_LEN];
/// blah
2024-02-09 22:31:05 +00:00
fn try_verify_vt(
&self, m: &[u8], sig_bytes: &[u8; SIG_LEN],
2024-02-09 21:28:59 +00:00
) -> Result<bool, &'static str> {
let sig = SlhDsaSig::<A, D, HP, K, Len, N>::serialize(sig_bytes);
2024-02-09 22:31:05 +00:00
let res = crate::slh::slh_verify::<A, D, H, HP, K, Len, M, N>(
&HASHERS, &m, &sig, &self.0,
);
2024-02-09 21:28:59 +00:00
Ok(res)
}
}
2024-02-09 22:31:05 +00:00
2024-02-09 21:28:59 +00:00
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> {
// Result: opportunity for validation
let mut pk = SlhPublicKey::default();
pk.pk_seed.copy_from_slice(&bytes[..(PK_LEN / 2)]);
pk.pk_root.copy_from_slice(&bytes[(PK_LEN / 2)..]);
Ok(PublicKey(pk))
}
}
2024-02-09 22:31:05 +00:00
2024-02-09 21:28:59 +00:00
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> {
// Result: opportunity for validation
let mut sk = SlhPrivateKey::default();
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)..]);
Ok(PrivateKey(sk))
}
2024-01-22 00:15:52 +00:00
}
2024-02-09 22:31:05 +00:00
2024-01-18 18:55:20 +00:00
#[cfg(test)]
mod tests {
use super::*;
2024-01-25 21:17:18 +00:00
use rand_chacha::rand_core::SeedableRng;
2024-01-18 18:55:20 +00:00
#[test]
2024-02-07 21:31:17 +00:00
fn simple_round_trips() {
2024-01-25 21:17:18 +00:00
let mut message = [0u8, 1, 2, 3];
let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(123);
for i in 0..5 {
message[3] = i as u8;
2024-02-09 21:28:59 +00:00
let (pk1, sk1) = KG::try_keygen_with_rng_vt(&mut rng).unwrap();
let pk1_bytes = pk1.into_bytes();
let pk2 = PublicKey::try_from_bytes(&pk1_bytes).unwrap();
let sk1_bytes = sk1.into_bytes();
let sk2 = PrivateKey::try_from_bytes(&sk1_bytes).unwrap();
let sig = sk2.try_sign_with_rng_ct(&mut rng, &message, true).unwrap();
let result = pk2.try_verify_vt(&message, &sig).unwrap();
2024-01-25 21:17:18 +00:00
assert_eq!(result, true, "Signature failed to verify");
2024-01-26 02:55:05 +00:00
message[3] = (i + 1) as u8;
2024-02-09 21:28:59 +00:00
let result = pk2.try_verify_vt(&message, &sig).unwrap();
2024-01-26 02:55:05 +00:00
assert_eq!(result, false, "Signature should not have verified");
2024-01-25 21:17:18 +00:00
}
2024-01-18 18:55:20 +00:00
}
2024-01-12 23:22:21 +00:00
}
2024-01-11 23:49:25 +00:00
};
}
2024-02-07 21:31:17 +00:00
2024-01-11 23:49:25 +00:00
/// TKTK
#[cfg(feature = "slh_dsa_sha2_128s")]
pub mod slh_dsa_sha2_128s {
2024-02-08 12:42:28 +00:00
use crate::hashers::sha2_cat_1::{f, h, h_msg, prf, prf_msg, t_l};
2024-01-27 20:43:14 +00:00
use crate::hashers::Hashers;
2024-01-31 00:57:53 +00:00
use generic_array::typenum::{Prod, Sum, U12, U14, U16, U2, U3, U30, U63, U7, U9};
2024-01-18 18:55:20 +00:00
type N = U16;
type H = U63;
type D = U7;
type HP = U9;
type A = U12;
type K = U14;
type M = U30;
2024-02-08 12:42:28 +00:00
type Len = Sum<Prod<U2, N>, U3>;
2024-02-09 22:31:05 +00:00
2024-02-09 21:28:59 +00:00
pub const PK_LEN: usize = 32;
pub const SIG_LEN: usize = 7856;
pub const SK_LEN: usize = PK_LEN * 2;
2024-02-08 12:42:28 +00:00
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 };
2024-01-11 23:49:25 +00:00
functionality!();
}
2024-02-07 21:31:17 +00:00
2024-01-11 23:49:25 +00:00
/// TKTK
#[cfg(feature = "slh_dsa_shake_128s")]
pub mod slh_dsa_shake_128s {
2024-02-08 12:42:28 +00:00
use crate::hashers::shake::{f, h, h_msg, prf, prf_msg, t_l};
2024-01-31 00:57:53 +00:00
use crate::hashers::Hashers;
use generic_array::typenum::{Prod, Sum, U12, U14, U16, U2, U3, U30, U63, U7, U9};
type N = U16;
type H = U63;
type D = U7;
type HP = U9;
type A = U12;
type K = U14;
type M = U30;
2024-02-08 12:42:28 +00:00
type Len = Sum<Prod<U2, N>, U3>;
2024-02-09 22:31:05 +00:00
2024-02-09 21:28:59 +00:00
pub const PK_LEN: usize = 32;
pub const SIG_LEN: usize = 7856;
pub const SK_LEN: usize = PK_LEN * 2;
2024-02-08 12:42:28 +00:00
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 };
2024-01-11 23:49:25 +00:00
functionality!();
}
2024-02-07 21:31:17 +00:00
2024-01-11 23:49:25 +00:00
/// TKTK
#[cfg(feature = "slh_dsa_sha2_128f")]
pub mod slh_dsa_sha2_128f {
2024-02-08 12:42:28 +00:00
use crate::hashers::sha2_cat_1::{f, h, h_msg, prf, prf_msg, t_l};
2024-01-31 00:57:53 +00:00
use crate::hashers::Hashers;
use generic_array::typenum::{Prod, Sum, U16, U2, U22, U3, U33, U34, U6, U66};
type N = U16;
type H = U66;
type D = U22;
type HP = U3;
type A = U6;
type K = U33;
type M = U34;
2024-02-08 12:42:28 +00:00
type Len = Sum<Prod<U2, N>, U3>;
2024-02-09 22:31:05 +00:00
2024-02-09 21:28:59 +00:00
pub const PK_LEN: usize = 32;
pub const SIG_LEN: usize = 17088;
pub const SK_LEN: usize = PK_LEN * 2;
2024-02-08 12:42:28 +00:00
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 };
2024-01-11 23:49:25 +00:00
functionality!();
}
2024-02-07 21:31:17 +00:00
2024-01-11 23:49:25 +00:00
/// TKTK
#[cfg(feature = "slh_dsa_shake_128f")]
pub mod slh_dsa_shake_128f {
2024-02-08 12:42:28 +00:00
use crate::hashers::shake::{f, h, h_msg, prf, prf_msg, t_l};
2024-01-31 00:57:53 +00:00
use crate::hashers::Hashers;
use generic_array::typenum::{Prod, Sum, U16, U2, U22, U3, U33, U34, U6, U66};
type N = U16;
type H = U66;
type D = U22;
type HP = U3;
type A = U6;
type K = U33;
type M = U34;
2024-02-08 12:42:28 +00:00
type Len = Sum<Prod<U2, N>, U3>;
2024-02-09 22:31:05 +00:00
2024-02-09 21:28:59 +00:00
pub const PK_LEN: usize = 32;
pub const SIG_LEN: usize = 17088;
pub const SK_LEN: usize = PK_LEN * 2;
2024-02-08 12:42:28 +00:00
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 };
2024-01-11 23:49:25 +00:00
functionality!();
}
2024-02-07 21:31:17 +00:00
2024-01-11 23:49:25 +00:00
/// TKTK
#[cfg(feature = "slh_dsa_sha2_192s")]
pub mod slh_dsa_sha2_192s {
2024-02-08 12:42:28 +00:00
use crate::hashers::sha2_cat_3_5::{f, h, h_msg, prf, prf_msg, t_l};
2024-01-31 00:57:53 +00:00
use crate::hashers::Hashers;
use generic_array::typenum::{Prod, Sum, U14, U17, U2, U24, U3, U39, U63, U7, U9};
type N = U24;
type H = U63;
type D = U7;
type HP = U9;
type A = U14;
type K = U17;
type M = U39;
2024-02-08 12:42:28 +00:00
type Len = Sum<Prod<U2, N>, U3>;
2024-02-09 22:31:05 +00:00
2024-02-09 21:28:59 +00:00
pub const PK_LEN: usize = 48;
pub const SIG_LEN: usize = 16224;
pub const SK_LEN: usize = PK_LEN * 2;
2024-02-08 12:42:28 +00:00
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 };
2024-01-11 23:49:25 +00:00
functionality!();
}
2024-02-07 21:31:17 +00:00
2024-01-11 23:49:25 +00:00
/// TKTK
#[cfg(feature = "slh_dsa_shake_192s")]
pub mod slh_dsa_shake_192s {
2024-02-08 12:42:28 +00:00
use crate::hashers::shake::{f, h, h_msg, prf, prf_msg, t_l};
2024-01-31 00:57:53 +00:00
use crate::hashers::Hashers;
use generic_array::typenum::{Prod, Sum, U14, U17, U2, U24, U3, U39, U63, U7, U9};
type N = U24;
type H = U63;
type D = U7;
type HP = U9;
type A = U14;
type K = U17;
type M = U39;
2024-02-08 12:42:28 +00:00
type Len = Sum<Prod<U2, N>, U3>;
2024-02-09 22:31:05 +00:00
2024-02-09 21:28:59 +00:00
pub const PK_LEN: usize = 48;
pub const SIG_LEN: usize = 16224;
pub const SK_LEN: usize = PK_LEN * 2;
2024-02-08 12:42:28 +00:00
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 };
2024-01-11 23:49:25 +00:00
functionality!();
}
2024-02-07 21:31:17 +00:00
2024-01-11 23:49:25 +00:00
/// TKTK
#[cfg(feature = "slh_dsa_sha2_192f")]
pub mod slh_dsa_sha2_192f {
2024-02-08 12:42:28 +00:00
use crate::hashers::sha2_cat_3_5::{f, h, h_msg, prf, prf_msg, t_l};
2024-01-31 00:57:53 +00:00
use crate::hashers::Hashers;
use generic_array::typenum::{Prod, Sum, U2, U22, U24, U3, U33, U42, U66, U8};
type N = U24;
type H = U66;
type D = U22;
type HP = U3;
type A = U8;
type K = U33;
type M = U42;
2024-02-08 12:42:28 +00:00
type Len = Sum<Prod<U2, N>, U3>;
2024-02-09 22:31:05 +00:00
2024-02-09 21:28:59 +00:00
pub const PK_LEN: usize = 48;
pub const SIG_LEN: usize = 35664;
pub const SK_LEN: usize = PK_LEN * 2;
2024-02-08 12:42:28 +00:00
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 };
2024-01-11 23:49:25 +00:00
functionality!();
}
2024-02-07 21:31:17 +00:00
2024-01-11 23:49:25 +00:00
/// TKTK
#[cfg(feature = "slh_dsa_shake_192f")]
pub mod slh_dsa_shake_192f {
2024-02-08 12:42:28 +00:00
use crate::hashers::shake::{f, h, h_msg, prf, prf_msg, t_l};
2024-01-31 00:57:53 +00:00
use crate::hashers::Hashers;
use generic_array::typenum::{Prod, Sum, U2, U22, U24, U3, U33, U42, U66, U8};
type N = U24;
type H = U66;
type D = U22;
type HP = U3;
type A = U8;
type K = U33;
type M = U42;
2024-02-08 12:42:28 +00:00
type Len = Sum<Prod<U2, N>, U3>;
2024-02-09 22:31:05 +00:00
2024-02-09 21:28:59 +00:00
pub const PK_LEN: usize = 48;
pub const SIG_LEN: usize = 35664;
pub const SK_LEN: usize = PK_LEN * 2;
2024-02-08 12:42:28 +00:00
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 };
2024-01-11 23:49:25 +00:00
functionality!();
}
2024-02-07 21:31:17 +00:00
2024-01-11 23:49:25 +00:00
/// TKTK
#[cfg(feature = "slh_dsa_sha2_256s")]
pub mod slh_dsa_sha2_256s {
2024-02-08 12:42:28 +00:00
use crate::hashers::sha2_cat_3_5::{f, h, h_msg, prf, prf_msg, t_l};
2024-01-31 00:57:53 +00:00
use crate::hashers::Hashers;
use generic_array::typenum::{Prod, Sum, U14, U2, U22, U3, U32, U47, U64, U8};
type N = U32;
type H = U64;
type D = U8;
type HP = U8;
type A = U14;
type K = U22;
type M = U47;
2024-02-08 12:42:28 +00:00
type Len = Sum<Prod<U2, N>, U3>;
2024-02-09 22:31:05 +00:00
2024-02-09 21:28:59 +00:00
pub const PK_LEN: usize = 64;
pub const SIG_LEN: usize = 29792;
pub const SK_LEN: usize = PK_LEN * 2;
2024-02-08 12:42:28 +00:00
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 };
2024-01-11 23:49:25 +00:00
functionality!();
2023-11-26 20:18:22 +00:00
}
2024-02-07 21:31:17 +00:00
2024-01-11 23:49:25 +00:00
/// TKTK
#[cfg(feature = "slh_dsa_shake_256s")]
pub mod slh_dsa_shake_256s {
2024-02-08 12:42:28 +00:00
use crate::hashers::shake::{f, h, h_msg, prf, prf_msg, t_l};
2024-01-31 00:57:53 +00:00
use crate::hashers::Hashers;
use generic_array::typenum::{Prod, Sum, U14, U2, U22, U3, U32, U47, U64, U8};
type N = U32;
type H = U64;
type D = U8;
type HP = U8;
type A = U14;
type K = U22;
type M = U47;
2024-02-08 12:42:28 +00:00
type Len = Sum<Prod<U2, N>, U3>;
2024-02-09 22:31:05 +00:00
2024-02-09 21:28:59 +00:00
pub const PK_LEN: usize = 64;
pub const SIG_LEN: usize = 29792;
pub const SK_LEN: usize = PK_LEN * 2;
2024-02-08 12:42:28 +00:00
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 };
2024-01-11 23:49:25 +00:00
functionality!();
}
2024-02-07 21:31:17 +00:00
2024-01-11 23:49:25 +00:00
/// TKTK
#[cfg(feature = "slh_dsa_sha2_256f")]
pub mod slh_dsa_sha2_256f {
2024-02-08 12:42:28 +00:00
use crate::hashers::sha2_cat_3_5::{f, h, h_msg, prf, prf_msg, t_l};
2024-01-31 00:57:53 +00:00
use crate::hashers::Hashers;
use generic_array::typenum::{Prod, Sum, U17, U2, U3, U32, U35, U4, U49, U68, U9};
type N = U32;
type H = U68;
type D = U17;
type HP = U4;
type A = U9;
type K = U35;
type M = U49;
2024-02-08 12:42:28 +00:00
type Len = Sum<Prod<U2, N>, U3>;
2024-02-09 22:31:05 +00:00
2024-02-09 21:28:59 +00:00
pub const PK_LEN: usize = 64;
pub const SIG_LEN: usize = 49856;
pub const SK_LEN: usize = PK_LEN * 2;
2024-02-08 12:42:28 +00:00
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 };
2024-01-11 23:49:25 +00:00
functionality!();
}
2024-02-07 21:31:17 +00:00
2024-01-11 23:49:25 +00:00
/// TKTK
#[cfg(feature = "slh_dsa_shake_256f")]
pub mod slh_dsa_shake_256f {
2024-02-08 12:42:28 +00:00
use crate::hashers::shake::{f, h, h_msg, prf, prf_msg, t_l};
2024-01-31 00:57:53 +00:00
use crate::hashers::Hashers;
use generic_array::typenum::{Prod, Sum, U17, U2, U3, U32, U35, U4, U49, U68, U9};
type N = U32;
type H = U68;
type D = U17;
type HP = U4;
type A = U9;
type K = U35;
type M = U49;
2024-02-08 12:42:28 +00:00
type Len = Sum<Prod<U2, N>, U3>;
2024-02-09 22:31:05 +00:00
2024-02-09 21:28:59 +00:00
pub const PK_LEN: usize = 64;
pub const SIG_LEN: usize = 49856;
pub const SK_LEN: usize = PK_LEN * 2;
2024-02-08 12:42:28 +00:00
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 };
2024-01-11 23:49:25 +00:00
functionality!();
}