99 StringPrototypeSplit,
1010 StringPrototypeStartsWith,
1111 StringPrototypeToLowerCase,
12- TypedArrayPrototypeGetLength,
1312} = primordials ;
1413
1514const {
@@ -30,8 +29,10 @@ const {
3029 validateMaxBufferLength,
3130 getBufferSourceByteLength,
3231 getBufferSourceBytes,
32+ isFips,
3333 kNamedCurveAliases,
3434 numBitsToBytes,
35+ validateKmacKeyLength,
3536} = require ( 'internal/crypto/util' ) ;
3637const {
3738 converters : webidl ,
@@ -303,30 +304,39 @@ function validateCShakeOutputLength(V) {
303304 }
304305}
305306
306- function bufferSourceEqualsAscii ( V , string ) {
307- if ( getBufferSourceByteLength ( V ) !== string . length ) return false ;
308-
309- const bytes = getBufferSourceBytes ( V ) ;
310- const length = TypedArrayPrototypeGetLength ( bytes ) ;
311- for ( let i = 0 ; i < length ; i ++ ) {
312- if ( bytes [ i ] !== StringPrototypeCharCodeAt ( string , i ) ) return false ;
313- }
314- return true ;
315- }
307+ const kCShakeFunctionNames = [ 'KMAC' , 'TupleHash' , 'ParallelHash' ] ;
316308
317309function validateCShakeFunctionName ( V ) {
318- if ( getBufferSourceByteLength ( V ) === 0 ||
319- bufferSourceEqualsAscii ( V , 'KMAC' ) ||
320- bufferSourceEqualsAscii ( V , 'TupleHash' ) ||
321- bufferSourceEqualsAscii ( V , 'ParallelHash' ) ) {
322- return ;
310+ const length = getBufferSourceByteLength ( V ) ;
311+ if ( length === 0 ) return ;
312+
313+ if ( ! isFips ) {
314+ const bytes = getBufferSourceBytes ( V ) ;
315+ for ( let i = 0 ; i < kCShakeFunctionNames . length ; i ++ ) {
316+ const functionName = kCShakeFunctionNames [ i ] ;
317+ if ( length !== functionName . length ) continue ;
318+
319+ let j = 0 ;
320+ for ( ; j < length ; j ++ ) {
321+ if ( bytes [ j ] !== StringPrototypeCharCodeAt ( functionName , j ) ) break ;
322+ }
323+ if ( j === length ) return ;
324+ }
323325 }
324326
325327 throw lazyDOMException (
326328 'Unsupported CShakeParams functionName' ,
327329 'NotSupportedError' ) ;
328330}
329331
332+ function validateCShakeCustomization ( V ) {
333+ if ( isFips && getBufferSourceByteLength ( V ) !== 0 )
334+ throw lazyDOMException (
335+ 'Unsupported CShakeParams customization' ,
336+ 'NotSupportedError' ) ;
337+ validateMaxBufferLength ( V , 'CShakeParams.customization' , 512 ) ;
338+ }
339+
330340converters . RsaPssParams = createDictionaryConverter (
331341 'RsaPssParams' , [
332342 dictAlgorithm ,
@@ -484,7 +494,7 @@ converters.CShakeParams = createDictionaryConverter(
484494 {
485495 key : 'customization' ,
486496 converter : converters . BufferSource ,
487- validator : ( V , opts ) => validateMaxBufferLength ( V , 'CShakeParams.customization' , 512 ) ,
497+ validator : validateCShakeCustomization ,
488498 } ,
489499 ] ,
490500 ] ) ;
@@ -782,6 +792,7 @@ for (let i = 0; i < kKmacDictionaries.length; i++) {
782792 key : 'length' ,
783793 converter : ( V , opts ) =>
784794 converters [ 'unsigned long' ] ( V , enforceRangeOptions ( opts ) ) ,
795+ validator : validateKmacKeyLength ,
785796 } ,
786797 ] ,
787798 ] ) ;
@@ -795,6 +806,12 @@ converters.KmacParams = createDictionaryConverter(
795806 key : 'outputLength' ,
796807 converter : ( V , opts ) =>
797808 converters [ 'unsigned long' ] ( V , enforceRangeOptions ( opts ) ) ,
809+ validator : ( V ) => {
810+ if ( ( V === 0 || V % 8 ) && isFips )
811+ throw lazyDOMException (
812+ 'Invalid KmacParams outputLength' ,
813+ 'NotSupportedError' ) ;
814+ } ,
798815 required : true ,
799816 } ,
800817 {
0 commit comments