@@ -7,7 +7,7 @@ if (!common.hasCrypto)
77 common . skip ( 'missing crypto' ) ;
88
99const assert = require ( 'assert' ) ;
10- const { subtle } = require ( 'crypto' ) . webcrypto ;
10+ const { webcrypto : { subtle } , KeyObject } = require ( 'crypto' ) ;
1111
1212const { internalBinding } = require ( 'internal/test/binding' ) ;
1313
@@ -152,3 +152,35 @@ if (typeof internalBinding('crypto').ScryptJob === 'function') {
152152
153153 tests . then ( common . mustCall ( ) ) ;
154154}
155+
156+ // Test default key lengths
157+ {
158+ const vectors = [
159+ [ 'PBKDF2' , 'deriveKey' , 528 ] ,
160+ [ 'HKDF' , 'deriveKey' , 528 ] ,
161+ [ { name : 'HMAC' , hash : 'SHA-1' } , 'sign' , 160 ] ,
162+ [ { name : 'HMAC' , hash : 'SHA-256' } , 'sign' , 256 ] ,
163+ [ { name : 'HMAC' , hash : 'SHA-384' } , 'sign' , 384 ] ,
164+ [ { name : 'HMAC' , hash : 'SHA-512' } , 'sign' , 512 ] ,
165+ ] ;
166+
167+ ( async ( ) => {
168+ const keyPair = await subtle . generateKey ( { name : 'ECDH' , namedCurve : 'P-521' } , false , [ 'deriveKey' ] ) ;
169+ for ( const [ derivedKeyAlgorithm , usage , expected ] of vectors ) {
170+ const derived = await subtle . deriveKey (
171+ { name : 'ECDH' , public : keyPair . publicKey } ,
172+ keyPair . privateKey ,
173+ derivedKeyAlgorithm ,
174+ false ,
175+ [ usage ] ) ;
176+
177+ if ( derived . algorithm . name === 'HMAC' ) {
178+ assert . strictEqual ( derived . algorithm . length , expected ) ;
179+ } else {
180+ // KDFs cannot be exportable and do not indicate their length
181+ const secretKey = KeyObject . from ( derived ) ;
182+ assert . strictEqual ( secretKey . symmetricKeySize , expected / 8 ) ;
183+ }
184+ }
185+ } ) ( ) . then ( common . mustCall ( ) ) ;
186+ }
0 commit comments