Skip to content

Commit b69e54b

Browse files
committed
crypto: support Ed448 and ML-DSA context parameter in Web Cryptography
1 parent d00228f commit b69e54b

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
@@ -1283,7 +1283,7 @@ changes:
12831283
12841284
<!--lint disable maximum-line-length remark-lint-->
12851285
1286-
* `algorithm` {string|Algorithm|RsaPssParams|EcdsaParams|Ed448Params|ContextParams}
1286+
* `algorithm` {string|Algorithm|RsaPssParams|EcdsaParams|ContextParams}
12871287
* `key` {CryptoKey}
12881288
* `data` {ArrayBuffer|TypedArray|DataView|Buffer}
12891289
* Returns: {Promise} Fulfills with an {ArrayBuffer} upon success.
@@ -1397,7 +1397,7 @@ changes:
13971397
13981398
<!--lint disable maximum-line-length remark-lint-->
13991399
1400-
* `algorithm` {string|Algorithm|RsaPssParams|EcdsaParams|Ed448Params|ContextParams}
1400+
* `algorithm` {string|Algorithm|RsaPssParams|EcdsaParams|ContextParams}
14011401
* `key` {CryptoKey}
14021402
* `signature` {ArrayBuffer|TypedArray|DataView|Buffer}
14031403
* `data` {ArrayBuffer|TypedArray|DataView|Buffer}
@@ -1678,20 +1678,23 @@ added: REPLACEME
16781678
added: REPLACEME
16791679
-->
16801680
1681-
* Type: {string} Must be `'ML-DSA-44'`[^modern-algos], `'ML-DSA-65'`[^modern-algos], or `'ML-DSA-87'`[^modern-algos].
1681+
* Type: {string} Must be `Ed448`[^secure-curves], `'ML-DSA-44'`[^modern-algos],
1682+
`'ML-DSA-65'`[^modern-algos], or `'ML-DSA-87'`[^modern-algos].
16821683
16831684
#### `contextParams.context`
16841685
16851686
<!-- YAML
16861687
added: REPLACEME
1688+
changes:
1689+
- version: REPLACEME
1690+
pr-url: https://github.com/nodejs/node/pull/59570
1691+
description: Non-empty context is now supported.
16871692
-->
16881693
16891694
* Type: {ArrayBuffer|TypedArray|DataView|Buffer|undefined}
16901695
16911696
The `context` member represents the optional context data to associate with
16921697
the message.
1693-
The Node.js Web Crypto API implementation only supports zero-length context
1694-
which is equivalent to not providing context at all.
16951698
16961699
### Class: `CShakeParams`
16971700
@@ -1872,37 +1875,6 @@ added: v15.0.0
18721875
18731876
* Type: {string} Must be one of `'P-256'`, `'P-384'`, `'P-521'`.
18741877
1875-
### Class: `Ed448Params`
1876-
1877-
<!-- YAML
1878-
added: v15.0.0
1879-
-->
1880-
1881-
#### `ed448Params.name`
1882-
1883-
<!-- YAML
1884-
added:
1885-
- v18.4.0
1886-
- v16.17.0
1887-
-->
1888-
1889-
* Type: {string} Must be `'Ed448'`[^secure-curves].
1890-
1891-
#### `ed448Params.context`
1892-
1893-
<!-- YAML
1894-
added:
1895-
- v18.4.0
1896-
- v16.17.0
1897-
-->
1898-
1899-
* Type: {ArrayBuffer|TypedArray|DataView|Buffer|undefined}
1900-
1901-
The `context` member represents the optional context data to associate with
1902-
the message.
1903-
The Node.js Web Crypto API implementation only supports zero-length context
1904-
which is equivalent to not providing context at all.
1905-
19061878
### Class: `EncapsulatedBits`
19071879
19081880
<!-- 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
@@ -251,8 +251,8 @@ const kAlgorithmDefinitions = {
251251
'generateKey': null,
252252
'exportKey': null,
253253
'importKey': null,
254-
'sign': 'Ed448Params',
255-
'verify': 'Ed448Params',
254+
'sign': 'ContextParams',
255+
'verify': 'ContextParams',
256256
},
257257
'HKDF': {
258258
'importKey': null,
@@ -451,7 +451,6 @@ const simpleAlgorithmDictionaries = {
451451
salt: 'BufferSource',
452452
info: 'BufferSource',
453453
},
454-
Ed448Params: { context: 'BufferSource' },
455454
ContextParams: { context: 'BufferSource' },
456455
Pbkdf2Params: { hash: 'HashAlgorithmIdentifier', salt: 'BufferSource' },
457456
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
module.exports = {
809817
converters,

0 commit comments

Comments
 (0)