dcrypt_api/traits/
symmetric.rs1use crate::Result;
4use dcrypt_internal::random::{CryptoRng, RngCore};
5use dcrypt_internal::zeroing::Zeroize;
6
7#[cfg(not(feature = "std"))]
8use alloc::vec::Vec;
9#[cfg(feature = "std")]
10use std::vec::Vec;
11
12pub trait Operation<T> {
14 fn execute(self) -> Result<T>;
16}
17
18pub trait EncryptOperation<'a, C: SymmetricCipher>: Operation<C::Ciphertext> {
20 fn with_nonce(self, nonce: &'a C::Nonce) -> Self;
22
23 fn with_aad(self, aad: &'a [u8]) -> Self;
25
26 fn encrypt(self, plaintext: &'a [u8]) -> Result<C::Ciphertext>;
28}
29
30pub trait DecryptOperation<'a, C: SymmetricCipher>: Operation<Vec<u8>> {
32 fn with_nonce(self, nonce: &'a C::Nonce) -> Self;
34
35 fn with_aad(self, aad: &'a [u8]) -> Self;
37
38 fn decrypt(self, ciphertext: &'a C::Ciphertext) -> Result<Vec<u8>>;
40}
41
42pub trait SymmetricCipher: Sized {
44 type Key: Zeroize + AsRef<[u8]> + AsMut<[u8]> + Clone;
46
47 type Nonce: AsRef<[u8]> + AsMut<[u8]> + Clone;
49
50 type Ciphertext: AsRef<[u8]> + AsMut<[u8]> + Clone;
52
53 type EncryptOperation<'a>: EncryptOperation<'a, Self>
55 where
56 Self: 'a;
57
58 type DecryptOperation<'a>: DecryptOperation<'a, Self>
60 where
61 Self: 'a;
62
63 fn name() -> &'static str;
65
66 fn encrypt(&self) -> Self::EncryptOperation<'_>;
68
69 fn decrypt(&self) -> Self::DecryptOperation<'_>;
71
72 fn generate_key<R: RngCore + CryptoRng>(rng: &mut R) -> Result<Self::Key>;
74
75 fn generate_nonce<R: RngCore + CryptoRng>(rng: &mut R) -> Result<Self::Nonce>;
77
78 fn derive_key_from_bytes(bytes: &[u8]) -> Result<Self::Key>;
80}