dcrypt_api/traits/
symmetric.rs1use crate::Result;
4use rand::{CryptoRng, RngCore};
5use zeroize::Zeroize;
6
7pub trait Operation<T> {
9 fn execute(self) -> Result<T>;
11}
12
13pub trait EncryptOperation<'a, C: SymmetricCipher>: Operation<C::Ciphertext> {
15 fn with_nonce(self, nonce: &'a C::Nonce) -> Self;
17
18 fn with_aad(self, aad: &'a [u8]) -> Self;
20
21 fn encrypt(self, plaintext: &'a [u8]) -> Result<C::Ciphertext>;
23}
24
25pub trait DecryptOperation<'a, C: SymmetricCipher>: Operation<Vec<u8>> {
27 fn with_nonce(self, nonce: &'a C::Nonce) -> Self;
29
30 fn with_aad(self, aad: &'a [u8]) -> Self;
32
33 fn decrypt(self, ciphertext: &'a C::Ciphertext) -> Result<Vec<u8>>;
35}
36
37pub trait SymmetricCipher: Sized {
39 type Key: Zeroize + AsRef<[u8]> + AsMut<[u8]> + Clone;
41
42 type Nonce: AsRef<[u8]> + AsMut<[u8]> + Clone;
44
45 type Ciphertext: AsRef<[u8]> + AsMut<[u8]> + Clone;
47
48 type EncryptOperation<'a>: EncryptOperation<'a, Self>
50 where
51 Self: 'a;
52
53 type DecryptOperation<'a>: DecryptOperation<'a, Self>
55 where
56 Self: 'a;
57
58 fn name() -> &'static str;
60
61 fn encrypt(&self) -> Self::EncryptOperation<'_>;
63
64 fn decrypt(&self) -> Self::DecryptOperation<'_>;
66
67 fn generate_key<R: RngCore + CryptoRng>(rng: &mut R) -> Result<Self::Key>;
69
70 fn generate_nonce<R: RngCore + CryptoRng>(rng: &mut R) -> Result<Self::Nonce>;
72
73 fn derive_key_from_bytes(bytes: &[u8]) -> Result<Self::Key>;
75}