Skip to content

Commit

Permalink
Merge pull request #32 from antonioconselheiro/feature/interoperability
Browse files Browse the repository at this point in the history
interoperability
  • Loading branch information
antonioconselheiro committed Feb 14, 2024
2 parents 45e45fb + 199094b commit 9285479
Show file tree
Hide file tree
Showing 37 changed files with 113 additions and 115 deletions.
Empty file modified .vscode/settings.json
100644 → 100755
Empty file.
25 changes: 13 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ Algorithm with no param:
Basic use, how to decode and encrypt and how to decode and decrypt:

```typescript
import { EncryptedURIAlgorithm, EncryptedURIDecrypter, EncryptedURIEncrypter, TEncryptedURI, TEncryptedURIKDFConfig, TEncryptedURIResultset, TURIParams } from '@encrypted-uri/core';
import { EncryptedURIAlgorithm, EncryptedURIDecrypter, EncryptedURIEncrypter, TEncryptedURI, TEncryptedURIResultset, TURIParams } from '@encrypted-uri/core';
import { ecb } from '@noble/ciphers/aes';
import { bytesToUtf8, utf8ToBytes } from '@noble/ciphers/utils';
import { randomBytes } from '@noble/hashes/utils';
Expand All @@ -70,10 +70,9 @@ import { base64 } from '@scure/base';
class EncryptedURIAESECBDecrypter<T extends TURIParams = TURIParams> extends EncryptedURIDecrypter<T> {
constructor(
decoded: TEncryptedURI<T>,
password: string,
defaultsKDF: Required<TEncryptedURIKDFConfig>
password: string
) {
super(decoded, password, defaultsKDF);
super(decoded, password);
}

async decrypt(): Promise<string> {
Expand All @@ -90,10 +89,11 @@ class EncryptedURIAESECBDecrypter<T extends TURIParams = TURIParams> extends Enc
algorithm: 'aes/ecb',
decrypter: EncryptedURIAESECBDecrypter
})
// eslint-disable-next-line @typescript-eslint/no-unused-vars
class EncryptedURIAESECBEncrypter<T extends TURIParams = TURIParams> extends EncryptedURIEncrypter<TURIParams> {

constructor(
protected override params: TEncryptedURIResultset<T>
params: TEncryptedURIResultset<T>
) {
super(params);
}
Expand All @@ -102,18 +102,19 @@ class EncryptedURIAESECBEncrypter<T extends TURIParams = TURIParams> extends Enc
const content = utf8ToBytes(this.params.content);
const saltLength = 8;
const salt = randomBytes(saltLength);
const rawCipher = await ecb(kdf(this.params.password, salt, this.params.kdf)).encrypt(content);
const rawCipher = await ecb(kdf(this.params.password, salt, this.params)).encrypt(content);
const cipher = base64.encode(OpenSSLSerializer.encode(rawCipher, salt));

return Promise.resolve({ cipher });
}
}


```

Advanced use, how to add default encrypter and how to add more alias to an algorithm:
```typescript
import { EncryptedURI, EncryptedURIAlgorithm, EncryptedURIDecrypter, EncryptedURIEncrypter, TEncryptedURI, TEncryptedURIKDFConfig, TEncryptedURIResultset } from '@encrypted-uri/core';
import { EncryptedURI, EncryptedURIAlgorithm, EncryptedURIDecrypter, EncryptedURIEncrypter, TEncryptedURI, TEncryptedURIResultset } from '@encrypted-uri/core';
import { bytesToUtf8, hexToBytes, utf8ToBytes } from '@noble/ciphers/utils';
import { cbc } from '@noble/ciphers/webcrypto/aes';
import { randomBytes } from '@noble/hashes/utils';
Expand All @@ -122,10 +123,9 @@ import { base64 } from '@scure/base';
class EncryptedURIAESCBCDecrypter extends EncryptedURIDecrypter<TInitializationVectorParams> {
constructor(
decoded: TEncryptedURI<TInitializationVectorParams>,
password: string,
defaultsKDF: Required<TEncryptedURIKDFConfig>
password: string
) {
super(decoded, password, defaultsKDF);
super(decoded, password);
}

async decrypt(): Promise<string> {
Expand All @@ -147,7 +147,7 @@ class EncryptedURIAESCBCDecrypter extends EncryptedURIDecrypter<TInitializationV
class EncryptedURIAESCBCEncrypter extends EncryptedURIEncrypter<TInitializationVectorParams> {

constructor(
protected override params: TEncryptedURIResultset<TInitializationVectorParams>
params: TEncryptedURIResultset<TInitializationVectorParams>
) {
super(params);
}
Expand All @@ -158,7 +158,7 @@ class EncryptedURIAESCBCEncrypter extends EncryptedURIEncrypter<TInitializationV
const content = utf8ToBytes(this.params.content);
const saltLength = 8;
const salt = randomBytes(saltLength);
const cipher = await cbc(kdf(this.params.password, salt, this.params.kdf), iv).encrypt(content);
const cipher = await cbc(kdf(this.params.password, salt, this.params), iv).encrypt(content);

return Promise.resolve({
cipher: base64.encode(OpenSSLSerializer.encode(cipher, salt)),
Expand All @@ -169,6 +169,7 @@ class EncryptedURIAESCBCEncrypter extends EncryptedURIEncrypter<TInitializationV

EncryptedURI.setAlgorithm('', EncryptedURIAESCBCEncrypter, EncryptedURIAESCBCDecrypter);
EncryptedURI.setAlgorithm('aes', EncryptedURIAESCBCEncrypter, EncryptedURIAESCBCDecrypter);

```

## Example of practical application
Expand Down
Empty file modified TODO
100644 → 100755
Empty file.

Large diffs are not rendered by default.

Empty file modified docs/ciphers/test-report.html
100644 → 100755
Empty file.
2 changes: 1 addition & 1 deletion docs/core/jest-html-reporters-attach/test-report/result.js

Large diffs are not rendered by default.

Empty file modified docs/core/test-report.html
100644 → 100755
Empty file.
Empty file modified package-lock.json
100644 → 100755
Empty file.
4 changes: 1 addition & 3 deletions packages/ciphers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@
[![Npm Total Downloads](https://img.shields.io/npm/dt/@encrypted-uri/ciphers.svg)](https://github.com/antonioconselheiro/encrypted-uri)
[![Npm Monthly Downloads](https://img.shields.io/npm/dm/@encrypted-uri/ciphers.svg)](https://github.com/antonioconselheiro/encrypted-uri)

Include AES algorithms from @noble/ciphers into Encrypted URI (@encrypted-uri/core). Only ```initialization vector``` and ```number once``` params are included.
Include AES algorithms from @noble/ciphers into Encrypted URI (@encrypted-uri/core).

Support for Encrypted URI using _@scure_ and _@noble_ packages.

*under beta test*

## Installation

```npm install @encrypted-uri/core @encrypted-uri/ciphers --save```
Expand Down
9 changes: 0 additions & 9 deletions packages/ciphers/aes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,6 @@ describe('success flow aes', () => {
expect(decoded).toEqual(originalMessage);
});

it('cbc generated from other implementation with the same algorithm type and params', async () => {
const decoded = await EncryptedURI.decrypt('encrypted:aes?iv=1dc8d28370372579a75feac6b5bf5290;U2FsdGVkX18K2mCM3jqJz9SSPC2Rss61NOk4JWeG5IE=', 'teste123', {
rounds: 250000,
hasher: 'sha256',
derivateKeyLength: 32
});
expect(decoded).toEqual('teste123');
});

it('ctr', async () => {
const originalMessage = 'mensagem secreta, favor não ler em voz alta';
const password = 'senha123';
Expand Down
8 changes: 6 additions & 2 deletions packages/ciphers/aes/cbc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ class EncryptedURIAESCBCDecrypter extends EncryptedURIDecrypter<TInitializationV
const cipher = base64.decode(this.decoded.cipher);
const params = getSalt(cipher, this.decoded?.params);

const result = await cbc(kdf(this.password, params.salt, this.decoded), hexToBytes(ivhex))
const derivatedKey = kdf(this.password, params.salt, this.decoded);

const result = await cbc(derivatedKey, hexToBytes(ivhex))
.decrypt(params.cipher);

return bytesToUtf8(result);
Expand All @@ -46,7 +48,9 @@ class EncryptedURIAESCBCEncrypter extends EncryptedURIEncrypter<TInitializationV
const content = utf8ToBytes(this.params.content);
const saltLength = 8;
const salt = randomBytes(saltLength);
const cipher = await cbc(kdf(this.params.password, salt, this.params), iv).encrypt(content);

const derivatedKey = kdf(this.params.password, salt, this.params);
const cipher = await cbc(derivatedKey, iv).encrypt(content);

return Promise.resolve({
cipher: base64.encode(OpenSSLSerializer.encode(cipher, salt)),
Expand Down
7 changes: 3 additions & 4 deletions packages/ciphers/aes/kdf.ts
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ import { HashSupport } from '../hashes/hash-support';
export function kdf<T extends TURIParams>(
password: string,
salt: Uint8Array,
decoded?: TEncryptedURI<T> | TEncryptedURIResultset<T>
kdfConfig?: TEncryptedURI<T> | TEncryptedURIResultset<T>
): Uint8Array {
const cfg = EncryptedURI.getKDFConfig(decoded);

const cfg = EncryptedURI.getKDFConfig(kdfConfig);
const saltLength = 8;
if (salt.length !== saltLength) {
throw new Error(`salt length must be 8 bytes, ${salt.length} bytes was given`);
throw new Error(`salt length must be ${saltLength} bytes, ${salt.length} bytes was given`);
}

if (cfg.kdf === 'pbkdf2') {
Expand Down
Empty file modified packages/ciphers/aes/openssl-serializer.ts
100644 → 100755
Empty file.
Empty file modified packages/ciphers/aes/salt.ts
100644 → 100755
Empty file.
Empty file modified packages/ciphers/hashes/hash-support.ts
100644 → 100755
Empty file.
Empty file modified packages/ciphers/hashes/index.ts
100644 → 100755
Empty file.
Empty file modified packages/ciphers/hashes/keccak_224.ts
100644 → 100755
Empty file.
Empty file modified packages/ciphers/hashes/keccak_256.ts
100644 → 100755
Empty file.
Empty file modified packages/ciphers/hashes/keccak_384.ts
100644 → 100755
Empty file.
Empty file modified packages/ciphers/hashes/keccak_512.ts
100644 → 100755
Empty file.
Empty file modified packages/ciphers/hashes/sha256.ts
100644 → 100755
Empty file.
Empty file modified packages/ciphers/hashes/sha384.ts
100644 → 100755
Empty file.
Empty file modified packages/ciphers/hashes/sha3_224.ts
100644 → 100755
Empty file.
Empty file modified packages/ciphers/hashes/sha3_256.ts
100644 → 100755
Empty file.
Empty file modified packages/ciphers/hashes/sha3_384.ts
100644 → 100755
Empty file.
Empty file modified packages/ciphers/hashes/sha3_512.ts
100644 → 100755
Empty file.
Empty file modified packages/ciphers/hashes/sha512.ts
100644 → 100755
Empty file.
Empty file modified packages/ciphers/hashes/sha512_256.ts
100644 → 100755
Empty file.
Empty file modified packages/ciphers/jest.config.js
100644 → 100755
Empty file.
26 changes: 2 additions & 24 deletions packages/ciphers/kdf.test.ts
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import { EncryptedURI, TEncryptedURIKDFConfig } from '@encrypted-uri/core';
import './aes';
import './hashes';

xdescribe('kdf success flow', () => {
describe('kdf success flow', () => {

it('[2] kdf include all parameters including default', async () => {
const kdf: TEncryptedURIKDFConfig = {
kdf: 'pbkdf2',
ignoreDefaults: false,
hasher: 'sha256',
rounds: 100,
rounds: 10,
derivateKeyLength: 32
};

Expand Down Expand Up @@ -41,7 +41,6 @@ xdescribe('kdf success flow', () => {
password,
kdf
});
console.info(' >>> encoded', encoded)

const decrypted = await EncryptedURI.decrypt(encoded, password);
expect(decrypted).toEqual(originalMessage);
Expand Down Expand Up @@ -237,24 +236,3 @@ xdescribe('kdf success flow', () => {
expect(decrypted).toEqual(originalMessage);
});
});

describe('kdf failure flow', () => {
it('[1] overriding kdf config with wrong default values', async () => {
const originalMessage = 'mensagem secreta, favor não ler em voz alta';
const password = 'senha123';

const encoded = await EncryptedURI.encrypt({
algorithm: 'aes/ctr',
content: originalMessage,
password
});

const decrypted = await EncryptedURI.decrypt(encoded, password, {
kdf: 'pbkdf2',
hasher: 'sha256',
rounds: 32,
derivateKeyLength: 32
});
expect(decrypted).not.toEqual(originalMessage);
});
});
Empty file modified packages/ciphers/package-lock.json
100644 → 100755
Empty file.
4 changes: 2 additions & 2 deletions packages/ciphers/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@encrypted-uri/ciphers",
"version": "1.0.10",
"version": "1.1.1",
"description": "encrypt and decrypt from encrypted uri",
"repository": {
"type": "git",
Expand Down Expand Up @@ -37,6 +37,6 @@
"tslib": "^2.6.2"
},
"peerDependencies": {
"@encrypted-uri/core": "^1.0.0"
"@encrypted-uri/core": "^1.1.1"
}
}
22 changes: 11 additions & 11 deletions packages/ciphers/params.test.ts
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { EncryptedURI, EncryptedURIParser, TEncryptedURIKDFConfig } from '@encry
import './aes';
import './hashes';

xdescribe('hashing customization', () => {
describe('hashing customization', () => {
it('[3] kdf with hasher sha512', async () => {
const kdf: TEncryptedURIKDFConfig = {
hasher: 'sha512' as any as 'sha256'
hasher: 'sha512'
};

const originalMessage = 'mensagem secreta, favor não ler em voz alta';
Expand All @@ -28,7 +28,7 @@ xdescribe('hashing customization', () => {

it('[4] kdf with hasher sha512_256', async () => {
const kdf: TEncryptedURIKDFConfig = {
hasher: 'sha512_256' as any as 'sha256'
hasher: 'sha512_256'
};

const originalMessage = 'mensagem secreta, favor não ler em voz alta';
Expand All @@ -51,7 +51,7 @@ xdescribe('hashing customization', () => {

it('[5] kdf with hasher sha384', async () => {
const kdf: TEncryptedURIKDFConfig = {
hasher: 'sha384' as any as 'sha256'
hasher: 'sha384'
};

const originalMessage = 'mensagem secreta, favor não ler em voz alta';
Expand All @@ -74,7 +74,7 @@ xdescribe('hashing customization', () => {

it('[6] kdf with hasher sha3_512', async () => {
const kdf: TEncryptedURIKDFConfig = {
hasher: 'sha3_512' as any as 'sha256'
hasher: 'sha3_512'
};

const originalMessage = 'mensagem secreta, favor não ler em voz alta';
Expand All @@ -97,7 +97,7 @@ xdescribe('hashing customization', () => {

it('[7] kdf with hasher sha3_384', async () => {
const kdf: TEncryptedURIKDFConfig = {
hasher: 'sha3_384' as any as 'sha256'
hasher: 'sha3_384'
};

const originalMessage = 'mensagem secreta, favor não ler em voz alta';
Expand All @@ -120,7 +120,7 @@ xdescribe('hashing customization', () => {

it('[8] kdf with hasher sha3_256', async () => {
const kdf: TEncryptedURIKDFConfig = {
hasher: 'sha3_256' as any as 'sha256'
hasher: 'sha3_256'
};

const originalMessage = 'mensagem secreta, favor não ler em voz alta';
Expand All @@ -143,7 +143,7 @@ xdescribe('hashing customization', () => {

it('[9] kdf with hasher sha3_224', async () => {
const kdf: TEncryptedURIKDFConfig = {
hasher: 'sha3_224' as any as 'sha256'
hasher: 'sha3_224'
};

const originalMessage = 'mensagem secreta, favor não ler em voz alta';
Expand All @@ -166,7 +166,7 @@ xdescribe('hashing customization', () => {

it('[10] kdf with hasher keccak_512', async () => {
const kdf: TEncryptedURIKDFConfig = {
hasher: 'keccak_512' as any as 'sha256'
hasher: 'keccak_512'
};

const originalMessage = 'mensagem secreta, favor não ler em voz alta';
Expand All @@ -189,7 +189,7 @@ xdescribe('hashing customization', () => {

it('[11] kdf with hasher keccak_384', async () => {
const kdf: TEncryptedURIKDFConfig = {
hasher: 'keccak_384' as any as 'sha256'
hasher: 'keccak_384'
};

const originalMessage = 'mensagem secreta, favor não ler em voz alta';
Expand All @@ -212,7 +212,7 @@ xdescribe('hashing customization', () => {

it('[12] kdf with hasher keccak_256', async () => {
const kdf: TEncryptedURIKDFConfig = {
hasher: 'keccak_256' as any as 'sha256'
hasher: 'keccak_256'
};

const originalMessage = 'mensagem secreta, favor não ler em voz alta';
Expand Down
49 changes: 49 additions & 0 deletions packages/core/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,3 +344,52 @@ describe('EncryptedURI object', () => {
})
});
});

describe('EncryptedURI getKDFConfig', () => {
it('[1] EncryptedURI get KDF config from KDF config', () => {
const configs = EncryptedURI.getKDFConfig({
algorithm: 'aes/cbc',
content: 'mensagem secreta, favor não ler em voz alta',
password: 'senha123',
kdf: {
kdf: 'pbkdf2',
ignoreDefaults: false,
hasher: 'sha256',
rounds: 10,
derivateKeyLength: 32
}
});

expect(configs).toEqual({
kdf: 'pbkdf2',
ignoreDefaults: false,
hasher: 'sha256',
rounds: 10,
derivateKeyLength: 32
});

});


it('[2] EncryptedURI get KDF config from decoded URI', () => {
const configs = EncryptedURI.getKDFConfig({
algorithm: 'aes/cbc',
cipher: 'U2FsdGVkX18WeA03azX1tWETWsG/oSiQYzgI0en6RPgQ7Z2i9YbxCL3VcfzL6nsFo5Sdf0xF/UVatnJEehkcHQ==',
queryString: 'iv=44b9c510f05a8461c0ad153ba915d9dc&h=keccak_224',
params: {
iv: '44b9c510f05a8461c0ad153ba915d9dc',
h: 'keccak_224'
}
});

expect(configs).toEqual({
kdf: 'pbkdf2',
ignoreDefaults: true,
hasher: 'keccak_224',
rounds: 32,
derivateKeyLength: 32
});

});

});
Loading

0 comments on commit 9285479

Please sign in to comment.