Skip to content

Commit 0124e0e

Browse files
panvanodejs-github-bot
authored andcommitted
crypto: support Ed448 and ML-DSA context parameter in Web Cryptography
PR-URL: #59570 Reviewed-By: James M Snell <[email protected]>
1 parent 3837993 commit 0124e0e

File tree

21 files changed

+363
-207
lines changed

21 files changed

+363
-207
lines changed

deps/ncrypto/ncrypto.cc

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4288,6 +4288,54 @@ std::optional<EVP_PKEY_CTX*> EVPMDCtxPointer::verifyInit(
42884288
return ctx;
42894289
}
42904290

4291+
std::optional<EVP_PKEY_CTX*> EVPMDCtxPointer::signInitWithContext(
4292+
const EVPKeyPointer& key,
4293+
const Digest& digest,
4294+
const Buffer<const unsigned char>& context_string) {
4295+
#ifdef OSSL_SIGNATURE_PARAM_CONTEXT_STRING
4296+
EVP_PKEY_CTX* ctx = nullptr;
4297+
4298+
const OSSL_PARAM params[] = {
4299+
OSSL_PARAM_construct_octet_string(
4300+
OSSL_SIGNATURE_PARAM_CONTEXT_STRING,
4301+
const_cast<unsigned char*>(context_string.data),
4302+
context_string.len),
4303+
OSSL_PARAM_END};
4304+
4305+
if (!EVP_DigestSignInit_ex(
4306+
ctx_.get(), &ctx, nullptr, nullptr, nullptr, key.get(), params)) {
4307+
return std::nullopt;
4308+
}
4309+
return ctx;
4310+
#else
4311+
return std::nullopt;
4312+
#endif
4313+
}
4314+
4315+
std::optional<EVP_PKEY_CTX*> EVPMDCtxPointer::verifyInitWithContext(
4316+
const EVPKeyPointer& key,
4317+
const Digest& digest,
4318+
const Buffer<const unsigned char>& context_string) {
4319+
#ifdef OSSL_SIGNATURE_PARAM_CONTEXT_STRING
4320+
EVP_PKEY_CTX* ctx = nullptr;
4321+
4322+
const OSSL_PARAM params[] = {
4323+
OSSL_PARAM_construct_octet_string(
4324+
OSSL_SIGNATURE_PARAM_CONTEXT_STRING,
4325+
const_cast<unsigned char*>(context_string.data),
4326+
context_string.len),
4327+
OSSL_PARAM_END};
4328+
4329+
if (!EVP_DigestVerifyInit_ex(
4330+
ctx_.get(), &ctx, nullptr, nullptr, nullptr, key.get(), params)) {
4331+
return std::nullopt;
4332+
}
4333+
return ctx;
4334+
#else
4335+
return std::nullopt;
4336+
#endif
4337+
}
4338+
42914339
DataPointer EVPMDCtxPointer::signOneShot(
42924340
const Buffer<const unsigned char>& buf) const {
42934341
if (!ctx_) return {};

deps/ncrypto/ncrypto.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1409,6 +1409,15 @@ class EVPMDCtxPointer final {
14091409
std::optional<EVP_PKEY_CTX*> verifyInit(const EVPKeyPointer& key,
14101410
const Digest& digest);
14111411

1412+
std::optional<EVP_PKEY_CTX*> signInitWithContext(
1413+
const EVPKeyPointer& key,
1414+
const Digest& digest,
1415+
const Buffer<const unsigned char>& context_string);
1416+
std::optional<EVP_PKEY_CTX*> verifyInitWithContext(
1417+
const EVPKeyPointer& key,
1418+
const Digest& digest,
1419+
const Buffer<const unsigned char>& context_string);
1420+
14121421
DataPointer signOneShot(const Buffer<const unsigned char>& buf) const;
14131422
DataPointer sign(const Buffer<const unsigned char>& buf) const;
14141423
bool verify(const Buffer<const unsigned char>& buf,

doc/api/webcrypto.md

Lines changed: 8 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1342,7 +1342,7 @@ changes:
13421342
13431343
<!--lint disable maximum-line-length remark-lint-->
13441344
1345-
* `algorithm` {string|Algorithm|RsaPssParams|EcdsaParams|Ed448Params|ContextParams|KmacParams}
1345+
* `algorithm` {string|Algorithm|RsaPssParams|EcdsaParams|ContextParams|KmacParams}
13461346
* `key` {CryptoKey}
13471347
* `data` {ArrayBuffer|TypedArray|DataView|Buffer}
13481348
* Returns: {Promise} Fulfills with an {ArrayBuffer} upon success.
@@ -1463,7 +1463,7 @@ changes:
14631463
14641464
<!--lint disable maximum-line-length remark-lint-->
14651465
1466-
* `algorithm` {string|Algorithm|RsaPssParams|EcdsaParams|Ed448Params|ContextParams}
1466+
* `algorithm` {string|Algorithm|RsaPssParams|EcdsaParams|ContextParams|KmacParams}
14671467
* `key` {CryptoKey}
14681468
* `signature` {ArrayBuffer|TypedArray|DataView|Buffer}
14691469
* `data` {ArrayBuffer|TypedArray|DataView|Buffer}
@@ -1830,20 +1830,23 @@ added: v24.7.0
18301830
added: v24.7.0
18311831
-->
18321832
1833-
* Type: {string} Must be `'ML-DSA-44'`[^modern-algos], `'ML-DSA-65'`[^modern-algos], or `'ML-DSA-87'`[^modern-algos].
1833+
* Type: {string} Must be `Ed448`[^secure-curves], `'ML-DSA-44'`[^modern-algos],
1834+
`'ML-DSA-65'`[^modern-algos], or `'ML-DSA-87'`[^modern-algos].
18341835
18351836
#### `contextParams.context`
18361837
18371838
<!-- YAML
18381839
added: v24.7.0
1840+
changes:
1841+
- version: REPLACEME
1842+
pr-url: https://github.com/nodejs/node/pull/59570
1843+
description: Non-empty context is now supported.
18391844
-->
18401845
18411846
* Type: {ArrayBuffer|TypedArray|DataView|Buffer|undefined}
18421847
18431848
The `context` member represents the optional context data to associate with
18441849
the message.
1845-
The Node.js Web Crypto API implementation only supports zero-length context
1846-
which is equivalent to not providing context at all.
18471850
18481851
### Class: `CShakeParams`
18491852
@@ -2024,37 +2027,6 @@ added: v15.0.0
20242027
20252028
* Type: {string} Must be one of `'P-256'`, `'P-384'`, `'P-521'`.
20262029
2027-
### Class: `Ed448Params`
2028-
2029-
<!-- YAML
2030-
added: v15.0.0
2031-
-->
2032-
2033-
#### `ed448Params.name`
2034-
2035-
<!-- YAML
2036-
added:
2037-
- v18.4.0
2038-
- v16.17.0
2039-
-->
2040-
2041-
* Type: {string} Must be `'Ed448'`[^secure-curves].
2042-
2043-
#### `ed448Params.context`
2044-
2045-
<!-- YAML
2046-
added:
2047-
- v18.4.0
2048-
- v16.17.0
2049-
-->
2050-
2051-
* Type: {ArrayBuffer|TypedArray|DataView|Buffer|undefined}
2052-
2053-
The `context` member represents the optional context data to associate with
2054-
the message.
2055-
The Node.js Web Crypto API implementation only supports zero-length context
2056-
which is equivalent to not providing context at all.
2057-
20582030
### Class: `EncapsulatedBits`
20592031
20602032
<!-- YAML

lib/internal/crypto/cfrg.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,7 @@ function eddsaSignVerify(key, data, algorithm, signature) {
359359
undefined,
360360
undefined,
361361
undefined,
362+
algorithm.name === 'Ed448' ? algorithm.context : undefined,
362363
signature));
363364
}
364365

lib/internal/crypto/ec.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ function ecdsaSignVerify(key, data, { name, hash }, signature) {
302302
undefined, // Salt length, not used with ECDSA
303303
undefined, // PSS Padding, not used with ECDSA
304304
kSigEncP1363,
305+
undefined,
305306
signature));
306307
}
307308

lib/internal/crypto/ml_dsa.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@ function mlDsaSignVerify(key, data, algorithm, signature) {
303303
undefined,
304304
undefined,
305305
undefined,
306+
algorithm.context,
306307
signature));
307308
}
308309

lib/internal/crypto/rsa.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,7 @@ function rsaSignVerify(key, data, { saltLength }, signature) {
355355
saltLength,
356356
key[kAlgorithm].name === 'RSA-PSS' ? RSA_PKCS1_PSS_PADDING : undefined,
357357
undefined,
358+
undefined,
358359
signature);
359360
});
360361
}

lib/internal/crypto/sig.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,9 @@ function signOneShot(algorithm, data, key, callback) {
171171
algorithm,
172172
pssSaltLength,
173173
rsaPadding,
174-
dsaSigEnc);
174+
dsaSigEnc,
175+
undefined,
176+
undefined);
175177

176178
if (!callback) {
177179
const { 0: err, 1: signature } = job.run();
@@ -276,6 +278,7 @@ function verifyOneShot(algorithm, data, key, signature, callback) {
276278
pssSaltLength,
277279
rsaPadding,
278280
dsaSigEnc,
281+
undefined,
279282
signature);
280283

281284
if (!callback) {

lib/internal/crypto/util.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,8 @@ const kAlgorithmDefinitions = {
268268
'generateKey': null,
269269
'exportKey': null,
270270
'importKey': null,
271-
'sign': 'Ed448Params',
272-
'verify': 'Ed448Params',
271+
'sign': 'ContextParams',
272+
'verify': 'ContextParams',
273273
},
274274
'HKDF': {
275275
'importKey': null,
@@ -494,7 +494,6 @@ const simpleAlgorithmDictionaries = {
494494
salt: 'BufferSource',
495495
info: 'BufferSource',
496496
},
497-
Ed448Params: { context: 'BufferSource' },
498497
ContextParams: { context: 'BufferSource' },
499498
Pbkdf2Params: { hash: 'HashAlgorithmIdentifier', salt: 'BufferSource' },
500499
RsaOaepParams: { label: 'BufferSource' },

lib/internal/crypto/webidl.js

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const {
1919
MathTrunc,
2020
Number,
2121
NumberIsFinite,
22+
NumberParseInt,
2223
ObjectPrototypeHasOwnProperty,
2324
ObjectPrototypeIsPrototypeOf,
2425
SafeArrayIterator,
@@ -304,13 +305,12 @@ function createDictionaryConverter(name, dictionaries) {
304305
const context = `'${key}' of '${name}'${
305306
opts.context ? ` (${opts.context})` : ''
306307
}`;
307-
const { converter, validator } = member;
308-
const idlMemberValue = converter(esMemberValue, {
308+
const idlMemberValue = member.converter(esMemberValue, {
309309
__proto__: null,
310310
...opts,
311311
context,
312312
});
313-
validator?.(idlMemberValue, esDict);
313+
member.validator?.(idlMemberValue, esDict);
314314
setOwnProperty(idlDict, key, idlMemberValue);
315315
} else if (member.required) {
316316
throw makeException(
@@ -769,17 +769,25 @@ converters.EcdhKeyDeriveParams = createDictionaryConverter(
769769
},
770770
]);
771771

772-
for (const name of ['Ed448Params', 'ContextParams']) {
773-
converters[name] = createDictionaryConverter(
774-
name, [
775-
...new SafeArrayIterator(dictAlgorithm),
776-
{
777-
key: 'context',
778-
converter: converters.BufferSource,
779-
validator: validateZeroLength(`${name}.context`),
772+
converters.ContextParams = createDictionaryConverter(
773+
'ContextParams', [
774+
...new SafeArrayIterator(dictAlgorithm),
775+
{
776+
key: 'context',
777+
converter: converters.BufferSource,
778+
validator(V, dict) {
779+
let { 0: major, 1: minor } = process.versions.openssl.split('.');
780+
major = NumberParseInt(major, 10);
781+
minor = NumberParseInt(minor, 10);
782+
if (major > 3 || (major === 3 && minor >= 2)) {
783+
this.validator = undefined;
784+
} else {
785+
this.validator = validateZeroLength('ContextParams.context');
786+
this.validator(V, dict);
787+
}
780788
},
781-
]);
782-
}
789+
},
790+
]);
783791

784792
converters.Argon2Params = createDictionaryConverter(
785793
'Argon2Params', [

0 commit comments

Comments
 (0)