pub struct MasterKey { /* private fields */ }Expand description
Master authentication key (Ku) before engine localization.
This is the intermediate result of the RFC 3414 password-to-key algorithm, computed by expanding the password to 1MB and hashing it. This step is computationally expensive (~850μs for SHA-256) but can be cached and reused across multiple engines that share the same credentials.
§Performance
| Operation | Time |
|---|---|
MasterKey::from_password (SHA-256) | ~850 μs |
MasterKey::localize | ~1 μs |
For applications polling many engines with shared credentials, caching the
MasterKey provides significant performance benefits.
§Security
Key material is automatically zeroed from memory when dropped, using the
zeroize crate. This provides defense-in-depth against memory scraping.
§Example
use async_snmp::{AuthProtocol, MasterKey};
// Derive master key once (expensive)
let master = MasterKey::from_password(AuthProtocol::Sha256, b"authpassword");
// Localize to different engines (cheap)
let engine1_id = b"\x80\x00\x1f\x88\x80\xe9\xb1\x04\x61\x73\x61\x00\x00\x00";
let engine2_id = b"\x80\x00\x1f\x88\x80\xe9\xb1\x04\x61\x73\x61\x00\x00\x01";
let key1 = master.localize(engine1_id);
let key2 = master.localize(engine2_id);Implementations§
Source§impl MasterKey
impl MasterKey
Sourcepub fn from_password(protocol: AuthProtocol, password: &[u8]) -> Self
pub fn from_password(protocol: AuthProtocol, password: &[u8]) -> Self
Derive a master key from a password.
This implements RFC 3414 Section A.2.1: expand the password to 1MB by repetition, then hash the result. This is computationally expensive (~850μs for SHA-256) but only needs to be done once per password.
§Empty and Short Passwords
Empty passwords result in an all-zero key. A warning is logged when
the password is shorter than MIN_PASSWORD_LENGTH (8 characters).
Sourcepub fn from_str_password(protocol: AuthProtocol, password: &str) -> Self
pub fn from_str_password(protocol: AuthProtocol, password: &str) -> Self
Derive a master key from a string password.
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 master key from raw bytes.
Use this if you already have a master key (e.g., from configuration). The bytes should be the raw digest output from the 1MB password expansion.
Sourcepub fn localize(&self, engine_id: &[u8]) -> LocalizedKey
pub fn localize(&self, engine_id: &[u8]) -> LocalizedKey
Localize this master key to a specific engine ID.
This implements RFC 3414 Section A.2.2:
localized_key = H(master_key || engine_id || master_key)
This operation is cheap (~1μs) compared to master key derivation.
Sourcepub fn protocol(&self) -> AuthProtocol
pub fn protocol(&self) -> AuthProtocol
Get the protocol this key is for.