pqbox 1.2.0

Post-quantum crypto toolkit: hybrid ML-KEM-1024 + P-384 sealed boxes, composite ML-DSA-87 signatures, an opt-in paranoid suite adding Classic McEliece and SLH-DSA, AEAD, KDF, hashing and Argon2id
Documentation
//! # pqbox
//!
//! A post-quantum crypto toolkit built from RustCrypto crates, Cryspen's formally verified
//! `libcrux-ml-kem` (ML-KEM-1024) and, for Classic McEliece, `classic-mceliece-rust`. It
//! implements no primitive itself: it composes pinned crates, defines hybrid combiners and a
//! versioned wire format.
//!
//! Three suites ([`Suite`]):
//!
//! | Id | Name | KEM | Signature |
//! |---|---|---|---|
//! | `0x01` | `xwing` (compat only) | X-Wing (ML-KEM-768 + X25519) | ML-DSA-65 + Ed25519 |
//! | `0x02` | `pqbox-max` (default) | ML-KEM-1024 + ECDH P-384 | ML-DSA-87 + ECDSA P-384 |
//! | `0x03` | `pqbox-paranoid` (feature `paranoid`, experimental) | ML-KEM-1024 + Classic McEliece 8192128 + ECDH P-384 | ML-DSA-87 + SLH-DSA-SHAKE-256s + ECDSA P-384 |
//!
//! `pqbox-paranoid` is compiled only with the `paranoid` cargo feature, which is off by default.
//! Parsers and [`open`] accept only `pqbox-max` unless given a [`SuitePolicy`] that allows more
//! (see [`suite`]).
//!
//! Symmetric primitives: XChaCha20-Poly1305 (default) and AES-256-GCM ([`aead`]), HKDF-SHA-256
//! and HMAC-SHA-256 ([`kdf`]), SHA-256, SHA3-256 and BLAKE3 ([`hash`]), Argon2id ([`password`]).
//! Sealed boxes ([`seal`]) combine a suite's KEM with HKDF-SHA-256 and XChaCha20-Poly1305.
//!
//! # Cargo features
//!
//! | Feature | Default | Compiles |
//! |---|---|---|
//! | `suite-max` | yes | the `pqbox-max` suite: ML-KEM-1024 (`libcrux-ml-kem`), ML-DSA-87, P-384 |
//! | `suite-xwing` | yes | the `xwing` suite: X-Wing (`x-wing`, `ml-kem`), ML-DSA-65, X25519, Ed25519 |
//! | `password` | yes | [`password`]: Argon2id (`argon2`) |
//! | `paranoid` | no | the `pqbox-paranoid` suite: Classic McEliece, SLH-DSA; implies `suite-max` |
//!
//! [`aead`], [`kdf`], [`hash`], [`format`], [`ct_eq`] and [`Error`] are always compiled. The
//! suite modules ([`suite`], [`kem`], [`sign`], [`seal`], [`hazmat`]) need at least one of
//! `suite-max` and `suite-xwing`. A suite that is compiled out keeps its id and name: its objects
//! are refused with [`Error::SuiteNotEnabled`], never parsed as another suite.
//!
//! The crate is sans-IO: it never touches files, the network or the clock, and every function
//! that needs randomness takes a caller-provided CSPRNG ([`Rng`]).
#![forbid(unsafe_code)]
#![warn(missing_docs, clippy::pedantic)]
#![cfg_attr(
    not(test),
    deny(
        clippy::unwrap_used,
        clippy::expect_used,
        clippy::indexing_slicing,
        clippy::panic,
        clippy::unreachable,
        clippy::todo,
        clippy::unimplemented
    )
)]

pub mod aead;
pub mod error;
pub mod format;
pub mod hash;
#[cfg(any(feature = "suite-max", feature = "suite-xwing"))]
pub mod hazmat;
pub mod kdf;
#[cfg(any(feature = "suite-max", feature = "suite-xwing"))]
pub mod kem;
#[cfg(feature = "password")]
pub mod password;
#[cfg(any(feature = "suite-max", feature = "suite-xwing"))]
pub mod seal;
#[cfg(any(feature = "suite-max", feature = "suite-xwing"))]
pub mod sign;
#[cfg(any(feature = "suite-max", feature = "suite-xwing"))]
pub mod suite;

pub use error::{Error, Result};
#[cfg(any(feature = "suite-max", feature = "suite-xwing"))]
pub use seal::{open, open_with_policy, seal};
#[cfg(any(feature = "suite-max", feature = "suite-xwing"))]
pub use suite::{Suite, SuitePolicy};

/// A cryptographically secure RNG as this crate expects it (`rand_core` 0.10, infallible).
///
/// With `getrandom` 0.4, `rand_core::UnwrapErr(getrandom::SysRng)` is one.
pub trait Rng: rand_core::CryptoRng {}
impl<R: rand_core::CryptoRng + ?Sized> Rng for R {}

/// Fills a fresh array with random bytes.
pub(crate) fn random<const N: usize, R: Rng + ?Sized>(rng: &mut R) -> [u8; N] {
    let mut out = [0u8; N];
    rng.fill_bytes(&mut out);
    out
}

/// Constant-time equality of two byte strings (lengths are not secret).
#[must_use]
pub fn ct_eq(a: &[u8], b: &[u8]) -> bool {
    use subtle::ConstantTimeEq;
    a.len() == b.len() && bool::from(a.ct_eq(b))
}

#[cfg(test)]
pub(crate) mod test_util {
    use rand_core::UnwrapErr;

    pub fn rng() -> UnwrapErr<getrandom::SysRng> {
        UnwrapErr(getrandom::SysRng)
    }

    pub fn hex(s: &str) -> Vec<u8> {
        assert!(s.len().is_multiple_of(2), "odd-length hex");
        (0..s.len())
            .step_by(2)
            .map(|i| u8::from_str_radix(&s[i..i + 2], 16).expect("hex digit"))
            .collect()
    }

    #[cfg(any(feature = "suite-max", feature = "suite-xwing"))]
    pub fn to_hex(b: &[u8]) -> String {
        use core::fmt::Write;
        b.iter().fold(String::new(), |mut s, x| {
            let _ = write!(s, "{x:02x}");
            s
        })
    }
}