EncryptedChannel

Struct EncryptedChannel 

Source
pub struct EncryptedChannel {
    pub peer_id: String,
    /* private fields */
}
Expand description

Encrypted communication channel using ChaCha20-Poly1305 AEAD

§Security Properties

  • Confidentiality: ChaCha20 stream cipher (IND-CPA)
  • Integrity: Poly1305 MAC (SUF-CMA)
  • AEAD: Combined mode (IND-CCA2)
  • Nonce: 96-bit random + 32-bit counter (unique per message)

§Example

let channel = EncryptedChannel::new(peer_id, shared_secret);
let ciphertext = channel.encrypt(b"secret message")?;
let plaintext = channel.decrypt(&ciphertext)?;

Fields§

§peer_id: String

Peer identifier

Implementations§

Source§

impl EncryptedChannel

Source

pub fn new(peer_id: String, shared_secret: SharedSecret) -> Self

Create a new encrypted channel from a shared secret

§Arguments
  • peer_id - Identifier for the peer (for auditing/logging)
  • shared_secret - Shared secret from Kyber KEM
§Security

Keys are derived using HKDF-SHA256 with domain separation.

Source

pub fn encrypt(&self, plaintext: &[u8]) -> Result<Vec<u8>>

Encrypt a message using ChaCha20-Poly1305

§Arguments
  • plaintext - Message to encrypt
§Returns

Ciphertext format: [nonce: 12 bytes][ciphertext][tag: 16 bytes]

§Errors

Returns CryptoError if encryption fails (should never happen).

§Security
  • Unique nonce per message (96-bit random + 32-bit counter)
  • Authenticated encryption (modify ciphertext = detection)
  • Quantum resistance: 128-bit security (Grover bound)
Source

pub fn decrypt(&self, ciphertext: &[u8]) -> Result<Vec<u8>>

Decrypt a message using ChaCha20-Poly1305

§Arguments
  • ciphertext - Encrypted message (format: [nonce: 12][ciphertext][tag: 16])
§Returns

Decrypted plaintext

§Errors

Returns CryptoError if:

  • Ciphertext is too short (< 28 bytes)
  • Authentication tag verification fails (tampering detected)
  • Decryption fails
§Security
  • Constant-time: Timing independent of plaintext content
  • Tamper-evident: Any modification causes authentication failure
Source

pub fn sign(&self, message: &[u8]) -> Vec<u8>

Sign a message with HMAC-SHA256

§Arguments
  • message - Message to authenticate
§Returns

32-byte HMAC tag

§Security
  • PRF security: tag reveals nothing about key
  • Quantum resistance: 128-bit security (Grover)
§Note

If using encrypt(), signatures are redundant (Poly1305 provides authentication). Use this for non-encrypted authenticated messages.

Source

pub fn verify(&self, message: &[u8], signature: &[u8]) -> bool

Verify a message signature using constant-time comparison

§Arguments
  • message - Original message
  • signature - HMAC tag to verify
§Returns

true if signature is valid, false otherwise

§Security
  • Constant-time: Execution time independent of signature validity
  • Timing-attack resistant: No early termination on mismatch
§Critical Security Property

This function MUST use constant-time comparison to prevent timing side-channels.

Trait Implementations§

Source§

impl Clone for EncryptedChannel

Source§

fn clone(&self) -> Self

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 EncryptedChannel

Source§

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

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

impl<'de> Deserialize<'de> for EncryptedChannel

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Serialize for EncryptedChannel

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

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

Source§

fn vzip(self) -> V

Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,