codlet-core 0.14.5

Core authentication primitives for codlet: code policy, generation, normalization, keyed lookup derivation, lifecycle state machines, and storage traits.
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
//! Secret hashing, key providers, domain separation, and key versioning
//! (RFC-004).
//!
//! Every persisted secret (code, session, form token) is stored only as a
//! keyed HMAC [`LookupKey`], never in plaintext (INV-1). Lookup keys are
//! domain-separated so the same plaintext used in two roles derives two
//! different keys, and every derivation is tagged with the [`KeyVersion`] of
//! the key that produced it so keys can be rotated without an all-or-nothing
//! migration (RFC-004 §12.2).
//!
//! ## Derivation scheme (prefixing — RFC-004 §9.1 recommendation)
//!
//! ```text
//! message = "codlet/v1/lookup" || 0x00 || domain_label || 0x00 || secret_bytes
//! LookupKey = lowercase_hex( HMAC-SHA256(key_bytes, message) )
//! ```
//!
//! The fixed context string and `0x00` separators make the label and secret
//! unambiguous, so distinct domains cannot collide. This is intentionally
//! **not** the `zinnias-ciao` derivation (`HMAC(pepper, value)` with no domain
//! or prefix); the migration adapter (RFC-014) supplies a legacy mode for
//! existing rows.

use hmac::{Hmac, Mac};
use sha2::Sha256;
use subtle::ConstantTimeEq;

use crate::FORMAT_VERSION;
use crate::error::KeyError;

type HmacSha256 = Hmac<Sha256>;

/// The lookup context label, combined with [`FORMAT_VERSION`] into the HMAC
/// message prefix. Bumping the format version changes every derived key.
const LOOKUP_CONTEXT: &str = "lookup";

/// Identifier of the key version that produced a [`LookupKey`]. Stored beside
/// every lookup key (RFC-004 §12.2). Not secret.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct KeyVersion(String);

impl KeyVersion {
    /// Wrap a version label.
    #[must_use]
    pub fn new(value: impl Into<String>) -> Self {
        Self(value.into())
    }

    /// Borrow the version label.
    #[must_use]
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

impl core::fmt::Display for KeyVersion {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.write_str(&self.0)
    }
}

/// A keyed lookup value: the lowercase-hex HMAC of a domain-separated message.
///
/// Contains no plaintext and is safe to persist, but is still sensitive (it is
/// the database lookup index). Compare lookup keys with
/// [`LookupKey::ct_eq`], not `==`, when the comparison could be timing-attacked.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct LookupKey(String);

impl LookupKey {
    /// Borrow the hex digest.
    #[must_use]
    pub fn as_str(&self) -> &str {
        &self.0
    }

    /// Constant-time equality over the hex bytes (RFC-004; service parity with
    /// `hmac_hex_eq`). Length is allowed to leak: lookup keys are fixed-width.
    #[must_use]
    pub fn ct_eq(&self, other: &LookupKey) -> bool {
        let a = self.0.as_bytes();
        let b = other.0.as_bytes();
        if a.len() != b.len() {
            return false;
        }
        a.ct_eq(b).into()
    }
}

/// The role a secret plays. Part of the HMAC message, so it cross-namespaces
/// lookup keys (RFC-004 §12.1).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum SecretDomain {
    /// One-time code lookup.
    Code,
    /// Session secret lookup.
    Session,
    /// Form-token lookup.
    FormToken,
    /// Pre-auth flow / join-ticket lookup.
    FlowTicket,
}

impl SecretDomain {
    /// The stable wire label embedded in the HMAC message. Changing these
    /// strings is a breaking change to stored lookup keys.
    #[must_use]
    pub const fn label(self) -> &'static str {
        match self {
            SecretDomain::Code => "code",
            SecretDomain::Session => "session",
            SecretDomain::FormToken => "form_token",
            SecretDomain::FlowTicket => "flow_ticket",
        }
    }
}

/// A borrowed HMAC key plus the version that identifies it.
pub struct HmacKeyRef<'a> {
    /// The version label of this key.
    pub version: KeyVersion,
    /// The raw key bytes. Never logged or formatted.
    pub bytes: &'a [u8],
}

impl core::fmt::Debug for HmacKeyRef<'_> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("HmacKeyRef")
            .field("version", &self.version)
            .field("bytes", &"<redacted>")
            .finish()
    }
}

/// Supplies HMAC key material. Synchronous, so key lookup does not couple to a
/// web/runtime async model (RFC-004 §3.3). **No fallback key exists**: missing
/// material is an error (INV-2, SR-29).
pub trait KeyProvider {
    /// The active key used for new derivations.
    ///
    /// # Errors
    /// [`KeyError::MissingActiveKey`] if none is configured.
    fn active_hmac_key(&self) -> Result<HmacKeyRef<'_>, KeyError>;

    /// A specific historical key, for validating records written under an older
    /// version during rotation.
    ///
    /// # Errors
    /// [`KeyError::MissingKeyVersion`] if that version is unknown. Callers must
    /// fail closed for that candidate rather than falling back.
    fn hmac_key_by_version(&self, version: &KeyVersion) -> Result<HmacKeyRef<'_>, KeyError>;
}

/// A key provider holding an active key and zero or more previous keys, in
/// memory. Suitable for production when constructed from real secret material
/// loaded at startup, and for tests/examples.
///
/// There is deliberately no `Default` or empty constructor that would yield a
/// usable-but-keyless provider: you must supply real bytes (INV-2).
#[derive(Clone)]
pub struct StaticKeyProvider {
    active_version: KeyVersion,
    keys: Vec<(KeyVersion, Vec<u8>)>,
}

impl StaticKeyProvider {
    /// Construct from an active version+key and optional previous versions.
    ///
    /// # Errors
    /// [`KeyError::InvalidKeyMaterial`] if the active key is empty.
    pub fn new(
        active_version: impl Into<String>,
        active_key: Vec<u8>,
        previous: Vec<(KeyVersion, Vec<u8>)>,
    ) -> Result<Self, KeyError> {
        if active_key.is_empty() {
            return Err(KeyError::InvalidKeyMaterial);
        }
        let active_version = KeyVersion::new(active_version);
        let mut keys = Vec::with_capacity(previous.len() + 1);
        keys.push((active_version.clone(), active_key));
        keys.extend(previous);
        Ok(Self {
            active_version,
            keys,
        })
    }

    /// Convenience constructor with a single key and no previous versions.
    ///
    /// # Errors
    /// [`KeyError::InvalidKeyMaterial`] if `key` is empty.
    pub fn single(version: impl Into<String>, key: Vec<u8>) -> Result<Self, KeyError> {
        Self::new(version, key, Vec::new())
    }
}

impl core::fmt::Debug for StaticKeyProvider {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("StaticKeyProvider")
            .field("active_version", &self.active_version)
            .field("key_versions", &self.keys.len())
            .field("keys", &"<redacted>")
            .finish()
    }
}

impl KeyProvider for StaticKeyProvider {
    fn active_hmac_key(&self) -> Result<HmacKeyRef<'_>, KeyError> {
        self.keys
            .iter()
            .find(|(v, _)| *v == self.active_version)
            .map(|(v, k)| HmacKeyRef {
                version: v.clone(),
                bytes: k,
            })
            .ok_or(KeyError::MissingActiveKey)
    }

    fn hmac_key_by_version(&self, version: &KeyVersion) -> Result<HmacKeyRef<'_>, KeyError> {
        self.keys
            .iter()
            .find(|(v, _)| v == version)
            .map(|(v, k)| HmacKeyRef {
                version: v.clone(),
                bytes: k,
            })
            .ok_or(KeyError::MissingKeyVersion)
    }
}

/// Derives [`LookupKey`]s from secrets using a [`KeyProvider`].
#[derive(Debug, Clone)]
pub struct SecretHasher<K> {
    key_provider: K,
}

impl<K: KeyProvider> SecretHasher<K> {
    /// Wrap a key provider.
    #[must_use]
    pub fn new(key_provider: K) -> Self {
        Self { key_provider }
    }

    /// Borrow the underlying key provider.
    #[must_use]
    pub fn key_provider(&self) -> &K {
        &self.key_provider
    }

    /// Derive a lookup key for `value` in `domain` using the **active** key.
    /// Returns the key plus the active [`KeyVersion`] to store alongside it.
    ///
    /// # Errors
    /// Propagates [`KeyError`] from the provider (e.g. missing active key).
    pub fn lookup_key(
        &self,
        domain: SecretDomain,
        value: &str,
    ) -> Result<(LookupKey, KeyVersion), KeyError> {
        let key = self.key_provider.active_hmac_key()?;
        let lk = derive(key.bytes, domain, value);
        Ok((lk, key.version))
    }

    /// Derive a lookup key for `value` in `domain` using a specific key
    /// `version`. Used during validation to re-derive candidates for records
    /// written under older keys.
    ///
    /// # Errors
    /// Propagates [`KeyError::MissingKeyVersion`] if the version is unknown.
    pub fn lookup_key_with_version(
        &self,
        domain: SecretDomain,
        value: &str,
        version: &KeyVersion,
    ) -> Result<LookupKey, KeyError> {
        let key = self.key_provider.hmac_key_by_version(version)?;
        Ok(derive(key.bytes, domain, value))
    }
}

/// Pure derivation: `HMAC-SHA256(key, ctx || 0x00 || domain || 0x00 || value)`,
/// returned as lowercase hex. Kept private; the public surface goes through
/// [`SecretHasher`].
fn derive(key_bytes: &[u8], domain: SecretDomain, value: &str) -> LookupKey {
    // HMAC accepts any key length; new_from_slice only errors for impossible
    // key sizes which Hmac<Sha256> does not have, so this cannot fail.
    let mut mac =
        HmacSha256::new_from_slice(key_bytes).expect("HMAC-SHA256 accepts any key length");
    mac.update(FORMAT_VERSION.as_bytes());
    mac.update(b"/");
    mac.update(LOOKUP_CONTEXT.as_bytes());
    mac.update(&[0u8]);
    mac.update(domain.label().as_bytes());
    mac.update(&[0u8]);
    mac.update(value.as_bytes());
    let digest = mac.finalize().into_bytes();
    LookupKey(hex_lower(&digest))
}

/// Lowercase hex encoding without pulling in the `hex` crate, keeping the core
/// dependency set minimal (NFR-3).
fn hex_lower(bytes: &[u8]) -> String {
    const HEX: &[u8; 16] = b"0123456789abcdef";
    let mut s = String::with_capacity(bytes.len() * 2);
    for &b in bytes {
        s.push(HEX[(b >> 4) as usize] as char);
        s.push(HEX[(b & 0x0f) as usize] as char);
    }
    s
}

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

    fn hasher() -> SecretHasher<StaticKeyProvider> {
        let kp = StaticKeyProvider::single("v1", b"super-secret-key-material".to_vec()).unwrap();
        SecretHasher::new(kp)
    }

    #[test]
    fn deterministic_same_inputs_same_key() {
        let h = hasher();
        let (a, va) = h.lookup_key(SecretDomain::Code, "ABCD2345").unwrap();
        let (b, vb) = h.lookup_key(SecretDomain::Code, "ABCD2345").unwrap();
        assert_eq!(a, b);
        assert_eq!(va, vb);
        assert_eq!(va.as_str(), "v1");
        // 32-byte digest → 64 hex chars.
        assert_eq!(a.as_str().len(), 64);
        assert!(a.as_str().bytes().all(|c| c.is_ascii_hexdigit()));
    }

    #[test]
    fn different_value_different_key() {
        let h = hasher();
        let (a, _) = h.lookup_key(SecretDomain::Code, "AAAAAAAA").unwrap();
        let (b, _) = h.lookup_key(SecretDomain::Code, "BBBBBBBB").unwrap();
        assert_ne!(a, b);
    }

    #[test]
    fn domain_separation_distinguishes_same_value() {
        let h = hasher();
        let (code, _) = h.lookup_key(SecretDomain::Code, "SAME").unwrap();
        let (sess, _) = h.lookup_key(SecretDomain::Session, "SAME").unwrap();
        let (form, _) = h.lookup_key(SecretDomain::FormToken, "SAME").unwrap();
        let (flow, _) = h.lookup_key(SecretDomain::FlowTicket, "SAME").unwrap();
        // All four must differ pairwise.
        let all = [&code, &sess, &form, &flow];
        for i in 0..all.len() {
            for j in (i + 1)..all.len() {
                assert_ne!(all[i], all[j], "domains {i},{j} collided");
            }
        }
    }

    #[test]
    fn different_key_different_output() {
        let h1 = SecretHasher::new(StaticKeyProvider::single("v1", b"key-one".to_vec()).unwrap());
        let h2 = SecretHasher::new(StaticKeyProvider::single("v1", b"key-two".to_vec()).unwrap());
        let (a, _) = h1.lookup_key(SecretDomain::Code, "X").unwrap();
        let (b, _) = h2.lookup_key(SecretDomain::Code, "X").unwrap();
        assert_ne!(a, b);
    }

    #[test]
    fn missing_active_key_fails_closed() {
        // A provider whose active version points at no stored key.
        let kp = StaticKeyProvider {
            active_version: KeyVersion::new("missing"),
            keys: vec![(KeyVersion::new("v1"), b"k".to_vec())],
        };
        let h = SecretHasher::new(kp);
        assert_eq!(
            h.lookup_key(SecretDomain::Code, "X").unwrap_err(),
            KeyError::MissingActiveKey
        );
    }

    #[test]
    fn empty_key_rejected_at_construction() {
        assert_eq!(
            StaticKeyProvider::single("v1", Vec::new()).unwrap_err(),
            KeyError::InvalidKeyMaterial
        );
    }

    #[test]
    fn key_version_round_trip_validation() {
        // Derive under v1, rotate active to v2, re-derive the v1 candidate.
        let kp = StaticKeyProvider::new(
            "v2",
            b"key-two".to_vec(),
            vec![(KeyVersion::new("v1"), b"key-one".to_vec())],
        )
        .unwrap();
        let h = SecretHasher::new(kp);
        let (active, av) = h.lookup_key(SecretDomain::Session, "tok").unwrap();
        assert_eq!(av.as_str(), "v2");
        let v1 = KeyVersion::new("v1");
        let prev = h
            .lookup_key_with_version(SecretDomain::Session, "tok", &v1)
            .unwrap();
        // v1 and v2 derivations differ; the active is v2.
        assert_ne!(active, prev);
        // Unknown version fails closed, not fallback.
        let missing = KeyVersion::new("v9");
        assert_eq!(
            h.lookup_key_with_version(SecretDomain::Session, "tok", &missing)
                .unwrap_err(),
            KeyError::MissingKeyVersion
        );
    }

    #[test]
    fn lookup_key_ct_eq_matches_value_eq() {
        let h = hasher();
        let (a, _) = h.lookup_key(SecretDomain::Code, "ABCD2345").unwrap();
        let (b, _) = h.lookup_key(SecretDomain::Code, "ABCD2345").unwrap();
        let (c, _) = h.lookup_key(SecretDomain::Code, "DIFFEREN").unwrap();
        assert!(a.ct_eq(&b));
        assert!(!a.ct_eq(&c));
    }

    #[test]
    fn key_material_redacted_in_debug() {
        let kp = StaticKeyProvider::single("v1", b"secret-bytes".to_vec()).unwrap();
        let dbg = format!("{kp:?}");
        assert!(!dbg.contains("secret-bytes"), "key bytes leaked: {dbg}");
        assert!(dbg.contains("<redacted>"));
        let key = kp.active_hmac_key().unwrap();
        let kdbg = format!("{key:?}");
        assert!(!kdbg.contains("secret-bytes"));
    }
}