1 /*
2  * Copyright (c) 1998, 2020, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 package java.security.spec;
27 
28 import java.math.BigInteger;
29 
30 /**
31  * This class specifies an RSA private key, as defined in the
32  * <a href="https://tools.ietf.org/rfc/rfc8017.txt">PKCS#1 v2.2</a> standard,
33  * using the Chinese Remainder Theorem (CRT) information values for efficiency.
34  *
35  * @author Jan Luehe
36  * @since 1.2
37  *
38  *
39  * @see java.security.Key
40  * @see java.security.KeyFactory
41  * @see KeySpec
42  * @see PKCS8EncodedKeySpec
43  * @see RSAPrivateKeySpec
44  * @see RSAPublicKeySpec
45  */
46 
47 public class RSAPrivateCrtKeySpec extends RSAPrivateKeySpec {
48 
49     private final BigInteger publicExponent;
50     private final BigInteger primeP;
51     private final BigInteger primeQ;
52     private final BigInteger primeExponentP;
53     private final BigInteger primeExponentQ;
54     private final BigInteger crtCoefficient;
55 
56    /**
57     * Creates a new {@code RSAPrivateCrtKeySpec}.
58     *
59     * @param modulus the modulus n
60     * @param publicExponent the public exponent e
61     * @param privateExponent the private exponent d
62     * @param primeP the prime factor p of n
63     * @param primeQ the prime factor q of n
64     * @param primeExponentP this is d mod (p-1)
65     * @param primeExponentQ this is d mod (q-1)
66     * @param crtCoefficient the Chinese Remainder Theorem
67     * coefficient q-1 mod p
68     */
RSAPrivateCrtKeySpec(BigInteger modulus, BigInteger publicExponent, BigInteger privateExponent, BigInteger primeP, BigInteger primeQ, BigInteger primeExponentP, BigInteger primeExponentQ, BigInteger crtCoefficient)69     public RSAPrivateCrtKeySpec(BigInteger modulus,
70                                 BigInteger publicExponent,
71                                 BigInteger privateExponent,
72                                 BigInteger primeP,
73                                 BigInteger primeQ,
74                                 BigInteger primeExponentP,
75                                 BigInteger primeExponentQ,
76                                 BigInteger crtCoefficient) {
77         this(modulus, publicExponent, privateExponent, primeP, primeQ,
78              primeExponentP, primeExponentQ, crtCoefficient, null);
79     }
80 
81    /**
82     * Creates a new {@code RSAPrivateCrtKeySpec} with additional
83     * key parameters.
84     *
85     * @param modulus the modulus n
86     * @param publicExponent the public exponent e
87     * @param privateExponent the private exponent d
88     * @param primeP the prime factor p of n
89     * @param primeQ the prime factor q of n
90     * @param primeExponentP this is d mod (p-1)
91     * @param primeExponentQ this is d mod (q-1)
92     * @param crtCoefficient the Chinese Remainder Theorem
93     * coefficient q-1 mod p
94     * @param keyParams the parameters associated with key
95     * @since 11
96     */
RSAPrivateCrtKeySpec(BigInteger modulus, BigInteger publicExponent, BigInteger privateExponent, BigInteger primeP, BigInteger primeQ, BigInteger primeExponentP, BigInteger primeExponentQ, BigInteger crtCoefficient, AlgorithmParameterSpec keyParams)97     public RSAPrivateCrtKeySpec(BigInteger modulus,
98                                 BigInteger publicExponent,
99                                 BigInteger privateExponent,
100                                 BigInteger primeP,
101                                 BigInteger primeQ,
102                                 BigInteger primeExponentP,
103                                 BigInteger primeExponentQ,
104                                 BigInteger crtCoefficient,
105                                 AlgorithmParameterSpec keyParams) {
106         super(modulus, privateExponent, keyParams);
107         this.publicExponent = publicExponent;
108         this.primeP = primeP;
109         this.primeQ = primeQ;
110         this.primeExponentP = primeExponentP;
111         this.primeExponentQ = primeExponentQ;
112         this.crtCoefficient = crtCoefficient;
113     }
114 
115     /**
116      * Returns the public exponent.
117      *
118      * @return the public exponent
119      */
getPublicExponent()120     public BigInteger getPublicExponent() {
121         return this.publicExponent;
122     }
123 
124     /**
125      * Returns the primeP.
126      *
127      * @return the primeP
128      */
getPrimeP()129     public BigInteger getPrimeP() {
130         return this.primeP;
131     }
132 
133     /**
134      * Returns the primeQ.
135      *
136      * @return the primeQ
137      */
getPrimeQ()138     public BigInteger getPrimeQ() {
139         return this.primeQ;
140     }
141 
142     /**
143      * Returns the primeExponentP.
144      *
145      * @return the primeExponentP
146      */
getPrimeExponentP()147     public BigInteger getPrimeExponentP() {
148         return this.primeExponentP;
149     }
150 
151     /**
152      * Returns the primeExponentQ.
153      *
154      * @return the primeExponentQ
155      */
getPrimeExponentQ()156     public BigInteger getPrimeExponentQ() {
157         return this.primeExponentQ;
158     }
159 
160     /**
161      * Returns the crtCoefficient.
162      *
163      * @return the crtCoefficient
164      */
getCrtCoefficient()165     public BigInteger getCrtCoefficient() {
166         return this.crtCoefficient;
167     }
168 }
169