1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17
18package java.security.spec;
19
20import java.math.BigInteger;
21
22/**
23 * The parameter specification used with Elliptic Curve Cryptography (ECC).
24 */
25public class ECParameterSpec implements AlgorithmParameterSpec {
26    // Elliptic curve for which this is parameter
27    private final EllipticCurve curve;
28    // Distinguished point on the elliptic curve called generator or base point
29    private final ECPoint generator;
30    // Order of the generator
31    private final BigInteger order;
32    // Cofactor
33    private final int cofactor;
34    // Name of curve if available.
35    private String curveName;
36
37    /**
38     * Creates a new {@code ECParameterSpec} with the specified elliptic curve,
39     * the base point, the order of the generator (or base point) and the
40     * co-factor.
41     *
42     * @param curve
43     *            the elliptic curve.
44     * @param generator
45     *            the generator (or base point).
46     * @param order
47     *            the order of the generator.
48     * @param cofactor
49     *            the co-factor.
50     * @throws IllegalArgumentException
51     *             if {@code order <= zero} or {@code cofactor <= zero}.
52     */
53    public ECParameterSpec(EllipticCurve curve, ECPoint generator,
54            BigInteger order, int cofactor) {
55        this.curve = curve;
56        this.generator = generator;
57        this.order = order;
58        this.cofactor = cofactor;
59        // throw NullPointerException if curve, generator or order is null
60        if (this.curve == null) {
61            throw new NullPointerException("curve == null");
62        }
63        if (this.generator == null) {
64            throw new NullPointerException("generator == null");
65        }
66        if (this.order == null) {
67            throw new NullPointerException("order == null");
68        }
69        // throw IllegalArgumentException if order or cofactor is not positive
70        if (!(this.order.compareTo(BigInteger.ZERO) > 0)) {
71            throw new IllegalArgumentException("order <= 0");
72        }
73        if (!(this.cofactor > 0)) {
74            throw new IllegalArgumentException("cofactor <= 0");
75        }
76    }
77
78    /**
79     * Returns the {@code cofactor}.
80     *
81     * @return the {@code cofactor}.
82     */
83    public int getCofactor() {
84        return cofactor;
85    }
86
87    /**
88     * Returns the elliptic curve.
89     *
90     * @return the elliptic curve.
91     */
92    public EllipticCurve getCurve() {
93        return curve;
94    }
95
96    /**
97     * Returns the generator (or base point).
98     *
99     * @return the generator (or base point).
100     */
101    public ECPoint getGenerator() {
102        return generator;
103    }
104
105    /**
106     * Returns the order of the generator.
107     *
108     * @return the order of the generator.
109     */
110    public BigInteger getOrder() {
111        return order;
112    }
113
114    /**
115     * Used to set the curve name if available.
116     *
117     * @hide
118     */
119    public void setCurveName(String curveName) {
120        this.curveName = curveName;
121    }
122
123    /**
124     * Returns the name of the curve if this is a named curve. Returns
125     * {@code null} if this is not known to be a named curve.
126     *
127     * @hide
128     */
129    public String getCurveName() {
130        return curveName;
131    }
132}
133