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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
use derive_more::Deref;
use engine_traits::EncryptionMethod as DBEncryptionMethod;
use kvproto::encryptionpb::EncryptionMethod;
use openssl::symm::{self, Cipher as OCipher};
use rand::{rngs::OsRng, RngCore};
use tikv_util::{box_err, impl_display_as_debug};
use crate::{Error, Result};
#[cfg(not(feature = "prost-codec"))]
pub fn encryption_method_to_db_encryption_method(method: EncryptionMethod) -> DBEncryptionMethod {
match method {
EncryptionMethod::Plaintext => DBEncryptionMethod::Plaintext,
EncryptionMethod::Aes128Ctr => DBEncryptionMethod::Aes128Ctr,
EncryptionMethod::Aes192Ctr => DBEncryptionMethod::Aes192Ctr,
EncryptionMethod::Aes256Ctr => DBEncryptionMethod::Aes256Ctr,
EncryptionMethod::Unknown => DBEncryptionMethod::Unknown,
}
}
pub fn encryption_method_from_db_encryption_method(method: DBEncryptionMethod) -> EncryptionMethod {
match method {
DBEncryptionMethod::Plaintext => EncryptionMethod::Plaintext,
DBEncryptionMethod::Aes128Ctr => EncryptionMethod::Aes128Ctr,
DBEncryptionMethod::Aes192Ctr => EncryptionMethod::Aes192Ctr,
DBEncryptionMethod::Aes256Ctr => EncryptionMethod::Aes256Ctr,
DBEncryptionMethod::Unknown => EncryptionMethod::Unknown,
}
}
#[cfg(not(feature = "prost-codec"))]
pub fn compat(method: EncryptionMethod) -> EncryptionMethod {
method
}
#[cfg(feature = "prost-codec")]
pub fn encryption_method_to_db_encryption_method(
method: i32,
) -> DBEncryptionMethod {
match method {
1 => DBEncryptionMethod::Plaintext,
2 => DBEncryptionMethod::Aes128Ctr,
3 => DBEncryptionMethod::Aes192Ctr,
4 => DBEncryptionMethod::Aes256Ctr,
_ => DBEncryptionMethod::Unknown,
}
}
#[cfg(feature = "prost-codec")]
pub fn compat(method: EncryptionMethod) -> i32 {
match method {
EncryptionMethod::Unknown => 0,
EncryptionMethod::Plaintext => 1,
EncryptionMethod::Aes128Ctr => 2,
EncryptionMethod::Aes192Ctr => 3,
EncryptionMethod::Aes256Ctr => 4,
}
}
pub fn get_method_key_length(method: EncryptionMethod) -> usize {
match method {
EncryptionMethod::Plaintext => 0,
EncryptionMethod::Aes128Ctr => 16,
EncryptionMethod::Aes192Ctr => 24,
EncryptionMethod::Aes256Ctr => 32,
unknown => panic!("bad EncryptionMethod {:?}", unknown),
}
}
const GCM_IV_12: usize = 12;
const CTR_IV_16: usize = 16;
#[derive(Debug, Clone, Copy)]
pub enum Iv {
Gcm([u8; GCM_IV_12]),
Ctr([u8; CTR_IV_16]),
}
impl Iv {
pub fn new_gcm() -> Iv {
let mut iv = [0u8; GCM_IV_12];
OsRng.fill_bytes(&mut iv);
Iv::Gcm(iv)
}
pub fn new_ctr() -> Iv {
let mut iv = [0u8; CTR_IV_16];
OsRng.fill_bytes(&mut iv);
Iv::Ctr(iv)
}
pub fn from_slice(src: &[u8]) -> Result<Iv> {
if src.len() == CTR_IV_16 {
let mut iv = [0; CTR_IV_16];
iv.copy_from_slice(src);
Ok(Iv::Ctr(iv))
} else if src.len() == GCM_IV_12 {
let mut iv = [0; GCM_IV_12];
iv.copy_from_slice(src);
Ok(Iv::Gcm(iv))
} else {
Err(box_err!(
"Nonce + Counter must be 12/16 bytes, {}",
src.len()
))
}
}
pub fn as_slice(&self) -> &[u8] {
match self {
Iv::Ctr(iv) => iv,
Iv::Gcm(iv) => iv,
}
}
}
const GCM_TAG_LEN: usize = 16;
pub struct AesGcmTag([u8; GCM_TAG_LEN]);
impl<'a> From<&'a [u8]> for AesGcmTag {
fn from(src: &'a [u8]) -> AesGcmTag {
assert!(src.len() >= GCM_TAG_LEN, "AES GCM tag must be 16 bytes");
let mut tag = [0; GCM_TAG_LEN];
tag.copy_from_slice(src);
AesGcmTag(tag)
}
}
impl AesGcmTag {
pub fn as_slice(&self) -> &[u8] {
&self.0
}
}
pub struct AesGcmCrypter<'k> {
iv: Iv,
key: &'k PlainKey,
}
impl<'k> AesGcmCrypter<'k> {
pub const KEY_LEN: usize = 32;
pub fn new(key: &'k PlainKey, iv: Iv) -> AesGcmCrypter<'k> {
AesGcmCrypter { iv, key }
}
pub fn encrypt(&self, pt: &[u8]) -> Result<(Vec<u8>, AesGcmTag)> {
let cipher = OCipher::aes_256_gcm();
let mut tag = AesGcmTag([0u8; GCM_TAG_LEN]);
let ciphertext = symm::encrypt_aead(
cipher,
&self.key.0,
Some(self.iv.as_slice()),
&[],
&pt,
&mut tag.0,
)?;
Ok((ciphertext, tag))
}
pub fn decrypt(&self, ct: &[u8], tag: AesGcmTag) -> Result<Vec<u8>> {
let cipher = OCipher::aes_256_gcm();
let plaintext = symm::decrypt_aead(
cipher,
&self.key.0,
Some(self.iv.as_slice()),
&[],
&ct,
&tag.0,
)?;
Ok(plaintext)
}
}
pub fn verify_encryption_config(method: EncryptionMethod, key: &[u8]) -> Result<()> {
if method == EncryptionMethod::Unknown {
return Err(Error::UnknownEncryption);
}
if method != EncryptionMethod::Plaintext {
let key_len = get_method_key_length(method);
if key.len() != key_len {
return Err(box_err!(
"unexpected key length, expected {} vs actual {}",
key_len,
key.len()
));
}
}
Ok(())
}
#[derive(Deref)]
pub struct PlainKey(Vec<u8>);
impl PlainKey {
pub fn new(key: Vec<u8>) -> Result<Self> {
if key.len() != AesGcmCrypter::KEY_LEN {
return Err(box_err!(
"encryption method and key length mismatch, expect {} get {}",
AesGcmCrypter::KEY_LEN,
key.len()
));
}
Ok(Self(key))
}
}
impl std::fmt::Debug for PlainKey {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_tuple("PlainKey")
.field(&"REDACTED".to_string())
.finish()
}
}
impl_display_as_debug!(PlainKey);
#[cfg(test)]
mod tests {
use hex::FromHex;
use super::*;
#[test]
fn test_iv() {
let mut ivs = Vec::with_capacity(100);
for c in 0..100 {
if c % 2 == 0 {
ivs.push(Iv::new_ctr());
} else {
ivs.push(Iv::new_gcm());
}
}
ivs.dedup_by(|a, b| a.as_slice() == b.as_slice());
assert_eq!(ivs.len(), 100);
for iv in ivs {
let iv1 = Iv::from_slice(iv.as_slice()).unwrap();
assert_eq!(iv.as_slice(), iv1.as_slice());
}
}
#[test]
fn test_ase_256_gcm() {
let pt = "25431587e9ecffc7c37f8d6d52a9bc3310651d46fb0e3bad2726c8f2db653749";
let ct = "84e5f23f95648fa247cb28eef53abec947dbf05ac953734618111583840bd980";
let key = "c3d99825f2181f4808acd2068eac7441a65bd428f14d2aab43fefc0129091139";
let iv = "cafabd9672ca6c79a2fbdc22";
let tag = "79651c875f7941793d42bbd0af1cce7c";
let pt = Vec::from_hex(pt).unwrap();
let ct = Vec::from_hex(ct).unwrap();
let key = PlainKey::new(Vec::from_hex(key).unwrap()).unwrap();
let iv = Iv::from_slice(Vec::from_hex(iv).unwrap().as_slice()).unwrap();
let tag = Vec::from_hex(tag).unwrap();
let crypter = AesGcmCrypter::new(&key, iv);
let (ciphertext, gcm_tag) = crypter.encrypt(&pt).unwrap();
assert_eq!(ciphertext, ct, "{}", hex::encode(&ciphertext));
assert_eq!(gcm_tag.0.to_vec(), tag, "{}", hex::encode(&gcm_tag.0));
let plaintext = crypter.decrypt(&ct, gcm_tag).unwrap();
assert_eq!(plaintext, pt, "{}", hex::encode(&plaintext));
crypter
.decrypt(&ct, AesGcmTag([0u8; GCM_TAG_LEN]))
.unwrap_err();
}
}