Skip to content

#45 move to updated cas lib #49

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 4, 2025
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
Expand Up @@ -11,7 +11,7 @@ crate-type = ["cdylib"]
napi = "2.16.17"
napi-derive = "2.16.13"
csbindgen = "1.9.3"
cas-lib = "0.1.6"
cas-lib = "0.2.50"

[build-dependencies]
napi-build = "1"
Binary file modified index.node
Binary file not shown.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"name": "cas-typescript-sdk",
"version": "1.0.30",
"version": "1.0.31",
"description": "",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"scripts": {
"test": "cargo test --release && npm run build && mocha -r ts-node/register ./test-ts/**/*.ts --timeout 20000 --recursive",
"node:test": "mocha -r ts-node/register ./test-ts/**/*.ts --timeout 20000 --recursive",
"node:test": "mocha -r ts-node/register ./test-ts/**/*.test.spec.ts --timeout 20000 --recursive",
"rust:test": "cargo test --release",
"build": "npm run build:rust && rimraf lib && tsc",
"build:rust": "napi build --release",
Expand Down
2 changes: 1 addition & 1 deletion src/asymmetric/cas_rsa.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use cas_lib::asymmetric::{cas_asymmetric_encryption::CASRSAEncryption, cas_rsa::CASRSA, types::RSAKeyPairResult};
use cas_lib::asymmetric::{cas_rsa::CASRSA, types::{CASRSAEncryption, RSAKeyPairResult}};
use napi_derive::napi;

#[napi(constructor)]
Expand Down
34 changes: 25 additions & 9 deletions src/digital_signature/sha_256_ed25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,45 @@ use super::types::CASSHAED25519DalekDigitalSignatureResult;

#[napi]
pub fn sha_256_ed25519_digital_signature(data_to_sign: Vec<u8>) -> CASSHAED25519DalekDigitalSignatureResult {
return <SHA256ED25519DigitalSignature as ED25519DigitalSignature>::digital_signature_ed25519(data_to_sign).into();
return <SHA256ED25519DigitalSignature as ED25519DigitalSignature>::digital_signature_ed25519(&data_to_sign).into();
}

#[napi]
pub fn sha_256_ed25519_digital_signature_verify(public_key: Vec<u8>, data_to_verify: Vec<u8>, signature: Vec<u8>) -> bool {
return SHA256ED25519DigitalSignature::digital_signature_ed25519_verify(public_key, data_to_verify, signature)
if public_key.len() != 32 || signature.len() != 64 {
return false;
}
let mut pk = [0u8; 32];
pk.copy_from_slice(&public_key[..32]);
let mut sig = [0u8; 64];
sig.copy_from_slice(&signature[..64]);
SHA256ED25519DigitalSignature::digital_signature_ed25519_verify(pk, &data_to_verify, sig)
}


#[test]
fn sha_256_ed25519_test() {
let key_size: u32 = 1024;
let data_to_sign = b"GetTheseBytes".to_vec();
let signature_result: CASSHAED25519DalekDigitalSignatureResult = SHA256ED25519DigitalSignature::digital_signature_ed25519(data_to_sign.clone()).into();
let is_verified: bool = SHA256ED25519DigitalSignature::digital_signature_ed25519_verify(signature_result.public_key, data_to_sign, signature_result.signature);
let data_to_sign = b"GetTheseBytes";
let signature_result: CASSHAED25519DalekDigitalSignatureResult = SHA256ED25519DigitalSignature::digital_signature_ed25519(&data_to_sign.clone()).into();
let mut pk = [0u8; 32];
pk.copy_from_slice(&signature_result.public_key[..32]);
let mut sig = [0u8; 64];
sig.copy_from_slice(&signature_result.signature[..64]);
let is_verified: bool = SHA256ED25519DigitalSignature::digital_signature_ed25519_verify(pk, data_to_sign, sig);
assert_eq!(is_verified, true);
}

#[test]
fn sha_512_ed25519_test_fail() {
let key_size: u32 = 1024;
let data_to_sign = b"GetTheseBytes".to_vec();
let signature_result: CASSHAED25519DalekDigitalSignatureResult = SHA256ED25519DigitalSignature::digital_signature_ed25519(data_to_sign.clone()).into();
let not_original_data = b"NOtTHoseBytes".to_vec();
let is_verified: bool = SHA256ED25519DigitalSignature::digital_signature_ed25519_verify(signature_result.public_key, not_original_data, signature_result.signature);
let data_to_sign = b"GetTheseBytes";
let signature_result: CASSHAED25519DalekDigitalSignatureResult = SHA256ED25519DigitalSignature::digital_signature_ed25519(&data_to_sign.clone()).into();
let not_original_data = b"NOtTHoseBytes";
let mut pk = [0u8; 32];
pk.copy_from_slice(&signature_result.public_key[..32]);
let mut sig = [0u8; 64];
sig.copy_from_slice(&signature_result.signature[..64]);
let is_verified: bool = SHA256ED25519DigitalSignature::digital_signature_ed25519_verify(pk, not_original_data, sig);
assert_eq!(is_verified, false);
}
26 changes: 19 additions & 7 deletions src/digital_signature/sha_512_ed25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,41 @@ use super::types::CASSHAED25519DalekDigitalSignatureResult;

#[napi]
pub fn sha_512_ed25519_digital_signature(data_to_sign: Vec<u8>) -> CASSHAED25519DalekDigitalSignatureResult {
return <SHA512ED25519DigitalSignature as ED25519DigitalSignature>::digital_signature_ed25519(data_to_sign).into();
return <SHA512ED25519DigitalSignature as ED25519DigitalSignature>::digital_signature_ed25519(&data_to_sign).into();
}

#[napi]
pub fn sha_512_ed25519_digital_signature_verify(public_key: Vec<u8>, data_to_verify: Vec<u8>, signature: Vec<u8>) -> bool {
return <SHA512ED25519DigitalSignature as ED25519DigitalSignature>::digital_signature_ed25519_verify(public_key, data_to_verify, signature)
let public_key_array: [u8; 32] = public_key.try_into().expect("public_key must be 32 bytes");
let signature_array: [u8; 64] = signature.try_into().expect("signature must be 64 bytes");
return <SHA512ED25519DigitalSignature as ED25519DigitalSignature>::digital_signature_ed25519_verify(
public_key_array,
&data_to_verify,
signature_array,
);
}

#[test]
fn sha_512_ed25519_test() {
let key_size: u32 = 1024;
let data_to_sign = b"GetTheseBytes".to_vec();
let signature_result: SHAED25519DalekDigitalSignatureResult = <SHA512ED25519DigitalSignature as ED25519DigitalSignature>::digital_signature_ed25519(data_to_sign.clone());
let data_to_sign = b"GetTheseBytes";
let signature_result: SHAED25519DalekDigitalSignatureResult = <SHA512ED25519DigitalSignature as ED25519DigitalSignature>::digital_signature_ed25519(&data_to_sign.clone());
let is_verified: bool = SHA512ED25519DigitalSignature::digital_signature_ed25519_verify(signature_result.public_key, data_to_sign, signature_result.signature);
assert_eq!(is_verified, true);
}

#[test]
fn sha_512_ed25519_test_fail() {
let key_size: u32 = 1024;
let data_to_sign = b"GetTheseBytes".to_vec();
let signature_result: CASSHAED25519DalekDigitalSignatureResult = <SHA512ED25519DigitalSignature as ED25519DigitalSignature>::digital_signature_ed25519(data_to_sign.clone()).into();
let data_to_sign = b"GetTheseBytes";
let signature_result: CASSHAED25519DalekDigitalSignatureResult = <SHA512ED25519DigitalSignature as ED25519DigitalSignature>::digital_signature_ed25519(&data_to_sign.clone()).into();
let not_original_data = b"NOtTHoseBytes".to_vec();
let is_verified: bool = SHA512ED25519DigitalSignature::digital_signature_ed25519_verify(signature_result.public_key, not_original_data, signature_result.signature);
let public_key_array: [u8; 32] = signature_result.public_key.clone().try_into().expect("public_key must be 32 bytes");
let signature_array: [u8; 64] = signature_result.signature.clone().try_into().expect("signature must be 64 bytes");
let is_verified: bool = SHA512ED25519DigitalSignature::digital_signature_ed25519_verify(
public_key_array,
&not_original_data,
signature_array,
);
assert_eq!(is_verified, false);
}
4 changes: 2 additions & 2 deletions src/digital_signature/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ pub struct CASSHAED25519DalekDigitalSignatureResult {
impl From<SHAED25519DalekDigitalSignatureResult> for CASSHAED25519DalekDigitalSignatureResult {
fn from(value: SHAED25519DalekDigitalSignatureResult) -> Self {
CASSHAED25519DalekDigitalSignatureResult {
public_key: value.public_key,
signature: value.signature
public_key: value.public_key.to_vec(),
signature: value.signature.to_vec()
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/hashers/sha.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub fn sha_512_verify_test() {
#[test]
pub fn sha_512_verify_fail_test() {
let data_to_hash = "NotMyDataToHash".as_bytes().to_vec();
let hashed_data = sha_512(data_to_hash.clone());
let _hashed_data = sha_512(data_to_hash.clone());
let data_to_verify = "NotMyDataToHash2".as_bytes().to_vec();
assert_ne!(true, sha_512_verify(data_to_hash, data_to_verify));
}
Expand All @@ -54,15 +54,15 @@ pub fn sha_256_test() {
#[test]
pub fn sha_256_verify_test() {
let data_to_hash = "NotMyDataToHash".as_bytes().to_vec();
let hashed_data = sha_256(data_to_hash.clone());
let _hashed_data = sha_256(data_to_hash.clone());
let data_to_verify = "NotMyDataToHash".as_bytes().to_vec();
assert_ne!(true, sha_256_verify(data_to_hash, data_to_verify));
}

#[test]
pub fn sha_256_verify_fail_test() {
let data_to_hash = "NotMyDataToHash".as_bytes().to_vec();
let hashed_data = sha_256(data_to_hash.clone());
let _hashed_data = sha_256(data_to_hash.clone());
let data_to_verify = "NotMyDataToHash2".as_bytes().to_vec();
assert_ne!(true, sha_256_verify(data_to_hash, data_to_verify));
}
6 changes: 3 additions & 3 deletions src/key_exchange/types.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use cas_lib::key_exchange::x25519::x25519SecretPublicKeyResult;
use cas_lib::key_exchange::x25519::X25519SecretPublicKeyResult;
use napi_derive::napi;

#[napi(constructor)]
Expand All @@ -7,8 +7,8 @@ pub struct CASx25519SecretPublicKeyResult {
pub secret_key: Vec<u8>,
}

impl From<x25519SecretPublicKeyResult> for CASx25519SecretPublicKeyResult {
fn from(value: x25519SecretPublicKeyResult) -> Self {
impl From<X25519SecretPublicKeyResult> for CASx25519SecretPublicKeyResult {
fn from(value: X25519SecretPublicKeyResult) -> Self {
CASx25519SecretPublicKeyResult {
public_key: value.public_key,
secret_key: value.secret_key
Expand Down
2 changes: 1 addition & 1 deletion src/key_exchange/x25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub fn x25519_generate_secret_and_public_key() -> CASx25519SecretPublicKeyResult

#[napi]
pub fn x25519_diffie_hellman(my_secret_key: Vec<u8>, users_public_key: Vec<u8>) -> Vec<u8> {
return <X25519 as CASKeyExchange>::diffie_hellman(my_secret_key, users_public_key);
return <X25519 as CASKeyExchange>::diffie_hellman(my_secret_key, users_public_key).to_vec();
}

#[test]
Expand Down
9 changes: 4 additions & 5 deletions src/password_hashers/argon2.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@

use napi_derive::napi;
use cas_lib::password_hashers::argon2::CASArgon;
use cas_lib::password_hashers::cas_password_hasher::CASPasswordHasher;

#[napi]
pub fn argon2_hash(password: String) -> String {
return <CASArgon as CASPasswordHasher>::hash_password(password);
return CASArgon::hash_password(password);
}

#[napi]
pub fn argon2_hash_thread_pool(password: String) -> String {
return <CASArgon as CASPasswordHasher>::hash__password_threadpool(password);
return CASArgon::hash_password_threadpool(password);
}

#[napi]
pub fn argon2_verify(hashed_password: String, password_to_verify: String) -> bool {
return <CASArgon as CASPasswordHasher>::verify_password(hashed_password, password_to_verify);
return CASArgon::verify_password(hashed_password, password_to_verify);
}

#[napi]
pub fn argon2_verify_threadpool(hashed_password: String, password_to_verify: String) -> bool {
return <CASArgon as CASPasswordHasher>::verify_password_threadpool(hashed_password, password_to_verify);
return CASArgon::verify_password_threadpool(hashed_password, password_to_verify);
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion src/password_hashers/bcrypt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub fn bcrypt_hash(password_to_hash: String) -> String {

#[napi]
pub fn bcrypt_hash_threadpool(password_to_hash: String) -> String {
return <CASBCrypt as CASPasswordHasher>::hash__password_threadpool(password_to_hash);
return <CASBCrypt as CASPasswordHasher>::hash_password_threadpool(password_to_hash);
}

#[napi]
Expand Down
2 changes: 1 addition & 1 deletion src/password_hashers/scrypt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub fn scrypt_verify(hashed_password: String, password_to_verify: String) -> boo

#[napi]
pub fn scrypt_hash_threadpool(password_to_hash: String) -> String {
return <CASScrypt as CASPasswordHasher>::hash__password_threadpool(password_to_hash);
return <CASScrypt as CASPasswordHasher>::hash_password_threadpool(password_to_hash);
}

#[napi]
Expand Down
38 changes: 27 additions & 11 deletions src/sponges/ascon_aead.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use napi_derive::napi;

#[napi]
pub fn ascon128_key_generate() -> Vec<u8> {
return <AsconAead as CASAsconAead>::generate_key();
return <AsconAead as CASAsconAead>::generate_key().to_vec();
}

#[test]
Expand All @@ -15,7 +15,7 @@ fn test_ascon128_key_generate() {

#[napi]
pub fn ascon128_nonce_generate() -> Vec<u8> {
return <AsconAead as CASAsconAead>::generate_nonce();
return <AsconAead as CASAsconAead>::generate_nonce().to_vec();
}

#[test]
Expand All @@ -26,29 +26,45 @@ pub fn test_ascon128_nonce_generate() {

#[napi]
pub fn ascon128_encrypt(key: Vec<u8>, nonce: Vec<u8>, plaintext: Vec<u8>) -> Vec<u8> {
return <AsconAead as CASAsconAead>::encrypt(key, nonce, plaintext);
let key_arr: [u8; 16] = key.try_into().expect("Key must be 16 bytes");
let nonce_arr: [u8; 16] = nonce.try_into().expect("Nonce must be 16 bytes");
return <AsconAead as CASAsconAead>::encrypt(key_arr, nonce_arr, plaintext);
}

#[test]
pub fn test_ascon128_encrypt() {
let key = <AsconAead as CASAsconAead>::generate_key();
let nonce = <AsconAead as CASAsconAead>::generate_nonce();
let plaintext = b"Hello, World!".to_vec();
let ciphertext = ascon128_encrypt(key.clone(), nonce.clone(), plaintext.clone());
assert_ne!(ciphertext, plaintext);
let plaintext = b"Hello, World!";
let ciphertext = ascon128_encrypt(
key.clone().to_vec(),
nonce.clone().to_vec(),
plaintext.to_vec(),
);
assert_ne!(ciphertext, plaintext.to_vec());
}

#[napi]
pub fn ascon128_decrypt(key: Vec<u8>, nonce: Vec<u8>, ciphertext: Vec<u8>) -> Vec<u8> {
return <AsconAead as CASAsconAead>::decrypt(key, nonce, ciphertext);
let key_arr: [u8; 16] = key.try_into().expect("Key must be 16 bytes");
let nonce_arr: [u8; 16] = nonce.try_into().expect("Nonce must be 16 bytes");
return <AsconAead as CASAsconAead>::decrypt(key_arr, nonce_arr, ciphertext);
}

#[test]
pub fn test_ascon128_decrypt() {
let key = <AsconAead as CASAsconAead>::generate_key();
let nonce = <AsconAead as CASAsconAead>::generate_nonce();
let plaintext = b"Hello, World!".to_vec();
let ciphertext = ascon128_encrypt(key.clone(), nonce.clone(), plaintext.clone());
let decrypted = ascon128_decrypt(key.clone(), nonce.clone(), ciphertext.clone());
assert_eq!(decrypted, plaintext);
let plaintext = b"Hello, World!";
let ciphertext = ascon128_encrypt(
key.clone().to_vec(),
nonce.clone().to_vec(),
plaintext.to_vec(),
);
let decrypted = ascon128_decrypt(
key.clone().to_vec(),
nonce.clone().to_vec(),
ciphertext.clone(),
);
assert_eq!(decrypted, plaintext.to_vec());
}
30 changes: 20 additions & 10 deletions src/symmetric/aes.rs
Original file line number Diff line number Diff line change
@@ -1,55 +1,65 @@
use cas_lib::symmetric::{aes::{CASAES128, CASAES256}, cas_symmetric_encryption::CASAESEncryption};
use cas_lib::symmetric::{aes::{CASAES128, CASAES256}, cas_symmetric_encryption::{CASAES128Encryption, CASAES256Encryption}};
use napi_derive::napi;

use super::types::CASAesKeyFromX25519SharedSecret;

#[napi]
pub fn aes_nonce() -> Vec<u8> {
return <CASAES256 as CASAESEncryption>::generate_nonce();
return <CASAES256 as CASAES256Encryption>::generate_nonce().to_vec();
}

#[napi]
pub fn aes128_key() -> Vec<u8> {
return <CASAES128 as CASAESEncryption>::generate_key();
return <CASAES128 as CASAES128Encryption>::generate_key().to_vec();
}

#[napi]
pub fn aes256_key() -> Vec<u8> {
return <CASAES256 as CASAESEncryption>::generate_key();
return <CASAES256 as CASAES256Encryption>::generate_key().to_vec();
}

#[napi]
pub fn aes128_encrypt(aes_key: Vec<u8>, nonce: Vec<u8>, plaintext: Vec<u8>) -> Vec<u8> {
return <CASAES128 as CASAESEncryption>::encrypt_plaintext(aes_key, nonce, plaintext);
let key: [u8; 16] = aes_key.try_into().expect("Key must be 16 bytes");
let nonce_arr: [u8; 12] = nonce.try_into().expect("Nonce must be 12 bytes");
<CASAES128 as CASAES128Encryption>::encrypt_plaintext(key, nonce_arr, plaintext)
}

#[napi]
pub fn aes128_decrypt(aes_key: Vec<u8>, nonce: Vec<u8>, ciphertext: Vec<u8>) -> Vec<u8> {
return <CASAES128 as CASAESEncryption>::decrypt_ciphertext(aes_key, nonce, ciphertext);
let key: [u8; 16] = aes_key.try_into().expect("Key must be 16 bytes");
let nonce_arr: [u8; 12] = nonce.try_into().expect("Nonce must be 12 bytes");
<CASAES128 as CASAES128Encryption>::decrypt_ciphertext(key, nonce_arr, ciphertext)
}

#[napi]
pub fn aes256_encrypt(aes_key: Vec<u8>, nonce: Vec<u8>, plaintext: Vec<u8>) -> Vec<u8> {
return <CASAES256 as CASAESEncryption>::encrypt_plaintext(aes_key, nonce, plaintext);
let key: [u8; 32] = aes_key.try_into().expect("Key must be 32 bytes");
let nonce_arr: [u8; 12] = nonce.try_into().expect("Nonce must be 12 bytes");
<CASAES256 as CASAES256Encryption>::encrypt_plaintext(key, nonce_arr, plaintext)
}

#[napi]
pub fn aes256_decrypt(aes_key: Vec<u8>, nonce: Vec<u8>, ciphertext: Vec<u8>) -> Vec<u8> {
return <CASAES256 as CASAESEncryption>::decrypt_ciphertext(aes_key, nonce, ciphertext);
let key: [u8; 32] = aes_key.try_into().expect("Key must be 32 bytes");
let nonce_arr: [u8; 12] = nonce.try_into().expect("Nonce must be 12 bytes");
<CASAES256 as CASAES256Encryption>::decrypt_ciphertext(key, nonce_arr, ciphertext)
}

#[napi]
pub fn aes_256_key_from_x25519_shared_secret(
shared_secret: Vec<u8>,
) -> CASAesKeyFromX25519SharedSecret {
return <CASAES256 as CASAESEncryption>::key_from_x25519_shared_secret(shared_secret).into();
let shared_secret_arr: [u8; 32] = shared_secret.try_into().expect("Shared secret must be 32 bytes");
return <CASAES256 as CASAES256Encryption>::key_from_x25519_shared_secret(shared_secret_arr).into();
}

#[napi]
pub fn aes_128_key_from_x25519_shared_secret(
shared_secret: Vec<u8>,
) -> CASAesKeyFromX25519SharedSecret {
return <CASAES128 as CASAESEncryption>::key_from_x25519_shared_secret(shared_secret).into();
let shared_secret_arr: [u8; 32] = shared_secret.try_into().expect("Shared secret must be 32 bytes");
return <CASAES128 as CASAES128Encryption>::key_from_x25519_shared_secret(shared_secret_arr).into();
}

#[test]
Expand Down
Loading