Skip to main content

async_snmp/v3/
auth.rs

1//! Authentication key derivation and HMAC operations (RFC 3414).
2//!
3//! This module implements:
4//! - Password-to-key derivation (1MB expansion + hash)
5//! - Key localization (binding key to engine ID)
6//! - HMAC authentication for message integrity
7//!
8//! # Two-Level Key Derivation
9//!
10//! SNMPv3 key derivation is a two-step process:
11//!
12//! 1. **Password to Master Key** (~850μs for SHA-256): Expand password to 1MB
13//!    by repetition and hash it. This produces a protocol-specific master key.
14//!
15//! 2. **Localization** (~1μs): Bind the master key to a specific engine ID by
16//!    computing `H(master_key || engine_id || master_key)`.
17//!
18//! When polling many engines with the same credentials, cache the [`MasterKey`]
19//! and call [`MasterKey::localize`] for each engine ID. This avoids repeating
20//! the expensive 1MB expansion for every engine.
21//!
22//! ```rust
23//! use async_snmp::{AuthProtocol, MasterKey};
24//!
25//! // Expensive: ~850μs - do once per password
26//! let master = MasterKey::from_password(AuthProtocol::Sha256, b"authpassword");
27//!
28//! // Cheap: ~1μs each - do per engine
29//! let key1 = master.localize(b"\x80\x00\x1f\x88\x80...");
30//! let key2 = master.localize(b"\x80\x00\x1f\x88\x81...");
31//! ```
32
33use digest::{Digest, KeyInit, Mac, OutputSizeUser, core_api::BlockSizeUser};
34use zeroize::{Zeroize, ZeroizeOnDrop};
35
36use super::AuthProtocol;
37
38/// Minimum password length recommended by net-snmp.
39///
40/// Net-snmp rejects passwords shorter than 8 characters with `USM_PASSWORDTOOSHORT`.
41/// While this library accepts shorter passwords for flexibility, applications should
42/// enforce this minimum for security.
43pub const MIN_PASSWORD_LENGTH: usize = 8;
44
45/// Master authentication key (Ku) before engine localization.
46///
47/// This is the intermediate result of the RFC 3414 password-to-key algorithm,
48/// computed by expanding the password to 1MB and hashing it. This step is
49/// computationally expensive (~850μs for SHA-256) but can be cached and reused
50/// across multiple engines that share the same credentials.
51///
52/// # Performance
53///
54/// | Operation | Time |
55/// |-----------|------|
56/// | `MasterKey::from_password` (SHA-256) | ~850 μs |
57/// | `MasterKey::localize` | ~1 μs |
58///
59/// For applications polling many engines with shared credentials, caching the
60/// `MasterKey` provides significant performance benefits.
61///
62/// # Security
63///
64/// Key material is automatically zeroed from memory when dropped, using the
65/// `zeroize` crate. This provides defense-in-depth against memory scraping.
66///
67/// # Example
68///
69/// ```rust
70/// use async_snmp::{AuthProtocol, MasterKey};
71///
72/// // Derive master key once (expensive)
73/// let master = MasterKey::from_password(AuthProtocol::Sha256, b"authpassword");
74///
75/// // Localize to different engines (cheap)
76/// let engine1_id = b"\x80\x00\x1f\x88\x80\xe9\xb1\x04\x61\x73\x61\x00\x00\x00";
77/// let engine2_id = b"\x80\x00\x1f\x88\x80\xe9\xb1\x04\x61\x73\x61\x00\x00\x01";
78///
79/// let key1 = master.localize(engine1_id);
80/// let key2 = master.localize(engine2_id);
81/// ```
82#[derive(Clone, Zeroize, ZeroizeOnDrop)]
83pub struct MasterKey {
84    key: Vec<u8>,
85    #[zeroize(skip)]
86    protocol: AuthProtocol,
87}
88
89impl MasterKey {
90    /// Derive a master key from a password.
91    ///
92    /// This implements RFC 3414 Section A.2.1: expand the password to 1MB by
93    /// repetition, then hash the result. This is computationally expensive
94    /// (~850μs for SHA-256) but only needs to be done once per password.
95    ///
96    /// # Empty and Short Passwords
97    ///
98    /// Empty passwords result in an all-zero key. A warning is logged when
99    /// the password is shorter than [`MIN_PASSWORD_LENGTH`] (8 characters).
100    pub fn from_password(protocol: AuthProtocol, password: &[u8]) -> Self {
101        if password.len() < MIN_PASSWORD_LENGTH {
102            tracing::warn!(target: "async_snmp::v3", { password_len = password.len(), min_len = MIN_PASSWORD_LENGTH }, "SNMPv3 password is shorter than recommended minimum; \
103                 net-snmp rejects passwords shorter than 8 characters");
104        }
105        let key = password_to_key(protocol, password);
106        Self { key, protocol }
107    }
108
109    /// Derive a master key from a string password.
110    pub fn from_str_password(protocol: AuthProtocol, password: &str) -> Self {
111        Self::from_password(protocol, password.as_bytes())
112    }
113
114    /// Create a master key from raw bytes.
115    ///
116    /// Use this if you already have a master key (e.g., from configuration).
117    /// The bytes should be the raw digest output from the 1MB password expansion.
118    pub fn from_bytes(protocol: AuthProtocol, key: impl Into<Vec<u8>>) -> Self {
119        Self {
120            key: key.into(),
121            protocol,
122        }
123    }
124
125    /// Localize this master key to a specific engine ID.
126    ///
127    /// This implements RFC 3414 Section A.2.2:
128    /// `localized_key = H(master_key || engine_id || master_key)`
129    ///
130    /// This operation is cheap (~1μs) compared to master key derivation.
131    pub fn localize(&self, engine_id: &[u8]) -> LocalizedKey {
132        let localized = localize_key(self.protocol, &self.key, engine_id);
133        LocalizedKey {
134            key: localized,
135            protocol: self.protocol,
136        }
137    }
138
139    /// Get the protocol this key is for.
140    pub fn protocol(&self) -> AuthProtocol {
141        self.protocol
142    }
143
144    /// Get the raw key bytes.
145    pub fn as_bytes(&self) -> &[u8] {
146        &self.key
147    }
148}
149
150impl std::fmt::Debug for MasterKey {
151    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
152        f.debug_struct("MasterKey")
153            .field("protocol", &self.protocol)
154            .field("key", &"[REDACTED]")
155            .finish()
156    }
157}
158
159/// Localized authentication key.
160///
161/// A key that has been derived from a password and bound to a specific engine ID.
162/// This key can be used for HMAC operations on messages to/from that engine.
163///
164/// # Security
165///
166/// Key material is automatically zeroed from memory when the key is dropped,
167/// using the `zeroize` crate. This provides defense-in-depth against memory
168/// scraping attacks.
169#[derive(Clone, Zeroize, ZeroizeOnDrop)]
170pub struct LocalizedKey {
171    key: Vec<u8>,
172    #[zeroize(skip)]
173    protocol: AuthProtocol,
174}
175
176impl LocalizedKey {
177    /// Derive a localized key from a password and engine ID.
178    ///
179    /// This implements the key localization algorithm from RFC 3414 Section A.2:
180    /// 1. Expand password to 1MB by repetition
181    /// 2. Hash the expansion to get the master key
182    /// 3. Hash (master_key || engine_id || master_key) to get the localized key
183    ///
184    /// # Performance Note
185    ///
186    /// This method performs the full key derivation (~850μs for SHA-256). When
187    /// polling many engines with shared credentials, use [`MasterKey`] to cache
188    /// the intermediate result and call [`MasterKey::localize`] for each engine.
189    ///
190    /// # Empty and Short Passwords
191    ///
192    /// Empty passwords result in an all-zero key of the appropriate length for
193    /// the authentication protocol. This differs from net-snmp, which rejects
194    /// passwords shorter than 8 characters with `USM_PASSWORDTOOSHORT`.
195    ///
196    /// While empty/short passwords are accepted for flexibility, they provide
197    /// minimal security. A warning is logged at the `WARN` level when the
198    /// password is shorter than [`MIN_PASSWORD_LENGTH`] (8 characters).
199    pub fn from_password(protocol: AuthProtocol, password: &[u8], engine_id: &[u8]) -> Self {
200        MasterKey::from_password(protocol, password).localize(engine_id)
201    }
202
203    /// Derive a localized key from a string password and engine ID.
204    ///
205    /// This is a convenience method that converts the string to bytes and calls
206    /// [`from_password`](Self::from_password).
207    pub fn from_str_password(protocol: AuthProtocol, password: &str, engine_id: &[u8]) -> Self {
208        Self::from_password(protocol, password.as_bytes(), engine_id)
209    }
210
211    /// Create a localized key from a master key and engine ID.
212    ///
213    /// This is the efficient path when you have a cached [`MasterKey`].
214    /// Equivalent to calling [`MasterKey::localize`].
215    pub fn from_master_key(master: &MasterKey, engine_id: &[u8]) -> Self {
216        master.localize(engine_id)
217    }
218
219    /// Create a localized key from raw bytes.
220    ///
221    /// Use this if you already have a localized key (e.g., from configuration).
222    pub fn from_bytes(protocol: AuthProtocol, key: impl Into<Vec<u8>>) -> Self {
223        Self {
224            key: key.into(),
225            protocol,
226        }
227    }
228
229    /// Get the protocol this key is for.
230    pub fn protocol(&self) -> AuthProtocol {
231        self.protocol
232    }
233
234    /// Get the raw key bytes.
235    pub fn as_bytes(&self) -> &[u8] {
236        &self.key
237    }
238
239    /// Get the MAC length for this key's protocol.
240    pub fn mac_len(&self) -> usize {
241        self.protocol.mac_len()
242    }
243
244    /// Compute HMAC over a message and return the truncated MAC.
245    ///
246    /// The returned MAC is truncated to the appropriate length for the protocol
247    /// (12 bytes for MD5/SHA-1, variable for SHA-2).
248    pub fn compute_hmac(&self, data: &[u8]) -> Vec<u8> {
249        compute_hmac(self.protocol, &self.key, data)
250    }
251
252    /// Verify an HMAC.
253    ///
254    /// Returns `true` if the MAC matches, `false` otherwise.
255    pub fn verify_hmac(&self, data: &[u8], expected: &[u8]) -> bool {
256        let computed = self.compute_hmac(data);
257        // Constant-time comparison
258        if computed.len() != expected.len() {
259            return false;
260        }
261        let mut result = 0u8;
262        for (a, b) in computed.iter().zip(expected.iter()) {
263            result |= a ^ b;
264        }
265        result == 0
266    }
267}
268
269impl std::fmt::Debug for LocalizedKey {
270    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
271        f.debug_struct("LocalizedKey")
272            .field("protocol", &self.protocol)
273            .field("key", &"[REDACTED]")
274            .finish()
275    }
276}
277
278/// Dispatch a generic function over the hash type for a given `AuthProtocol`.
279///
280/// Usage: `dispatch_auth!(protocol, fn_name, arg1, arg2, ...)`
281/// Expands to `fn_name::<HashType>(arg1, arg2, ...)` for the matching hash.
282macro_rules! dispatch_auth {
283    ($protocol:expr, $fn:ident, $($arg:expr),*) => {
284        match $protocol {
285            AuthProtocol::Md5 => $fn::<md5::Md5>($($arg),*),
286            AuthProtocol::Sha1 => $fn::<sha1::Sha1>($($arg),*),
287            AuthProtocol::Sha224 => $fn::<sha2::Sha224>($($arg),*),
288            AuthProtocol::Sha256 => $fn::<sha2::Sha256>($($arg),*),
289            AuthProtocol::Sha384 => $fn::<sha2::Sha384>($($arg),*),
290            AuthProtocol::Sha512 => $fn::<sha2::Sha512>($($arg),*),
291        }
292    };
293}
294
295/// Password to key transformation (RFC 3414 Section A.2.1).
296///
297/// Creates a 1MB string by repeating the password, then hashes it.
298fn password_to_key(protocol: AuthProtocol, password: &[u8]) -> Vec<u8> {
299    const EXPANSION_SIZE: usize = 1_048_576; // 1MB
300    dispatch_auth!(protocol, password_to_key_impl, password, EXPANSION_SIZE)
301}
302
303fn password_to_key_impl<D>(password: &[u8], expansion_size: usize) -> Vec<u8>
304where
305    D: Digest + Default,
306{
307    if password.is_empty() {
308        // Empty password results in all-zero key
309        return vec![0u8; <D as OutputSizeUser>::output_size()];
310    }
311
312    let mut hasher = D::new();
313
314    // RFC 3414 A.2.1: Form a 1MB string by repeating the password
315    // and hash it in 64-byte chunks (matching net-snmp's approach)
316    let mut buf = [0u8; 64];
317    let password_len = password.len();
318    let mut password_index = 0;
319    let mut count = 0;
320
321    while count < expansion_size {
322        // Fill buffer with password bytes
323        for byte in &mut buf {
324            *byte = password[password_index];
325            password_index = (password_index + 1) % password_len;
326        }
327        hasher.update(buf);
328        count += 64;
329    }
330
331    hasher.finalize().to_vec()
332}
333
334/// Key localization (RFC 3414 Section A.2.2).
335///
336/// Binds a master key to a specific engine ID:
337/// localized_key = H(master_key || engine_id || master_key)
338fn localize_key(protocol: AuthProtocol, master_key: &[u8], engine_id: &[u8]) -> Vec<u8> {
339    dispatch_auth!(protocol, localize_key_impl, master_key, engine_id)
340}
341
342fn localize_key_impl<D>(master_key: &[u8], engine_id: &[u8]) -> Vec<u8>
343where
344    D: Digest + Default,
345{
346    let mut hasher = D::new();
347    hasher.update(master_key);
348    hasher.update(engine_id);
349    hasher.update(master_key);
350    hasher.finalize().to_vec()
351}
352
353/// Compute HMAC with the appropriate algorithm.
354fn compute_hmac(protocol: AuthProtocol, key: &[u8], data: &[u8]) -> Vec<u8> {
355    let truncate_len = protocol.mac_len();
356    dispatch_auth!(protocol, compute_hmac_impl, key, data, truncate_len)
357}
358
359/// Generic HMAC computation with truncation.
360fn compute_hmac_impl<D>(key: &[u8], data: &[u8], truncate_len: usize) -> Vec<u8>
361where
362    D: Digest + BlockSizeUser + Clone,
363{
364    use hmac::SimpleHmac;
365
366    let mut mac =
367        <SimpleHmac<D> as KeyInit>::new_from_slice(key).expect("HMAC can take key of any size");
368    Mac::update(&mut mac, data);
369    let result = mac.finalize().into_bytes();
370    result[..truncate_len].to_vec()
371}
372
373/// Authenticate an outgoing message by computing and inserting the HMAC.
374///
375/// The message must already have placeholder zeros in the auth params field.
376/// This function computes the HMAC over the entire message (with zeros in place)
377/// and returns the message with the actual HMAC inserted.
378pub fn authenticate_message(
379    key: &LocalizedKey,
380    message: &mut [u8],
381    auth_offset: usize,
382    auth_len: usize,
383) {
384    let end = match auth_offset.checked_add(auth_len) {
385        Some(e) if e <= message.len() => e,
386        _ => return,
387    };
388
389    // Compute HMAC over the message with zeros in auth params position
390    let mac = key.compute_hmac(message);
391
392    // Replace zeros with actual MAC
393    message[auth_offset..end].copy_from_slice(&mac);
394}
395
396/// Verify the authentication of an incoming message.
397///
398/// Returns `true` if the MAC is valid, `false` otherwise.
399pub fn verify_message(
400    key: &LocalizedKey,
401    message: &[u8],
402    auth_offset: usize,
403    auth_len: usize,
404) -> bool {
405    let end = match auth_offset.checked_add(auth_len) {
406        Some(e) if e <= message.len() => e,
407        _ => return false,
408    };
409
410    // Extract the received MAC
411    let received_mac = &message[auth_offset..end];
412
413    // Create a copy with zeros in the auth position
414    let mut msg_copy = message.to_vec();
415    msg_copy[auth_offset..end].fill(0);
416
417    // Compute expected MAC
418    key.verify_hmac(&msg_copy, received_mac)
419}
420
421/// Pre-computed master keys for SNMPv3 authentication and privacy.
422///
423/// This struct caches the expensive password-to-key derivation results for
424/// both authentication and privacy passwords. When polling many engines with
425/// shared credentials, create a `MasterKeys` once and use it with
426/// [`UsmBuilder`](crate::UsmBuilder) to avoid repeating the ~850μs key derivation for each engine.
427///
428/// # Example
429///
430/// ```rust
431/// use async_snmp::{AuthProtocol, PrivProtocol, MasterKeys};
432///
433/// // Create master keys once (expensive)
434/// let master_keys = MasterKeys::new(AuthProtocol::Sha256, b"authpassword")
435///     .with_privacy(PrivProtocol::Aes128, b"privpassword");
436///
437/// // Use with multiple clients - localization is cheap (~1μs per engine)
438/// ```
439#[derive(Clone, Zeroize, ZeroizeOnDrop)]
440pub struct MasterKeys {
441    /// Master key for authentication (and base for privacy key derivation)
442    auth_master: MasterKey,
443    /// Optional separate master key for privacy password
444    /// If None, the auth_master is used for privacy (common case: same password)
445    #[zeroize(skip)]
446    priv_protocol: Option<super::PrivProtocol>,
447    priv_master: Option<MasterKey>,
448}
449
450impl MasterKeys {
451    /// Create master keys with just authentication.
452    ///
453    /// # Example
454    ///
455    /// ```rust
456    /// use async_snmp::{AuthProtocol, MasterKeys};
457    ///
458    /// let keys = MasterKeys::new(AuthProtocol::Sha256, b"authpassword");
459    /// ```
460    pub fn new(auth_protocol: AuthProtocol, auth_password: &[u8]) -> Self {
461        Self {
462            auth_master: MasterKey::from_password(auth_protocol, auth_password),
463            priv_protocol: None,
464            priv_master: None,
465        }
466    }
467
468    /// Add privacy with the same password as authentication.
469    ///
470    /// This is the common case where auth and priv passwords are identical.
471    /// The same master key is reused, avoiding duplicate derivation.
472    pub fn with_privacy_same_password(mut self, priv_protocol: super::PrivProtocol) -> Self {
473        self.priv_protocol = Some(priv_protocol);
474        // priv_master stays None - we'll use auth_master for priv key derivation
475        self
476    }
477
478    /// Add privacy with a different password than authentication.
479    ///
480    /// Use this when auth and priv passwords differ. A separate master key
481    /// derivation is performed for the privacy password.
482    pub fn with_privacy(
483        mut self,
484        priv_protocol: super::PrivProtocol,
485        priv_password: &[u8],
486    ) -> Self {
487        self.priv_protocol = Some(priv_protocol);
488        // Use the auth protocol for priv key derivation (per RFC 3826 Section 1.2)
489        self.priv_master = Some(MasterKey::from_password(
490            self.auth_master.protocol(),
491            priv_password,
492        ));
493        self
494    }
495
496    /// Get the authentication master key.
497    pub fn auth_master(&self) -> &MasterKey {
498        &self.auth_master
499    }
500
501    /// Get the privacy master key, if configured.
502    ///
503    /// Returns the separate priv master key if set, otherwise returns the
504    /// auth master key (for same-password case).
505    pub fn priv_master(&self) -> Option<&MasterKey> {
506        if self.priv_protocol.is_some() {
507            Some(self.priv_master.as_ref().unwrap_or(&self.auth_master))
508        } else {
509            None
510        }
511    }
512
513    /// Get the configured privacy protocol.
514    pub fn priv_protocol(&self) -> Option<super::PrivProtocol> {
515        self.priv_protocol
516    }
517
518    /// Get the authentication protocol.
519    pub fn auth_protocol(&self) -> AuthProtocol {
520        self.auth_master.protocol()
521    }
522
523    /// Derive localized keys for a specific engine ID.
524    ///
525    /// Returns (auth_key, priv_key) where priv_key is None if no privacy
526    /// was configured.
527    ///
528    /// Key extension is automatically applied when needed based on the auth/priv
529    /// protocol combination:
530    ///
531    /// - AES-192/256 with SHA-1 or MD5: Blumenthal extension (draft-blumenthal-aes-usm-04)
532    /// - 3DES with SHA-1 or MD5: Reeder extension (draft-reeder-snmpv3-usm-3desede-00)
533    ///
534    /// # Example
535    ///
536    /// ```rust
537    /// use async_snmp::{AuthProtocol, MasterKeys, PrivProtocol};
538    ///
539    /// let keys = MasterKeys::new(AuthProtocol::Sha1, b"authpassword")
540    ///     .with_privacy_same_password(PrivProtocol::Aes256);
541    ///
542    /// let engine_id = [0x80, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04];
543    ///
544    /// // SHA-1 only produces 20 bytes, but AES-256 needs 32.
545    /// // Blumenthal extension is automatically applied.
546    /// let (auth, priv_key) = keys.localize(&engine_id);
547    /// ```
548    pub fn localize(&self, engine_id: &[u8]) -> (LocalizedKey, Option<crate::v3::PrivKey>) {
549        let auth_key = self.auth_master.localize(engine_id);
550
551        let priv_key = self.priv_protocol.map(|priv_protocol| {
552            let master = self.priv_master.as_ref().unwrap_or(&self.auth_master);
553            crate::v3::PrivKey::from_master_key(master, priv_protocol, engine_id)
554        });
555
556        (auth_key, priv_key)
557    }
558}
559
560impl std::fmt::Debug for MasterKeys {
561    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
562        f.debug_struct("MasterKeys")
563            .field("auth_protocol", &self.auth_master.protocol())
564            .field("priv_protocol", &self.priv_protocol)
565            .field("has_separate_priv_password", &self.priv_master.is_some())
566            .finish()
567    }
568}
569
570/// Extend a localized key to the required length using the Blumenthal algorithm.
571///
572/// This implements the key extension algorithm from draft-blumenthal-aes-usm-04
573/// Section 3.1.2.1, which allows AES-192/256 to be used with authentication
574/// protocols that produce shorter digests (e.g., SHA-1 with AES-256).
575///
576/// The algorithm iteratively appends hash digests:
577/// ```text
578/// Kul' = Kul || H(Kul) || H(Kul || H(Kul)) || ...
579/// ```
580///
581/// Where H() is the hash function of the authentication protocol.
582pub(crate) fn extend_key(protocol: AuthProtocol, key: &[u8], target_len: usize) -> Vec<u8> {
583    // If we already have enough bytes, just truncate
584    if key.len() >= target_len {
585        return key[..target_len].to_vec();
586    }
587
588    match protocol {
589        AuthProtocol::Md5 => extend_key_impl::<md5::Md5>(key, target_len),
590        AuthProtocol::Sha1 => extend_key_impl::<sha1::Sha1>(key, target_len),
591        AuthProtocol::Sha224 => extend_key_impl::<sha2::Sha224>(key, target_len),
592        AuthProtocol::Sha256 => extend_key_impl::<sha2::Sha256>(key, target_len),
593        AuthProtocol::Sha384 => extend_key_impl::<sha2::Sha384>(key, target_len),
594        AuthProtocol::Sha512 => extend_key_impl::<sha2::Sha512>(key, target_len),
595    }
596}
597
598/// Generic implementation of Blumenthal key extension.
599///
600/// Algorithm: Kul' = Kul || H(Kul) || H(Kul || H(Kul)) || ...
601fn extend_key_impl<D>(key: &[u8], target_len: usize) -> Vec<u8>
602where
603    D: Digest + Default,
604{
605    let mut result = key.to_vec();
606
607    // Keep appending H(result) until we have enough bytes
608    while result.len() < target_len {
609        let mut hasher = D::new();
610        hasher.update(&result);
611        let hash = hasher.finalize();
612        result.extend_from_slice(&hash);
613    }
614
615    // Truncate to exact length
616    result.truncate(target_len);
617    result
618}
619
620/// Extend a localized key using the Reeder key extension algorithm.
621///
622/// This implements the key extension algorithm from draft-reeder-snmpv3-usm-3desede-00
623/// Section 2.1. Unlike Blumenthal, this algorithm re-runs the full password-to-key (P2K)
624/// algorithm using the current localized key as the "passphrase":
625///
626/// ```text
627/// K1 = P2K(passphrase, engine_id)   // Original localized key (input)
628/// K2 = P2K(K1, engine_id)           // Run full P2K with K1 as passphrase
629/// localized_key = K1 || K2
630/// K3 = P2K(K2, engine_id)           // If more bytes needed
631/// localized_key = K1 || K2 || K3
632/// ... and so on
633/// ```
634///
635/// # Performance Warning
636///
637/// This is approximately 1000x slower than [`extend_key`] (Blumenthal) because each
638/// iteration requires the full 1MB password expansion.
639pub(crate) fn extend_key_reeder(
640    protocol: AuthProtocol,
641    key: &[u8],
642    engine_id: &[u8],
643    target_len: usize,
644) -> Vec<u8> {
645    // If we already have enough bytes, just truncate
646    if key.len() >= target_len {
647        return key[..target_len].to_vec();
648    }
649
650    let mut result = key.to_vec();
651    let mut current_kul = key.to_vec();
652
653    // Keep extending until we have enough bytes
654    while result.len() < target_len {
655        // Run full password-to-key using current Kul as the "passphrase"
656        // This is the expensive 1MB expansion step
657        let ku = password_to_key(protocol, &current_kul);
658
659        // Localize the new Ku to get Kul
660        let new_kul = localize_key(protocol, &ku, engine_id);
661
662        // Append as many bytes as we need (or all of them)
663        let bytes_needed = target_len - result.len();
664        let bytes_to_copy = bytes_needed.min(new_kul.len());
665        result.extend_from_slice(&new_kul[..bytes_to_copy]);
666
667        // The next iteration uses the new Kul as input
668        current_kul = new_kul;
669    }
670
671    result
672}
673
674#[cfg(test)]
675mod tests {
676    use super::*;
677    use crate::format::hex::{decode as decode_hex, encode as encode_hex};
678
679    #[test]
680    fn test_password_to_key_md5() {
681        // Test vector from RFC 3414 Appendix A.3.1
682        // Password: "maplesyrup"
683        // Expected Ku (hex): 9faf 3283 884e 9283 4ebc 9847 d8ed d963
684        let password = b"maplesyrup";
685        let key = password_to_key(AuthProtocol::Md5, password);
686
687        assert_eq!(key.len(), 16);
688        assert_eq!(encode_hex(&key), "9faf3283884e92834ebc9847d8edd963");
689    }
690
691    #[test]
692    fn test_password_to_key_sha1() {
693        // Test vector from RFC 3414 Appendix A.3.2
694        // Password: "maplesyrup"
695        // Expected Ku (hex): 9fb5 cc03 8149 7b37 9352 8939 ff78 8d5d 7914 5211
696        let password = b"maplesyrup";
697        let key = password_to_key(AuthProtocol::Sha1, password);
698
699        assert_eq!(key.len(), 20);
700        assert_eq!(encode_hex(&key), "9fb5cc0381497b3793528939ff788d5d79145211");
701    }
702
703    #[test]
704    fn test_localize_key_md5() {
705        // Test vector from RFC 3414 Appendix A.3.1
706        // Master key from "maplesyrup"
707        // Engine ID: 00 00 00 00 00 00 00 00 00 00 00 02
708        // Expected Kul (hex): 526f 5eed 9fcc e26f 8964 c293 0787 d82b
709        let password = b"maplesyrup";
710        let engine_id = decode_hex("000000000000000000000002").unwrap();
711
712        let key = LocalizedKey::from_password(AuthProtocol::Md5, password, &engine_id);
713
714        assert_eq!(key.as_bytes().len(), 16);
715        assert_eq!(
716            encode_hex(key.as_bytes()),
717            "526f5eed9fcce26f8964c2930787d82b"
718        );
719    }
720
721    #[test]
722    fn test_localize_key_sha1() {
723        // Test vector from RFC 3414 Appendix A.3.2
724        // Engine ID: 00 00 00 00 00 00 00 00 00 00 00 02
725        // Expected Kul (hex): 6695 febc 9288 e362 8223 5fc7 151f 1284 97b3 8f3f
726        let password = b"maplesyrup";
727        let engine_id = decode_hex("000000000000000000000002").unwrap();
728
729        let key = LocalizedKey::from_password(AuthProtocol::Sha1, password, &engine_id);
730
731        assert_eq!(key.as_bytes().len(), 20);
732        assert_eq!(
733            encode_hex(key.as_bytes()),
734            "6695febc9288e36282235fc7151f128497b38f3f"
735        );
736    }
737
738    #[test]
739    fn test_hmac_computation() {
740        let key = LocalizedKey::from_bytes(
741            AuthProtocol::Md5,
742            vec![
743                0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
744                0x0f, 0x10,
745            ],
746        );
747
748        let data = b"test message";
749        let mac = key.compute_hmac(data);
750
751        // HMAC-MD5-96: 12 bytes
752        assert_eq!(mac.len(), 12);
753
754        // Verify returns true for correct MAC
755        assert!(key.verify_hmac(data, &mac));
756
757        // Verify returns false for wrong MAC
758        let mut wrong_mac = mac.clone();
759        wrong_mac[0] ^= 0xFF;
760        assert!(!key.verify_hmac(data, &wrong_mac));
761    }
762
763    #[test]
764    fn test_empty_password() {
765        let key = password_to_key(AuthProtocol::Md5, b"");
766        assert_eq!(key.len(), 16);
767        assert!(key.iter().all(|&b| b == 0));
768    }
769
770    #[test]
771    fn test_from_str_password() {
772        // Verify from_str_password produces same result as from_password with bytes
773        let engine_id = decode_hex("000000000000000000000002").unwrap();
774
775        let key_from_bytes =
776            LocalizedKey::from_password(AuthProtocol::Sha1, b"maplesyrup", &engine_id);
777        let key_from_str =
778            LocalizedKey::from_str_password(AuthProtocol::Sha1, "maplesyrup", &engine_id);
779
780        assert_eq!(key_from_bytes.as_bytes(), key_from_str.as_bytes());
781        assert_eq!(key_from_bytes.protocol(), key_from_str.protocol());
782    }
783
784    #[test]
785    fn test_master_key_localize_md5() {
786        // Verify MasterKey produces same result as LocalizedKey::from_password
787        let password = b"maplesyrup";
788        let engine_id = decode_hex("000000000000000000000002").unwrap();
789
790        let master = MasterKey::from_password(AuthProtocol::Md5, password);
791        let localized_via_master = master.localize(&engine_id);
792        let localized_direct = LocalizedKey::from_password(AuthProtocol::Md5, password, &engine_id);
793
794        assert_eq!(localized_via_master.as_bytes(), localized_direct.as_bytes());
795        assert_eq!(localized_via_master.protocol(), localized_direct.protocol());
796
797        // Verify the master key itself matches RFC 3414 test vector
798        assert_eq!(
799            encode_hex(master.as_bytes()),
800            "9faf3283884e92834ebc9847d8edd963"
801        );
802    }
803
804    #[test]
805    fn test_master_key_localize_sha1() {
806        let password = b"maplesyrup";
807        let engine_id = decode_hex("000000000000000000000002").unwrap();
808
809        let master = MasterKey::from_password(AuthProtocol::Sha1, password);
810        let localized_via_master = master.localize(&engine_id);
811        let localized_direct =
812            LocalizedKey::from_password(AuthProtocol::Sha1, password, &engine_id);
813
814        assert_eq!(localized_via_master.as_bytes(), localized_direct.as_bytes());
815
816        // Verify the master key itself matches RFC 3414 test vector
817        assert_eq!(
818            encode_hex(master.as_bytes()),
819            "9fb5cc0381497b3793528939ff788d5d79145211"
820        );
821    }
822
823    #[test]
824    fn test_master_key_reuse_for_multiple_engines() {
825        // Demonstrate that a single MasterKey can localize to multiple engines
826        let password = b"maplesyrup";
827        let engine_id_1 = decode_hex("000000000000000000000001").unwrap();
828        let engine_id_2 = decode_hex("000000000000000000000002").unwrap();
829
830        let master = MasterKey::from_password(AuthProtocol::Sha256, password);
831
832        let key1 = master.localize(&engine_id_1);
833        let key2 = master.localize(&engine_id_2);
834
835        // Keys should be different for different engines
836        assert_ne!(key1.as_bytes(), key2.as_bytes());
837
838        // Each key should match what from_password produces
839        let direct1 = LocalizedKey::from_password(AuthProtocol::Sha256, password, &engine_id_1);
840        let direct2 = LocalizedKey::from_password(AuthProtocol::Sha256, password, &engine_id_2);
841
842        assert_eq!(key1.as_bytes(), direct1.as_bytes());
843        assert_eq!(key2.as_bytes(), direct2.as_bytes());
844    }
845
846    #[test]
847    fn test_from_master_key() {
848        let password = b"maplesyrup";
849        let engine_id = decode_hex("000000000000000000000002").unwrap();
850
851        let master = MasterKey::from_password(AuthProtocol::Sha256, password);
852        let key_via_localize = master.localize(&engine_id);
853        let key_via_from_master = LocalizedKey::from_master_key(&master, &engine_id);
854
855        assert_eq!(key_via_localize.as_bytes(), key_via_from_master.as_bytes());
856    }
857
858    #[test]
859    fn test_master_keys_auth_only() {
860        let engine_id = decode_hex("000000000000000000000002").unwrap();
861        let master_keys = MasterKeys::new(AuthProtocol::Sha256, b"authpassword");
862
863        assert_eq!(master_keys.auth_protocol(), AuthProtocol::Sha256);
864        assert!(master_keys.priv_protocol().is_none());
865        assert!(master_keys.priv_master().is_none());
866
867        let (auth_key, priv_key) = master_keys.localize(&engine_id);
868        assert!(priv_key.is_none());
869        assert_eq!(auth_key.protocol(), AuthProtocol::Sha256);
870    }
871
872    #[test]
873    fn test_master_keys_with_privacy_same_password() {
874        use crate::v3::PrivProtocol;
875
876        let engine_id = decode_hex("000000000000000000000002").unwrap();
877        let master_keys = MasterKeys::new(AuthProtocol::Sha256, b"sharedpassword")
878            .with_privacy_same_password(PrivProtocol::Aes128);
879
880        assert_eq!(master_keys.auth_protocol(), AuthProtocol::Sha256);
881        assert_eq!(master_keys.priv_protocol(), Some(PrivProtocol::Aes128));
882
883        let (auth_key, priv_key) = master_keys.localize(&engine_id);
884        assert!(priv_key.is_some());
885        assert_eq!(auth_key.protocol(), AuthProtocol::Sha256);
886    }
887
888    #[test]
889    fn test_master_keys_with_privacy_different_password() {
890        use crate::v3::PrivProtocol;
891
892        let engine_id = decode_hex("000000000000000000000002").unwrap();
893        let master_keys = MasterKeys::new(AuthProtocol::Sha256, b"authpassword")
894            .with_privacy(PrivProtocol::Aes128, b"privpassword");
895
896        let (_auth_key, priv_key) = master_keys.localize(&engine_id);
897        assert!(priv_key.is_some());
898
899        // Verify that different passwords produce different keys
900        let same_password_keys = MasterKeys::new(AuthProtocol::Sha256, b"authpassword")
901            .with_privacy_same_password(PrivProtocol::Aes128);
902        let (_, priv_key_same) = same_password_keys.localize(&engine_id);
903
904        // The priv keys should differ when using different passwords
905        // (auth keys are the same since they use same auth password)
906        assert_ne!(
907            priv_key.as_ref().unwrap().encryption_key(),
908            priv_key_same.as_ref().unwrap().encryption_key()
909        );
910    }
911
912    // Known-Answer Tests (KAT) for Reeder key extension algorithm
913    // Test vectors from draft-reeder-snmpv3-usm-3desede-00 Appendix B
914
915    #[test]
916    fn test_reeder_extend_key_md5_kat() {
917        // Test vector from draft-reeder Appendix B.1
918        // Password: "maplesyrup"
919        // Engine ID: 00 00 00 00 00 00 00 00 00 00 00 02
920        // Expected 32-byte localized key:
921        //   52 6f 5e ed 9f cc e2 6f 89 64 c2 93 07 87 d8 2b   (first 16 bytes = K1)
922        //   79 ef f4 4a 90 65 0e e0 a3 a4 0a bf ac 5a cc 12   (next 16 bytes = K2)
923        let password = b"maplesyrup";
924        let engine_id = decode_hex("000000000000000000000002").unwrap();
925
926        // Get the standard localized key (K1)
927        let k1 = LocalizedKey::from_password(AuthProtocol::Md5, password, &engine_id);
928        assert_eq!(
929            encode_hex(k1.as_bytes()),
930            "526f5eed9fcce26f8964c2930787d82b"
931        );
932
933        // Extend using Reeder algorithm to 32 bytes
934        let extended = extend_key_reeder(AuthProtocol::Md5, k1.as_bytes(), &engine_id, 32);
935        assert_eq!(extended.len(), 32);
936        assert_eq!(
937            encode_hex(&extended),
938            "526f5eed9fcce26f8964c2930787d82b79eff44a90650ee0a3a40abfac5acc12"
939        );
940    }
941
942    #[test]
943    fn test_reeder_extend_key_sha1_kat() {
944        // Test vector from draft-reeder Appendix B.2
945        // Password: "maplesyrup"
946        // Engine ID: 00 00 00 00 00 00 00 00 00 00 00 02
947        // Expected 40-byte localized key:
948        //   66 95 fe bc 92 88 e3 62 82 23 5f c7 15 1f 12 84 97 b3 8f 3f  (first 20 bytes = K1)
949        //   9b 8b 6d 78 93 6b a6 e7 d1 9d fd 9c d2 d5 06 55 47 74 3f b5  (next 20 bytes = K2)
950        let password = b"maplesyrup";
951        let engine_id = decode_hex("000000000000000000000002").unwrap();
952
953        // Get the standard localized key (K1)
954        let k1 = LocalizedKey::from_password(AuthProtocol::Sha1, password, &engine_id);
955        assert_eq!(
956            encode_hex(k1.as_bytes()),
957            "6695febc9288e36282235fc7151f128497b38f3f"
958        );
959
960        // Extend using Reeder algorithm to 40 bytes
961        let extended = extend_key_reeder(AuthProtocol::Sha1, k1.as_bytes(), &engine_id, 40);
962        assert_eq!(extended.len(), 40);
963        assert_eq!(
964            encode_hex(&extended),
965            "6695febc9288e36282235fc7151f128497b38f3f9b8b6d78936ba6e7d19dfd9cd2d5065547743fb5"
966        );
967    }
968
969    #[test]
970    fn test_reeder_extend_key_sha1_to_32_bytes() {
971        // Extending SHA-1 key to 32 bytes (for AES-256)
972        // Should be the first 32 bytes of the 40-byte result
973        let password = b"maplesyrup";
974        let engine_id = decode_hex("000000000000000000000002").unwrap();
975
976        let k1 = LocalizedKey::from_password(AuthProtocol::Sha1, password, &engine_id);
977        let extended = extend_key_reeder(AuthProtocol::Sha1, k1.as_bytes(), &engine_id, 32);
978
979        assert_eq!(extended.len(), 32);
980        // First 20 bytes = K1, next 12 bytes = first 12 bytes of K2
981        assert_eq!(
982            encode_hex(&extended),
983            "6695febc9288e36282235fc7151f128497b38f3f9b8b6d78936ba6e7d19dfd9c"
984        );
985    }
986
987    #[test]
988    fn test_reeder_extend_key_truncation() {
989        // When key is already long enough, should truncate
990        let long_key = vec![0xAAu8; 64];
991        let engine_id = decode_hex("000000000000000000000002").unwrap();
992
993        let extended = extend_key_reeder(AuthProtocol::Sha256, &long_key, &engine_id, 32);
994        assert_eq!(extended.len(), 32);
995        assert_eq!(extended, vec![0xAAu8; 32]);
996    }
997
998    #[test]
999    fn test_reeder_vs_blumenthal_differ() {
1000        // Verify that Reeder and Blumenthal produce different results
1001        let password = b"maplesyrup";
1002        let engine_id = decode_hex("000000000000000000000002").unwrap();
1003
1004        let k1 = LocalizedKey::from_password(AuthProtocol::Sha1, password, &engine_id);
1005
1006        let reeder = extend_key_reeder(AuthProtocol::Sha1, k1.as_bytes(), &engine_id, 32);
1007        let blumenthal = extend_key(AuthProtocol::Sha1, k1.as_bytes(), 32);
1008
1009        assert_eq!(reeder.len(), 32);
1010        assert_eq!(blumenthal.len(), 32);
1011
1012        // First 20 bytes should be identical (both start with K1)
1013        assert_eq!(&reeder[..20], &blumenthal[..20]);
1014        // Extension bytes should differ (different algorithms)
1015        assert_ne!(&reeder[20..], &blumenthal[20..]);
1016    }
1017}