aboutsummaryrefslogtreecommitdiff
path: root/contrib/Curve25519Donna.java
diff options
context:
space:
mode:
authorMark Haines <mjark@negativecurvature.net>2015-02-26 16:40:56 +0000
committerMark Haines <mjark@negativecurvature.net>2015-02-26 16:40:56 +0000
commite50ac707316ea6d8059f7036322450727773952d (patch)
tree0cd42bcd79e20f54f4944522576a1e35e231ddd5 /contrib/Curve25519Donna.java
Squashed 'lib/curve25519-donna/' content from commit 28772f3
git-subtree-dir: lib/curve25519-donna git-subtree-split: 28772f37a4b8a57ab9439b9e79b19f9abee686da
Diffstat (limited to 'contrib/Curve25519Donna.java')
-rw-r--r--contrib/Curve25519Donna.java77
1 files changed, 77 insertions, 0 deletions
diff --git a/contrib/Curve25519Donna.java b/contrib/Curve25519Donna.java
new file mode 100644
index 0000000..e28cb53
--- /dev/null
+++ b/contrib/Curve25519Donna.java
@@ -0,0 +1,77 @@
+/*
+ James Robson
+ Public domain.
+*/
+
+public class Curve25519Donna {
+
+ final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();
+
+ public static String bytesToHex(byte[] bytes) {
+ char[] hexChars = new char[bytes.length * 2];
+ int v;
+ for ( int j = 0; j < bytes.length; j++ ) {
+ v = bytes[j] & 0xFF;
+ hexChars[j * 2] = hexArray[v >>> 4];
+ hexChars[j * 2 + 1] = hexArray[v & 0x0F];
+ }
+ return new String(hexChars);
+ }
+
+ public native byte[] curve25519Donna(byte[] a, byte[] b);
+ public native byte[] makePrivate(byte[] secret);
+ public native byte[] getPublic(byte[] privkey);
+ public native byte[] makeSharedSecret(byte[] privkey, byte[] theirPubKey);
+ public native void helowrld();
+
+ // Uncomment if your Java is 32-bit:
+ //static { System.loadLibrary("Curve25519Donna"); }
+
+ // Otherwise, load this 64-bit .jnilib:
+ static { System.loadLibrary("Curve25519Donna_64"); }
+
+ /*
+ To give the old tires a kick (OSX):
+ java -cp `pwd` Curve25519Donna
+ */
+ public static void main (String[] args) {
+
+ Curve25519Donna c = new Curve25519Donna();
+
+ // These should be 32 bytes long
+ byte[] user1Secret = "abcdefghijklmnopqrstuvwxyz123456".getBytes();
+ byte[] user2Secret = "654321zyxwvutsrqponmlkjihgfedcba".getBytes();
+
+
+ // You can use the curve function directly...
+
+ //byte[] o = c.curve25519Donna(a, b);
+ //System.out.println("o = " + bytesToHex(o));
+
+
+ // ... but it's not really necessary. Just use the following
+ // convenience methods:
+
+ byte[] privKey = c.makePrivate(user1Secret);
+ byte[] pubKey = c.getPublic(privKey);
+
+ byte[] privKey2 = c.makePrivate(user2Secret);
+ byte[] pubKey2 = c.getPublic(privKey2);
+
+ System.out.println("'user1' privKey = " + bytesToHex(privKey));
+ System.out.println("'user1' pubKey = " + bytesToHex(pubKey));
+ System.out.println("===================================================");
+
+ System.out.println("'user2' privKey = " + bytesToHex(privKey2));
+ System.out.println("'user2' pubKey = " + bytesToHex(pubKey2));
+ System.out.println("===================================================");
+
+
+ byte[] ss1 = c.makeSharedSecret(privKey, pubKey2);
+ System.out.println("'user1' computes shared secret: " + bytesToHex(ss1));
+
+ byte[] ss2 = c.makeSharedSecret(privKey2, pubKey);
+ System.out.println("'user2' computes shared secret: " + bytesToHex(ss2));
+
+ }
+}