Skip to content

Commit

Permalink
crypto: fix webcrypto deriveBits for non-byte lengths
Browse files Browse the repository at this point in the history
PR-URL: #43431
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
  • Loading branch information
panva authored and targos committed Jul 12, 2022
1 parent e0fedcf commit 3ee0bb8
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
8 changes: 4 additions & 4 deletions lib/internal/crypto/diffiehellman.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const {
ArrayBufferPrototypeSlice,
FunctionPrototypeCall,
MathFloor,
MathCeil,
ObjectDefineProperty,
Promise,
SafeSet,
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-webcrypto-derivebits-cfrg.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ async function prepareKeys() {

assert.strictEqual(
Buffer.from(bits).toString('hex'),
result.slice(0, -4));
result.slice(0, -2));
}
}));

Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-webcrypto-derivebits-ecdh.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ async function prepareKeys() {

assert.strictEqual(
Buffer.from(bits).toString('hex'),
result.slice(0, -4));
result.slice(0, -2));
}
}));

Expand Down

0 comments on commit 3ee0bb8

Please sign in to comment.