@@ -58,7 +58,6 @@ const {
5858  generateKey : _generateKey , 
5959}  =  require ( 'internal/crypto/keygen' ) ; 
6060
61- const  kMaxCounterLength  =  128 ; 
6261const  kTagLengths  =  [ 32 ,  64 ,  96 ,  104 ,  112 ,  120 ,  128 ] ; 
6362const  generateKey  =  promisify ( _generateKey ) ; 
6463
@@ -109,35 +108,43 @@ function getVariant(name, length) {
109108  } 
110109} 
111110
112- function  asyncAesCtrCipher ( mode ,   key ,   data ,   {  counter ,  length  } )  { 
113-   validateByteLength ( counter ,  'algorithm.counter' ,  16 ) ; 
111+ function  validateAesCtrAlgorithm ( algorithm )  { 
112+   validateByteLength ( algorithm . counter ,  'algorithm.counter' ,  16 ) ; 
114113  // The length must specify an integer between 1 and 128. While 
115114  // there is no default, this should typically be 64. 
116-   if  ( length  ===  0  ||  length  >  kMaxCounterLength )  { 
115+   if  ( algorithm . length  ===  0  ||  algorithm . length  >  128 )  { 
117116    throw  lazyDOMException ( 
118117      'AES-CTR algorithm.length must be between 1 and 128' , 
119118      'OperationError' ) ; 
120119  } 
120+ } 
121+ 
122+ function  asyncAesCtrCipher ( mode ,  key ,  data ,  algorithm )  { 
123+   validateAesCtrAlgorithm ( algorithm ) ; 
121124
122125  return  jobPromise ( ( )  =>  new  AESCipherJob ( 
123126    kCryptoJobAsync , 
124127    mode , 
125128    key [ kKeyObject ] [ kHandle ] , 
126129    data , 
127130    getVariant ( 'AES-CTR' ,  key . algorithm . length ) , 
128-     counter , 
129-     length ) ) ; 
131+     algorithm . counter , 
132+     algorithm . length ) ) ; 
133+ } 
134+ 
135+ function  validateAesCbcAlgorithm ( algorithm )  { 
136+   validateByteLength ( algorithm . iv ,  'algorithm.iv' ,  16 ) ; 
130137} 
131138
132- function  asyncAesCbcCipher ( mode ,  key ,  data ,  {  iv  } )  { 
133-   validateByteLength ( iv ,   ' algorithm.iv' ,   16 ) ; 
139+ function  asyncAesCbcCipher ( mode ,  key ,  data ,  algorithm )  { 
140+   validateAesCbcAlgorithm ( algorithm ) ; 
134141  return  jobPromise ( ( )  =>  new  AESCipherJob ( 
135142    kCryptoJobAsync , 
136143    mode , 
137144    key [ kKeyObject ] [ kHandle ] , 
138145    data , 
139146    getVariant ( 'AES-CBC' ,  key . algorithm . length ) , 
140-     iv ) ) ; 
147+     algorithm . iv ) ) ; 
141148} 
142149
143150function  asyncAesKwCipher ( mode ,  key ,  data )  { 
@@ -149,24 +156,25 @@ function asyncAesKwCipher(mode, key, data) {
149156    getVariant ( 'AES-KW' ,  key . algorithm . length ) ) ) ; 
150157} 
151158
152- function  asyncAesGcmCipher ( 
153-   mode , 
154-   key , 
155-   data , 
156-   {  iv,  additionalData,  tagLength =  128  } )  { 
157-   if  ( ! ArrayPrototypeIncludes ( kTagLengths ,  tagLength ) )  { 
158-     return  PromiseReject ( lazyDOMException ( 
159-       `${ tagLength }  , 
160-       'OperationError' ) ) ; 
159+ function  validateAesGcmAlgorithm ( algorithm )  { 
160+   if  ( ! ArrayPrototypeIncludes ( kTagLengths ,  algorithm . tagLength ) )  { 
161+     throw  lazyDOMException ( 
162+       `${ algorithm . tagLength }  , 
163+       'OperationError' ) ; 
161164  } 
162165
163-   validateMaxBufferLength ( iv ,  'algorithm.iv' ) ; 
166+   validateMaxBufferLength ( algorithm . iv ,  'algorithm.iv' ) ; 
164167
165-   if  ( additionalData  !==  undefined )  { 
166-     validateMaxBufferLength ( additionalData ,  'algorithm.additionalData' ) ; 
168+   if  ( algorithm . additionalData  !==  undefined )  { 
169+     validateMaxBufferLength ( algorithm . additionalData ,  'algorithm.additionalData' ) ; 
167170  } 
171+ } 
168172
169-   const  tagByteLength  =  MathFloor ( tagLength  /  8 ) ; 
173+ function  asyncAesGcmCipher ( mode ,  key ,  data ,  algorithm )  { 
174+   algorithm . tagLength  ??=  128 ; 
175+   validateAesGcmAlgorithm ( algorithm ) ; 
176+ 
177+   const  tagByteLength  =  MathFloor ( algorithm . tagLength  /  8 ) ; 
170178  let  tag ; 
171179  switch  ( mode )  { 
172180    case  kWebCryptoCipherDecrypt : { 
@@ -198,9 +206,9 @@ function asyncAesGcmCipher(
198206    key [ kKeyObject ] [ kHandle ] , 
199207    data , 
200208    getVariant ( 'AES-GCM' ,  key . algorithm . length ) , 
201-     iv , 
209+     algorithm . iv , 
202210    tag , 
203-     additionalData ) ) ; 
211+     algorithm . additionalData ) ) ; 
204212} 
205213
206214function  aesCipher ( mode ,  key ,  data ,  algorithm )  { 
@@ -212,13 +220,17 @@ function aesCipher(mode, key, data, algorithm) {
212220  } 
213221} 
214222
215- async  function  aesGenerateKey ( algorithm ,  extractable ,  keyUsages )  { 
216-   const  {  name,  length }  =  algorithm ; 
217-   if  ( ! ArrayPrototypeIncludes ( kAesKeyLengths ,  length ) )  { 
223+ function  validateAesGenerateKeyAlgorithm ( algorithm )  { 
224+   if  ( ! ArrayPrototypeIncludes ( kAesKeyLengths ,  algorithm . length ) )  { 
218225    throw  lazyDOMException ( 
219226      'AES key length must be 128, 192, or 256 bits' , 
220227      'OperationError' ) ; 
221228  } 
229+ } 
230+ 
231+ async  function  aesGenerateKey ( algorithm ,  extractable ,  keyUsages )  { 
232+   validateAesGenerateKeyAlgorithm ( algorithm ) ; 
233+   const  {  name,  length }  =  algorithm ; 
222234
223235  const  checkUsages  =  [ 'wrapKey' ,  'unwrapKey' ] ; 
224236  if  ( name  !==  'AES-KW' ) 
0 commit comments