From cfdd960e51d14547b3219ea7fff5ed2b85b9d145 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ant=C3=B3nio=20Conselheiro?= Date: Mon, 26 Feb 2024 19:19:23 -0300 Subject: [PATCH] incluindo estrutura de pastas para adotar o chacha e o salsa --- .../test-report/index.js | 0 .../test-report/result.js | 0 packages/ciphers/aes/cbc/index.ts | 8 +-- packages/ciphers/aes/ctr/index.ts | 8 +-- packages/ciphers/aes/ecb/index.ts | 6 +- packages/ciphers/aes/gcm/index.ts | 8 +-- packages/ciphers/aes/siv/index.ts | 8 +-- packages/ciphers/chacha/chacha20.ts | 0 packages/ciphers/chacha/chacha20poly1305.ts | 0 packages/ciphers/chacha/chacha8.ts | 58 +++++++++++++++++++ packages/ciphers/chacha/xchacha20.ts | 0 packages/ciphers/chacha/xchacha20poly1305.ts | 0 packages/ciphers/hashes/ripemd160.ts | 4 ++ .../{aes => }/initialization-vector.ts | 0 packages/ciphers/{aes => }/kdf.ts | 2 +- packages/ciphers/{aes => }/number-once.ts | 0 .../ciphers/{aes => }/openssl-serializer.ts | 0 packages/ciphers/salsa/salsa20.ts | 0 packages/ciphers/salsa/salsa20poly1305.ts | 0 packages/ciphers/salsa/xsalsa20.ts | 0 packages/ciphers/salsa/xsalsa20poly1305.ts | 0 packages/ciphers/{aes => }/salt.ts | 0 22 files changed, 82 insertions(+), 20 deletions(-) mode change 100644 => 100755 docs/ciphers/jest-html-reporters-attach/test-report/index.js mode change 100644 => 100755 docs/ciphers/jest-html-reporters-attach/test-report/result.js create mode 100755 packages/ciphers/chacha/chacha20.ts create mode 100755 packages/ciphers/chacha/chacha20poly1305.ts create mode 100755 packages/ciphers/chacha/chacha8.ts create mode 100755 packages/ciphers/chacha/xchacha20.ts create mode 100755 packages/ciphers/chacha/xchacha20poly1305.ts create mode 100755 packages/ciphers/hashes/ripemd160.ts rename packages/ciphers/{aes => }/initialization-vector.ts (100%) rename packages/ciphers/{aes => }/kdf.ts (93%) rename packages/ciphers/{aes => }/number-once.ts (100%) rename packages/ciphers/{aes => }/openssl-serializer.ts (100%) create mode 100755 packages/ciphers/salsa/salsa20.ts create mode 100755 packages/ciphers/salsa/salsa20poly1305.ts create mode 100755 packages/ciphers/salsa/xsalsa20.ts create mode 100755 packages/ciphers/salsa/xsalsa20poly1305.ts rename packages/ciphers/{aes => }/salt.ts (100%) diff --git a/docs/ciphers/jest-html-reporters-attach/test-report/index.js b/docs/ciphers/jest-html-reporters-attach/test-report/index.js old mode 100644 new mode 100755 diff --git a/docs/ciphers/jest-html-reporters-attach/test-report/result.js b/docs/ciphers/jest-html-reporters-attach/test-report/result.js old mode 100644 new mode 100755 diff --git a/packages/ciphers/aes/cbc/index.ts b/packages/ciphers/aes/cbc/index.ts index cba6bb1..7379754 100755 --- a/packages/ciphers/aes/cbc/index.ts +++ b/packages/ciphers/aes/cbc/index.ts @@ -3,10 +3,10 @@ import { bytesToUtf8, hexToBytes, utf8ToBytes } from '@noble/ciphers/utils'; import { cbc } from '@noble/ciphers/webcrypto/aes'; import { randomBytes } from '@noble/hashes/utils'; import { base64 } from '@scure/base'; -import { TInitializationVectorParams, getInitializationVector } from '../initialization-vector'; -import { kdf } from '../kdf'; -import { OpenSSLSerializer } from '../openssl-serializer'; -import { getSalt } from '../salt'; +import { TInitializationVectorParams, getInitializationVector } from '../../initialization-vector'; +import { kdf } from '../../kdf'; +import { OpenSSLSerializer } from '../../openssl-serializer'; +import { getSalt } from '../../salt'; class EncryptedURIAESCBCDecrypter extends EncryptedURIDecrypter { constructor( diff --git a/packages/ciphers/aes/ctr/index.ts b/packages/ciphers/aes/ctr/index.ts index ceffdfe..977e30c 100755 --- a/packages/ciphers/aes/ctr/index.ts +++ b/packages/ciphers/aes/ctr/index.ts @@ -3,10 +3,10 @@ import { bytesToUtf8, hexToBytes, utf8ToBytes } from '@noble/ciphers/utils'; import { ctr } from '@noble/ciphers/webcrypto/aes'; import { randomBytes } from "@noble/hashes/utils"; import { base64 } from '@scure/base'; -import { TInitializationVectorParams, getInitializationVector } from "../initialization-vector"; -import { kdf } from '../kdf'; -import { OpenSSLSerializer } from "../openssl-serializer"; -import { getSalt } from '../salt'; +import { TInitializationVectorParams, getInitializationVector } from "../../initialization-vector"; +import { kdf } from '../../kdf'; +import { OpenSSLSerializer } from "../../openssl-serializer"; +import { getSalt } from '../../salt'; class EncryptedURIAESCTRDecrypter extends EncryptedURIDecrypter { constructor( diff --git a/packages/ciphers/aes/ecb/index.ts b/packages/ciphers/aes/ecb/index.ts index db609db..4a0cde5 100755 --- a/packages/ciphers/aes/ecb/index.ts +++ b/packages/ciphers/aes/ecb/index.ts @@ -3,9 +3,9 @@ import { ecb } from '@noble/ciphers/aes'; import { bytesToUtf8, utf8ToBytes } from '@noble/ciphers/utils'; import { randomBytes } from '@noble/hashes/utils'; import { base64 } from '@scure/base'; -import { kdf } from '../kdf'; -import { OpenSSLSerializer } from '../openssl-serializer'; -import { getSalt } from '../salt'; +import { kdf } from '../../kdf'; +import { OpenSSLSerializer } from '../../openssl-serializer'; +import { getSalt } from '../../salt'; class EncryptedURIAESECBDecrypter extends EncryptedURIDecrypter { constructor( diff --git a/packages/ciphers/aes/gcm/index.ts b/packages/ciphers/aes/gcm/index.ts index c64838f..6b26749 100755 --- a/packages/ciphers/aes/gcm/index.ts +++ b/packages/ciphers/aes/gcm/index.ts @@ -3,10 +3,10 @@ import { bytesToUtf8, hexToBytes, utf8ToBytes } from '@noble/ciphers/utils'; import { gcm } from '@noble/ciphers/webcrypto/aes'; import { randomBytes } from '@noble/hashes/utils'; import { base64 } from '@scure/base'; -import { kdf } from '../kdf'; -import { TNumberOnceParams, getNumberOnce } from '../number-once'; -import { OpenSSLSerializer } from '../openssl-serializer'; -import { getSalt } from '../salt'; +import { kdf } from '../../kdf'; +import { TNumberOnceParams, getNumberOnce } from '../../number-once'; +import { OpenSSLSerializer } from '../../openssl-serializer'; +import { getSalt } from '../../salt'; class EncryptedURIAESGCMDecrypter extends EncryptedURIDecrypter { constructor( diff --git a/packages/ciphers/aes/siv/index.ts b/packages/ciphers/aes/siv/index.ts index 82e439c..2842473 100755 --- a/packages/ciphers/aes/siv/index.ts +++ b/packages/ciphers/aes/siv/index.ts @@ -3,10 +3,10 @@ import { siv } from '@noble/ciphers/aes'; import { bytesToUtf8, hexToBytes, utf8ToBytes } from '@noble/ciphers/utils'; import { randomBytes } from '@noble/hashes/utils'; import { base64 } from '@scure/base'; -import { kdf } from '../kdf'; -import { TNumberOnceParams, getNumberOnce } from '../number-once'; -import { OpenSSLSerializer } from '../openssl-serializer'; -import { getSalt } from '../salt'; +import { kdf } from '../../kdf'; +import { TNumberOnceParams, getNumberOnce } from '../../number-once'; +import { OpenSSLSerializer } from '../../openssl-serializer'; +import { getSalt } from '../../salt'; class EncryptedURIAESSIVDecrypter extends EncryptedURIDecrypter { constructor( diff --git a/packages/ciphers/chacha/chacha20.ts b/packages/ciphers/chacha/chacha20.ts new file mode 100755 index 0000000..e69de29 diff --git a/packages/ciphers/chacha/chacha20poly1305.ts b/packages/ciphers/chacha/chacha20poly1305.ts new file mode 100755 index 0000000..e69de29 diff --git a/packages/ciphers/chacha/chacha8.ts b/packages/ciphers/chacha/chacha8.ts new file mode 100755 index 0000000..30a45df --- /dev/null +++ b/packages/ciphers/chacha/chacha8.ts @@ -0,0 +1,58 @@ +import { EncryptedURIAlgorithm, EncryptedURIDecrypter, EncryptedURIEncrypter, TEncryptedURI, TEncryptedURIResultset } from '@encrypted-uri/core'; +import { chacha8 } from '@noble/ciphers/chacha'; +import { TNumberOnceParams, getNumberOnce } from '../number-once'; +import { hexToBytes, randomBytes, utf8ToBytes } from '@noble/hashes/utils'; +import { kdf } from '../kdf'; +import { OpenSSLSerializer } from '../openssl-serializer'; +import { base64 } from '@scure/base'; +import { getSalt } from '../salt'; + +class EncryptedURIAESSIVDecrypter extends EncryptedURIDecrypter { + constructor( + decoded: TEncryptedURI, + password: string + ) { + super(decoded, password); + } + + async decrypt(): Promise { + const nonce = getNumberOnce(this.decoded); + const cipher = base64.decode(this.decoded.cipher); + const params = getSalt(cipher, this.decoded?.params); + const derivatedKey = kdf(this.password, params.salt, this.decoded); + const result = await chacha8(derivatedKey, hexToBytes(nonce)) + .decrypt(params.cipher); + + return bytesToUtf8(result); + } + } + + @EncryptedURIAlgorithm({ + algorithm: 'aes/siv', + decrypter: EncryptedURIAESSIVDecrypter + }) + // eslint-disable-next-line @typescript-eslint/no-unused-vars + class EncryptedURIAESSIVEncrypter extends EncryptedURIEncrypter { + + constructor( + params: TEncryptedURIResultset + ) { + super(params); + } + + async encrypt(): Promise> { + const numberOnceHex = getNumberOnce(this.params); + const nonce = hexToBytes(numberOnceHex); + const content = utf8ToBytes(this.params.content); + const saltLength = 8; + const salt = randomBytes(saltLength); + const derivatedKey = kdf(this.params.password, salt, this.params); + const cipher = await chacha8(derivatedKey, nonce, content); + + return Promise.resolve({ + cipher: base64.encode(OpenSSLSerializer.encode(cipher, salt)), + params: { no: numberOnceHex } + }); + } + } + \ No newline at end of file diff --git a/packages/ciphers/chacha/xchacha20.ts b/packages/ciphers/chacha/xchacha20.ts new file mode 100755 index 0000000..e69de29 diff --git a/packages/ciphers/chacha/xchacha20poly1305.ts b/packages/ciphers/chacha/xchacha20poly1305.ts new file mode 100755 index 0000000..e69de29 diff --git a/packages/ciphers/hashes/ripemd160.ts b/packages/ciphers/hashes/ripemd160.ts new file mode 100755 index 0000000..0e1979a --- /dev/null +++ b/packages/ciphers/hashes/ripemd160.ts @@ -0,0 +1,4 @@ +import { ripemd160 } from '@noble/hashes/ripemd160'; +import { HashSupport } from './hash-support'; + +HashSupport.addSupport('ripemd160', ripemd160); diff --git a/packages/ciphers/aes/initialization-vector.ts b/packages/ciphers/initialization-vector.ts similarity index 100% rename from packages/ciphers/aes/initialization-vector.ts rename to packages/ciphers/initialization-vector.ts diff --git a/packages/ciphers/aes/kdf.ts b/packages/ciphers/kdf.ts similarity index 93% rename from packages/ciphers/aes/kdf.ts rename to packages/ciphers/kdf.ts index 2acdcaa..b41fb89 100755 --- a/packages/ciphers/aes/kdf.ts +++ b/packages/ciphers/kdf.ts @@ -1,6 +1,6 @@ import { EncryptedURI, TEncryptedURI, TEncryptedURIResultset, TURIParams } from '@encrypted-uri/core'; import { pbkdf2 } from '@noble/hashes/pbkdf2'; -import { HashSupport } from '../hashes/hash-support'; +import { HashSupport } from './hashes/hash-support'; export function kdf( password: string, diff --git a/packages/ciphers/aes/number-once.ts b/packages/ciphers/number-once.ts similarity index 100% rename from packages/ciphers/aes/number-once.ts rename to packages/ciphers/number-once.ts diff --git a/packages/ciphers/aes/openssl-serializer.ts b/packages/ciphers/openssl-serializer.ts similarity index 100% rename from packages/ciphers/aes/openssl-serializer.ts rename to packages/ciphers/openssl-serializer.ts diff --git a/packages/ciphers/salsa/salsa20.ts b/packages/ciphers/salsa/salsa20.ts new file mode 100755 index 0000000..e69de29 diff --git a/packages/ciphers/salsa/salsa20poly1305.ts b/packages/ciphers/salsa/salsa20poly1305.ts new file mode 100755 index 0000000..e69de29 diff --git a/packages/ciphers/salsa/xsalsa20.ts b/packages/ciphers/salsa/xsalsa20.ts new file mode 100755 index 0000000..e69de29 diff --git a/packages/ciphers/salsa/xsalsa20poly1305.ts b/packages/ciphers/salsa/xsalsa20poly1305.ts new file mode 100755 index 0000000..e69de29 diff --git a/packages/ciphers/aes/salt.ts b/packages/ciphers/salt.ts similarity index 100% rename from packages/ciphers/aes/salt.ts rename to packages/ciphers/salt.ts