pulseengine_mcp_auth/crypto/
mod.rs

1//! Cryptographic utilities for secure authentication
2//!
3//! This module provides encryption, hashing, and key derivation functions
4//! for secure API key management, inspired by Loxone MCP's security model.
5
6pub mod encryption;
7pub mod hashing;
8pub mod keys;
9
10pub use encryption::{EncryptionError, decrypt_data, encrypt_data};
11pub use hashing::{HashingError, generate_salt, hash_api_key, verify_api_key};
12pub use keys::{KeyDerivationError, derive_key, generate_secure_key};
13
14pub use encryption::EncryptedData;
15/// Re-export common types
16pub use hashing::Salt;
17
18/// Initialize the crypto module (perform any necessary setup)
19pub fn init() -> Result<(), CryptoError> {
20    // Ensure we have good randomness available
21    use rand::RngCore;
22    let mut rng = rand::thread_rng();
23    let mut test_bytes = [0u8; 32];
24    rng.fill_bytes(&mut test_bytes);
25
26    // Verify we got non-zero random bytes
27    if test_bytes.iter().all(|&b| b == 0) {
28        return Err(CryptoError::RandomnessError(
29            "Failed to generate random bytes".into(),
30        ));
31    }
32
33    Ok(())
34}
35
36/// General crypto error type
37#[derive(Debug, thiserror::Error)]
38pub enum CryptoError {
39    #[error("Encryption error: {0}")]
40    Encryption(#[from] EncryptionError),
41
42    #[error("Hashing error: {0}")]
43    Hashing(#[from] HashingError),
44
45    #[error("Key derivation error: {0}")]
46    KeyDerivation(#[from] KeyDerivationError),
47
48    #[error("Randomness error: {0}")]
49    RandomnessError(String),
50}
51
52#[cfg(test)]
53mod tests {
54    use super::*;
55
56    #[test]
57    fn test_crypto_init() {
58        assert!(init().is_ok());
59    }
60}