1 /* 2 * Copyright (c) 2003, 2012, 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 sun.security.provider; 27 28 import java.util.*; 29 import java.util.concurrent.ConcurrentHashMap; 30 import java.math.BigInteger; 31 32 import java.security.*; 33 import java.security.SecureRandom; 34 import java.security.spec.*; 35 36 import javax.crypto.spec.DHParameterSpec; 37 38 /** 39 * Cache for DSA and DH parameter specs. Used by the KeyPairGenerators 40 * in the Sun, SunJCE, and SunPKCS11 provider if no parameters have been 41 * explicitly specified by the application. 42 * 43 * @author Andreas Sterbenz 44 * @since 1.5 45 */ 46 public final class ParameterCache { 47 48 private ParameterCache() { 49 // empty 50 } 51 52 // cache of DSA parameters 53 private static final Map<Integer,DSAParameterSpec> dsaCache; 54 55 // cache of DH parameters 56 private static final Map<Integer,DHParameterSpec> dhCache; 57 58 /** 59 * Return cached DSA parameters for the given length combination of 60 * prime and subprime, or null if none are available in the cache. 61 */ 62 public static DSAParameterSpec getCachedDSAParameterSpec(int primeLen, 63 int subprimeLen) { 64 // ensure the sum is unique in all cases, i.e. 65 // case#1: (512 <= p <= 1024) AND q=160 66 // case#2: p=2048 AND q=224 67 // case#3: p=2048 AND q=256 68 // (NOT-YET-SUPPORTED)case#4: p=3072 AND q=256 69 return dsaCache.get(Integer.valueOf(primeLen+subprimeLen)); 70 } 71 72 /** 73 * Return cached DH parameters for the given keylength, or null if none 74 * are available in the cache. 75 */ 76 public static DHParameterSpec getCachedDHParameterSpec(int keyLength) { 77 return dhCache.get(Integer.valueOf(keyLength)); 78 } 79 80 /** 81 * Return DSA parameters for the given primeLen. Uses cache if 82 * possible, generates new parameters and adds them to the cache 83 * otherwise. 84 */ 85 public static DSAParameterSpec getDSAParameterSpec(int primeLen, 86 SecureRandom random) 87 throws NoSuchAlgorithmException, InvalidParameterSpecException, 88 InvalidAlgorithmParameterException { 89 if (primeLen <= 1024) { 90 return getDSAParameterSpec(primeLen, 160, random); 91 } else if (primeLen == 2048) { 92 return getDSAParameterSpec(primeLen, 224, random); 93 } else { 94 return null; 95 } 96 } 97 98 /** 99 * Return DSA parameters for the given primeLen and subprimeLen. 100 * Uses cache if possible, generates new parameters and adds them to the 101 * cache otherwise. 102 */ 103 public static DSAParameterSpec getDSAParameterSpec(int primeLen, 104 int subprimeLen, SecureRandom random) 105 throws NoSuchAlgorithmException, InvalidParameterSpecException, 106 InvalidAlgorithmParameterException { 107 DSAParameterSpec spec = 108 getCachedDSAParameterSpec(primeLen, subprimeLen); 109 if (spec != null) { 110 return spec; 111 } 112 spec = getNewDSAParameterSpec(primeLen, subprimeLen, random); 113 dsaCache.put(Integer.valueOf(primeLen + subprimeLen), spec); 114 return spec; 115 } 116 117 /** 118 * Return DH parameters for the given keylength. Uses cache if possible, 119 * generates new parameters and adds them to the cache otherwise. 120 */ 121 public static DHParameterSpec getDHParameterSpec(int keyLength, 122 SecureRandom random) 123 throws NoSuchAlgorithmException, InvalidParameterSpecException { 124 DHParameterSpec spec = getCachedDHParameterSpec(keyLength); 125 if (spec != null) { 126 return spec; 127 } 128 AlgorithmParameterGenerator gen = 129 AlgorithmParameterGenerator.getInstance("DH"); 130 gen.init(keyLength, random); 131 AlgorithmParameters params = gen.generateParameters(); 132 spec = params.getParameterSpec(DHParameterSpec.class); 133 dhCache.put(Integer.valueOf(keyLength), spec); 134 return spec; 135 } 136 137 /** 138 * Return new DSA parameters for the given length combination of prime and 139 * sub prime. Do not lookup in cache and do not cache the newly generated 140 * parameters. This method really only exists for the legacy method 141 * DSAKeyPairGenerator.initialize(int, boolean, SecureRandom). 142 */ 143 public static DSAParameterSpec getNewDSAParameterSpec(int primeLen, 144 int subprimeLen, SecureRandom random) 145 throws NoSuchAlgorithmException, InvalidParameterSpecException, 146 InvalidAlgorithmParameterException { 147 AlgorithmParameterGenerator gen = 148 AlgorithmParameterGenerator.getInstance("DSA"); 149 // Use init(int size, SecureRandom random) for legacy DSA key sizes 150 if (primeLen < 1024) { 151 gen.init(primeLen, random); 152 } else { 153 DSAGenParameterSpec genParams = 154 new DSAGenParameterSpec(primeLen, subprimeLen); 155 gen.init(genParams, random); 156 } 157 AlgorithmParameters params = gen.generateParameters(); 158 DSAParameterSpec spec = params.getParameterSpec(DSAParameterSpec.class); 159 return spec; 160 } 161 162 static { 163 dhCache = new ConcurrentHashMap<Integer,DHParameterSpec>(); 164 dsaCache = new ConcurrentHashMap<Integer,DSAParameterSpec>(); 165 166 /* 167 * We support precomputed parameter for legacy 512, 768 bit moduli, 168 * and (L, N) combinations of (1024, 160), (2048, 224), (2048, 256). 169 * In this file we provide both the seed and counter 170 * value of the generation process for each of these seeds, 171 * for validation purposes. We also include the test vectors 172 * from the DSA specification, FIPS 186, and the FIPS 186 173 * Change No 1, which updates the test vector using SHA-1 174 * instead of SHA (for both the G function and the message 175 * hash. 176 */ 177 178 /* 179 * L = 512 180 * SEED = b869c82b35d70e1b1ff91b28e37a62ecdc34409b 181 * counter = 123 182 */ 183 BigInteger p512 = 184 new BigInteger("fca682ce8e12caba26efccf7110e526db078b05edecb" + 185 "cd1eb4a208f3ae1617ae01f35b91a47e6df63413c5e1" + 186 "2ed0899bcd132acd50d99151bdc43ee737592e17", 16); 187 188 BigInteger q512 = 189 new BigInteger("962eddcc369cba8ebb260ee6b6a126d9346e38c5", 16); 190 191 BigInteger g512 = 192 new BigInteger("678471b27a9cf44ee91a49c5147db1a9aaf244f05a43" + 193 "4d6486931d2d14271b9e35030b71fd73da179069b32e" + 194 "2935630e1c2062354d0da20a6c416e50be794ca4", 16); 195 196 /* 197 * L = 768 198 * SEED = 77d0f8c4dad15eb8c4f2f8d6726cefd96d5bb399 199 * counter = 263 200 */ 201 BigInteger p768 = 202 new BigInteger("e9e642599d355f37c97ffd3567120b8e25c9cd43e" + 203 "927b3a9670fbec5d890141922d2c3b3ad24800937" + 204 "99869d1e846aab49fab0ad26d2ce6a22219d470bc" + 205 "e7d777d4a21fbe9c270b57f607002f3cef8393694" + 206 "cf45ee3688c11a8c56ab127a3daf", 16); 207 208 BigInteger q768 = 209 new BigInteger("9cdbd84c9f1ac2f38d0f80f42ab952e7338bf511", 210 16); 211 212 BigInteger g768 = 213 new BigInteger("30470ad5a005fb14ce2d9dcd87e38bc7d1b1c5fac" + 214 "baecbe95f190aa7a31d23c4dbbcbe06174544401a" + 215 "5b2c020965d8c2bd2171d3668445771f74ba084d2" + 216 "029d83c1c158547f3a9f1a2715be23d51ae4d3e5a" + 217 "1f6a7064f316933a346d3f529252", 16); 218 219 220 /* 221 * L = 1024 222 * SEED = 8d5155894229d5e689ee01e6018a237e2cae64cd 223 * counter = 92 224 */ 225 BigInteger p1024 = 226 new BigInteger("fd7f53811d75122952df4a9c2eece4e7f611b7523c" + 227 "ef4400c31e3f80b6512669455d402251fb593d8d58" + 228 "fabfc5f5ba30f6cb9b556cd7813b801d346ff26660" + 229 "b76b9950a5a49f9fe8047b1022c24fbba9d7feb7c6" + 230 "1bf83b57e7c6a8a6150f04fb83f6d3c51ec3023554" + 231 "135a169132f675f3ae2b61d72aeff22203199dd148" + 232 "01c7", 16); 233 234 BigInteger q1024 = 235 new BigInteger("9760508f15230bccb292b982a2eb840bf0581cf5", 236 16); 237 238 BigInteger g1024 = 239 new BigInteger("f7e1a085d69b3ddecbbcab5c36b857b97994afbbfa" + 240 "3aea82f9574c0b3d0782675159578ebad4594fe671" + 241 "07108180b449167123e84c281613b7cf09328cc8a6" + 242 "e13c167a8b547c8d28e0a3ae1e2bb3a675916ea37f" + 243 "0bfa213562f1fb627a01243bcca4f1bea8519089a8" + 244 "83dfe15ae59f06928b665e807b552564014c3bfecf" + 245 "492a", 16); 246 247 dsaCache.put(Integer.valueOf(512+160), 248 new DSAParameterSpec(p512, q512, g512)); 249 dsaCache.put(Integer.valueOf(768+160), 250 new DSAParameterSpec(p768, q768, g768)); 251 dsaCache.put(Integer.valueOf(1024+160), 252 new DSAParameterSpec(p1024, q1024, g1024)); 253 /* 254 * L = 2048, N = 224 255 * SEED = 584236080cfa43c09b02354135f4cc5198a19efada08bd866d601ba4 256 * counter = 2666 257 */ 258 BigInteger p2048_224 = 259 new BigInteger("8f7935d9b9aae9bfabed887acf4951b6f32ec59e3b" + 260 "af3718e8eac4961f3efd3606e74351a9c4183339b8" + 261 "09e7c2ae1c539ba7475b85d011adb8b47987754984" + 262 "695cac0e8f14b3360828a22ffa27110a3d62a99345" + 263 "3409a0fe696c4658f84bdd20819c3709a01057b195" + 264 "adcd00233dba5484b6291f9d648ef883448677979c" + 265 "ec04b434a6ac2e75e9985de23db0292fc1118c9ffa" + 266 "9d8181e7338db792b730d7b9e349592f6809987215" + 267 "3915ea3d6b8b4653c633458f803b32a4c2e0f27290" + 268 "256e4e3f8a3b0838a1c450e4e18c1a29a37ddf5ea1" + 269 "43de4b66ff04903ed5cf1623e158d487c608e97f21" + 270 "1cd81dca23cb6e380765f822e342be484c05763939" + 271 "601cd667", 16); 272 273 BigInteger q2048_224 = 274 new BigInteger("baf696a68578f7dfdee7fa67c977c785ef32b233ba" + 275 "e580c0bcd5695d", 16); 276 277 BigInteger g2048_224 = 278 new BigInteger("16a65c58204850704e7502a39757040d34da3a3478" + 279 "c154d4e4a5c02d242ee04f96e61e4bd0904abdac8f" + 280 "37eeb1e09f3182d23c9043cb642f88004160edf9ca" + 281 "09b32076a79c32a627f2473e91879ba2c4e744bd20" + 282 "81544cb55b802c368d1fa83ed489e94e0fa0688e32" + 283 "428a5c78c478c68d0527b71c9a3abb0b0be12c4468" + 284 "9639e7d3ce74db101a65aa2b87f64c6826db3ec72f" + 285 "4b5599834bb4edb02f7c90e9a496d3a55d535bebfc" + 286 "45d4f619f63f3dedbb873925c2f224e07731296da8" + 287 "87ec1e4748f87efb5fdeb75484316b2232dee553dd" + 288 "af02112b0d1f02da30973224fe27aeda8b9d4b2922" + 289 "d9ba8be39ed9e103a63c52810bc688b7e2ed4316e1" + 290 "ef17dbde", 16); 291 dsaCache.put(Integer.valueOf(2048+224), 292 new DSAParameterSpec(p2048_224, q2048_224, g2048_224)); 293 294 /* 295 * L = 2048, N = 256 296 * SEED = b0b4417601b59cbc9d8ac8f935cadaec4f5fbb2f23785609ae466748d9b5a536 297 * counter = 497 298 */ 299 BigInteger p2048_256 = 300 new BigInteger("95475cf5d93e596c3fcd1d902add02f427f5f3c721" + 301 "0313bb45fb4d5bb2e5fe1cbd678cd4bbdd84c9836b" + 302 "e1f31c0777725aeb6c2fc38b85f48076fa76bcd814" + 303 "6cc89a6fb2f706dd719898c2083dc8d896f84062e2" + 304 "c9c94d137b054a8d8096adb8d51952398eeca852a0" + 305 "af12df83e475aa65d4ec0c38a9560d5661186ff98b" + 306 "9fc9eb60eee8b030376b236bc73be3acdbd74fd61c" + 307 "1d2475fa3077b8f080467881ff7e1ca56fee066d79" + 308 "506ade51edbb5443a563927dbc4ba520086746175c" + 309 "8885925ebc64c6147906773496990cb714ec667304" + 310 "e261faee33b3cbdf008e0c3fa90650d97d3909c927" + 311 "5bf4ac86ffcb3d03e6dfc8ada5934242dd6d3bcca2" + 312 "a406cb0b", 16); 313 314 BigInteger q2048_256 = 315 new BigInteger("f8183668ba5fc5bb06b5981e6d8b795d30b8978d43" + 316 "ca0ec572e37e09939a9773", 16); 317 318 BigInteger g2048_256 = 319 new BigInteger("42debb9da5b3d88cc956e08787ec3f3a09bba5f48b" + 320 "889a74aaf53174aa0fbe7e3c5b8fcd7a53bef563b0" + 321 "e98560328960a9517f4014d3325fc7962bf1e04937" + 322 "0d76d1314a76137e792f3f0db859d095e4a5b93202" + 323 "4f079ecf2ef09c797452b0770e1350782ed57ddf79" + 324 "4979dcef23cb96f183061965c4ebc93c9c71c56b92" + 325 "5955a75f94cccf1449ac43d586d0beee43251b0b22" + 326 "87349d68de0d144403f13e802f4146d882e057af19" + 327 "b6f6275c6676c8fa0e3ca2713a3257fd1b27d0639f" + 328 "695e347d8d1cf9ac819a26ca9b04cb0eb9b7b03598" + 329 "8d15bbac65212a55239cfc7e58fae38d7250ab9991" + 330 "ffbc97134025fe8ce04c4399ad96569be91a546f49" + 331 "78693c7a", 16); 332 dsaCache.put(Integer.valueOf(2048+256), 333 new DSAParameterSpec(p2048_256, q2048_256, g2048_256)); 334 335 // use DSA parameters for DH as well 336 dhCache.put(Integer.valueOf(512), new DHParameterSpec(p512, g512)); 337 dhCache.put(Integer.valueOf(768), new DHParameterSpec(p768, g768)); 338 dhCache.put(Integer.valueOf(1024), new DHParameterSpec(p1024, g1024)); 339 dhCache.put(Integer.valueOf(2048), new DHParameterSpec(p2048_224, g2048_224)); 340 } 341 342 }