paserk 0.4.0

Platform-Agnostic Serialized Keys (PASERK) for PASETO
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
//! Argon2id-based PBKW implementation for K2/K4.
//!
//! This module implements password-based key wrapping using Argon2id for
//! key derivation and `XChaCha20` + `BLAKE2b` for authenticated encryption.

use crate::core::error::{PaserkError, PaserkResult};

/// Salt size for Argon2id (16 bytes).
pub const ARGON2_SALT_SIZE: usize = 16;

/// Nonce size for PBKW (24 bytes).
/// This nonce is used as input to the KDF, and the `XChaCha20` nonce (also 24 bytes)
/// is derived from the KDF output.
pub const XCHACHA20_NONCE_SIZE: usize = 24;

/// Tag size for BLAKE2b-MAC (32 bytes).
pub const PBKW_TAG_SIZE: usize = 32;

/// Output type for local (32-byte) key wrapping: (salt, nonce, ciphertext, tag).
pub type PbkwLocalOutput = (
    [u8; ARGON2_SALT_SIZE],
    [u8; XCHACHA20_NONCE_SIZE],
    [u8; 32],
    [u8; PBKW_TAG_SIZE],
);

/// Output type for secret (64-byte) key wrapping: (salt, nonce, ciphertext, tag).
pub type PbkwSecretOutput = (
    [u8; ARGON2_SALT_SIZE],
    [u8; XCHACHA20_NONCE_SIZE],
    [u8; 64],
    [u8; PBKW_TAG_SIZE],
);

/// Domain separation byte for encryption key derivation (0xFF per PBKW spec).
const PBKW_ENCRYPTION_KEY_DOMAIN: u8 = 0xFF;

/// Domain separation byte for authentication key derivation (0xFE per PBKW spec).
const PBKW_AUTH_KEY_DOMAIN: u8 = 0xFE;

/// Default Argon2id parameters.
#[derive(Debug, Clone, Copy)]
pub struct Argon2Params {
    /// Memory cost in KiB.
    pub memory_kib: u32,
    /// Number of iterations.
    pub iterations: u32,
    /// Degree of parallelism.
    pub parallelism: u32,
}

impl Default for Argon2Params {
    fn default() -> Self {
        Self::moderate()
    }
}

impl Argon2Params {
    /// Interactive profile: Fast, suitable for interactive logins.
    /// - Memory: 64 MiB
    /// - Iterations: 2
    /// - Parallelism: 1
    #[must_use]
    pub const fn interactive() -> Self {
        Self {
            memory_kib: 64 * 1024,
            iterations: 2,
            parallelism: 1,
        }
    }

    /// Moderate profile: Balanced security and performance.
    /// - Memory: 256 MiB
    /// - Iterations: 3
    /// - Parallelism: 1
    #[must_use]
    pub const fn moderate() -> Self {
        Self {
            memory_kib: 256 * 1024,
            iterations: 3,
            parallelism: 1,
        }
    }

    /// Sensitive profile: High security, slower computation.
    /// - Memory: 1 GiB
    /// - Iterations: 4
    /// - Parallelism: 1
    #[must_use]
    pub const fn sensitive() -> Self {
        Self {
            memory_kib: 1024 * 1024,
            iterations: 4,
            parallelism: 1,
        }
    }
}

/// Wraps a local (symmetric) key using PBKW for K2/K4.
///
/// # Arguments
///
/// * `plaintext_key` - The 32-byte key to wrap
/// * `password` - The password to use for wrapping
/// * `params` - Argon2id parameters
/// * `header` - The PASERK header (e.g., "k4.local-pw.")
///
/// # Returns
///
/// A tuple of (salt, nonce, ciphertext, tag).
#[cfg(any(feature = "k2", feature = "k4"))]
pub fn pbkw_wrap_local_k2k4(
    plaintext_key: &[u8; 32],
    password: &[u8],
    params: &Argon2Params,
    header: &str,
) -> PaserkResult<PbkwLocalOutput> {
    use argon2::{Algorithm, Argon2, ParamsBuilder, Version};
    use blake2::digest::{FixedOutput, KeyInit, Update};
    use blake2::{Blake2b, Blake2bMac};
    use chacha20::cipher::{KeyIvInit, StreamCipher};
    use chacha20::XChaCha20;
    use rand_core::{OsRng, TryRngCore};

    // Type aliases for BLAKE2b variants
    type Blake2b32 = Blake2b<blake2::digest::consts::U32>;
    type Blake2bMac32 = Blake2bMac<blake2::digest::consts::U32>;

    // Generate random salt and nonce
    let mut salt = [0u8; ARGON2_SALT_SIZE];
    let mut nonce = [0u8; XCHACHA20_NONCE_SIZE]; // 24 bytes for XChaCha20
    OsRng
        .try_fill_bytes(&mut salt)
        .map_err(|_| PaserkError::CryptoError)?;
    OsRng
        .try_fill_bytes(&mut nonce)
        .map_err(|_| PaserkError::CryptoError)?;

    // Build Argon2id parameters
    let argon2_params = ParamsBuilder::new()
        .m_cost(params.memory_kib)
        .t_cost(params.iterations)
        .p_cost(params.parallelism)
        .build()
        .map_err(|_| PaserkError::KeyDerivationFailed)?;

    let argon2 = Argon2::new(Algorithm::Argon2id, Version::V0x13, argon2_params);

    // Derive pre-shared key from password: k = Argon2id(pw, s, mem, time, para)
    let mut psk = [0u8; 32];
    argon2
        .hash_password_into(password, &salt, &mut psk)
        .map_err(|_| PaserkError::KeyDerivationFailed)?;

    // Derive encryption key: Ek = crypto_generichash(0xFF || k) - UNKEYED BLAKE2b
    let mut ek_hasher = <Blake2b32 as Default>::default();
    <Blake2b32 as Update>::update(&mut ek_hasher, &[PBKW_ENCRYPTION_KEY_DOMAIN]);
    <Blake2b32 as Update>::update(&mut ek_hasher, &psk);
    let mut encryption_key: [u8; 32] = <Blake2b32 as FixedOutput>::finalize_fixed(ek_hasher).into();

    // Derive authentication key: Ak = crypto_generichash(0xFE || k) - UNKEYED BLAKE2b
    let mut ak_hasher = <Blake2b32 as Default>::default();
    <Blake2b32 as Update>::update(&mut ak_hasher, &[PBKW_AUTH_KEY_DOMAIN]);
    <Blake2b32 as Update>::update(&mut ak_hasher, &psk);
    let mut auth_key: [u8; 32] = <Blake2b32 as FixedOutput>::finalize_fixed(ak_hasher).into();

    // Zeroize psk now that we've derived the keys
    zeroize::Zeroize::zeroize(&mut psk);

    // Encrypt the plaintext key: edk = XChaCha20(msg=ptk, key=Ek, nonce=n)
    // Note: nonce is used DIRECTLY, not derived
    let mut ciphertext = *plaintext_key;
    let mut cipher = XChaCha20::new(&encryption_key.into(), &nonce.into());
    cipher.apply_keystream(&mut ciphertext);

    // Compute authentication tag:
    // t = crypto_generichash(msg=h || s || memlimit || opslimit || parallelism || n || edk, key=Ak, len=32)
    let memlimit_bytes = u64::from(params.memory_kib) * 1024;
    let mut tag_mac = <Blake2bMac32 as KeyInit>::new_from_slice(&auth_key)
        .map_err(|_| PaserkError::CryptoError)?;
    <Blake2bMac32 as Update>::update(&mut tag_mac, header.as_bytes());
    <Blake2bMac32 as Update>::update(&mut tag_mac, &salt);
    <Blake2bMac32 as Update>::update(&mut tag_mac, &memlimit_bytes.to_be_bytes());
    <Blake2bMac32 as Update>::update(&mut tag_mac, &params.iterations.to_be_bytes());
    <Blake2bMac32 as Update>::update(&mut tag_mac, &params.parallelism.to_be_bytes());
    <Blake2bMac32 as Update>::update(&mut tag_mac, &nonce);
    <Blake2bMac32 as Update>::update(&mut tag_mac, &ciphertext);
    let tag: [u8; PBKW_TAG_SIZE] = <Blake2bMac32 as FixedOutput>::finalize_fixed(tag_mac).into();

    // Zeroize derived keys
    zeroize::Zeroize::zeroize(&mut encryption_key);
    zeroize::Zeroize::zeroize(&mut auth_key);

    Ok((salt, nonce, ciphertext, tag))
}

/// Unwraps a local (symmetric) key using PBKW for K2/K4.
///
/// # Arguments
///
/// * `salt` - The 16-byte Argon2 salt
/// * `nonce` - The 24-byte `XChaCha20` nonce
/// * `ciphertext` - The 32-byte encrypted key
/// * `tag` - The 32-byte authentication tag
/// * `password` - The password used for wrapping
/// * `params` - Argon2id parameters (must match those used for wrapping)
/// * `header` - The PASERK header (e.g., "k4.local-pw.")
///
/// # Returns
///
/// The unwrapped 32-byte plaintext key.
#[cfg(any(feature = "k2", feature = "k4"))]
pub fn pbkw_unwrap_local_k2k4(
    salt: &[u8; ARGON2_SALT_SIZE],
    nonce: &[u8; XCHACHA20_NONCE_SIZE],
    ciphertext: &[u8; 32],
    tag: &[u8; PBKW_TAG_SIZE],
    password: &[u8],
    params: &Argon2Params,
    header: &str,
) -> PaserkResult<[u8; 32]> {
    use argon2::{Algorithm, Argon2, ParamsBuilder, Version};
    use blake2::digest::{FixedOutput, KeyInit, Update};
    use blake2::{Blake2b, Blake2bMac};
    use chacha20::cipher::{KeyIvInit, StreamCipher};
    use chacha20::XChaCha20;
    use subtle::ConstantTimeEq;

    // Type aliases for BLAKE2b variants
    type Blake2b32 = Blake2b<blake2::digest::consts::U32>;
    type Blake2bMac32 = Blake2bMac<blake2::digest::consts::U32>;

    // Build Argon2id parameters
    let argon2_params = ParamsBuilder::new()
        .m_cost(params.memory_kib)
        .t_cost(params.iterations)
        .p_cost(params.parallelism)
        .build()
        .map_err(|_| PaserkError::KeyDerivationFailed)?;

    let argon2 = Argon2::new(Algorithm::Argon2id, Version::V0x13, argon2_params);

    // Derive pre-shared key from password: k = Argon2id(pw, s, mem, time, para)
    let mut psk = [0u8; 32];
    argon2
        .hash_password_into(password, salt, &mut psk)
        .map_err(|_| PaserkError::KeyDerivationFailed)?;

    // Derive encryption key: Ek = crypto_generichash(0xFF || k) - UNKEYED BLAKE2b
    let mut ek_hasher = <Blake2b32 as Default>::default();
    <Blake2b32 as Update>::update(&mut ek_hasher, &[PBKW_ENCRYPTION_KEY_DOMAIN]);
    <Blake2b32 as Update>::update(&mut ek_hasher, &psk);
    let mut encryption_key: [u8; 32] = <Blake2b32 as FixedOutput>::finalize_fixed(ek_hasher).into();

    // Derive authentication key: Ak = crypto_generichash(0xFE || k) - UNKEYED BLAKE2b
    let mut ak_hasher = <Blake2b32 as Default>::default();
    <Blake2b32 as Update>::update(&mut ak_hasher, &[PBKW_AUTH_KEY_DOMAIN]);
    <Blake2b32 as Update>::update(&mut ak_hasher, &psk);
    let mut auth_key: [u8; 32] = <Blake2b32 as FixedOutput>::finalize_fixed(ak_hasher).into();

    // Zeroize psk now that we've derived the keys
    zeroize::Zeroize::zeroize(&mut psk);

    // Verify authentication tag:
    // t = crypto_generichash(msg=h || s || memlimit || opslimit || parallelism || n || edk, key=Ak, len=32)
    let memlimit_bytes = u64::from(params.memory_kib) * 1024;
    let mut tag_mac = <Blake2bMac32 as KeyInit>::new_from_slice(&auth_key)
        .map_err(|_| PaserkError::CryptoError)?;
    <Blake2bMac32 as Update>::update(&mut tag_mac, header.as_bytes());
    <Blake2bMac32 as Update>::update(&mut tag_mac, salt);
    <Blake2bMac32 as Update>::update(&mut tag_mac, &memlimit_bytes.to_be_bytes());
    <Blake2bMac32 as Update>::update(&mut tag_mac, &params.iterations.to_be_bytes());
    <Blake2bMac32 as Update>::update(&mut tag_mac, &params.parallelism.to_be_bytes());
    <Blake2bMac32 as Update>::update(&mut tag_mac, nonce);
    <Blake2bMac32 as Update>::update(&mut tag_mac, ciphertext);
    let computed_tag: [u8; PBKW_TAG_SIZE] =
        <Blake2bMac32 as FixedOutput>::finalize_fixed(tag_mac).into();

    // Zeroize auth key after computing tag
    zeroize::Zeroize::zeroize(&mut auth_key);

    if computed_tag.ct_eq(tag).into() {
        // Decrypt the ciphertext: ptk = XChaCha20(edk, Ek, n)
        // Note: nonce is used DIRECTLY, not derived
        let mut plaintext = *ciphertext;
        let mut cipher = XChaCha20::new(&encryption_key.into(), nonce.into());
        cipher.apply_keystream(&mut plaintext);

        // Zeroize encryption key
        zeroize::Zeroize::zeroize(&mut encryption_key);

        Ok(plaintext)
    } else {
        // Zeroize encryption key on error path
        zeroize::Zeroize::zeroize(&mut encryption_key);
        Err(PaserkError::AuthenticationFailed)
    }
}

/// Wraps a secret (signing) key using PBKW for K2/K4.
///
/// # Arguments
///
/// * `plaintext_key` - The 64-byte key to wrap
/// * `password` - The password to use for wrapping
/// * `params` - Argon2id parameters
/// * `header` - The PASERK header (e.g., "k4.secret-pw.")
///
/// # Returns
///
/// A tuple of (salt, nonce, ciphertext, tag).
#[cfg(any(feature = "k2", feature = "k4"))]
pub fn pbkw_wrap_secret_k2k4(
    plaintext_key: &[u8; 64],
    password: &[u8],
    params: &Argon2Params,
    header: &str,
) -> PaserkResult<PbkwSecretOutput> {
    use argon2::{Algorithm, Argon2, ParamsBuilder, Version};
    use blake2::digest::{FixedOutput, KeyInit, Update};
    use blake2::{Blake2b, Blake2bMac};
    use chacha20::cipher::{KeyIvInit, StreamCipher};
    use chacha20::XChaCha20;
    use rand_core::{OsRng, TryRngCore};

    // Type aliases for BLAKE2b variants
    type Blake2b32 = Blake2b<blake2::digest::consts::U32>;
    type Blake2bMac32 = Blake2bMac<blake2::digest::consts::U32>;

    // Generate random salt and nonce
    let mut salt = [0u8; ARGON2_SALT_SIZE];
    let mut nonce = [0u8; XCHACHA20_NONCE_SIZE]; // 24 bytes for XChaCha20
    OsRng
        .try_fill_bytes(&mut salt)
        .map_err(|_| PaserkError::CryptoError)?;
    OsRng
        .try_fill_bytes(&mut nonce)
        .map_err(|_| PaserkError::CryptoError)?;

    // Build Argon2id parameters
    let argon2_params = ParamsBuilder::new()
        .m_cost(params.memory_kib)
        .t_cost(params.iterations)
        .p_cost(params.parallelism)
        .build()
        .map_err(|_| PaserkError::KeyDerivationFailed)?;

    let argon2 = Argon2::new(Algorithm::Argon2id, Version::V0x13, argon2_params);

    // Derive pre-shared key from password: k = Argon2id(pw, s, mem, time, para)
    let mut psk = [0u8; 32];
    argon2
        .hash_password_into(password, &salt, &mut psk)
        .map_err(|_| PaserkError::KeyDerivationFailed)?;

    // Derive encryption key: Ek = crypto_generichash(0xFF || k) - UNKEYED BLAKE2b
    let mut ek_hasher = <Blake2b32 as Default>::default();
    <Blake2b32 as Update>::update(&mut ek_hasher, &[PBKW_ENCRYPTION_KEY_DOMAIN]);
    <Blake2b32 as Update>::update(&mut ek_hasher, &psk);
    let mut encryption_key: [u8; 32] = <Blake2b32 as FixedOutput>::finalize_fixed(ek_hasher).into();

    // Derive authentication key: Ak = crypto_generichash(0xFE || k) - UNKEYED BLAKE2b
    let mut ak_hasher = <Blake2b32 as Default>::default();
    <Blake2b32 as Update>::update(&mut ak_hasher, &[PBKW_AUTH_KEY_DOMAIN]);
    <Blake2b32 as Update>::update(&mut ak_hasher, &psk);
    let mut auth_key: [u8; 32] = <Blake2b32 as FixedOutput>::finalize_fixed(ak_hasher).into();

    // Zeroize psk now that we've derived the keys
    zeroize::Zeroize::zeroize(&mut psk);

    // Encrypt the plaintext key: edk = XChaCha20(msg=ptk, key=Ek, nonce=n)
    // Note: nonce is used DIRECTLY, not derived
    let mut ciphertext = *plaintext_key;
    let mut cipher = XChaCha20::new(&encryption_key.into(), &nonce.into());
    cipher.apply_keystream(&mut ciphertext);

    // Compute authentication tag:
    // t = crypto_generichash(msg=h || s || memlimit || opslimit || parallelism || n || edk, key=Ak, len=32)
    let memlimit_bytes = u64::from(params.memory_kib) * 1024;
    let mut tag_mac = <Blake2bMac32 as KeyInit>::new_from_slice(&auth_key)
        .map_err(|_| PaserkError::CryptoError)?;
    <Blake2bMac32 as Update>::update(&mut tag_mac, header.as_bytes());
    <Blake2bMac32 as Update>::update(&mut tag_mac, &salt);
    <Blake2bMac32 as Update>::update(&mut tag_mac, &memlimit_bytes.to_be_bytes());
    <Blake2bMac32 as Update>::update(&mut tag_mac, &params.iterations.to_be_bytes());
    <Blake2bMac32 as Update>::update(&mut tag_mac, &params.parallelism.to_be_bytes());
    <Blake2bMac32 as Update>::update(&mut tag_mac, &nonce);
    <Blake2bMac32 as Update>::update(&mut tag_mac, &ciphertext);
    let tag: [u8; PBKW_TAG_SIZE] = <Blake2bMac32 as FixedOutput>::finalize_fixed(tag_mac).into();

    // Zeroize derived keys
    zeroize::Zeroize::zeroize(&mut encryption_key);
    zeroize::Zeroize::zeroize(&mut auth_key);

    Ok((salt, nonce, ciphertext, tag))
}

/// Unwraps a secret (signing) key using PBKW for K2/K4.
///
/// # Arguments
///
/// * `salt` - The 16-byte Argon2 salt
/// * `nonce` - The 24-byte `XChaCha20` nonce
/// * `ciphertext` - The 64-byte encrypted key
/// * `tag` - The 32-byte authentication tag
/// * `password` - The password used for wrapping
/// * `params` - Argon2id parameters (must match those used for wrapping)
/// * `header` - The PASERK header (e.g., "k4.secret-pw.")
///
/// # Returns
///
/// The unwrapped 64-byte plaintext key.
#[cfg(any(feature = "k2", feature = "k4"))]
pub fn pbkw_unwrap_secret_k2k4(
    salt: &[u8; ARGON2_SALT_SIZE],
    nonce: &[u8; XCHACHA20_NONCE_SIZE],
    ciphertext: &[u8; 64],
    tag: &[u8; PBKW_TAG_SIZE],
    password: &[u8],
    params: &Argon2Params,
    header: &str,
) -> PaserkResult<[u8; 64]> {
    use argon2::{Algorithm, Argon2, ParamsBuilder, Version};
    use blake2::digest::{FixedOutput, KeyInit, Update};
    use blake2::{Blake2b, Blake2bMac};
    use chacha20::cipher::{KeyIvInit, StreamCipher};
    use chacha20::XChaCha20;
    use subtle::ConstantTimeEq;

    // Type aliases for BLAKE2b variants
    type Blake2b32 = Blake2b<blake2::digest::consts::U32>;
    type Blake2bMac32 = Blake2bMac<blake2::digest::consts::U32>;

    // Build Argon2id parameters
    let argon2_params = ParamsBuilder::new()
        .m_cost(params.memory_kib)
        .t_cost(params.iterations)
        .p_cost(params.parallelism)
        .build()
        .map_err(|_| PaserkError::KeyDerivationFailed)?;

    let argon2 = Argon2::new(Algorithm::Argon2id, Version::V0x13, argon2_params);

    // Derive pre-shared key from password: k = Argon2id(pw, s, mem, time, para)
    let mut psk = [0u8; 32];
    argon2
        .hash_password_into(password, salt, &mut psk)
        .map_err(|_| PaserkError::KeyDerivationFailed)?;

    // Derive encryption key: Ek = crypto_generichash(0xFF || k) - UNKEYED BLAKE2b
    let mut ek_hasher = <Blake2b32 as Default>::default();
    <Blake2b32 as Update>::update(&mut ek_hasher, &[PBKW_ENCRYPTION_KEY_DOMAIN]);
    <Blake2b32 as Update>::update(&mut ek_hasher, &psk);
    let mut encryption_key: [u8; 32] = <Blake2b32 as FixedOutput>::finalize_fixed(ek_hasher).into();

    // Derive authentication key: Ak = crypto_generichash(0xFE || k) - UNKEYED BLAKE2b
    let mut ak_hasher = <Blake2b32 as Default>::default();
    <Blake2b32 as Update>::update(&mut ak_hasher, &[PBKW_AUTH_KEY_DOMAIN]);
    <Blake2b32 as Update>::update(&mut ak_hasher, &psk);
    let mut auth_key: [u8; 32] = <Blake2b32 as FixedOutput>::finalize_fixed(ak_hasher).into();

    // Zeroize psk now that we've derived the keys
    zeroize::Zeroize::zeroize(&mut psk);

    // Verify authentication tag:
    // t = crypto_generichash(msg=h || s || memlimit || opslimit || parallelism || n || edk, key=Ak, len=32)
    let memlimit_bytes = u64::from(params.memory_kib) * 1024;
    let mut tag_mac = <Blake2bMac32 as KeyInit>::new_from_slice(&auth_key)
        .map_err(|_| PaserkError::CryptoError)?;
    <Blake2bMac32 as Update>::update(&mut tag_mac, header.as_bytes());
    <Blake2bMac32 as Update>::update(&mut tag_mac, salt);
    <Blake2bMac32 as Update>::update(&mut tag_mac, &memlimit_bytes.to_be_bytes());
    <Blake2bMac32 as Update>::update(&mut tag_mac, &params.iterations.to_be_bytes());
    <Blake2bMac32 as Update>::update(&mut tag_mac, &params.parallelism.to_be_bytes());
    <Blake2bMac32 as Update>::update(&mut tag_mac, nonce);
    <Blake2bMac32 as Update>::update(&mut tag_mac, ciphertext);
    let computed_tag: [u8; PBKW_TAG_SIZE] =
        <Blake2bMac32 as FixedOutput>::finalize_fixed(tag_mac).into();

    // Zeroize auth key after computing tag
    zeroize::Zeroize::zeroize(&mut auth_key);

    if computed_tag.ct_eq(tag).into() {
        // Decrypt the ciphertext: ptk = XChaCha20(edk, Ek, n)
        // Note: nonce is used DIRECTLY, not derived
        let mut plaintext = *ciphertext;
        let mut cipher = XChaCha20::new(&encryption_key.into(), nonce.into());
        cipher.apply_keystream(&mut plaintext);

        // Zeroize encryption key
        zeroize::Zeroize::zeroize(&mut encryption_key);

        Ok(plaintext)
    } else {
        // Zeroize encryption key on error path
        zeroize::Zeroize::zeroize(&mut encryption_key);
        Err(PaserkError::AuthenticationFailed)
    }
}

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

    // Use minimal params for fast tests
    fn test_params() -> Argon2Params {
        Argon2Params {
            memory_kib: 1024, // 1 MiB
            iterations: 1,
            parallelism: 1,
        }
    }

    #[test]
    #[cfg(feature = "k4")]
    fn test_pbkw_unwrap_k4_local_pw_vector() -> PaserkResult<()> {
        use base64::{engine::general_purpose::URL_SAFE_NO_PAD, Engine};

        // Test vector k4.local-pw-1
        // The password field is used as-is (not hex-decoded)
        let paserk = "k4.local-pw.9VvzoqE_i23NOqsP9xoijQAAAAAEAAAAAAAAAgAAAAG_uxDZC-NsYyOW8OUOqISJqgHN8xIfAXiPfmFTfB4GPidUzm4aKzMGJmZtRPeyZCV11MxEJS3VMIRHXxYsfUQsmWLALpFwqUhxZdk_ymFcK2Nk0-N7CVp-";
        let header = "k4.local-pw.";
        let password = b"636f727265637420686f727365206261747465727920737461706c65";
        let expected_key =
            hex::decode("707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f")
                .unwrap();

        // Decode the data part
        let data_b64 = paserk.strip_prefix(header).unwrap();
        let data = URL_SAFE_NO_PAD.decode(data_b64).unwrap();

        // Parse components
        let salt: [u8; ARGON2_SALT_SIZE] = data[0..16].try_into().unwrap();
        let memlimit_bytes = u64::from_be_bytes(data[16..24].try_into().unwrap());
        let opslimit = u32::from_be_bytes(data[24..28].try_into().unwrap());
        let parallelism = u32::from_be_bytes(data[28..32].try_into().unwrap());
        let nonce: [u8; XCHACHA20_NONCE_SIZE] = data[32..56].try_into().unwrap();
        let ciphertext: [u8; 32] = data[56..88].try_into().unwrap();
        let tag: [u8; PBKW_TAG_SIZE] = data[88..120].try_into().unwrap();

        let params = Argon2Params {
            memory_kib: u32::try_from(memlimit_bytes / 1024)
                .map_err(|_| PaserkError::InvalidKey)?,
            iterations: opslimit,
            parallelism,
        };

        let unwrapped =
            pbkw_unwrap_local_k2k4(&salt, &nonce, &ciphertext, &tag, password, &params, header)?;

        assert_eq!(unwrapped.as_slice(), expected_key.as_slice());
        Ok(())
    }

    #[test]
    #[cfg(feature = "k4")]
    fn test_pbkw_wrap_unwrap_local_roundtrip() -> PaserkResult<()> {
        let plaintext_key = [0x13u8; 32];
        let password = b"hunter2";
        let params = test_params();
        let header = "k4.local-pw.";

        let (salt, nonce, ciphertext, tag) =
            pbkw_wrap_local_k2k4(&plaintext_key, password, &params, header)?;

        assert_ne!(ciphertext, plaintext_key);

        let unwrapped =
            pbkw_unwrap_local_k2k4(&salt, &nonce, &ciphertext, &tag, password, &params, header)?;

        assert_eq!(unwrapped, plaintext_key);
        Ok(())
    }

    #[test]
    #[cfg(feature = "k4")]
    fn test_pbkw_wrap_unwrap_secret_roundtrip() -> PaserkResult<()> {
        let plaintext_key = [0x13u8; 64];
        let password = b"hunter2";
        let params = test_params();
        let header = "k4.secret-pw.";

        let (salt, nonce, ciphertext, tag) =
            pbkw_wrap_secret_k2k4(&plaintext_key, password, &params, header)?;

        assert_ne!(ciphertext, plaintext_key);

        let unwrapped =
            pbkw_unwrap_secret_k2k4(&salt, &nonce, &ciphertext, &tag, password, &params, header)?;

        assert_eq!(unwrapped, plaintext_key);
        Ok(())
    }

    #[test]
    #[cfg(feature = "k4")]
    fn test_pbkw_unwrap_wrong_password() -> PaserkResult<()> {
        let plaintext_key = [0x13u8; 32];
        let password = b"hunter2";
        let wrong_password = b"hunter3";
        let params = test_params();
        let header = "k4.local-pw.";

        let (salt, nonce, ciphertext, tag) =
            pbkw_wrap_local_k2k4(&plaintext_key, password, &params, header)?;

        let result = pbkw_unwrap_local_k2k4(
            &salt,
            &nonce,
            &ciphertext,
            &tag,
            wrong_password,
            &params,
            header,
        );

        assert!(matches!(result, Err(PaserkError::AuthenticationFailed)));
        Ok(())
    }

    #[test]
    #[cfg(feature = "k4")]
    fn test_pbkw_unwrap_modified_tag() -> PaserkResult<()> {
        let plaintext_key = [0x13u8; 32];
        let password = b"hunter2";
        let params = test_params();
        let header = "k4.local-pw.";

        let (salt, nonce, ciphertext, mut tag) =
            pbkw_wrap_local_k2k4(&plaintext_key, password, &params, header)?;

        tag[0] ^= 0xff;

        let result =
            pbkw_unwrap_local_k2k4(&salt, &nonce, &ciphertext, &tag, password, &params, header);

        assert!(matches!(result, Err(PaserkError::AuthenticationFailed)));
        Ok(())
    }

    #[test]
    fn test_argon2_params_presets() {
        let interactive = Argon2Params::interactive();
        assert_eq!(interactive.memory_kib, 64 * 1024);
        assert_eq!(interactive.iterations, 2);
        assert_eq!(interactive.parallelism, 1);

        let moderate = Argon2Params::moderate();
        assert_eq!(moderate.memory_kib, 256 * 1024);
        assert_eq!(moderate.iterations, 3);
        assert_eq!(moderate.parallelism, 1);

        let sensitive = Argon2Params::sensitive();
        assert_eq!(sensitive.memory_kib, 1024 * 1024);
        assert_eq!(sensitive.iterations, 4);
        assert_eq!(sensitive.parallelism, 1);
    }
}