2022-11-23 16:59:15 +11:00
|
|
|
import { expect } from "chai";
|
|
|
|
|
import fc from "fast-check";
|
|
|
|
|
|
|
|
|
|
import { getPublicKey } from "./crypto/index.js";
|
2022-12-05 15:14:17 +11:00
|
|
|
import { createDecoder, createEncoder } from "./symmetric.js";
|
2022-11-23 16:59:15 +11:00
|
|
|
|
|
|
|
|
describe("Symmetric Encryption", function () {
|
|
|
|
|
it("Round trip binary encryption [symmetric, no signature]", async function () {
|
|
|
|
|
await fc.assert(
|
|
|
|
|
fc.asyncProperty(
|
2023-03-13 14:15:57 +11:00
|
|
|
fc.string(),
|
|
|
|
|
fc.string(),
|
2022-11-23 16:59:15 +11:00
|
|
|
fc.uint8Array({ minLength: 1 }),
|
|
|
|
|
fc.uint8Array({ min: 1, minLength: 32, maxLength: 32 }),
|
2023-03-13 14:15:57 +11:00
|
|
|
async (pubSubTopic, contentTopic, payload, symKey) => {
|
2023-02-02 11:37:28 +05:30
|
|
|
const encoder = createEncoder({
|
2023-03-13 14:15:57 +11:00
|
|
|
contentTopic,
|
2023-02-02 11:37:28 +05:30
|
|
|
symKey,
|
|
|
|
|
});
|
2022-11-23 16:59:15 +11:00
|
|
|
const bytes = await encoder.toWire({ payload });
|
|
|
|
|
|
2023-03-13 14:15:57 +11:00
|
|
|
const decoder = createDecoder(contentTopic, symKey);
|
2022-11-23 16:59:15 +11:00
|
|
|
const protoResult = await decoder.fromWireToProtoObj(bytes!);
|
|
|
|
|
if (!protoResult) throw "Failed to proto decode";
|
2023-03-13 14:15:57 +11:00
|
|
|
const result = await decoder.fromProtoObj(pubSubTopic, protoResult);
|
2022-11-23 16:59:15 +11:00
|
|
|
if (!result) throw "Failed to decode";
|
|
|
|
|
|
2023-03-13 14:15:57 +11:00
|
|
|
expect(result.contentTopic).to.equal(contentTopic);
|
|
|
|
|
expect(result.pubSubTopic).to.equal(pubSubTopic);
|
2022-11-23 16:59:15 +11:00
|
|
|
expect(result.version).to.equal(1);
|
|
|
|
|
expect(result?.payload).to.deep.equal(payload);
|
|
|
|
|
expect(result.signature).to.be.undefined;
|
|
|
|
|
expect(result.signaturePublicKey).to.be.undefined;
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("Round trip binary encryption [symmetric, signature]", async function () {
|
|
|
|
|
await fc.assert(
|
|
|
|
|
fc.asyncProperty(
|
2023-03-13 14:15:57 +11:00
|
|
|
fc.string(),
|
|
|
|
|
fc.string(),
|
2022-11-23 16:59:15 +11:00
|
|
|
fc.uint8Array({ minLength: 1 }),
|
|
|
|
|
fc.uint8Array({ min: 1, minLength: 32, maxLength: 32 }),
|
|
|
|
|
fc.uint8Array({ min: 1, minLength: 32, maxLength: 32 }),
|
2023-03-13 14:15:57 +11:00
|
|
|
async (pubSubTopic, contentTopic, payload, sigPrivKey, symKey) => {
|
2022-11-23 16:59:15 +11:00
|
|
|
const sigPubKey = getPublicKey(sigPrivKey);
|
|
|
|
|
|
2023-02-02 11:37:28 +05:30
|
|
|
const encoder = createEncoder({
|
2023-03-13 14:15:57 +11:00
|
|
|
contentTopic,
|
2023-02-02 11:37:28 +05:30
|
|
|
symKey,
|
|
|
|
|
sigPrivKey,
|
|
|
|
|
});
|
2022-11-23 16:59:15 +11:00
|
|
|
const bytes = await encoder.toWire({ payload });
|
|
|
|
|
|
2023-03-13 14:15:57 +11:00
|
|
|
const decoder = createDecoder(contentTopic, symKey);
|
2022-11-23 16:59:15 +11:00
|
|
|
const protoResult = await decoder.fromWireToProtoObj(bytes!);
|
|
|
|
|
if (!protoResult) throw "Failed to proto decode";
|
2023-03-13 14:15:57 +11:00
|
|
|
const result = await decoder.fromProtoObj(pubSubTopic, protoResult);
|
2022-11-23 16:59:15 +11:00
|
|
|
if (!result) throw "Failed to decode";
|
|
|
|
|
|
2023-03-13 14:15:57 +11:00
|
|
|
expect(result.contentTopic).to.equal(contentTopic);
|
|
|
|
|
expect(result.pubSubTopic).to.equal(pubSubTopic);
|
2022-11-23 16:59:15 +11:00
|
|
|
expect(result.version).to.equal(1);
|
|
|
|
|
expect(result?.payload).to.deep.equal(payload);
|
|
|
|
|
expect(result.signature).to.not.be.undefined;
|
|
|
|
|
expect(result.signaturePublicKey).to.deep.eq(sigPubKey);
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
});
|