jdk/src/share/classes/java/security/spec/EllipticCurve.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2 90ce3da70b43
child 9556 79c47f5e241b
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2003, 2006, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.security.spec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.math.BigInteger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * This immutable class holds the necessary values needed to represent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * an elliptic curve.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * @see ECField
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * @see ECFieldFp
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * @see ECFieldF2m
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * @author Valerie Peng
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
public class EllipticCurve {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private final ECField field;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private final BigInteger a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private final BigInteger b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private final byte[] seed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    // Check coefficient c is a valid element in ECField field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private static void checkValidity(ECField field, BigInteger c,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        String cName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        // can only perform check if field is ECFieldFp or ECFieldF2m.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        if (field instanceof ECFieldFp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            BigInteger p = ((ECFieldFp)field).getP();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            if (p.compareTo(c) != 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                throw new IllegalArgumentException(cName + " is too large");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            } else if (c.signum() < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                throw new IllegalArgumentException(cName + " is negative");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        } else if (field instanceof ECFieldF2m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            int m = ((ECFieldF2m)field).getM();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            if (c.bitLength() > m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                throw new IllegalArgumentException(cName + " is too large");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * Creates an elliptic curve with the specified elliptic field
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * <code>field</code> and the coefficients <code>a</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * <code>b</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * @param field the finite field that this elliptic curve is over.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * @param a the first coefficient of this elliptic curve.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * @param b the second coefficient of this elliptic curve.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * @exception NullPointerException if <code>field</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * <code>a</code>, or <code>b</code> is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * @exception IllegalArgumentException if <code>a</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * or <code>b</code> is not null and not in <code>field</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    public EllipticCurve(ECField field, BigInteger a,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                         BigInteger b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        this(field, a, b, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * Creates an elliptic curve with the specified elliptic field
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * <code>field</code>, the coefficients <code>a</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * <code>b</code>, and the <code>seed</code> used for curve generation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * @param field the finite field that this elliptic curve is over.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * @param a the first coefficient of this elliptic curve.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * @param b the second coefficient of this elliptic curve.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * @param seed the bytes used during curve generation for later
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * validation. Contents of this array are copied to protect against
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * subsequent modification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * @exception NullPointerException if <code>field</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * <code>a</code>, or <code>b</code> is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * @exception IllegalArgumentException if <code>a</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * or <code>b</code> is not null and not in <code>field</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    public EllipticCurve(ECField field, BigInteger a,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                         BigInteger b, byte[] seed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        if (field == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            throw new NullPointerException("field is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        if (a == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            throw new NullPointerException("first coefficient is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        if (b == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            throw new NullPointerException("second coefficient is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        checkValidity(field, a, "first coefficient");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        checkValidity(field, b, "second coefficient");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        this.field = field;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        this.a = a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        this.b = b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        if (seed != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            this.seed = seed.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            this.seed = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * Returns the finite field <code>field</code> that this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * elliptic curve is over.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * @return the field <code>field</code> that this curve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * is over.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    public ECField getField() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        return field;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * Returns the first coefficient <code>a</code> of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * elliptic curve.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @return the first coefficient <code>a</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    public BigInteger getA() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * Returns the second coefficient <code>b</code> of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * elliptic curve.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * @return the second coefficient <code>b</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    public BigInteger getB() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        return b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * Returns the seeding bytes <code>seed</code> used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * during curve generation. May be null if not specified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @return the seeding bytes <code>seed</code>. A new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * array is returned each time this method is called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    public byte[] getSeed() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        if (seed == null) return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        else return seed.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * Compares this elliptic curve for equality with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * specified object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * @param obj the object to be compared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * @return true if <code>obj</code> is an instance of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * EllipticCurve and the field, A, B, and seeding bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * match, false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        if (this == obj) return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        if (obj instanceof EllipticCurve) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            EllipticCurve curve = (EllipticCurve) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            if ((field.equals(curve.field)) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                (a.equals(curve.a)) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                (b.equals(curve.b)) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                (Arrays.equals(seed, curve.seed))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * Returns a hash code value for this elliptic curve.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * @return a hash code value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        return (field.hashCode() << 6 +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            (a.hashCode() << 4) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            (b.hashCode() << 2) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            (seed==null? 0:seed.length));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
}