1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This code is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License version 2 only, as
8  * published by the Free Software Foundation.  Oracle designates this
9  * particular file as subject to the "Classpath" exception as provided
10  * by Oracle in the LICENSE file that accompanied this code.
11  *
12  * This code is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15  * version 2 for more details (a copy is included in the LICENSE file that
16  * accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License version
19  * 2 along with this work; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21  *
22  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
23  * or visit www.oracle.com if you need additional information or have any
24  * questions.
25  */
26 package java.security.spec;
27 
28 import java.math.BigInteger;
29 
30 /**
31  * This immutable class specifies the set of domain parameters
32  * used with elliptic curve cryptography (ECC).
33  *
34  * @see AlgorithmParameterSpec
35  *
36  * @author Valerie Peng
37  *
38  * @since 1.5
39  */
40 public class ECParameterSpec implements AlgorithmParameterSpec {
41 
42     private final EllipticCurve curve;
43     private final ECPoint g;
44     private final BigInteger n;
45     private final int h;
46 
47     /**
48      * Creates elliptic curve domain parameters based on the
49      * specified values.
50      * @param curve the elliptic curve which this parameter
51      * defines.
52      * @param g the generator which is also known as the base point.
53      * @param n the order of the generator {@code g}.
54      * @param h the cofactor.
55      * @exception NullPointerException if {@code curve},
56      * {@code g}, or {@code n} is null.
57      * @exception IllegalArgumentException if {@code n}
58      * or {@code h} is not positive.
59      */
ECParameterSpec(EllipticCurve curve, ECPoint g, BigInteger n, int h)60     public ECParameterSpec(EllipticCurve curve, ECPoint g,
61                            BigInteger n, int h) {
62         if (curve == null) {
63             throw new NullPointerException("curve is null");
64         }
65         if (g == null) {
66             throw new NullPointerException("g is null");
67         }
68         if (n == null) {
69             throw new NullPointerException("n is null");
70         }
71         if (n.signum() != 1) {
72             throw new IllegalArgumentException("n is not positive");
73         }
74         if (h <= 0) {
75             throw new IllegalArgumentException("h is not positive");
76         }
77         this.curve = curve;
78         this.g = g;
79         this.n = n;
80         this.h = h;
81     }
82 
83     /**
84      * Returns the elliptic curve that this parameter defines.
85      * @return the elliptic curve that this parameter defines.
86      */
getCurve()87     public EllipticCurve getCurve() {
88         return curve;
89     }
90 
91     /**
92      * Returns the generator which is also known as the base point.
93      * @return the generator which is also known as the base point.
94      */
getGenerator()95     public ECPoint getGenerator() {
96         return g;
97     }
98 
99     /**
100      * Returns the order of the generator.
101      * @return the order of the generator.
102      */
getOrder()103     public BigInteger getOrder() {
104         return n;
105     }
106 
107     /**
108      * Returns the cofactor.
109      * @return the cofactor.
110      */
getCofactor()111     public int getCofactor() {
112         return h;
113     }
114     // BEGIN Android-changed
115     private String curveName;
116 
117     /**
118      * Used to set the curve name if available.
119      *
120      * @hide
121      */
setCurveName(String curveName)122     public void setCurveName(String curveName) {
123         this.curveName = curveName;
124     }
125 
126     /**
127      * Returns the name of the curve if this is a named curve. Returns
128      * {@code null} if this is not known to be a named curve.
129      *
130      * @hide
131      */
getCurveName()132     public String getCurveName() {
133         return curveName;
134     }
135     // END Android-changed
136 }
137