[BREAKGLASS] Pure Java and JNI backed Curve25519 implementation.
Go to file
Moxie Marlinspike fc5af285f5 Fix for some devices that behave strangely with native code.
Sometimes, for whatever reason, native code fails to get installed
on some small percentage of Android devices.  In contrast to every
other device, on LG devices, the call to System.loadLibrary
 *succeeds*, and then the individual native calls throw exceptions.

This change allows the degredation path to work correctly on LG.
2015-02-09 14:00:01 -08:00
android Fix for some devices that behave strangely with native code. 2015-02-09 14:00:01 -08:00
common Switch NoSuchProviderException to a RuntimeException 2015-02-02 16:02:28 -08:00
gradle/wrapper Move over to gradle for all building. 2015-01-08 14:32:17 -08:00
j2me Add J2ME tests. 2015-02-02 15:19:40 -08:00
java Fix for some devices that behave strangely with native code. 2015-02-09 14:00:01 -08:00
tests Add J2ME tests. 2015-02-02 15:19:40 -08:00
.gitignore Add license information. 2015-01-08 15:33:41 -08:00
build.gradle Bump version to 0.2.1 2015-02-02 16:07:35 -08:00
gradlew Move over to gradle for all building. 2015-01-08 14:32:17 -08:00
gradlew.bat Move over to gradle for all building. 2015-01-08 14:32:17 -08:00
README.md Updated README 2015-02-03 10:20:08 -08:00
settings.gradle Refactored into provider model for initial support of J2ME. 2015-01-10 21:31:00 -08:00

curve25519-java

A Java Curve25519 implementation that is backed by native code when available, and pure Java when a native library is not available. There is also a J2ME build variant.

Installing

To use on Android:

dependencies {
  compile 'org.whispersystems:curve25519-android:(latest version number here)'
}

To use from pure Java:

<dependency>
  <groupId>org.whispersystems</groupId>
  <artifactId>curve25519-java</artifactId>
  <version>(latest version number here)</version>
</dependency>

To use from J2ME:

<dependency>
  <groupId>org.whispersystems</groupId>
  <artifactId>curve25519-j2me</artifactId>
  <version>(latest version number here)</version>
</dependency>

The Android artifact is an AAR that contains an NDK-backed native implementation, while the Java artifact is a JAR that only contains the pure-Java Curve25519 provider.

Using

Obtaining an instance

The caller needs to specify a provider when obtaining a Curve25519 instance. There are four built in providers:

  1. Curve25519.NATIVE -- This is a JNI backed provider.
  2. Curve25519.JAVA -- This is a pure Java 7 backed provider.
  3. Curve25519.J2ME -- This is a J2ME compatible provider.
  4. Curve25519.BEST -- This is a provider that attempts to use NATIVE, but falls back to JAVA if the former is unavailable.

The caller specifies a provider during instance creation:

Curve25519 cipher = Curve25519.getInstance(Curve25519.BEST);

Since J2ME doesn't have built-in SecureRandom support, J2ME users need to supply their own source of SecureRandom by implementing the SecureRandomProvider interface and passing it in:

Curve25519 cipher = Curve25519.getInstance(Curve25519.J2ME, new MySecureRandomProvider());

Generating a Curve25519 keypair:

Curve25519KeyPair keyPair = Curve25519.getInstance(Curve25519.BEST).generateKeyPair();

Calculating a shared secret:

Curve25519 cipher       = Curve25519.getInstance(Curve25519.BEST);
byte[]     sharedSecret = cipher.calculateAgreement(publicKey, privateKey);

Calculating a signature:

Curve25519 cipher    = Curve25519.getInstance(Curve25519.BEST);
byte[]     signature = cipher.calculateSignature(secureRandom, privateKey, message);

Verifying a signature:

Curve25519 cipher         = Curve25519.getInstance(Curve25519.BEST);
boolean    validSignature = cipher.verifySignature(publicKey, message, signature);

License

Copyright 2015 Open Whisper Systems

Licensed under the GPLv3: http://www.gnu.org/licenses/gpl-3.0.html