@@ -5,72 +5,87 @@ import { assert } from 'chai';
55
66const SUITE = 'dh' ;
77
8- test ( SUITE , 'should create DiffieHellman with size' , ( ) => {
9- const dh = crypto . createDiffieHellman ( 512 ) ;
10- const prime = dh . getPrime ( ) ;
11- assert . isOk ( prime ) ;
12- // Size check approx
13- assert . isAtLeast ( prime . length , 64 ) ;
14- } ) ;
8+ // RFC 3526 MODP Group 14 prime (2048-bit) for testing with explicit prime
9+ const MODP14_PRIME =
10+ 'FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1' +
11+ '29024E088A67CC74020BBEA63B139B22514A08798E3404DD' +
12+ 'EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245' +
13+ 'E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED' +
14+ 'EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D' +
15+ 'C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F' +
16+ '83655D23DCA3AD961C62F356208552BB9ED529077096966D' +
17+ '670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B' +
18+ 'E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9' +
19+ 'DE2BCBF6955817183995497CEA956AE515D2261898FA0510' +
20+ '15728E5A8AACAA68FFFFFFFFFFFFFFFF' ;
1521
16- test ( SUITE , 'should create DiffieHellman with prime' , ( ) => {
17- // 512-bit prime (Group 1 from RFC 2409)
18- const prime = Buffer . from (
19- 'FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1' +
20- '29024E088A67CC74020BBEA63B139B22514A08798E3404DD' +
21- 'EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245' +
22- 'E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED' +
23- 'EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE65381' +
24- 'FFFFFFFFFFFFFFFF' ,
25- 'hex' ,
26- ) ;
27- const generator = Buffer . from ( [ 2 ] ) ;
28- const dh = crypto . createDiffieHellman ( prime , generator ) ;
22+ test (
23+ SUITE ,
24+ 'should create DiffieHellman with prime and numeric generator' ,
25+ ( ) => {
26+ const prime = Buffer . from ( MODP14_PRIME , 'hex' ) ;
27+ const dh = crypto . createDiffieHellman ( prime , 2 ) ;
2928
30- assert . strictEqual ( dh . getPrime ( 'hex' ) , prime . toString ( 'hex' ) . toLowerCase ( ) ) ;
31- assert . strictEqual (
32- dh . getGenerator ( 'hex' ) ,
33- generator . toString ( 'hex' ) . toLowerCase ( ) ,
34- ) ;
35- } ) ;
29+ assert . strictEqual ( dh . getPrime ( 'hex' ) , prime . toString ( 'hex' ) . toLowerCase ( ) ) ;
30+ assert . strictEqual ( dh . getGenerator ( 'hex' ) , '02' ) ;
31+ } ,
32+ ) ;
33+
34+ test (
35+ SUITE ,
36+ 'should create DiffieHellman with prime and Buffer generator' ,
37+ ( ) => {
38+ const prime = Buffer . from ( MODP14_PRIME , 'hex' ) ;
39+ const generator = Buffer . from ( [ 2 ] ) ;
40+ const dh = crypto . createDiffieHellman ( prime , generator ) ;
41+
42+ assert . strictEqual ( dh . getPrime ( 'hex' ) , prime . toString ( 'hex' ) . toLowerCase ( ) ) ;
43+ assert . strictEqual (
44+ dh . getGenerator ( 'hex' ) ,
45+ generator . toString ( 'hex' ) . toLowerCase ( ) ,
46+ ) ;
47+ } ,
48+ ) ;
3649
3750test ( SUITE , 'should compute shared secret' , ( ) => {
38- const alice = crypto . createDiffieHellman ( 512 ) ;
39- const aliceKeys = alice . generateKeys ( ) ;
51+ const alice = crypto . getDiffieHellman ( 'modp14' ) ;
52+ alice . generateKeys ( ) ;
4053
41- const bob = crypto . createDiffieHellman (
42- alice . getPrime ( ) ,
43- alice . getGenerator ( ) ,
44- ) ;
45- const bobKeys = bob . generateKeys ( ) ;
54+ const bob = crypto . getDiffieHellman ( 'modp14' ) ;
55+ bob . generateKeys ( ) ;
4656
47- const aliceSecret = alice . computeSecret ( bobKeys ) ;
48- const bobSecret = bob . computeSecret ( aliceKeys ) ;
57+ const aliceSecret = alice . computeSecret ( bob . getPublicKey ( ) ) ;
58+ const bobSecret = bob . computeSecret ( alice . getPublicKey ( ) ) ;
4959
5060 assert . strictEqual ( aliceSecret . toString ( 'hex' ) , bobSecret . toString ( 'hex' ) ) ;
5161} ) ;
5262
5363test ( SUITE , 'should set keys' , ( ) => {
54- const alice = crypto . createDiffieHellman ( 512 ) ;
64+ const alice = crypto . getDiffieHellman ( 'modp14' ) ;
5565 alice . generateKeys ( ) ;
5666
57- const dh2 = crypto . createDiffieHellman (
67+ const bob = crypto . createDiffieHellman (
5868 alice . getPrime ( ) ,
5969 alice . getGenerator ( ) ,
6070 ) ;
61- dh2 . setPublicKey ( alice . getPublicKey ( ) ) ;
62- dh2 . setPrivateKey ( alice . getPrivateKey ( ) ) ;
71+ bob . setPublicKey ( alice . getPublicKey ( ) ) ;
72+ bob . setPrivateKey ( alice . getPrivateKey ( ) ) ;
6373
64- assert . strictEqual ( dh2 . getPublicKey ( 'hex' ) , alice . getPublicKey ( 'hex' ) ) ;
65- assert . strictEqual ( dh2 . getPrivateKey ( 'hex' ) , alice . getPrivateKey ( 'hex' ) ) ;
74+ assert . strictEqual ( bob . getPublicKey ( 'hex' ) , alice . getPublicKey ( 'hex' ) ) ;
75+ assert . strictEqual ( bob . getPrivateKey ( 'hex' ) , alice . getPrivateKey ( 'hex' ) ) ;
6676} ) ;
6777
6878test ( SUITE , 'should create DiffieHellman from standard group' , ( ) => {
6979 const dh = crypto . getDiffieHellman ( 'modp14' ) ;
7080 assert . isOk ( dh ) ;
7181 const prime = dh . getPrime ( ) ;
7282 assert . isTrue ( Buffer . isBuffer ( prime ) ) ;
73- // modp14 is 2048-bit group
7483 assert . strictEqual ( prime . length , 256 ) ;
7584 assert . strictEqual ( dh . getGenerator ( 'hex' ) , '02' ) ;
7685} ) ;
86+
87+ test ( SUITE , 'should reject prime length below 2048 bits' , ( ) => {
88+ assert . throws ( ( ) => {
89+ crypto . createDiffieHellman ( 512 ) ;
90+ } , / p r i m e l e n g t h m u s t b e a t l e a s t 2 0 4 8 b i t s / ) ;
91+ } ) ;
0 commit comments