2022-11-23 16:59:15 +11:00
|
|
|
import { expect } from "chai";
|
|
|
|
|
import fc from "fast-check";
|
|
|
|
|
|
|
|
|
|
import { getPublicKey } from "./crypto/index.js";
|
|
|
|
|
import {
|
|
|
|
|
decryptAsymmetric,
|
|
|
|
|
decryptSymmetric,
|
|
|
|
|
encryptAsymmetric,
|
|
|
|
|
encryptSymmetric,
|
|
|
|
|
postCipher,
|
2023-08-16 20:18:13 +05:30
|
|
|
preCipher
|
2023-11-28 01:02:12 +01:00
|
|
|
} from "./encryption.js";
|
2022-11-23 16:59:15 +11:00
|
|
|
|
2023-11-28 01:02:12 +01:00
|
|
|
describe("Waku Encryption", function () {
|
2023-10-23 17:53:56 +03:00
|
|
|
this.timeout(20000);
|
2022-11-23 16:59:15 +11:00
|
|
|
it("Asymmetric encrypt & decrypt", async function () {
|
|
|
|
|
await fc.assert(
|
|
|
|
|
fc.asyncProperty(
|
|
|
|
|
fc.uint8Array({ minLength: 1 }),
|
|
|
|
|
fc.uint8Array({ min: 1, minLength: 32, maxLength: 32 }),
|
|
|
|
|
async (message, privKey) => {
|
|
|
|
|
const publicKey = getPublicKey(privKey);
|
|
|
|
|
|
|
|
|
|
const enc = await encryptAsymmetric(message, publicKey);
|
|
|
|
|
const res = await decryptAsymmetric(enc, privKey);
|
|
|
|
|
|
|
|
|
|
expect(res).deep.equal(message);
|
2023-08-16 20:18:13 +05:30
|
|
|
}
|
|
|
|
|
)
|
2022-11-23 16:59:15 +11:00
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("Symmetric encrypt & Decrypt", async function () {
|
|
|
|
|
await fc.assert(
|
|
|
|
|
fc.asyncProperty(
|
|
|
|
|
fc.uint8Array(),
|
|
|
|
|
fc.uint8Array({ minLength: 32, maxLength: 32 }),
|
|
|
|
|
async (message, key) => {
|
|
|
|
|
const enc = await encryptSymmetric(message, key);
|
|
|
|
|
const res = await decryptSymmetric(enc, key);
|
|
|
|
|
|
|
|
|
|
expect(res).deep.equal(message);
|
2023-08-16 20:18:13 +05:30
|
|
|
}
|
|
|
|
|
)
|
2022-11-23 16:59:15 +11:00
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("pre and post cipher", async function () {
|
|
|
|
|
await fc.assert(
|
|
|
|
|
fc.asyncProperty(fc.uint8Array(), async (message) => {
|
|
|
|
|
const enc = await preCipher(message);
|
|
|
|
|
const res = postCipher(enc);
|
|
|
|
|
|
|
|
|
|
expect(res?.payload).deep.equal(
|
|
|
|
|
message,
|
2023-08-16 20:18:13 +05:30
|
|
|
"Payload was not encrypted then decrypted correctly"
|
2022-11-23 16:59:15 +11:00
|
|
|
);
|
2023-08-16 20:18:13 +05:30
|
|
|
})
|
2022-11-23 16:59:15 +11:00
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("Sign & Recover", async function () {
|
|
|
|
|
await fc.assert(
|
|
|
|
|
fc.asyncProperty(
|
|
|
|
|
fc.uint8Array(),
|
|
|
|
|
fc.uint8Array({ minLength: 32, maxLength: 32 }),
|
|
|
|
|
async (message, sigPrivKey) => {
|
|
|
|
|
const sigPubKey = getPublicKey(sigPrivKey);
|
|
|
|
|
|
|
|
|
|
const enc = await preCipher(message, sigPrivKey);
|
|
|
|
|
const res = postCipher(enc);
|
|
|
|
|
|
|
|
|
|
expect(res?.payload).deep.equal(
|
|
|
|
|
message,
|
2023-08-16 20:18:13 +05:30
|
|
|
"Payload was not encrypted then decrypted correctly"
|
2022-11-23 16:59:15 +11:00
|
|
|
);
|
|
|
|
|
expect(res?.sig?.publicKey).deep.equal(
|
|
|
|
|
sigPubKey,
|
2023-08-16 20:18:13 +05:30
|
|
|
"signature Public key was not recovered from encrypted then decrypted signature"
|
2022-11-23 16:59:15 +11:00
|
|
|
);
|
2023-08-16 20:18:13 +05:30
|
|
|
}
|
|
|
|
|
)
|
2022-11-23 16:59:15 +11:00
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
});
|