Skip to main content

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 (hidden from docs) ──
164/// Entropy analysis — internal utility.
165#[doc(hidden)]
166pub(crate) mod entropy;
167
168/// Seed handling with TTL and memory security.
169pub mod seed;
170
171/// Merkle Mountain Range — internal utility.
172#[doc(hidden)]
173pub(crate) mod mmr;
174
175/// Stealth address primitives (KDF + PoW).
176pub mod stealth;
177
178/// Error correction codes — internal utility.
179#[doc(hidden)]
180pub mod error_correction;
181
182/// Low-level cryptographic primitives (hashes, ciphers, etc.).
183///
184/// **Internal use only.** Application code should use the higher-level
185/// modules ([`chacha20_blake3`], [`kdf`], etc.) instead.
186#[doc(hidden)]
187pub(crate) mod primitives;
188
189/// SipHash keyed hash — internal utility.
190#[doc(hidden)]
191pub(crate) mod siphash;
192
193/// Compression utility — internal.
194#[doc(hidden)]
195pub(crate) mod compression;
196
197/// Blob storage — internal utility.
198#[doc(hidden)]
199pub(crate) mod blob;
200
201// ──────────────────────────────────────────────────────────────────────
202// Internal modules (not part of public API; use at your own risk)
203// ──────────────────────────────────────────────────────────────────────
204#[doc(hidden)]
205pub mod internal;
206
207// ── Hash function re-exports (primitives is pub(crate); these are the public surface) ──
208/// SHA3-256 hash.
209pub use primitives::sha3_256;
210/// SHA3-512 hash.
211pub use primitives::sha3_512;
212/// BLAKE3 hash module — fast, parallelizable.
213pub use primitives::blake3;
214/// ChaCha40 — post-quantum hardened stream cipher (512-bit key, 40 rounds, dual-state).
215///
216/// Provides 256-bit post-quantum security against Grover's algorithm.
217/// See [`primitives::chacha40`] for full documentation.
218pub use primitives::chacha40;
219
220// ── Additional cipher module re-exports ──
221/// ChaCha20 stream cipher module.
222pub use primitives::chacha20;
223
224// ──────────────────────────────────────────────────────────────────────
225// Re-exports at crate root for convenience
226// ──────────────────────────────────────────────────────────────────────
227
228// Errors
229pub use error::{CryptoError as Error, Result as StdResult};
230
231// Encryption
232pub use aead::XChaCha20Poly1305;
233
234// KDF
235pub use kdf::hkdf::{derive_subkeys, hkdf_sha3_256};
236pub use kdf::Argon2id;
237
238// MAC
239pub use kdf::mac::hmac_sha3_256;
240
241// DRBG
242pub use drbg::ChaCha20Drbg;
243
244// Seed handling
245pub use seed::{derive_child_seed, derive_signing_keys, derive_verifying_keys, SeedHandle};
246
247// Hybrid bundle (canonical PQ-hybrid signing)
248pub use signing::hybrid::falcon1024_keygen;
249pub use signing::hybrid::HybridSigningKeyBundle;
250pub use signing::hybrid::{skein1024, skein512};
251
252// PQC direct access — use `pqc::{falcon1024, falcon512, ed448, ...}::module` for raw types.
253// Re-exports at crate root are intentionally omitted to keep the surface clean.
254// Advanced users access via `origin_crypto_sdk::pqc::falcon1024::FalconPrivateKey` etc.
255
256// Ed448 — RFC 8032 signature scheme on the 448-bit Goldilocks curve.
257// Exposed at `origin_crypto_sdk::pqc::ed448`.
258// Ed41417 — Daniel J. Bernstein's signature scheme on curve41417.
259// pub use pqc::curve41417::ed41417 as ed41417_raw;  // intentionally not re-exported
260
261// SLH-DSA direct access (feature-gated)
262#[cfg(feature = "slh-dsa")]
263pub use pqc::slhdsa::{
264    sign as slhdsa_sign, verify as slhdsa_verify, SlhDsaPrivateKey, SlhDsaPublicKey,
265};
266
267// ML-DSA direct access (feature-gated)
268#[cfg(feature = "ml-dsa")]
269pub use pqc::mldsa::{MldsaPrivateKey, MldsaPublicKey};