GeezerVison is showing github.com — lib: prefer TypedArrayPrototype primordials · nodejs/node@39befdd
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/argon2.js +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 4 | 4 | FunctionPrototypeCall, | 5 | 5 | MathPow, | 6 | 6 | StringPrototypeToLowerCase, | | 7 | + TypedArrayPrototypeGetBuffer, | 7 | 8 | Uint8Array, | 8 | 9 | } = primordials; | 9 | 10 |
| @@ -231,7 +232,7 @@ async function argon2DeriveBits(algorithm, baseKey, length) { | 231 | 232 | { name: 'OperationError', cause: err }); | 232 | 233 | } | 233 | 234 |
| 234 | | - return result.buffer; | | 235 | + return TypedArrayPrototypeGetBuffer(result); | 235 | 236 | } | 236 | 237 |
| 237 | 238 | module.exports = { | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/crypto/diffiehellman.js +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 6 | 6 | MathCeil, | 7 | 7 | ObjectDefineProperty, | 8 | 8 | SafeSet, | | 9 | + TypedArrayPrototypeGetBuffer, | 9 | 10 | Uint8Array, | 10 | 11 | } = primordials; | 11 | 12 |
| @@ -374,7 +375,7 @@ async function ecdhDeriveBits(algorithm, baseKey, length) { | 374 | 375 |
| 375 | 376 | const masked = new Uint8Array(slice); | 376 | 377 | masked[sliceLength - 1] = masked[sliceLength - 1] & masks[mod]; | 377 | | - return masked.buffer; | | 378 | + return TypedArrayPrototypeGetBuffer(masked); | 378 | 379 | } | 379 | 380 |
| 380 | 381 | module.exports = { | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/crypto/ml_dsa.js +16-19Lines changed: 16 additions & 19 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 2 | 2 |
| 3 | 3 | const { | 4 | 4 | SafeSet, | | 5 | + TypedArrayPrototypeGetBuffer, | | 6 | + TypedArrayPrototypeSet, | 5 | 7 | Uint8Array, | 6 | 8 | } = primordials; | 7 | 9 |
| @@ -119,35 +121,30 @@ function mlDsaExportKey(key, format) { | 119 | 121 | switch (format) { | 120 | 122 | case kWebCryptoKeyFormatRaw: { | 121 | 123 | if (key[kKeyType] === 'private') { | 122 | | - return key[kKeyObject][kHandle].rawSeed().buffer; | | 124 | + return TypedArrayPrototypeGetBuffer(key[kKeyObject][kHandle].rawSeed()); | 123 | 125 | } | 124 | 126 |
| 125 | | - return key[kKeyObject][kHandle].rawPublicKey().buffer; | | 127 | + return TypedArrayPrototypeGetBuffer(key[kKeyObject][kHandle].rawPublicKey()); | 126 | 128 | } | 127 | 129 | case kWebCryptoKeyFormatSPKI: { | 128 | | - return key[kKeyObject][kHandle].export(kKeyFormatDER, kWebCryptoKeyFormatSPKI).buffer; | | 130 | + return TypedArrayPrototypeGetBuffer(key[kKeyObject][kHandle].export(kKeyFormatDER, kWebCryptoKeyFormatSPKI)); | 129 | 131 | } | 130 | 132 | case kWebCryptoKeyFormatPKCS8: { | 131 | 133 | const seed = key[kKeyObject][kHandle].rawSeed(); | 132 | 134 | const buffer = new Uint8Array(54); | 133 | | - buffer.set([ | 134 | | - 0x30, 0x34, 0x02, 0x01, 0x00, 0x30, 0x0B, 0x06, | | 135 | + const orc = { | | 136 | + '__proto__': null, | | 137 | + 'ML-DSA-44': 0x11, | | 138 | + 'ML-DSA-65': 0x12, | | 139 | + 'ML-DSA-87': 0x13, | | 140 | + }[key[kAlgorithm].name]; | | 141 | + TypedArrayPrototypeSet(buffer, [ | | 142 | + 0x30, 0x34, 0x02, 0x01, 0x00, 0x30, 0x0b, 0x06, | 135 | 143 | 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, | 136 | | - 0x03, 0x00, 0x04, 0x22, 0x80, 0x20, | | 144 | + 0x03, orc, 0x04, 0x22, 0x80, 0x20, | 137 | 145 | ], 0); | 138 | | - switch (key[kAlgorithm].name) { | 139 | | - case 'ML-DSA-44': | 140 | | - buffer.set([0x11], 17); | 141 | | - break; | 142 | | - case 'ML-DSA-65': | 143 | | - buffer.set([0x12], 17); | 144 | | - break; | 145 | | - case 'ML-DSA-87': | 146 | | - buffer.set([0x13], 17); | 147 | | - break; | 148 | | - } | 149 | | - buffer.set(seed, 22); | 150 | | - return buffer.buffer; | | 146 | + TypedArrayPrototypeSet(buffer, seed, 22); | | 147 | + return TypedArrayPrototypeGetBuffer(buffer); | 151 | 148 | } | 152 | 149 | default: | 153 | 150 | return undefined; | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/crypto/ml_kem.js +22-21Lines changed: 22 additions & 21 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 3 | 3 | const { | 4 | 4 | PromiseWithResolvers, | 5 | 5 | SafeSet, | | 6 | + TypedArrayPrototypeGetBuffer, | | 7 | + TypedArrayPrototypeSet, | 6 | 8 | Uint8Array, | 7 | 9 | } = primordials; | 8 | 10 |
| @@ -90,35 +92,30 @@ function mlKemExportKey(key, format) { | 90 | 92 | switch (format) { | 91 | 93 | case kWebCryptoKeyFormatRaw: { | 92 | 94 | if (key[kKeyType] === 'private') { | 93 | | - return key[kKeyObject][kHandle].rawSeed().buffer; | | 95 | + return TypedArrayPrototypeGetBuffer(key[kKeyObject][kHandle].rawSeed()); | 94 | 96 | } | 95 | 97 |
| 96 | | - return key[kKeyObject][kHandle].rawPublicKey().buffer; | | 98 | + return TypedArrayPrototypeGetBuffer(key[kKeyObject][kHandle].rawPublicKey()); | 97 | 99 | } | 98 | 100 | case kWebCryptoKeyFormatSPKI: { | 99 | | - return key[kKeyObject][kHandle].export(kKeyFormatDER, kWebCryptoKeyFormatSPKI).buffer; | | 101 | + return TypedArrayPrototypeGetBuffer(key[kKeyObject][kHandle].export(kKeyFormatDER, kWebCryptoKeyFormatSPKI)); | 100 | 102 | } | 101 | 103 | case kWebCryptoKeyFormatPKCS8: { | 102 | 104 | const seed = key[kKeyObject][kHandle].rawSeed(); | 103 | 105 | const buffer = new Uint8Array(86); | 104 | | - buffer.set([ | 105 | | - 0x30, 0x54, 0x02, 0x01, 0x00, 0x30, 0x0B, 0x06, | | 106 | + const orc = { | | 107 | + '__proto__': null, | | 108 | + 'ML-KEM-512': 0x01, | | 109 | + 'ML-KEM-768': 0x02, | | 110 | + 'ML-KEM-1024': 0x03, | | 111 | + }[key[kAlgorithm].name]; | | 112 | + TypedArrayPrototypeSet(buffer, [ | | 113 | + 0x30, 0x54, 0x02, 0x01, 0x00, 0x30, 0x0b, 0x06, | 106 | 114 | 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, | 107 | | - 0x04, 0x00, 0x04, 0x42, 0x80, 0x40, | | 115 | + 0x04, orc, 0x04, 0x42, 0x80, 0x40, | 108 | 116 | ], 0); | 109 | | - switch (key[kAlgorithm].name) { | 110 | | - case 'ML-KEM-512': | 111 | | - buffer.set([0x01], 17); | 112 | | - break; | 113 | | - case 'ML-KEM-768': | 114 | | - buffer.set([0x02], 17); | 115 | | - break; | 116 | | - case 'ML-KEM-1024': | 117 | | - buffer.set([0x03], 17); | 118 | | - break; | 119 | | - } | 120 | | - buffer.set(seed, 22); | 121 | | - return buffer.buffer; | | 117 | + TypedArrayPrototypeSet(buffer, seed, 22); | | 118 | + return TypedArrayPrototypeGetBuffer(buffer); | 122 | 119 | } | 123 | 120 | default: | 124 | 121 | return undefined; | @@ -241,7 +238,11 @@ function mlKemEncapsulate(encapsulationKey) { | 241 | 238 | { name: 'OperationError', cause: error })); | 242 | 239 | } else { | 243 | 240 | const { 0: sharedKey, 1: ciphertext } = result; | 244 | | - resolve({ sharedKey: sharedKey.buffer, ciphertext: ciphertext.buffer }); | | 241 | + | | 242 | + resolve({ | | 243 | + sharedKey: TypedArrayPrototypeGetBuffer(sharedKey), | | 244 | + ciphertext: TypedArrayPrototypeGetBuffer(ciphertext), | | 245 | + }); | 245 | 246 | } | 246 | 247 | }; | 247 | 248 | job.run(); | @@ -270,7 +271,7 @@ function mlKemDecapsulate(decapsulationKey, ciphertext) { | 270 | 271 | 'The operation failed for an operation-specific reason', | 271 | 272 | { name: 'OperationError', cause: error })); | 272 | 273 | } else { | 273 | | - resolve(result.buffer); | | 274 | + resolve(TypedArrayPrototypeGetBuffer(result)); | 274 | 275 | } | 275 | 276 | }; | 276 | 277 | job.run(); | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/crypto/pbkdf2.js +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 3 | 3 | const { | 4 | 4 | ArrayBuffer, | 5 | 5 | FunctionPrototypeCall, | | 6 | + TypedArrayPrototypeGetBuffer, | 6 | 7 | } = primordials; | 7 | 8 |
| 8 | 9 | const { Buffer } = require('buffer'); | @@ -121,7 +122,7 @@ async function pbkdf2DeriveBits(algorithm, baseKey, length) { | 121 | 122 | { name: 'OperationError', cause: err }); | 122 | 123 | } | 123 | 124 |
| 124 | | - return result.buffer; | | 125 | + return TypedArrayPrototypeGetBuffer(result); | 125 | 126 | } | 126 | 127 |
| 127 | 128 | module.exports = { | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/crypto/random.js +3-2Lines changed: 3 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -19,6 +19,7 @@ const { | 19 | 19 | NumberPrototypeToString, | 20 | 20 | StringFromCharCodeApply, | 21 | 21 | StringPrototypePadStart, | | 22 | + TypedArrayPrototypeGetBuffer, | 22 | 23 | } = primordials; | 23 | 24 |
| 24 | 25 | const { | @@ -104,12 +105,12 @@ function randomBytes(size, callback) { | 104 | 105 | const buf = new FastBuffer(size); | 105 | 106 |
| 106 | 107 | if (callback === undefined) { | 107 | | - randomFillSync(buf.buffer, 0, size); | | 108 | + randomFillSync(TypedArrayPrototypeGetBuffer(buf), 0, size); | 108 | 109 | return buf; | 109 | 110 | } | 110 | 111 |
| 111 | 112 | // Keep the callback as a regular function so this is propagated. | 112 | | - randomFill(buf.buffer, 0, size, function(error) { | | 113 | + randomFill(TypedArrayPrototypeGetBuffer(buf), 0, size, function(error) { | 113 | 114 | if (error) return FunctionPrototypeCall(callback, this, error); | 114 | 115 | FunctionPrototypeCall(callback, this, null, buf); | 115 | 116 | }); | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/crypto/webcrypto.js +3-2Lines changed: 3 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -11,6 +11,7 @@ const { | 11 | 11 | StringPrototypeSlice, | 12 | 12 | StringPrototypeStartsWith, | 13 | 13 | SymbolToStringTag, | | 14 | + TypedArrayPrototypeGetBuffer, | 14 | 15 | } = primordials; | 15 | 16 |
| 16 | 17 | const { | @@ -544,7 +545,7 @@ async function exportKeyRawSecret(key, format) { | 544 | 545 | case 'AES-KW': | 545 | 546 | // Fall through | 546 | 547 | case 'HMAC': | 547 | | - return key[kKeyObject][kHandle].export().buffer; | | 548 | + return TypedArrayPrototypeGetBuffer(key[kKeyObject][kHandle].export()); | 548 | 549 | case 'AES-OCB': | 549 | 550 | // Fall through | 550 | 551 | case 'KMAC128': | @@ -553,7 +554,7 @@ async function exportKeyRawSecret(key, format) { | 553 | 554 | // Fall through | 554 | 555 | case 'ChaCha20-Poly1305': | 555 | 556 | if (format === 'raw-secret') { | 556 | | - return key[kKeyObject][kHandle].export().buffer; | | 557 | + return TypedArrayPrototypeGetBuffer(key[kKeyObject][kHandle].export()); | 557 | 558 | } | 558 | 559 | return undefined; | 559 | 560 | default: | |
|