pub struct LocalizedKey { /* private fields */ }Expand description
Localized authentication key.
A key that has been derived from a password and bound to a specific engine ID. This key can be used for HMAC operations on messages to/from that engine.
§Security
Key material is automatically zeroed from memory when the key is dropped,
using the zeroize crate. This provides defense-in-depth against memory
scraping attacks.
Implementations§
Source§impl LocalizedKey
impl LocalizedKey
Sourcepub fn from_password(
protocol: AuthProtocol,
password: &[u8],
engine_id: &[u8],
) -> Self
pub fn from_password( protocol: AuthProtocol, password: &[u8], engine_id: &[u8], ) -> Self
Derive a localized key from a password and engine ID.
This implements the key localization algorithm from RFC 3414 Section A.2:
- Expand password to 1MB by repetition
- Hash the expansion to get the master key
- Hash (master_key || engine_id || master_key) to get the localized key
§Performance Note
This method performs the full key derivation (~850μs for SHA-256). When
polling many engines with shared credentials, use MasterKey to cache
the intermediate result and call MasterKey::localize for each engine.
§Empty and Short Passwords
Empty passwords result in an all-zero key of the appropriate length for
the authentication protocol. This differs from net-snmp, which rejects
passwords shorter than 8 characters with USM_PASSWORDTOOSHORT.
While empty/short passwords are accepted for flexibility, they provide
minimal security. A warning is logged at the WARN level when the
password is shorter than MIN_PASSWORD_LENGTH (8 characters).
Sourcepub fn from_str_password(
protocol: AuthProtocol,
password: &str,
engine_id: &[u8],
) -> Self
pub fn from_str_password( protocol: AuthProtocol, password: &str, engine_id: &[u8], ) -> Self
Derive a localized key from a string password and engine ID.
This is a convenience method that converts the string to bytes and calls
from_password.
Sourcepub fn from_master_key(master: &MasterKey, engine_id: &[u8]) -> Self
pub fn from_master_key(master: &MasterKey, engine_id: &[u8]) -> Self
Create a localized key from a master key and engine ID.
This is the efficient path when you have a cached MasterKey.
Equivalent to calling MasterKey::localize.
Sourcepub fn from_bytes(protocol: AuthProtocol, key: impl Into<Vec<u8>>) -> Self
pub fn from_bytes(protocol: AuthProtocol, key: impl Into<Vec<u8>>) -> Self
Create a localized key from raw bytes.
Use this if you already have a localized key (e.g., from configuration).
Sourcepub fn protocol(&self) -> AuthProtocol
pub fn protocol(&self) -> AuthProtocol
Get the protocol this key is for.
Sourcepub fn compute_hmac(&self, data: &[u8]) -> Vec<u8> ⓘ
pub fn compute_hmac(&self, data: &[u8]) -> Vec<u8> ⓘ
Compute HMAC over a message and return the truncated MAC.
The returned MAC is truncated to the appropriate length for the protocol (12 bytes for MD5/SHA-1, variable for SHA-2).
Sourcepub fn verify_hmac(&self, data: &[u8], expected: &[u8]) -> bool
pub fn verify_hmac(&self, data: &[u8], expected: &[u8]) -> bool
Verify an HMAC.
Returns true if the MAC matches, false otherwise.
Trait Implementations§
Source§impl Clone for LocalizedKey
impl Clone for LocalizedKey
Source§fn clone(&self) -> LocalizedKey
fn clone(&self) -> LocalizedKey
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more