
org.spongycastle.jcajce.provider.asymmetric.util.EC5Util Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of prov Show documentation
Show all versions of prov Show documentation
Spongy Castle is a package-rename (org.bouncycastle.* to org.spongycastle.*) of Bouncy Castle
intended for the Android platform. Android unfortunately ships with a stripped-down version of
Bouncy Castle, which prevents easy upgrades - Spongy Castle overcomes this and provides a full,
up-to-date version of the Bouncy Castle cryptographic libs.
package org.spongycastle.jcajce.provider.asymmetric.util;
import java.math.BigInteger;
import java.security.spec.ECField;
import java.security.spec.ECFieldF2m;
import java.security.spec.ECFieldFp;
import java.security.spec.ECParameterSpec;
import java.security.spec.ECPoint;
import java.security.spec.EllipticCurve;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import org.spongycastle.asn1.x9.ECNamedCurveTable;
import org.spongycastle.asn1.x9.X9ECParameters;
import org.spongycastle.crypto.ec.CustomNamedCurves;
import org.spongycastle.jce.spec.ECNamedCurveParameterSpec;
import org.spongycastle.jce.spec.ECNamedCurveSpec;
import org.spongycastle.math.ec.ECAlgorithms;
import org.spongycastle.math.ec.ECCurve;
public class EC5Util
{
private static Map customCurves = new HashMap();
static
{
Enumeration e = CustomNamedCurves.getNames();
while (e.hasMoreElements())
{
String name = (String)e.nextElement();
X9ECParameters curveParams = ECNamedCurveTable.getByName(name);
if (curveParams != null) // there may not be a regular curve, may just be a custom curve.
{
customCurves.put(curveParams.getCurve(), CustomNamedCurves.getByName(name).getCurve());
}
}
}
public static EllipticCurve convertCurve(
ECCurve curve,
byte[] seed)
{
// TODO: the Sun EC implementation doesn't currently handle the seed properly
// so at the moment it's set to null. Should probably look at making this configurable
if (ECAlgorithms.isFpCurve(curve))
{
return new EllipticCurve(new ECFieldFp(curve.getField().getCharacteristic()), curve.getA().toBigInteger(), curve.getB().toBigInteger(), null);
}
else
{
ECCurve.F2m curveF2m = (ECCurve.F2m)curve;
int ks[];
if (curveF2m.isTrinomial())
{
ks = new int[] { curveF2m.getK1() };
return new EllipticCurve(new ECFieldF2m(curveF2m.getM(), ks), curve.getA().toBigInteger(), curve.getB().toBigInteger(), null);
}
else
{
ks = new int[] { curveF2m.getK3(), curveF2m.getK2(), curveF2m.getK1() };
return new EllipticCurve(new ECFieldF2m(curveF2m.getM(), ks), curve.getA().toBigInteger(), curve.getB().toBigInteger(), null);
}
}
}
public static ECCurve convertCurve(
EllipticCurve ec)
{
ECField field = ec.getField();
BigInteger a = ec.getA();
BigInteger b = ec.getB();
if (field instanceof ECFieldFp)
{
ECCurve.Fp curve = new ECCurve.Fp(((ECFieldFp)field).getP(), a, b);
if (customCurves.containsKey(curve))
{
return (ECCurve)customCurves.get(curve);
}
return curve;
}
else
{
ECFieldF2m fieldF2m = (ECFieldF2m)field;
int m = fieldF2m.getM();
int ks[] = ECUtil.convertMidTerms(fieldF2m.getMidTermsOfReductionPolynomial());
return new ECCurve.F2m(m, ks[0], ks[1], ks[2], a, b);
}
}
public static ECParameterSpec convertSpec(
EllipticCurve ellipticCurve,
org.spongycastle.jce.spec.ECParameterSpec spec)
{
if (spec instanceof ECNamedCurveParameterSpec)
{
return new ECNamedCurveSpec(
((ECNamedCurveParameterSpec)spec).getName(),
ellipticCurve,
new ECPoint(
spec.getG().getAffineXCoord().toBigInteger(),
spec.getG().getAffineYCoord().toBigInteger()),
spec.getN(),
spec.getH());
}
else
{
return new ECParameterSpec(
ellipticCurve,
new ECPoint(
spec.getG().getAffineXCoord().toBigInteger(),
spec.getG().getAffineYCoord().toBigInteger()),
spec.getN(),
spec.getH().intValue());
}
}
public static org.spongycastle.jce.spec.ECParameterSpec convertSpec(
ECParameterSpec ecSpec,
boolean withCompression)
{
ECCurve curve = convertCurve(ecSpec.getCurve());
return new org.spongycastle.jce.spec.ECParameterSpec(
curve,
convertPoint(curve, ecSpec.getGenerator(), withCompression),
ecSpec.getOrder(),
BigInteger.valueOf(ecSpec.getCofactor()),
ecSpec.getCurve().getSeed());
}
public static org.spongycastle.math.ec.ECPoint convertPoint(
ECParameterSpec ecSpec,
ECPoint point,
boolean withCompression)
{
return convertPoint(convertCurve(ecSpec.getCurve()), point, withCompression);
}
public static org.spongycastle.math.ec.ECPoint convertPoint(
ECCurve curve,
ECPoint point,
boolean withCompression)
{
return curve.createPoint(point.getAffineX(), point.getAffineY(), withCompression);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy