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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cas-lib"
version = "0.2.54"
version = "0.2.55"
edition = "2021"
description = "Core lib for CAS"
license = "Apache-2.0"
Expand Down
8 changes: 8 additions & 0 deletions src/compression/zstd.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
use std::{io::Cursor, sync::mpsc};

/// Compresses data using Zstandard compression algorithm.
/// The `level` parameter controls the compression level (0-22).
/// Higher levels result in better compression but slower performance.
pub fn compress(data_to_compress: Vec<u8>, level: i32) -> Vec<u8> {
let cursor = Cursor::new(data_to_compress);
let mut compressed_data = Vec::new();
zstd::stream::copy_encode(cursor, &mut compressed_data, level).unwrap();
compressed_data
}

/// Compresses data using Zstandard compression algorithm on the threadpool.
/// The `level` parameter controls the compression level (0-22).
/// Higher levels result in better compression but slower performance.
pub fn compress_threadpool(data_to_compress: Vec<u8>, level: i32) -> Vec<u8> {
let (sender, receiver) = mpsc::channel();
rayon::spawn(move || {
Expand All @@ -17,13 +23,15 @@ pub fn compress_threadpool(data_to_compress: Vec<u8>, level: i32) -> Vec<u8> {
result
}

/// Decompresses data using Zstandard decompression algorithm.
pub fn decompress(data_to_decompress: Vec<u8>) -> Vec<u8> {
let mut cursor = Cursor::new(data_to_decompress);
let mut decompressed_data = Vec::new();
zstd::stream::copy_decode(&mut cursor, &mut decompressed_data).unwrap();
decompressed_data
}

/// Decompresses data using Zstandard decompression algorithm on the threadpool.
pub fn decompress_threadpool(data_to_decompress: Vec<u8>) -> Vec<u8> {
let (sender, receiver) = mpsc::channel();
rayon::spawn(move || {
Expand Down
4 changes: 4 additions & 0 deletions src/digital_signature/sha_256_ed25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use super::cas_digital_signature_rsa::{
pub struct SHA256ED25519DigitalSignature;

impl ED25519DigitalSignature for SHA256ED25519DigitalSignature {
/// Creates a digital signature using SHA-256 as the hashing algorithm and Ed25519-Dalek as the signing algorithm.
fn digital_signature_ed25519(data_to_sign: Vec<u8>) -> SHAED25519DalekDigitalSignatureResult {
let mut hasher = Sha3_256::new();
hasher.update(data_to_sign);
Expand All @@ -25,6 +26,7 @@ impl ED25519DigitalSignature for SHA256ED25519DigitalSignature {
result
}

/// Verifys a digital signature using SHA-256 as the hashing algorithm and Ed25519-Dalek as the verification algorithm.
fn digital_signature_ed25519_verify(
public_key: Vec<u8>,
data_to_verify: Vec<u8>,
Expand All @@ -37,6 +39,7 @@ impl ED25519DigitalSignature for SHA256ED25519DigitalSignature {
return ed25519_verify_with_public_key(public_key, signature, sha_hash_bytes);
}

/// Creates a digital signature using SHA-256 as the hashing algorithm and Ed25519-Dalek as the signing algorithm on the threadpool.
fn digital_signature_ed25519_threadpool(data_to_sign: Vec<u8>) -> SHAED25519DalekDigitalSignatureResult {
let (sender, receiver) = mpsc::channel();
rayon::spawn(move || {
Expand All @@ -47,6 +50,7 @@ impl ED25519DigitalSignature for SHA256ED25519DigitalSignature {
result
}

/// Verifys a digital signature using SHA-256 as the hashing algorithm and Ed25519-Dalek as the verification algorithm on the threadpool.
fn digital_signature_ed25519_verify_threadpool(public_key: Vec<u8>, data_to_verify: Vec<u8>, signature: Vec<u8>) -> bool {
let (sender, receiver) = mpsc::channel();
rayon::spawn(move || {
Expand Down
6 changes: 6 additions & 0 deletions src/digital_signature/sha_256_rsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use super::cas_digital_signature_rsa::{RSADigitalSignatureResult, RSADigitalSign
pub struct SHA256RSADigitalSignature;

impl RSADigitalSignature for SHA256RSADigitalSignature {
/// Creates a digital signature using SHA-256 as the hashing algorithm and RSA as the signing algorithm.
fn digital_signature_rsa(
rsa_key_size: u32,
data_to_sign: Vec<u8>,
Expand Down Expand Up @@ -42,6 +43,7 @@ impl RSADigitalSignature for SHA256RSADigitalSignature {
result
}

/// Creates a digital signature using SHA-256 as the hashing algorithm and RSA as the signing algorithm on the threadpool.
fn digital_signature_rsa_threadpool(rsa_key_size: u32, data_to_sign: Vec<u8>) -> RSADigitalSignatureResult {
let (sender, receiver) = mpsc::channel();
rayon::spawn(move || {
Expand All @@ -52,6 +54,8 @@ impl RSADigitalSignature for SHA256RSADigitalSignature {
result
}

/// Verifys a digital signature using SHA-256 as the hashing algorithm and RSA as the verification algorithm.
/// The public key is expected to be in PEM format.
fn verify_rsa(public_key: String, data_to_verify: Vec<u8>, signature: Vec<u8>) -> bool {
let mut hasher = Sha3_256::new();
hasher.update(data_to_verify);
Expand All @@ -69,6 +73,8 @@ impl RSADigitalSignature for SHA256RSADigitalSignature {
}
}

/// Verifys a digital signature using SHA-256 as the hashing algorithm and RSA as the verification algorithm on the threadpool.
/// The public key is expected to be in PEM format.
fn verify_rsa_threadpool(public_key: String, data_to_verify: Vec<u8>, signature: Vec<u8>) -> bool {
let (sender, receiver) = mpsc::channel();
rayon::spawn(move || {
Expand Down
4 changes: 4 additions & 0 deletions src/digital_signature/sha_512_ed25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use super::cas_digital_signature_rsa::{
pub struct SHA512ED25519DigitalSignature;

impl ED25519DigitalSignature for SHA512ED25519DigitalSignature {
/// Creates a digital signature using SHA-512 as the hashing algorithm and Ed25519-Dalek as the signing algorithm.
fn digital_signature_ed25519(
data_to_sign: Vec<u8>,
) -> SHAED25519DalekDigitalSignatureResult {
Expand All @@ -28,6 +29,7 @@ impl ED25519DigitalSignature for SHA512ED25519DigitalSignature {
result
}

/// Verifys a digital signature using SHA-512 as the hashing algorithm and Ed25519-Dalek as the verification algorithm.
fn digital_signature_ed25519_verify(
public_key: Vec<u8>,
data_to_verify: Vec<u8>,
Expand All @@ -40,6 +42,7 @@ impl ED25519DigitalSignature for SHA512ED25519DigitalSignature {
return ed25519_verify_with_public_key(public_key, signature, sha_hash_bytes);
}

/// Creates a digital signature using SHA-512 as the hashing algorithm and Ed25519-Dalek as the signing algorithm on the threadpool.
fn digital_signature_ed25519_threadpool(data_to_sign: Vec<u8>) -> SHAED25519DalekDigitalSignatureResult {
let (sender, receiver) = mpsc::channel();
rayon::spawn(move || {
Expand All @@ -50,6 +53,7 @@ impl ED25519DigitalSignature for SHA512ED25519DigitalSignature {
result
}

/// Verifys a digital signature using SHA-512 as the hashing algorithm and Ed25519-Dalek as the verification algorithm on the threadpool.
fn digital_signature_ed25519_verify_threadpool(public_key: Vec<u8>, data_to_verify: Vec<u8>, signature: Vec<u8>) -> bool {
let (sender, receiver) = mpsc::channel();
rayon::spawn(move || {
Expand Down
6 changes: 6 additions & 0 deletions src/digital_signature/sha_512_rsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use super::cas_digital_signature_rsa::{RSADigitalSignatureResult, RSADigitalSign
pub struct SHA512RSADigitalSignature;

impl RSADigitalSignature for SHA512RSADigitalSignature {
/// Creates a digital signature using SHA-512 as the hashing algorithm and RSA as the signing algorithm.
fn digital_signature_rsa(
rsa_key_size: u32,
data_to_sign: Vec<u8>,
Expand Down Expand Up @@ -41,6 +42,7 @@ impl RSADigitalSignature for SHA512RSADigitalSignature {
result
}

/// Creates a digital signature using SHA-512 as the hashing algorithm and RSA as the signing algorithm on the threadpool.
fn digital_signature_rsa_threadpool(rsa_key_size: u32, data_to_sign: Vec<u8>) -> RSADigitalSignatureResult {
let (sender, receiver) = mpsc::channel();
rayon::spawn(move || {
Expand All @@ -51,6 +53,8 @@ impl RSADigitalSignature for SHA512RSADigitalSignature {
result
}

/// Verifys a digital signature using SHA-512 as the hashing algorithm and RSA as the verification algorithm.
/// The public key is expected to be in PEM format.
fn verify_rsa(public_key: String, data_to_verify: Vec<u8>, signature: Vec<u8>) -> bool {
let mut hasher = Sha3_512::new();
hasher.update(data_to_verify);
Expand All @@ -68,6 +72,8 @@ impl RSADigitalSignature for SHA512RSADigitalSignature {
}
}

/// Verifys a digital signature using SHA-512 as the hashing algorithm and RSA as the verification algorithm on the threadpool.
/// The public key is expected to be in PEM format.
fn verify_rsa_threadpool(public_key: String, data_to_verify: Vec<u8>, signature: Vec<u8>) -> bool {
let (sender, receiver) = mpsc::channel();
rayon::spawn(move || {
Expand Down
16 changes: 16 additions & 0 deletions src/hashers/blake2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,44 @@ use blake2::{Blake2b512, Blake2s256, Digest};
pub struct CASBlake2;

impl CASHasher for CASBlake2 {
/// Hashes data using the Blake2b-512 algorithm.
/// Returns the hash as a vector of bytes.
fn hash_512(data_to_hash: Vec<u8>) -> Vec<u8> {
let mut hasher = Blake2b512::new();
hasher.update(data_to_hash);
let result = hasher.finalize();
return result.to_vec();
}

/// Verifies a hash using the Blake2b-512 algorithm.
/// Returns true if the hash matches the data, false otherwise.
fn verify_512(hash_to_verify: Vec<u8>, data_to_verify: Vec<u8>) -> bool {
let mut hasher = Blake2b512::new();
hasher.update(data_to_verify);
let result = hasher.finalize();
return hash_to_verify.eq(&result.to_vec());
}

/// Hashes data using the Blake2s-256 algorithm.
/// Returns the hash as a vector of bytes.
fn hash_256(data_to_hash: Vec<u8>) -> Vec<u8> {
let mut hasher = Blake2s256::new();
hasher.update(data_to_hash);
let result = hasher.finalize();
return result.to_vec();
}

/// Verifies a hash using the Blake2s-256 algorithm.
/// Returns true if the hash matches the data, false otherwise.
fn verify_256(hash_to_verify: Vec<u8>, data_to_verify: Vec<u8>) -> bool {
let mut hasher = Blake2s256::new();
hasher.update(data_to_verify);
let result = hasher.finalize();
return hash_to_verify.eq(&result.to_vec());
}

/// Hashes data using the Blake2b-512 algorithm on the threadpool.
/// Returns the hash as a vector of bytes.
fn hash_512_threadpool(data_to_hash: Vec<u8>) -> Vec<u8> {
let (sender, receiver) = mpsc::channel();
rayon::spawn(move || {
Expand All @@ -44,6 +54,8 @@ impl CASHasher for CASBlake2 {
result
}

/// Verifies a hash using the Blake2b-512 algorithm on the threadpool.
/// Returns true if the hash matches the data, false otherwise.
fn verify_512_threadpool(hash_to_verify: Vec<u8>, data_to_verify: Vec<u8>) -> bool {
let (sender, receiver) = mpsc::channel();
rayon::spawn(move || {
Expand All @@ -54,6 +66,8 @@ impl CASHasher for CASBlake2 {
result
}

/// Hashes data using the Blake2s-256 algorithm on the threadpool.
/// Returns the hash as a vector of bytes.
fn hash_256_threadpool(data_to_hash: Vec<u8>) -> Vec<u8> {
let (sender, receiver) = mpsc::channel();
rayon::spawn(move || {
Expand All @@ -64,6 +78,8 @@ impl CASHasher for CASBlake2 {
result
}

/// Verifies a hash using the Blake2s-256 algorithm on the threadpool.
/// Returns true if the hash matches the data, false otherwise.
fn verify_256_threadpool(hash_to_verify: Vec<u8>, data_to_verify: Vec<u8>) -> bool {
let (sender, receiver) = mpsc::channel();
rayon::spawn(move || {
Expand Down
16 changes: 16 additions & 0 deletions src/hashers/sha.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,44 @@ use sha3::{Digest, Sha3_256, Sha3_512};
pub struct CASSHA;

impl CASHasher for CASSHA {
/// Hashes data using the SHA-512 algorithm.
/// Returns the hash as a vector of bytes.
fn hash_512(data_to_hash: Vec<u8>) -> Vec<u8> {
let mut hasher = Sha3_512::new();
hasher.update(data_to_hash);
let result = hasher.finalize();
return result.to_vec();
}

/// Verifies a hash using the SHA-512 algorithm.
/// Returns true if the hash matches the data, false otherwise.
fn verify_512(hash_to_verify: Vec<u8>, data_to_verify: Vec<u8>) -> bool {
let mut hasher = Sha3_512::new();
hasher.update(data_to_verify);
let result = hasher.finalize();
return hash_to_verify.eq(&result.to_vec());
}

/// Hashes data using the SHA-256 algorithm.
/// Returns the hash as a vector of bytes.
fn hash_256(data_to_hash: Vec<u8>) -> Vec<u8> {
let mut hasher = Sha3_256::new();
hasher.update(data_to_hash);
let result = hasher.finalize();
return result.to_vec();
}

/// Verifies a hash using the SHA-256 algorithm.
/// Returns true if the hash matches the data, false otherwise.
fn verify_256(hash_to_verify: Vec<u8>, data_to_verify: Vec<u8>) -> bool {
let mut hasher = Sha3_256::new();
hasher.update(data_to_verify);
let result = hasher.finalize();
return hash_to_verify.eq(&result.to_vec());
}

/// Hashes data using the SHA-512 algorithm on the threadpool.
/// Returns the hash as a vector of bytes.
fn hash_512_threadpool(data_to_hash: Vec<u8>) -> Vec<u8> {
let (sender, receiver) = mpsc::channel();
rayon::spawn(move || {
Expand All @@ -43,6 +53,8 @@ impl CASHasher for CASSHA {
result
}

/// Verifies a hash using the SHA-512 algorithm on the threadpool.
/// Returns true if the hash matches the data, false otherwise.
fn verify_512_threadpool(hash_to_verify: Vec<u8>, data_to_verify: Vec<u8>) -> bool {
let (sender, receiver) = mpsc::channel();
rayon::spawn(move || {
Expand All @@ -53,6 +65,8 @@ impl CASHasher for CASSHA {
result
}

/// Hashes data using the SHA-256 algorithm on the threadpool.
/// Returns the hash as a vector of bytes.
fn hash_256_threadpool(data_to_hash: Vec<u8>) -> Vec<u8> {
let (sender, receiver) = mpsc::channel();
rayon::spawn(move || {
Expand All @@ -63,6 +77,8 @@ impl CASHasher for CASSHA {
result
}

/// Verifies a hash using the SHA-256 algorithm on the threadpool.
/// Returns true if the hash matches the data, false otherwise.
fn verify_256_threadpool(hash_to_verify: Vec<u8>, data_to_verify: Vec<u8>) -> bool {
let (sender, receiver) = mpsc::channel();
rayon::spawn(move || {
Expand Down
8 changes: 8 additions & 0 deletions src/hybrid/hpke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ type Kdf = HkdfSha512;
pub struct CASHPKE;

impl CASHybrid for CASHPKE {
/// Generates a key pair for HPKE using X25519 as the KEM algorithm.
/// Returns the private key, public key, and an info string.
fn generate_key_pair() -> (Vec<u8>, Vec<u8>, Vec<u8>) {
let mut csprng = StdRng::from_entropy();
let (private_key, public_key) = Kem::gen_keypair(&mut csprng);
Expand All @@ -27,12 +29,16 @@ impl CASHybrid for CASHPKE {
)
}

/// Generates an info string for HPKE.
/// Returns a vector of bytes representing the info string.
fn generate_info_str() -> Vec<u8> {
let uuid = Uuid::new_v4();
let uuid_bytes: Vec<u8> = uuid.as_bytes().to_vec();
uuid_bytes
}

/// Encrypts data using HPKE with the provided public key and info string.
/// Returns the encapsulated key, ciphertext, and tag.
fn encrypt(
plaintext: Vec<u8>,
public_key: Vec<u8>,
Expand Down Expand Up @@ -60,6 +66,8 @@ impl CASHybrid for CASHPKE {
)
}

/// Decrypts data using HPKE with the provided private key, encapsulated key, tag, and info string.
/// Returns the decrypted plaintext.
fn decrypt(
ciphertext: Vec<u8>,
private_key: Vec<u8>,
Expand Down
14 changes: 14 additions & 0 deletions src/key_exchange/x25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ pub struct X25519SecretPublicKeyResult {
pub struct X25519;

impl CASKeyExchange for X25519 {
/// Generates a secret key and public key using X25519.
/// Returns a result containing the secret key and public key as vectors of bytes.
fn generate_secret_and_public_key() -> X25519SecretPublicKeyResult {
let secret_key = StaticSecret::random_from_rng(OsRng);
let public_key = PublicKey::from(&secret_key);
Expand All @@ -23,6 +25,10 @@ impl CASKeyExchange for X25519 {
result
}

/// Performs a Diffie-Hellman key exchange using the provided secret key and user's public key.
/// Returns the shared secret as a vector of bytes.
/// The shared secret is computed as the Diffie-Hellman result of the secret key and the user's public key.
/// The secret key and user's public key are expected to be in byte array format.
fn diffie_hellman(my_secret_key: Vec<u8>, users_public_key: Vec<u8>) -> Vec<u8> {
let mut secret_key_box = Box::new([0u8; 32]);
secret_key_box.copy_from_slice(&my_secret_key);
Expand All @@ -34,6 +40,10 @@ impl CASKeyExchange for X25519 {
return secret_key.diffie_hellman(&public_key).as_bytes().to_vec();
}

/// Generates a secret key and public key using X25519 on the threadpool.
/// Returns a result containing the secret key and public key as vectors of bytes.
/// This function spawns a thread to perform the key generation.
/// Returns the result of the key generation.
fn generate_secret_and_public_key_threadpool() -> X25519SecretPublicKeyResult {
let (sender, receiver) = mpsc::channel();
rayon::spawn(move || {
Expand All @@ -44,6 +54,10 @@ impl CASKeyExchange for X25519 {
result
}

/// Performs a Diffie-Hellman key exchange using the provided secret key and user's public key on the threadpool.
/// Returns the shared secret as a vector of bytes.
/// The shared secret is computed as the Diffie-Hellman result of the secret key and the user's public key.
/// The secret key and user's public key are expected to be in byte array format.
fn diffie_hellman_threadpool(my_secret_key: Vec<u8>, users_public_key: Vec<u8>) -> Vec<u8> {
let (sender, receiver) = mpsc::channel();
rayon::spawn(move || {
Expand Down
Loading