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 flagsadditional-data
: Optional unencrypted datapayload
: The encrypted external representation of type Ttrailer
: Optional encrypted variable-sized datatag + 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>
impl<T: AnyBitPattern + NoUninit> EncryptedMessage<T>
Sourcepub fn new(
id: &MsgId,
ttl: u32,
flags: u16,
trailer: usize,
scheme: &dyn EncryptionScheme,
) -> Self
pub fn new( id: &MsgId, ttl: u32, flags: u16, trailer: usize, scheme: &dyn EncryptionScheme, ) -> Self
Sourcepub fn new_with_ad(
id: &MsgId,
ttl: u32,
flags: u16,
additional_data: usize,
trailer: usize,
scheme: &dyn EncryptionScheme,
) -> Self
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 identifierttl
- Time-to-live valueflags
- Message flagsadditional_data
- Size of additional data in bytestrailer
- Size of trailer data in bytesscheme
- The encryption scheme to use
§Returns
A new EncryptedMessage
instance with space for additional data
Sourcepub fn from_buffer(
buffer: Vec<u8>,
id: &MsgId,
ttl: u32,
flags: u16,
additional_data: usize,
trailer: usize,
scheme: &dyn EncryptionScheme,
) -> Self
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 useid
- Message identifierttl
- Time-to-live valueflags
- Message flagsadditional_data
- Size of additional data in bytestrailer
- Size of trailer data in bytesscheme
- The encryption scheme to use
§Returns
A new EncryptedMessage
instance using the provided buffer
Sourcepub fn payload_with_ad(
&mut self,
scheme: &dyn EncryptionScheme,
) -> (&mut T, &mut [u8], &mut [u8])
pub fn payload_with_ad( &mut self, scheme: &dyn EncryptionScheme, ) -> (&mut T, &mut [u8], &mut [u8])
Sourcepub fn payload(&mut self, scheme: &dyn EncryptionScheme) -> (&mut T, &mut [u8])
pub fn payload(&mut self, scheme: &dyn EncryptionScheme) -> (&mut T, &mut [u8])
Sourcepub fn encrypt(
self,
scheme: &mut dyn EncryptionScheme,
receiver: usize,
) -> Option<Vec<u8>>
pub fn encrypt( self, scheme: &mut dyn EncryptionScheme, receiver: usize, ) -> Option<Vec<u8>>
Sourcepub 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])>
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 bufferadditional_data
- Size of additional data in bytestrailer
- Size of trailer data in bytesscheme
- The encryption scheme to usesender
- The ID of the message sender
§Returns
A tuple containing references to the decrypted payload, trailer, and additional data,
or None
if decryption failed
Sourcepub fn decrypt<'msg>(
buffer: &'msg mut [u8],
trailer: usize,
scheme: &dyn EncryptionScheme,
sender: usize,
) -> Option<(&'msg T, &'msg [u8])>
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 buffertrailer
- Size of trailer data in bytesscheme
- The encryption scheme to usesender
- The ID of the message sender
§Returns
A tuple containing references to the decrypted payload and trailer,
or None
if decryption failed