Struct EncryptedMessage

Source
pub struct EncryptedMessage<T> { /* private fields */ }
Expand description

A wrapper for a message of type T with support for in-place encryption/decryption.

This struct provides functionality for encrypting and decrypting messages while maintaining a specific format:

[ msg-hdr | additional-data | payload | trailer | tag + nonce ]

Where:

  • msg-hdr: Message header containing ID, TTL, and flags
  • additional-data: Optional unencrypted data
  • payload: The encrypted external representation of type T
  • trailer: Optional encrypted variable-sized data
  • tag + nonce: Authentication tag and nonce for the encryption scheme

The payload and trailer sections are encrypted, while the header and additional data remain in plaintext.

Implementations§

Source§

impl<T: AnyBitPattern + NoUninit> EncryptedMessage<T>

Source

pub fn size(ad: usize, trailer: usize, scheme: &dyn EncryptionScheme) -> usize

Calculates the total size of an encrypted message.

§Arguments
  • ad - Size of additional data in bytes
  • trailer - Size of trailer data in bytes
  • scheme - The encryption scheme to use
§Returns

The total size in bytes needed for the encrypted message

Source

pub fn new( id: &MsgId, ttl: u32, flags: u16, trailer: usize, scheme: &dyn EncryptionScheme, ) -> Self

Creates a new encrypted message with the specified parameters.

§Arguments
  • id - Message identifier
  • ttl - Time-to-live value
  • flags - Message flags
  • trailer - Size of trailer data in bytes
  • scheme - The encryption scheme to use
§Returns

A new EncryptedMessage instance

Source

pub fn new_with_ad( id: &MsgId, ttl: u32, flags: u16, additional_data: usize, trailer: usize, scheme: &dyn EncryptionScheme, ) -> Self

Creates a new encrypted message with additional data.

§Arguments
  • id - Message identifier
  • ttl - Time-to-live value
  • flags - Message flags
  • additional_data - Size of additional data in bytes
  • trailer - Size of trailer data in bytes
  • scheme - The encryption scheme to use
§Returns

A new EncryptedMessage instance with space for additional data

Source

pub fn from_buffer( buffer: Vec<u8>, id: &MsgId, ttl: u32, flags: u16, additional_data: usize, trailer: usize, scheme: &dyn EncryptionScheme, ) -> Self

Creates an encrypted message from an existing buffer.

§Arguments
  • buffer - Existing buffer to use
  • id - Message identifier
  • ttl - Time-to-live value
  • flags - Message flags
  • additional_data - Size of additional data in bytes
  • trailer - Size of trailer data in bytes
  • scheme - The encryption scheme to use
§Returns

A new EncryptedMessage instance using the provided buffer

Source

pub fn payload_with_ad( &mut self, scheme: &dyn EncryptionScheme, ) -> (&mut T, &mut [u8], &mut [u8])

Returns mutable references to the message payload, trailer, and additional data.

§Arguments
  • scheme - The encryption scheme to use
§Returns

A tuple containing:

  • Mutable reference to the payload object
  • Mutable reference to the trailer bytes
  • Mutable reference to the additional data bytes
Source

pub fn payload(&mut self, scheme: &dyn EncryptionScheme) -> (&mut T, &mut [u8])

Returns mutable references to the message payload and trailer.

§Arguments
  • scheme - The encryption scheme to use
§Returns

A tuple containing:

  • Mutable reference to the payload object
  • Mutable reference to the trailer bytes
Source

pub fn encrypt( self, scheme: &mut dyn EncryptionScheme, receiver: usize, ) -> Option<Vec<u8>>

Encrypts the message using the provided encryption scheme.

§Arguments
  • scheme - The encryption scheme to use
  • receiver - The ID of the intended receiver
§Returns

The encrypted message as a byte vector, or None if encryption failed

Source

pub fn decrypt_with_ad<'msg>( buffer: &'msg mut [u8], additional_data: usize, trailer: usize, scheme: &dyn EncryptionScheme, sender: usize, ) -> Option<(&'msg T, &'msg [u8], &'msg [u8])>

Decrypts a message and returns references to the payload, trailer, and additional data.

§Arguments
  • buffer - The encrypted message buffer
  • additional_data - Size of additional data in bytes
  • trailer - Size of trailer data in bytes
  • scheme - The encryption scheme to use
  • sender - The ID of the message sender
§Returns

A tuple containing references to the decrypted payload, trailer, and additional data, or None if decryption failed

Source

pub fn decrypt<'msg>( buffer: &'msg mut [u8], trailer: usize, scheme: &dyn EncryptionScheme, sender: usize, ) -> Option<(&'msg T, &'msg [u8])>

Decrypts a message and returns references to the payload and trailer.

§Arguments
  • buffer - The encrypted message buffer
  • trailer - Size of trailer data in bytes
  • scheme - The encryption scheme to use
  • sender - The ID of the message sender
§Returns

A tuple containing references to the decrypted payload and trailer, or None if decryption failed

Auto Trait Implementations§

§

impl<T> Freeze for EncryptedMessage<T>

§

impl<T> RefUnwindSafe for EncryptedMessage<T>
where T: RefUnwindSafe,

§

impl<T> Send for EncryptedMessage<T>
where T: Send,

§

impl<T> Sync for EncryptedMessage<T>
where T: Sync,

§

impl<T> Unpin for EncryptedMessage<T>
where T: Unpin,

§

impl<T> UnwindSafe for EncryptedMessage<T>
where T: UnwindSafe,

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

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

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

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

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, 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.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

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
§

fn with_current_subscriber(self) -> WithDispatch<Self>

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