Skip to main content

rsa/traits/
encryption.rs

1//! Encryption-related traits.
2
3#[cfg(feature = "alloc")]
4use alloc::vec::Vec;
5use rand_core::{CryptoRng, TryCryptoRng};
6
7use crate::errors::Result;
8
9/// Encrypt the message using provided random source
10pub trait RandomizedEncryptor {
11    /// Encrypt the given message into caller-provided storage.
12    fn encrypt_with_rng_into<'a, R: TryCryptoRng + ?Sized>(
13        &self,
14        rng: &mut R,
15        msg: &[u8],
16        storage: &'a mut [u8],
17    ) -> Result<&'a [u8]>;
18
19    /// Encrypt the given message.
20    #[cfg(feature = "alloc")]
21    fn encrypt_with_rng<R: CryptoRng + ?Sized>(&self, rng: &mut R, msg: &[u8]) -> Result<Vec<u8>>;
22}
23
24/// Decrypt the given message
25pub trait Decryptor {
26    /// Decrypt the given message.
27    #[cfg(feature = "alloc")]
28    fn decrypt(&self, ciphertext: &[u8]) -> Result<Vec<u8>>;
29}
30
31/// Decrypt the given message using provided random source
32pub trait RandomizedDecryptor {
33    /// Decrypt the given message.
34    #[cfg(feature = "alloc")]
35    fn decrypt_with_rng<R: CryptoRng + ?Sized>(
36        &self,
37        rng: &mut R,
38        ciphertext: &[u8],
39    ) -> Result<Vec<u8>>;
40}
41
42/// Encryption keypair with an associated encryption key.
43pub trait EncryptingKeypair {
44    /// Encrypting key type for this keypair.
45    type EncryptingKey: Clone;
46
47    /// Get the encrypting key which can encrypt messages to be decrypted by
48    /// the decryption key portion of this keypair.
49    fn encrypting_key(&self) -> Self::EncryptingKey;
50}