Skip to content

Commit

Permalink
Merge pull request #54 from Cryptographic-API-Services/#52-thread-poo…
Browse files Browse the repository at this point in the history
…l-implementation

#52 thread pool implementation
  • Loading branch information
WingZer0o authored May 26, 2024
2 parents 6c66ba3 + 081f440 commit d864967
Show file tree
Hide file tree
Showing 22 changed files with 1,779 additions and 1,151 deletions.
22 changes: 1 addition & 21 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,5 @@ crate-type = ["dylib"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
aes-gcm = "0.10.1"
argon2 = "0.4.1"
base64 = "0.20.0"
bcrypt = "0.13.0"
rand = "0.8.5"
rand_07 = { package = "rand", version = "0.7.0" }
rsa = "0.7.2"
scrypt = "0.10.0"
sha3 = "0.10.6"
hmac = "0.12.1"
sha2 = "0.10.6"
blake2 = "0.10.6"
libc = "0.2.146"
rayon = "1.8.0"
x25519-dalek = {version = "2.0.0", features = ["static_secrets"]}
ascon-aead = "0.4.2"

[profile.dev.package.num-bigint-dig]
opt-level = 3

[dependencies.ed25519-dalek]
version = "1"
cas-lib = "0.1.6"
273 changes: 223 additions & 50 deletions src/aes.rs

Large diffs are not rendered by default.

163 changes: 124 additions & 39 deletions src/ascon_aead.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::ffi::{c_char, c_uchar, CStr, CString};

use ascon_aead::aead::{generic_array::GenericArray, Aead, AeadCore, KeyInit, OsRng};
use ascon_aead::Ascon128;
use cas_lib::sponges::ascon_aead::AsconAead;
use cas_lib::sponges::cas_ascon_aead::{CASAsconAead};

#[repr(C)]
pub struct Ascon128EncryptResult {
Expand All @@ -15,39 +14,85 @@ pub struct Ascon128DecryptResult {
length: usize,
}

#[repr(C)]
pub struct Ascon128Key {
key: *mut c_uchar,
length: usize
}

#[repr(C)]
pub struct Ascon128Nonce {
nonce: *mut c_uchar,
length: usize
}

#[no_mangle]
pub extern "C" fn ascon_128_key() -> *mut c_char {
return CString::new(base64::encode(Ascon128::generate_key(&mut OsRng)))
.unwrap()
.into_raw();
pub extern "C" fn ascon_128_key() -> Ascon128Key {
let mut key = <AsconAead as CASAsconAead>::generate_key();
let capacity = key.capacity();
key.reserve_exact(capacity);
let result = Ascon128Key {
key: key.as_mut_ptr(),
length: key.len()
};
std::mem::forget(key);
result
}

#[no_mangle]
pub extern "C" fn ascon_128_nonce() -> *mut c_char {
return CString::new(base64::encode(Ascon128::generate_nonce(&mut OsRng)))
.unwrap()
.into_raw();
pub extern "C" fn ascon_128_key_threadpool() -> Ascon128Key {
let mut key = <AsconAead as CASAsconAead>::generate_key_threadpool();
let capacity = key.capacity();
key.reserve_exact(capacity);
let result = Ascon128Key {
key: key.as_mut_ptr(),
length: key.len()
};
std::mem::forget(key);
result
}



#[no_mangle]
pub extern "C" fn ascon_128_nonce() -> Ascon128Nonce {
let mut nonce = <AsconAead as CASAsconAead>::generate_nonce();
let capacity = nonce.capacity();
nonce.reserve_exact(capacity);
let result = Ascon128Nonce {
nonce: nonce.as_mut_ptr(),
length: nonce.len()
};
std::mem::forget(nonce);
result
}

#[no_mangle]
pub extern "C" fn ascon_128_nonce_threadpool() -> Ascon128Nonce {
let mut nonce = <AsconAead as CASAsconAead>::generate_nonce_threadpool();
let capacity = nonce.capacity();
nonce.reserve_exact(capacity);
let result = Ascon128Nonce {
nonce: nonce.as_mut_ptr(),
length: nonce.len()
};
std::mem::forget(nonce);
result
}

#[no_mangle]
pub extern "C" fn ascon_128_encrypt(
nonce_key: *const c_char,
key: *const c_char,
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,
) -> Ascon128EncryptResult {
let nonce_key = unsafe { CStr::from_ptr(nonce_key) }.to_str().unwrap();
let key = unsafe { CStr::from_ptr(key) }.to_str().unwrap();
let to_encrypt = unsafe { std::slice::from_raw_parts(to_encrypt, to_encrypt_length) };

let decoded_nonce_key = base64::decode(nonce_key).unwrap();
let decoded_key = base64::decode(key).unwrap();

let key = GenericArray::from_slice(&decoded_nonce_key);
let nonce_key = GenericArray::from_slice(&decoded_key);

let cipher = Ascon128::new(key);
let mut ciphertext = cipher.encrypt(&nonce_key, to_encrypt.as_ref()).unwrap();
let nonce_key = unsafe { std::slice::from_raw_parts(nonce_key, nonce_key_length) }.to_vec();
let key = unsafe { std::slice::from_raw_parts(key, key_length) }.to_vec();
let to_encrypt = unsafe { std::slice::from_raw_parts(to_encrypt, to_encrypt_length) }.to_vec();
let mut ciphertext = <AsconAead as CASAsconAead>::encrypt(key, nonce_key, to_encrypt);
let capacity = ciphertext.capacity();
ciphertext.reserve_exact(capacity);
let result = Ascon128EncryptResult {
Expand All @@ -58,26 +103,66 @@ pub extern "C" fn ascon_128_encrypt(
result
}


#[no_mangle]
pub extern "C" fn ascon_128_decrypt(
nonce_key: *const c_char,
key: *const c_char,
pub extern "C" fn ascon_128_encrypt_threadpool(
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,
) -> Ascon128DecryptResult {
let nonce_key = unsafe { CStr::from_ptr(nonce_key) }.to_str().unwrap();
let key = unsafe { CStr::from_ptr(key) }.to_str().unwrap();
let to_encrypt = unsafe { std::slice::from_raw_parts(to_encrypt, to_encrypt_length) };
) -> Ascon128EncryptResult {
let nonce_key = unsafe { std::slice::from_raw_parts(nonce_key, nonce_key_length) }.to_vec();
let key = unsafe { std::slice::from_raw_parts(key, key_length) }.to_vec();
let to_encrypt = unsafe { std::slice::from_raw_parts(to_encrypt, to_encrypt_length) }.to_vec();
let mut ciphertext = <AsconAead as CASAsconAead>::encrypt_threadpool(key, nonce_key, to_encrypt);
let capacity = ciphertext.capacity();
ciphertext.reserve_exact(capacity);
let result = Ascon128EncryptResult {
ciphertext: ciphertext.as_mut_ptr(),
length: ciphertext.len(),
};
std::mem::forget(ciphertext);
result
}

let decoded_nonce_key = base64::decode(nonce_key).unwrap();
let decoded_key = base64::decode(key).unwrap();

let key = GenericArray::from_slice(&decoded_nonce_key);
let nonce_key = GenericArray::from_slice(&decoded_key);
#[no_mangle]
pub extern "C" fn ascon_128_decrypt(
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,
) -> Ascon128DecryptResult {
let nonce_key = unsafe { std::slice::from_raw_parts(nonce_key, nonce_key_length) }.to_vec();
let key = unsafe { std::slice::from_raw_parts(key, key_length) }.to_vec();
let to_decrypt = unsafe { std::slice::from_raw_parts(to_decrypt, to_decrypt_length) }.to_vec();
let mut plaintext = <AsconAead as CASAsconAead>::decrypt(key, nonce_key, to_decrypt);
let capacity = plaintext.capacity();
plaintext.reserve_exact(capacity);
let result = Ascon128DecryptResult {
plaintext: plaintext.as_mut_ptr(),
length: plaintext.len(),
};
std::mem::forget(plaintext);
result
}

let cipher = Ascon128::new(key);
let mut plaintext = cipher.decrypt(&nonce_key, to_encrypt.as_ref()).unwrap();
#[no_mangle]
pub extern "C" fn ascon_128_decrypt_threadpool(
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,
) -> Ascon128DecryptResult {
let nonce_key = unsafe { std::slice::from_raw_parts(nonce_key, nonce_key_length) }.to_vec();
let key = unsafe { std::slice::from_raw_parts(key, key_length) }.to_vec();
let to_decrypt = unsafe { std::slice::from_raw_parts(to_decrypt, to_decrypt_length) }.to_vec();
let mut plaintext = <AsconAead as CASAsconAead>::decrypt_threadpool(key, nonce_key, to_decrypt);
let capacity = plaintext.capacity();
plaintext.reserve_exact(capacity);
let result = Ascon128DecryptResult {
Expand Down
135 changes: 0 additions & 135 deletions src/blake2.rs

This file was deleted.

Loading

0 comments on commit d864967

Please sign in to comment.