phalanx-crypto 0.1.0

🛡️ Phalanx - General-purpose group E2E encryption protocol
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
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
//! Advanced key management for Phalanx Protocol
//! 
//! Provides enterprise-grade key lifecycle management, rotation scheduling,
//! backup/recovery, and security compliance features.

use crate::{
    Identity, PublicKey, 
    error::{PhalanxError, Result},
    crypto::{SymmetricKey, derive_phalanx_key, contexts},
    message::{GroupMessage, MessageContent},
};
use std::collections::{HashMap, BTreeMap, HashSet};
use std::time::{SystemTime, Duration, UNIX_EPOCH};
use tokio::sync::{RwLock, Mutex};
use serde::{Serialize, Deserialize};
use tracing::{debug, info, warn, error, instrument};
use async_trait::async_trait;

/// Advanced key manager for Phalanx groups
pub struct AdvancedKeyManager {
    /// Current active keys by group
    active_keys: RwLock<HashMap<[u8; 32], KeySet>>,
    /// Key rotation schedules
    rotation_schedules: RwLock<HashMap<[u8; 32], RotationSchedule>>,
    /// Key backup storage
    backup_storage: RwLock<Box<dyn KeyBackupStorage>>,
    /// Key derivation cache
    derivation_cache: RwLock<HashMap<KeyDerivationRequest, CachedKey>>,
    /// Security policies
    security_policies: RwLock<SecurityPolicies>,
    /// Key usage statistics
    usage_stats: RwLock<HashMap<[u8; 32], KeyUsageStats>>,
    /// Pending key operations
    pending_operations: Mutex<Vec<PendingKeyOperation>>,
    /// HSM integration (if available)
    hsm_provider: Option<Box<dyn HsmProvider>>,
}

/// Complete key set for a group
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct KeySet {
    /// Current encryption key
    pub current_key: EncryptionKeyInfo,
    /// Previous keys for decryption
    pub previous_keys: BTreeMap<u64, EncryptionKeyInfo>,
    /// Key exchange keys by member
    pub member_keys: HashMap<[u8; 32], MemberKeyInfo>,
    /// Root key for derivation
    pub root_key: RootKeyInfo,
    /// Key metadata
    pub metadata: KeyMetadata,
}

/// Information about an encryption key
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EncryptionKeyInfo {
    /// Key sequence number
    pub sequence: u64,
    /// Symmetric encryption key
    pub key: SymmetricKey,
    /// Key creation timestamp
    pub created_at: SystemTime,
    /// Key expiration timestamp
    pub expires_at: Option<SystemTime>,
    /// Key derivation info
    pub derivation: KeyDerivation,
    /// Usage statistics
    pub usage_count: u64,
    /// Security level
    pub security_level: SecurityLevel,
}

/// Information about a member's keys
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MemberKeyInfo {
    /// Member public key
    pub public_key: PublicKey,
    /// Key exchange public key
    pub kx_public_key: [u8; 32],
    /// Shared secrets with this member
    pub shared_secrets: HashMap<u64, [u8; 32]>,
    /// Member key status
    pub status: MemberKeyStatus,
    /// Last key update
    pub last_updated: SystemTime,
}

/// Root key information for key derivation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RootKeyInfo {
    /// Root key identifier
    pub key_id: [u8; 32],
    /// Key derivation parameters
    pub derivation_params: KeyDerivationParams,
    /// Root key creation time
    pub created_at: SystemTime,
    /// Root key version
    pub version: u32,
    /// Security classification
    pub classification: SecurityClassification,
}

/// Key metadata for management
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct KeyMetadata {
    /// Group identifier
    pub group_id: [u8; 32],
    /// Key set version
    pub version: u64,
    /// Creation timestamp
    pub created_at: SystemTime,
    /// Last rotation timestamp
    pub last_rotation: Option<SystemTime>,
    /// Rotation policy
    pub rotation_policy: RotationPolicy,
    /// Backup policy
    pub backup_policy: BackupPolicy,
    /// Compliance tags
    pub compliance_tags: HashSet<String>,
}

/// Key rotation schedule
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RotationSchedule {
    /// Next rotation time
    pub next_rotation: SystemTime,
    /// Rotation interval
    pub interval: Duration,
    /// Automatic rotation enabled
    pub auto_rotate: bool,
    /// Rotation policy
    pub policy: RotationPolicy,
    /// Retry configuration
    pub retry_config: RetryConfig,
}

/// Key rotation policy
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RotationPolicy {
    /// Maximum key age before forced rotation
    pub max_key_age: Duration,
    /// Maximum usage count before rotation
    pub max_usage_count: u64,
    /// Rotation trigger conditions
    pub triggers: HashSet<RotationTrigger>,
    /// Pre-rotation notification time
    pub notification_time: Duration,
    /// Emergency rotation capability
    pub emergency_rotation: bool,
}

/// Key backup policy
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BackupPolicy {
    /// Backup enabled
    pub enabled: bool,
    /// Backup frequency
    pub frequency: Duration,
    /// Retention period
    pub retention_period: Duration,
    /// Encryption for backups
    pub backup_encryption: BackupEncryption,
    /// Backup verification
    pub verify_backups: bool,
    /// Geographic distribution
    pub geo_distribution: Option<GeoDistribution>,
}

/// Security policies for key management
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SecurityPolicies {
    /// Minimum key strength
    pub min_key_strength: SecurityLevel,
    /// Required compliance frameworks
    pub compliance_frameworks: HashSet<ComplianceFramework>,
    /// Key escrow requirements
    pub key_escrow: Option<KeyEscrowPolicy>,
    /// Audit requirements
    pub audit_policy: AuditPolicy,
    /// Access control policies
    pub access_control: AccessControlPolicy,
}

/// Key usage statistics
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct KeyUsageStats {
    /// Total operations
    pub total_operations: u64,
    /// Encryption operations
    pub encryption_count: u64,
    /// Decryption operations
    pub decryption_count: u64,
    /// Key derivation operations
    pub derivation_count: u64,
    /// First use timestamp
    pub first_used: Option<SystemTime>,
    /// Last use timestamp
    pub last_used: Option<SystemTime>,
    /// Bytes processed
    pub bytes_processed: u64,
    /// Error count
    pub error_count: u64,
}

/// Pending key operation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PendingKeyOperation {
    /// Operation ID
    pub operation_id: [u8; 32],
    /// Group ID
    pub group_id: [u8; 32],
    /// Operation type
    pub operation: KeyOperation,
    /// Scheduled execution time
    pub scheduled_at: SystemTime,
    /// Operation priority
    pub priority: OperationPriority,
    /// Retry count
    pub retry_count: u32,
    /// Dependencies
    pub dependencies: Vec<[u8; 32]>,
}

/// Key derivation request for caching
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub struct KeyDerivationRequest {
    /// Base key identifier
    pub base_key_id: [u8; 32],
    /// Derivation context
    pub context: String,
    /// Additional parameters
    pub params: Vec<u8>,
}

/// Cached derived key
#[derive(Debug, Clone)]
pub struct CachedKey {
    /// Derived key
    pub key: SymmetricKey,
    /// Cache timestamp
    pub cached_at: SystemTime,
    /// Cache TTL
    pub ttl: Duration,
    /// Usage count since cached
    pub usage_count: u64,
}

/// Types of key operations
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum KeyOperation {
    /// Rotate group keys
    Rotate {
        /// New key sequence
        sequence: u64,
        /// Rotation reason
        reason: RotationReason,
    },
    /// Generate new keys
    Generate {
        /// Key type
        key_type: KeyType,
        /// Security level
        security_level: SecurityLevel,
    },
    /// Backup keys
    Backup {
        /// Backup destination
        destination: BackupDestination,
        /// Include historical keys
        include_history: bool,
    },
    /// Restore keys
    Restore {
        /// Restore source
        source: BackupSource,
        /// Restore point
        restore_point: SystemTime,
    },
    /// Derive keys
    Derive {
        /// Derivation context
        context: String,
        /// Parameters
        params: HashMap<String, Vec<u8>>,
    },
    /// Clean up old keys
    Cleanup {
        /// Cutoff time
        cutoff_time: SystemTime,
        /// Preserve count
        preserve_count: u32,
    },
}

/// Various enums and supporting types
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum RotationTrigger {
    TimeExpired,
    UsageExceeded,
    SecurityBreach,
    MembershipChange,
    ComplianceRequirement,
    ManualRequest,
    EmergencyRotation,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum RotationReason {
    Scheduled,
    Emergency,
    Compromise,
    Compliance,
    MembershipChange,
    Manual,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum KeyType {
    Encryption,
    Authentication,
    KeyExchange,
    Derivation,
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub enum SecurityLevel {
    Standard,
    High,
    Critical,
    TopSecret,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum MemberKeyStatus {
    Active,
    Revoked,
    Expired,
    Pending,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum SecurityClassification {
    Public,
    Internal,
    Confidential,
    Secret,
    TopSecret,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum ComplianceFramework {
    FIPS140_2,
    CommonCriteria,
    NIST,
    ISO27001,
    SOX,
    GDPR,
    HIPAA,
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub enum OperationPriority {
    Low,
    Normal,
    High,
    Emergency,
}

/// Supporting structures
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct KeyDerivation {
    pub method: DerivationMethod,
    pub parameters: HashMap<String, Vec<u8>>,
    pub context: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct KeyDerivationParams {
    pub method: DerivationMethod,
    pub salt: [u8; 32],
    pub iterations: u32,
    pub key_length: usize,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum DerivationMethod {
    HKDF,
    PBKDF2,
    Scrypt,
    Argon2,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RetryConfig {
    pub max_retries: u32,
    pub base_delay: Duration,
    pub max_delay: Duration,
    pub backoff_multiplier: f64,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BackupEncryption {
    pub enabled: bool,
    pub key_id: Option<[u8; 32]>,
    pub cipher: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GeoDistribution {
    pub regions: Vec<String>,
    pub min_replicas: u32,
    pub consistency_level: ConsistencyLevel,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ConsistencyLevel {
    Eventual,
    Strong,
    Linearizable,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct KeyEscrowPolicy {
    pub enabled: bool,
    pub trustees: Vec<String>,
    pub threshold: u32,
    pub escrow_duration: Duration,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AuditPolicy {
    pub enabled: bool,
    pub log_all_operations: bool,
    pub retention_period: Duration,
    pub compliance_reporting: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AccessControlPolicy {
    pub require_authentication: bool,
    pub multi_factor_auth: bool,
    pub role_based_access: bool,
    pub audit_access: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum BackupDestination {
    Local { path: String },
    Remote { url: String, credentials: String },
    HSM { module_id: String },
    Cloud { provider: String, config: HashMap<String, String> },
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum BackupSource {
    Local { path: String },
    Remote { url: String, credentials: String },
    HSM { module_id: String },
    Cloud { provider: String, config: HashMap<String, String> },
}

/// Trait for key backup storage
#[async_trait]
pub trait KeyBackupStorage: Send + Sync {
    async fn store_backup(&self, group_id: [u8; 32], keyset: &KeySet, metadata: &BackupMetadata) -> Result<BackupId>;
    async fn retrieve_backup(&self, backup_id: BackupId) -> Result<(KeySet, BackupMetadata)>;
    async fn list_backups(&self, group_id: [u8; 32]) -> Result<Vec<BackupInfo>>;
    async fn delete_backup(&self, backup_id: BackupId) -> Result<()>;
    async fn verify_backup(&self, backup_id: BackupId) -> Result<bool>;
}

/// Trait for HSM integration
#[async_trait]
pub trait HsmProvider: Send + Sync {
    async fn generate_key(&self, key_type: KeyType, security_level: SecurityLevel) -> Result<HsmKeyHandle>;
    async fn derive_key(&self, base_key: HsmKeyHandle, context: &str, params: &[u8]) -> Result<HsmKeyHandle>;
    async fn encrypt(&self, key_handle: HsmKeyHandle, plaintext: &[u8], aad: &[u8]) -> Result<Vec<u8>>;
    async fn decrypt(&self, key_handle: HsmKeyHandle, ciphertext: &[u8], aad: &[u8]) -> Result<Vec<u8>>;
    async fn export_key(&self, key_handle: HsmKeyHandle) -> Result<Vec<u8>>;
    async fn import_key(&self, key_data: &[u8], key_type: KeyType) -> Result<HsmKeyHandle>;
}

/// Supporting types for backup and HSM
pub type BackupId = [u8; 32];
pub type HsmKeyHandle = String;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BackupMetadata {
    pub backup_id: BackupId,
    pub group_id: [u8; 32],
    pub created_at: SystemTime,
    pub version: u64,
    pub encryption: BackupEncryption,
    pub compression: bool,
    pub integrity_hash: [u8; 32],
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BackupInfo {
    pub backup_id: BackupId,
    pub created_at: SystemTime,
    pub size: u64,
    pub version: u64,
    pub verified: bool,
}

impl Default for SecurityPolicies {
    fn default() -> Self {
        Self {
            min_key_strength: SecurityLevel::Standard,
            compliance_frameworks: HashSet::new(),
            key_escrow: None,
            audit_policy: AuditPolicy {
                enabled: true,
                log_all_operations: false,
                retention_period: Duration::from_secs(365 * 24 * 3600), // 1 year
                compliance_reporting: false,
            },
            access_control: AccessControlPolicy {
                require_authentication: true,
                multi_factor_auth: false,
                role_based_access: false,
                audit_access: true,
            },
        }
    }
}

impl Default for RotationPolicy {
    fn default() -> Self {
        Self {
            max_key_age: Duration::from_secs(30 * 24 * 3600), // 30 days
            max_usage_count: 1_000_000,
            triggers: {
                let mut triggers = HashSet::new();
                triggers.insert(RotationTrigger::TimeExpired);
                triggers.insert(RotationTrigger::UsageExceeded);
                triggers
            },
            notification_time: Duration::from_secs(24 * 3600), // 24 hours
            emergency_rotation: true,
        }
    }
}

impl Default for BackupPolicy {
    fn default() -> Self {
        Self {
            enabled: true,
            frequency: Duration::from_secs(7 * 24 * 3600), // Weekly
            retention_period: Duration::from_secs(90 * 24 * 3600), // 90 days
            backup_encryption: BackupEncryption {
                enabled: true,
                key_id: None,
                cipher: "ChaCha20-Poly1305".to_string(),
            },
            verify_backups: true,
            geo_distribution: None,
        }
    }
}

impl AdvancedKeyManager {
    /// Create a new advanced key manager
    pub async fn new() -> Result<Self> {
        Ok(Self {
            active_keys: RwLock::new(HashMap::new()),
            rotation_schedules: RwLock::new(HashMap::new()),
            backup_storage: RwLock::new(Box::new(LocalBackupStorage::new("/tmp/phalanx_backups".to_string()).await?)),
            derivation_cache: RwLock::new(HashMap::new()),
            security_policies: RwLock::new(SecurityPolicies::default()),
            usage_stats: RwLock::new(HashMap::new()),
            pending_operations: Mutex::new(Vec::new()),
            hsm_provider: None,
        })
    }
    
    /// Create a new key set for a group
    #[instrument(skip(self, identity))]
    pub async fn create_key_set(&self, group_id: [u8; 32], identity: &Identity) -> Result<KeySet> {
        info!("Creating new key set for group {:?}", hex::encode(group_id));
        
        // Generate root key
        let root_key_id = self.generate_random_bytes(32);
        let root_key = RootKeyInfo {
            key_id: root_key_id,
            derivation_params: KeyDerivationParams {
                method: DerivationMethod::HKDF,
                salt: self.generate_random_bytes(32),
                iterations: 100_000,
                key_length: 32,
            },
            created_at: SystemTime::now(),
            version: 1,
            classification: SecurityClassification::Internal,
        };
        
        // Generate initial encryption key
        let encryption_key = self.generate_encryption_key(1, &root_key).await?;
        
        // Create member key info
        let mut member_keys = HashMap::new();
        let public_key = identity.public_key();
        member_keys.insert(public_key.id(), MemberKeyInfo {
            public_key,
            kx_public_key: self.generate_random_bytes(32),
            shared_secrets: HashMap::new(),
            status: MemberKeyStatus::Active,
            last_updated: SystemTime::now(),
        });
        
        // Create key metadata
        let metadata = KeyMetadata {
            group_id,
            version: 1,
            created_at: SystemTime::now(),
            last_rotation: None,
            rotation_policy: RotationPolicy::default(),
            backup_policy: BackupPolicy::default(),
            compliance_tags: HashSet::new(),
        };
        
        let key_set = KeySet {
            current_key: encryption_key,
            previous_keys: BTreeMap::new(),
            member_keys,
            root_key,
            metadata,
        };
        
        // Store the key set
        let mut active_keys = self.active_keys.write().await;
        active_keys.insert(group_id, key_set.clone());
        
        // Schedule rotation
        self.schedule_rotation(group_id, &key_set.metadata.rotation_policy).await?;
        
        // Initialize usage stats
        let mut usage_stats = self.usage_stats.write().await;
        usage_stats.insert(group_id, KeyUsageStats::default());
        
        info!("Created key set for group {:?}", hex::encode(group_id));
        Ok(key_set)
    }
    
    /// Rotate keys for a group
    #[instrument(skip(self))]
    pub async fn rotate_keys(&self, group_id: [u8; 32], reason: RotationReason) -> Result<u64> {
        info!("Rotating keys for group {:?}, reason: {:?}", hex::encode(group_id), reason);
        
        let mut active_keys = self.active_keys.write().await;
        let key_set = active_keys.get_mut(&group_id)
            .ok_or_else(|| PhalanxError::crypto("Group not found for rotation"))?;
        
        // Move current key to previous keys
        let old_sequence = key_set.current_key.sequence;
        let old_key = key_set.current_key.clone();
        key_set.previous_keys.insert(old_sequence, old_key);
        
        // Generate new current key
        let new_sequence = old_sequence + 1;
        let new_key = self.generate_encryption_key(new_sequence, &key_set.root_key).await?;
        key_set.current_key = new_key;
        
        // Update metadata
        key_set.metadata.version += 1;
        key_set.metadata.last_rotation = Some(SystemTime::now());
        
        // Clean up old keys (keep last 10)
        while key_set.previous_keys.len() > 10 {
            if let Some((oldest_seq, _)) = key_set.previous_keys.iter().next() {
                let oldest_seq = *oldest_seq;
                key_set.previous_keys.remove(&oldest_seq);
            }
        }
        
        // Update usage stats
        let mut usage_stats = self.usage_stats.write().await;
        if let Some(stats) = usage_stats.get_mut(&group_id) {
            stats.total_operations += 1;
        }
        
        // Schedule next rotation
        self.schedule_rotation(group_id, &key_set.metadata.rotation_policy).await?;
        
        // Create backup if enabled
        if key_set.metadata.backup_policy.enabled {
            self.create_backup(group_id).await?;
        }
        
        info!("Rotated keys for group {:?} to sequence {}", hex::encode(group_id), new_sequence);
        Ok(new_sequence)
    }
    
    /// Add a new member to the key set
    #[instrument(skip(self, identity))]
    pub async fn add_member(&self, group_id: [u8; 32], identity: &Identity) -> Result<()> {
        info!("Adding member to group {:?}", hex::encode(group_id));
        
        let mut active_keys = self.active_keys.write().await;
        let key_set = active_keys.get_mut(&group_id)
            .ok_or_else(|| PhalanxError::crypto("Group not found for member addition"))?;
        
        let public_key = identity.public_key();
        let member_id = public_key.id();
        
        if key_set.member_keys.contains_key(&member_id) {
            return Err(PhalanxError::crypto("Member already exists in group"));
        }
        
        // Generate key exchange material for the new member
        let kx_public_key = self.generate_random_bytes(32);
        let member_info = MemberKeyInfo {
            public_key,
            kx_public_key,
            shared_secrets: HashMap::new(),
            status: MemberKeyStatus::Active,
            last_updated: SystemTime::now(),
        };
        
        key_set.member_keys.insert(member_id, member_info);
        key_set.metadata.version += 1;
        
        info!("Added member {:?} to group {:?}", hex::encode(member_id), hex::encode(group_id));
        
        // Trigger key rotation due to membership change
        if key_set.metadata.rotation_policy.triggers.contains(&RotationTrigger::MembershipChange) {
            self.schedule_immediate_operation(group_id, KeyOperation::Rotate {
                sequence: key_set.current_key.sequence + 1,
                reason: RotationReason::MembershipChange,
            }).await?;
        }
        
        Ok(())
    }
    
    /// Remove a member from the key set
    #[instrument(skip(self))]
    pub async fn remove_member(&self, group_id: [u8; 32], member_id: [u8; 32]) -> Result<()> {
        info!("Removing member {:?} from group {:?}", hex::encode(member_id), hex::encode(group_id));
        
        let mut active_keys = self.active_keys.write().await;
        let key_set = active_keys.get_mut(&group_id)
            .ok_or_else(|| PhalanxError::crypto("Group not found for member removal"))?;
        
        if let Some(mut member_info) = key_set.member_keys.remove(&member_id) {
            member_info.status = MemberKeyStatus::Revoked;
            key_set.metadata.version += 1;
            
            info!("Removed member {:?} from group {:?}", hex::encode(member_id), hex::encode(group_id));
            
            // Trigger key rotation due to membership change
            if key_set.metadata.rotation_policy.triggers.contains(&RotationTrigger::MembershipChange) {
                self.schedule_immediate_operation(group_id, KeyOperation::Rotate {
                    sequence: key_set.current_key.sequence + 1,
                    reason: RotationReason::MembershipChange,
                }).await?;
            }
        } else {
            return Err(PhalanxError::crypto("Member not found in group"));
        }
        
        Ok(())
    }
    
    /// Get current encryption key for a group
    #[instrument(skip(self))]
    pub async fn get_encryption_key(&self, group_id: [u8; 32]) -> Result<SymmetricKey> {
        let active_keys = self.active_keys.read().await;
        let key_set = active_keys.get(&group_id)
            .ok_or_else(|| PhalanxError::crypto("Group not found"))?;
        
        // Update usage stats
        let mut usage_stats = self.usage_stats.write().await;
        if let Some(stats) = usage_stats.get_mut(&group_id) {
            stats.encryption_count += 1;
            stats.total_operations += 1;
            stats.last_used = Some(SystemTime::now());
            if stats.first_used.is_none() {
                stats.first_used = Some(SystemTime::now());
            }
        }
        
        Ok(key_set.current_key.key.clone())
    }
    
    /// Get decryption key for a specific sequence
    #[instrument(skip(self))]
    pub async fn get_decryption_key(&self, group_id: [u8; 32], sequence: u64) -> Result<SymmetricKey> {
        let active_keys = self.active_keys.read().await;
        let key_set = active_keys.get(&group_id)
            .ok_or_else(|| PhalanxError::crypto("Group not found"))?;
        
        let key = if sequence == key_set.current_key.sequence {
            key_set.current_key.key.clone()
        } else if let Some(prev_key) = key_set.previous_keys.get(&sequence) {
            prev_key.key.clone()
        } else {
            return Err(PhalanxError::crypto(format!("Key sequence {} not found", sequence)));
        };
        
        // Update usage stats
        let mut usage_stats = self.usage_stats.write().await;
        if let Some(stats) = usage_stats.get_mut(&group_id) {
            stats.decryption_count += 1;
            stats.total_operations += 1;
            stats.last_used = Some(SystemTime::now());
            if stats.first_used.is_none() {
                stats.first_used = Some(SystemTime::now());
            }
        }
        
        Ok(key)
    }
    
    /// Derive a specialized key for a specific context
    #[instrument(skip(self))]
    pub async fn derive_key(&self, group_id: [u8; 32], context: &str, params: &[u8]) -> Result<SymmetricKey> {
        let request = KeyDerivationRequest {
            base_key_id: group_id,
            context: context.to_string(),
            params: params.to_vec(),
        };
        
        // Check cache first
        {
            let cache = self.derivation_cache.read().await;
            if let Some(cached) = cache.get(&request) {
                let age = SystemTime::now().duration_since(cached.cached_at).unwrap_or_default();
                if age < cached.ttl {
                    debug!("Using cached derived key for context: {}", context);
                    return Ok(cached.key.clone());
                }
            }
        }
        
        // Get base key
        let active_keys = self.active_keys.read().await;
        let key_set = active_keys.get(&group_id)
            .ok_or_else(|| PhalanxError::crypto("Group not found"))?;
        
        // Derive new key
        let derived_key = derive_phalanx_key(
            key_set.current_key.key.as_bytes(),
            context.as_bytes(),
            contexts::KEY_DERIVATION,
        );
        
        // Cache the derived key
        {
            let mut cache = self.derivation_cache.write().await;
            cache.insert(request, CachedKey {
                key: derived_key.clone(),
                cached_at: SystemTime::now(),
                ttl: Duration::from_secs(3600), // 1 hour
                usage_count: 0,
            });
            
            // Limit cache size
            if cache.len() > 1000 {
                let cutoff = SystemTime::now() - Duration::from_secs(1800);
                cache.retain(|_, v| v.cached_at > cutoff);
            }
        }
        
        // Update usage stats
        let mut usage_stats = self.usage_stats.write().await;
        if let Some(stats) = usage_stats.get_mut(&group_id) {
            stats.derivation_count += 1;
            stats.total_operations += 1;
        }
        
        debug!("Derived new key for context: {}", context);
        Ok(derived_key)
    }
    
    /// Create a backup of the key set
    #[instrument(skip(self))]
    pub async fn create_backup(&self, group_id: [u8; 32]) -> Result<BackupId> {
        info!("Creating backup for group {:?}", hex::encode(group_id));
        
        let active_keys = self.active_keys.read().await;
        let key_set = active_keys.get(&group_id)
            .ok_or_else(|| PhalanxError::crypto("Group not found for backup"))?;
        
        let backup_id = self.generate_random_bytes(32);
        let metadata = BackupMetadata {
            backup_id,
            group_id,
            created_at: SystemTime::now(),
            version: key_set.metadata.version,
            encryption: key_set.metadata.backup_policy.backup_encryption.clone(),
            compression: true,
            integrity_hash: self.generate_random_bytes(32), // TODO: Calculate actual hash
        };
        
        let backup_storage = self.backup_storage.read().await;
        let stored_id = backup_storage.store_backup(group_id, key_set, &metadata).await?;
        
        info!("Created backup {:?} for group {:?}", hex::encode(stored_id), hex::encode(group_id));
        Ok(stored_id)
    }
    
    /// Process pending operations
    #[instrument(skip(self))]
    pub async fn process_pending_operations(&self) -> Result<usize> {
        let now = SystemTime::now();
        let mut pending = self.pending_operations.lock().await;
        let mut processed = 0;
        
        let mut remaining_operations = Vec::new();
        
        for operation in pending.drain(..) {
            if operation.scheduled_at <= now {
                match self.execute_operation(operation.group_id, operation.operation.clone()).await {
                    Ok(_) => {
                        processed += 1;
                        info!("Executed scheduled operation {:?} for group {:?}", 
                              operation.operation, hex::encode(operation.group_id));
                    },
                    Err(e) => {
                        error!("Failed to execute operation: {}", e);
                        if operation.retry_count < 3 {
                            let mut retried_op = operation;
                            retried_op.retry_count += 1;
                            retried_op.scheduled_at = now + Duration::from_secs(60 * (2u64.pow(retried_op.retry_count)));
                            remaining_operations.push(retried_op);
                        }
                    }
                }
            } else {
                remaining_operations.push(operation);
            }
        }
        
        *pending = remaining_operations;
        Ok(processed)
    }
    
    /// Get key usage statistics
    pub async fn get_usage_stats(&self, group_id: [u8; 32]) -> Result<KeyUsageStats> {
        let usage_stats = self.usage_stats.read().await;
        Ok(usage_stats.get(&group_id).cloned().unwrap_or_default())
    }
    
    /// Helper methods
    async fn generate_encryption_key(&self, sequence: u64, root_key: &RootKeyInfo) -> Result<EncryptionKeyInfo> {
        let key = derive_phalanx_key(
            &root_key.key_id,
            &sequence.to_be_bytes(),
            contexts::KEY_DERIVATION,
        );
        
        Ok(EncryptionKeyInfo {
            sequence,
            key,
            created_at: SystemTime::now(),
            expires_at: None,
            derivation: KeyDerivation {
                method: DerivationMethod::HKDF,
                parameters: HashMap::new(),
                context: "encryption_key".to_string(),
            },
            usage_count: 0,
            security_level: SecurityLevel::Standard,
        })
    }
    
    async fn schedule_rotation(&self, group_id: [u8; 32], policy: &RotationPolicy) -> Result<()> {
        let next_rotation = SystemTime::now() + policy.max_key_age;
        let schedule = RotationSchedule {
            next_rotation,
            interval: policy.max_key_age,
            auto_rotate: true,
            policy: policy.clone(),
            retry_config: RetryConfig {
                max_retries: 3,
                base_delay: Duration::from_secs(60),
                max_delay: Duration::from_secs(3600),
                backoff_multiplier: 2.0,
            },
        };
        
        let mut schedules = self.rotation_schedules.write().await;
        schedules.insert(group_id, schedule);
        
        // Schedule the actual operation
        self.schedule_operation(group_id, KeyOperation::Rotate {
            sequence: 0, // Will be determined at execution time
            reason: RotationReason::Scheduled,
        }, next_rotation, OperationPriority::Normal).await?;
        
        Ok(())
    }
    
    async fn schedule_operation(&self, group_id: [u8; 32], operation: KeyOperation, scheduled_at: SystemTime, priority: OperationPriority) -> Result<()> {
        let operation_id = self.generate_random_bytes(32);
        let pending_op = PendingKeyOperation {
            operation_id,
            group_id,
            operation,
            scheduled_at,
            priority,
            retry_count: 0,
            dependencies: Vec::new(),
        };
        
        let mut pending = self.pending_operations.lock().await;
        pending.push(pending_op);
        pending.sort_by(|a, b| a.scheduled_at.cmp(&b.scheduled_at));
        
        Ok(())
    }
    
    async fn schedule_immediate_operation(&self, group_id: [u8; 32], operation: KeyOperation) -> Result<()> {
        self.schedule_operation(group_id, operation, SystemTime::now(), OperationPriority::High).await
    }
    
    async fn execute_operation(&self, group_id: [u8; 32], operation: KeyOperation) -> Result<()> {
        match operation {
            KeyOperation::Rotate { reason, .. } => {
                self.rotate_keys(group_id, reason).await?;
            },
            KeyOperation::Backup { .. } => {
                self.create_backup(group_id).await?;
            },
            KeyOperation::Cleanup { cutoff_time, preserve_count } => {
                self.cleanup_old_keys(group_id, cutoff_time, preserve_count).await?;
            },
            _ => {
                warn!("Unsupported operation type: {:?}", operation);
            }
        }
        Ok(())
    }
    
    async fn cleanup_old_keys(&self, group_id: [u8; 32], cutoff_time: SystemTime, preserve_count: u32) -> Result<()> {
        let mut active_keys = self.active_keys.write().await;
        let key_set = active_keys.get_mut(&group_id)
            .ok_or_else(|| PhalanxError::crypto("Group not found for cleanup"))?;
        
        let mut old_keys: Vec<_> = key_set.previous_keys.iter()
            .filter(|(_, key)| key.created_at < cutoff_time)
            .map(|(&seq, _)| seq)
            .collect();
        
        old_keys.sort();
        old_keys.reverse();
        
        // Keep the specified number of keys
        let to_remove = old_keys.len().saturating_sub(preserve_count as usize);
        for &seq in old_keys.iter().take(to_remove) {
            key_set.previous_keys.remove(&seq);
        }
        
        info!("Cleaned up {} old keys for group {:?}", to_remove, hex::encode(group_id));
        Ok(())
    }
    
    fn generate_random_bytes(&self, length: usize) -> [u8; 32] {
        use rand::RngCore;
        let mut rng = rand::thread_rng();
        let mut bytes = [0u8; 32];
        rng.fill_bytes(&mut bytes[..length.min(32)]);
        bytes
    }
}

/// Local backup storage implementation
pub struct LocalBackupStorage {
    backup_dir: String,
}

impl LocalBackupStorage {
    pub async fn new(backup_dir: String) -> Result<Self> {
        tokio::fs::create_dir_all(&backup_dir).await
            .map_err(|e| PhalanxError::crypto(format!("Failed to create backup directory: {}", e)))?;
        
        Ok(Self { backup_dir })
    }
}

#[async_trait]
impl KeyBackupStorage for LocalBackupStorage {
    async fn store_backup(&self, group_id: [u8; 32], keyset: &KeySet, metadata: &BackupMetadata) -> Result<BackupId> {
        let backup_path = format!("{}/{}.backup", self.backup_dir, hex::encode(metadata.backup_id));
        
        let backup_data = bincode::serialize(&(keyset, metadata))
            .map_err(|e| PhalanxError::crypto(format!("Serialization failed: {}", e)))?;
        
        tokio::fs::write(&backup_path, backup_data).await
            .map_err(|e| PhalanxError::crypto(format!("Failed to write backup: {}", e)))?;
        
        Ok(metadata.backup_id)
    }
    
    async fn retrieve_backup(&self, backup_id: BackupId) -> Result<(KeySet, BackupMetadata)> {
        let backup_path = format!("{}/{}.backup", self.backup_dir, hex::encode(backup_id));
        
        let backup_data = tokio::fs::read(&backup_path).await
            .map_err(|e| PhalanxError::crypto(format!("Failed to read backup: {}", e)))?;
        
        let (keyset, metadata) = bincode::deserialize(&backup_data)
            .map_err(|e| PhalanxError::crypto(format!("Deserialization failed: {}", e)))?;
        
        Ok((keyset, metadata))
    }
    
    async fn list_backups(&self, _group_id: [u8; 32]) -> Result<Vec<BackupInfo>> {
        // TODO: Implement proper backup listing
        Ok(Vec::new())
    }
    
    async fn delete_backup(&self, backup_id: BackupId) -> Result<()> {
        let backup_path = format!("{}/{}.backup", self.backup_dir, hex::encode(backup_id));
        tokio::fs::remove_file(&backup_path).await
            .map_err(|e| PhalanxError::crypto(format!("Failed to delete backup: {}", e)))?;
        Ok(())
    }
    
    async fn verify_backup(&self, backup_id: BackupId) -> Result<bool> {
        let backup_path = format!("{}/{}.backup", self.backup_dir, hex::encode(backup_id));
        Ok(tokio::fs::metadata(&backup_path).await.is_ok())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    
    #[tokio::test]
    async fn test_key_manager_creation() {
        let manager = AdvancedKeyManager::new().await.unwrap();
        assert!(manager.active_keys.read().await.is_empty());
    }
    
    #[tokio::test]
    async fn test_key_set_creation() {
        let manager = AdvancedKeyManager::new().await.unwrap();
        let identity = Identity::generate();
        let group_id = [1u8; 32];
        
        let key_set = manager.create_key_set(group_id, &identity).await.unwrap();
        assert_eq!(key_set.current_key.sequence, 1);
        assert_eq!(key_set.member_keys.len(), 1);
    }
    
    #[tokio::test]
    async fn test_key_rotation() {
        let manager = AdvancedKeyManager::new().await.unwrap();
        let identity = Identity::generate();
        let group_id = [1u8; 32];
        
        manager.create_key_set(group_id, &identity).await.unwrap();
        let new_sequence = manager.rotate_keys(group_id, RotationReason::Manual).await.unwrap();
        
        assert_eq!(new_sequence, 2);
    }
    
    #[tokio::test]
    async fn test_member_management() {
        let manager = AdvancedKeyManager::new().await.unwrap();
        let identity1 = Identity::generate();
        let identity2 = Identity::generate();
        let group_id = [1u8; 32];
        
        manager.create_key_set(group_id, &identity1).await.unwrap();
        manager.add_member(group_id, &identity2).await.unwrap();
        
        let key_set = manager.active_keys.read().await;
        let key_set = key_set.get(&group_id).unwrap();
        assert_eq!(key_set.member_keys.len(), 2);
    }
}