@@ -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' ) ;
@@ -176,30 +187,31 @@ assert(firstByte === 6 || firstByte === 7);
176
187
const ecdh3 = crypto . createECDH ( 'secp256k1' ) ;
177
188
const key3 = ecdh3 . generateKeys ( ) ;
178
189
179
- assert . throws ( function ( ) {
190
+ assert . throws ( ( ) => {
180
191
ecdh2 . computeSecret ( key3 , 'latin1' , 'buffer' ) ;
181
- } ) ;
192
+ } , / ^ 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 $ / ) ;
182
193
183
194
// ECDH should allow .setPrivateKey()/.setPublicKey()
184
195
const ecdh4 = crypto . createECDH ( 'prime256v1' ) ;
185
196
186
197
ecdh4 . setPrivateKey ( ecdh1 . getPrivateKey ( ) ) ;
187
198
ecdh4 . setPublicKey ( ecdh1 . getPublicKey ( ) ) ;
188
199
189
- assert . throws ( function ( ) {
200
+ assert . throws ( ( ) => {
190
201
ecdh4 . setPublicKey ( ecdh3 . getPublicKey ( ) ) ;
191
- } , / 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 / ) ;
202
+ } , / ^ 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 $ / ) ;
192
203
193
204
// Verify that we can use ECDH without having to use newly generated keys.
194
205
const ecdh5 = crypto . createECDH ( 'secp256k1' ) ;
195
206
196
207
// Verify errors are thrown when retrieving keys from an uninitialized object.
197
- assert . throws ( function ( ) {
208
+ assert . throws ( ( ) => {
198
209
ecdh5 . getPublicKey ( ) ;
199
- } , / F a i l e d t o g e t E C D H p u b l i c k e y / ) ;
200
- assert . throws ( function ( ) {
210
+ } , / ^ 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 $ / ) ;
211
+
212
+ assert . throws ( ( ) => {
201
213
ecdh5 . getPrivateKey ( ) ;
202
- } , / 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 / ) ;
214
+ } , / ^ 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 $ / ) ;
203
215
204
216
// A valid private key for the secp256k1 curve.
205
217
const cafebabeKey = 'cafebabe' . repeat ( 8 ) ;
@@ -245,10 +257,10 @@ assert.strictEqual(ecdh5.getPublicKey('hex', 'compressed'), cafebabePubPtComp);
245
257
// Show why allowing the public key to be set on this type does not make sense.
246
258
ecdh5 . setPublicKey ( peerPubPtComp , 'hex' ) ;
247
259
assert . strictEqual ( ecdh5 . getPublicKey ( 'hex' ) , peerPubPtUnComp ) ;
248
- assert . throws ( function ( ) {
260
+ assert . throws ( ( ) => {
249
261
// Error because the public key does not match the private key anymore.
250
262
ecdh5 . computeSecret ( peerPubPtComp , 'hex' , 'hex' ) ;
251
- } , / I n v a l i d k e y p a i r / ) ;
263
+ } , / ^ E r r o r : I n v a l i d k e y p a i r $ / ) ;
252
264
253
265
// Set to a valid key to show that later attempts to set an invalid key are
254
266
// rejected.
@@ -258,10 +270,10 @@ ecdh5.setPrivateKey(cafebabeKey, 'hex');
258
270
'0000000000000000000000000000000000000000000000000000000000000000' ,
259
271
'FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141' ,
260
272
'FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF' ,
261
- ] . forEach ( function ( element , index , object ) {
262
- assert . throws ( function ( ) {
273
+ ] . forEach ( ( element ) => {
274
+ assert . throws ( ( ) => {
263
275
ecdh5 . setPrivateKey ( element , 'hex' ) ;
264
- } , / 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 / ) ;
276
+ } , / ^ 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 . $ / ) ;
265
277
// Verify object state did not change.
266
278
assert . strictEqual ( ecdh5 . getPrivateKey ( 'hex' ) , cafebabeKey ) ;
267
279
} ) ;
0 commit comments