X9FieldID.java revision 5db505e1f6a68c8d5dfdb0fed0b8607dea7bed96
1package org.bouncycastle.asn1.x9; 2 3import java.math.BigInteger; 4 5import org.bouncycastle.asn1.ASN1EncodableVector; 6import org.bouncycastle.asn1.ASN1Integer; 7import org.bouncycastle.asn1.ASN1Object; 8import org.bouncycastle.asn1.ASN1ObjectIdentifier; 9import org.bouncycastle.asn1.ASN1Primitive; 10import org.bouncycastle.asn1.ASN1Sequence; 11import org.bouncycastle.asn1.DERSequence; 12 13/** 14 * ASN.1 def for Elliptic-Curve Field ID structure. See 15 * X9.62, for further details. 16 */ 17public class X9FieldID 18 extends ASN1Object 19 implements X9ObjectIdentifiers 20{ 21 private ASN1ObjectIdentifier id; 22 private ASN1Primitive parameters; 23 24 /** 25 * Constructor for elliptic curves over prime fields 26 * <code>F<sub>2</sub></code>. 27 * @param primeP The prime <code>p</code> defining the prime field. 28 */ 29 public X9FieldID(BigInteger primeP) 30 { 31 this.id = prime_field; 32 this.parameters = new ASN1Integer(primeP); 33 } 34 35 /** 36 * Constructor for elliptic curves over binary fields 37 * <code>F<sub>2<sup>m</sup></sub></code>. 38 * @param m The exponent <code>m</code> of 39 * <code>F<sub>2<sup>m</sup></sub></code>. 40 * @param k1 The integer <code>k1</code> where <code>x<sup>m</sup> + 41 * x<sup>k3</sup> + x<sup>k2</sup> + x<sup>k1</sup> + 1</code> 42 * represents the reduction polynomial <code>f(z)</code>. 43 * @param k2 The integer <code>k2</code> where <code>x<sup>m</sup> + 44 * x<sup>k3</sup> + x<sup>k2</sup> + x<sup>k1</sup> + 1</code> 45 * represents the reduction polynomial <code>f(z)</code>. 46 * @param k3 The integer <code>k3</code> where <code>x<sup>m</sup> + 47 * x<sup>k3</sup> + x<sup>k2</sup> + x<sup>k1</sup> + 1</code> 48 * represents the reduction polynomial <code>f(z)</code>.. 49 */ 50 public X9FieldID(int m, int k1, int k2, int k3) 51 { 52 this.id = characteristic_two_field; 53 ASN1EncodableVector fieldIdParams = new ASN1EncodableVector(); 54 fieldIdParams.add(new ASN1Integer(m)); 55 56 if (k2 == 0) 57 { 58 fieldIdParams.add(tpBasis); 59 fieldIdParams.add(new ASN1Integer(k1)); 60 } 61 else 62 { 63 fieldIdParams.add(ppBasis); 64 ASN1EncodableVector pentanomialParams = new ASN1EncodableVector(); 65 pentanomialParams.add(new ASN1Integer(k1)); 66 pentanomialParams.add(new ASN1Integer(k2)); 67 pentanomialParams.add(new ASN1Integer(k3)); 68 fieldIdParams.add(new DERSequence(pentanomialParams)); 69 } 70 71 this.parameters = new DERSequence(fieldIdParams); 72 } 73 74 private X9FieldID( 75 ASN1Sequence seq) 76 { 77 this.id = ASN1ObjectIdentifier.getInstance(seq.getObjectAt(0)); 78 this.parameters = seq.getObjectAt(1).toASN1Primitive(); 79 } 80 81 public static X9FieldID getInstance(Object obj) 82 { 83 if (obj instanceof X9FieldID) 84 { 85 return (X9FieldID)obj; 86 } 87 88 if (obj != null) 89 { 90 return new X9FieldID(ASN1Sequence.getInstance(obj)); 91 } 92 93 return null; 94 } 95 96 public ASN1ObjectIdentifier getIdentifier() 97 { 98 return id; 99 } 100 101 public ASN1Primitive getParameters() 102 { 103 return parameters; 104 } 105 106 /** 107 * Produce a DER encoding of the following structure. 108 * <pre> 109 * FieldID ::= SEQUENCE { 110 * fieldType FIELD-ID.&id({IOSet}), 111 * parameters FIELD-ID.&Type({IOSet}{@fieldType}) 112 * } 113 * </pre> 114 */ 115 public ASN1Primitive toASN1Primitive() 116 { 117 ASN1EncodableVector v = new ASN1EncodableVector(); 118 119 v.add(this.id); 120 v.add(this.parameters); 121 122 return new DERSequence(v); 123 } 124} 125