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
use rusoto_kms::Kms;
use std::{cell::RefCell, collections::HashMap, rc::Rc};
use tink_core::utils::wrap_err;
#[derive(Clone)]
pub struct AwsAead {
key_uri: String,
kms: rusoto_kms::KmsClient,
runtime: Rc<RefCell<tokio::runtime::Runtime>>,
}
impl AwsAead {
pub(crate) fn new(
key_uri: &str,
kms: rusoto_kms::KmsClient,
) -> Result<AwsAead, tink_core::TinkError> {
Ok(AwsAead {
key_uri: key_uri.to_string(),
kms,
runtime: Rc::new(RefCell::new(
tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.map_err(|e| wrap_err("failed to build tokio runtime", e))?,
)),
})
}
}
impl tink_core::Aead for AwsAead {
fn encrypt(
&self,
plaintext: &[u8],
additional_data: &[u8],
) -> Result<Vec<u8>, tink_core::TinkError> {
let ad = hex::encode(additional_data);
let encryption_context = if ad.is_empty() {
None
} else {
let mut context = HashMap::new();
context.insert("additionalData".to_string(), ad);
Some(context)
};
let req = rusoto_kms::EncryptRequest {
encryption_algorithm: None,
grant_tokens: None,
key_id: self.key_uri.clone(),
encryption_context,
plaintext: plaintext.to_vec().into(),
};
let rsp = self
.runtime
.borrow_mut()
.block_on(self.kms.encrypt(req))
.map_err(|e| wrap_err("request failed", e))?;
match rsp.ciphertext_blob {
None => Err("no ciphertext".into()),
Some(ct) => Ok(ct.to_vec()),
}
}
fn decrypt(
&self,
ciphertext: &[u8],
additional_data: &[u8],
) -> Result<Vec<u8>, tink_core::TinkError> {
let ad = hex::encode(additional_data);
let encryption_context = if ad.is_empty() {
None
} else {
let mut context = HashMap::new();
context.insert("additionalData".to_string(), ad);
Some(context)
};
let req = rusoto_kms::DecryptRequest {
ciphertext_blob: ciphertext.to_vec().into(),
encryption_algorithm: None,
encryption_context,
grant_tokens: None,
key_id: Some(self.key_uri.clone()),
};
let rsp = self
.runtime
.borrow_mut()
.block_on(self.kms.decrypt(req))
.map_err(|e| wrap_err("request failed", e))?;
if let Some(key_id) = rsp.key_id {
if key_id != self.key_uri {
return Err("decryption failed: wrong key id".into());
}
} else {
return Err("decryption failed: no key id".into());
}
match rsp.plaintext {
None => Err("no plaintext in response".into()),
Some(b) => Ok(b.to_vec()),
}
}
}