@@ -19,25 +19,28 @@ let key2 = dh2.generateKeys('hex');
1919let  secret1  =  dh1 . computeSecret ( key2 ,  'hex' ,  'base64' ) ; 
2020let  secret2  =  dh2 . computeSecret ( key1 ,  'latin1' ,  'buffer' ) ; 
2121
22- assert . strictEqual ( secret1 ,   secret2 . toString ( 'base64' ) ) ; 
22+ assert . strictEqual ( secret2 . toString ( 'base64' ) ,   secret1 ) ; 
2323assert . strictEqual ( dh1 . verifyError ,  0 ) ; 
2424assert . strictEqual ( dh2 . verifyError ,  0 ) ; 
2525
26- assert . throws ( function ( )  { 
26+ const  argumentsError  = 
27+   / ^ T y p e E r r o r :   F i r s t   a r g u m e n t   s h o u l d   b e   n u m b e r ,   s t r i n g   o r   B u f f e r $ / ; 
28+ 
29+ assert . throws ( ( )  =>  { 
2730  crypto . createDiffieHellman ( [ 0x1 ,  0x2 ] ) ; 
28- } ) ; 
31+ } ,   argumentsError ) ; 
2932
30- assert . throws ( function ( )  { 
31-   crypto . createDiffieHellman ( function ( )  {  } ) ; 
32- } ) ; 
33+ assert . throws ( ( )   =>  { 
34+   crypto . createDiffieHellman ( ( )   =>  {  } ) ; 
35+ } ,   argumentsError ) ; 
3336
34- assert . throws ( function ( )  { 
37+ assert . throws ( ( )   =>  { 
3538  crypto . createDiffieHellman ( / a b c / ) ; 
36- } ) ; 
39+ } ,   argumentsError ) ; 
3740
38- assert . throws ( function ( )  { 
41+ assert . throws ( ( )   =>  { 
3942  crypto . createDiffieHellman ( { } ) ; 
40- } ) ; 
43+ } ,   argumentsError ) ; 
4144
4245// Create "another dh1" using generated keys from dh1, 
4346// and compute secret again 
@@ -56,21 +59,29 @@ const secret3 = dh3.computeSecret(key2, 'hex', 'base64');
5659
5760assert . strictEqual ( secret1 ,  secret3 ) ; 
5861
62+ const  wrongBlockLength  = 
63+     new  RegExp ( '^Error: error:0606506D:digital envelope'  + 
64+     ' routines:EVP_DecryptFinal_ex:wrong final block length$' ) ; 
65+ 
5966// Run this one twice to make sure that the dh3 clears its error properly 
6067{ 
6168  const  c  =  crypto . createDecipheriv ( 'aes-128-ecb' ,  crypto . randomBytes ( 16 ) ,  '' ) ; 
62-   assert . throws ( function ( )  {  c . final ( 'utf8' ) ;  } ,  / w r o n g   f i n a l   b l o c k   l e n g t h / ) ; 
69+   assert . throws ( ( )  =>  { 
70+     c . final ( 'utf8' ) ; 
71+   } ,  wrongBlockLength ) ; 
6372} 
6473
65- assert . throws ( function ( )  { 
66-   dh3 . computeSecret ( '' ) ; 
67- } ,  / k e y   i s   t o o   s m a l l / i) ; 
68- 
6974{ 
7075  const  c  =  crypto . createDecipheriv ( 'aes-128-ecb' ,  crypto . randomBytes ( 16 ) ,  '' ) ; 
71-   assert . throws ( function ( )  {  c . final ( 'utf8' ) ;  } ,  / w r o n g   f i n a l   b l o c k   l e n g t h / ) ; 
76+   assert . throws ( ( )  =>  { 
77+     c . final ( 'utf8' ) ; 
78+   } ,  wrongBlockLength ) ; 
7279} 
7380
81+ assert . throws ( ( )  =>  { 
82+   dh3 . computeSecret ( '' ) ; 
83+ } ,  / ^ E r r o r :   S u p p l i e d   k e y   i s   t o o   s m a l l $ / ) ; 
84+ 
7485// Create a shared using a DH group. 
7586const  alice  =  crypto . createDiffieHellmanGroup ( 'modp5' ) ; 
7687const  bob  =  crypto . createDiffieHellmanGroup ( 'modp5' ) ; 
@@ -180,30 +191,31 @@ assert.throws(() => {
180191const  ecdh3  =  crypto . createECDH ( 'secp256k1' ) ; 
181192const  key3  =  ecdh3 . generateKeys ( ) ; 
182193
183- assert . throws ( function ( )  { 
194+ assert . throws ( ( )   =>  { 
184195  ecdh2 . computeSecret ( key3 ,  'latin1' ,  'buffer' ) ; 
185- } ) ; 
196+ } ,   / ^ E r r o r :   F a i l e d   t o   t r a n s l a t e   B u f f e r   t o   a   E C _ P O I N T $ / ) ; 
186197
187198// ECDH should allow .setPrivateKey()/.setPublicKey() 
188199const  ecdh4  =  crypto . createECDH ( 'prime256v1' ) ; 
189200
190201ecdh4 . setPrivateKey ( ecdh1 . getPrivateKey ( ) ) ; 
191202ecdh4 . setPublicKey ( ecdh1 . getPublicKey ( ) ) ; 
192203
193- assert . throws ( function ( )  { 
204+ assert . throws ( ( )   =>  { 
194205  ecdh4 . setPublicKey ( ecdh3 . getPublicKey ( ) ) ; 
195- } ,  / F a i l e d   t o   c o n v e r t   B u f f e r   t o   E C _ P O I N T / ) ; 
206+ } ,  / ^ E r r o r :   F a i l e d   t o   c o n v e r t   B u f f e r   t o   E C _ P O I N T $ / ) ; 
196207
197208// Verify that we can use ECDH without having to use newly generated keys. 
198209const  ecdh5  =  crypto . createECDH ( 'secp256k1' ) ; 
199210
200211// Verify errors are thrown when retrieving keys from an uninitialized object. 
201- assert . throws ( function ( )  { 
212+ assert . throws ( ( )   =>  { 
202213  ecdh5 . getPublicKey ( ) ; 
203- } ,  / F a i l e d   t o   g e t   E C D H   p u b l i c   k e y / ) ; 
204- assert . throws ( function ( )  { 
214+ } ,  / ^ E r r o r :   F a i l e d   t o   g e t   E C D H   p u b l i c   k e y $ / ) ; 
215+ 
216+ assert . throws ( ( )  =>  { 
205217  ecdh5 . getPrivateKey ( ) ; 
206- } ,  / F a i l e d   t o   g e t   E C D H   p r i v a t e   k e y / ) ; 
218+ } ,  / ^ E r r o r :   F a i l e d   t o   g e t   E C D H   p r i v a t e   k e y $ / ) ; 
207219
208220// A valid private key for the secp256k1 curve. 
209221const  cafebabeKey  =  'cafebabe' . repeat ( 8 ) ; 
@@ -249,10 +261,10 @@ assert.strictEqual(ecdh5.getPublicKey('hex', 'compressed'), cafebabePubPtComp);
249261// Show why allowing the public key to be set on this type does not make sense. 
250262ecdh5 . setPublicKey ( peerPubPtComp ,  'hex' ) ; 
251263assert . strictEqual ( ecdh5 . getPublicKey ( 'hex' ) ,  peerPubPtUnComp ) ; 
252- assert . throws ( function ( )  { 
264+ assert . throws ( ( )   =>  { 
253265  // Error because the public key does not match the private key anymore. 
254266  ecdh5 . computeSecret ( peerPubPtComp ,  'hex' ,  'hex' ) ; 
255- } ,  / I n v a l i d   k e y   p a i r / ) ; 
267+ } ,  / ^ E r r o r :   I n v a l i d   k e y   p a i r $ / ) ; 
256268
257269// Set to a valid key to show that later attempts to set an invalid key are 
258270// rejected. 
@@ -262,10 +274,10 @@ ecdh5.setPrivateKey(cafebabeKey, 'hex');
262274  '0000000000000000000000000000000000000000000000000000000000000000' , 
263275  'FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141' , 
264276  'FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF' , 
265- ] . forEach ( function ( element ,   index ,   object )  { 
266-   assert . throws ( function ( )  { 
277+ ] . forEach ( ( element )   =>  { 
278+   assert . throws ( ( )   =>  { 
267279    ecdh5 . setPrivateKey ( element ,  'hex' ) ; 
268-   } ,  / P r i v a t e   k e y   i s   n o t   v a l i d   f o r   s p e c i f i e d   c u r v e / ) ; 
280+   } ,  / ^ E r r o r :   P r i v a t e   k e y   i s   n o t   v a l i d   f o r   s p e c i f i e d   c u r v e . $ / ) ; 
269281  // Verify object state did not change. 
270282  assert . strictEqual ( ecdh5 . getPrivateKey ( 'hex' ) ,  cafebabeKey ) ; 
271283} ) ; 
0 commit comments