Esempio n. 1
0
 /**
  * Creates an elliptic curve characteristic 2 finite field which has 2^{@code m} elements with
  * polynomial basis. The reduction polynomial for this field is based on {@code ks} whose content
  * contains the order of the middle term(s) of the reduction polynomial. Note: A valid reduction
  * polynomial is either a trinomial (X^{@code m} + X^{@code k} + 1 with {@code m} > {@code k}
  * >= 1) or a pentanomial (X^{@code m} + X^{@code k3} + X^{@code k2} + X^{@code k1} + 1 with
  * {@code m} > {@code k3} > {@code k2} > {@code k1} >= 1), so {@code ks} should have
  * length 1 or 3.
  *
  * @param m with 2^{@code m} being the number of elements.
  * @param ks the order of the middle term(s) of the reduction polynomial. Contents of this array
  *     are copied to protect against subsequent modification.
  * @exception NullPointerException if {@code ks} is null.
  * @exception IllegalArgumentException if{@code m} is not positive, or the length of {@code ks} is
  *     neither 1 nor 3, or values in {@code ks} are not between {@code m}-1 and 1 (inclusive) and
  *     in descending order.
  */
 public ECFieldF2m(int m, int[] ks) {
   // check m and ks
   this.m = m;
   this.ks = ks.clone();
   if (m <= 0) {
     throw new IllegalArgumentException("m is not positive");
   }
   if ((this.ks.length != 1) && (this.ks.length != 3)) {
     throw new IllegalArgumentException("length of ks is neither 1 nor 3");
   }
   for (int i = 0; i < this.ks.length; i++) {
     if ((this.ks[i] < 1) || (this.ks[i] > m - 1)) {
       throw new IllegalArgumentException("ks[" + i + "] is out of range");
     }
     if ((i != 0) && (this.ks[i] >= this.ks[i - 1])) {
       throw new IllegalArgumentException("values in ks are not in descending order");
     }
   }
   // convert ks into rp
   this.rp = BigInteger.ONE;
   this.rp = rp.setBit(m);
   for (int j = 0; j < this.ks.length; j++) {
     rp = rp.setBit(this.ks[j]);
   }
 }
Esempio n. 2
0
 /**
  * Encodes an APRep object.
  *
  * @return byte array of encoded APRep object.
  * @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.
  * @exception IOException if an I/O error occurs while reading encoded data.
  */
 public byte[] asn1Encode() throws Asn1Exception, IOException {
   DerOutputStream bytes = new DerOutputStream();
   DerOutputStream temp = new DerOutputStream();
   temp.putInteger(BigInteger.valueOf(pvno));
   bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 0x00), temp);
   temp = new DerOutputStream();
   temp.putInteger(BigInteger.valueOf(msgType));
   bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 0x01), temp);
   bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 0x02), encPart.asn1Encode());
   temp = new DerOutputStream();
   temp.write(DerValue.tag_Sequence, bytes);
   DerOutputStream aprep = new DerOutputStream();
   aprep.write(DerValue.createTag(DerValue.TAG_APPLICATION, true, (byte) 0x0F), temp);
   return aprep.toByteArray();
 }
Esempio n. 3
0
 /**
  * Returns a hash code value for this characteristic 2 finite field.
  *
  * @return a hash code value.
  */
 public int hashCode() {
   int value = m << 5;
   value += (rp == null ? 0 : rp.hashCode());
   // no need to involve ks here since ks and rp
   // should be equivalent.
   return value;
 }
Esempio n. 4
0
 /**
  * Creates an elliptic curve characteristic 2 finite field which has 2^{@code m} elements with
  * polynomial basis. The reduction polynomial for this field is based on {@code rp} whose i-th bit
  * corresponds to the i-th coefficient of the reduction polynomial.
  *
  * <p>Note: A valid reduction polynomial is either a trinomial (X^{@code m} + X^{@code k} + 1 with
  * {@code m} &gt; {@code k} &gt;= 1) or a pentanomial (X^{@code m} + X^{@code k3} + X^{@code k2} +
  * X^{@code k1} + 1 with {@code m} &gt; {@code k3} &gt; {@code k2} &gt; {@code k1} &gt;= 1).
  *
  * @param m with 2^{@code m} being the number of elements.
  * @param rp the BigInteger whose i-th bit corresponds to the i-th coefficient of the reduction
  *     polynomial.
  * @exception NullPointerException if {@code rp} is null.
  * @exception IllegalArgumentException if {@code m} is not positive, or {@code rp} does not
  *     represent a valid reduction polynomial.
  */
 public ECFieldF2m(int m, BigInteger rp) {
   // check m and rp
   this.m = m;
   this.rp = rp;
   if (m <= 0) {
     throw new IllegalArgumentException("m is not positive");
   }
   int bitCount = this.rp.bitCount();
   if (!this.rp.testBit(0) || !this.rp.testBit(m) || ((bitCount != 3) && (bitCount != 5))) {
     throw new IllegalArgumentException("rp does not represent a valid reduction polynomial");
   }
   // convert rp into ks
   BigInteger temp = this.rp.clearBit(0).clearBit(m);
   this.ks = new int[bitCount - 2];
   for (int i = this.ks.length - 1; i >= 0; i--) {
     int index = temp.getLowestSetBit();
     this.ks[i] = index;
     temp = temp.clearBit(index);
   }
 }
Esempio n. 5
0
 /**
  * Encodes a Checksum object. <xmp> Checksum ::= SEQUENCE { cksumtype [0] Int32, checksum [1]
  * OCTET STRING } </xmp>
  *
  * <p>This definition reflects the Network Working Group RFC 4120 specification available at <a
  * href="http://www.ietf.org/rfc/rfc4120.txt">http://www.ietf.org/rfc/rfc4120.txt</a>.
  *
  * @return byte array of enocded Checksum.
  * @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.
  * @exception IOException if an I/O error occurs while reading encoded data.
  */
 public byte[] asn1Encode() throws Asn1Exception, IOException {
   DerOutputStream bytes = new DerOutputStream();
   DerOutputStream temp = new DerOutputStream();
   temp.putInteger(BigInteger.valueOf(cksumType));
   bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 0x00), temp);
   temp = new DerOutputStream();
   temp.putOctetString(checksum);
   bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 0x01), temp);
   temp = new DerOutputStream();
   temp.write(DerValue.tag_Sequence, bytes);
   return temp.toByteArray();
 }
Esempio n. 6
0
 /** Construct a key from its components. Used by the KeyFactory. */
 ECPrivateKeyImpl(BigInteger s, ECParameterSpec params) throws InvalidKeyException {
   this.s = s;
   this.params = params;
   // generate the encoding
   algid = new AlgorithmId(AlgorithmId.EC_oid, ECParameters.getAlgorithmParameters(params));
   try {
     DerOutputStream out = new DerOutputStream();
     out.putInteger(1); // version 1
     byte[] privBytes = ECUtil.trimZeroes(s.toByteArray());
     out.putOctetString(privBytes);
     DerValue val = new DerValue(DerValue.tag_Sequence, out.toByteArray());
     key = val.toByteArray();
   } catch (IOException exc) {
     // should never occur
     throw new InvalidKeyException(exc);
   }
 }