Skip to main content

metamorphic_crypto/
lib.rs

1//! # metamorphic-crypto
2//!
3//! Zero-knowledge end-to-end encryption core for the Metamorphic platform.
4//!
5//! This library implements the cryptographic operations required by all Metamorphic
6//! clients (web/WASM, iOS/UniFFI, Android/UniFFI). It produces byte-compatible
7//! ciphertext with the existing JavaScript implementation so that data encrypted
8//! by one client can be decrypted by any other.
9//!
10//! ## Security guarantees
11//!
12//! - All secret key material is [`Zeroize`]-on-drop
13//! - No `unsafe` code
14//! - Constant-time comparisons via the underlying RustCrypto crates
15//! - Randomness sourced directly from the OS CSPRNG ([`getrandom`])
16//!
17//! ## Ciphertext formats
18//!
19//! | Format | Layout |
20//! |--------|--------|
21//! | Secretbox | `nonce (24B) \|\| ciphertext (len + 16B MAC)` |
22//! | box_seal | `ephemeral_pk (32B) \|\| box ciphertext` |
23//! | Hybrid v2 (Cat-3) | `0x02 \|\| ML-KEM-768 ct (1088B) \|\| X25519 eph pk (32B) \|\| nonce (24B) \|\| secretbox ct` |
24//! | Hybrid v3 (Cat-5) | `0x03 \|\| ML-KEM-1024 ct (1568B) \|\| X25519 eph pk (32B) \|\| nonce (24B) \|\| secretbox ct` |
25
26#![forbid(unsafe_code)]
27#![deny(missing_docs)]
28
29pub mod b64;
30pub mod box_seal;
31pub mod error;
32pub mod hybrid;
33pub mod kdf;
34pub mod keys;
35pub mod recovery;
36pub mod seal;
37pub mod secretbox;
38
39#[cfg(target_arch = "wasm32")]
40pub mod wasm;
41
42pub use error::CryptoError;
43
44// Re-export the primary public API
45pub use b64::parse_salt_from_key_hash;
46pub use box_seal::{box_seal, box_seal_open};
47pub use hybrid::{
48    HybridKeyPair, SecurityLevel, generate_hybrid_keypair, generate_hybrid_keypair_1024,
49    generate_hybrid_keypair_with_level, hybrid_open, hybrid_seal, hybrid_seal_1024,
50    hybrid_seal_with_level, is_hybrid_ciphertext,
51};
52pub use kdf::derive_session_key;
53pub use keys::{
54    KeyPair, decrypt_private_key, encrypt_private_key, generate_key, generate_keypair,
55    generate_salt,
56};
57pub use recovery::{
58    RecoveryKey, decrypt_private_key_with_recovery, encrypt_private_key_for_recovery,
59    generate_recovery_key, recovery_key_to_secret,
60};
61pub use seal::{seal_for_user, seal_for_user_with_level, unseal_from_user};
62pub use secretbox::{
63    decrypt_secretbox, decrypt_secretbox_to_string, encrypt_secretbox, encrypt_secretbox_string,
64};