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: StringPeer identifier
Implementations§
Source§impl EncryptedChannel
impl EncryptedChannel
Sourcepub fn new(peer_id: String, shared_secret: SharedSecret) -> Self
pub fn new(peer_id: String, shared_secret: SharedSecret) -> Self
Sourcepub fn encrypt(&self, plaintext: &[u8]) -> Result<Vec<u8>>
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)
Sourcepub fn decrypt(&self, ciphertext: &[u8]) -> Result<Vec<u8>>
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
Sourcepub fn sign(&self, message: &[u8]) -> Vec<u8> ⓘ
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.
Sourcepub fn verify(&self, message: &[u8], signature: &[u8]) -> bool
pub fn verify(&self, message: &[u8], signature: &[u8]) -> bool
Verify a message signature using constant-time comparison
§Arguments
message- Original messagesignature- 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
impl Clone for EncryptedChannel
Source§impl Debug for EncryptedChannel
impl Debug for EncryptedChannel
Source§impl<'de> Deserialize<'de> for EncryptedChannel
impl<'de> Deserialize<'de> for EncryptedChannel
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
Auto Trait Implementations§
impl !Freeze for EncryptedChannel
impl RefUnwindSafe for EncryptedChannel
impl Send for EncryptedChannel
impl Sync for EncryptedChannel
impl Unpin for EncryptedChannel
impl UnwindSafe for EncryptedChannel
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more