001 /*
002 * Copyright 2003-2006 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package java.security.spec;
027
028 import java.math.BigInteger;
029 import java.util.Arrays;
030
031 /**
032 * This immutable class holds the necessary values needed to represent
033 * an elliptic curve.
034 *
035 * @see ECField
036 * @see ECFieldFp
037 * @see ECFieldF2m
038 *
039 * @author Valerie Peng
040 * @version 1.11, 05/05/07
041 *
042 * @since 1.5
043 */
044 public class EllipticCurve {
045
046 private final ECField field;
047 private final BigInteger a;
048 private final BigInteger b;
049 private final byte[] seed;
050
051 // Check coefficient c is a valid element in ECField field.
052 private static void checkValidity(ECField field, BigInteger c,
053 String cName) {
054 // can only perform check if field is ECFieldFp or ECFieldF2m.
055 if (field instanceof ECFieldFp) {
056 BigInteger p = ((ECFieldFp) field).getP();
057 if (p.compareTo(c) != 1) {
058 throw new IllegalArgumentException(cName
059 + " is too large");
060 } else if (c.signum() < 0) {
061 throw new IllegalArgumentException(cName
062 + " is negative");
063 }
064 } else if (field instanceof ECFieldF2m) {
065 int m = ((ECFieldF2m) field).getM();
066 if (c.bitLength() > m) {
067 throw new IllegalArgumentException(cName
068 + " is too large");
069 }
070 }
071 }
072
073 /**
074 * Creates an elliptic curve with the specified elliptic field
075 * <code>field</code> and the coefficients <code>a</code> and
076 * <code>b</code>.
077 * @param field the finite field that this elliptic curve is over.
078 * @param a the first coefficient of this elliptic curve.
079 * @param b the second coefficient of this elliptic curve.
080 * @exception NullPointerException if <code>field</code>,
081 * <code>a</code>, or <code>b</code> is null.
082 * @exception IllegalArgumentException if <code>a</code>
083 * or <code>b</code> is not null and not in <code>field</code>.
084 */
085 public EllipticCurve(ECField field, BigInteger a, BigInteger b) {
086 this (field, a, b, null);
087 }
088
089 /**
090 * Creates an elliptic curve with the specified elliptic field
091 * <code>field</code>, the coefficients <code>a</code> and
092 * <code>b</code>, and the <code>seed</code> used for curve generation.
093 * @param field the finite field that this elliptic curve is over.
094 * @param a the first coefficient of this elliptic curve.
095 * @param b the second coefficient of this elliptic curve.
096 * @param seed the bytes used during curve generation for later
097 * validation. Contents of this array are copied to protect against
098 * subsequent modification.
099 * @exception NullPointerException if <code>field</code>,
100 * <code>a</code>, or <code>b</code> is null.
101 * @exception IllegalArgumentException if <code>a</code>
102 * or <code>b</code> is not null and not in <code>field</code>.
103 */
104 public EllipticCurve(ECField field, BigInteger a, BigInteger b,
105 byte[] seed) {
106 if (field == null) {
107 throw new NullPointerException("field is null");
108 }
109 if (a == null) {
110 throw new NullPointerException("first coefficient is null");
111 }
112 if (b == null) {
113 throw new NullPointerException("second coefficient is null");
114 }
115 checkValidity(field, a, "first coefficient");
116 checkValidity(field, b, "second coefficient");
117 this .field = field;
118 this .a = a;
119 this .b = b;
120 if (seed != null) {
121 this .seed = (byte[]) seed.clone();
122 } else {
123 this .seed = null;
124 }
125 }
126
127 /**
128 * Returns the finite field <code>field</code> that this
129 * elliptic curve is over.
130 * @return the field <code>field</code> that this curve
131 * is over.
132 */
133 public ECField getField() {
134 return field;
135 }
136
137 /**
138 * Returns the first coefficient <code>a</code> of the
139 * elliptic curve.
140 * @return the first coefficient <code>a</code>.
141 */
142 public BigInteger getA() {
143 return a;
144 }
145
146 /**
147 * Returns the second coefficient <code>b</code> of the
148 * elliptic curve.
149 * @return the second coefficient <code>b</code>.
150 */
151 public BigInteger getB() {
152 return b;
153 }
154
155 /**
156 * Returns the seeding bytes <code>seed</code> used
157 * during curve generation. May be null if not specified.
158 * @return the seeding bytes <code>seed</code>. A new
159 * array is returned each time this method is called.
160 */
161 public byte[] getSeed() {
162 if (seed == null)
163 return null;
164 else
165 return (byte[]) seed.clone();
166 }
167
168 /**
169 * Compares this elliptic curve for equality with the
170 * specified object.
171 * @param obj the object to be compared.
172 * @return true if <code>obj</code> is an instance of
173 * EllipticCurve and the field, A, B, and seeding bytes
174 * match, false otherwise.
175 */
176 public boolean equals(Object obj) {
177 if (this == obj)
178 return true;
179 if (obj instanceof EllipticCurve) {
180 EllipticCurve curve = (EllipticCurve) obj;
181 if ((field.equals(curve.field)) && (a.equals(curve.a))
182 && (b.equals(curve.b))
183 && (Arrays.equals(seed, curve.seed))) {
184 return true;
185 }
186 }
187 return false;
188 }
189
190 /**
191 * Returns a hash code value for this elliptic curve.
192 * @return a hash code value.
193 */
194 public int hashCode() {
195 return (field.hashCode() << 6 + (a.hashCode() << 4)
196 + (b.hashCode() << 2)
197 + (seed == null ? 0 : seed.length));
198 }
199 }
|