From 592f2449d2da429075d37b2e48589b2a1362b2d3 Mon Sep 17 00:00:00 2001
From: zzz
+ 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.
+
+ Interfaces for ElGamal keys, used for I2PProvider.
+ Copied from Bouncy Castle 1.53.
+
+ Since 0.9.25.
+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 @@
+
+
+
+
+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.
+
+
+
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. +
+