Crypto: Change X25519 key classes from Java keys to I2P keys,

in prep for new crypto (Proposal 144)
Add EncType
Fix PrivateKey constructor w/ EncType
Add support to KeyGenerator
This commit is contained in:
zzz
2018-11-30 15:15:31 +00:00
parent cc4da1b4da
commit 2487bca47c
12 changed files with 103 additions and 113 deletions

View File

@ -10,7 +10,10 @@ package net.i2p.crypto;
public enum EncAlgo {
ELGAMAL("ElGamal"),
EC("EC");
EC("EC"),
/** @since 0.9.38 */
ECIES("ECIES");
private final String name;

View File

@ -6,6 +6,7 @@ import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import static net.i2p.crypto.x25519.spec.X25519Spec.X25519_SPEC;
import net.i2p.data.Hash;
import net.i2p.data.SimpleDataStructure;
@ -36,7 +37,13 @@ public enum EncType {
EC_P384(2, 96, 48, EncAlgo.EC, "EC/None/NoPadding", ECConstants.P384_SPEC, "0.9.20"),
/** Pubkey 132 bytes; privkey 66 bytes; */
EC_P521(3, 132, 66, EncAlgo.EC, "EC/None/NoPadding", ECConstants.P521_SPEC, "0.9.20");
EC_P521(3, 132, 66, EncAlgo.EC, "EC/None/NoPadding", ECConstants.P521_SPEC, "0.9.20"),
/**
* Pubkey 32 bytes; privkey 32 bytes
* @since 0.9.38
*/
ECIES_X25519(4, 32, 32, EncAlgo.ECIES, "EC/None/NoPadding", X25519_SPEC, "0.9.38");

View File

@ -31,6 +31,8 @@ import java.util.Arrays;
import java.util.ArrayList;
import java.util.Collection;
import com.southernstorm.noise.crypto.x25519.Curve25519;
import net.i2p.I2PAppContext;
import net.i2p.crypto.eddsa.EdDSAPrivateKey;
import net.i2p.crypto.eddsa.EdDSAPublicKey;
@ -173,20 +175,38 @@ public final class KeyGenerator {
return keys;
}
/** Convert a PrivateKey to its corresponding PublicKey
/**
* Convert a PrivateKey to its corresponding PublicKey.
* As of 0.9.38, supports EncTypes
*
* @param priv PrivateKey object
* @return the corresponding PublicKey object
* @throws IllegalArgumentException on bad key
*/
public static PublicKey getPublicKey(PrivateKey priv) {
BigInteger a = new NativeBigInteger(1, priv.toByteArray());
BigInteger aalpha = CryptoConstants.elgg.modPow(a, CryptoConstants.elgp);
PublicKey pub = new PublicKey();
try {
pub.setData(SigUtil.rectify(aalpha, PublicKey.KEYSIZE_BYTES));
} catch (InvalidKeyException ike) {
throw new IllegalArgumentException(ike);
EncType type = priv.getType();
byte[] data;
switch (type) {
case ELGAMAL_2048:
BigInteger a = new NativeBigInteger(1, priv.toByteArray());
BigInteger aalpha = CryptoConstants.elgg.modPow(a, CryptoConstants.elgp);
try {
data = SigUtil.rectify(aalpha, PublicKey.KEYSIZE_BYTES);
} catch (InvalidKeyException ike) {
throw new IllegalArgumentException(ike);
}
break;
case ECIES_X25519:
data = new byte[32];
Curve25519.eval(data, 0, priv.getData(), null);
break;
default:
throw new IllegalArgumentException("Unsupported algorithm");
}
PublicKey pub = new PublicKey(type, data);
return pub;
}

View File

@ -0,0 +1,28 @@
package net.i2p.crypto;
import net.i2p.data.PrivateKey;
import net.i2p.data.PublicKey;
/**
* Same as java.security.KeyPair, but with I2P keys
*
* @since 0.9.38
*/
public class KeyPair {
private final PublicKey pub;
private final PrivateKey priv;
public KeyPair(PublicKey publicKey, PrivateKey privateKey) {
pub = publicKey;
priv = privateKey;
}
public PublicKey getPublic() {
return pub;
}
public PrivateKey getPrivate() {
return priv;
}
}

View File

@ -0,0 +1,9 @@
package net.i2p.crypto.x25519.spec;
import java.security.spec.AlgorithmParameterSpec;
public class X25519Spec implements AlgorithmParameterSpec {
public static final X25519Spec X25519_SPEC = new X25519Spec();
}

View File

@ -0,0 +1,7 @@
<html><body>
<p>
AlgorithmParameterSpec for X25519.
</p><p>
Since 0.9.38.
</p>
</body></html>

View File

@ -53,8 +53,11 @@ public class PrivateKey extends SimpleDataStructure {
* @since 0.9.38
*/
public PrivateKey(EncType type, byte data[]) {
super(data);
super();
_type = type;
if (data == null)
throw new IllegalArgumentException("Data must be specified");
_data = data;
}
/** constructs from base64