Trait serde_encrypt::traits::SerdeEncryptSharedKey
source · [−]pub trait SerdeEncryptSharedKey {
type S: TypedSerialized<T = Self>;
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.
Popular use cases
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
type S: TypedSerialized<T = Self>
type S: TypedSerialized<T = Self>
Serializer implementation
Provided Methods
Serialize and encrypt.
Failures
- SerializationError when failed to serialize message.
- EncryptionError when failed to encrypt serialized message.
fn decrypt_owned(
encrypted_message: &EncryptedMessage,
shared_key: &SharedKey
) -> Result<Self, Error> where
Self: DeserializeOwned,
fn decrypt_owned(
encrypted_message: &EncryptedMessage,
shared_key: &SharedKey
) -> Result<Self, Error> where
Self: DeserializeOwned,
Decrypt and deserialize into DeserializeOwned type.
Failures
- DecryptionError when failed to decrypt message.
- DeserializationError when failed to deserialize decrypted message.
fn decrypt_ref<'de>(
encrypted_message: &EncryptedMessage,
shared_key: &SharedKey
) -> Result<Self::S, Error> where
Self: Deserialize<'de>,
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
- DecryptionError when failed to decrypt message.