aria2-protocol 0.3.2

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
//! MSE (Message Stream Encryption) crypto primitives.
//!
//! Matches C++ aria2 `MSEHandshake.cc` key derivation:
//! - `keyA = SHA1("keyA" || S || infoHash)` — full 20-byte RC4 key
//! - `keyB = SHA1("keyB" || S || infoHash)` — full 20-byte RC4 key
//! - `req1 = SHA1("req1" || S)` — used as hash marker
//! - `req2 = SHA1("req2" || infoHash)` — used for info_hash verification
//! - `req3 = SHA1("req3" || S)` — XORed with req2
//! - VC = 8 zero bytes encrypted with RC4 (NOT a SHA1 hash)
//! - Both encryptor and decryptor discard first 1024 bytes of RC4 keystream

use sha1::{Digest, Sha1};

use super::mse_dh::{INFO_HASH_LENGTH, KEY_LENGTH, SHA1_LENGTH, VC_LENGTH};

const KEYSTREAM_DISCARD: usize = 1024;

/// MSE crypto negotiation methods (4-byte big-endian bitmask).
///
/// Bit values match C++ aria2 `CRYPTO_PLAIN_TEXT = 0x01`, `CRYPTO_ARC4 = 0x02`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MseCryptoMethod {
    /// No encryption (plaintext after MSE negotiation).
    Plain = 0x00000001,
    /// RC4 stream cipher.
    Rc4 = 0x00000002,
}

impl MseCryptoMethod {
    /// Parse from the least-significant byte of the 4-byte crypto bitmask.
    pub fn from_u32(v: u32) -> Self {
        if v & 0x02 != 0 {
            Self::Rc4
        } else {
            Self::Plain
        }
    }

    /// Convert to the 4-byte big-endian bitmask value.
    pub fn as_u32(self) -> u32 {
        self as u32
    }

    /// Whether this method provides actual encryption.
    pub fn is_encrypted(self) -> bool {
        matches!(self, Self::Rc4)
    }
}

/// ARC4 (Alleged RC4) stream cipher implementation for MSE.
/// Symmetric: encryption and decryption are the same operation.
#[derive(Debug, Clone)]
pub struct Arc4Cipher {
    s: [u8; 256],
    i: u8,
    j: u8,
}

impl Arc4Cipher {
    /// Create a new ARC4 cipher instance with the given key.
    pub fn new(key: &[u8]) -> Self {
        let mut s = [0u8; 256];
        #[allow(clippy::needless_range_loop)]
        for i in 0..256 {
            s[i] = i as u8;
        }

        let mut j: usize = 0;
        #[allow(clippy::needless_range_loop)]
        for i in 0..256 {
            let si = s[i] as usize;
            j = (j + si + key[i % key.len()] as usize) % 256;
            s.swap(i, j);
        }

        Arc4Cipher { s, i: 0, j: 0 }
    }

    /// Encrypt/decrypt data in-place (ARC4 is symmetric).
    pub fn encrypt(&mut self, data: &mut [u8]) {
        for byte in data.iter_mut() {
            self.i = self.i.wrapping_add(1);
            self.j = self.j.wrapping_add(self.s[self.i as usize]);
            self.s.swap(self.i as usize, self.j as usize);
            let k =
                self.s[(self.s[self.i as usize] as usize + self.s[self.j as usize] as usize) % 256];
            *byte ^= k;
        }
    }

    /// Decrypt data in-place (same as encrypt for ARC4).
    pub fn decrypt(&mut self, data: &mut [u8]) {
        self.encrypt(data);
    }
}

/// Derived keys for the MSE handshake, matching C++ `MSEHandshake::initCipher()`.
///
/// All keys are full 20-byte SHA-1 digests, matching C++ which uses the
/// complete `localCipherKey[20]` / `peerCipherKey[20]` as RC4 keys.
#[derive(Debug, Clone)]
pub struct MseDerivedKeys {
    /// `SHA1("keyA" || S || infoHash)` — encryptor key for initiator,
    /// decryptor key for receiver.
    pub key_a: [u8; SHA1_LENGTH],
    /// `SHA1("keyB" || S || infoHash)` — decryptor key for initiator,
    /// encryptor key for receiver.
    pub key_b: [u8; SHA1_LENGTH],
    /// `SHA1("req1" || S)` — hash marker sent in step 3.
    pub req1: [u8; SHA1_LENGTH],
    /// `SHA1("req2" || infoHash)` — used to verify info_hash.
    pub req2: [u8; SHA1_LENGTH],
    /// `SHA1("req3" || S)` — XORed with req2 to conceal info_hash.
    pub req3: [u8; SHA1_LENGTH],
}

impl MseDerivedKeys {
    /// Derive all MSE keys from the DH shared secret and info_hash.
    ///
    /// Matches C++ `MSEHandshake::initCipher()` and the hash functions:
    /// - `keyA = SHA1("keyA" || S || infoHash)` (20 bytes)
    /// - `keyB = SHA1("keyB" || S || infoHash)` (20 bytes)
    /// - `req1 = SHA1("req1" || S)` (20 bytes)
    /// - `req2 = SHA1("req2" || infoHash)` (20 bytes)
    /// - `req3 = SHA1("req3" || S)` (20 bytes)
    ///
    /// Note: VC is NOT derived here — it is 8 zero bytes encrypted with
    /// the appropriate RC4 cipher at handshake time.
    pub fn derive(shared_secret: &[u8; KEY_LENGTH], info_hash: &[u8; INFO_HASH_LENGTH]) -> Self {
        // keyA = SHA1("keyA" || S || infoHash)
        let key_a = {
            let mut input = Vec::with_capacity(4 + KEY_LENGTH + INFO_HASH_LENGTH);
            input.extend_from_slice(b"keyA");
            input.extend_from_slice(shared_secret);
            input.extend_from_slice(info_hash);
            sha1_digest_array(&input)
        };

        // keyB = SHA1("keyB" || S || infoHash)
        let key_b = {
            let mut input = Vec::with_capacity(4 + KEY_LENGTH + INFO_HASH_LENGTH);
            input.extend_from_slice(b"keyB");
            input.extend_from_slice(shared_secret);
            input.extend_from_slice(info_hash);
            sha1_digest_array(&input)
        };

        // req1 = SHA1("req1" || S)
        let req1 = {
            let mut input = Vec::with_capacity(4 + KEY_LENGTH);
            input.extend_from_slice(b"req1");
            input.extend_from_slice(shared_secret);
            sha1_digest_array(&input)
        };

        // req2 = SHA1("req2" || infoHash)
        let req2 = {
            let mut input = Vec::with_capacity(4 + INFO_HASH_LENGTH);
            input.extend_from_slice(b"req2");
            input.extend_from_slice(info_hash);
            sha1_digest_array(&input)
        };

        // req3 = SHA1("req3" || S)
        let req3 = {
            let mut input = Vec::with_capacity(4 + KEY_LENGTH);
            input.extend_from_slice(b"req3");
            input.extend_from_slice(shared_secret);
            sha1_digest_array(&input)
        };

        MseDerivedKeys {
            key_a,
            key_b,
            req1,
            req2,
            req3,
        }
    }

    /// Compute `req2 XOR req3` — the 20-byte hash sent in step 3
    /// that allows the receiver to verify the info_hash without
    /// revealing it in plaintext.
    pub fn req2_xor_req3(&self) -> [u8; SHA1_LENGTH] {
        let mut result = [0u8; SHA1_LENGTH];
        for (result, (&req2, &req3)) in result
            .iter_mut()
            .zip(self.req2.iter().zip(self.req3.iter()))
        {
            *result = req2 ^ req3;
        }
        result
    }
}

/// Compute SHA-1 digest and return as a fixed 20-byte array.
fn sha1_digest_array(data: &[u8]) -> [u8; SHA1_LENGTH] {
    let mut hasher = Sha1::new();
    hasher.update(data);
    let result = hasher.finalize();
    let mut arr = [0u8; SHA1_LENGTH];
    arr.copy_from_slice(&result);
    arr
}

/// RC4 cipher state with 1024-byte keystream discard (MSE spec section 5.2).
#[derive(Debug)]
pub struct Rc4State {
    s: [u8; 256],
    i: u8,
    j: u8,
}

impl Rc4State {
    fn new(key: &[u8]) -> Self {
        let mut s = [0u8; 256];
        #[allow(clippy::needless_range_loop)]
        for i in 0..256 {
            s[i] = i as u8;
        }

        let mut j: usize = 0;
        #[allow(clippy::needless_range_loop)]
        for i in 0..256 {
            let si = s[i] as usize;
            j = (j + si + key[i % key.len()] as usize) % 256;
            s.swap(i, j);
        }

        Rc4State { s, i: 0, j: 0 }
    }

    /// Process (encrypt/decrypt) data in-place.
    pub fn process(&mut self, data: &mut [u8]) {
        for byte in data.iter_mut() {
            self.i = self.i.wrapping_add(1);
            self.j = self.j.wrapping_add(self.s[self.i as usize]);
            self.s.swap(self.i as usize, self.j as usize);
            let k =
                self.s[(self.s[self.i as usize] as usize + self.s[self.j as usize] as usize) % 256];
            *byte ^= k;
        }
    }
}

/// Initialize an RC4 cipher with the given key and discard 1024 bytes
/// of keystream, matching C++ `ARC4Encryptor::init()` + discard.
pub fn init_rc4(key: &[u8]) -> Rc4State {
    let mut rc4 = Rc4State::new(key);
    let mut discard = [0u8; KEYSTREAM_DISCARD];
    rc4.process(&mut discard);
    rc4
}

/// Compute the VC (Verification Constant): 8 zero bytes encrypted
/// with the given RC4 cipher. This matches C++ `initCipher()` which
/// pre-computes `initiatorVCMarker_` by encrypting zeros.
///
/// IMPORTANT: The cipher must already have 1024 bytes discarded.
pub fn compute_vc(cipher: &mut Rc4State) -> [u8; VC_LENGTH] {
    let mut vc = [0u8; VC_LENGTH];
    cipher.process(&mut vc);
    vc
}

/// Ongoing MSE crypto state used after the handshake completes.
/// Provides encrypt/decrypt for the BT message stream.
#[derive(Debug)]
pub struct MseCryptoState {
    send_cipher: Option<Rc4State>,
    recv_cipher: Option<Rc4State>,
    method: MseCryptoMethod,
}

impl MseCryptoState {
    /// Create a plaintext (no encryption) state.
    pub fn new_plain() -> Self {
        MseCryptoState {
            send_cipher: None,
            recv_cipher: None,
            method: MseCryptoMethod::Plain,
        }
    }

    /// Create an encrypted state with the given derived keys.
    ///
    /// Matches C++ `initCipher()`:
    /// - Initiator: encryptor = keyA, decryptor = keyB
    /// - Receiver: encryptor = keyB, decryptor = keyA
    pub fn new_encrypted(keys: &MseDerivedKeys, initiator: bool) -> Self {
        if initiator {
            MseCryptoState {
                send_cipher: Some(init_rc4(&keys.key_a)),
                recv_cipher: Some(init_rc4(&keys.key_b)),
                method: MseCryptoMethod::Rc4,
            }
        } else {
            MseCryptoState {
                send_cipher: Some(init_rc4(&keys.key_b)),
                recv_cipher: Some(init_rc4(&keys.key_a)),
                method: MseCryptoMethod::Rc4,
            }
        }
    }

    /// Create an encrypted state from raw 20-byte RC4 keys.
    /// This allows the handshake code to pass the exact keys used
    /// during negotiation (which may differ from the derived keys
    /// if a different crypto method was selected).
    pub fn from_raw_keys(
        send_key: &[u8; SHA1_LENGTH],
        recv_key: &[u8; SHA1_LENGTH],
        method: MseCryptoMethod,
    ) -> Self {
        match method {
            MseCryptoMethod::Rc4 => MseCryptoState {
                send_cipher: Some(init_rc4(send_key)),
                recv_cipher: Some(init_rc4(recv_key)),
                method: MseCryptoMethod::Rc4,
            },
            MseCryptoMethod::Plain => MseCryptoState::new_plain(),
        }
    }

    /// Continue an MSE stream with the cipher positions reached by the
    /// handshake. Reinitializing from raw keys here would replay the first
    /// keystream bytes and corrupt the post-MSE BitTorrent handshake.
    pub(crate) fn from_rc4_states(
        send_cipher: Rc4State,
        recv_cipher: Rc4State,
        method: MseCryptoMethod,
    ) -> Self {
        Self {
            send_cipher: Some(send_cipher),
            recv_cipher: Some(recv_cipher),
            method,
        }
    }

    /// Encrypt data in-place (send direction).
    pub fn encrypt(&mut self, data: &mut [u8]) {
        if let Some(ref mut cipher) = self.send_cipher {
            cipher.process(data);
        }
    }

    /// Decrypt data in-place (receive direction).
    pub fn decrypt(&mut self, data: &mut [u8]) {
        if let Some(ref mut cipher) = self.recv_cipher {
            cipher.process(data);
        }
    }

    /// Whether encryption is active.
    pub fn is_encrypted(&self) -> bool {
        self.send_cipher.is_some()
    }

    /// The negotiated crypto method.
    pub fn method(&self) -> MseCryptoMethod {
        self.method
    }
}

impl Default for MseCryptoState {
    fn default() -> Self {
        Self::new_plain()
    }
}

impl Clone for MseCryptoState {
    fn clone(&self) -> Self {
        // RC4 state cannot be meaningfully cloned (stream position lost).
        // Return a plain state; callers who need to replicate must use
        // the same key material to create a new instance.
        MseCryptoState::new_plain()
    }
}

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

    #[test]
    fn test_arc4_encrypt_decrypt_roundtrip() {
        let key = b"test_key_123";
        let mut cipher1 = Arc4Cipher::new(key);
        let mut cipher2 = Arc4Cipher::new(key);

        let original = b"Hello, BitTorrent MSE!";
        let mut encrypted = original.to_vec();

        cipher1.encrypt(&mut encrypted);
        assert_ne!(encrypted, original.to_vec(), "encrypted should differ");

        cipher2.decrypt(&mut encrypted);
        assert_eq!(
            encrypted,
            original.to_vec(),
            "decrypted should match original"
        );
    }

    #[test]
    fn test_arc4_symmetric() {
        let key = b"symmetric_key";
        let mut cipher = Arc4Cipher::new(key);

        let original = b"test_data";
        let mut data1 = original.to_vec();
        let mut data2 = original.to_vec();

        cipher.encrypt(&mut data1);
        let mut cipher2 = Arc4Cipher::new(key);
        cipher2.decrypt(&mut data2);

        assert_eq!(data1, data2);
    }

    #[test]
    fn test_arc4_different_keys_different_output() {
        let mut cipher1 = Arc4Cipher::new(b"key1");
        let mut cipher2 = Arc4Cipher::new(b"key2");

        let original = b"same_plaintext";
        let mut enc1 = original.to_vec();
        let mut enc2 = original.to_vec();

        cipher1.encrypt(&mut enc1);
        cipher2.encrypt(&mut enc2);

        assert_ne!(enc1, enc2);
    }

    #[test]
    fn test_derive_keys_deterministic() {
        let secret = [0xAAu8; KEY_LENGTH];
        let info_hash = [0xBBu8; INFO_HASH_LENGTH];

        let keys1 = MseDerivedKeys::derive(&secret, &info_hash);
        let keys2 = MseDerivedKeys::derive(&secret, &info_hash);

        assert_eq!(keys1.key_a, keys2.key_a);
        assert_eq!(keys1.key_b, keys2.key_b);
        assert_eq!(keys1.req1, keys2.req1);
        assert_eq!(keys1.req2, keys2.req2);
        assert_eq!(keys1.req3, keys2.req3);
    }

    #[test]
    fn test_derive_keys_matches_cpp_format() {
        // Verify the key derivation input format matches C++:
        // keyA = SHA1("keyA" || S || infoHash)
        let secret = [0x42u8; KEY_LENGTH];
        let info_hash = [0x17u8; INFO_HASH_LENGTH];

        let keys = MseDerivedKeys::derive(&secret, &info_hash);

        // Manually compute key_a to verify
        let mut input = Vec::new();
        input.extend_from_slice(b"keyA");
        input.extend_from_slice(&secret);
        input.extend_from_slice(&info_hash);
        let expected = sha1_digest_array(&input);

        assert_eq!(keys.key_a, expected);
    }

    #[test]
    fn test_derive_keys_are_full_20_bytes() {
        let secret = [0xAAu8; KEY_LENGTH];
        let info_hash = [0xBBu8; INFO_HASH_LENGTH];

        let keys = MseDerivedKeys::derive(&secret, &info_hash);

        // All keys should be full 20-byte SHA-1 digests, not truncated to 16
        assert_eq!(keys.key_a.len(), SHA1_LENGTH);
        assert_eq!(keys.key_b.len(), SHA1_LENGTH);
        assert_eq!(keys.req1.len(), SHA1_LENGTH);
        assert_eq!(keys.req2.len(), SHA1_LENGTH);
        assert_eq!(keys.req3.len(), SHA1_LENGTH);
    }

    #[test]
    fn test_req2_xor_req3() {
        let secret = [0xAAu8; KEY_LENGTH];
        let info_hash = [0xBBu8; INFO_HASH_LENGTH];

        let keys = MseDerivedKeys::derive(&secret, &info_hash);
        let xor = keys.req2_xor_req3();

        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;
        }
        assert_eq!(xor, expected);
    }

    #[test]
    fn test_crypto_method_roundtrip() {
        assert_eq!(MseCryptoMethod::from_u32(0x01), MseCryptoMethod::Plain);
        assert_eq!(MseCryptoMethod::from_u32(0x02), MseCryptoMethod::Rc4);
        assert_eq!(MseCryptoMethod::from_u32(0x03), MseCryptoMethod::Rc4); // bit 0x02 set
        assert_eq!(MseCryptoMethod::Rc4.as_u32(), 0x02);
        assert_eq!(MseCryptoMethod::Plain.as_u32(), 0x01);
    }

    #[test]
    fn test_rc4_encrypt_decrypt_roundtrip_via_state() {
        let secret = [0xCCu8; KEY_LENGTH];
        let info_hash = [0xDDu8; INFO_HASH_LENGTH];
        let keys = MseDerivedKeys::derive(&secret, &info_hash);

        let mut crypto_initiator = MseCryptoState::new_encrypted(&keys, true);
        let mut crypto_responder = MseCryptoState::new_encrypted(&keys, false);

        let original = b"Hello, BitTorrent MSE!";
        let mut encrypted = original.to_vec();

        crypto_initiator.encrypt(&mut encrypted);
        assert_ne!(encrypted, original.to_vec());

        crypto_responder.decrypt(&mut encrypted);
        assert_eq!(encrypted, original.to_vec());
    }

    #[test]
    fn test_plain_mode_noop() {
        let mut crypto = MseCryptoState::new_plain();
        let mut data = b"unchanged".to_vec();

        crypto.encrypt(&mut data);
        crypto.decrypt(&mut data);

        assert_eq!(data, b"unchanged".to_vec());
    }

    #[test]
    fn test_vc_is_rc4_encrypted_zeros() {
        // VC = RC4(8 zero bytes), NOT SHA1-based
        let key = [0x42u8; SHA1_LENGTH];
        let mut cipher = init_rc4(&key);
        let vc = compute_vc(&mut cipher);

        // VC should not be all zeros (RC4 of zeros is not zeros)
        assert_ne!(
            vc, [0u8; VC_LENGTH],
            "VC should not be all zeros after RC4 encryption"
        );

        // Verify VC is deterministic for same key
        let mut cipher2 = init_rc4(&key);
        let vc2 = compute_vc(&mut cipher2);
        assert_eq!(vc, vc2, "VC should be deterministic for same key");
    }

    #[test]
    fn test_different_secrets_different_keys() {
        let secret1 = [0x01u8; KEY_LENGTH];
        let secret2 = [0x02u8; KEY_LENGTH];
        let info_hash = [0xBBu8; INFO_HASH_LENGTH];

        let keys1 = MseDerivedKeys::derive(&secret1, &info_hash);
        let keys2 = MseDerivedKeys::derive(&secret2, &info_hash);

        assert_ne!(keys1.key_a, keys2.key_a);
        assert_ne!(keys1.key_b, keys2.key_b);
        assert_ne!(keys1.req1, keys2.req1);
    }

    #[test]
    fn test_different_info_hashes_different_keys() {
        let secret = [0xAAu8; KEY_LENGTH];
        let info_hash1 = [0x01u8; INFO_HASH_LENGTH];
        let info_hash2 = [0x02u8; INFO_HASH_LENGTH];

        let keys1 = MseDerivedKeys::derive(&secret, &info_hash1);
        let keys2 = MseDerivedKeys::derive(&secret, &info_hash2);

        // key_a and key_b include infoHash, so they differ
        assert_ne!(keys1.key_a, keys2.key_a);
        assert_ne!(keys1.key_b, keys2.key_b);
        // req2 includes infoHash, so it differs
        assert_ne!(keys1.req2, keys2.req2);
        // req1 and req3 depend only on S, so they are the same
        assert_eq!(keys1.req1, keys2.req1);
        assert_eq!(keys1.req3, keys2.req3);
    }

    #[test]
    fn test_multiple_messages_independent() {
        let secret = [0xEEu8; KEY_LENGTH];
        let info_hash = [0xFFu8; INFO_HASH_LENGTH];
        let keys = MseDerivedKeys::derive(&secret, &info_hash);

        let mut sender = MseCryptoState::new_encrypted(&keys, true);
        let mut receiver = MseCryptoState::new_encrypted(&keys, false);

        let msgs: &[&[u8]] = &[b"first", b"second message", b"third longer msg"];
        for msg in msgs {
            let mut enc = msg.to_vec();
            sender.encrypt(&mut enc);
            receiver.decrypt(&mut enc);
            assert_eq!(enc, *msg);
        }
    }
}