src/java.base/share/classes/java/security/spec/EllipticCurve.java
author jboes
Fri, 20 Sep 2019 11:07:52 +0100
changeset 58242 94bb65cb37d3
parent 47216 71c04702a3d5
permissions -rw-r--r--
8230648: Replace @exception tag with @throws in java.base Summary: Minor coding style update of javadoc tag in any file in java.base Reviewed-by: prappo, lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
     2
 * Copyright (c) 2003, 2019, 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
18552
005e115dc6ee 8017326: Cleanup of the javadoc <code> tag in java.security.spec
juh
parents: 18156
diff changeset
    71
     * {@code field} and the coefficients {@code a} and
005e115dc6ee 8017326: Cleanup of the javadoc <code> tag in java.security.spec
juh
parents: 18156
diff changeset
    72
     * {@code b}.
2
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.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
    76
     * @throws    NullPointerException if {@code field},
18552
005e115dc6ee 8017326: Cleanup of the javadoc <code> tag in java.security.spec
juh
parents: 18156
diff changeset
    77
     * {@code a}, or {@code b} is null.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
    78
     * @throws    IllegalArgumentException if {@code a}
18552
005e115dc6ee 8017326: Cleanup of the javadoc <code> tag in java.security.spec
juh
parents: 18156
diff changeset
    79
     * or {@code b} is not null and not in {@code field}.
2
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
18552
005e115dc6ee 8017326: Cleanup of the javadoc <code> tag in java.security.spec
juh
parents: 18156
diff changeset
    88
     * {@code field}, the coefficients {@code a} and
005e115dc6ee 8017326: Cleanup of the javadoc <code> tag in java.security.spec
juh
parents: 18156
diff changeset
    89
     * {@code b}, and the {@code seed} used for curve generation.
2
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.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
    96
     * @throws    NullPointerException if {@code field},
18552
005e115dc6ee 8017326: Cleanup of the javadoc <code> tag in java.security.spec
juh
parents: 18156
diff changeset
    97
     * {@code a}, or {@code b} is null.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
    98
     * @throws    IllegalArgumentException if {@code a}
18552
005e115dc6ee 8017326: Cleanup of the javadoc <code> tag in java.security.spec
juh
parents: 18156
diff changeset
    99
     * or {@code b} is not null and not in {@code field}.
2
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
    /**
18552
005e115dc6ee 8017326: Cleanup of the javadoc <code> tag in java.security.spec
juh
parents: 18156
diff changeset
   125
     * Returns the finite field {@code field} that this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * elliptic curve is over.
18552
005e115dc6ee 8017326: Cleanup of the javadoc <code> tag in java.security.spec
juh
parents: 18156
diff changeset
   127
     * @return the field {@code field} that this curve
2
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
    /**
18552
005e115dc6ee 8017326: Cleanup of the javadoc <code> tag in java.security.spec
juh
parents: 18156
diff changeset
   135
     * Returns the first coefficient {@code a} of the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * elliptic curve.
18552
005e115dc6ee 8017326: Cleanup of the javadoc <code> tag in java.security.spec
juh
parents: 18156
diff changeset
   137
     * @return the first coefficient {@code a}.
2
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
    /**
18552
005e115dc6ee 8017326: Cleanup of the javadoc <code> tag in java.security.spec
juh
parents: 18156
diff changeset
   144
     * Returns the second coefficient {@code b} of the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * elliptic curve.
18552
005e115dc6ee 8017326: Cleanup of the javadoc <code> tag in java.security.spec
juh
parents: 18156
diff changeset
   146
     * @return the second coefficient {@code b}.
2
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
    /**
18552
005e115dc6ee 8017326: Cleanup of the javadoc <code> tag in java.security.spec
juh
parents: 18156
diff changeset
   153
     * Returns the seeding bytes {@code seed} used
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * during curve generation. May be null if not specified.
18552
005e115dc6ee 8017326: Cleanup of the javadoc <code> tag in java.security.spec
juh
parents: 18156
diff changeset
   155
     * @return the seeding bytes {@code seed}. A new
2
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.
18552
005e115dc6ee 8017326: Cleanup of the javadoc <code> tag in java.security.spec
juh
parents: 18156
diff changeset
   167
     * @return true if {@code obj} is an instance of
9556
79c47f5e241b 6738532: Error in Elliptic Curve NamedCurve determination. (related to PKCS11)
vinnie
parents: 5506
diff changeset
   168
     * EllipticCurve and the field, A, and B match, false otherwise.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        if (this == obj) return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        if (obj instanceof EllipticCurve) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            EllipticCurve curve = (EllipticCurve) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            if ((field.equals(curve.field)) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                (a.equals(curve.a)) &&
9556
79c47f5e241b 6738532: Error in Elliptic Curve NamedCurve determination. (related to PKCS11)
vinnie
parents: 5506
diff changeset
   176
                (b.equals(curve.b))) {
79c47f5e241b 6738532: Error in Elliptic Curve NamedCurve determination. (related to PKCS11)
vinnie
parents: 5506
diff changeset
   177
                    return true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * Returns a hash code value for this elliptic curve.
9556
79c47f5e241b 6738532: Error in Elliptic Curve NamedCurve determination. (related to PKCS11)
vinnie
parents: 5506
diff changeset
   185
     * @return a hash code value computed from the hash codes of the field, A,
79c47f5e241b 6738532: Error in Elliptic Curve NamedCurve determination. (related to PKCS11)
vinnie
parents: 5506
diff changeset
   186
     * and B, as follows:
18156
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 9556
diff changeset
   187
     * <pre>{@code
9556
79c47f5e241b 6738532: Error in Elliptic Curve NamedCurve determination. (related to PKCS11)
vinnie
parents: 5506
diff changeset
   188
     *     (field.hashCode() << 6) + (a.hashCode() << 4) + (b.hashCode() << 2)
18156
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 9556
diff changeset
   189
     * }</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        return (field.hashCode() << 6 +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            (a.hashCode() << 4) +
9556
79c47f5e241b 6738532: Error in Elliptic Curve NamedCurve determination. (related to PKCS11)
vinnie
parents: 5506
diff changeset
   194
            (b.hashCode() << 2));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
}