trelent_hyok/held_data/encryption/custom.rs
1//! Custom encryption strategy implementation.
2//!
3//! This module enables integration with any encryption algorithm by allowing
4//! users to provide their own encryption and decryption functions. It supports:
5//!
6//! - Custom encryption algorithms
7//! - Flexible metadata handling
8//! - Error handling
9//! - Thread-safe operations
10
11use crate::dek::DEK;
12use crate::error::encryption::EncryptionError;
13use crate::held_data::encryption::EncryptionStrategy;
14use async_trait::async_trait;
15use std::sync::Arc;
16
17/// A customizable encryption strategy that accepts user-defined functions.
18///
19/// This strategy allows integration with any encryption algorithm by accepting
20/// closures for encryption and decryption. Features include:
21///
22/// - Flexible algorithm choice
23/// - Custom metadata handling
24/// - Error handling
25/// - Thread-safe operations
26///
27/// # Security
28///
29/// When implementing custom operations, ensure:
30/// - Strong encryption algorithms
31/// - Proper key handling
32/// - Secure metadata management
33/// - Authentication where needed
34///
35/// # Example
36/// ```no_run
37/// use hyokashi::{CustomStrategy, EncryptionError, DEK};
38///
39/// // Define custom encryption/decryption functions
40/// let encrypt = |dek: DEK, data: Vec<u8>, metadata: Vec<u8>| {
41/// Box::pin(async move {
42/// // Implement secure encryption...
43/// Ok(data)
44/// })
45/// };
46///
47/// let decrypt = |dek: DEK, data: Vec<u8>, metadata: Vec<u8>| {
48/// Box::pin(async move {
49/// // Implement secure decryption...
50/// Ok(data)
51/// })
52/// };
53///
54/// // Create the custom strategy
55/// let strategy = CustomStrategy::new(encrypt, decrypt);
56///
57/// // Use the strategy
58/// let dek = DEK::new(vec![1, 2, 3]);
59/// let data = vec![4, 5, 6];
60/// let metadata = vec![7, 8, 9];
61///
62/// let encrypted = strategy.encrypt(dek.clone(), data.clone(), metadata.clone()).await?;
63/// let decrypted = strategy.decrypt(dek, encrypted, metadata).await?;
64/// assert_eq!(data, decrypted);
65///
66/// ```
67pub struct CustomStrategy<ED: Send> {
68 encrypt_fn: Arc<
69 dyn (Fn(
70 DEK,
71 Vec<u8>,
72 ED
73 ) -> futures::future::BoxFuture<'static, Result<Vec<u8>, EncryptionError>>) +
74 Send +
75 Sync
76 >,
77 decrypt_fn: Arc<
78 dyn (Fn(
79 DEK,
80 Vec<u8>,
81 ED
82 ) -> futures::future::BoxFuture<'static, Result<Vec<u8>, EncryptionError>>) +
83 Send +
84 Sync
85 >,
86}
87
88impl<ED: Send> CustomStrategy<ED> {
89 /// Creates a new custom strategy with provided encryption and decryption functions
90 ///
91 /// # Arguments
92 /// * `encrypt_fn` - Async function for encryption
93 /// * `decrypt_fn` - Async function for decryption
94 ///
95 /// # Returns
96 /// * `Self` - New instance of CustomStrategy
97 pub fn new<E, D>(encrypt_fn: E, decrypt_fn: D) -> Self
98 where
99 E: Fn(
100 DEK,
101 Vec<u8>,
102 ED
103 ) -> futures::future::BoxFuture<'static, Result<Vec<u8>, EncryptionError>> +
104 Send +
105 Sync +
106 'static,
107 D: Fn(
108 DEK,
109 Vec<u8>,
110 ED
111 ) -> futures::future::BoxFuture<'static, Result<Vec<u8>, EncryptionError>> +
112 Send +
113 Sync +
114 'static
115 {
116 CustomStrategy {
117 encrypt_fn: Arc::new(encrypt_fn),
118 decrypt_fn: Arc::new(decrypt_fn),
119 }
120 }
121}
122
123/// Container for custom encryption metadata
124#[derive(Debug, Clone)]
125pub struct CustomEncryptionData {
126 /// Custom metadata for encryption/decryption operations
127 pub metadata: Vec<u8>,
128}
129
130#[async_trait]
131impl<ED: Send> EncryptionStrategy for CustomStrategy<ED> {
132 type EncryptionData = ED;
133
134 /// Encrypts data using the custom encryption function
135 ///
136 /// # Arguments
137 /// * `dek` - Data Encryption Key
138 /// * `plaintext` - Data to encrypt
139 /// * `encryption_data` - Custom encryption metadata
140 ///
141 /// # Returns
142 /// * `Result<Vec<u8>, EncryptionError>` - Encrypted data or error
143 async fn encrypt(
144 &self,
145 dek: DEK,
146 plaintext: Vec<u8>,
147 encryption_data: Self::EncryptionData
148 ) -> Result<Vec<u8>, EncryptionError> {
149 (self.encrypt_fn)(dek, plaintext, encryption_data).await
150 }
151
152 /// Decrypts data using the custom decryption function
153 ///
154 /// # Arguments
155 /// * `dek` - Data Encryption Key
156 /// * `ciphertext` - Encrypted data
157 /// * `encryption_data` - Custom encryption metadata
158 ///
159 /// # Returns
160 /// * `Result<Vec<u8>, EncryptionError>` - Decrypted data or error
161 async fn decrypt(
162 &self,
163 dek: DEK,
164 ciphertext: Vec<u8>,
165 encryption_data: Self::EncryptionData
166 ) -> Result<Vec<u8>, EncryptionError> {
167 (self.decrypt_fn)(dek, ciphertext, encryption_data).await
168 }
169}