GeezerVison is showing github.com — crypto: fix webcrypto HMAC "get key length" in deriveKey and generateKey · nodejs/node@57f507f
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/mac.js +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -14,7 +14,7 @@ const { | 14 | 14 | } = internalBinding('crypto'); | 15 | 15 |
| 16 | 16 | const { | 17 | | - getHashLength, | | 17 | + getBlockSize, | 18 | 18 | hasAnyNotIn, | 19 | 19 | jobPromise, | 20 | 20 | normalizeHashName, | @@ -54,7 +54,7 @@ async function hmacGenerateKey(algorithm, extractable, keyUsages) { | 54 | 54 | throw new ERR_MISSING_OPTION('algorithm.hash'); | 55 | 55 |
| 56 | 56 | if (length === undefined) | 57 | | - length = getHashLength(hash.name); | | 57 | + length = getBlockSize(hash.name); | 58 | 58 |
| 59 | 59 | validateBitLength(length, 'algorithm.length', true); | 60 | 60 |
| |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/crypto/util.js +6-6Lines changed: 6 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -340,12 +340,12 @@ function getUsagesUnion(usageSet, ...usages) { | 340 | 340 | return newset; | 341 | 341 | } | 342 | 342 |
| 343 | | -function getHashLength(name) { | | 343 | +function getBlockSize(name) { | 344 | 344 | switch (name) { | 345 | | - case 'SHA-1': return 160; | 346 | | - case 'SHA-256': return 256; | 347 | | - case 'SHA-384': return 384; | 348 | | - case 'SHA-512': return 512; | | 345 | + case 'SHA-1': return 512; | | 346 | + case 'SHA-256': return 512; | | 347 | + case 'SHA-384': return 1024; | | 348 | + case 'SHA-512': return 1024; | 349 | 349 | } | 350 | 350 | } | 351 | 351 |
| @@ -430,8 +430,8 @@ module.exports = { | 430 | 430 | validateMaxBufferLength, | 431 | 431 | bigIntArrayToUnsignedBigInt, | 432 | 432 | bigIntArrayToUnsignedInt, | | 433 | + getBlockSize, | 433 | 434 | getStringOption, | 434 | 435 | getUsagesUnion, | 435 | | - getHashLength, | 436 | 436 | secureHeapUsed, | 437 | 437 | }; |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/crypto/webcrypto.js +2-10Lines changed: 2 additions & 10 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -51,6 +51,7 @@ const { | 51 | 51 |
| 52 | 52 | const { | 53 | 53 | getArrayBufferOrView, | | 54 | + getBlockSize, | 54 | 55 | hasAnyNotIn, | 55 | 56 | lazyRequire, | 56 | 57 | normalizeAlgorithm, | @@ -195,16 +196,7 @@ function getKeyLength({ name, length, hash }) { | 195 | 196 | return length; | 196 | 197 | case 'HMAC': | 197 | 198 | if (length === undefined) { | 198 | | - switch (hash?.name) { | 199 | | - case 'SHA-1': | 200 | | - return 160; | 201 | | - case 'SHA-256': | 202 | | - return 256; | 203 | | - case 'SHA-384': | 204 | | - return 384; | 205 | | - case 'SHA-512': | 206 | | - return 512; | 207 | | - } | | 199 | + return getBlockSize(hash?.name); | 208 | 200 | } | 209 | 201 |
| 210 | 202 | if (typeof length === 'number' && length !== 0) { | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-webcrypto-derivekey.js +27-4Lines changed: 27 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -124,10 +124,11 @@ const { webcrypto: { subtle }, KeyObject } = require('crypto'); | 124 | 124 | const vectors = [ | 125 | 125 | ['PBKDF2', 'deriveKey', 528], | 126 | 126 | ['HKDF', 'deriveKey', 528], | 127 | | - [{ name: 'HMAC', hash: 'SHA-1' }, 'sign', 160], | 128 | | - [{ name: 'HMAC', hash: 'SHA-256' }, 'sign', 256], | 129 | | - [{ name: 'HMAC', hash: 'SHA-384' }, 'sign', 384], | 130 | | - [{ name: 'HMAC', hash: 'SHA-512' }, 'sign', 512], | | 127 | + [{ name: 'HMAC', hash: 'SHA-1' }, 'sign', 512], | | 128 | + [{ name: 'HMAC', hash: 'SHA-256' }, 'sign', 512], | | 129 | + // Not long enough secret generated by ECDH | | 130 | + // [{ name: 'HMAC', hash: 'SHA-384' }, 'sign', 1024], | | 131 | + // [{ name: 'HMAC', hash: 'SHA-512' }, 'sign', 1024], | 131 | 132 | ]; | 132 | 133 |
| 133 | 134 | (async () => { | @@ -151,6 +152,28 @@ const { webcrypto: { subtle }, KeyObject } = require('crypto'); | 151 | 152 | })().then(common.mustCall()); | 152 | 153 | } | 153 | 154 |
| | 155 | +{ | | 156 | + const vectors = [ | | 157 | + [{ name: 'HMAC', hash: 'SHA-1' }, 'sign', 512], | | 158 | + [{ name: 'HMAC', hash: 'SHA-256' }, 'sign', 512], | | 159 | + [{ name: 'HMAC', hash: 'SHA-384' }, 'sign', 1024], | | 160 | + [{ name: 'HMAC', hash: 'SHA-512' }, 'sign', 1024], | | 161 | + ]; | | 162 | + | | 163 | + (async () => { | | 164 | + for (const [derivedKeyAlgorithm, usage, expected] of vectors) { | | 165 | + const derived = await subtle.deriveKey( | | 166 | + { name: 'PBKDF2', salt: new Uint8Array([]), hash: 'SHA-256', iterations: 20 }, | | 167 | + await subtle.importKey('raw', new Uint8Array([]), { name: 'PBKDF2' }, false, ['deriveKey']), | | 168 | + derivedKeyAlgorithm, | | 169 | + false, | | 170 | + [usage]); | | 171 | + | | 172 | + assert.strictEqual(derived.algorithm.length, expected); | | 173 | + } | | 174 | + })().then(common.mustCall()); | | 175 | +} | | 176 | + | 154 | 177 | // Test X25519 and X448 key derivation | 155 | 178 | { | 156 | 179 | async function test(name) { | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-webcrypto-keygen.js +4-4Lines changed: 4 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -551,10 +551,10 @@ const vectors = { | 551 | 551 |
| 552 | 552 | if (length === undefined) { | 553 | 553 | switch (hash) { | 554 | | - case 'SHA-1': length = 160; break; | 555 | | - case 'SHA-256': length = 256; break; | 556 | | - case 'SHA-384': length = 384; break; | 557 | | - case 'SHA-512': length = 512; break; | | 554 | + case 'SHA-1': length = 512; break; | | 555 | + case 'SHA-256': length = 512; break; | | 556 | + case 'SHA-384': length = 1024; break; | | 557 | + case 'SHA-512': length = 1024; break; | 558 | 558 | } | 559 | 559 | } | 560 | 560 |
| |
|