| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -596,9 +596,6 @@ Using the method and parameters specified in `algorithm` and the keying | |||
| 596 | 596 | material provided by `baseKey`, `subtle.deriveBits()` attempts to generate | |
| 597 | 597 | `length` bits. | |
| 598 | 598 | ||
| 599 | - The Node.js implementation requires that `length`, when a number, is a multiple | ||
| 600 | - of `8`. | ||
| 601 | - | ||
| 602 | 599 | When `length` is not provided or `null` the maximum number of bits for a given | |
| 603 | 600 | algorithm is generated. This is allowed for the `'ECDH'`, `'X25519'`, and `'X448'` | |
| 604 | 601 | algorithms, for other algorithms `length` is required to be a number. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -5,6 +5,7 @@ const { | |||
| 5 | 5 | MathCeil, | |
| 6 | 6 | ObjectDefineProperty, | |
| 7 | 7 | SafeSet, | |
| 8 | + Uint8Array, | ||
| 8 | 9 | } = primordials; | |
| 9 | 10 | ||
| 10 | 11 | const { Buffer } = require('buffer'); | |
@@ -295,6 +296,8 @@ function diffieHellman(options) { | |||
| 295 | 296 | return statelessDH(privateKey[kHandle], publicKey[kHandle]); | |
| 296 | 297 | } | |
| 297 | 298 | ||
| 299 | + let masks; | ||
| 300 | + | ||
| 298 | 301 | // The ecdhDeriveBits function is part of the Web Crypto API and serves both | |
| 299 | 302 | // deriveKeys and deriveBits functions. | |
| 300 | 303 | async function ecdhDeriveBits(algorithm, baseKey, length) { | |
@@ -342,18 +345,25 @@ async function ecdhDeriveBits(algorithm, baseKey, length) { | |||
| 342 | 345 | ||
| 343 | 346 | // If the length is not a multiple of 8 the nearest ceiled | |
| 344 | 347 | // multiple of 8 is sliced. | |
| 345 | - length = MathCeil(length / 8); | ||
| 346 | - const { byteLength } = bits; | ||
| 348 | + const sliceLength = MathCeil(length / 8); | ||
| 347 | 349 | ||
| 350 | + const { byteLength } = bits; | ||
| 348 | 351 | // If the length is larger than the derived secret, throw. | |
| 349 | - // Otherwise, we either return the secret or a truncated | ||
| 350 | - // slice. | ||
| 351 | - if (byteLength < length) | ||
| 352 | + if (byteLength < sliceLength) | ||
| 352 | 353 | throw lazyDOMException('derived bit length is too small', 'OperationError'); | |
| 353 | 354 | ||
| 354 | - return length === byteLength ? | ||
| 355 | - bits : | ||
| 356 | - ArrayBufferPrototypeSlice(bits, 0, length); | ||
| 355 | + const slice = ArrayBufferPrototypeSlice(bits, 0, sliceLength); | ||
| 356 | + | ||
| 357 | + const mod = length % 8; | ||
| 358 | + if (mod === 0) | ||
| 359 | + return slice; | ||
| 360 | + | ||
| 361 | + // eslint-disable-next-line no-sparse-arrays | ||
| 362 | + masks ||= [, 0b10000000, 0b11000000, 0b11100000, 0b11110000, 0b11111000, 0b11111100, 0b11111110]; | ||
| 363 | + | ||
| 364 | + const masked = new Uint8Array(slice); | ||
| 365 | + masked[sliceLength - 1] = masked[sliceLength - 1] & masks[mod]; | ||
| 366 | + return masked.buffer; | ||
| 357 | 367 | } | |
| 358 | 368 | ||
| 359 | 369 | module.exports = { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -140,9 +140,11 @@ async function prepareKeys() { | |||
| 140 | 140 | public: publicKey | |
| 141 | 141 | }, privateKey, 8 * size - 11); | |
| 142 | 142 | ||
| 143 | - assert.strictEqual( | ||
| 144 | - Buffer.from(bits).toString('hex'), | ||
| 145 | - result.slice(0, -2)); | ||
| 143 | + const expected = Buffer.from(result.slice(0, -2), 'hex'); | ||
| 144 | + expected[size - 2] = expected[size - 2] & 0b11111000; | ||
| 145 | + assert.deepStrictEqual( | ||
| 146 | + Buffer.from(bits), | ||
| 147 | + expected); | ||
| 146 | 148 | } | |
| 147 | 149 | })); | |
| 148 | 150 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -161,9 +161,11 @@ async function prepareKeys() { | |||
| 161 | 161 | public: publicKey | |
| 162 | 162 | }, privateKey, 8 * size - 11); | |
| 163 | 163 | ||
| 164 | - assert.strictEqual( | ||
| 165 | - Buffer.from(bits).toString('hex'), | ||
| 166 | - result.slice(0, -2)); | ||
| 164 | + const expected = Buffer.from(result.slice(0, -2), 'hex'); | ||
| 165 | + expected[size - 2] = expected[size - 2] & 0b11111000; | ||
| 166 | + assert.deepStrictEqual( | ||
| 167 | + Buffer.from(bits), | ||
| 168 | + expected); | ||
| 167 | 169 | } | |
| 168 | 170 | })); | |
| 169 | 171 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments