cas-typescript-sdk 1.0.22 → 1.0.24
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 +3 -17
- package/README.md +7 -5
- package/index.d.ts +22 -21
- package/index.node +0 -0
- package/lib/asymmetric/RSAWrapper.d.ts +33 -3
- package/lib/asymmetric/RSAWrapper.js +33 -3
- package/lib/asymmetric/index.d.ts +2 -2
- package/lib/asymmetric/index.js +1 -3
- package/lib/digital-signature/digital-siganture-sha-512.d.ts +28 -3
- package/lib/digital-signature/digital-siganture-sha-512.js +25 -0
- package/lib/digital-signature/digital-signature-base.d.ts +3 -3
- package/lib/digital-signature/digital-signature-factory.d.ts +5 -0
- package/lib/digital-signature/digital-signature-factory.js +5 -0
- package/lib/digital-signature/digital-signaturte-sha-256.d.ts +28 -3
- package/lib/digital-signature/digital-signaturte-sha-256.js +25 -0
- package/lib/digital-signature/index.d.ts +2 -1
- package/lib/hashers/hasher-factory.d.ts +5 -0
- package/lib/hashers/hasher-factory.js +5 -0
- package/lib/hashers/sha-wrapper.d.ts +22 -0
- package/lib/hashers/sha-wrapper.js +22 -0
- package/lib/hybrid/hybrid-encryption-wrapper.d.ts +12 -0
- package/lib/hybrid/hybrid-encryption-wrapper.js +12 -0
- package/lib/hybrid/types/aes-rsa-hybrid-initializer.d.ts +7 -2
- package/lib/hybrid/types/aes-rsa-hybrid-initializer.js +5 -0
- package/lib/index.d.ts +8 -9
- package/lib/index.js +22 -29
- package/lib/key_exchange/index.d.ts +2 -1
- package/lib/key_exchange/x25519.d.ts +15 -3
- package/lib/key_exchange/x25519.js +12 -0
- package/lib/password-hashers/argon2-wrapper.d.ts +22 -0
- package/lib/password-hashers/argon2-wrapper.js +22 -0
- package/lib/password-hashers/bcrypt-wrapper.d.ts +22 -0
- package/lib/password-hashers/bcrypt-wrapper.js +22 -0
- package/lib/password-hashers/password-hasher-factory.d.ts +5 -0
- package/lib/password-hashers/password-hasher-factory.js +5 -0
- package/lib/password-hashers/scrypt-wrapper.d.ts +22 -0
- package/lib/password-hashers/scrypt-wrapper.js +22 -0
- package/lib/sponges/ascon-wrapper.d.ts +22 -0
- package/lib/sponges/ascon-wrapper.js +22 -0
- package/lib/symmetric/aes-wrapper.d.ts +45 -15
- package/lib/symmetric/aes-wrapper.js +42 -12
- package/lib/symmetric/index.d.ts +2 -1
- package/package.json +1 -1
- package/src/asymmetric/cas_rsa.rs +13 -59
- package/src/digital_signature/sha_256_ed25519.rs +6 -42
- package/src/digital_signature/sha_256_rsa.rs +7 -63
- package/src/digital_signature/sha_512_ed25519.rs +8 -48
- package/src/digital_signature/sha_512_rsa.rs +9 -62
- package/src/digital_signature/types.rs +34 -0
- package/src/hashers/sha.rs +1 -35
- package/src/key_exchange/types.rs +17 -0
- package/src/key_exchange/x25519.rs +4 -36
- package/src/lib.rs +3 -8
- package/src/password_hashers/argon2.rs +4 -44
- package/src/password_hashers/bcrypt.rs +4 -31
- package/src/password_hashers/scrypt.rs +3 -42
- package/src/sponges/ascon_aead.rs +9 -39
- package/src/symmetric/aes.rs +12 -88
- package/src/symmetric/types.rs +17 -0
- package/src-ts/asymmetric/RSAWrapper.ts +38 -5
- package/src-ts/asymmetric/index.ts +2 -2
- package/src-ts/digital-signature/digital-siganture-sha-512.ts +29 -4
- package/src-ts/digital-signature/digital-signature-base.ts +3 -3
- package/src-ts/digital-signature/digital-signature-factory.ts +6 -0
- package/src-ts/digital-signature/digital-signaturte-sha-256.ts +28 -3
- package/src-ts/digital-signature/index.ts +4 -1
- package/src-ts/hashers/hasher-factory.ts +5 -0
- package/src-ts/hashers/sha-wrapper.ts +22 -0
- package/src-ts/hybrid/hybrid-encryption-wrapper.ts +12 -0
- package/src-ts/hybrid/types/aes-rsa-hybrid-initializer.ts +7 -2
- package/src-ts/index.ts +8 -46
- package/src-ts/key_exchange/index.ts +2 -1
- package/src-ts/key_exchange/x25519.ts +15 -3
- package/src-ts/password-hashers/argon2-wrapper.ts +22 -0
- package/src-ts/password-hashers/bcrypt-wrapper.ts +22 -0
- package/src-ts/password-hashers/password-hasher-factory.ts +5 -0
- package/src-ts/password-hashers/scrypt-wrapper.ts +22 -0
- package/src-ts/sponges/ascon-wrapper.ts +22 -0
- package/src-ts/symmetric/aes-wrapper.ts +46 -15
- package/src-ts/symmetric/index.ts +2 -1
- package/test-ts/asymmetric.test.spec.ts +3 -3
- package/test-ts/digital-signature.test.spec.ts +5 -5
- package/test-ts/insecure-channel.test.spec.ts +5 -5
- package/lib/helpers/nonce-generator.d.ts +0 -3
- package/lib/helpers/nonce-generator.js +0 -34
- package/src/asymmetric/cas_asymmetric_encryption.rs +0 -15
- package/src/digital_signature/cas_digital_signature_rsa.rs +0 -27
- package/src/hashers/blake2.rs +0 -37
- package/src/hashers/cas_hasher.rs +0 -8
- package/src/key_exchange/cas_key_exchange.rs +0 -6
- package/src/password_hashers/cas_password_hasher.rs +0 -4
- package/src/sponges/cas_ascon_aead.rs +0 -6
- package/src/symmetric/cas_symmetric_encryption.rs +0 -14
- package/src-ts/helpers/nonce-generator.ts +0 -9
|
@@ -1,35 +1,7 @@
|
|
|
1
1
|
|
|
2
2
|
use napi_derive::napi;
|
|
3
|
-
|
|
4
|
-
use
|
|
5
|
-
password_hash::{rand_core::OsRng, SaltString},
|
|
6
|
-
Argon2, PasswordHash, PasswordHasher, PasswordVerifier,
|
|
7
|
-
};
|
|
8
|
-
|
|
9
|
-
use crate::symmetric::aes::CASAES128;
|
|
10
|
-
|
|
11
|
-
use super::cas_password_hasher::CASPasswordHasher;
|
|
12
|
-
|
|
13
|
-
pub struct CASArgon;
|
|
14
|
-
|
|
15
|
-
impl CASPasswordHasher for CASArgon {
|
|
16
|
-
fn hash_password(password_to_hash: String) -> String {
|
|
17
|
-
let salt = SaltString::generate(&mut OsRng);
|
|
18
|
-
let argon2 = Argon2::default();
|
|
19
|
-
let hashed_password = argon2
|
|
20
|
-
.hash_password(password_to_hash.as_bytes(), &salt)
|
|
21
|
-
.unwrap()
|
|
22
|
-
.to_string();
|
|
23
|
-
return hashed_password;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
fn verify_password(hashed_password: String, password_to_verify: String) -> bool {
|
|
27
|
-
let hashed_password = PasswordHash::new(&hashed_password).unwrap();
|
|
28
|
-
return Argon2::default()
|
|
29
|
-
.verify_password(password_to_verify.as_bytes(), &hashed_password)
|
|
30
|
-
.is_ok();
|
|
31
|
-
}
|
|
32
|
-
}
|
|
3
|
+
use cas_lib::password_hashers::argon2::CASArgon;
|
|
4
|
+
use cas_lib::password_hashers::cas_password_hasher::CASPasswordHasher;
|
|
33
5
|
|
|
34
6
|
#[napi]
|
|
35
7
|
pub fn argon2_hash(password: String) -> String {
|
|
@@ -38,13 +10,7 @@ pub fn argon2_hash(password: String) -> String {
|
|
|
38
10
|
|
|
39
11
|
#[napi]
|
|
40
12
|
pub fn argon2_hash_thread_pool(password: String) -> String {
|
|
41
|
-
|
|
42
|
-
rayon::spawn(move || {
|
|
43
|
-
let hash_result = <CASArgon as CASPasswordHasher>::hash_password(password);
|
|
44
|
-
sender.send(hash_result);
|
|
45
|
-
});
|
|
46
|
-
let result = receiver.recv().unwrap();
|
|
47
|
-
result
|
|
13
|
+
return <CASArgon as CASPasswordHasher>::hash__password_threadpool(password);
|
|
48
14
|
}
|
|
49
15
|
|
|
50
16
|
#[napi]
|
|
@@ -54,13 +20,7 @@ pub fn argon2_verify(hashed_password: String, password_to_verify: String) -> boo
|
|
|
54
20
|
|
|
55
21
|
#[napi]
|
|
56
22
|
pub fn argon2_verify_threadpool(hashed_password: String, password_to_verify: String) -> bool {
|
|
57
|
-
|
|
58
|
-
rayon::spawn(move || {
|
|
59
|
-
let verify_result = <CASArgon as CASPasswordHasher>::verify_password(hashed_password, password_to_verify);
|
|
60
|
-
sender.send(verify_result);
|
|
61
|
-
});
|
|
62
|
-
let result = receiver.recv().unwrap();
|
|
63
|
-
result
|
|
23
|
+
return <CASArgon as CASPasswordHasher>::verify_password_threadpool(hashed_password, password_to_verify);
|
|
64
24
|
}
|
|
65
25
|
|
|
66
26
|
#[test]
|
|
@@ -1,22 +1,7 @@
|
|
|
1
|
-
use
|
|
2
|
-
|
|
3
|
-
use bcrypt::{hash, verify, DEFAULT_COST};
|
|
1
|
+
use cas_lib::password_hashers::bcrypt::CASBCrypt;
|
|
2
|
+
use cas_lib::password_hashers::cas_password_hasher::CASPasswordHasher;
|
|
4
3
|
use napi_derive::napi;
|
|
5
4
|
|
|
6
|
-
use super::cas_password_hasher::CASPasswordHasher;
|
|
7
|
-
|
|
8
|
-
pub struct CASBCrypt;
|
|
9
|
-
|
|
10
|
-
impl CASPasswordHasher for CASBCrypt {
|
|
11
|
-
fn hash_password(password_to_hash: String) -> String {
|
|
12
|
-
return hash(password_to_hash, DEFAULT_COST).unwrap();
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
fn verify_password(hashed_password: String, password_to_verify: String) -> bool {
|
|
16
|
-
return verify(password_to_verify, &hashed_password).unwrap();
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
|
|
20
5
|
#[napi]
|
|
21
6
|
pub fn bcrypt_hash(password_to_hash: String) -> String {
|
|
22
7
|
return <CASBCrypt as CASPasswordHasher>::hash_password(password_to_hash);
|
|
@@ -24,13 +9,7 @@ pub fn bcrypt_hash(password_to_hash: String) -> String {
|
|
|
24
9
|
|
|
25
10
|
#[napi]
|
|
26
11
|
pub fn bcrypt_hash_threadpool(password_to_hash: String) -> String {
|
|
27
|
-
|
|
28
|
-
rayon::spawn(move || {
|
|
29
|
-
let thread_result = <CASBCrypt as CASPasswordHasher>::hash_password(password_to_hash);
|
|
30
|
-
sender.send(thread_result);
|
|
31
|
-
});
|
|
32
|
-
let result = receiver.recv().unwrap();
|
|
33
|
-
result
|
|
12
|
+
return <CASBCrypt as CASPasswordHasher>::hash__password_threadpool(password_to_hash);
|
|
34
13
|
}
|
|
35
14
|
|
|
36
15
|
#[napi]
|
|
@@ -40,13 +19,7 @@ pub fn bcrypt_verify(hashed_password: String, password_to_verify: String) -> boo
|
|
|
40
19
|
|
|
41
20
|
#[napi]
|
|
42
21
|
pub fn bcrypt_verify_threadpool(password_to_hash: String, password_to_verify: String) -> bool {
|
|
43
|
-
|
|
44
|
-
rayon::spawn(move || {
|
|
45
|
-
let thread_result = <CASBCrypt as CASPasswordHasher>::verify_password(password_to_hash, password_to_verify);
|
|
46
|
-
sender.send(thread_result);
|
|
47
|
-
});
|
|
48
|
-
let result = receiver.recv().unwrap();
|
|
49
|
-
result
|
|
22
|
+
return <CASBCrypt as CASPasswordHasher>::verify_password_threadpool(password_to_hash, password_to_verify);
|
|
50
23
|
}
|
|
51
24
|
|
|
52
25
|
#[test]
|
|
@@ -1,33 +1,6 @@
|
|
|
1
|
-
use
|
|
2
|
-
|
|
1
|
+
use cas_lib::password_hashers::{cas_password_hasher::CASPasswordHasher, scrypt::CASScrypt};
|
|
3
2
|
use napi_derive::napi;
|
|
4
3
|
|
|
5
|
-
use scrypt::{
|
|
6
|
-
password_hash::{rand_core::OsRng, PasswordHash, PasswordHasher, PasswordVerifier, SaltString},
|
|
7
|
-
Scrypt,
|
|
8
|
-
};
|
|
9
|
-
|
|
10
|
-
use super::cas_password_hasher::CASPasswordHasher;
|
|
11
|
-
|
|
12
|
-
pub struct CASScrypt;
|
|
13
|
-
|
|
14
|
-
impl CASPasswordHasher for CASScrypt {
|
|
15
|
-
fn hash_password(password_to_hash: String) -> String {
|
|
16
|
-
let salt = SaltString::generate(&mut OsRng);
|
|
17
|
-
return Scrypt
|
|
18
|
-
.hash_password(password_to_hash.as_bytes(), &salt)
|
|
19
|
-
.unwrap()
|
|
20
|
-
.to_string();
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
fn verify_password(hashed_password: String, password_to_verify: String) -> bool {
|
|
24
|
-
let parsed_hash = PasswordHash::new(&hashed_password).unwrap();
|
|
25
|
-
return Scrypt
|
|
26
|
-
.verify_password(password_to_verify.as_bytes(), &parsed_hash)
|
|
27
|
-
.is_ok();
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
4
|
#[napi]
|
|
32
5
|
pub fn scrypt_hash(password_to_hash: String) -> String {
|
|
33
6
|
return <CASScrypt as CASPasswordHasher>::hash_password(password_to_hash);
|
|
@@ -40,24 +13,12 @@ pub fn scrypt_verify(hashed_password: String, password_to_verify: String) -> boo
|
|
|
40
13
|
|
|
41
14
|
#[napi]
|
|
42
15
|
pub fn scrypt_hash_threadpool(password_to_hash: String) -> String {
|
|
43
|
-
|
|
44
|
-
rayon::spawn(move || {
|
|
45
|
-
let thread_result = <CASScrypt as CASPasswordHasher>::hash_password(password_to_hash);
|
|
46
|
-
sender.send(thread_result);
|
|
47
|
-
});
|
|
48
|
-
let result = receiver.recv().unwrap();
|
|
49
|
-
result
|
|
16
|
+
return <CASScrypt as CASPasswordHasher>::hash__password_threadpool(password_to_hash);
|
|
50
17
|
}
|
|
51
18
|
|
|
52
19
|
#[napi]
|
|
53
20
|
pub fn scrypt_verify_threadpool(hashed_password: String, password_to_verify: String) -> bool {
|
|
54
|
-
|
|
55
|
-
rayon::spawn(move || {
|
|
56
|
-
let thread_result = <CASScrypt as CASPasswordHasher>::verify_password(hashed_password, password_to_verify);
|
|
57
|
-
sender.send(thread_result);
|
|
58
|
-
});
|
|
59
|
-
let result = receiver.recv().unwrap();
|
|
60
|
-
result
|
|
21
|
+
return <CASScrypt as CASPasswordHasher>::verify_password_threadpool(hashed_password, password_to_verify);
|
|
61
22
|
}
|
|
62
23
|
|
|
63
24
|
#[test]
|
|
@@ -1,40 +1,10 @@
|
|
|
1
1
|
|
|
2
|
-
use
|
|
3
|
-
use ascon_aead::{aead::{generic_array::GenericArray, Aead, KeyInit, OsRng}, Ascon128};
|
|
2
|
+
use cas_lib::sponges::{ascon_aead::AsconAead, cas_ascon_aead::CASAsconAead};
|
|
4
3
|
use napi_derive::napi;
|
|
5
4
|
|
|
6
|
-
use super::cas_ascon_aead::{CASAsconAead};
|
|
7
|
-
pub struct AsconAead;
|
|
8
|
-
|
|
9
|
-
impl CASAsconAead for AsconAead {
|
|
10
|
-
fn encrypt(key: Vec<u8>, nonce: Vec<u8>, plaintext: Vec<u8>) -> Vec<u8> {
|
|
11
|
-
let key_generic_array = GenericArray::from_slice(&key);
|
|
12
|
-
let nonce_generic_array = GenericArray::from_slice(&nonce);
|
|
13
|
-
let cipher = Ascon128::new(key_generic_array);
|
|
14
|
-
let ciphertext = cipher.encrypt(&nonce_generic_array, plaintext.as_ref()).unwrap();
|
|
15
|
-
ciphertext
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
fn decrypt(key: Vec<u8>, nonce: Vec<u8>, ciphertext: Vec<u8>) -> Vec<u8> {
|
|
19
|
-
let key_generic_array = GenericArray::from_slice(&key);
|
|
20
|
-
let nonce_generic_array = GenericArray::from_slice(&nonce);
|
|
21
|
-
let cipher = Ascon128::new(key_generic_array);
|
|
22
|
-
let plaintext = cipher.decrypt(&nonce_generic_array, ciphertext.as_ref()).unwrap();
|
|
23
|
-
plaintext
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
fn generate_key() -> Vec<u8> {
|
|
27
|
-
return Ascon128::generate_key(&mut OsRng).to_vec();
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
fn generate_nonce() -> Vec<u8> {
|
|
31
|
-
return Ascon128::generate_nonce(&mut OsRng).to_vec();
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
|
|
35
5
|
#[napi]
|
|
36
6
|
pub fn ascon128_key_generate() -> Vec<u8> {
|
|
37
|
-
return AsconAead
|
|
7
|
+
return <AsconAead as CASAsconAead>::generate_key();
|
|
38
8
|
}
|
|
39
9
|
|
|
40
10
|
#[test]
|
|
@@ -45,7 +15,7 @@ fn test_ascon128_key_generate() {
|
|
|
45
15
|
|
|
46
16
|
#[napi]
|
|
47
17
|
pub fn ascon128_nonce_generate() -> Vec<u8> {
|
|
48
|
-
return AsconAead
|
|
18
|
+
return <AsconAead as CASAsconAead>::generate_nonce();
|
|
49
19
|
}
|
|
50
20
|
|
|
51
21
|
#[test]
|
|
@@ -56,13 +26,13 @@ pub fn test_ascon128_nonce_generate() {
|
|
|
56
26
|
|
|
57
27
|
#[napi]
|
|
58
28
|
pub fn ascon128_encrypt(key: Vec<u8>, nonce: Vec<u8>, plaintext: Vec<u8>) -> Vec<u8> {
|
|
59
|
-
return AsconAead
|
|
29
|
+
return <AsconAead as CASAsconAead>::encrypt(key, nonce, plaintext);
|
|
60
30
|
}
|
|
61
31
|
|
|
62
32
|
#[test]
|
|
63
33
|
pub fn test_ascon128_encrypt() {
|
|
64
|
-
let key = AsconAead
|
|
65
|
-
let nonce = AsconAead
|
|
34
|
+
let key = <AsconAead as CASAsconAead>::generate_key();
|
|
35
|
+
let nonce = <AsconAead as CASAsconAead>::generate_nonce();
|
|
66
36
|
let plaintext = b"Hello, World!".to_vec();
|
|
67
37
|
let ciphertext = ascon128_encrypt(key.clone(), nonce.clone(), plaintext.clone());
|
|
68
38
|
assert_ne!(ciphertext, plaintext);
|
|
@@ -70,13 +40,13 @@ pub fn test_ascon128_encrypt() {
|
|
|
70
40
|
|
|
71
41
|
#[napi]
|
|
72
42
|
pub fn ascon128_decrypt(key: Vec<u8>, nonce: Vec<u8>, ciphertext: Vec<u8>) -> Vec<u8> {
|
|
73
|
-
return AsconAead
|
|
43
|
+
return <AsconAead as CASAsconAead>::decrypt(key, nonce, ciphertext);
|
|
74
44
|
}
|
|
75
45
|
|
|
76
46
|
#[test]
|
|
77
47
|
pub fn test_ascon128_decrypt() {
|
|
78
|
-
let key = AsconAead
|
|
79
|
-
let nonce = AsconAead
|
|
48
|
+
let key = <AsconAead as CASAsconAead>::generate_key();
|
|
49
|
+
let nonce = <AsconAead as CASAsconAead>::generate_nonce();
|
|
80
50
|
let plaintext = b"Hello, World!".to_vec();
|
|
81
51
|
let ciphertext = ascon128_encrypt(key.clone(), nonce.clone(), plaintext.clone());
|
|
82
52
|
let decrypted = ascon128_decrypt(key.clone(), nonce.clone(), ciphertext.clone());
|
package/src/symmetric/aes.rs
CHANGED
|
@@ -1,85 +1,9 @@
|
|
|
1
|
-
use
|
|
1
|
+
use cas_lib::symmetric::{aes::{CASAES128, CASAES256}, cas_symmetric_encryption::CASAESEncryption};
|
|
2
2
|
use napi_derive::napi;
|
|
3
|
-
use rand::rngs::OsRng;
|
|
4
3
|
use rand::{RngCore, SeedableRng};
|
|
5
4
|
use rand_chacha::ChaCha20Rng;
|
|
6
5
|
|
|
7
|
-
use
|
|
8
|
-
aead::{generic_array::GenericArray, Aead},
|
|
9
|
-
Aes128Gcm, Aes256Gcm, KeyInit, Nonce,
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
use super::cas_symmetric_encryption::{AesKeyFromX25519SharedSecret, CASAESEncryption};
|
|
13
|
-
pub struct CASAES128;
|
|
14
|
-
pub struct CASAES256;
|
|
15
|
-
|
|
16
|
-
impl CASAESEncryption for CASAES256 {
|
|
17
|
-
fn generate_key() -> Vec<u8> {
|
|
18
|
-
return Aes256Gcm::generate_key(&mut OsRng).to_vec();
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
fn encrypt_plaintext(aes_key: Vec<u8>, nonce: Vec<u8>, plaintext: Vec<u8>) -> Vec<u8> {
|
|
22
|
-
let key = GenericArray::from_slice(&aes_key);
|
|
23
|
-
let mut cipher = Aes256Gcm::new(&key);
|
|
24
|
-
let nonce = Nonce::from_slice(&nonce);
|
|
25
|
-
let ciphertext = cipher.encrypt(nonce, plaintext.as_ref()).unwrap();
|
|
26
|
-
ciphertext
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
fn decrypt_ciphertext(aes_key: Vec<u8>, nonce: Vec<u8>, ciphertext: Vec<u8>) -> Vec<u8> {
|
|
30
|
-
let key = GenericArray::from_slice(&aes_key);
|
|
31
|
-
let mut cipher = Aes256Gcm::new(&key);
|
|
32
|
-
let nonce = Nonce::from_slice(&nonce);
|
|
33
|
-
let plaintext = cipher.decrypt(nonce, ciphertext.as_ref()).unwrap();
|
|
34
|
-
plaintext
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
fn key_from_x25519_shared_secret(shared_secret: Vec<u8>) -> AesKeyFromX25519SharedSecret {
|
|
38
|
-
let aes_key = Key::<Aes256Gcm>::from_slice(&shared_secret);
|
|
39
|
-
let mut aes_nonce: [u8; 12] = Default::default();
|
|
40
|
-
aes_nonce.copy_from_slice(&shared_secret[..12]);
|
|
41
|
-
let result = AesKeyFromX25519SharedSecret {
|
|
42
|
-
aes_key: aes_key.to_vec(),
|
|
43
|
-
aes_nonce: aes_nonce.to_vec(),
|
|
44
|
-
};
|
|
45
|
-
result
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
impl CASAESEncryption for CASAES128 {
|
|
50
|
-
fn generate_key() -> Vec<u8> {
|
|
51
|
-
return Aes128Gcm::generate_key(&mut OsRng).to_vec();
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
fn encrypt_plaintext(aes_key: Vec<u8>, nonce: Vec<u8>, plaintext: Vec<u8>) -> Vec<u8> {
|
|
55
|
-
let key = GenericArray::from_slice(&aes_key);
|
|
56
|
-
let mut cipher = Aes128Gcm::new(&key);
|
|
57
|
-
let nonce = Nonce::from_slice(&nonce);
|
|
58
|
-
let ciphertext = cipher.encrypt(nonce, plaintext.as_ref()).unwrap();
|
|
59
|
-
ciphertext
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
fn decrypt_ciphertext(aes_key: Vec<u8>, nonce: Vec<u8>, ciphertext: Vec<u8>) -> Vec<u8> {
|
|
63
|
-
let key = GenericArray::from_slice(&aes_key);
|
|
64
|
-
let mut cipher = Aes128Gcm::new(&key);
|
|
65
|
-
let nonce = Nonce::from_slice(&nonce);
|
|
66
|
-
let plaintext = cipher.decrypt(nonce, ciphertext.as_ref()).unwrap();
|
|
67
|
-
plaintext
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
fn key_from_x25519_shared_secret(shared_secret: Vec<u8>) -> AesKeyFromX25519SharedSecret {
|
|
71
|
-
let mut aes_key: [u8; 16] = Default::default();
|
|
72
|
-
aes_key.copy_from_slice(&shared_secret[..16]);
|
|
73
|
-
let aes_key_slice = Key::<Aes128Gcm>::from_slice(&aes_key);
|
|
74
|
-
let mut aes_nonce: [u8; 12] = Default::default();
|
|
75
|
-
aes_nonce.copy_from_slice(&shared_secret[..12]);
|
|
76
|
-
let result = AesKeyFromX25519SharedSecret {
|
|
77
|
-
aes_key: aes_key_slice.to_vec(),
|
|
78
|
-
aes_nonce: aes_nonce.to_vec(),
|
|
79
|
-
};
|
|
80
|
-
result
|
|
81
|
-
}
|
|
82
|
-
}
|
|
6
|
+
use super::types::CASAesKeyFromX25519SharedSecret;
|
|
83
7
|
|
|
84
8
|
#[napi]
|
|
85
9
|
pub fn aes_nonce() -> Vec<u8> {
|
|
@@ -92,46 +16,46 @@ pub fn aes_nonce() -> Vec<u8> {
|
|
|
92
16
|
|
|
93
17
|
#[napi]
|
|
94
18
|
pub fn aes128_key() -> Vec<u8> {
|
|
95
|
-
return CASAES128
|
|
19
|
+
return <CASAES128 as CASAESEncryption>::generate_key();
|
|
96
20
|
}
|
|
97
21
|
|
|
98
22
|
#[napi]
|
|
99
23
|
pub fn aes256_key() -> Vec<u8> {
|
|
100
|
-
return CASAES256
|
|
24
|
+
return <CASAES256 as CASAESEncryption>::generate_key();
|
|
101
25
|
}
|
|
102
26
|
|
|
103
27
|
#[napi]
|
|
104
28
|
pub fn aes128_encrypt(aes_key: Vec<u8>, nonce: Vec<u8>, plaintext: Vec<u8>) -> Vec<u8> {
|
|
105
|
-
return CASAES128
|
|
29
|
+
return <CASAES128 as CASAESEncryption>::encrypt_plaintext(aes_key, nonce, plaintext);
|
|
106
30
|
}
|
|
107
31
|
|
|
108
32
|
#[napi]
|
|
109
33
|
pub fn aes128_decrypt(aes_key: Vec<u8>, nonce: Vec<u8>, ciphertext: Vec<u8>) -> Vec<u8> {
|
|
110
|
-
return CASAES128
|
|
34
|
+
return <CASAES128 as CASAESEncryption>::decrypt_ciphertext(aes_key, nonce, ciphertext);
|
|
111
35
|
}
|
|
112
36
|
|
|
113
37
|
#[napi]
|
|
114
38
|
pub fn aes256_encrypt(aes_key: Vec<u8>, nonce: Vec<u8>, plaintext: Vec<u8>) -> Vec<u8> {
|
|
115
|
-
return CASAES256
|
|
39
|
+
return <CASAES256 as CASAESEncryption>::encrypt_plaintext(aes_key, nonce, plaintext);
|
|
116
40
|
}
|
|
117
41
|
|
|
118
42
|
#[napi]
|
|
119
43
|
pub fn aes256_decrypt(aes_key: Vec<u8>, nonce: Vec<u8>, ciphertext: Vec<u8>) -> Vec<u8> {
|
|
120
|
-
return CASAES256
|
|
44
|
+
return <CASAES256 as CASAESEncryption>::decrypt_ciphertext(aes_key, nonce, ciphertext);
|
|
121
45
|
}
|
|
122
46
|
|
|
123
47
|
#[napi]
|
|
124
48
|
pub fn aes_256_key_from_x25519_shared_secret(
|
|
125
49
|
shared_secret: Vec<u8>,
|
|
126
|
-
) ->
|
|
127
|
-
return CASAES256
|
|
50
|
+
) -> CASAesKeyFromX25519SharedSecret {
|
|
51
|
+
return <CASAES256 as CASAESEncryption>::key_from_x25519_shared_secret(shared_secret).into();
|
|
128
52
|
}
|
|
129
53
|
|
|
130
54
|
#[napi]
|
|
131
55
|
pub fn aes_128_key_from_x25519_shared_secret(
|
|
132
56
|
shared_secret: Vec<u8>,
|
|
133
|
-
) ->
|
|
134
|
-
return CASAES128
|
|
57
|
+
) -> CASAesKeyFromX25519SharedSecret {
|
|
58
|
+
return <CASAES128 as CASAESEncryption>::key_from_x25519_shared_secret(shared_secret).into();
|
|
135
59
|
}
|
|
136
60
|
|
|
137
61
|
#[test]
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
use cas_lib::symmetric::cas_symmetric_encryption::AesKeyFromX25519SharedSecret;
|
|
2
|
+
use napi_derive::napi;
|
|
3
|
+
|
|
4
|
+
#[napi(constructor)]
|
|
5
|
+
pub struct CASAesKeyFromX25519SharedSecret {
|
|
6
|
+
pub aes_key: Vec<u8>,
|
|
7
|
+
pub aes_nonce: Vec<u8>,
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
impl From<AesKeyFromX25519SharedSecret> for CASAesKeyFromX25519SharedSecret {
|
|
11
|
+
fn from(value: AesKeyFromX25519SharedSecret) -> Self {
|
|
12
|
+
CASAesKeyFromX25519SharedSecret {
|
|
13
|
+
aes_key: value.aes_key,
|
|
14
|
+
aes_nonce: value.aes_nonce
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -1,13 +1,26 @@
|
|
|
1
|
-
import { decryptCiphertextRsa, encryptPlaintextRsa, generateRsaKeys,
|
|
1
|
+
import { CASRSAKeyPairResult, decryptCiphertextRsa, encryptPlaintextRsa, generateRsaKeys, signRsa, verifyRsa } from "../../index";
|
|
2
2
|
|
|
3
3
|
export class RSAWrapper {
|
|
4
|
-
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Generates an RSA key pair based of parameter sent in 1024, 2048, and 4096 are supported.
|
|
7
|
+
* @param keySize
|
|
8
|
+
* @returns CASRSAKeyPairResult
|
|
9
|
+
*/
|
|
10
|
+
public generateKeys(keySize: number): CASRSAKeyPairResult {
|
|
5
11
|
if (keySize !== 1024 && keySize !== 2048 && keySize !== 4096) {
|
|
6
12
|
throw new Error("You must provide an appropriate key size to generate RSA keys");
|
|
7
13
|
}
|
|
8
14
|
return generateRsaKeys(keySize);
|
|
9
15
|
}
|
|
10
16
|
|
|
17
|
+
/**
|
|
18
|
+
* Encrypts a plaintext byte array with a RSA public key
|
|
19
|
+
* @param publicKey
|
|
20
|
+
* @param plaintext
|
|
21
|
+
* @returns Array<number>
|
|
22
|
+
*/
|
|
23
|
+
|
|
11
24
|
public encrypt(publicKey: string, plaintext: Array<number>): Array<number> {
|
|
12
25
|
if (!publicKey) {
|
|
13
26
|
throw new Error("You must provide a public key to encrypt with RSA");
|
|
@@ -18,6 +31,13 @@ export class RSAWrapper {
|
|
|
18
31
|
return encryptPlaintextRsa(publicKey, plaintext);
|
|
19
32
|
}
|
|
20
33
|
|
|
34
|
+
/**
|
|
35
|
+
* Decrypts a ciphertext with an RSA private key.
|
|
36
|
+
* @param privateKey
|
|
37
|
+
* @param ciphertext
|
|
38
|
+
* @returns Array<number>
|
|
39
|
+
*/
|
|
40
|
+
|
|
21
41
|
public decrypt(privateKey: string, ciphertext: Array<number>): Array<number> {
|
|
22
42
|
if (!privateKey) {
|
|
23
43
|
throw new Error("You must provide a private key to encrypt with RSA");
|
|
@@ -28,16 +48,29 @@ export class RSAWrapper {
|
|
|
28
48
|
return decryptCiphertextRsa(privateKey, ciphertext);
|
|
29
49
|
}
|
|
30
50
|
|
|
31
|
-
|
|
51
|
+
/**
|
|
52
|
+
* Signs a byte array with an RSA private key for verification.
|
|
53
|
+
* @param privateKey
|
|
54
|
+
* @param hash
|
|
55
|
+
* @returns Array<number>
|
|
56
|
+
*/
|
|
57
|
+
public sign(privateKey: string, dataToSign: Array<number>): Array<number> {
|
|
32
58
|
if (!privateKey) {
|
|
33
59
|
throw new Error("You must provide a private key to sign with RSA");
|
|
34
60
|
}
|
|
35
|
-
if (!
|
|
61
|
+
if (!dataToSign || dataToSign.length === 0) {
|
|
36
62
|
throw new Error("You must provide an allocated hash to sign with RSA");
|
|
37
63
|
}
|
|
38
|
-
return signRsa(privateKey,
|
|
64
|
+
return signRsa(privateKey, dataToSign);
|
|
39
65
|
}
|
|
40
66
|
|
|
67
|
+
/**
|
|
68
|
+
* Verifies signed data by the corresponding private key with an RSA public key.
|
|
69
|
+
* @param publicKey
|
|
70
|
+
* @param hash
|
|
71
|
+
* @param signature
|
|
72
|
+
* @returns boolean
|
|
73
|
+
*/
|
|
41
74
|
public verify(publicKey: string, hash: Array<number>, signature: Array<number>): boolean {
|
|
42
75
|
if (!publicKey) {
|
|
43
76
|
throw new Error("You must provide a public key to verify with RSA");
|
|
@@ -1,15 +1,27 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { CASRSADigitalSignatureResult, CASSHAED25519DalekDigitalSignatureResult, sha512Ed25519DigitalSignature, sha512Ed25519DigitalSignatureVerify, sha512RsaDigitalSignature, sha512RsaVerifyDigitalSignature } from "../../index";
|
|
2
2
|
import { IDigitalSignature } from "./digital-signature-base";
|
|
3
3
|
|
|
4
4
|
export class DigitalSignatureSHA512Wrapper implements IDigitalSignature {
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
/**
|
|
7
|
+
* Creates an ED25519 siganture from an array of bytes with SHA3-512.
|
|
8
|
+
* @param dataToSign
|
|
9
|
+
* @returns CASSHAED25519DalekDigitalSignatureResult
|
|
10
|
+
*/
|
|
11
|
+
createED25519(dataToSign: number[]): CASSHAED25519DalekDigitalSignatureResult {
|
|
7
12
|
if (dataToSign?.length === 0) {
|
|
8
13
|
throw new Error("Must provide allocated data to sign");
|
|
9
14
|
}
|
|
10
15
|
return sha512Ed25519DigitalSignature(dataToSign);
|
|
11
16
|
}
|
|
12
|
-
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Verifies an ED25519 signature with the public key generated from running createED25519() with SHA3-512
|
|
20
|
+
* @param publicKey
|
|
21
|
+
* @param dataToVerify
|
|
22
|
+
* @param signature
|
|
23
|
+
* @returns boolean
|
|
24
|
+
*/
|
|
13
25
|
verifyED25519(publicKey: number[], dataToVerify: number[], signature: number[]): boolean {
|
|
14
26
|
if (!publicKey) {
|
|
15
27
|
throw new Error("You must provide a public key for verify with ED25519");
|
|
@@ -23,7 +35,13 @@ export class DigitalSignatureSHA512Wrapper implements IDigitalSignature {
|
|
|
23
35
|
return sha512Ed25519DigitalSignatureVerify(publicKey, dataToVerify, signature);
|
|
24
36
|
}
|
|
25
37
|
|
|
26
|
-
|
|
38
|
+
/**
|
|
39
|
+
* Generates and RSA digital signature with SHA3-512
|
|
40
|
+
* @param rsa_key_size
|
|
41
|
+
* @param data_to_sign
|
|
42
|
+
* @returns CASRSADigitalSignatureResult
|
|
43
|
+
*/
|
|
44
|
+
createRsa(rsa_key_size: number, data_to_sign: number[]): CASRSADigitalSignatureResult {
|
|
27
45
|
if (rsa_key_size !== 1024 && rsa_key_size !== 2048 && rsa_key_size !== 4096) {
|
|
28
46
|
throw new Error("You need to provide an appropriate RSA key size.");
|
|
29
47
|
}
|
|
@@ -33,6 +51,13 @@ export class DigitalSignatureSHA512Wrapper implements IDigitalSignature {
|
|
|
33
51
|
return sha512RsaDigitalSignature(rsa_key_size, data_to_sign);
|
|
34
52
|
}
|
|
35
53
|
|
|
54
|
+
/**
|
|
55
|
+
* Verifies a digital signature created with the RSA public key.
|
|
56
|
+
* @param public_key
|
|
57
|
+
* @param data_to_verify
|
|
58
|
+
* @param signature
|
|
59
|
+
* @returns boolean
|
|
60
|
+
*/
|
|
36
61
|
verifyRSa(public_key: string, data_to_verify: number[], signature: number[]): boolean {
|
|
37
62
|
if (!public_key) {
|
|
38
63
|
throw new Error("Must provide a public key");
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { CASRSADigitalSignatureResult, CASSHAED25519DalekDigitalSignatureResult } from "../../index";
|
|
2
2
|
|
|
3
3
|
export interface IDigitalSignature {
|
|
4
|
-
createRsa(rsa_key_size: number, data_to_sign: Array<number>):
|
|
4
|
+
createRsa(rsa_key_size: number, data_to_sign: Array<number>): CASRSADigitalSignatureResult;
|
|
5
5
|
verifyRSa(public_key: string, data_to_verify: Array<number>, signature: Array<number>): boolean;
|
|
6
|
-
createED25519(dataToSign: Array<number>):
|
|
6
|
+
createED25519(dataToSign: Array<number>): CASSHAED25519DalekDigitalSignatureResult;
|
|
7
7
|
verifyED25519(publicKey: Array<number>, dataToVerify: Array<number>, signature: Array<number>): boolean;
|
|
8
8
|
}
|
|
@@ -7,6 +7,12 @@ export enum DigitalSignatureType {
|
|
|
7
7
|
}
|
|
8
8
|
|
|
9
9
|
export class DigitalSignatureFactory {
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Get the appropriate digital signature wrapper based upon the type passed in.
|
|
13
|
+
* @param type
|
|
14
|
+
* @returns
|
|
15
|
+
*/
|
|
10
16
|
public static get(type: DigitalSignatureType) {
|
|
11
17
|
let ds = new DigitalSignatureSHA512Wrapper();
|
|
12
18
|
switch (type) {
|
|
@@ -1,15 +1,27 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { CASRSADigitalSignatureResult, CASSHAED25519DalekDigitalSignatureResult, sha256Ed25519DigitalSignature, sha256Ed25519DigitalSignatureVerify, sha256RsaDigitalSignature, sha256RsaVerifyDigitalSignature, sha512Ed25519DigitalSignature } from "../../index";
|
|
2
2
|
import { IDigitalSignature } from "./digital-signature-base";
|
|
3
3
|
|
|
4
4
|
export class DigitalSignatureSHA256Wrapper implements IDigitalSignature {
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
/**
|
|
7
|
+
* Creates an ED25519 siganture from an array of bytes with SHA3-512.
|
|
8
|
+
* @param dataToSign
|
|
9
|
+
* @returns SHAED25519DalekDigitalSignatureResult
|
|
10
|
+
*/
|
|
11
|
+
createED25519(dataToSign: number[]): CASSHAED25519DalekDigitalSignatureResult {
|
|
7
12
|
if (dataToSign?.length === 0) {
|
|
8
13
|
throw new Error("Must provide allocated data to sign");
|
|
9
14
|
}
|
|
10
15
|
return sha256Ed25519DigitalSignature(dataToSign);
|
|
11
16
|
}
|
|
12
17
|
|
|
18
|
+
/**
|
|
19
|
+
* Verifies an ED25519 signature with the public key generated from running createED25519() with SHA3-512
|
|
20
|
+
* @param publicKey
|
|
21
|
+
* @param dataToVerify
|
|
22
|
+
* @param signature
|
|
23
|
+
* @returns boolean
|
|
24
|
+
*/
|
|
13
25
|
verifyED25519(publicKey: number[], dataToVerify: number[], signature: number[]): boolean {
|
|
14
26
|
if (!publicKey) {
|
|
15
27
|
throw new Error("You must provide a public key for verify with ED25519");
|
|
@@ -23,7 +35,13 @@ export class DigitalSignatureSHA256Wrapper implements IDigitalSignature {
|
|
|
23
35
|
return sha256Ed25519DigitalSignatureVerify(publicKey, dataToVerify, signature);
|
|
24
36
|
}
|
|
25
37
|
|
|
26
|
-
|
|
38
|
+
/**
|
|
39
|
+
* Generates and RSA digital signature with SHA3-512
|
|
40
|
+
* @param rsa_key_size
|
|
41
|
+
* @param data_to_sign
|
|
42
|
+
* @returns RsaDigitalSignatureResult
|
|
43
|
+
*/
|
|
44
|
+
createRsa(rsa_key_size: number, data_to_sign: number[]): CASRSADigitalSignatureResult {
|
|
27
45
|
if (rsa_key_size !== 1024 && rsa_key_size !== 2048 && rsa_key_size !== 4096) {
|
|
28
46
|
throw new Error("You need to provide an appropriate RSA key size.");
|
|
29
47
|
}
|
|
@@ -33,6 +51,13 @@ export class DigitalSignatureSHA256Wrapper implements IDigitalSignature {
|
|
|
33
51
|
return sha256RsaDigitalSignature(rsa_key_size, data_to_sign);
|
|
34
52
|
}
|
|
35
53
|
|
|
54
|
+
/**
|
|
55
|
+
* Verifies a digital signature created with the RSA public key.
|
|
56
|
+
* @param public_key
|
|
57
|
+
* @param data_to_verify
|
|
58
|
+
* @param signature
|
|
59
|
+
* @returns boolean
|
|
60
|
+
*/
|
|
36
61
|
verifyRSa(public_key: string, data_to_verify: number[], signature: number[]): boolean {
|
|
37
62
|
if (!public_key) {
|
|
38
63
|
throw new Error("Must provide a public key");
|
|
@@ -2,10 +2,13 @@ import { DigitalSignatureType } from "./digital-signature-factory";
|
|
|
2
2
|
import { DigitalSignatureFactory } from "./digital-signature-factory";
|
|
3
3
|
import { DigitalSignatureSHA256Wrapper } from "./digital-signaturte-sha-256";
|
|
4
4
|
import { DigitalSignatureSHA512Wrapper } from "./digital-siganture-sha-512";
|
|
5
|
+
import { CASRSADigitalSignatureResult, CASSHAED25519DalekDigitalSignatureResult } from "../../index";
|
|
5
6
|
|
|
6
7
|
export {
|
|
7
8
|
DigitalSignatureFactory,
|
|
8
9
|
DigitalSignatureSHA256Wrapper,
|
|
9
10
|
DigitalSignatureSHA512Wrapper,
|
|
10
|
-
DigitalSignatureType
|
|
11
|
+
DigitalSignatureType,
|
|
12
|
+
CASSHAED25519DalekDigitalSignatureResult,
|
|
13
|
+
CASRSADigitalSignatureResult
|
|
11
14
|
};
|