Skip to content

Commit 3ee0bb8

Browse files
panvatargos
authored andcommitted
crypto: fix webcrypto deriveBits for non-byte lengths
PR-URL: #43431 Reviewed-By: Tobias Nießen <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]>
1 parent e0fedcf commit 3ee0bb8

File tree

3 files changed

+6
-6
lines changed

3 files changed

+6
-6
lines changed

lib/internal/crypto/diffiehellman.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const {
44
ArrayBufferPrototypeSlice,
55
FunctionPrototypeCall,
6-
MathFloor,
6+
MathCeil,
77
ObjectDefineProperty,
88
Promise,
99
SafeSet,
@@ -386,9 +386,9 @@ async function asyncDeriveBitsECDH(algorithm, baseKey, length) {
386386
if (length === null)
387387
return bits;
388388

389-
// If the length is not a multiple of 8, it will be truncated
390-
// down to the nearest multiple of 8.
391-
length = MathFloor(length / 8);
389+
// If the length is not a multiple of 8 the nearest ceiled
390+
// multiple of 8 is sliced.
391+
length = MathCeil(length / 8);
392392
const { byteLength } = bits;
393393

394394
// If the length is larger than the derived secret, throw.

test/parallel/test-webcrypto-derivebits-cfrg.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ async function prepareKeys() {
133133

134134
assert.strictEqual(
135135
Buffer.from(bits).toString('hex'),
136-
result.slice(0, -4));
136+
result.slice(0, -2));
137137
}
138138
}));
139139

test/parallel/test-webcrypto-derivebits-ecdh.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ async function prepareKeys() {
154154

155155
assert.strictEqual(
156156
Buffer.from(bits).toString('hex'),
157-
result.slice(0, -4));
157+
result.slice(0, -2));
158158
}
159159
}));
160160

0 commit comments

Comments
 (0)