aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/org/gnunet/util/CryptoECCTest.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/test/java/org/gnunet/util/CryptoECCTest.java')
-rw-r--r--src/test/java/org/gnunet/util/CryptoECCTest.java82
1 files changed, 82 insertions, 0 deletions
diff --git a/src/test/java/org/gnunet/util/CryptoECCTest.java b/src/test/java/org/gnunet/util/CryptoECCTest.java
new file mode 100644
index 0000000..cc88409
--- /dev/null
+++ b/src/test/java/org/gnunet/util/CryptoECCTest.java
@@ -0,0 +1,82 @@
1package org.gnunet.util;
2
3
4import org.junit.Assert;
5import org.junit.Test;
6
7import java.util.Random;
8
9public class CryptoECCTest {
10 /**
11 * Check that signed messages can be verified correctly.
12 */
13 @Test
14 public void test_sign_success() {
15 Random r = new Random();
16 // the test uses random data, repeat it multiple times!
17 for (int i = 0; i < 10; i++) {
18 byte[] msg = new byte[16];
19 r.nextBytes(msg);
20
21 CryptoECC.PrivateKey privateKey = new CryptoECC.PrivateKey();
22 privateKey.d = new byte[32];
23 r.nextBytes(privateKey.d);
24 CryptoECC.PublicKey publicKey = CryptoECC.computePublicKey(privateKey);
25
26 CryptoECC.Signature sig = CryptoECC.sign(msg, privateKey, publicKey);
27
28 boolean valid = CryptoECC.verify(sig, msg, publicKey);
29
30 Assert.assertTrue(valid);
31 }
32 }
33
34 /**
35 * Check that signature verification fails for manipulated data.
36 */
37 @Test
38 public void test_sign_failure() {
39 Random r = new Random();
40 // the test uses random data, repeat it multiple times!
41 for (int i = 0; i < 10; i++) {
42 byte[] msg = new byte[16];
43 r.nextBytes(msg);
44
45 CryptoECC.PrivateKey privateKey = new CryptoECC.PrivateKey();
46 privateKey.d = new byte[32];
47 r.nextBytes(privateKey.d);
48 CryptoECC.PublicKey publicKey = CryptoECC.computePublicKey(privateKey);
49
50 CryptoECC.Signature sig = CryptoECC.sign(msg, privateKey, publicKey);
51
52 msg[0] = (byte) (msg[0] + 1);
53
54 boolean valid = CryptoECC.verify(sig, msg, publicKey);
55
56 Assert.assertFalse(valid);
57 }
58 }
59
60 /**
61 * Check whether ecdh key coincide
62 */
63 @Test
64 public void test_ecdh() {
65 Random r = new Random();
66
67 CryptoECC.PrivateKey privateAlice = new CryptoECC.PrivateKey();
68 privateAlice.d = new byte[32];
69 r.nextBytes(privateAlice.d);
70 CryptoECC.PublicKey publicAlice = CryptoECC.computePublicKey(privateAlice);
71
72 CryptoECC.PrivateKey privateBob = new CryptoECC.PrivateKey();
73 privateBob.d = new byte[32];
74 r.nextBytes(privateBob.d);
75 CryptoECC.PublicKey publicBob = CryptoECC.computePublicKey(privateBob);
76
77 HashCode ssAlice = CryptoECC.ecdh(privateAlice, publicBob);
78 HashCode ssBob = CryptoECC.ecdh(privateBob, publicAlice);
79
80 Assert.assertArrayEquals(ssAlice.data, ssBob.data);
81 }
82}