1 package org.bouncycastle.asn1.x9; 2 3 import java.math.BigInteger; 4 5 import org.bouncycastle.asn1.ASN1Encodable; 6 import org.bouncycastle.asn1.ASN1OctetString; 7 import org.bouncycastle.asn1.DERObject; 8 import org.bouncycastle.asn1.DEROctetString; 9 import org.bouncycastle.math.ec.ECFieldElement; 10 11 /** 12 * class for processing an FieldElement as a DER object. 13 */ 14 public class X9FieldElement 15 extends ASN1Encodable 16 { 17 protected ECFieldElement f; 18 19 private static X9IntegerConverter converter = new X9IntegerConverter(); 20 X9FieldElement(ECFieldElement f)21 public X9FieldElement(ECFieldElement f) 22 { 23 this.f = f; 24 } 25 X9FieldElement(BigInteger p, ASN1OctetString s)26 public X9FieldElement(BigInteger p, ASN1OctetString s) 27 { 28 this(new ECFieldElement.Fp(p, new BigInteger(1, s.getOctets()))); 29 } 30 X9FieldElement(int m, int k1, int k2, int k3, ASN1OctetString s)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 getValue()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 */ toASN1Object()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