aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/app/src/main/java/com/wireguard/crypto/Keypair.java
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2017-08-08 18:22:41 +0200
committerSamuel Holland <samuel@sholland.org>2017-08-08 18:22:41 +0200
commitf6b864d4e70192ce780c3487b200506dec64f275 (patch)
tree2dd246d8891ce38393a6d16bcae3d1dbccf2b901 /app/src/main/java/com/wireguard/crypto/Keypair.java
parentres: Add icons for quick settings tile (diff)
downloadwireguard-android-f6b864d4e70192ce780c3487b200506dec64f275.tar.xz
wireguard-android-f6b864d4e70192ce780c3487b200506dec64f275.zip
Constant time base64
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'app/src/main/java/com/wireguard/crypto/Keypair.java')
-rw-r--r--app/src/main/java/com/wireguard/crypto/Keypair.java24
1 files changed, 5 insertions, 19 deletions
diff --git a/app/src/main/java/com/wireguard/crypto/Keypair.java b/app/src/main/java/com/wireguard/crypto/Keypair.java
index ef2a4b31..f98b5e21 100644
--- a/app/src/main/java/com/wireguard/crypto/Keypair.java
+++ b/app/src/main/java/com/wireguard/crypto/Keypair.java
@@ -9,12 +9,9 @@ import java.security.SecureRandom;
*/
public class Keypair {
- private static final int KEY_LENGTH = 32;
- public static final int KEY_STRING_LENGTH = 44;
-
private static byte[] generatePrivateKey() {
final SecureRandom secureRandom = new SecureRandom();
- final byte privateKey[] = new byte[KEY_LENGTH];
+ final byte privateKey[] = new byte[KeyEncoding.WG_KEY_LEN];
secureRandom.nextBytes(privateKey);
privateKey[0] &= 248;
privateKey[31] &= 127;
@@ -23,22 +20,11 @@ public class Keypair {
}
private static byte[] generatePublicKey(byte privateKey[]) {
- final byte publicKey[] = new byte[KEY_LENGTH];
+ final byte publicKey[] = new byte[KeyEncoding.WG_KEY_LEN];
Curve25519.eval(publicKey, 0, privateKey, null);
return publicKey;
}
- private static byte[] parseKey(String key) {
- final byte keyBytes[] = Base64.decode(key, Base64.NO_WRAP);
- if (keyBytes.length != KEY_LENGTH)
- throw new IndexOutOfBoundsException("Key is not the correct length");
- return keyBytes;
- }
-
- private static String unParseKey(byte keyBytes[]) {
- return Base64.encodeToString(keyBytes, Base64.NO_WRAP);
- }
-
private final byte privateKey[];
private final byte publicKey[];
@@ -52,14 +38,14 @@ public class Keypair {
}
public Keypair(String privateKey) {
- this(parseKey(privateKey));
+ this(KeyEncoding.keyFromBase64(privateKey));
}
public String getPrivateKey() {
- return unParseKey(privateKey);
+ return KeyEncoding.keyToBase64(privateKey);
}
public String getPublicKey() {
- return unParseKey(publicKey);
+ return KeyEncoding.keyToBase64(publicKey);
}
}