diff options
Diffstat (limited to 'crypto/src/asn1/x9/X9ECParameters.cs')
-rw-r--r-- | crypto/src/asn1/x9/X9ECParameters.cs | 106 |
1 files changed, 61 insertions, 45 deletions
diff --git a/crypto/src/asn1/x9/X9ECParameters.cs b/crypto/src/asn1/x9/X9ECParameters.cs index d025b36ce..6389defa8 100644 --- a/crypto/src/asn1/x9/X9ECParameters.cs +++ b/crypto/src/asn1/x9/X9ECParameters.cs @@ -2,6 +2,7 @@ using System; using Org.BouncyCastle.Math; using Org.BouncyCastle.Math.EC; +using Org.BouncyCastle.Math.Field; namespace Org.BouncyCastle.Asn1.X9 { @@ -19,7 +20,7 @@ namespace Org.BouncyCastle.Asn1.X9 private BigInteger h; private byte[] seed; - public X9ECParameters( + public X9ECParameters( Asn1Sequence seq) { if (!(seq[0] is DerInteger) @@ -28,7 +29,7 @@ namespace Org.BouncyCastle.Asn1.X9 throw new ArgumentException("bad version in X9ECParameters"); } - X9Curve x9c = null; + X9Curve x9c = null; if (seq[2] is X9Curve) { x9c = (X9Curve) seq[2]; @@ -36,14 +37,14 @@ namespace Org.BouncyCastle.Asn1.X9 else { x9c = new X9Curve( - new X9FieldID( - (Asn1Sequence) seq[1]), - (Asn1Sequence) seq[2]); + new X9FieldID( + (Asn1Sequence) seq[1]), + (Asn1Sequence) seq[2]); } - this.curve = x9c.Curve; + this.curve = x9c.Curve; - if (seq[3] is X9ECPoint) + if (seq[3] is X9ECPoint) { this.g = ((X9ECPoint) seq[3]).Point; } @@ -52,16 +53,16 @@ namespace Org.BouncyCastle.Asn1.X9 this.g = new X9ECPoint(curve, (Asn1OctetString) seq[3]).Point; } - this.n = ((DerInteger) seq[4]).Value; + this.n = ((DerInteger) seq[4]).Value; this.seed = x9c.GetSeed(); - if (seq.Count == 6) + if (seq.Count == 6) { this.h = ((DerInteger) seq[5]).Value; } } - public X9ECParameters( + public X9ECParameters( ECCurve curve, ECPoint g, BigInteger n) @@ -69,7 +70,7 @@ namespace Org.BouncyCastle.Asn1.X9 { } - public X9ECParameters( + public X9ECParameters( ECCurve curve, ECPoint g, BigInteger n, @@ -78,7 +79,7 @@ namespace Org.BouncyCastle.Asn1.X9 { } - public X9ECParameters( + public X9ECParameters( ECCurve curve, ECPoint g, BigInteger n, @@ -91,53 +92,68 @@ namespace Org.BouncyCastle.Asn1.X9 this.h = h; this.seed = seed; - if (curve is FpCurve) - { - this.fieldID = new X9FieldID(((FpCurve) curve).Q); - } - else if (curve is F2mCurve) - { - F2mCurve curveF2m = (F2mCurve) curve; - this.fieldID = new X9FieldID(curveF2m.M, curveF2m.K1, - curveF2m.K2, curveF2m.K3); - } + if (ECAlgorithms.IsFpCurve(curve)) + { + this.fieldID = new X9FieldID(curve.Field.Characteristic); + } + else if (ECAlgorithms.IsF2mCurve(curve)) + { + IPolynomialExtensionField field = (IPolynomialExtensionField)curve.Field; + int[] exponents = field.MinimalPolynomial.GetExponentsPresent(); + if (exponents.Length == 3) + { + this.fieldID = new X9FieldID(exponents[2], exponents[1]); + } + else if (exponents.Length == 5) + { + this.fieldID = new X9FieldID(exponents[4], exponents[1], exponents[2], exponents[3]); + } + else + { + throw new ArgumentException("Only trinomial and pentomial curves are supported"); + } + } + else + { + throw new ArgumentException("'curve' is of an unsupported type"); + } } - public ECCurve Curve + public ECCurve Curve { - get { return curve; } + get { return curve; } } - public ECPoint G + public ECPoint G { get { return g; } } - public BigInteger N + public BigInteger N { get { return n; } } - public BigInteger H + public BigInteger H { get - { - if (h == null) - { - // TODO - this should be calculated, it will cause issues with custom curves. - return BigInteger.One; - } - - return h; - } + { + if (h == null) + { + // TODO - this should be calculated, it will cause issues with custom curves. + return BigInteger.One; + } + + return h; + } } - public byte[] GetSeed() + public byte[] GetSeed() { return seed; } - /** + /** * Produce an object suitable for an Asn1OutputStream. * <pre> * ECParameters ::= Sequence { @@ -153,18 +169,18 @@ namespace Org.BouncyCastle.Asn1.X9 public override Asn1Object ToAsn1Object() { Asn1EncodableVector v = new Asn1EncodableVector( - new DerInteger(1), - fieldID, - new X9Curve(curve, seed), - new X9ECPoint(g), - new DerInteger(n)); + new DerInteger(1), + fieldID, + new X9Curve(curve, seed), + new X9ECPoint(g), + new DerInteger(n)); - if (h != null) + if (h != null) { v.Add(new DerInteger(h)); } - return new DerSequence(v); + return new DerSequence(v); } } } |