Skip to content

Commit d864967

Browse files
authored
Merge pull request #54 from Cryptographic-API-Services/#52-thread-pool-implementation
#52 thread pool implementation
2 parents 6c66ba3 + 081f440 commit d864967

File tree

22 files changed

+1779
-1151
lines changed

22 files changed

+1779
-1151
lines changed

Cargo.toml

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,5 @@ crate-type = ["dylib"]
1313
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1414

1515
[dependencies]
16-
aes-gcm = "0.10.1"
17-
argon2 = "0.4.1"
18-
base64 = "0.20.0"
19-
bcrypt = "0.13.0"
20-
rand = "0.8.5"
21-
rand_07 = { package = "rand", version = "0.7.0" }
22-
rsa = "0.7.2"
23-
scrypt = "0.10.0"
24-
sha3 = "0.10.6"
25-
hmac = "0.12.1"
26-
sha2 = "0.10.6"
27-
blake2 = "0.10.6"
2816
libc = "0.2.146"
29-
rayon = "1.8.0"
30-
x25519-dalek = {version = "2.0.0", features = ["static_secrets"]}
31-
ascon-aead = "0.4.2"
32-
33-
[profile.dev.package.num-bigint-dig]
34-
opt-level = 3
35-
36-
[dependencies.ed25519-dalek]
37-
version = "1"
17+
cas-lib = "0.1.6"

src/aes.rs

Lines changed: 223 additions & 50 deletions
Large diffs are not rendered by default.

src/ascon_aead.rs

Lines changed: 124 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use std::ffi::{c_char, c_uchar, CStr, CString};
2-
3-
use ascon_aead::aead::{generic_array::GenericArray, Aead, AeadCore, KeyInit, OsRng};
4-
use ascon_aead::Ascon128;
2+
use cas_lib::sponges::ascon_aead::AsconAead;
3+
use cas_lib::sponges::cas_ascon_aead::{CASAsconAead};
54

65
#[repr(C)]
76
pub struct Ascon128EncryptResult {
@@ -15,39 +14,85 @@ pub struct Ascon128DecryptResult {
1514
length: usize,
1615
}
1716

17+
#[repr(C)]
18+
pub struct Ascon128Key {
19+
key: *mut c_uchar,
20+
length: usize
21+
}
22+
23+
#[repr(C)]
24+
pub struct Ascon128Nonce {
25+
nonce: *mut c_uchar,
26+
length: usize
27+
}
28+
1829
#[no_mangle]
19-
pub extern "C" fn ascon_128_key() -> *mut c_char {
20-
return CString::new(base64::encode(Ascon128::generate_key(&mut OsRng)))
21-
.unwrap()
22-
.into_raw();
30+
pub extern "C" fn ascon_128_key() -> Ascon128Key {
31+
let mut key = <AsconAead as CASAsconAead>::generate_key();
32+
let capacity = key.capacity();
33+
key.reserve_exact(capacity);
34+
let result = Ascon128Key {
35+
key: key.as_mut_ptr(),
36+
length: key.len()
37+
};
38+
std::mem::forget(key);
39+
result
2340
}
2441

2542
#[no_mangle]
26-
pub extern "C" fn ascon_128_nonce() -> *mut c_char {
27-
return CString::new(base64::encode(Ascon128::generate_nonce(&mut OsRng)))
28-
.unwrap()
29-
.into_raw();
43+
pub extern "C" fn ascon_128_key_threadpool() -> Ascon128Key {
44+
let mut key = <AsconAead as CASAsconAead>::generate_key_threadpool();
45+
let capacity = key.capacity();
46+
key.reserve_exact(capacity);
47+
let result = Ascon128Key {
48+
key: key.as_mut_ptr(),
49+
length: key.len()
50+
};
51+
std::mem::forget(key);
52+
result
53+
}
54+
55+
56+
57+
#[no_mangle]
58+
pub extern "C" fn ascon_128_nonce() -> Ascon128Nonce {
59+
let mut nonce = <AsconAead as CASAsconAead>::generate_nonce();
60+
let capacity = nonce.capacity();
61+
nonce.reserve_exact(capacity);
62+
let result = Ascon128Nonce {
63+
nonce: nonce.as_mut_ptr(),
64+
length: nonce.len()
65+
};
66+
std::mem::forget(nonce);
67+
result
68+
}
69+
70+
#[no_mangle]
71+
pub extern "C" fn ascon_128_nonce_threadpool() -> Ascon128Nonce {
72+
let mut nonce = <AsconAead as CASAsconAead>::generate_nonce_threadpool();
73+
let capacity = nonce.capacity();
74+
nonce.reserve_exact(capacity);
75+
let result = Ascon128Nonce {
76+
nonce: nonce.as_mut_ptr(),
77+
length: nonce.len()
78+
};
79+
std::mem::forget(nonce);
80+
result
3081
}
3182

3283
#[no_mangle]
3384
pub extern "C" fn ascon_128_encrypt(
34-
nonce_key: *const c_char,
35-
key: *const c_char,
85+
nonce_key: *const c_uchar,
86+
nonce_key_length: usize,
87+
key: *const c_uchar,
88+
key_length: usize,
3689
to_encrypt: *const c_uchar,
3790
to_encrypt_length: usize,
3891
) -> Ascon128EncryptResult {
39-
let nonce_key = unsafe { CStr::from_ptr(nonce_key) }.to_str().unwrap();
40-
let key = unsafe { CStr::from_ptr(key) }.to_str().unwrap();
41-
let to_encrypt = unsafe { std::slice::from_raw_parts(to_encrypt, to_encrypt_length) };
42-
43-
let decoded_nonce_key = base64::decode(nonce_key).unwrap();
44-
let decoded_key = base64::decode(key).unwrap();
45-
46-
let key = GenericArray::from_slice(&decoded_nonce_key);
47-
let nonce_key = GenericArray::from_slice(&decoded_key);
48-
49-
let cipher = Ascon128::new(key);
50-
let mut ciphertext = cipher.encrypt(&nonce_key, to_encrypt.as_ref()).unwrap();
92+
let nonce_key = unsafe { std::slice::from_raw_parts(nonce_key, nonce_key_length) }.to_vec();
93+
let key = unsafe { std::slice::from_raw_parts(key, key_length) }.to_vec();
94+
let to_encrypt = unsafe { std::slice::from_raw_parts(to_encrypt, to_encrypt_length) }.to_vec();
95+
let mut ciphertext = <AsconAead as CASAsconAead>::encrypt(key, nonce_key, to_encrypt);
5196
let capacity = ciphertext.capacity();
5297
ciphertext.reserve_exact(capacity);
5398
let result = Ascon128EncryptResult {
@@ -58,26 +103,66 @@ pub extern "C" fn ascon_128_encrypt(
58103
result
59104
}
60105

61-
62106
#[no_mangle]
63-
pub extern "C" fn ascon_128_decrypt(
64-
nonce_key: *const c_char,
65-
key: *const c_char,
107+
pub extern "C" fn ascon_128_encrypt_threadpool(
108+
nonce_key: *const c_uchar,
109+
nonce_key_length: usize,
110+
key: *const c_uchar,
111+
key_length: usize,
66112
to_encrypt: *const c_uchar,
67113
to_encrypt_length: usize,
68-
) -> Ascon128DecryptResult {
69-
let nonce_key = unsafe { CStr::from_ptr(nonce_key) }.to_str().unwrap();
70-
let key = unsafe { CStr::from_ptr(key) }.to_str().unwrap();
71-
let to_encrypt = unsafe { std::slice::from_raw_parts(to_encrypt, to_encrypt_length) };
114+
) -> Ascon128EncryptResult {
115+
let nonce_key = unsafe { std::slice::from_raw_parts(nonce_key, nonce_key_length) }.to_vec();
116+
let key = unsafe { std::slice::from_raw_parts(key, key_length) }.to_vec();
117+
let to_encrypt = unsafe { std::slice::from_raw_parts(to_encrypt, to_encrypt_length) }.to_vec();
118+
let mut ciphertext = <AsconAead as CASAsconAead>::encrypt_threadpool(key, nonce_key, to_encrypt);
119+
let capacity = ciphertext.capacity();
120+
ciphertext.reserve_exact(capacity);
121+
let result = Ascon128EncryptResult {
122+
ciphertext: ciphertext.as_mut_ptr(),
123+
length: ciphertext.len(),
124+
};
125+
std::mem::forget(ciphertext);
126+
result
127+
}
72128

73-
let decoded_nonce_key = base64::decode(nonce_key).unwrap();
74-
let decoded_key = base64::decode(key).unwrap();
75129

76-
let key = GenericArray::from_slice(&decoded_nonce_key);
77-
let nonce_key = GenericArray::from_slice(&decoded_key);
130+
#[no_mangle]
131+
pub extern "C" fn ascon_128_decrypt(
132+
nonce_key: *const c_uchar,
133+
nonce_key_length: usize,
134+
key: *const c_uchar,
135+
key_length: usize,
136+
to_decrypt: *const c_uchar,
137+
to_decrypt_length: usize,
138+
) -> Ascon128DecryptResult {
139+
let nonce_key = unsafe { std::slice::from_raw_parts(nonce_key, nonce_key_length) }.to_vec();
140+
let key = unsafe { std::slice::from_raw_parts(key, key_length) }.to_vec();
141+
let to_decrypt = unsafe { std::slice::from_raw_parts(to_decrypt, to_decrypt_length) }.to_vec();
142+
let mut plaintext = <AsconAead as CASAsconAead>::decrypt(key, nonce_key, to_decrypt);
143+
let capacity = plaintext.capacity();
144+
plaintext.reserve_exact(capacity);
145+
let result = Ascon128DecryptResult {
146+
plaintext: plaintext.as_mut_ptr(),
147+
length: plaintext.len(),
148+
};
149+
std::mem::forget(plaintext);
150+
result
151+
}
78152

79-
let cipher = Ascon128::new(key);
80-
let mut plaintext = cipher.decrypt(&nonce_key, to_encrypt.as_ref()).unwrap();
153+
#[no_mangle]
154+
pub extern "C" fn ascon_128_decrypt_threadpool(
155+
nonce_key: *const c_uchar,
156+
nonce_key_length: usize,
157+
key: *const c_uchar,
158+
key_length: usize,
159+
to_decrypt: *const c_uchar,
160+
to_decrypt_length: usize,
161+
) -> Ascon128DecryptResult {
162+
let nonce_key = unsafe { std::slice::from_raw_parts(nonce_key, nonce_key_length) }.to_vec();
163+
let key = unsafe { std::slice::from_raw_parts(key, key_length) }.to_vec();
164+
let to_decrypt = unsafe { std::slice::from_raw_parts(to_decrypt, to_decrypt_length) }.to_vec();
165+
let mut plaintext = <AsconAead as CASAsconAead>::decrypt_threadpool(key, nonce_key, to_decrypt);
81166
let capacity = plaintext.capacity();
82167
plaintext.reserve_exact(capacity);
83168
let result = Ascon128DecryptResult {

src/blake2.rs

Lines changed: 0 additions & 135 deletions
This file was deleted.

0 commit comments

Comments
 (0)