aboutsummaryrefslogtreecommitdiff
path: root/lib/curve25519-donna/contrib/Curve25519Donna.java
blob: e28cb53bbd4eaf275d12fdcc26e60b8b837347f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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));

    }
}