Skip to content

Commit 45e62ab

Browse files
committed
fixup! crypto: support ML-KEM, DHKEM, and RSASVE key encapsulation mechanisms
1 parent 1cec197 commit 45e62ab

6 files changed

Lines changed: 11 additions & 27 deletions

File tree

deps/ncrypto/ncrypto.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1589,13 +1589,13 @@ class KEM final {
15891589
: ciphertext(std::move(ct)), shared_key(std::move(sk)) {}
15901590
};
15911591

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

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

doc/api/errors.md

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,16 +1076,6 @@ added: REPLACEME
10761076
Attempted to use KEM operations while Node.js was not compiled with
10771077
OpenSSL with KEM support.
10781078

1079-
<a id="ERR_CRYPTO_KEY_REQUIRED"></a>
1080-
1081-
### `ERR_CRYPTO_KEY_REQUIRED`
1082-
1083-
<!-- YAML
1084-
added: REPLACEME
1085-
-->
1086-
1087-
A `key` was not provided.
1088-
10891079
<a id="ERR_CRYPTO_OPERATION_FAILED"></a>
10901080

10911081
### `ERR_CRYPTO_OPERATION_FAILED`

lib/internal/crypto/kem.js

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ const {
77
const {
88
codes: {
99
ERR_CRYPTO_KEM_NOT_SUPPORTED,
10-
ERR_CRYPTO_KEY_REQUIRED,
1110
},
1211
} = require('internal/errors');
1312

@@ -35,9 +34,6 @@ function encapsulate(key, callback) {
3534
if (!KEMEncapsulateJob)
3635
throw new ERR_CRYPTO_KEM_NOT_SUPPORTED();
3736

38-
if (!key)
39-
throw new ERR_CRYPTO_KEY_REQUIRED();
40-
4137
if (callback !== undefined)
4238
validateFunction(callback, 'callback');
4339

@@ -75,21 +71,18 @@ function decapsulate(key, ciphertext, callback) {
7571
if (!KEMDecapsulateJob)
7672
throw new ERR_CRYPTO_KEM_NOT_SUPPORTED();
7773

78-
if (!key)
79-
throw new ERR_CRYPTO_KEY_REQUIRED();
80-
8174
if (callback !== undefined)
8275
validateFunction(callback, 'callback');
8376

84-
ciphertext = getArrayBufferOrView(ciphertext, 'ciphertext');
85-
8677
const {
8778
data: keyData,
8879
format: keyFormat,
8980
type: keyType,
9081
passphrase: keyPassphrase,
9182
} = preparePrivateKey(key);
9283

84+
ciphertext = getArrayBufferOrView(ciphertext, 'ciphertext');
85+
9386
const job = new KEMDecapsulateJob(
9487
callback ? kCryptoJobAsync : kCryptoJobSync,
9588
keyData,

lib/internal/errors.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1185,7 +1185,6 @@ E('ERR_CRYPTO_INVALID_KEY_OBJECT_TYPE',
11851185
'Invalid key object type %s, expected %s.', TypeError);
11861186
E('ERR_CRYPTO_INVALID_STATE', 'Invalid state for operation %s', Error);
11871187
E('ERR_CRYPTO_KEM_NOT_SUPPORTED', 'KEM is not supported', Error);
1188-
E('ERR_CRYPTO_KEY_REQUIRED', 'No key provided', Error);
11891188
E('ERR_CRYPTO_PBKDF2_ERROR', 'PBKDF2 error', Error);
11901189
E('ERR_CRYPTO_SCRYPT_NOT_SUPPORTED', 'Scrypt algorithm not supported', Error);
11911190
// Switch to TypeError. The current implementation does not seem right.

src/crypto/crypto_kem.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ MaybeLocal<Value> KEMEncapsulateTraits::EncodeOutput(
174174
return MaybeLocal<Value>();
175175
}
176176

177-
// Return an array [sharedKey, ciphertext]
177+
// Return an array [sharedKey, ciphertext].
178178
Local<Array> result = Array::New(env->isolate(), 2);
179179
if (result->Set(env->context(), 0, shared_key_obj).IsNothing() ||
180180
result->Set(env->context(), 1, ciphertext_obj).IsNothing()) {

test/parallel/test-crypto-encap-decap.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ if (!hasOpenSSL(3)) {
1414
return;
1515
}
1616

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

2022
const keys = {
2123
'rsa': {

0 commit comments

Comments
 (0)