Added more curve options to serialization

This commit is contained in:
nathan 2016-03-10 23:35:13 +01:00
parent b3d4dc431c
commit dd908b63df

View File

@ -36,6 +36,9 @@ import java.math.BigInteger;
public public
class EccPrivateKeySerializer extends Serializer<ECPrivateKeyParameters> { class EccPrivateKeySerializer extends Serializer<ECPrivateKeyParameters> {
private static final byte usesName = (byte) 1;
private static final byte usesOid = (byte) 2;
public static public static
void write(Output output, ECPrivateKeyParameters key) { void write(Output output, ECPrivateKeyParameters key) {
byte[] bytes; byte[] bytes;
@ -111,23 +114,30 @@ class EccPrivateKeySerializer extends Serializer<ECPrivateKeyParameters> {
// save out if it's a NAMED curve, or a UN-NAMED curve. If it is named, we can do less work. // save out if it's a NAMED curve, or a UN-NAMED curve. If it is named, we can do less work.
String curveName = curve.getClass() String curveName = curve.getClass()
.getSimpleName(); .getSimpleName();
if (curveName.endsWith("Curve")) {
if (CustomNamedCurves.getByName(curveName) != null) {
// we use the name instead of serializing the full curve
output.writeInt(usesName, true);
output.writeString(curveName);
return;
}
else if (curveName.endsWith("Curve")) {
String cleanedName = curveName.substring(0, curveName.indexOf("Curve")); String cleanedName = curveName.substring(0, curveName.indexOf("Curve"));
curveName = null;
if (!cleanedName.isEmpty()) { if (!cleanedName.isEmpty()) {
ASN1ObjectIdentifier oid = CustomNamedCurves.getOID(cleanedName); ASN1ObjectIdentifier oid = CustomNamedCurves.getOID(cleanedName);
if (oid != null) { if (oid != null) {
// we use the OID (instead of serializing the entire curve) // we use the OID (instead of serializing the entire curve)
output.writeBoolean(true); output.writeInt(usesOid, true);
curveName = oid.getId(); curveName = oid.getId();
output.writeString(curveName); output.writeString(curveName);
return;
} }
} }
} }
// we have to serialize the ENTIRE curve. // we have to serialize the ENTIRE curve.
if (curveName == null) {
// save out the curve info // save out the curve info
BigInteger a = curve.getA() BigInteger a = curve.getA()
.toBigInteger(); .toBigInteger();
@ -175,7 +185,6 @@ class EccPrivateKeySerializer extends Serializer<ECPrivateKeyParameters> {
int coordinateSystem = curve.getCoordinateSystem(); int coordinateSystem = curve.getCoordinateSystem();
output.writeInt(coordinateSystem, true); output.writeInt(coordinateSystem, true);
} }
}
static static
ECCurve deserializeCurve(Input input) { ECCurve deserializeCurve(Input input) {
@ -183,14 +192,22 @@ class EccPrivateKeySerializer extends Serializer<ECPrivateKeyParameters> {
int length; int length;
ECCurve curve; ECCurve curve;
boolean usesOid = input.readBoolean(); int serializatioType = input.readInt(true);
// lookup via name
if (serializatioType == usesName) {
String curveName = input.readString();
X9ECParameters x9Curve = CustomNamedCurves.getByName(curveName);
curve = x9Curve.getCurve();
}
// this means we just lookup the curve via the OID // this means we just lookup the curve via the OID
if (usesOid) { else if (serializatioType == usesOid) {
String oid = input.readString(); String oid = input.readString();
X9ECParameters x9Curve = CustomNamedCurves.getByOID(new ASN1ObjectIdentifier(oid)); X9ECParameters x9Curve = CustomNamedCurves.getByOID(new ASN1ObjectIdentifier(oid));
curve = x9Curve.getCurve(); curve = x9Curve.getCurve();
} }
// we have to read in the entire curve information. // we have to read in the entire curve information.
else { else {
///////////// /////////////