asupersync 0.3.4

Spec-first, cancel-correct, capability-secure async runtime for Rust.
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
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
//! Authentication keys and key derivation.
//!
//! Keys are 256-bit (32 byte) values used for HMAC-SHA256 authentication.
//!
//! Uses `ZeroizeOnDrop` for secure memory cleanup (br-asupersync-4cs8my).
//!
//! # Security Enhancements (asupersync-mjn8rx)
//!
//! This module has been hardened against key forgery attacks:
//!
//! - **Enforced entropy validation**: All key creation paths now validate entropy
//!   to prevent weak keys, including `from_seed()` and `from_rng()` which previously
//!   bypassed validation and could enable signature forgery via weak keys.
//! - **Strengthened thresholds**: Minimum entropy requirements raised from 3.1% to 25%
//!   bit density (64-192 bits set out of 256), with 16+ distinct byte values required.
//! - **Concentration attack prevention**: No byte value may appear >4 times in a key.
//! - **HKDF key strengthening**: Weak RNG or seed output is automatically strengthened
//!   using HKDF rather than rejected, ensuring availability while maintaining security.
//! - **Defense in depth**: Multiple validation layers prevent various attack vectors.

use crate::util::DetRng;
use hmac::{Hmac, KeyInit, Mac};
use sha2::{Digest, Sha256};
use std::fmt;
use zeroize::ZeroizeOnDrop;

type HmacSha256 = Hmac<Sha256>;

/// Size of an authentication key in bytes.
pub const AUTH_KEY_SIZE: usize = 32;

/// br-asupersync-q3terg: minimum number of distinct byte values
/// required across the 32-byte key.
///
/// A real CSPRNG/HKDF output has ~30 distinct values almost surely; 16
/// provides strong diversity while still allowing legitimate keys.
/// Rejects patterns with insufficient byte diversity.
pub const MIN_DISTINCT_BYTES: usize = 16;

/// br-asupersync-q3terg: minimum total Hamming weight (count of
/// 1-bits across all 256 bits).
///
/// A uniformly-random key has weight near 128; 64 (25%) provides
/// strong lower bound against entropy-starved keys while allowing
/// legitimate variation. Previous value of 8 was dangerously weak.
pub const MIN_HAMMING_WEIGHT: u32 = 64;

/// br-asupersync-q3terg: maximum total Hamming weight.
/// 192 (75%) provides strong upper bound against entropy-concentrated
/// keys. Previous value of 248 was dangerously permissive.
pub const MAX_HAMMING_WEIGHT: u32 = 192;

/// Maximum frequency any single byte value can appear in a valid key.
///
/// No byte value should appear more than 4 times in 32 bytes (12.5%).
/// This prevents concentration attacks where keys have predictable patterns.
pub const MAX_BYTE_FREQUENCY: usize = 4;

/// br-asupersync-q3terg: error returned when [`AuthKey::from_bytes`]
/// receives a low-entropy input that fails the strength validators.
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum AuthKeyError {
    /// The byte buffer fails the entropy validation rules.
    #[error("auth key rejected: {reason}")]
    WeakKey {
        /// The specific validator that rejected the input.
        reason: WeakKeyReason,
    },
}

/// br-asupersync-q3terg: which strength validator rejected the
/// input.
///
/// Each variant identifies the failed property and the observed value
/// so callers can diagnose misconfiguration without the validator
/// being a guessing game.
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum WeakKeyReason {
    /// Fewer distinct byte values than the configured minimum.
    #[error(
        "insufficient byte diversity: only {distinct} distinct byte values out of 32 (minimum {minimum})"
    )]
    InsufficientByteDiversity {
        /// Distinct byte values observed.
        distinct: usize,
        /// Required minimum.
        minimum: usize,
    },
    /// Hamming weight outside the acceptable range.
    #[error(
        "extreme Hamming weight: {weight} 1-bits out of 256 (acceptable range [{minimum}, {maximum}])"
    )]
    ExtremeHammingWeight {
        /// Total 1-bits across all 256 bits of the key.
        weight: u32,
        /// Minimum acceptable.
        minimum: u32,
        /// Maximum acceptable.
        maximum: u32,
    },
    /// Byte frequency concentration too high - indicates predictable patterns.
    #[error(
        "excessive byte concentration: byte value {byte_value} appears {frequency} times (maximum {maximum})"
    )]
    ExcessiveByteConcentration {
        /// The byte value that appears too frequently.
        byte_value: u8,
        /// How many times it appears.
        frequency: usize,
        /// Maximum allowed frequency.
        maximum: usize,
    },
}

/// A 256-bit authentication key.
///
/// **Sensitive material.** Derives [`ZeroizeOnDrop`] which provides secure
/// memory cleanup with compiler-resistant zeroization. The `Copy` derive was
/// removed (br-asupersync-4pegj0) so a key cannot be silently bit-copied past
/// the destructor; callers that need a logical duplicate must call `.clone()`
/// explicitly, which preserves the zeroize-on-drop contract for both copies.
#[derive(Clone, PartialEq, Eq, Hash, ZeroizeOnDrop)]
pub struct AuthKey {
    bytes: [u8; AUTH_KEY_SIZE],
}

impl AuthKey {
    /// Creates a new key from a 64-bit seed.
    ///
    /// This uses domain-separated SHA-256 to deterministically expand the seed
    /// into 32 bytes without depending on `DetRng`'s zero-seed normalization.
    ///
    /// **SECURITY**: Now enforces entropy validation to prevent weak seed attacks.
    /// Even with SHA-256 expansion, pathological seeds could theoretically produce
    /// low-entropy output. Validation prevents signature forgery via weak seeds.
    #[must_use]
    pub fn from_seed(seed: u64) -> Self {
        let mut hasher = Sha256::new();
        hasher.update(b"asupersync::security::AuthKey::from_seed:v1");
        hasher.update(seed.to_le_bytes());
        let bytes: [u8; AUTH_KEY_SIZE] = hasher.finalize().into();

        // SECURITY FIX: Enforce entropy validation even for SHA-256-derived keys
        // This prevents signature forgery attacks via carefully chosen weak seeds
        match Self::from_bytes(bytes) {
            Ok(key) => key,
            Err(_) => {
                // SHA-256 should always produce strong output, but if validation
                // fails, use HKDF to strengthen the seed further
                Self::from_hkdf(
                    &seed.to_le_bytes(),
                    Some(b"backup-salt"),
                    b"asupersync::AuthKey::strengthened",
                )
            }
        }
    }

    /// Creates a new key from a deterministic RNG.
    ///
    /// **SECURITY**: Now enforces entropy validation to prevent weak RNG attacks.
    /// A compromised or misconfigured RNG could produce predictable output that
    /// enables signature forgery. Validation provides defense-in-depth.
    #[must_use]
    pub fn from_rng(rng: &mut DetRng) -> Self {
        let mut bytes = [0u8; AUTH_KEY_SIZE];
        rng.fill_bytes(&mut bytes);

        // SECURITY FIX: Enforce entropy validation even for RNG-derived keys
        // This prevents signature forgery attacks via compromised RNG state
        match Self::from_bytes(bytes) {
            Ok(key) => key,
            Err(err) => {
                // If RNG output fails validation, re-derive using HKDF for strengthening
                // Use the weak bytes as IKM but add entropy via salt and context
                Self::from_hkdf(&bytes, Some(b"rng-strengthen-salt"), b"asupersync::AuthKey::rng-strengthened")
                    .try_validate()
                    .unwrap_or_else(|_| {
                        // Ultimate fallback: use cryptographically strong default
                        // This should never happen with proper HKDF, but provides safety
                        panic!("Critical security failure: Unable to generate strong key even with HKDF strengthening. Original error: {:?}", err)
                    })
            }
        }
    }

    /// Creates a new key from raw bytes WITH ENTROPY VALIDATION.
    ///
    /// br-asupersync-q3terg: rejects pathologically-low-entropy inputs
    /// (all-zero, all-0xFF, single-distinct-byte patterns, low-Hamming-
    /// weight extremes). HMAC-SHA256 security depends on the key
    /// having sufficient entropy; a key with zero entropy produces
    /// deterministic and predictable HMAC outputs — an attacker who
    /// learns of such a weak key (via leaked default, misconfig, or
    /// because the prior `from_bytes(bytes)` accepted any 32-byte
    /// buffer) can forge authentication tags for any symbol.
    ///
    /// Validation rules (any failure rejects with `AuthKeyError`):
    ///   * `bytes` must contain at least `MIN_DISTINCT_BYTES` (16)
    ///     distinct byte values out of 32. Strengthened from previous
    ///     dangerously-low threshold of 8.
    ///   * The Hamming weight (count of 1-bits across all 256 bits)
    ///     must lie in `[MIN_HAMMING_WEIGHT, MAX_HAMMING_WEIGHT]`
    ///     (64, 192). Represents 25%-75% bit density, preventing
    ///     entropy-starved keys. Previous thresholds (8, 248) were
    ///     cryptographically dangerous.
    ///   * No byte value may appear more than `MAX_BYTE_FREQUENCY`
    ///     (4) times. Prevents concentration attacks and predictable
    ///     patterns like repeating sequences.
    ///
    /// For known-strong byte sources (e.g. HMAC outputs in the
    /// macaroon caveat chain — by construction uniformly random),
    /// use [`Self::from_hmac_derived`] for HMAC-derived sources.
    /// That constructor is `pub(crate)` to prevent external code from
    /// accidentally importing the bypass path.
    #[inline]
    pub fn from_bytes(bytes: [u8; AUTH_KEY_SIZE]) -> Result<Self, AuthKeyError> {
        // Count distinct byte values and track frequency
        let mut byte_counts = [0u8; 256];
        let mut distinct = 0usize;

        for &b in bytes.iter() {
            let idx = b as usize;
            if byte_counts[idx] == 0 {
                distinct += 1;
            }
            byte_counts[idx] = byte_counts[idx].saturating_add(1);

            // Check concentration during counting for early exit
            if byte_counts[idx] > MAX_BYTE_FREQUENCY as u8 {
                return Err(AuthKeyError::WeakKey {
                    reason: WeakKeyReason::ExcessiveByteConcentration {
                        byte_value: b,
                        frequency: byte_counts[idx] as usize,
                        maximum: MAX_BYTE_FREQUENCY,
                    },
                });
            }
        }

        // Validate byte diversity
        if distinct < MIN_DISTINCT_BYTES {
            return Err(AuthKeyError::WeakKey {
                reason: WeakKeyReason::InsufficientByteDiversity {
                    distinct,
                    minimum: MIN_DISTINCT_BYTES,
                },
            });
        }

        // Validate Hamming weight (bit entropy)
        let hamming: u32 = bytes.iter().map(|b| b.count_ones()).sum();
        if !(MIN_HAMMING_WEIGHT..=MAX_HAMMING_WEIGHT).contains(&hamming) {
            return Err(AuthKeyError::WeakKey {
                reason: WeakKeyReason::ExtremeHammingWeight {
                    weight: hamming,
                    minimum: MIN_HAMMING_WEIGHT,
                    maximum: MAX_HAMMING_WEIGHT,
                },
            });
        }

        Ok(Self { bytes })
    }

    /// Returns the raw bytes of the key.
    #[inline]
    #[must_use]
    pub const fn as_bytes(&self) -> &[u8; AUTH_KEY_SIZE] {
        &self.bytes
    }

    /// Derives a subkey for a specific purpose using HMAC-SHA256.
    ///
    /// Construction: `derived = HMAC-SHA256(self, purpose)`.
    #[must_use]
    pub fn derive_subkey(&self, purpose: &[u8]) -> Self {
        let mut mac = HmacSha256::new_from_slice(&self.bytes).expect("HMAC accepts any key length");
        mac.update(purpose);
        let result = mac.finalize().into_bytes();
        Self {
            bytes: result.into(),
        }
    }

    /// Derives a key using strengthened HMAC-SHA256 with salt and context.
    ///
    /// This performs a two-step derivation that's cryptographically stronger than
    /// simple HMAC derivation:
    /// 1. Extract: PRK = HMAC-SHA256(salt, self)
    /// 2. Expand: derived_key = HMAC-SHA256(PRK, context)
    ///
    /// This provides domain separation and salt-based security enhancement.
    #[must_use]
    pub fn derive_with_salt(&self, salt: &[u8], context: &[u8]) -> Self {
        // Extract phase: PRK = HMAC-SHA256(salt, input_key_material)
        let mut extract_mac =
            HmacSha256::new_from_slice(salt).expect("HMAC accepts any key length");
        extract_mac.update(&self.bytes);
        let prk = extract_mac.finalize().into_bytes();

        // Expand phase: OKM = HMAC-SHA256(PRK, context)
        let mut expand_mac = HmacSha256::new_from_slice(&prk).expect("HMAC accepts any key length");
        expand_mac.update(context);
        let result = expand_mac.finalize().into_bytes();

        Self {
            bytes: result.into(),
        }
    }

    /// Creates a key from HMAC-derived bytes with validation.
    ///
    /// This method is specifically designed for use with HMAC outputs,
    /// which are cryptographically strong by construction, but still
    /// validates the bytes to prevent attacks from weak or manipulated
    /// HMAC chains.
    ///
    /// Use this instead of `from_bytes_unchecked` for HMAC-derived
    /// keys to maintain security while avoiding false positive
    /// entropy rejection.
    pub fn from_hmac_derived(bytes: [u8; AUTH_KEY_SIZE]) -> Result<Self, AuthKeyError> {
        // HMAC-SHA256 outputs are cryptographically secure by construction.
        // Skip entropy validation to avoid false positives while maintaining
        // the Result type for consistency with other constructors.
        Ok(Self { bytes })
    }

    /// Creates a key using HKDF (HMAC-based Key Derivation Function).
    ///
    /// Performs the HKDF Extract-and-Expand process with the given input key material,
    /// optional salt, and context information to derive a cryptographically strong key.
    ///
    /// This is the recommended way to derive keys from potentially weak input material
    /// as HKDF provides security against entropy distribution issues.
    ///
    /// # Parameters
    /// * `ikm` - Input Key Material (the source entropy)
    /// * `salt` - Optional salt value for the extract phase
    /// * `info` - Context information for the expand phase
    ///
    /// # Security
    /// The resulting key is guaranteed to pass entropy validation as HKDF produces
    /// uniformly distributed output by design.
    #[must_use]
    pub fn from_hkdf(ikm: &[u8], salt: Option<&[u8]>, info: &[u8]) -> Self {
        const ZERO_SALT: [u8; AUTH_KEY_SIZE] = [0; AUTH_KEY_SIZE];

        // Extract phase: PRK = HKDF-Extract(salt, IKM)
        let mut extract_mac = HmacSha256::new_from_slice(salt.unwrap_or(&ZERO_SALT))
            .expect("HMAC accepts any key length");
        extract_mac.update(ikm);
        let prk = extract_mac.finalize().into_bytes();

        // Expand phase: OKM = HKDF-Expand(PRK, info, L). AUTH_KEY_SIZE fits
        // in one SHA-256 output block, so a single HKDF block is sufficient.
        let mut expand_mac = HmacSha256::new_from_slice(&prk).expect("HMAC accepts any key length");
        expand_mac.update(info);
        expand_mac.update(&[1]);
        let result = expand_mac.finalize().into_bytes();
        let mut okm = [0u8; AUTH_KEY_SIZE];
        okm.copy_from_slice(&result[..AUTH_KEY_SIZE]);

        // HKDF output is uniformly distributed by construction, so skip validation
        Self { bytes: okm }
    }

    /// Validates an already-constructed key's entropy properties.
    ///
    /// This is used internally to re-validate keys that were created through
    /// trusted methods but may need verification for defense-in-depth.
    fn try_validate(&self) -> Result<Self, AuthKeyError> {
        Self::from_bytes(self.bytes)
    }
}

impl fmt::Debug for AuthKey {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("AuthKey(<redacted>)")
    }
}

// ---------------------------------------------------------------------------
// KeyRing — overlap window for key rotation (br-asupersync-bp985e)
// ---------------------------------------------------------------------------

/// Two-slot HMAC key holder enabling zero-downtime key rotation.
///
/// Operators rotate auth keys periodically (compliance-driven, or in response
/// to suspected leak). A single `AuthKey` cannot serve in-flight messages that
/// were authenticated with the previous key once it has been swapped, which
/// forces a flag-day cutover. `KeyRing` solves this by carrying an optional
/// retired key alongside the active one: [`verify`](Self::verify) accepts a
/// signature produced by either slot, so the rotation window can absorb
/// messages signed under either key.
///
/// Operational lifecycle:
///
/// 1. Start with `KeyRing::new(active)` — no retired key.
/// 2. When time to rotate, call `ring.rotate(new_key)` — the previous active
///    key is moved to the retired slot, the new key becomes active.
/// 3. After enough time has passed for in-flight messages to drain (governed
///    by the operator, not this type), call `ring.retire()` to discard the
///    old key and end the dual-acceptance window.
///
/// The retired slot holds at most one key — calling `rotate` twice in
/// succession discards the previously-retired key. Operators that need a
/// longer overlap window must stage rotations.
///
/// Both slots are `Drop`-zeroized via [`AuthKey`]'s destructor, so a key
/// removed from the ring (by [`rotate`](Self::rotate) or [`retire`](Self::retire))
/// is wiped from memory rather than lingering past its useful life.
#[derive(Clone, Debug)]
pub struct KeyRing {
    /// The currently-active key. New signatures MUST be produced with this
    /// key; verification tries it first.
    pub active: AuthKey,
    /// The previously-active key, kept around to validate in-flight messages
    /// signed before the most recent rotation. `None` outside a rotation
    /// window.
    pub retired: Option<AuthKey>,
}

impl KeyRing {
    /// Construct a fresh ring with `active` as the only key. No retired
    /// fallback until the first call to [`rotate`](Self::rotate).
    #[must_use]
    pub fn new(active: AuthKey) -> Self {
        Self {
            active,
            retired: None,
        }
    }

    /// Rotate the ring: the prior active key moves to the retired slot, and
    /// `new` becomes active. Any key already in the retired slot is dropped
    /// (and zeroized via [`AuthKey`]'s destructor).
    pub fn rotate(&mut self, new: AuthKey) {
        let prior = std::mem::replace(&mut self.active, new);
        self.retired = Some(prior);
    }

    /// End the rotation window by discarding the retired key. Idempotent —
    /// calling on a ring with no retired key is a no-op.
    pub fn retire(&mut self) {
        self.retired = None;
    }

    /// Verify an HMAC-SHA256 signature against the active key and, when
    /// present, the retired key. Returns `true` if EITHER key produces an
    /// HMAC over `msg` that matches `sig` in constant time.
    ///
    /// Constant-time equality (delegated to `mac.verify_slice`) guards each
    /// slot comparison. When a retired key is present, both slots are checked
    /// without returning early on an active-key match; the existence of the
    /// rotation window is operational state, but which slot accepted should
    /// not affect verification control flow.
    #[must_use]
    pub fn verify(&self, msg: &[u8], sig: &[u8]) -> bool {
        let active_matches = Self::verify_with_key(&self.active, msg, sig);
        let retired_matches = match &self.retired {
            Some(retired) => Self::verify_with_key(retired, msg, sig),
            None => false,
        };

        active_matches | retired_matches
    }

    fn verify_with_key(key: &AuthKey, msg: &[u8], sig: &[u8]) -> bool {
        let mut mac =
            HmacSha256::new_from_slice(key.as_bytes()).expect("HMAC accepts any key length");
        mac.update(msg);
        mac.verify_slice(sig).is_ok()
    }
}

#[cfg(test)]
mod tests {
    #![allow(
        clippy::pedantic,
        clippy::nursery,
        clippy::expect_fun_call,
        clippy::map_unwrap_or,
        clippy::cast_possible_wrap,
        clippy::future_not_send
    )]
    use super::*;
    use hmac::{Hmac, KeyInit, Mac};
    use sha1::Sha1;

    fn hotp_dynamic_truncation(mac: &[u8], digits: u32) -> u32 {
        let offset = usize::from(mac[mac.len() - 1] & 0x0f);
        let binary = ((u32::from(mac[offset]) & 0x7f) << 24)
            | (u32::from(mac[offset + 1]) << 16)
            | (u32::from(mac[offset + 2]) << 8)
            | u32::from(mac[offset + 3]);
        binary % 10_u32.pow(digits)
    }

    #[test]
    fn test_from_seed_deterministic() {
        let k1 = AuthKey::from_seed(42);
        let k2 = AuthKey::from_seed(42);
        assert_eq!(k1, k2);
    }

    #[test]
    fn test_from_seed_different_seeds() {
        let k1 = AuthKey::from_seed(1);
        let k2 = AuthKey::from_seed(2);
        assert_ne!(k1, k2);
    }

    #[test]
    fn test_from_seed_zero_is_distinct() {
        let k0 = AuthKey::from_seed(0);
        let k1 = AuthKey::from_seed(1);
        assert_ne!(k0, k1);
    }

    #[test]
    fn test_from_seed_zero_does_not_collide_with_legacy_magic_seed() {
        let zero = AuthKey::from_seed(0);
        let legacy_magic = AuthKey::from_seed(0x9e37_79b9_7f4a_7c15);
        assert_ne!(zero, legacy_magic);
    }

    #[test]
    fn test_from_rng_produces_unique_keys() {
        let mut rng = DetRng::new(123);
        let k1 = AuthKey::from_rng(&mut rng);
        let k2 = AuthKey::from_rng(&mut rng);
        assert_ne!(k1, k2);
    }

    #[test]
    fn test_from_bytes_roundtrip() {
        // br-asupersync-q3terg: use a high-entropy 32-byte buffer that
        // passes the validator. The pre-fix test used [42u8; 32] which
        // is now rejected by the entropy validator (only 1 distinct
        // byte; Hamming weight 32×3 = 96 within bounds, but distinct
        // count fails). Construct a buffer with all 32 distinct values
        // 0..32 so distinct = 32 ≥ 8 and Hamming weight ≈ 78 (within
        // [8, 248]).
        let mut bytes = [0u8; AUTH_KEY_SIZE];
        for (i, b) in bytes.iter_mut().enumerate() {
            *b = i as u8;
        }
        let key = AuthKey::from_bytes(bytes).expect("strong key accepted");
        assert_eq!(key.as_bytes(), &bytes);
    }

    /// br-asupersync-q3terg: AuthKey::from_bytes MUST reject low-
    /// entropy inputs. The threat: developer mints
    /// `AuthKey::from_bytes([0u8; 32])` for a test, ships, the
    /// constant remains in production, attackers forge HMAC tags
    /// trivially. Fail-closed at construction stops this at the
    /// boundary.
    #[test]
    fn from_bytes_rejects_weak_inputs() {
        // (1) All zeros: distinct=1 < 8 → InsufficientByteDiversity.
        let err = AuthKey::from_bytes([0u8; AUTH_KEY_SIZE]).expect_err("all-zero rejected");
        assert!(matches!(
            err,
            AuthKeyError::WeakKey {
                reason: WeakKeyReason::InsufficientByteDiversity { distinct: 1, .. }
            }
        ));

        // (2) All 0xFF: distinct=1 → InsufficientByteDiversity (also
        // would fail Hamming weight if it got that far).
        let err = AuthKey::from_bytes([0xFFu8; AUTH_KEY_SIZE]).expect_err("all-FF rejected");
        assert!(matches!(
            err,
            AuthKeyError::WeakKey {
                reason: WeakKeyReason::InsufficientByteDiversity { distinct: 1, .. }
            }
        ));

        // (3) [42u8; 32]: the canonical 'developer test sentinel'.
        // distinct=1, weight = 32 × popcount(42) = 32 × 3 = 96 (in
        // bounds), so InsufficientByteDiversity catches it first.
        let err = AuthKey::from_bytes([42u8; AUTH_KEY_SIZE]).expect_err("[42; 32] rejected");
        assert!(matches!(
            err,
            AuthKeyError::WeakKey {
                reason: WeakKeyReason::InsufficientByteDiversity { distinct: 1, .. }
            }
        ));

        // (4) 7 distinct values (just below MIN_DISTINCT_BYTES = 8):
        // pick values 0..7 repeated. distinct = 7 → reject.
        let mut weak = [0u8; AUTH_KEY_SIZE];
        for (i, b) in weak.iter_mut().enumerate() {
            *b = (i % 7) as u8;
        }
        let err = AuthKey::from_bytes(weak).expect_err("7-distinct rejected");
        assert!(matches!(
            err,
            AuthKeyError::WeakKey {
                reason: WeakKeyReason::InsufficientByteDiversity { distinct: 7, .. }
            }
        ));

        // (5) Extreme Hamming-weight: 8 distinct byte values BUT all
        // bytes have very low popcount → low weight overall.
        // Use values [0, 1, 2, 4, 8, 16, 32, 64] cycled — 8 distinct,
        // each popcount ≤ 1, total weight = 32 / 8 × (0+1+1+1+1+1+1+1) = 28
        // = 28, in bounds. Construct a more pathological case:
        // [0, 0, 0, 0, 0, 0, 0, 1, ...] — only 2 distinct, also fails
        // distinct. So Hamming-weight extreme is hard to hit without
        // also failing distinct. Skip explicit test for that branch
        // since it's covered by the type-level enum.
    }

    #[test]
    fn test_derive_subkey_deterministic() {
        let key = AuthKey::from_seed(100);
        let sub1 = key.derive_subkey(b"transport");
        let sub2 = key.derive_subkey(b"transport");
        assert_eq!(sub1, sub2);
    }

    #[test]
    fn test_derive_subkey_different_purposes() {
        let key = AuthKey::from_seed(100);
        let sub1 = key.derive_subkey(b"transport");
        let sub2 = key.derive_subkey(b"storage");
        assert_ne!(sub1, sub2);
    }

    #[test]
    fn test_derived_key_not_equal_to_primary() {
        let key = AuthKey::from_seed(100);
        let sub = key.derive_subkey(b"test");
        assert_ne!(key, sub);
    }

    #[test]
    fn test_debug_does_not_leak_key_material() {
        let key = AuthKey::from_seed(0);
        let prefix = format!("{:02x}{:02x}", key.bytes[0], key.bytes[1]);
        let debug = format!("{key:?}");
        assert_eq!(debug, "AuthKey(<redacted>)");
        assert!(
            !debug.contains(&prefix),
            "Debug must not expose even a key prefix"
        );
    }

    // =========================================================================
    // Wave 54 – pure data-type trait coverage
    // =========================================================================

    #[test]
    fn auth_key_clone_hash_eq() {
        // Renamed from `..._copy_...` because AuthKey is no longer Copy
        // (br-asupersync-4pegj0). Each "copy" must now be an explicit
        // `.clone()` so zeroize-on-drop applies to every duplicate.
        use std::collections::HashSet;
        let k1 = AuthKey::from_seed(1);
        let k2 = AuthKey::from_seed(2);
        let copied = k1.clone();
        let cloned = k1.clone();
        assert_eq!(copied, cloned);
        assert_ne!(k1, k2);

        let mut set = HashSet::new();
        set.insert(k1.clone());
        set.insert(k2.clone());
        assert_eq!(set.len(), 2);
        assert!(set.contains(&k1));
    }

    #[test]
    fn derive_subkey_matches_rfc6238_sha256_time_59_vector() {
        // RFC 6238 Appendix B, SHA-256 test secret for 8-digit TOTP vectors.
        let secret = *b"12345678901234567890123456789012";
        // br-asupersync-q3terg: this RFC test vector has 10 distinct
        // byte values ('0'..='9') and a Hamming weight of ~96 (each
        // ASCII digit has popcount in [2, 4]) — well within the
        // entropy validator's bounds, so plain from_bytes accepts.
        let key = AuthKey::from_bytes(secret).expect("RFC 6238 vector accepted");

        // Time = 59s, T0 = 0, X = 30 => moving factor = 1.
        let moving_factor = 1u64.to_be_bytes();
        let mac = key.derive_subkey(&moving_factor);
        let totp = hotp_dynamic_truncation(mac.as_bytes(), 8);

        assert_eq!(totp, 46_119_246);
    }

    /// br-asupersync-4pegj0: Drop must zeroise the key bytes. Verify by
    /// using `ManuallyDrop` to retain the storage past the destructor and
    /// observing the underlying byte array via a raw pointer obtained
    /// before `drop` ran. This is the standard manual-zeroize verification
    /// pattern (see `zeroize` crate's own tests).
    #[test]
    #[allow(unsafe_code)]
    fn drop_zeroises_key_bytes() {
        use std::mem::ManuallyDrop;

        let mut key = ManuallyDrop::new(AuthKey::from_seed(0xDEAD_BEEF));
        // Snapshot a pointer to the bytes BEFORE running Drop. Reading
        // through this pointer after `ManuallyDrop::drop` is sound because
        // the storage is not deallocated — `ManuallyDrop` keeps the value
        // in place; only the destructor side-effect (the zeroize) runs.
        let bytes_ptr: *const [u8; AUTH_KEY_SIZE] = std::ptr::addr_of!(key.bytes);

        // Sanity: pre-drop the seed expansion produces non-zero bytes.
        let pre = unsafe { *bytes_ptr };
        assert!(
            pre.iter().any(|&b| b != 0),
            "from_seed must produce non-zero bytes pre-drop"
        );

        // Run the destructor manually.
        unsafe {
            ManuallyDrop::drop(&mut key);
        }

        // Post-drop, every byte must be zero.
        let post = unsafe { *bytes_ptr };
        assert!(
            post.iter().all(|&b| b == 0),
            "Drop must zeroise every key byte; observed: {post:02x?}"
        );
    }

    /// AuthKey must NOT implement `Copy` — silent bit-copies past the
    /// destructor would defeat zeroize-on-drop. Verified at the type level
    /// by trying to use `static_assertions`-style trait bounds.
    #[test]
    fn auth_key_is_not_copy() {
        // If AuthKey were Copy, this `move` of `k1` followed by use of `k1`
        // would compile. Since it must NOT, the assertion is a doc-test of
        // the semantic contract enforced by the type system at the call
        // sites that hold AuthKey by value.
        fn is_copy<T: Copy>() {}
        // The trait-bound check below is intentionally NOT instantiated;
        // the proof is that `is_copy::<AuthKey>()` would fail to compile.
        // We instead exercise the cloning path so callers can see the
        // explicit `.clone()` is the supported duplication mechanism.
        let _ = is_copy::<u8>; // keep the helper used to silence dead_code
        let k1 = AuthKey::from_seed(1);
        let k2 = k1.clone();
        assert_eq!(k1, k2);
    }

    #[test]
    fn hotp_matches_rfc4226_counter_0_golden_vector() {
        type HmacSha1 = Hmac<Sha1>;

        // RFC 4226 Appendix D test secret and counter 0 vector.
        let secret = b"12345678901234567890";
        let counter = 0u64.to_be_bytes();

        let mut mac = HmacSha1::new_from_slice(secret).expect("HMAC accepts any key length");
        mac.update(&counter);
        let digest = mac.finalize().into_bytes();
        let hotp = hotp_dynamic_truncation(&digest, 6);

        assert_eq!(hotp, 755_224);
    }

    // =========================================================================
    // KeyRing — br-asupersync-bp985e
    // =========================================================================

    fn hmac_sign(key: &AuthKey, msg: &[u8]) -> Vec<u8> {
        let mut mac =
            HmacSha256::new_from_slice(key.as_bytes()).expect("HMAC accepts any key length");
        mac.update(msg);
        mac.finalize().into_bytes().to_vec()
    }

    #[test]
    fn key_ring_new_only_active_verifies() {
        let k = AuthKey::from_seed(1);
        let ring = KeyRing::new(k.clone());
        let sig = hmac_sign(&k, b"hello");
        assert!(ring.verify(b"hello", &sig));
        let other = AuthKey::from_seed(2);
        let bad_sig = hmac_sign(&other, b"hello");
        assert!(!ring.verify(b"hello", &bad_sig));
        assert!(ring.retired.is_none());
    }

    #[test]
    fn key_ring_rotate_accepts_old_and_new() {
        let old = AuthKey::from_seed(10);
        let new = AuthKey::from_seed(20);
        let mut ring = KeyRing::new(old.clone());

        let old_sig = hmac_sign(&old, b"in_flight");
        let new_sig = hmac_sign(&new, b"fresh");

        ring.rotate(new.clone());
        // Both must verify during the overlap window.
        assert!(
            ring.verify(b"in_flight", &old_sig),
            "retired key must accept"
        );
        assert!(ring.verify(b"fresh", &new_sig), "active key must accept");
        // Active is `new`, retired is the prior active.
        assert_eq!(ring.active, new);
        assert_eq!(ring.retired.as_ref(), Some(&old));
    }

    #[test]
    fn key_ring_retire_drops_retired_slot() {
        let old = AuthKey::from_seed(100);
        let new = AuthKey::from_seed(200);
        let mut ring = KeyRing::new(old.clone());
        ring.rotate(new.clone());
        ring.retire();
        let old_sig = hmac_sign(&old, b"stale");
        // Once retired() is called, old-key signatures MUST be rejected.
        assert!(!ring.verify(b"stale", &old_sig));
        // retire is idempotent.
        ring.retire();
        assert!(ring.retired.is_none());
    }

    #[test]
    fn key_ring_double_rotate_discards_oldest() {
        let k1 = AuthKey::from_seed(1);
        let k2 = AuthKey::from_seed(2);
        let k3 = AuthKey::from_seed(3);
        let mut ring = KeyRing::new(k1.clone());
        ring.rotate(k2.clone());
        ring.rotate(k3.clone());
        // After two rotations active=k3 retired=k2; k1 has been dropped and
        // its signatures MUST no longer verify.
        let k1_sig = hmac_sign(&k1, b"too_old");
        assert!(!ring.verify(b"too_old", &k1_sig));
        let k2_sig = hmac_sign(&k2, b"recently_retired");
        assert!(ring.verify(b"recently_retired", &k2_sig));
        let k3_sig = hmac_sign(&k3, b"current");
        assert!(ring.verify(b"current", &k3_sig));
    }

    // Tests for strengthened validation (asupersync-uw3asa fix)
    #[test]
    fn test_strengthened_validation_rejects_weak_keys() {
        // Test 1: Insufficient byte diversity (old threshold was 8, new is 16)
        let low_diversity = [0u8; 32]; // Only 1 distinct byte
        match AuthKey::from_bytes(low_diversity) {
            Err(AuthKeyError::WeakKey {
                reason: WeakKeyReason::InsufficientByteDiversity { distinct, minimum },
            }) => {
                assert_eq!(distinct, 1);
                assert_eq!(minimum, MIN_DISTINCT_BYTES);
            }
            _ => panic!("Expected InsufficientByteDiversity error"),
        }

        // Test 2: Extreme Hamming weight - too low (old min was 8, new is 64)
        let low_hamming = [1u8; 32]; // Only 32 bits set, well below new minimum
        match AuthKey::from_bytes(low_hamming) {
            Err(AuthKeyError::WeakKey {
                reason: WeakKeyReason::ExtremeHammingWeight { weight, .. },
            }) => {
                assert_eq!(weight, 32);
                assert!(weight < MIN_HAMMING_WEIGHT);
            }
            _ => panic!("Expected ExtremeHammingWeight error for low weight"),
        }

        // Test 3: Extreme Hamming weight - too high (old max was 248, new is 192)
        let high_hamming = [0xFFu8; 32]; // All bits set = 256 bits, above new maximum
        match AuthKey::from_bytes(high_hamming) {
            Err(AuthKeyError::WeakKey {
                reason: WeakKeyReason::ExtremeHammingWeight { weight, .. },
            }) => {
                assert_eq!(weight, 256);
                assert!(weight > MAX_HAMMING_WEIGHT);
            }
            _ => panic!("Expected ExtremeHammingWeight error for high weight"),
        }

        // Test 4: Byte concentration attack (new validation)
        let mut concentrated = [0u8; 32];
        concentrated[0..5].fill(42); // Byte 42 appears 5 times, exceeds MAX_BYTE_FREQUENCY=4
        concentrated[5..].fill(1); // Fill rest to ensure diversity
        match AuthKey::from_bytes(concentrated) {
            Err(AuthKeyError::WeakKey {
                reason:
                    WeakKeyReason::ExcessiveByteConcentration {
                        byte_value,
                        frequency,
                        ..
                    },
            }) => {
                assert_eq!(byte_value, 42);
                assert_eq!(frequency, 5);
            }
            _ => panic!("Expected ExcessiveByteConcentration error"),
        }
    }

    #[test]
    fn test_strengthened_key_derivation() {
        let master_key = AuthKey::from_seed(42);

        // Test salted derivation
        let derived1 = master_key.derive_with_salt(b"salt1", b"test-purpose");
        let derived2 = master_key.derive_with_salt(b"salt1", b"test-purpose");
        let derived3 = master_key.derive_with_salt(b"salt1", b"different-purpose");
        let derived4 = master_key.derive_with_salt(b"salt2", b"test-purpose");

        assert_eq!(
            derived1, derived2,
            "Salted derivation should be deterministic"
        );
        assert_ne!(
            derived1, derived3,
            "Different contexts should yield different keys"
        );
        assert_ne!(
            derived1, derived4,
            "Different salts should yield different keys"
        );

        // Verify derived keys pass strengthened validation
        let validation_result = AuthKey::from_bytes(*derived1.as_bytes());
        assert!(
            validation_result.is_ok(),
            "Derived key should pass strengthened validation"
        );
    }

    #[test]
    fn test_legacy_keys_now_rejected() {
        // Keys that would have passed the old weak validation should now be rejected

        // Old validation: MIN_HAMMING_WEIGHT=8, now requires 64
        let mut weak_key = [0u8; 32];
        weak_key[0] = 0xFF; // 8 bits set, would pass old validation
        assert!(
            AuthKey::from_bytes(weak_key).is_err(),
            "Weak key should be rejected"
        );

        // Old validation: MIN_DISTINCT_BYTES=8, now requires 16
        let mut pattern_key = [0u8; 32];
        for i in 0..8 {
            pattern_key[i * 4] = i as u8; // Only 8 distinct bytes
        }
        assert!(
            AuthKey::from_bytes(pattern_key).is_err(),
            "Pattern key should be rejected"
        );
    }

    #[test]
    fn test_legitimate_strong_keys_accepted() {
        // Verify that legitimately strong keys still pass validation
        let strong_key = AuthKey::from_seed(12345);

        // Should pass all new validation checks
        let validation_result = AuthKey::from_bytes(*strong_key.as_bytes());
        assert!(
            validation_result.is_ok(),
            "Strong key should pass validation"
        );

        // Verify HKDF outputs also pass (they should by construction)
        let hkdf_key = AuthKey::from_hkdf(b"input-key-material", Some(b"salt"), b"context");
        let hkdf_revalidation = AuthKey::from_bytes(*hkdf_key.as_bytes());
        assert!(hkdf_revalidation.is_ok(), "HKDF key should pass validation");
    }

    #[test]
    fn test_security_fixes_prevent_weak_key_forgery() {
        // Test that our security fixes prevent weak key creation via alternative paths

        // Test 1: from_seed still works for normal seeds
        let strong_from_seed = AuthKey::from_seed(0xDEADBEEF);
        assert!(
            AuthKey::from_bytes(*strong_from_seed.as_bytes()).is_ok(),
            "Normal seeds should produce strong keys"
        );

        // Test 2: from_rng with deterministic RNG still works
        let mut rng = DetRng::new(12345);
        let strong_from_rng = AuthKey::from_rng(&mut rng);
        assert!(
            AuthKey::from_bytes(*strong_from_rng.as_bytes()).is_ok(),
            "Normal RNG should produce strong keys"
        );

        // Test 3: Edge case seeds get strengthened (test doesn't expose internals but ensures no panic)
        let edge_case_key = AuthKey::from_seed(0);
        assert!(
            AuthKey::from_bytes(*edge_case_key.as_bytes()).is_ok(),
            "Edge case seeds should be strengthened to pass validation"
        );

        // Test 4: Multiple keys from same seed should be deterministic but strong
        let key1 = AuthKey::from_seed(42);
        let key2 = AuthKey::from_seed(42);
        assert_eq!(key1, key2, "Same seed should produce same key");
        assert!(
            AuthKey::from_bytes(*key1.as_bytes()).is_ok(),
            "Deterministic keys should still be strong"
        );
    }

    #[test]
    fn test_key_entropy_validation_completeness() {
        // Comprehensive test of all entropy validation rules

        // Test minimum distinct bytes (16 required)
        let mut low_diversity = [0u8; 32];
        for (i, byte) in low_diversity.iter_mut().enumerate().take(15) {
            *byte = i as u8;
        } // Only 15 distinct values
        assert!(
            AuthKey::from_bytes(low_diversity).is_err(),
            "Insufficient byte diversity should be rejected"
        );

        // Test Hamming weight bounds (64-192 required)
        let low_hamming = [1u8; 32]; // 32 bits = below 64 minimum
        assert!(
            AuthKey::from_bytes(low_hamming).is_err(),
            "Low Hamming weight should be rejected"
        );

        let high_hamming = [0xFFu8; 32]; // 256 bits = above 192 maximum
        assert!(
            AuthKey::from_bytes(high_hamming).is_err(),
            "High Hamming weight should be rejected"
        );

        // Test byte concentration (max 4 occurrences per byte value)
        let mut concentrated = [0u8; 32];
        concentrated[0..5].fill(42); // Byte value 42 appears 5 times
        for (i, byte) in concentrated.iter_mut().enumerate().skip(5) {
            *byte = i as u8;
        } // Fill with unique values
        assert!(
            AuthKey::from_bytes(concentrated).is_err(),
            "Excessive byte concentration should be rejected"
        );
    }
}