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