99 StringPrototypeSplit,
1010 StringPrototypeStartsWith,
1111 StringPrototypeToLowerCase,
12- TypedArrayPrototypeGetLength,
1312} = primordials ;
1413
1514const {
@@ -28,8 +27,10 @@ const {
2827 validateMaxBufferLength,
2928 getBufferSourceByteLength,
3029 getBufferSourceBytes,
30+ isFips,
3131 kNamedCurveAliases,
3232 numBitsToBytes,
33+ validateKmacKeyLength,
3334} = require ( 'internal/crypto/util' ) ;
3435const {
3536 converters : webidl ,
@@ -276,30 +277,39 @@ function validateCShakeOutputLength(V) {
276277 }
277278}
278279
279- function bufferSourceEqualsAscii ( V , string ) {
280- if ( getBufferSourceByteLength ( V ) !== string . length ) return false ;
281-
282- const bytes = getBufferSourceBytes ( V ) ;
283- const length = TypedArrayPrototypeGetLength ( bytes ) ;
284- for ( let i = 0 ; i < length ; i ++ ) {
285- if ( bytes [ i ] !== StringPrototypeCharCodeAt ( string , i ) ) return false ;
286- }
287- return true ;
288- }
280+ const kCShakeFunctionNames = [ 'KMAC' , 'TupleHash' , 'ParallelHash' ] ;
289281
290282function validateCShakeFunctionName ( V ) {
291- if ( getBufferSourceByteLength ( V ) === 0 ||
292- bufferSourceEqualsAscii ( V , 'KMAC' ) ||
293- bufferSourceEqualsAscii ( V , 'TupleHash' ) ||
294- bufferSourceEqualsAscii ( V , 'ParallelHash' ) ) {
295- return ;
283+ const length = getBufferSourceByteLength ( V ) ;
284+ if ( length === 0 ) return ;
285+
286+ if ( ! isFips ) {
287+ const bytes = getBufferSourceBytes ( V ) ;
288+ for ( let i = 0 ; i < kCShakeFunctionNames . length ; i ++ ) {
289+ const functionName = kCShakeFunctionNames [ i ] ;
290+ if ( length !== functionName . length ) continue ;
291+
292+ let j = 0 ;
293+ for ( ; j < length ; j ++ ) {
294+ if ( bytes [ j ] !== StringPrototypeCharCodeAt ( functionName , j ) ) break ;
295+ }
296+ if ( j === length ) return ;
297+ }
296298 }
297299
298300 throw lazyDOMException (
299301 'Unsupported CShakeParams functionName' ,
300302 'NotSupportedError' ) ;
301303}
302304
305+ function validateCShakeCustomization ( V ) {
306+ if ( isFips && getBufferSourceByteLength ( V ) !== 0 )
307+ throw lazyDOMException (
308+ 'Unsupported CShakeParams customization' ,
309+ 'NotSupportedError' ) ;
310+ validateMaxBufferLength ( V , 'CShakeParams.customization' , 512 ) ;
311+ }
312+
303313converters . RsaPssParams = createDictionaryConverter (
304314 'RsaPssParams' , [
305315 dictAlgorithm ,
@@ -457,7 +467,7 @@ converters.CShakeParams = createDictionaryConverter(
457467 {
458468 key : 'customization' ,
459469 converter : converters . BufferSource ,
460- validator : ( V , opts ) => validateMaxBufferLength ( V , 'CShakeParams.customization' , 512 ) ,
470+ validator : validateCShakeCustomization ,
461471 } ,
462472 ] ,
463473 ] ) ;
@@ -743,6 +753,7 @@ for (let i = 0; i < kKmacDictionaries.length; i++) {
743753 key : 'length' ,
744754 converter : ( V , opts ) =>
745755 converters [ 'unsigned long' ] ( V , enforceRangeOptions ( opts ) ) ,
756+ validator : validateKmacKeyLength ,
746757 } ,
747758 ] ,
748759 ] ) ;
@@ -756,6 +767,12 @@ converters.KmacParams = createDictionaryConverter(
756767 key : 'outputLength' ,
757768 converter : ( V , opts ) =>
758769 converters [ 'unsigned long' ] ( V , enforceRangeOptions ( opts ) ) ,
770+ validator : ( V ) => {
771+ if ( ( V === 0 || V % 8 ) && isFips )
772+ throw lazyDOMException (
773+ 'Invalid KmacParams outputLength' ,
774+ 'NotSupportedError' ) ;
775+ } ,
759776 required : true ,
760777 } ,
761778 {
0 commit comments