From 3ee0bb8d03fefface450fa96e5c63c3c31109274 Mon Sep 17 00:00:00 2001 From: Filip Skokan Date: Tue, 14 Jun 2022 15:07:50 +0200 Subject: [PATCH] crypto: fix webcrypto deriveBits for non-byte lengths MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/43431 Reviewed-By: Tobias Nießen Reviewed-By: James M Snell Reviewed-By: Antoine du Hamel --- lib/internal/crypto/diffiehellman.js | 8 ++++---- test/parallel/test-webcrypto-derivebits-cfrg.js | 2 +- test/parallel/test-webcrypto-derivebits-ecdh.js | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/internal/crypto/diffiehellman.js b/lib/internal/crypto/diffiehellman.js index 85f2be16007..036f661fad6 100644 --- a/lib/internal/crypto/diffiehellman.js +++ b/lib/internal/crypto/diffiehellman.js @@ -3,7 +3,7 @@ const { ArrayBufferPrototypeSlice, FunctionPrototypeCall, - MathFloor, + MathCeil, ObjectDefineProperty, Promise, SafeSet, @@ -386,9 +386,9 @@ async function asyncDeriveBitsECDH(algorithm, baseKey, length) { if (length === null) return bits; - // If the length is not a multiple of 8, it will be truncated - // down to the nearest multiple of 8. - length = MathFloor(length / 8); + // If the length is not a multiple of 8 the nearest ceiled + // multiple of 8 is sliced. + length = MathCeil(length / 8); const { byteLength } = bits; // If the length is larger than the derived secret, throw. diff --git a/test/parallel/test-webcrypto-derivebits-cfrg.js b/test/parallel/test-webcrypto-derivebits-cfrg.js index ff3e4ef7de5..2233d1a2d27 100644 --- a/test/parallel/test-webcrypto-derivebits-cfrg.js +++ b/test/parallel/test-webcrypto-derivebits-cfrg.js @@ -133,7 +133,7 @@ async function prepareKeys() { assert.strictEqual( Buffer.from(bits).toString('hex'), - result.slice(0, -4)); + result.slice(0, -2)); } })); diff --git a/test/parallel/test-webcrypto-derivebits-ecdh.js b/test/parallel/test-webcrypto-derivebits-ecdh.js index de19ac1cd4e..826b591d37c 100644 --- a/test/parallel/test-webcrypto-derivebits-ecdh.js +++ b/test/parallel/test-webcrypto-derivebits-ecdh.js @@ -154,7 +154,7 @@ async function prepareKeys() { assert.strictEqual( Buffer.from(bits).toString('hex'), - result.slice(0, -4)); + result.slice(0, -2)); } }));