99 StringPrototypeSplit,
1010 StringPrototypeStartsWith,
1111 StringPrototypeToLowerCase,
12- TypedArrayPrototypeGetLength,
1312} = primordials ;
1413
1514const {
@@ -27,8 +26,10 @@ const {
2726 validateMaxBufferLength,
2827 getBufferSourceByteLength,
2928 getBufferSourceBytes,
29+ isFips,
3030 kNamedCurveAliases,
3131 numBitsToBytes,
32+ validateKmacKeyLength,
3233} = require ( 'internal/crypto/util' ) ;
3334const {
3435 converters : webidl ,
@@ -252,30 +253,39 @@ function validateCShakeOutputLength(V) {
252253 }
253254}
254255
255- function bufferSourceEqualsAscii ( V , string ) {
256- if ( getBufferSourceByteLength ( V ) !== string . length ) return false ;
257-
258- const bytes = getBufferSourceBytes ( V ) ;
259- const length = TypedArrayPrototypeGetLength ( bytes ) ;
260- for ( let i = 0 ; i < length ; i ++ ) {
261- if ( bytes [ i ] !== StringPrototypeCharCodeAt ( string , i ) ) return false ;
262- }
263- return true ;
264- }
256+ const kCShakeFunctionNames = [ 'KMAC' , 'TupleHash' , 'ParallelHash' ] ;
265257
266258function validateCShakeFunctionName ( V ) {
267- if ( getBufferSourceByteLength ( V ) === 0 ||
268- bufferSourceEqualsAscii ( V , 'KMAC' ) ||
269- bufferSourceEqualsAscii ( V , 'TupleHash' ) ||
270- bufferSourceEqualsAscii ( V , 'ParallelHash' ) ) {
271- return ;
259+ const length = getBufferSourceByteLength ( V ) ;
260+ if ( length === 0 ) return ;
261+
262+ if ( ! isFips ) {
263+ const bytes = getBufferSourceBytes ( V ) ;
264+ for ( let i = 0 ; i < kCShakeFunctionNames . length ; i ++ ) {
265+ const functionName = kCShakeFunctionNames [ i ] ;
266+ if ( length !== functionName . length ) continue ;
267+
268+ let j = 0 ;
269+ for ( ; j < length ; j ++ ) {
270+ if ( bytes [ j ] !== StringPrototypeCharCodeAt ( functionName , j ) ) break ;
271+ }
272+ if ( j === length ) return ;
273+ }
272274 }
273275
274276 throw lazyDOMException (
275277 'Unsupported CShakeParams functionName' ,
276278 'NotSupportedError' ) ;
277279}
278280
281+ function validateCShakeCustomization ( V ) {
282+ if ( isFips && getBufferSourceByteLength ( V ) !== 0 )
283+ throw lazyDOMException (
284+ 'Unsupported CShakeParams customization' ,
285+ 'NotSupportedError' ) ;
286+ validateMaxBufferLength ( V , 'CShakeParams.customization' , 512 ) ;
287+ }
288+
279289converters . RsaPssParams = createDictionaryConverter (
280290 'RsaPssParams' , [
281291 dictAlgorithm ,
@@ -433,7 +443,7 @@ converters.CShakeParams = createDictionaryConverter(
433443 {
434444 key : 'customization' ,
435445 converter : converters . BufferSource ,
436- validator : ( V , opts ) => validateMaxBufferLength ( V , 'CShakeParams.customization' , 512 ) ,
446+ validator : validateCShakeCustomization ,
437447 } ,
438448 ] ,
439449 ] ) ;
@@ -719,6 +729,7 @@ for (let i = 0; i < kKmacDictionaries.length; i++) {
719729 key : 'length' ,
720730 converter : ( V , opts ) =>
721731 converters [ 'unsigned long' ] ( V , enforceRangeOptions ( opts ) ) ,
732+ validator : validateKmacKeyLength ,
722733 } ,
723734 ] ,
724735 ] ) ;
@@ -732,6 +743,12 @@ converters.KmacParams = createDictionaryConverter(
732743 key : 'outputLength' ,
733744 converter : ( V , opts ) =>
734745 converters [ 'unsigned long' ] ( V , enforceRangeOptions ( opts ) ) ,
746+ validator : ( V ) => {
747+ if ( ( V === 0 || V % 8 ) && isFips )
748+ throw lazyDOMException (
749+ 'Invalid KmacParams outputLength' ,
750+ 'NotSupportedError' ) ;
751+ } ,
735752 required : true ,
736753 } ,
737754 {
0 commit comments