1use crate::primitives::{Address, Timestamp};
7use serde::{Deserialize, Serialize};
8
9#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
11#[serde(rename_all = "PascalCase")]
12pub enum TeeVendor {
13 IntelSGX,
15 IntelTdx,
17 AMDSEV,
19 AmdSevSnp,
21 ARMTrustZone,
23 AWSNitro,
25 AwsNitro,
27 NvidiaGpu,
29 #[default]
31 Generic,
32}
33
34impl TeeVendor {
35 pub fn as_str(&self) -> &str {
37 match self {
38 Self::IntelSGX => "Intel SGX",
39 Self::IntelTdx => "Intel TDX",
40 Self::AMDSEV => "AMD SEV",
41 Self::AmdSevSnp => "AMD SEV-SNP",
42 Self::ARMTrustZone => "ARM TrustZone",
43 Self::AWSNitro => "AWS Nitro",
44 Self::AwsNitro => "AWS Nitro",
45 Self::NvidiaGpu => "NVIDIA GPU CC",
46 Self::Generic => "Generic",
47 }
48 }
49}
50
51#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
56#[serde(default)]
57pub struct AttestationReport {
58 pub id: uuid::Uuid,
60 pub vendor: TeeVendor,
62 pub user_data: Vec<u8>,
64 pub attestation_data: Vec<u8>,
66 pub certificates: Vec<Vec<u8>>,
68 pub timestamp: Timestamp,
70 pub metadata: std::collections::HashMap<String, String>,
72 pub quote: Vec<u8>,
74 pub measurement: Vec<u8>,
76 pub signature: Vec<u8>,
78 pub vendor_data: Vec<u8>,
80}
81
82impl Default for AttestationReport {
83 fn default() -> Self {
84 Self {
85 id: uuid::Uuid::nil(),
86 vendor: TeeVendor::default(),
87 user_data: Vec::new(),
88 attestation_data: Vec::new(),
89 certificates: Vec::new(),
90 timestamp: Timestamp::default(),
91 metadata: std::collections::HashMap::new(),
92 quote: Vec::new(),
93 measurement: Vec::new(),
94 signature: Vec::new(),
95 vendor_data: Vec::new(),
96 }
97 }
98}
99
100impl AttestationReport {
101 pub fn new(
103 vendor: TeeVendor,
104 attestation_data: Vec<u8>,
105 quote: Vec<u8>,
106 measurement: Vec<u8>,
107 ) -> Self {
108 Self {
109 id: uuid::Uuid::new_v4(),
110 vendor,
111 user_data: Vec::new(),
112 attestation_data,
113 certificates: Vec::new(),
114 timestamp: Timestamp::now(),
115 metadata: std::collections::HashMap::new(),
116 quote,
117 measurement,
118 signature: Vec::new(),
119 vendor_data: Vec::new(),
120 }
121 }
122
123 pub fn with_signature(mut self, signature: Vec<u8>) -> Self {
125 self.signature = signature;
126 self
127 }
128
129 pub fn with_vendor_data(mut self, data: Vec<u8>) -> Self {
131 self.vendor_data = data;
132 self
133 }
134}
135
136#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
138pub struct Measurement {
139 #[serde(default)]
141 pub index: u32,
142 #[serde(default)]
144 pub algorithm: String,
145 #[serde(default)]
147 pub value: Vec<u8>,
148 pub register: String,
150 pub description: Option<String>,
152}
153
154#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
156pub struct AttestationResult {
157 #[serde(default)]
159 pub valid: bool,
160 #[serde(default)]
162 pub verified_at: Timestamp,
163 #[serde(default)]
165 pub vendor: TeeVendor,
166 #[serde(default)]
168 pub tcb_version: String,
169 #[serde(default)]
171 pub measurements: Vec<Measurement>,
172 #[serde(default)]
174 pub cert_chain_valid: bool,
175 #[serde(default)]
177 pub details: std::collections::HashMap<String, String>,
178 pub verified_measurement: Vec<u8>,
180 pub error: Option<String>,
182 pub metadata: AttestationMetadata,
184}
185
186impl Default for AttestationResult {
187 fn default() -> Self {
188 Self {
189 valid: false,
190 verified_at: Timestamp::now(),
191 vendor: TeeVendor::Generic,
192 tcb_version: String::new(),
193 measurements: Vec::new(),
194 cert_chain_valid: false,
195 details: std::collections::HashMap::new(),
196 verified_measurement: Vec::new(),
197 error: None,
198 metadata: AttestationMetadata::default(),
199 }
200 }
201}
202
203impl AttestationResult {
204 pub fn success(vendor: TeeVendor, measurement: Vec<u8>) -> Self {
206 Self {
207 valid: true,
208 verified_at: Timestamp::now(),
209 vendor,
210 tcb_version: String::new(),
211 measurements: Vec::new(),
212 cert_chain_valid: true,
213 details: std::collections::HashMap::new(),
214 verified_measurement: measurement,
215 error: None,
216 metadata: AttestationMetadata::default(),
217 }
218 }
219
220 pub fn failure(vendor: TeeVendor, error: String) -> Self {
222 Self {
223 valid: false,
224 verified_at: Timestamp::now(),
225 vendor,
226 tcb_version: String::new(),
227 measurements: Vec::new(),
228 cert_chain_valid: false,
229 details: std::collections::HashMap::new(),
230 verified_measurement: Vec::new(),
231 error: Some(error),
232 metadata: AttestationMetadata::default(),
233 }
234 }
235}
236
237#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
239pub struct AttestationMetadata {
240 pub firmware_version: Option<String>,
242 pub security_patch_level: Option<String>,
244 pub debug_mode: bool,
246 pub production: bool,
248}
249
250#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
252pub struct TeeCapacity {
253 pub max_concurrent_jobs: u32,
255 pub active_jobs: u32,
257 pub max_operations: u32,
259 pub current_operations: u32,
261 pub total_memory: u64,
263 pub available_memory: u64,
265 pub total_cpu_cores: u32,
267 pub available_cpu_cores: u32,
269 pub supported_vendors: Vec<TeeVendor>,
271}
272
273impl TeeCapacity {
274 pub fn new(max_concurrent_jobs: u32, total_memory: u64, total_cpu_cores: u32) -> Self {
276 Self {
277 max_concurrent_jobs,
278 active_jobs: 0,
279 max_operations: max_concurrent_jobs,
280 current_operations: 0,
281 total_memory,
282 available_memory: total_memory,
283 total_cpu_cores,
284 available_cpu_cores: total_cpu_cores,
285 supported_vendors: Vec::new(),
286 }
287 }
288
289 pub fn has_capacity(&self) -> bool {
291 self.active_jobs < self.max_concurrent_jobs
292 && self.available_memory > 0
293 && self.available_cpu_cores > 0
294 }
295
296 pub fn utilization(&self) -> u8 {
298 if self.max_concurrent_jobs == 0 {
299 0
300 } else {
301 ((self.active_jobs as f64 / self.max_concurrent_jobs as f64) * 100.0) as u8
302 }
303 }
304}
305
306#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
308pub struct TeeProviderInfo {
309 pub id: uuid::Uuid,
311 pub vendor: TeeVendor,
313 pub address: String,
315 pub public_key: Vec<u8>,
317 pub attestation: AttestationReport,
319 pub registered_at: Timestamp,
321 pub last_heartbeat: Timestamp,
323 pub capacity: TeeCapacity,
325 pub status: TeeProviderStatus,
327 pub attestation_result: Option<AttestationResult>,
329 pub pricing: TeeProviderPricing,
331 pub reputation: u64,
333 pub total_jobs_completed: u64,
335 pub last_attestation_update: Timestamp,
337}
338
339impl TeeProviderInfo {
340 pub fn new(address: Address, attestation: AttestationReport, capacity: TeeCapacity) -> Self {
342 let now = Timestamp::now();
343 Self {
344 id: uuid::Uuid::new_v4(),
345 vendor: attestation.vendor,
346 address: address.to_base58(),
347 public_key: Vec::new(),
348 attestation,
349 registered_at: now,
350 last_heartbeat: now,
351 capacity,
352 status: TeeProviderStatus::Pending,
353 attestation_result: None,
354 pricing: TeeProviderPricing::default(),
355 reputation: 0,
356 total_jobs_completed: 0,
357 last_attestation_update: now,
358 }
359 }
360
361 pub fn is_available(&self) -> bool {
363 self.status == TeeProviderStatus::Active && self.capacity.has_capacity()
364 }
365}
366
367#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
369pub struct TeeProviderPricing {
370 pub price_per_compute_unit: u64,
372 pub price_per_memory_gb_hour: u64,
374 pub minimum_job_price: u64,
376}
377
378impl Default for TeeProviderPricing {
379 fn default() -> Self {
380 Self {
381 price_per_compute_unit: 1000,
382 price_per_memory_gb_hour: 5000,
383 minimum_job_price: 100,
384 }
385 }
386}
387
388#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
390pub enum TeeProviderStatus {
391 Pending,
393 Active,
395 Inactive,
397 Draining,
399 Offline,
401 Untrusted,
403 Suspended,
405 AttestationExpired,
407}
408
409#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
411pub struct EnclaveRequest {
412 pub id: uuid::Uuid,
414 pub operation: String,
416 pub params: Vec<u8>,
418 pub include_attestation: bool,
420}
421
422impl EnclaveRequest {
423 pub fn new(operation: String, params: Vec<u8>) -> Self {
425 Self {
426 id: uuid::Uuid::new_v4(),
427 operation,
428 params,
429 include_attestation: false,
430 }
431 }
432}
433
434#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
436pub struct EnclaveResponse {
437 pub request_id: uuid::Uuid,
439 pub success: bool,
441 pub data: Vec<u8>,
443 pub error: Option<String>,
445 pub attestation: Option<AttestationReport>,
447}
448
449#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
451pub struct KeyGenParams {
452 pub algorithm: KeyAlgorithm,
454 pub purpose: KeyPurpose,
456 pub exportable: bool,
458 pub params: std::collections::HashMap<String, String>,
460}
461
462#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
464pub enum KeyAlgorithm {
465 Ed25519,
467 Secp256k1,
469 Aes256Gcm,
471}
472
473#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
475pub enum KeyPurpose {
476 Signing,
478 Encryption,
480 KeyAgreement,
482}
483
484#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
486pub struct EnclaveKeyHandle {
487 pub id: uuid::Uuid,
489 pub algorithm: KeyAlgorithm,
491 pub public_key: Option<Vec<u8>>,
493 pub created_at: Timestamp,
495 pub attestation: Option<AttestationReport>,
497}