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 hash;
33pub mod hybrid;
34pub mod kdf;
35pub mod keys;
36pub mod recovery;
37pub mod seal;
38pub mod secretbox;
39
40#[cfg(target_arch = "wasm32")]
41pub mod wasm;
42
43pub use error::CryptoError;
44
45// Re-export the primary public API
46pub use b64::parse_salt_from_key_hash;
47pub use box_seal::{box_seal, box_seal_open};
48pub use hash::{sha3_256, sha3_512, sha3_512_with_context, sha256, sha512};
49pub use hybrid::{
50    HybridKeyPair, SecurityLevel, generate_hybrid_keypair, generate_hybrid_keypair_1024,
51    generate_hybrid_keypair_with_level, hybrid_open, hybrid_seal, hybrid_seal_1024,
52    hybrid_seal_with_level, is_hybrid_ciphertext,
53};
54pub use kdf::derive_session_key;
55pub use keys::{
56    KeyPair, decrypt_private_key, encrypt_private_key, generate_key, generate_keypair,
57    generate_salt,
58};
59pub use recovery::{
60    RecoveryKey, decrypt_private_key_with_recovery, encrypt_private_key_for_recovery,
61    generate_recovery_key, recovery_key_to_secret,
62};
63pub use seal::{seal_for_user, seal_for_user_with_level, unseal_from_user};
64pub use secretbox::{
65    decrypt_secretbox, decrypt_secretbox_to_string, encrypt_secretbox, encrypt_secretbox_string,
66};