diff --git a/LICENSE.txt b/LICENSE.txt index 295da33aa..e6ae86cba 100644 --- a/LICENSE.txt +++ b/LICENSE.txt @@ -40,6 +40,10 @@ Public domain except as listed below: Copyright (c) 2000 - 2004 The Legion Of The Bouncy Castle See licenses/LICENSE-SHA256.txt + ElGamal: + Copyright (c) 2000 - 2013 The Legion of the Bouncy Castle Inc. (http://www.bouncycastle.org) + See licenses/LICENSE-SHA256.txt + AES code: Copyright (c) 1995-2005 The Cryptix Foundation Limited. See licenses/LICENSE-Cryptix.txt diff --git a/core/java/src/net/i2p/crypto/CryptoConstants.java b/core/java/src/net/i2p/crypto/CryptoConstants.java index b9e0327dd..39807d688 100644 --- a/core/java/src/net/i2p/crypto/CryptoConstants.java +++ b/core/java/src/net/i2p/crypto/CryptoConstants.java @@ -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; /** diff --git a/core/java/src/net/i2p/crypto/ElGamalParameterSpec.java b/core/java/src/net/i2p/crypto/ElGamalParameterSpec.java deleted file mode 100644 index cee640804..000000000 --- a/core/java/src/net/i2p/crypto/ElGamalParameterSpec.java +++ /dev/null @@ -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 - * p and a base generator g. - * - * @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 p. - * - * @return the prime modulus p - */ - public BigInteger getP() { - return p; - } - - /** - * Returns the base generator g. - * - * @return the base generator g - */ - public BigInteger getG() { - return g; - } -} diff --git a/core/java/src/net/i2p/crypto/elgamal/ElGamalKey.java b/core/java/src/net/i2p/crypto/elgamal/ElGamalKey.java new file mode 100644 index 000000000..0d2520709 --- /dev/null +++ b/core/java/src/net/i2p/crypto/elgamal/ElGamalKey.java @@ -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(); +} diff --git a/core/java/src/net/i2p/crypto/elgamal/ElGamalPrivateKey.java b/core/java/src/net/i2p/crypto/elgamal/ElGamalPrivateKey.java new file mode 100644 index 000000000..b7093a7da --- /dev/null +++ b/core/java/src/net/i2p/crypto/elgamal/ElGamalPrivateKey.java @@ -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(); +} diff --git a/core/java/src/net/i2p/crypto/elgamal/ElGamalPublicKey.java b/core/java/src/net/i2p/crypto/elgamal/ElGamalPublicKey.java new file mode 100644 index 000000000..fc1b98145 --- /dev/null +++ b/core/java/src/net/i2p/crypto/elgamal/ElGamalPublicKey.java @@ -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(); +} diff --git a/core/java/src/net/i2p/crypto/elgamal/impl/ElGamalPrivateKeyImpl.java b/core/java/src/net/i2p/crypto/elgamal/impl/ElGamalPrivateKeyImpl.java new file mode 100644 index 000000000..9460e6bb9 --- /dev/null +++ b/core/java/src/net/i2p/crypto/elgamal/impl/ElGamalPrivateKeyImpl.java @@ -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()); + } +} diff --git a/core/java/src/net/i2p/crypto/elgamal/impl/ElGamalPublicKeyImpl.java b/core/java/src/net/i2p/crypto/elgamal/impl/ElGamalPublicKeyImpl.java new file mode 100644 index 000000000..2fd16732c --- /dev/null +++ b/core/java/src/net/i2p/crypto/elgamal/impl/ElGamalPublicKeyImpl.java @@ -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()); + } +} diff --git a/core/java/src/net/i2p/crypto/elgamal/impl/package.html b/core/java/src/net/i2p/crypto/elgamal/impl/package.html new file mode 100644 index 000000000..b977d38db --- /dev/null +++ b/core/java/src/net/i2p/crypto/elgamal/impl/package.html @@ -0,0 +1,9 @@ + +

+ Implementation of ElGamal keys, used for I2PProvider. + Modified from Bouncy Castle 1.53. + See net.i2p.crypto.elgamal for license info. +

+ Since 0.9.25. +

+ diff --git a/core/java/src/net/i2p/crypto/elgamal/package.html b/core/java/src/net/i2p/crypto/elgamal/package.html new file mode 100644 index 000000000..f3fc75522 --- /dev/null +++ b/core/java/src/net/i2p/crypto/elgamal/package.html @@ -0,0 +1,29 @@ + +

+ Interfaces for ElGamal keys, used for I2PProvider. + Copied from Bouncy Castle 1.53. +

+ Since 0.9.25. +

+
+
+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.
+
+
+

+ diff --git a/core/java/src/net/i2p/crypto/elgamal/spec/ElGamalGenParameterSpec.java b/core/java/src/net/i2p/crypto/elgamal/spec/ElGamalGenParameterSpec.java new file mode 100644 index 000000000..e2cbc3d6a --- /dev/null +++ b/core/java/src/net/i2p/crypto/elgamal/spec/ElGamalGenParameterSpec.java @@ -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; + } +} diff --git a/core/java/src/net/i2p/crypto/elgamal/spec/ElGamalKeySpec.java b/core/java/src/net/i2p/crypto/elgamal/spec/ElGamalKeySpec.java new file mode 100644 index 000000000..4817e4699 --- /dev/null +++ b/core/java/src/net/i2p/crypto/elgamal/spec/ElGamalKeySpec.java @@ -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; + } +} diff --git a/core/java/src/net/i2p/crypto/elgamal/spec/ElGamalParameterSpec.java b/core/java/src/net/i2p/crypto/elgamal/spec/ElGamalParameterSpec.java new file mode 100644 index 000000000..b4ae9ea36 --- /dev/null +++ b/core/java/src/net/i2p/crypto/elgamal/spec/ElGamalParameterSpec.java @@ -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 + * p and a base generator g. + * + * @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 p. + * + * @return the prime modulus p + */ + public BigInteger getP() { + return p; + } + + /** + * Returns the base generator g. + * + * @return the base generator g + */ + public BigInteger getG() { + return g; + } +} diff --git a/core/java/src/net/i2p/crypto/elgamal/spec/ElGamalPrivateKeySpec.java b/core/java/src/net/i2p/crypto/elgamal/spec/ElGamalPrivateKeySpec.java new file mode 100644 index 000000000..03b292166 --- /dev/null +++ b/core/java/src/net/i2p/crypto/elgamal/spec/ElGamalPrivateKeySpec.java @@ -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 x. + * + * @return the private value x + */ + public BigInteger getX() + { + return x; + } +} diff --git a/core/java/src/net/i2p/crypto/elgamal/spec/ElGamalPublicKeySpec.java b/core/java/src/net/i2p/crypto/elgamal/spec/ElGamalPublicKeySpec.java new file mode 100644 index 000000000..e2e20e993 --- /dev/null +++ b/core/java/src/net/i2p/crypto/elgamal/spec/ElGamalPublicKeySpec.java @@ -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 y. + * + * @return the public value y + */ + public BigInteger getY() + { + return y; + } +} diff --git a/core/java/src/net/i2p/crypto/elgamal/spec/package.html b/core/java/src/net/i2p/crypto/elgamal/spec/package.html new file mode 100644 index 000000000..4c767ee4c --- /dev/null +++ b/core/java/src/net/i2p/crypto/elgamal/spec/package.html @@ -0,0 +1,9 @@ + +

+ Specs ElGamal keys, used for I2PProvider. + Copied from Bouncy Castle 1.53. + See net.i2p.crypto.elgamal for license info. +

+ Since 0.9.25. +

+