Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/internal/crypto/argon2.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const {
FunctionPrototypeCall,
MathPow,
StringPrototypeToLowerCase,
TypedArrayPrototypeGetBuffer,
Uint8Array,
} = primordials;

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

return result.buffer;
return TypedArrayPrototypeGetBuffer(result);
}

module.exports = {
Expand Down
3 changes: 2 additions & 1 deletion lib/internal/crypto/diffiehellman.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const {
MathCeil,
ObjectDefineProperty,
SafeSet,
TypedArrayPrototypeGetBuffer,
Uint8Array,
} = primordials;

Expand Down Expand Up @@ -377,7 +378,7 @@ async function ecdhDeriveBits(algorithm, baseKey, length) {

const masked = new Uint8Array(slice);
masked[sliceLength - 1] = masked[sliceLength - 1] & masks[mod];
return masked.buffer;
return TypedArrayPrototypeGetBuffer(masked);
}

module.exports = {
Expand Down
36 changes: 15 additions & 21 deletions lib/internal/crypto/ml_dsa.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const {
SafeSet,
TypedArrayPrototypeGetBuffer,
Uint8Array,
} = primordials;

Expand Down Expand Up @@ -119,35 +120,28 @@ function mlDsaExportKey(key, format) {
switch (format) {
case kWebCryptoKeyFormatRaw: {
if (key[kKeyType] === 'private') {
return key[kKeyObject][kHandle].rawSeed().buffer;
return TypedArrayPrototypeGetBuffer(key[kKeyObject][kHandle].rawSeed());
}

return key[kKeyObject][kHandle].rawPublicKey().buffer;
return TypedArrayPrototypeGetBuffer(key[kKeyObject][kHandle].rawPublicKey());
}
case kWebCryptoKeyFormatSPKI: {
return key[kKeyObject][kHandle].export(kKeyFormatDER, kWebCryptoKeyFormatSPKI).buffer;
return TypedArrayPrototypeGetBuffer(key[kKeyObject][kHandle].export(kKeyFormatDER, kWebCryptoKeyFormatSPKI));
}
case kWebCryptoKeyFormatPKCS8: {
const seed = key[kKeyObject][kHandle].rawSeed();
const buffer = new Uint8Array(54);
buffer.set([
0x30, 0x34, 0x02, 0x01, 0x00, 0x30, 0x0B, 0x06,
const orc = {
'__proto__': null,
'ML-DSA-44': 0x11,
'ML-DSA-65': 0x12,
'ML-DSA-87': 0x13,
}[key[kAlgorithm].name];
const buffer = new Uint8Array([
0x30, 0x34, 0x02, 0x01, 0x00, 0x30, 0x0b, 0x06,
0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04,
0x03, 0x00, 0x04, 0x22, 0x80, 0x20,
], 0);
switch (key[kAlgorithm].name) {
case 'ML-DSA-44':
buffer.set([0x11], 17);
break;
case 'ML-DSA-65':
buffer.set([0x12], 17);
break;
case 'ML-DSA-87':
buffer.set([0x13], 17);
break;
}
buffer.set(seed, 22);
return buffer.buffer;
0x03, orc, 0x04, 0x22, 0x80, 0x20, ...seed,
]);
return TypedArrayPrototypeGetBuffer(buffer);
}
default:
return undefined;
Expand Down
44 changes: 21 additions & 23 deletions lib/internal/crypto/ml_kem.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const {
PromiseWithResolvers,
SafeSet,
TypedArrayPrototypeGetBuffer,
Uint8Array,
} = primordials;

Expand Down Expand Up @@ -90,35 +91,28 @@ function mlKemExportKey(key, format) {
switch (format) {
case kWebCryptoKeyFormatRaw: {
if (key[kKeyType] === 'private') {
return key[kKeyObject][kHandle].rawSeed().buffer;
return TypedArrayPrototypeGetBuffer(key[kKeyObject][kHandle].rawSeed());
}

return key[kKeyObject][kHandle].rawPublicKey().buffer;
return TypedArrayPrototypeGetBuffer(key[kKeyObject][kHandle].rawPublicKey());
}
case kWebCryptoKeyFormatSPKI: {
return key[kKeyObject][kHandle].export(kKeyFormatDER, kWebCryptoKeyFormatSPKI).buffer;
return TypedArrayPrototypeGetBuffer(key[kKeyObject][kHandle].export(kKeyFormatDER, kWebCryptoKeyFormatSPKI));
}
case kWebCryptoKeyFormatPKCS8: {
const seed = key[kKeyObject][kHandle].rawSeed();
const buffer = new Uint8Array(86);
buffer.set([
0x30, 0x54, 0x02, 0x01, 0x00, 0x30, 0x0B, 0x06,
const orc = {
'__proto__': null,
'ML-KEM-512': 0x01,
'ML-KEM-768': 0x02,
'ML-KEM-1024': 0x03,
}[key[kAlgorithm].name];
const buffer = new Uint8Array([
0x30, 0x54, 0x02, 0x01, 0x00, 0x30, 0x0b, 0x06,
0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04,
0x04, 0x00, 0x04, 0x42, 0x80, 0x40,
], 0);
switch (key[kAlgorithm].name) {
case 'ML-KEM-512':
buffer.set([0x01], 17);
break;
case 'ML-KEM-768':
buffer.set([0x02], 17);
break;
case 'ML-KEM-1024':
buffer.set([0x03], 17);
break;
}
buffer.set(seed, 22);
return buffer.buffer;
0x04, orc, 0x04, 0x42, 0x80, 0x40, ...seed,
]);
return TypedArrayPrototypeGetBuffer(buffer);
}
default:
return undefined;
Expand Down Expand Up @@ -241,7 +235,11 @@ function mlKemEncapsulate(encapsulationKey) {
{ name: 'OperationError', cause: error }));
} else {
const { 0: sharedKey, 1: ciphertext } = result;
resolve({ sharedKey: sharedKey.buffer, ciphertext: ciphertext.buffer });

resolve({
sharedKey: TypedArrayPrototypeGetBuffer(sharedKey),
ciphertext: TypedArrayPrototypeGetBuffer(ciphertext),
});
}
};
job.run();
Expand Down Expand Up @@ -270,7 +268,7 @@ function mlKemDecapsulate(decapsulationKey, ciphertext) {
'The operation failed for an operation-specific reason',
{ name: 'OperationError', cause: error }));
} else {
resolve(result.buffer);
resolve(TypedArrayPrototypeGetBuffer(result));
}
};
job.run();
Expand Down
3 changes: 2 additions & 1 deletion lib/internal/crypto/pbkdf2.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const {
ArrayBuffer,
FunctionPrototypeCall,
TypedArrayPrototypeGetBuffer,
} = primordials;

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

return result.buffer;
return TypedArrayPrototypeGetBuffer(result);
}

module.exports = {
Expand Down
5 changes: 3 additions & 2 deletions lib/internal/crypto/random.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const {
NumberPrototypeToString,
StringFromCharCodeApply,
StringPrototypePadStart,
TypedArrayPrototypeGetBuffer,
} = primordials;

const {
Expand Down Expand Up @@ -104,12 +105,12 @@ function randomBytes(size, callback) {
const buf = new FastBuffer(size);

if (callback === undefined) {
randomFillSync(buf.buffer, 0, size);
randomFillSync(TypedArrayPrototypeGetBuffer(buf), 0, size);
return buf;
}

// Keep the callback as a regular function so this is propagated.
randomFill(buf.buffer, 0, size, function(error) {
randomFill(TypedArrayPrototypeGetBuffer(buf), 0, size, function(error) {
if (error) return FunctionPrototypeCall(callback, this, error);
FunctionPrototypeCall(callback, this, null, buf);
});
Expand Down
5 changes: 3 additions & 2 deletions lib/internal/crypto/webcrypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const {
StringPrototypeSlice,
StringPrototypeStartsWith,
SymbolToStringTag,
TypedArrayPrototypeGetBuffer,
} = primordials;

const {
Expand Down Expand Up @@ -530,12 +531,12 @@ async function exportKeyRawSecret(key, format) {
case 'AES-KW':
// Fall through
case 'HMAC':
return key[kKeyObject][kHandle].export().buffer;
return TypedArrayPrototypeGetBuffer(key[kKeyObject][kHandle].export());
case 'AES-OCB':
// Fall through
case 'ChaCha20-Poly1305':
if (format === 'raw-secret') {
return key[kKeyObject][kHandle].export().buffer;
return TypedArrayPrototypeGetBuffer(key[kKeyObject][kHandle].export());
}
return undefined;
default:
Expand Down
Loading