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
#![forbid(unsafe_code)]

//! Argon2id password hashing / key derivation for the OxiCrypto stack.
//!
//! Backed by `argon2` (RustCrypto, Pure Rust, digest 0.11 chain).
//! Only the `Argon2id` variant is exposed — it is recommended by RFC 9106 §4.

extern crate alloc;

use alloc::string::String;
use argon2::{Algorithm, Argon2, Params, Version};
use oxicrypto_core::{CryptoError, PasswordHash as PasswordHashTrait, PasswordHashParams};

/// Parameters for Argon2id key derivation.
///
/// RFC 9106 §4 recommends at least `m_cost = 65536` (64 MiB), `t_cost = 3`, `p_cost = 4`
/// for offline use and lower values for interactive contexts.
#[derive(Debug, Clone, Copy)]
pub struct Argon2Params {
    /// Memory cost in KiB (must be ≥ 8 × `p_cost`).
    pub m_cost: u32,
    /// Time cost (number of passes over memory).
    pub t_cost: u32,
    /// Parallelism cost (number of lanes).
    pub p_cost: u32,
}

impl Argon2Params {
    /// Minimum parameters suitable for unit tests (fast, not secure for production).
    pub const TEST_PARAMS: Self = Self {
        m_cost: 64,
        t_cost: 1,
        p_cost: 1,
    };

    /// Validate that parameters meet OWASP 2023 / RFC 9106 minimum requirements.
    ///
    /// Enforces:
    /// - `m_cost >= 19_456` (19 MiB — OWASP 2023 Password Storage Cheat Sheet minimum)
    /// - `t_cost >= 2` (RFC 9106 §4 recommended minimum passes)
    /// - `p_cost >= 1` (at least one lane required)
    /// - `m_cost >= 8 * p_cost` (RFC 9106 §3.1 block-count constraint)
    ///
    /// Note: [`Argon2Params::TEST_PARAMS`] intentionally violates these bounds for
    /// test speed; this method is opt-in and not invoked by [`argon2id_derive`].
    ///
    /// # Errors
    /// Returns [`CryptoError::BadInput`] if any constraint is violated.
    pub fn validate(&self) -> Result<(), CryptoError> {
        // OWASP 2023: memory >= 19 MiB (19 456 KiB)
        if self.m_cost < 19_456 {
            return Err(CryptoError::BadInput);
        }
        // Minimum iteration count (RFC 9106 §4)
        if self.t_cost < 2 {
            return Err(CryptoError::BadInput);
        }
        // At least one lane
        if self.p_cost < 1 {
            return Err(CryptoError::BadInput);
        }
        // RFC 9106 §3.1: m_cost must be >= 8 * p_cost
        if self.m_cost < 8 * self.p_cost {
            return Err(CryptoError::BadInput);
        }
        Ok(())
    }

    /// Interactive login preset (OWASP 2024 / libsodium `OPSLIMIT_INTERACTIVE`).
    ///
    /// m=65536 (64 MiB), t=2, p=1
    /// Provides ~1 s latency on a modern server.
    #[must_use]
    pub fn interactive() -> Self {
        Self {
            m_cost: 65_536,
            t_cost: 2,
            p_cost: 1,
        }
    }

    /// Moderate preset — balanced between interactive and sensitive.
    ///
    /// m=262144 (256 MiB), t=3, p=4
    /// Provides ~2–4 s on a modern server; suitable for non-interactive key derivation.
    #[must_use]
    pub fn moderate() -> Self {
        Self {
            m_cost: 262_144,
            t_cost: 3,
            p_cost: 4,
        }
    }

    /// Sensitive preset (libsodium `OPSLIMIT_SENSITIVE`).
    ///
    /// m=1048576 (1 GiB), t=4, p=8
    /// High-security offline key derivation; only use where latency is acceptable.
    #[must_use]
    pub fn sensitive() -> Self {
        Self {
            m_cost: 1_048_576,
            t_cost: 4,
            p_cost: 8,
        }
    }
}

impl PasswordHashParams for Argon2Params {
    fn memory_cost(&self) -> Option<u32> {
        Some(self.m_cost)
    }

    fn time_cost(&self) -> Option<u32> {
        Some(self.t_cost)
    }

    fn parallelism(&self) -> Option<u32> {
        Some(self.p_cost)
    }
}

/// Argon2id key derivation.
///
/// # Arguments
/// - `password` — secret password bytes
/// - `salt`     — random salt (must be ≥ 8 bytes per the Argon2 spec)
/// - `params`   — Argon2 cost parameters
/// - `out`      — output buffer (1–64 bytes per the spec)
#[must_use = "argon2id derive result must be checked"]
pub fn argon2id_derive(
    password: &[u8],
    salt: &[u8],
    params: Argon2Params,
    out: &mut [u8],
) -> Result<(), CryptoError> {
    if out.is_empty() {
        return Err(CryptoError::BadInput);
    }
    if salt.len() < 8 {
        return Err(CryptoError::BadInput);
    }
    let a2_params = Params::new(params.m_cost, params.t_cost, params.p_cost, Some(out.len()))
        .map_err(|_| CryptoError::BadInput)?;
    let argon2 = Argon2::new(Algorithm::Argon2id, Version::V0x13, a2_params);
    argon2
        .hash_password_into(password, salt, out)
        .map_err(|_| CryptoError::Internal("Argon2id derive failed"))
}

/// Argon2d (data-dependent memory access) key derivation.
///
/// Argon2d is faster than Argon2id but vulnerable to side-channel attacks from
/// GPU-based memory access timing. Suitable for crypto key derivation contexts
/// where the attacker does not have local access to the machine.
///
/// # Arguments
/// - `password` — secret password bytes
/// - `salt`     — random salt (must be ≥ 8 bytes per the Argon2 spec)
/// - `params`   — Argon2 cost parameters
/// - `out`      — output buffer (1–64 bytes per the spec)
///
/// # Errors
/// Returns [`CryptoError::BadInput`] if `out` is empty, `salt.len() < 8`,
/// or the parameter values are out of range.
#[must_use = "argon2d derive result must be checked"]
pub fn argon2d_derive(
    password: &[u8],
    salt: &[u8],
    params: Argon2Params,
    out: &mut [u8],
) -> Result<(), CryptoError> {
    if out.is_empty() {
        return Err(CryptoError::BadInput);
    }
    if salt.len() < 8 {
        return Err(CryptoError::BadInput);
    }
    let a2_params = Params::new(params.m_cost, params.t_cost, params.p_cost, Some(out.len()))
        .map_err(|_| CryptoError::BadInput)?;
    let argon2 = Argon2::new(Algorithm::Argon2d, Version::V0x13, a2_params);
    argon2
        .hash_password_into(password, salt, out)
        .map_err(|_| CryptoError::Internal("Argon2d derive failed"))
}

/// Argon2i (data-independent memory access) key derivation.
///
/// Argon2i uses data-independent memory access, making it resistant to
/// side-channel attacks. It is weaker against GPU and ASIC attacks than
/// Argon2id, but suitable where side-channel resistance is the primary concern.
///
/// # Arguments
/// - `password` — secret password bytes
/// - `salt`     — random salt (must be ≥ 8 bytes per the Argon2 spec)
/// - `params`   — Argon2 cost parameters
/// - `out`      — output buffer (1–64 bytes per the spec)
///
/// # Errors
/// Returns [`CryptoError::BadInput`] if `out` is empty, `salt.len() < 8`,
/// or the parameter values are out of range.
#[must_use = "argon2i derive result must be checked"]
pub fn argon2i_derive(
    password: &[u8],
    salt: &[u8],
    params: Argon2Params,
    out: &mut [u8],
) -> Result<(), CryptoError> {
    if out.is_empty() {
        return Err(CryptoError::BadInput);
    }
    if salt.len() < 8 {
        return Err(CryptoError::BadInput);
    }
    let a2_params = Params::new(params.m_cost, params.t_cost, params.p_cost, Some(out.len()))
        .map_err(|_| CryptoError::BadInput)?;
    let argon2 = Argon2::new(Algorithm::Argon2i, Version::V0x13, a2_params);
    argon2
        .hash_password_into(password, salt, out)
        .map_err(|_| CryptoError::Internal("Argon2i derive failed"))
}

// ---------------------------------------------------------------------------
// Argon2idHasher — implements the `PasswordHash` trait from `oxicrypto-core`
// ---------------------------------------------------------------------------

/// An Argon2id password hasher that bundles its own cost parameters.
///
/// Implements [`PasswordHash`](oxicrypto_core::PasswordHash) so it can be
/// used polymorphically with [`crate::verify_password`].
///
/// # Design note
/// The `PasswordHash::hash_password` method from `oxicrypto-core` accepts a
/// `&dyn PasswordHashParams` argument, but `Argon2idHasher` uses its own
/// stored `params` field rather than the external parameter object. This is
/// intentional: Argon2 parameters are typed (`m_cost`/`t_cost`/`p_cost`) and
/// the generic `PasswordHashParams` trait only surfaces them as `Option<u32>`,
/// which is insufficient for safe Argon2 initialization. Callers that need
/// dynamic parameter tuning should construct a new `Argon2idHasher` with the
/// desired `Argon2Params`.
#[derive(Debug, Clone, Copy)]
pub struct Argon2idHasher {
    /// Argon2id cost parameters.
    pub params: Argon2Params,
}

impl Argon2idHasher {
    /// Create a new hasher with explicit parameters.
    #[must_use]
    pub fn new(params: Argon2Params) -> Self {
        Self { params }
    }

    /// Interactive login preset.
    #[must_use]
    pub fn interactive() -> Self {
        Self::new(Argon2Params::interactive())
    }

    /// Moderate (balanced) preset.
    #[must_use]
    pub fn moderate() -> Self {
        Self::new(Argon2Params::moderate())
    }

    /// Sensitive (high-security) preset.
    #[must_use]
    pub fn sensitive() -> Self {
        Self::new(Argon2Params::sensitive())
    }
}

impl PasswordHashTrait for Argon2idHasher {
    fn name(&self) -> &'static str {
        "argon2id"
    }

    fn hash_password(
        &self,
        password: &[u8],
        salt: &[u8],
        _params: &dyn PasswordHashParams,
        out: &mut [u8],
    ) -> Result<(), CryptoError> {
        // Use self.params directly (see design note on Argon2idHasher).
        argon2id_derive(password, salt, self.params, out)
    }
}

// ---------------------------------------------------------------------------
// PHC string support — uses the `argon2` crate's `PasswordHasher` impl
// ---------------------------------------------------------------------------

/// Encode an Argon2id hash as a PHC string.
///
/// Builds the standard `$argon2id$v=19$m=<m>,t=<t>,p=<p>$<b64-salt>$<b64-hash>`
/// string from pre-computed hash bytes and the hasher's parameters.
///
/// `salt` must be ≥ 8 bytes; `hash` must be between
/// [`password_hash::phc::Output::MIN_LENGTH`] and
/// [`password_hash::phc::Output::MAX_LENGTH`] bytes (1–64).
///
/// # Errors
/// - [`CryptoError::BadInput`] — parameter values are out of range.
/// - [`CryptoError::Encoding`] — PHC string assembly failed.
#[must_use = "PHC string result must be checked"]
pub fn argon2id_to_phc_string(
    hasher: &Argon2idHasher,
    salt: &[u8],
    hash: &[u8],
) -> Result<String, CryptoError> {
    use argon2::PasswordHash;
    use password_hash::phc::{Output, ParamsString, Salt};

    // Validate and build argon2 Params so we can convert to ParamsString.
    let a2_params = Params::new(
        hasher.params.m_cost,
        hasher.params.t_cost,
        hasher.params.p_cost,
        Some(hash.len()),
    )
    .map_err(|_| CryptoError::BadInput)?;

    let params_str = ParamsString::try_from(&a2_params).map_err(|_| CryptoError::Encoding)?;

    let salt_val = Salt::new(salt).map_err(|_| CryptoError::BadInput)?;

    let output = Output::new(hash).map_err(|_| CryptoError::Encoding)?;

    let ph = PasswordHash {
        algorithm: argon2::ARGON2ID_IDENT,
        version: Some(Version::V0x13.into()),
        params: params_str,
        salt: Some(salt_val),
        hash: Some(output),
    };

    Ok(alloc::format!("{ph}"))
}

/// Verify a password against an Argon2id PHC string.
///
/// Parses the PHC string, extracts the embedded algorithm parameters, and
/// re-derives the hash in constant time.
///
/// # Errors
/// - [`CryptoError::Encoding`] — malformed PHC string.
/// - [`CryptoError::InvalidTag`] — password does not match.
#[must_use = "PHC verification result must be checked"]
pub fn argon2id_verify_phc(phc: &str, password: &[u8]) -> Result<(), CryptoError> {
    use argon2::{PasswordHash, PasswordVerifier};

    let hash = PasswordHash::new(phc).map_err(|_| CryptoError::Encoding)?;
    Argon2::default()
        .verify_password(password, &hash)
        .map_err(|_| CryptoError::InvalidTag)
}

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

    /// Use tiny params so tests finish quickly.
    fn test_hasher() -> Argon2idHasher {
        Argon2idHasher::new(Argon2Params::TEST_PARAMS)
    }

    const TEST_SALT: &[u8] = b"01234567890abcde"; // 16 bytes

    #[test]
    fn argon2id_derive_deterministic() {
        let mut out1 = [0u8; 32];
        let mut out2 = [0u8; 32];
        argon2id_derive(b"password", TEST_SALT, Argon2Params::TEST_PARAMS, &mut out1)
            .expect("derive 1");
        argon2id_derive(b"password", TEST_SALT, Argon2Params::TEST_PARAMS, &mut out2)
            .expect("derive 2");
        assert_eq!(out1, out2, "Argon2id must be deterministic");
        assert_ne!(out1, [0u8; 32]);
    }

    #[test]
    fn argon2id_derive_short_salt_errors() {
        let mut out = [0u8; 32];
        let result = argon2id_derive(b"password", b"short", Argon2Params::TEST_PARAMS, &mut out);
        assert_eq!(result, Err(CryptoError::BadInput));
    }

    #[test]
    fn argon2id_derive_empty_output_errors() {
        let result = argon2id_derive(b"password", TEST_SALT, Argon2Params::TEST_PARAMS, &mut []);
        assert_eq!(result, Err(CryptoError::BadInput));
    }

    #[test]
    fn password_hash_trait_hash_password_deterministic() {
        let hasher = test_hasher();
        let mut out1 = [0u8; 32];
        let mut out2 = [0u8; 32];
        hasher
            .hash_password(b"password", TEST_SALT, &hasher.params, &mut out1)
            .expect("hash 1");
        hasher
            .hash_password(b"password", TEST_SALT, &hasher.params, &mut out2)
            .expect("hash 2");
        assert_eq!(out1, out2, "PasswordHash must be deterministic");
        assert_ne!(out1, [0u8; 32]);
    }

    #[test]
    fn preset_cost_ordering() {
        let interactive = Argon2Params::interactive();
        let moderate = Argon2Params::moderate();
        let sensitive = Argon2Params::sensitive();
        // Memory cost: sensitive > moderate > interactive
        assert!(sensitive.m_cost > moderate.m_cost);
        assert!(moderate.m_cost > interactive.m_cost);
        // Time cost: sensitive ≥ moderate ≥ interactive
        assert!(sensitive.t_cost >= moderate.t_cost);
        assert!(moderate.t_cost >= interactive.t_cost);
    }

    #[test]
    fn hasher_name() {
        assert_eq!(test_hasher().name(), "argon2id");
    }

    #[test]
    fn phc_round_trip() {
        let hasher = test_hasher();
        let mut hash = [0u8; 32];
        argon2id_derive(b"password", TEST_SALT, hasher.params, &mut hash).expect("derive");

        let phc = argon2id_to_phc_string(&hasher, TEST_SALT, &hash).expect("to_phc");
        assert!(
            phc.starts_with("$argon2id$"),
            "PHC must start with $argon2id$"
        );

        // Verify with same password: must succeed.
        argon2id_verify_phc(&phc, b"password").expect("verify correct");
    }

    #[test]
    fn phc_wrong_password_rejected() {
        let hasher = test_hasher();
        let mut hash = [0u8; 32];
        argon2id_derive(b"password", TEST_SALT, hasher.params, &mut hash).expect("derive");

        let phc = argon2id_to_phc_string(&hasher, TEST_SALT, &hash).expect("to_phc");
        let result = argon2id_verify_phc(&phc, b"wrongpassword");
        assert_eq!(result, Err(CryptoError::InvalidTag));
    }

    #[test]
    fn phc_malformed_string_rejected() {
        let result = argon2id_verify_phc("invalid-phc-string", b"password");
        assert_eq!(result, Err(CryptoError::Encoding));
    }

    // ---------------------------------------------------------------------------
    // Argon2d and Argon2i variant tests
    // ---------------------------------------------------------------------------

    #[test]
    fn argon2d_derive_deterministic() {
        let mut out1 = [0u8; 32];
        let mut out2 = [0u8; 32];
        argon2d_derive(b"password", TEST_SALT, Argon2Params::TEST_PARAMS, &mut out1)
            .expect("argon2d derive 1");
        argon2d_derive(b"password", TEST_SALT, Argon2Params::TEST_PARAMS, &mut out2)
            .expect("argon2d derive 2");
        assert_eq!(out1, out2, "Argon2d must be deterministic");
        assert_ne!(out1, [0u8; 32]);
    }

    #[test]
    fn argon2i_derive_deterministic() {
        let mut out1 = [0u8; 32];
        let mut out2 = [0u8; 32];
        argon2i_derive(b"password", TEST_SALT, Argon2Params::TEST_PARAMS, &mut out1)
            .expect("argon2i derive 1");
        argon2i_derive(b"password", TEST_SALT, Argon2Params::TEST_PARAMS, &mut out2)
            .expect("argon2i derive 2");
        assert_eq!(out1, out2, "Argon2i must be deterministic");
        assert_ne!(out1, [0u8; 32]);
    }

    /// Verify that all three Argon2 variants (d, i, id) produce distinct outputs
    /// for identical inputs — proving they actually use different algorithm modes.
    #[test]
    fn argon2_variants_produce_distinct_outputs() {
        let mut out_id = [0u8; 32];
        let mut out_d = [0u8; 32];
        let mut out_i = [0u8; 32];
        argon2id_derive(
            b"password",
            TEST_SALT,
            Argon2Params::TEST_PARAMS,
            &mut out_id,
        )
        .expect("argon2id");
        argon2d_derive(
            b"password",
            TEST_SALT,
            Argon2Params::TEST_PARAMS,
            &mut out_d,
        )
        .expect("argon2d");
        argon2i_derive(
            b"password",
            TEST_SALT,
            Argon2Params::TEST_PARAMS,
            &mut out_i,
        )
        .expect("argon2i");
        // All three algorithm variants must produce different derived keys.
        assert_ne!(out_id, out_d, "Argon2id must differ from Argon2d");
        assert_ne!(out_id, out_i, "Argon2id must differ from Argon2i");
        assert_ne!(out_d, out_i, "Argon2d must differ from Argon2i");
    }

    #[test]
    fn argon2d_short_salt_errors() {
        let mut out = [0u8; 32];
        let result = argon2d_derive(b"password", b"short", Argon2Params::TEST_PARAMS, &mut out);
        assert_eq!(result, Err(CryptoError::BadInput));
    }

    #[test]
    fn argon2d_empty_output_errors() {
        let result = argon2d_derive(b"password", TEST_SALT, Argon2Params::TEST_PARAMS, &mut []);
        assert_eq!(result, Err(CryptoError::BadInput));
    }

    #[test]
    fn argon2i_short_salt_errors() {
        let mut out = [0u8; 32];
        let result = argon2i_derive(b"password", b"short", Argon2Params::TEST_PARAMS, &mut out);
        assert_eq!(result, Err(CryptoError::BadInput));
    }

    #[test]
    fn argon2i_empty_output_errors() {
        let result = argon2i_derive(b"password", TEST_SALT, Argon2Params::TEST_PARAMS, &mut []);
        assert_eq!(result, Err(CryptoError::BadInput));
    }
}