1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
use crate::ring_encryption::KmsAeadRingAeadEncryption;
use crate::*;
use async_trait::*;
use ring::rand::SystemRandom;
use secret_vault_value::SecretValue;
use std::sync::Arc;
use tokio::sync::RwLock;
#[async_trait]
pub trait KmsAeadRingEncryptionProvider {
async fn encrypt_data_encryption_key(
&self,
encryption_key: &DataEncryptionKey,
) -> KmsAeadResult<EncryptedDataEncryptionKey>;
async fn decrypt_data_encryption_key(
&self,
encrypted_key: &EncryptedDataEncryptionKey,
) -> KmsAeadResult<DataEncryptionKey>;
async fn generate_encryption_key(
&self,
aead_encryption: &KmsAeadRingAeadEncryption,
) -> KmsAeadResult<DataEncryptionKey>;
}
pub struct KmsAeadRingEnvelopeEncryption<P>
where
P: KmsAeadRingEncryptionProvider + Send + Sync,
{
provider: P,
aead_encryption: KmsAeadRingAeadEncryption,
current_dek: Arc<RwLock<EncryptedDataEncryptionKey>>,
}
impl<P> KmsAeadRingEnvelopeEncryption<P>
where
P: KmsAeadRingEncryptionProvider + Send + Sync,
{
pub async fn new(provider: P) -> KmsAeadResult<Self> {
Self::with_algorithm(provider, &ring::aead::CHACHA20_POLY1305).await
}
pub async fn with_algorithm(
provider: P,
algo: &'static ring::aead::Algorithm,
) -> KmsAeadResult<Self> {
let secure_rand = SystemRandom::new();
let aead_encryption = KmsAeadRingAeadEncryption::with_algorithm(algo, secure_rand)?;
let dek = provider.generate_encryption_key(&aead_encryption).await?;
let current_encrypted_dek = provider.encrypt_data_encryption_key(&dek).await?;
Ok(Self {
provider,
aead_encryption,
current_dek: Arc::new(RwLock::new(current_encrypted_dek)),
})
}
}
#[async_trait]
impl<Aad, P> KmsAeadEnvelopeEncryption<Aad> for KmsAeadRingEnvelopeEncryption<P>
where
Aad: AsRef<[u8]> + Send + Sync + 'static,
P: KmsAeadRingEncryptionProvider + Send + Sync,
{
async fn encrypt_value(
&self,
aad: &Aad,
plain_text: &SecretValue,
) -> KmsAeadResult<CipherTextWithEncryptedKey> {
let (cipher_text, dek) = self.encrypt_value_with_new_key(aad, plain_text).await?;
Ok(CipherTextWithEncryptedKey::new(&cipher_text, &dek))
}
async fn decrypt_value(
&self,
aad: &Aad,
cipher_text: &CipherTextWithEncryptedKey,
) -> KmsAeadResult<SecretValue> {
let (cipher_text, dek) = cipher_text.separate()?;
self.decrypt_value_with_key(aad, &cipher_text, &dek).await
}
async fn encrypt_value_with_current_key(
&self,
aad: &Aad,
plain_text: &SecretValue,
) -> KmsAeadResult<(CipherText, EncryptedDataEncryptionKey)> {
let encrypted_current_dek = { self.current_dek.read().await.clone() };
let current_dek = self
.provider
.decrypt_data_encryption_key(&encrypted_current_dek)
.await?;
let cipher_text = self
.aead_encryption
.encrypt_value(aad, plain_text, ¤t_dek)
.await?;
Ok((cipher_text, encrypted_current_dek))
}
async fn encrypt_value_with_new_key(
&self,
aad: &Aad,
plain_text: &SecretValue,
) -> KmsAeadResult<(CipherText, EncryptedDataEncryptionKey)> {
let dek = self
.provider
.generate_encryption_key(&self.aead_encryption)
.await?;
let new_encrypted_dek = self.provider.encrypt_data_encryption_key(&dek).await?;
let cipher_text = self
.aead_encryption
.encrypt_value(aad, plain_text, &dek)
.await?;
Ok((cipher_text, new_encrypted_dek))
}
async fn decrypt_value_with_current_key(
&self,
aad: &Aad,
cipher_text: &CipherText,
) -> KmsAeadResult<(SecretValue, EncryptedDataEncryptionKey)> {
let current_dek = { self.current_dek.read().await.clone() };
let cipher_text = self
.decrypt_value_with_key(aad, cipher_text, ¤t_dek)
.await?;
Ok((cipher_text, current_dek))
}
async fn decrypt_value_with_key(
&self,
aad: &Aad,
cipher_text: &CipherText,
encrypted_data_encryption_key: &EncryptedDataEncryptionKey,
) -> KmsAeadResult<SecretValue> {
let dek = self
.provider
.decrypt_data_encryption_key(encrypted_data_encryption_key)
.await?;
self.aead_encryption
.decrypt_value(aad, cipher_text, &dek)
.await
}
async fn rotate_current_key(
&self,
) -> KmsAeadResult<(EncryptedDataEncryptionKey, EncryptedDataEncryptionKey)> {
let dek = self
.provider
.generate_encryption_key(&self.aead_encryption)
.await?;
let new_encrypted_dek = self.provider.encrypt_data_encryption_key(&dek).await?;
let previous_encrypted_key = {
let mut write_current_dek = self.current_dek.write().await;
let previous_encrypted_dek = write_current_dek.clone();
*write_current_dek = new_encrypted_dek.clone();
previous_encrypted_dek
};
Ok((previous_encrypted_key, new_encrypted_dek))
}
}