Skip to content

Commit 07cfb14

Browse files
committed
crypto: wire up SLH-DSA with BoringSSL
Map SLH-DSA-SHA2-128s and SLH-DSA-SHAKE-256f to the native key IDs and raw-key descriptors for key generation, raw and JWK operations, and one-shot signatures with contexts. Run the existing cases for these variants with JWK fixtures on BoringSSL, whose generic key parsers omit SLH-DSA. Signed-off-by: Filip Skokan <panva.ip@gmail.com> Assisted-by: Codex
1 parent fb0a576 commit 07cfb14

4 files changed

Lines changed: 43 additions & 25 deletions

File tree

‎deps/ncrypto/ncrypto.cc‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3636,6 +3636,8 @@ const BoringSSLKeyAlgorithm kBoringSSLKeyAlgorithms[] = {
36363636
{KeyAlgorithm::ML_DSA_87.name(), EVP_PKEY_ML_DSA_87, EVP_pkey_ml_dsa_87},
36373637
{KeyAlgorithm::ML_KEM_768.name(), EVP_PKEY_ML_KEM_768, EVP_pkey_ml_kem_768},
36383638
{KeyAlgorithm::ML_KEM_1024.name(), EVP_PKEY_ML_KEM_1024, EVP_pkey_ml_kem_1024},
3639+
{KeyAlgorithm::SLH_DSA_SHA2_128S.name(), EVP_PKEY_SLH_DSA_SHA2_128S, EVP_pkey_slh_dsa_sha2_128s},
3640+
{KeyAlgorithm::SLH_DSA_SHAKE_256F.name(), EVP_PKEY_SLH_DSA_SHAKE_256F, EVP_pkey_slh_dsa_shake_256f},
36393641
};
36403642
// NOLINTEND(whitespace/line_length)
36413643
// clang-format on

‎test/parallel/test-crypto-pqc-key-objects-slh-dsa.js‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,21 @@ for (const asymmetricKeyType of [
107107
assert.throws(() => createPrivateKey(keys.private), {
108108
code: isBoringSSL ? 'ERR_OSSL_EVP_UNSUPPORTED_ALGORITHM' : 'ERR_OSSL_UNSUPPORTED',
109109
});
110-
} else {
111-
const publicKey = createPublicKey(keys.public);
110+
}
111+
112+
const supported = hasOpenSSL(3, 5) ||
113+
(isBoringSSL && (asymmetricKeyType === 'slh-dsa-sha2-128s' ||
114+
asymmetricKeyType === 'slh-dsa-shake-256f'));
115+
if (supported) {
116+
const publicKey = createPublicKey(isBoringSSL ?
117+
{ format: 'jwk', key: keys.jwk } : keys.public);
118+
const privateKey = createPrivateKey(isBoringSSL ?
119+
{ format: 'jwk', key: keys.jwk } : keys.private);
112120
assertPublicKey(publicKey);
113121

114-
const pubFromPriv = createPublicKey(keys.private);
122+
const pubFromPriv = createPublicKey(isBoringSSL ? privateKey : keys.private);
115123
assertPublicKey(pubFromPriv);
116-
assertPrivateKey(createPrivateKey(keys.private));
124+
assertPrivateKey(privateKey);
117125
assert.strictEqual(pubFromPriv.equals(publicKey), true);
118126

119127
// JWK import error tests

‎test/parallel/test-crypto-pqc-keygen-slh-dsa.js‎

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,27 @@ const common = require('../common');
44
if (!common.hasCrypto)
55
common.skip('missing crypto');
66

7-
const { hasOpenSSL } = require('../common/crypto');
7+
const { hasOpenSSL, isBoringSSL } = require('../common/crypto');
88

99
const assert = require('assert');
1010
const {
1111
generateKeyPair,
1212
} = require('crypto');
1313

14-
if (!hasOpenSSL(3, 5)) {
15-
for (const asymmetricKeyType of [
16-
'slh-dsa-sha2-128f', 'slh-dsa-sha2-128s', 'slh-dsa-sha2-192f', 'slh-dsa-sha2-192s',
17-
'slh-dsa-sha2-256f', 'slh-dsa-sha2-256s', 'slh-dsa-shake-128f', 'slh-dsa-shake-128s',
18-
'slh-dsa-shake-192f', 'slh-dsa-shake-192s', 'slh-dsa-shake-256f', 'slh-dsa-shake-256s',
19-
]) {
14+
for (const asymmetricKeyType of [
15+
'slh-dsa-sha2-128f', 'slh-dsa-sha2-128s', 'slh-dsa-sha2-192f', 'slh-dsa-sha2-192s',
16+
'slh-dsa-sha2-256f', 'slh-dsa-sha2-256s', 'slh-dsa-shake-128f', 'slh-dsa-shake-128s',
17+
'slh-dsa-shake-192f', 'slh-dsa-shake-192s', 'slh-dsa-shake-256f', 'slh-dsa-shake-256s',
18+
]) {
19+
const supported = hasOpenSSL(3, 5) ||
20+
(isBoringSSL && (asymmetricKeyType === 'slh-dsa-sha2-128s' ||
21+
asymmetricKeyType === 'slh-dsa-shake-256f'));
22+
if (!supported) {
2023
assert.throws(() => generateKeyPair(asymmetricKeyType, common.mustNotCall()), {
2124
code: 'ERR_INVALID_ARG_VALUE',
2225
message: /The argument 'type' must be a supported key type/
2326
});
24-
}
25-
} else {
26-
for (const asymmetricKeyType of [
27-
'slh-dsa-sha2-128f', 'slh-dsa-sha2-128s', 'slh-dsa-sha2-192f', 'slh-dsa-sha2-192s',
28-
'slh-dsa-sha2-256f', 'slh-dsa-sha2-256s', 'slh-dsa-shake-128f', 'slh-dsa-shake-128s',
29-
'slh-dsa-shake-192f', 'slh-dsa-shake-192s', 'slh-dsa-shake-256f', 'slh-dsa-shake-256s',
30-
]) {
31-
27+
} else {
3228
function assertJwk(jwk) {
3329
assert.strictEqual(jwk.kty, 'AKP');
3430
// SLH-DSA algorithm names keep the last character (f/s) lowercase.

‎test/pummel/test-crypto-pqc-sign-verify-slh-dsa.mjs‎

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ import * as common from '../common/index.mjs';
33
if (!common.hasCrypto)
44
common.skip('missing crypto');
55

6-
import { hasOpenSSL } from '../common/crypto.js';
6+
import { hasOpenSSL, isBoringSSL } from '../common/crypto.js';
77

8-
if (!hasOpenSSL(3, 5))
9-
common.skip('requires OpenSSL >= 3.5');
8+
if (!hasOpenSSL(3, 5) && !isBoringSSL)
9+
common.skip('requires OpenSSL >= 3.5 or BoringSSL');
1010

1111
import * as assert from 'node:assert';
1212
import { promisify } from 'node:util';
13-
import { randomBytes, sign, verify } from 'node:crypto';
13+
import { createPrivateKey, createPublicKey, randomBytes, sign, verify } from 'node:crypto';
1414
import fixtures from '../common/fixtures.js';
1515

1616
const pSign = promisify(sign);
@@ -34,10 +34,20 @@ for (const [asymmetricKeyType, sigLen] of [
3434
['slh-dsa-shake-256f', 49856],
3535
['slh-dsa-shake-256s', 29792],
3636
]) {
37+
if (isBoringSSL && asymmetricKeyType !== 'slh-dsa-sha2-128s' &&
38+
asymmetricKeyType !== 'slh-dsa-shake-256f') {
39+
common.printSkipMessage(`Skipping unsupported ${asymmetricKeyType}`);
40+
continue;
41+
}
3742
const keys = {
3843
public: fixtures.readKey(getKeyFileName(asymmetricKeyType, 'public'), 'ascii'),
3944
private: fixtures.readKey(getKeyFileName(asymmetricKeyType, 'private'), 'ascii'),
4045
};
46+
if (isBoringSSL) {
47+
const jwk = JSON.parse(fixtures.readKey(`${asymmetricKeyType}.json`));
48+
keys.public = createPublicKey({ format: 'jwk', key: jwk });
49+
keys.private = createPrivateKey({ format: 'jwk', key: jwk });
50+
}
4151

4252
const data = randomBytes(32);
4353
// Start the async signature before the sync work to overlap the two.
@@ -59,8 +69,10 @@ for (const [asymmetricKeyType, sigLen] of [
5969
assert.strictEqual(await pVerify(undefined, data, keys.public, signature), true);
6070
}
6171

62-
assert.throws(() => sign('sha256', data, keys.private), { code: 'ERR_OSSL_INVALID_DIGEST' });
72+
const code = isBoringSSL ?
73+
'ERR_OSSL_EVP_COMMAND_NOT_SUPPORTED' : 'ERR_OSSL_INVALID_DIGEST';
74+
assert.throws(() => sign('sha256', data, keys.private), { code });
6375
assert.throws(
6476
() => verify('sha256', data, keys.public, Buffer.alloc(sigLen)),
65-
{ code: 'ERR_OSSL_INVALID_DIGEST' });
77+
{ code });
6678
}

0 commit comments

Comments
 (0)