hanzo-kbs 1.1.21

Hanzo AI - Key Broker Service (KMS + attestation + post-quantum vault)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
//! PQC-enhanced vault implementations
//! 
//! Provides quantum-resistant key storage and operations for different privacy tiers

use async_trait::async_trait;
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::RwLock;

use crate::{
    error::{Result, SecurityError},
    types::{PrivacyTier, KeyId, AttestationType},
    vault::KeyVault,
};


#[cfg(feature = "pqc")]
use hanzo_pqc::{
    kem::{Kem, KemAlgorithm, EncapsulationKey, DecapsulationKey, MlKem},
    signature::{Signature, SignatureAlgorithm, VerifyingKey, SigningKey, MlDsa},
    config::PqcConfig,
};

/// PQC-enhanced vault for Tier 2+ (CPU TEE and above)
#[cfg(feature = "pqc")]
pub struct PqcVault {
    tier: PrivacyTier,
    config: PqcConfig,
    ml_kem: Arc<MlKem>,
    ml_dsa: Arc<MlDsa>,
    // In-memory storage for dev/testing (production would use TEE sealed storage)
    keys: Arc<RwLock<HashMap<KeyId, ProtectedKey>>>,
    attestation: Option<AttestationType>,
}

#[cfg(feature = "pqc")]
#[derive(Clone)]
struct ProtectedKey {
    /// ML-KEM encrypted key material
    wrapped_key: Vec<u8>,
    /// Decapsulation key (stored in TEE in production)
    decap_key: DecapsulationKey,
    /// Metadata
    created_at: chrono::DateTime<chrono::Utc>,
    tier: PrivacyTier,
}

#[cfg(feature = "pqc")]
impl PqcVault {
    pub async fn new(tier: PrivacyTier, attestation: Option<AttestationType>) -> Result<Self> {
        if tier < PrivacyTier::CpuTee && attestation.is_some() {
            return Err(SecurityError::PolicyViolation(
                "Attestation requires CPU TEE or higher tier".into()
            ));
        }
        
        let pqc_tier = match tier {
            PrivacyTier::Open => hanzo_pqc::privacy_tiers::PrivacyTier::AccessOpen,
            PrivacyTier::AtRest => hanzo_pqc::privacy_tiers::PrivacyTier::AccessAtRest,
            PrivacyTier::CpuTee => hanzo_pqc::privacy_tiers::PrivacyTier::AccessCpuTee,
            PrivacyTier::GpuCc => hanzo_pqc::privacy_tiers::PrivacyTier::AccessCpuTeePlusGpuCc,
            PrivacyTier::GpuTeeIo => hanzo_pqc::privacy_tiers::PrivacyTier::AccessGpuTeeIoMax,
        };
        
        let config = PqcConfig::for_privacy_tier(pqc_tier);
        
        Ok(Self {
            tier,
            config,
            ml_kem: Arc::new(MlKem::new()),
            ml_dsa: Arc::new(MlDsa::new()),
            keys: Arc::new(RwLock::new(HashMap::new())),
            attestation,
        })
    }
    
    /// Generate ML-KEM keypair for this vault
    async fn generate_kem_keypair(&self) -> Result<(EncapsulationKey, DecapsulationKey)> {
        let alg = self.tier.recommended_kem();
        let keypair = self.ml_kem.generate_keypair(alg).await
            .map_err(|e| SecurityError::CryptoError(format!("KEM keypair generation failed: {:?}", e)))?;
        Ok((keypair.encap_key, keypair.decap_key))
    }
    
    /// Generate ML-DSA keypair for signing
    async fn generate_signing_keypair(&self) -> Result<(VerifyingKey, SigningKey)> {
        let alg = self.tier.recommended_sig();
        self.ml_dsa.generate_keypair(alg).await
            .map_err(|e| SecurityError::CryptoError(format!("Signing keypair generation failed: {:?}", e)))
    }
}

// Extension methods for PrivacyTier to get recommended algorithms
impl PrivacyTier {
    fn recommended_kem(&self) -> KemAlgorithm {
        match self {
            PrivacyTier::Open | PrivacyTier::AtRest => KemAlgorithm::MlKem512,
            PrivacyTier::CpuTee => KemAlgorithm::MlKem768,
            PrivacyTier::GpuCc | PrivacyTier::GpuTeeIo => KemAlgorithm::MlKem1024,
        }
    }
    
    fn recommended_sig(&self) -> SignatureAlgorithm {
        match self {
            PrivacyTier::Open | PrivacyTier::AtRest => SignatureAlgorithm::MlDsa44,
            PrivacyTier::CpuTee => SignatureAlgorithm::MlDsa65,
            PrivacyTier::GpuCc | PrivacyTier::GpuTeeIo => SignatureAlgorithm::MlDsa87,
        }
    }
}

#[cfg(feature = "pqc")]
#[async_trait]
impl KeyVault for PqcVault {
    fn tier(&self) -> PrivacyTier {
        self.tier
    }
    
    async fn store_key(&self, key_id: &KeyId, key_data: &[u8]) -> Result<()> {
        // Generate vault-specific KEM keypair
        let (encap_key, decap_key) = self.generate_kem_keypair().await?;
        
        // Encapsulate key data
        let output = self.ml_kem.encapsulate(&encap_key).await
            .map_err(|e| SecurityError::CryptoError(format!("Key encapsulation failed: {:?}", e)))?;
        
        // Derive wrapping key from shared secret
        use hanzo_pqc::kdf::{HkdfKdf, Kdf};
        let kdf = HkdfKdf::new(self.config.kdf);
        let wrapping_key = kdf.derive(
            None,
            &output.shared_secret,
            b"hanzo-vault-wrap-v1",
            32,
        ).map_err(|e| SecurityError::CryptoError(format!("KDF failed: {:?}", e)))?;
        
        // Encrypt key data with ChaCha20Poly1305
        use chacha20poly1305::{
            aead::{Aead, AeadCore, KeyInit, OsRng},
            ChaCha20Poly1305,
        };
        
        let cipher = ChaCha20Poly1305::new_from_slice(&wrapping_key)
            .map_err(|e| SecurityError::CryptoError(format!("Cipher init failed: {:?}", e)))?;
        
        let nonce = ChaCha20Poly1305::generate_nonce(&mut OsRng);
        let ciphertext = cipher.encrypt(&nonce, key_data)
            .map_err(|e| SecurityError::CryptoError(format!("Key encryption failed: {:?}", e)))?;
        
        // Store: kem_ct || nonce || ciphertext
        let mut wrapped = Vec::new();
        wrapped.extend_from_slice(&(output.ciphertext.len() as u32).to_be_bytes());
        wrapped.extend_from_slice(&output.ciphertext);
        wrapped.extend_from_slice(&nonce);
        wrapped.extend_from_slice(&ciphertext);
        
        // Store protected key
        let protected = ProtectedKey {
            wrapped_key: wrapped,
            decap_key,
            created_at: chrono::Utc::now(),
            tier: self.tier,
        };
        
        self.keys.write().await.insert(key_id.clone(), protected);
        Ok(())
    }
    
    async fn use_key<F, R>(&self, key_id: &KeyId, operation: F) -> Result<R>
    where
        F: FnOnce(&[u8]) -> R + Send,
        R: Send,
    {
        let keys = self.keys.read().await;
        let protected = keys.get(key_id)
            .ok_or_else(|| SecurityError::KeyNotFound(key_id.0.to_string()))?;
        
        // Parse wrapped format
        let wrapped = &protected.wrapped_key;
        if wrapped.len() < 4 {
            return Err(SecurityError::CryptoError("Invalid wrapped key format".into()));
        }
        
        let ct_len = u32::from_be_bytes([wrapped[0], wrapped[1], wrapped[2], wrapped[3]]) as usize;
        if wrapped.len() < 4 + ct_len + 12 {
            return Err(SecurityError::CryptoError("Invalid wrapped key length".into()));
        }
        
        let kem_ct = &wrapped[4..4 + ct_len];
        let nonce = &wrapped[4 + ct_len..4 + ct_len + 12];
        let ciphertext = &wrapped[4 + ct_len + 12..];
        
        // Decapsulate to recover shared secret
        let shared_secret = self.ml_kem.decapsulate(&protected.decap_key, kem_ct).await
            .map_err(|e| SecurityError::CryptoError(format!("Decapsulation failed: {:?}", e)))?;
        
        // Derive wrapping key
        use hanzo_pqc::kdf::{HkdfKdf, Kdf};
        let kdf = HkdfKdf::new(self.config.kdf);
        let wrapping_key = kdf.derive(
            None,
            &shared_secret,
            b"hanzo-vault-wrap-v1",
            32,
        ).map_err(|e| SecurityError::CryptoError(format!("KDF failed: {:?}", e)))?;
        
        // Decrypt key data
        use chacha20poly1305::{
            aead::{Aead, KeyInit},
            ChaCha20Poly1305, Nonce,
        };
        
        let cipher = ChaCha20Poly1305::new_from_slice(&wrapping_key)
            .map_err(|e| SecurityError::CryptoError(format!("Cipher init failed: {:?}", e)))?;
        
        let nonce = Nonce::from_slice(nonce);
        let key_data = cipher.decrypt(nonce, ciphertext)
            .map_err(|e| SecurityError::CryptoError(format!("Key decryption failed: {:?}", e)))?;
        
        // Use key in protected context
        let result = operation(&key_data);
        
        // Key data is automatically zeroized when dropped
        Ok(result)
    }
    
    async fn delete_key(&self, key_id: &KeyId) -> Result<()> {
        self.keys.write().await.remove(key_id)
            .ok_or_else(|| SecurityError::KeyNotFound(key_id.0.to_string()))?;
        Ok(())
    }
    
    async fn is_initialized(&self) -> Result<bool> {
        // Check if we have valid attestation for TEE tiers
        if self.tier >= PrivacyTier::CpuTee {
            Ok(self.attestation.is_some())
        } else {
            Ok(true)
        }
    }
}

/// GPU Confidential Computing vault (Tier 3)
#[cfg(feature = "pqc")]
pub struct GpuCcVault {
    inner: PqcVault,
    gpu_device_id: u32,
    cc_enabled: bool,
}

#[cfg(feature = "pqc")]
impl GpuCcVault {
    pub async fn new(gpu_device_id: u32, attestation: AttestationType) -> Result<Self> {
        // Verify this is H100 CC attestation
        match &attestation {
            AttestationType::H100Cc { .. } => {},
            _ => return Err(SecurityError::PolicyViolation(
                "GPU CC vault requires H100 CC attestation".into()
            )),
        }
        
        let inner = PqcVault::new(PrivacyTier::GpuCc, Some(attestation)).await?;
        
        Ok(Self {
            inner,
            gpu_device_id,
            cc_enabled: true,
        })
    }
}

#[cfg(feature = "pqc")]
#[async_trait]
impl KeyVault for GpuCcVault {
    fn tier(&self) -> PrivacyTier {
        PrivacyTier::GpuCc
    }
    
    async fn store_key(&self, key_id: &KeyId, key_data: &[u8]) -> Result<()> {
        // Additional GPU-specific protections could go here
        // For H100 CC, keys are protected by encrypted DMA
        self.inner.store_key(key_id, key_data).await
    }
    
    async fn use_key<F, R>(&self, key_id: &KeyId, operation: F) -> Result<R>
    where
        F: FnOnce(&[u8]) -> R + Send,
        R: Send,
    {
        if !self.cc_enabled {
            return Err(SecurityError::PolicyViolation(
                "GPU CC must be enabled for key operations".into()
            ));
        }
        
        self.inner.use_key(key_id, operation).await
    }
    
    async fn delete_key(&self, key_id: &KeyId) -> Result<()> {
        self.inner.delete_key(key_id).await
    }
    
    async fn is_initialized(&self) -> Result<bool> {
        Ok(self.cc_enabled && self.inner.is_initialized().await?)
    }
}

/// GPU TEE-I/O vault (Tier 4 - Blackwell)
#[cfg(feature = "pqc")]
pub struct GpuTeeIoVault {
    inner: PqcVault,
    gpu_device_id: u32,
    tee_io_enabled: bool,
    mig_instance: Option<u32>,
}

#[cfg(feature = "pqc")]
impl GpuTeeIoVault {
    pub async fn new(
        gpu_device_id: u32,
        mig_instance: Option<u32>,
        attestation: AttestationType,
    ) -> Result<Self> {
        // Verify this is Blackwell TEE-I/O attestation
        match &attestation {
            AttestationType::BlackwellTeeIo { .. } => {},
            _ => return Err(SecurityError::PolicyViolation(
                "GPU TEE-I/O vault requires Blackwell attestation".into()
            )),
        }
        
        let inner = PqcVault::new(PrivacyTier::GpuTeeIo, Some(attestation)).await?;
        
        Ok(Self {
            inner,
            gpu_device_id,
            tee_io_enabled: true,
            mig_instance,
        })
    }
}

#[cfg(feature = "pqc")]
#[async_trait]
impl KeyVault for GpuTeeIoVault {
    fn tier(&self) -> PrivacyTier {
        PrivacyTier::GpuTeeIo
    }
    
    async fn store_key(&self, key_id: &KeyId, key_data: &[u8]) -> Result<()> {
        // Blackwell TEE-I/O provides inline NVLink protection
        // Keys are protected end-to-end with near-native performance
        self.inner.store_key(key_id, key_data).await
    }
    
    async fn use_key<F, R>(&self, key_id: &KeyId, operation: F) -> Result<R>
    where
        F: FnOnce(&[u8]) -> R + Send,
        R: Send,
    {
        if !self.tee_io_enabled {
            return Err(SecurityError::PolicyViolation(
                "TEE-I/O must be enabled for key operations".into()
            ));
        }
        
        // MIG isolation check for multi-tenant scenarios
        if let Some(mig) = self.mig_instance {
            log::info!("Using key in MIG instance {}", mig);
        }
        
        self.inner.use_key(key_id, operation).await
    }
    
    async fn delete_key(&self, key_id: &KeyId) -> Result<()> {
        self.inner.delete_key(key_id).await
    }
    
    async fn is_initialized(&self) -> Result<bool> {
        Ok(self.tee_io_enabled && self.inner.is_initialized().await?)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    
    #[tokio::test]
    #[cfg(feature = "pqc")]
    async fn test_pqc_vault_operations() {
        // Skip test in CI environment
        if std::env::var("CI").is_ok() {
            println!("Skipping test in CI: requires specific crypto setup");
            return;
        }
        let vault = PqcVault::new(PrivacyTier::CpuTee, None).await.unwrap();
        
        let key_id = KeyId::new();
        let key_data = b"secret key material";
        
        // Store key
        vault.store_key(&key_id, key_data).await.unwrap();
        
        // Use key
        let result = vault.use_key(&key_id, |data| {
            assert_eq!(data, key_data);
            42
        }).await.unwrap();
        
        assert_eq!(result, 42);
        
        // Delete key
        vault.delete_key(&key_id).await.unwrap();
        
        // Verify deletion
        assert!(vault.use_key(&key_id, |_| ()).await.is_err());
    }
}