Skip to content

Commit 39befdd

Browse files
panvatargos
authored andcommitted
lib: prefer TypedArrayPrototype primordials
Refs: #59699 PR-URL: #59766 Refs: #59699 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Jordan Harband <ljharb@gmail.com>
1 parent 6696d1d commit 39befdd

File tree

7 files changed

+50
-47
lines changed

7 files changed

+50
-47
lines changed

lib/internal/crypto/argon2.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const {
44
FunctionPrototypeCall,
55
MathPow,
66
StringPrototypeToLowerCase,
7+
TypedArrayPrototypeGetBuffer,
78
Uint8Array,
89
} = primordials;
910

@@ -231,7 +232,7 @@ async function argon2DeriveBits(algorithm, baseKey, length) {
231232
{ name: 'OperationError', cause: err });
232233
}
233234

234-
return result.buffer;
235+
return TypedArrayPrototypeGetBuffer(result);
235236
}
236237

237238
module.exports = {

lib/internal/crypto/diffiehellman.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const {
66
MathCeil,
77
ObjectDefineProperty,
88
SafeSet,
9+
TypedArrayPrototypeGetBuffer,
910
Uint8Array,
1011
} = primordials;
1112

@@ -374,7 +375,7 @@ async function ecdhDeriveBits(algorithm, baseKey, length) {
374375

375376
const masked = new Uint8Array(slice);
376377
masked[sliceLength - 1] = masked[sliceLength - 1] & masks[mod];
377-
return masked.buffer;
378+
return TypedArrayPrototypeGetBuffer(masked);
378379
}
379380

380381
module.exports = {

lib/internal/crypto/ml_dsa.js

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
const {
44
SafeSet,
5+
TypedArrayPrototypeGetBuffer,
6+
TypedArrayPrototypeSet,
57
Uint8Array,
68
} = primordials;
79

@@ -119,35 +121,30 @@ function mlDsaExportKey(key, format) {
119121
switch (format) {
120122
case kWebCryptoKeyFormatRaw: {
121123
if (key[kKeyType] === 'private') {
122-
return key[kKeyObject][kHandle].rawSeed().buffer;
124+
return TypedArrayPrototypeGetBuffer(key[kKeyObject][kHandle].rawSeed());
123125
}
124126

125-
return key[kKeyObject][kHandle].rawPublicKey().buffer;
127+
return TypedArrayPrototypeGetBuffer(key[kKeyObject][kHandle].rawPublicKey());
126128
}
127129
case kWebCryptoKeyFormatSPKI: {
128-
return key[kKeyObject][kHandle].export(kKeyFormatDER, kWebCryptoKeyFormatSPKI).buffer;
130+
return TypedArrayPrototypeGetBuffer(key[kKeyObject][kHandle].export(kKeyFormatDER, kWebCryptoKeyFormatSPKI));
129131
}
130132
case kWebCryptoKeyFormatPKCS8: {
131133
const seed = key[kKeyObject][kHandle].rawSeed();
132134
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,
135143
0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04,
136-
0x03, 0x00, 0x04, 0x22, 0x80, 0x20,
144+
0x03, orc, 0x04, 0x22, 0x80, 0x20,
137145
], 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);
151148
}
152149
default:
153150
return undefined;

lib/internal/crypto/ml_kem.js

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
const {
44
PromiseWithResolvers,
55
SafeSet,
6+
TypedArrayPrototypeGetBuffer,
7+
TypedArrayPrototypeSet,
68
Uint8Array,
79
} = primordials;
810

@@ -90,35 +92,30 @@ function mlKemExportKey(key, format) {
9092
switch (format) {
9193
case kWebCryptoKeyFormatRaw: {
9294
if (key[kKeyType] === 'private') {
93-
return key[kKeyObject][kHandle].rawSeed().buffer;
95+
return TypedArrayPrototypeGetBuffer(key[kKeyObject][kHandle].rawSeed());
9496
}
9597

96-
return key[kKeyObject][kHandle].rawPublicKey().buffer;
98+
return TypedArrayPrototypeGetBuffer(key[kKeyObject][kHandle].rawPublicKey());
9799
}
98100
case kWebCryptoKeyFormatSPKI: {
99-
return key[kKeyObject][kHandle].export(kKeyFormatDER, kWebCryptoKeyFormatSPKI).buffer;
101+
return TypedArrayPrototypeGetBuffer(key[kKeyObject][kHandle].export(kKeyFormatDER, kWebCryptoKeyFormatSPKI));
100102
}
101103
case kWebCryptoKeyFormatPKCS8: {
102104
const seed = key[kKeyObject][kHandle].rawSeed();
103105
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,
106114
0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04,
107-
0x04, 0x00, 0x04, 0x42, 0x80, 0x40,
115+
0x04, orc, 0x04, 0x42, 0x80, 0x40,
108116
], 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);
122119
}
123120
default:
124121
return undefined;
@@ -241,7 +238,11 @@ function mlKemEncapsulate(encapsulationKey) {
241238
{ name: 'OperationError', cause: error }));
242239
} else {
243240
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+
});
245246
}
246247
};
247248
job.run();
@@ -270,7 +271,7 @@ function mlKemDecapsulate(decapsulationKey, ciphertext) {
270271
'The operation failed for an operation-specific reason',
271272
{ name: 'OperationError', cause: error }));
272273
} else {
273-
resolve(result.buffer);
274+
resolve(TypedArrayPrototypeGetBuffer(result));
274275
}
275276
};
276277
job.run();

lib/internal/crypto/pbkdf2.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const {
44
ArrayBuffer,
55
FunctionPrototypeCall,
6+
TypedArrayPrototypeGetBuffer,
67
} = primordials;
78

89
const { Buffer } = require('buffer');
@@ -121,7 +122,7 @@ async function pbkdf2DeriveBits(algorithm, baseKey, length) {
121122
{ name: 'OperationError', cause: err });
122123
}
123124

124-
return result.buffer;
125+
return TypedArrayPrototypeGetBuffer(result);
125126
}
126127

127128
module.exports = {

lib/internal/crypto/random.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const {
1919
NumberPrototypeToString,
2020
StringFromCharCodeApply,
2121
StringPrototypePadStart,
22+
TypedArrayPrototypeGetBuffer,
2223
} = primordials;
2324

2425
const {
@@ -104,12 +105,12 @@ function randomBytes(size, callback) {
104105
const buf = new FastBuffer(size);
105106

106107
if (callback === undefined) {
107-
randomFillSync(buf.buffer, 0, size);
108+
randomFillSync(TypedArrayPrototypeGetBuffer(buf), 0, size);
108109
return buf;
109110
}
110111

111112
// 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) {
113114
if (error) return FunctionPrototypeCall(callback, this, error);
114115
FunctionPrototypeCall(callback, this, null, buf);
115116
});

lib/internal/crypto/webcrypto.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const {
1111
StringPrototypeSlice,
1212
StringPrototypeStartsWith,
1313
SymbolToStringTag,
14+
TypedArrayPrototypeGetBuffer,
1415
} = primordials;
1516

1617
const {
@@ -544,7 +545,7 @@ async function exportKeyRawSecret(key, format) {
544545
case 'AES-KW':
545546
// Fall through
546547
case 'HMAC':
547-
return key[kKeyObject][kHandle].export().buffer;
548+
return TypedArrayPrototypeGetBuffer(key[kKeyObject][kHandle].export());
548549
case 'AES-OCB':
549550
// Fall through
550551
case 'KMAC128':
@@ -553,7 +554,7 @@ async function exportKeyRawSecret(key, format) {
553554
// Fall through
554555
case 'ChaCha20-Poly1305':
555556
if (format === 'raw-secret') {
556-
return key[kKeyObject][kHandle].export().buffer;
557+
return TypedArrayPrototypeGetBuffer(key[kKeyObject][kHandle].export());
557558
}
558559
return undefined;
559560
default:

0 commit comments

Comments
 (0)