forked from I2P_Developers/i2p.i2p
ElGamal classes, from Bouncy Castle 1.53, for I2PProvider.
License: BSD Encoding/decoding/sigs: todo.
This commit is contained in:
@ -34,6 +34,7 @@ import java.math.BigInteger;
|
||||
import java.security.spec.AlgorithmParameterSpec;
|
||||
import java.security.spec.DSAParameterSpec;
|
||||
|
||||
import net.i2p.crypto.elgamal.spec.ElGamalParameterSpec;
|
||||
import net.i2p.util.NativeBigInteger;
|
||||
|
||||
/**
|
||||
|
@ -1,66 +0,0 @@
|
||||
package net.i2p.crypto;
|
||||
|
||||
/*
|
||||
* Copyright (c) 2000 - 2013 The Legion of the Bouncy Castle Inc. (http://www.bouncycastle.org)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
|
||||
* and associated documentation files (the "Software"), to deal in the Software without restriction,
|
||||
* including without limitation the rights to use, copy, modify, merge, publish, distribute,
|
||||
* sublicense, and/or sell copies of the Software, and to permit persons to whom the Software
|
||||
* is furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all copies or
|
||||
* substantial portions of the Software.
|
||||
*
|
||||
*THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
||||
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
|
||||
* AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
*/
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.security.spec.AlgorithmParameterSpec;
|
||||
|
||||
/**
|
||||
* Copied from org.bouncycastle.jce.spec
|
||||
* This can't actually be passed to the BC provider, we would have to
|
||||
* use reflection to create a "real" org.bouncycasle.jce.spec.ElGamalParameterSpec.
|
||||
*
|
||||
* @since 0.9.18
|
||||
*/
|
||||
public class ElGamalParameterSpec implements AlgorithmParameterSpec {
|
||||
private final BigInteger p;
|
||||
private final BigInteger g;
|
||||
|
||||
/**
|
||||
* Constructs a parameter set for Diffie-Hellman, using a prime modulus
|
||||
* <code>p</code> and a base generator <code>g</code>.
|
||||
*
|
||||
* @param p the prime modulus
|
||||
* @param g the base generator
|
||||
*/
|
||||
public ElGamalParameterSpec(BigInteger p, BigInteger g) {
|
||||
this.p = p;
|
||||
this.g = g;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the prime modulus <code>p</code>.
|
||||
*
|
||||
* @return the prime modulus <code>p</code>
|
||||
*/
|
||||
public BigInteger getP() {
|
||||
return p;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the base generator <code>g</code>.
|
||||
*
|
||||
* @return the base generator <code>g</code>
|
||||
*/
|
||||
public BigInteger getG() {
|
||||
return g;
|
||||
}
|
||||
}
|
11
core/java/src/net/i2p/crypto/elgamal/ElGamalKey.java
Normal file
11
core/java/src/net/i2p/crypto/elgamal/ElGamalKey.java
Normal file
@ -0,0 +1,11 @@
|
||||
package net.i2p.crypto.elgamal;
|
||||
|
||||
import javax.crypto.interfaces.DHKey;
|
||||
|
||||
import net.i2p.crypto.elgamal.spec.ElGamalParameterSpec;
|
||||
|
||||
public interface ElGamalKey
|
||||
extends DHKey
|
||||
{
|
||||
public ElGamalParameterSpec getParameters();
|
||||
}
|
11
core/java/src/net/i2p/crypto/elgamal/ElGamalPrivateKey.java
Normal file
11
core/java/src/net/i2p/crypto/elgamal/ElGamalPrivateKey.java
Normal file
@ -0,0 +1,11 @@
|
||||
package net.i2p.crypto.elgamal;
|
||||
|
||||
import java.math.BigInteger;
|
||||
|
||||
import javax.crypto.interfaces.DHPrivateKey;
|
||||
|
||||
public interface ElGamalPrivateKey
|
||||
extends ElGamalKey, DHPrivateKey
|
||||
{
|
||||
public BigInteger getX();
|
||||
}
|
11
core/java/src/net/i2p/crypto/elgamal/ElGamalPublicKey.java
Normal file
11
core/java/src/net/i2p/crypto/elgamal/ElGamalPublicKey.java
Normal file
@ -0,0 +1,11 @@
|
||||
package net.i2p.crypto.elgamal;
|
||||
|
||||
import java.math.BigInteger;
|
||||
|
||||
import javax.crypto.interfaces.DHPublicKey;
|
||||
|
||||
public interface ElGamalPublicKey
|
||||
extends ElGamalKey, DHPublicKey
|
||||
{
|
||||
public BigInteger getY();
|
||||
}
|
@ -0,0 +1,115 @@
|
||||
package net.i2p.crypto.elgamal.impl;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.math.BigInteger;
|
||||
|
||||
import javax.crypto.interfaces.DHPrivateKey;
|
||||
import javax.crypto.spec.DHParameterSpec;
|
||||
import javax.crypto.spec.DHPrivateKeySpec;
|
||||
|
||||
import net.i2p.crypto.elgamal.ElGamalPrivateKey;
|
||||
import net.i2p.crypto.elgamal.spec.ElGamalParameterSpec;
|
||||
import net.i2p.crypto.elgamal.spec.ElGamalPrivateKeySpec;
|
||||
|
||||
public class ElGamalPrivateKeyImpl
|
||||
implements ElGamalPrivateKey, DHPrivateKey
|
||||
{
|
||||
static final long serialVersionUID = 4819350091141529678L;
|
||||
|
||||
BigInteger x;
|
||||
|
||||
ElGamalParameterSpec elSpec;
|
||||
|
||||
protected ElGamalPrivateKeyImpl()
|
||||
{
|
||||
}
|
||||
|
||||
ElGamalPrivateKeyImpl(
|
||||
ElGamalPrivateKey key)
|
||||
{
|
||||
this.x = key.getX();
|
||||
this.elSpec = key.getParameters();
|
||||
}
|
||||
|
||||
ElGamalPrivateKeyImpl(
|
||||
DHPrivateKey key)
|
||||
{
|
||||
this.x = key.getX();
|
||||
this.elSpec = new ElGamalParameterSpec(key.getParams().getP(), key.getParams().getG());
|
||||
}
|
||||
|
||||
ElGamalPrivateKeyImpl(
|
||||
ElGamalPrivateKeySpec spec)
|
||||
{
|
||||
this.x = spec.getX();
|
||||
this.elSpec = new ElGamalParameterSpec(spec.getParams().getP(), spec.getParams().getG());
|
||||
}
|
||||
|
||||
ElGamalPrivateKeyImpl(
|
||||
DHPrivateKeySpec spec)
|
||||
{
|
||||
this.x = spec.getX();
|
||||
this.elSpec = new ElGamalParameterSpec(spec.getP(), spec.getG());
|
||||
}
|
||||
|
||||
public String getAlgorithm()
|
||||
{
|
||||
return "ElGamal";
|
||||
}
|
||||
|
||||
/**
|
||||
* return the encoding format we produce in getEncoded().
|
||||
*
|
||||
* @return the string "PKCS#8"
|
||||
*/
|
||||
public String getFormat()
|
||||
{
|
||||
return "PKCS#8";
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a PKCS8 representation of the key. The sequence returned
|
||||
* represents a full PrivateKeyInfo object.
|
||||
*
|
||||
* @return a PKCS8 representation of the key.
|
||||
*/
|
||||
public byte[] getEncoded()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public ElGamalParameterSpec getParameters()
|
||||
{
|
||||
return elSpec;
|
||||
}
|
||||
|
||||
public DHParameterSpec getParams()
|
||||
{
|
||||
return new DHParameterSpec(elSpec.getP(), elSpec.getG());
|
||||
}
|
||||
|
||||
public BigInteger getX()
|
||||
{
|
||||
return x;
|
||||
}
|
||||
|
||||
private void readObject(
|
||||
ObjectInputStream in)
|
||||
throws IOException, ClassNotFoundException
|
||||
{
|
||||
x = (BigInteger)in.readObject();
|
||||
|
||||
this.elSpec = new ElGamalParameterSpec((BigInteger)in.readObject(), (BigInteger)in.readObject());
|
||||
}
|
||||
|
||||
private void writeObject(
|
||||
ObjectOutputStream out)
|
||||
throws IOException
|
||||
{
|
||||
out.writeObject(this.getX());
|
||||
out.writeObject(elSpec.getP());
|
||||
out.writeObject(elSpec.getG());
|
||||
}
|
||||
}
|
@ -0,0 +1,106 @@
|
||||
package net.i2p.crypto.elgamal.impl;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.math.BigInteger;
|
||||
|
||||
import javax.crypto.interfaces.DHPublicKey;
|
||||
import javax.crypto.spec.DHParameterSpec;
|
||||
import javax.crypto.spec.DHPublicKeySpec;
|
||||
|
||||
import net.i2p.crypto.elgamal.ElGamalPublicKey;
|
||||
import net.i2p.crypto.elgamal.spec.ElGamalParameterSpec;
|
||||
import net.i2p.crypto.elgamal.spec.ElGamalPublicKeySpec;
|
||||
|
||||
public class ElGamalPublicKeyImpl
|
||||
implements ElGamalPublicKey, DHPublicKey
|
||||
{
|
||||
static final long serialVersionUID = 8712728417091216948L;
|
||||
|
||||
private BigInteger y;
|
||||
private ElGamalParameterSpec elSpec;
|
||||
|
||||
ElGamalPublicKeyImpl(
|
||||
ElGamalPublicKeySpec spec)
|
||||
{
|
||||
this.y = spec.getY();
|
||||
this.elSpec = new ElGamalParameterSpec(spec.getParams().getP(), spec.getParams().getG());
|
||||
}
|
||||
|
||||
ElGamalPublicKeyImpl(
|
||||
DHPublicKeySpec spec)
|
||||
{
|
||||
this.y = spec.getY();
|
||||
this.elSpec = new ElGamalParameterSpec(spec.getP(), spec.getG());
|
||||
}
|
||||
|
||||
ElGamalPublicKeyImpl(
|
||||
ElGamalPublicKey key)
|
||||
{
|
||||
this.y = key.getY();
|
||||
this.elSpec = key.getParameters();
|
||||
}
|
||||
|
||||
ElGamalPublicKeyImpl(
|
||||
DHPublicKey key)
|
||||
{
|
||||
this.y = key.getY();
|
||||
this.elSpec = new ElGamalParameterSpec(key.getParams().getP(), key.getParams().getG());
|
||||
}
|
||||
|
||||
ElGamalPublicKeyImpl(
|
||||
BigInteger y,
|
||||
ElGamalParameterSpec elSpec)
|
||||
{
|
||||
this.y = y;
|
||||
this.elSpec = elSpec;
|
||||
}
|
||||
|
||||
public String getAlgorithm()
|
||||
{
|
||||
return "ElGamal";
|
||||
}
|
||||
|
||||
public String getFormat()
|
||||
{
|
||||
return "X.509";
|
||||
}
|
||||
|
||||
public byte[] getEncoded()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public ElGamalParameterSpec getParameters()
|
||||
{
|
||||
return elSpec;
|
||||
}
|
||||
|
||||
public DHParameterSpec getParams()
|
||||
{
|
||||
return new DHParameterSpec(elSpec.getP(), elSpec.getG());
|
||||
}
|
||||
|
||||
public BigInteger getY()
|
||||
{
|
||||
return y;
|
||||
}
|
||||
|
||||
private void readObject(
|
||||
ObjectInputStream in)
|
||||
throws IOException, ClassNotFoundException
|
||||
{
|
||||
this.y = (BigInteger)in.readObject();
|
||||
this.elSpec = new ElGamalParameterSpec((BigInteger)in.readObject(), (BigInteger)in.readObject());
|
||||
}
|
||||
|
||||
private void writeObject(
|
||||
ObjectOutputStream out)
|
||||
throws IOException
|
||||
{
|
||||
out.writeObject(this.getY());
|
||||
out.writeObject(elSpec.getP());
|
||||
out.writeObject(elSpec.getG());
|
||||
}
|
||||
}
|
9
core/java/src/net/i2p/crypto/elgamal/impl/package.html
Normal file
9
core/java/src/net/i2p/crypto/elgamal/impl/package.html
Normal file
@ -0,0 +1,9 @@
|
||||
<html><body>
|
||||
<p>
|
||||
Implementation of ElGamal keys, used for I2PProvider.
|
||||
Modified from Bouncy Castle 1.53.
|
||||
See net.i2p.crypto.elgamal for license info.
|
||||
</p><p>
|
||||
Since 0.9.25.
|
||||
</p>
|
||||
</body></html>
|
29
core/java/src/net/i2p/crypto/elgamal/package.html
Normal file
29
core/java/src/net/i2p/crypto/elgamal/package.html
Normal file
@ -0,0 +1,29 @@
|
||||
<html><body>
|
||||
<p>
|
||||
Interfaces for ElGamal keys, used for I2PProvider.
|
||||
Copied from Bouncy Castle 1.53.
|
||||
</p><p>
|
||||
Since 0.9.25.
|
||||
</p><p><pre>
|
||||
|
||||
|
||||
Copyright (c) 2000 - 2013 The Legion of the Bouncy Castle Inc. (http://www.bouncycastle.org)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software
|
||||
and associated documentation files (the "Software"), to deal in the Software without restriction,
|
||||
including without limitation the rights to use, copy, modify, merge, publish, distribute,
|
||||
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software
|
||||
is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or
|
||||
substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
||||
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
|
||||
AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
|
||||
</pre></p>
|
||||
</body></html>
|
@ -0,0 +1,28 @@
|
||||
package net.i2p.crypto.elgamal.spec;
|
||||
|
||||
import java.security.spec.AlgorithmParameterSpec;
|
||||
|
||||
public class ElGamalGenParameterSpec
|
||||
implements AlgorithmParameterSpec
|
||||
{
|
||||
private int primeSize;
|
||||
|
||||
/*
|
||||
* @param primeSize the size (in bits) of the prime modulus.
|
||||
*/
|
||||
public ElGamalGenParameterSpec(
|
||||
int primeSize)
|
||||
{
|
||||
this.primeSize = primeSize;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the size in bits of the prime modulus.
|
||||
*
|
||||
* @return the size in bits of the prime modulus
|
||||
*/
|
||||
public int getPrimeSize()
|
||||
{
|
||||
return primeSize;
|
||||
}
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
package net.i2p.crypto.elgamal.spec;
|
||||
|
||||
import java.security.spec.KeySpec;
|
||||
|
||||
public class ElGamalKeySpec
|
||||
implements KeySpec
|
||||
{
|
||||
private ElGamalParameterSpec spec;
|
||||
|
||||
public ElGamalKeySpec(
|
||||
ElGamalParameterSpec spec)
|
||||
{
|
||||
this.spec = spec;
|
||||
}
|
||||
|
||||
public ElGamalParameterSpec getParams()
|
||||
{
|
||||
return spec;
|
||||
}
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
package net.i2p.crypto.elgamal.spec;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.security.spec.AlgorithmParameterSpec;
|
||||
|
||||
/**
|
||||
* Copied from org.bouncycastle.jce.spec
|
||||
* This can't actually be passed to the BC provider, we would have to
|
||||
* use reflection to create a "real" org.bouncycasle.jce.spec.ElGamalParameterSpec.
|
||||
*
|
||||
* @since 0.9.18
|
||||
*/
|
||||
public class ElGamalParameterSpec implements AlgorithmParameterSpec {
|
||||
private final BigInteger p;
|
||||
private final BigInteger g;
|
||||
|
||||
/**
|
||||
* Constructs a parameter set for Diffie-Hellman, using a prime modulus
|
||||
* <code>p</code> and a base generator <code>g</code>.
|
||||
*
|
||||
* @param p the prime modulus
|
||||
* @param g the base generator
|
||||
*/
|
||||
public ElGamalParameterSpec(BigInteger p, BigInteger g) {
|
||||
this.p = p;
|
||||
this.g = g;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the prime modulus <code>p</code>.
|
||||
*
|
||||
* @return the prime modulus <code>p</code>
|
||||
*/
|
||||
public BigInteger getP() {
|
||||
return p;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the base generator <code>g</code>.
|
||||
*
|
||||
* @return the base generator <code>g</code>
|
||||
*/
|
||||
public BigInteger getG() {
|
||||
return g;
|
||||
}
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
package net.i2p.crypto.elgamal.spec;
|
||||
|
||||
import java.math.BigInteger;
|
||||
|
||||
/**
|
||||
* This class specifies an ElGamal private key with its associated parameters.
|
||||
*
|
||||
* @see ElGamalPublicKeySpec
|
||||
*/
|
||||
public class ElGamalPrivateKeySpec
|
||||
extends ElGamalKeySpec
|
||||
{
|
||||
private BigInteger x;
|
||||
|
||||
public ElGamalPrivateKeySpec(
|
||||
BigInteger x,
|
||||
ElGamalParameterSpec spec)
|
||||
{
|
||||
super(spec);
|
||||
|
||||
this.x = x;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the private value <code>x</code>.
|
||||
*
|
||||
* @return the private value <code>x</code>
|
||||
*/
|
||||
public BigInteger getX()
|
||||
{
|
||||
return x;
|
||||
}
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
package net.i2p.crypto.elgamal.spec;
|
||||
|
||||
import java.math.BigInteger;
|
||||
|
||||
/**
|
||||
* This class specifies an ElGamal public key with its associated parameters.
|
||||
*
|
||||
* @see ElGamalPrivateKeySpec
|
||||
*/
|
||||
public class ElGamalPublicKeySpec
|
||||
extends ElGamalKeySpec
|
||||
{
|
||||
private BigInteger y;
|
||||
|
||||
public ElGamalPublicKeySpec(
|
||||
BigInteger y,
|
||||
ElGamalParameterSpec spec)
|
||||
{
|
||||
super(spec);
|
||||
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the public value <code>y</code>.
|
||||
*
|
||||
* @return the public value <code>y</code>
|
||||
*/
|
||||
public BigInteger getY()
|
||||
{
|
||||
return y;
|
||||
}
|
||||
}
|
9
core/java/src/net/i2p/crypto/elgamal/spec/package.html
Normal file
9
core/java/src/net/i2p/crypto/elgamal/spec/package.html
Normal file
@ -0,0 +1,9 @@
|
||||
<html><body>
|
||||
<p>
|
||||
Specs ElGamal keys, used for I2PProvider.
|
||||
Copied from Bouncy Castle 1.53.
|
||||
See net.i2p.crypto.elgamal for license info.
|
||||
</p><p>
|
||||
Since 0.9.25.
|
||||
</p>
|
||||
</body></html>
|
Reference in New Issue
Block a user