Skip to content

Commit

Permalink
Add 1 more test: should throw an error when decryption fails due to b…
Browse files Browse the repository at this point in the history
…ad MAC
  • Loading branch information
hieu-w committed Oct 1, 2024
1 parent 74f99f6 commit cac00f9
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ describe("ECDH", function () {

describe("ECIES", function () {
let ephemPublicKey, encOpts, decOpts, iv, ciphertext, mac;

beforeEach(function () {
const ephemPrivateKey = Buffer.alloc(32);
ephemPrivateKey.fill(4);
Expand Down Expand Up @@ -410,4 +411,22 @@ describe("ECIES", function () {
done(e);
});
});

it("should throw an error when decryption fails due to bad MAC", async function () {
const testPrivateKey = Buffer.from("1111111111111111111111111111111111111111111111111111111111111111", "hex");
const testPublicKey = eccrypto.getPublic(testPrivateKey);

const testMessage = Buffer.from("Hello, world!");
const encrypted = await eccrypto.encrypt(testPublicKey, testMessage);

// Tamper with the MAC to make it invalid
encrypted.mac = Buffer.from("0000000000000000000000000000000000000000000000000000000000000000", "hex");

try {
await eccrypto.decrypt(testPrivateKey, encrypted);
throw new Error("Decryption should have failed");
} catch (error) {
expect(error.message).to.equal("bad MAC after trying padded");
}
});
});

0 comments on commit cac00f9

Please sign in to comment.