|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+import org.junit.Before;
|
|
|
2
|
+import org.junit.Ignore;
|
|
|
3
|
+import org.junit.Test;
|
|
|
4
|
+
|
|
|
5
|
+import static org.junit.Assert.assertNotEquals;
|
|
|
6
|
+import static org.junit.Assert.assertTrue;
|
|
|
7
|
+import static org.junit.Assert.assertEquals;
|
|
|
8
|
+
|
|
|
9
|
+import java.math.BigInteger;
|
|
|
10
|
+import java.util.*;
|
|
|
11
|
+
|
|
|
12
|
+public class DiffieHellmanTest {
|
|
|
13
|
+
|
|
|
14
|
+ private DiffieHellman diffieHellman;
|
|
|
15
|
+
|
|
|
16
|
+ @Before
|
|
|
17
|
+ public void setUp() {
|
|
|
18
|
+ diffieHellman = new DiffieHellman();
|
|
|
19
|
+ }
|
|
|
20
|
+
|
|
|
21
|
+ @Test
|
|
|
22
|
+ public void testPrivateKeyInRange() {
|
|
|
23
|
+ BigInteger prime = BigInteger.valueOf(23);
|
|
|
24
|
+ List<BigInteger> privateKeys = new ArrayList<>();
|
|
|
25
|
+ for (int i = 0; i < 10; i++) {
|
|
|
26
|
+ privateKeys.add(diffieHellman.privateKey(prime));
|
|
|
27
|
+ }
|
|
|
28
|
+
|
|
|
29
|
+ for (BigInteger privateKey : privateKeys) {
|
|
|
30
|
+ assertTrue(privateKey.compareTo(BigInteger.ONE) >= 0);
|
|
|
31
|
+ assertTrue(privateKey.compareTo(prime) < 0);
|
|
|
32
|
+ }
|
|
|
33
|
+ }
|
|
|
34
|
+
|
|
|
35
|
+ // Note: due to the nature of randomness, there is always a chance that this test fails
|
|
|
36
|
+ // Be sure to check the actual generated values
|
|
|
37
|
+ @Ignore("Remove to run test")
|
|
|
38
|
+ @Test
|
|
|
39
|
+ public void testPrivateKeyRandomlyGenerated() {
|
|
|
40
|
+ BigInteger prime = BigInteger.valueOf(7919);
|
|
|
41
|
+ BigInteger privateKeyA = diffieHellman.privateKey(prime);
|
|
|
42
|
+ BigInteger privateKeyB = diffieHellman.privateKey(prime);
|
|
|
43
|
+
|
|
|
44
|
+ assertNotEquals(privateKeyA, privateKeyB);
|
|
|
45
|
+ }
|
|
|
46
|
+
|
|
|
47
|
+ @Ignore("Remove to run test")
|
|
|
48
|
+ @Test
|
|
|
49
|
+ public void testPublicKeyCorrectlyCalculated() {
|
|
|
50
|
+ BigInteger primeA = BigInteger.valueOf(23);
|
|
|
51
|
+ BigInteger primeB = BigInteger.valueOf(5);
|
|
|
52
|
+ BigInteger privateKey = BigInteger.valueOf(6);
|
|
|
53
|
+ BigInteger expected = BigInteger.valueOf(8);
|
|
|
54
|
+
|
|
|
55
|
+ assertEquals(expected, diffieHellman.publicKey(primeA, primeB, privateKey));
|
|
|
56
|
+ }
|
|
|
57
|
+
|
|
|
58
|
+ @Ignore("Remove to run test")
|
|
|
59
|
+ @Test
|
|
|
60
|
+ public void testSecretKeyCorrectlyCalculated() {
|
|
|
61
|
+ BigInteger prime = BigInteger.valueOf(23);
|
|
|
62
|
+ BigInteger publicKey = BigInteger.valueOf(19);
|
|
|
63
|
+ BigInteger privateKey = BigInteger.valueOf(6);
|
|
|
64
|
+ BigInteger expected = BigInteger.valueOf(2);
|
|
|
65
|
+
|
|
|
66
|
+ assertEquals(expected, diffieHellman.secret(prime, publicKey, privateKey));
|
|
|
67
|
+ }
|
|
|
68
|
+
|
|
|
69
|
+ @Ignore("Remove to run test")
|
|
|
70
|
+ @Test
|
|
|
71
|
+ public void testSecretKeyCorrectlyCalculatedWhenUsingLargePrimes() {
|
|
|
72
|
+ BigInteger prime = new BigInteger("120227323036150778550155526710966921740030662694578947298423549235265759593711587341037426347114541533006628856300552706996143592240453345642869233562886752930249953227657883929905072620233073626594386072962776144691433658814261874113232461749035425712805067202910389407991986070558964461330091797026762932543");
|
|
|
73
|
+ BigInteger publicKey = new BigInteger("75205441154357919442925546169208711235485855904969178206313309299205868312399046149367516336607966149689640419216591714331722664409474612463910928128055994157922930443733535659848264364106037925315974095321112757711756912144137705613776063541350548911512715512539186192176020596861210448363099541947258202188");
|
|
|
74
|
+ BigInteger privateKey = new BigInteger("2483479393625932939911081304356888505153797135447327501792696199190469015215177630758617902200417377685436170904594686456961202706692908603181062371925882");
|
|
|
75
|
+ BigInteger expected = new BigInteger("70900735223964890815905879227737819348808518698920446491346508980461201746567735331455825644429877946556431095820785835497384849778344216981228226252639932672153547963980483673419756271345828771971984887453014488572245819864454136618980914729839523581263886740821363010486083940557620831348661126601106717071");
|
|
|
76
|
+
|
|
|
77
|
+ assertEquals(expected, diffieHellman.secret(prime, publicKey, privateKey));
|
|
|
78
|
+ }
|
|
|
79
|
+
|
|
|
80
|
+ @Ignore("Remove to run test")
|
|
|
81
|
+ @Test
|
|
|
82
|
+ public void testExchange() {
|
|
|
83
|
+ BigInteger primeA = BigInteger.valueOf(23);
|
|
|
84
|
+ BigInteger primeB = BigInteger.valueOf(5);
|
|
|
85
|
+
|
|
|
86
|
+ BigInteger privateKeyA = diffieHellman.privateKey(primeA);
|
|
|
87
|
+ BigInteger privateKeyB = diffieHellman.privateKey(primeB);
|
|
|
88
|
+
|
|
|
89
|
+ BigInteger publicKeyA = diffieHellman.publicKey(primeA, primeB, privateKeyA);
|
|
|
90
|
+ BigInteger publicKeyB = diffieHellman.publicKey(primeA, primeB, privateKeyB);
|
|
|
91
|
+
|
|
|
92
|
+ BigInteger secretA = diffieHellman.secret(primeA, publicKeyB, privateKeyA);
|
|
|
93
|
+ BigInteger secretB = diffieHellman.secret(primeA, publicKeyA, privateKeyB);
|
|
|
94
|
+
|
|
|
95
|
+ assertEquals(secretA, secretB);
|
|
|
96
|
+ }
|
|
|
97
|
+
|
|
|
98
|
+}
|