diff options
Diffstat (limited to 'core/src/main/java/org/bouncycastle/crypto/parsers')
-rw-r--r-- | core/src/main/java/org/bouncycastle/crypto/parsers/DHIESPublicKeyParser.java | 31 | ||||
-rw-r--r-- | core/src/main/java/org/bouncycastle/crypto/parsers/ECIESPublicKeyParser.java | 53 |
2 files changed, 84 insertions, 0 deletions
diff --git a/core/src/main/java/org/bouncycastle/crypto/parsers/DHIESPublicKeyParser.java b/core/src/main/java/org/bouncycastle/crypto/parsers/DHIESPublicKeyParser.java new file mode 100644 index 00000000..44f5b571 --- /dev/null +++ b/core/src/main/java/org/bouncycastle/crypto/parsers/DHIESPublicKeyParser.java @@ -0,0 +1,31 @@ +package org.bouncycastle.crypto.parsers; + +import java.io.IOException; +import java.io.InputStream; +import java.math.BigInteger; + +import org.bouncycastle.crypto.KeyParser; +import org.bouncycastle.crypto.params.AsymmetricKeyParameter; +import org.bouncycastle.crypto.params.DHParameters; +import org.bouncycastle.crypto.params.DHPublicKeyParameters; + +public class DHIESPublicKeyParser + implements KeyParser +{ + private DHParameters dhParams; + + public DHIESPublicKeyParser(DHParameters dhParams) + { + this.dhParams = dhParams; + } + + public AsymmetricKeyParameter readKey(InputStream stream) + throws IOException + { + byte[] V = new byte[(dhParams.getP().bitLength() + 7) / 8]; + + stream.read(V, 0, V.length); + + return new DHPublicKeyParameters(new BigInteger(1, V), dhParams); + } +} diff --git a/core/src/main/java/org/bouncycastle/crypto/parsers/ECIESPublicKeyParser.java b/core/src/main/java/org/bouncycastle/crypto/parsers/ECIESPublicKeyParser.java new file mode 100644 index 00000000..1880a506 --- /dev/null +++ b/core/src/main/java/org/bouncycastle/crypto/parsers/ECIESPublicKeyParser.java @@ -0,0 +1,53 @@ +package org.bouncycastle.crypto.parsers; + +import java.io.IOException; +import java.io.InputStream; + +import org.bouncycastle.crypto.KeyParser; +import org.bouncycastle.crypto.params.AsymmetricKeyParameter; +import org.bouncycastle.crypto.params.ECDomainParameters; +import org.bouncycastle.crypto.params.ECPublicKeyParameters; + +public class ECIESPublicKeyParser + implements KeyParser +{ + private ECDomainParameters ecParams; + + public ECIESPublicKeyParser(ECDomainParameters ecParams) + { + this.ecParams = ecParams; + } + + public AsymmetricKeyParameter readKey(InputStream stream) + throws IOException + { + byte[] V; + int first = stream.read(); + + // Decode the public ephemeral key + switch (first) + { + case 0x00: // infinity + throw new IOException("Sender's public key invalid."); + + case 0x02: // compressed + case 0x03: // Byte length calculated as in ECPoint.getEncoded(); + V = new byte[1 + (ecParams.getCurve().getFieldSize()+7)/8]; + break; + + case 0x04: // uncompressed or + case 0x06: // hybrid + case 0x07: // Byte length calculated as in ECPoint.getEncoded(); + V = new byte[1 + 2*((ecParams.getCurve().getFieldSize()+7)/8)]; + break; + + default: + throw new IOException("Sender's public key has invalid point encoding 0x" + Integer.toString(first, 16)); + } + + V[0] = (byte)first; + stream.read(V, 1, V.length - 1); + + return new ECPublicKeyParameters(ecParams.getCurve().decodePoint(V), ecParams); + } +} |