Skip to main content

opentdf_crypto/
lib.rs

1//! OpenTDF Cryptographic Operations
2//!
3//! This crate provides cryptographic operations for OpenTDF with security hardening:
4//! - Zeroizing key types that automatically clear memory
5//! - Constant-time HMAC verification
6//! - KEM abstractions for RSA, EC, and future post-quantum algorithms
7//! - Segment-based encryption for standard TDF
8//! - NanoTDF API structure (implementation pending)
9//!
10//! # Security Features
11//!
12//! - **Zeroization**: All key material uses `zeroize` to clear memory on drop
13//! - **Constant-time comparison**: MAC verification uses `subtle::ConstantTimeEq`
14//! - **Future-ready**: Abstractions for post-quantum cryptography (ML-KEM)
15//!
16//! # Example
17//!
18//! ```
19//! use opentdf_crypto::tdf::TdfEncryption;
20//!
21//! # fn example() -> Result<(), Box<dyn std::error::Error>> {
22//! // Create encryption instance with generated keys
23//! let tdf = TdfEncryption::new()?;
24//!
25//! // Encrypt data
26//! let data = b"sensitive information";
27//! let encrypted = tdf.encrypt(data)?;
28//!
29//! // Decrypt (legacy format)
30//! let decrypted = TdfEncryption::decrypt_legacy(tdf.policy_key(), &encrypted)?;
31//! assert_eq!(data, decrypted.as_slice());
32//! # Ok(())
33//! # }
34//! ```
35
36pub mod helpers;
37pub mod hmac;
38pub mod kem;
39pub mod tdf;
40pub mod types;
41
42// Re-export commonly used types
43pub use helpers::{CryptoError, create_aes_cipher, generate_key_32, generate_nonce};
44pub use hmac::{
45    HmacError, calculate_hmac, calculate_policy_binding, calculate_root_signature,
46    verify_root_signature, verify_segment_hash,
47};
48pub use kem::{KemError, KeyEncapsulation};
49pub use tdf::{EncryptedPayload, EncryptedSegment, SegmentInfo, SegmentedPayload, TdfEncryption};
50pub use types::{AesKey, KeyError, Nonce96, PayloadKey, PolicyKey};
51
52// NanoTDF exports (requires EC KEM)
53#[cfg(feature = "kem-ec")]
54pub use tdf::{
55    NanoTdf, NanoTdfBuilder, NanoTdfCryptoError, NanoTdfError, NanoTdfIv, NanoTdfPayload,
56    NanoTdfSignature, TagSize, decrypt, encrypt, generate_gmac, verify_gmac,
57};
58
59// RSA KEM exports
60#[cfg(feature = "kem-rsa")]
61pub use kem::rsa::{OaepHash, RsaOaepKem, wrap_key_with_rsa_oaep};
62
63// EC KEM exports
64#[cfg(feature = "kem-ec")]
65pub use kem::ec::{EcCurve, EcWrappedKeyResult, EcdhKem, unwrap_key_with_ec, wrap_key_with_ec};
66
67// PQC (Post-Quantum Cryptography) exports - always available
68pub use kem::pqc::{HybridKem, MlKemLevel, MlKemWrapper};
69
70#[cfg(feature = "kem-ec")]
71pub use types::EcPrivateKey;
72
73// Re-export underlying crypto libraries when KEM features are enabled
74#[cfg(feature = "kem-ec")]
75pub use hkdf;
76
77#[cfg(feature = "kem-ec")]
78pub use p256;
79
80#[cfg(any(feature = "kem-rsa", feature = "kem-ec"))]
81pub use pkcs8;
82
83#[cfg(any(feature = "kem-rsa", feature = "kem-ec"))]
84pub use sha1;
85
86#[cfg(feature = "kem-ec")]
87pub use sha2;
88
89// Re-export rsa crate only for rustcrypto-provider feature (rsa 0.10, pure Rust)
90#[cfg(feature = "rustcrypto-provider")]
91pub use rsa;
92
93// Re-export encryption error
94pub use tdf::encryption::EncryptionError;