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//! - [`kdf`] — Key derivation (Argon2id, HKDF-SHA3-256)
52//! - [`pqc`] — Raw post-quantum primitives (advanced use)
53//! - [`ec_schnorr`] — secp256k1 Schnorr signatures (native, no external deps)
54//! - [`prelude`] — One-line import for common types
55//!
56//! # Security defaults
57//!
58//! - For **signatures**: use the hybrid (Ed25519 + PQ) — see [`signing::hybrid`].
59//! Both must verify, so a vulnerability in one primitive does not break security.
60//! - For **encryption**: prefer [`chacha20_blake3`] (committing AEAD).
61//! Avoid XChaCha20-Poly1305 unless you specifically need the smaller tag
62//! and accept the partitioning-oracle risk.
63//! - For **key derivation**: use Argon2id (memory-hard) for password-based KDF,
64//! HKDF-SHA3-256 for HKDF-style derivation.
65//!
66//! # Cargo features
67//!
68//! | Feature | Enables | Default |
69//! |-----------|----------------------------------------|---------|
70//! | `parallel` | Rayon-based parallel processing | **yes** |
71//! | `slh-dsa` | SLH-DSA / SPHINCS+ signatures (FIPS 205) | no |
72//! | `ml-dsa` | ML-DSA / Dilithium signatures (FIPS 204) | no |
73//! | `pqc-simd` | SIMD acceleration for NTRU Prime | no |
74//!
75//! Low-level PQC primitives are always available through their modules
76//! (`pqc::falcon1024`, `pqc::ed448`, etc.) — the features only gate the
77//! optional FIPS schemes that pull in extra dependencies.
78
79//! # Threat modeling for application authors
80//!
81//! For applications that wrap this SDK and serve users facing legal
82//! compulsion or physical coercion (journalists, dissidents, civil-society
83//! organizers), see the application-level guidance in
84//! <https://github.com/KidIkaros/OriginSDK/blob/HEAD/docs/duress-pattern.md>.
85//! **The SDK deliberately ships no duress or self-destruct primitive**;
86//! applications compose existing primitives (`create_blob`, `recover_seed`,
87//! `SeedHandle::with_tier`, `MemoryTier::Standard`/`Sovereign`) following
88//! the patterns documented there.
89//!
90//! See also `SECURITY.md` for the implementation source table.
91
92#![doc(html_root_url = "https://docs.rs/origin-crypto-sdk/0.4.0")]
93#![allow(clippy::result_large_err)]
94
95// ──────────────────────────────────────────────────────────────────────
96// Public modules (matching the README map exactly)
97// ──────────────────────────────────────────────────────────────────────
98
99pub mod error;
100/// Error types and `Result` alias.
101pub mod types;
102pub use error::{CryptoError, Result};
103
104/// Test utilities for downstream users.
105///
106/// **Feature-gated** — enable with `features = ["test-utils"]`.
107///
108/// Provides deterministic seeds, keys, nonces, and byte generators so
109/// users can write tests against the SDK without hitting real randomness.
110///
111/// # Example
112///
113/// ```ignore
114/// use origin_crypto_sdk::test_utils::deterministic_seed;
115/// let seed = deterministic_seed(42);
116/// ```
117#[cfg(feature = "test-utils")]
118pub mod test_utils;
119
120/// Prelude — re-exports common types for `use origin_crypto_sdk::prelude::*`.
121pub mod prelude;
122
123// ── Symmetric encryption ──
124pub mod aead;
125pub mod chacha20_blake3;
126
127// ── Hashing & KDF ──
128pub mod kdf;
129
130// ── Signatures ──
131pub mod ec_schnorr;
132pub mod pqc;
133/// Signing primitives: classical, post-quantum, and hybrid.
134///
135/// **Recommended**: use [`signing::hybrid::HybridSigningKeyBundle`] for
136/// new applications. It pairs Ed25519 with a post-quantum primitive so
137/// the signature remains unforgeable even if one primitive is broken.
138pub mod signing;
139
140// ── Randomness & DRBG ──
141pub mod drbg;
142
143// ── OCRA (RFC 6287: OATH Challenge-Response Algorithms) ──
144/// RFC 6287 OCRA core — challenge → response.
145///
146/// Implements RFC 6287 §7.1 with explicitly-named fields. No
147/// Suite-string parsing (deferred to 0.6.2). Uses
148/// [`crate::drbg::otp::HashAlgorithm`] for the underlying HMAC family.
149pub mod ocra;
150
151// ── Recovery phrases ──
152///
153/// BIP-39-shaped Unicode recovery phrases. NOT BIP-39-compatible —
154/// uses a curated Unicode codepoint wordlist and SHA-3-256 (not
155/// SHA-256). For interop with other wallets, use the `bip39` crate.
156pub mod recovery;
157
158// ── Utility modules (hidden from docs) ──
159/// Entropy analysis — internal utility.
160#[doc(hidden)]
161pub(crate) mod entropy;
162
163/// Seed handling with TTL and memory security.
164pub mod seed;
165
166/// Merkle Mountain Range — internal utility.
167#[doc(hidden)]
168pub(crate) mod mmr;
169
170/// Stealth address primitives (KDF + PoW).
171pub mod stealth;
172
173/// Error correction codes — internal utility.
174#[doc(hidden)]
175pub mod error_correction;
176
177/// Low-level cryptographic primitives (hashes, ciphers, etc.).
178///
179/// **Internal use only.** Application code should use the higher-level
180/// modules ([`chacha20_blake3`], [`kdf`], etc.) instead.
181#[doc(hidden)]
182pub(crate) mod primitives;
183
184/// SipHash keyed hash — internal utility.
185#[doc(hidden)]
186pub(crate) mod siphash;
187
188/// Compression utility — internal.
189#[doc(hidden)]
190pub(crate) mod compression;
191
192/// Blob storage — internal utility.
193#[doc(hidden)]
194pub(crate) mod blob;
195
196// ──────────────────────────────────────────────────────────────────────
197// Internal modules (not part of public API; use at your own risk)
198// ──────────────────────────────────────────────────────────────────────
199#[doc(hidden)]
200pub mod internal;
201
202// ── Hash function re-exports (primitives is pub(crate); these are the public surface) ──
203/// SHA3-256 hash.
204pub use primitives::sha3_256;
205/// SHA3-512 hash.
206pub use primitives::sha3_512;
207/// BLAKE3 hash module — fast, parallelizable.
208pub use primitives::blake3;
209
210// ──────────────────────────────────────────────────────────────────────
211// Re-exports at crate root for convenience
212// ──────────────────────────────────────────────────────────────────────
213
214// Errors
215pub use error::{CryptoError as Error, Result as StdResult};
216
217// Encryption
218pub use aead::XChaCha20Poly1305;
219
220// KDF
221pub use kdf::hkdf::{derive_subkeys, hkdf_sha3_256};
222pub use kdf::Argon2id;
223
224// MAC
225pub use kdf::mac::hmac_sha3_256;
226
227// DRBG
228pub use drbg::ChaCha20Drbg;
229
230// Seed handling
231pub use seed::{derive_child_seed, derive_signing_keys, derive_verifying_keys, SeedHandle};
232
233// Hybrid bundle (canonical PQ-hybrid signing)
234pub use signing::hybrid::falcon1024_keygen;
235pub use signing::hybrid::HybridSigningKeyBundle;
236pub use signing::hybrid::{skein1024, skein512};
237
238// PQC direct access — use `pqc::{falcon1024, falcon512, ed448, ...}::module` for raw types.
239// Re-exports at crate root are intentionally omitted to keep the surface clean.
240// Advanced users access via `origin_crypto_sdk::pqc::falcon1024::FalconPrivateKey` etc.
241
242// Ed448 — RFC 8032 signature scheme on the 448-bit Goldilocks curve.
243// Exposed at `origin_crypto_sdk::pqc::ed448`.
244// Ed41417 — Daniel J. Bernstein's signature scheme on curve41417.
245// pub use pqc::curve41417::ed41417 as ed41417_raw; // intentionally not re-exported
246
247// SLH-DSA direct access (feature-gated)
248#[cfg(feature = "slh-dsa")]
249pub use pqc::slhdsa::{
250 sign as slhdsa_sign, verify as slhdsa_verify, SlhDsaPrivateKey, SlhDsaPublicKey,
251};
252
253// ML-DSA direct access (feature-gated)
254#[cfg(feature = "ml-dsa")]
255pub use pqc::mldsa::{MldsaPrivateKey, MldsaPublicKey};