GeezerVison is showing github.com — crypto: handle PBKDF2 iteration limits · nodejs/node@5e05532
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/internal/crypto/pbkdf2.js +10-3Lines changed: 10 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -16,6 +16,7 @@ const { | 16 | 16 | } = internalBinding('crypto'); | 17 | 17 |
| 18 | 18 | const { | | 19 | + isInt32, | 19 | 20 | validateFunction, | 20 | 21 | validateInt32, | 21 | 22 | validateString, | @@ -98,7 +99,7 @@ function check(password, salt, iterations, keylen, digest) { | 98 | 99 | return { password, salt, iterations, keylen, digest }; | 99 | 100 | } | 100 | 101 |
| 101 | | -function validatePbkdf2DeriveBitsLength(length) { | | 102 | +function validatePbkdf2DeriveBits({ iterations }, length) { | 102 | 103 | if (length === null) | 103 | 104 | throw lazyDOMException('length cannot be null', 'OperationError'); | 104 | 105 |
| @@ -107,10 +108,16 @@ function validatePbkdf2DeriveBitsLength(length) { | 107 | 108 | 'length must be a multiple of 8', | 108 | 109 | 'OperationError'); | 109 | 110 | } | | 111 | + if (iterations === 0) | | 112 | + throw lazyDOMException('iterations cannot be zero', 'OperationError'); | | 113 | + if (length !== 0 && !isInt32(iterations)) { | | 114 | + throw lazyDOMException( | | 115 | + 'iterations exceeds the implementation limit', 'OperationError'); | | 116 | + } | 110 | 117 | } | 111 | 118 |
| 112 | 119 | function pbkdf2DeriveBits(algorithm, baseKey, length) { | 113 | | - validatePbkdf2DeriveBitsLength(length); | | 120 | + validatePbkdf2DeriveBits(algorithm, length); | 114 | 121 | const { iterations, hash, salt } = algorithm; | 115 | 122 |
| 116 | 123 | if (length === 0) | @@ -129,5 +136,5 @@ module.exports = { | 129 | 136 | pbkdf2, | 130 | 137 | pbkdf2Sync, | 131 | 138 | pbkdf2DeriveBits, | 132 | | - validatePbkdf2DeriveBitsLength, | | 139 | + validatePbkdf2DeriveBits, | 133 | 140 | }; |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/crypto/webcrypto.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -1937,7 +1937,7 @@ function check(op, alg, length) { | 1937 | 1937 | } | 1938 | 1938 |
| 1939 | 1939 | if (normalizedAlgorithm.name === 'PBKDF2') { | 1940 | | - require('internal/crypto/pbkdf2').validatePbkdf2DeriveBitsLength(length); | | 1940 | + require('internal/crypto/pbkdf2').validatePbkdf2DeriveBits(normalizedAlgorithm, length); | 1941 | 1941 | } | 1942 | 1942 |
| 1943 | 1943 | if (StringPrototypeStartsWith(normalizedAlgorithm.name, 'Argon2')) { | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/crypto/webidl.js -10Lines changed: 0 additions & 10 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -15,7 +15,6 @@ const { | 15 | 15 | kEmptyObject, | 16 | 16 | } = require('internal/util'); | 17 | 17 | const { | 18 | | - isInt32, | 19 | 18 | isUint32, | 20 | 19 | } = require('internal/validators'); | 21 | 20 | const { | @@ -492,15 +491,6 @@ converters.Pbkdf2Params = createDictionaryConverter( | 492 | 491 | key: 'iterations', | 493 | 492 | converter: (V, opts) => | 494 | 493 | converters['unsigned long'](V, enforceRangeOptions(opts)), | 495 | | - validator: (V, dict) => { | 496 | | - if (V === 0) | 497 | | - throw lazyDOMException('iterations cannot be zero', 'OperationError'); | 498 | | - if (!isInt32(V)) { | 499 | | - throw lazyDOMException( | 500 | | - 'iterations exceeds the implementation limit', | 501 | | - 'NotSupportedError'); | 502 | | - } | 503 | | - }, | 504 | 494 | required: true, | 505 | 495 | }, | 506 | 496 | { | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-webcrypto-derivebits.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -136,7 +136,7 @@ const rejectsXCurves = hasFIPS(3, 5); | 136 | 136 | salt: new Uint8Array([2]), | 137 | 137 | iterations: 2 ** 31, | 138 | 138 | }, key, 8), | 139 | | - { name: 'NotSupportedError' }); | | 139 | + { name: 'OperationError' }); | 140 | 140 | } | 141 | 141 |
| 142 | 142 | test().then(common.mustCall()); | |
Collapse file Copy file name to clipboard +21Lines changed: 21 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +'use strict'; | | 2 | + | | 3 | +const common = require('../common'); | | 4 | +if (!common.hasCrypto) | | 5 | + common.skip('missing crypto'); | | 6 | + | | 7 | +const assert = require('assert'); | | 8 | +const { subtle } = globalThis.crypto; | | 9 | + | | 10 | +(async () => { | | 11 | + const key = await subtle.importKey('raw', new Uint8Array(16), 'PBKDF2', false, ['deriveBits']); | | 12 | + for (const iterations of [2 ** 31, 2 ** 32 - 1]) { | | 13 | + const algorithm = { name: 'PBKDF2', hash: 'SHA-256', salt: new Uint8Array(16), iterations }; | | 14 | + assert.strictEqual((await subtle.deriveBits(algorithm, key, 0)).byteLength, 0); | | 15 | + assert.strictEqual(SubtleCrypto.supports('deriveBits', algorithm, 0), true); | | 16 | + assert.strictEqual(SubtleCrypto.supports('deriveBits', algorithm, 8), false); | | 17 | + await assert.rejects(subtle.deriveBits(algorithm, key, 8), { name: 'OperationError' }); | | 18 | + } | | 19 | + const algorithm = { name: 'PBKDF2', hash: 'SHA-256', salt: new Uint8Array(16), iterations: 0 }; | | 20 | + await assert.rejects(subtle.deriveBits(algorithm, key, 0), { name: 'OperationError' }); | | 21 | +})().then(common.mustCall()); |
|