cryptex 3.0.0

Store and retrieve secrets in OS keyrings, encrypted SQLite or Turso files, in-memory and browser keyrings, or under a YubiHSM 2 or Nitrokey 3
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
/*
    Copyright Michael Lodder. All Rights Reserved.
    SPDX-License-Identifier: Apache-2.0
*/
//! An encrypted, in-memory keyring.
//!
//! [`MemoryKeyring`] keeps every secret in a [`BTreeMap`] and never touches the
//! disk, so it builds for every target, including browsers and WASI. Secrets
//! only leave memory through [`MemoryKeyring::export`], which returns a single
//! AEAD-sealed blob, and return through [`MemoryKeyring::import`].
//!
//! ```
//! use cryptex::KeyRing;
//! use cryptex::memory::MemoryKeyring;
//!
//! let params: cryptex::memory::ConnectionParams =
//!     "password=1qaz2wsx3edc4rfv salt=0okm9ijn8uhb7ygv".parse().unwrap();
//! let mut keyring = MemoryKeyring::with_params(&params).unwrap();
//! keyring.set_secret("token", b"secret value").unwrap();
//!
//! // Persist the sealed blob wherever you like, then reopen it later.
//! let blob = keyring.export().unwrap();
//! // Reopening needs the same password (or raw key); the salt, cipher and
//! // Argon2 parameters come from the blob itself.
//! let mut reopened =
//!     MemoryKeyring::import(&blob, b"1qaz2wsx3edc4rfv").unwrap();
//! assert_eq!(reopened.get_secret("token").unwrap().as_slice(), b"secret value");
//! ```

use super::cipher::CipherAlgorithm;
use super::file_common::{self, check_stored_kdf_costs, derive_key_32};
use super::*;
use crate::error::KeyRingError;
use aes_gcm::aead::{Aead, KeyInit};
use std::collections::BTreeMap;
use zeroize::Zeroizing;

/// Connection parameters for [`MemoryKeyring`].
///
/// Accepted format:
/// ```text
/// password=mysecret salt=0okm9ijn8uhb7ygv [cipher=chacha20poly1305|aes256gcm]
/// ```
/// or, to supply the 32-byte key directly, `key=<hex>`.
pub type ConnectionParams = file_common::ConnectionParams<CipherAlgorithm>;

// Sealed blob layout. The whole header is the AEAD associated data, so the
// Argon2 parameters, salt and cipher travel with the blob and cannot be
// altered without failing the tag check.
//
// magic(8) | version(1) | kind(1) | cipher(1) | memory(4) | threads(4) |
// parallel(4) | salt_len(4) | salt(salt_len) | nonce(12) | ciphertext + tag
const BLOB_MAGIC: &[u8; 8] = b"CRYPTEX\0";
const BLOB_VERSION: u8 = 1;
const HEADER_LEN: usize = 8 + 1 + 1 + 1 + 4 + 4 + 4 + 4;
const NONCE_SIZE: usize = 12;

/// How the blob's key was produced.
mod kind {
    /// Argon2id over password + salt.
    pub(super) const ARGON2ID: u8 = 1;
    /// A caller-supplied 32-byte key.
    pub(super) const RAW_KEY: u8 = 2;
    /// PBKDF2-HMAC-SHA256 over password + salt (`fips` builds).
    pub(super) const PBKDF2_SHA256: u8 = 3;

    /// The password KDF this build creates and accepts.
    #[cfg(not(feature = "fips"))]
    pub(super) const PASSWORD: u8 = ARGON2ID;
    #[cfg(feature = "fips")]
    pub(super) const PASSWORD: u8 = PBKDF2_SHA256;
}

fn seal_cipher(cipher: CipherAlgorithm) -> u8 {
    match cipher {
        #[cfg(not(feature = "fips"))]
        CipherAlgorithm::ChaCha20Poly1305 => 1,
        CipherAlgorithm::Aes256Gcm => 2,
    }
}

fn open_cipher(tag: u8) -> Result<CipherAlgorithm> {
    match tag {
        #[cfg(not(feature = "fips"))]
        1 => Ok(CipherAlgorithm::ChaCha20Poly1305),
        2 => Ok(CipherAlgorithm::Aes256Gcm),
        other => Err(KeyRingError::GeneralError {
            msg: format!("unsupported cipher in keyring blob: {}", other),
        }),
    }
}

/// The error for a blob that will not open: wrong password or tampering.
fn invalid_blob() -> KeyRingError {
    KeyRingError::AccessDenied {
        msg: "Invalid key or corrupted keyring blob".to_string(),
    }
}

/// Authenticated encryption over a whole buffer, for either supported cipher.
enum Sealer {
    #[cfg(not(feature = "fips"))]
    ChaCha(Box<chacha20poly1305::ChaCha20Poly1305>),
    Aes(Box<aes_gcm::Aes256Gcm>),
}

impl Sealer {
    fn new(cipher: CipherAlgorithm, key: &[u8; 32]) -> Self {
        match cipher {
            #[cfg(not(feature = "fips"))]
            CipherAlgorithm::ChaCha20Poly1305 => Self::ChaCha(Box::new(
                chacha20poly1305::ChaCha20Poly1305::new(key.into()),
            )),
            CipherAlgorithm::Aes256Gcm => Self::Aes(Box::new(aes_gcm::Aes256Gcm::new(key.into()))),
        }
    }

    fn nonce(bytes: &[u8]) -> Option<chacha20poly1305::Nonce> {
        chacha20poly1305::Nonce::try_from(bytes).ok()
    }

    fn seal(&self, nonce: &[u8; NONCE_SIZE], aad: &[u8], plaintext: &[u8]) -> Option<Vec<u8>> {
        let nonce = Self::nonce(nonce)?;
        let payload = chacha20poly1305::aead::Payload {
            msg: plaintext,
            aad,
        };
        match self {
            #[cfg(not(feature = "fips"))]
            Self::ChaCha(c) => c.encrypt(&nonce, payload).ok(),
            Self::Aes(c) => c.encrypt(&nonce, payload).ok(),
        }
    }

    fn open(&self, nonce: &[u8], aad: &[u8], ciphertext: &[u8]) -> Option<Vec<u8>> {
        let nonce = Self::nonce(nonce)?;
        let payload = chacha20poly1305::aead::Payload {
            msg: ciphertext,
            aad,
        };
        match self {
            #[cfg(not(feature = "fips"))]
            Self::ChaCha(c) => c.decrypt(&nonce, payload).ok(),
            Self::Aes(c) => c.decrypt(&nonce, payload).ok(),
        }
    }
}

/// Length-prefixed encoding of the secret table:
/// `count(u32) | [id_len(u32) id value_len(u32) value]*`.
fn encode_secrets(secrets: &BTreeMap<String, Zeroizing<Vec<u8>>>) -> Result<Zeroizing<Vec<u8>>> {
    let too_long = |what: &str| KeyRingError::GeneralError {
        msg: format!("{} is too large to export", what),
    };
    // Size the buffer up front: a growing buffer copies the plaintext into
    // freed allocations, which zeroizing afterwards cannot reach.
    let mut len: usize = 4;
    for (id, value) in secrets {
        len = len
            .checked_add(4 + id.len() + 4 + value.len())
            .ok_or_else(|| too_long("the secret table"))?;
    }
    let mut out = Zeroizing::new(Vec::with_capacity(len));
    out.extend_from_slice(
        &u32::try_from(secrets.len())
            .map_err(|_| too_long("the secret count"))?
            .to_le_bytes(),
    );
    for (id, value) in secrets {
        let id_len = u32::try_from(id.len()).map_err(|_| too_long("a secret id"))?;
        let value_len = u32::try_from(value.len()).map_err(|_| too_long("a secret value"))?;
        out.extend_from_slice(&id_len.to_le_bytes());
        out.extend_from_slice(id.as_bytes());
        out.extend_from_slice(&value_len.to_le_bytes());
        out.extend_from_slice(value);
    }
    Ok(out)
}

/// Read a `u32` at `pos`, advancing past it.
fn read_u32(data: &[u8], pos: &mut usize) -> Option<u32> {
    let mut raw = [0u8; 4];
    raw.copy_from_slice(data.get(*pos..*pos + 4)?);
    *pos += 4;
    Some(u32::from_le_bytes(raw))
}

/// Decode the plaintext table produced by [`encode_secrets`], rejecting any
/// trailing bytes.
fn decode_secrets(data: &[u8]) -> Option<BTreeMap<String, Zeroizing<Vec<u8>>>> {
    let mut pos = 0usize;
    let count = read_u32(data, &mut pos)? as usize;
    let mut secrets = BTreeMap::new();
    for _ in 0..count {
        let id_len = read_u32(data, &mut pos)? as usize;
        let id = std::str::from_utf8(data.get(pos..pos.checked_add(id_len)?)?)
            .ok()?
            .to_string();
        pos += id_len;
        let value_len = read_u32(data, &mut pos)? as usize;
        let value = Zeroizing::new(data.get(pos..pos.checked_add(value_len)?)?.to_vec());
        pos += value_len;
        secrets.insert(id, value);
    }
    (pos == data.len()).then_some(secrets)
}

/// Header fields of a sealed blob.
struct Header<'a> {
    kind: u8,
    cipher: CipherAlgorithm,
    costs: [u32; 3],
    salt: &'a [u8],
}

/// A keyring that keeps its secrets in memory.
///
/// The key is derived once from the connection parameters and zeroized on
/// drop; [`export`](Self::export) seals everything under a fresh nonce.
pub struct MemoryKeyring {
    secrets: BTreeMap<String, Zeroizing<Vec<u8>>>,
    key: Zeroizing<[u8; 32]>,
    cipher: CipherAlgorithm,
    kind: u8,
    salt: Vec<u8>,
    /// The password KDF's costs; see [`ConnectionParams::kdf_costs`].
    costs: [u32; 3],
}

impl MemoryKeyring {
    /// Open an empty keyring from the connection parameters.
    pub fn with_params(params: &ConnectionParams) -> Result<Self> {
        let kind = if params.key.is_empty() {
            kind::PASSWORD
        } else {
            kind::RAW_KEY
        };
        Ok(Self {
            secrets: BTreeMap::new(),
            key: derive_key_32(params)?,
            cipher: params.cipher,
            kind,
            salt: params.salt.clone(),
            costs: params.kdf_costs(),
        })
    }

    /// Reopen a keyring from a blob returned by [`export`](Self::export).
    ///
    /// A blob made from a password derives its key from the salt and Argon2
    /// parameters stored in the blob, so it needs only the same password. A
    /// blob made from a raw key needs the same key.
    ///
    /// A wrong password or a modified blob returns
    /// [`KeyRingError::AccessDenied`].
    pub fn import(blob: &[u8], password_or_key: &[u8]) -> Result<Self> {
        let header = Self::parse_header(blob)?;
        // The costs sit in the unauthenticated header, so they are checked
        // before the KDF runs.
        check_stored_kdf_costs(header.costs)?;
        let mut params = ConnectionParams::default();
        if header.kind == kind::RAW_KEY {
            params.key = password_or_key.to_vec();
        } else {
            params.password = password_or_key.to_vec();
        }
        params.salt = header.salt.to_vec();
        params.set_kdf_costs(header.costs);
        params.cipher = header.cipher;
        let key = derive_key_32(&params).map_err(|_| invalid_blob())?;
        let aad_end = HEADER_LEN + header.salt.len();
        let body = &blob[aad_end..];
        let sealer = Sealer::new(header.cipher, &key);
        let plaintext = Zeroizing::new(
            sealer
                .open(&body[..NONCE_SIZE], &blob[..aad_end], &body[NONCE_SIZE..])
                .ok_or_else(invalid_blob)?,
        );
        let secrets = decode_secrets(&plaintext).ok_or_else(invalid_blob)?;
        Ok(Self {
            secrets,
            key,
            cipher: header.cipher,
            kind: header.kind,
            salt: header.salt.to_vec(),
            costs: header.costs,
        })
    }

    fn parse_header(blob: &[u8]) -> Result<Header<'_>> {
        if blob.len() < HEADER_LEN + NONCE_SIZE {
            return Err(invalid_blob());
        }
        if blob.get(..8) != Some(BLOB_MAGIC.as_slice()) {
            return Err(invalid_blob());
        }
        if blob[8] != BLOB_VERSION {
            return Err(KeyRingError::GeneralError {
                msg: format!("unsupported keyring blob version: {}", blob[8]),
            });
        }
        let blob_kind = blob[9];
        match blob_kind {
            kind::RAW_KEY => {}
            // A guard, because `PASSWORD` is one of the two KDF kinds below.
            id if id == kind::PASSWORD => {}
            kind::ARGON2ID | kind::PBKDF2_SHA256 => {
                return Err(KeyRingError::GeneralError {
                    msg: "this keyring blob was made by a build with a different password \
                          KDF (fips builds use PBKDF2, others Argon2id)"
                        .to_string(),
                });
            }
            _ => return Err(invalid_blob()),
        }
        let mut pos = 11;
        let memory = read_u32(blob, &mut pos).ok_or_else(invalid_blob)?;
        let threads = read_u32(blob, &mut pos).ok_or_else(invalid_blob)?;
        let parallel = read_u32(blob, &mut pos).ok_or_else(invalid_blob)?;
        let salt_len = read_u32(blob, &mut pos).ok_or_else(invalid_blob)? as usize;
        // `usize` is 32 bits on wasm32, so this addition can wrap before a
        // bounds check would notice.
        let salt_end = HEADER_LEN.checked_add(salt_len).ok_or_else(invalid_blob)?;
        let salt = blob.get(HEADER_LEN..salt_end).ok_or_else(invalid_blob)?;
        // The body must hold a nonce and at least the AEAD tag.
        let body_start = salt_end
            .checked_add(NONCE_SIZE + 16)
            .ok_or_else(invalid_blob)?;
        if body_start > blob.len() {
            return Err(invalid_blob());
        }
        Ok(Header {
            kind: blob_kind,
            cipher: open_cipher(blob[10]).map_err(|_| invalid_blob())?,
            costs: [memory, threads, parallel],
            salt,
        })
    }

    /// Seal every secret into a single AEAD blob.
    ///
    /// The blob carries the salt, Argon2 parameters, cipher and key kind, so
    /// [`import`](Self::import) needs only the same password (or raw key).
    pub fn export(&self) -> Result<Vec<u8>> {
        let mut nonce = [0u8; NONCE_SIZE];
        getrandom::fill(&mut nonce).map_err(|e| KeyRingError::GeneralError {
            msg: format!("unable to generate a nonce: {}", e),
        })?;

        let plaintext = encode_secrets(&self.secrets)?;
        let salt_len = u32::try_from(self.salt.len()).map_err(|_| KeyRingError::GeneralError {
            msg: "salt is too long to export".to_string(),
        })?;
        let mut header = Vec::with_capacity(HEADER_LEN + self.salt.len());
        header.extend_from_slice(BLOB_MAGIC);
        header.push(BLOB_VERSION);
        header.push(self.kind);
        header.push(seal_cipher(self.cipher));
        for cost in self.costs {
            header.extend_from_slice(&cost.to_le_bytes());
        }
        header.extend_from_slice(&salt_len.to_le_bytes());
        header.extend_from_slice(&self.salt);

        let ciphertext = Sealer::new(self.cipher, &self.key)
            .seal(&nonce, &header, &plaintext)
            .ok_or_else(|| KeyRingError::GeneralError {
                msg: "unable to encrypt the keyring".to_string(),
            })?;
        let mut blob = header;
        blob.extend_from_slice(&nonce);
        blob.extend_from_slice(&ciphertext);
        Ok(blob)
    }

    /// Every id in the keyring, in order.
    pub fn keys(&self) -> Vec<String> {
        self.secrets.keys().cloned().collect()
    }

    /// Re-encrypt under new connection parameters, optionally with a new cipher.
    ///
    /// The secrets stay in memory; only the key, salt and cipher change, so
    /// the next [`export`](Self::export) uses the new parameters.
    pub fn rekey(&mut self, new_params: &ConnectionParams) -> Result<()> {
        let key = derive_key_32(new_params)?;
        self.kind = if new_params.key.is_empty() {
            kind::PASSWORD
        } else {
            kind::RAW_KEY
        };
        self.salt = new_params.salt.clone();
        self.costs = new_params.kdf_costs();
        self.cipher = new_params.cipher;
        self.key = key;
        Ok(())
    }

    /// The cipher this keyring seals blobs with.
    pub fn cipher(&self) -> CipherAlgorithm {
        self.cipher
    }
}

impl NewKeyRing for MemoryKeyring {
    fn new<S: AsRef<str>>(lock_key: S) -> Result<Self> {
        let params = lock_key.as_ref().parse::<ConnectionParams>()?;
        Self::with_params(&params)
    }
}

impl DynKeyRing for MemoryKeyring {
    fn get_secret(&mut self, id: &str) -> Result<KeyRingSecret> {
        match self.secrets.get(id) {
            Some(value) => Ok(KeyRingSecret(value.to_vec())),
            None => Err(KeyRingError::ItemNotFound),
        }
    }

    fn set_secret(&mut self, id: &str, secret: &[u8]) -> Result<()> {
        self.secrets
            .insert(id.to_string(), Zeroizing::new(secret.to_vec()));
        Ok(())
    }

    fn delete_secret(&mut self, id: &str) -> Result<()> {
        match self.secrets.remove(id) {
            Some(mut value) => {
                zeroize::Zeroize::zeroize(&mut *value);
                Ok(())
            }
            None => Err(KeyRingError::ItemNotFound),
        }
    }
}

#[cfg(test)]
mod tests {
    // Only `DynKeyRing` is imported, so `kr.get_secret(..)` is unambiguous.
    use super::super::DynKeyRing;
    use super::{CipherAlgorithm, ConnectionParams, HEADER_LEN, MemoryKeyring};
    use crate::error::KeyRingError;
    use argon2::Params as Argon2Params;

    const PASSWORD: &[u8] = b"1qaz2wsx3edc4rfv";

    fn params(s: &str) -> ConnectionParams {
        s.parse().unwrap()
    }

    fn password_params(extra: &str) -> ConnectionParams {
        params(&format!(
            "password=1qaz2wsx3edc4rfv salt=0okm9ijn8uhb7ygv {extra}"
        ))
    }

    #[test]
    fn round_trip_and_persistence() {
        let mut kr = MemoryKeyring::with_params(&password_params("")).unwrap();
        kr.set_secret("a", b"one").unwrap();
        kr.set_secret("b", b"two").unwrap();
        kr.delete_secret("b").unwrap();
        assert_eq!(kr.get_secret("a").unwrap().as_slice(), b"one");
        assert!(matches!(
            kr.get_secret("b"),
            Err(KeyRingError::ItemNotFound)
        ));

        let blob = kr.export().unwrap();
        let mut reopened = MemoryKeyring::import(&blob, PASSWORD).unwrap();
        assert_eq!(reopened.get_secret("a").unwrap().as_slice(), b"one");
        reopened.set_secret("c", b"three").unwrap();
        assert_eq!(reopened.get_secret("c").unwrap().as_slice(), b"three");
    }

    #[test]
    fn keys_lists_the_ids_in_order() {
        let mut kr = MemoryKeyring::with_params(&password_params("")).unwrap();
        assert!(kr.keys().is_empty());
        kr.set_secret("b", b"two").unwrap();
        kr.set_secret("a", b"one").unwrap();
        assert_eq!(kr.keys(), ["a", "b"]);
        kr.delete_secret("a").unwrap();
        assert_eq!(kr.keys(), ["b"]);
    }

    #[test]
    fn empty_keyring_round_trips() {
        let kr = MemoryKeyring::with_params(&password_params("")).unwrap();
        let blob = kr.export().unwrap();
        let mut reopened = MemoryKeyring::import(&blob, PASSWORD).unwrap();
        assert!(matches!(
            reopened.get_secret("missing"),
            Err(KeyRingError::ItemNotFound)
        ));
    }

    #[test]
    fn wrong_password_is_denied() {
        let mut kr = MemoryKeyring::with_params(&password_params("")).unwrap();
        kr.set_secret("a", b"one").unwrap();
        let blob = kr.export().unwrap();
        assert!(matches!(
            MemoryKeyring::import(&blob, b"wrong"),
            Err(KeyRingError::AccessDenied { .. })
        ));
    }

    /// A blob header with whatever Argon2 costs a hostile image would ask for.
    fn blob_with_costs(memory: u32, threads: u32, parallel: u32, salt_len: u32) -> Vec<u8> {
        let mut blob = Vec::new();
        blob.extend_from_slice(b"CRYPTEX\0");
        blob.push(1); // version
        blob.push(super::kind::PASSWORD);
        blob.push(super::seal_cipher(CipherAlgorithm::default()));
        blob.extend_from_slice(&memory.to_le_bytes());
        blob.extend_from_slice(&threads.to_le_bytes());
        blob.extend_from_slice(&parallel.to_le_bytes());
        blob.extend_from_slice(&salt_len.to_le_bytes());
        blob.extend_from_slice(b"saltsalt");
        blob.extend_from_slice(&[0u8; super::NONCE_SIZE + 16]);
        blob
    }

    #[cfg(feature = "fips")]
    #[test]
    fn fips_blobs_record_pbkdf2_and_aes() {
        let kr = MemoryKeyring::with_params(&password_params("")).unwrap();
        let blob = kr.export().unwrap();
        assert_eq!(blob[9], super::kind::PBKDF2_SHA256);
        assert_eq!(blob[10], super::seal_cipher(CipherAlgorithm::Aes256Gcm));
        assert!(MemoryKeyring::import(&blob, PASSWORD).is_ok());
    }

    #[test]
    fn import_refuses_unreasonable_costs_before_deriving() {
        // These sit in an unauthenticated header, so they must be bounded
        // before the KDF tries to honour them. A fips build reads only the
        // first, as the PBKDF2 iteration count.
        for (memory, threads, parallel) in [
            (u32::MAX, 1, 1),
            #[cfg(not(feature = "fips"))]
            (Argon2Params::DEFAULT_M_COST, u32::MAX, 1),
            #[cfg(not(feature = "fips"))]
            (Argon2Params::DEFAULT_M_COST, 1, u32::MAX),
            #[cfg(feature = "fips")]
            (crate::file_common::MAX_ITERATIONS + 1, 0, 0),
        ] {
            let blob = blob_with_costs(memory, threads, parallel, 8);
            assert!(matches!(
                MemoryKeyring::import(&blob, PASSWORD),
                Err(KeyRingError::GeneralError { .. })
            ));
        }
    }

    #[test]
    fn import_refuses_a_salt_length_that_would_overflow() {
        // `usize` is 32 bits on wasm32, where the old bounds check wrapped and
        // a debug build then panicked slicing the blob.
        let blob = blob_with_costs(
            Argon2Params::DEFAULT_M_COST,
            Argon2Params::DEFAULT_T_COST,
            Argon2Params::DEFAULT_P_COST,
            u32::MAX,
        );
        assert!(matches!(
            MemoryKeyring::import(&blob, PASSWORD),
            Err(KeyRingError::AccessDenied { .. })
        ));
    }

    #[test]
    fn a_duplicate_parameter_is_refused() {
        // A password containing ` salt=` used to silently replace the salt.
        assert!(
            "password=a salt=b salt=c"
                .parse::<ConnectionParams>()
                .is_err()
        );
        assert!("password=a password=b".parse::<ConnectionParams>().is_err());
    }

    #[test]
    fn tampering_is_detected() {
        let mut kr = MemoryKeyring::with_params(&password_params("")).unwrap();
        kr.set_secret("a", b"one").unwrap();
        let blob = kr.export().unwrap();

        // Header, salt and body are all authenticated.
        for index in [9usize, 10, 12, HEADER_LEN + 1, blob.len() - 1] {
            let mut flipped = blob.clone();
            flipped[index] ^= 0x80;
            assert!(
                MemoryKeyring::import(&flipped, PASSWORD).is_err(),
                "flipping byte {} went undetected",
                index
            );
        }

        assert!(MemoryKeyring::import(&blob[..blob.len() - 1], PASSWORD).is_err());
        assert!(MemoryKeyring::import(b"", PASSWORD).is_err());
    }

    #[cfg(not(feature = "fips"))]
    #[test]
    fn both_ciphers_and_rekey() {
        for (cipher, other) in [
            ("chacha20poly1305", "aes256gcm"),
            ("aes256gcm", "chacha20poly1305"),
        ] {
            let mut kr =
                MemoryKeyring::with_params(&password_params(&format!("cipher={cipher}"))).unwrap();
            kr.set_secret("a", b"one").unwrap();
            let mut reopened = MemoryKeyring::import(&kr.export().unwrap(), PASSWORD).unwrap();
            assert_eq!(reopened.get_secret("a").unwrap().as_slice(), b"one");

            reopened
                .rekey(&params(&format!(
                    "password=new_password salt=another_salt_value cipher={other}"
                )))
                .unwrap();
            assert_eq!(
                reopened.cipher(),
                if other == "aes256gcm" {
                    CipherAlgorithm::Aes256Gcm
                } else {
                    CipherAlgorithm::ChaCha20Poly1305
                }
            );

            let rekeyed = reopened.export().unwrap();
            assert!(MemoryKeyring::import(&rekeyed, PASSWORD).is_err());
            let mut again = MemoryKeyring::import(&rekeyed, b"new_password").unwrap();
            assert_eq!(again.get_secret("a").unwrap().as_slice(), b"one");
        }
    }

    #[test]
    fn raw_key_blobs_need_the_key() {
        let key = "0f1e2d3c4b5a69788796a5b4c3d2e1f00f1e2d3c4b5a69788796a5b4c3d2e1f0";
        let mut kr =
            MemoryKeyring::with_params(&params(&format!("key={} salt=ignored_salt_value", key)))
                .unwrap();
        kr.set_secret("a", b"one").unwrap();
        let blob = kr.export().unwrap();

        let key_bytes = hex::decode(key).unwrap();
        let mut reopened = MemoryKeyring::import(&blob, &key_bytes).unwrap();
        assert_eq!(reopened.get_secret("a").unwrap().as_slice(), b"one");
        assert!(MemoryKeyring::import(&blob, PASSWORD).is_err());

        // A raw-key blob carries no password, so the password path must fail.
        assert!(MemoryKeyring::import(&blob, b"anything").is_err());
    }

    #[test]
    fn blobs_carry_no_plaintext() {
        let mut kr = MemoryKeyring::with_params(&password_params("")).unwrap();
        kr.set_secret("account", b"super-secret-value").unwrap();
        let blob = kr.export().unwrap();
        assert!(!blob.windows(18).any(|w| w == b"super-secret-value"));
        assert!(!blob.windows(7).any(|w| w == b"account"));
    }

    #[test]
    fn ids_and_values_survive_odd_characters() {
        let mut kr = MemoryKeyring::with_params(&password_params("")).unwrap();
        kr.set_secret("ünïcode/ключ", b"\x00\x01\x02bytes").unwrap();
        let blob = kr.export().unwrap();
        let mut reopened = MemoryKeyring::import(&blob, PASSWORD).unwrap();
        assert_eq!(
            reopened.get_secret("ünïcode/ключ").unwrap().as_slice(),
            b"\x00\x01\x02bytes"
        );
    }
}