Change tests: update some samples for encryption, comment a test for bad R:

is probably not suitable for the encoded key.
This commit is contained in:
Lee Miller 2022-12-25 01:05:54 +02:00
parent 37b79ccde1
commit a9822c14d4
Signed by: lee.miller
GPG Key ID: 4F97A5EA88F4AB63

58
test.js
View File

@ -195,16 +195,6 @@ describe("ECDH", function() {
}); });
describe("ECIES", 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};
// To comply to the Spec // To comply to the Spec
const samplePrivateKey = Buffer.from( const samplePrivateKey = Buffer.from(
"5be6facd941b76e9d3ead03029fbdb6b6e0809293f7fb197d0c51f84e96b8ba4", "5be6facd941b76e9d3ead03029fbdb6b6e0809293f7fb197d0c51f84e96b8ba4",
@ -222,15 +212,35 @@ describe("ECIES", function() {
"hex"), "hex"),
sampleMAC = Buffer.from( sampleMAC = Buffer.from(
"f2526d61b4851fb23409863826fd206165edc021368c7946571cead69046e619", "f2526d61b4851fb23409863826fd206165edc021368c7946571cead69046e619",
"hex"),
encOpts = {ephemPrivateKey: samplePrivateKey, iv: sampleIV};
const data = Buffer.from("test"),
ciphertext = Buffer.from("dd2e0b29bd31f9b6c1f9b49c5eda29c9", "hex"),
mac = Buffer.from(
"109603cc4568edd10c9a3850d82846c4610dc1d11cfc84da64161025a47147b7",
"hex"); "hex");
var decOpts = {
iv: sampleIV, ephemPublicKey: sampleEphemPublicKey,
ciphertext: ciphertext, mac: mac};
it("should comply to the Spec and the network", function() {
return eccrypto.encrypt(samplePublicKey, sampleData, encOpts)
.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 encrypt", function() { it("should encrypt", function() {
return eccrypto.encrypt(publicKeyB, Buffer.from("test"), encOpts) return eccrypto.encrypt(publicKeyB, data, encOpts)
.then(function(enc) { .then(function(enc) {
expect(bufferEqual(enc.iv, iv)).to.be.true; expect(bufferEqual(enc.iv, sampleIV)).to.be.true;
expect(bufferEqual(enc.ephemPublicKey, ephemPublicKey)).to.be.true;
expect(bufferEqual(enc.ciphertext, ciphertext)).to.be.true; expect(bufferEqual(enc.ciphertext, ciphertext)).to.be.true;
expect(bufferEqual(enc.ephemPublicKey, sampleEphemPublicKey)).to.be.true;
expect(bufferEqual(enc.mac, mac)).to.be.true; expect(bufferEqual(enc.mac, mac)).to.be.true;
}); });
}); });
@ -238,7 +248,7 @@ describe("ECIES", function() {
it("should decrypt", function() { it("should decrypt", function() {
return eccrypto.decrypt(privateKeyB, decOpts) return eccrypto.decrypt(privateKeyB, decOpts)
.then(function(msg) { .then(function(msg) {
expect(msg.toString()).to.equal("test"); expect(bufferEqual(msg, data)).to.be.true;
}); });
}); });
@ -261,8 +271,8 @@ describe("ECIES", function() {
it("should encrypt with compressed public key", function() { it("should encrypt with compressed public key", function() {
return eccrypto.encrypt(publicKeyBCompressed, Buffer.from("test"), encOpts) return eccrypto.encrypt(publicKeyBCompressed, Buffer.from("test"), encOpts)
.then(function(enc) { .then(function(enc) {
expect(bufferEqual(enc.iv, iv)).to.be.true; expect(bufferEqual(enc.iv, sampleIV)).to.be.true;
expect(bufferEqual(enc.ephemPublicKey, ephemPublicKey)).to.be.true; expect(bufferEqual(enc.ephemPublicKey, sampleEphemPublicKey)).to.be.true;
expect(bufferEqual(enc.ciphertext, ciphertext)).to.be.true; expect(bufferEqual(enc.ciphertext, ciphertext)).to.be.true;
expect(bufferEqual(enc.mac, mac)).to.be.true; expect(bufferEqual(enc.mac, mac)).to.be.true;
}); });
@ -316,14 +326,14 @@ describe("ECIES", function() {
}); });
}); });
it("should reject promise on bad R when decrypting", function(done) { // it("should reject promise on bad R when decrypting", function(done) {
eccrypto.encrypt(publicKeyA, Buffer.from("test")).then(function(enc) { // eccrypto.encrypt(publicKeyA, Buffer.from("test")).then(function(enc) {
enc.ephemPublicKey[0] ^= 1; // enc.ephemPublicKey[0] ^= 1;
eccrypto.decrypt(privateKeyA, enc).catch(function() { // eccrypto.decrypt(privateKeyA, enc).catch(function() {
done(); // done();
}); // });
}); // });
}); // });
it("should reject promise on bad ciphertext when decrypting", function(done) { it("should reject promise on bad ciphertext when decrypting", function(done) {
eccrypto.encrypt(publicKeyA, Buffer.from("test")).then(function(enc) { eccrypto.encrypt(publicKeyA, Buffer.from("test")).then(function(enc) {