Skip to content

Commit 1a2b5cc

Browse files
panvaaduh95
authored andcommitted
crypto: use current FIPS state for availability
Refresh conditional algorithm registration on FIPS changes and use the current state for parameter restrictions. Use per-algorithm availability checks in normalization and native named key generation. Remove getPqcKeyTypes(). Enable supports() tests under FIPS and cover state transitions in warmed workers. Signed-off-by: Filip Skokan <panva.ip@gmail.com> Assisted-by: Codex PR-URL: #66160 Backport-PR-URL: #66970 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent 81162db commit 1a2b5cc

42 files changed

Lines changed: 607 additions & 405 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎deps/ncrypto/ncrypto.cc‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3258,12 +3258,6 @@ const KeyAlgorithm* KeyAlgorithm::FromName(const char* name) {
32583258
return nullptr;
32593259
}
32603260

3261-
void KeyAlgorithm::ForEachPqc(Callback callback) {
3262-
for (const auto* algorithm : kKeyAlgorithms) {
3263-
if (algorithm->isPqc() && algorithm->isAvailable()) callback(*algorithm);
3264-
}
3265-
}
3266-
32673261
bool KeyAlgorithm::isRsa() const {
32683262
return this == &RSA || this == &RSA_PSS;
32693263
}

‎deps/ncrypto/ncrypto.h‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1121,8 +1121,6 @@ class KeyAlgorithm final {
11211121
// Look up a canonical name case-insensitively, including unavailable
11221122
// algorithms.
11231123
static const KeyAlgorithm* FromName(const char* name);
1124-
using Callback = std::function<void(const KeyAlgorithm&)>;
1125-
static void ForEachPqc(Callback callback);
11261124

11271125
const char* name() const { return name_; }
11281126
const char* keyTypeName() const {

‎lib/internal/crypto/keygen.js‎

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@ const {
44
FunctionPrototypeCall,
55
ObjectDefineProperty,
66
SafeArrayIterator,
7-
StringPrototypeToLowerCase,
87
} = primordials;
98

109
const {
1110
DhKeyPairGenJob,
1211
DsaKeyPairGenJob,
1312
EcKeyPairGenJob,
1413
NamedKeyPairGenJob,
15-
getPqcKeyTypes,
1614
RsaKeyPairGenJob,
1715
SecretKeyGenJob,
1816
kCryptoJobAsync,
@@ -160,17 +158,6 @@ function parseKeyEncoding(keyType, options = kEmptyObject) {
160158
];
161159
}
162160

163-
const namedKeyPairs = {
164-
'__proto__': null,
165-
'ed25519': 'Ed25519',
166-
'ed448': 'Ed448',
167-
'x25519': 'X25519',
168-
'x448': 'X448',
169-
};
170-
for (const name of new SafeArrayIterator(getPqcKeyTypes())) {
171-
namedKeyPairs[StringPrototypeToLowerCase(name)] = name;
172-
}
173-
174161
function createJob(mode, type, options) {
175162
validateString(type, 'type');
176163

@@ -339,12 +326,8 @@ function createJob(mode, type, options) {
339326
generator == null ? 2 : generator,
340327
...encoding);
341328
}
342-
default: {
343-
if (namedKeyPairs[type] === undefined) {
344-
throw new ERR_INVALID_ARG_VALUE('type', type, 'must be a supported key type');
345-
}
346-
return new NamedKeyPairGenJob(mode, namedKeyPairs[type], ...encoding);
347-
}
329+
default:
330+
return new NamedKeyPairGenJob(mode, type, ...encoding);
348331
}
349332
}
350333

‎lib/internal/crypto/util.js‎

Lines changed: 95 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,19 @@ const {
5050
getFipsCrypto,
5151
getFipsCryptoGeneration,
5252
KmacJob,
53-
getPqcKeyTypes,
53+
isKeyAlgorithmAvailable,
5454
} = internalBinding('crypto');
5555

56-
const isFips = getFipsCrypto() === 1;
56+
let fips;
57+
let fipsGeneration;
58+
function isFips() {
59+
const generation = getFipsCryptoGeneration();
60+
if (fipsGeneration !== generation) {
61+
fips = getFipsCrypto() === 1;
62+
fipsGeneration = generation;
63+
}
64+
return fips;
65+
}
5766

5867
const { getOptionValue } = require('internal/options');
5968

@@ -122,6 +131,9 @@ let _hashCache;
122131
let _macCache;
123132
if (isBuildingSnapshot()) {
124133
addSerializeCallback(() => {
134+
fips = undefined;
135+
fipsGeneration = undefined;
136+
supportedAlgorithmsGeneration = undefined;
125137
_hashCache = undefined;
126138
_macCache = undefined;
127139
});
@@ -491,53 +503,69 @@ const kAlgorithmDefinitions = {
491503
},
492504
};
493505

494-
// Conditionally supported algorithms
495-
const pqcKeyTypes = getPqcKeyTypes();
496-
497-
const conditionalAlgorithms = {
498-
'AES-OCB': !!hasAesOcbMode,
499-
'Argon2d': !!Argon2Job,
500-
'Argon2i': !!Argon2Job,
501-
'Argon2id': !!Argon2Job,
502-
'ChaCha20-Poly1305': process.features.openssl_is_boringssl ||
503-
ArrayPrototypeIncludes(getCiphers(), 'chacha20-poly1305'),
504-
'cSHAKE128': !process.features.openssl_is_boringssl ||
505-
ArrayPrototypeIncludes(getHashes(), 'shake128'),
506-
'cSHAKE256': !process.features.openssl_is_boringssl ||
507-
ArrayPrototypeIncludes(getHashes(), 'shake256'),
508-
'Ed448': !process.features.openssl_is_boringssl,
509-
'KMAC128': !!KmacJob,
510-
'KMAC256': !!KmacJob,
511-
'KT128': !isFips,
512-
'KT256': !isFips,
513-
'ML-DSA-44': ArrayPrototypeIncludes(pqcKeyTypes, 'ML-DSA-44'),
514-
'ML-DSA-65': ArrayPrototypeIncludes(pqcKeyTypes, 'ML-DSA-65'),
515-
'ML-DSA-87': ArrayPrototypeIncludes(pqcKeyTypes, 'ML-DSA-87'),
516-
'ML-KEM-512': ArrayPrototypeIncludes(pqcKeyTypes, 'ML-KEM-512'),
517-
'ML-KEM-768': ArrayPrototypeIncludes(pqcKeyTypes, 'ML-KEM-768'),
518-
'ML-KEM-1024': ArrayPrototypeIncludes(pqcKeyTypes, 'ML-KEM-1024'),
519-
'MLKEM768-P256': !isFips && ArrayPrototypeIncludes(pqcKeyTypes, 'ML-KEM-768') &&
520-
(!process.features.openssl_is_boringssl ||
521-
(ArrayPrototypeIncludes(getHashes(), 'sha3-256') &&
522-
ArrayPrototypeIncludes(getHashes(), 'shake256'))),
523-
'MLKEM768-X25519': !isFips && ArrayPrototypeIncludes(pqcKeyTypes, 'ML-KEM-768') &&
524-
(!process.features.openssl_is_boringssl ||
525-
(ArrayPrototypeIncludes(getHashes(), 'sha3-256') &&
526-
ArrayPrototypeIncludes(getHashes(), 'shake256'))),
527-
'MLKEM1024-P384': !isFips && ArrayPrototypeIncludes(pqcKeyTypes, 'ML-KEM-1024') &&
528-
(!process.features.openssl_is_boringssl ||
529-
(ArrayPrototypeIncludes(getHashes(), 'sha3-256') &&
530-
ArrayPrototypeIncludes(getHashes(), 'shake256'))),
531-
'SHA3-256': !process.features.openssl_is_boringssl ||
532-
ArrayPrototypeIncludes(getHashes(), 'sha3-256'),
533-
'SHA3-384': !process.features.openssl_is_boringssl ||
534-
ArrayPrototypeIncludes(getHashes(), 'sha3-384'),
535-
'SHA3-512': !process.features.openssl_is_boringssl ||
536-
ArrayPrototypeIncludes(getHashes(), 'sha3-512'),
537-
'TurboSHAKE128': !isFips,
538-
'TurboSHAKE256': !isFips,
539-
'X448': !process.features.openssl_is_boringssl,
540-
};
506+
function has(algorithms, name) {
507+
return ArrayPrototypeIncludes(algorithms, name);
508+
}
509+
510+
// Re-evaluated when the active FIPS state changes.
511+
function getConditionalAlgorithms() {
512+
const mlKem768 = isKeyAlgorithmAvailable('ML-KEM-768');
513+
const mlKem1024 = isKeyAlgorithmAvailable('ML-KEM-1024');
514+
const ciphers = getCiphers();
515+
const hashes = getHashes();
516+
const macs = getMacs();
517+
518+
const fips = isFips();
519+
520+
return {
521+
'AES-OCB': !!hasAesOcbMode &&
522+
(has(ciphers, 'aes-128-ocb') ||
523+
has(ciphers, 'aes-192-ocb') ||
524+
has(ciphers, 'aes-256-ocb')),
525+
'Argon2d': !!Argon2Job && !fips,
526+
'Argon2i': !!Argon2Job && !fips,
527+
'Argon2id': !!Argon2Job && !fips,
528+
'ChaCha20-Poly1305': process.features.openssl_is_boringssl ||
529+
has(ciphers, 'chacha20-poly1305'),
530+
'cSHAKE128': has(hashes, 'shake128'),
531+
'cSHAKE256': has(hashes, 'shake256'),
532+
'Ed25519': isKeyAlgorithmAvailable('Ed25519'),
533+
'Ed448': isKeyAlgorithmAvailable('Ed448'),
534+
'KMAC128': !!KmacJob && has(macs, 'kmac128'),
535+
'KMAC256': !!KmacJob && has(macs, 'kmac256'),
536+
'KT128': !fips,
537+
'KT256': !fips,
538+
'ML-DSA-44': isKeyAlgorithmAvailable('ML-DSA-44'),
539+
'ML-DSA-65': isKeyAlgorithmAvailable('ML-DSA-65'),
540+
'ML-DSA-87': isKeyAlgorithmAvailable('ML-DSA-87'),
541+
'ML-KEM-512': isKeyAlgorithmAvailable('ML-KEM-512'),
542+
'ML-KEM-768': mlKem768,
543+
'ML-KEM-1024': mlKem1024,
544+
'MLKEM768-P256': !fips && mlKem768 &&
545+
(!process.features.openssl_is_boringssl ||
546+
(has(hashes, 'sha3-256') &&
547+
has(hashes, 'shake256'))),
548+
'MLKEM768-X25519': !fips && mlKem768 &&
549+
(!process.features.openssl_is_boringssl ||
550+
(has(hashes, 'sha3-256') &&
551+
has(hashes, 'shake256'))),
552+
'MLKEM1024-P384': !fips && mlKem1024 &&
553+
(!process.features.openssl_is_boringssl ||
554+
(has(hashes, 'sha3-256') &&
555+
has(hashes, 'shake256'))),
556+
'SHA-1': has(hashes, 'sha1'),
557+
'SHA-256': has(hashes, 'sha256'),
558+
'SHA-384': has(hashes, 'sha384'),
559+
'SHA-512': has(hashes, 'sha512'),
560+
'SHA3-256': has(hashes, 'sha3-256'),
561+
'SHA3-384': has(hashes, 'sha3-384'),
562+
'SHA3-512': has(hashes, 'sha3-512'),
563+
'TurboSHAKE128': !fips,
564+
'TurboSHAKE256': !fips,
565+
'X25519': isKeyAlgorithmAvailable('X25519'),
566+
'X448': isKeyAlgorithmAvailable('X448'),
567+
};
568+
}
541569

542570
// Experimental algorithms that emit warnings.
543571
const experimentalAlgorithmsWithWarnings = [
@@ -575,6 +603,7 @@ const experimentalAlgorithmsWithWarnings = [
575603
// Also builds a parallel Map<UPPERCASED_NAME, canonicalName> per operation
576604
// for O(1) case-insensitive algorithm name lookup in normalizeAlgorithm.
577605
function createSupportedAlgorithms(algorithmDefs) {
606+
const conditionalAlgorithms = getConditionalAlgorithms();
578607
// Detached below rather than declared `__proto__: null`: V8 puts that
579608
// literal form in dictionary mode, slowing every registry lookup.
580609
const result = {};
@@ -622,8 +651,16 @@ function createSupportedAlgorithms(algorithmDefs) {
622651
return { algorithms: result, nameMap };
623652
}
624653

625-
const { algorithms: kSupportedAlgorithms, nameMap: kAlgorithmNameMap } =
626-
createSupportedAlgorithms(kAlgorithmDefinitions);
654+
let supportedAlgorithms;
655+
let supportedAlgorithmsGeneration;
656+
function getSupportedAlgorithms() {
657+
const generation = getFipsCryptoGeneration();
658+
if (supportedAlgorithmsGeneration !== generation) {
659+
supportedAlgorithms = createSupportedAlgorithms(kAlgorithmDefinitions);
660+
supportedAlgorithmsGeneration = generation;
661+
}
662+
return supportedAlgorithms;
663+
}
627664

628665
const simpleAlgorithmDictionaries = {
629666
AesCbcParams: { iv: 'BufferSource' },
@@ -685,7 +722,7 @@ function validateMaxBufferLength(data, name, max = kMaxBufferLength) {
685722
}
686723

687724
function validateKmacKeyLength(length) {
688-
if ((length < 32 || length % 8) && isFips)
725+
if ((length < 32 || length % 8) && isFips())
689726
throw lazyDOMException('Invalid key length', 'NotSupportedError');
690727
}
691728

@@ -745,16 +782,16 @@ function normalizeAlgorithm(algorithm, op) {
745782

746783
webidl ??= require('internal/crypto/webidl');
747784

748-
// 1.
749-
const registeredAlgorithms = kSupportedAlgorithms[op];
750785
// 2. 3.
751786
const initialAlg = webidl.converters.Algorithm(algorithm,
752787
kNormalizeAlgorithmOpts);
788+
const { algorithms, nameMap } = getSupportedAlgorithms();
789+
const registeredAlgorithms = algorithms[op];
753790
// 4.
754791
let algName = initialAlg.name;
755792

756793
// 5. Case-insensitive lookup via pre-built Map (O(1) instead of O(n)).
757-
const canonicalName = kAlgorithmNameMap[op]?.get(
794+
const canonicalName = nameMap[op]?.get(
758795
StringPrototypeToUpperCase(algName));
759796
if (canonicalName === undefined)
760797
throw lazyDOMException('Unrecognized algorithm name', 'NotSupportedError');
@@ -1229,7 +1266,9 @@ module.exports = {
12291266
toBuf,
12301267

12311268
kNamedCurveAliases,
1232-
kSupportedAlgorithms,
1269+
get kSupportedAlgorithms() {
1270+
return getSupportedAlgorithms().algorithms;
1271+
},
12331272
isFips,
12341273
normalizeAlgorithm,
12351274
normalizeHashName,

‎lib/internal/crypto/webcrypto.js‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1775,7 +1775,6 @@ class SubtleCrypto {
17751775
}
17761776

17771777
// Implements https://wicg.github.io/webcrypto-modern-algos/#SubtleCrypto-method-supports
1778-
// TODO(panva): Make supports() account for the active FIPS state.
17791778
static supports(operation, algorithm, lengthOrAdditionalAlgorithm = null) {
17801779
webidl ??= require('internal/crypto/webidl');
17811780
const prefix = "Failed to execute 'supports' on 'SubtleCrypto'";

‎lib/internal/crypto/webidl.js‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,6 @@ const {
4242
type,
4343
} = require('internal/webidl');
4444

45-
const { getFipsCrypto } = internalBinding('crypto');
46-
const kRsaKeyGenMinimumModulusLength = getFipsCrypto() === 1 ? 2048 : 512;
47-
4845
function validateByteLength(buf, name, target) {
4946
if (getBufferSourceByteLength(buf) !== target) {
5047
throw lazyDOMException(
@@ -168,6 +165,7 @@ const dictRsaKeyGenParams = [
168165
converter: (V, opts) =>
169166
converters['unsigned long'](V, enforceRangeOptions(opts)),
170167
validator: (modulusLength) => {
168+
const kRsaKeyGenMinimumModulusLength = isFips() ? 2048 : 512;
171169
if (modulusLength < kRsaKeyGenMinimumModulusLength) {
172170
throw lazyDOMException(
173171
`algorithm.modulusLength must be at least ${kRsaKeyGenMinimumModulusLength}`,
@@ -294,7 +292,7 @@ function validateCShakeFunctionName(V) {
294292
const length = getBufferSourceByteLength(V);
295293
if (length === 0) return;
296294

297-
if (!isFips) {
295+
if (!isFips()) {
298296
const bytes = getBufferSourceBytes(V);
299297
for (let i = 0; i < kCShakeFunctionNames.length; i++) {
300298
const functionName = kCShakeFunctionNames[i];
@@ -314,7 +312,7 @@ function validateCShakeFunctionName(V) {
314312
}
315313

316314
function validateCShakeCustomization(V) {
317-
if (isFips && getBufferSourceByteLength(V) !== 0)
315+
if (isFips() && getBufferSourceByteLength(V) !== 0)
318316
throw lazyDOMException(
319317
'Unsupported CShakeParams customization',
320318
'NotSupportedError');
@@ -778,7 +776,7 @@ converters.KmacParams = createDictionaryConverter(
778776
converter: (V, opts) =>
779777
converters['unsigned long'](V, enforceRangeOptions(opts)),
780778
validator: (V) => {
781-
if ((V === 0 || V % 8) && isFips)
779+
if ((V === 0 || V % 8) && isFips())
782780
throw lazyDOMException(
783781
'Invalid KmacParams outputLength',
784782
'NotSupportedError');

‎node.gyp‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,6 @@
394394
'src/crypto/crypto_cipher.cc',
395395
'src/crypto/crypto_context.cc',
396396
'src/crypto/crypto_ec.cc',
397-
'src/crypto/crypto_pqc.cc',
398397
'src/crypto/crypto_kem.cc',
399398
'src/crypto/crypto_hmac.cc',
400399
'src/crypto/crypto_kmac.cc',
@@ -433,7 +432,6 @@
433432
'src/crypto/crypto_tls.h',
434433
'src/crypto/crypto_context.h',
435434
'src/crypto/crypto_ec.h',
436-
'src/crypto/crypto_pqc.h',
437435
'src/crypto/crypto_hkdf.h',
438436
'src/crypto/crypto_pbkdf2.h',
439437
'src/crypto/crypto_sig.h',

‎src/crypto/README.md‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ following table:
4646
| `crypto_keys` | Utilities for using and generating secret, private, and public keys. |
4747
| `crypto_mac` | Provider-generic MAC implementations. |
4848
| `crypto_pbkdf2` | PBKDF2 key / bit generation implementation. |
49-
| `crypto_pqc` | Post-quantum algorithm enumeration. |
5049
| `crypto_rsa` | RSA Key Generation functions. |
5150
| `crypto_scrypt` | Scrypt key / bit generation implementation. |
5251
| `crypto_sig` | General digital signature and verification utilities. |
@@ -205,10 +204,11 @@ Public input validation remains specific to each API: PQC JWK `alg` values use
205204
exact canonical names such as `ML-DSA-44`, while raw imports require exact public
206205
`asymmetricKeyType` values such as `ml-dsa-44`.
207206

208-
The internal JavaScript binding exposes `getPqcKeyTypes()` for the available
209-
known PQC algorithm names in their canonical spelling. Named key generation
210-
passes algorithm names to `NamedKeyPairGenJob`, which resolves the name to a static
211-
`KeyAlgorithm` descriptor. Asymmetric key IDs are not exposed to JavaScript.
207+
The internal JavaScript binding exposes `isKeyAlgorithmAvailable()` to check
208+
whether a known key algorithm is available from the current backend. Named key
209+
generation passes algorithm names to `NamedKeyPairGenJob`, which resolves the
210+
name to a static `KeyAlgorithm` descriptor. Asymmetric key IDs are not exposed to
211+
JavaScript.
212212

213213
Real EC curve and ASN.1/OID NIDs still have their own uses. The EC generation
214214
path keeps Ed/X algorithm descriptors separate from curve NIDs while preserving

0 commit comments

Comments
 (0)