scirs2-core 0.4.3

Core utilities and common functionality for SciRS2 (scirs2-core)
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
//! Security management and encryption features
//!
//! This module provides comprehensive security features including encryption,
//! key management, access control, and audit logging for cloud storage operations.

use crate::error::{CoreError, CoreResult};
use super::types::*;
use std::collections::HashMap;
use std::time::{Duration, Instant};

/// Cloud security manager
#[derive(Debug)]
pub struct CloudSecurityManager {
    /// Encryption engines
    encryption_engines: HashMap<EncryptionAlgorithm, EncryptionEngine>,
    /// Key management
    key_management: KeyManagementSystem,
    /// Security policies
    security_policies: Vec<SecurityPolicy>,
    /// Audit logger
    audit_logger: AuditLogger,
}

/// Encryption engine
#[derive(Debug)]
pub struct EncryptionEngine {
    /// Algorithm
    pub algorithm: EncryptionAlgorithm,
    /// Key size
    pub key_size: usize,
    /// Performance metrics
    pub performance: EncryptionPerformance,
}

/// Encryption performance
#[derive(Debug, Clone)]
pub struct EncryptionPerformance {
    /// Encryption speed (MB/s)
    pub encryption_speed_mbps: f64,
    /// Decryption speed (MB/s)
    pub decryption_speed_mbps: f64,
    /// Memory overhead (MB)
    pub memory_overhead_mb: f64,
    /// CPU utilization
    pub cpu_utilization: f64,
}

/// Key management system
#[derive(Debug)]
pub struct KeyManagementSystem {
    /// Key store
    key_store: HashMap<String, EncryptionKey>,
    /// Key rotation policy
    rotation_policy: KeyRotationPolicy,
    /// Key derivation
    key_derivation: KeyDerivationConfig,
}

/// Encryption key
#[derive(Debug)]
pub struct EncryptionKey {
    /// Key ID
    pub id: String,
    /// Key data
    pub data: Vec<u8>,
    /// Algorithm
    pub algorithm: EncryptionAlgorithm,
    /// Created timestamp
    pub created: Instant,
    /// Expires timestamp
    pub expires: Option<Instant>,
    /// Usage count
    pub usage_count: u64,
}

/// Key rotation policy
#[derive(Debug, Clone)]
pub struct KeyRotationPolicy {
    /// Rotation interval
    pub rotation_interval: Duration,
    /// Maximum usage count
    pub max_usage_count: u64,
    /// Automatic rotation
    pub automatic_rotation: bool,
}

/// Key derivation configuration
#[derive(Debug, Clone)]
pub struct KeyDerivationConfig {
    /// Derivation function
    pub function: KeyDerivationFunction,
    /// Salt length
    pub salt_length: usize,
    /// Iteration count
    pub iterations: u32,
}

/// Key derivation functions
#[derive(Debug, Clone)]
pub enum KeyDerivationFunction {
    PBKDF2,
    Scrypt,
    Argon2,
    HKDF,
}

/// Security policy
#[derive(Debug, Clone)]
pub struct SecurityPolicy {
    /// Policy name
    pub name: String,
    /// Policy rules
    pub rules: Vec<SecurityRule>,
    /// Enforcement level
    pub enforcement_level: EnforcementLevel,
}

/// Security rule
#[derive(Debug, Clone)]
pub struct SecurityRule {
    /// Rule type
    pub rule_type: SecurityRuleType,
    /// Condition
    pub condition: String,
    /// Action
    pub action: SecurityAction,
}

/// Security rule types
#[derive(Debug, Clone)]
pub enum SecurityRuleType {
    Access,
    Encryption,
    Transfer,
    Storage,
    Audit,
}

/// Security actions
#[derive(Debug, Clone)]
pub enum SecurityAction {
    Allow,
    Deny,
    Encrypt,
    Log,
    Alert,
}

/// Enforcement levels
#[derive(Debug, Clone)]
pub enum EnforcementLevel {
    Advisory,
    Enforcing,
    Blocking,
}

/// Audit logger
#[derive(Debug)]
pub struct AuditLogger {
    /// Log entries
    log_entries: Vec<AuditLogEntry>,
    /// Log configuration
    config: AuditLogConfig,
}

/// Audit log entry
#[derive(Debug, Clone)]
pub struct AuditLogEntry {
    /// Timestamp
    pub timestamp: Instant,
    /// Event type
    pub event_type: AuditEventType,
    /// User/actor
    pub actor: String,
    /// Resource
    pub resource: String,
    /// Action
    pub action: String,
    /// Result
    pub result: AuditResult,
    /// Additional details
    pub details: HashMap<String, String>,
}

/// Audit event types
#[derive(Debug, Clone)]
pub enum AuditEventType {
    Access,
    Upload,
    Download,
    Delete,
    Configuration,
    Security,
    Error,
}

/// Audit results
#[derive(Debug, Clone)]
pub enum AuditResult {
    Success,
    Failure,
    Partial,
    Unknown,
}

/// Audit log configuration
#[derive(Debug, Clone)]
pub struct AuditLogConfig {
    /// Log level
    pub log_level: AuditLogLevel,
    /// Retention period
    pub retention_period: Duration,
    /// Log rotation
    pub log_rotation: LogRotationConfig,
}

/// Audit log levels
#[derive(Debug, Clone)]
pub enum AuditLogLevel {
    Minimal,
    Standard,
    Detailed,
    Verbose,
}

/// Log rotation configuration
#[derive(Debug, Clone)]
pub struct LogRotationConfig {
    /// Max file size (MB)
    pub max_file_size_mb: usize,
    /// Max files to keep
    pub max_files: usize,
    /// Rotation interval
    pub rotation_interval: Duration,
}

// Implementations

impl Default for CloudSecurityManager {
    fn default() -> Self {
        Self::new()
    }
}

impl CloudSecurityManager {
    pub fn new() -> Self {
        Self {
            encryption_engines: {
                let mut map = HashMap::new();
                map.insert(
                    EncryptionAlgorithm::AES256,
                    EncryptionEngine {
                        algorithm: EncryptionAlgorithm::AES256,
                        key_size: 256,
                        performance: EncryptionPerformance {
                            encryption_speed_mbps: 100.0,
                            decryption_speed_mbps: 120.0,
                            memory_overhead_mb: 1.0,
                            cpu_utilization: 0.1,
                        },
                    },
                );
                map
            },
            key_management: KeyManagementSystem {
                key_store: HashMap::new(),
                rotation_policy: KeyRotationPolicy {
                    rotation_interval: Duration::from_secs(30 * 24 * 60 * 60), // 30 days
                    max_usage_count: 1000000,
                    automatic_rotation: true,
                },
                key_derivation: KeyDerivationConfig {
                    function: KeyDerivationFunction::PBKDF2,
                    salt_length: 32,
                    iterations: 100000,
                },
            },
            security_policies: vec![SecurityPolicy {
                name: "default_encryption".to_string(),
                rules: vec![SecurityRule {
                    rule_type: SecurityRuleType::Encryption,
                    condition: "always".to_string(),
                    action: SecurityAction::Encrypt,
                }],
                enforcement_level: EnforcementLevel::Enforcing,
            }],
            audit_logger: AuditLogger {
                log_entries: Vec::new(),
                config: AuditLogConfig {
                    log_level: AuditLogLevel::Standard,
                    retention_period: Duration::from_secs(90 * 24 * 60 * 60), // 90 days
                    log_rotation: LogRotationConfig {
                        max_file_size_mb: 100,
                        max_files: 10,
                        rotation_interval: Duration::from_secs(24 * 60 * 60), // 1 day
                    },
                },
            },
        }
    }

    /// Encrypt data using specified algorithm
    pub fn encrypt_data(&mut self, data: &[u8], algorithm: &EncryptionAlgorithm, key_id: &str) -> CoreResult<Vec<u8>> {
        // Log the encryption operation
        self.log_security_event(
            AuditEventType::Security,
            "system".to_string(),
            "data".to_string(),
            "encrypt".to_string(),
            AuditResult::Success,
        )?;

        // Get the encryption engine
        let engine = self.encryption_engines.get(algorithm)
            .ok_or_else(|| CoreError::InvalidArgument(
                crate::error::ErrorContext::new(
                    format!("Encryption algorithm {:?} not available", algorithm)
                )
            ))?;

        // Get the key
        let key = self.key_management.get_key(key_id)?;

        // Simulate encryption (in real implementation, this would use actual crypto libraries)
        let encrypted_data = self.simulate_encryption(data, &key, &engine.algorithm)?;

        // Update key usage
        self.key_management.update_key_usage(key_id)?;

        Ok(encrypted_data)
    }

    /// Decrypt data using specified algorithm
    pub fn decrypt_data(&mut self, encrypted_data: &[u8], algorithm: &EncryptionAlgorithm, key_id: &str) -> CoreResult<Vec<u8>> {
        // Log the decryption operation
        self.log_security_event(
            AuditEventType::Security,
            "system".to_string(),
            "data".to_string(),
            "decrypt".to_string(),
            AuditResult::Success,
        )?;

        // Get the encryption engine
        let engine = self.encryption_engines.get(algorithm)
            .ok_or_else(|| CoreError::InvalidArgument(
                crate::error::ErrorContext::new(
                    format!("Encryption algorithm {:?} not available", algorithm)
                )
            ))?;

        // Get the key
        let key = self.key_management.get_key(key_id)?;

        // Simulate decryption
        let decrypted_data = self.simulate_decryption(encrypted_data, &key, &engine.algorithm)?;

        // Update key usage
        self.key_management.update_key_usage(key_id)?;

        Ok(decrypted_data)
    }

    /// Generate a new encryption key
    pub fn generate_key(&mut self, algorithm: EncryptionAlgorithm) -> CoreResult<String> {
        let key_id = format!("key_{}", std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap_or_default()
            .as_nanos());

        let key_size = match algorithm {
            EncryptionAlgorithm::AES256 => 32,
            EncryptionAlgorithm::AES128 => 16,
            EncryptionAlgorithm::ChaCha20Poly1305 => 32,
            EncryptionAlgorithm::ProviderManaged => 32,
        };

        // Generate random key data (in real implementation, use secure random generator)
        let key_data = (0..key_size).map(|i| (i % 256) as u8).collect();

        let encryption_key = EncryptionKey {
            id: key_id.clone(),
            data: key_data,
            algorithm,
            created: Instant::now(),
            expires: None,
            usage_count: 0,
        };

        self.key_management.store_key(key_id.clone(), encryption_key)?;

        // Log key generation
        self.log_security_event(
            AuditEventType::Security,
            "system".to_string(),
            &key_id,
            "generate_key".to_string(),
            AuditResult::Success,
        )?;

        Ok(key_id)
    }

    /// Rotate encryption keys
    pub fn rotate_keys(&mut self) -> CoreResult<Vec<String>> {
        let mut rotated_keys = Vec::new();

        // Check which keys need rotation
        let keys_to_rotate = self.key_management.get_keys_for_rotation()?;

        for old_key_id in keys_to_rotate {
            // Get the old key to determine algorithm
            let old_key = self.key_management.get_key(&old_key_id)?;
            let algorithm = old_key.algorithm.clone();

            // Generate new key
            let new_key_id = self.generate_key(algorithm)?;

            // Mark old key as expired
            self.key_management.expire_key(&old_key_id)?;

            rotated_keys.push(new_key_id);

            // Log key rotation
            self.log_security_event(
                AuditEventType::Security,
                "system".to_string(),
                &old_key_id,
                "rotate_key".to_string(),
                AuditResult::Success,
            )?;
        }

        Ok(rotated_keys)
    }

    /// Check if operation is allowed by security policies
    pub fn check_access(&self, operation: &str, resource: &str) -> CoreResult<bool> {
        for policy in &self.security_policies {
            for rule in &policy.rules {
                if self.rule_matches(&rule, operation, resource) {
                    return Ok(match rule.action {
                        SecurityAction::Allow => true,
                        SecurityAction::Deny => false,
                        _ => true, // Other actions don't block access
                    });
                }
            }
        }

        // Default: allow if no explicit deny
        Ok(true)
    }

    /// Add a security policy
    pub fn add_security_policy(&mut self, policy: SecurityPolicy) -> CoreResult<()> {
        self.security_policies.push(policy);
        Ok(())
    }

    /// Get audit logs
    pub fn get_audit_logs(&self, start_time: Instant, end_time: Instant) -> Vec<AuditLogEntry> {
        self.audit_logger.log_entries.iter()
            .filter(|entry| entry.timestamp >= start_time && entry.timestamp <= end_time)
            .cloned()
            .collect()
    }

    /// Get security statistics
    pub fn get_security_statistics(&self) -> SecurityStatistics {
        SecurityStatistics {
            total_audit_entries: self.audit_logger.log_entries.len() as u64,
            active_keys: self.key_management.key_store.len() as u32,
            security_policies: self.security_policies.len() as u32,
            encryption_algorithms: self.encryption_engines.len() as u32,
        }
    }

    // Private helper methods

    fn simulate_encryption(&self, data: &[u8], _key: &EncryptionKey, _algorithm: &EncryptionAlgorithm) -> CoreResult<Vec<u8>> {
        // Simulate encryption by XORing with a pattern
        let encrypted: Vec<u8> = data.iter()
            .enumerate()
            .map(|(i, &byte)| byte ^ ((i % 256) as u8))
            .collect();
        Ok(encrypted)
    }

    fn simulate_decryption(&self, encrypted_data: &[u8], _key: &EncryptionKey, _algorithm: &EncryptionAlgorithm) -> CoreResult<Vec<u8>> {
        // Simulate decryption by reversing the XOR
        let decrypted: Vec<u8> = encrypted_data.iter()
            .enumerate()
            .map(|(i, &byte)| byte ^ ((i % 256) as u8))
            .collect();
        Ok(decrypted)
    }

    fn rule_matches(&self, rule: &SecurityRule, operation: &str, _resource: &str) -> bool {
        match rule.rule_type {
            SecurityRuleType::Access => operation.contains("access"),
            SecurityRuleType::Encryption => operation.contains("encrypt") || operation.contains("decrypt"),
            SecurityRuleType::Transfer => operation.contains("upload") || operation.contains("download"),
            SecurityRuleType::Storage => operation.contains("store") || operation.contains("delete"),
            SecurityRuleType::Audit => operation.contains("audit") || operation.contains("log"),
        }
    }

    fn log_security_event(
        &mut self,
        event_type: AuditEventType,
        actor: String,
        resource: String,
        action: String,
        result: AuditResult,
    ) -> CoreResult<()> {
        let entry = AuditLogEntry {
            timestamp: Instant::now(),
            event_type,
            actor,
            resource,
            action,
            result,
            details: HashMap::new(),
        };

        self.audit_logger.log_entries.push(entry);

        // Rotate logs if needed
        self.audit_logger.rotate_logs_if_needed()?;

        Ok(())
    }
}

impl KeyManagementSystem {
    fn get_key(&self, key_id: &str) -> CoreResult<&EncryptionKey> {
        self.key_store.get(key_id)
            .ok_or_else(|| CoreError::InvalidArgument(
                crate::error::ErrorContext::new(
                    format!("Key {} not found", key_id)
                )
            ))
    }

    fn store_key(&mut self, key_id: String, key: EncryptionKey) -> CoreResult<()> {
        self.key_store.insert(key_id, key);
        Ok(())
    }

    fn update_key_usage(&mut self, key_id: &str) -> CoreResult<()> {
        if let Some(key) = self.key_store.get_mut(key_id) {
            key.usage_count += 1;
        }
        Ok(())
    }

    fn expire_key(&mut self, key_id: &str) -> CoreResult<()> {
        if let Some(key) = self.key_store.get_mut(key_id) {
            key.expires = Some(Instant::now());
        }
        Ok(())
    }

    fn get_keys_for_rotation(&self) -> CoreResult<Vec<String>> {
        let mut keys_to_rotate = Vec::new();

        for (key_id, key) in &self.key_store {
            // Check if key needs rotation based on age or usage
            let needs_rotation = key.created.elapsed() > self.rotation_policy.rotation_interval
                || key.usage_count > self.rotation_policy.max_usage_count;

            if needs_rotation && key.expires.is_none() {
                keys_to_rotate.push(key_id.clone());
            }
        }

        Ok(keys_to_rotate)
    }
}

impl AuditLogger {
    fn rotate_logs_if_needed(&mut self) -> CoreResult<()> {
        // Simple rotation based on entry count (in real implementation, would use file size)
        let max_entries = 10000;

        if self.log_entries.len() > max_entries {
            // Keep only recent entries
            let keep_count = max_entries / 2;
            self.log_entries = self.log_entries.split_off(self.log_entries.len() - keep_count);
        }

        Ok(())
    }
}

/// Security statistics
#[derive(Debug, Clone)]
pub struct SecurityStatistics {
    /// Total audit entries
    pub total_audit_entries: u64,
    /// Number of active keys
    pub active_keys: u32,
    /// Number of security policies
    pub security_policies: u32,
    /// Number of encryption algorithms
    pub encryption_algorithms: u32,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_security_manager_creation() {
        let manager = CloudSecurityManager::new();
        assert!(!manager.encryption_engines.is_empty());
        assert!(!manager.security_policies.is_empty());
    }

    #[test]
    fn test_key_generation() {
        let mut manager = CloudSecurityManager::new();
        let key_id = manager.generate_key(EncryptionAlgorithm::AES256).expect("Operation failed");
        assert!(!key_id.is_empty());

        // Verify key was stored
        let key = manager.key_management.get_key(&key_id).expect("Operation failed");
        assert_eq!(key.algorithm, EncryptionAlgorithm::AES256);
        assert_eq!(key.data.len(), 32); // AES256 key size
    }

    #[test]
    fn test_encryption_decryption() {
        let mut manager = CloudSecurityManager::new();
        let key_id = manager.generate_key(EncryptionAlgorithm::AES256).expect("Operation failed");

        let original_data = b"Hello, World!";
        let encrypted = manager.encrypt_data(original_data, &EncryptionAlgorithm::AES256, &key_id).expect("Operation failed");
        let decrypted = manager.decrypt_data(&encrypted, &EncryptionAlgorithm::AES256, &key_id).expect("Operation failed");

        assert_eq!(original_data, decrypted.as_slice());
        assert_ne!(original_data, encrypted.as_slice());
    }

    #[test]
    fn test_access_control() {
        let manager = CloudSecurityManager::new();

        // Test with default policy (should allow most operations)
        let allowed = manager.check_access("read", "file.txt").expect("Operation failed");
        assert!(allowed);
    }

    #[test]
    fn test_audit_logging() {
        let mut manager = CloudSecurityManager::new();

        let start_time = Instant::now();
        manager.log_security_event(
            AuditEventType::Access,
            "user1".to_string(),
            "file.txt".to_string(),
            "read".to_string(),
            AuditResult::Success,
        ).expect("Operation failed");

        let logs = manager.get_audit_logs(start_time, Instant::now());
        assert_eq!(logs.len(), 1);
        assert_eq!(logs[0].actor, "user1");
    }
}