quantum_shield/
types.rs

1//! Core types for hybrid cryptography
2
3use serde::{Deserialize, Serialize};
4
5/// Cryptography version for algorithm agility
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
7pub struct CryptoVersion(pub u32);
8
9impl CryptoVersion {
10    /// Current version
11    pub const CURRENT: CryptoVersion = CryptoVersion(1);
12    
13    /// Check if this version is supported
14    pub fn is_supported(&self) -> bool {
15        self.0 <= Self::CURRENT.0
16    }
17}
18
19impl Default for CryptoVersion {
20    fn default() -> Self {
21        Self::CURRENT
22    }
23}
24
25/// Hybrid encrypted data (RSA + Kyber + AES)
26#[derive(Debug, Clone, Serialize, Deserialize)]
27pub struct HybridCiphertext {
28    /// Crypto version for forward compatibility
29    pub version: CryptoVersion,
30    
31    /// AES-256-GCM encrypted data (base64)
32    pub ciphertext: String,
33    
34    /// Symmetric key encrypted with RSA-4096 (base64)
35    pub encrypted_key_rsa: String,
36    
37    /// Symmetric key encrypted with Kyber-1024 (base64)
38    pub encrypted_key_kyber: String,
39    
40    /// Algorithm description for informational purposes
41    pub algorithm: String,
42}
43
44impl HybridCiphertext {
45    /// Create a new hybrid ciphertext
46    pub fn new(
47        ciphertext: String,
48        encrypted_key_rsa: String,
49        encrypted_key_kyber: String,
50    ) -> Self {
51        Self {
52            version: CryptoVersion::CURRENT,
53            ciphertext,
54            encrypted_key_rsa,
55            encrypted_key_kyber,
56            algorithm: "AES-256-GCM + RSA-4096-OAEP + Kyber-1024".to_string(),
57        }
58    }
59    
60    /// Serialize to JSON
61    pub fn to_json(&self) -> Result<String, serde_json::Error> {
62        serde_json::to_string(self)
63    }
64    
65    /// Deserialize from JSON
66    pub fn from_json(json: &str) -> Result<Self, serde_json::Error> {
67        serde_json::from_str(json)
68    }
69}
70
71/// Hybrid digital signature (RSA + Dilithium)
72#[derive(Debug, Clone, Serialize, Deserialize)]
73pub struct HybridSignature {
74    /// Crypto version
75    pub version: CryptoVersion,
76    
77    /// RSA-4096-PSS signature (base64)
78    pub rsa_signature: String,
79    
80    /// Dilithium5 signature (base64, optional for backward compatibility)
81    pub dilithium_signature: Option<String>,
82}
83
84impl HybridSignature {
85    /// Create a new hybrid signature
86    pub fn new(rsa_signature: String, dilithium_signature: Option<String>) -> Self {
87        Self {
88            version: CryptoVersion::CURRENT,
89            rsa_signature,
90            dilithium_signature,
91        }
92    }
93    
94    /// Check if this signature includes post-quantum component
95    pub fn is_quantum_resistant(&self) -> bool {
96        self.dilithium_signature.is_some()
97    }
98    
99    /// Serialize to JSON
100    pub fn to_json(&self) -> Result<String, serde_json::Error> {
101        serde_json::to_string(self)
102    }
103    
104    /// Deserialize from JSON
105    pub fn from_json(json: &str) -> Result<Self, serde_json::Error> {
106        serde_json::from_str(json)
107    }
108}
109
110#[cfg(test)]
111mod tests {
112    use super::*;
113
114    #[test]
115    fn test_version() {
116        let v = CryptoVersion::CURRENT;
117        assert!(v.is_supported());
118        
119        let future = CryptoVersion(999);
120        assert!(!future.is_supported());
121    }
122
123    #[test]
124    fn test_ciphertext_serialization() {
125        let ct = HybridCiphertext::new(
126            "ciphertext".to_string(),
127            "rsa_key".to_string(),
128            "kyber_key".to_string(),
129        );
130        
131        let json = ct.to_json().unwrap();
132        let ct2 = HybridCiphertext::from_json(&json).unwrap();
133        
134        assert_eq!(ct.ciphertext, ct2.ciphertext);
135    }
136
137    #[test]
138    fn test_signature_quantum_resistant() {
139        let sig1 = HybridSignature::new("rsa".to_string(), None);
140        assert!(!sig1.is_quantum_resistant());
141        
142        let sig2 = HybridSignature::new("rsa".to_string(), Some("dilithium".to_string()));
143        assert!(sig2.is_quantum_resistant());
144    }
145}
146