oxicrypto-kdf 0.1.0

Pure Rust KDF implementations for OxiCrypto (HKDF, PBKDF2, Argon2id, scrypt)
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
#![forbid(unsafe_code)]

//! Pure Rust KDF implementations for the OxiCrypto stack.
//!
//! | Function | Module | Backend |
//! |----------|--------|---------|
//! | HKDF-SHA-256 / SHA-512 | (inline) | `hkdf` |
//! | HKDF-Expand-Label (TLS 1.3 / QUIC) | [`hkdf_label`] | `hkdf` |
//! | PBKDF2-SHA-256 / SHA-512 | [`pbkdf2_kdf`] | `pbkdf2` |
//! | Argon2id | [`argon2_kdf`] | `argon2` |
//! | scrypt | [`scrypt_kdf`] | `scrypt` |
//! | Balloon (SHA-256 / SHA-512) | [`balloon`] | `sha2` |

pub mod argon2_kdf;
pub mod balloon;
pub mod hkdf_label;
pub mod kbkdf;
pub mod pbkdf2_kdf;
pub mod scrypt_kdf;
pub mod stretcher;

// ── OWASP 2023 minimum iteration counts ───────────────────────────────────────

/// OWASP 2023 Password Storage Cheat Sheet minimum iteration count for
/// PBKDF2-HMAC-SHA-256.
///
/// Reference: <https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html>
pub const PBKDF2_SHA256_MIN_ITERATIONS: u32 = 600_000;

/// OWASP 2023 Password Storage Cheat Sheet minimum iteration count for
/// PBKDF2-HMAC-SHA-512.
///
/// SHA-512 is ~2× faster than SHA-256 per round on 64-bit CPUs, so the
/// equivalent minimum is approximately 210,000.
pub const PBKDF2_SHA512_MIN_ITERATIONS: u32 = 210_000;

pub use argon2_kdf::{
    argon2d_derive, argon2i_derive, argon2id_derive, argon2id_to_phc_string, argon2id_verify_phc,
    Argon2Params, Argon2idHasher,
};
pub use balloon::{
    balloon_sha256, balloon_sha256_secret, balloon_sha512, balloon_sha512_secret, BalloonHasher,
    BalloonParams, BalloonVariant, BALLOON_DELTA,
};
pub use hkdf_label::{hkdf_expand_label_sha256, hkdf_expand_label_sha384};
pub use kbkdf::{
    kbkdf_counter_hmac_sha256, kbkdf_counter_hmac_sha256_secret, kbkdf_counter_hmac_sha384,
    kbkdf_counter_hmac_sha512,
};
pub use pbkdf2_kdf::{
    pbkdf2_sha256, pbkdf2_sha512, Pbkdf2Params, Pbkdf2Sha256Hasher, Pbkdf2Sha512Hasher,
};
pub use scrypt_kdf::{scrypt_derive, ScryptHasher, ScryptParams};
pub use stretcher::{
    Argon2idStretchParams, BalloonStretchParams, KeyStretcher, Pbkdf2StretchParams,
    ScryptStretchParams, StretchParams, Stretcher,
};

use hkdf::Hkdf;
use oxicrypto_core::{CryptoError, Kdf, PasswordHash};
use subtle::ConstantTimeEq;

// ── HKDF-SHA-256 ──────────────────────────────────────────────────────────────

/// HKDF-SHA-256 key derivation function.
#[derive(Debug, Default, Clone, Copy)]
pub struct HkdfSha256;

impl Kdf for HkdfSha256 {
    fn name(&self) -> &'static str {
        "HKDF-SHA-256"
    }
    fn derive(
        &self,
        ikm: &[u8],
        salt: &[u8],
        info: &[u8],
        okm_out: &mut [u8],
    ) -> Result<(), CryptoError> {
        if okm_out.is_empty() {
            return Err(CryptoError::BadInput);
        }
        let salt_opt = if salt.is_empty() { None } else { Some(salt) };
        let hk = Hkdf::<sha2::Sha256>::new(salt_opt, ikm);
        hk.expand(info, okm_out)
            .map_err(|_| CryptoError::Internal("HKDF expand failed (output too long)"))?;
        Ok(())
    }
}

// ── HKDF-SHA-512 ──────────────────────────────────────────────────────────────

/// HKDF-SHA-512 key derivation function.
#[derive(Debug, Default, Clone, Copy)]
pub struct HkdfSha512;

impl Kdf for HkdfSha512 {
    fn name(&self) -> &'static str {
        "HKDF-SHA-512"
    }
    fn derive(
        &self,
        ikm: &[u8],
        salt: &[u8],
        info: &[u8],
        okm_out: &mut [u8],
    ) -> Result<(), CryptoError> {
        if okm_out.is_empty() {
            return Err(CryptoError::BadInput);
        }
        let salt_opt = if salt.is_empty() { None } else { Some(salt) };
        let hk = Hkdf::<sha2::Sha512>::new(salt_opt, ikm);
        hk.expand(info, okm_out)
            .map_err(|_| CryptoError::Internal("HKDF expand failed (output too long)"))?;
        Ok(())
    }
}

// ── HKDF-SHA-384 ──────────────────────────────────────────────────────────────

/// HKDF-SHA-384 key derivation function.
#[derive(Debug, Default, Clone, Copy)]
pub struct HkdfSha384;

impl Kdf for HkdfSha384 {
    fn name(&self) -> &'static str {
        "HKDF-SHA-384"
    }
    fn derive(
        &self,
        ikm: &[u8],
        salt: &[u8],
        info: &[u8],
        okm_out: &mut [u8],
    ) -> Result<(), CryptoError> {
        if okm_out.is_empty() {
            return Err(CryptoError::BadInput);
        }
        let salt_opt = if salt.is_empty() { None } else { Some(salt) };
        let hk = Hkdf::<sha2::Sha384>::new(salt_opt, ikm);
        hk.expand(info, okm_out)
            .map_err(|_| CryptoError::Internal("HKDF-SHA-384 expand failed (output too long)"))?;
        Ok(())
    }
}

// ── HKDF Extract-only / Expand-only (RFC 5869 separated phases) ─────────────

/// Perform HKDF-Extract with SHA-256, returning the pseudorandom key (PRK).
///
/// This is the extraction phase only (RFC 5869 Section 2.2).
/// The PRK is always 32 bytes (the output size of SHA-256).
///
/// Used by protocols like TLS 1.3 that need separated extract/expand.
#[must_use]
pub fn hkdf_sha256_extract(salt: &[u8], ikm: &[u8]) -> [u8; 32] {
    let salt_opt = if salt.is_empty() { None } else { Some(salt) };
    let (prk, _) = Hkdf::<sha2::Sha256>::extract(salt_opt, ikm);
    let mut out = [0u8; 32];
    out.copy_from_slice(&prk);
    out
}

/// Perform HKDF-Expand with SHA-256 from a pre-extracted PRK.
///
/// This is the expansion phase only (RFC 5869 Section 2.3).
/// `prk` should be the output of [`hkdf_sha256_extract`] (32 bytes).
#[must_use = "HKDF expand result must be checked"]
pub fn hkdf_sha256_expand(prk: &[u8], info: &[u8], okm_out: &mut [u8]) -> Result<(), CryptoError> {
    if okm_out.is_empty() {
        return Err(CryptoError::BadInput);
    }
    let hk = Hkdf::<sha2::Sha256>::from_prk(prk).map_err(|_| CryptoError::InvalidKey)?;
    hk.expand(info, okm_out)
        .map_err(|_| CryptoError::Internal("HKDF-SHA-256 expand failed (output too long)"))?;
    Ok(())
}

/// Perform HKDF-Extract with SHA-384, returning the pseudorandom key (PRK).
///
/// The PRK is always 48 bytes (the output size of SHA-384).
#[must_use]
pub fn hkdf_sha384_extract(salt: &[u8], ikm: &[u8]) -> [u8; 48] {
    let salt_opt = if salt.is_empty() { None } else { Some(salt) };
    let (prk, _) = Hkdf::<sha2::Sha384>::extract(salt_opt, ikm);
    let mut out = [0u8; 48];
    out.copy_from_slice(&prk);
    out
}

/// Perform HKDF-Expand with SHA-384 from a pre-extracted PRK.
#[must_use = "HKDF expand result must be checked"]
pub fn hkdf_sha384_expand(prk: &[u8], info: &[u8], okm_out: &mut [u8]) -> Result<(), CryptoError> {
    if okm_out.is_empty() {
        return Err(CryptoError::BadInput);
    }
    let hk = Hkdf::<sha2::Sha384>::from_prk(prk).map_err(|_| CryptoError::InvalidKey)?;
    hk.expand(info, okm_out)
        .map_err(|_| CryptoError::Internal("HKDF-SHA-384 expand failed (output too long)"))?;
    Ok(())
}

/// Perform HKDF-Extract with SHA-512, returning the pseudorandom key (PRK).
///
/// The PRK is always 64 bytes (the output size of SHA-512).
#[must_use]
pub fn hkdf_sha512_extract(salt: &[u8], ikm: &[u8]) -> [u8; 64] {
    let salt_opt = if salt.is_empty() { None } else { Some(salt) };
    let (prk, _) = Hkdf::<sha2::Sha512>::extract(salt_opt, ikm);
    let mut out = [0u8; 64];
    out.copy_from_slice(&prk);
    out
}

/// Perform HKDF-Expand with SHA-512 from a pre-extracted PRK.
#[must_use = "HKDF expand result must be checked"]
pub fn hkdf_sha512_expand(prk: &[u8], info: &[u8], okm_out: &mut [u8]) -> Result<(), CryptoError> {
    if okm_out.is_empty() {
        return Err(CryptoError::BadInput);
    }
    let hk = Hkdf::<sha2::Sha512>::from_prk(prk).map_err(|_| CryptoError::InvalidKey)?;
    hk.expand(info, okm_out)
        .map_err(|_| CryptoError::Internal("HKDF-SHA-512 expand failed (output too long)"))?;
    Ok(())
}

// ── HKDF derive-to-Vec convenience wrappers ───────────────────────────────────

/// Derive `len` bytes from `ikm`, `salt`, and `info` using HKDF-SHA-256, returning
/// the output as an owned `Vec<u8>`.
///
/// This is a convenience wrapper around [`HkdfSha256::derive`] (which performs
/// the full extract+expand sequence per RFC 5869).
///
/// # Errors
/// Returns [`CryptoError::BadInput`] if `len == 0` or if the requested output
/// exceeds 255 × 32 bytes (HKDF-SHA-256 maximum).
#[must_use = "HKDF derive result must be checked"]
pub fn hkdf_sha256_derive_to_vec(
    ikm: &[u8],
    salt: &[u8],
    info: &[u8],
    len: usize,
) -> Result<Vec<u8>, CryptoError> {
    if len == 0 {
        return Err(CryptoError::BadInput);
    }
    let mut out = vec![0u8; len];
    HkdfSha256.derive(ikm, salt, info, &mut out)?;
    Ok(out)
}

/// Derive `len` bytes from `ikm`, `salt`, and `info` using HKDF-SHA-384, returning
/// the output as an owned `Vec<u8>`.
///
/// # Errors
/// Returns [`CryptoError::BadInput`] if `len == 0` or if the requested output
/// exceeds 255 × 48 bytes (HKDF-SHA-384 maximum).
#[must_use = "HKDF derive result must be checked"]
pub fn hkdf_sha384_derive_to_vec(
    ikm: &[u8],
    salt: &[u8],
    info: &[u8],
    len: usize,
) -> Result<Vec<u8>, CryptoError> {
    if len == 0 {
        return Err(CryptoError::BadInput);
    }
    let mut out = vec![0u8; len];
    HkdfSha384.derive(ikm, salt, info, &mut out)?;
    Ok(out)
}

/// Derive `len` bytes from `ikm`, `salt`, and `info` using HKDF-SHA-512, returning
/// the output as an owned `Vec<u8>`.
///
/// # Errors
/// Returns [`CryptoError::BadInput`] if `len == 0` or if the requested output
/// exceeds 255 × 64 bytes (HKDF-SHA-512 maximum).
#[must_use = "HKDF derive result must be checked"]
pub fn hkdf_sha512_derive_to_vec(
    ikm: &[u8],
    salt: &[u8],
    info: &[u8],
    len: usize,
) -> Result<Vec<u8>, CryptoError> {
    if len == 0 {
        return Err(CryptoError::BadInput);
    }
    let mut out = vec![0u8; len];
    HkdfSha512.derive(ikm, salt, info, &mut out)?;
    Ok(out)
}

// ── Salt generation helpers ────────────────────────────────────────────────────

/// Generate a random 16-byte salt using the system CSPRNG.
///
/// Suitable for PBKDF2 (recommended ≥ 16 bytes per NIST SP 800-132) and
/// Argon2id (requires ≥ 8 bytes per RFC 9106).
///
/// # Errors
/// Returns [`CryptoError::Rng`] if the OS entropy source is unavailable.
#[must_use = "generated salt result must be checked"]
pub fn generate_salt_16() -> Result<[u8; 16], CryptoError> {
    let bytes = oxicrypto_rand::random_bytes(16)?;
    let mut out = [0u8; 16];
    out.copy_from_slice(&bytes);
    Ok(out)
}

/// Generate a random 32-byte salt using the system CSPRNG.
///
/// Suitable for Argon2id and scrypt where a longer salt provides additional
/// domain separation.
///
/// # Errors
/// Returns [`CryptoError::Rng`] if the OS entropy source is unavailable.
#[must_use = "generated salt result must be checked"]
pub fn generate_salt_32() -> Result<[u8; 32], CryptoError> {
    let bytes = oxicrypto_rand::random_bytes(32)?;
    let mut out = [0u8; 32];
    out.copy_from_slice(&bytes);
    Ok(out)
}

// ---------------------------------------------------------------------------
// verify_password — constant-time password verification
// ---------------------------------------------------------------------------

/// Verify a password by re-hashing and comparing in constant time.
///
/// Hashes `password` with `salt` using `hasher` into a temporary buffer of
/// `expected.len()` bytes, then compares the result to `expected` using
/// [`subtle::ConstantTimeEq`].  The comparison time does not depend on the
/// position of the first differing byte.
///
/// # Errors
/// - Returns `Err(CryptoError::BadInput)` if `expected` is empty.
/// - Returns the underlying [`CryptoError`] if hashing fails (e.g. bad salt length).
/// - Returns `Err(CryptoError::InvalidTag)` if the password does not match.
///
/// # Example
/// ```ignore
/// use oxicrypto_kdf::{Argon2idHasher, Argon2Params, verify_password};
///
/// let hasher = Argon2idHasher::new(Argon2Params::TEST_PARAMS);
/// let salt   = b"0123456789abcdef";
/// let mut expected = [0u8; 32];
/// hasher.hash_password(b"password", salt, &hasher.params, &mut expected).unwrap();
///
/// verify_password(&hasher, b"password", salt, &expected).unwrap();        // ok
/// assert!(verify_password(&hasher, b"wrong", salt, &expected).is_err()); // rejected
/// ```
#[must_use = "password verification result must be checked"]
pub fn verify_password<H>(
    hasher: &H,
    password: &[u8],
    salt: &[u8],
    expected: &[u8],
) -> Result<(), CryptoError>
where
    H: PasswordHash,
{
    if expected.is_empty() {
        return Err(CryptoError::BadInput);
    }

    // Allocate a stack-sized temporary buffer.  For passwords the expected
    // output is typically 16–64 bytes, so heap allocation is not required;
    // but we use a Vec here to support arbitrary output lengths.
    let mut computed = vec![0u8; expected.len()];

    // Use empty params — each concrete hasher uses its own stored params.
    struct NullParams;
    impl oxicrypto_core::PasswordHashParams for NullParams {
        fn memory_cost(&self) -> Option<u32> {
            None
        }
        fn time_cost(&self) -> Option<u32> {
            None
        }
        fn parallelism(&self) -> Option<u32> {
            None
        }
    }

    hasher.hash_password(password, salt, &NullParams, &mut computed)?;

    // Constant-time comparison: returns 0x01 iff equal.
    let ok: bool = computed.ct_eq(expected).into();
    if ok {
        Ok(())
    } else {
        Err(CryptoError::InvalidTag)
    }
}

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

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

    // RFC 5869 Test Case 1 for HKDF-SHA-256
    // Hash = SHA-256
    // IKM  = 0x0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b (22 bytes)
    // salt = 0x000102030405060708090a0b0c (13 bytes)
    // info = 0xf0f1f2f3f4f5f6f7f8f9 (10 bytes)
    // L    = 42 bytes
    // OKM  = 0x3cb25f25faacd57a90434f64d0362f2a2d2d0a90cf1a5a4c5db02d56ecc4c5bf34007208d5b887185865
    #[test]
    fn hkdf_sha256_rfc5869_tc1() {
        let ikm = hex_decode("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b");
        let salt = hex_decode("000102030405060708090a0b0c");
        let info = hex_decode("f0f1f2f3f4f5f6f7f8f9");
        let expected = hex_decode(
            "3cb25f25faacd57a90434f64d0362f2a2d2d0a90cf1a5a4c5db02d56ecc4c5bf34007208d5b887185865",
        );

        let kdf = HkdfSha256;
        let mut okm = vec![0u8; 42];
        kdf.derive(&ikm, &salt, &info, &mut okm)
            .expect("HKDF-SHA-256 RFC5869 TC1 failed");
        assert_eq!(okm, expected, "HKDF-SHA-256 RFC5869 TC1 mismatch");
    }

    #[test]
    fn hkdf_sha256_empty_salt() {
        // Empty salt causes HKDF to use a zero-filled salt of hash length.
        let kdf = HkdfSha256;
        let mut okm = [0u8; 32];
        kdf.derive(b"input key material", b"", b"info", &mut okm)
            .expect("HKDF with empty salt failed");
        assert_ne!(okm, [0u8; 32]);
    }

    #[test]
    fn hkdf_sha512_round_trip() {
        let kdf = HkdfSha512;
        let mut okm1 = [0u8; 64];
        let mut okm2 = [0u8; 64];
        kdf.derive(b"secret", b"salt", b"info", &mut okm1).unwrap();
        kdf.derive(b"secret", b"salt", b"info", &mut okm2).unwrap();
        assert_eq!(okm1, okm2, "HKDF-SHA-512 must be deterministic");
        assert_ne!(okm1, [0u8; 64]);
    }

    #[test]
    fn hkdf_empty_output_errors() {
        let kdf = HkdfSha256;
        let result = kdf.derive(b"ikm", b"salt", b"info", &mut []);
        assert_eq!(result, Err(CryptoError::BadInput));
    }

    // ── HKDF-SHA-384 ─────────────────────────────────────────────────────────

    #[test]
    fn hkdf_sha384_round_trip() {
        let kdf = HkdfSha384;
        let mut okm1 = [0u8; 48];
        let mut okm2 = [0u8; 48];
        kdf.derive(b"secret", b"salt", b"info", &mut okm1)
            .expect("derive 1 failed");
        kdf.derive(b"secret", b"salt", b"info", &mut okm2)
            .expect("derive 2 failed");
        assert_eq!(okm1, okm2, "HKDF-SHA-384 must be deterministic");
        assert_ne!(okm1, [0u8; 48]);
    }

    #[test]
    fn hkdf_sha384_empty_output_errors() {
        let kdf = HkdfSha384;
        let result = kdf.derive(b"ikm", b"salt", b"info", &mut []);
        assert_eq!(result, Err(CryptoError::BadInput));
    }

    // ── Extract-only / Expand-only ───────────────────────────────────────────

    #[test]
    fn hkdf_sha256_extract_expand_equivalent() {
        // Extract+Expand should produce the same result as the full Kdf::derive.
        let ikm = b"input key material";
        let salt = b"salt value";
        let info = b"info";

        // Full derive.
        let kdf = HkdfSha256;
        let mut okm_full = [0u8; 42];
        kdf.derive(ikm, salt, info, &mut okm_full)
            .expect("full derive failed");

        // Separated extract + expand.
        let prk = hkdf_sha256_extract(salt, ikm);
        let mut okm_sep = [0u8; 42];
        hkdf_sha256_expand(&prk, info, &mut okm_sep).expect("expand failed");

        assert_eq!(okm_full, okm_sep, "Extract+Expand must equal full derive");
    }

    #[test]
    fn hkdf_sha384_extract_expand_round_trip() {
        let prk = hkdf_sha384_extract(b"salt", b"ikm");
        assert_eq!(prk.len(), 48);
        let mut okm = [0u8; 32];
        hkdf_sha384_expand(&prk, b"info", &mut okm).expect("expand failed");
        assert_ne!(okm, [0u8; 32]);
    }

    #[test]
    fn hkdf_sha512_extract_expand_round_trip() {
        let prk = hkdf_sha512_extract(b"salt", b"ikm");
        assert_eq!(prk.len(), 64);
        let mut okm = [0u8; 64];
        hkdf_sha512_expand(&prk, b"info", &mut okm).expect("expand failed");
        assert_ne!(okm, [0u8; 64]);
    }

    #[test]
    fn hkdf_expand_empty_output_errors() {
        let prk = hkdf_sha256_extract(b"salt", b"ikm");
        let result = hkdf_sha256_expand(&prk, b"info", &mut []);
        assert_eq!(result, Err(CryptoError::BadInput));
    }

    // ── verify_password ──────────────────────────────────────────────────────

    const VERIFY_SALT: &[u8] = b"0123456789abcdef"; // 16 bytes

    #[test]
    fn verify_password_argon2id_correct() {
        let hasher = Argon2idHasher::new(Argon2Params::TEST_PARAMS);
        let mut expected = [0u8; 32];
        hasher
            .hash_password(b"password", VERIFY_SALT, &hasher.params, &mut expected)
            .expect("hash");
        verify_password(&hasher, b"password", VERIFY_SALT, &expected)
            .expect("correct password must pass");
    }

    #[test]
    fn verify_password_argon2id_wrong_password() {
        let hasher = Argon2idHasher::new(Argon2Params::TEST_PARAMS);
        let mut expected = [0u8; 32];
        hasher
            .hash_password(b"password", VERIFY_SALT, &hasher.params, &mut expected)
            .expect("hash");
        let result = verify_password(&hasher, b"wrongpassword", VERIFY_SALT, &expected);
        assert_eq!(result, Err(CryptoError::InvalidTag));
    }

    #[test]
    fn verify_password_pbkdf2_correct() {
        let hasher = Pbkdf2Sha256Hasher::new(1_000);
        let mut expected = [0u8; 32];
        hasher
            .hash_password(b"mypassword", VERIFY_SALT, &hasher.params(), &mut expected)
            .expect("hash");
        verify_password(&hasher, b"mypassword", VERIFY_SALT, &expected)
            .expect("correct password must pass");
    }

    #[test]
    fn verify_password_pbkdf2_wrong_password() {
        let hasher = Pbkdf2Sha256Hasher::new(1_000);
        let mut expected = [0u8; 32];
        hasher
            .hash_password(b"mypassword", VERIFY_SALT, &hasher.params(), &mut expected)
            .expect("hash");
        let result = verify_password(&hasher, b"notmypassword", VERIFY_SALT, &expected);
        assert_eq!(result, Err(CryptoError::InvalidTag));
    }

    #[test]
    fn verify_password_empty_expected_errors() {
        let hasher = Pbkdf2Sha256Hasher::new(1_000);
        let result = verify_password(&hasher, b"password", VERIFY_SALT, &[]);
        assert_eq!(result, Err(CryptoError::BadInput));
    }
}