0N/A/*
2362N/A * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.security.provider;
0N/A
0N/Aimport java.util.*;
0N/Aimport java.math.BigInteger;
0N/A
0N/Aimport java.security.*;
0N/Aimport java.security.SecureRandom;
0N/Aimport java.security.spec.*;
0N/A
0N/Aimport javax.crypto.spec.DHParameterSpec;
0N/A
0N/A/**
0N/A * Cache for DSA and DH parameter specs. Used by the KeyPairGenerators
0N/A * in the Sun, SunJCE, and SunPKCS11 provider if no parameters have been
0N/A * explicitly specified by the application.
0N/A *
0N/A * @author Andreas Sterbenz
0N/A * @since 1.5
0N/A */
0N/Apublic final class ParameterCache {
0N/A
0N/A private ParameterCache() {
0N/A // empty
0N/A }
0N/A
0N/A // cache of DSA parameters
0N/A private final static Map<Integer,DSAParameterSpec> dsaCache;
0N/A
0N/A // cache of DH parameters
0N/A private final static Map<Integer,DHParameterSpec> dhCache;
0N/A
0N/A /**
0N/A * Return cached DSA parameters for the given keylength, or null if none
0N/A * are available in the cache.
0N/A */
0N/A public static DSAParameterSpec getCachedDSAParameterSpec(int keyLength) {
0N/A return dsaCache.get(Integer.valueOf(keyLength));
0N/A }
0N/A
0N/A /**
0N/A * Return cached DH parameters for the given keylength, or null if none
0N/A * are available in the cache.
0N/A */
0N/A public static DHParameterSpec getCachedDHParameterSpec(int keyLength) {
0N/A return dhCache.get(Integer.valueOf(keyLength));
0N/A }
0N/A
0N/A /**
0N/A * Return DSA parameters for the given keylength. Uses cache if possible,
0N/A * generates new parameters and adds them to the cache otherwise.
0N/A */
0N/A public static DSAParameterSpec getDSAParameterSpec(int keyLength,
0N/A SecureRandom random)
0N/A throws NoSuchAlgorithmException, InvalidParameterSpecException {
0N/A DSAParameterSpec spec = getCachedDSAParameterSpec(keyLength);
0N/A if (spec != null) {
0N/A return spec;
0N/A }
0N/A spec = getNewDSAParameterSpec(keyLength, random);
0N/A dsaCache.put(Integer.valueOf(keyLength), spec);
0N/A return spec;
0N/A }
0N/A
0N/A /**
0N/A * Return DH parameters for the given keylength. Uses cache if possible,
0N/A * generates new parameters and adds them to the cache otherwise.
0N/A */
0N/A public static DHParameterSpec getDHParameterSpec(int keyLength,
0N/A SecureRandom random)
0N/A throws NoSuchAlgorithmException, InvalidParameterSpecException {
0N/A DHParameterSpec spec = getCachedDHParameterSpec(keyLength);
0N/A if (spec != null) {
0N/A return spec;
0N/A }
0N/A AlgorithmParameterGenerator gen =
0N/A AlgorithmParameterGenerator.getInstance("DH");
0N/A gen.init(keyLength, random);
0N/A AlgorithmParameters params = gen.generateParameters();
0N/A spec = params.getParameterSpec(DHParameterSpec.class);
0N/A dhCache.put(Integer.valueOf(keyLength), spec);
0N/A return spec;
0N/A }
0N/A
0N/A /**
0N/A * Return new DSA parameters for the given keylength. Do not lookup in
0N/A * cache and do not cache the newly generated parameters. This method
0N/A * really only exists for the legacy method
0N/A * DSAKeyPairGenerator.initialize(int, boolean, SecureRandom).
0N/A */
0N/A public static DSAParameterSpec getNewDSAParameterSpec(int keyLength,
0N/A SecureRandom random)
0N/A throws NoSuchAlgorithmException, InvalidParameterSpecException {
0N/A AlgorithmParameterGenerator gen =
0N/A AlgorithmParameterGenerator.getInstance("DSA");
0N/A gen.init(keyLength, random);
0N/A AlgorithmParameters params = gen.generateParameters();
0N/A DSAParameterSpec spec = params.getParameterSpec(DSAParameterSpec.class);
0N/A return spec;
0N/A }
0N/A
0N/A static {
0N/A // XXX change to ConcurrentHashMap once available
0N/A dhCache = Collections.synchronizedMap
0N/A (new HashMap<Integer,DHParameterSpec>());
0N/A dsaCache = Collections.synchronizedMap
0N/A (new HashMap<Integer,DSAParameterSpec>());
0N/A
0N/A /*
0N/A * We support precomputed parameter for 512, 768 and 1024 bit
0N/A * moduli. In this file we provide both the seed and counter
0N/A * value of the generation process for each of these seeds,
0N/A * for validation purposes. We also include the test vectors
0N/A * from the DSA specification, FIPS 186, and the FIPS 186
0N/A * Change No 1, which updates the test vector using SHA-1
0N/A * instead of SHA (for both the G function and the message
0N/A * hash.
0N/A */
0N/A
0N/A /*
0N/A * L = 512
0N/A * SEED = b869c82b35d70e1b1ff91b28e37a62ecdc34409b
0N/A * counter = 123
0N/A */
0N/A BigInteger p512 =
0N/A new BigInteger("fca682ce8e12caba26efccf7110e526db078b05edecb" +
0N/A "cd1eb4a208f3ae1617ae01f35b91a47e6df63413c5e1" +
0N/A "2ed0899bcd132acd50d99151bdc43ee737592e17", 16);
0N/A
0N/A BigInteger q512 =
0N/A new BigInteger("962eddcc369cba8ebb260ee6b6a126d9346e38c5", 16);
0N/A
0N/A BigInteger g512 =
0N/A new BigInteger("678471b27a9cf44ee91a49c5147db1a9aaf244f05a43" +
0N/A "4d6486931d2d14271b9e35030b71fd73da179069b32e" +
0N/A "2935630e1c2062354d0da20a6c416e50be794ca4", 16);
0N/A
0N/A /*
0N/A * L = 768
0N/A * SEED = 77d0f8c4dad15eb8c4f2f8d6726cefd96d5bb399
0N/A * counter = 263
0N/A */
0N/A BigInteger p768 =
0N/A new BigInteger("e9e642599d355f37c97ffd3567120b8e25c9cd43e" +
0N/A "927b3a9670fbec5d890141922d2c3b3ad24800937" +
0N/A "99869d1e846aab49fab0ad26d2ce6a22219d470bc" +
0N/A "e7d777d4a21fbe9c270b57f607002f3cef8393694" +
0N/A "cf45ee3688c11a8c56ab127a3daf", 16);
0N/A
0N/A BigInteger q768 =
0N/A new BigInteger("9cdbd84c9f1ac2f38d0f80f42ab952e7338bf511",
0N/A 16);
0N/A
0N/A BigInteger g768 =
0N/A new BigInteger("30470ad5a005fb14ce2d9dcd87e38bc7d1b1c5fac" +
0N/A "baecbe95f190aa7a31d23c4dbbcbe06174544401a" +
0N/A "5b2c020965d8c2bd2171d3668445771f74ba084d2" +
0N/A "029d83c1c158547f3a9f1a2715be23d51ae4d3e5a" +
0N/A "1f6a7064f316933a346d3f529252", 16);
0N/A
0N/A
0N/A /*
0N/A * L = 1024
0N/A * SEED = 8d5155894229d5e689ee01e6018a237e2cae64cd
0N/A * counter = 92
0N/A */
0N/A BigInteger p1024 =
0N/A new BigInteger("fd7f53811d75122952df4a9c2eece4e7f611b7523c" +
0N/A "ef4400c31e3f80b6512669455d402251fb593d8d58" +
0N/A "fabfc5f5ba30f6cb9b556cd7813b801d346ff26660" +
0N/A "b76b9950a5a49f9fe8047b1022c24fbba9d7feb7c6" +
0N/A "1bf83b57e7c6a8a6150f04fb83f6d3c51ec3023554" +
0N/A "135a169132f675f3ae2b61d72aeff22203199dd148" +
0N/A "01c7", 16);
0N/A
0N/A BigInteger q1024 =
0N/A new BigInteger("9760508f15230bccb292b982a2eb840bf0581cf5",
0N/A 16);
0N/A
0N/A BigInteger g1024 =
0N/A new BigInteger("f7e1a085d69b3ddecbbcab5c36b857b97994afbbfa" +
0N/A "3aea82f9574c0b3d0782675159578ebad4594fe671" +
0N/A "07108180b449167123e84c281613b7cf09328cc8a6" +
0N/A "e13c167a8b547c8d28e0a3ae1e2bb3a675916ea37f" +
0N/A "0bfa213562f1fb627a01243bcca4f1bea8519089a8" +
0N/A "83dfe15ae59f06928b665e807b552564014c3bfecf" +
0N/A "492a", 16);
0N/A
0N/A dsaCache.put(Integer.valueOf(512),
0N/A new DSAParameterSpec(p512, q512, g512));
0N/A dsaCache.put(Integer.valueOf(768),
0N/A new DSAParameterSpec(p768, q768, g768));
0N/A dsaCache.put(Integer.valueOf(1024),
0N/A new DSAParameterSpec(p1024, q1024, g1024));
0N/A
0N/A // use DSA parameters for DH as well
0N/A dhCache.put(Integer.valueOf(512), new DHParameterSpec(p512, g512));
0N/A dhCache.put(Integer.valueOf(768), new DHParameterSpec(p768, g768));
0N/A dhCache.put(Integer.valueOf(1024), new DHParameterSpec(p1024, g1024));
0N/A }
0N/A
0N/A}