Files
node/lib/internal/crypto/kem.js
Filip Skokan 1ccae7cce4 crypto: unify asymmetric key import through KeyObjectHandle::Init
Consolidate all asymmetric key import paths (DER/PEM, JWK, raw) into
a single KeyObjectHandle::Init() entry point with a uniform signature.

Remove the per-type C++ init methods (InitECRaw, InitEDRaw, InitPqcRaw,
InitJwk, InitECPrivateRaw) and their JS-side callers, replacing them
with shared C++ and JS helpers.

createPublicKey, createPrivateKey, sign, verify, and other operations
that accept key material now handle JWK and raw formats directly in
C++, removing redundant JS-to-C++ key handle round-trips.

Signed-off-by: Filip Skokan <panva.ip@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/62499
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
2026-03-31 19:57:34 +00:00

117 lines
2.4 KiB
JavaScript

'use strict';
const {
FunctionPrototypeCall,
} = primordials;
const {
codes: {
ERR_CRYPTO_KEM_NOT_SUPPORTED,
},
} = require('internal/errors');
const {
validateFunction,
} = require('internal/validators');
const {
kCryptoJobAsync,
kCryptoJobSync,
KEMDecapsulateJob,
KEMEncapsulateJob,
} = internalBinding('crypto');
const {
preparePrivateKey,
preparePublicOrPrivateKey,
} = require('internal/crypto/keys');
const {
getArrayBufferOrView,
} = require('internal/crypto/util');
function encapsulate(key, callback) {
if (!KEMEncapsulateJob)
throw new ERR_CRYPTO_KEM_NOT_SUPPORTED();
if (callback !== undefined)
validateFunction(callback, 'callback');
const {
data: keyData,
format: keyFormat,
type: keyType,
passphrase: keyPassphrase,
namedCurve: keyNamedCurve,
} = preparePublicOrPrivateKey(key);
const job = new KEMEncapsulateJob(
callback ? kCryptoJobAsync : kCryptoJobSync,
keyData,
keyFormat,
keyType,
keyPassphrase,
keyNamedCurve);
if (!callback) {
const { 0: err, 1: result } = job.run();
if (err !== undefined)
throw err;
const { 0: sharedKey, 1: ciphertext } = result;
return { sharedKey, ciphertext };
}
job.ondone = (error, result) => {
if (error) return FunctionPrototypeCall(callback, job, error);
const { 0: sharedKey, 1: ciphertext } = result;
FunctionPrototypeCall(callback, job, null, { sharedKey, ciphertext });
};
job.run();
}
function decapsulate(key, ciphertext, callback) {
if (!KEMDecapsulateJob)
throw new ERR_CRYPTO_KEM_NOT_SUPPORTED();
if (callback !== undefined)
validateFunction(callback, 'callback');
const {
data: keyData,
format: keyFormat,
type: keyType,
passphrase: keyPassphrase,
namedCurve: keyNamedCurve,
} = preparePrivateKey(key);
ciphertext = getArrayBufferOrView(ciphertext, 'ciphertext');
const job = new KEMDecapsulateJob(
callback ? kCryptoJobAsync : kCryptoJobSync,
keyData,
keyFormat,
keyType,
keyPassphrase,
keyNamedCurve,
ciphertext);
if (!callback) {
const { 0: err, 1: result } = job.run();
if (err !== undefined)
throw err;
return result;
}
job.ondone = (error, result) => {
if (error) return FunctionPrototypeCall(callback, job, error);
FunctionPrototypeCall(callback, job, null, result);
};
job.run();
}
module.exports = {
encapsulate,
decapsulate,
};