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 1a6cb74b82
Signed by untrusted user: lee.miller
GPG Key ID: 4F97A5EA88F4AB63
1 changed files with 8 additions and 16 deletions

View File

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