crypter 0.3.1

A AES-GCM 256 encryption and decryption library.
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
//! Stream support for AES-GCM-SIV 256-bits encrypting and decrypting in chunks.
//!
//! The chunks are counted and used when generating the nonce of the next chunk. Therefore, it is
//! important that the [`Encrypter`] and [`Decrypter`] use the same chunk size so the nonces can be
//! in sync. Decryption will fail otherwise.
//!
//! # Examples
//!
//! ## Encrypting
//!
//! ```
//! # fn get_key() -> aes_gcm_siv::Key<aes_gcm_siv::Aes256GcmSiv> { Default::default() }
//! # fn stdout() -> impl std::io::Write { Vec::<u8>::new() }
//! # fn stdin() -> impl std::io::Read { std::io::Cursor::new(Vec::new()) }
//! use std::io::{BufRead, Write};
//!
//! let key = get_key();
//! let mut encrypter = crypter::stream::Encrypter::new(&key, stdout())
//!     .expect("Failed to write to stdout");
//! let reader = std::io::BufReader::new(stdin());
//!
//! for line in reader.lines() {
//!     let line = line.expect("Failed to read from stdin");
//!     encrypter
//!         .write_all(line.as_bytes())
//!         .expect("Failed to encrypt stream");
//! }
//! ```
//!
//! ## Decrypting
//!
//! ```
//! # fn get_key() -> aes_gcm_siv::Key<aes_gcm_siv::Aes256GcmSiv> { Default::default() }
//! # fn stdin() -> impl std::io::Read {
//! #     let mut bytes = Vec::new();
//! #     crypter::stream::Encrypter::new(&get_key(), &mut bytes).unwrap();
//! #     std::io::Cursor::new(bytes)
//! # }
//! use std::io::BufRead;
//!
//! let key = get_key();
//! let decrypter = crypter::stream::Decrypter::new(&key, stdin())
//!     .expect("Failed to read from stdin");
//! let reader = std::io::BufReader::new(decrypter);
//!
//! for line in reader.lines() {
//!     let line = line.expect("Failed to read from stdin");
//!     println!("{line}");
//! }
//! ```

use crate::sizes;

/// The default chunk size for encryption. That is, 512 KiB.
pub const DEFAULT_CHUNK: usize = 512 * 1024;

/// cbindgen:ignore
type Nonce = aead::stream::Nonce<
    aes_gcm_siv::Aes256GcmSiv,
    aead::stream::StreamLE31<aes_gcm_siv::Aes256GcmSiv>,
>;

/// A streaming AES-GCM-SIV 256-bits encrypter.
///
/// Implements the [`Write`](std::io::Write) trait to provide streaming, while internally keeping a
/// buffer of the chunk to encrypt as a single message.
///
/// It will auto-finalize on drop, but will fail silently in that case. To get any errors that may
/// happen while finalizing, explicitly call [`finish`](Encrypter::finish).
///
/// **Note:** The size of the chunk used must match the chunk size used by the [`Decrypter`]. The decryption
/// will fail if there is a mismatch.
///
/// # Examples
///
/// ```
/// # fn get_key() -> aes_gcm_siv::Key<aes_gcm_siv::Aes256GcmSiv> { Default::default() }
/// # fn stdout() -> impl std::io::Write { Vec::<u8>::new() }
/// # fn stdin() -> impl std::io::Read { std::io::Cursor::new(Vec::new()) }
/// use std::io::{BufRead, Write};
///
/// let key = get_key();
/// let mut encrypter = crypter::stream::Encrypter::new(&key, stdout())
///     .expect("Failed to write to stdout");
/// let reader = std::io::BufReader::new(stdin());
///
/// for line in reader.lines() {
///     let line = line.expect("Failed to read from stdin");
///     encrypter
///         .write_all(line.as_bytes())
///         .expect("Failed to encrypt stream");
/// }
/// ```
pub struct Encrypter<Out>
where
    Out: std::io::Write,
{
    stream: Option<aead::stream::EncryptorLE31<aes_gcm_siv::Aes256GcmSiv>>,
    buffer: Vec<u8>,
    output: Out,
    plain_capacity: usize,
}

impl<Out> Encrypter<Out>
where
    Out: std::io::Write,
{
    /// Creates a new [`Encrypter`] using the writer `output` and a default chunk size [`DEFAULT_CHUNK`]
    /// encrypted with the cryptographic key `key`.
    ///
    /// # Errors
    ///
    /// When initializing, the [`Encrypter`] will write a few bytes to `output`. If any error happens at
    /// that stage, this function will fail.
    pub fn new<'k, Key>(key: Key, output: Out) -> std::io::Result<Self>
    where
        Key: Into<&'k aes_gcm_siv::Key<aes_gcm_siv::Aes256GcmSiv>>,
    {
        Self::with_chunk(key, output, DEFAULT_CHUNK)
    }

    /// Creates a new [`Encrypter`] using the writer `output` and a default chunk size [`DEFAULT_CHUNK`]
    /// encrypted with the cryptographic key derived from `password` using Argon2.
    ///
    /// # Errors
    ///
    /// * If the key derivation fails, an [`InvalidData`](std::io::ErrorKind::InvalidData) is
    ///   returned;
    /// * When initializing, the [`Encrypter`] will write a few bytes to `output`. If any error happens at
    ///   that stage, this function will fail.
    #[cfg(feature = "argon")]
    pub fn new_with_password<Password>(password: Password, output: Out) -> std::io::Result<Self>
    where
        Password: AsRef<[u8]>,
    {
        Self::with_chunk_and_password(password, output, DEFAULT_CHUNK)
    }

    /// Creates a new [`Encrypter`] using the writer `output` and a chunk size `chunk` encrypted with
    /// the cryptographic key `key`.
    ///
    /// # Errors
    ///
    /// * If the value of `chunk` is less than 32, an [`InvalidInput`](std::io::ErrorKind::InvalidInput) is
    ///   returned.
    /// * When initializing, the [`Encrypter`] will write a few bytes to `output`. If any error happens at
    ///   that stage, this function will fail.
    pub fn with_chunk<'k, Key>(key: Key, mut output: Out, chunk: usize) -> std::io::Result<Self>
    where
        Key: Into<&'k aes_gcm_siv::Key<aes_gcm_siv::Aes256GcmSiv>>,
    {
        use aes_gcm_siv::aead::KeyInit;

        if chunk < 32 {
            return Err(std::io::ErrorKind::InvalidInput.into());
        }

        let key = key.into();
        let nonce = make_nonce();

        output.write_all(&nonce)?;

        let stream = Some(aead::stream::EncryptorLE31::from_aead(
            aes_gcm_siv::Aes256GcmSiv::new(key),
            &nonce,
        ));
        let buffer = Vec::with_capacity(chunk);
        let plain_capacity = buffer.capacity() - sizes::TAG_LEN;

        Ok(Self {
            stream,
            buffer,
            output,
            plain_capacity,
        })
    }

    /// Creates a new [`Encrypter`] using the writer `output` and a chunk size `chunk` encrypted with
    /// the cryptographic key derived from `password` using Argon2.
    ///
    /// # Errors
    ///
    /// * If the value of `chunk` is less than 32, an [`InvalidInput`](std::io::ErrorKind::InvalidInput) is
    ///   returned.
    /// * If the key derivation fails, an [`InvalidData`](std::io::ErrorKind::InvalidData) is
    ///   returned;
    /// * When initializing, the [`Encrypter`] will write a few bytes to `output`. If any error happens at
    ///   that stage, this function will fail.
    #[cfg(feature = "argon")]
    pub fn with_chunk_and_password<Password>(
        password: Password,
        mut output: Out,
        chunk: usize,
    ) -> std::io::Result<Self>
    where
        Password: AsRef<[u8]>,
    {
        use aes_gcm_siv::aead::KeyInit;

        if chunk < 32 {
            return Err(std::io::ErrorKind::InvalidInput.into());
        }

        let (key, salt) =
            crate::argon::derive_key(password).ok_or(std::io::ErrorKind::InvalidInput)?;
        output.write_all(&salt)?;

        let nonce = make_nonce();
        output.write_all(&nonce)?;

        let stream = Some(aead::stream::EncryptorLE31::from_aead(
            aes_gcm_siv::Aes256GcmSiv::new(&key),
            &nonce,
        ));
        let buffer = Vec::with_capacity(chunk);
        let plain_capacity = buffer.capacity() - sizes::TAG_LEN;

        Ok(Self {
            stream,
            buffer,
            output,
            plain_capacity,
        })
    }

    /// Finalizes the stream by encrypting any reamining bytes and setting the `last` flag,
    /// flushing the output, and dropping this [`Encrypter`].
    ///
    /// If the plain text message matches exactly with the chunk division, an empty chunk will be
    /// sent to signal the end of the stream. The chunk division is 16 bytes smaller than the chunk
    /// size to accommodate the AES-GCM-SIV tag.
    ///
    /// This function will be called on [`drop()`](std::mem::drop) if not explicitly called.
    /// Though, if called on [`drop()`](std::mem::drop), any errors will be ignored.
    ///
    /// # Errors
    ///
    /// The function may fail while encrypting any buffered bytes and flushing the encrypted message.
    pub fn finish(mut self) -> std::io::Result<()> {
        self.finish_inner()
    }

    fn flush_block(&mut self) -> std::io::Result<()> {
        // SAFETY: The option is only removed on drop
        unsafe {
            self.stream
                .as_mut()
                .unwrap_unchecked()
                .encrypt_next_in_place(b"", &mut self.buffer)
                .map_err(|err| std::io::Error::other(err.to_string()))?;
        }

        self.output.write_all(&self.buffer)?;
        self.buffer.clear();
        Ok(())
    }

    fn finish_inner(&mut self) -> std::io::Result<()> {
        // SAFETY: The option is only removed on drop
        let mut stream = unsafe { self.stream.take().unwrap_unchecked() };

        if self.buffer.len() == self.plain_capacity {
            stream
                .encrypt_next_in_place(b"", &mut self.buffer)
                .map_err(|err| std::io::Error::other(err.to_string()))?;

            self.output.write_all(&self.buffer)?;
            self.buffer.clear();
        }

        stream
            .encrypt_last_in_place(b"", &mut self.buffer)
            .map_err(|err| std::io::Error::other(err.to_string()))?;

        self.output.write_all(&self.buffer)?;
        self.output.flush()
    }

    /// # Safety
    ///
    /// The capacity of `self.buffer` must accommodate `buf.len()`
    unsafe fn fill_buf(&mut self, buf: &[u8]) {
        let len = self.buffer.len();
        unsafe { self.buffer.set_len(len + buf.len()) };
        self.buffer[len..].copy_from_slice(buf);
    }
}

impl<Out> std::io::Write for Encrypter<Out>
where
    Out: std::io::Write,
{
    fn write(&mut self, mut buf: &[u8]) -> std::io::Result<usize> {
        let mut sent = 0;
        let mut rem_cap = self.plain_capacity.saturating_sub(self.buffer.len());

        while buf.len() > rem_cap {
            // SAFETY: The length was check before entering the loop
            unsafe { self.fill_buf(&buf[..rem_cap]) };
            self.flush_block()?;

            buf = &buf[rem_cap..];
            sent += rem_cap;
            rem_cap = self.plain_capacity;
        }

        // SAFETY: The length was checked by the loop before reaching here
        unsafe { self.fill_buf(buf) };
        Ok(sent + buf.len())
    }

    fn flush(&mut self) -> std::io::Result<()> {
        if self.buffer.len() == self.plain_capacity {
            self.flush_block()?;
        }
        self.output.flush()
    }
}

impl<Out> Drop for Encrypter<Out>
where
    Out: std::io::Write,
{
    fn drop(&mut self) {
        if self.stream.is_some() {
            drop(self.finish_inner());
        }
    }
}

/// A streaming AES-GCM-SIV 256-bits decrypter.
///
/// Implements the [`Read`](std::io::Read) trait to provide streaming, while internally keeping a
/// buffer of the chunk to decrypt as a single message.
///
/// **Note:** The size of the chunk used must match the chunk size used by the [`Encrypter`]. The decryption
/// will fail if there is a mismatch.
///
/// # Examples
///
/// ```
/// # fn get_key() -> aes_gcm_siv::Key<aes_gcm_siv::Aes256GcmSiv> { Default::default() }
/// # fn stdin() -> impl std::io::Read {
/// #     let mut bytes = Vec::new();
/// #     crypter::stream::Encrypter::new(&get_key(), &mut bytes).unwrap();
/// #     std::io::Cursor::new(bytes)
/// # }
/// use std::io::BufRead;
///
/// let key = get_key();
/// let decrypter = crypter::stream::Decrypter::new(&key, stdin())
///    .expect("Failed to read from stdin");
/// let reader = std::io::BufReader::new(decrypter);
///
/// for line in reader.lines() {
///     let line = line.expect("Failed to read from stdin");
///     println!("{line}");
/// }
/// ```
pub struct Decrypter<In>
where
    In: std::io::Read,
{
    stream: Option<aead::stream::DecryptorLE31<aes_gcm_siv::Aes256GcmSiv>>,
    buffer: Vec<u8>,
    cursor: usize,
    input: In,
}

impl<In> Decrypter<In>
where
    In: std::io::Read,
{
    /// Creates a new [`Decrypter`] using the reader `input` and a default size [`DEFAULT_CHUNK`] decrypted
    /// with the cryptographic key `key`.
    ///
    /// # Errors
    ///
    /// When initializing, the [`Decrypter`] will read the first few bytes of `input`. If any error
    /// happens at that stage, this function will fail.
    pub fn new<'k, Key>(key: Key, input: In) -> std::io::Result<Self>
    where
        Key: Into<&'k aes_gcm_siv::Key<aes_gcm_siv::Aes256GcmSiv>>,
    {
        Self::with_chunk(key, input, DEFAULT_CHUNK)
    }

    /// Creates a new [`Decrypter`] using the reader `input` and a default size [`DEFAULT_CHUNK`] decrypted
    /// with the cryptographic key derived from `password` using Argon2.
    ///
    /// # Errors
    ///
    /// * If the key derivation fails, an [`InvalidData`](std::io::ErrorKind::InvalidData) is
    ///   returned;
    /// * When initializing, the [`Decrypter`] will read the first few bytes of `input`. If any error
    ///   happens at that stage, this function will fail.
    #[cfg(feature = "argon")]
    pub fn new_with_password<Password>(password: Password, input: In) -> std::io::Result<Self>
    where
        Password: AsRef<[u8]>,
    {
        Self::with_chunk_with_password(password, input, DEFAULT_CHUNK)
    }

    /// Creates a new [`Decrypter`] using the reader `input` and a chunk size `chunk` decrypted
    /// with the cryptographic key `key`.
    ///
    /// # Errors
    ///
    /// * If the value of `chunk` is less than 32, an [`InvalidInput`](std::io::ErrorKind::InvalidInput) is
    ///   returned.
    /// * When initializing, the [`Decrypter`] will read the first few bytes of `input`. If any error
    ///   happens at that stage, this function will fail.
    pub fn with_chunk<'k, Key>(key: Key, mut input: In, chunk: usize) -> std::io::Result<Self>
    where
        Key: Into<&'k aes_gcm_siv::Key<aes_gcm_siv::Aes256GcmSiv>>,
    {
        use aead::KeyInit;

        if chunk < 32 {
            return Err(std::io::ErrorKind::InvalidInput.into());
        }

        let key = key.into();

        let mut nonce = Nonce::default();
        input.read_exact(&mut nonce)?;

        let stream = Some(aead::stream::DecryptorLE31::from_aead(
            aes_gcm_siv::Aes256GcmSiv::new(key),
            nonce.as_slice().into(),
        ));
        let buffer = Vec::with_capacity(chunk);

        Ok(Self {
            stream,
            buffer,
            cursor: 0,
            input,
        })
    }

    /// Creates a new [`Decrypter`] using the reader `input` and a chunk size `chunk` decrypted
    /// with the cryptographic key derived from `password` using Argon2.
    ///
    /// # Errors
    ///
    /// * If the value of `chunk` is less than 32, an [`InvalidInput`](std::io::ErrorKind::InvalidInput) is
    ///   returned.
    /// * If the key derivation fails, an [`InvalidData`](std::io::ErrorKind::InvalidData) is
    ///   returned;
    /// * When initializing, the [`Decrypter`] will read the first few bytes of `input`. If any error
    ///   happens at that stage, this function will fail.
    #[cfg(feature = "argon")]
    pub fn with_chunk_with_password<Password>(
        password: Password,
        mut input: In,
        chunk: usize,
    ) -> std::io::Result<Self>
    where
        Password: AsRef<[u8]>,
    {
        use aead::KeyInit;

        if chunk < 32 {
            return Err(std::io::ErrorKind::InvalidInput.into());
        }

        let mut salt = crate::argon::Salt::default();
        input.read_exact(&mut salt)?;

        let key = crate::argon::derive_with_salt(password, &salt)
            .ok_or(std::io::ErrorKind::InvalidData)?;

        let mut nonce = Nonce::default();
        input.read_exact(&mut nonce)?;

        let stream = Some(aead::stream::DecryptorLE31::from_aead(
            aes_gcm_siv::Aes256GcmSiv::new(&key),
            nonce.as_slice().into(),
        ));
        let buffer = Vec::with_capacity(chunk);

        Ok(Self {
            stream,
            buffer,
            cursor: 0,
            input,
        })
    }

    fn fill_buf(&mut self) -> std::io::Result<()> {
        // SAFETY: The unused length will be truncated after reading
        unsafe { self.buffer.set_len(self.buffer.capacity()) };
        let mut read = 0;
        while read < self.buffer.capacity() {
            read += {
                let bytes = self.input.read(&mut self.buffer[read..])?;
                if bytes == 0 {
                    break;
                }
                bytes
            };
        }
        // SAFETY: Truncating the length to ensure safety from the previous `set_len`
        unsafe { self.buffer.set_len(read) };
        self.cursor = 0;
        Ok(())
    }

    /// # Safety
    ///
    /// The presence of `self.stream` must be guaranteed
    unsafe fn decrypt(&mut self) -> std::io::Result<()> {
        unsafe {
            if self.buffer.len() < self.buffer.capacity() {
                self.stream
                    .take()
                    .unwrap_unchecked()
                    .decrypt_last_in_place(b"", &mut self.buffer)
                    .map_err(|err| std::io::Error::other(err.to_string()))?;
            } else {
                self.stream
                    .as_mut()
                    .unwrap_unchecked()
                    .decrypt_next_in_place(b"", &mut self.buffer)
                    .map_err(|err| std::io::Error::other(err.to_string()))?;
            }
        }
        Ok(())
    }
}

impl<In> std::io::Read for Decrypter<In>
where
    In: std::io::Read,
{
    fn read(&mut self, mut buf: &mut [u8]) -> std::io::Result<usize> {
        let mut read = 0;
        while !buf.is_empty() {
            let buf_size = self.buffer.len() - self.cursor;
            if buf_size > 0 {
                let len = buf_size.min(buf.len());
                buf[..len].copy_from_slice(&self.buffer[self.cursor..self.cursor + len]);
                self.cursor += len;
                buf = &mut buf[len..];
                read += len;

                continue;
            }

            if self.stream.is_none() {
                break;
            }
            self.fill_buf()?;
            // SAFETY: The presence of `self.stream` was checked above
            unsafe { self.decrypt() }?;
        }
        Ok(read)
    }
}

fn make_nonce() -> Nonce {
    let mut nonce = Nonce::default();
    aes_gcm_siv::aead::rand_core::RngCore::fill_bytes(&mut aes_gcm_siv::aead::OsRng, &mut nonce);
    nonce
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::{Read, Write};

    fn make_key() -> aes_gcm_siv::Key<aes_gcm_siv::Aes256GcmSiv> {
        <aes_gcm_siv::Aes256GcmSiv as aes_gcm_siv::KeyInit>::generate_key(aes_gcm_siv::aead::OsRng)
    }

    #[test]
    fn round_trip() {
        let key = make_key();
        let input = (u8::MIN..=u8::MAX)
            .flat_map(|_| u8::MIN..u8::MAX)
            .collect::<Vec<_>>();

        let mut transient = Vec::with_capacity(input.len());
        let mut output = Vec::with_capacity(input.len());

        let mut encrypter = Encrypter::new(&key, &mut transient).unwrap();
        encrypter.write_all(input.as_slice()).unwrap();
        encrypter.finish().unwrap();
        assert_ne!(input, transient);

        let mut decrypter = Decrypter::new(&key, transient.as_slice()).unwrap();
        decrypter.read_to_end(&mut output).unwrap();
        assert_eq!(input, output);
    }

    #[test]
    #[cfg(feature = "argon")]
    fn round_trip_with_password() {
        let password = "super secret password";
        let input = (u8::MIN..=u8::MAX)
            .flat_map(|_| u8::MIN..u8::MAX)
            .collect::<Vec<_>>();

        let mut transient = Vec::with_capacity(input.len());
        let mut output = Vec::with_capacity(input.len());

        let mut encrypter = Encrypter::new_with_password(password, &mut transient).unwrap();
        encrypter.write_all(input.as_slice()).unwrap();
        encrypter.finish().unwrap();
        assert_ne!(input, transient);

        let mut decrypter = Decrypter::new_with_password(password, transient.as_slice()).unwrap();
        decrypter.read_to_end(&mut output).unwrap();
        assert_eq!(input, output);
    }

    #[test]
    fn round_trip_with_drop() {
        let key = make_key();
        let input = (u8::MIN..=u8::MAX)
            .flat_map(|_| u8::MIN..u8::MAX)
            .collect::<Vec<_>>();

        let mut transient = Vec::with_capacity(input.len());
        let mut output = Vec::with_capacity(input.len());

        {
            let mut encrypter = Encrypter::new(&key, &mut transient).unwrap();
            encrypter.write_all(input.as_slice()).unwrap();
        }
        assert_ne!(input, transient);

        let mut decrypter = Decrypter::new(&key, transient.as_slice()).unwrap();
        decrypter.read_to_end(&mut output).unwrap();
        assert_eq!(input, output);
    }

    #[test]
    fn different_block_size() {
        let key = make_key();
        let input = (u8::MIN..=u8::MAX)
            .flat_map(|_| u8::MIN..u8::MAX)
            .collect::<Vec<_>>();

        let mut transient = Vec::with_capacity(input.len());
        let mut output = Vec::with_capacity(input.len());

        let mut encrypter = Encrypter::with_chunk(&key, &mut transient, 256).unwrap();
        encrypter.write_all(input.as_slice()).unwrap();
        encrypter.finish().unwrap();
        assert_ne!(input, transient);

        let mut decrypter = Decrypter::with_chunk(&key, transient.as_slice(), 128).unwrap();
        let err = decrypter.read_to_end(&mut output).unwrap_err();
        assert_eq!(err.kind(), std::io::ErrorKind::Other);
        assert_eq!(err.to_string(), "aead::Error");
    }

    #[test]
    fn variable_chunk() {
        let key = make_key();
        for chunk in [64, 240, 256, 272, 512, 1024, 16 * 1024 * 1024] {
            for chunk in chunk - 1..=chunk + 1 {
                if let Err(err) = std::thread::spawn(move || {
                    let input = (u8::MIN..=u8::MAX)
                        .flat_map(|_| u8::MIN..u8::MAX)
                        .collect::<Vec<_>>();

                    let mut transient = Vec::with_capacity(input.len());
                    let mut output = Vec::with_capacity(input.len());

                    eprintln!("Chunk {chunk}");
                    let mut encrypter = Encrypter::with_chunk(&key, &mut transient, chunk).unwrap();
                    encrypter.write_all(input.as_slice()).unwrap();
                    encrypter.finish().unwrap();
                    assert_ne!(input, transient);

                    let mut decrypter =
                        Decrypter::with_chunk(&key, transient.as_slice(), chunk).unwrap();
                    decrypter.read_to_end(&mut output).unwrap();
                    assert_eq!(input, output);
                })
                .join()
                {
                    panic!("Chunk {chunk}: {err:?}");
                }
            }
        }
    }

    #[test]
    fn minimum_chunk() {
        let key = make_key();
        for chunk in 0..64 {
            let mut buffer = Vec::new();
            let result = Encrypter::with_chunk(&key, &mut buffer, chunk);
            if chunk < 32 {
                assert!(result.is_err(), "Chunk {chunk}: Expected error");
            } else if let Err(err) = result {
                panic!("Chunk {chunk}: {err:?}");
            }
        }
    }
}