From cac00f9db553e4646f40e92a9406ced40df56fed Mon Sep 17 00:00:00 2001 From: hieu-w Date: Wed, 2 Oct 2024 02:01:20 +0700 Subject: [PATCH] Add 1 more test: should throw an error when decryption fails due to bad MAC --- test/test.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/test/test.js b/test/test.js index d297f39..ee265b6 100644 --- a/test/test.js +++ b/test/test.js @@ -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); @@ -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"); + } + }); });