Skip to content

Commit 0f10718

Browse files
committed
Refactored into provider model for initial support of J2ME.
1 parent 834de83 commit 0f10718

File tree

74 files changed

+528
-215
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+528
-215
lines changed

android/src/androidTest/java/org/whispersystems/curve25519/NativeCurve25519ProviderTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
public class NativeCurve25519ProviderTest extends Curve25519ProviderTest {
44
@Override
5-
protected Curve25519Provider createProvider() {
5+
protected Curve25519Provider createProvider() throws NoSuchProviderException {
66
return new NativeCurve25519Provider();
77
}
88
}

android/src/androidTest/java/org/whispersystems/curve25519/NativeCurve25519Test.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@
33
public class NativeCurve25519Test extends Curve25519Test {
44

55
@Override
6-
public void testCheckProvider() {
7-
assertTrue(Curve25519.isNative());
6+
public void testCheckProvider() throws NoSuchProviderException {
7+
assertTrue(Curve25519.getInstance(getProviderName()).isNative());
8+
}
9+
10+
@Override
11+
public String getProviderName() {
12+
return "native";
813
}
914

1015
}

common/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
apply plugin: 'java'
2+
3+
sourceCompatibility = 1.2
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/**
2+
* Copyright (C) 2015 Open Whisper Systems
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
package org.whispersystems.curve25519;
18+
19+
import org.whispersystems.curve25519.java.Sha512;
20+
import org.whispersystems.curve25519.java.curve_sigs;
21+
import org.whispersystems.curve25519.java.scalarmult;
22+
23+
abstract class BaseJavaCurve25519Provider implements Curve25519Provider {
24+
25+
// @Override
26+
public boolean isNative() {
27+
return false;
28+
}
29+
30+
// @Override
31+
public byte[] calculateAgreement(byte[] ourPrivate, byte[] theirPublic) {
32+
byte[] agreement = new byte[32];
33+
scalarmult.crypto_scalarmult(agreement, ourPrivate, theirPublic);
34+
35+
return agreement;
36+
}
37+
38+
// @Override
39+
public byte[] generatePublicKey(byte[] privateKey) {
40+
byte[] publicKey = new byte[32];
41+
curve_sigs.curve25519_keygen(publicKey, privateKey);
42+
43+
return publicKey;
44+
}
45+
46+
// @Override
47+
public byte[] generatePrivateKey() {
48+
byte[] random = getRandom(PRIVATE_KEY_LEN);
49+
return generatePrivateKey(random);
50+
}
51+
52+
// @Override
53+
public byte[] generatePrivateKey(byte[] random) {
54+
byte[] privateKey = new byte[32];
55+
56+
System.arraycopy(random, 0, privateKey, 0, 32);
57+
58+
privateKey[0] &= 248;
59+
privateKey[31] &= 127;
60+
privateKey[31] |= 64;
61+
62+
return privateKey;
63+
}
64+
65+
// @Override
66+
public byte[] calculateSignature(byte[] random, byte[] privateKey, byte[] message) {
67+
byte[] result = new byte[64];
68+
69+
if (curve_sigs.curve25519_sign(getSha512(), result, privateKey, message, message.length, random) != 0) {
70+
throw new IllegalArgumentException("Message exceeds max length!");
71+
}
72+
73+
return result;
74+
}
75+
76+
// @Override
77+
public boolean verifySignature(byte[] publicKey, byte[] message, byte[] signature) {
78+
return curve_sigs.curve25519_verify(getSha512(), signature, publicKey, message, message.length) == 0;
79+
}
80+
81+
protected abstract Sha512 getSha512();
82+
83+
}
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
/**
2+
* Copyright (C) 2015 Open Whisper Systems
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
package org.whispersystems.curve25519;
18+
19+
/**
20+
* A Curve25519 interface for generating keys, calculating agreements, creating signatures,
21+
* and verifying signatures.
22+
*
23+
* @author Moxie Marlinspike
24+
*/
25+
public class Curve25519 {
26+
27+
public static final String NATIVE = "native";
28+
public static final String JAVA = "java";
29+
public static final String J2ME = "j2me";
30+
public static final String BEST = "best";
31+
32+
public static Curve25519 getInstance(String type) throws NoSuchProviderException {
33+
if (NATIVE.equals(type)) return new Curve25519(constructNativeProvider());
34+
else if (JAVA.equals(type)) return new Curve25519(constructJavaProvider());
35+
else if (J2ME.equals(type)) return new Curve25519(constructJ2meProvider());
36+
else if (BEST.equals(type)) return new Curve25519(constructOpportunisticProvider());
37+
else throw new NoSuchProviderException(type);
38+
}
39+
40+
private final Curve25519Provider provider;
41+
42+
private Curve25519(Curve25519Provider provider) {
43+
this.provider = provider;
44+
}
45+
46+
47+
// static {
48+
// try {
49+
// provider = new NativeCurve25519Provider();
50+
// } catch (UnsatisfiedLinkError ule) {
51+
// provider = new JavaCurve25519Provider();
52+
// }
53+
// }
54+
55+
/**
56+
* {@link Curve25519} is backed by either a native (via JNI)
57+
* or pure-Java provider. By default it prefers the native provider, and falls back to the
58+
* pure-Java provider if the native library fails to load.
59+
*
60+
* @return true if backed by a native provider, false otherwise.
61+
*/
62+
public boolean isNative() {
63+
return provider.isNative();
64+
}
65+
66+
/**
67+
* Generates a Curve25519 keypair.
68+
*
69+
* @return A randomly generated Curve25519 keypair.
70+
*/
71+
public Curve25519KeyPair generateKeyPair() {
72+
byte[] privateKey = provider.generatePrivateKey();
73+
byte[] publicKey = provider.generatePublicKey(privateKey);
74+
75+
return new Curve25519KeyPair(publicKey, privateKey);
76+
}
77+
78+
/**
79+
* Calculates an ECDH agreement.
80+
*
81+
* @param publicKey The Curve25519 (typically remote party's) public key.
82+
* @param privateKey The Curve25519 (typically yours) private key.
83+
* @return A 32-byte shared secret.
84+
*/
85+
public byte[] calculateAgreement(byte[] publicKey, byte[] privateKey) {
86+
return provider.calculateAgreement(privateKey, publicKey);
87+
}
88+
89+
/**
90+
* Calculates a Curve25519 signature.
91+
*
92+
* @param privateKey The private Curve25519 key to create the signature with.
93+
* @param message The message to sign.
94+
* @return A 64-byte signature.
95+
*/
96+
public byte[] calculateSignature(byte[] privateKey, byte[] message) {
97+
byte[] random = provider.getRandom(64);
98+
return provider.calculateSignature(random, privateKey, message);
99+
}
100+
101+
/**
102+
* Verify a Curve25519 signature.
103+
*
104+
* @param publicKey The Curve25519 public key the signature belongs to.
105+
* @param message The message that was signed.
106+
* @param signature The signature to verify.
107+
* @return true if valid, false if not.
108+
*/
109+
public boolean verifySignature(byte[] publicKey, byte[] message, byte[] signature) {
110+
return provider.verifySignature(publicKey, message, signature);
111+
}
112+
113+
// private byte[] generatePrivateKey() {
114+
// byte[] privateKey = new byte[32];
115+
// random.nextBytes(privateKey);
116+
//
117+
// return provider.generatePrivateKey(privateKey);
118+
// }
119+
120+
// private byte[] getRandom(SecureRandom secureRandom, int size) {
121+
// byte[] output = new byte[size];
122+
// secureRandom.nextBytes(output);
123+
//
124+
// return output;
125+
// }
126+
127+
private static Curve25519Provider constructNativeProvider() throws NoSuchProviderException {
128+
return constructClass("NativeCurve25519Provider");
129+
}
130+
131+
private static Curve25519Provider constructJavaProvider() throws NoSuchProviderException {
132+
return constructClass("JavaCurve25519Provider");
133+
}
134+
135+
private static Curve25519Provider constructJ2meProvider() throws NoSuchProviderException {
136+
return constructClass("J2meCurve25519Provider");
137+
}
138+
139+
private static Curve25519Provider constructOpportunisticProvider() throws NoSuchProviderException {
140+
return constructClass("OpportunisticCurve25519Provider");
141+
}
142+
143+
private static Curve25519Provider constructClass(String name) throws NoSuchProviderException {
144+
try {
145+
return (Curve25519Provider)Class.forName("org.whispersystems.curve25519." + name).newInstance();
146+
} catch (InstantiationException e) {
147+
throw new NoSuchProviderException(e);
148+
} catch (IllegalAccessException e) {
149+
throw new NoSuchProviderException(e);
150+
} catch (ClassNotFoundException e) {
151+
throw new NoSuchProviderException(e);
152+
}
153+
}
154+
155+
}

java/src/main/java/org/whispersystems/curve25519/Curve25519Provider.java renamed to common/src/main/java/org/whispersystems/curve25519/Curve25519Provider.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,14 @@
1818

1919
interface Curve25519Provider {
2020

21+
static final int PRIVATE_KEY_LEN = 32;
22+
2123
boolean isNative();
2224
byte[] calculateAgreement(byte[] ourPrivate, byte[] theirPublic);
2325
byte[] generatePublicKey(byte[] privateKey);
26+
byte[] generatePrivateKey();
2427
byte[] generatePrivateKey(byte[] random);
28+
byte[] getRandom(int length);
2529

2630
byte[] calculateSignature(byte[] random, byte[] privateKey, byte[] message);
2731
boolean verifySignature(byte[] publicKey, byte[] message, byte[] signature);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.whispersystems.curve25519;
2+
3+
public class NoSuchProviderException extends Exception {
4+
public NoSuchProviderException(Throwable e) {
5+
super(e);
6+
}
7+
8+
public NoSuchProviderException(String type) {
9+
super(type);
10+
}
11+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.whispersystems.curve25519.java;
2+
3+
public class Arrays {
4+
/**
5+
* Assigns the specified byte value to each element of the specified array
6+
* of bytes.
7+
*
8+
* @param a the array to be filled
9+
* @param val the value to be stored in all elements of the array
10+
*/
11+
public static void fill(byte[] a, byte val) {
12+
for (int i = 0, len = a.length; i < len; i++)
13+
a[i] = val;
14+
}
15+
16+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.whispersystems.curve25519.java;
2+
3+
public interface Sha512 {
4+
5+
public void calculateDigest(byte[] out, byte[] in, long length);
6+
7+
}

0 commit comments

Comments
 (0)