diff --git a/.github/workflows/main-pr-linux.yml b/.github/workflows/main-pr-linux.yml index 6314a27..1e1e513 100644 --- a/.github/workflows/main-pr-linux.yml +++ b/.github/workflows/main-pr-linux.yml @@ -14,7 +14,7 @@ jobs: strategy: matrix: - node-version: [20.x] + node-version: [22.x] # See supported Node.js release schedule at https://nodejs.org/en/about/releases/ steps: diff --git a/.github/workflows/main-pr-windows.yml b/.github/workflows/main-pr-windows.yml index 53b8104..70d4f1a 100644 --- a/.github/workflows/main-pr-windows.yml +++ b/.github/workflows/main-pr-windows.yml @@ -14,7 +14,7 @@ jobs: strategy: matrix: - node-version: [20.x] + node-version: [22.x] # See supported Node.js release schedule at https://nodejs.org/en/about/releases/ steps: diff --git a/.github/workflows/main-publish.yml b/.github/workflows/main-publish.yml index c3199f8..c750b0d 100644 --- a/.github/workflows/main-publish.yml +++ b/.github/workflows/main-publish.yml @@ -14,7 +14,7 @@ jobs: strategy: matrix: - node-version: [20.x] + node-version: [22.x] # See supported Node.js release schedule at https://nodejs.org/en/about/releases/ steps: diff --git a/Cargo.toml b/Cargo.toml index 6fda28b..1670a80 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,10 +8,10 @@ path = "src/lib.rs" crate-type = ["cdylib"] [dependencies] -napi = "2" -napi-derive = "2" +napi = "2.16.13" +napi-derive = "2.16.12" csbindgen = "1.9.1" -cas-lib = "0.1.6" +cas-lib = "0.2.2" [build-dependencies] napi-build = "1" \ No newline at end of file diff --git a/index.d.ts b/index.d.ts index d1a7399..ffdac83 100644 --- a/index.d.ts +++ b/index.d.ts @@ -3,80 +3,78 @@ /* auto-generated by NAPI-RS */ -export function argon2Hash(password: string): string -export function argon2HashThreadPool(password: string): string -export function argon2Verify(hashedPassword: string, passwordToVerify: string): boolean -export function argon2VerifyThreadpool(hashedPassword: string, passwordToVerify: string): boolean -export function bcryptHash(passwordToHash: string): string -export function bcryptHashThreadpool(passwordToHash: string): string -export function bcryptVerify(hashedPassword: string, passwordToVerify: string): boolean -export function bcryptVerifyThreadpool(passwordToHash: string, passwordToVerify: string): boolean -export function scryptHash(passwordToHash: string): string -export function scryptVerify(hashedPassword: string, passwordToVerify: string): boolean -export function scryptHashThreadpool(passwordToHash: string): string -export function scryptVerifyThreadpool(hashedPassword: string, passwordToVerify: string): boolean -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(): CASx25519SecretPublicKeyResult -export function x25519DiffieHellman(mySecretKey: Array, usersPublicKey: Array): Array -export function aesNonce(): Array -export function aes128Key(): Array -export function aes256Key(): Array -export function aes128Encrypt(aesKey: Array, nonce: Array, plaintext: Array): Array -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): 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): CASRSADigitalSignatureResult -export function sha512RsaVerifyDigitalSignature(publicKey: string, dataToVerify: Array, signature: Array): boolean -export function sha256RsaDigitalSignature(rsaKeySize: number, dataToSign: Array): CASRSADigitalSignatureResult -export function sha256RsaVerifyDigitalSignature(publicKey: string, dataToVerify: Array, signature: Array): boolean -export function sha512Ed25519DigitalSignature(dataToSign: Array): CASSHAED25519DalekDigitalSignatureResult -export function sha512Ed25519DigitalSignatureVerify(publicKey: Array, dataToVerify: Array, signature: Array): boolean -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 function hmacSign(key: Array, message: Array): Array -export function hmacSignThreadpool(key: Array, message: Array): Array -export function hmacVerify(key: Array, message: Array, signature: Array): boolean -export function hmacVerifyThreadpool(key: Array, message: Array, signature: Array): boolean +export declare function argon2Hash(password: string): string +export declare function argon2HashThreadPool(password: string): string +export declare function argon2Verify(hashedPassword: string, passwordToVerify: string): boolean +export declare function argon2VerifyThreadpool(hashedPassword: string, passwordToVerify: string): boolean +export declare function bcryptHash(passwordToHash: string): string +export declare function bcryptHashThreadpool(passwordToHash: string): string +export declare function bcryptVerify(hashedPassword: string, passwordToVerify: string): boolean +export declare function bcryptVerifyThreadpool(passwordToHash: string, passwordToVerify: string): boolean +export declare function scryptHash(passwordToHash: string): string +export declare function scryptVerify(hashedPassword: string, passwordToVerify: string): boolean +export declare function scryptHashThreadpool(passwordToHash: string): string +export declare function scryptVerifyThreadpool(hashedPassword: string, passwordToVerify: string): boolean +export declare function sha512(dataToHash: Array): Array +export declare function sha512Verify(dataToHash: Array, dataToVerify: Array): boolean +export declare function sha256(dataToHash: Array): Array +export declare function sha256Verify(dataToHash: Array, dataToVerify: Array): boolean +export declare function x25519GenerateSecretAndPublicKey(): CASx25519SecretPublicKeyResult +export declare function x25519DiffieHellman(mySecretKey: Array, usersPublicKey: Array): Array +export declare function aesNonce(): Array +export declare function aes128Key(): Array +export declare function aes256Key(): Array +export declare function aes128Encrypt(aesKey: Array, nonce: Array, plaintext: Array): Array +export declare function aes128Decrypt(aesKey: Array, nonce: Array, ciphertext: Array): Array +export declare function aes256Encrypt(aesKey: Array, nonce: Array, plaintext: Array): Array +export declare function aes256Decrypt(aesKey: Array, nonce: Array, ciphertext: Array): Array +export declare function aes256KeyFromX25519SharedSecret(sharedSecret: Array): CASAesKeyFromX25519SharedSecret +export declare function aes128KeyFromX25519SharedSecret(sharedSecret: Array): CASAesKeyFromX25519SharedSecret +export declare function generateRsaKeys(keySize: number): CasrsaKeyPairResult +export declare function signRsa(privateKey: string, hash: Array): Array +export declare function verifyRsa(publicKey: string, hash: Array, signature: Array): boolean +export declare function sha512RsaDigitalSignature(rsaKeySize: number, dataToSign: Array): CASRSADigitalSignatureResult +export declare function sha512RsaVerifyDigitalSignature(publicKey: string, dataToVerify: Array, signature: Array): boolean +export declare function sha256RsaDigitalSignature(rsaKeySize: number, dataToSign: Array): CASRSADigitalSignatureResult +export declare function sha256RsaVerifyDigitalSignature(publicKey: string, dataToVerify: Array, signature: Array): boolean +export declare function sha512Ed25519DigitalSignature(dataToSign: Array): CASSHAED25519DalekDigitalSignatureResult +export declare function sha512Ed25519DigitalSignatureVerify(publicKey: Array, dataToVerify: Array, signature: Array): boolean +export declare function sha256Ed25519DigitalSignature(dataToSign: Array): CASSHAED25519DalekDigitalSignatureResult +export declare function sha256Ed25519DigitalSignatureVerify(publicKey: Array, dataToVerify: Array, signature: Array): boolean +export declare function ascon128KeyGenerate(): Array +export declare function ascon128NonceGenerate(): Array +export declare function ascon128Encrypt(key: Array, nonce: Array, plaintext: Array): Array +export declare function ascon128Decrypt(key: Array, nonce: Array, ciphertext: Array): Array +export declare function hmacSign(key: Array, message: Array): Array +export declare function hmacSignThreadpool(key: Array, message: Array): Array +export declare function hmacVerify(key: Array, message: Array, signature: Array): boolean +export declare function hmacVerifyThreadpool(key: Array, message: Array, signature: Array): boolean export type CASx25519SecretPublicKeyResult = CaSx25519SecretPublicKeyResult -export class CaSx25519SecretPublicKeyResult { +export declare class CaSx25519SecretPublicKeyResult { publicKey: Array secretKey: Array constructor(publicKey: Array, secretKey: Array) } export type CASAesKeyFromX25519SharedSecret = CasAesKeyFromX25519SharedSecret -export class CasAesKeyFromX25519SharedSecret { +export declare class CasAesKeyFromX25519SharedSecret { aesKey: Array aesNonce: Array constructor(aesKey: Array, aesNonce: Array) } export type CASRSAKeyPairResult = CasrsaKeyPairResult -export class CasrsaKeyPairResult { +export declare class CasrsaKeyPairResult { privateKey: string publicKey: string constructor(privateKey: string, publicKey: string) } export type CASSHAED25519DalekDigitalSignatureResult = Casshaed25519DalekDigitalSignatureResult -export class Casshaed25519DalekDigitalSignatureResult { +export declare class Casshaed25519DalekDigitalSignatureResult { publicKey: Array signature: Array constructor(publicKey: Array, signature: Array) } export type CASRSADigitalSignatureResult = CasrsaDigitalSignatureResult -export class CasrsaDigitalSignatureResult { +export declare class CasrsaDigitalSignatureResult { publicKey: string privateKey: string signature: Array diff --git a/index.node b/index.node old mode 100644 new mode 100755 index f306b0c..444059d Binary files a/index.node and b/index.node differ diff --git a/package-lock.json b/package-lock.json index 311535b..1dd08d5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "cas-typescript-sdk", - "version": "1.0.0", + "version": "1.0.27", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "cas-typescript-sdk", - "version": "1.0.0", + "version": "1.0.27", "license": "Apache 2.0", "devDependencies": { "@napi-rs/cli": "^2.17.0", diff --git a/package.json b/package.json index bfb1bfa..7856eed 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "cas-typescript-sdk", - "version": "1.0.26", + "version": "1.0.27", "description": "", "main": "lib/index.js", "types": "lib/index.d.ts", @@ -9,7 +9,7 @@ "test": "cargo test && npm run build && mocha -r ts-node/register ./test-ts/**/*.ts --timeout 20000 --recursive", "node:test": "mocha -r ts-node/register ./test-ts/**/*.ts --timeout 20000 --recursive", "rust:test": "cargo test", - "build": "npm run build:rust && rm -rf lib && tsc", + "build": "npm run build:rust && tsc", "build:rust": "napi build --release", "prepare": "npm run build" }, diff --git a/src-ts/asymmetric/RSAWrapper.ts b/src-ts/asymmetric/RSAWrapper.ts index 6c91cd7..113fc16 100644 --- a/src-ts/asymmetric/RSAWrapper.ts +++ b/src-ts/asymmetric/RSAWrapper.ts @@ -1,4 +1,4 @@ -import { CASRSAKeyPairResult, decryptCiphertextRsa, encryptPlaintextRsa, generateRsaKeys, signRsa, verifyRsa } from "../../index"; +import { CASRSAKeyPairResult, generateRsaKeys, signRsa, verifyRsa } from "../../index"; export class RSAWrapper { @@ -21,33 +21,6 @@ export class RSAWrapper { * @returns Array */ - public encrypt(publicKey: string, plaintext: Array): Array { - if (!publicKey) { - throw new Error("You must provide a public key to encrypt with RSA"); - } - if (!plaintext || plaintext.length === 0) { - throw new Error("You must provide an array of plaintext bytes to encrypt with RSA"); - } - return encryptPlaintextRsa(publicKey, plaintext); - } - - /** - * Decrypts a ciphertext with an RSA private key. - * @param privateKey - * @param ciphertext - * @returns Array - */ - - public decrypt(privateKey: string, ciphertext: Array): Array { - if (!privateKey) { - throw new Error("You must provide a private key to encrypt with RSA"); - } - if (!ciphertext || ciphertext.length === 0) { - throw new Error("You must provide an array of ciphertext bytes to encrypt with RSA"); - } - return decryptCiphertextRsa(privateKey, ciphertext); - } - /** * Signs a byte array with an RSA private key for verification. * @param privateKey diff --git a/src-ts/hybrid/hybrid-encryption-wrapper.ts b/src-ts/hybrid/hybrid-encryption-wrapper.ts deleted file mode 100644 index 9a2a96e..0000000 --- a/src-ts/hybrid/hybrid-encryption-wrapper.ts +++ /dev/null @@ -1,76 +0,0 @@ -import { RSAWrapper } from "../asymmetric"; -import { AESWrapper } from "../symmetric"; -import { AesRsaHybridEncryptResult } from "./types/aes-rsa-hybird-encrypt-result"; -import { AESRSAHybridInitializer } from "./types/aes-rsa-hybrid-initializer"; - -export class HybridEncryptionWrapper { - private aesWrapper: AESWrapper; - private rsaWrapper: RSAWrapper; - - constructor() { - this.aesWrapper = new AESWrapper(); - this.rsaWrapper = new RSAWrapper(); - } - - /** - * Encrypts data with RSA/AES hybrid encryption. The data is encrypted with AES-GCM and the AES key is encrypted with the RSA public key. - * @param dataToEncrypt - * @param initalizer - * @returns AesRsaHybridEncryptResult - */ - public encrypt( - dataToEncrypt: Array, - initalizer: AESRSAHybridInitializer, - ): AesRsaHybridEncryptResult { - let encryptedData: Array = (initalizer.aesType === 128) - ? this.aesWrapper.aes128Encrypt( - initalizer.aesKey, - initalizer.aesNonce, - dataToEncrypt, - ) - : this.aesWrapper.aes256Encrypt( - initalizer.aesKey, - initalizer.aesNonce, - dataToEncrypt, - ); - let encryptedAesKey: Array = this.rsaWrapper.encrypt( - initalizer.rsaKeyPair.publicKey, - initalizer.aesKey, - ); - let result: AesRsaHybridEncryptResult = new AesRsaHybridEncryptResult( - encryptedData, - encryptedAesKey, - initalizer.aesType, - initalizer.aesNonce, - ); - return result; - } - - /** - * Decrypts data with RSA/AES hybrid encryption. The RSA private key decrypts the AES key and then the data is decrypted with AES-GCM. - * @param dataToEncrypt - * @param initalizer - * @returns AesRsaHybridEncryptResult - */ - public decrypt( - privateKey: string, - encryptResult: AesRsaHybridEncryptResult, - ): Array { - let plaintextAesKey = this.rsaWrapper.decrypt( - privateKey, - encryptResult.encryptedAesKey, - ); - let plaintext = (encryptResult.aesType === 128) - ? this.aesWrapper.aes128Decrypt( - plaintextAesKey, - encryptResult.aesNonce, - encryptResult.ciphertext, - ) - : this.aesWrapper.aes256Decrypt( - plaintextAesKey, - encryptResult.aesNonce, - encryptResult.ciphertext, - ); - return plaintext; - } -} diff --git a/src-ts/hybrid/index.ts b/src-ts/hybrid/index.ts deleted file mode 100644 index fb66f8c..0000000 --- a/src-ts/hybrid/index.ts +++ /dev/null @@ -1,9 +0,0 @@ -import { HybridEncryptionWrapper } from "./hybrid-encryption-wrapper"; -import { AesRsaHybridEncryptResult } from "./types/aes-rsa-hybird-encrypt-result"; -import { AESRSAHybridInitializer } from "./types/aes-rsa-hybrid-initializer"; - -export { - AesRsaHybridEncryptResult, - AESRSAHybridInitializer, - HybridEncryptionWrapper, -}; diff --git a/src-ts/hybrid/types/aes-rsa-hybird-encrypt-result.ts b/src-ts/hybrid/types/aes-rsa-hybird-encrypt-result.ts deleted file mode 100644 index e9f59c0..0000000 --- a/src-ts/hybrid/types/aes-rsa-hybird-encrypt-result.ts +++ /dev/null @@ -1,13 +0,0 @@ -export class AesRsaHybridEncryptResult { - ciphertext: Array; - encryptedAesKey: Array; - aesType: number; - aesNonce: Array; - - constructor(cipherText: Array, encryptAesKey: Array, aesType: number, aesNonce: Array) { - this.ciphertext = cipherText; - this.encryptedAesKey = encryptAesKey; - this.aesType = aesType; - this.aesNonce = aesNonce; - } -} \ No newline at end of file diff --git a/src-ts/hybrid/types/aes-rsa-hybrid-initializer.ts b/src-ts/hybrid/types/aes-rsa-hybrid-initializer.ts deleted file mode 100644 index f4f9d1b..0000000 --- a/src-ts/hybrid/types/aes-rsa-hybrid-initializer.ts +++ /dev/null @@ -1,29 +0,0 @@ -import { CASRSAKeyPairResult } from "../../.."; -import { RSAWrapper } from "../../asymmetric"; -import { AESWrapper } from "../../symmetric"; - -export class AESRSAHybridInitializer { - public aesType: number; - public aesKey: Array; - public aesNonce: Array; - 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. - * @param aesType - * @param rsaSize - */ - constructor(aesType: number, rsaSize: number) { - if (aesType !== 128 && aesType !== 256) { - throw new Error("Need an appropriate AES size to generate a hybrid initalizer"); - } - this.aesType = aesType; - let aesWrapper = new AESWrapper(); - this.aesKey = (aesType === 128) ? aesWrapper.aes128Key() : aesWrapper.aes256Key(); - this.aesNonce = aesWrapper.generateAESNonce(); - if (rsaSize !== 1028 && rsaSize !== 2048 && rsaSize !== 4096) { - throw new Error("You must provide an appropriate RSA Key pair size to generate a hybrid initalizer"); - } - this.rsaKeyPair = new RSAWrapper().generateKeys(rsaSize); - } -} \ No newline at end of file diff --git a/src-ts/index.ts b/src-ts/index.ts index cea822a..4b9b190 100644 --- a/src-ts/index.ts +++ b/src-ts/index.ts @@ -3,7 +3,6 @@ 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"; export * from "./message/index"; \ No newline at end of file diff --git a/src/asymmetric/cas_rsa.rs b/src/asymmetric/cas_rsa.rs index 07b9c5b..03cb218 100644 --- a/src/asymmetric/cas_rsa.rs +++ b/src/asymmetric/cas_rsa.rs @@ -21,16 +21,6 @@ pub fn generate_rsa_keys(key_size: u32) -> CASRSAKeyPairResult { return CASRSA::generate_rsa_keys(key_size as usize).into(); } -#[napi] -pub fn encrypt_plaintext_rsa(public_key: String, plaintext: Vec) -> Vec { - return CASRSA::encrypt_plaintext(public_key, plaintext); -} - -#[napi] -pub fn decrypt_ciphertext_rsa(private_key: String, ciphertext: Vec) -> Vec { - return CASRSA::decrypt_ciphertext(private_key, ciphertext); -} - #[napi] pub fn sign_rsa(private_key: String, hash: Vec) -> Vec { return CASRSA::sign(private_key, hash); diff --git a/test-ts/asymmetric.test.spec.ts b/test-ts/asymmetric.test.spec.ts index 879c751..0cf40c8 100644 --- a/test-ts/asymmetric.test.spec.ts +++ b/test-ts/asymmetric.test.spec.ts @@ -1,20 +1,7 @@ import { assert } from "chai"; 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: 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)); - const ciphertext = rsaWrapper.encrypt(keys.publicKey, tohashBytes); - const plaintext = rsaWrapper.decrypt(keys.privateKey, ciphertext); - let result = areEqual(tohashBytes, plaintext); - assert.isTrue(result); - }); - it("RSA 2048 Sign and Verify", () => { const rsaWrapper = new RSAWrapper(); const keys: CASRSAKeyPairResult = rsaWrapper.generateKeys(2048);