@@ -109,35 +109,43 @@ function getVariant(name, length) {
109109 }
110110}
111111
112- function asyncAesCtrCipher ( mode , key , data , { counter , length } ) {
113- validateByteLength ( counter , 'algorithm.counter' , 16 ) ;
112+ function validateAesCtrAlgorithm ( algorithm ) {
113+ validateByteLength ( algorithm . counter , 'algorithm.counter' , 16 ) ;
114114 // The length must specify an integer between 1 and 128. While
115115 // there is no default, this should typically be 64.
116- if ( length === 0 || length > kMaxCounterLength ) {
116+ if ( algorithm . length === 0 || algorithm . length > kMaxCounterLength ) {
117117 throw lazyDOMException (
118118 'AES-CTR algorithm.length must be between 1 and 128' ,
119119 'OperationError' ) ;
120120 }
121+ }
122+
123+ function asyncAesCtrCipher ( mode , key , data , algorithm ) {
124+ validateAesCtrAlgorithm ( algorithm ) ;
121125
122126 return jobPromise ( ( ) => new AESCipherJob (
123127 kCryptoJobAsync ,
124128 mode ,
125129 key [ kKeyObject ] [ kHandle ] ,
126130 data ,
127131 getVariant ( 'AES-CTR' , key . algorithm . length ) ,
128- counter ,
129- length ) ) ;
132+ algorithm . counter ,
133+ algorithm . length ) ) ;
134+ }
135+
136+ function validateAesCbcAlgorithm ( algorithm ) {
137+ validateByteLength ( algorithm . iv , 'algorithm.iv' , 16 ) ;
130138}
131139
132- function asyncAesCbcCipher ( mode , key , data , { iv } ) {
133- validateByteLength ( iv , ' algorithm.iv' , 16 ) ;
140+ function asyncAesCbcCipher ( mode , key , data , algorithm ) {
141+ validateAesCbcAlgorithm ( algorithm ) ;
134142 return jobPromise ( ( ) => new AESCipherJob (
135143 kCryptoJobAsync ,
136144 mode ,
137145 key [ kKeyObject ] [ kHandle ] ,
138146 data ,
139147 getVariant ( 'AES-CBC' , key . algorithm . length ) ,
140- iv ) ) ;
148+ algorithm . iv ) ) ;
141149}
142150
143151function asyncAesKwCipher ( mode , key , data ) {
@@ -149,24 +157,25 @@ function asyncAesKwCipher(mode, key, data) {
149157 getVariant ( 'AES-KW' , key . algorithm . length ) ) ) ;
150158}
151159
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 } is not a valid AES-GCM tag length` ,
160- 'OperationError' ) ) ;
160+ function validateAesGcmAlgorithm ( algorithm ) {
161+ if ( ! ArrayPrototypeIncludes ( kTagLengths , algorithm . tagLength ) ) {
162+ throw lazyDOMException (
163+ `${ algorithm . tagLength } is not a valid AES-GCM tag length` ,
164+ 'OperationError' ) ;
161165 }
162166
163- validateMaxBufferLength ( iv , 'algorithm.iv' ) ;
167+ validateMaxBufferLength ( algorithm . iv , 'algorithm.iv' ) ;
164168
165- if ( additionalData !== undefined ) {
166- validateMaxBufferLength ( additionalData , 'algorithm.additionalData' ) ;
169+ if ( algorithm . additionalData !== undefined ) {
170+ validateMaxBufferLength ( algorithm . additionalData , 'algorithm.additionalData' ) ;
167171 }
172+ }
168173
169- const tagByteLength = MathFloor ( tagLength / 8 ) ;
174+ function asyncAesGcmCipher ( mode , key , data , algorithm ) {
175+ algorithm . tagLength ??= 128 ;
176+ validateAesGcmAlgorithm ( algorithm ) ;
177+
178+ const tagByteLength = MathFloor ( algorithm . tagLength / 8 ) ;
170179 let tag ;
171180 switch ( mode ) {
172181 case kWebCryptoCipherDecrypt : {
@@ -198,9 +207,9 @@ function asyncAesGcmCipher(
198207 key [ kKeyObject ] [ kHandle ] ,
199208 data ,
200209 getVariant ( 'AES-GCM' , key . algorithm . length ) ,
201- iv ,
210+ algorithm . iv ,
202211 tag ,
203- additionalData ) ) ;
212+ algorithm . additionalData ) ) ;
204213}
205214
206215function aesCipher ( mode , key , data , algorithm ) {
@@ -212,13 +221,17 @@ function aesCipher(mode, key, data, algorithm) {
212221 }
213222}
214223
215- async function aesGenerateKey ( algorithm , extractable , keyUsages ) {
216- const { name, length } = algorithm ;
217- if ( ! ArrayPrototypeIncludes ( kAesKeyLengths , length ) ) {
224+ function validateAesGenerateKeyAlgorithm ( algorithm ) {
225+ if ( ! ArrayPrototypeIncludes ( kAesKeyLengths , algorithm . length ) ) {
218226 throw lazyDOMException (
219227 'AES key length must be 128, 192, or 256 bits' ,
220228 'OperationError' ) ;
221229 }
230+ }
231+
232+ async function aesGenerateKey ( algorithm , extractable , keyUsages ) {
233+ validateAesGenerateKeyAlgorithm ( algorithm ) ;
234+ const { name, length } = algorithm ;
222235
223236 const checkUsages = [ 'wrapKey' , 'unwrapKey' ] ;
224237 if ( name !== 'AES-KW' )
0 commit comments