SerdeEncryptSharedKey

Trait SerdeEncryptSharedKey 

Source
pub trait SerdeEncryptSharedKey {
    type S: TypedSerialized<T = Self>;

    // Provided methods
    fn encrypt(&self, shared_key: &SharedKey) -> Result<EncryptedMessage, Error>
       where Self: Serialize { ... }
    fn decrypt_owned(
        encrypted_message: &EncryptedMessage,
        shared_key: &SharedKey,
    ) -> Result<Self, Error>
       where Self: DeserializeOwned { ... }
    fn decrypt_ref<'de>(
        encrypted_message: &EncryptedMessage,
        shared_key: &SharedKey,
    ) -> Result<Self::S, Error>
       where Self: Deserialize<'de> { ... }
}
Expand description

Shared-key authenticated encryption for serde-serializable types.

§Features

  • Message authentication.
  • Different cipher-text for the same plain-text to avoid attacks such as statistical analysis of cipher-text.
  • Uses small (32-byte) key.

§Anti-features

  • Identity authentication of sender nor receiver.

Good for both large and small message encryption / decryption.

§when sender and receiver does not hold shared key yet:

First, message sender or receiver should generate SharedKey.

And then sender or receiver who generated the key should give it to another using safe communication. SerdeEncryptPublicKey is recommended for it.

§Examples

§Encrypting owned data with already-shared key

See this example.

§Generate and exchange shared key and encrypt struct with reference fields

See this example.

§Algorithm

  • Encryption: XChaCha20
  • Message authentication: Poly1305 MAC

Required Associated Types§

Source

type S: TypedSerialized<T = Self>

Serializer implementation

Provided Methods§

Source

fn encrypt(&self, shared_key: &SharedKey) -> Result<EncryptedMessage, Error>
where Self: Serialize,

Serialize and encrypt.

§Failures
Source

fn decrypt_owned( encrypted_message: &EncryptedMessage, shared_key: &SharedKey, ) -> Result<Self, Error>
where Self: DeserializeOwned,

Decrypt and deserialize into DeserializeOwned type.

§Failures
Source

fn decrypt_ref<'de>( encrypted_message: &EncryptedMessage, shared_key: &SharedKey, ) -> Result<Self::S, Error>
where Self: Deserialize<'de>,

Just decrypts cipher-text. Returned data must be deserialized later. Types implementing serde::Deserialize<'de> (not serde::de::DeserializeOwned) should use this function to resolve lifetime.

§Failures

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§