diff options
author | Florian Dold <florian.dold@gmail.com> | 2013-11-19 11:15:52 +0000 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2013-11-19 11:15:52 +0000 |
commit | e1dbf0dae09d3ecddc992a5a7b04a82ca03dcd2a (patch) | |
tree | 2447f7f92541c738d13a7659c7ba791146defa27 /src/test/java/org | |
parent | 3d17385928f938d170230be1b334ff159355775d (diff) | |
download | gnunet-java-e1dbf0dae09d3ecddc992a5a7b04a82ca03dcd2a.tar.gz gnunet-java-e1dbf0dae09d3ecddc992a5a7b04a82ca03dcd2a.zip |
- crypto + crypto tests
Diffstat (limited to 'src/test/java/org')
-rw-r--r-- | src/test/java/org/gnunet/util/EcdheTest.java | 79 | ||||
-rw-r--r-- | src/test/java/org/gnunet/util/EcdsaTest.java | 45 | ||||
-rw-r--r-- | src/test/java/org/gnunet/util/Ed25519Test.java | 142 | ||||
-rw-r--r-- | src/test/java/org/gnunet/util/EddsaTest.java | 11 |
4 files changed, 276 insertions, 1 deletions
diff --git a/src/test/java/org/gnunet/util/EcdheTest.java b/src/test/java/org/gnunet/util/EcdheTest.java new file mode 100644 index 0000000..4d789e2 --- /dev/null +++ b/src/test/java/org/gnunet/util/EcdheTest.java | |||
@@ -0,0 +1,79 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2012, 2013 Christian Grothoff (and other contributing authors) | ||
4 | |||
5 | GNUnet is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published | ||
7 | by the Free Software Foundation; either version 3, or (at your | ||
8 | option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with GNUnet; see the file COPYING. If not, write to the | ||
17 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | package org.gnunet.util; | ||
22 | |||
23 | import org.gnunet.util.crypto.EcdhePrivateKey; | ||
24 | import org.gnunet.util.crypto.EcdhePublicKey; | ||
25 | import org.junit.Assert; | ||
26 | import org.junit.Test; | ||
27 | |||
28 | /** | ||
29 | * Test ECDHE. | ||
30 | */ | ||
31 | public class EcdheTest { | ||
32 | |||
33 | /** | ||
34 | * Test whether the key is correctly restored when serializing it. | ||
35 | */ | ||
36 | @Test | ||
37 | public void test_compress() { | ||
38 | EcdhePrivateKey privKey1 = EcdhePrivateKey.createRandom(); | ||
39 | EcdhePublicKey pubKey1 = privKey1.getPublicKey(); | ||
40 | EcdhePublicKey pubKey2 = new EcdhePublicKey(pubKey1.asPoint()); | ||
41 | Assert.assertArrayEquals(pubKey1.y, pubKey2.y); | ||
42 | } | ||
43 | |||
44 | /** | ||
45 | * Test a single ecdh operation. | ||
46 | */ | ||
47 | @Test | ||
48 | public void test_simple() { | ||
49 | EcdhePrivateKey privKey1 = EcdhePrivateKey.createRandom(); | ||
50 | EcdhePrivateKey privKey2 = EcdhePrivateKey.createRandom(); | ||
51 | EcdhePublicKey pubKey1 = privKey1.getPublicKey(); | ||
52 | EcdhePublicKey pubKey2 = privKey2.getPublicKey(); | ||
53 | |||
54 | HashCode h1 = privKey1.ecdh(pubKey2); | ||
55 | HashCode h2 = privKey2.ecdh(pubKey1); | ||
56 | |||
57 | Assert.assertArrayEquals(h1.data, h2.data); | ||
58 | } | ||
59 | |||
60 | /** | ||
61 | * Test with values from the C GNUnet implementation. | ||
62 | * Generated with 'gnunet-ecc -E' | ||
63 | */ | ||
64 | @Test | ||
65 | public void test_gnunet_values() { | ||
66 | EcdhePrivateKey privKey1 = new EcdhePrivateKey(); | ||
67 | privKey1.d = new byte[32]; | ||
68 | EcdhePrivateKey privKey2 = new EcdhePrivateKey(); | ||
69 | privKey2.d = new byte[32]; | ||
70 | Strings.stringToData("FH7DONLOP82RMELK26NDEP8655HQFU9LUO36LNO1ENOJ5KOSRR00", privKey1.d); | ||
71 | Strings.stringToData("LAJES5PBR68MJQP067I7DLJO3RUGG0EUHSOAVOFIDF24KBDE0SEG", privKey2.d); | ||
72 | |||
73 | EcdhePublicKey pubKey1 = privKey1.getPublicKey(); | ||
74 | EcdhePublicKey pubKey2 = privKey2.getPublicKey(); | ||
75 | |||
76 | Assert.assertEquals("VU5U4KNC3OH5RUL6M5T6BPH52BFBICEG5Q60A43V97HQV1VD3AG0", pubKey1.toString()); | ||
77 | Assert.assertEquals("8DSB6D0OKS1SD71BKRTVT8OK18Q73Q7MUALJ76V9DQJO6J0170RG", pubKey2.toString()); | ||
78 | } | ||
79 | } | ||
diff --git a/src/test/java/org/gnunet/util/EcdsaTest.java b/src/test/java/org/gnunet/util/EcdsaTest.java new file mode 100644 index 0000000..898bc9a --- /dev/null +++ b/src/test/java/org/gnunet/util/EcdsaTest.java | |||
@@ -0,0 +1,45 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2012, 2013 Christian Grothoff (and other contributing authors) | ||
4 | |||
5 | GNUnet is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published | ||
7 | by the Free Software Foundation; either version 3, or (at your | ||
8 | option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with GNUnet; see the file COPYING. If not, write to the | ||
17 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | package org.gnunet.util; | ||
22 | |||
23 | import org.gnunet.util.crypto.*; | ||
24 | import org.junit.Assert; | ||
25 | import org.junit.Test; | ||
26 | |||
27 | public class EcdsaTest { | ||
28 | @Test | ||
29 | public void test_sign_success() { | ||
30 | byte[] data = "GNUnet".getBytes(); | ||
31 | EcdsaPrivateKey privateKey = EcdsaPrivateKey.createRandom(); | ||
32 | EcdsaPublicKey publicKey = privateKey.getPublicKey(); | ||
33 | EcdsaSignature signature = privateKey.sign(publicKey, 0, data); | ||
34 | Assert.assertTrue(signature.verify(data, 0, publicKey)); | ||
35 | } | ||
36 | |||
37 | @Test | ||
38 | public void test_sign_failure() { | ||
39 | byte[] data = "GNUnet".getBytes(); | ||
40 | EcdsaPrivateKey privateKey = EcdsaPrivateKey.createRandom(); | ||
41 | EcdsaPublicKey publicKey = privateKey.getPublicKey(); | ||
42 | EcdsaSignature signature = EcdsaSignature.randomGarbage(); | ||
43 | Assert.assertFalse(signature.verify(data, 0, publicKey)); | ||
44 | } | ||
45 | } | ||
diff --git a/src/test/java/org/gnunet/util/Ed25519Test.java b/src/test/java/org/gnunet/util/Ed25519Test.java new file mode 100644 index 0000000..4ae39a7 --- /dev/null +++ b/src/test/java/org/gnunet/util/Ed25519Test.java | |||
@@ -0,0 +1,142 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2012, 2013 Christian Grothoff (and other contributing authors) | ||
4 | |||
5 | GNUnet is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published | ||
7 | by the Free Software Foundation; either version 3, or (at your | ||
8 | option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with GNUnet; see the file COPYING. If not, write to the | ||
17 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | package org.gnunet.util; | ||
22 | |||
23 | import org.gnunet.util.crypto.Ed25519; | ||
24 | import org.junit.Assert; | ||
25 | import org.junit.Test; | ||
26 | |||
27 | import java.math.BigInteger; | ||
28 | import java.util.Random; | ||
29 | |||
30 | public class Ed25519Test { | ||
31 | |||
32 | /** | ||
33 | * Simple test for commutativity. | ||
34 | */ | ||
35 | @Test | ||
36 | public void test_commutative() { | ||
37 | Ed25519 p1 = Ed25519.B.scalarmult(BigInteger.valueOf(42)).scalarmult(BigInteger.valueOf(100)); | ||
38 | Ed25519 p2 = Ed25519.B.scalarmult(BigInteger.valueOf(100)).scalarmult(BigInteger.valueOf(42)); | ||
39 | |||
40 | byte[] data1 = p1.encode(); | ||
41 | byte[] data2 = p2.encode(); | ||
42 | Assert.assertArrayEquals(data1, data2); | ||
43 | } | ||
44 | |||
45 | |||
46 | /** | ||
47 | * Raw ECDH check. | ||
48 | */ | ||
49 | //@Test | ||
50 | public void test_ecdh() { | ||
51 | for (int i = 0; i < 5; ++i) { | ||
52 | System.out.println("try " + i); | ||
53 | byte[] d1 = new byte[32]; | ||
54 | byte[] d2 = new byte[32]; | ||
55 | Random r = new Random(); | ||
56 | r.nextBytes(d1); | ||
57 | r.nextBytes(d2); | ||
58 | d1[0] &= 248; | ||
59 | d1[31] &= 127; | ||
60 | d1[31] |= 64; | ||
61 | d2[0] &= 248; | ||
62 | d2[31] &= 127; | ||
63 | d2[31] |= 64; | ||
64 | |||
65 | |||
66 | Ed25519 pk1 = Ed25519.B.scalarmult(Ed25519.decodeScalar(d1)); | ||
67 | Ed25519 pk2 = Ed25519.B.scalarmult(Ed25519.decodeScalar(d2)); | ||
68 | byte[] pk1Data = pk1.encode(); | ||
69 | byte[] pk2Data = pk2.encode(); | ||
70 | |||
71 | Assert.assertEquals(pk1, Ed25519.decode(pk1Data)); | ||
72 | Assert.assertEquals(pk2, Ed25519.decode(pk2Data)); | ||
73 | |||
74 | |||
75 | |||
76 | byte[] data1 = Ed25519.decode(pk1Data).scalarmult(Ed25519.decodeScalar(d2)).encode(); | ||
77 | byte[] data2 = Ed25519.decode(pk2Data).scalarmult(Ed25519.decodeScalar(d1)).encode(); | ||
78 | |||
79 | |||
80 | |||
81 | /* | ||
82 | Ed25519 p1 = Ed25519.B.scalarmult(Ed25519.decodeScalar(d1)).scalarmult(Ed25519.decodeScalar(d2)); | ||
83 | Ed25519 p2 = Ed25519.B.scalarmult(Ed25519.decodeScalar(d2)).scalarmult(Ed25519.decodeScalar(d1)); | ||
84 | byte[] data1 = p1.encode(); | ||
85 | byte[] data2 = p2.encode(); | ||
86 | */ | ||
87 | |||
88 | Assert.assertArrayEquals(data1, data2); | ||
89 | } | ||
90 | } | ||
91 | |||
92 | /** | ||
93 | * Test if decode is the inverse of encode. | ||
94 | */ | ||
95 | //@Test | ||
96 | public void test_encode_inverse() { | ||
97 | Random r = new Random(); | ||
98 | for (int i = 0; i < 100; i++) { | ||
99 | byte[] d1 = new byte[32]; | ||
100 | r.nextBytes(d1); | ||
101 | d1[31] &= 127; | ||
102 | Ed25519 p1 = Ed25519.B.scalarmult(Ed25519.decodeScalar(d1)); | ||
103 | // encode and decode the same value! | ||
104 | byte[] data1 = p1.encode(); | ||
105 | Ed25519 p2 = Ed25519.decode(data1); | ||
106 | Assert.assertEquals(p1, p2); | ||
107 | Assert.assertArrayEquals(p1.encode(), p2.encode()); | ||
108 | } | ||
109 | } | ||
110 | |||
111 | @Test | ||
112 | public void test_encode_inverse_simple() { | ||
113 | Ed25519 p1 = Ed25519.B; | ||
114 | // encode and decode the same value! | ||
115 | byte[] data1 = p1.encode(); | ||
116 | for (int i = 0; i < 32; i++) { | ||
117 | System.out.print(data1[i] + " "); | ||
118 | } | ||
119 | System.out.println(); | ||
120 | Ed25519 p2 = Ed25519.decode(data1); | ||
121 | Assert.assertArrayEquals(p1.encode(), p2.encode()); | ||
122 | Assert.assertEquals(p1, p2); | ||
123 | } | ||
124 | |||
125 | /** | ||
126 | * | ||
127 | */ | ||
128 | @Test | ||
129 | public void test_encode_scalar() { | ||
130 | Random r = new Random(); | ||
131 | for (int i = 0; i < 10; i++) { | ||
132 | byte[] d1 = new byte[32]; | ||
133 | r.nextBytes(d1); | ||
134 | d1[31] &= 127; | ||
135 | BigInteger s1 = Ed25519.decodeScalar(d1); | ||
136 | byte[] d2 = Ed25519.encodeScalar(s1); | ||
137 | BigInteger s2 = Ed25519.decodeScalar(d2); | ||
138 | Assert.assertEquals(s1, s2); | ||
139 | Assert.assertArrayEquals(d1, d2); | ||
140 | } | ||
141 | } | ||
142 | } | ||
diff --git a/src/test/java/org/gnunet/util/EddsaTest.java b/src/test/java/org/gnunet/util/EddsaTest.java index 302751b..4296440 100644 --- a/src/test/java/org/gnunet/util/EddsaTest.java +++ b/src/test/java/org/gnunet/util/EddsaTest.java | |||
@@ -30,10 +30,19 @@ import org.junit.Test; | |||
30 | public class EddsaTest { | 30 | public class EddsaTest { |
31 | @Test | 31 | @Test |
32 | public void test_eddsa_sign_success() { | 32 | public void test_eddsa_sign_success() { |
33 | byte[] data = "foo".getBytes(); | 33 | byte[] data = "GNUnet".getBytes(); |
34 | EddsaPrivateKey privateKey = EddsaPrivateKey.createRandom(); | 34 | EddsaPrivateKey privateKey = EddsaPrivateKey.createRandom(); |
35 | EddsaPublicKey publicKey = privateKey.getPublicKey(); | 35 | EddsaPublicKey publicKey = privateKey.getPublicKey(); |
36 | EddsaSignature signature = privateKey.sign(publicKey, 0, data); | 36 | EddsaSignature signature = privateKey.sign(publicKey, 0, data); |
37 | Assert.assertTrue(signature.verify(data, 0, publicKey)); | 37 | Assert.assertTrue(signature.verify(data, 0, publicKey)); |
38 | } | 38 | } |
39 | |||
40 | @Test | ||
41 | public void test_eddsa_sign_failure() { | ||
42 | byte[] data = "GNUnet".getBytes(); | ||
43 | EddsaPrivateKey privateKey = EddsaPrivateKey.createRandom(); | ||
44 | EddsaPublicKey publicKey = privateKey.getPublicKey(); | ||
45 | EddsaSignature signature = EddsaSignature.randomGarbage(); | ||
46 | Assert.assertFalse(signature.verify(data, 0, publicKey)); | ||
47 | } | ||
39 | } | 48 | } |