diff --git a/lib/crypto.js b/lib/crypto.js index 95daf2f..1235ba6 100644 --- a/lib/crypto.js +++ b/lib/crypto.js @@ -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]); }, };