lib: prefer TypedArrayPrototype primordials · nodejs/node@39befdd

@@ -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

}

959796-

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();