forked from I2P_Developers/i2p.i2p
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:
@ -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;
|
||||
|
||||
|
@ -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");
|
||||
|
||||
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
28
core/java/src/net/i2p/crypto/KeyPair.java
Normal file
28
core/java/src/net/i2p/crypto/KeyPair.java
Normal 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;
|
||||
}
|
||||
}
|
9
core/java/src/net/i2p/crypto/x25519/spec/X25519Spec.java
Normal file
9
core/java/src/net/i2p/crypto/x25519/spec/X25519Spec.java
Normal 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();
|
||||
|
||||
}
|
7
core/java/src/net/i2p/crypto/x25519/spec/package.html
Normal file
7
core/java/src/net/i2p/crypto/x25519/spec/package.html
Normal file
@ -0,0 +1,7 @@
|
||||
<html><body>
|
||||
<p>
|
||||
AlgorithmParameterSpec for X25519.
|
||||
</p><p>
|
||||
Since 0.9.38.
|
||||
</p>
|
||||
</body></html>
|
@ -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
|
||||
|
Reference in New Issue
Block a user