Skip to content

Commit

Permalink
Swig ed25519 api refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
aldenml committed Feb 4, 2016
1 parent 21fdcd5 commit 3f49c32
Show file tree
Hide file tree
Showing 10 changed files with 1,127 additions and 1,387 deletions.
59 changes: 30 additions & 29 deletions src/main/java/com/frostwire/jlibtorrent/Ed25519.java
@@ -1,20 +1,21 @@
package com.frostwire.jlibtorrent;

import com.frostwire.jlibtorrent.swig.char_vector;
import com.frostwire.jlibtorrent.swig.ed25519;
import com.frostwire.jlibtorrent.swig.byte_vector;

import static com.frostwire.jlibtorrent.swig.libtorrent.*;

/**
* @author gubatron
* @author aldenml
*/
public final class Ed25519 {

public final static int SEED_SIZE = ed25519.seed_size;
public final static int PRIVATE_KEY_SIZE = ed25519.private_key_size;
public final static int PUBLIC_KEY_SIZE = ed25519.public_key_size;
public final static int SIGNATURE_SIZE = ed25519.signature_size;
public final static int SCALAR_SIZE = ed25519.scalar_size;
public final static int SHARED_SECRET_SIZE = ed25519.shared_secret_size;
public final static int SEED_SIZE = ed25519_seed_size;
public final static int PRIVATE_KEY_SIZE = ed25519_private_key_size;
public final static int PUBLIC_KEY_SIZE = ed25519_public_key_size;
public final static int SIGNATURE_SIZE = ed25519_signature_size;
public final static int SCALAR_SIZE = ed25519_scalar_size;
public final static int SHARED_SECRET_SIZE = ed25519_shared_secret_size;

private Ed25519() {
}
Expand All @@ -23,10 +24,10 @@ public static void createSeed(byte[] seed) {
if (seed == null || seed.length != SEED_SIZE) {
throw new IllegalArgumentException("seed buffer must be not null and of size " + SEED_SIZE);
}
char_vector v = Vectors.new_char_vector(SEED_SIZE);
ed25519.create_seed(v);
byte_vector v = Vectors.new_byte_vector(SEED_SIZE);
ed25519_create_seed(v);

Vectors.char_vector2bytes(v, seed);
Vectors.byte_vector2bytes(v, seed);
}

public static void createKeypair(byte[] publicKey, byte[] privateKey, byte[] seed) {
Expand All @@ -37,29 +38,29 @@ public static void createKeypair(byte[] publicKey, byte[] privateKey, byte[] see
throw new IllegalArgumentException("private key buffer must be not null and of size " + PRIVATE_KEY_SIZE);
}

char_vector v1 = Vectors.new_char_vector(PUBLIC_KEY_SIZE);
char_vector v2 = Vectors.new_char_vector(PRIVATE_KEY_SIZE);
byte_vector v1 = Vectors.new_byte_vector(PUBLIC_KEY_SIZE);
byte_vector v2 = Vectors.new_byte_vector(PRIVATE_KEY_SIZE);

ed25519.create_keypair(v1, v2, Vectors.bytes2char_vector(seed));
ed25519_create_keypair(v1, v2, Vectors.bytes2byte_vector(seed));

Vectors.char_vector2bytes(v1, publicKey);
Vectors.char_vector2bytes(v2, privateKey);
Vectors.byte_vector2bytes(v1, publicKey);
Vectors.byte_vector2bytes(v2, privateKey);
}

public static void sign(byte[] signature, byte[] message, byte[] publicKey, byte[] privateKey) {
if (signature == null || signature.length != SIGNATURE_SIZE) {
throw new IllegalArgumentException("signature buffer must be not null and of size " + SIGNATURE_SIZE);
}

char_vector v1 = Vectors.new_char_vector(SIGNATURE_SIZE);
byte_vector v1 = Vectors.new_byte_vector(SIGNATURE_SIZE);

ed25519.sign(v1, Vectors.bytes2char_vector(message), Vectors.bytes2char_vector(publicKey), Vectors.bytes2char_vector(privateKey));
ed25519_sign(v1, Vectors.bytes2byte_vector(message), Vectors.bytes2byte_vector(publicKey), Vectors.bytes2byte_vector(privateKey));

Vectors.char_vector2bytes(v1, signature);
Vectors.byte_vector2bytes(v1, signature);
}

public static int verify(byte[] signature, byte[] message, byte[] privateKey) {
return ed25519.verify(Vectors.bytes2char_vector(signature), Vectors.bytes2char_vector(message), Vectors.bytes2char_vector(privateKey));
return ed25519_verify(Vectors.bytes2byte_vector(signature), Vectors.bytes2byte_vector(message), Vectors.bytes2byte_vector(privateKey));
}

public static void addScalar(byte[] publicKey, byte[] privateKey, byte[] scalar) {
Expand All @@ -73,25 +74,25 @@ public static void addScalar(byte[] publicKey, byte[] privateKey, byte[] scalar)
throw new IllegalArgumentException("scalar must be not null and of size " + SCALAR_SIZE);
}

char_vector v1 = Vectors.bytes2char_vector(publicKey);
char_vector v2 = Vectors.bytes2char_vector(privateKey);
char_vector v3 = Vectors.bytes2char_vector(scalar);
byte_vector v1 = Vectors.bytes2byte_vector(publicKey);
byte_vector v2 = Vectors.bytes2byte_vector(privateKey);
byte_vector v3 = Vectors.bytes2byte_vector(scalar);

ed25519.add_scalar(v1, v2, v3);
ed25519_add_scalar(v1, v2, v3);

Vectors.char_vector2bytes(v1, publicKey);
Vectors.char_vector2bytes(v2, privateKey);
Vectors.byte_vector2bytes(v1, publicKey);
Vectors.byte_vector2bytes(v2, privateKey);
}

public static void keyExchange(byte[] sharedSecret, byte[] publicKey, byte[] privateKey) {
if (sharedSecret == null || sharedSecret.length != SHARED_SECRET_SIZE) {
throw new IllegalArgumentException("shared secret buffer must be not null and of size " + SHARED_SECRET_SIZE);
}

char_vector v1 = Vectors.new_char_vector(SHARED_SECRET_SIZE);
byte_vector v1 = Vectors.new_byte_vector(SHARED_SECRET_SIZE);

ed25519.key_exchange(v1, Vectors.bytes2char_vector(publicKey), Vectors.bytes2char_vector(privateKey));
ed25519_key_exchange(v1, Vectors.bytes2byte_vector(publicKey), Vectors.bytes2byte_vector(privateKey));

Vectors.char_vector2bytes(v1, sharedSecret);
Vectors.byte_vector2bytes(v1, sharedSecret);
}
}
37 changes: 21 additions & 16 deletions src/main/java/com/frostwire/jlibtorrent/Vectors.java
@@ -1,9 +1,6 @@
package com.frostwire.jlibtorrent;

import com.frostwire.jlibtorrent.swig.char_vector;
import com.frostwire.jlibtorrent.swig.int64_vector;
import com.frostwire.jlibtorrent.swig.int_vector;
import com.frostwire.jlibtorrent.swig.unsigned_char_vector;
import com.frostwire.jlibtorrent.swig.*;

/**
* @author gubatron
Expand All @@ -25,6 +22,14 @@ public static byte[] char_vector2bytes(char_vector v) {
return arr;
}

public static void byte_vector2bytes(byte_vector v, byte[] arr) {
int size = (int) v.size();

for (int i = 0; i < size; i++) {
arr[i] = v.get(i);
}
}

public static void char_vector2bytes(char_vector v, byte[] arr) {
int size = (int) v.size();

Expand Down Expand Up @@ -63,18 +68,8 @@ public static char_vector bytes2char_vector(byte[] arr) {
return v;
}

public static unsigned_char_vector bytes2unsigned_char_vector(byte[] arr) {
unsigned_char_vector v = new unsigned_char_vector();

for (int i = 0; i < arr.length; i++) {
v.add((short) arr[i]);
}

return v;
}

public static int_vector bytes2int_vector(byte[] arr) {
int_vector v = new int_vector();
public static byte_vector bytes2byte_vector(byte[] arr) {
byte_vector v = new byte_vector();

for (int i = 0; i < arr.length; i++) {
v.add(arr[i]);
Expand Down Expand Up @@ -133,6 +128,16 @@ public static char_vector new_char_vector(int size) {
return v;
}

public static byte_vector new_byte_vector(int size) {
byte_vector v = new byte_vector();
byte z = (byte) 0;
for (int i = 0; i < size; i++) {
v.add(z);
}

return v;
}

public static unsigned_char_vector priorities2unsigned_char_vector(Priority[] arr) {
unsigned_char_vector v = new unsigned_char_vector();

Expand Down
72 changes: 0 additions & 72 deletions src/main/java/com/frostwire/jlibtorrent/swig/ed25519.java

This file was deleted.

24 changes: 24 additions & 0 deletions src/main/java/com/frostwire/jlibtorrent/swig/libtorrent.java
Expand Up @@ -193,6 +193,30 @@ public static high_resolution_clock.duration to_hours(long n) {
return new high_resolution_clock.duration(libtorrent_jni.to_hours(n), true);
}

public static void ed25519_create_seed(byte_vector seed) {
libtorrent_jni.ed25519_create_seed(byte_vector.getCPtr(seed), seed);
}

public static void ed25519_create_keypair(byte_vector public_key, byte_vector private_key, byte_vector seed) {
libtorrent_jni.ed25519_create_keypair(byte_vector.getCPtr(public_key), public_key, byte_vector.getCPtr(private_key), private_key, byte_vector.getCPtr(seed), seed);
}

public static void ed25519_sign(byte_vector signature, byte_vector message, byte_vector public_key, byte_vector private_key) {
libtorrent_jni.ed25519_sign(byte_vector.getCPtr(signature), signature, byte_vector.getCPtr(message), message, byte_vector.getCPtr(public_key), public_key, byte_vector.getCPtr(private_key), private_key);
}

public static int ed25519_verify(byte_vector signature, byte_vector message, byte_vector private_key) {
return libtorrent_jni.ed25519_verify(byte_vector.getCPtr(signature), signature, byte_vector.getCPtr(message), message, byte_vector.getCPtr(private_key), private_key);
}

public static void ed25519_add_scalar(byte_vector public_key, byte_vector private_key, byte_vector scalar) {
libtorrent_jni.ed25519_add_scalar(byte_vector.getCPtr(public_key), public_key, byte_vector.getCPtr(private_key), private_key, byte_vector.getCPtr(scalar), scalar);
}

public static void ed25519_key_exchange(byte_vector shared_secret, byte_vector public_key, byte_vector private_key) {
libtorrent_jni.ed25519_key_exchange(byte_vector.getCPtr(shared_secret), shared_secret, byte_vector.getCPtr(public_key), public_key, byte_vector.getCPtr(private_key), private_key);
}

public static boolean add_files_cb(String p, add_files_listener listener) {
return libtorrent_jni.add_files_cb(p, add_files_listener.getCPtr(listener), listener);
}
Expand Down
Expand Up @@ -19,6 +19,13 @@ public interface libtorrentConstants {
public final static int num_alert_types = libtorrent_jni.num_alert_types_get();

public final static int TORRENT_ALERT_MANAGER_MAX_ARITY = libtorrent_jni.TORRENT_ALERT_MANAGER_MAX_ARITY_get();
public final static int ed25519_seed_size = libtorrent_jni.ed25519_seed_size_get();
public final static int ed25519_private_key_size = libtorrent_jni.ed25519_private_key_size_get();
public final static int ed25519_public_key_size = libtorrent_jni.ed25519_public_key_size_get();
public final static int ed25519_signature_size = libtorrent_jni.ed25519_signature_size_get();
public final static int ed25519_scalar_size = libtorrent_jni.ed25519_scalar_size_get();
public final static int ed25519_shared_secret_size = libtorrent_jni.ed25519_shared_secret_size_get();

public final static String LIBTORRENT_REVISION_SHA1 = libtorrent_jni.LIBTORRENT_REVISION_SHA1_get();
public final static String JLIBTORRENT_REVISION_SHA1 = libtorrent_jni.JLIBTORRENT_REVISION_SHA1_get();
}
26 changes: 12 additions & 14 deletions src/main/java/com/frostwire/jlibtorrent/swig/libtorrent_jni.java
Expand Up @@ -2972,6 +2972,12 @@ public class libtorrent_jni {
public final static native boolean torrent_status_stop_when_ready_get(long jarg1, torrent_status jarg1_);
public final static native void torrent_status_info_hash_set(long jarg1, torrent_status jarg1_, long jarg2, sha1_hash jarg2_);
public final static native long torrent_status_info_hash_get(long jarg1, torrent_status jarg1_);
public final static native int ed25519_seed_size_get();
public final static native int ed25519_private_key_size_get();
public final static native int ed25519_public_key_size_get();
public final static native int ed25519_signature_size_get();
public final static native int ed25519_scalar_size_get();
public final static native int ed25519_shared_secret_size_get();
public final static native long new_address__SWIG_0();
public final static native long new_address__SWIG_1(long jarg1, address_v4 jarg1_);
public final static native long new_address__SWIG_2(long jarg1, address_v6 jarg1_);
Expand Down Expand Up @@ -3050,20 +3056,6 @@ public class libtorrent_jni {
public final static native void delete_udp_endpoint(long jarg1);
public final static native boolean is_utp_stream_logging();
public final static native void set_utp_stream_logging(boolean jarg1);
public final static native int ed25519_seed_size_get();
public final static native int ed25519_private_key_size_get();
public final static native int ed25519_public_key_size_get();
public final static native int ed25519_signature_size_get();
public final static native int ed25519_scalar_size_get();
public final static native int ed25519_shared_secret_size_get();
public final static native void ed25519_create_seed(long jarg1, char_vector jarg1_);
public final static native void ed25519_create_keypair(long jarg1, char_vector jarg1_, long jarg2, char_vector jarg2_, long jarg3, char_vector jarg3_);
public final static native void ed25519_sign(long jarg1, char_vector jarg1_, long jarg2, char_vector jarg2_, long jarg3, char_vector jarg3_, long jarg4, char_vector jarg4_);
public final static native int ed25519_verify(long jarg1, char_vector jarg1_, long jarg2, char_vector jarg2_, long jarg3, char_vector jarg3_);
public final static native void ed25519_add_scalar(long jarg1, char_vector jarg1_, long jarg2, char_vector jarg2_, long jarg3, char_vector jarg3_);
public final static native void ed25519_key_exchange(long jarg1, char_vector jarg1_, long jarg2, char_vector jarg2_, long jarg3, char_vector jarg3_);
public final static native long new_ed25519();
public final static native void delete_ed25519(long jarg1);
public final static native int dht_item_canonical_string(long jarg1, char_vector jarg1_, int jarg2, String jarg3, long jarg4, char_vector jarg4_);
public final static native long dht_item_item_target_id__SWIG_0(long jarg1, char_vector jarg1_);
public final static native long dht_item_item_target_id__SWIG_1(long jarg1, char_vector jarg1_, long jarg2, char_vector jarg2_);
Expand All @@ -3078,6 +3070,12 @@ public class libtorrent_jni {
public final static native long to_microseconds(long jarg1);
public final static native long to_minutes(long jarg1);
public final static native long to_hours(long jarg1);
public final static native void ed25519_create_seed(long jarg1, byte_vector jarg1_);
public final static native void ed25519_create_keypair(long jarg1, byte_vector jarg1_, long jarg2, byte_vector jarg2_, long jarg3, byte_vector jarg3_);
public final static native void ed25519_sign(long jarg1, byte_vector jarg1_, long jarg2, byte_vector jarg2_, long jarg3, byte_vector jarg3_, long jarg4, byte_vector jarg4_);
public final static native int ed25519_verify(long jarg1, byte_vector jarg1_, long jarg2, byte_vector jarg2_, long jarg3, byte_vector jarg3_);
public final static native void ed25519_add_scalar(long jarg1, byte_vector jarg1_, long jarg2, byte_vector jarg2_, long jarg3, byte_vector jarg3_);
public final static native void ed25519_key_exchange(long jarg1, byte_vector jarg1_, long jarg2, byte_vector jarg2_, long jarg3, byte_vector jarg3_);
public final static native void delete_add_files_listener(long jarg1);
public final static native boolean add_files_listener_pred(long jarg1, add_files_listener jarg1_, String jarg2);
public final static native boolean add_files_listener_predSwigExplicitadd_files_listener(long jarg1, add_files_listener jarg1_, String jarg2);
Expand Down

0 comments on commit 3f49c32

Please sign in to comment.