scytale-ring 0.10.1

A drop-in replacement for ring 0.17, implemented with scytale: pure Rust, no C compiler, no build script
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
//! Authenticated encryption: AES-GCM and ChaCha20-Poly1305.
//!
//! Everything is in place. A message is sealed where it lies and its
//! tag handed back or appended; it is opened where it lies, and the
//! plaintext is what the returned slice covers.

use core::fmt;
use core::ops::RangeFrom;

use scytale::Key;
use scytale::aead::{Aead, ChaCha20Poly1305, Gcm};
use scytale::cipher::aes::{Aes128, Aes256};
use zeroize::Zeroize;

use crate::{error, hkdf};

pub mod chacha20_poly1305_openssh;
pub mod quic;

/// The length of every nonce here.
pub const NONCE_LEN: usize = 96 / 8;

/// The length of every tag here.
pub const MAX_TAG_LEN: usize = TAG_LEN;

const TAG_LEN: usize = 16;

/// The longest key of any algorithm here.
const MAX_KEY_LEN: usize = 32;

/// Which AEAD an [`Algorithm`] is, and what [`Debug`] prints.
#[allow(non_camel_case_types)]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum Id {
    AES_128_GCM,
    AES_256_GCM,
    CHACHA20_POLY1305,
}

/// An AEAD algorithm. The only values are the `static`s in this
/// module.
pub struct Algorithm {
    id: Id,
    key_len: usize,
}

impl Algorithm {
    /// The length of a key, in bytes.
    #[inline]
    pub fn key_len(&self) -> usize {
        self.key_len
    }

    /// The length of a tag, in bytes.
    #[inline]
    pub fn tag_len(&self) -> usize {
        TAG_LEN
    }

    /// The length of a nonce, in bytes.
    #[inline]
    pub fn nonce_len(&self) -> usize {
        NONCE_LEN
    }
}

impl PartialEq for Algorithm {
    fn eq(&self, other: &Self) -> bool {
        self.id == other.id
    }
}

impl Eq for Algorithm {}

impl fmt::Debug for Algorithm {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        fmt::Debug::fmt(&self.id, f)
    }
}

impl hkdf::KeyType for &'static Algorithm {
    #[inline]
    fn len(&self) -> usize {
        self.key_len()
    }
}

/// AES-128 in GCM.
pub static AES_128_GCM: Algorithm = Algorithm {
    id: Id::AES_128_GCM,
    key_len: 16,
};

/// AES-256 in GCM.
pub static AES_256_GCM: Algorithm = Algorithm {
    id: Id::AES_256_GCM,
    key_len: 32,
};

/// ChaCha20-Poly1305 (RFC 8439).
pub static CHACHA20_POLY1305: Algorithm = Algorithm {
    id: Id::CHACHA20_POLY1305,
    key_len: 32,
};

/// A nonce. The caller promises it is used once under a key: nothing
/// here can check that.
pub struct Nonce([u8; NONCE_LEN]);

impl Nonce {
    /// A nonce from bytes, which must be [`NONCE_LEN`] long.
    #[inline]
    pub fn try_assume_unique_for_key(
        value: &[u8],
    ) -> Result<Self, error::Unspecified> {
        let value: &[u8; NONCE_LEN] = value.try_into()?;
        Ok(Self::assume_unique_for_key(*value))
    }

    /// A nonce from an array.
    #[inline]
    pub fn assume_unique_for_key(value: [u8; NONCE_LEN]) -> Self {
        Self(value)
    }
}

impl AsRef<[u8; NONCE_LEN]> for Nonce {
    fn as_ref(&self) -> &[u8; NONCE_LEN] {
        &self.0
    }
}

/// Data that is authenticated and not encrypted.
#[derive(Clone, Copy)]
pub struct Aad<A>(A);

impl<A: AsRef<[u8]>> Aad<A> {
    /// Wraps `aad`.
    #[inline]
    pub fn from(aad: A) -> Self {
        Self(aad)
    }
}

impl<A> AsRef<[u8]> for Aad<A>
where
    A: AsRef<[u8]>,
{
    fn as_ref(&self) -> &[u8] {
        self.0.as_ref()
    }
}

impl Aad<[u8; 0]> {
    /// No data.
    pub fn empty() -> Self {
        Self::from([])
    }
}

impl<A> fmt::Debug for Aad<A>
where
    A: fmt::Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_tuple("Aad").field(&self.0).finish()
    }
}

impl<A> PartialEq for Aad<A>
where
    A: PartialEq,
{
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        self.0.eq(&other.0)
    }
}

impl<A> Eq for Aad<A> where A: Eq {}

/// An authentication tag.
#[must_use]
#[derive(Clone, Copy)]
pub struct Tag([u8; TAG_LEN]);

impl AsRef<[u8]> for Tag {
    fn as_ref(&self) -> &[u8] {
        self.0.as_ref()
    }
}

impl TryFrom<&[u8]> for Tag {
    type Error = error::Unspecified;

    fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
        let raw: [u8; TAG_LEN] = value.try_into()?;
        Ok(Self::from(raw))
    }
}

impl From<[u8; TAG_LEN]> for Tag {
    #[inline]
    fn from(value: [u8; TAG_LEN]) -> Self {
        Self(value)
    }
}

/// The keyed cipher, one arm per algorithm.
#[derive(Clone)]
enum Inner {
    Aes128Gcm(Gcm<Aes128>),
    Aes256Gcm(Gcm<Aes256>),
    ChaCha20Poly1305(ChaCha20Poly1305),
}

/// A key of the wrong length is the only way to fail.
fn key<B: scytale::ByteArray>(
    bytes: &[u8],
) -> Result<Key<B>, error::Unspecified> {
    Key::try_from(bytes).map_err(error::erase)
}

impl Inner {
    fn new(
        algorithm: &'static Algorithm,
        bytes: &[u8],
    ) -> Result<Self, error::Unspecified> {
        Ok(match algorithm.id {
            Id::AES_128_GCM => Inner::Aes128Gcm(Gcm::new(&key(bytes)?)),
            Id::AES_256_GCM => Inner::Aes256Gcm(Gcm::new(&key(bytes)?)),
            Id::CHACHA20_POLY1305 => {
                Inner::ChaCha20Poly1305(ChaCha20Poly1305::new(&key(bytes)?))
            }
        })
    }

    fn seal(
        &self,
        nonce: &Nonce,
        aad: &[u8],
        in_out: &mut [u8],
    ) -> Result<Tag, error::Unspecified> {
        fn with<A>(
            a: &A,
            nonce: &Nonce,
            aad: &[u8],
            in_out: &mut [u8],
        ) -> Result<Tag, error::Unspecified>
        where
            A: Aead<Nonce = [u8; NONCE_LEN], Tag = [u8; TAG_LEN]>,
        {
            let mut tag = [0u8; TAG_LEN];
            a.encrypt(&nonce.0, aad, in_out, &mut tag)
                .map_err(error::erase)?;
            Ok(Tag(tag))
        }
        match self {
            Inner::Aes128Gcm(a) => with(a, nonce, aad, in_out),
            Inner::Aes256Gcm(a) => with(a, nonce, aad, in_out),
            Inner::ChaCha20Poly1305(a) => with(a, nonce, aad, in_out),
        }
    }

    /// Opens `data` in place. On failure scytale has already wiped it.
    fn open(
        &self,
        nonce: &Nonce,
        aad: &[u8],
        data: &mut [u8],
        tag: &Tag,
    ) -> Result<(), error::Unspecified> {
        fn with<A>(
            a: &A,
            nonce: &Nonce,
            aad: &[u8],
            data: &mut [u8],
            tag: &Tag,
        ) -> Result<(), error::Unspecified>
        where
            A: Aead<Nonce = [u8; NONCE_LEN], Tag = [u8; TAG_LEN]>,
        {
            a.decrypt(&nonce.0, aad, data, &tag.0).map_err(error::erase)
        }
        match self {
            Inner::Aes128Gcm(a) => with(a, nonce, aad, data, tag),
            Inner::Aes256Gcm(a) => with(a, nonce, aad, data, tag),
            Inner::ChaCha20Poly1305(a) => with(a, nonce, aad, data, tag),
        }
    }
}

/// A key not yet tied to a way of choosing nonces.
pub struct UnboundKey {
    inner: LessSafeKey,
}

impl UnboundKey {
    /// A key for `algorithm`. Fails only if `key_bytes` is the wrong
    /// length.
    #[inline]
    pub fn new(
        algorithm: &'static Algorithm,
        key_bytes: &[u8],
    ) -> Result<Self, error::Unspecified> {
        Ok(Self {
            inner: LessSafeKey::new_(algorithm, key_bytes)?,
        })
    }

    /// The algorithm.
    #[inline]
    pub fn algorithm(&self) -> &'static Algorithm {
        self.inner.algorithm()
    }

    fn into_inner(self) -> LessSafeKey {
        self.inner
    }
}

impl fmt::Debug for UnboundKey {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        self.inner.fmt_debug("UnboundKey", f)
    }
}

impl From<hkdf::Okm<'_, &'static Algorithm>> for UnboundKey {
    fn from(okm: hkdf::Okm<&'static Algorithm>) -> Self {
        let algorithm = *okm.len();
        let mut bytes = [0u8; MAX_KEY_LEN];
        let bytes = &mut bytes[..algorithm.key_len()];
        // A key is far below HKDF's limit, and the buffer is exactly
        // the key's length, so neither step can fail.
        okm.fill(bytes).unwrap_or_else(|error::Unspecified| {
            unreachable!("one key exceeded HKDF's limit")
        });
        let key =
            Self::new(algorithm, bytes).unwrap_or_else(|error::Unspecified| {
                unreachable!("a key of its own length")
            });
        bytes.zeroize();
        key
    }
}

/// A key the caller supplies each nonce for.
///
/// "Less safe" because nothing stops a nonce being used twice, which
/// under either algorithm gives away the plaintexts and the key's
/// authentication.
#[derive(Clone)]
pub struct LessSafeKey {
    inner: Inner,
    algorithm: &'static Algorithm,
}

impl LessSafeKey {
    /// Takes the key.
    #[inline]
    pub fn new(key: UnboundKey) -> Self {
        key.into_inner()
    }

    fn new_(
        algorithm: &'static Algorithm,
        key_bytes: &[u8],
    ) -> Result<Self, error::Unspecified> {
        Ok(Self {
            inner: Inner::new(algorithm, key_bytes)?,
            algorithm,
        })
    }

    /// Opens the ciphertext at `in_out[ciphertext..]` against `tag`,
    /// leaving the plaintext at the front of `in_out`. The returned
    /// slice is the plaintext. On failure the region is zeroed.
    pub fn open_in_place_separate_tag<'in_out, A>(
        &self,
        nonce: Nonce,
        aad: Aad<A>,
        tag: Tag,
        in_out: &'in_out mut [u8],
        ciphertext: RangeFrom<usize>,
    ) -> Result<&'in_out mut [u8], error::Unspecified>
    where
        A: AsRef<[u8]>,
    {
        let start = ciphertext.start;
        let len = in_out.len().checked_sub(start).ok_or(error::Unspecified)?;
        // scytale opens in place, so the plaintext is moved to the
        // front once it is known to be genuine.
        match self
            .inner
            .open(&nonce, aad.as_ref(), &mut in_out[start..], &tag)
        {
            Ok(()) => {
                in_out.copy_within(start.., 0);
                Ok(&mut in_out[..len])
            }
            Err(e) => {
                in_out[..len].fill(0);
                Err(e)
            }
        }
    }

    /// Opens `in_out`, which is ciphertext then tag. The returned slice
    /// is the plaintext.
    #[inline]
    pub fn open_in_place<'in_out, A>(
        &self,
        nonce: Nonce,
        aad: Aad<A>,
        in_out: &'in_out mut [u8],
    ) -> Result<&'in_out mut [u8], error::Unspecified>
    where
        A: AsRef<[u8]>,
    {
        self.open_within(nonce, aad, in_out, 0..)
    }

    /// Opens the ciphertext and tag at `in_out[ciphertext_and_tag..]`,
    /// leaving the plaintext at the front of `in_out`.
    pub fn open_within<'in_out, A>(
        &self,
        nonce: Nonce,
        aad: Aad<A>,
        in_out: &'in_out mut [u8],
        ciphertext_and_tag: RangeFrom<usize>,
    ) -> Result<&'in_out mut [u8], error::Unspecified>
    where
        A: AsRef<[u8]>,
    {
        let tag_offset = in_out
            .len()
            .checked_sub(TAG_LEN)
            .ok_or(error::Unspecified)?;
        let (in_out, received) = in_out.split_at_mut(tag_offset);
        let tag = Tag::try_from(&*received)?;
        self.open_in_place_separate_tag(
            nonce,
            aad,
            tag,
            in_out,
            ciphertext_and_tag,
        )
    }

    /// Seals `in_out` and appends the tag to it.
    #[inline]
    pub fn seal_in_place_append_tag<A, InOut>(
        &self,
        nonce: Nonce,
        aad: Aad<A>,
        in_out: &mut InOut,
    ) -> Result<(), error::Unspecified>
    where
        A: AsRef<[u8]>,
        InOut: AsMut<[u8]> + for<'in_out> Extend<&'in_out u8>,
    {
        self.seal_in_place_separate_tag(nonce, aad, in_out.as_mut())
            .map(|tag| in_out.extend(tag.as_ref()))
    }

    /// Seals `in_out` and returns the tag.
    #[inline]
    pub fn seal_in_place_separate_tag<A>(
        &self,
        nonce: Nonce,
        aad: Aad<A>,
        in_out: &mut [u8],
    ) -> Result<Tag, error::Unspecified>
    where
        A: AsRef<[u8]>,
    {
        self.inner.seal(&nonce, aad.as_ref(), in_out)
    }

    /// The algorithm.
    #[inline]
    pub fn algorithm(&self) -> &'static Algorithm {
        self.algorithm
    }

    fn fmt_debug(
        &self,
        type_name: &'static str,
        f: &mut fmt::Formatter,
    ) -> fmt::Result {
        f.debug_struct(type_name)
            .field("algorithm", &self.algorithm())
            .finish()
    }
}

impl fmt::Debug for LessSafeKey {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        self.fmt_debug("LessSafeKey", f)
    }
}

/// Where a bound key's nonces come from. Each call must return a
/// nonce never returned before under the same key.
pub trait NonceSequence {
    /// The next nonce, or an error once there are no more.
    fn advance(&mut self) -> Result<Nonce, error::Unspecified>;
}

/// A key bound to a [`NonceSequence`].
pub trait BoundKey<N: NonceSequence>: fmt::Debug {
    /// Binds `key` to `nonce_sequence`.
    fn new(key: UnboundKey, nonce_sequence: N) -> Self;

    /// The algorithm.
    fn algorithm(&self) -> &'static Algorithm;
}

/// A key that seals, taking each nonce from its sequence.
pub struct SealingKey<N: NonceSequence> {
    key: LessSafeKey,
    nonce_sequence: N,
}

impl<N: NonceSequence> BoundKey<N> for SealingKey<N> {
    fn new(key: UnboundKey, nonce_sequence: N) -> Self {
        Self {
            key: key.into_inner(),
            nonce_sequence,
        }
    }

    #[inline]
    fn algorithm(&self) -> &'static Algorithm {
        self.key.algorithm()
    }
}

impl<N: NonceSequence> fmt::Debug for SealingKey<N> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        self.key.fmt_debug("SealingKey", f)
    }
}

impl<N: NonceSequence> SealingKey<N> {
    /// As [`LessSafeKey::seal_in_place_append_tag`], with the next
    /// nonce.
    #[inline]
    pub fn seal_in_place_append_tag<A, InOut>(
        &mut self,
        aad: Aad<A>,
        in_out: &mut InOut,
    ) -> Result<(), error::Unspecified>
    where
        A: AsRef<[u8]>,
        InOut: AsMut<[u8]> + for<'in_out> Extend<&'in_out u8>,
    {
        let nonce = self.nonce_sequence.advance()?;
        self.key.seal_in_place_append_tag(nonce, aad, in_out)
    }

    /// As [`LessSafeKey::seal_in_place_separate_tag`], with the next
    /// nonce.
    #[inline]
    pub fn seal_in_place_separate_tag<A>(
        &mut self,
        aad: Aad<A>,
        in_out: &mut [u8],
    ) -> Result<Tag, error::Unspecified>
    where
        A: AsRef<[u8]>,
    {
        let nonce = self.nonce_sequence.advance()?;
        self.key.seal_in_place_separate_tag(nonce, aad, in_out)
    }
}

/// A key that opens, taking each nonce from its sequence.
pub struct OpeningKey<N: NonceSequence> {
    key: LessSafeKey,
    nonce_sequence: N,
}

impl<N: NonceSequence> BoundKey<N> for OpeningKey<N> {
    fn new(key: UnboundKey, nonce_sequence: N) -> Self {
        Self {
            key: key.into_inner(),
            nonce_sequence,
        }
    }

    #[inline]
    fn algorithm(&self) -> &'static Algorithm {
        self.key.algorithm()
    }
}

impl<N: NonceSequence> fmt::Debug for OpeningKey<N> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        self.key.fmt_debug("OpeningKey", f)
    }
}

impl<N: NonceSequence> OpeningKey<N> {
    /// As [`LessSafeKey::open_in_place`], with the next nonce.
    #[inline]
    pub fn open_in_place<'in_out, A>(
        &mut self,
        aad: Aad<A>,
        in_out: &'in_out mut [u8],
    ) -> Result<&'in_out mut [u8], error::Unspecified>
    where
        A: AsRef<[u8]>,
    {
        let nonce = self.nonce_sequence.advance()?;
        self.key.open_in_place(nonce, aad, in_out)
    }

    /// As [`LessSafeKey::open_within`], with the next nonce.
    #[inline]
    pub fn open_within<'in_out, A>(
        &mut self,
        aad: Aad<A>,
        in_out: &'in_out mut [u8],
        ciphertext_and_tag: RangeFrom<usize>,
    ) -> Result<&'in_out mut [u8], error::Unspecified>
    where
        A: AsRef<[u8]>,
    {
        let nonce = self.nonce_sequence.advance()?;
        self.key.open_within(nonce, aad, in_out, ciphertext_and_tag)
    }
}

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

    extern crate std;
    use std::vec::Vec;

    fn hex(s: &str) -> Vec<u8> {
        (0..s.len())
            .step_by(2)
            .map(|i| u8::from_str_radix(&s[i..i + 2], 16).expect("hex"))
            .collect()
    }

    // RFC 8439 section 2.8.2.
    #[test]
    fn chacha20_poly1305_gives_the_published_output() {
        let key = hex("808182838485868788898a8b8c8d8e8f\
             909192939495969798999a9b9c9d9e9f");
        let nonce = hex("070000004041424344454647");
        let aad = hex("50515253c0c1c2c3c4c5c6c7");
        let plaintext = b"Ladies and Gentlemen of the class of '99: \
If I could offer you only one tip for the future, sunscreen would be it.";
        let key = LessSafeKey::new(
            UnboundKey::new(&CHACHA20_POLY1305, &key).expect("key"),
        );
        let mut in_out = plaintext.to_vec();
        key.seal_in_place_append_tag(
            Nonce::try_assume_unique_for_key(&nonce).expect("nonce"),
            Aad::from(&aad),
            &mut in_out,
        )
        .expect("seal");
        assert_eq!(
            &in_out[in_out.len() - 16..],
            &hex("1ae10b594f09e26a7e902ecbd0600691")[..]
        );
        assert_eq!(&in_out[..4], &hex("d31a8d34")[..]);
        let opened = key
            .open_in_place(
                Nonce::try_assume_unique_for_key(&nonce).expect("nonce"),
                Aad::from(&aad),
                &mut in_out,
            )
            .expect("open");
        assert_eq!(opened, &plaintext[..]);
    }

    fn round_trip(algorithm: &'static Algorithm) {
        let key_bytes = [0x11u8; 32];
        let key = LessSafeKey::new(
            UnboundKey::new(algorithm, &key_bytes[..algorithm.key_len()])
                .expect("key"),
        );
        let n = || Nonce::assume_unique_for_key([7u8; NONCE_LEN]);
        let message = *b"a message of no particular length";

        // Sealed with the tag apart, then opened from an offset, as a
        // TLS 1.2 record with an explicit nonce is.
        let mut buf = [0u8; 8 + 33 + 16];
        buf[8..41].copy_from_slice(&message);
        let tag = key
            .seal_in_place_separate_tag(n(), Aad::empty(), &mut buf[8..41])
            .expect("seal");
        buf[41..].copy_from_slice(tag.as_ref());
        let opened = key
            .open_within(n(), Aad::empty(), &mut buf, 8..)
            .expect("open");
        assert_eq!(opened, &message[..]);

        // A flipped bit is refused and the region zeroed.
        let mut sealed = message.to_vec();
        key.seal_in_place_append_tag(n(), Aad::from(b"hdr"), &mut sealed)
            .expect("seal");
        sealed[3] ^= 1;
        assert!(
            key.open_in_place(n(), Aad::from(b"hdr"), &mut sealed)
                .is_err()
        );
        assert!(sealed[..33].iter().all(|&b| b == 0));
    }

    #[test]
    fn every_algorithm_round_trips_and_refuses_forgery() {
        round_trip(&AES_128_GCM);
        round_trip(&AES_256_GCM);
        round_trip(&CHACHA20_POLY1305);
    }

    #[test]
    fn short_input_and_wrong_lengths_are_refused() {
        assert!(UnboundKey::new(&AES_128_GCM, &[0u8; 32]).is_err());
        assert!(Nonce::try_assume_unique_for_key(&[0u8; 11]).is_err());
        let key = LessSafeKey::new(
            UnboundKey::new(&AES_256_GCM, &[0u8; 32]).expect("key"),
        );
        let n = Nonce::assume_unique_for_key([0u8; NONCE_LEN]);
        assert!(key.open_in_place(n, Aad::empty(), &mut [0u8; 15]).is_err());
        let n = Nonce::assume_unique_for_key([0u8; NONCE_LEN]);
        assert!(
            key.open_within(n, Aad::empty(), &mut [0u8; 20], 5..)
                .is_err()
        );
    }

    #[test]
    fn algorithms_print_by_name() {
        extern crate std;
        assert_eq!(std::format!("{:?}", AES_128_GCM), "AES_128_GCM");
        let key = LessSafeKey::new(
            UnboundKey::new(&CHACHA20_POLY1305, &[0u8; 32]).expect("key"),
        );
        assert_eq!(
            std::format!("{key:?}"),
            "LessSafeKey { algorithm: CHACHA20_POLY1305 }"
        );
    }
}