BCRSAPrivateCrtKey.java revision e1142c149e244797ce73b0e7fad40816e447a817
1package org.bouncycastle.jcajce.provider.asymmetric.rsa;
2
3import java.io.IOException;
4import java.math.BigInteger;
5import java.security.interfaces.RSAPrivateCrtKey;
6import java.security.spec.RSAPrivateCrtKeySpec;
7
8import org.bouncycastle.asn1.DERNull;
9import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
10import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
11import org.bouncycastle.asn1.pkcs.RSAPrivateKey;
12import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
13import org.bouncycastle.crypto.params.RSAPrivateCrtKeyParameters;
14import org.bouncycastle.jcajce.provider.asymmetric.util.KeyUtil;
15
16/**
17 * A provider representation for a RSA private key, with CRT factors included.
18 */
19public class BCRSAPrivateCrtKey
20    extends BCRSAPrivateKey
21    implements RSAPrivateCrtKey
22{
23    static final long serialVersionUID = 7834723820638524718L;
24
25    private BigInteger  publicExponent;
26    private BigInteger  primeP;
27    private BigInteger  primeQ;
28    private BigInteger  primeExponentP;
29    private BigInteger  primeExponentQ;
30    private BigInteger  crtCoefficient;
31
32    /**
33     * construct a private key from it's org.bouncycastle.crypto equivalent.
34     *
35     * @param key the parameters object representing the private key.
36     */
37    BCRSAPrivateCrtKey(
38        RSAPrivateCrtKeyParameters key)
39    {
40        super(key);
41
42        this.publicExponent = key.getPublicExponent();
43        this.primeP = key.getP();
44        this.primeQ = key.getQ();
45        this.primeExponentP = key.getDP();
46        this.primeExponentQ = key.getDQ();
47        this.crtCoefficient = key.getQInv();
48    }
49
50    /**
51     * construct a private key from an RSAPrivateCrtKeySpec
52     *
53     * @param spec the spec to be used in construction.
54     */
55    BCRSAPrivateCrtKey(
56        RSAPrivateCrtKeySpec spec)
57    {
58        this.modulus = spec.getModulus();
59        this.publicExponent = spec.getPublicExponent();
60        this.privateExponent = spec.getPrivateExponent();
61        this.primeP = spec.getPrimeP();
62        this.primeQ = spec.getPrimeQ();
63        this.primeExponentP = spec.getPrimeExponentP();
64        this.primeExponentQ = spec.getPrimeExponentQ();
65        this.crtCoefficient = spec.getCrtCoefficient();
66    }
67
68    /**
69     * construct a private key from another RSAPrivateCrtKey.
70     *
71     * @param key the object implementing the RSAPrivateCrtKey interface.
72     */
73    BCRSAPrivateCrtKey(
74        RSAPrivateCrtKey key)
75    {
76        this.modulus = key.getModulus();
77        this.publicExponent = key.getPublicExponent();
78        this.privateExponent = key.getPrivateExponent();
79        this.primeP = key.getPrimeP();
80        this.primeQ = key.getPrimeQ();
81        this.primeExponentP = key.getPrimeExponentP();
82        this.primeExponentQ = key.getPrimeExponentQ();
83        this.crtCoefficient = key.getCrtCoefficient();
84    }
85
86    /**
87     * construct an RSA key from a private key info object.
88     */
89    BCRSAPrivateCrtKey(
90        PrivateKeyInfo info)
91        throws IOException
92    {
93        this(RSAPrivateKey.getInstance(info.parsePrivateKey()));
94    }
95
96    /**
97     * construct an RSA key from a ASN.1 RSA private key object.
98     */
99    BCRSAPrivateCrtKey(
100        RSAPrivateKey key)
101    {
102        this.modulus = key.getModulus();
103        this.publicExponent = key.getPublicExponent();
104        this.privateExponent = key.getPrivateExponent();
105        this.primeP = key.getPrime1();
106        this.primeQ = key.getPrime2();
107        this.primeExponentP = key.getExponent1();
108        this.primeExponentQ = key.getExponent2();
109        this.crtCoefficient = key.getCoefficient();
110    }
111
112    /**
113     * return the encoding format we produce in getEncoded().
114     *
115     * @return the encoding format we produce in getEncoded().
116     */
117    public String getFormat()
118    {
119        return "PKCS#8";
120    }
121
122    /**
123     * Return a PKCS8 representation of the key. The sequence returned
124     * represents a full PrivateKeyInfo object.
125     *
126     * @return a PKCS8 representation of the key.
127     */
128    public byte[] getEncoded()
129    {
130        return KeyUtil.getEncodedPrivateKeyInfo(new AlgorithmIdentifier(PKCSObjectIdentifiers.rsaEncryption, DERNull.INSTANCE), new RSAPrivateKey(getModulus(), getPublicExponent(), getPrivateExponent(), getPrimeP(), getPrimeQ(), getPrimeExponentP(), getPrimeExponentQ(), getCrtCoefficient()));
131    }
132
133    /**
134     * return the public exponent.
135     *
136     * @return the public exponent.
137     */
138    public BigInteger getPublicExponent()
139    {
140        return publicExponent;
141    }
142
143    /**
144     * return the prime P.
145     *
146     * @return the prime P.
147     */
148    public BigInteger getPrimeP()
149    {
150        return primeP;
151    }
152
153    /**
154     * return the prime Q.
155     *
156     * @return the prime Q.
157     */
158    public BigInteger getPrimeQ()
159    {
160        return primeQ;
161    }
162
163    /**
164     * return the prime exponent for P.
165     *
166     * @return the prime exponent for P.
167     */
168    public BigInteger getPrimeExponentP()
169    {
170        return primeExponentP;
171    }
172
173    /**
174     * return the prime exponent for Q.
175     *
176     * @return the prime exponent for Q.
177     */
178    public BigInteger getPrimeExponentQ()
179    {
180        return primeExponentQ;
181    }
182
183    /**
184     * return the CRT coefficient.
185     *
186     * @return the CRT coefficient.
187     */
188    public BigInteger getCrtCoefficient()
189    {
190        return crtCoefficient;
191    }
192
193    public int hashCode()
194    {
195        return this.getModulus().hashCode()
196               ^ this.getPublicExponent().hashCode()
197               ^ this.getPrivateExponent().hashCode();
198    }
199
200    public boolean equals(Object o)
201    {
202        if (o == this)
203        {
204            return true;
205        }
206
207        if (!(o instanceof RSAPrivateCrtKey))
208        {
209            return false;
210        }
211
212        RSAPrivateCrtKey key = (RSAPrivateCrtKey)o;
213
214        return this.getModulus().equals(key.getModulus())
215         && this.getPublicExponent().equals(key.getPublicExponent())
216         && this.getPrivateExponent().equals(key.getPrivateExponent())
217         && this.getPrimeP().equals(key.getPrimeP())
218         && this.getPrimeQ().equals(key.getPrimeQ())
219         && this.getPrimeExponentP().equals(key.getPrimeExponentP())
220         && this.getPrimeExponentQ().equals(key.getPrimeExponentQ())
221         && this.getCrtCoefficient().equals(key.getCrtCoefficient());
222    }
223
224    public String toString()
225    {
226        StringBuffer    buf = new StringBuffer();
227        String          nl = System.getProperty("line.separator");
228
229        buf.append("RSA Private CRT Key").append(nl);
230        buf.append("            modulus: ").append(this.getModulus().toString(16)).append(nl);
231        buf.append("    public exponent: ").append(this.getPublicExponent().toString(16)).append(nl);
232        buf.append("   private exponent: ").append(this.getPrivateExponent().toString(16)).append(nl);
233        buf.append("             primeP: ").append(this.getPrimeP().toString(16)).append(nl);
234        buf.append("             primeQ: ").append(this.getPrimeQ().toString(16)).append(nl);
235        buf.append("     primeExponentP: ").append(this.getPrimeExponentP().toString(16)).append(nl);
236        buf.append("     primeExponentQ: ").append(this.getPrimeExponentQ().toString(16)).append(nl);
237        buf.append("     crtCoefficient: ").append(this.getCrtCoefficient().toString(16)).append(nl);
238
239        return buf.toString();
240    }
241}
242