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

//! PBKDF2 password-based key derivation for the OxiCrypto stack.
//!
//! Provides PBKDF2-HMAC-SHA-256 and PBKDF2-HMAC-SHA-512 via the
//! `pbkdf2` crate (RustCrypto, digest 0.11 chain).

use oxicrypto_core::{CryptoError, Kdf, PasswordHash as PasswordHashTrait, PasswordHashParams};
use sha2::{Sha256, Sha512};

// ---------------------------------------------------------------------------
// Low-level standalone functions (existing API — preserved for compatibility)
// ---------------------------------------------------------------------------

/// PBKDF2-HMAC-SHA-256 key derivation.
///
/// # Arguments
/// - `password`   — secret password bytes
/// - `salt`       — random salt (recommended ≥ 16 bytes)
/// - `iterations` — NIST SP 800-132 recommends ≥ 310_000 for interactive logins
/// - `out`        — output buffer (any length)
#[must_use = "PBKDF2 derive result must be checked"]
pub fn pbkdf2_sha256(
    password: &[u8],
    salt: &[u8],
    iterations: u32,
    out: &mut [u8],
) -> Result<(), CryptoError> {
    if out.is_empty() {
        return Err(CryptoError::BadInput);
    }
    if iterations == 0 {
        return Err(CryptoError::BadInput);
    }
    pbkdf2::pbkdf2_hmac::<Sha256>(password, salt, iterations, out);
    Ok(())
}

/// PBKDF2-HMAC-SHA-512 key derivation.
#[must_use = "PBKDF2 derive result must be checked"]
pub fn pbkdf2_sha512(
    password: &[u8],
    salt: &[u8],
    iterations: u32,
    out: &mut [u8],
) -> Result<(), CryptoError> {
    if out.is_empty() {
        return Err(CryptoError::BadInput);
    }
    if iterations == 0 {
        return Err(CryptoError::BadInput);
    }
    pbkdf2::pbkdf2_hmac::<Sha512>(password, salt, iterations, out);
    Ok(())
}

// ---------------------------------------------------------------------------
// Pbkdf2Params — implements `PasswordHashParams` for the core trait surface
// ---------------------------------------------------------------------------

/// Cost parameters for PBKDF2.
///
/// PBKDF2 has only a time cost (iteration count); there is no memory cost or
/// parallelism parameter.
#[derive(Debug, Clone, Copy)]
pub struct Pbkdf2Params {
    /// Number of PBKDF2 iterations.
    pub iterations: u32,
}

impl PasswordHashParams for Pbkdf2Params {
    fn memory_cost(&self) -> Option<u32> {
        None
    }

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

    fn parallelism(&self) -> Option<u32> {
        None
    }
}

// ---------------------------------------------------------------------------
// Pbkdf2Sha256Hasher — PasswordHash + Kdf + presets
// ---------------------------------------------------------------------------

/// PBKDF2-HMAC-SHA-256 password hasher.
///
/// Implements both [`PasswordHash`](oxicrypto_core::PasswordHash) (for use
/// with [`crate::verify_password`]) and [`Kdf`] (for use as a standard key
/// derivation function).
///
/// # Design note — `params` argument is ignored
/// The [`PasswordHash::hash_password`](oxicrypto_core::PasswordHash::hash_password)
/// trait method accepts a `params: &dyn PasswordHashParams` argument, but this
/// implementation ignores it and uses `self.iterations` instead. Callers that
/// need different iteration counts should construct a new `Pbkdf2Sha256Hasher`
/// with the desired count rather than passing a different `PasswordHashParams` object.
#[derive(Debug, Clone, Copy)]
pub struct Pbkdf2Sha256Hasher {
    /// Number of PBKDF2 iterations.
    pub iterations: u32,
}

impl Pbkdf2Sha256Hasher {
    /// Create a new hasher with an explicit iteration count.
    #[must_use]
    pub fn new(iterations: u32) -> Self {
        Self { iterations }
    }

    /// Interactive login preset — 310,000 iterations (NIST SP 800-132 minimum).
    #[must_use]
    pub fn interactive() -> Self {
        Self {
            iterations: 310_000,
        }
    }

    /// Moderate preset — 600,000 iterations (OWASP 2024 recommendation).
    #[must_use]
    pub fn moderate() -> Self {
        Self {
            iterations: 600_000,
        }
    }

    /// Sensitive (high-security) preset — 1,000,000 iterations.
    #[must_use]
    pub fn sensitive() -> Self {
        Self {
            iterations: 1_000_000,
        }
    }

    /// Return the cost parameters as a [`Pbkdf2Params`].
    #[must_use]
    pub fn params(&self) -> Pbkdf2Params {
        Pbkdf2Params {
            iterations: self.iterations,
        }
    }
}

impl PasswordHashTrait for Pbkdf2Sha256Hasher {
    fn name(&self) -> &'static str {
        "pbkdf2-sha256"
    }

    fn hash_password(
        &self,
        password: &[u8],
        salt: &[u8],
        _params: &dyn PasswordHashParams,
        out: &mut [u8],
    ) -> Result<(), CryptoError> {
        pbkdf2_sha256(password, salt, self.iterations, out)
    }
}

impl Kdf for Pbkdf2Sha256Hasher {
    fn name(&self) -> &'static str {
        "PBKDF2-SHA-256"
    }

    /// Derive key material from a password (IKM) and salt.
    ///
    /// The `info` argument is not used by PBKDF2 (it has no native concept of
    /// application-specific context); pass an empty slice.
    fn derive(
        &self,
        ikm: &[u8],
        salt: &[u8],
        _info: &[u8],
        okm_out: &mut [u8],
    ) -> Result<(), CryptoError> {
        pbkdf2_sha256(ikm, salt, self.iterations, okm_out)
    }
}

// ---------------------------------------------------------------------------
// Pbkdf2Sha512Hasher — PasswordHash + Kdf + presets
// ---------------------------------------------------------------------------

/// PBKDF2-HMAC-SHA-512 password hasher.
///
/// Implements both [`PasswordHash`](oxicrypto_core::PasswordHash) and [`Kdf`].
///
/// # Design note — `params` argument is ignored
/// The [`PasswordHash::hash_password`](oxicrypto_core::PasswordHash::hash_password)
/// trait method accepts a `params: &dyn PasswordHashParams` argument, but this
/// implementation ignores it and uses `self.iterations` instead. Callers that
/// need different iteration counts should construct a new `Pbkdf2Sha512Hasher`
/// with the desired count rather than passing a different `PasswordHashParams` object.
#[derive(Debug, Clone, Copy)]
pub struct Pbkdf2Sha512Hasher {
    /// Number of PBKDF2 iterations.
    pub iterations: u32,
}

impl Pbkdf2Sha512Hasher {
    /// Create a new hasher with an explicit iteration count.
    #[must_use]
    pub fn new(iterations: u32) -> Self {
        Self { iterations }
    }

    /// Interactive login preset — 210,000 iterations (approx. equivalent CPU
    /// cost to 310,000 SHA-256 rounds, per OWASP guidance).
    #[must_use]
    pub fn interactive() -> Self {
        Self {
            iterations: 210_000,
        }
    }

    /// Moderate preset — 400,000 iterations.
    #[must_use]
    pub fn moderate() -> Self {
        Self {
            iterations: 400_000,
        }
    }

    /// Sensitive (high-security) preset — 700,000 iterations.
    #[must_use]
    pub fn sensitive() -> Self {
        Self {
            iterations: 700_000,
        }
    }

    /// Return the cost parameters as a [`Pbkdf2Params`].
    #[must_use]
    pub fn params(&self) -> Pbkdf2Params {
        Pbkdf2Params {
            iterations: self.iterations,
        }
    }
}

impl PasswordHashTrait for Pbkdf2Sha512Hasher {
    fn name(&self) -> &'static str {
        "pbkdf2-sha512"
    }

    fn hash_password(
        &self,
        password: &[u8],
        salt: &[u8],
        _params: &dyn PasswordHashParams,
        out: &mut [u8],
    ) -> Result<(), CryptoError> {
        pbkdf2_sha512(password, salt, self.iterations, out)
    }
}

impl Kdf for Pbkdf2Sha512Hasher {
    fn name(&self) -> &'static str {
        "PBKDF2-SHA-512"
    }

    /// Derive key material from a password (IKM) and salt.
    ///
    /// The `info` argument is not used by PBKDF2; pass an empty slice.
    fn derive(
        &self,
        ikm: &[u8],
        salt: &[u8],
        _info: &[u8],
        okm_out: &mut [u8],
    ) -> Result<(), CryptoError> {
        pbkdf2_sha512(ikm, salt, self.iterations, okm_out)
    }
}

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

    const SALT: &[u8] = b"test-salt-16byte";
    const ITERS: u32 = 1_000; // Fast for tests

    #[test]
    fn pbkdf2_sha256_deterministic() {
        let mut out1 = [0u8; 32];
        let mut out2 = [0u8; 32];
        pbkdf2_sha256(b"password", SALT, ITERS, &mut out1).expect("derive 1");
        pbkdf2_sha256(b"password", SALT, ITERS, &mut out2).expect("derive 2");
        assert_eq!(out1, out2);
        assert_ne!(out1, [0u8; 32]);
    }

    #[test]
    fn pbkdf2_sha512_deterministic() {
        let mut out1 = [0u8; 64];
        let mut out2 = [0u8; 64];
        pbkdf2_sha512(b"password", SALT, ITERS, &mut out1).expect("derive 1");
        pbkdf2_sha512(b"password", SALT, ITERS, &mut out2).expect("derive 2");
        assert_eq!(out1, out2);
        assert_ne!(out1, [0u8; 64]);
    }

    #[test]
    fn pbkdf2_sha256_zero_iterations_errors() {
        let mut out = [0u8; 32];
        assert_eq!(
            pbkdf2_sha256(b"pw", SALT, 0, &mut out),
            Err(CryptoError::BadInput)
        );
    }

    #[test]
    fn pbkdf2_sha256_empty_output_errors() {
        assert_eq!(
            pbkdf2_sha256(b"pw", SALT, ITERS, &mut []),
            Err(CryptoError::BadInput)
        );
    }

    // ── PasswordHash trait ───────────────────────────────────────────────────

    #[test]
    fn pbkdf2_sha256_hasher_hash_password_deterministic() {
        let hasher = Pbkdf2Sha256Hasher::new(ITERS);
        let params = hasher.params();
        let mut out1 = [0u8; 32];
        let mut out2 = [0u8; 32];
        hasher
            .hash_password(b"password", SALT, &params, &mut out1)
            .expect("hash 1");
        hasher
            .hash_password(b"password", SALT, &params, &mut out2)
            .expect("hash 2");
        assert_eq!(out1, out2);
        assert_ne!(out1, [0u8; 32]);
    }

    #[test]
    fn pbkdf2_sha512_hasher_hash_password_deterministic() {
        let hasher = Pbkdf2Sha512Hasher::new(ITERS);
        let params = hasher.params();
        let mut out1 = [0u8; 64];
        let mut out2 = [0u8; 64];
        hasher
            .hash_password(b"password", SALT, &params, &mut out1)
            .expect("hash 1");
        hasher
            .hash_password(b"password", SALT, &params, &mut out2)
            .expect("hash 2");
        assert_eq!(out1, out2);
        assert_ne!(out1, [0u8; 64]);
    }

    // ── Kdf trait ───────────────────────────────────────────────────────────

    #[test]
    fn pbkdf2_sha256_kdf_matches_standalone() {
        let hasher = Pbkdf2Sha256Hasher::new(ITERS);
        let mut from_kdf = [0u8; 32];
        let mut from_fn = [0u8; 32];
        hasher
            .derive(b"key", SALT, b"", &mut from_kdf)
            .expect("kdf derive");
        pbkdf2_sha256(b"key", SALT, ITERS, &mut from_fn).expect("fn derive");
        assert_eq!(from_kdf, from_fn, "Kdf::derive must match standalone fn");
    }

    #[test]
    fn pbkdf2_sha512_kdf_matches_standalone() {
        let hasher = Pbkdf2Sha512Hasher::new(ITERS);
        let mut from_kdf = [0u8; 64];
        let mut from_fn = [0u8; 64];
        hasher
            .derive(b"key", SALT, b"", &mut from_kdf)
            .expect("kdf derive");
        pbkdf2_sha512(b"key", SALT, ITERS, &mut from_fn).expect("fn derive");
        assert_eq!(from_kdf, from_fn, "Kdf::derive must match standalone fn");
    }

    // ── Presets ─────────────────────────────────────────────────────────────

    #[test]
    fn pbkdf2_sha256_preset_cost_ordering() {
        let interactive = Pbkdf2Sha256Hasher::interactive();
        let moderate = Pbkdf2Sha256Hasher::moderate();
        let sensitive = Pbkdf2Sha256Hasher::sensitive();
        assert!(sensitive.iterations > moderate.iterations);
        assert!(moderate.iterations > interactive.iterations);
    }

    #[test]
    fn pbkdf2_sha512_preset_cost_ordering() {
        let interactive = Pbkdf2Sha512Hasher::interactive();
        let moderate = Pbkdf2Sha512Hasher::moderate();
        let sensitive = Pbkdf2Sha512Hasher::sensitive();
        assert!(sensitive.iterations > moderate.iterations);
        assert!(moderate.iterations > interactive.iterations);
    }

    #[test]
    fn pbkdf2_params_trait_impl() {
        let params = Pbkdf2Params { iterations: 42 };
        assert_eq!(params.memory_cost(), None);
        assert_eq!(params.time_cost(), Some(42));
        assert_eq!(params.parallelism(), None);
    }

    #[test]
    fn hasher_names() {
        assert_eq!(
            <Pbkdf2Sha256Hasher as oxicrypto_core::PasswordHash>::name(&Pbkdf2Sha256Hasher::new(1)),
            "pbkdf2-sha256"
        );
        assert_eq!(
            <Pbkdf2Sha512Hasher as oxicrypto_core::PasswordHash>::name(&Pbkdf2Sha512Hasher::new(1)),
            "pbkdf2-sha512"
        );
    }
}