author | valeriep |
Wed, 07 Nov 2018 01:04:26 +0000 | |
changeset 52430 | 4ee78b5583f9 |
parent 50204 | 3195a713e24d |
permissions | -rw-r--r-- |
2 | 1 |
/* |
50204
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47421
diff
changeset
|
2 |
* Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
||
26 |
package sun.security.rsa; |
|
27 |
||
28 |
import java.math.BigInteger; |
|
29 |
||
30 |
import java.security.*; |
|
31 |
import java.security.spec.AlgorithmParameterSpec; |
|
32 |
import java.security.spec.RSAKeyGenParameterSpec; |
|
33 |
||
34 |
import sun.security.jca.JCAUtil; |
|
47421
f9e03aef3a49
8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents:
47216
diff
changeset
|
35 |
import static sun.security.util.SecurityProviderConstants.DEF_RSA_KEY_SIZE; |
50204
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47421
diff
changeset
|
36 |
import static sun.security.util.SecurityProviderConstants.DEF_RSASSA_PSS_KEY_SIZE; |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47421
diff
changeset
|
37 |
import sun.security.x509.AlgorithmId; |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47421
diff
changeset
|
38 |
import static sun.security.rsa.RSAUtil.KeyType; |
2 | 39 |
|
40 |
/** |
|
41 |
* RSA keypair generation. Standard algorithm, minimum key length 512 bit. |
|
42 |
* We generate two random primes until we find two where phi is relative |
|
43 |
* prime to the public exponent. Default exponent is 65537. It has only bit 0 |
|
44 |
* and bit 4 set, which makes it particularly efficient. |
|
45 |
* |
|
46 |
* @since 1.5 |
|
47 |
* @author Andreas Sterbenz |
|
48 |
*/ |
|
50204
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47421
diff
changeset
|
49 |
public abstract class RSAKeyPairGenerator extends KeyPairGeneratorSpi { |
2 | 50 |
|
51 |
// public exponent to use |
|
52 |
private BigInteger publicExponent; |
|
53 |
||
2596 | 54 |
// size of the key to generate, >= RSAKeyFactory.MIN_MODLEN |
2 | 55 |
private int keySize; |
56 |
||
50204
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47421
diff
changeset
|
57 |
private final KeyType type; |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47421
diff
changeset
|
58 |
private AlgorithmId rsaId; |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47421
diff
changeset
|
59 |
|
2 | 60 |
// PRNG to use |
61 |
private SecureRandom random; |
|
62 |
||
50204
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47421
diff
changeset
|
63 |
RSAKeyPairGenerator(KeyType type, int defKeySize) { |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47421
diff
changeset
|
64 |
this.type = type; |
2 | 65 |
// initialize to default in case the app does not call initialize() |
50204
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47421
diff
changeset
|
66 |
initialize(defKeySize, null); |
2 | 67 |
} |
68 |
||
69 |
// initialize the generator. See JCA doc |
|
70 |
public void initialize(int keySize, SecureRandom random) { |
|
2596 | 71 |
try { |
50204
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47421
diff
changeset
|
72 |
initialize(new RSAKeyGenParameterSpec(keySize, |
52430
4ee78b5583f9
8211049: Second parameter of "initialize" method is not used
valeriep
parents:
50204
diff
changeset
|
73 |
RSAKeyGenParameterSpec.F4), random); |
50204
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47421
diff
changeset
|
74 |
} catch (InvalidAlgorithmParameterException iape) { |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47421
diff
changeset
|
75 |
throw new InvalidParameterException(iape.getMessage()); |
2 | 76 |
} |
77 |
} |
|
78 |
||
79 |
// second initialize method. See JCA doc. |
|
80 |
public void initialize(AlgorithmParameterSpec params, SecureRandom random) |
|
81 |
throws InvalidAlgorithmParameterException { |
|
82 |
if (params instanceof RSAKeyGenParameterSpec == false) { |
|
83 |
throw new InvalidAlgorithmParameterException |
|
84 |
("Params must be instance of RSAKeyGenParameterSpec"); |
|
85 |
} |
|
2596 | 86 |
|
2 | 87 |
RSAKeyGenParameterSpec rsaSpec = (RSAKeyGenParameterSpec)params; |
2596 | 88 |
int tmpKeySize = rsaSpec.getKeysize(); |
89 |
BigInteger tmpPublicExponent = rsaSpec.getPublicExponent(); |
|
50204
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47421
diff
changeset
|
90 |
AlgorithmParameterSpec tmpParams = rsaSpec.getKeyParams(); |
2596 | 91 |
|
92 |
if (tmpPublicExponent == null) { |
|
93 |
tmpPublicExponent = RSAKeyGenParameterSpec.F4; |
|
2 | 94 |
} else { |
2596 | 95 |
if (tmpPublicExponent.compareTo(RSAKeyGenParameterSpec.F0) < 0) { |
2 | 96 |
throw new InvalidAlgorithmParameterException |
97 |
("Public exponent must be 3 or larger"); |
|
98 |
} |
|
2596 | 99 |
if (tmpPublicExponent.bitLength() > tmpKeySize) { |
2 | 100 |
throw new InvalidAlgorithmParameterException |
101 |
("Public exponent must be smaller than key size"); |
|
102 |
} |
|
103 |
} |
|
2596 | 104 |
|
105 |
// do not allow unreasonably large key sizes, probably user error |
|
106 |
try { |
|
107 |
RSAKeyFactory.checkKeyLengths(tmpKeySize, tmpPublicExponent, |
|
108 |
512, 64 * 1024); |
|
109 |
} catch (InvalidKeyException e) { |
|
110 |
throw new InvalidAlgorithmParameterException( |
|
111 |
"Invalid key sizes", e); |
|
112 |
} |
|
113 |
||
50204
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47421
diff
changeset
|
114 |
try { |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47421
diff
changeset
|
115 |
this.rsaId = RSAUtil.createAlgorithmId(type, tmpParams); |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47421
diff
changeset
|
116 |
} catch (ProviderException e) { |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47421
diff
changeset
|
117 |
throw new InvalidAlgorithmParameterException( |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47421
diff
changeset
|
118 |
"Invalid key parameters", e); |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47421
diff
changeset
|
119 |
} |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47421
diff
changeset
|
120 |
|
2596 | 121 |
this.keySize = tmpKeySize; |
122 |
this.publicExponent = tmpPublicExponent; |
|
123 |
this.random = random; |
|
2 | 124 |
} |
125 |
||
126 |
// generate the keypair. See JCA doc |
|
127 |
public KeyPair generateKeyPair() { |
|
21278 | 128 |
// accommodate odd key sizes in case anybody wants to use them |
2 | 129 |
int lp = (keySize + 1) >> 1; |
130 |
int lq = keySize - lp; |
|
131 |
if (random == null) { |
|
132 |
random = JCAUtil.getSecureRandom(); |
|
133 |
} |
|
134 |
BigInteger e = publicExponent; |
|
135 |
while (true) { |
|
136 |
// generate two random primes of size lp/lq |
|
137 |
BigInteger p = BigInteger.probablePrime(lp, random); |
|
138 |
BigInteger q, n; |
|
139 |
do { |
|
140 |
q = BigInteger.probablePrime(lq, random); |
|
141 |
// convention is for p > q |
|
142 |
if (p.compareTo(q) < 0) { |
|
143 |
BigInteger tmp = p; |
|
144 |
p = q; |
|
145 |
q = tmp; |
|
146 |
} |
|
147 |
// modulus n = p * q |
|
148 |
n = p.multiply(q); |
|
149 |
// even with correctly sized p and q, there is a chance that |
|
150 |
// n will be one bit short. re-generate the smaller prime if so |
|
151 |
} while (n.bitLength() < keySize); |
|
152 |
||
153 |
// phi = (p - 1) * (q - 1) must be relative prime to e |
|
154 |
// otherwise RSA just won't work ;-) |
|
155 |
BigInteger p1 = p.subtract(BigInteger.ONE); |
|
156 |
BigInteger q1 = q.subtract(BigInteger.ONE); |
|
157 |
BigInteger phi = p1.multiply(q1); |
|
158 |
// generate new p and q until they work. typically |
|
159 |
// the first try will succeed when using F4 |
|
160 |
if (e.gcd(phi).equals(BigInteger.ONE) == false) { |
|
161 |
continue; |
|
162 |
} |
|
163 |
||
164 |
// private exponent d is the inverse of e mod phi |
|
165 |
BigInteger d = e.modInverse(phi); |
|
166 |
||
167 |
// 1st prime exponent pe = d mod (p - 1) |
|
168 |
BigInteger pe = d.mod(p1); |
|
169 |
// 2nd prime exponent qe = d mod (q - 1) |
|
170 |
BigInteger qe = d.mod(q1); |
|
171 |
||
172 |
// crt coefficient coeff is the inverse of q mod p |
|
173 |
BigInteger coeff = q.modInverse(p); |
|
174 |
||
175 |
try { |
|
50204
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47421
diff
changeset
|
176 |
PublicKey publicKey = new RSAPublicKeyImpl(rsaId, n, e); |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47421
diff
changeset
|
177 |
PrivateKey privateKey = new RSAPrivateCrtKeyImpl( |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47421
diff
changeset
|
178 |
rsaId, n, e, d, p, q, pe, qe, coeff); |
2 | 179 |
return new KeyPair(publicKey, privateKey); |
180 |
} catch (InvalidKeyException exc) { |
|
181 |
// invalid key exception only thrown for keys < 512 bit, |
|
182 |
// will not happen here |
|
183 |
throw new RuntimeException(exc); |
|
184 |
} |
|
185 |
} |
|
186 |
} |
|
187 |
||
50204
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47421
diff
changeset
|
188 |
public static final class Legacy extends RSAKeyPairGenerator { |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47421
diff
changeset
|
189 |
public Legacy() { |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47421
diff
changeset
|
190 |
super(KeyType.RSA, DEF_RSA_KEY_SIZE); |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47421
diff
changeset
|
191 |
} |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47421
diff
changeset
|
192 |
} |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47421
diff
changeset
|
193 |
|
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47421
diff
changeset
|
194 |
public static final class PSS extends RSAKeyPairGenerator { |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47421
diff
changeset
|
195 |
public PSS() { |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47421
diff
changeset
|
196 |
super(KeyType.PSS, DEF_RSASSA_PSS_KEY_SIZE); |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47421
diff
changeset
|
197 |
} |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47421
diff
changeset
|
198 |
} |
2 | 199 |
} |