Skip to main content

pqbox/
lib.rs

1//! # pqbox
2//!
3//! A post-quantum crypto toolkit built from RustCrypto crates, Cryspen's formally verified
4//! `libcrux-ml-kem` (ML-KEM-1024) and, for Classic McEliece, `classic-mceliece-rust`. It
5//! implements no primitive itself: it composes pinned crates, defines hybrid combiners and a
6//! versioned wire format.
7//!
8//! Three suites ([`Suite`]):
9//!
10//! | Id | Name | KEM | Signature |
11//! |---|---|---|---|
12//! | `0x01` | `xwing` (compat only) | X-Wing (ML-KEM-768 + X25519) | ML-DSA-65 + Ed25519 |
13//! | `0x02` | `pqbox-max` (default) | ML-KEM-1024 + ECDH P-384 | ML-DSA-87 + ECDSA P-384 |
14//! | `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 |
15//!
16//! `pqbox-paranoid` is compiled only with the `paranoid` cargo feature, which is off by default.
17//! Parsers and [`open`] accept only `pqbox-max` unless given a [`SuitePolicy`] that allows more
18//! (see [`suite`]).
19//!
20//! Symmetric primitives: XChaCha20-Poly1305 (default) and AES-256-GCM ([`aead`]), HKDF-SHA-256
21//! and HMAC-SHA-256 ([`kdf`]), SHA-256, SHA3-256 and BLAKE3 ([`hash`]), Argon2id ([`password`]).
22//! Sealed boxes ([`seal`]) combine a suite's KEM with HKDF-SHA-256 and XChaCha20-Poly1305.
23//!
24//! The crate is sans-IO: it never touches files, the network or the clock, and every function
25//! that needs randomness takes a caller-provided CSPRNG ([`Rng`]).
26#![forbid(unsafe_code)]
27#![warn(missing_docs, clippy::pedantic)]
28#![cfg_attr(
29    not(test),
30    deny(
31        clippy::unwrap_used,
32        clippy::expect_used,
33        clippy::indexing_slicing,
34        clippy::panic,
35        clippy::unreachable,
36        clippy::todo,
37        clippy::unimplemented
38    )
39)]
40
41pub mod aead;
42pub mod error;
43pub mod format;
44pub mod hash;
45pub mod hazmat;
46pub mod kdf;
47pub mod kem;
48pub mod password;
49pub mod seal;
50pub mod sign;
51pub mod suite;
52
53pub use error::{Error, Result};
54pub use seal::{open, open_with_policy, seal};
55pub use suite::{Suite, SuitePolicy};
56
57/// A cryptographically secure RNG as this crate expects it (`rand_core` 0.10, infallible).
58///
59/// With `getrandom` 0.4, `rand_core::UnwrapErr(getrandom::SysRng)` is one.
60pub trait Rng: rand_core::CryptoRng {}
61impl<R: rand_core::CryptoRng + ?Sized> Rng for R {}
62
63/// Fills a fresh array with random bytes.
64pub(crate) fn random<const N: usize, R: Rng + ?Sized>(rng: &mut R) -> [u8; N] {
65    let mut out = [0u8; N];
66    rng.fill_bytes(&mut out);
67    out
68}
69
70/// Constant-time equality of two byte strings (lengths are not secret).
71#[must_use]
72pub fn ct_eq(a: &[u8], b: &[u8]) -> bool {
73    use subtle::ConstantTimeEq;
74    a.len() == b.len() && bool::from(a.ct_eq(b))
75}
76
77#[cfg(test)]
78pub(crate) mod test_util {
79    use rand_core::UnwrapErr;
80
81    pub fn rng() -> UnwrapErr<getrandom::SysRng> {
82        UnwrapErr(getrandom::SysRng)
83    }
84
85    pub fn hex(s: &str) -> Vec<u8> {
86        assert!(s.len().is_multiple_of(2), "odd-length hex");
87        (0..s.len())
88            .step_by(2)
89            .map(|i| u8::from_str_radix(&s[i..i + 2], 16).expect("hex digit"))
90            .collect()
91    }
92
93    pub fn to_hex(b: &[u8]) -> String {
94        use core::fmt::Write;
95        b.iter().fold(String::new(), |mut s, x| {
96            let _ = write!(s, "{x:02x}");
97            s
98        })
99    }
100}