Skip to content

Commit

Permalink
github bcgit#589/bcgit#620 multi-release classes for Java11+
Browse files Browse the repository at this point in the history
  • Loading branch information
dghgit committed Mar 18, 2020
1 parent dd7f708 commit 18c3bb7
Show file tree
Hide file tree
Showing 4 changed files with 1,017 additions and 0 deletions.
@@ -0,0 +1,192 @@
package org.bouncycastle.jcajce.provider.asymmetric.edec;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.security.interfaces.XECPrivateKey;
import java.security.spec.AlgorithmParameterSpec;
import java.security.spec.NamedParameterSpec;
import java.util.Optional;

import org.bouncycastle.asn1.ASN1Encodable;
import org.bouncycastle.asn1.ASN1OctetString;
import org.bouncycastle.asn1.ASN1Set;
import org.bouncycastle.asn1.edec.EdECObjectIdentifiers;
import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
import org.bouncycastle.crypto.params.AsymmetricKeyParameter;
import org.bouncycastle.crypto.params.X25519PrivateKeyParameters;
import org.bouncycastle.crypto.params.X448PrivateKeyParameters;
import org.bouncycastle.crypto.util.PrivateKeyInfoFactory;
import org.bouncycastle.jcajce.interfaces.XDHPrivateKey;
import org.bouncycastle.jcajce.interfaces.XDHPublicKey;
import org.bouncycastle.util.Arrays;

class BC11XDHPrivateKey
implements XDHPrivateKey, XECPrivateKey
{
static final long serialVersionUID = 1L;

private transient AsymmetricKeyParameter xdhPrivateKey;

private final boolean hasPublicKey;
private final byte[] attributes;

BC11XDHPrivateKey(AsymmetricKeyParameter privKey)
{
this.hasPublicKey = true;
this.attributes = null;
this.xdhPrivateKey = privKey;
}

BC11XDHPrivateKey(PrivateKeyInfo keyInfo)
throws IOException
{
this.hasPublicKey = keyInfo.hasPublicKey();
this.attributes = (keyInfo.getAttributes() != null) ? keyInfo.getAttributes().getEncoded() : null;

populateFromPrivateKeyInfo(keyInfo);
}

private void populateFromPrivateKeyInfo(PrivateKeyInfo keyInfo)
throws IOException
{
ASN1Encodable keyOcts = keyInfo.parsePrivateKey();
if (EdECObjectIdentifiers.id_X448.equals(keyInfo.getPrivateKeyAlgorithm().getAlgorithm()))
{
xdhPrivateKey = new X448PrivateKeyParameters(ASN1OctetString.getInstance(keyOcts).getOctets(), 0);
}
else
{
xdhPrivateKey = new X25519PrivateKeyParameters(ASN1OctetString.getInstance(keyOcts).getOctets(), 0);
}
}

public AlgorithmParameterSpec getParams()
{
if (xdhPrivateKey instanceof X448PrivateKeyParameters)
{
return NamedParameterSpec.X448;
}
else
{
return NamedParameterSpec.X25519;
}
}

public Optional<byte[]> getScalar()
{
if (xdhPrivateKey instanceof X448PrivateKeyParameters)
{
return Optional.of(((X448PrivateKeyParameters)xdhPrivateKey).getEncoded());
}
else
{
return Optional.of(((X25519PrivateKeyParameters)xdhPrivateKey).getEncoded());
}
}

public String getAlgorithm()
{
return (xdhPrivateKey instanceof X448PrivateKeyParameters) ? "X448" : "X25519";
}

public String getFormat()
{
return "PKCS#8";
}

public byte[] getEncoded()
{
try
{
ASN1Set attrSet = ASN1Set.getInstance(attributes);
PrivateKeyInfo privInfo = PrivateKeyInfoFactory.createPrivateKeyInfo(xdhPrivateKey, attrSet);

if (hasPublicKey)
{
return privInfo.getEncoded();
}
else
{
return new PrivateKeyInfo(privInfo.getPrivateKeyAlgorithm(), privInfo.parsePrivateKey(), attrSet).getEncoded();
}
}
catch (IOException e)
{
return null;
}
}

public XDHPublicKey getPublicKey()
{
if (xdhPrivateKey instanceof X448PrivateKeyParameters)
{
return new BCXDHPublicKey(((X448PrivateKeyParameters)xdhPrivateKey).generatePublicKey());
}
else
{
return new BCXDHPublicKey(((X25519PrivateKeyParameters)xdhPrivateKey).generatePublicKey());
}
}

AsymmetricKeyParameter engineGetKeyParameters()
{
return xdhPrivateKey;
}

public String toString()
{
AsymmetricKeyParameter pubKey;
if (xdhPrivateKey instanceof X448PrivateKeyParameters)
{
pubKey = ((X448PrivateKeyParameters)xdhPrivateKey).generatePublicKey();
}
else
{
pubKey = ((X25519PrivateKeyParameters)xdhPrivateKey).generatePublicKey();
}
return Utils.keyToString("Private Key", getAlgorithm(), pubKey);
}

public boolean equals(Object o)
{
if (o == this)
{
return true;
}

if (!(o instanceof BC11XDHPrivateKey))
{
return false;
}

BC11XDHPrivateKey other = (BC11XDHPrivateKey)o;

return Arrays.areEqual(other.getEncoded(), this.getEncoded());
}

public int hashCode()
{
return Arrays.hashCode(this.getEncoded());
}

private void readObject(
ObjectInputStream in)
throws IOException, ClassNotFoundException
{
in.defaultReadObject();

byte[] enc = (byte[])in.readObject();

populateFromPrivateKeyInfo(PrivateKeyInfo.getInstance(enc));
}

private void writeObject(
ObjectOutputStream out)
throws IOException
{
out.defaultWriteObject();

out.writeObject(this.getEncoded());
}
}
@@ -0,0 +1,184 @@
package org.bouncycastle.jcajce.provider.asymmetric.edec;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.math.BigInteger;
import java.security.interfaces.XECPublicKey;
import java.security.spec.AlgorithmParameterSpec;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.NamedParameterSpec;

import org.bouncycastle.asn1.edec.EdECObjectIdentifiers;
import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
import org.bouncycastle.crypto.params.AsymmetricKeyParameter;
import org.bouncycastle.crypto.params.X25519PublicKeyParameters;
import org.bouncycastle.crypto.params.X448PublicKeyParameters;
import org.bouncycastle.jcajce.interfaces.XDHPublicKey;
import org.bouncycastle.util.Arrays;

class BC11XDHPublicKey
implements XDHPublicKey, XECPublicKey
{
static final long serialVersionUID = 1L;

private transient AsymmetricKeyParameter xdhPublicKey;

BC11XDHPublicKey(AsymmetricKeyParameter pubKey)
{
this.xdhPublicKey = pubKey;
}

BC11XDHPublicKey(SubjectPublicKeyInfo keyInfo)
{
populateFromPubKeyInfo(keyInfo);
}

BC11XDHPublicKey(byte[] prefix, byte[] rawData)
throws InvalidKeySpecException
{
int prefixLength = prefix.length;

if (Utils.isValidPrefix(prefix, rawData))
{
if ((rawData.length - prefixLength) == X448PublicKeyParameters.KEY_SIZE)
{
xdhPublicKey = new X448PublicKeyParameters(rawData, prefixLength);
}
else if ((rawData.length - prefixLength) == X25519PublicKeyParameters.KEY_SIZE)
{
xdhPublicKey = new X25519PublicKeyParameters(rawData, prefixLength);
}
else
{
throw new InvalidKeySpecException("raw key data not recognised");
}
}
else
{
throw new InvalidKeySpecException("raw key data not recognised");
}
}

private void populateFromPubKeyInfo(SubjectPublicKeyInfo keyInfo)
{
if (EdECObjectIdentifiers.id_X448.equals(keyInfo.getAlgorithm().getAlgorithm()))
{
xdhPublicKey = new X448PublicKeyParameters(keyInfo.getPublicKeyData().getOctets(), 0);
}
else
{
xdhPublicKey = new X25519PublicKeyParameters(keyInfo.getPublicKeyData().getOctets(), 0);
}
}

public AlgorithmParameterSpec getParams()
{
if (xdhPublicKey instanceof X448PublicKeyParameters)
{
return NamedParameterSpec.X448;
}
else
{
return NamedParameterSpec.X25519;
}
}

public BigInteger getU()
{
if (xdhPublicKey instanceof X448PublicKeyParameters)
{
return new BigInteger(1, ((X448PublicKeyParameters)xdhPublicKey).getEncoded());
}
else
{
return new BigInteger(1, ((X25519PublicKeyParameters)xdhPublicKey).getEncoded());
}
}

public String getAlgorithm()
{
return (xdhPublicKey instanceof X448PublicKeyParameters) ? "X448" : "X25519";
}

public String getFormat()
{
return "X.509";
}

public byte[] getEncoded()
{
if (xdhPublicKey instanceof X448PublicKeyParameters)
{
byte[] encoding = new byte[KeyFactorySpi.x448Prefix.length + X448PublicKeyParameters.KEY_SIZE];

System.arraycopy(KeyFactorySpi.x448Prefix, 0, encoding, 0, KeyFactorySpi.x448Prefix.length);

((X448PublicKeyParameters)xdhPublicKey).encode(encoding, KeyFactorySpi.x448Prefix.length);

return encoding;
}
else
{
byte[] encoding = new byte[KeyFactorySpi.x25519Prefix.length + X25519PublicKeyParameters.KEY_SIZE];

System.arraycopy(KeyFactorySpi.x25519Prefix, 0, encoding, 0, KeyFactorySpi.x25519Prefix.length);

((X25519PublicKeyParameters)xdhPublicKey).encode(encoding, KeyFactorySpi.x25519Prefix.length);

return encoding;
}
}

AsymmetricKeyParameter engineGetKeyParameters()
{
return xdhPublicKey;
}

public String toString()
{
return Utils.keyToString("Public Key", getAlgorithm(), xdhPublicKey);
}

public boolean equals(Object o)
{
if (o == this)
{
return true;
}

if (!(o instanceof BC11XDHPublicKey))
{
return false;
}

BC11XDHPublicKey other = (BC11XDHPublicKey)o;

return Arrays.areEqual(other.getEncoded(), this.getEncoded());
}

public int hashCode()
{
return Arrays.hashCode(this.getEncoded());
}

private void readObject(
ObjectInputStream in)
throws IOException, ClassNotFoundException
{
in.defaultReadObject();

byte[] enc = (byte[])in.readObject();

populateFromPubKeyInfo(SubjectPublicKeyInfo.getInstance(enc));
}

private void writeObject(
ObjectOutputStream out)
throws IOException
{
out.defaultWriteObject();

out.writeObject(this.getEncoded());
}
}

0 comments on commit 18c3bb7

Please sign in to comment.