cas-typescript-sdk 1.0.29 → 1.0.31
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Cargo.toml +1 -1
- package/index.node +0 -0
- package/package.json +3 -3
- package/src/asymmetric/cas_rsa.rs +1 -1
- package/src/digital_signature/sha_256_ed25519.rs +25 -9
- package/src/digital_signature/sha_512_ed25519.rs +19 -7
- package/src/digital_signature/types.rs +2 -2
- package/src/hashers/sha.rs +3 -3
- package/src/key_exchange/types.rs +3 -3
- package/src/key_exchange/x25519.rs +1 -1
- package/src/password_hashers/argon2.rs +4 -5
- package/src/password_hashers/bcrypt.rs +1 -1
- package/src/password_hashers/scrypt.rs +1 -1
- package/src/sponges/ascon_aead.rs +27 -11
- package/src/symmetric/aes.rs +20 -10
- package/src/symmetric/types.rs +15 -5
package/Cargo.toml
CHANGED
package/index.node
CHANGED
|
Binary file
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cas-typescript-sdk",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.31",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "lib/index.d.ts",
|
|
7
7
|
"scripts": {
|
|
8
8
|
"test": "cargo test --release && npm run build && mocha -r ts-node/register ./test-ts/**/*.ts --timeout 20000 --recursive",
|
|
9
|
-
"node:test": "mocha -r ts-node/register ./test-ts/**/*.ts --timeout 20000 --recursive",
|
|
10
|
-
"rust:test": "cargo test",
|
|
9
|
+
"node:test": "mocha -r ts-node/register ./test-ts/**/*.test.spec.ts --timeout 20000 --recursive",
|
|
10
|
+
"rust:test": "cargo test --release",
|
|
11
11
|
"build": "npm run build:rust && rimraf lib && tsc",
|
|
12
12
|
"build:rust": "napi build --release",
|
|
13
13
|
"prepare": "npm run build"
|
|
@@ -5,29 +5,45 @@ use super::types::CASSHAED25519DalekDigitalSignatureResult;
|
|
|
5
5
|
|
|
6
6
|
#[napi]
|
|
7
7
|
pub fn sha_256_ed25519_digital_signature(data_to_sign: Vec<u8>) -> CASSHAED25519DalekDigitalSignatureResult {
|
|
8
|
-
return <SHA256ED25519DigitalSignature as ED25519DigitalSignature>::digital_signature_ed25519(data_to_sign).into();
|
|
8
|
+
return <SHA256ED25519DigitalSignature as ED25519DigitalSignature>::digital_signature_ed25519(&data_to_sign).into();
|
|
9
9
|
}
|
|
10
10
|
|
|
11
11
|
#[napi]
|
|
12
12
|
pub fn sha_256_ed25519_digital_signature_verify(public_key: Vec<u8>, data_to_verify: Vec<u8>, signature: Vec<u8>) -> bool {
|
|
13
|
-
|
|
13
|
+
if public_key.len() != 32 || signature.len() != 64 {
|
|
14
|
+
return false;
|
|
15
|
+
}
|
|
16
|
+
let mut pk = [0u8; 32];
|
|
17
|
+
pk.copy_from_slice(&public_key[..32]);
|
|
18
|
+
let mut sig = [0u8; 64];
|
|
19
|
+
sig.copy_from_slice(&signature[..64]);
|
|
20
|
+
SHA256ED25519DigitalSignature::digital_signature_ed25519_verify(pk, &data_to_verify, sig)
|
|
14
21
|
}
|
|
15
22
|
|
|
23
|
+
|
|
16
24
|
#[test]
|
|
17
25
|
fn sha_256_ed25519_test() {
|
|
18
26
|
let key_size: u32 = 1024;
|
|
19
|
-
let data_to_sign = b"GetTheseBytes"
|
|
20
|
-
let signature_result: CASSHAED25519DalekDigitalSignatureResult = SHA256ED25519DigitalSignature::digital_signature_ed25519(data_to_sign.clone()).into();
|
|
21
|
-
let
|
|
27
|
+
let data_to_sign = b"GetTheseBytes";
|
|
28
|
+
let signature_result: CASSHAED25519DalekDigitalSignatureResult = SHA256ED25519DigitalSignature::digital_signature_ed25519(&data_to_sign.clone()).into();
|
|
29
|
+
let mut pk = [0u8; 32];
|
|
30
|
+
pk.copy_from_slice(&signature_result.public_key[..32]);
|
|
31
|
+
let mut sig = [0u8; 64];
|
|
32
|
+
sig.copy_from_slice(&signature_result.signature[..64]);
|
|
33
|
+
let is_verified: bool = SHA256ED25519DigitalSignature::digital_signature_ed25519_verify(pk, data_to_sign, sig);
|
|
22
34
|
assert_eq!(is_verified, true);
|
|
23
35
|
}
|
|
24
36
|
|
|
25
37
|
#[test]
|
|
26
38
|
fn sha_512_ed25519_test_fail() {
|
|
27
39
|
let key_size: u32 = 1024;
|
|
28
|
-
let data_to_sign = b"GetTheseBytes"
|
|
29
|
-
let signature_result: CASSHAED25519DalekDigitalSignatureResult = SHA256ED25519DigitalSignature::digital_signature_ed25519(data_to_sign.clone()).into();
|
|
30
|
-
let not_original_data = b"NOtTHoseBytes"
|
|
31
|
-
let
|
|
40
|
+
let data_to_sign = b"GetTheseBytes";
|
|
41
|
+
let signature_result: CASSHAED25519DalekDigitalSignatureResult = SHA256ED25519DigitalSignature::digital_signature_ed25519(&data_to_sign.clone()).into();
|
|
42
|
+
let not_original_data = b"NOtTHoseBytes";
|
|
43
|
+
let mut pk = [0u8; 32];
|
|
44
|
+
pk.copy_from_slice(&signature_result.public_key[..32]);
|
|
45
|
+
let mut sig = [0u8; 64];
|
|
46
|
+
sig.copy_from_slice(&signature_result.signature[..64]);
|
|
47
|
+
let is_verified: bool = SHA256ED25519DigitalSignature::digital_signature_ed25519_verify(pk, not_original_data, sig);
|
|
32
48
|
assert_eq!(is_verified, false);
|
|
33
49
|
}
|
|
@@ -7,19 +7,25 @@ use super::types::CASSHAED25519DalekDigitalSignatureResult;
|
|
|
7
7
|
|
|
8
8
|
#[napi]
|
|
9
9
|
pub fn sha_512_ed25519_digital_signature(data_to_sign: Vec<u8>) -> CASSHAED25519DalekDigitalSignatureResult {
|
|
10
|
-
return <SHA512ED25519DigitalSignature as ED25519DigitalSignature>::digital_signature_ed25519(data_to_sign).into();
|
|
10
|
+
return <SHA512ED25519DigitalSignature as ED25519DigitalSignature>::digital_signature_ed25519(&data_to_sign).into();
|
|
11
11
|
}
|
|
12
12
|
|
|
13
13
|
#[napi]
|
|
14
14
|
pub fn sha_512_ed25519_digital_signature_verify(public_key: Vec<u8>, data_to_verify: Vec<u8>, signature: Vec<u8>) -> bool {
|
|
15
|
-
|
|
15
|
+
let public_key_array: [u8; 32] = public_key.try_into().expect("public_key must be 32 bytes");
|
|
16
|
+
let signature_array: [u8; 64] = signature.try_into().expect("signature must be 64 bytes");
|
|
17
|
+
return <SHA512ED25519DigitalSignature as ED25519DigitalSignature>::digital_signature_ed25519_verify(
|
|
18
|
+
public_key_array,
|
|
19
|
+
&data_to_verify,
|
|
20
|
+
signature_array,
|
|
21
|
+
);
|
|
16
22
|
}
|
|
17
23
|
|
|
18
24
|
#[test]
|
|
19
25
|
fn sha_512_ed25519_test() {
|
|
20
26
|
let key_size: u32 = 1024;
|
|
21
|
-
let data_to_sign = b"GetTheseBytes"
|
|
22
|
-
let signature_result: SHAED25519DalekDigitalSignatureResult = <SHA512ED25519DigitalSignature as ED25519DigitalSignature>::digital_signature_ed25519(data_to_sign.clone());
|
|
27
|
+
let data_to_sign = b"GetTheseBytes";
|
|
28
|
+
let signature_result: SHAED25519DalekDigitalSignatureResult = <SHA512ED25519DigitalSignature as ED25519DigitalSignature>::digital_signature_ed25519(&data_to_sign.clone());
|
|
23
29
|
let is_verified: bool = SHA512ED25519DigitalSignature::digital_signature_ed25519_verify(signature_result.public_key, data_to_sign, signature_result.signature);
|
|
24
30
|
assert_eq!(is_verified, true);
|
|
25
31
|
}
|
|
@@ -27,9 +33,15 @@ fn sha_512_ed25519_test() {
|
|
|
27
33
|
#[test]
|
|
28
34
|
fn sha_512_ed25519_test_fail() {
|
|
29
35
|
let key_size: u32 = 1024;
|
|
30
|
-
let data_to_sign = b"GetTheseBytes"
|
|
31
|
-
let signature_result: CASSHAED25519DalekDigitalSignatureResult = <SHA512ED25519DigitalSignature as ED25519DigitalSignature>::digital_signature_ed25519(data_to_sign.clone()).into();
|
|
36
|
+
let data_to_sign = b"GetTheseBytes";
|
|
37
|
+
let signature_result: CASSHAED25519DalekDigitalSignatureResult = <SHA512ED25519DigitalSignature as ED25519DigitalSignature>::digital_signature_ed25519(&data_to_sign.clone()).into();
|
|
32
38
|
let not_original_data = b"NOtTHoseBytes".to_vec();
|
|
33
|
-
let
|
|
39
|
+
let public_key_array: [u8; 32] = signature_result.public_key.clone().try_into().expect("public_key must be 32 bytes");
|
|
40
|
+
let signature_array: [u8; 64] = signature_result.signature.clone().try_into().expect("signature must be 64 bytes");
|
|
41
|
+
let is_verified: bool = SHA512ED25519DigitalSignature::digital_signature_ed25519_verify(
|
|
42
|
+
public_key_array,
|
|
43
|
+
¬_original_data,
|
|
44
|
+
signature_array,
|
|
45
|
+
);
|
|
34
46
|
assert_eq!(is_verified, false);
|
|
35
47
|
}
|
|
@@ -10,8 +10,8 @@ pub struct CASSHAED25519DalekDigitalSignatureResult {
|
|
|
10
10
|
impl From<SHAED25519DalekDigitalSignatureResult> for CASSHAED25519DalekDigitalSignatureResult {
|
|
11
11
|
fn from(value: SHAED25519DalekDigitalSignatureResult) -> Self {
|
|
12
12
|
CASSHAED25519DalekDigitalSignatureResult {
|
|
13
|
-
public_key: value.public_key,
|
|
14
|
-
signature: value.signature
|
|
13
|
+
public_key: value.public_key.to_vec(),
|
|
14
|
+
signature: value.signature.to_vec()
|
|
15
15
|
}
|
|
16
16
|
}
|
|
17
17
|
}
|
package/src/hashers/sha.rs
CHANGED
|
@@ -39,7 +39,7 @@ pub fn sha_512_verify_test() {
|
|
|
39
39
|
#[test]
|
|
40
40
|
pub fn sha_512_verify_fail_test() {
|
|
41
41
|
let data_to_hash = "NotMyDataToHash".as_bytes().to_vec();
|
|
42
|
-
let
|
|
42
|
+
let _hashed_data = sha_512(data_to_hash.clone());
|
|
43
43
|
let data_to_verify = "NotMyDataToHash2".as_bytes().to_vec();
|
|
44
44
|
assert_ne!(true, sha_512_verify(data_to_hash, data_to_verify));
|
|
45
45
|
}
|
|
@@ -54,7 +54,7 @@ pub fn sha_256_test() {
|
|
|
54
54
|
#[test]
|
|
55
55
|
pub fn sha_256_verify_test() {
|
|
56
56
|
let data_to_hash = "NotMyDataToHash".as_bytes().to_vec();
|
|
57
|
-
let
|
|
57
|
+
let _hashed_data = sha_256(data_to_hash.clone());
|
|
58
58
|
let data_to_verify = "NotMyDataToHash".as_bytes().to_vec();
|
|
59
59
|
assert_ne!(true, sha_256_verify(data_to_hash, data_to_verify));
|
|
60
60
|
}
|
|
@@ -62,7 +62,7 @@ pub fn sha_256_verify_test() {
|
|
|
62
62
|
#[test]
|
|
63
63
|
pub fn sha_256_verify_fail_test() {
|
|
64
64
|
let data_to_hash = "NotMyDataToHash".as_bytes().to_vec();
|
|
65
|
-
let
|
|
65
|
+
let _hashed_data = sha_256(data_to_hash.clone());
|
|
66
66
|
let data_to_verify = "NotMyDataToHash2".as_bytes().to_vec();
|
|
67
67
|
assert_ne!(true, sha_256_verify(data_to_hash, data_to_verify));
|
|
68
68
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
use cas_lib::key_exchange::x25519::
|
|
1
|
+
use cas_lib::key_exchange::x25519::X25519SecretPublicKeyResult;
|
|
2
2
|
use napi_derive::napi;
|
|
3
3
|
|
|
4
4
|
#[napi(constructor)]
|
|
@@ -7,8 +7,8 @@ pub struct CASx25519SecretPublicKeyResult {
|
|
|
7
7
|
pub secret_key: Vec<u8>,
|
|
8
8
|
}
|
|
9
9
|
|
|
10
|
-
impl From<
|
|
11
|
-
fn from(value:
|
|
10
|
+
impl From<X25519SecretPublicKeyResult> for CASx25519SecretPublicKeyResult {
|
|
11
|
+
fn from(value: X25519SecretPublicKeyResult) -> Self {
|
|
12
12
|
CASx25519SecretPublicKeyResult {
|
|
13
13
|
public_key: value.public_key,
|
|
14
14
|
secret_key: value.secret_key
|
|
@@ -11,7 +11,7 @@ pub fn x25519_generate_secret_and_public_key() -> CASx25519SecretPublicKeyResult
|
|
|
11
11
|
|
|
12
12
|
#[napi]
|
|
13
13
|
pub fn x25519_diffie_hellman(my_secret_key: Vec<u8>, users_public_key: Vec<u8>) -> Vec<u8> {
|
|
14
|
-
return <X25519 as CASKeyExchange>::diffie_hellman(my_secret_key, users_public_key);
|
|
14
|
+
return <X25519 as CASKeyExchange>::diffie_hellman(my_secret_key, users_public_key).to_vec();
|
|
15
15
|
}
|
|
16
16
|
|
|
17
17
|
#[test]
|
|
@@ -1,26 +1,25 @@
|
|
|
1
1
|
|
|
2
2
|
use napi_derive::napi;
|
|
3
3
|
use cas_lib::password_hashers::argon2::CASArgon;
|
|
4
|
-
use cas_lib::password_hashers::cas_password_hasher::CASPasswordHasher;
|
|
5
4
|
|
|
6
5
|
#[napi]
|
|
7
6
|
pub fn argon2_hash(password: String) -> String {
|
|
8
|
-
return
|
|
7
|
+
return CASArgon::hash_password(password);
|
|
9
8
|
}
|
|
10
9
|
|
|
11
10
|
#[napi]
|
|
12
11
|
pub fn argon2_hash_thread_pool(password: String) -> String {
|
|
13
|
-
return
|
|
12
|
+
return CASArgon::hash_password_threadpool(password);
|
|
14
13
|
}
|
|
15
14
|
|
|
16
15
|
#[napi]
|
|
17
16
|
pub fn argon2_verify(hashed_password: String, password_to_verify: String) -> bool {
|
|
18
|
-
return
|
|
17
|
+
return CASArgon::verify_password(hashed_password, password_to_verify);
|
|
19
18
|
}
|
|
20
19
|
|
|
21
20
|
#[napi]
|
|
22
21
|
pub fn argon2_verify_threadpool(hashed_password: String, password_to_verify: String) -> bool {
|
|
23
|
-
return
|
|
22
|
+
return CASArgon::verify_password_threadpool(hashed_password, password_to_verify);
|
|
24
23
|
}
|
|
25
24
|
|
|
26
25
|
#[test]
|
|
@@ -9,7 +9,7 @@ pub fn bcrypt_hash(password_to_hash: String) -> String {
|
|
|
9
9
|
|
|
10
10
|
#[napi]
|
|
11
11
|
pub fn bcrypt_hash_threadpool(password_to_hash: String) -> String {
|
|
12
|
-
return <CASBCrypt as CASPasswordHasher>::
|
|
12
|
+
return <CASBCrypt as CASPasswordHasher>::hash_password_threadpool(password_to_hash);
|
|
13
13
|
}
|
|
14
14
|
|
|
15
15
|
#[napi]
|
|
@@ -13,7 +13,7 @@ pub fn scrypt_verify(hashed_password: String, password_to_verify: String) -> boo
|
|
|
13
13
|
|
|
14
14
|
#[napi]
|
|
15
15
|
pub fn scrypt_hash_threadpool(password_to_hash: String) -> String {
|
|
16
|
-
return <CASScrypt as CASPasswordHasher>::
|
|
16
|
+
return <CASScrypt as CASPasswordHasher>::hash_password_threadpool(password_to_hash);
|
|
17
17
|
}
|
|
18
18
|
|
|
19
19
|
#[napi]
|
|
@@ -4,7 +4,7 @@ use napi_derive::napi;
|
|
|
4
4
|
|
|
5
5
|
#[napi]
|
|
6
6
|
pub fn ascon128_key_generate() -> Vec<u8> {
|
|
7
|
-
return <AsconAead as CASAsconAead>::generate_key();
|
|
7
|
+
return <AsconAead as CASAsconAead>::generate_key().to_vec();
|
|
8
8
|
}
|
|
9
9
|
|
|
10
10
|
#[test]
|
|
@@ -15,7 +15,7 @@ fn test_ascon128_key_generate() {
|
|
|
15
15
|
|
|
16
16
|
#[napi]
|
|
17
17
|
pub fn ascon128_nonce_generate() -> Vec<u8> {
|
|
18
|
-
return <AsconAead as CASAsconAead>::generate_nonce();
|
|
18
|
+
return <AsconAead as CASAsconAead>::generate_nonce().to_vec();
|
|
19
19
|
}
|
|
20
20
|
|
|
21
21
|
#[test]
|
|
@@ -26,29 +26,45 @@ pub fn test_ascon128_nonce_generate() {
|
|
|
26
26
|
|
|
27
27
|
#[napi]
|
|
28
28
|
pub fn ascon128_encrypt(key: Vec<u8>, nonce: Vec<u8>, plaintext: Vec<u8>) -> Vec<u8> {
|
|
29
|
-
|
|
29
|
+
let key_arr: [u8; 16] = key.try_into().expect("Key must be 16 bytes");
|
|
30
|
+
let nonce_arr: [u8; 16] = nonce.try_into().expect("Nonce must be 16 bytes");
|
|
31
|
+
return <AsconAead as CASAsconAead>::encrypt(key_arr, nonce_arr, plaintext);
|
|
30
32
|
}
|
|
31
33
|
|
|
32
34
|
#[test]
|
|
33
35
|
pub fn test_ascon128_encrypt() {
|
|
34
36
|
let key = <AsconAead as CASAsconAead>::generate_key();
|
|
35
37
|
let nonce = <AsconAead as CASAsconAead>::generate_nonce();
|
|
36
|
-
let plaintext = b"Hello, World!"
|
|
37
|
-
let ciphertext = ascon128_encrypt(
|
|
38
|
-
|
|
38
|
+
let plaintext = b"Hello, World!";
|
|
39
|
+
let ciphertext = ascon128_encrypt(
|
|
40
|
+
key.clone().to_vec(),
|
|
41
|
+
nonce.clone().to_vec(),
|
|
42
|
+
plaintext.to_vec(),
|
|
43
|
+
);
|
|
44
|
+
assert_ne!(ciphertext, plaintext.to_vec());
|
|
39
45
|
}
|
|
40
46
|
|
|
41
47
|
#[napi]
|
|
42
48
|
pub fn ascon128_decrypt(key: Vec<u8>, nonce: Vec<u8>, ciphertext: Vec<u8>) -> Vec<u8> {
|
|
43
|
-
|
|
49
|
+
let key_arr: [u8; 16] = key.try_into().expect("Key must be 16 bytes");
|
|
50
|
+
let nonce_arr: [u8; 16] = nonce.try_into().expect("Nonce must be 16 bytes");
|
|
51
|
+
return <AsconAead as CASAsconAead>::decrypt(key_arr, nonce_arr, ciphertext);
|
|
44
52
|
}
|
|
45
53
|
|
|
46
54
|
#[test]
|
|
47
55
|
pub fn test_ascon128_decrypt() {
|
|
48
56
|
let key = <AsconAead as CASAsconAead>::generate_key();
|
|
49
57
|
let nonce = <AsconAead as CASAsconAead>::generate_nonce();
|
|
50
|
-
let plaintext = b"Hello, World!"
|
|
51
|
-
let ciphertext = ascon128_encrypt(
|
|
52
|
-
|
|
53
|
-
|
|
58
|
+
let plaintext = b"Hello, World!";
|
|
59
|
+
let ciphertext = ascon128_encrypt(
|
|
60
|
+
key.clone().to_vec(),
|
|
61
|
+
nonce.clone().to_vec(),
|
|
62
|
+
plaintext.to_vec(),
|
|
63
|
+
);
|
|
64
|
+
let decrypted = ascon128_decrypt(
|
|
65
|
+
key.clone().to_vec(),
|
|
66
|
+
nonce.clone().to_vec(),
|
|
67
|
+
ciphertext.clone(),
|
|
68
|
+
);
|
|
69
|
+
assert_eq!(decrypted, plaintext.to_vec());
|
|
54
70
|
}
|
package/src/symmetric/aes.rs
CHANGED
|
@@ -1,55 +1,65 @@
|
|
|
1
|
-
use cas_lib::symmetric::{aes::{CASAES128, CASAES256}, cas_symmetric_encryption::
|
|
1
|
+
use cas_lib::symmetric::{aes::{CASAES128, CASAES256}, cas_symmetric_encryption::{CASAES128Encryption, CASAES256Encryption}};
|
|
2
2
|
use napi_derive::napi;
|
|
3
3
|
|
|
4
4
|
use super::types::CASAesKeyFromX25519SharedSecret;
|
|
5
5
|
|
|
6
6
|
#[napi]
|
|
7
7
|
pub fn aes_nonce() -> Vec<u8> {
|
|
8
|
-
return <CASAES256 as
|
|
8
|
+
return <CASAES256 as CASAES256Encryption>::generate_nonce().to_vec();
|
|
9
9
|
}
|
|
10
10
|
|
|
11
11
|
#[napi]
|
|
12
12
|
pub fn aes128_key() -> Vec<u8> {
|
|
13
|
-
return <CASAES128 as
|
|
13
|
+
return <CASAES128 as CASAES128Encryption>::generate_key().to_vec();
|
|
14
14
|
}
|
|
15
15
|
|
|
16
16
|
#[napi]
|
|
17
17
|
pub fn aes256_key() -> Vec<u8> {
|
|
18
|
-
return <CASAES256 as
|
|
18
|
+
return <CASAES256 as CASAES256Encryption>::generate_key().to_vec();
|
|
19
19
|
}
|
|
20
20
|
|
|
21
21
|
#[napi]
|
|
22
22
|
pub fn aes128_encrypt(aes_key: Vec<u8>, nonce: Vec<u8>, plaintext: Vec<u8>) -> Vec<u8> {
|
|
23
|
-
|
|
23
|
+
let key: [u8; 16] = aes_key.try_into().expect("Key must be 16 bytes");
|
|
24
|
+
let nonce_arr: [u8; 12] = nonce.try_into().expect("Nonce must be 12 bytes");
|
|
25
|
+
<CASAES128 as CASAES128Encryption>::encrypt_plaintext(key, nonce_arr, plaintext)
|
|
24
26
|
}
|
|
25
27
|
|
|
26
28
|
#[napi]
|
|
27
29
|
pub fn aes128_decrypt(aes_key: Vec<u8>, nonce: Vec<u8>, ciphertext: Vec<u8>) -> Vec<u8> {
|
|
28
|
-
|
|
30
|
+
let key: [u8; 16] = aes_key.try_into().expect("Key must be 16 bytes");
|
|
31
|
+
let nonce_arr: [u8; 12] = nonce.try_into().expect("Nonce must be 12 bytes");
|
|
32
|
+
<CASAES128 as CASAES128Encryption>::decrypt_ciphertext(key, nonce_arr, ciphertext)
|
|
29
33
|
}
|
|
30
34
|
|
|
31
35
|
#[napi]
|
|
32
36
|
pub fn aes256_encrypt(aes_key: Vec<u8>, nonce: Vec<u8>, plaintext: Vec<u8>) -> Vec<u8> {
|
|
33
|
-
|
|
37
|
+
let key: [u8; 32] = aes_key.try_into().expect("Key must be 32 bytes");
|
|
38
|
+
let nonce_arr: [u8; 12] = nonce.try_into().expect("Nonce must be 12 bytes");
|
|
39
|
+
<CASAES256 as CASAES256Encryption>::encrypt_plaintext(key, nonce_arr, plaintext)
|
|
34
40
|
}
|
|
35
41
|
|
|
36
42
|
#[napi]
|
|
37
43
|
pub fn aes256_decrypt(aes_key: Vec<u8>, nonce: Vec<u8>, ciphertext: Vec<u8>) -> Vec<u8> {
|
|
38
|
-
|
|
44
|
+
let key: [u8; 32] = aes_key.try_into().expect("Key must be 32 bytes");
|
|
45
|
+
let nonce_arr: [u8; 12] = nonce.try_into().expect("Nonce must be 12 bytes");
|
|
46
|
+
<CASAES256 as CASAES256Encryption>::decrypt_ciphertext(key, nonce_arr, ciphertext)
|
|
39
47
|
}
|
|
40
48
|
|
|
41
49
|
#[napi]
|
|
42
50
|
pub fn aes_256_key_from_x25519_shared_secret(
|
|
43
51
|
shared_secret: Vec<u8>,
|
|
44
52
|
) -> CASAesKeyFromX25519SharedSecret {
|
|
45
|
-
|
|
53
|
+
let shared_secret_arr: [u8; 32] = shared_secret.try_into().expect("Shared secret must be 32 bytes");
|
|
54
|
+
return <CASAES256 as CASAES256Encryption>::key_from_x25519_shared_secret(shared_secret_arr).into();
|
|
46
55
|
}
|
|
47
56
|
|
|
48
57
|
#[napi]
|
|
49
58
|
pub fn aes_128_key_from_x25519_shared_secret(
|
|
50
59
|
shared_secret: Vec<u8>,
|
|
51
60
|
) -> CASAesKeyFromX25519SharedSecret {
|
|
52
|
-
|
|
61
|
+
let shared_secret_arr: [u8; 32] = shared_secret.try_into().expect("Shared secret must be 32 bytes");
|
|
62
|
+
return <CASAES128 as CASAES128Encryption>::key_from_x25519_shared_secret(shared_secret_arr).into();
|
|
53
63
|
}
|
|
54
64
|
|
|
55
65
|
#[test]
|
package/src/symmetric/types.rs
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
|
|
1
|
+
|
|
2
|
+
use cas_lib::symmetric::cas_symmetric_encryption::{Aes128KeyFromX25519SharedSecret, Aes256KeyFromX25519SharedSecret};
|
|
2
3
|
use napi_derive::napi;
|
|
3
4
|
|
|
4
5
|
#[napi(constructor)]
|
|
@@ -7,11 +8,20 @@ pub struct CASAesKeyFromX25519SharedSecret {
|
|
|
7
8
|
pub aes_nonce: Vec<u8>,
|
|
8
9
|
}
|
|
9
10
|
|
|
10
|
-
impl From<
|
|
11
|
-
fn from(value:
|
|
11
|
+
impl From<Aes128KeyFromX25519SharedSecret> for CASAesKeyFromX25519SharedSecret {
|
|
12
|
+
fn from(value: Aes128KeyFromX25519SharedSecret) -> Self {
|
|
13
|
+
CASAesKeyFromX25519SharedSecret {
|
|
14
|
+
aes_key: value.aes_key.to_vec(),
|
|
15
|
+
aes_nonce: value.aes_nonce.to_vec()
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
impl From<Aes256KeyFromX25519SharedSecret> for CASAesKeyFromX25519SharedSecret {
|
|
21
|
+
fn from(value: Aes256KeyFromX25519SharedSecret) -> Self {
|
|
12
22
|
CASAesKeyFromX25519SharedSecret {
|
|
13
|
-
aes_key: value.aes_key,
|
|
14
|
-
aes_nonce: value.aes_nonce
|
|
23
|
+
aes_key: value.aes_key.to_vec(),
|
|
24
|
+
aes_nonce: value.aes_nonce.to_vec()
|
|
15
25
|
}
|
|
16
26
|
}
|
|
17
27
|
}
|