SPB Git

Repository Public Apache-2.0

kat.rs 5.3 KB 157 lines rust

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project          : SPB Quantum Crypto7// File             : crates/spb-pqc-core/src/kat.rs8// Module           : Crates9// Repository       : https://git.spboucher.ai/spb-quantum-crypto10// Project Website  : https://pqce.spboucher.ai11// Created          : 2026-12-0312// Last Modified    : 2026-12-0313//14// Author           : Simon-Pierre Boucher15// Position         : Professor of Finance16// Institution      : Université du Québec en Outaouais (UQO)17// Organization     : SP Boucher18// Website          : https://spboucher.ai19// Git              : https://git.spboucher.ai20//21// Preferred Contact: contact@spboucher.ai22// Development      : dev@spboucher.ai23// Support          : support@spboucher.ai24// Security/Admin   : admin@spboucher.ai25//26// Copyright (c) 202627// Simon-Pierre Boucher28// All rights reserved where applicable. This file is governed by the29// applicable project license, copyright notices and relevant law.30// Existing third-party rights and notices remain unaffected.31//32// License          : Apache-2.033//34// Do not embed credentials, private keys, access tokens,35// certificates or confidential personal information.36//37// =============================================================================38// END OF SP BOUCHER SOURCE HEADER39// =============================================================================4041//! Deterministic "internal" interfaces of FIPS 203 / 204 / 205, exposed **only**42//! with the `kat` cargo feature so that official test vectors (which fix the43//! seeds) can be replayed. Products must never enable this feature: with it, a44//! caller chooses the randomness.4546use crate::error::Result;47use crate::kem::{mlkem, KemAlg};4849/// `ML-KEM.KeyGen_internal(d, z)` → `(ek, dk)`.50pub fn mlkem_keygen(alg: KemAlg, d: &[u8; 32], z: &[u8; 32]) -> Result<(Vec<u8>, Vec<u8>)> {51    let p = alg.params();52    let mut ek = vec![0u8; p.ek_len()];53    let mut dk = vec![0u8; p.dk_len()];54    mlkem::keygen_internal(&p, d, z, &mut ek, &mut dk)?;55    Ok((ek, dk))56}5758/// `ML-KEM.Encaps_internal(ek, m)` → `(c, K)`, after the §7.2 key check.59pub fn mlkem_encaps(alg: KemAlg, ek: &[u8], m: &[u8; 32]) -> Result<(Vec<u8>, [u8; 32])> {60    let p = alg.params();61    mlkem::check_encapsulation_key(&p, ek)?;62    let mut c = vec![0u8; p.ct_len()];63    let k = mlkem::encaps_internal(&p, ek, m, &mut c)?;64    Ok((c, k))65}6667/// `ML-KEM.Decaps_internal(dk, c)` → `K`, after the §7.3 key check.68pub fn mlkem_decaps(alg: KemAlg, dk: &[u8], c: &[u8]) -> Result<[u8; 32]> {69    let p = alg.params();70    mlkem::check_decapsulation_key(&p, dk)?;71    mlkem::decaps_internal(&p, dk, c)72}7374use crate::hash::HashAlg;75use crate::signature::{self, mldsa, slhdsa, SigAlg, SignOptions};7677fn mldsa_params(alg: SigAlg) -> Result<mldsa::Params> {78    Ok(match alg {79        SigAlg::MlDsa44 => mldsa::MLDSA_44,80        SigAlg::MlDsa65 => mldsa::MLDSA_65,81        SigAlg::MlDsa87 => mldsa::MLDSA_87,82        _ => return Err(crate::Error::UnsupportedAlgorithm),83    })84}8586fn slhdsa_params(alg: SigAlg) -> Result<slhdsa::Params> {87    slhdsa::ALL88        .iter()89        .find(|(name, _)| *name == alg.name())90        .map(|(_, p)| *p)91        .ok_or(crate::Error::UnsupportedAlgorithm)92}9394/// `ML-DSA.KeyGen_internal(ξ)` → `(pk, sk)`.95pub fn mldsa_keygen(alg: SigAlg, xi: &[u8; 32]) -> Result<(Vec<u8>, Vec<u8>)> {96    Ok(mldsa::keygen_internal(&mldsa_params(alg)?, xi))97}9899/// ML-DSA signing from an externally computed `μ` with explicit `rnd`.100pub fn mldsa_sign_mu(alg: SigAlg, sk: &[u8], mu: &[u8; 64], rnd: &[u8; 32]) -> Result<Vec<u8>> {101    mldsa::sign_mu(&mldsa_params(alg)?, sk, mu, rnd)102}103104/// ML-DSA verification from an externally computed `μ`.105pub fn mldsa_verify_mu(alg: SigAlg, pk: &[u8], mu: &[u8; 64], sig: &[u8]) -> Result<bool> {106    Ok(mldsa::verify_mu(&mldsa_params(alg)?, pk, mu, sig))107}108109/// `slh_keygen_internal(SK.seed, SK.prf, PK.seed)` → `(sk, pk)`.110pub fn slhdsa_keygen(111    alg: SigAlg,112    sk_seed: &[u8],113    sk_prf: &[u8],114    pk_seed: &[u8],115) -> Result<(Vec<u8>, Vec<u8>)> {116    slhdsa::keygen_internal(&slhdsa_params(alg)?, sk_seed, sk_prf, pk_seed)117}118119/// Internal signing interface of FIPS 204 / 205 on an already-formatted `M'`.120/// `rnd = None` is the deterministic variant.121pub fn sign_internal(122    alg: SigAlg,123    sk: &[u8],124    m_prime: &[u8],125    rnd: Option<&[u8]>,126) -> Result<Vec<u8>> {127    signature::sign_raw(alg, sk, &[m_prime], rnd)128}129130/// Internal verification interface on an already-formatted `M'`.131pub fn verify_internal(alg: SigAlg, pk: &[u8], m_prime: &[u8], sig: &[u8]) -> bool {132    signature::verify_raw(alg, pk, &[m_prime], sig)133}134135/// External signing interface (context, optional pre-hash) with explicit136/// randomness instead of the OS CSPRNG. `rnd = None` is the deterministic variant.137pub fn sign_external(138    alg: SigAlg,139    sk: &[u8],140    message: &[u8],141    context: &[u8],142    prehash: Option<HashAlg>,143    rnd: Option<&[u8]>,144) -> Result<Vec<u8>> {145    signature::sign_external_with_rnd(146        alg,147        sk,148        message,149        &SignOptions {150            context,151            prehash,152            deterministic: rnd.is_none(),153        },154        rnd,155    )156}157
Files main
Menu

Theme