1package org.bouncycastle.asn1.x9; 2 3import java.math.BigInteger; 4 5import org.bouncycastle.asn1.ASN1Encodable; 6import org.bouncycastle.asn1.ASN1OctetString; 7import org.bouncycastle.asn1.DERObject; 8import org.bouncycastle.asn1.DEROctetString; 9import org.bouncycastle.math.ec.ECFieldElement; 10 11/** 12 * class for processing an FieldElement as a DER object. 13 */ 14public class X9FieldElement 15 extends ASN1Encodable 16{ 17 protected ECFieldElement f; 18 19 private static X9IntegerConverter converter = new X9IntegerConverter(); 20 21 public X9FieldElement(ECFieldElement f) 22 { 23 this.f = f; 24 } 25 26 public X9FieldElement(BigInteger p, ASN1OctetString s) 27 { 28 this(new ECFieldElement.Fp(p, new BigInteger(1, s.getOctets()))); 29 } 30 31 public X9FieldElement(int m, int k1, int k2, int k3, ASN1OctetString s) 32 { 33 this(new ECFieldElement.F2m(m, k1, k2, k3, new BigInteger(1, s.getOctets()))); 34 } 35 36 public ECFieldElement getValue() 37 { 38 return f; 39 } 40 41 /** 42 * Produce an object suitable for an ASN1OutputStream. 43 * <pre> 44 * FieldElement ::= OCTET STRING 45 * </pre> 46 * <p> 47 * <ol> 48 * <li> if <i>q</i> is an odd prime then the field element is 49 * processed as an Integer and converted to an octet string 50 * according to x 9.62 4.3.1.</li> 51 * <li> if <i>q</i> is 2<sup>m</sup> then the bit string 52 * contained in the field element is converted into an octet 53 * string with the same ordering padded at the front if necessary. 54 * </li> 55 * </ol> 56 */ 57 public DERObject toASN1Object() 58 { 59 int byteCount = converter.getByteLength(f); 60 byte[] paddedBigInteger = converter.integerToBytes(f.toBigInteger(), byteCount); 61 62 return new DEROctetString(paddedBigInteger); 63 } 64} 65