@@ -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
5867const { getOptionValue } = require ( 'internal/options' ) ;
5968
@@ -122,6 +131,9 @@ let _hashCache;
122131let _macCache ;
123132if ( 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.
543571const 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.
577605function 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
628665const simpleAlgorithmDictionaries = {
629666 AesCbcParams : { iv : 'BufferSource' } ,
@@ -685,7 +722,7 @@ function validateMaxBufferLength(data, name, max = kMaxBufferLength) {
685722}
686723
687724function 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,
0 commit comments