rvoip-rtp-core 0.2.2

RTP/RTCP protocol implementation for the rvoip stack
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
//! ZRTP (Z Real-time Transport Protocol) implementation
//!
//! This module implements ZRTP key exchange as specified in RFC 6189.
//! ZRTP is a key management protocol that performs a Diffie-Hellman key exchange
//! during call setup to establish SRTP keys, without requiring PKI or pre-provisioned
//! certificates.
//!
//! Reference: <https://tools.ietf.org/html/rfc6189>

use crate::security::SecurityKeyExchange;
use crate::srtp::crypto::SrtpCryptoKey;
use crate::srtp::{SrtpCryptoSuite, SRTP_AES128_CM_SHA1_80};
use crate::Error;
use hmac::{Hmac, Mac};
use p256::ecdh::EphemeralSecret;
use p256::PublicKey;
use rand::{rngs::OsRng, RngCore};
use sha2::{Digest, Sha256};

pub mod hash;
pub mod packet;

use packet::{ZrtpMessageType, ZrtpPacket, ZrtpVersion};

/// ZRTP cipher algorithms
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ZrtpCipher {
    /// AES-128 Counter Mode
    Aes1,
    /// AES-256 Counter Mode
    Aes3,
    /// Two-Fish 128-bit Counter Mode
    TwoF,
}

/// ZRTP hash algorithms
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ZrtpHash {
    /// SHA-256
    S256,
    /// SHA-384
    S384,
}

/// ZRTP authentication tag algorithms
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ZrtpAuthTag {
    /// HMAC-SHA1 32-bit
    HS32,
    /// HMAC-SHA1 80-bit
    HS80,
}

/// ZRTP key agreement algorithms
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ZrtpKeyAgreement {
    /// Diffie-Hellman 3072-bit
    DH3k,
    /// Diffie-Hellman 4096-bit
    DH4k,
    /// Elliptic Curve P-256
    EC25,
    /// Elliptic Curve P-384
    EC38,
}

/// ZRTP SAS rendering algorithms
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ZrtpSasType {
    /// 32-bit SAS using ASCII decimal (base 10)
    B32,
    /// 32-bit SAS using base 32 (5 bits)
    B32E,
}

/// ZRTP configuration
#[derive(Debug, Clone)]
pub struct ZrtpConfig {
    /// Supported cipher algorithms in order of preference
    pub ciphers: Vec<ZrtpCipher>,
    /// Supported hash algorithms in order of preference
    pub hashes: Vec<ZrtpHash>,
    /// Supported authentication tag algorithms in order of preference
    pub auth_tags: Vec<ZrtpAuthTag>,
    /// Supported key agreement algorithms in order of preference
    pub key_agreements: Vec<ZrtpKeyAgreement>,
    /// Supported SAS rendering algorithms in order of preference
    pub sas_types: Vec<ZrtpSasType>,
    /// Client identifier string
    pub client_id: String,
    /// SRTP crypto suite to use
    pub srtp_profile: SrtpCryptoSuite,
}

impl Default for ZrtpConfig {
    fn default() -> Self {
        Self {
            ciphers: vec![ZrtpCipher::Aes1],
            hashes: vec![ZrtpHash::S256],
            auth_tags: vec![ZrtpAuthTag::HS80, ZrtpAuthTag::HS32],
            key_agreements: vec![ZrtpKeyAgreement::EC25, ZrtpKeyAgreement::DH3k],
            sas_types: vec![ZrtpSasType::B32],
            client_id: "RVOIP ZRTP 1.0".to_string(),
            srtp_profile: SRTP_AES128_CM_SHA1_80,
        }
    }
}

/// ZRTP role in key exchange
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ZrtpRole {
    /// Initiator (sends Hello first)
    Initiator,
    /// Responder (responds to Hello)
    Responder,
}

/// ZRTP state machine states
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ZrtpState {
    /// Initial state
    Initial,
    /// Hello sent
    HelloSent,
    /// Hello received
    HelloReceived,
    /// Hello ACK sent
    HelloAckSent,
    /// Hello ACK received
    HelloAckReceived,
    /// Commit sent
    CommitSent,
    /// Commit received
    CommitReceived,
    /// DH Part 1 sent
    DhPart1Sent,
    /// DH Part 1 received
    DhPart1Received,
    /// DH Part 2 sent
    DhPart2Sent,
    /// DH Part 2 received
    DhPart2Received,
    /// Confirm 1 sent
    Confirm1Sent,
    /// Confirm 1 received
    Confirm1Received,
    /// Confirm 2 sent
    Confirm2Sent,
    /// Confirm 2 received
    Confirm2Received,
    /// Confirm ACK sent
    ConfirmAckSent,
    /// Confirm ACK received
    ConfirmAckReceived,
    /// Completed
    Completed,
    /// Error state
    Error,
}

/// ZRTP key exchange implementation
pub struct Zrtp {
    /// Configuration
    config: ZrtpConfig,
    /// Role in the exchange (initiator or responder)
    role: ZrtpRole,
    /// Current state
    state: ZrtpState,
    /// Local ZRTP ID (ZID)
    zid: [u8; 12],
    /// Remote ZRTP ID (ZID)
    peer_zid: Option<[u8; 12]>,
    /// Local Hello hash
    hello_hash: Option<[u8; 32]>,
    /// Remote Hello hash
    peer_hello_hash: Option<[u8; 32]>,
    /// Selected cipher
    selected_cipher: Option<ZrtpCipher>,
    /// Selected hash
    selected_hash: Option<ZrtpHash>,
    /// Selected authentication tag
    selected_auth_tag: Option<ZrtpAuthTag>,
    /// Selected key agreement
    selected_key_agreement: Option<ZrtpKeyAgreement>,
    /// Selected SAS type
    selected_sas_type: Option<ZrtpSasType>,
    /// Local DH key pair
    dh_key: Option<EphemeralSecret>,
    /// Remote DH public key
    peer_public_key: Option<PublicKey>,
    /// Shared secret
    shared_secret: Option<Vec<u8>>,
    /// SRTP keys for initiator
    srtp_initiator_key: Option<SrtpCryptoKey>,
    /// SRTP keys for responder
    srtp_responder_key: Option<SrtpCryptoKey>,
}

impl Zrtp {
    /// Create a new ZRTP key exchange
    pub fn new(config: ZrtpConfig, role: ZrtpRole) -> Self {
        // Generate random ZID
        let mut zid = [0u8; 12];
        OsRng.fill_bytes(&mut zid);

        Self {
            config,
            role,
            state: ZrtpState::Initial,
            zid,
            peer_zid: None,
            hello_hash: None,
            peer_hello_hash: None,
            selected_cipher: None,
            selected_hash: None,
            selected_auth_tag: None,
            selected_key_agreement: None,
            selected_sas_type: None,
            dh_key: None,
            peer_public_key: None,
            shared_secret: None,
            srtp_initiator_key: None,
            srtp_responder_key: None,
        }
    }

    /// Create Hello message
    fn create_hello(&mut self) -> Result<ZrtpPacket, Error> {
        // Create ZRTP Hello packet
        let mut hello = ZrtpPacket::new(ZrtpMessageType::Hello);

        // Set version
        hello.set_version(ZrtpVersion::V12);

        // Set client ID
        hello.set_client_id(&self.config.client_id);

        // Set ZID
        hello.set_zid(&self.zid);

        // Add supported algorithms
        for cipher in &self.config.ciphers {
            hello.add_cipher(*cipher);
        }

        for hash in &self.config.hashes {
            hello.add_hash(*hash);
        }

        for auth_tag in &self.config.auth_tags {
            hello.add_auth_tag(*auth_tag);
        }

        for key_agreement in &self.config.key_agreements {
            hello.add_key_agreement(*key_agreement);
        }

        for sas_type in &self.config.sas_types {
            hello.add_sas_type(*sas_type);
        }

        // Calculate Hello hash
        let hello_bytes = hello.to_bytes();
        let mut hasher = Sha256::new();
        hasher.update(&hello_bytes);
        let mut hash = [0u8; 32];
        hash.copy_from_slice(&hasher.finalize());
        self.hello_hash = Some(hash);

        // Update state
        self.state = ZrtpState::HelloSent;

        Ok(hello)
    }

    /// Process Hello message
    fn process_hello(&mut self, packet: &ZrtpPacket) -> Result<ZrtpPacket, Error> {
        if packet.message_type() != ZrtpMessageType::Hello {
            return Err(Error::InvalidMessage("Expected Hello message".into()));
        }

        // Extract peer ZID
        let peer_zid = packet
            .zid()
            .ok_or_else(|| Error::InvalidMessage("Missing ZID in Hello".into()))?;
        self.peer_zid = Some(peer_zid);

        // Calculate Hello hash for peer's Hello
        let hello_bytes = packet.to_bytes();
        let mut hasher = Sha256::new();
        hasher.update(&hello_bytes);
        let mut hash = [0u8; 32];
        hash.copy_from_slice(&hasher.finalize());
        self.peer_hello_hash = Some(hash);

        // Find common algorithms
        // For simplicity, we'll just select the first ones in common

        // Find common cipher
        for our_cipher in &self.config.ciphers {
            if packet.ciphers().contains(our_cipher) {
                self.selected_cipher = Some(*our_cipher);
                break;
            }
        }

        // Find common hash
        for our_hash in &self.config.hashes {
            if packet.hashes().contains(our_hash) {
                self.selected_hash = Some(*our_hash);
                break;
            }
        }

        // Find common auth tag
        for our_auth_tag in &self.config.auth_tags {
            if packet.auth_tags().contains(our_auth_tag) {
                self.selected_auth_tag = Some(*our_auth_tag);
                break;
            }
        }

        // Find common key agreement
        for our_key_agreement in &self.config.key_agreements {
            if packet.key_agreements().contains(our_key_agreement) {
                self.selected_key_agreement = Some(*our_key_agreement);
                break;
            }
        }

        // Find common SAS type
        for our_sas_type in &self.config.sas_types {
            if packet.sas_types().contains(our_sas_type) {
                self.selected_sas_type = Some(*our_sas_type);
                break;
            }
        }

        // Verify we have common algorithms
        if self.selected_cipher.is_none()
            || self.selected_hash.is_none()
            || self.selected_auth_tag.is_none()
            || self.selected_key_agreement.is_none()
            || self.selected_sas_type.is_none()
        {
            return Err(Error::NegotiationFailed("No common algorithms".into()));
        }

        // Create Hello ACK
        let hello_ack = ZrtpPacket::new(ZrtpMessageType::HelloAck);

        // Update state
        self.state = ZrtpState::HelloReceived;

        Ok(hello_ack)
    }

    /// Process Hello ACK
    fn process_hello_ack(&mut self, packet: &ZrtpPacket) -> Result<ZrtpPacket, Error> {
        if packet.message_type() != ZrtpMessageType::HelloAck {
            return Err(Error::InvalidMessage("Expected HelloAck message".into()));
        }

        // Create Commit
        let mut commit = ZrtpPacket::new(ZrtpMessageType::Commit);

        // Set ZID
        commit.set_zid(&self.zid);

        // Set selected algorithms
        commit.set_cipher(self.selected_cipher.unwrap());
        commit.set_hash(self.selected_hash.unwrap());
        commit.set_auth_tag(self.selected_auth_tag.unwrap());
        commit.set_key_agreement(self.selected_key_agreement.unwrap());
        commit.set_sas_type(self.selected_sas_type.unwrap());

        // Generate DH key pair based on selected key agreement
        if self.selected_key_agreement == Some(ZrtpKeyAgreement::EC25) {
            self.dh_key = Some(EphemeralSecret::random(&mut OsRng));
        } else {
            // Fallback to default EC25 for now
            // In a full implementation, we'd support all key agreement methods
            self.dh_key = Some(EphemeralSecret::random(&mut OsRng));
        }

        // Update state
        self.state = ZrtpState::HelloAckReceived;

        Ok(commit)
    }

    /// Process Commit
    fn process_commit(&mut self, packet: &ZrtpPacket) -> Result<ZrtpPacket, Error> {
        if packet.message_type() != ZrtpMessageType::Commit {
            return Err(Error::InvalidMessage("Expected Commit message".into()));
        }

        // Extract selected algorithms
        self.selected_cipher = Some(packet.cipher().unwrap());
        self.selected_hash = Some(packet.hash().unwrap());
        self.selected_auth_tag = Some(packet.auth_tag().unwrap());
        self.selected_key_agreement = Some(packet.key_agreement().unwrap());
        self.selected_sas_type = Some(packet.sas_type().unwrap());

        // Generate DH key pair based on selected key agreement
        if self.selected_key_agreement == Some(ZrtpKeyAgreement::EC25) {
            self.dh_key = Some(EphemeralSecret::random(&mut OsRng));
        } else {
            // Fallback to default EC25 for now
            self.dh_key = Some(EphemeralSecret::random(&mut OsRng));
        }

        // Create DH Part 1
        let mut dh_part1 = ZrtpPacket::new(ZrtpMessageType::DHPart1);

        // Set public key
        if let Some(key) = &self.dh_key {
            let public_key = PublicKey::from(key);
            let public_key_bytes = public_key.to_sec1_bytes();
            dh_part1.set_public_key(&public_key_bytes);
        }

        // Update state
        self.state = ZrtpState::CommitReceived;

        Ok(dh_part1)
    }

    /// Process DH Part 1
    fn process_dh_part1(&mut self, packet: &ZrtpPacket) -> Result<ZrtpPacket, Error> {
        if packet.message_type() != ZrtpMessageType::DHPart1 {
            return Err(Error::InvalidMessage("Expected DHPart1 message".into()));
        }

        // Extract peer's public key
        let peer_public_key_bytes = packet
            .public_key()
            .ok_or_else(|| Error::InvalidMessage("Missing public key in DHPart1".into()))?;

        // Parse peer's public key
        let peer_public_key = PublicKey::from_sec1_bytes(&peer_public_key_bytes)
            .map_err(|_| Error::CryptoError("Invalid peer public key".into()))?;

        self.peer_public_key = Some(peer_public_key);

        // Create DH Part 2
        let mut dh_part2 = ZrtpPacket::new(ZrtpMessageType::DHPart2);

        // Set public key
        if let Some(key) = &self.dh_key {
            let public_key = PublicKey::from(key);
            let public_key_bytes = public_key.to_sec1_bytes();
            dh_part2.set_public_key(&public_key_bytes);
        }

        // Update state
        self.state = ZrtpState::DhPart1Received;

        Ok(dh_part2)
    }

    /// Process DH Part 2
    fn process_dh_part2(&mut self, packet: &ZrtpPacket) -> Result<ZrtpPacket, Error> {
        if packet.message_type() != ZrtpMessageType::DHPart2 {
            return Err(Error::InvalidMessage("Expected DHPart2 message".into()));
        }

        // Extract peer's public key
        let peer_public_key_bytes = packet
            .public_key()
            .ok_or_else(|| Error::InvalidMessage("Missing public key in DHPart2".into()))?;

        // Parse peer's public key
        let peer_public_key = PublicKey::from_sec1_bytes(&peer_public_key_bytes)
            .map_err(|_| Error::CryptoError("Invalid peer public key".into()))?;

        self.peer_public_key = Some(peer_public_key);

        // Compute shared secret
        if let (Some(dh_key), Some(peer_key)) = (&self.dh_key, &self.peer_public_key) {
            let shared_secret = dh_key.diffie_hellman(peer_key);
            let shared_secret_bytes = shared_secret.raw_secret_bytes().to_vec();
            self.shared_secret = Some(shared_secret_bytes.clone());

            // Derive SRTP keys
            self.derive_srtp_keys()?;
        } else {
            return Err(Error::CryptoError("Missing keys for DH exchange".into()));
        }

        // Create Confirm1
        let mut confirm1 = ZrtpPacket::new(ZrtpMessageType::Confirm1);

        // Set ZID
        confirm1.set_zid(&self.zid);

        // Calculate HMAC
        if let Some(shared_secret) = &self.shared_secret {
            let mut mac = Hmac::<Sha256>::new_from_slice(shared_secret)
                .map_err(|_| Error::CryptoError("Failed to create HMAC".into()))?;

            mac.update(&self.zid);
            if let Some(peer_zid) = &self.peer_zid {
                mac.update(peer_zid);
            }

            let mac_result = mac.finalize().into_bytes();
            confirm1.set_mac(&mac_result);
        }

        // Update state
        self.state = ZrtpState::DhPart2Received;

        Ok(confirm1)
    }

    /// Process Confirm1
    fn process_confirm1(&mut self, packet: &ZrtpPacket) -> Result<ZrtpPacket, Error> {
        if packet.message_type() != ZrtpMessageType::Confirm1 {
            return Err(Error::InvalidMessage("Expected Confirm1 message".into()));
        }

        // Verify MAC
        if let (Some(shared_secret), Some(mac)) = (&self.shared_secret, packet.mac()) {
            let mut expected_mac = Hmac::<Sha256>::new_from_slice(shared_secret)
                .map_err(|_| Error::CryptoError("Failed to create HMAC".into()))?;

            if let Some(peer_zid) = &self.peer_zid {
                expected_mac.update(peer_zid);
            }
            expected_mac.update(&self.zid);

            expected_mac
                .verify_slice(&mac)
                .map_err(|_| Error::AuthenticationFailed("ZRTP MAC verification failed".into()))?;
        } else {
            return Err(Error::CryptoError("Missing shared secret or MAC".into()));
        }

        // Create Confirm2
        let mut confirm2 = ZrtpPacket::new(ZrtpMessageType::Confirm2);

        // Set ZID
        confirm2.set_zid(&self.zid);

        // Calculate HMAC
        if let Some(shared_secret) = &self.shared_secret {
            let mut mac = Hmac::<Sha256>::new_from_slice(shared_secret)
                .map_err(|_| Error::CryptoError("Failed to create HMAC".into()))?;

            mac.update(&self.zid);
            if let Some(peer_zid) = &self.peer_zid {
                mac.update(peer_zid);
            }

            let mac_result = mac.finalize().into_bytes();
            confirm2.set_mac(&mac_result);
        }

        // Update state
        self.state = ZrtpState::Confirm1Received;

        Ok(confirm2)
    }

    /// Process Confirm2
    fn process_confirm2(&mut self, packet: &ZrtpPacket) -> Result<ZrtpPacket, Error> {
        if packet.message_type() != ZrtpMessageType::Confirm2 {
            return Err(Error::InvalidMessage("Expected Confirm2 message".into()));
        }

        // Verify MAC
        if let (Some(shared_secret), Some(mac)) = (&self.shared_secret, packet.mac()) {
            let mut expected_mac = Hmac::<Sha256>::new_from_slice(shared_secret)
                .map_err(|_| Error::CryptoError("Failed to create HMAC".into()))?;

            if let Some(peer_zid) = &self.peer_zid {
                expected_mac.update(peer_zid);
            }
            expected_mac.update(&self.zid);

            expected_mac
                .verify_slice(&mac)
                .map_err(|_| Error::AuthenticationFailed("ZRTP MAC verification failed".into()))?;
        } else {
            return Err(Error::CryptoError("Missing shared secret or MAC".into()));
        }

        // Create Conf2Ack
        let conf2_ack = ZrtpPacket::new(ZrtpMessageType::Conf2Ack);

        // Update state
        self.state = ZrtpState::Confirm2Received;

        Ok(conf2_ack)
    }

    /// Process Conf2Ack
    fn process_conf2_ack(&mut self, packet: &ZrtpPacket) -> Result<(), Error> {
        if packet.message_type() != ZrtpMessageType::Conf2Ack {
            return Err(Error::InvalidMessage("Expected Conf2Ack message".into()));
        }

        // Update state
        self.state = ZrtpState::Completed;

        Ok(())
    }

    /// Derive SRTP keys from shared secret
    fn derive_srtp_keys(&mut self) -> Result<(), Error> {
        if let Some(shared_secret) = &self.shared_secret {
            // Derive initiator keys
            let mut hasher = Sha256::new();
            hasher.update(b"Initiator SRTP master key");
            hasher.update(shared_secret);
            let initiator_key_hash = hasher.finalize();

            let initiator_key = initiator_key_hash[0..16].to_vec();

            let mut hasher = Sha256::new();
            hasher.update(b"Initiator SRTP master salt");
            hasher.update(shared_secret);
            let initiator_salt_hash = hasher.finalize();

            let initiator_salt = initiator_salt_hash[0..14].to_vec();

            self.srtp_initiator_key = Some(SrtpCryptoKey::new(initiator_key, initiator_salt));

            // Derive responder keys
            let mut hasher = Sha256::new();
            hasher.update(b"Responder SRTP master key");
            hasher.update(shared_secret);
            let responder_key_hash = hasher.finalize();

            let responder_key = responder_key_hash[0..16].to_vec();

            let mut hasher = Sha256::new();
            hasher.update(b"Responder SRTP master salt");
            hasher.update(shared_secret);
            let responder_salt_hash = hasher.finalize();

            let responder_salt = responder_salt_hash[0..14].to_vec();

            self.srtp_responder_key = Some(SrtpCryptoKey::new(responder_key, responder_salt));

            Ok(())
        } else {
            Err(Error::CryptoError(
                "Missing shared secret for key derivation".into(),
            ))
        }
    }

    /// Generate SAS (Short Authentication String) for user verification
    pub fn generate_sas(&self) -> Result<String, Error> {
        if !self.is_complete() {
            return Err(Error::InvalidState("ZRTP exchange not complete".into()));
        }

        let shared_secret = self
            .shared_secret
            .as_ref()
            .ok_or_else(|| Error::CryptoError("No shared secret available".into()))?;

        let hello_hash_i = self
            .hello_hash
            .as_ref()
            .ok_or_else(|| Error::CryptoError("Missing local Hello hash".into()))?;

        let hello_hash_r = self
            .peer_hello_hash
            .as_ref()
            .ok_or_else(|| Error::CryptoError("Missing peer Hello hash".into()))?;

        // ZRTP SAS uses consistent ordering: shared_secret || Hello_hash_I || Hello_hash_R
        // Where I is always initiator and R is always responder (not local/peer)
        let mut sas_input = Vec::new();
        sas_input.extend_from_slice(shared_secret);

        // Use consistent ordering based on role
        match self.role {
            ZrtpRole::Initiator => {
                // For initiator: local=I, peer=R
                sas_input.extend_from_slice(hello_hash_i); // Our hello = initiator
                sas_input.extend_from_slice(hello_hash_r); // Peer hello = responder
            }
            ZrtpRole::Responder => {
                // For responder: peer=I, local=R
                sas_input.extend_from_slice(hello_hash_r); // Peer hello = initiator
                sas_input.extend_from_slice(hello_hash_i); // Our hello = responder
            }
        }

        // Hash the SAS input
        let mut hasher = Sha256::new();
        hasher.update(&sas_input);
        let sas_hash = hasher.finalize();

        // Generate SAS based on selected type
        let sas_type = self
            .selected_sas_type
            .ok_or_else(|| Error::InvalidState("No SAS type selected".into()))?;

        match sas_type {
            ZrtpSasType::B32 => {
                // Base 32 encoding - 4 characters, 20 bits from hash
                let sas_value =
                    u32::from_be_bytes([sas_hash[0], sas_hash[1], sas_hash[2], sas_hash[3]])
                        & 0x000FFFFF; // 20 bits

                // Convert to base 32 (A-Z, 2-7)
                let charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
                let mut sas = String::new();
                let mut value = sas_value;

                for _ in 0..4 {
                    let index = (value & 0x1F) as usize;
                    sas.insert(0, charset.chars().nth(index).unwrap());
                    value >>= 5;
                }

                Ok(sas)
            }
            ZrtpSasType::B32E => {
                // Base 32 extended - use first 20 bits for 4-character SAS
                let sas_value =
                    u32::from_be_bytes([sas_hash[0], sas_hash[1], sas_hash[2], sas_hash[3]])
                        & 0x000FFFFF; // 20 bits

                // Convert to decimal for user display
                Ok(format!("{:04}", sas_value % 10000))
            }
        }
    }

    /// Verify SAS matches what the user sees on both endpoints
    pub fn verify_sas(&self, user_sas: &str) -> Result<bool, Error> {
        let generated_sas = self.generate_sas()?;
        Ok(generated_sas.eq_ignore_ascii_case(user_sas))
    }

    /// Get human-readable SAS display for the user
    pub fn get_sas_display(&self) -> Result<String, Error> {
        let sas = self.generate_sas()?;
        let sas_type = self
            .selected_sas_type
            .ok_or_else(|| Error::InvalidState("No SAS type selected".into()))?;

        match sas_type {
            ZrtpSasType::B32 => Ok(format!(
                "SAS: {} (Read aloud: \"{}\")",
                sas,
                sas.chars()
                    .map(|c| c.to_string())
                    .collect::<Vec<String>>()
                    .join(" ")
            )),
            ZrtpSasType::B32E => Ok(format!(
                "SAS: {} (Read as: \"{}\")",
                sas,
                sas.chars()
                    .map(|c| c.to_string())
                    .collect::<Vec<String>>()
                    .join(" ")
            )),
        }
    }
}

impl SecurityKeyExchange for Zrtp {
    fn init(&mut self) -> Result<(), Error> {
        match self.role {
            ZrtpRole::Initiator => {
                // Initiator creates and sends Hello
                let _ = self.create_hello()?;
                Ok(())
            }
            ZrtpRole::Responder => {
                // Responder waits for Hello
                Ok(())
            }
        }
    }

    fn process_message(&mut self, message: &[u8]) -> Result<Option<Vec<u8>>, Error> {
        // Parse ZRTP packet
        let packet = ZrtpPacket::parse(message)
            .map_err(|_| Error::ParseError("Failed to parse ZRTP packet".into()))?;

        let response = match (self.role, &self.state) {
            (ZrtpRole::Initiator, ZrtpState::HelloSent) => {
                // Process Hello response
                if packet.message_type() == ZrtpMessageType::Hello {
                    // Process peer's Hello
                    let hello_ack = self.process_hello(&packet)?;
                    self.state = ZrtpState::HelloAckSent;
                    Ok(Some(hello_ack.to_bytes()))
                } else {
                    Err(Error::InvalidMessage("Expected Hello message".into()))
                }
            }
            (ZrtpRole::Initiator, ZrtpState::HelloAckSent) => {
                // Process Commit from responder
                let dh_part1 = self.process_commit(&packet)?;
                self.state = ZrtpState::DhPart1Sent;
                Ok(Some(dh_part1.to_bytes()))
            }
            (ZrtpRole::Initiator, ZrtpState::DhPart1Sent) => {
                // Process DH Part 2 from responder
                let confirm1 = self.process_dh_part2(&packet)?;
                self.state = ZrtpState::Confirm1Sent;
                Ok(Some(confirm1.to_bytes()))
            }
            (ZrtpRole::Initiator, ZrtpState::Confirm1Sent) => {
                // Process Confirm 2 from responder
                let conf2_ack = self.process_confirm2(&packet)?;
                self.state = ZrtpState::ConfirmAckSent;
                Ok(Some(conf2_ack.to_bytes()))
            }
            (ZrtpRole::Responder, ZrtpState::Initial) => {
                // Process Hello from initiator
                let hello = self.create_hello()?;
                let hello_bytes = hello.to_bytes();

                // Also process Hello message from initiator
                let hello_ack = self.process_hello(&packet)?;
                let hello_ack_bytes = hello_ack.to_bytes();

                // Combine responses
                let mut combined = Vec::new();
                combined.extend_from_slice(&hello_bytes);
                combined.extend_from_slice(&hello_ack_bytes);

                Ok(Some(combined))
            }
            (ZrtpRole::Responder, ZrtpState::HelloReceived) => {
                // Process Hello ACK from initiator
                let commit = self.process_hello_ack(&packet)?;
                self.state = ZrtpState::CommitSent;
                Ok(Some(commit.to_bytes()))
            }
            (ZrtpRole::Responder, ZrtpState::CommitSent) => {
                // Process DH Part 1 from initiator
                let dh_part2 = self.process_dh_part1(&packet)?;
                self.state = ZrtpState::DhPart2Sent;
                Ok(Some(dh_part2.to_bytes()))
            }
            (ZrtpRole::Responder, ZrtpState::DhPart2Sent) => {
                // Process Confirm 1 from initiator
                let confirm2 = self.process_confirm1(&packet)?;
                self.state = ZrtpState::Confirm2Sent;
                Ok(Some(confirm2.to_bytes()))
            }
            (ZrtpRole::Responder, ZrtpState::Confirm2Sent) => {
                // Process Confirm ACK from initiator
                self.process_conf2_ack(&packet)?;
                self.state = ZrtpState::Completed;
                Ok(None)
            }
            _ => Err(Error::InvalidState(format!(
                "Invalid state {:?} for message processing",
                self.state
            ))),
        };

        response
    }

    fn get_srtp_key(&self) -> Option<SrtpCryptoKey> {
        match self.role {
            ZrtpRole::Initiator => self.srtp_initiator_key.clone(),
            ZrtpRole::Responder => self.srtp_responder_key.clone(),
        }
    }

    fn get_srtp_suite(&self) -> Option<SrtpCryptoSuite> {
        Some(self.config.srtp_profile.clone())
    }

    fn is_complete(&self) -> bool {
        self.state == ZrtpState::Completed
    }
}

#[cfg(test)]
mod tests;