summaryrefslogtreecommitdiffstats
path: root/luni/src/main/java/java/security/spec/ECParameterSpec.java
blob: 9860ac0e0e6144cb6eaf2f15cb71bfd0072cb497 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package java.security.spec;

import java.math.BigInteger;

/**
 * The parameter specification used with Elliptic Curve Cryptography (ECC).
 */
public class ECParameterSpec implements AlgorithmParameterSpec {
    // Elliptic curve for which this is parameter
    private final EllipticCurve curve;
    // Distinguished point on the elliptic curve called generator or base point
    private final ECPoint generator;
    // Order of the generator
    private final BigInteger order;
    // Cofactor
    private final int cofactor;
    // Name of curve if available.
    private final String curveName;

    /**
     * Creates a new {@code ECParameterSpec} with the specified elliptic curve,
     * the base point, the order of the generator (or base point) and the
     * co-factor.
     *
     * @param curve
     *            the elliptic curve.
     * @param generator
     *            the generator (or base point).
     * @param order
     *            the order of the generator.
     * @param cofactor
     *            the co-factor.
     * @throws IllegalArgumentException
     *             if {@code order <= zero} or {@code cofactor <= zero}.
     */
    public ECParameterSpec(EllipticCurve curve, ECPoint generator,
            BigInteger order, int cofactor) {
        this(curve, generator, order, cofactor, null);
    }

    /**
     * Creates a new {@code ECParameterSpec} with the specified named curve
     * and all of its parameters.
     *
     * @see #ECParameterSpec(EllipticCurve, ECPoint, BigInteger, int)
     * @hide
     */
    public ECParameterSpec(EllipticCurve curve, ECPoint generator,
            BigInteger order, int cofactor, String curveName) {
        this.curve = curve;
        this.generator = generator;
        this.order = order;
        this.cofactor = cofactor;
        this.curveName = curveName;
        // throw NullPointerException if curve, generator or order is null
        if (this.curve == null) {
            throw new NullPointerException("curve == null");
        }
        if (this.generator == null) {
            throw new NullPointerException("generator == null");
        }
        if (this.order == null) {
            throw new NullPointerException("order == null");
        }
        // throw IllegalArgumentException if order or cofactor is not positive
        if (!(this.order.compareTo(BigInteger.ZERO) > 0)) {
            throw new IllegalArgumentException("order <= 0");
        }
        if (!(this.cofactor > 0)) {
            throw new IllegalArgumentException("cofactor <= 0");
        }
    }

    /**
     * Returns the {@code cofactor}.
     *
     * @return the {@code cofactor}.
     */
    public int getCofactor() {
        return cofactor;
    }

    /**
     * Returns the elliptic curve.
     *
     * @return the elliptic curve.
     */
    public EllipticCurve getCurve() {
        return curve;
    }

    /**
     * Returns the generator (or base point).
     *
     * @return the generator (or base point).
     */
    public ECPoint getGenerator() {
        return generator;
    }

    /**
     * Returns the order of the generator.
     *
     * @return the order of the generator.
     */
    public BigInteger getOrder() {
        return order;
    }

    /**
     * Returns the name of the curve if this is a named curve. Returns
     * {@code null} if this is not known to be a named curve.
     *
     * @hide
     */
    public String getCurveName() {
        return curveName;
    }
}