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
use bytes::Bytes;
use crypto::publickey::{self, ec_math_utils, Generator, Public, Random, Secret};
use ethereum_types::{H256, H512};
use jsonrpc_core::Error;
use rand::{rngs::OsRng, RngCore};
use std::collections::BTreeSet;
use tiny_keccak::Keccak;
use v1::{helpers::errors, types::EncryptedDocumentKey};
const INIT_VEC_LEN: usize = 16;
pub fn generate_document_key(
account_public: Public,
server_key_public: Public,
) -> Result<EncryptedDocumentKey, Error> {
let document_key = Random.generate();
let (common_point, encrypted_point) =
encrypt_secret(document_key.public(), &server_key_public)?;
let encrypted_key = publickey::ecies::encrypt(
&account_public,
&crypto::DEFAULT_MAC,
document_key.public().as_bytes(),
)
.map_err(errors::encryption)?;
Ok(EncryptedDocumentKey {
common_point: common_point.into(),
encrypted_point: encrypted_point.into(),
encrypted_key: encrypted_key.into(),
})
}
pub fn encrypt_document(key: Bytes, document: Bytes) -> Result<Bytes, Error> {
let key = into_document_key(key)?;
let iv = initialization_vector();
let mut encrypted_document = vec![0; document.len() + iv.len()];
{
let (mut encryption_buffer, iv_buffer) = encrypted_document.split_at_mut(document.len());
crypto::aes::encrypt_128_ctr(&key, &iv, &document, &mut encryption_buffer)
.map_err(errors::encryption)?;
iv_buffer.copy_from_slice(&iv);
}
Ok(encrypted_document)
}
pub fn decrypt_document(key: Bytes, mut encrypted_document: Bytes) -> Result<Bytes, Error> {
let encrypted_document_len = encrypted_document.len();
if encrypted_document_len < INIT_VEC_LEN {
return Err(errors::invalid_params(
"encrypted_document",
"invalid encrypted data",
));
}
let key = into_document_key(key)?;
let iv = encrypted_document.split_off(encrypted_document_len - INIT_VEC_LEN);
let mut document = vec![0; encrypted_document_len - INIT_VEC_LEN];
crypto::aes::decrypt_128_ctr(&key, &iv, &encrypted_document, &mut document)
.map_err(errors::encryption)?;
Ok(document)
}
pub fn decrypt_document_with_shadow(
decrypted_secret: Public,
common_point: Public,
shadows: Vec<Secret>,
encrypted_document: Bytes,
) -> Result<Bytes, Error> {
let key = decrypt_with_shadow_coefficients(decrypted_secret, common_point, shadows)?;
decrypt_document(key.as_bytes().to_vec(), encrypted_document)
}
pub fn ordered_servers_keccak(servers_set: BTreeSet<H512>) -> H256 {
let mut servers_set_keccak = Keccak::new_keccak256();
for server in servers_set {
servers_set_keccak.update(&server.0);
}
let mut servers_set_keccak_value = [0u8; 32];
servers_set_keccak.finalize(&mut servers_set_keccak_value);
servers_set_keccak_value.into()
}
fn into_document_key(key: Bytes) -> Result<Bytes, Error> {
if key.len() != 64 {
return Err(errors::invalid_params("key", "invalid public key length"));
}
Ok(key[..INIT_VEC_LEN].into())
}
fn initialization_vector() -> [u8; INIT_VEC_LEN] {
let mut result = [0u8; INIT_VEC_LEN];
let mut rng = OsRng;
rng.fill_bytes(&mut result);
result
}
fn decrypt_with_shadow_coefficients(
mut decrypted_shadow: Public,
mut common_shadow_point: Public,
shadow_coefficients: Vec<Secret>,
) -> Result<Public, Error> {
let mut shadow_coefficients_sum = shadow_coefficients[0].clone();
for shadow_coefficient in shadow_coefficients.iter().skip(1) {
shadow_coefficients_sum
.add(shadow_coefficient)
.map_err(errors::encryption)?;
}
ec_math_utils::public_mul_secret(&mut common_shadow_point, &shadow_coefficients_sum)
.map_err(errors::encryption)?;
ec_math_utils::public_add(&mut decrypted_shadow, &common_shadow_point)
.map_err(errors::encryption)?;
Ok(decrypted_shadow)
}
fn encrypt_secret(secret: &Public, joint_public: &Public) -> Result<(Public, Public), Error> {
let key_pair = Random.generate();
let mut common_point = ec_math_utils::generation_point();
ec_math_utils::public_mul_secret(&mut common_point, key_pair.secret())
.map_err(errors::encryption)?;
let mut encrypted_point = joint_public.clone();
ec_math_utils::public_mul_secret(&mut encrypted_point, key_pair.secret())
.map_err(errors::encryption)?;
ec_math_utils::public_add(&mut encrypted_point, secret).map_err(errors::encryption)?;
Ok((common_point, encrypted_point))
}
#[cfg(test)]
mod tests {
use super::{decrypt_document, decrypt_document_with_shadow, encrypt_document};
use bytes::Bytes;
use rustc_hex::FromHex;
#[test]
fn encrypt_and_decrypt_document() {
let document_key: Bytes = "cac6c205eb06c8308d65156ff6c862c62b000b8ead121a4455a8ddeff7248128d895692136f240d5d1614dc7cc4147b1bd584bd617e30560bb872064d09ea325".from_hex().unwrap();
let document: Bytes = b"Hello, world!!!"[..].into();
let encrypted_document = encrypt_document(document_key.clone(), document.clone()).unwrap();
assert!(document != encrypted_document);
let decrypted_document =
decrypt_document(document_key.clone(), encrypted_document).unwrap();
assert_eq!(decrypted_document, document);
}
#[test]
fn encrypt_and_shadow_decrypt_document() {
let document: Bytes = "deadbeef".from_hex().unwrap();
let encrypted_document = "2ddec1f96229efa2916988d8b2a82a47ef36f71c"
.from_hex()
.unwrap();
let decrypted_secret = "843645726384530ffb0c52f175278143b5a93959af7864460f5a4fec9afd1450cfb8aef63dec90657f43f55b13e0a73c7524d4e9a13c051b4e5f1e53f39ecd91".parse().unwrap();
let common_point = "07230e34ebfe41337d3ed53b186b3861751f2401ee74b988bba55694e2a6f60c757677e194be2e53c3523cc8548694e636e6acb35c4e8fdc5e29d28679b9b2f3".parse().unwrap();
let shadows = vec![
"46f542416216f66a7d7881f5a283d2a1ab7a87b381cbc5f29d0b093c7c89ee31"
.parse()
.unwrap(),
];
let decrypted_document = decrypt_document_with_shadow(
decrypted_secret,
common_point,
shadows,
encrypted_document,
)
.unwrap();
assert_eq!(decrypted_document, document);
}
}