secured_enclave/
traits.rs

1use secured_cipher::{Key, KeyDerivationStrategy};
2
3use crate::EnclaveError;
4
5/// The `Encryptable` trait provides a common interface for encryption operations.
6/// It is implemented by types that can be encrypted.
7///
8/// # Type Parameters
9/// * `KEY_SIZE` - The size of the key to be used for encryption.
10///
11/// # Methods
12/// * `encrypt` - Encrypts the type using a specified password and key derivation strategy.
13/// * `encrypt_with_key` - Encrypts the type using a specified key.
14/// * `encrypt_with_metadata` - Encrypts the type using a specified key and metadata.
15pub trait Encryptable<const KEY_SIZE: usize> {
16  fn encrypt(&self, password: String, strategy: KeyDerivationStrategy) -> Vec<u8>;
17
18  fn encrypt_with_key(&self, key: &Key<KEY_SIZE, 16>) -> Vec<u8>;
19
20  fn encrypt_with_raw_key(&self, key: [u8; KEY_SIZE]) -> Vec<u8>;
21
22  fn encrypt_with_metadata<T>(&self, key: [u8; KEY_SIZE], metadata: T) -> Vec<u8>
23  where
24    T: From<Vec<u8>> + Into<Vec<u8>> + Clone;
25}
26
27/// The `Decryptable` trait provides a common interface for decryption operations.
28/// It is implemented by types that can be decrypted.
29///
30/// # Type Parameters
31/// * `KEY_SIZE` - The size of the key to be used for decryption.
32///
33/// # Methods
34/// * `decrypt` - Decrypts the type using a specified password.
35/// * `decrypt_with_key` - Decrypts the type using a specified key.
36/// * `decrypt_with_metadata` - Decrypts the type using a specified key and metadata.
37pub trait Decryptable<const KEY_SIZE: usize> {
38  fn decrypt(&self, password: String) -> Result<Vec<u8>, EnclaveError>;
39
40  fn decrypt_with_key(&self, key: [u8; KEY_SIZE]) -> Result<Vec<u8>, EnclaveError>;
41
42  fn decrypt_with_metadata<T>(&self, key: [u8; KEY_SIZE]) -> Result<(Vec<u8>, T), EnclaveError>
43  where
44    T: TryFrom<Vec<u8>> + Into<Vec<u8>> + Clone;
45}