pub struct PrivKey { /* private fields */ }Expand description
Privacy key for encryption/decryption operations.
Derives encryption keys from a password and engine ID using the same process as authentication keys, then uses the appropriate portion based on the privacy protocol.
§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 PrivKey
impl PrivKey
Sourcepub fn from_password(
auth_protocol: AuthProtocol,
priv_protocol: PrivProtocol,
password: &[u8],
engine_id: &[u8],
) -> Self
pub fn from_password( auth_protocol: AuthProtocol, priv_protocol: PrivProtocol, password: &[u8], engine_id: &[u8], ) -> Self
Derive a privacy key from a password and engine ID.
The key derivation uses the same algorithm as authentication keys (RFC 3414 A.2), but the resulting key is used differently:
- DES: first 8 bytes = key, last 8 bytes = pre-IV
- 3DES: first 24 bytes = key, last 8 bytes = pre-IV
- AES: first 16/24/32 bytes = key (depending on AES variant)
Key extension is automatically applied when needed based on the auth/priv protocol combination:
- AES-192/256 with SHA-1 or MD5: Blumenthal extension (draft-blumenthal-aes-usm-04)
- 3DES with SHA-1 or MD5: Reeder extension (draft-reeder-snmpv3-usm-3desede-00)
§Performance Note
This method performs the full key derivation (~850μs for SHA-256). When
polling many engines with shared credentials, use MasterKey
and call PrivKey::from_master_key for each engine.
§Example
use async_snmp::{AuthProtocol, PrivProtocol, v3::PrivKey};
let engine_id = [0x80, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04];
// SHA-1 only produces 20 bytes, but AES-256 needs 32.
// Blumenthal extension is automatically applied.
let priv_key = PrivKey::from_password(
AuthProtocol::Sha1,
PrivProtocol::Aes256,
b"password",
&engine_id,
);Sourcepub fn from_master_key(
master: &MasterKey,
priv_protocol: PrivProtocol,
engine_id: &[u8],
) -> Self
pub fn from_master_key( master: &MasterKey, priv_protocol: PrivProtocol, engine_id: &[u8], ) -> Self
Derive a privacy key from a master key and engine ID.
This is the efficient path when you have a cached MasterKey.
Key extension is automatically applied when needed based on the auth/priv
protocol combination:
- AES-192/256 with SHA-1 or MD5: Blumenthal extension (draft-blumenthal-aes-usm-04)
- 3DES with SHA-1 or MD5: Reeder extension (draft-reeder-snmpv3-usm-3desede-00)
§Example
use async_snmp::{AuthProtocol, MasterKey, PrivProtocol, v3::PrivKey};
let master = MasterKey::from_password(AuthProtocol::Sha1, b"password");
let engine_id = [0x80, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04];
// SHA-1 only produces 20 bytes, but AES-256 needs 32.
// Blumenthal extension is automatically applied.
let priv_key = PrivKey::from_master_key(&master, PrivProtocol::Aes256, &engine_id);Sourcepub fn from_bytes(protocol: PrivProtocol, key: impl Into<Vec<u8>>) -> Self
pub fn from_bytes(protocol: PrivProtocol, key: impl Into<Vec<u8>>) -> Self
Create a privacy key from raw localized key bytes.
Sourcepub fn protocol(&self) -> PrivProtocol
pub fn protocol(&self) -> PrivProtocol
Get the privacy protocol.
Sourcepub fn encryption_key(&self) -> &[u8] ⓘ
pub fn encryption_key(&self) -> &[u8] ⓘ
Get the encryption key portion.
Sourcepub fn encrypt(
&mut self,
plaintext: &[u8],
engine_boots: u32,
engine_time: u32,
salt_counter: Option<&SaltCounter>,
) -> PrivacyResult<(Bytes, Bytes)>
pub fn encrypt( &mut self, plaintext: &[u8], engine_boots: u32, engine_time: u32, salt_counter: Option<&SaltCounter>, ) -> PrivacyResult<(Bytes, Bytes)>
Encrypt data and return (ciphertext, privParameters).
§Arguments
plaintext- The data to encrypt (typically the serialized ScopedPDU)engine_boots- The authoritative engine’s boot countengine_time- The authoritative engine’s timesalt_counter- Optional shared salt counter; if None, uses internal counter
§Returns
Ok((ciphertext, priv_params))on successErron encryption failure
Sourcepub fn decrypt(
&self,
ciphertext: &[u8],
engine_boots: u32,
engine_time: u32,
priv_params: &[u8],
) -> PrivacyResult<Bytes>
pub fn decrypt( &self, ciphertext: &[u8], engine_boots: u32, engine_time: u32, priv_params: &[u8], ) -> PrivacyResult<Bytes>
Decrypt data using the privParameters from the message.
§Arguments
ciphertext- The encrypted dataengine_boots- The authoritative engine’s boot count (from message)engine_time- The authoritative engine’s time (from message)priv_params- The privParameters field from the message
§Returns
Ok(plaintext)on successErron decryption failure