@@ -12,8 +12,6 @@ const {
1212const { Buffer } = require ( 'buffer' ) ;
1313
1414const {
15- DHBitsJob,
16- DHKeyExportJob,
1715 DiffieHellman : _DiffieHellman ,
1816 DiffieHellmanGroup : _DiffieHellmanGroup ,
1917 ECDH : _ECDH ,
@@ -53,23 +51,12 @@ const {
5351
5452const {
5553 KeyObject,
56- InternalCryptoKey,
57- createPrivateKey,
58- createPublicKey,
5954 isCryptoKey,
60- isKeyObject,
6155} = require ( 'internal/crypto/keys' ) ;
6256
63- const {
64- generateKeyPair,
65- } = require ( 'internal/crypto/keygen' ) ;
66-
6757const {
6858 getArrayBufferOrView,
6959 getDefaultEncoding,
70- getUsagesUnion,
71- hasAnyNotIn,
72- jobPromise,
7360 toBuf,
7461 kHandle,
7562 kKeyObject,
@@ -343,89 +330,6 @@ function deriveBitsECDH(name, publicKey, privateKey, callback) {
343330 job . run ( ) ;
344331}
345332
346- // The deriveBitsDH function is part of the Web Crypto API and serves both
347- // deriveKeys and deriveBits functions.
348- function deriveBitsDH ( publicKey , privateKey , callback ) {
349- validateObject ( publicKey , 'publicKey' ) ;
350- validateObject ( privateKey , 'privateKey' ) ;
351- validateCallback ( callback ) ;
352- const job = new DHBitsJob ( kCryptoJobAsync , publicKey , privateKey ) ;
353- job . ondone = ( error , bits ) => {
354- if ( error ) return FunctionPrototypeCall ( callback , job , error ) ;
355- FunctionPrototypeCall ( callback , job , null , bits ) ;
356- } ;
357- job . run ( ) ;
358- }
359-
360- function verifyAcceptableDhKeyUse ( name , type , usages ) {
361- let checkSet ;
362- switch ( type ) {
363- case 'private' :
364- checkSet = [ 'deriveBits' , 'deriveKey' ] ;
365- break ;
366- case 'public' :
367- checkSet = [ ] ;
368- break ;
369- }
370- if ( hasAnyNotIn ( usages , checkSet ) ) {
371- throw lazyDOMException (
372- `Unsupported key usage for an ${ name } key` ,
373- 'SyntaxError' ) ;
374- }
375- }
376-
377- async function dhGenerateKey (
378- algorithm ,
379- extractable ,
380- keyUsages ) {
381- const usageSet = new SafeSet ( keyUsages ) ;
382-
383- if ( hasAnyNotIn ( usageSet , [ 'deriveKey' , 'deriveBits' ] ) ) {
384- throw lazyDOMException (
385- 'Unsupported key usage for a DH key' ,
386- 'SyntaxError' ) ;
387- }
388-
389- const {
390- name,
391- primeLength,
392- generator,
393- group
394- } = algorithm ;
395- let { prime } = algorithm ;
396-
397- if ( prime !== undefined )
398- prime = getArrayBufferOrView ( prime ) ;
399-
400- return new Promise ( ( resolve , reject ) => {
401- generateKeyPair ( 'dh' , {
402- prime,
403- primeLength,
404- generator,
405- group,
406- } , ( err , pubKey , privKey ) => {
407- if ( err ) {
408- return reject ( lazyDOMException (
409- 'The operation failed for an operation-specific reason' ,
410- 'OperationError' ) ) ;
411- }
412-
413- const algorithm = { name, prime, primeLength, generator, group } ;
414-
415- const publicKey = new InternalCryptoKey ( pubKey , algorithm , [ ] , true ) ;
416-
417- const privateKey =
418- new InternalCryptoKey (
419- privKey ,
420- algorithm ,
421- getUsagesUnion ( usageSet , 'deriveBits' , 'deriveKey' ) ,
422- extractable ) ;
423-
424- resolve ( { publicKey, privateKey } ) ;
425- } ) ;
426- } ) ;
427- }
428-
429333async function asyncDeriveBitsECDH ( algorithm , baseKey , length ) {
430334 const { 'public' : key } = algorithm ;
431335
@@ -496,136 +400,11 @@ async function asyncDeriveBitsECDH(algorithm, baseKey, length) {
496400 ArrayBufferPrototypeSlice ( bits , 0 , length ) ;
497401}
498402
499- async function asyncDeriveBitsDH ( algorithm , baseKey , length ) {
500- const { 'public' : key } = algorithm ;
501- // Null has a specific meaning for DH
502- if ( length !== null )
503- validateUint32 ( length , 'length' ) ;
504- if ( ! isCryptoKey ( key ) )
505- throw new ERR_INVALID_ARG_TYPE ( 'algorithm.public' , 'CryptoKey' , key ) ;
506-
507- if ( key . type !== 'public' ) {
508- throw lazyDOMException (
509- 'algorithm.public must be a public key' , 'InvalidAccessError' ) ;
510- }
511- if ( baseKey . type !== 'private' ) {
512- throw lazyDOMException (
513- 'baseKey must be a private key' , 'InvalidAccessError' ) ;
514- }
515-
516- if ( key . algorithm . name !== 'NODE-DH' )
517- throw lazyDOMException ( 'Keys must be DH keys' , 'InvalidAccessError' ) ;
518-
519- if ( key . algorithm . name !== baseKey . algorithm . name ) {
520- throw lazyDOMException (
521- 'The public and private keys must be of the same type' ,
522- 'InvalidAccessError' ) ;
523- }
524-
525- const bits = await new Promise ( ( resolve , reject ) => {
526- deriveBitsDH (
527- key [ kKeyObject ] [ kHandle ] ,
528- baseKey [ kKeyObject ] [ kHandle ] , ( err , bits ) => {
529- if ( err ) return reject ( err ) ;
530- resolve ( bits ) ;
531- } ) ;
532- } ) ;
533-
534- // If a length is not specified, return the full derived secret
535- if ( length === null )
536- return bits ;
537-
538- // If the length is not a multiple of 8, it will be truncated
539- // down to the nearest multiple of 8.
540- length = MathFloor ( length / 8 ) ;
541- const { byteLength } = bits ;
542-
543- // If the length is larger than the derived secret, throw.
544- // Otherwise, we either return the secret or a truncated
545- // slice.
546- if ( byteLength < length )
547- throw lazyDOMException ( 'derived bit length is too small' , 'OperationError' ) ;
548-
549- return length === byteLength ?
550- bits :
551- ArrayBufferPrototypeSlice ( bits , 0 , length ) ;
552- }
553-
554- function dhExportKey ( key , format ) {
555- return jobPromise ( new DHKeyExportJob (
556- kCryptoJobAsync ,
557- format ,
558- key [ kKeyObject ] [ kHandle ] ) ) ;
559- }
560-
561- async function dhImportKey (
562- format ,
563- keyData ,
564- algorithm ,
565- extractable ,
566- keyUsages ) {
567- const usagesSet = new SafeSet ( keyUsages ) ;
568- let keyObject ;
569- switch ( format ) {
570- case 'node.keyObject' : {
571- if ( ! isKeyObject ( keyData ) )
572- throw new ERR_INVALID_ARG_TYPE ( 'keyData' , 'KeyObject' , keyData ) ;
573- if ( keyData . type === 'secret' )
574- throw lazyDOMException ( 'Invalid key type' , 'InvalidAccessException' ) ;
575- verifyAcceptableDhKeyUse ( algorithm . name , keyData . type , usagesSet ) ;
576- keyObject = keyData ;
577- break ;
578- }
579- case 'spki' : {
580- verifyAcceptableDhKeyUse ( algorithm . name , 'public' , usagesSet ) ;
581- keyObject = createPublicKey ( {
582- key : keyData ,
583- format : 'der' ,
584- type : 'spki'
585- } ) ;
586- break ;
587- }
588- case 'pkcs8' : {
589- verifyAcceptableDhKeyUse ( algorithm . name , 'private' , usagesSet ) ;
590- keyObject = createPrivateKey ( {
591- key : keyData ,
592- format : 'der' ,
593- type : 'pkcs8'
594- } ) ;
595- break ;
596- }
597- default :
598- throw lazyDOMException (
599- `Unable to import DH key with format ${ format } ` ,
600- 'NotSupportedError' ) ;
601- }
602-
603- const {
604- prime,
605- primeLength,
606- generator,
607- group,
608- } = keyObject [ kHandle ] . keyDetail ( { } ) ;
609-
610- return new InternalCryptoKey ( keyObject , {
611- name : algorithm . name ,
612- prime,
613- primeLength,
614- generator,
615- group,
616- } , keyUsages , extractable ) ;
617- }
618-
619403module . exports = {
620404 DiffieHellman,
621405 DiffieHellmanGroup,
622406 ECDH ,
623407 diffieHellman,
624408 deriveBitsECDH,
625- deriveBitsDH,
626- dhGenerateKey,
627409 asyncDeriveBitsECDH,
628- asyncDeriveBitsDH,
629- dhExportKey,
630- dhImportKey,
631410} ;
0 commit comments