1 /*
2     James Robson
3     Public domain.
4 */
5 
6 public class Curve25519Donna {
7 
8     final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();
9 
bytesToHex(byte[] bytes)10     public static String bytesToHex(byte[] bytes) {
11         char[] hexChars = new char[bytes.length * 2];
12         int v;
13         for ( int j = 0; j < bytes.length; j++ ) {
14             v = bytes[j] & 0xFF;
15             hexChars[j * 2] = hexArray[v >>> 4];
16             hexChars[j * 2 + 1] = hexArray[v & 0x0F];
17         }
18         return new String(hexChars);
19     }
20 
curve25519Donna(byte[] a, byte[] b)21     public native byte[] curve25519Donna(byte[] a, byte[] b);
makePrivate(byte[] secret)22     public native byte[] makePrivate(byte[] secret);
getPublic(byte[] privkey)23     public native byte[] getPublic(byte[] privkey);
makeSharedSecret(byte[] privkey, byte[] theirPubKey)24     public native byte[] makeSharedSecret(byte[] privkey, byte[] theirPubKey);
helowrld()25     public native void helowrld();
26 
27     // Uncomment if your Java is 32-bit:
28     //static { System.loadLibrary("Curve25519Donna"); }
29 
30     // Otherwise, load this 64-bit .jnilib:
31     static { System.loadLibrary("Curve25519Donna_64"); }
32 
33     /*
34         To give the old tires a kick (OSX):
35         java -cp `pwd` Curve25519Donna
36     */
main(String[] args)37     public static void main (String[] args) {
38 
39         Curve25519Donna c = new Curve25519Donna();
40 
41         // These should be 32 bytes long
42         byte[] user1Secret = "abcdefghijklmnopqrstuvwxyz123456".getBytes();
43         byte[] user2Secret = "654321zyxwvutsrqponmlkjihgfedcba".getBytes();
44 
45 
46         // You can use the curve function directly...
47 
48         //byte[] o = c.curve25519Donna(a, b);
49         //System.out.println("o = " + bytesToHex(o));
50 
51 
52         // ... but it's not really necessary. Just use the following
53         // convenience methods:
54 
55         byte[] privKey = c.makePrivate(user1Secret);
56         byte[] pubKey = c.getPublic(privKey);
57 
58         byte[] privKey2 = c.makePrivate(user2Secret);
59         byte[] pubKey2 = c.getPublic(privKey2);
60 
61         System.out.println("'user1' privKey = " + bytesToHex(privKey));
62         System.out.println("'user1' pubKey = " + bytesToHex(pubKey));
63         System.out.println("===================================================");
64 
65         System.out.println("'user2' privKey = " + bytesToHex(privKey2));
66         System.out.println("'user2' pubKey = " + bytesToHex(pubKey2));
67         System.out.println("===================================================");
68 
69 
70         byte[] ss1 = c.makeSharedSecret(privKey, pubKey2);
71         System.out.println("'user1' computes shared secret: " + bytesToHex(ss1));
72 
73         byte[] ss2 = c.makeSharedSecret(privKey2, pubKey);
74         System.out.println("'user2' computes shared secret: " + bytesToHex(ss2));
75 
76     }
77 }
78