fips205-source/src/lib.rs

381 lines
11 KiB
Rust
Raw Normal View History

2024-01-25 21:17:18 +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-01-11 23:49:25 +00:00
2024-01-28 00:58:56 +00:00
/// TKTK crate doc
2024-01-27 20:43:14 +00:00
/// crate doc?
2024-01-28 00:58:56 +00:00
// TODO
// 1. General clean-up
2024-01-31 23:40:32 +00:00
// 2. NOTE: SHA2 HASHERS DUMMIED UP TO PASS TEST; IMPLEMENT!
// 3. Implement SHA2 KATs
2024-01-28 00:58:56 +00:00
// 4. Separate into proper files
// 5. Doc, of course!
2024-01-11 23:49:25 +00:00
mod algs;
2024-01-27 20:43:14 +00:00
mod hashers;
2024-01-22 00:15:52 +00:00
mod test;
2024-01-11 23:49:25 +00:00
mod traits;
2024-01-12 23:22:21 +00:00
mod types;
2024-01-11 23:49:25 +00:00
2024-01-18 18:55:20 +00:00
// Per eqns 5.1-4 on page 16, LGW=4, W=16 and LEN2=3 are constant across all parameter sets.
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-01-28 00:58:56 +00:00
/// blah
2024-01-11 23:49:25 +00:00
macro_rules! functionality {
() => {
2024-01-25 21:17:18 +00:00
use crate::types::{SlhDsaSig, SlhPrivateKey, SlhPublicKey};
2024-01-31 00:57:53 +00:00
//use generic_array::typenum::{Prod, Sum, U2, U3};
2024-01-22 00:15:52 +00:00
use rand_core::CryptoRngCore;
/// blah
2024-01-25 21:17:18 +00:00
/// # Errors
2024-01-22 00:15:52 +00:00
pub fn slh_keygen_with_rng(
rng: &mut impl CryptoRngCore,
) -> Result<(SlhPrivateKey<N>, SlhPublicKey<N>), &'static str> {
2024-01-28 00:58:56 +00:00
crate::algs::slh_keygen_with_rng::<D, H, HP, K, LEN, M, N>(rng, &HASHERS)
2024-01-22 00:15:52 +00:00
}
/// blah
2024-01-25 21:17:18 +00:00
/// # Errors
2024-01-22 00:15:52 +00:00
pub fn slh_sign_with_rng(
rng: &mut impl CryptoRngCore, m: &[u8], sk: &SlhPrivateKey<N>, randomize: bool,
2024-01-26 02:55:05 +00:00
) -> Result<[u8; SIG_LEN], &'static str> {
2024-01-28 00:58:56 +00:00
let sig = crate::algs::slh_sign_with_rng::<A, D, H, HP, K, LEN, M, N>(
2024-01-27 20:43:14 +00:00
rng, &HASHERS, &m, &sk, randomize,
);
2024-01-26 02:55:05 +00:00
sig.map(|s| s.deserialize())
2024-01-22 00:15:52 +00:00
}
/// blah
2024-01-25 21:17:18 +00:00
#[must_use]
2024-01-27 20:43:14 +00:00
pub fn slh_verify(m: &[u8], sig_bytes: &[u8; SIG_LEN], pk: &SlhPublicKey<N>) -> bool {
2024-01-28 00:58:56 +00:00
let sig = SlhDsaSig::<A, D, HP, K, LEN, N>::serialize(sig_bytes);
crate::algs::slh_verify::<A, D, H, HP, K, LEN, M, N>(&HASHERS, &m, &sig, &pk)
2024-01-22 00:15:52 +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-01-25 21:17:18 +00:00
fn simple_loop() {
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;
let (sk, pk) = slh_keygen_with_rng(&mut rng).unwrap();
let sig = slh_sign_with_rng(&mut rng, &message, &sk, false).unwrap();
let result = slh_verify(&message, &sig, &pk);
assert_eq!(result, true, "Signature failed to verify");
2024-01-26 02:55:05 +00:00
message[3] = (i + 1) as u8;
2024-01-25 21:17:18 +00:00
let result = slh_verify(&message, &sig, &pk);
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
};
}
/// TKTK
#[cfg(feature = "slh_dsa_sha2_128s")]
pub mod slh_dsa_sha2_128s {
2024-01-31 23:40:32 +00:00
//use crate::hashers::sha2_cat_1::{f, h, h_msg, prf, prf_msg, t_l, t_len};
use crate::hashers::shake::{f, h, h_msg, prf, prf_msg, t_l, t_len};
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-01-27 20:43:14 +00:00
type LEN = Sum<Prod<U2, N>, U3>;
2024-01-25 21:17:18 +00:00
//const PK_LEN: usize = 32;
2024-01-26 02:55:05 +00:00
const SIG_LEN: usize = 7856;
2024-01-25 21:17:18 +00:00
//const SK_LEN: usize = 0000;
2024-01-28 00:58:56 +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 };
2024-01-11 23:49:25 +00:00
functionality!();
}
/// TKTK
#[cfg(feature = "slh_dsa_shake_128s")]
pub mod slh_dsa_shake_128s {
2024-01-31 00:57:53 +00:00
use crate::hashers::shake::{f, h, h_msg, prf, prf_msg, t_l, t_len};
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;
type LEN = Sum<Prod<U2, N>, U3>;
//const PK_LEN: usize = 32;
2024-01-11 23:49:25 +00:00
const SIG_LEN: usize = 7856;
2024-01-31 00:57:53 +00:00
//const SK_LEN: usize = 0000;
static HASHERS: Hashers<K, LEN, M, N> =
Hashers::<K, LEN, M, N> { h_msg, prf, prf_msg, f, h, t_l, t_len };
2024-01-11 23:49:25 +00:00
functionality!();
}
/// TKTK
#[cfg(feature = "slh_dsa_sha2_128f")]
pub mod slh_dsa_sha2_128f {
2024-01-31 23:40:32 +00:00
//use crate::hashers::sha2_cat_1::{f, h, h_msg, prf, prf_msg, t_l, t_len};
use crate::hashers::shake::{f, h, h_msg, prf, prf_msg, t_l, t_len};
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;
type LEN = Sum<Prod<U2, N>, U3>;
//const PK_LEN: usize = 32;
2024-01-11 23:49:25 +00:00
const SIG_LEN: usize = 17088;
2024-01-31 00:57:53 +00:00
//const SK_LEN: usize = 0000;
static HASHERS: Hashers<K, LEN, M, N> =
Hashers::<K, LEN, M, N> { h_msg, prf, prf_msg, f, h, t_l, t_len };
2024-01-11 23:49:25 +00:00
functionality!();
}
/// TKTK
#[cfg(feature = "slh_dsa_shake_128f")]
pub mod slh_dsa_shake_128f {
2024-01-31 00:57:53 +00:00
use crate::hashers::shake::{f, h, h_msg, prf, prf_msg, t_l, t_len};
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;
type LEN = Sum<Prod<U2, N>, U3>;
//const PK_LEN: usize = 32;
2024-01-11 23:49:25 +00:00
const SIG_LEN: usize = 17088;
2024-01-31 00:57:53 +00:00
//const SK_LEN: usize = 0000;
static HASHERS: Hashers<K, LEN, M, N> =
Hashers::<K, LEN, M, N> { h_msg, prf, prf_msg, f, h, t_l, t_len };
2024-01-11 23:49:25 +00:00
functionality!();
}
/// TKTK
#[cfg(feature = "slh_dsa_sha2_192s")]
pub mod slh_dsa_sha2_192s {
2024-01-31 23:40:32 +00:00
//use crate::hashers::sha2_cat_3_5::{f, h, h_msg, prf, prf_msg, t_l, t_len};
use crate::hashers::shake::{f, h, h_msg, prf, prf_msg, t_l, t_len};
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;
type LEN = Sum<Prod<U2, N>, U3>;
//const PK_LEN: usize = 32;
2024-01-11 23:49:25 +00:00
const SIG_LEN: usize = 16224;
2024-01-31 00:57:53 +00:00
//const SK_LEN: usize = 0000;
static HASHERS: Hashers<K, LEN, M, N> =
Hashers::<K, LEN, M, N> { h_msg, prf, prf_msg, f, h, t_l, t_len };
2024-01-11 23:49:25 +00:00
functionality!();
}
/// TKTK
#[cfg(feature = "slh_dsa_shake_192s")]
pub mod slh_dsa_shake_192s {
2024-01-31 00:57:53 +00:00
use crate::hashers::shake::{f, h, h_msg, prf, prf_msg, t_l, t_len};
2024-01-31 23:40:32 +00:00
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;
type LEN = Sum<Prod<U2, N>, U3>;
//const PK_LEN: usize = 32;
2024-01-11 23:49:25 +00:00
const SIG_LEN: usize = 16224;
2024-01-31 00:57:53 +00:00
//const SK_LEN: usize = 0000;
static HASHERS: Hashers<K, LEN, M, N> =
Hashers::<K, LEN, M, N> { h_msg, prf, prf_msg, f, h, t_l, t_len };
2024-01-11 23:49:25 +00:00
functionality!();
}
/// TKTK
#[cfg(feature = "slh_dsa_sha2_192f")]
pub mod slh_dsa_sha2_192f {
2024-01-31 23:40:32 +00:00
//use crate::hashers::sha2_cat_3_5::{f, h, h_msg, prf, prf_msg, t_l, t_len};
use crate::hashers::shake::{f, h, h_msg, prf, prf_msg, t_l, t_len};
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;
type LEN = Sum<Prod<U2, N>, U3>;
//const PK_LEN: usize = 32;
2024-01-11 23:49:25 +00:00
const SIG_LEN: usize = 35664;
2024-01-31 00:57:53 +00:00
//const SK_LEN: usize = 0000;
static HASHERS: Hashers<K, LEN, M, N> =
Hashers::<K, LEN, M, N> { h_msg, prf, prf_msg, f, h, t_l, t_len };
2024-01-11 23:49:25 +00:00
functionality!();
}
/// TKTK
#[cfg(feature = "slh_dsa_shake_192f")]
pub mod slh_dsa_shake_192f {
2024-01-31 00:57:53 +00:00
use crate::hashers::shake::{f, h, h_msg, prf, prf_msg, t_l, t_len};
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;
type LEN = Sum<Prod<U2, N>, U3>;
//const PK_LEN: usize = 32;
2024-01-11 23:49:25 +00:00
const SIG_LEN: usize = 35664;
2024-01-31 00:57:53 +00:00
//const SK_LEN: usize = 0000;
static HASHERS: Hashers<K, LEN, M, N> =
Hashers::<K, LEN, M, N> { h_msg, prf, prf_msg, f, h, t_l, t_len };
2024-01-11 23:49:25 +00:00
functionality!();
}
/// TKTK
#[cfg(feature = "slh_dsa_sha2_256s")]
pub mod slh_dsa_sha2_256s {
2024-01-31 23:40:32 +00:00
//use crate::hashers::sha2_cat_3_5::{f, h, h_msg, prf, prf_msg, t_l, t_len};
use crate::hashers::shake::{f, h, h_msg, prf, prf_msg, t_l, t_len};
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;
type LEN = Sum<Prod<U2, N>, U3>;
//const PK_LEN: usize = 32;
2024-01-11 23:49:25 +00:00
const SIG_LEN: usize = 29792;
2024-01-31 00:57:53 +00:00
//const SK_LEN: usize = 0000;
static HASHERS: Hashers<K, LEN, M, N> =
Hashers::<K, LEN, M, N> { h_msg, prf, prf_msg, f, h, t_l, t_len };
2024-01-11 23:49:25 +00:00
functionality!();
2023-11-26 20:18:22 +00:00
}
2024-01-11 23:49:25 +00:00
/// TKTK
#[cfg(feature = "slh_dsa_shake_256s")]
pub mod slh_dsa_shake_256s {
2024-01-31 00:57:53 +00:00
use crate::hashers::shake::{f, h, h_msg, prf, prf_msg, t_l, t_len};
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;
type LEN = Sum<Prod<U2, N>, U3>;
//const PK_LEN: usize = 32;
2024-01-11 23:49:25 +00:00
const SIG_LEN: usize = 29792;
2024-01-31 00:57:53 +00:00
//const SK_LEN: usize = 0000;
static HASHERS: Hashers<K, LEN, M, N> =
Hashers::<K, LEN, M, N> { h_msg, prf, prf_msg, f, h, t_l, t_len };
2024-01-11 23:49:25 +00:00
functionality!();
}
/// TKTK
#[cfg(feature = "slh_dsa_sha2_256f")]
pub mod slh_dsa_sha2_256f {
2024-01-31 23:40:32 +00:00
//use crate::hashers::sha2_cat_3_5::{f, h, h_msg, prf, prf_msg, t_l, t_len};
use crate::hashers::shake::{f, h, h_msg, prf, prf_msg, t_l, t_len};
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;
type LEN = Sum<Prod<U2, N>, U3>;
//const PK_LEN: usize = 32;
2024-01-11 23:49:25 +00:00
const SIG_LEN: usize = 49856;
2024-01-31 00:57:53 +00:00
//const SK_LEN: usize = 0000;
static HASHERS: Hashers<K, LEN, M, N> =
Hashers::<K, LEN, M, N> { h_msg, prf, prf_msg, f, h, t_l, t_len };
2024-01-11 23:49:25 +00:00
functionality!();
}
/// TKTK
#[cfg(feature = "slh_dsa_shake_256f")]
pub mod slh_dsa_shake_256f {
2024-01-31 00:57:53 +00:00
use crate::hashers::shake::{f, h, h_msg, prf, prf_msg, t_l, t_len};
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;
type LEN = Sum<Prod<U2, N>, U3>;
//const PK_LEN: usize = 32;
2024-01-11 23:49:25 +00:00
const SIG_LEN: usize = 49856;
2024-01-31 00:57:53 +00:00
//const SK_LEN: usize = 0000;
static HASHERS: Hashers<K, LEN, M, N> =
Hashers::<K, LEN, M, N> { h_msg, prf, prf_msg, f, h, t_l, t_len };
2024-01-11 23:49:25 +00:00
functionality!();
}