Skip to main content

MessageAuthenticator

Struct MessageAuthenticator 

Source
pub struct MessageAuthenticator { /* private fields */ }
Expand description

Production-quality message authenticator with:

  • HMAC-FNV64 signing / verification (three algorithm variants).
  • Replay attack prevention via a bounded nonce window.
  • Key expiry and atomic key rotation.
  • Configurable AuthPolicy enforcement.
  • Monotonic per-key sequence numbers.
  • Cumulative statistics and drainable audit log.

§Example

use ipfrs_network::message_authenticator::{
    AuthAlgorithm, AuthKey, AuthPolicy, MessageAuthenticator,
};

let mut auth = MessageAuthenticator::new(vec![AuthPolicy::RequireAll], 256);
let key = AuthKey::new("k1", b"super-secret".to_vec(), 0, None, AuthAlgorithm::HmacFnv64);
auth.add_key(key).unwrap();
let msg = auth.sign(b"hello world".to_vec(), "k1", 1_000_000).unwrap();
auth.verify(&msg, 1_000_000).unwrap();

Implementations§

Source§

impl MessageAuthenticator

Source

pub fn new(policies: Vec<AuthPolicy>, replay_window_size: usize) -> Self

Create a new MessageAuthenticator.

  • policies — list of AuthPolicy variants to enforce.
  • replay_window_size — maximum number of nonces retained for replay detection.
§Panics

Does not panic; replay_window_size == 0 is accepted but provides no replay protection.

Source

pub fn add_key(&mut self, key: AuthKey) -> Result<(), AuthError>

Add a new key to the keystore.

Fails with AuthError::KeyNotFound if a key with the same ID already exists (use rotate_key for atomic replacement).

Source

pub fn remove_key(&mut self, key_id: &str) -> Result<(), AuthError>

Remove a key from the keystore.

Returns AuthError::KeyNotFound if no such key exists.

Source

pub fn rotate_key( &mut self, old_id: &str, new_key: AuthKey, ) -> Result<(), AuthError>

Atomically replace the key identified by old_id with new_key.

The sequence counter is reset to 1 for the new key. Returns AuthError::KeyNotFound if old_id is not in the keystore.

Source

pub fn sign( &mut self, payload: Vec<u8>, key_id: &str, current_ts: u64, ) -> Result<SignedMessage, AuthError>

Sign payload using the key identified by key_id.

  • A fresh nonce is generated via xorshift64.
  • The signature is computed per the key’s AuthAlgorithm.
  • The per-key sequence counter is incremented atomically.
§Errors
  • AuthError::KeyNotFound — unknown key.
  • AuthError::KeyExpired — key has passed its expiry.
  • AuthError::NonceExhausted — PRNG state wrapped to zero (extremely unlikely in practice).
  • AuthError::KeyRotationRequired — (via policy check) key age exceeds the rotation threshold.
Source

pub fn verify( &mut self, msg: &SignedMessage, current_ts: u64, ) -> Result<(), AuthError>

Verify the authenticity of msg at time current_ts.

Checks (in order):

  1. Key exists in the keystore.
  2. Key has not expired.
  3. Nonce has not been replayed.
  4. Sequence number is valid (when SequentialNonce policy is active).
  5. Signature matches the recomputed value.

On success the nonce is recorded and statistics are updated.

Source

pub fn check_replay(&self, nonce: u64) -> bool

Return true if nonce is present in the current replay window.

Source

pub fn record_nonce(&mut self, nonce: u64)

Add nonce to the replay window, evicting the oldest entry if full.

Source

pub fn expire_keys(&mut self, current_ts: u64) -> Vec<String>

Remove all keys whose expires_at is ≤ current_ts.

Returns the IDs of all removed keys.

Source

pub fn stats(&self) -> AuthStats

Return a snapshot of the current cumulative statistics.

Source

pub fn drain_events(&mut self) -> Vec<String>

Drain the internal audit log, returning all accumulated events.

The log is cleared after this call.

Source

pub fn key_count(&self) -> usize

Return the number of keys currently in the keystore.

Source

pub fn has_key(&self, key_id: &str) -> bool

Return true if a key with key_id is currently registered.

Trait Implementations§

Source§

impl Debug for MessageAuthenticator

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Allocation for T
where T: RefUnwindSafe + Send + Sync,

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<'a, T, E> AsTaggedExplicit<'a, E> for T
where T: 'a,

Source§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self, E>

Source§

impl<'a, T, E> AsTaggedImplicit<'a, E> for T
where T: 'a,

Source§

fn implicit( self, class: Class, constructed: bool, tag: u32, ) -> TaggedParser<'a, Implicit, Self, E>

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

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