Showing the changes to java/pkg/sun/security/provider/ParameterCache.java (1 of 18352 files). Show all
Unified · Split
| @@ -0,0 +1,226 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (c) 2003, 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.math.BigInteger; | |
| 30 | + | |
| 31 | +import java.security.*; | |
| 32 | +import java.security.SecureRandom; | |
| 33 | +import java.security.spec.*; | |
| 34 | + | |
| 35 | +import javax.crypto.spec.DHParameterSpec; | |
| 36 | + | |
| 37 | +/** | |
| 38 | + * Cache for DSA and DH parameter specs. Used by the KeyPairGenerators | |
| 39 | + * in the Sun, SunJCE, and SunPKCS11 provider if no parameters have been | |
| 40 | + * explicitly specified by the application. | |
| 41 | + * | |
| 42 | + * @author Andreas Sterbenz | |
| 43 | + * @since 1.5 | |
| 44 | + */ | |
| 45 | +public final class ParameterCache { | |
| 46 | + | |
| 47 | + private ParameterCache() { | |
| 48 | + // empty | |
| 49 | + } | |
| 50 | + | |
| 51 | + // cache of DSA parameters | |
| 52 | + private final static Map<Integer,DSAParameterSpec> dsaCache; | |
| 53 | + | |
| 54 | + // cache of DH parameters | |
| 55 | + private final static Map<Integer,DHParameterSpec> dhCache; | |
| 56 | + | |
| 57 | + /** | |
| 58 | + * Return cached DSA parameters for the given keylength, or null if none | |
| 59 | + * are available in the cache. | |
| 60 | + */ | |
| 61 | + public static DSAParameterSpec getCachedDSAParameterSpec(int keyLength) { | |
| 62 | + return dsaCache.get(Integer.valueOf(keyLength)); | |
| 63 | + } | |
| 64 | + | |
| 65 | + /** | |
| 66 | + * Return cached DH parameters for the given keylength, or null if none | |
| 67 | + * are available in the cache. | |
| 68 | + */ | |
| 69 | + public static DHParameterSpec getCachedDHParameterSpec(int keyLength) { | |
| 70 | + return dhCache.get(Integer.valueOf(keyLength)); | |
| 71 | + } | |
| 72 | + | |
| 73 | + /** | |
| 74 | + * Return DSA parameters for the given keylength. Uses cache if possible, | |
| 75 | + * generates new parameters and adds them to the cache otherwise. | |
| 76 | + */ | |
| 77 | + public static DSAParameterSpec getDSAParameterSpec(int keyLength, | |
| 78 | + SecureRandom random) | |
| 79 | + throws NoSuchAlgorithmException, InvalidParameterSpecException { | |
| 80 | + DSAParameterSpec spec = getCachedDSAParameterSpec(keyLength); | |
| 81 | + if (spec != null) { | |
| 82 | + return spec; | |
| 83 | + } | |
| 84 | + spec = getNewDSAParameterSpec(keyLength, random); | |
| 85 | + dsaCache.put(Integer.valueOf(keyLength), spec); | |
| 86 | + return spec; | |
| 87 | + } | |
| 88 | + | |
| 89 | + /** | |
| 90 | + * Return DH parameters for the given keylength. Uses cache if possible, | |
| 91 | + * generates new parameters and adds them to the cache otherwise. | |
| 92 | + */ | |
| 93 | + public static DHParameterSpec getDHParameterSpec(int keyLength, | |
| 94 | + SecureRandom random) | |
| 95 | + throws NoSuchAlgorithmException, InvalidParameterSpecException { | |
| 96 | + DHParameterSpec spec = getCachedDHParameterSpec(keyLength); | |
| 97 | + if (spec != null) { | |
| 98 | + return spec; | |
| 99 | + } | |
| 100 | + AlgorithmParameterGenerator gen = | |
| 101 | + AlgorithmParameterGenerator.getInstance("DH"); | |
| 102 | + gen.init(keyLength, random); | |
| 103 | + AlgorithmParameters params = gen.generateParameters(); | |
| 104 | + spec = params.getParameterSpec(DHParameterSpec.class); | |
| 105 | + dhCache.put(Integer.valueOf(keyLength), spec); | |
| 106 | + return spec; | |
| 107 | + } | |
| 108 | + | |
| 109 | + /** | |
| 110 | + * Return new DSA parameters for the given keylength. Do not lookup in | |
| 111 | + * cache and do not cache the newly generated parameters. This method | |
| 112 | + * really only exists for the legacy method | |
| 113 | + * DSAKeyPairGenerator.initialize(int, boolean, SecureRandom). | |
| 114 | + */ | |
| 115 | + public static DSAParameterSpec getNewDSAParameterSpec(int keyLength, | |
| 116 | + SecureRandom random) | |
| 117 | + throws NoSuchAlgorithmException, InvalidParameterSpecException { | |
| 118 | + AlgorithmParameterGenerator gen = | |
| 119 | + AlgorithmParameterGenerator.getInstance("DSA"); | |
| 120 | + gen.init(keyLength, random); | |
| 121 | + AlgorithmParameters params = gen.generateParameters(); | |
| 122 | + DSAParameterSpec spec = params.getParameterSpec(DSAParameterSpec.class); | |
| 123 | + return spec; | |
| 124 | + } | |
| 125 | + | |
| 126 | + static { | |
| 127 | + // XXX change to ConcurrentHashMap once available | |
| 128 | + dhCache = Collections.synchronizedMap | |
| 129 | + (new HashMap<Integer,DHParameterSpec>()); | |
| 130 | + dsaCache = Collections.synchronizedMap | |
| 131 | + (new HashMap<Integer,DSAParameterSpec>()); | |
| 132 | + | |
| 133 | + /* | |
| 134 | + * We support precomputed parameter for 512, 768 and 1024 bit | |
| 135 | + * moduli. In this file we provide both the seed and counter | |
| 136 | + * value of the generation process for each of these seeds, | |
| 137 | + * for validation purposes. We also include the test vectors | |
| 138 | + * from the DSA specification, FIPS 186, and the FIPS 186 | |
| 139 | + * Change No 1, which updates the test vector using SHA-1 | |
| 140 | + * instead of SHA (for both the G function and the message | |
| 141 | + * hash. | |
| 142 | + */ | |
| 143 | + | |
| 144 | + /* | |
| 145 | + * L = 512 | |
| 146 | + * SEED = b869c82b35d70e1b1ff91b28e37a62ecdc34409b | |
| 147 | + * counter = 123 | |
| 148 | + */ | |
| 149 | + BigInteger p512 = | |
| 150 | + new BigInteger("fca682ce8e12caba26efccf7110e526db078b05edecb" + | |
| 151 | + "cd1eb4a208f3ae1617ae01f35b91a47e6df63413c5e1" + | |
| 152 | + "2ed0899bcd132acd50d99151bdc43ee737592e17", 16); | |
| 153 | + | |
| 154 | + BigInteger q512 = | |
| 155 | + new BigInteger("962eddcc369cba8ebb260ee6b6a126d9346e38c5", 16); | |
| 156 | + | |
| 157 | + BigInteger g512 = | |
| 158 | + new BigInteger("678471b27a9cf44ee91a49c5147db1a9aaf244f05a43" + | |
| 159 | + "4d6486931d2d14271b9e35030b71fd73da179069b32e" + | |
| 160 | + "2935630e1c2062354d0da20a6c416e50be794ca4", 16); | |
| 161 | + | |
| 162 | + /* | |
| 163 | + * L = 768 | |
| 164 | + * SEED = 77d0f8c4dad15eb8c4f2f8d6726cefd96d5bb399 | |
| 165 | + * counter = 263 | |
| 166 | + */ | |
| 167 | + BigInteger p768 = | |
| 168 | + new BigInteger("e9e642599d355f37c97ffd3567120b8e25c9cd43e" + | |
| 169 | + "927b3a9670fbec5d890141922d2c3b3ad24800937" + | |
| 170 | + "99869d1e846aab49fab0ad26d2ce6a22219d470bc" + | |
| 171 | + "e7d777d4a21fbe9c270b57f607002f3cef8393694" + | |
| 172 | + "cf45ee3688c11a8c56ab127a3daf", 16); | |
| 173 | + | |
| 174 | + BigInteger q768 = | |
| 175 | + new BigInteger("9cdbd84c9f1ac2f38d0f80f42ab952e7338bf511", | |
| 176 | + 16); | |
| 177 | + | |
| 178 | + BigInteger g768 = | |
| 179 | + new BigInteger("30470ad5a005fb14ce2d9dcd87e38bc7d1b1c5fac" + | |
| 180 | + "baecbe95f190aa7a31d23c4dbbcbe06174544401a" + | |
| 181 | + "5b2c020965d8c2bd2171d3668445771f74ba084d2" + | |
| 182 | + "029d83c1c158547f3a9f1a2715be23d51ae4d3e5a" + | |
| 183 | + "1f6a7064f316933a346d3f529252", 16); | |
| 184 | + | |
| 185 | + | |
| 186 | + /* | |
| 187 | + * L = 1024 | |
| 188 | + * SEED = 8d5155894229d5e689ee01e6018a237e2cae64cd | |
| 189 | + * counter = 92 | |
| 190 | + */ | |
| 191 | + BigInteger p1024 = | |
| 192 | + new BigInteger("fd7f53811d75122952df4a9c2eece4e7f611b7523c" + | |
| 193 | + "ef4400c31e3f80b6512669455d402251fb593d8d58" + | |
| 194 | + "fabfc5f5ba30f6cb9b556cd7813b801d346ff26660" + | |
| 195 | + "b76b9950a5a49f9fe8047b1022c24fbba9d7feb7c6" + | |
| 196 | + "1bf83b57e7c6a8a6150f04fb83f6d3c51ec3023554" + | |
| 197 | + "135a169132f675f3ae2b61d72aeff22203199dd148" + | |
| 198 | + "01c7", 16); | |
| 199 | + | |
| 200 | + BigInteger q1024 = | |
| 201 | + new BigInteger("9760508f15230bccb292b982a2eb840bf0581cf5", | |
| 202 | + 16); | |
| 203 | + | |
| 204 | + BigInteger g1024 = | |
| 205 | + new BigInteger("f7e1a085d69b3ddecbbcab5c36b857b97994afbbfa" + | |
| 206 | + "3aea82f9574c0b3d0782675159578ebad4594fe671" + | |
| 207 | + "07108180b449167123e84c281613b7cf09328cc8a6" + | |
| 208 | + "e13c167a8b547c8d28e0a3ae1e2bb3a675916ea37f" + | |
| 209 | + "0bfa213562f1fb627a01243bcca4f1bea8519089a8" + | |
| 210 | + "83dfe15ae59f06928b665e807b552564014c3bfecf" + | |
| 211 | + "492a", 16); | |
| 212 | + | |
| 213 | + dsaCache.put(Integer.valueOf(512), | |
| 214 | + new DSAParameterSpec(p512, q512, g512)); | |
| 215 | + dsaCache.put(Integer.valueOf(768), | |
| 216 | + new DSAParameterSpec(p768, q768, g768)); | |
| 217 | + dsaCache.put(Integer.valueOf(1024), | |
| 218 | + new DSAParameterSpec(p1024, q1024, g1024)); | |
| 219 | + | |
| 220 | + // use DSA parameters for DH as well | |
| 221 | + dhCache.put(Integer.valueOf(512), new DHParameterSpec(p512, g512)); | |
| 222 | + dhCache.put(Integer.valueOf(768), new DHParameterSpec(p768, g768)); | |
| 223 | + dhCache.put(Integer.valueOf(1024), new DHParameterSpec(p1024, g1024)); | |
| 224 | + } | |
| 225 | + | |
| 226 | +} | |