Skip to content

Commit 9159687

Browse files
committed
crypto: support Ed448 and ML-DSA context parameter in Web Cryptography
1 parent 27e2d81 commit 9159687

File tree

21 files changed

+365
-207
lines changed

21 files changed

+365
-207
lines changed

deps/ncrypto/ncrypto.cc

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4288,6 +4288,56 @@ 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+
const char* digest_name = digest ? EVP_MD_get0_name(digest) : nullptr;
4306+
if (!EVP_DigestSignInit_ex(
4307+
ctx_.get(), &ctx, digest_name, nullptr, nullptr, key.get(), params)) {
4308+
return std::nullopt;
4309+
}
4310+
return ctx;
4311+
#else
4312+
return std::nullopt;
4313+
#endif
4314+
}
4315+
4316+
std::optional<EVP_PKEY_CTX*> EVPMDCtxPointer::verifyInitWithContext(
4317+
const EVPKeyPointer& key,
4318+
const Digest& digest,
4319+
const Buffer<const unsigned char>& context_string) {
4320+
#ifdef OSSL_SIGNATURE_PARAM_CONTEXT_STRING
4321+
EVP_PKEY_CTX* ctx = nullptr;
4322+
4323+
const OSSL_PARAM params[] = {
4324+
OSSL_PARAM_construct_octet_string(
4325+
OSSL_SIGNATURE_PARAM_CONTEXT_STRING,
4326+
const_cast<unsigned char*>(context_string.data),
4327+
context_string.len),
4328+
OSSL_PARAM_END};
4329+
4330+
const char* digest_name = digest ? EVP_MD_get0_name(digest) : nullptr;
4331+
if (!EVP_DigestVerifyInit_ex(
4332+
ctx_.get(), &ctx, digest_name, nullptr, nullptr, key.get(), params)) {
4333+
return std::nullopt;
4334+
}
4335+
return ctx;
4336+
#else
4337+
return std::nullopt;
4338+
#endif
4339+
}
4340+
42914341
DataPointer EVPMDCtxPointer::signOneShot(
42924342
const Buffer<const unsigned char>& buf) const {
42934343
if (!ctx_) return {};

deps/ncrypto/ncrypto.h

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

1410+
std::optional<EVP_PKEY_CTX*> signInitWithContext(
1411+
const EVPKeyPointer& key,
1412+
const Digest& digest,
1413+
const Buffer<const unsigned char>& context_string);
1414+
std::optional<EVP_PKEY_CTX*> verifyInitWithContext(
1415+
const EVPKeyPointer& key,
1416+
const Digest& digest,
1417+
const Buffer<const unsigned char>& context_string);
1418+
14101419
DataPointer signOneShot(const Buffer<const unsigned char>& buf) const;
14111420
DataPointer sign(const Buffer<const unsigned char>& buf) const;
14121421
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
@@ -1313,7 +1313,7 @@ changes:
13131313
13141314
<!--lint disable maximum-line-length remark-lint-->
13151315
1316-
* `algorithm` {string|Algorithm|RsaPssParams|EcdsaParams|Ed448Params|ContextParams}
1316+
* `algorithm` {string|Algorithm|RsaPssParams|EcdsaParams|ContextParams}
13171317
* `key` {CryptoKey}
13181318
* `data` {ArrayBuffer|TypedArray|DataView|Buffer}
13191319
* Returns: {Promise} Fulfills with an {ArrayBuffer} upon success.
@@ -1427,7 +1427,7 @@ changes:
14271427
14281428
<!--lint disable maximum-line-length remark-lint-->
14291429
1430-
* `algorithm` {string|Algorithm|RsaPssParams|EcdsaParams|Ed448Params|ContextParams}
1430+
* `algorithm` {string|Algorithm|RsaPssParams|EcdsaParams|ContextParams}
14311431
* `key` {CryptoKey}
14321432
* `signature` {ArrayBuffer|TypedArray|DataView|Buffer}
14331433
* `data` {ArrayBuffer|TypedArray|DataView|Buffer}
@@ -1792,20 +1792,23 @@ added: REPLACEME
17921792
added: REPLACEME
17931793
-->
17941794
1795-
* Type: {string} Must be `'ML-DSA-44'`[^modern-algos], `'ML-DSA-65'`[^modern-algos], or `'ML-DSA-87'`[^modern-algos].
1795+
* Type: {string} Must be `Ed448`[^secure-curves], `'ML-DSA-44'`[^modern-algos],
1796+
`'ML-DSA-65'`[^modern-algos], or `'ML-DSA-87'`[^modern-algos].
17961797
17971798
#### `contextParams.context`
17981799
17991800
<!-- YAML
18001801
added: REPLACEME
1802+
changes:
1803+
- version: REPLACEME
1804+
pr-url: https://github.com/nodejs/node/pull/59570
1805+
description: Non-empty context is now supported.
18011806
-->
18021807
18031808
* Type: {ArrayBuffer|TypedArray|DataView|Buffer|undefined}
18041809
18051810
The `context` member represents the optional context data to associate with
18061811
the message.
1807-
The Node.js Web Crypto API implementation only supports zero-length context
1808-
which is equivalent to not providing context at all.
18091812
18101813
### Class: `CShakeParams`
18111814
@@ -1986,37 +1989,6 @@ added: v15.0.0
19861989
19871990
* Type: {string} Must be one of `'P-256'`, `'P-384'`, `'P-521'`.
19881991
1989-
### Class: `Ed448Params`
1990-
1991-
<!-- YAML
1992-
added: v15.0.0
1993-
-->
1994-
1995-
#### `ed448Params.name`
1996-
1997-
<!-- YAML
1998-
added:
1999-
- v18.4.0
2000-
- v16.17.0
2001-
-->
2002-
2003-
* Type: {string} Must be `'Ed448'`[^secure-curves].
2004-
2005-
#### `ed448Params.context`
2006-
2007-
<!-- YAML
2008-
added:
2009-
- v18.4.0
2010-
- v16.17.0
2011-
-->
2012-
2013-
* Type: {ArrayBuffer|TypedArray|DataView|Buffer|undefined}
2014-
2015-
The `context` member represents the optional context data to associate with
2016-
the message.
2017-
The Node.js Web Crypto API implementation only supports zero-length context
2018-
which is equivalent to not providing context at all.
2019-
20201992
### Class: `EncapsulatedBits`
20211993
20221994
<!-- 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
@@ -306,6 +306,7 @@ function mlDsaSignVerify(key, data, algorithm, signature) {
306306
undefined,
307307
undefined,
308308
undefined,
309+
algorithm.context,
309310
signature));
310311
}
311312

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
@@ -267,8 +267,8 @@ const kAlgorithmDefinitions = {
267267
'generateKey': null,
268268
'exportKey': null,
269269
'importKey': null,
270-
'sign': 'Ed448Params',
271-
'verify': 'Ed448Params',
270+
'sign': 'ContextParams',
271+
'verify': 'ContextParams',
272272
},
273273
'HKDF': {
274274
'importKey': null,
@@ -473,7 +473,6 @@ const simpleAlgorithmDictionaries = {
473473
salt: 'BufferSource',
474474
info: 'BufferSource',
475475
},
476-
Ed448Params: { context: 'BufferSource' },
477476
ContextParams: { context: 'BufferSource' },
478477
Pbkdf2Params: { hash: 'HashAlgorithmIdentifier', salt: 'BufferSource' },
479478
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(
@@ -793,17 +793,25 @@ converters.EcdhKeyDeriveParams = createDictionaryConverter(
793793
},
794794
]);
795795

796-
for (const name of ['Ed448Params', 'ContextParams']) {
797-
converters[name] = createDictionaryConverter(
798-
name, [
799-
...new SafeArrayIterator(dictAlgorithm),
800-
{
801-
key: 'context',
802-
converter: converters.BufferSource,
803-
validator: validateZeroLength(`${name}.context`),
796+
converters.ContextParams = createDictionaryConverter(
797+
'ContextParams', [
798+
...new SafeArrayIterator(dictAlgorithm),
799+
{
800+
key: 'context',
801+
converter: converters.BufferSource,
802+
validator(V, dict) {
803+
let { 0: major, 1: minor } = process.versions.openssl.split('.');
804+
major = NumberParseInt(major, 10);
805+
minor = NumberParseInt(minor, 10);
806+
if (major > 3 || (major === 3 && minor >= 2)) {
807+
this.validator = undefined;
808+
} else {
809+
this.validator = validateZeroLength('ContextParams.context');
810+
this.validator(V, dict);
811+
}
804812
},
805-
]);
806-
}
813+
},
814+
]);
807815

808816
converters.Argon2Params = createDictionaryConverter(
809817
'Argon2Params', [

0 commit comments

Comments
 (0)