aria2-protocol 0.3.6

Multi-protocol networking stack for aria2-rust: HTTP/HTTPS client, FTP/SFTP, full BitTorrent (DHT/PEX/MSE), and Metalink V3/V4 parser
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
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
//! MSE (Message Stream Encryption) handshake state machine.
//!
//! Matches C++ aria2 `MSEHandshake.cc` 4-step wire format:
//!
//! | Step | Direction | Payload |
//! |------|-----------|---------|
//! | 1    | I → R     | `YA \|\| PadA` (96 + 0..512 bytes) |
//! | 2    | R → I     | `YB \|\| PadB` (96 + 0..512 bytes) |
//! | 3    | I → R     | `Hash('req1',S) \|\| [Hash('req2',SKEY) XOR Hash('req3',S)] \|\| E(VC \|\| crypto_provide \|\| len(PadC) \|\| PadC \|\| len(IA) \|\| IA)` |
//! | 4    | R → I     | `E(VC \|\| crypto_select \|\| len(PadD) \|\| PadD)` |
//!
//! Where:
//! - `S` = DH shared secret (96 bytes)
//! - `SKEY` = info_hash (20 bytes)
//! - `VC` = 8 zero bytes, RC4-encrypted
//! - `E(...)` = RC4-encrypted with the initiator's encryptor key (keyA for initiator, keyB for receiver)
//! - `crypto_provide` / `crypto_select` = 4-byte big-endian bitmask

use super::mse_crypto::{
    MseCryptoMethod, MseCryptoState, MseDerivedKeys, Rc4State, compute_vc, init_rc4,
};
use super::mse_dh::{
    CRYPTO_BITFIELD_LENGTH, INFO_HASH_LENGTH, KEY_LENGTH, MAX_PAD_LENGTH, MseDhKeyExchange,
    SHA1_LENGTH, VC_LENGTH,
};
use rand::Rng;

/// Synchronization limit for the initiator: 616 bytes max before finding VC marker.
const INITIATOR_SYNC_LIMIT: usize = 616;

/// Synchronization limit for the receiver: 628 bytes max before finding req1 marker.
const RECEIVER_SYNC_LIMIT: usize = 628;

/// Public wire limits used by the asynchronous incoming-peer adapter.
pub const MSE_PUBLIC_KEY_LENGTH: usize = KEY_LENGTH;
pub const MSE_MAX_BUFFER_LENGTH: usize = 636;
/// Maximum absolute size of the incoming step-2 wire payload before the
/// incremental adapter has consumed its PadA and encrypted padding fields.
pub const MSE_MAX_INCOMING_HANDSHAKE_LENGTH: usize = MAX_PAD_LENGTH
    + SHA1_LENGTH
    + SHA1_LENGTH
    + VC_LENGTH
    + CRYPTO_BITFIELD_LENGTH
    + 2
    + MAX_PAD_LENGTH
    + 2
    + 68;

/// Handshake phase tracking.
#[derive(Debug, Clone, PartialEq)]
pub enum MseHandshakePhase {
    /// Initial state; DH keys generated but nothing sent/received.
    Idle,
    /// Public key sent; waiting for remote public key.
    PublicKeySent,
    /// Remote public key received; shared secret computed.
    PublicKeyReceived,
    /// Initiator step2 sent (req1 + req2^req3 + encrypted payload).
    InitiatorStep2Sent,
    /// Handshake completed successfully.
    Completed(MseCryptoMethod),
    /// Handshake failed.
    Failed(String),
}

/// MSE handshake state machine implementing the C++ 4-step protocol.
pub struct MseHandshake {
    phase: MseHandshakePhase,
    dh: MseDhKeyExchange,
    /// Whether this side initiated the connection.
    initiator: bool,
    /// The info_hash for the torrent being downloaded.
    info_hash: [u8; INFO_HASH_LENGTH],
    /// DH shared secret S (96 bytes, fixed size).
    shared_secret: Option<[u8; KEY_LENGTH]>,
    /// Derived keys (set after shared secret is computed).
    keys: Option<MseDerivedKeys>,
    /// Negotiated crypto method.
    negotiated_method: MseCryptoMethod,
    /// Whether force encryption is required (no plaintext fallback).
    force_encryption: bool,
    /// Minimum crypto level: true = require RC4, false = allow plaintext.
    prefer_encryption: bool,
    /// Initiator's pre-computed VC marker (8 bytes), used for searching.
    /// Matches C++ `initiatorVCMarker_`.
    initiator_vc_marker: Option<[u8; VC_LENGTH]>,
    /// Encryptor RC4 state for the initiator role (keyA-based).
    /// Used during handshake for step 3 encryption and step 4 decryption.
    initiator_encryptor: Option<Rc4State>,
    /// Decryptor RC4 state for the initiator role (keyB-based).
    /// Used during handshake for VC marker computation.
    initiator_decryptor: Option<Rc4State>,
    /// Receiver-side stream states, transferred to the post-handshake peer.
    receiver_encryptor: Option<Rc4State>,
    receiver_decryptor: Option<Rc4State>,
}

impl MseHandshake {
    /// Create a new initiator handshake.
    pub fn new_initiator(info_hash: [u8; INFO_HASH_LENGTH]) -> Self {
        MseHandshake {
            phase: MseHandshakePhase::Idle,
            dh: MseDhKeyExchange::new(),
            initiator: true,
            info_hash,
            shared_secret: None,
            keys: None,
            negotiated_method: MseCryptoMethod::Plain,
            force_encryption: false,
            prefer_encryption: true,
            initiator_vc_marker: None,
            initiator_encryptor: None,
            initiator_decryptor: None,
            receiver_encryptor: None,
            receiver_decryptor: None,
        }
    }

    /// Create a new responder handshake.
    pub fn new_responder(info_hash: [u8; INFO_HASH_LENGTH]) -> Self {
        MseHandshake {
            phase: MseHandshakePhase::Idle,
            dh: MseDhKeyExchange::new(),
            initiator: false,
            info_hash,
            shared_secret: None,
            keys: None,
            negotiated_method: MseCryptoMethod::Plain,
            force_encryption: false,
            prefer_encryption: true,
            initiator_vc_marker: None,
            initiator_encryptor: None,
            initiator_decryptor: None,
            receiver_encryptor: None,
            receiver_decryptor: None,
        }
    }

    /// Set encryption preferences.
    pub fn set_crypto_preferences(&mut self, force_encryption: bool, prefer_encryption: bool) {
        self.force_encryption = force_encryption;
        self.prefer_encryption = prefer_encryption;
    }

    /// Get the current handshake phase.
    pub fn phase(&self) -> &MseHandshakePhase {
        &self.phase
    }

    /// Get the info_hash.
    pub fn info_hash(&self) -> &[u8; INFO_HASH_LENGTH] {
        &self.info_hash
    }

    /// Get the negotiated crypto method (valid after completion).
    pub fn negotiated_method(&self) -> MseCryptoMethod {
        self.negotiated_method
    }

    // ── Step 1: Public key exchange ──────────────────────────────────

    /// Build the step 1 payload: `YA || PadA` (initiator) or `YB || PadB` (responder).
    ///
    /// Matches C++ `MSEHandshake::sendPublicKey()`.
    pub fn build_step1(&self) -> Vec<u8> {
        let mut rng = rand::thread_rng();
        let pad_length: usize = rng.gen_range(0..=MAX_PAD_LENGTH);
        let mut buf = Vec::with_capacity(KEY_LENGTH + pad_length);

        // Public key (96 bytes)
        buf.extend_from_slice(&self.dh.generate_public_key());

        // Random padding (0..512 bytes)
        let mut pad = vec![0u8; pad_length];
        rng.fill(&mut pad[..]);
        buf.extend_from_slice(&pad);

        buf
    }

    /// Process the received step 1 payload (remote public key + padding).
    ///
    /// Extracts the remote public key (first KEY_LENGTH bytes) and computes
    /// the shared secret. Matches C++ `MSEHandshake::receivePublicKey()`.
    pub fn receive_step1(&mut self, data: &[u8]) -> Result<(), String> {
        if data.len() < KEY_LENGTH {
            return Err(format!(
                "Step1 data too short: got {} bytes, need at least {}",
                data.len(),
                KEY_LENGTH
            ));
        }

        // The remote public key is the first KEY_LENGTH bytes
        let mut remote_public = [0u8; KEY_LENGTH];
        remote_public.copy_from_slice(&data[..KEY_LENGTH]);

        // Compute shared secret
        let shared = self.dh.compute_shared_secret(&remote_public);
        self.shared_secret = Some(shared);

        // Derive keys using the shared secret and our info_hash
        let keys = MseDerivedKeys::derive(&shared, &self.info_hash);
        self.keys = Some(keys);

        // If initiator, pre-compute the VC marker for step 4 search.
        // Matches C++ initCipher() lines 209-215:
        //   ARC4Encryptor enc;
        //   enc.init(peerCipherKey, sizeof(peerCipherKey));
        //   enc.encrypt(1024 discard);
        //   enc.encrypt(VC_LENGTH, initiatorVCMarker_, VC.data());
        if self.initiator {
            let keys = self.keys.as_ref().expect("keys just set");
            // The initiator's encryptor uses keyA (for sending step 3)
            self.initiator_encryptor = Some(init_rc4(&keys.key_a));
            // The initiator's decryptor uses keyB (for receiving step 4)
            let mut marker_cipher = init_rc4(&keys.key_b);
            // Compute VC marker = RC4(zeros) using peer cipher
            let vc_marker = compute_vc(&mut marker_cipher);
            self.initiator_vc_marker = Some(vc_marker);
            // Keep a separate cipher at the post-discard position. The VC
            // marker is only a look-ahead value and must not consume the
            // cipher used to decrypt the receiver response.
            self.initiator_decryptor = Some(init_rc4(&keys.key_b));
        }

        self.phase = MseHandshakePhase::PublicKeyReceived;
        Ok(())
    }

    // ── Step 3 (initiator): Send negotiation ─────────────────────────

    /// Build the initiator's step 3 payload.
    ///
    /// Format: `Hash('req1', S) || [Hash('req2', SKEY) XOR Hash('req3', S)] || E(VC || crypto_provide || len(PadC) || PadC || len(IA) || IA)`
    ///
    /// Matches C++ `MSEHandshake::sendInitiatorStep2()`.
    pub fn build_initiator_step2(&mut self) -> Result<Vec<u8>, String> {
        if !self.initiator {
            return Err("Only initiator can build step 3".to_string());
        }
        let keys = self.keys.as_ref().ok_or("Keys not derived yet")?;

        let mut buf = Vec::new();

        // Hash('req1', S) — 20 bytes, plaintext
        buf.extend_from_slice(&keys.req1);

        // [Hash('req2', SKEY) XOR Hash('req3', S)] — 20 bytes, plaintext
        buf.extend_from_slice(&keys.req2_xor_req3());

        // Encrypted payload: VC(8) + crypto_provide(4) + len(PadC)(2) + PadC + len(IA)(2) + IA
        let mut rng = rand::thread_rng();
        let pad_c_length: u16 = rng.gen_range(0..=MAX_PAD_LENGTH as u16);
        let ia_length: u16 = 0; // No initial data currently

        let mut encrypted =
            Vec::with_capacity(VC_LENGTH + CRYPTO_BITFIELD_LENGTH + 2 + pad_c_length as usize + 2);

        // VC = 8 zero bytes (will be encrypted by the cipher)
        encrypted.extend_from_slice(&[0u8; VC_LENGTH]);

        // crypto_provide = 4-byte big-endian bitmask
        let mut crypto_provide: u32 = 0;
        if !self.force_encryption && !self.prefer_encryption {
            crypto_provide |= MseCryptoMethod::Plain.as_u32();
        }
        crypto_provide |= MseCryptoMethod::Rc4.as_u32();
        encrypted.extend_from_slice(&crypto_provide.to_be_bytes());

        // len(PadC) — 2 bytes big-endian
        encrypted.extend_from_slice(&pad_c_length.to_be_bytes());

        // PadC — random bytes
        let mut pad_c = vec![0u8; pad_c_length as usize];
        rng.fill(&mut pad_c[..]);
        encrypted.extend_from_slice(&pad_c);

        // len(IA) — 2 bytes big-endian
        encrypted.extend_from_slice(&ia_length.to_be_bytes());

        // IA — currently empty (ia_length = 0)

        // Encrypt with the initiator's encryptor (keyA)
        if let Some(ref mut cipher) = self.initiator_encryptor {
            cipher.process(&mut encrypted);
        } else {
            return Err("Initiator encryptor not initialized".to_string());
        }

        buf.extend_from_slice(&encrypted);

        self.phase = MseHandshakePhase::InitiatorStep2Sent;
        Ok(buf)
    }

    // ── Step 3 (receiver): Process initiator's step 3 ────────────────

    /// Process the initiator's step 3 payload (receiver side).
    ///
    /// The receiver must find the req1 hash marker, verify req2^req3
    /// against known info_hashes, then decrypt the encrypted payload.
    ///
    /// Returns the crypto method provided by the initiator.
    ///
    /// Matches C++ `findReceiverHashMarker()` + `receiveReceiverHashAndPadCLength()`.
    pub fn receive_initiator_step2(
        &mut self,
        data: &[u8],
        known_info_hashes: &[[u8; INFO_HASH_LENGTH]],
    ) -> Result<MseCryptoMethod, String> {
        if self.initiator {
            return Err("Only receiver can process initiator step 3".to_string());
        }

        // Minimum size: req1(20) + req2^req3(20) + VC(8) + crypto_provide(4) + len(PadC)(2) = 54
        if data.len() < SHA1_LENGTH + SHA1_LENGTH + VC_LENGTH + CRYPTO_BITFIELD_LENGTH + 2 {
            return Err(format!(
                "Initiator step3 data too short: {} bytes",
                data.len()
            ));
        }

        // Step 1: Find req1 hash marker in the data.
        // In C++ this uses std::search with a sync limit of 628 bytes.
        let keys = self.keys.as_ref().ok_or("Keys not derived yet")?;

        let req1_match = find_req1_marker(data, &keys.req1, RECEIVER_SYNC_LIMIT)?;
        let req1_end = req1_match + SHA1_LENGTH;

        // Step 2: Verify req2^req3 to identify the info_hash.
        // This allows multi-torrent support where the receiver doesn't know
        // which info_hash the initiator is using.
        let req2_xor_req3 = &data[req1_end..req1_end + SHA1_LENGTH];

        let matched_info_hash = verify_req2_xor_req3(req2_xor_req3, known_info_hashes, &keys.req3);

        // If we already know the info_hash, verify it matches
        let verified_info_hash = match matched_info_hash {
            Some(hash) => hash,
            None => {
                // Try our own info_hash
                let expected_xor = {
                    let mut expected = [0u8; SHA1_LENGTH];
                    for (expected, (&req2, &req3)) in expected
                        .iter_mut()
                        .zip(keys.req2.iter().zip(keys.req3.iter()))
                    {
                        *expected = req2 ^ req3;
                    }
                    expected
                };
                if req2_xor_req3 == expected_xor {
                    self.info_hash
                } else {
                    return Err("Unknown info hash: req2^req3 verification failed".to_string());
                }
            }
        };

        // If the matched info_hash differs from ours, update
        if verified_info_hash != self.info_hash {
            self.info_hash = verified_info_hash;
            // Re-derive keys with the correct info_hash
            let shared = self.shared_secret.ok_or("Shared secret not computed")?;
            let new_keys = MseDerivedKeys::derive(&shared, &verified_info_hash);
            self.keys = Some(new_keys);
        }

        let keys = self.keys.as_ref().expect("keys just set");

        // Step 3: Decrypt the encrypted payload.
        // The receiver's decryptor uses keyA (initiator's encryptor key).
        // Matches C++ initCipher() for receiver: decryptor = keyA.
        let mut decryptor = init_rc4(&keys.key_a);

        let encrypted_start = req1_end + SHA1_LENGTH;
        let encrypted_data = &data[encrypted_start..];

        if encrypted_data.len() < VC_LENGTH + CRYPTO_BITFIELD_LENGTH + 2 {
            return Err("Encrypted payload too short".to_string());
        }

        // Decrypt the entire encrypted portion
        let mut decrypted = encrypted_data.to_vec();
        decryptor.process(&mut decrypted);
        self.receiver_decryptor = Some(decryptor);

        // Step 4: Verify VC (should be 8 zero bytes after decryption)
        let vc = &decrypted[..VC_LENGTH];
        if vc != [0u8; VC_LENGTH] {
            return Err(format!(
                "VC verification failed: expected zeros, got {:02X?}",
                &vc[..8.min(vc.len())]
            ));
        }

        // Step 5: Read crypto_provide
        let crypto_provide =
            u32::from_be_bytes([decrypted[8], decrypted[9], decrypted[10], decrypted[11]]);

        // Determine negotiated method
        if !self.prefer_encryption
            && !self.force_encryption
            && (crypto_provide & MseCryptoMethod::Plain.as_u32()) != 0
        {
            self.negotiated_method = MseCryptoMethod::Plain;
        } else if (crypto_provide & MseCryptoMethod::Rc4.as_u32()) != 0 {
            self.negotiated_method = MseCryptoMethod::Rc4;
        } else {
            return Err(format!(
                "No supported crypto method in provide: {:#010X}",
                crypto_provide
            ));
        }

        self.phase = MseHandshakePhase::Completed(self.negotiated_method);
        Ok(self.negotiated_method)
    }

    /// Return the complete receiver-side step-2 length once PadC and IA
    /// lengths are available, or `None` while more bytes are needed.
    ///
    /// The shared process listener uses this incremental seam before routing
    /// an encrypted connection. MSE deliberately hides the torrent identity
    /// until `req2 ^ req3` is verified against the active route catalog.
    pub fn receiver_step2_required_len(&self, data: &[u8]) -> Result<Option<usize>, String> {
        if self.initiator {
            return Err("Only receiver can inspect initiator step 2".to_string());
        }
        let keys = self.keys.as_ref().ok_or("Keys not derived yet")?;
        let Some(req1_match) = find_marker_if_present(data, &keys.req1) else {
            if data.len() >= RECEIVER_SYNC_LIMIT {
                return Err("Failed to find req1 hash marker within sync limit".to_string());
            }
            return Ok(None);
        };
        let encrypted_start = req1_match + SHA1_LENGTH + SHA1_LENGTH;
        let header_len = VC_LENGTH + CRYPTO_BITFIELD_LENGTH + 2;
        if data.len() < encrypted_start + header_len {
            return Ok(None);
        }

        let mut header = data[encrypted_start..encrypted_start + header_len].to_vec();
        let mut decryptor = init_rc4(&keys.key_a);
        decryptor.process(&mut header);
        if header[..VC_LENGTH] != [0u8; VC_LENGTH] {
            return Err("VC verification failed".to_string());
        }
        let pad_c_length = u16::from_be_bytes([
            header[VC_LENGTH + CRYPTO_BITFIELD_LENGTH],
            header[VC_LENGTH + CRYPTO_BITFIELD_LENGTH + 1],
        ]) as usize;
        let ia_length_offset = encrypted_start + header_len + pad_c_length;
        if data.len() < ia_length_offset + 2 {
            return Ok(None);
        }

        let mut ia_length = [0u8; 2];
        let mut length_prefix = data[encrypted_start..ia_length_offset + 2].to_vec();
        let mut decryptor = init_rc4(&keys.key_a);
        decryptor.process(&mut length_prefix);
        ia_length.copy_from_slice(
            &length_prefix
                [ia_length_offset - encrypted_start..ia_length_offset - encrypted_start + 2],
        );
        let ia_length = u16::from_be_bytes(ia_length) as usize;
        Ok(Some(ia_length_offset + 2 + ia_length))
    }

    /// Identify the concealed torrent identity in a receiver-side MSE
    /// buffer. The caller can use this before the encrypted payload is fully
    /// received, then call [`Self::set_info_hash`] to derive the final keys.
    pub fn receiver_info_hash(
        &self,
        data: &[u8],
        known_info_hashes: &[[u8; INFO_HASH_LENGTH]],
    ) -> Result<Option<[u8; INFO_HASH_LENGTH]>, String> {
        if self.initiator {
            return Err("Only receiver can identify an incoming info hash".to_string());
        }
        let keys = self.keys.as_ref().ok_or("Keys not derived yet")?;
        let Some(req1_match) = find_marker_if_present(data, &keys.req1) else {
            return Ok(None);
        };
        let xor_start = req1_match + SHA1_LENGTH;
        if data.len() < xor_start + SHA1_LENGTH {
            return Ok(None);
        }
        Ok(verify_req2_xor_req3(
            &data[xor_start..xor_start + SHA1_LENGTH],
            known_info_hashes,
            &keys.req3,
        ))
    }

    /// Select the torrent identity discovered from `req2 ^ req3` and derive
    /// the receiver-side MSE keys for the remainder of the handshake.
    pub fn set_info_hash(&mut self, info_hash: [u8; INFO_HASH_LENGTH]) -> Result<(), String> {
        if self.initiator {
            return Err("Only receiver can set an incoming info hash".to_string());
        }
        let shared = self.shared_secret.ok_or("Shared secret not computed")?;
        self.info_hash = info_hash;
        self.keys = Some(MseDerivedKeys::derive(&shared, &info_hash));
        Ok(())
    }

    // ── Step 4 (receiver): Send response ─────────────────────────────

    /// Build the receiver's step 4 response.
    ///
    /// Format: `E(VC || crypto_select || len(PadD) || PadD)`
    ///
    /// Matches C++ `MSEHandshake::sendReceiverStep2()`.
    pub fn build_receiver_step2(&mut self) -> Result<Vec<u8>, String> {
        if self.initiator {
            return Err("Only receiver can build step 4".to_string());
        }
        if !matches!(self.phase, MseHandshakePhase::Completed(_)) {
            return Err("Handshake not completed yet".to_string());
        }

        let keys = self.keys.as_ref().ok_or("Keys not derived yet")?;

        let mut rng = rand::thread_rng();
        let pad_d_length: u16 = rng.gen_range(0..=MAX_PAD_LENGTH as u16);

        let mut encrypted =
            Vec::with_capacity(VC_LENGTH + CRYPTO_BITFIELD_LENGTH + 2 + pad_d_length as usize);

        // VC = 8 zero bytes
        encrypted.extend_from_slice(&[0u8; VC_LENGTH]);

        // crypto_select = 4-byte big-endian bitmask
        encrypted.extend_from_slice(&self.negotiated_method.as_u32().to_be_bytes());

        // len(PadD) = 2 bytes big-endian
        encrypted.extend_from_slice(&pad_d_length.to_be_bytes());

        // PadD = random bytes
        let mut pad_d = vec![0u8; pad_d_length as usize];
        rng.fill(&mut pad_d[..]);
        encrypted.extend_from_slice(&pad_d);

        // Encrypt with the receiver's encryptor (keyB for receiver)
        let cipher = self
            .receiver_encryptor
            .get_or_insert_with(|| init_rc4(&keys.key_b));
        cipher.process(&mut encrypted);

        Ok(encrypted)
    }

    // ── Step 4 (initiator): Process receiver's response ──────────────

    /// Process the receiver's step 4 response (initiator side).
    ///
    /// The initiator must find the VC marker, then decrypt crypto_select and PadD.
    ///
    /// Matches C++ `findInitiatorVCMarker()` + `receiveInitiatorCryptoSelectAndPadDLength()`.
    pub fn receive_receiver_step2(&mut self, data: &[u8]) -> Result<MseCryptoMethod, String> {
        if !self.initiator {
            return Err("Only initiator can process receiver step 4".to_string());
        }

        let vc_marker = self.initiator_vc_marker.ok_or("VC marker not computed")?;

        // Find the VC marker in the data
        let vc_pos = find_vc_marker(data, &vc_marker, INITIATOR_SYNC_LIMIT)?;

        // Consume and verify the VC with the main decryptor before decoding the
        // following fields. The marker cipher is only a look-ahead cipher.
        let mut vc = [0u8; VC_LENGTH];
        vc.copy_from_slice(&data[vc_pos..vc_pos + VC_LENGTH]);
        if let Some(ref mut cipher) = self.initiator_decryptor {
            cipher.process(&mut vc);
        } else {
            return Err("Initiator decryptor not initialized".to_string());
        }
        if vc != [0u8; VC_LENGTH] {
            return Err("VC verification failed".to_string());
        }

        // Decrypt everything after the VC marker using the same decryptor.
        let encrypted_start = vc_pos + VC_LENGTH;
        if data.len() < encrypted_start + CRYPTO_BITFIELD_LENGTH + 2 {
            return Err(format!(
                "Receiver step4 data too short after VC: {} bytes",
                data.len() - encrypted_start
            ));
        }

        let mut remaining = data[encrypted_start..].to_vec();
        if let Some(ref mut cipher) = self.initiator_decryptor {
            cipher.process(&mut remaining);
        } else {
            return Err("Initiator decryptor not initialized".to_string());
        }

        // Read crypto_select (4 bytes big-endian)
        let crypto_select =
            u32::from_be_bytes([remaining[0], remaining[1], remaining[2], remaining[3]]);

        // Determine negotiated method
        if (crypto_select & MseCryptoMethod::Plain.as_u32()) != 0
            && !self.force_encryption
            && !self.prefer_encryption
        {
            self.negotiated_method = MseCryptoMethod::Plain;
        } else if (crypto_select & MseCryptoMethod::Rc4.as_u32()) != 0 {
            self.negotiated_method = MseCryptoMethod::Rc4;
        } else {
            return Err(format!(
                "No supported crypto method in select: {:#010X}",
                crypto_select
            ));
        }

        // Read PadD length (2 bytes big-endian)
        if remaining.len() < CRYPTO_BITFIELD_LENGTH + 2 {
            return Err("Not enough data for PadD length".to_string());
        }
        let _pad_d_length = u16::from_be_bytes([remaining[4], remaining[5]]);
        if _pad_d_length as usize > MAX_PAD_LENGTH {
            return Err(format!("PadD length too large: {}", _pad_d_length));
        }

        self.phase = MseHandshakePhase::Completed(self.negotiated_method);
        Ok(self.negotiated_method)
    }

    /// Return the exact receiver response length once PadD is available.
    ///
    /// The response can be preceded by responder padding, so callers must
    /// synchronize on the encrypted VC marker before decoding the encrypted
    /// PadD length. This keeps the following BitTorrent handshake on the
    /// stream for the next protocol phase.
    pub fn initiator_step2_required_len(&self, data: &[u8]) -> Result<Option<usize>, String> {
        if !self.initiator {
            return Err("Only initiator can inspect receiver step 2".to_string());
        }
        let vc_marker = self.initiator_vc_marker.ok_or("VC marker not computed")?;
        let Some(vc_pos) = find_marker_if_present(data, &vc_marker) else {
            if data.len() >= INITIATOR_SYNC_LIMIT {
                return Err("Failed to find VC marker within sync limit".to_string());
            }
            return Ok(None);
        };
        let header_start = vc_pos + VC_LENGTH;
        if data.len() < header_start + CRYPTO_BITFIELD_LENGTH + 2 {
            return Ok(None);
        }

        let keys = self.keys.as_ref().ok_or("Keys not derived yet")?;
        let mut decryptor = init_rc4(&keys.key_b);
        let mut vc = [0u8; VC_LENGTH];
        decryptor.process(&mut vc);
        let mut header = data[header_start..header_start + CRYPTO_BITFIELD_LENGTH + 2].to_vec();
        decryptor.process(&mut header);
        let pad_d_length = u16::from_be_bytes([
            header[CRYPTO_BITFIELD_LENGTH],
            header[CRYPTO_BITFIELD_LENGTH + 1],
        ]) as usize;
        Ok(Some(
            header_start + CRYPTO_BITFIELD_LENGTH + 2 + pad_d_length,
        ))
    }

    // ── Finalize: Extract crypto state ───────────────────────────────

    /// Finalize the handshake and return the ongoing crypto state.
    ///
    /// The returned `MseCryptoState` can be used for encrypting/decrypting
    /// all subsequent BT protocol messages.
    pub fn finalize(mut self) -> Result<MseCryptoState, String> {
        match self.phase {
            MseHandshakePhase::Completed(method) => match method {
                MseCryptoMethod::Plain => Ok(MseCryptoState::new_plain()),
                MseCryptoMethod::Rc4 if self.initiator => {
                    let send = self
                        .initiator_encryptor
                        .take()
                        .ok_or("Initiator encryptor not initialized")?;
                    let recv = self
                        .initiator_decryptor
                        .take()
                        .ok_or("Initiator decryptor not initialized")?;
                    Ok(MseCryptoState::from_rc4_states(
                        send,
                        recv,
                        MseCryptoMethod::Rc4,
                    ))
                }
                MseCryptoMethod::Rc4 => {
                    let send = self
                        .receiver_encryptor
                        .take()
                        .ok_or("Receiver encryptor not initialized")?;
                    let recv = self
                        .receiver_decryptor
                        .take()
                        .ok_or("Receiver decryptor not initialized")?;
                    Ok(MseCryptoState::from_rc4_states(
                        send,
                        recv,
                        MseCryptoMethod::Rc4,
                    ))
                }
            },
            MseHandshakePhase::Failed(e) => Err(e),
            _ => Err(format!("Handshake not completed: {:?}", self.phase)),
        }
    }

    // ── Utility ──────────────────────────────────────────────────────

    /// Determine whether MSE should be negotiated based on reserved bytes.
    ///
    /// Convention: `reserved[7] & 0x01` indicates MSE support.
    pub fn should_negotiate(local_supports_mse: bool, remote_reserved: &[u8]) -> bool {
        local_supports_mse && remote_reserved.len() >= 8 && (remote_reserved[7] & 0x01) != 0
    }

    /// Identify whether incoming data is a legacy BT handshake or encrypted.
    ///
    /// Matches C++ `MSEHandshake::identifyHandshakeType()`.
    pub fn identify_handshake_type(data: &[u8]) -> HandshakeType {
        if data.len() < 20 {
            return HandshakeType::NotYet;
        }
        // Legacy BT handshake: first byte = 19 (pstrlen), next 19 bytes = "BitTorrent protocol"
        if data[0] == 19 && &data[1..20] == b"BitTorrent protocol" {
            return HandshakeType::Legacy;
        }
        HandshakeType::Encrypted
    }
}

/// Result of identifying the handshake type from incoming data.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum HandshakeType {
    /// Not enough data yet.
    NotYet,
    /// Standard BT handshake detected.
    Legacy,
    /// Encrypted (MSE) handshake detected.
    Encrypted,
}

/// Search for the req1 hash marker in the data buffer.
///
/// Returns the byte offset where the marker starts.
/// Matches C++ `findReceiverHashMarker()`.
fn find_req1_marker(
    data: &[u8],
    req1_hash: &[u8; SHA1_LENGTH],
    sync_limit: usize,
) -> Result<usize, String> {
    if data.len() < SHA1_LENGTH {
        return Err("Data too short for req1 marker search".to_string());
    }

    // Search for the req1 hash in the data
    for i in 0..=(data.len().saturating_sub(SHA1_LENGTH)) {
        if data[i..].starts_with(req1_hash) {
            return Ok(i);
        }
        // Sync limit check
        if i + SHA1_LENGTH > sync_limit {
            return Err("Failed to find req1 hash marker within sync limit".to_string());
        }
    }

    Err("Failed to find req1 hash marker".to_string())
}

fn find_marker_if_present(data: &[u8], marker: &[u8]) -> Option<usize> {
    data.windows(marker.len())
        .position(|window| window == marker)
}

/// Find the VC marker in the receiver's response data.
///
/// Returns the byte offset where the VC marker starts.
/// Matches C++ `findInitiatorVCMarker()`.
fn find_vc_marker(
    data: &[u8],
    vc_marker: &[u8; VC_LENGTH],
    sync_limit: usize,
) -> Result<usize, String> {
    if data.len() < VC_LENGTH {
        return Err("Data too short for VC marker search".to_string());
    }

    for i in 0..=(data.len().saturating_sub(VC_LENGTH)) {
        if data[i..].starts_with(vc_marker) {
            return Ok(i);
        }
        // Sync limit check (adjusted for KEY_LENGTH offset as in C++)
        if i + VC_LENGTH > sync_limit - KEY_LENGTH {
            return Err("Failed to find VC marker within sync limit".to_string());
        }
    }

    Err("Failed to find VC marker".to_string())
}

/// Verify req2^req3 against a list of known info_hashes.
///
/// Returns the matching info_hash if found, or None.
/// Matches C++ `receiveReceiverHashAndPadCLength()`.
fn verify_req2_xor_req3(
    req2_xor_req3: &[u8],
    known_info_hashes: &[[u8; INFO_HASH_LENGTH]],
    req3: &[u8; SHA1_LENGTH],
) -> Option<[u8; INFO_HASH_LENGTH]> {
    for info_hash in known_info_hashes {
        // Compute Hash('req2', SKEY)
        let req2 = {
            use sha1::{Digest, Sha1};
            let mut hasher = Sha1::new();
            hasher.update(b"req2");
            hasher.update(info_hash);
            let result = hasher.finalize();
            let mut arr = [0u8; SHA1_LENGTH];
            arr.copy_from_slice(&result);
            arr
        };

        // Compute Hash('req2', SKEY) XOR Hash('req3', S)
        let mut expected = [0u8; SHA1_LENGTH];
        for i in 0..SHA1_LENGTH {
            expected[i] = req2[i] ^ req3[i];
        }

        if req2_xor_req3 == expected {
            return Some(*info_hash);
        }
    }
    None
}

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

    fn test_info_hash() -> [u8; INFO_HASH_LENGTH] {
        [
            0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54,
            0x32, 0x10, 0xAA, 0xBB, 0xCC, 0xDD,
        ]
    }

    // ── Step 1: Public key exchange ──────────────────────────────────

    #[test]
    fn test_step1_payload_size() {
        let h = MseHandshake::new_initiator(test_info_hash());
        let s1 = h.build_step1();
        // Must be at least 96 bytes (public key) and at most 96 + 512
        assert!(s1.len() >= KEY_LENGTH);
        assert!(s1.len() <= KEY_LENGTH + MAX_PAD_LENGTH);
    }

    #[test]
    fn test_receive_step1_too_short() {
        let mut h = MseHandshake::new_responder(test_info_hash());
        let result = h.receive_step1(&[0u8; 16]);
        assert!(result.is_err());
    }

    #[test]
    fn test_receive_step1_computes_shared_secret() {
        let initiator = MseHandshake::new_initiator(test_info_hash());
        let step1 = initiator.build_step1();

        let mut responder = MseHandshake::new_responder(test_info_hash());
        responder.receive_step1(&step1).expect("receive step1");

        assert!(responder.shared_secret.is_some());
        assert!(responder.keys.is_some());
    }

    // ── Full initiator-responder handshake ───────────────────────────

    #[test]
    fn test_full_handshake_rc4() {
        let info_hash = test_info_hash();

        // Create initiator and responder
        let mut initiator = MseHandshake::new_initiator(info_hash);
        let mut responder = MseHandshake::new_responder(info_hash);

        // Step 1: Exchange public keys
        let i_step1 = initiator.build_step1();
        let r_step1 = responder.build_step1();

        initiator
            .receive_step1(&r_step1)
            .expect("I receive R step1");
        responder
            .receive_step1(&i_step1)
            .expect("R receive I step1");

        // Verify both computed the same shared secret
        let i_secret = initiator.shared_secret.expect("I secret");
        let r_secret = responder.shared_secret.expect("R secret");
        assert_eq!(i_secret, r_secret, "Shared secrets must match");

        // Step 3: Initiator sends negotiation
        let i_step3 = initiator.build_initiator_step2().expect("I build step3");

        // Receiver processes initiator's step 3
        let method_r = responder
            .receive_initiator_step2(&i_step3, &[info_hash])
            .expect("R receive step3");
        assert_eq!(method_r, MseCryptoMethod::Rc4);

        // Step 4: Receiver sends response
        let r_step4 = responder.build_receiver_step2().expect("R build step4");

        // Initiator processes receiver's step 4
        let method_i = initiator
            .receive_receiver_step2(&r_step4)
            .expect("I receive step4");
        assert_eq!(method_i, MseCryptoMethod::Rc4);

        // Finalize both sides
        let mut crypto_i = initiator.finalize().expect("I finalize");
        let crypto_r = responder.finalize().expect("R finalize");

        assert!(crypto_i.is_encrypted());
        assert!(crypto_r.is_encrypted());

        // Verify encrypted communication works
        let original = b"Hello, BitTorrent MSE!";
        let mut encrypted = original.to_vec();
        crypto_i.encrypt(&mut encrypted);
        // Create a fresh pair since Clone loses cipher state
        let info_hash2 = test_info_hash();
        let mut initiator2 = MseHandshake::new_initiator(info_hash2);
        let mut responder2 = MseHandshake::new_responder(info_hash2);
        let i2_step1 = initiator2.build_step1();
        let r2_step1 = responder2.build_step1();
        initiator2
            .receive_step1(&r2_step1)
            .expect("I2 receive R2 step1");
        responder2
            .receive_step1(&i2_step1)
            .expect("R2 receive I2 step1");
        let i2_step3 = initiator2.build_initiator_step2().expect("I2 build step3");
        responder2
            .receive_initiator_step2(&i2_step3, &[test_info_hash()])
            .expect("R2 receive step3");
        let r2_step4 = responder2.build_receiver_step2().expect("R2 build step4");
        initiator2
            .receive_receiver_step2(&r2_step4)
            .expect("I2 receive step4");
        let mut crypto_i2 = initiator2.finalize().expect("I2 finalize");
        let mut crypto_r2 = responder2.finalize().expect("R2 finalize");

        let original2 = b"Test encrypted data";
        let mut enc2 = original2.to_vec();
        crypto_i2.encrypt(&mut enc2);
        assert_ne!(enc2, original2.to_vec());
        crypto_r2.decrypt(&mut enc2);
        assert_eq!(enc2, original2.to_vec());
    }

    #[test]
    fn test_receiver_response_with_plain_pad_b_prefix() {
        let info_hash = test_info_hash();
        let mut initiator = MseHandshake::new_initiator(info_hash);
        let mut responder = MseHandshake::new_responder(info_hash);
        let i_step1 = initiator.build_step1();
        let r_step1 = responder.build_step1();
        initiator.receive_step1(&r_step1).unwrap();
        responder.receive_step1(&i_step1).unwrap();
        let i_step3 = initiator.build_initiator_step2().unwrap();
        responder
            .receive_initiator_step2(&i_step3, &[info_hash])
            .unwrap();
        let r_step4 = responder.build_receiver_step2().unwrap();

        let mut buffered_response = vec![0xA5; 17];
        buffered_response.extend_from_slice(&r_step4);
        let required = initiator
            .initiator_step2_required_len(&buffered_response)
            .unwrap()
            .unwrap();
        assert_eq!(required, buffered_response.len());
        initiator
            .receive_receiver_step2(&buffered_response)
            .unwrap();
        let mut crypto_i = initiator.finalize().unwrap();
        let mut crypto_r = responder.finalize().unwrap();

        let mut handshake = [0x5Au8; 68];
        crypto_i.encrypt(&mut handshake);
        crypto_r.decrypt(&mut handshake);
        assert_eq!(handshake, [0x5Au8; 68]);

        let mut reverse = [0x6Bu8; 68];
        crypto_r.encrypt(&mut reverse);
        crypto_i.decrypt(&mut reverse);
        assert_eq!(reverse, [0x6Bu8; 68]);
    }

    #[test]
    fn test_full_wire_flow_retains_pad_a_and_pad_b() {
        let info_hash = test_info_hash();
        let mut initiator = MseHandshake::new_initiator(info_hash);
        let mut responder = MseHandshake::new_responder([0u8; 20]);
        let initiator_step1 = initiator.build_step1();
        let responder_step1 = responder.build_step1();

        initiator
            .receive_step1(&responder_step1[..KEY_LENGTH])
            .unwrap();
        responder
            .receive_step1(&initiator_step1[..KEY_LENGTH])
            .unwrap();

        let initiator_step3 = initiator.build_initiator_step2().unwrap();
        let mut responder_buffer = initiator_step1[KEY_LENGTH..].to_vec();
        responder_buffer.extend_from_slice(&initiator_step3);
        let discovered = responder
            .receiver_info_hash(&responder_buffer, &[info_hash])
            .unwrap()
            .unwrap();
        responder.set_info_hash(discovered).unwrap();
        let required = responder
            .receiver_step2_required_len(&responder_buffer)
            .unwrap()
            .unwrap();
        assert_eq!(required, responder_buffer.len());
        responder
            .receive_initiator_step2(&responder_buffer, &[info_hash])
            .unwrap();
        let responder_step4 = responder.build_receiver_step2().unwrap();

        let mut initiator_buffer = responder_step1[KEY_LENGTH..].to_vec();
        initiator_buffer.extend_from_slice(&responder_step4);
        let required = initiator
            .initiator_step2_required_len(&initiator_buffer)
            .unwrap()
            .unwrap();
        assert_eq!(required, initiator_buffer.len());
        initiator.receive_receiver_step2(&initiator_buffer).unwrap();
        let mut initiator_crypto = initiator.finalize().unwrap();
        let mut responder_crypto = responder.finalize().unwrap();

        let mut message = [0x77u8; 68];
        initiator_crypto.encrypt(&mut message);
        responder_crypto.decrypt(&mut message);
        assert_eq!(message, [0x77u8; 68]);

        let mut reverse = [0x88u8; 68];
        responder_crypto.encrypt(&mut reverse);
        initiator_crypto.decrypt(&mut reverse);
        assert_eq!(reverse, [0x88u8; 68]);
    }

    #[test]
    fn test_handshake_type_detection() {
        // Legacy BT handshake: pstrlen=19 + "BitTorrent protocol"
        let mut legacy = vec![19u8];
        legacy.extend_from_slice(b"BitTorrent protocol");
        assert_eq!(
            MseHandshake::identify_handshake_type(&legacy),
            HandshakeType::Legacy
        );

        // Encrypted: first byte is not 19
        let encrypted = [0u8; 20];
        assert_eq!(
            MseHandshake::identify_handshake_type(&encrypted),
            HandshakeType::Encrypted
        );

        // Not enough data
        let short = [19u8; 10];
        assert_eq!(
            MseHandshake::identify_handshake_type(&short),
            HandshakeType::NotYet
        );
    }

    #[test]
    fn test_should_negotiate() {
        let reserved_all_zero = [0u8; 8];
        let mut reserved_mse_set = [0u8; 8];
        reserved_mse_set[7] = 0x01;

        assert!(!MseHandshake::should_negotiate(true, &reserved_all_zero));
        assert!(MseHandshake::should_negotiate(true, &reserved_mse_set));
        assert!(!MseHandshake::should_negotiate(false, &reserved_mse_set));
        assert!(!MseHandshake::should_negotiate(true, &[]));
    }

    #[test]
    fn test_different_instances_different_keys() {
        let h1 = MseHandshake::new_initiator(test_info_hash());
        let h2 = MseHandshake::new_initiator(test_info_hash());

        let pk1 = h1.dh.generate_public_key();
        let pk2 = h2.dh.generate_public_key();

        assert_ne!(
            pk1, pk2,
            "Different instances should have different public keys"
        );
    }

    #[test]
    fn test_initiator_step2_before_keys() {
        let mut h = MseHandshake::new_initiator(test_info_hash());
        let result = h.build_initiator_step2();
        assert!(result.is_err());
    }

    #[test]
    fn test_finalize_before_completed() {
        let h = MseHandshake::new_initiator(test_info_hash());
        let result = h.finalize();
        assert!(result.is_err());
    }

    #[test]
    fn test_receiver_step4_before_completed() {
        let mut h = MseHandshake::new_responder(test_info_hash());
        let result = h.build_receiver_step2();
        assert!(result.is_err());
    }

    #[test]
    fn test_only_initiator_builds_step3() {
        let mut h = MseHandshake::new_responder(test_info_hash());
        let result = h.build_initiator_step2();
        assert!(result.is_err());
    }

    #[test]
    fn test_only_receiver_builds_step4() {
        let info_hash = test_info_hash();
        let mut h = MseHandshake::new_initiator(info_hash);
        let result = h.build_receiver_step2();
        assert!(result.is_err());
    }

    #[test]
    fn test_only_receiver_processes_step3() {
        let mut h = MseHandshake::new_initiator(test_info_hash());
        let result = h.receive_initiator_step2(&[], &[]);
        assert!(result.is_err());
    }

    #[test]
    fn test_only_initiator_processes_step4() {
        let mut h = MseHandshake::new_responder(test_info_hash());
        let result = h.receive_receiver_step2(&[]);
        assert!(result.is_err());
    }

    // ── Key derivation matches C++ format ────────────────────────────

    #[test]
    fn test_key_derivation_format() {
        let info_hash = test_info_hash();
        let initiator = MseHandshake::new_initiator(info_hash);
        let step1 = initiator.build_step1();

        let mut responder = MseHandshake::new_responder(info_hash);
        responder.receive_step1(&step1).expect("receive step1");

        let keys = responder.keys.as_ref().expect("keys derived");

        // Verify keyA format: SHA1("keyA" || S || infoHash)
        let shared = responder.shared_secret.expect("shared secret");
        let mut input = Vec::new();
        input.extend_from_slice(b"keyA");
        input.extend_from_slice(&shared);
        input.extend_from_slice(&info_hash);

        use sha1::{Digest, Sha1};
        let mut hasher = Sha1::new();
        hasher.update(&input);
        let expected = hasher.finalize();

        assert_eq!(&keys.key_a[..], &expected[..]);
    }

    // ── End-to-end with multiple info_hashes ─────────────────────────

    #[test]
    fn test_multi_torrent_info_hash_selection() {
        let info_hash_1 = [0x01u8; INFO_HASH_LENGTH];
        let info_hash_2 = [0x02u8; INFO_HASH_LENGTH];

        // Initiator wants info_hash_2
        let mut initiator = MseHandshake::new_initiator(info_hash_2);
        // Responder knows about both torrents
        let mut responder = MseHandshake::new_responder(info_hash_1);

        // Exchange public keys
        let i_step1 = initiator.build_step1();
        let r_step1 = responder.build_step1();
        initiator
            .receive_step1(&r_step1)
            .expect("I receive R step1");
        responder
            .receive_step1(&i_step1)
            .expect("R receive I step1");

        // Initiator sends step 3 with info_hash_2
        let i_step3 = initiator.build_initiator_step2().expect("I build step3");

        // Responder verifies against known info_hashes and finds info_hash_2
        let method = responder
            .receive_initiator_step2(&i_step3, &[info_hash_1, info_hash_2])
            .expect("R receive step3");
        assert_eq!(method, MseCryptoMethod::Rc4);

        // Responder should now be using info_hash_2
        assert_eq!(responder.info_hash, info_hash_2);
    }
}