diff --git a/Cargo.toml b/Cargo.toml index ddb508c..c8644c4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,27 +8,13 @@ path = "src/lib.rs" crate-type = ["cdylib"] [dependencies] -aes-gcm = "0.10.3" -argon2 = "0.5.2" -bcrypt = "0.15.0" -blake2 = "0.10.6" napi = "2" napi-derive = "2" rand = "0.8.5" rand_chacha = "0.3.1" -rsa = "0.9.6" -scrypt = "0.11.0" -sha3 = "0.10.8" -x25519-dalek = {version = "2.0.0", features = ["static_secrets"]} -rand_07 = { package = "rand", version = "0.7.0" } -ascon-aead = "0.4.2" rayon = "1.10.0" - -[profile.dev.package.num-bigint-dig] -opt-level = 3 - -[dependencies.ed25519-dalek] -version = "1" +csbindgen = "1.9.1" +cas-lib = "0.1.3" [build-dependencies] -napi-build = "1" +napi-build = "1" \ No newline at end of file diff --git a/index.d.ts b/index.d.ts index 5e867e8..00946fb 100644 --- a/index.d.ts +++ b/index.d.ts @@ -19,7 +19,7 @@ export function sha512(dataToHash: Array): Array export function sha512Verify(dataToHash: Array, dataToVerify: Array): boolean export function sha256(dataToHash: Array): Array export function sha256Verify(dataToHash: Array, dataToVerify: Array): boolean -export function x25519GenerateSecretAndPublicKey(): X25519SecretPublicKeyResult +export function x25519GenerateSecretAndPublicKey(): CASx25519SecretPublicKeyResult export function x25519DiffieHellman(mySecretKey: Array, usersPublicKey: Array): Array export function aesNonce(): Array export function aes128Key(): Array @@ -28,52 +28,53 @@ export function aes128Encrypt(aesKey: Array, nonce: Array, plain export function aes128Decrypt(aesKey: Array, nonce: Array, ciphertext: Array): Array export function aes256Encrypt(aesKey: Array, nonce: Array, plaintext: Array): Array export function aes256Decrypt(aesKey: Array, nonce: Array, ciphertext: Array): Array -export function aes256KeyFromX25519SharedSecret(sharedSecret: Array): AesKeyFromX25519SharedSecret -export function aes128KeyFromX25519SharedSecret(sharedSecret: Array): AesKeyFromX25519SharedSecret -export function generateRsaKeys(keySize: number): RsaKeyPairResult +export function aes256KeyFromX25519SharedSecret(sharedSecret: Array): CASAesKeyFromX25519SharedSecret +export function aes128KeyFromX25519SharedSecret(sharedSecret: Array): CASAesKeyFromX25519SharedSecret +export function generateRsaKeys(keySize: number): CasrsaKeyPairResult export function encryptPlaintextRsa(publicKey: string, plaintext: Array): Array export function decryptCiphertextRsa(privateKey: string, ciphertext: Array): Array export function signRsa(privateKey: string, hash: Array): Array export function verifyRsa(publicKey: string, hash: Array, signature: Array): boolean -export function sha512RsaDigitalSignature(rsaKeySize: number, dataToSign: Array): RsaDigitalSignatureResult +export function sha512RsaDigitalSignature(rsaKeySize: number, dataToSign: Array): CASRSADigitalSignatureResult export function sha512RsaVerifyDigitalSignature(publicKey: string, dataToVerify: Array, signature: Array): boolean -export function sha256RsaDigitalSignature(rsaKeySize: number, dataToSign: Array): RsaDigitalSignatureResult +export function sha256RsaDigitalSignature(rsaKeySize: number, dataToSign: Array): CASRSADigitalSignatureResult export function sha256RsaVerifyDigitalSignature(publicKey: string, dataToVerify: Array, signature: Array): boolean -export function sha512Ed25519DigitalSignature(dataToSign: Array): Shaed25519DalekDigitalSignatureResult +export function sha512Ed25519DigitalSignature(dataToSign: Array): CASSHAED25519DalekDigitalSignatureResult export function sha512Ed25519DigitalSignatureVerify(publicKey: Array, dataToVerify: Array, signature: Array): boolean -export function sha256Ed25519DigitalSignature(dataToSign: Array): Shaed25519DalekDigitalSignatureResult +export function sha256Ed25519DigitalSignature(dataToSign: Array): CASSHAED25519DalekDigitalSignatureResult export function sha256Ed25519DigitalSignatureVerify(publicKey: Array, dataToVerify: Array, signature: Array): boolean export function ascon128KeyGenerate(): Array export function ascon128NonceGenerate(): Array export function ascon128Encrypt(key: Array, nonce: Array, plaintext: Array): Array export function ascon128Decrypt(key: Array, nonce: Array, ciphertext: Array): Array -export type x25519SecretPublicKeyResult = X25519SecretPublicKeyResult -export class X25519SecretPublicKeyResult { +export type CASx25519SecretPublicKeyResult = CaSx25519SecretPublicKeyResult +export class CaSx25519SecretPublicKeyResult { publicKey: Array secretKey: Array constructor(publicKey: Array, secretKey: Array) } -export class AesKeyFromX25519SharedSecret { +export type CASAesKeyFromX25519SharedSecret = CasAesKeyFromX25519SharedSecret +export class CasAesKeyFromX25519SharedSecret { aesKey: Array aesNonce: Array constructor(aesKey: Array, aesNonce: Array) } -export type RSAKeyPairResult = RsaKeyPairResult -export class RsaKeyPairResult { +export type CASRSAKeyPairResult = CasrsaKeyPairResult +export class CasrsaKeyPairResult { privateKey: string publicKey: string constructor(privateKey: string, publicKey: string) } -export type RSADigitalSignatureResult = RsaDigitalSignatureResult -export class RsaDigitalSignatureResult { +export type CASSHAED25519DalekDigitalSignatureResult = Casshaed25519DalekDigitalSignatureResult +export class Casshaed25519DalekDigitalSignatureResult { + publicKey: Array + signature: Array + constructor(publicKey: Array, signature: Array) +} +export type CASRSADigitalSignatureResult = CasrsaDigitalSignatureResult +export class CasrsaDigitalSignatureResult { publicKey: string privateKey: string signature: Array constructor(publicKey: string, privateKey: string, signature: Array) } -export type SHAED25519DalekDigitalSignatureResult = Shaed25519DalekDigitalSignatureResult -export class Shaed25519DalekDigitalSignatureResult { - publicKey: Array - signature: Array - constructor(publicKey: Array, signature: Array) -} diff --git a/index.node b/index.node index 9ce6f29..9821339 100644 Binary files a/index.node and b/index.node differ diff --git a/package.json b/package.json index 50ab2ae..5efd823 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "cas-typescript-sdk", - "version": "1.0.23", + "version": "1.0.24", "description": "", "main": "lib/index.js", "types": "lib/index.d.ts", diff --git a/src-ts/asymmetric/RSAWrapper.ts b/src-ts/asymmetric/RSAWrapper.ts index 5bcad04..6c91cd7 100644 --- a/src-ts/asymmetric/RSAWrapper.ts +++ b/src-ts/asymmetric/RSAWrapper.ts @@ -1,13 +1,13 @@ -import { decryptCiphertextRsa, encryptPlaintextRsa, generateRsaKeys, RsaKeyPairResult, signRsa, verifyRsa } from "../../index"; +import { CASRSAKeyPairResult, decryptCiphertextRsa, encryptPlaintextRsa, generateRsaKeys, signRsa, verifyRsa } from "../../index"; export class RSAWrapper { /** * Generates an RSA key pair based of parameter sent in 1024, 2048, and 4096 are supported. * @param keySize - * @returns RsaKeyPairResult + * @returns CASRSAKeyPairResult */ - public generateKeys(keySize: number): RsaKeyPairResult { + public generateKeys(keySize: number): CASRSAKeyPairResult { if (keySize !== 1024 && keySize !== 2048 && keySize !== 4096) { throw new Error("You must provide an appropriate key size to generate RSA keys"); } diff --git a/src-ts/asymmetric/index.ts b/src-ts/asymmetric/index.ts index 55730ea..4e57164 100644 --- a/src-ts/asymmetric/index.ts +++ b/src-ts/asymmetric/index.ts @@ -1,4 +1,4 @@ import { RSAWrapper } from "./RSAWrapper"; -import { RsaKeyPairResult } from "../../index"; +import { CASRSAKeyPairResult } from "../../index"; -export { RSAWrapper, RsaKeyPairResult }; \ No newline at end of file +export { RSAWrapper, CASRSAKeyPairResult }; \ No newline at end of file diff --git a/src-ts/digital-signature/digital-siganture-sha-512.ts b/src-ts/digital-signature/digital-siganture-sha-512.ts index c5e0f63..e828f52 100644 --- a/src-ts/digital-signature/digital-siganture-sha-512.ts +++ b/src-ts/digital-signature/digital-siganture-sha-512.ts @@ -1,4 +1,4 @@ -import { RsaDigitalSignatureResult, SHAED25519DalekDigitalSignatureResult, sha512Ed25519DigitalSignature, sha512Ed25519DigitalSignatureVerify, sha512RsaDigitalSignature, sha512RsaVerifyDigitalSignature } from "../../index"; +import { CASRSADigitalSignatureResult, CASSHAED25519DalekDigitalSignatureResult, sha512Ed25519DigitalSignature, sha512Ed25519DigitalSignatureVerify, sha512RsaDigitalSignature, sha512RsaVerifyDigitalSignature } from "../../index"; import { IDigitalSignature } from "./digital-signature-base"; export class DigitalSignatureSHA512Wrapper implements IDigitalSignature { @@ -6,9 +6,9 @@ export class DigitalSignatureSHA512Wrapper implements IDigitalSignature { /** * Creates an ED25519 siganture from an array of bytes with SHA3-512. * @param dataToSign - * @returns SHAED25519DalekDigitalSignatureResult + * @returns CASSHAED25519DalekDigitalSignatureResult */ - createED25519(dataToSign: number[]): SHAED25519DalekDigitalSignatureResult { + createED25519(dataToSign: number[]): CASSHAED25519DalekDigitalSignatureResult { if (dataToSign?.length === 0) { throw new Error("Must provide allocated data to sign"); } @@ -39,9 +39,9 @@ export class DigitalSignatureSHA512Wrapper implements IDigitalSignature { * Generates and RSA digital signature with SHA3-512 * @param rsa_key_size * @param data_to_sign - * @returns RsaDigitalSignatureResult + * @returns CASRSADigitalSignatureResult */ - createRsa(rsa_key_size: number, data_to_sign: number[]): RsaDigitalSignatureResult { + createRsa(rsa_key_size: number, data_to_sign: number[]): CASRSADigitalSignatureResult { if (rsa_key_size !== 1024 && rsa_key_size !== 2048 && rsa_key_size !== 4096) { throw new Error("You need to provide an appropriate RSA key size."); } diff --git a/src-ts/digital-signature/digital-signature-base.ts b/src-ts/digital-signature/digital-signature-base.ts index 034744f..eb5fa63 100644 --- a/src-ts/digital-signature/digital-signature-base.ts +++ b/src-ts/digital-signature/digital-signature-base.ts @@ -1,8 +1,8 @@ -import { RSADigitalSignatureResult, SHAED25519DalekDigitalSignatureResult } from "../../index"; +import { CASRSADigitalSignatureResult, CASSHAED25519DalekDigitalSignatureResult } from "../../index"; export interface IDigitalSignature { - createRsa(rsa_key_size: number, data_to_sign: Array): RSADigitalSignatureResult; + createRsa(rsa_key_size: number, data_to_sign: Array): CASRSADigitalSignatureResult; verifyRSa(public_key: string, data_to_verify: Array, signature: Array): boolean; - createED25519(dataToSign: Array): SHAED25519DalekDigitalSignatureResult; + createED25519(dataToSign: Array): CASSHAED25519DalekDigitalSignatureResult; verifyED25519(publicKey: Array, dataToVerify: Array, signature: Array): boolean; } \ No newline at end of file diff --git a/src-ts/digital-signature/digital-signaturte-sha-256.ts b/src-ts/digital-signature/digital-signaturte-sha-256.ts index c013093..61d899b 100644 --- a/src-ts/digital-signature/digital-signaturte-sha-256.ts +++ b/src-ts/digital-signature/digital-signaturte-sha-256.ts @@ -1,4 +1,4 @@ -import { RsaDigitalSignatureResult, SHAED25519DalekDigitalSignatureResult, Shaed25519DalekDigitalSignatureResult, sha256Ed25519DigitalSignature, sha256Ed25519DigitalSignatureVerify, sha256RsaDigitalSignature, sha256RsaVerifyDigitalSignature, sha512Ed25519DigitalSignature } from "../../index"; +import { CASRSADigitalSignatureResult, CASSHAED25519DalekDigitalSignatureResult, sha256Ed25519DigitalSignature, sha256Ed25519DigitalSignatureVerify, sha256RsaDigitalSignature, sha256RsaVerifyDigitalSignature, sha512Ed25519DigitalSignature } from "../../index"; import { IDigitalSignature } from "./digital-signature-base"; export class DigitalSignatureSHA256Wrapper implements IDigitalSignature { @@ -8,7 +8,7 @@ export class DigitalSignatureSHA256Wrapper implements IDigitalSignature { * @param dataToSign * @returns SHAED25519DalekDigitalSignatureResult */ - createED25519(dataToSign: number[]): Shaed25519DalekDigitalSignatureResult { + createED25519(dataToSign: number[]): CASSHAED25519DalekDigitalSignatureResult { if (dataToSign?.length === 0) { throw new Error("Must provide allocated data to sign"); } @@ -41,7 +41,7 @@ export class DigitalSignatureSHA256Wrapper implements IDigitalSignature { * @param data_to_sign * @returns RsaDigitalSignatureResult */ - createRsa(rsa_key_size: number, data_to_sign: number[]): RsaDigitalSignatureResult { + createRsa(rsa_key_size: number, data_to_sign: number[]): CASRSADigitalSignatureResult { if (rsa_key_size !== 1024 && rsa_key_size !== 2048 && rsa_key_size !== 4096) { throw new Error("You need to provide an appropriate RSA key size."); } diff --git a/src-ts/digital-signature/index.ts b/src-ts/digital-signature/index.ts index 456f2eb..d8e47aa 100644 --- a/src-ts/digital-signature/index.ts +++ b/src-ts/digital-signature/index.ts @@ -2,10 +2,13 @@ import { DigitalSignatureType } from "./digital-signature-factory"; import { DigitalSignatureFactory } from "./digital-signature-factory"; import { DigitalSignatureSHA256Wrapper } from "./digital-signaturte-sha-256"; import { DigitalSignatureSHA512Wrapper } from "./digital-siganture-sha-512"; +import { CASRSADigitalSignatureResult, CASSHAED25519DalekDigitalSignatureResult } from "../../index"; export { DigitalSignatureFactory, DigitalSignatureSHA256Wrapper, DigitalSignatureSHA512Wrapper, - DigitalSignatureType + DigitalSignatureType, + CASSHAED25519DalekDigitalSignatureResult, + CASRSADigitalSignatureResult }; diff --git a/src-ts/hybrid/types/aes-rsa-hybrid-initializer.ts b/src-ts/hybrid/types/aes-rsa-hybrid-initializer.ts index 95eb8f3..f4f9d1b 100644 --- a/src-ts/hybrid/types/aes-rsa-hybrid-initializer.ts +++ b/src-ts/hybrid/types/aes-rsa-hybrid-initializer.ts @@ -1,4 +1,4 @@ -import { RsaKeyPairResult } from "../../.."; +import { CASRSAKeyPairResult } from "../../.."; import { RSAWrapper } from "../../asymmetric"; import { AESWrapper } from "../../symmetric"; @@ -6,7 +6,7 @@ export class AESRSAHybridInitializer { public aesType: number; public aesKey: Array; public aesNonce: Array; - public rsaKeyPair: RsaKeyPairResult; + public rsaKeyPair: CASRSAKeyPairResult; /** * Constructs an initalizer to use with Hybrid Encryption wrapper. Generates your RSA key pair, AES nonce, and AES key based on the parameters passed in. diff --git a/src-ts/index.ts b/src-ts/index.ts index 08e0b07..3f2920d 100644 --- a/src-ts/index.ts +++ b/src-ts/index.ts @@ -1,46 +1,8 @@ -import { - Argon2Wrapper, - BCryptWrapper, - PasswordHasherFactory, - PasswordHasherType, - ScryptWrapper, -} from "./password-hashers/index"; -import { HasherFactory, HasherType, SHAWrapper } from "./hashers/index"; -import { X25519Wrapper } from "./key_exchange/index"; -import { AESWrapper } from "./symmetric/index"; -import { RsaKeyPairResult, RSAWrapper } from "./asymmetric/index"; -import { - AesRsaHybridEncryptResult, - AESRSAHybridInitializer, - HybridEncryptionWrapper, -} from "./hybrid/index"; -import { - DigitalSignatureFactory, - DigitalSignatureSHA256Wrapper, - DigitalSignatureSHA512Wrapper, - DigitalSignatureType, -} from "./digital-signature"; -import { AsconWrapper } from "./sponges/index"; - -export { - AesRsaHybridEncryptResult, - AESRSAHybridInitializer, - AESWrapper, - Argon2Wrapper, - AsconWrapper, - BCryptWrapper, - DigitalSignatureFactory, - DigitalSignatureSHA256Wrapper, - DigitalSignatureSHA512Wrapper, - DigitalSignatureType, - HasherFactory, - HasherType, - HybridEncryptionWrapper, - PasswordHasherFactory, - PasswordHasherType, - RsaKeyPairResult, - RSAWrapper, - ScryptWrapper, - SHAWrapper, - X25519Wrapper, -}; +export * from "./password-hashers/index"; +export * from "./hashers/index"; +export * from "./key_exchange/index"; +export * from "./symmetric/index"; +export * from "./asymmetric/index"; +export * from "./hybrid/index"; +export * from "./digital-signature"; +export * from "./sponges/index"; \ No newline at end of file diff --git a/src-ts/key_exchange/index.ts b/src-ts/key_exchange/index.ts index e210f7f..63237fa 100644 --- a/src-ts/key_exchange/index.ts +++ b/src-ts/key_exchange/index.ts @@ -1,3 +1,4 @@ +import { CASx25519SecretPublicKeyResult } from "../../index"; import { X25519Wrapper } from "./x25519"; -export { X25519Wrapper }; +export { X25519Wrapper, CASx25519SecretPublicKeyResult }; diff --git a/src-ts/key_exchange/x25519.ts b/src-ts/key_exchange/x25519.ts index 3d8e984..291710d 100644 --- a/src-ts/key_exchange/x25519.ts +++ b/src-ts/key_exchange/x25519.ts @@ -1,4 +1,4 @@ -import { x25519DiffieHellman, x25519GenerateSecretAndPublicKey, X25519SecretPublicKeyResult } from "../../index" +import { CASx25519SecretPublicKeyResult, x25519DiffieHellman, x25519GenerateSecretAndPublicKey } from "../../index" export class X25519Wrapper { /** @@ -6,7 +6,7 @@ export class X25519Wrapper { * User should share their public key with the other user and take the other user's public key and they can generate a Shared Secret. * @returns X25519SecretPublicKeyResult */ - public generateSecretAndPublicKey(): X25519SecretPublicKeyResult { + public generateSecretAndPublicKey(): CASx25519SecretPublicKeyResult { return x25519GenerateSecretAndPublicKey(); } diff --git a/src-ts/symmetric/aes-wrapper.ts b/src-ts/symmetric/aes-wrapper.ts index 82adb9a..68463d2 100644 --- a/src-ts/symmetric/aes-wrapper.ts +++ b/src-ts/symmetric/aes-wrapper.ts @@ -1,5 +1,5 @@ import { - AesKeyFromX25519SharedSecret, + CASAesKeyFromX25519SharedSecret, aes128Decrypt, aes128Encrypt, aes128Key, @@ -87,7 +87,7 @@ export class AESWrapper { * @param shared_secret * @returns */ - public aes256KeyNonceX25519DiffieHellman(shared_secret: Array): AesKeyFromX25519SharedSecret { + public aes256KeyNonceX25519DiffieHellman(shared_secret: Array): CASAesKeyFromX25519SharedSecret { return aes256KeyFromX25519SharedSecret(shared_secret); } @@ -96,7 +96,7 @@ export class AESWrapper { * @param shared_secret * @returns */ - public aes128KeyNonceX25519DiffieHellman(shared_secret: Array): AesKeyFromX25519SharedSecret { + public aes128KeyNonceX25519DiffieHellman(shared_secret: Array): CASAesKeyFromX25519SharedSecret { return aes128KeyFromX25519SharedSecret(shared_secret); } } diff --git a/src-ts/symmetric/index.ts b/src-ts/symmetric/index.ts index c54f1ca..58d066d 100644 --- a/src-ts/symmetric/index.ts +++ b/src-ts/symmetric/index.ts @@ -1,3 +1,4 @@ +import { CASAesKeyFromX25519SharedSecret } from "../../index"; import { AESWrapper } from "./aes-wrapper"; -export { AESWrapper }; +export { AESWrapper, CASAesKeyFromX25519SharedSecret }; diff --git a/src/asymmetric/cas_asymmetric_encryption.rs b/src/asymmetric/cas_asymmetric_encryption.rs deleted file mode 100644 index b4462ba..0000000 --- a/src/asymmetric/cas_asymmetric_encryption.rs +++ /dev/null @@ -1,15 +0,0 @@ -use napi_derive::napi; - -#[napi(constructor)] -pub struct RSAKeyPairResult { - pub private_key: String, - pub public_key: String, -} - -pub trait CASRSAEncryption { - fn generate_rsa_keys(key_size: u32) -> RSAKeyPairResult; - fn encrypt_plaintext(public_key: String, plaintext: Vec) -> Vec; - fn decrypt_ciphertext(private_key: String, ciphertext: Vec) -> Vec; - fn sign(private_key: String, hash: Vec) -> Vec; - fn verify(public_key: String, hash: Vec, signed_text: Vec) -> bool; -} diff --git a/src/asymmetric/cas_rsa.rs b/src/asymmetric/cas_rsa.rs index 96263c2..d8d8f79 100644 --- a/src/asymmetric/cas_rsa.rs +++ b/src/asymmetric/cas_rsa.rs @@ -1,70 +1,24 @@ +use cas_lib::asymmetric::{cas_asymmetric_encryption::{CASRSAEncryption, RSAKeyPairResult}, cas_rsa::CASRSA}; use napi_derive::napi; -use rand::rngs::OsRng; -use rsa::{ - pkcs1::{DecodeRsaPublicKey, EncodeRsaPublicKey}, - pkcs8::{DecodePrivateKey, EncodePrivateKey}, - Pkcs1v15Encrypt, Pkcs1v15Sign, RsaPrivateKey, RsaPublicKey, -}; -use super::cas_asymmetric_encryption::{CASRSAEncryption, RSAKeyPairResult}; -pub struct CASRSA; - -impl CASRSAEncryption for CASRSA { - fn generate_rsa_keys(key_size: u32) -> RSAKeyPairResult { - let mut rng: OsRng = OsRng; - let private_key: RsaPrivateKey = - RsaPrivateKey::new(&mut rng, key_size as usize).expect("failed to generate a key"); - let public_key: RsaPublicKey = private_key.to_public_key(); - let result = RSAKeyPairResult { - public_key: public_key - .to_pkcs1_pem(rsa::pkcs1::LineEnding::LF) - .unwrap() - .to_string(), - private_key: private_key - .to_pkcs8_pem(rsa::pkcs8::LineEnding::LF) - .unwrap() - .to_string(), - }; - result - } - - fn encrypt_plaintext(public_key: String, plaintext: Vec) -> Vec { - let public_key = RsaPublicKey::from_pkcs1_pem(&public_key).unwrap(); - let mut rng = rand::thread_rng(); - let ciphertext = public_key - .encrypt(&mut rng, Pkcs1v15Encrypt, &plaintext) - .unwrap(); - ciphertext - } - - fn decrypt_ciphertext(private_key: String, ciphertext: Vec) -> Vec { - let private_key = RsaPrivateKey::from_pkcs8_pem(&private_key).unwrap(); - let plaintext = private_key.decrypt(Pkcs1v15Encrypt, &ciphertext).unwrap(); - plaintext - } - - fn sign(private_key: String, hash: Vec) -> Vec { - let private_key = RsaPrivateKey::from_pkcs8_pem(&private_key).unwrap(); - let mut signed_data = private_key - .sign(Pkcs1v15Sign::new_unprefixed(), &hash) - .unwrap(); - signed_data - } +#[napi(constructor)] +pub struct CASRSAKeyPairResult { + pub private_key: String, + pub public_key: String, +} - fn verify(public_key: String, hash: Vec, signature: Vec) -> bool { - let public_key = RsaPublicKey::from_pkcs1_pem(&public_key).unwrap(); - let verified = public_key.verify(Pkcs1v15Sign::new_unprefixed(), &hash, &signature); - if verified.is_err() == false { - return true; - } else { - return false; +impl From for CASRSAKeyPairResult { + fn from(result: RSAKeyPairResult) -> Self { + CASRSAKeyPairResult { + private_key: result.private_key, + public_key: result.public_key, } } } #[napi] -pub fn generate_rsa_keys(key_size: u32) -> RSAKeyPairResult { - return CASRSA::generate_rsa_keys(key_size); +pub fn generate_rsa_keys(key_size: u32) -> CASRSAKeyPairResult { + return CASRSA::generate_rsa_keys(key_size).into(); } #[napi] diff --git a/src/digital_signature/cas_digital_signature_rsa.rs b/src/digital_signature/cas_digital_signature_rsa.rs deleted file mode 100644 index 22b945f..0000000 --- a/src/digital_signature/cas_digital_signature_rsa.rs +++ /dev/null @@ -1,27 +0,0 @@ -use napi_derive::napi; - -#[napi(constructor)] -pub struct RSADigitalSignatureResult { - pub public_key: String, - pub private_key: String, - pub signature: Vec, -} - -#[napi(constructor)] -pub struct SHAED25519DalekDigitalSignatureResult { - pub public_key: Vec, - pub signature: Vec -} - -pub trait RSADigitalSignature { - fn digital_signature_rsa( - rsa_key_size: u32, - data_to_sign: Vec, - ) -> RSADigitalSignatureResult; - fn verify_rsa(public_key: String, data_to_verify: Vec, signature: Vec) -> bool; -} - -pub trait ED25519DigitalSignature { - fn digital_signature_ed25519(data_to_sign: Vec) -> SHAED25519DalekDigitalSignatureResult; - fn digital_signature_ed25519_verify(public_key: Vec, data_to_verify: Vec, signature: Vec) -> bool; -} \ No newline at end of file diff --git a/src/digital_signature/sha_256_ed25519.rs b/src/digital_signature/sha_256_ed25519.rs index 29cd4c3..c1c6182 100644 --- a/src/digital_signature/sha_256_ed25519.rs +++ b/src/digital_signature/sha_256_ed25519.rs @@ -1,47 +1,11 @@ -use ed25519_dalek::{Keypair, Signature, Signer, Verifier}; +use cas_lib::digital_signature::{cas_digital_signature_rsa::ED25519DigitalSignature, sha_256_ed25519::SHA256ED25519DigitalSignature}; use napi_derive::napi; -use sha3::{Digest, Sha3_256}; -use super::cas_digital_signature_rsa::{ - ED25519DigitalSignature, SHAED25519DalekDigitalSignatureResult, -}; - -pub struct SHA256ED25519DigitalSignature; - -impl ED25519DigitalSignature for SHA256ED25519DigitalSignature { - fn digital_signature_ed25519(data_to_sign: Vec) -> SHAED25519DalekDigitalSignatureResult { - let mut hasher = Sha3_256::new(); - hasher.update(data_to_sign); - let sha_hasher_result = hasher.finalize(); - let mut csprng = rand_07::rngs::OsRng {}; - let keypair = ed25519_dalek::Keypair::generate(&mut csprng); - - let signature = keypair.sign(&sha_hasher_result); - let signature_bytes = signature.to_bytes(); - let public_keypair_bytes = keypair.public.to_bytes(); - let result = SHAED25519DalekDigitalSignatureResult { - public_key: public_keypair_bytes.to_vec(), - signature: signature_bytes.to_vec(), - }; - result - } - - fn digital_signature_ed25519_verify(public_key: Vec, data_to_verify: Vec, signature: Vec) -> bool { - let mut hasher = Sha3_256::new(); - hasher.update(data_to_verify); - let sha_hasher_result = hasher.finalize(); - - let public_key_parsed = ed25519_dalek::PublicKey::from_bytes(&public_key).unwrap(); - let signature_parsed = Signature::from_bytes(&signature).unwrap(); - return public_key_parsed - .verify(&sha_hasher_result, &signature_parsed) - .is_ok(); - } -} +use super::types::CASSHAED25519DalekDigitalSignatureResult; #[napi] -pub fn sha_256_ed25519_digital_signature(data_to_sign: Vec) -> SHAED25519DalekDigitalSignatureResult { - return SHA256ED25519DigitalSignature::digital_signature_ed25519(data_to_sign); +pub fn sha_256_ed25519_digital_signature(data_to_sign: Vec) -> CASSHAED25519DalekDigitalSignatureResult { + return ::digital_signature_ed25519(data_to_sign).into(); } #[napi] @@ -53,7 +17,7 @@ pub fn sha_256_ed25519_digital_signature_verify(public_key: Vec, data_to_ver fn sha_256_ed25519_test() { let key_size: u32 = 1024; let data_to_sign = b"GetTheseBytes".to_vec(); - let signature_result: SHAED25519DalekDigitalSignatureResult = SHA256ED25519DigitalSignature::digital_signature_ed25519(data_to_sign.clone()); + 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); assert_eq!(is_verified, true); } @@ -62,7 +26,7 @@ fn sha_256_ed25519_test() { fn sha_512_ed25519_test_fail() { let key_size: u32 = 1024; let data_to_sign = b"GetTheseBytes".to_vec(); - let signature_result: SHAED25519DalekDigitalSignatureResult = SHA256ED25519DigitalSignature::digital_signature_ed25519(data_to_sign.clone()); + 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); assert_eq!(is_verified, false); diff --git a/src/digital_signature/sha_256_rsa.rs b/src/digital_signature/sha_256_rsa.rs index 1a1e710..d910140 100644 --- a/src/digital_signature/sha_256_rsa.rs +++ b/src/digital_signature/sha_256_rsa.rs @@ -1,70 +1,14 @@ +use cas_lib::digital_signature::{cas_digital_signature_rsa::RSADigitalSignature, sha_256_rsa::SHA256RSADigitalSignature}; use napi_derive::napi; -use rand::rngs::OsRng; -use rsa::{ - pkcs1::{DecodeRsaPublicKey, EncodeRsaPublicKey}, - pkcs8::EncodePrivateKey, - Pkcs1v15Sign, RsaPrivateKey, RsaPublicKey, -}; -use sha3::{Digest, Sha3_256}; - -use super::cas_digital_signature_rsa::{RSADigitalSignatureResult, RSADigitalSignature}; - -pub struct SHA256RSADigitalSignature; - -impl RSADigitalSignature for SHA256RSADigitalSignature { - fn digital_signature_rsa( - rsa_key_size: u32, - data_to_sign: Vec, - ) -> RSADigitalSignatureResult { - let mut hasher = Sha3_256::new(); - hasher.update(data_to_sign); - let sha_hasher_result = hasher.finalize(); - let mut rng: OsRng = OsRng; - let private_key: RsaPrivateKey = - RsaPrivateKey::new(&mut rng, rsa_key_size as usize).expect("failed to generate a key"); - let public_key = private_key.to_public_key(); - let mut signed_data = private_key - .sign(Pkcs1v15Sign::new_unprefixed(), &sha_hasher_result) - .unwrap(); - let result = RSADigitalSignatureResult { - private_key: private_key - .to_pkcs8_pem(rsa::pkcs8::LineEnding::LF) - .unwrap() - .to_string(), - public_key: public_key - .to_pkcs1_pem(rsa::pkcs8::LineEnding::LF) - .unwrap() - .to_string(), - signature: signed_data, - }; - result - } - - fn verify_rsa(public_key: String, data_to_verify: Vec, signature: Vec) -> bool { - let mut hasher = Sha3_256::new(); - hasher.update(data_to_verify); - let sha_hasher_result = hasher.finalize(); - let public_key = RsaPublicKey::from_pkcs1_pem(&public_key).unwrap(); - let verified = public_key.verify( - Pkcs1v15Sign::new_unprefixed(), - &sha_hasher_result, - &signature, - ); - if verified.is_err() == false { - return true; - } else { - return false; - } - } -} +use super::types::CASRSADigitalSignatureResult; #[napi] pub fn sha_256_rsa_digital_signature( rsa_key_size: u32, data_to_sign: Vec, -) -> RSADigitalSignatureResult { - return SHA256RSADigitalSignature::digital_signature_rsa(rsa_key_size, data_to_sign); +) -> CASRSADigitalSignatureResult { + return ::digital_signature_rsa(rsa_key_size, data_to_sign).into(); } #[napi] @@ -73,14 +17,14 @@ pub fn sha_256_rsa_verify_digital_signature( data_to_verify: Vec, signature: Vec, ) -> bool { - return SHA256RSADigitalSignature::verify_rsa(public_key, data_to_verify, signature); + return ::verify_rsa(public_key, data_to_verify, signature); } #[test] fn sha_256_rsa_digital_signature_test() { let key_size: u32 = 1024; let data_to_sign = b"GetTheseBytes".to_vec(); - let signature_result: RSADigitalSignatureResult = SHA256RSADigitalSignature::digital_signature_rsa(key_size, data_to_sign.clone()); + let signature_result: CASRSADigitalSignatureResult = ::digital_signature_rsa(key_size, data_to_sign.clone()).into(); let is_verified: bool = SHA256RSADigitalSignature::verify_rsa(signature_result.public_key, data_to_sign, signature_result.signature); assert_eq!(is_verified, true); } @@ -89,7 +33,7 @@ fn sha_256_rsa_digital_signature_test() { fn sha_256_rsa_digital_signature_fail_test() { let key_size: u32 = 1024; let data_to_sign = b"GetTheseBytes".to_vec(); - let signature_result: RSADigitalSignatureResult = SHA256RSADigitalSignature::digital_signature_rsa(key_size, data_to_sign.clone()); + let signature_result: CASRSADigitalSignatureResult = ::digital_signature_rsa(key_size, data_to_sign.clone()).into(); let new_data = b"NOtTheOriginalData".to_vec(); let is_verified: bool = SHA256RSADigitalSignature::verify_rsa(signature_result.public_key, new_data, signature_result.signature); assert_eq!(is_verified, false); diff --git a/src/digital_signature/sha_512_ed25519.rs b/src/digital_signature/sha_512_ed25519.rs index 22352d3..b431f06 100644 --- a/src/digital_signature/sha_512_ed25519.rs +++ b/src/digital_signature/sha_512_ed25519.rs @@ -1,65 +1,25 @@ -use ed25519_dalek::{Keypair, Signature, Signer, Verifier}; -use napi_derive::napi; -use sha3::{Digest, Sha3_512}; - -use super::cas_digital_signature_rsa::{ - ED25519DigitalSignature, SHAED25519DalekDigitalSignatureResult, -}; - -pub struct SHA512ED25519DigitalSignature; +use cas_lib::digital_signature::{cas_digital_signature_rsa::{ED25519DigitalSignature, SHAED25519DalekDigitalSignatureResult}, sha_512_ed25519::SHA512ED25519DigitalSignature}; -impl ED25519DigitalSignature for SHA512ED25519DigitalSignature { - fn digital_signature_ed25519( - data_to_sign: Vec, - ) -> SHAED25519DalekDigitalSignatureResult { - let mut hasher = Sha3_512::new(); - hasher.update(data_to_sign); - let sha_hasher_result = hasher.finalize(); - let mut csprng = rand_07::rngs::OsRng {}; - let keypair = Keypair::generate(&mut csprng); - - let signature = keypair.sign(&sha_hasher_result); - let signature_bytes = signature.to_bytes(); - let public_keypair_bytes = keypair.public.to_bytes(); - let result = SHAED25519DalekDigitalSignatureResult { - public_key: public_keypair_bytes.to_vec(), - signature: signature_bytes.to_vec(), - }; - result - } +use napi_derive::napi; - fn digital_signature_ed25519_verify( - public_key: Vec, - data_to_verify: Vec, - signature: Vec, - ) -> bool { - let mut hasher = Sha3_512::new(); - hasher.update(data_to_verify); - let sha_hasher_result = hasher.finalize(); +use super::types::CASSHAED25519DalekDigitalSignatureResult; - let public_key_parsed = ed25519_dalek::PublicKey::from_bytes(&public_key).unwrap(); - let signature_parsed = Signature::from_bytes(&signature).unwrap(); - return public_key_parsed - .verify(&sha_hasher_result, &signature_parsed) - .is_ok(); - } -} #[napi] -pub fn sha_512_ed25519_digital_signature(data_to_sign: Vec) -> SHAED25519DalekDigitalSignatureResult { - return SHA512ED25519DigitalSignature::digital_signature_ed25519(data_to_sign); +pub fn sha_512_ed25519_digital_signature(data_to_sign: Vec) -> CASSHAED25519DalekDigitalSignatureResult { + return ::digital_signature_ed25519(data_to_sign).into(); } #[napi] pub fn sha_512_ed25519_digital_signature_verify(public_key: Vec, data_to_verify: Vec, signature: Vec) -> bool { - return SHA512ED25519DigitalSignature::digital_signature_ed25519_verify(public_key, data_to_verify, signature) + return ::digital_signature_ed25519_verify(public_key, data_to_verify, signature) } #[test] fn sha_512_ed25519_test() { let key_size: u32 = 1024; let data_to_sign = b"GetTheseBytes".to_vec(); - let signature_result: SHAED25519DalekDigitalSignatureResult = SHA512ED25519DigitalSignature::digital_signature_ed25519(data_to_sign.clone()); + let signature_result: SHAED25519DalekDigitalSignatureResult = ::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); } @@ -68,7 +28,7 @@ fn sha_512_ed25519_test() { fn sha_512_ed25519_test_fail() { let key_size: u32 = 1024; let data_to_sign = b"GetTheseBytes".to_vec(); - let signature_result: SHAED25519DalekDigitalSignatureResult = SHA512ED25519DigitalSignature::digital_signature_ed25519(data_to_sign.clone()); + let signature_result: CASSHAED25519DalekDigitalSignatureResult = ::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); assert_eq!(is_verified, false); diff --git a/src/digital_signature/sha_512_rsa.rs b/src/digital_signature/sha_512_rsa.rs index fa79518..1597256 100644 --- a/src/digital_signature/sha_512_rsa.rs +++ b/src/digital_signature/sha_512_rsa.rs @@ -1,67 +1,14 @@ +use cas_lib::digital_signature::{cas_digital_signature_rsa::RSADigitalSignature, sha_512_rsa::SHA512RSADigitalSignature}; use napi_derive::napi; -use rand::rngs::OsRng; -use rsa::{ - pkcs1::{DecodeRsaPublicKey, EncodeRsaPublicKey}, - pkcs8::EncodePrivateKey, - Pkcs1v15Sign, RsaPrivateKey, RsaPublicKey, -}; -use sha3::{Digest, Sha3_512}; -use super::cas_digital_signature_rsa::{RSADigitalSignatureResult, RSADigitalSignature}; -pub struct SHA512RSADigitalSignature; -impl RSADigitalSignature for SHA512RSADigitalSignature { - fn digital_signature_rsa( - rsa_key_size: u32, - data_to_sign: Vec, - ) -> RSADigitalSignatureResult { - let mut hasher = Sha3_512::new(); - hasher.update(data_to_sign); - let sha_hasher_result = hasher.finalize(); - let mut rng: OsRng = OsRng; - let private_key: RsaPrivateKey = - RsaPrivateKey::new(&mut rng, rsa_key_size as usize).expect("failed to generate a key"); - let public_key = private_key.to_public_key(); - let mut signed_data = private_key - .sign(Pkcs1v15Sign::new_unprefixed(), &sha_hasher_result) - .unwrap(); - let result = RSADigitalSignatureResult { - private_key: private_key - .to_pkcs8_pem(rsa::pkcs8::LineEnding::LF) - .unwrap() - .to_string(), - public_key: public_key - .to_pkcs1_pem(rsa::pkcs8::LineEnding::LF) - .unwrap() - .to_string(), - signature: signed_data, - }; - result - } - - fn verify_rsa(public_key: String, data_to_verify: Vec, signature: Vec) -> bool { - let mut hasher = Sha3_512::new(); - hasher.update(data_to_verify); - let sha_hasher_result = hasher.finalize(); - let public_key = RsaPublicKey::from_pkcs1_pem(&public_key).unwrap(); - let verified = public_key.verify( - Pkcs1v15Sign::new_unprefixed(), - &sha_hasher_result, - &signature, - ); - if verified.is_err() == false { - return true; - } else { - return false; - } - } -} +use super::types::CASRSADigitalSignatureResult; #[napi] pub fn sha_512_rsa_digital_signature( rsa_key_size: u32, data_to_sign: Vec, -) -> RSADigitalSignatureResult { - return SHA512RSADigitalSignature::digital_signature_rsa(rsa_key_size, data_to_sign); +) -> CASRSADigitalSignatureResult { + return ::digital_signature_rsa(rsa_key_size, data_to_sign).into(); } #[napi] @@ -70,15 +17,15 @@ pub fn sha_512_rsa_verify_digital_signature( data_to_verify: Vec, signature: Vec, ) -> bool { - return SHA512RSADigitalSignature::verify_rsa(public_key, data_to_verify, signature); + return ::verify_rsa(public_key, data_to_verify, signature); } #[test] fn sha_512_rsa_digital_signature_test() { let key_size: u32 = 1024; let data_to_sign = b"GetTheseBytes".to_vec(); - let signature_result: RSADigitalSignatureResult = SHA512RSADigitalSignature::digital_signature_rsa(key_size, data_to_sign.clone()); - let is_verified: bool = SHA512RSADigitalSignature::verify_rsa(signature_result.public_key, data_to_sign, signature_result.signature); + let signature_result: CASRSADigitalSignatureResult = SHA512RSADigitalSignature::digital_signature_rsa(key_size, data_to_sign.clone()).into(); + let is_verified: bool = ::verify_rsa(signature_result.public_key, data_to_sign, signature_result.signature); assert_eq!(is_verified, true); } @@ -86,8 +33,8 @@ fn sha_512_rsa_digital_signature_test() { fn sha_512_rsa_digital_signature_fail_test() { let key_size: u32 = 1024; let data_to_sign = b"GetTheseBytes".to_vec(); - let signature_result: RSADigitalSignatureResult = SHA512RSADigitalSignature::digital_signature_rsa(key_size, data_to_sign.clone()); + let signature_result: CASRSADigitalSignatureResult = SHA512RSADigitalSignature::digital_signature_rsa(key_size, data_to_sign.clone()).into(); let new_data = b"NOtTheOriginalData".to_vec(); - let is_verified: bool = SHA512RSADigitalSignature::verify_rsa(signature_result.public_key, new_data, signature_result.signature); + let is_verified: bool = ::verify_rsa(signature_result.public_key, new_data, signature_result.signature); assert_eq!(is_verified, false); } diff --git a/src/digital_signature/types.rs b/src/digital_signature/types.rs new file mode 100644 index 0000000..7a03f53 --- /dev/null +++ b/src/digital_signature/types.rs @@ -0,0 +1,34 @@ +use cas_lib::digital_signature::cas_digital_signature_rsa::{RSADigitalSignatureResult, SHAED25519DalekDigitalSignatureResult}; +use napi_derive::napi; + +#[napi(constructor)] +pub struct CASSHAED25519DalekDigitalSignatureResult { + pub public_key: Vec, + pub signature: Vec +} + +impl From for CASSHAED25519DalekDigitalSignatureResult { + fn from(value: SHAED25519DalekDigitalSignatureResult) -> Self { + CASSHAED25519DalekDigitalSignatureResult { + public_key: value.public_key, + signature: value.signature + } + } +} + +#[napi(constructor)] +pub struct CASRSADigitalSignatureResult { + pub public_key: String, + pub private_key: String, + pub signature: Vec, +} + +impl From for CASRSADigitalSignatureResult { + fn from(value: RSADigitalSignatureResult) -> Self { + CASRSADigitalSignatureResult { + public_key: value.public_key, + private_key: value.private_key, + signature: value.signature + } + } +} \ No newline at end of file diff --git a/src/hashers/blake2.rs b/src/hashers/blake2.rs deleted file mode 100644 index 846abff..0000000 --- a/src/hashers/blake2.rs +++ /dev/null @@ -1,37 +0,0 @@ -use super::cas_hasher::CASHasher; -use blake2::{Blake2b512, Blake2s256, Digest}; - -pub struct CASBlake2; - -impl CASHasher for CASBlake2 { - fn hash_512(data_to_hash: Vec) -> Vec { - let mut hasher = Blake2b512::new(); - hasher.update(data_to_hash); - let result = hasher.finalize(); - return result.to_vec(); - } - - fn verify_512(hash_to_verify: Vec, data_to_verify: Vec) -> bool { - let mut hasher = Blake2b512::new(); - hasher.update(data_to_verify); - let result = hasher.finalize(); - return hash_to_verify.eq(&result.to_vec()); - } - - fn hash_256(data_to_hash: Vec) -> Vec { - let mut hasher = Blake2s256::new(); - hasher.update(data_to_hash); - let result = hasher.finalize(); - return result.to_vec(); - } - - fn verify_256(hash_to_verify: Vec, data_to_verify: Vec) -> bool { - let mut hasher = Blake2s256::new(); - hasher.update(data_to_verify); - let result = hasher.finalize(); - return hash_to_verify.eq(&result.to_vec()); - } -} - -#[test] -fn hash_512_test() {} diff --git a/src/hashers/cas_hasher.rs b/src/hashers/cas_hasher.rs deleted file mode 100644 index 7096bf4..0000000 --- a/src/hashers/cas_hasher.rs +++ /dev/null @@ -1,8 +0,0 @@ -use napi::bindgen_prelude::Array; - -pub trait CASHasher { - fn hash_512(data_to_hash: Vec) -> Vec; - fn verify_512(hash_to_verify: Vec, data_to_verify: Vec) -> bool; - fn hash_256(data_to_hash: Vec) -> Vec; - fn verify_256(hash_to_verify: Vec, data_to_verify: Vec) -> bool; -} diff --git a/src/hashers/sha.rs b/src/hashers/sha.rs index f8fbd15..d5abcf3 100644 --- a/src/hashers/sha.rs +++ b/src/hashers/sha.rs @@ -1,40 +1,6 @@ -use napi::bindgen_prelude::Array; +use cas_lib::hashers::{cas_hasher::CASHasher, sha::CASSHA}; use napi_derive::napi; -use super::cas_hasher::CASHasher; -use sha3::{Digest, Sha3_256, Sha3_512}; -pub struct CASSHA; - -impl CASHasher for CASSHA { - fn hash_512(data_to_hash: Vec) -> Vec { - let mut hasher = Sha3_512::new(); - hasher.update(data_to_hash); - let result = hasher.finalize(); - return result.to_vec(); - } - - fn verify_512(hash_to_verify: Vec, data_to_verify: Vec) -> bool { - let mut hasher = Sha3_512::new(); - hasher.update(data_to_verify); - let result = hasher.finalize(); - return hash_to_verify.eq(&result.to_vec()); - } - - fn hash_256(data_to_hash: Vec) -> Vec { - let mut hasher = Sha3_256::new(); - hasher.update(data_to_hash); - let result = hasher.finalize(); - return result.to_vec(); - } - - fn verify_256(hash_to_verify: Vec, data_to_verify: Vec) -> bool { - let mut hasher = Sha3_256::new(); - hasher.update(data_to_verify); - let result = hasher.finalize(); - return hash_to_verify.eq(&result.to_vec()); - } -} - #[napi] pub fn sha_512(data_to_hash: Vec) -> Vec { return ::hash_512(data_to_hash); diff --git a/src/key_exchange/cas_key_exchange.rs b/src/key_exchange/cas_key_exchange.rs deleted file mode 100644 index d2aa75e..0000000 --- a/src/key_exchange/cas_key_exchange.rs +++ /dev/null @@ -1,6 +0,0 @@ -use super::x25519::x25519SecretPublicKeyResult; - -pub trait CASKeyExchange { - fn generate_secret_and_public_key() -> x25519SecretPublicKeyResult; - fn diffie_hellman(my_secret_key: Vec, users_public_key: Vec) -> Vec; -} diff --git a/src/key_exchange/types.rs b/src/key_exchange/types.rs new file mode 100644 index 0000000..68e66ba --- /dev/null +++ b/src/key_exchange/types.rs @@ -0,0 +1,17 @@ +use cas_lib::key_exchange::x25519::x25519SecretPublicKeyResult; +use napi_derive::napi; + +#[napi(constructor)] +pub struct CASx25519SecretPublicKeyResult { + pub public_key: Vec, + pub secret_key: Vec, +} + +impl From for CASx25519SecretPublicKeyResult { + fn from(value: x25519SecretPublicKeyResult) -> Self { + CASx25519SecretPublicKeyResult { + public_key: value.public_key, + secret_key: value.secret_key + } + } +} \ No newline at end of file diff --git a/src/key_exchange/x25519.rs b/src/key_exchange/x25519.rs index a195be0..93b30ed 100644 --- a/src/key_exchange/x25519.rs +++ b/src/key_exchange/x25519.rs @@ -1,44 +1,12 @@ -use napi::bindgen_prelude::ClassInstance; +use cas_lib::key_exchange::{cas_key_exchange::CASKeyExchange, x25519::X25519}; use napi_derive::napi; -use rand::rngs::OsRng; -use x25519_dalek::{PublicKey, StaticSecret}; -use super::cas_key_exchange::CASKeyExchange; +use super::types::CASx25519SecretPublicKeyResult; -#[napi(constructor)] -pub struct x25519SecretPublicKeyResult { - pub public_key: Vec, - pub secret_key: Vec, -} - -pub struct X25519; - -impl CASKeyExchange for X25519 { - fn generate_secret_and_public_key() -> x25519SecretPublicKeyResult { - let secret_key = StaticSecret::random_from_rng(OsRng); - let public_key = PublicKey::from(&secret_key); - let result = x25519SecretPublicKeyResult { - secret_key: secret_key.as_bytes().to_vec(), - public_key: public_key.as_bytes().to_vec(), - }; - result - } - - fn diffie_hellman(my_secret_key: Vec, users_public_key: Vec) -> Vec { - let mut secret_key_array: [u8; 32] = Default::default(); - secret_key_array.copy_from_slice(&my_secret_key); - let mut users_public_key_array: [u8; 32] = Default::default(); - users_public_key_array.copy_from_slice(&users_public_key); - - let secret_key = StaticSecret::from(secret_key_array); - let public_key = PublicKey::from(users_public_key_array); - return secret_key.diffie_hellman(&public_key).as_bytes().to_vec(); - } -} #[napi] -pub fn x25519_generate_secret_and_public_key() -> x25519SecretPublicKeyResult { - return ::generate_secret_and_public_key(); +pub fn x25519_generate_secret_and_public_key() -> CASx25519SecretPublicKeyResult { + return ::generate_secret_and_public_key().into(); } #[napi] diff --git a/src/lib.rs b/src/lib.rs index a730ec9..63d609b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,40 +1,35 @@ mod password_hashers { pub mod argon2; pub mod bcrypt; - pub mod cas_password_hasher; pub mod scrypt; } mod hashers { - pub mod blake2; - pub mod cas_hasher; pub mod sha; } mod key_exchange { - pub mod cas_key_exchange; pub mod x25519; + mod types; } mod symmetric { pub mod aes; - pub mod cas_symmetric_encryption; + mod types; } mod asymmetric { - pub mod cas_asymmetric_encryption; pub mod cas_rsa; } mod digital_signature { - pub mod cas_digital_signature_rsa; pub mod sha_512_rsa; pub mod sha_256_rsa; pub mod sha_512_ed25519; pub mod sha_256_ed25519; + mod types; } mod sponges { - pub mod cas_ascon_aead; pub mod ascon_aead; } \ No newline at end of file diff --git a/src/password_hashers/argon2.rs b/src/password_hashers/argon2.rs index 670a12f..04bbfdf 100644 --- a/src/password_hashers/argon2.rs +++ b/src/password_hashers/argon2.rs @@ -1,35 +1,7 @@ use napi_derive::napi; - -use argon2::{ - password_hash::{rand_core::OsRng, SaltString}, - Argon2, PasswordHash, PasswordHasher, PasswordVerifier, -}; - -use crate::symmetric::aes::CASAES128; - -use super::cas_password_hasher::CASPasswordHasher; - -pub struct CASArgon; - -impl CASPasswordHasher for CASArgon { - fn hash_password(password_to_hash: String) -> String { - let salt = SaltString::generate(&mut OsRng); - let argon2 = Argon2::default(); - let hashed_password = argon2 - .hash_password(password_to_hash.as_bytes(), &salt) - .unwrap() - .to_string(); - return hashed_password; - } - - fn verify_password(hashed_password: String, password_to_verify: String) -> bool { - let hashed_password = PasswordHash::new(&hashed_password).unwrap(); - return Argon2::default() - .verify_password(password_to_verify.as_bytes(), &hashed_password) - .is_ok(); - } -} +use cas_lib::password_hashers::argon2::CASArgon; +use cas_lib::password_hashers::cas_password_hasher::CASPasswordHasher; #[napi] pub fn argon2_hash(password: String) -> String { @@ -38,13 +10,7 @@ pub fn argon2_hash(password: String) -> String { #[napi] pub fn argon2_hash_thread_pool(password: String) -> String { - let (sender, receiver) = std::sync::mpsc::channel(); - rayon::spawn(move || { - let hash_result = ::hash_password(password); - sender.send(hash_result); - }); - let result = receiver.recv().unwrap(); - result + return ::hash__password_threadpool(password); } #[napi] @@ -54,13 +20,7 @@ pub fn argon2_verify(hashed_password: String, password_to_verify: String) -> boo #[napi] pub fn argon2_verify_threadpool(hashed_password: String, password_to_verify: String) -> bool { - let (sender, receiver) = std::sync::mpsc::channel(); - rayon::spawn(move || { - let verify_result = ::verify_password(hashed_password, password_to_verify); - sender.send(verify_result); - }); - let result = receiver.recv().unwrap(); - result + return ::verify_password_threadpool(hashed_password, password_to_verify); } #[test] diff --git a/src/password_hashers/bcrypt.rs b/src/password_hashers/bcrypt.rs index c51e6d9..1ce120e 100644 --- a/src/password_hashers/bcrypt.rs +++ b/src/password_hashers/bcrypt.rs @@ -1,22 +1,7 @@ -use std::sync::mpsc; - -use bcrypt::{hash, verify, DEFAULT_COST}; +use cas_lib::password_hashers::bcrypt::CASBCrypt; +use cas_lib::password_hashers::cas_password_hasher::CASPasswordHasher; use napi_derive::napi; -use super::cas_password_hasher::CASPasswordHasher; - -pub struct CASBCrypt; - -impl CASPasswordHasher for CASBCrypt { - fn hash_password(password_to_hash: String) -> String { - return hash(password_to_hash, DEFAULT_COST).unwrap(); - } - - fn verify_password(hashed_password: String, password_to_verify: String) -> bool { - return verify(password_to_verify, &hashed_password).unwrap(); - } -} - #[napi] pub fn bcrypt_hash(password_to_hash: String) -> String { return ::hash_password(password_to_hash); @@ -24,13 +9,7 @@ pub fn bcrypt_hash(password_to_hash: String) -> String { #[napi] pub fn bcrypt_hash_threadpool(password_to_hash: String) -> String { - let (sender, receiver) = mpsc::channel(); - rayon::spawn(move || { - let thread_result = ::hash_password(password_to_hash); - sender.send(thread_result); - }); - let result = receiver.recv().unwrap(); - result + return ::hash__password_threadpool(password_to_hash); } #[napi] @@ -40,13 +19,7 @@ pub fn bcrypt_verify(hashed_password: String, password_to_verify: String) -> boo #[napi] pub fn bcrypt_verify_threadpool(password_to_hash: String, password_to_verify: String) -> bool { - let (sender, receiver) = mpsc::channel(); - rayon::spawn(move || { - let thread_result = ::verify_password(password_to_hash, password_to_verify); - sender.send(thread_result); - }); - let result = receiver.recv().unwrap(); - result + return ::verify_password_threadpool(password_to_hash, password_to_verify); } #[test] diff --git a/src/password_hashers/cas_password_hasher.rs b/src/password_hashers/cas_password_hasher.rs deleted file mode 100644 index 5408fcf..0000000 --- a/src/password_hashers/cas_password_hasher.rs +++ /dev/null @@ -1,4 +0,0 @@ -pub trait CASPasswordHasher { - fn hash_password(password_to_hash: String) -> String; - fn verify_password(hashed_password: String, password_to_verify: String) -> bool; -} diff --git a/src/password_hashers/scrypt.rs b/src/password_hashers/scrypt.rs index 72c3314..7eaf347 100644 --- a/src/password_hashers/scrypt.rs +++ b/src/password_hashers/scrypt.rs @@ -1,33 +1,6 @@ -use std::sync::mpsc; - +use cas_lib::password_hashers::{cas_password_hasher::CASPasswordHasher, scrypt::CASScrypt}; use napi_derive::napi; -use scrypt::{ - password_hash::{rand_core::OsRng, PasswordHash, PasswordHasher, PasswordVerifier, SaltString}, - Scrypt, -}; - -use super::cas_password_hasher::CASPasswordHasher; - -pub struct CASScrypt; - -impl CASPasswordHasher for CASScrypt { - fn hash_password(password_to_hash: String) -> String { - let salt = SaltString::generate(&mut OsRng); - return Scrypt - .hash_password(password_to_hash.as_bytes(), &salt) - .unwrap() - .to_string(); - } - - fn verify_password(hashed_password: String, password_to_verify: String) -> bool { - let parsed_hash = PasswordHash::new(&hashed_password).unwrap(); - return Scrypt - .verify_password(password_to_verify.as_bytes(), &parsed_hash) - .is_ok(); - } -} - #[napi] pub fn scrypt_hash(password_to_hash: String) -> String { return ::hash_password(password_to_hash); @@ -40,24 +13,12 @@ pub fn scrypt_verify(hashed_password: String, password_to_verify: String) -> boo #[napi] pub fn scrypt_hash_threadpool(password_to_hash: String) -> String { - let (sender, receiver) = mpsc::channel(); - rayon::spawn(move || { - let thread_result = ::hash_password(password_to_hash); - sender.send(thread_result); - }); - let result = receiver.recv().unwrap(); - result + return ::hash__password_threadpool(password_to_hash); } #[napi] pub fn scrypt_verify_threadpool(hashed_password: String, password_to_verify: String) -> bool { - let (sender, receiver) = mpsc::channel(); - rayon::spawn(move || { - let thread_result = ::verify_password(hashed_password, password_to_verify); - sender.send(thread_result); - }); - let result = receiver.recv().unwrap(); - result + return ::verify_password_threadpool(hashed_password, password_to_verify); } #[test] diff --git a/src/sponges/ascon_aead.rs b/src/sponges/ascon_aead.rs index bc1b519..41e311a 100644 --- a/src/sponges/ascon_aead.rs +++ b/src/sponges/ascon_aead.rs @@ -1,40 +1,10 @@ -use aes_gcm::AeadCore; -use ascon_aead::{aead::{generic_array::GenericArray, Aead, KeyInit, OsRng}, Ascon128}; +use cas_lib::sponges::{ascon_aead::AsconAead, cas_ascon_aead::CASAsconAead}; use napi_derive::napi; -use super::cas_ascon_aead::{CASAsconAead}; -pub struct AsconAead; - -impl CASAsconAead for AsconAead { - fn encrypt(key: Vec, nonce: Vec, plaintext: Vec) -> Vec { - let key_generic_array = GenericArray::from_slice(&key); - let nonce_generic_array = GenericArray::from_slice(&nonce); - let cipher = Ascon128::new(key_generic_array); - let ciphertext = cipher.encrypt(&nonce_generic_array, plaintext.as_ref()).unwrap(); - ciphertext - } - - fn decrypt(key: Vec, nonce: Vec, ciphertext: Vec) -> Vec { - let key_generic_array = GenericArray::from_slice(&key); - let nonce_generic_array = GenericArray::from_slice(&nonce); - let cipher = Ascon128::new(key_generic_array); - let plaintext = cipher.decrypt(&nonce_generic_array, ciphertext.as_ref()).unwrap(); - plaintext - } - - fn generate_key() -> Vec { - return Ascon128::generate_key(&mut OsRng).to_vec(); - } - - fn generate_nonce() -> Vec { - return Ascon128::generate_nonce(&mut OsRng).to_vec(); - } -} - #[napi] pub fn ascon128_key_generate() -> Vec { - return AsconAead::generate_key(); + return ::generate_key(); } #[test] @@ -45,7 +15,7 @@ fn test_ascon128_key_generate() { #[napi] pub fn ascon128_nonce_generate() -> Vec { - return AsconAead::generate_nonce(); + return ::generate_nonce(); } #[test] @@ -56,13 +26,13 @@ pub fn test_ascon128_nonce_generate() { #[napi] pub fn ascon128_encrypt(key: Vec, nonce: Vec, plaintext: Vec) -> Vec { - return AsconAead::encrypt(key, nonce, plaintext); + return ::encrypt(key, nonce, plaintext); } #[test] pub fn test_ascon128_encrypt() { - let key = AsconAead::generate_key(); - let nonce = AsconAead::generate_nonce(); + let key = ::generate_key(); + let nonce = ::generate_nonce(); let plaintext = b"Hello, World!".to_vec(); let ciphertext = ascon128_encrypt(key.clone(), nonce.clone(), plaintext.clone()); assert_ne!(ciphertext, plaintext); @@ -70,13 +40,13 @@ pub fn test_ascon128_encrypt() { #[napi] pub fn ascon128_decrypt(key: Vec, nonce: Vec, ciphertext: Vec) -> Vec { - return AsconAead::decrypt(key, nonce, ciphertext); + return ::decrypt(key, nonce, ciphertext); } #[test] pub fn test_ascon128_decrypt() { - let key = AsconAead::generate_key(); - let nonce = AsconAead::generate_nonce(); + let key = ::generate_key(); + let nonce = ::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()); diff --git a/src/sponges/cas_ascon_aead.rs b/src/sponges/cas_ascon_aead.rs deleted file mode 100644 index f41556e..0000000 --- a/src/sponges/cas_ascon_aead.rs +++ /dev/null @@ -1,6 +0,0 @@ -pub trait CASAsconAead { - fn generate_key() -> Vec; - fn generate_nonce() -> Vec; - fn encrypt(key: Vec, nonce: Vec, plaintext: Vec) -> Vec; - fn decrypt(key: Vec, nonce: Vec, ciphertext: Vec) -> Vec; -} \ No newline at end of file diff --git a/src/symmetric/aes.rs b/src/symmetric/aes.rs index 085266f..6dd1227 100644 --- a/src/symmetric/aes.rs +++ b/src/symmetric/aes.rs @@ -1,85 +1,9 @@ -use aes_gcm::Key; +use cas_lib::symmetric::{aes::{CASAES128, CASAES256}, cas_symmetric_encryption::CASAESEncryption}; use napi_derive::napi; -use rand::rngs::OsRng; use rand::{RngCore, SeedableRng}; use rand_chacha::ChaCha20Rng; -use aes_gcm::{ - aead::{generic_array::GenericArray, Aead}, - Aes128Gcm, Aes256Gcm, KeyInit, Nonce, -}; - -use super::cas_symmetric_encryption::{AesKeyFromX25519SharedSecret, CASAESEncryption}; -pub struct CASAES128; -pub struct CASAES256; - -impl CASAESEncryption for CASAES256 { - fn generate_key() -> Vec { - return Aes256Gcm::generate_key(&mut OsRng).to_vec(); - } - - fn encrypt_plaintext(aes_key: Vec, nonce: Vec, plaintext: Vec) -> Vec { - let key = GenericArray::from_slice(&aes_key); - let mut cipher = Aes256Gcm::new(&key); - let nonce = Nonce::from_slice(&nonce); - let ciphertext = cipher.encrypt(nonce, plaintext.as_ref()).unwrap(); - ciphertext - } - - fn decrypt_ciphertext(aes_key: Vec, nonce: Vec, ciphertext: Vec) -> Vec { - let key = GenericArray::from_slice(&aes_key); - let mut cipher = Aes256Gcm::new(&key); - let nonce = Nonce::from_slice(&nonce); - let plaintext = cipher.decrypt(nonce, ciphertext.as_ref()).unwrap(); - plaintext - } - - fn key_from_x25519_shared_secret(shared_secret: Vec) -> AesKeyFromX25519SharedSecret { - let aes_key = Key::::from_slice(&shared_secret); - let mut aes_nonce: [u8; 12] = Default::default(); - aes_nonce.copy_from_slice(&shared_secret[..12]); - let result = AesKeyFromX25519SharedSecret { - aes_key: aes_key.to_vec(), - aes_nonce: aes_nonce.to_vec(), - }; - result - } -} - -impl CASAESEncryption for CASAES128 { - fn generate_key() -> Vec { - return Aes128Gcm::generate_key(&mut OsRng).to_vec(); - } - - fn encrypt_plaintext(aes_key: Vec, nonce: Vec, plaintext: Vec) -> Vec { - let key = GenericArray::from_slice(&aes_key); - let mut cipher = Aes128Gcm::new(&key); - let nonce = Nonce::from_slice(&nonce); - let ciphertext = cipher.encrypt(nonce, plaintext.as_ref()).unwrap(); - ciphertext - } - - fn decrypt_ciphertext(aes_key: Vec, nonce: Vec, ciphertext: Vec) -> Vec { - let key = GenericArray::from_slice(&aes_key); - let mut cipher = Aes128Gcm::new(&key); - let nonce = Nonce::from_slice(&nonce); - let plaintext = cipher.decrypt(nonce, ciphertext.as_ref()).unwrap(); - plaintext - } - - fn key_from_x25519_shared_secret(shared_secret: Vec) -> AesKeyFromX25519SharedSecret { - let mut aes_key: [u8; 16] = Default::default(); - aes_key.copy_from_slice(&shared_secret[..16]); - let aes_key_slice = Key::::from_slice(&aes_key); - let mut aes_nonce: [u8; 12] = Default::default(); - aes_nonce.copy_from_slice(&shared_secret[..12]); - let result = AesKeyFromX25519SharedSecret { - aes_key: aes_key_slice.to_vec(), - aes_nonce: aes_nonce.to_vec(), - }; - result - } -} +use super::types::CASAesKeyFromX25519SharedSecret; #[napi] pub fn aes_nonce() -> Vec { @@ -92,46 +16,46 @@ pub fn aes_nonce() -> Vec { #[napi] pub fn aes128_key() -> Vec { - return CASAES128::generate_key(); + return ::generate_key(); } #[napi] pub fn aes256_key() -> Vec { - return CASAES256::generate_key(); + return ::generate_key(); } #[napi] pub fn aes128_encrypt(aes_key: Vec, nonce: Vec, plaintext: Vec) -> Vec { - return CASAES128::encrypt_plaintext(aes_key, nonce, plaintext); + return ::encrypt_plaintext(aes_key, nonce, plaintext); } #[napi] pub fn aes128_decrypt(aes_key: Vec, nonce: Vec, ciphertext: Vec) -> Vec { - return CASAES128::decrypt_ciphertext(aes_key, nonce, ciphertext); + return ::decrypt_ciphertext(aes_key, nonce, ciphertext); } #[napi] pub fn aes256_encrypt(aes_key: Vec, nonce: Vec, plaintext: Vec) -> Vec { - return CASAES256::encrypt_plaintext(aes_key, nonce, plaintext); + return ::encrypt_plaintext(aes_key, nonce, plaintext); } #[napi] pub fn aes256_decrypt(aes_key: Vec, nonce: Vec, ciphertext: Vec) -> Vec { - return CASAES256::decrypt_ciphertext(aes_key, nonce, ciphertext); + return ::decrypt_ciphertext(aes_key, nonce, ciphertext); } #[napi] pub fn aes_256_key_from_x25519_shared_secret( shared_secret: Vec, -) -> AesKeyFromX25519SharedSecret { - return CASAES256::key_from_x25519_shared_secret(shared_secret); +) -> CASAesKeyFromX25519SharedSecret { + return ::key_from_x25519_shared_secret(shared_secret).into(); } #[napi] pub fn aes_128_key_from_x25519_shared_secret( shared_secret: Vec, -) -> AesKeyFromX25519SharedSecret { - return CASAES128::key_from_x25519_shared_secret(shared_secret); +) -> CASAesKeyFromX25519SharedSecret { + return ::key_from_x25519_shared_secret(shared_secret).into(); } #[test] diff --git a/src/symmetric/cas_symmetric_encryption.rs b/src/symmetric/cas_symmetric_encryption.rs deleted file mode 100644 index a4949c6..0000000 --- a/src/symmetric/cas_symmetric_encryption.rs +++ /dev/null @@ -1,14 +0,0 @@ -use napi_derive::napi; - -#[napi(constructor)] -pub struct AesKeyFromX25519SharedSecret { - pub aes_key: Vec, - pub aes_nonce: Vec, -} - -pub trait CASAESEncryption { - fn generate_key() -> Vec; - fn encrypt_plaintext(aes_key: Vec, nonce: Vec, plaintext: Vec) -> Vec; - fn decrypt_ciphertext(aes_key: Vec, nonce: Vec, ciphertext: Vec) -> Vec; - fn key_from_x25519_shared_secret(shared_secret: Vec) -> AesKeyFromX25519SharedSecret; -} diff --git a/src/symmetric/types.rs b/src/symmetric/types.rs new file mode 100644 index 0000000..7b509fc --- /dev/null +++ b/src/symmetric/types.rs @@ -0,0 +1,17 @@ +use cas_lib::symmetric::cas_symmetric_encryption::AesKeyFromX25519SharedSecret; +use napi_derive::napi; + +#[napi(constructor)] +pub struct CASAesKeyFromX25519SharedSecret { + pub aes_key: Vec, + pub aes_nonce: Vec, +} + +impl From for CASAesKeyFromX25519SharedSecret { + fn from(value: AesKeyFromX25519SharedSecret) -> Self { + CASAesKeyFromX25519SharedSecret { + aes_key: value.aes_key, + aes_nonce: value.aes_nonce + } + } +} \ No newline at end of file diff --git a/test-ts/asymmetric.test.spec.ts b/test-ts/asymmetric.test.spec.ts index 30bd82d..879c751 100644 --- a/test-ts/asymmetric.test.spec.ts +++ b/test-ts/asymmetric.test.spec.ts @@ -1,11 +1,11 @@ import { assert } from "chai"; -import { RSAWrapper, RsaKeyPairResult } from ".."; +import { CASRSAKeyPairResult, RSAWrapper } from ".."; import { areEqual } from "./helpers/array"; describe("Asymmetric Tests", () => { it("RSA 4096 encrypt and decrypt equals", () => { const rsaWrapper: RSAWrapper = new RSAWrapper(); - const keys: RsaKeyPairResult = rsaWrapper.generateKeys(4096); + const keys: CASRSAKeyPairResult = rsaWrapper.generateKeys(4096); const tohashed: string = "This is my array to encrypt"; const encoder = new TextEncoder(); const tohashBytes: Array = Array.from(encoder.encode(tohashed)); @@ -17,7 +17,7 @@ describe("Asymmetric Tests", () => { it("RSA 2048 Sign and Verify", () => { const rsaWrapper = new RSAWrapper(); - const keys: RsaKeyPairResult = rsaWrapper.generateKeys(2048); + const keys: CASRSAKeyPairResult = rsaWrapper.generateKeys(2048); const tohashed: string = "This is my encrypt"; const encoder = new TextEncoder(); const toSignBytes: Array = Array.from(encoder.encode(tohashed)); diff --git a/test-ts/digital-signature.test.spec.ts b/test-ts/digital-signature.test.spec.ts index ebee53c..96a2a23 100644 --- a/test-ts/digital-signature.test.spec.ts +++ b/test-ts/digital-signature.test.spec.ts @@ -1,6 +1,6 @@ import { assert } from "chai"; import { DigitalSignatureFactory, DigitalSignatureType } from "../src-ts/digital-signature/digital-signature-factory"; -import { RSADigitalSignatureResult } from "../index"; +import { CASRSADigitalSignatureResult } from "../index"; describe("Digital Signature", () => { it("SHA 512 RSA pass", () => { @@ -8,7 +8,7 @@ describe("Digital Signature", () => { const tohashed: string = "This is my array to encrypt"; const encoder = new TextEncoder(); const tohashBytes: Array = Array.from(encoder.encode(tohashed)); - const dsResult: RSADigitalSignatureResult = shaDsWrapper.createRsa(2048, tohashBytes); + const dsResult: CASRSADigitalSignatureResult = shaDsWrapper.createRsa(2048, tohashBytes); const verify = shaDsWrapper.verifyRSa(dsResult.publicKey, tohashBytes, dsResult.signature); assert.equal(verify, true); }); @@ -20,7 +20,7 @@ describe("Digital Signature", () => { const encoder = new TextEncoder(); const tohashBytes: Array = Array.from(encoder.encode(tohashed)); const badBytes: Array = Array.from(encoder.encode(notOriginal)); - const dsResult: RSADigitalSignatureResult = shaDsWrapper.createRsa(4096, tohashBytes); + const dsResult: CASRSADigitalSignatureResult = shaDsWrapper.createRsa(4096, tohashBytes); const verify = shaDsWrapper.verifyRSa(dsResult.publicKey, badBytes, dsResult.signature); assert.equal(verify, false); }); @@ -30,7 +30,7 @@ describe("Digital Signature", () => { const tohashed: string = "This is my array to encrypt"; const encoder = new TextEncoder(); const tohashBytes: Array = Array.from(encoder.encode(tohashed)); - const dsResult: RSADigitalSignatureResult = shaDsWrapper.createRsa(2048, tohashBytes); + const dsResult: CASRSADigitalSignatureResult = shaDsWrapper.createRsa(2048, tohashBytes); const verify = shaDsWrapper.verifyRSa(dsResult.publicKey, tohashBytes, dsResult.signature); assert.equal(verify, true); }); @@ -42,7 +42,7 @@ describe("Digital Signature", () => { const encoder = new TextEncoder(); const tohashBytes: Array = Array.from(encoder.encode(tohashed)); const badBytes: Array = Array.from(encoder.encode(notOriginal)); - const dsResult: RSADigitalSignatureResult = shaDsWrapper.createRsa(4096, tohashBytes); + const dsResult: CASRSADigitalSignatureResult = shaDsWrapper.createRsa(4096, tohashBytes); const verify = shaDsWrapper.verifyRSa(dsResult.publicKey, badBytes, dsResult.signature); assert.equal(verify, false); }); diff --git a/test-ts/insecure-channel.test.spec.ts b/test-ts/insecure-channel.test.spec.ts index 43872e9..1b01a8d 100644 --- a/test-ts/insecure-channel.test.spec.ts +++ b/test-ts/insecure-channel.test.spec.ts @@ -1,6 +1,6 @@ import {AESWrapper} from "../src-ts/symmetric/index"; import {X25519Wrapper} from "../src-ts/key_exchange/index"; -import {X25519SecretPublicKeyResult} from "../index"; +import {CASx25519SecretPublicKeyResult} from "../index"; import { areEqual } from "./helpers/array"; import { assert } from "chai"; @@ -8,8 +8,8 @@ describe("Insecure Channel Tests", () => { it("AES256-GBC Diffie Hellman X25519", () => { const aesWrapper = new AESWrapper(); const x25519Wrapper = new X25519Wrapper(); - const alice_keys: X25519SecretPublicKeyResult = x25519Wrapper.generateSecretAndPublicKey(); - const bob_keys: X25519SecretPublicKeyResult = x25519Wrapper.generateSecretAndPublicKey(); + const alice_keys: CASx25519SecretPublicKeyResult = x25519Wrapper.generateSecretAndPublicKey(); + const bob_keys: CASx25519SecretPublicKeyResult = x25519Wrapper.generateSecretAndPublicKey(); const alice_shared_secret = x25519Wrapper.generateSharedSecret(alice_keys.secretKey, bob_keys.publicKey); const bob_shared_secret = x25519Wrapper.generateSharedSecret(bob_keys.secretKey, alice_keys.publicKey); @@ -30,8 +30,8 @@ describe("Insecure Channel Tests", () => { it("AES128-GBC Diffie Hellman X25519", () => { const aesWrapper = new AESWrapper(); const x25519Wrapper = new X25519Wrapper(); - const alice_keys: X25519SecretPublicKeyResult = x25519Wrapper.generateSecretAndPublicKey(); - const bob_keys: X25519SecretPublicKeyResult = x25519Wrapper.generateSecretAndPublicKey(); + const alice_keys: CASx25519SecretPublicKeyResult = x25519Wrapper.generateSecretAndPublicKey(); + const bob_keys: CASx25519SecretPublicKeyResult = x25519Wrapper.generateSecretAndPublicKey(); const alice_shared_secret = x25519Wrapper.generateSharedSecret(alice_keys.secretKey, bob_keys.publicKey); const bob_shared_secret = x25519Wrapper.generateSharedSecret(bob_keys.secretKey, alice_keys.publicKey);