diff options
author | Florian Dold <florian.dold@gmail.com> | 2013-10-29 02:37:48 +0000 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2013-10-29 02:37:48 +0000 |
commit | 56991ab6ca6a639cc9cfa6bd3b9a195a95f13d69 (patch) | |
tree | 17e6e85abef96fe8e1c135b80fd3d8d51731d38c /src/main/java/org/gnunet/util/crypto | |
parent | 93c0e940bcfc83681cce859aac7f07c4470fe88a (diff) | |
download | gnunet-java-56991ab6ca6a639cc9cfa6bd3b9a195a95f13d69.tar.gz gnunet-java-56991ab6ca6a639cc9cfa6bd3b9a195a95f13d69.zip |
- automatic test for voting
- signed messages
Diffstat (limited to 'src/main/java/org/gnunet/util/crypto')
5 files changed, 156 insertions, 2 deletions
diff --git a/src/main/java/org/gnunet/util/crypto/EcdsaPrivateKey.java b/src/main/java/org/gnunet/util/crypto/EcdsaPrivateKey.java index dca60b2..43bd55a 100644 --- a/src/main/java/org/gnunet/util/crypto/EcdsaPrivateKey.java +++ b/src/main/java/org/gnunet/util/crypto/EcdsaPrivateKey.java | |||
@@ -23,7 +23,17 @@ public class EcdsaPrivateKey implements Message { | |||
23 | } | 23 | } |
24 | 24 | ||
25 | public EcdsaPublicKey getPublicKey() { | 25 | public EcdsaPublicKey getPublicKey() { |
26 | return EcdsaPublicKey.random(); | 26 | // FIXME: this is not the real implementation, |
27 | // beware that this dummy implementation leaks the key! | ||
28 | EcdsaPublicKey publicKey = new EcdsaPublicKey(); | ||
29 | byte[] v = new byte[32]; | ||
30 | System.arraycopy(d, 0, v, 0, 8); | ||
31 | System.arraycopy(d, 0, v, 8, 8); | ||
32 | System.arraycopy(d, 0, v, 16, 8); | ||
33 | System.arraycopy(d, 0, v, 24, 8); | ||
34 | System.arraycopy(v, 0, publicKey.x, 0, 32); | ||
35 | System.arraycopy(v, 0, publicKey.y, 0, 32); | ||
36 | return publicKey; | ||
27 | } | 37 | } |
28 | 38 | ||
29 | public static EcdsaPrivateKey createRandom() { | 39 | public static EcdsaPrivateKey createRandom() { |
diff --git a/src/main/java/org/gnunet/util/crypto/EcdsaSignature.java b/src/main/java/org/gnunet/util/crypto/EcdsaSignature.java index 1589b67..b668a3f 100644 --- a/src/main/java/org/gnunet/util/crypto/EcdsaSignature.java +++ b/src/main/java/org/gnunet/util/crypto/EcdsaSignature.java | |||
@@ -48,7 +48,7 @@ public class EcdsaSignature implements Message { | |||
48 | this.s = new byte[32]; | 48 | this.s = new byte[32]; |
49 | } | 49 | } |
50 | 50 | ||
51 | public boolean verify(byte[] m, int purpose, EddsaPublicKey publicKey) { | 51 | public boolean verify(byte[] m, int purpose, EcdsaPublicKey publicKey) { |
52 | return false; | 52 | return false; |
53 | } | 53 | } |
54 | 54 | ||
diff --git a/src/main/java/org/gnunet/util/crypto/EcdsaSignedMessage.java b/src/main/java/org/gnunet/util/crypto/EcdsaSignedMessage.java new file mode 100644 index 0000000..0cb293a --- /dev/null +++ b/src/main/java/org/gnunet/util/crypto/EcdsaSignedMessage.java | |||
@@ -0,0 +1,60 @@ | |||
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.crypto; | ||
22 | |||
23 | import org.gnunet.construct.Construct; | ||
24 | import org.gnunet.construct.Message; | ||
25 | import org.gnunet.construct.NestedMessage; | ||
26 | import org.gnunet.construct.UInt32; | ||
27 | |||
28 | /** | ||
29 | * A message together with a signature on the message and it's purpose. | ||
30 | */ | ||
31 | public class EcdsaSignedMessage<M extends Message> implements Message { | ||
32 | @NestedMessage | ||
33 | public EcdsaSignature signature; | ||
34 | @UInt32 | ||
35 | public int purpose; | ||
36 | @NestedMessage | ||
37 | public M innerMessage; | ||
38 | |||
39 | public EcdsaSignedMessage() { | ||
40 | // empty constructor required by org.gnunet.construct | ||
41 | } | ||
42 | |||
43 | public boolean verify(EcdsaPublicKey signerPublicKey) { | ||
44 | return signature.verify(Construct.toBinary(innerMessage), purpose, signerPublicKey); | ||
45 | } | ||
46 | |||
47 | public static <T extends Message> EcdsaSignedMessage<T> signMessage(T innerMessage, int purpose, | ||
48 | EcdsaPrivateKey privateKey, EcdsaPublicKey publicKey) { | ||
49 | EcdsaSignedMessage<T> esm = new EcdsaSignedMessage<T>(); | ||
50 | esm.purpose = purpose; | ||
51 | esm.innerMessage = innerMessage; | ||
52 | esm.signature = privateKey.sign(purpose, Construct.toBinary(innerMessage)); | ||
53 | return esm; | ||
54 | } | ||
55 | |||
56 | public static <T extends Message> EcdsaSignedMessage<T> signMessage(T innerMessage, int purpose, | ||
57 | EcdsaPrivateKey privateKey) { | ||
58 | return signMessage(innerMessage, purpose, privateKey, privateKey.getPublicKey()); | ||
59 | } | ||
60 | } | ||
diff --git a/src/main/java/org/gnunet/util/crypto/EddsaPrivateKey.java b/src/main/java/org/gnunet/util/crypto/EddsaPrivateKey.java index 973706f..21aa647 100644 --- a/src/main/java/org/gnunet/util/crypto/EddsaPrivateKey.java +++ b/src/main/java/org/gnunet/util/crypto/EddsaPrivateKey.java | |||
@@ -13,6 +13,10 @@ public class EddsaPrivateKey implements Message { | |||
13 | @FixedSizeIntegerArray(bitSize = 8, signed = false, length = 32) | 13 | @FixedSizeIntegerArray(bitSize = 8, signed = false, length = 32) |
14 | public byte[] d; | 14 | public byte[] d; |
15 | 15 | ||
16 | public EddsaSignature sign(int purpose, byte[] m) { | ||
17 | return sign(getPublicKey(), purpose, m); | ||
18 | } | ||
19 | |||
16 | public EddsaSignature sign(EddsaPublicKey publicKey, int purpose, byte[] m) { | 20 | public EddsaSignature sign(EddsaPublicKey publicKey, int purpose, byte[] m) { |
17 | MessageDigest sha512; | 21 | MessageDigest sha512; |
18 | try { | 22 | try { |
diff --git a/src/main/java/org/gnunet/util/crypto/EddsaSignedMessage.java b/src/main/java/org/gnunet/util/crypto/EddsaSignedMessage.java new file mode 100644 index 0000000..27f969a --- /dev/null +++ b/src/main/java/org/gnunet/util/crypto/EddsaSignedMessage.java | |||
@@ -0,0 +1,80 @@ | |||
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 | /* | ||
22 | This file is part of GNUnet. | ||
23 | (C) 2012, 2013 Christian Grothoff (and other contributing authors) | ||
24 | |||
25 | GNUnet is free software; you can redistribute it and/or modify | ||
26 | it under the terms of the GNU General Public License as published | ||
27 | by the Free Software Foundation; either version 3, or (at your | ||
28 | option) any later version. | ||
29 | |||
30 | GNUnet is distributed in the hope that it will be useful, but | ||
31 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
32 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
33 | General Public License for more details. | ||
34 | |||
35 | You should have received a copy of the GNU General Public License | ||
36 | along with GNUnet; see the file COPYING. If not, write to the | ||
37 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
38 | Boston, MA 02111-1307, USA. | ||
39 | */ | ||
40 | |||
41 | package org.gnunet.util.crypto; | ||
42 | |||
43 | import org.gnunet.construct.Construct; | ||
44 | import org.gnunet.construct.Message; | ||
45 | import org.gnunet.construct.NestedMessage; | ||
46 | import org.gnunet.construct.UInt32; | ||
47 | |||
48 | /** | ||
49 | * A message together with a signature on the message and it's purpose. | ||
50 | */ | ||
51 | public class EddsaSignedMessage<M extends Message> implements Message { | ||
52 | @NestedMessage | ||
53 | public EddsaSignature signature; | ||
54 | @UInt32 | ||
55 | public int purpose; | ||
56 | @NestedMessage | ||
57 | public M innerMessage; | ||
58 | |||
59 | public EddsaSignedMessage() { | ||
60 | // empty constructor required by org.gnunet.construct | ||
61 | } | ||
62 | |||
63 | public boolean verify(EddsaPublicKey signerPublicKey) { | ||
64 | return signature.verify(Construct.toBinary(innerMessage), purpose, signerPublicKey); | ||
65 | } | ||
66 | |||
67 | public static <T extends Message> EddsaSignedMessage<T> signMessage(T innerMessage, int purpose, | ||
68 | EddsaPrivateKey privateKey, EddsaPublicKey publicKey) { | ||
69 | EddsaSignedMessage<T> esm = new EddsaSignedMessage<T>(); | ||
70 | esm.purpose = purpose; | ||
71 | esm.innerMessage = innerMessage; | ||
72 | esm.signature = privateKey.sign(publicKey, purpose, Construct.toBinary(innerMessage)); | ||
73 | return esm; | ||
74 | } | ||
75 | |||
76 | public static <T extends Message> EddsaSignedMessage<T> signMessage(T innerMessage, int purpose, | ||
77 | EddsaPrivateKey privateKey) { | ||
78 | return signMessage(innerMessage, purpose, privateKey, privateKey.getPublicKey()); | ||
79 | } | ||
80 | } | ||