From 592f2449d2da429075d37b2e48589b2a1362b2d3 Mon Sep 17 00:00:00 2001 From: zzz <zzz@mail.i2p> Date: Sat, 23 Jan 2016 14:51:52 +0000 Subject: [PATCH] ElGamal classes, from Bouncy Castle 1.53, for I2PProvider. License: BSD Encoding/decoding/sigs: todo. --- LICENSE.txt | 4 + .../src/net/i2p/crypto/CryptoConstants.java | 1 + .../net/i2p/crypto/ElGamalParameterSpec.java | 66 ---------- .../net/i2p/crypto/elgamal/ElGamalKey.java | 11 ++ .../i2p/crypto/elgamal/ElGamalPrivateKey.java | 11 ++ .../i2p/crypto/elgamal/ElGamalPublicKey.java | 11 ++ .../elgamal/impl/ElGamalPrivateKeyImpl.java | 115 ++++++++++++++++++ .../elgamal/impl/ElGamalPublicKeyImpl.java | 106 ++++++++++++++++ .../net/i2p/crypto/elgamal/impl/package.html | 9 ++ .../src/net/i2p/crypto/elgamal/package.html | 29 +++++ .../elgamal/spec/ElGamalGenParameterSpec.java | 28 +++++ .../crypto/elgamal/spec/ElGamalKeySpec.java | 20 +++ .../elgamal/spec/ElGamalParameterSpec.java | 46 +++++++ .../elgamal/spec/ElGamalPrivateKeySpec.java | 33 +++++ .../elgamal/spec/ElGamalPublicKeySpec.java | 33 +++++ .../net/i2p/crypto/elgamal/spec/package.html | 9 ++ 16 files changed, 466 insertions(+), 66 deletions(-) delete mode 100644 core/java/src/net/i2p/crypto/ElGamalParameterSpec.java create mode 100644 core/java/src/net/i2p/crypto/elgamal/ElGamalKey.java create mode 100644 core/java/src/net/i2p/crypto/elgamal/ElGamalPrivateKey.java create mode 100644 core/java/src/net/i2p/crypto/elgamal/ElGamalPublicKey.java create mode 100644 core/java/src/net/i2p/crypto/elgamal/impl/ElGamalPrivateKeyImpl.java create mode 100644 core/java/src/net/i2p/crypto/elgamal/impl/ElGamalPublicKeyImpl.java create mode 100644 core/java/src/net/i2p/crypto/elgamal/impl/package.html create mode 100644 core/java/src/net/i2p/crypto/elgamal/package.html create mode 100644 core/java/src/net/i2p/crypto/elgamal/spec/ElGamalGenParameterSpec.java create mode 100644 core/java/src/net/i2p/crypto/elgamal/spec/ElGamalKeySpec.java create mode 100644 core/java/src/net/i2p/crypto/elgamal/spec/ElGamalParameterSpec.java create mode 100644 core/java/src/net/i2p/crypto/elgamal/spec/ElGamalPrivateKeySpec.java create mode 100644 core/java/src/net/i2p/crypto/elgamal/spec/ElGamalPublicKeySpec.java create mode 100644 core/java/src/net/i2p/crypto/elgamal/spec/package.html diff --git a/LICENSE.txt b/LICENSE.txt index 295da33aa2..e6ae86cba8 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 b9e0327dd3..39807d688f 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 cee6408046..0000000000 --- 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 - * <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; - } -} 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 0000000000..0d2520709f --- /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 0000000000..b7093a7dae --- /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 0000000000..fc1b981459 --- /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 0000000000..9460e6bb93 --- /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 0000000000..2fd16732c9 --- /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 0000000000..b977d38db0 --- /dev/null +++ b/core/java/src/net/i2p/crypto/elgamal/impl/package.html @@ -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> 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 0000000000..f3fc75522d --- /dev/null +++ b/core/java/src/net/i2p/crypto/elgamal/package.html @@ -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> 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 0000000000..e2cbc3d6a3 --- /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 0000000000..4817e4699c --- /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 0000000000..b4ae9ea36e --- /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 + * <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; + } +} 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 0000000000..03b292166c --- /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 <code>x</code>. + * + * @return the private value <code>x</code> + */ + 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 0000000000..e2e20e993f --- /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 <code>y</code>. + * + * @return the public value <code>y</code> + */ + 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 0000000000..4c767ee4c8 --- /dev/null +++ b/core/java/src/net/i2p/crypto/elgamal/spec/package.html @@ -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> -- GitLab