Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cas_core_lib"
version = "0.2.2"
version = "0.2.3"
edition = "2021"
description = "This is a Rust library providing external facing functions to performant and trusted encryption in Rust"
license = "Apache-2.0"
Expand All @@ -14,5 +14,5 @@ crate-type = ["dylib"]

[dependencies]
libc = "0.2.146"
cas-lib = "0.2.56"
cas-lib = "0.2.58"
zeroizing-alloc = "0.1.0"
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
[![image](https://img.shields.io/badge/Discord-5865F2?style=for-the-badge&logo=discord&logoColor=white)](https://discord.gg/7bXXCQj45q)

## Overview
This is our experimental core library which provides a simple FFI layer that takes advantage of Rust's thread safe nature to provide an abstraction layer to higher level languages to run industry standard crytographic operations sequentially and in parallel. Our goal is to decrease the redundancy of engineer's creating this layer proprietary within their internal systems.
This is our experimental core library which provides a simple FFI layer that takes advantage of Rust's thread safe nature to provide an abstraction layer to higher level languages to run industry standard crytographic operations. Our goal is to decrease the redundancy of engineer's creating this layer proprietary within their internal systems.

## Consuming Library Documentation
We utilize some smart people's existing work and we believe their documentation should be reviewed when possible.
- [Spin Research](https://github.com/SpinResearch)
- [Dalek-Cryptography](https://github.com/dalek-cryptography)
- [Rust Crypto](https://github.com/RustCrypto)
- [Rayon](https://github.com/rayon-rs/rayon)


## Disclaimer
Expand Down
188 changes: 5 additions & 183 deletions src/aes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,34 +64,7 @@ pub extern "C" fn aes_256_key_and_nonce_from_x25519_diffie_hellman_shared_secret
result
}

#[no_mangle]
pub extern "C" fn aes_256_key_and_nonce_from_x25519_diffie_hellman_shared_secret_threadpool(
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();

let mut aes_nonce = Vec::with_capacity(12);
aes_nonce.resize(12, 0);
aes_nonce.copy_from_slice(&shared_secret_slice[..12]);
let capacity = aes_nonce.capacity();
aes_nonce.reserve_exact(capacity);

let mut aes_key = <CASAES256 as CASAES256Encryption>::key_from_vec_threadpool(shared_secret_slice);
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(),
aes_nonce_ptr: aes_nonce.as_mut_ptr(),
aes_nonce_ptr_length: aes_nonce.len()
};
std::mem::forget(aes_nonce);
std::mem::forget(aes_key);
result
}

#[test]
pub fn aes_256_key_and_nonce_from_x25519_diffie_hellman_shared_secret_test() {
Expand Down Expand Up @@ -180,36 +153,7 @@ pub extern "C" fn aes_128_key_and_nonce_from_x25519_diffie_hellman_shared_secret
result
}

#[no_mangle]
pub extern "C" fn aes_128_key_and_nonce_from_x25519_diffie_hellman_shared_secret_threadpool(
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();

let mut shorted_shared_secret: [u8; 16] = Default::default();
shorted_shared_secret.copy_from_slice(&shared_secret_slice[..16]);
let mut aes_nonce = Vec::with_capacity(12);
aes_nonce.resize(12, 0);
aes_nonce.copy_from_slice(&shared_secret_slice[..12]);
let capacity = aes_nonce.capacity();
aes_nonce.reserve_exact(capacity);

let mut aes_key = <CASAES128 as CASAES128Encryption>::key_from_vec_threadpool(shorted_shared_secret.to_vec());
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(),
aes_nonce_ptr: aes_nonce.as_mut_ptr(),
aes_nonce_ptr_length: aes_nonce.len()
};
std::mem::forget(aes_nonce);
std::mem::forget(aes_key);
result
}

#[test]
pub fn aes_128_key_and_nonce_from_x25519_diffie_hellman_shared_secret_test() {
Expand Down Expand Up @@ -280,18 +224,7 @@ pub extern "C" fn aes_nonce() -> AesNonce {
result
}

#[no_mangle]
pub extern "C" fn aes_nonce_threadpool() -> AesNonce {
let mut random_bytes = <CASAES256 as CASAES256Encryption>::generate_nonce_threadpool();
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 {
Expand All @@ -306,18 +239,7 @@ pub extern "C" fn aes_256_key() -> AesKeyResult {
result
}

#[no_mangle]
pub extern "C" fn aes_256_key_threadpool() -> AesKeyResult {
let mut key = <CASAES256 as CASAES256Encryption>::generate_key_threadpool();
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 {
Expand All @@ -332,18 +254,7 @@ pub extern "C" fn aes_128_key() -> AesKeyResult {
result
}

#[no_mangle]
pub extern "C" fn aes_128_key_threadpool() -> AesKeyResult {
let mut key = <CASAES128 as CASAES128Encryption>::generate_key_threadpool();
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]
Expand All @@ -370,29 +281,7 @@ pub extern "C" fn aes_128_encrypt_bytes_with_key(
return result;
}

#[no_mangle]
pub extern "C" fn aes_128_encrypt_bytes_with_key_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,
) -> 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();
let mut ciphertext = <CASAES128 as CASAES128Encryption>::encrypt_plaintext_threadpool(key_slice, nonce_slice, to_encrypt_slice);
let capacity = ciphertext.capacity();
ciphertext.reserve_exact(capacity);
let result = AesBytesEncrypt {
ciphertext: ciphertext.as_mut_ptr(),
length: ciphertext.len(),
};
std::mem::forget(ciphertext);
return result;
}


#[no_mangle]
pub extern "C" fn aes_256_encrypt_bytes_with_key(
Expand All @@ -417,28 +306,7 @@ pub extern "C" fn aes_256_encrypt_bytes_with_key(
return result;
}

#[no_mangle]
pub extern "C" fn aes_256_encrypt_bytes_with_key_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,
) -> 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();
let mut ciphertext = <CASAES256 as CASAES256Encryption>::encrypt_plaintext_threadpool(key_slice, nonce_slice, to_encrypt_slice);
let capacity = ciphertext.capacity();
ciphertext.reserve_exact(capacity);
let result = AesBytesEncrypt {
ciphertext: ciphertext.as_mut_ptr(),
length: ciphertext.len(),
};
std::mem::forget(ciphertext);
return result;
}


#[no_mangle]
pub extern "C" fn aes_128_decrypt_bytes_with_key(
Expand All @@ -463,29 +331,6 @@ pub extern "C" fn aes_128_decrypt_bytes_with_key(
return result;
}

#[no_mangle]
pub extern "C" fn aes_128_decrypt_bytes_with_key_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,
) -> 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();
let mut plaintext = <CASAES128 as CASAES128Encryption>::decrypt_ciphertext_threadpool(key_slice, nonce_slice, to_decrypt_slice);
let capacity = plaintext.capacity();
plaintext.reserve_exact(capacity);
let result = AesBytesDecrypt {
plaintext: plaintext.as_mut_ptr(),
length: plaintext.len(),
};
std::mem::forget(plaintext);
return result;
}

#[no_mangle]
pub extern "C" fn aes_256_decrypt_bytes_with_key(
nonce_key: *const c_uchar,
Expand All @@ -508,26 +353,3 @@ pub extern "C" fn aes_256_decrypt_bytes_with_key(
std::mem::forget(plaintext);
return result;
}

#[no_mangle]
pub extern "C" fn aes_256_decrypt_bytes_with_key_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,
) -> 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();
let mut plaintext = <CASAES256 as CASAES256Encryption>::decrypt_ciphertext_threadpool(key_slice, nonce_slice, to_decrypt_slice);
let capacity = plaintext.capacity();
plaintext.reserve_exact(capacity);
let result = AesBytesDecrypt {
plaintext: plaintext.as_mut_ptr(),
length: plaintext.len(),
};
std::mem::forget(plaintext);
return result;
}
71 changes: 1 addition & 70 deletions src/ascon_aead.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,6 @@ pub extern "C" fn ascon_128_key() -> Ascon128Key {
result
}

#[no_mangle]
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]
Expand All @@ -67,18 +54,6 @@ pub extern "C" fn ascon_128_nonce() -> Ascon128Nonce {
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(
Expand All @@ -103,28 +78,7 @@ pub extern "C" fn ascon_128_encrypt(
result
}

#[no_mangle]
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,
) -> 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
}



#[no_mangle]
Expand All @@ -148,27 +102,4 @@ pub extern "C" fn ascon_128_decrypt(
};
std::mem::forget(plaintext);
result
}

#[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 {
plaintext: plaintext.as_mut_ptr(),
length: plaintext.len(),
};
std::mem::forget(plaintext);
result
}
Loading
Loading