Skip to content

Commit

Permalink
Added e2e encryption with two Ed25519 keys and started documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
fuzzc0re committed Dec 6, 2022
1 parent 38c4437 commit 2b203ac
Show file tree
Hide file tree
Showing 14 changed files with 907 additions and 149 deletions.
61 changes: 60 additions & 1 deletion __tests__/symmetricEncrypt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,66 @@ const arraysAreEqual = (arr1: Uint8Array, arr2: Uint8Array): boolean => {
};

describe("Encryption and decryption with symmetric key test suite.", () => {
test("Encryption and decryption work.", async () => {
test("End to End encryption and decryption work.", async () => {
const message = await dcrypto.randomBytes(32);
const aliceKeyPair = await dcrypto.keyPair();
const bobKeyPair = await dcrypto.keyPair();

const previousBlockHash = await dcrypto.randomBytes(
crypto_hash_sha512_BYTES,
);

const encrypted = await dcrypto.encrypt(
message,
bobKeyPair.publicKey,
aliceKeyPair.secretKey,
previousBlockHash,
);
const decrypted = await dcrypto.decrypt(
encrypted,
aliceKeyPair.publicKey,
bobKeyPair.secretKey,
previousBlockHash,
);

const encryptionMemory = dcrypto.loadWasmMemory.encrypt(
message.length,
crypto_hash_sha512_BYTES,
);
const encryptionModule = await dcrypto.loadWasmModule({
wasmMemory: encryptionMemory,
});
const encryptedWithModule = await dcrypto.encrypt(
message,
bobKeyPair.publicKey,
aliceKeyPair.secretKey,
previousBlockHash,
encryptionModule,
);

const decryptionMemory = dcrypto.loadWasmMemory.decrypt(
encrypted.length,
crypto_hash_sha512_BYTES,
);
const decryptionModule = await dcrypto.loadWasmModule({
wasmMemory: decryptionMemory,
});
const decryptedWithModule = await dcrypto.decrypt(
encrypted,
aliceKeyPair.publicKey,
bobKeyPair.secretKey,
previousBlockHash,
decryptionModule,
);

expect(decrypted[0]).toBe(message[0]);
expect(decrypted[1]).toBe(message[1]);
expect(decrypted[31]).toBe(message[31]);
expect(arraysAreEqual(encryptedWithModule, encrypted)).toBe(false);
expect(arraysAreEqual(decryptedWithModule, decrypted)).toBe(true);
});

test("Encryption and decryption with provided key work.", async () => {
const message = await dcrypto.randomBytes(32);
const key = await dcrypto.randomBytes(crypto_kx_SESSIONKEYBYTES);

Expand Down
29 changes: 29 additions & 0 deletions examples/browser/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,35 @@
Original : ${uint8ToHex(message)}\n`,
);

const aliceKeyPair = await dcrypto.keyPairFromMnemonic(mnemonic4);
const bobKeyPair = await dcrypto.keyPairFromMnemonic(mnemonic3);

const e2eencrypted = await dcrypto.encrypt(
message,
bobKeyPair.publicKey,
aliceKeyPair.secretKey,
hash,
);

console.log(
`AEAD e2e encrypted box for random message between alice and bob: ${uint8ToHex(
encrypted,
)}`,
);

const e2edecrypted = await dcrypto.decrypt(
encrypted,
aliceKeyPair.publicKey,
bobKeyPair.secretKey,
hash,
);

console.log(
`E2E decrypted AEAD box should be equal to random message: \n\
Decrypted: ${uint8ToHex(e2edecrypted)} \n\
Original : ${uint8ToHex(message)}\n`,
);

const sharesLen = 255;
const threshold = 165;
const shares = await dcrypto.splitSecret(
Expand Down
Loading

0 comments on commit 2b203ac

Please sign in to comment.