Fix encrypted encoding/decoding according to the changes in eccrypto

This commit is contained in:
Lee Miller 2023-01-03 05:02:21 +02:00
parent 399e227815
commit ce6623d089
Signed by untrusted user: lee.miller
GPG Key ID: 4F97A5EA88F4AB63

View File

@ -106,10 +106,8 @@ var encrypted = exports.encrypted = {
assert(buf.readUInt16BE(52, true) === 32, "Bad Ry length"); assert(buf.readUInt16BE(52, true) === 32, "Bad Ry length");
var iv = new Buffer(16); var iv = new Buffer(16);
buf.copy(iv, 0, 0, 16); buf.copy(iv, 0, 0, 16);
var ephemPublicKey = new Buffer(65); var ephemPublicKey = new Buffer(70);
ephemPublicKey[0] = 0x04; buf.copy(ephemPublicKey, 0, 16, 86);
buf.copy(ephemPublicKey, 1, 20, 52);
buf.copy(ephemPublicKey, 33, 54, 86);
// NOTE(Kagami): We do copy instead of slice to protect against // NOTE(Kagami): We do copy instead of slice to protect against
// possible source buffer modification by user. // possible source buffer modification by user.
var ciphertext = new Buffer(buf.length - 118); var ciphertext = new Buffer(buf.length - 118);
@ -126,19 +124,13 @@ var encrypted = exports.encrypted = {
encode: function(opts) { encode: function(opts) {
assert(opts.iv.length === 16, "Bad IV"); assert(opts.iv.length === 16, "Bad IV");
assert(opts.ephemPublicKey.length === 65, "Bad public key"); assert(opts.ephemPublicKey.length === 70, "Bad public key");
assert(
opts.ephemPublicKey.readUInt16BE(0, true) === SECP256K1_TYPE,
"Bad curve type");
assert(opts.mac.length === 32, "Bad MAC"); assert(opts.mac.length === 32, "Bad MAC");
// 16 + 2 + 2 + 32 + 2 + 32 + ? + 32 return Buffer.concat(
var buf = new Buffer(118 + opts.ciphertext.length); [opts.iv, opts.ephemPublicKey, opts.ciphertext, opts.mac])
opts.iv.copy(buf);
buf.writeUInt16BE(SECP256K1_TYPE, 16, true); // Curve type
buf.writeUInt16BE(32, 18, true); // Rx length
opts.ephemPublicKey.copy(buf, 20, 1, 33); // Rx
buf.writeUInt16BE(32, 52, true); // Ry length
opts.ephemPublicKey.copy(buf, 54, 33); // Ry
opts.ciphertext.copy(buf, 86);
opts.mac.copy(buf, 86 + opts.ciphertext.length);
return buf;
}, },
}; };