origin_crypto_sdk/lib.rs
1// SPDX-License-Identifier: Apache-2.0
2#![cfg_attr(docsrs, warn(missing_docs))]
3
4//! Origin Crypto SDK — Post-quantum and classical cryptographic primitives.
5//!
6//! A standalone Rust SDK for signing, encryption, key derivation, and
7//! key encapsulation, with first-class support for both classical
8//! (Ed25519, X25519) and post-quantum (Falcon-1024, SLH-DSA, ML-DSA,
9//! NTRU Prime, Curve41417) primitives.
10//!
11//! # Quick start
12//!
13//! For most users, importing the [`prelude`] gives you everything:
14//!
15//! ```rust
16//! use origin_crypto_sdk::prelude::*;
17//!
18//! // Hybrid Ed25519 + Falcon-1024 signature (recommended default)
19//! let master_seed = [0x42u8; 32];
20//! let bundle = HybridSigningKeyBundle::from_seed(&master_seed, "my-app")
21//! .expect("valid seed");
22//! let msg = b"sign this";
23//! let sig = bundle.sign_hybrid(msg);
24//! // sig is an Ed25519 + Falcon-1024 hybrid signature
25//! ```
26//!
27//! For users who only need a single primitive:
28//!
29//! ```rust
30//! use origin_crypto_sdk::signing::classical::Ed25519Signer;
31//! use origin_crypto_sdk::signing::postquantum::Falcon1024Signer;
32//!
33//! // Just Ed25519
34//! let ed = Ed25519Signer::from_seed(&[1u8; 32]); // from_seed returns Self
35//! let sig = ed.sign(b"hello");
36//! assert!(ed.verify(b"hello", &sig));
37//!
38//! // Just Falcon-1024
39//! let falcon = Falcon1024Signer::from_seed(&[1u8; 32]).expect("valid seed");
40//! let sig = falcon.sign(b"hello").expect("Falcon sign");
41//! assert!(falcon.verify(b"hello", &sig));
42//! ```
43//!
44//! # Module organization
45//!
46//! - [`signing`] — Direct access to signing primitives, organized by family:
47//! - [`signing::classical`] — Ed25519 (no PQ dependencies)
48//! - [`signing::postquantum`] — Falcon-512/1024, SLH-DSA, ML-DSA
49//! - [`signing::hybrid`] — Recommended default. Ed25519 + PQ combined.
50//! - [`aead`] / [`chacha20_blake3`] — Symmetric encryption (committing & non-committing)
51//! - [`chacha40`] — Post-quantum hardened stream cipher (512-bit key, 40 rounds, dual-state)
52//! - [`chacha20`] — Standard ChaCha20 stream cipher (RFC 8439)
53//! - [`kdf`] — Key derivation (Argon2id, HKDF-SHA3-256)
54//! - [`pqc`] — Raw post-quantum primitives (advanced use)
55//! - [`ec_schnorr`] — secp256k1 Schnorr signatures (native, no external deps)
56//! - [`prelude`] — One-line import for common types
57//!
58//! # Security defaults
59//!
60//! - For **signatures**: use the hybrid (Ed25519 + PQ) — see [`signing::hybrid`].
61//! Both must verify, so a vulnerability in one primitive does not break security.
62//! - For **encryption**: prefer [`chacha20_blake3`] (committing AEAD).
63//! Avoid XChaCha20-Poly1305 unless you specifically need the smaller tag
64//! and accept the partitioning-oracle risk.
65//! - For **PQ-hardened stream cipher**: use [`chacha40`] when 256-bit post-quantum
66//! security against Grover's algorithm is desired. Note that ChaCha40 is a bare
67//! stream cipher — pair with Poly1305 or BLAKE3 for authenticated encryption.
68//! - For **key derivation**: use Argon2id (memory-hard) for password-based KDF,
69//! HKDF-SHA3-256 for HKDF-style derivation.
70//!
71//! # Cargo features
72//!
73//! | Feature | Enables | Default |
74//! |-----------|----------------------------------------|---------|
75//! | `parallel` | Rayon-based parallel processing | **yes** |
76//! | `slh-dsa` | SLH-DSA / SPHINCS+ signatures (FIPS 205) | no |
77//! | `ml-dsa` | ML-DSA / Dilithium signatures (FIPS 204) | no |
78//! | `pqc-simd` | SIMD acceleration for NTRU Prime | no |
79//!
80//! Low-level PQC primitives are always available through their modules
81//! (`pqc::falcon1024`, `pqc::ed448`, etc.) — the features only gate the
82//! optional FIPS schemes that pull in extra dependencies.
83
84//! # Threat modeling for application authors
85//!
86//! For applications that wrap this SDK and serve users facing legal
87//! compulsion or physical coercion (journalists, dissidents, civil-society
88//! organizers), see the application-level guidance in
89//! <https://github.com/KidIkaros/OriginSDK/blob/HEAD/docs/duress-pattern.md>.
90//! **The SDK deliberately ships no duress or self-destruct primitive**;
91//! applications compose existing primitives (`create_blob`, `recover_seed`,
92//! `SeedHandle::with_tier`, `MemoryTier::Standard`/`Sovereign`) following
93//! the patterns documented there.
94//!
95//! See also `SECURITY.md` for the implementation source table.
96
97#![doc(html_root_url = "https://docs.rs/origin-crypto-sdk/0.4.0")]
98#![allow(clippy::result_large_err)]
99
100// ──────────────────────────────────────────────────────────────────────
101// Public modules (matching the README map exactly)
102// ──────────────────────────────────────────────────────────────────────
103
104pub mod error;
105/// Error types and `Result` alias.
106pub mod types;
107pub use error::{CryptoError, Result};
108
109/// Test utilities for downstream users.
110///
111/// **Feature-gated** — enable with `features = ["test-utils"]`.
112///
113/// Provides deterministic seeds, keys, nonces, and byte generators so
114/// users can write tests against the SDK without hitting real randomness.
115///
116/// # Example
117///
118/// ```ignore
119/// use origin_crypto_sdk::test_utils::deterministic_seed;
120/// let seed = deterministic_seed(42);
121/// ```
122#[cfg(feature = "test-utils")]
123pub mod test_utils;
124
125/// Prelude — re-exports common types for `use origin_crypto_sdk::prelude::*`.
126pub mod prelude;
127
128// ── Symmetric encryption ──
129pub mod aead;
130pub mod chacha20_blake3;
131
132// ── Hashing & KDF ──
133pub mod kdf;
134
135// ── Signatures ──
136pub mod ec_schnorr;
137pub mod pqc;
138/// Signing primitives: classical, post-quantum, and hybrid.
139///
140/// **Recommended**: use [`signing::hybrid::HybridSigningKeyBundle`] for
141/// new applications. It pairs Ed25519 with a post-quantum primitive so
142/// the signature remains unforgeable even if one primitive is broken.
143pub mod signing;
144
145// ── Randomness & DRBG ──
146pub mod drbg;
147
148// ── OCRA (RFC 6287: OATH Challenge-Response Algorithms) ──
149/// RFC 6287 OCRA core — challenge → response.
150///
151/// Implements RFC 6287 §7.1 with explicitly-named fields. No
152/// Suite-string parsing (deferred to 0.6.2). Uses
153/// [`crate::drbg::otp::HashAlgorithm`] for the underlying HMAC family.
154pub mod ocra;
155
156// ── Recovery phrases ──
157///
158/// BIP-39-shaped Unicode recovery phrases. NOT BIP-39-compatible —
159/// uses a curated Unicode codepoint wordlist and SHA-3-256 (not
160/// SHA-256). For interop with other wallets, use the `bip39` crate.
161pub mod recovery;
162
163// ── Utility modules ──
164/// Entropy analysis — Shannon/min/collision entropy, chi-squared, serial
165/// correlation, and bit-bias metrics for auditing randomness quality.
166///
167/// Public so downstream tools (and the origin-web browser teaser) can use the
168/// SDK's real analysis rather than reimplementing it.
169pub mod entropy;
170
171/// Seed handling with TTL and memory security.
172pub mod seed;
173
174/// Merkle Mountain Range — internal utility.
175#[doc(hidden)]
176pub(crate) mod mmr;
177
178/// Stealth address primitives (KDF + PoW).
179pub mod stealth;
180
181/// Error correction codes — internal utility.
182#[doc(hidden)]
183pub mod error_correction;
184
185/// Low-level cryptographic primitives (hashes, ciphers, etc.).
186///
187/// **Internal use only.** Application code should use the higher-level
188/// modules ([`chacha20_blake3`], [`kdf`], etc.) instead.
189#[doc(hidden)]
190pub(crate) mod primitives;
191
192/// SipHash keyed hash — internal utility.
193#[doc(hidden)]
194pub mod siphash;
195
196/// Compression utility — internal.
197#[doc(hidden)]
198pub mod compression;
199
200/// Blob storage — internal utility.
201#[doc(hidden)]
202pub mod blob;
203
204// ──────────────────────────────────────────────────────────────────────
205// Internal modules (not part of public API; use at your own risk)
206// ──────────────────────────────────────────────────────────────────────
207#[doc(hidden)]
208pub mod internal;
209
210// ── Hash function re-exports (primitives is pub(crate); these are the public surface) ──
211/// BLAKE3 hash module — fast, parallelizable.
212pub use primitives::blake3;
213/// ChaCha40 — post-quantum hardened stream cipher (512-bit key, 40 rounds, dual-state).
214///
215/// Provides 256-bit post-quantum security against Grover's algorithm.
216/// See [`primitives::chacha40`] for full documentation.
217pub use primitives::chacha40;
218/// SHA3-256 hash.
219pub use primitives::sha3_256;
220/// SHA3-512 hash.
221pub use primitives::sha3_512;
222
223// ── Additional cipher module re-exports ──
224/// ChaCha20 stream cipher module.
225pub use primitives::chacha20;
226/// Poly1305 message authentication code.
227#[doc(hidden)]
228pub use primitives::poly1305;
229/// Memory tier configuration for Argon2id KDF.
230#[doc(hidden)]
231pub use primitives::tier;
232
233// ──────────────────────────────────────────────────────────────────────
234// Re-exports at crate root for convenience
235// ──────────────────────────────────────────────────────────────────────
236
237// Errors
238pub use error::{CryptoError as Error, Result as StdResult};
239
240// Encryption
241pub use aead::XChaCha20Poly1305;
242
243// KDF
244pub use kdf::hkdf::{derive_subkeys, hkdf_sha3_256};
245pub use kdf::Argon2id;
246
247// MAC
248pub use kdf::mac::hmac_sha3_256;
249
250// DRBG
251pub use drbg::ChaCha20Drbg;
252
253// Seed handling
254pub use seed::{derive_child_seed, derive_signing_keys, derive_verifying_keys, SeedHandle};
255
256// Hybrid bundle (canonical PQ-hybrid signing)
257pub use signing::hybrid::falcon1024_keygen;
258pub use signing::hybrid::HybridSigningKeyBundle;
259pub use signing::hybrid::{skein1024, skein512};
260
261// PQC direct access — use `pqc::{falcon1024, falcon512, ed448, ...}::module` for raw types.
262// Re-exports at crate root are intentionally omitted to keep the surface clean.
263// Advanced users access via `origin_crypto_sdk::pqc::falcon1024::FalconPrivateKey` etc.
264
265// Ed448 — RFC 8032 signature scheme on the 448-bit Goldilocks curve.
266// Exposed at `origin_crypto_sdk::pqc::ed448`.
267// Ed41417 — Daniel J. Bernstein's signature scheme on curve41417.
268// pub use pqc::curve41417::ed41417 as ed41417_raw; // intentionally not re-exported
269
270// SLH-DSA direct access (feature-gated)
271#[cfg(feature = "slh-dsa")]
272pub use pqc::slhdsa::{
273 sign as slhdsa_sign, verify as slhdsa_verify, SlhDsaPrivateKey, SlhDsaPublicKey,
274};
275
276// ML-DSA direct access (feature-gated)
277#[cfg(feature = "ml-dsa")]
278pub use pqc::mldsa::{MldsaPrivateKey, MldsaPublicKey};