Skip to content

Commit 43bd38c

Browse files
committed
fixup! crypto: add signDigest/verifyDigest and Ed25519ctx support
1 parent 8427faf commit 43bd38c

2 files changed

Lines changed: 33 additions & 10 deletions

File tree

doc/api/crypto.md

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5842,12 +5842,20 @@ internally — `digest` is expected to be a pre-computed hash digest.
58425842
The interpretation of `algorithm` and `digest` depends on the key type:
58435843

58445844
* RSA, ECDSA, DSA: `algorithm` identifies the hash function used to create
5845-
`digest`.
5846-
* Ed25519, Ed448: `algorithm` must be `null` or `undefined`. `digest` must
5845+
`digest`. The resulting signatures are compatible with [`crypto.verify()`][]
5846+
and signatures produced by [`crypto.sign()`][] can be verified with
5847+
[`crypto.verifyDigest()`][].
5848+
* Ed25519, Ed448: `algorithm` must be `null` or `undefined`. These keys
5849+
use the Ed25519ph and Ed448ph prehash variants respectively. `digest` must
58475850
be the output of the appropriate prehash function (SHA-512 for Ed25519ph,
5848-
SHAKE256 with 64-byte output for Ed448ph).
5851+
SHAKE256 with 64-byte output for Ed448ph). The resulting signatures are
5852+
not compatible with [`crypto.sign()`][] or [`crypto.verify()`][] because
5853+
those use the non-prehash Ed25519/Ed448 variants which have different domain
5854+
separation.
58495855
* ML-DSA: `algorithm` must be `null` or `undefined`. `digest` must be the
5850-
64-byte external mu value per FIPS 204.
5856+
64-byte external mu value per FIPS 204. The resulting signatures are
5857+
compatible with [`crypto.verify()`][] when the mu value is correctly computed
5858+
from the message per FIPS 204.
58515859

58525860
If `key` is not a [`KeyObject`][], this function behaves as if `key` had been
58535861
passed to [`crypto.createPrivateKey()`][]. If it is an object, the following
@@ -6043,12 +6051,20 @@ internally — `digest` is expected to be a pre-computed hash digest.
60436051
The interpretation of `algorithm` and `digest` depends on the key type:
60446052

60456053
* RSA, ECDSA, DSA: `algorithm` identifies the hash function used to create
6046-
`digest`.
6047-
* Ed25519, Ed448: `algorithm` must be `null` or `undefined`. `digest` must
6054+
`digest`. Signatures produced by [`crypto.sign()`][] can be verified with
6055+
this function, and signatures produced by [`crypto.signDigest()`][] can be
6056+
verified with [`crypto.verify()`][].
6057+
* Ed25519, Ed448: `algorithm` must be `null` or `undefined`. These keys
6058+
use the Ed25519ph and Ed448ph prehash variants respectively. `digest` must
60486059
be the output of the appropriate prehash function (SHA-512 for Ed25519ph,
6049-
SHAKE256 with 64-byte output for Ed448ph).
6060+
SHAKE256 with 64-byte output for Ed448ph). The resulting signatures are
6061+
not compatible with [`crypto.sign()`][] or [`crypto.verify()`][] because
6062+
those use the non-prehash Ed25519/Ed448 variants which have different domain
6063+
separation.
60506064
* ML-DSA: `algorithm` must be `null` or `undefined`. `digest` must be the
6051-
64-byte external mu value per FIPS 204.
6065+
64-byte external mu value per FIPS 204. Signatures produced by
6066+
[`crypto.sign()`][] can be verified with this function when the mu value is
6067+
correctly computed from the message per FIPS 204.
60526068

60536069
If `key` is not a [`KeyObject`][], this function behaves as if `key` had been
60546070
passed to [`crypto.createPublicKey()`][]. If it is an object, the following
@@ -6705,7 +6721,9 @@ See the [list of SSL OP Flags][] for details.
67056721
[`crypto.randomBytes()`]: #cryptorandombytessize-callback
67066722
[`crypto.randomFill()`]: #cryptorandomfillbuffer-offset-size-callback
67076723
[`crypto.sign()`]: #cryptosignalgorithm-data-key-callback
6724+
[`crypto.signDigest()`]: #cryptosigndigestalgorithm-digest-key-callback
67086725
[`crypto.verify()`]: #cryptoverifyalgorithm-data-key-signature-callback
6726+
[`crypto.verifyDigest()`]: #cryptoverifydigestalgorithm-digest-key-signature-callback
67096727
[`crypto.webcrypto.getRandomValues()`]: webcrypto.md#cryptogetrandomvaluestypedarray
67106728
[`crypto.webcrypto.subtle`]: webcrypto.md#class-subtlecrypto
67116729
[`decipher.final()`]: #decipherfinaloutputencoding

test/parallel/test-crypto-sign-verify-digest.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,9 @@ if (hasOpenSSL(3, 2)) {
248248
const wrongDigest = crypto.createHash('sha512').update(Buffer.from('wrong')).digest();
249249
assert.strictEqual(crypto.verifyDigest(null, wrongDigest, pubKey, sig), false);
250250

251-
// Note: Ed25519ph signatures are NOT compatible with Ed25519 signatures
252-
// (crypto.sign(null, data, privKey)), so no cross-verify with crypto.sign.
251+
// Ed25519ph signatures are NOT compatible with Ed25519 signatures.
252+
// Different domain separation means cross-verify must fail.
253+
assert.strictEqual(crypto.verify(null, data, pubKey, sig), false);
253254

254255
// KeyObject forms
255256
const privKeyObj = crypto.createPrivateKey(privKey);
@@ -292,6 +293,10 @@ if (hasOpenSSL(3, 2)) {
292293
const wrongDigest = crypto.createHash('shake256', { outputLength: 64 }).update(Buffer.from('wrong')).digest();
293294
assert.strictEqual(crypto.verifyDigest(null, wrongDigest, pubKey, sig), false);
294295

296+
// Ed448ph signatures are NOT compatible with Ed448 signatures.
297+
// Different domain separation means cross-verify must fail.
298+
assert.strictEqual(crypto.verify(null, data, pubKey, sig), false);
299+
295300
// Ed448ph with context string
296301
{
297302
const context = Buffer.from('my context');

0 commit comments

Comments
 (0)