@@ -19,25 +19,28 @@ let key2 = dh2.generateKeys('hex');
19
19
let secret1 = dh1 . computeSecret ( key2 , 'hex' , 'base64' ) ;
20
20
let secret2 = dh2 . computeSecret ( key1 , 'latin1' , 'buffer' ) ;
21
21
22
- assert . strictEqual ( secret1 , secret2 . toString ( 'base64' ) ) ;
22
+ assert . strictEqual ( secret2 . toString ( 'base64' ) , secret1 ) ;
23
23
assert . strictEqual ( dh1 . verifyError , 0 ) ;
24
24
assert . strictEqual ( dh2 . verifyError , 0 ) ;
25
25
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 ( ( ) => {
27
30
crypto . createDiffieHellman ( [ 0x1 , 0x2 ] ) ;
28
- } ) ;
31
+ } , argumentsError ) ;
29
32
30
- assert . throws ( function ( ) {
31
- crypto . createDiffieHellman ( function ( ) { } ) ;
32
- } ) ;
33
+ assert . throws ( ( ) => {
34
+ crypto . createDiffieHellman ( ( ) => { } ) ;
35
+ } , argumentsError ) ;
33
36
34
- assert . throws ( function ( ) {
37
+ assert . throws ( ( ) => {
35
38
crypto . createDiffieHellman ( / a b c / ) ;
36
- } ) ;
39
+ } , argumentsError ) ;
37
40
38
- assert . throws ( function ( ) {
41
+ assert . throws ( ( ) => {
39
42
crypto . createDiffieHellman ( { } ) ;
40
- } ) ;
43
+ } , argumentsError ) ;
41
44
42
45
// Create "another dh1" using generated keys from dh1,
43
46
// and compute secret again
@@ -56,21 +59,29 @@ const secret3 = dh3.computeSecret(key2, 'hex', 'base64');
56
59
57
60
assert . strictEqual ( secret1 , secret3 ) ;
58
61
62
+ const wrongBlockLength =
63
+ new RegExp ( '^Error: error:0606506D:digital envelope' +
64
+ ' routines:EVP_DecryptFinal_ex:wrong final block length$' ) ;
65
+
59
66
// Run this one twice to make sure that the dh3 clears its error properly
60
67
{
61
68
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 ) ;
63
72
}
64
73
65
- assert . throws ( function ( ) {
66
- dh3 . computeSecret ( '' ) ;
67
- } , / k e y i s t o o s m a l l / i) ;
68
-
69
74
{
70
75
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 ) ;
72
79
}
73
80
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
+
74
85
// Create a shared using a DH group.
75
86
const alice = crypto . createDiffieHellmanGroup ( 'modp5' ) ;
76
87
const bob = crypto . createDiffieHellmanGroup ( 'modp5' ) ;
@@ -180,30 +191,31 @@ assert.throws(() => {
180
191
const ecdh3 = crypto . createECDH ( 'secp256k1' ) ;
181
192
const key3 = ecdh3 . generateKeys ( ) ;
182
193
183
- assert . throws ( function ( ) {
194
+ assert . throws ( ( ) => {
184
195
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 $ / ) ;
186
197
187
198
// ECDH should allow .setPrivateKey()/.setPublicKey()
188
199
const ecdh4 = crypto . createECDH ( 'prime256v1' ) ;
189
200
190
201
ecdh4 . setPrivateKey ( ecdh1 . getPrivateKey ( ) ) ;
191
202
ecdh4 . setPublicKey ( ecdh1 . getPublicKey ( ) ) ;
192
203
193
- assert . throws ( function ( ) {
204
+ assert . throws ( ( ) => {
194
205
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 $ / ) ;
196
207
197
208
// Verify that we can use ECDH without having to use newly generated keys.
198
209
const ecdh5 = crypto . createECDH ( 'secp256k1' ) ;
199
210
200
211
// Verify errors are thrown when retrieving keys from an uninitialized object.
201
- assert . throws ( function ( ) {
212
+ assert . throws ( ( ) => {
202
213
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 ( ( ) => {
205
217
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 $ / ) ;
207
219
208
220
// A valid private key for the secp256k1 curve.
209
221
const cafebabeKey = 'cafebabe' . repeat ( 8 ) ;
@@ -249,10 +261,10 @@ assert.strictEqual(ecdh5.getPublicKey('hex', 'compressed'), cafebabePubPtComp);
249
261
// Show why allowing the public key to be set on this type does not make sense.
250
262
ecdh5 . setPublicKey ( peerPubPtComp , 'hex' ) ;
251
263
assert . strictEqual ( ecdh5 . getPublicKey ( 'hex' ) , peerPubPtUnComp ) ;
252
- assert . throws ( function ( ) {
264
+ assert . throws ( ( ) => {
253
265
// Error because the public key does not match the private key anymore.
254
266
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 $ / ) ;
256
268
257
269
// Set to a valid key to show that later attempts to set an invalid key are
258
270
// rejected.
@@ -262,10 +274,10 @@ ecdh5.setPrivateKey(cafebabeKey, 'hex');
262
274
'0000000000000000000000000000000000000000000000000000000000000000' ,
263
275
'FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141' ,
264
276
'FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF' ,
265
- ] . forEach ( function ( element , index , object ) {
266
- assert . throws ( function ( ) {
277
+ ] . forEach ( ( element ) => {
278
+ assert . throws ( ( ) => {
267
279
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 . $ / ) ;
269
281
// Verify object state did not change.
270
282
assert . strictEqual ( ecdh5 . getPrivateKey ( 'hex' ) , cafebabeKey ) ;
271
283
} ) ;
0 commit comments