LocalizedKey

Struct LocalizedKey 

Source
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

Source

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:

  1. Expand password to 1MB by repetition
  2. Hash the expansion to get the master key
  3. 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).

Source

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.

Source

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.

Source

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).

Source

pub fn protocol(&self) -> AuthProtocol

Get the protocol this key is for.

Source

pub fn as_bytes(&self) -> &[u8]

Get the raw key bytes.

Source

pub fn mac_len(&self) -> usize

Get the MAC length for this key’s protocol.

Source

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).

Source

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

Source§

fn clone(&self) -> LocalizedKey

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for LocalizedKey

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for LocalizedKey

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl Zeroize for LocalizedKey

Source§

fn zeroize(&mut self)

Zero out this object from memory using Rust intrinsics which ensure the zeroization operation is not “optimized away” by the compiler.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more