use crate::data::traits::{BatchEncryptable, Encryptable, Encrypted};
#[cfg(feature = "offline")]
use crate::keys::GlobalPublicKeys;
use crate::keys::{KeyProvider, SessionKeys};
use rand_core::{CryptoRng, Rng};
#[derive(Clone)]
pub struct Client {
pub(crate) keys: SessionKeys,
}
impl Client {
pub fn new(keys: SessionKeys) -> Self {
Self { keys }
}
pub fn dump(&self) -> &SessionKeys {
&self.keys
}
pub fn restore(keys: SessionKeys) -> Self {
Self { keys }
}
pub fn encrypt<M, R>(&self, message: &M, rng: &mut R) -> M::EncryptedType
where
M: Encryptable,
SessionKeys: KeyProvider<M::PublicKeyType>,
R: Rng + CryptoRng,
{
message.encrypt(self.keys.get_key(), rng)
}
#[cfg(feature = "elgamal3")]
pub fn decrypt<E>(&self, encrypted: &E) -> Option<E::UnencryptedType>
where
E: Encrypted,
SessionKeys: KeyProvider<E::SecretKeyType>,
{
encrypted.decrypt(self.keys.get_key())
}
#[cfg(not(feature = "elgamal3"))]
pub fn decrypt<E>(&self, encrypted: &E) -> E::UnencryptedType
where
E: Encrypted,
SessionKeys: KeyProvider<E::SecretKeyType>,
{
encrypted.decrypt(self.keys.get_key())
}
#[cfg(feature = "batch")]
pub fn encrypt_batch<M, R>(
&self,
messages: &[M],
rng: &mut R,
) -> Result<Vec<M::EncryptedType>, crate::transcryptor::BatchError>
where
M: BatchEncryptable,
SessionKeys: KeyProvider<M::PublicKeyType>,
R: Rng + CryptoRng,
{
super::batch::encrypt_batch(messages, self.keys.get_key(), rng)
}
#[cfg(feature = "insecure")]
pub fn encrypt_batch_raw<M, R>(
&self,
messages: &[M],
rng: &mut R,
) -> Result<Vec<M::EncryptedType>, crate::transcryptor::BatchError>
where
M: Encryptable,
SessionKeys: KeyProvider<M::PublicKeyType>,
R: Rng + CryptoRng,
{
super::batch::encrypt_batch_raw(messages, self.keys.get_key(), rng)
}
#[cfg(all(feature = "batch", feature = "elgamal3"))]
pub fn decrypt_batch<E>(
&self,
encrypted: &[E],
) -> Result<Vec<E::UnencryptedType>, crate::transcryptor::BatchError>
where
E: Encrypted,
SessionKeys: KeyProvider<E::SecretKeyType>,
{
super::batch::decrypt_batch(encrypted, self.keys.get_key())
}
#[cfg(all(feature = "batch", not(feature = "elgamal3")))]
pub fn decrypt_batch<E>(
&self,
encrypted: &[E],
) -> Result<Vec<E::UnencryptedType>, crate::transcryptor::BatchError>
where
E: Encrypted,
SessionKeys: KeyProvider<E::SecretKeyType>,
{
super::batch::decrypt_batch(encrypted, self.keys.get_key())
}
}
#[cfg(feature = "offline")]
#[derive(Clone)]
pub struct OfflineClient {
pub global_public_keys: GlobalPublicKeys,
}
#[cfg(feature = "offline")]
impl OfflineClient {
pub fn new(global_public_keys: GlobalPublicKeys) -> Self {
Self { global_public_keys }
}
pub fn encrypt<M, R>(&self, message: &M, rng: &mut R) -> M::EncryptedType
where
M: Encryptable,
GlobalPublicKeys: KeyProvider<M::GlobalPublicKeyType>,
R: Rng + CryptoRng,
{
message.encrypt_global(self.global_public_keys.get_key(), rng)
}
#[cfg(feature = "batch")]
pub fn encrypt_batch<M, R>(
&self,
messages: &[M],
rng: &mut R,
) -> Result<Vec<M::EncryptedType>, crate::transcryptor::BatchError>
where
M: Encryptable,
GlobalPublicKeys: KeyProvider<M::GlobalPublicKeyType>,
R: Rng + CryptoRng,
{
super::batch::encrypt_global_batch(messages, self.global_public_keys.get_key(), rng)
}
}