Repository navigation
Expand file tree
/
Copy pathaes.rs
More file actions
389 lines (348 loc) · 13.3 KB
/
Copy pathaes.rs
File metadata and controls
389 lines (348 loc) · 13.3 KB
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
use cas_lib::symmetric::{aes::{CASAES128, CASAES256}, cas_symmetric_encryption::{CASAES128Encryption, CASAES256Encryption}};
use std::ffi::c_uchar;
use crate::{helpers::cas_error_code, x25519};
#[repr(C)]
pub struct AesNonce {
pub nonce: *mut c_uchar,
pub length: usize
}
#[repr(C)]
pub struct AesKeyResult {
pub key: *mut c_uchar,
pub length: usize
}
#[repr(C)]
pub struct AesBytesEncrypt {
pub ciphertext: *mut c_uchar,
pub length: usize,
pub error_code: i32,
}
#[repr(C)]
pub struct AesBytesDecrypt {
pub plaintext: *mut c_uchar,
pub length: usize,
pub error_code: i32,
}
#[repr(C)]
pub struct AesNonceAndKeyFromX25519DiffieHellman {
pub aes_key_ptr: *mut c_uchar,
pub aes_key_ptr_length: usize,
pub error_code: i32,
}
#[no_mangle]
pub extern "C" fn aes_256_key_from_x25519_diffie_hellman_shared_secret(
shared_secret: *const c_uchar,
shared_secret_length: usize,
) -> AesNonceAndKeyFromX25519DiffieHellman {
let shared_secret_slice: Vec<u8> =
unsafe { std::slice::from_raw_parts(shared_secret, shared_secret_length) }.to_vec();
match <CASAES256 as CASAES256Encryption>::key_from_vec(shared_secret_slice) {
Ok(mut aes_key) => {
let aes_key_capacity = aes_key.capacity();
aes_key.reserve_exact(aes_key_capacity);
let result = AesNonceAndKeyFromX25519DiffieHellman {
aes_key_ptr: aes_key.as_mut_ptr(),
aes_key_ptr_length: aes_key.len(),
error_code: 0,
};
std::mem::forget(aes_key);
result
}
Err(e) => AesNonceAndKeyFromX25519DiffieHellman {
aes_key_ptr: std::ptr::null_mut(),
aes_key_ptr_length: 0,
error_code: cas_error_code(&e),
},
}
}
#[test]
pub fn aes_256_key_and_nonce_from_x25519_diffie_hellman_shared_secret_test() {
let alice_secret_and_public_key = x25519::generate_secret_and_public_key();
let bob_secret_and_public_key = x25519::generate_secret_and_public_key();
let alice_shared_secret = x25519::diffie_hellman(
alice_secret_and_public_key.secret_key,
alice_secret_and_public_key.secret_key_length,
bob_secret_and_public_key.public_key,
bob_secret_and_public_key.public_key_length,
);
let bob_shared_secret = x25519::diffie_hellman(
bob_secret_and_public_key.secret_key,
bob_secret_and_public_key.secret_key_length,
alice_secret_and_public_key.public_key,
alice_secret_and_public_key.public_key_length,
);
let alice_shared_secret_slice: &[u8] = unsafe {
std::slice::from_raw_parts(
alice_shared_secret.shared_secret,
alice_shared_secret.shared_secret_length,
)
};
let bob_shared_secret_slice: &[u8] = unsafe {
std::slice::from_raw_parts(
bob_shared_secret.shared_secret,
bob_shared_secret.shared_secret_length,
)
};
assert_eq!(alice_shared_secret_slice, bob_shared_secret_slice);
let alice_secret_key: *const std::os::raw::c_uchar = alice_shared_secret.shared_secret;
let alice_aes = aes_256_key_from_x25519_diffie_hellman_shared_secret(
alice_secret_key,
alice_shared_secret.shared_secret_length,
);
let bob_aes = aes_256_key_from_x25519_diffie_hellman_shared_secret(
bob_shared_secret.shared_secret,
bob_shared_secret.shared_secret_length,
);
let password = "DontUseThisPassword";
let password_cstr = password.as_bytes();
let password_ptr = password.as_ptr();
let nonce = CASAES256::generate_nonce();
let nonce_ptr = nonce.as_ptr();
let cipher_text_result = aes_256_encrypt_bytes_with_key(nonce_ptr, nonce.len(), alice_aes.aes_key_ptr, alice_aes.aes_key_ptr_length, password_ptr, password_cstr.len());
let plain_text_result = aes_256_decrypt_bytes_with_key(nonce_ptr, nonce.len(), bob_aes.aes_key_ptr, bob_aes.aes_key_ptr_length, cipher_text_result.ciphertext, cipher_text_result.length);
let plain_text_result_slice: &[u8] = unsafe {
std::slice::from_raw_parts(
plain_text_result.plaintext,
plain_text_result.length,
)
};
assert_eq!(password_cstr, plain_text_result_slice);
}
#[no_mangle]
pub extern "C" fn aes_128_key_from_x25519_diffie_hellman_shared_secret(
shared_secret: *const c_uchar,
shared_secret_length: usize,
) -> AesNonceAndKeyFromX25519DiffieHellman {
let shared_secret_slice: Vec<u8> =
unsafe { std::slice::from_raw_parts(shared_secret, shared_secret_length) }.to_vec();
match <CASAES128 as CASAES128Encryption>::key_from_x25519_shared_secret(shared_secret_slice) {
Ok(mut aes_key) => {
let result = AesNonceAndKeyFromX25519DiffieHellman {
aes_key_ptr: aes_key.as_mut_ptr(),
aes_key_ptr_length: aes_key.len(),
error_code: 0,
};
std::mem::forget(aes_key);
result
}
Err(e) => AesNonceAndKeyFromX25519DiffieHellman {
aes_key_ptr: std::ptr::null_mut(),
aes_key_ptr_length: 0,
error_code: cas_error_code(&e),
},
}
}
#[test]
pub fn aes_128_key_from_x25519_diffie_hellman_shared_secret_test() {
let alice_secret_and_public_key = x25519::generate_secret_and_public_key();
let bob_secret_and_public_key = x25519::generate_secret_and_public_key();
let alice_shared_secret = x25519::diffie_hellman(
alice_secret_and_public_key.secret_key,
alice_secret_and_public_key.secret_key_length,
bob_secret_and_public_key.public_key,
bob_secret_and_public_key.public_key_length,
);
let bob_shared_secret = x25519::diffie_hellman(
bob_secret_and_public_key.secret_key,
bob_secret_and_public_key.secret_key_length,
alice_secret_and_public_key.public_key,
alice_secret_and_public_key.public_key_length,
);
let alice_shared_secret_slice: &[u8] = unsafe {
std::slice::from_raw_parts(
alice_shared_secret.shared_secret,
alice_shared_secret.shared_secret_length,
)
};
let bob_shared_secret_slice: &[u8] = unsafe {
std::slice::from_raw_parts(
bob_shared_secret.shared_secret,
bob_shared_secret.shared_secret_length,
)
};
assert_eq!(alice_shared_secret_slice, bob_shared_secret_slice);
let alice_secret_key: *const std::os::raw::c_uchar = alice_shared_secret.shared_secret;
let alice_aes = aes_128_key_from_x25519_diffie_hellman_shared_secret(
alice_secret_key,
alice_shared_secret.shared_secret_length,
);
let bob_aes = aes_128_key_from_x25519_diffie_hellman_shared_secret(
bob_shared_secret.shared_secret,
bob_shared_secret.shared_secret_length,
);
let password = "DontUseThisPassword";
let password_cstr = password.as_bytes();
let password_ptr = password.as_ptr();
let nonce = CASAES128::generate_nonce();
let nonce_ptr = nonce.as_ptr();
let cipher_text_result = aes_128_encrypt_bytes_with_key(nonce_ptr, nonce.len(), alice_aes.aes_key_ptr, alice_aes.aes_key_ptr_length, password_ptr, password_cstr.len());
let plain_text_result = aes_128_decrypt_bytes_with_key(nonce_ptr, nonce.len(), bob_aes.aes_key_ptr, bob_aes.aes_key_ptr_length, cipher_text_result.ciphertext, cipher_text_result.length);
let plain_text_result_slice: &[u8] = unsafe {
std::slice::from_raw_parts(
plain_text_result.plaintext,
plain_text_result.length,
)
};
assert_eq!(password_cstr, plain_text_result_slice);
}
#[no_mangle]
pub extern "C" fn aes_nonce() -> AesNonce {
let mut random_bytes = <CASAES256 as CASAES256Encryption>::generate_nonce();
let capacity = random_bytes.capacity();
random_bytes.reserve_exact(capacity);
let result = AesNonce {
nonce: random_bytes.as_mut_ptr(),
length: random_bytes.len()
};
std::mem::forget(random_bytes);
result
}
#[no_mangle]
pub extern "C" fn aes_256_key() -> AesKeyResult {
let mut key = <CASAES256 as CASAES256Encryption>::generate_key();
let capacity = key.capacity();
key.reserve_exact(capacity);
let result = AesKeyResult {
key: key.as_mut_ptr(),
length: key.len()
};
std::mem::forget(key);
result
}
#[no_mangle]
pub extern "C" fn aes_128_key() -> AesKeyResult {
let mut key = <CASAES128 as CASAES128Encryption>::generate_key();
let capacity = key.capacity();
key.reserve_exact(capacity);
let result = AesKeyResult {
key: key.as_mut_ptr(),
length: key.len()
};
std::mem::forget(key);
result
}
#[no_mangle]
pub extern "C" fn aes_128_encrypt_bytes_with_key(
nonce_key: *const c_uchar,
nonce_key_length: usize,
key: *const c_uchar,
key_length: usize,
to_encrypt: *const c_uchar,
to_encrypt_length: usize,
) -> AesBytesEncrypt {
let nonce_slice: Vec<u8> = unsafe { std::slice::from_raw_parts(nonce_key, nonce_key_length) }.to_vec();
let key_slice: Vec<u8> = unsafe {std::slice::from_raw_parts(key, key_length)}.to_vec();
let to_encrypt_slice: Vec<u8> =
unsafe { std::slice::from_raw_parts(to_encrypt, to_encrypt_length) }.to_vec();
match <CASAES128 as CASAES128Encryption>::encrypt_plaintext(key_slice, nonce_slice, to_encrypt_slice) {
Ok(mut ciphertext) => {
let capacity = ciphertext.capacity();
ciphertext.reserve_exact(capacity);
let result = AesBytesEncrypt {
ciphertext: ciphertext.as_mut_ptr(),
length: ciphertext.len(),
error_code: 0,
};
std::mem::forget(ciphertext);
result
}
Err(e) => AesBytesEncrypt {
ciphertext: std::ptr::null_mut(),
length: 0,
error_code: cas_error_code(&e),
},
}
}
#[no_mangle]
pub extern "C" fn aes_256_encrypt_bytes_with_key(
nonce_key: *const c_uchar,
nonce_key_length: usize,
key: *const c_uchar,
key_length: usize,
to_encrypt: *const c_uchar,
to_encrypt_length: usize,
) -> AesBytesEncrypt {
let nonce_slice = unsafe { std::slice::from_raw_parts(nonce_key, nonce_key_length) }.to_vec();
let key_slice = unsafe {std::slice::from_raw_parts(key, key_length)}.to_vec();
let to_encrypt_slice = unsafe { std::slice::from_raw_parts(to_encrypt, to_encrypt_length) }.to_vec();
match <CASAES256 as CASAES256Encryption>::encrypt_plaintext(key_slice, nonce_slice, to_encrypt_slice) {
Ok(mut ciphertext) => {
let capacity = ciphertext.capacity();
ciphertext.reserve_exact(capacity);
let result = AesBytesEncrypt {
ciphertext: ciphertext.as_mut_ptr(),
length: ciphertext.len(),
error_code: 0,
};
std::mem::forget(ciphertext);
result
}
Err(e) => AesBytesEncrypt {
ciphertext: std::ptr::null_mut(),
length: 0,
error_code: cas_error_code(&e),
},
}
}
#[no_mangle]
pub extern "C" fn aes_128_decrypt_bytes_with_key(
nonce_key: *const c_uchar,
nonce_key_length: usize,
key: *const c_uchar,
key_length: usize,
to_decrypt: *const c_uchar,
to_decrypt_length: usize,
) -> AesBytesDecrypt {
let nonce_slice = unsafe { std::slice::from_raw_parts(nonce_key, nonce_key_length) }.to_vec();
let key_slice = unsafe {std::slice::from_raw_parts(key, key_length)}.to_vec();
let to_decrypt_slice = unsafe { std::slice::from_raw_parts(to_decrypt, to_decrypt_length) }.to_vec();
match <CASAES128 as CASAES128Encryption>::decrypt_ciphertext(key_slice, nonce_slice, to_decrypt_slice) {
Ok(mut plaintext) => {
let capacity = plaintext.capacity();
plaintext.reserve_exact(capacity);
let result = AesBytesDecrypt {
plaintext: plaintext.as_mut_ptr(),
length: plaintext.len(),
error_code: 0,
};
std::mem::forget(plaintext);
result
}
Err(e) => AesBytesDecrypt {
plaintext: std::ptr::null_mut(),
length: 0,
error_code: cas_error_code(&e),
},
}
}
#[no_mangle]
pub extern "C" fn aes_256_decrypt_bytes_with_key(
nonce_key: *const c_uchar,
nonce_key_length: usize,
key: *const c_uchar,
key_length: usize,
to_decrypt: *const c_uchar,
to_decrypt_length: usize,
) -> AesBytesDecrypt {
let nonce_slice = unsafe { std::slice::from_raw_parts(nonce_key, nonce_key_length) }.to_vec();
let key_slice = unsafe {std::slice::from_raw_parts(key, key_length)}.to_vec();
let to_decrypt_slice = unsafe { std::slice::from_raw_parts(to_decrypt, to_decrypt_length) }.to_vec();
match <CASAES256 as CASAES256Encryption>::decrypt_ciphertext(key_slice, nonce_slice, to_decrypt_slice) {
Ok(mut plaintext) => {
let capacity = plaintext.capacity();
plaintext.reserve_exact(capacity);
let result = AesBytesDecrypt {
plaintext: plaintext.as_mut_ptr(),
length: plaintext.len(),
error_code: 0,
};
std::mem::forget(plaintext);
result
}
Err(e) => AesBytesDecrypt {
plaintext: std::ptr::null_mut(),
length: 0,
error_code: cas_error_code(&e),
},
}
}