Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
fixup! crypto: support ML-KEM, DHKEM, and RSASVE key encapsulation me…
…chanisms
  • Loading branch information
panva committed Aug 19, 2025
commit 45e62ab4f20e4ba72ff3812849b5d944f0e2985e
8 changes: 4 additions & 4 deletions deps/ncrypto/ncrypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -1589,13 +1589,13 @@ class KEM final {
: ciphertext(std::move(ct)), shared_key(std::move(sk)) {}
};

// Encapsulate a shared secret using KEM with a public key
// Returns both the ciphertext and shared secret
// Encapsulate a shared secret using KEM with a public key.
// Returns both the ciphertext and shared secret.
static std::optional<EncapsulateResult> Encapsulate(
const EVPKeyPointer& public_key);

// Decapsulate a shared secret using KEM with a private key and ciphertext
// Returns the shared secret
// Decapsulate a shared secret using KEM with a private key and ciphertext.
// Returns the shared secret.
static DataPointer Decapsulate(const EVPKeyPointer& private_key,
const Buffer<const void>& ciphertext);

Expand Down
10 changes: 0 additions & 10 deletions doc/api/errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -1076,16 +1076,6 @@ added: REPLACEME
Attempted to use KEM operations while Node.js was not compiled with
OpenSSL with KEM support.

<a id="ERR_CRYPTO_KEY_REQUIRED"></a>

### `ERR_CRYPTO_KEY_REQUIRED`

<!-- YAML
added: REPLACEME
-->

A `key` was not provided.

<a id="ERR_CRYPTO_OPERATION_FAILED"></a>

### `ERR_CRYPTO_OPERATION_FAILED`
Expand Down
11 changes: 2 additions & 9 deletions lib/internal/crypto/kem.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const {
const {
codes: {
ERR_CRYPTO_KEM_NOT_SUPPORTED,
ERR_CRYPTO_KEY_REQUIRED,
},
} = require('internal/errors');

Expand Down Expand Up @@ -35,9 +34,6 @@ function encapsulate(key, callback) {
if (!KEMEncapsulateJob)
throw new ERR_CRYPTO_KEM_NOT_SUPPORTED();

if (!key)
throw new ERR_CRYPTO_KEY_REQUIRED();

if (callback !== undefined)
validateFunction(callback, 'callback');

Expand Down Expand Up @@ -75,21 +71,18 @@ function decapsulate(key, ciphertext, callback) {
if (!KEMDecapsulateJob)
throw new ERR_CRYPTO_KEM_NOT_SUPPORTED();

if (!key)
throw new ERR_CRYPTO_KEY_REQUIRED();

if (callback !== undefined)
validateFunction(callback, 'callback');

ciphertext = getArrayBufferOrView(ciphertext, 'ciphertext');

const {
data: keyData,
format: keyFormat,
type: keyType,
passphrase: keyPassphrase,
} = preparePrivateKey(key);

ciphertext = getArrayBufferOrView(ciphertext, 'ciphertext');

const job = new KEMDecapsulateJob(
callback ? kCryptoJobAsync : kCryptoJobSync,
keyData,
Expand Down
1 change: 0 additions & 1 deletion lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -1185,7 +1185,6 @@ E('ERR_CRYPTO_INVALID_KEY_OBJECT_TYPE',
'Invalid key object type %s, expected %s.', TypeError);
E('ERR_CRYPTO_INVALID_STATE', 'Invalid state for operation %s', Error);
E('ERR_CRYPTO_KEM_NOT_SUPPORTED', 'KEM is not supported', Error);
E('ERR_CRYPTO_KEY_REQUIRED', 'No key provided', Error);
E('ERR_CRYPTO_PBKDF2_ERROR', 'PBKDF2 error', Error);
E('ERR_CRYPTO_SCRYPT_NOT_SUPPORTED', 'Scrypt algorithm not supported', Error);
// Switch to TypeError. The current implementation does not seem right.
Expand Down
2 changes: 1 addition & 1 deletion src/crypto/crypto_kem.cc
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ MaybeLocal<Value> KEMEncapsulateTraits::EncodeOutput(
return MaybeLocal<Value>();
}

// Return an array [sharedKey, ciphertext]
// Return an array [sharedKey, ciphertext].
Local<Array> result = Array::New(env->isolate(), 2);
if (result->Set(env->context(), 0, shared_key_obj).IsNothing() ||
result->Set(env->context(), 1, ciphertext_obj).IsNothing()) {
Expand Down
6 changes: 4 additions & 2 deletions test/parallel/test-crypto-encap-decap.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ if (!hasOpenSSL(3)) {
return;
}

assert.throws(() => crypto.encapsulate(), { code: 'ERR_CRYPTO_KEY_REQUIRED' });
assert.throws(() => crypto.decapsulate(), { code: 'ERR_CRYPTO_KEY_REQUIRED' });
assert.throws(() => crypto.encapsulate(), { code: 'ERR_INVALID_ARG_TYPE',
message: /The "key" argument must be of type/ });
assert.throws(() => crypto.decapsulate(), { code: 'ERR_INVALID_ARG_TYPE',
message: /The "key" argument must be of type/ });

const keys = {
'rsa': {
Expand Down