Update tests: reproduce the MAC failure.

This commit is contained in:
Lee Miller 2022-12-24 23:33:51 +02:00
parent 5d0a5f43aa
commit 8c9d298dc9
Signed by: lee.miller
GPG Key ID: 4F97A5EA88F4AB63
1 changed files with 41 additions and 9 deletions

50
test.js
View File

@ -195,15 +195,35 @@ describe("ECDH", function() {
});
describe("ECIES", function() {
var ephemPrivateKey = Buffer.alloc(32);
ephemPrivateKey.fill(4);
var ephemPublicKey = eccrypto.getPublic(ephemPrivateKey);
var iv = Buffer.alloc(16);
iv.fill(5);
var ciphertext = Buffer.from("bbf3f0e7486b552b0e2ba9c4ca8c4579", "hex");
var mac = Buffer.from("dbb14a9b53dbd6b763dba24dc99520f570cdf8095a8571db4bf501b535fda1ed", "hex");
var encOpts = {ephemPrivateKey: ephemPrivateKey, iv: iv};
var decOpts = {iv: iv, ephemPublicKey: ephemPublicKey, ciphertext: ciphertext, mac: mac};
var ephemPrivateKey = Buffer.alloc(32);
ephemPrivateKey.fill(4);
var ephemPublicKey = eccrypto.getPublic(ephemPrivateKey);
var iv = Buffer.alloc(16);
iv.fill(5);
var ciphertext = Buffer.from("bbf3f0e7486b552b0e2ba9c4ca8c4579", "hex");
var mac = Buffer.from("dbb14a9b53dbd6b763dba24dc99520f570cdf8095a8571db4bf501b535fda1ed", "hex");
var encOpts = {ephemPrivateKey: ephemPrivateKey, iv: iv};
var decOpts = {iv: iv, ephemPublicKey: ephemPublicKey, ciphertext: ciphertext, mac: mac};
// To comply to the Spec
const samplePrivateKey = Buffer.from(
"5be6facd941b76e9d3ead03029fbdb6b6e0809293f7fb197d0c51f84e96b8ba4",
"hex"),
sampleIV = Buffer.from("bddb7c2829b08038753084a2f3991681", "hex"),
// notice the simple (not encoded) pubkey here
samplePublicKey = Buffer.from(
"0409d4e5c0ab3d25fe048c64c9da1a242c7f19417e9517cd266950d72c755713585c6178e97fe092fc897c9a1f1720d5770ae8eaad2fa8fcbd08e9324a5dde1857", "hex"),
sampleData = "The quick brown fox jumps over the lazy dog.",
sampleCiphertext = Buffer.from(
"64203d5b24688e2547bba345fa139a5a1d962220d4d48a0cf3b1572c0d95b61643a6f9a0d75af7eacc1bd957147bf723", "hex"),
// a pubkey encoded with curve number and length
sampleEphemPublicKey = Buffer.from(
"02ca00200293213dcf1388b61c2ae5cf80fee6ffffc049a2f9fe7365fe3867813ca812920020df94686c6afb565ac6149b153d61b3b287ee2c7f997c14238796c12b43a3865a",
"hex"),
sampleMAC = Buffer.from(
"f2526d61b4851fb23409863826fd206165edc021368c7946571cead69046e619",
"hex");
it("should encrypt", function() {
return eccrypto.encrypt(publicKeyB, Buffer.from("test"), encOpts)
@ -266,6 +286,18 @@ describe("ECIES", function() {
});
});
// To comply to the Spec
it("should comply to the Spec", function() {
return eccrypto.encrypt(samplePublicKey, sampleData, {
iv: sampleIV, ephemPrivateKey: samplePrivateKey
}).then(
function(enc) {
expect(bufferEqual(enc.iv, sampleIV)).to.be.true;
expect(bufferEqual(enc.ciphertext, sampleCiphertext)).to.be.true;
expect(bufferEqual(enc.ephemPublicKey, sampleEphemPublicKey)).to.be.true;
expect(bufferEqual(enc.mac, sampleMAC)).to.be.true;
});
});
it("should reject promise on bad private key when decrypting", function(done) {
eccrypto.encrypt(publicKeyA, Buffer.from("test")).then(function(enc) {