Path: blob/master/src/java.base/share/classes/sun/security/provider/DSAKeyPairGenerator.java
41159 views
/*1* Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.security.provider;2627import java.math.BigInteger;2829import java.security.*;30import java.security.SecureRandom;31import java.security.interfaces.DSAParams;32import java.security.spec.AlgorithmParameterSpec;33import java.security.spec.InvalidParameterSpecException;34import java.security.spec.DSAParameterSpec;3536import sun.security.jca.JCAUtil;37import static sun.security.util.SecurityProviderConstants.DEF_DSA_KEY_SIZE;38import static sun.security.util.SecurityProviderConstants.getDefDSASubprimeSize;3940/**41* This class generates DSA key parameters and public/private key42* pairs according to the DSS standard NIST FIPS 186. It uses the43* updated version of SHA, SHA-1 as described in FIPS 180-1.44*45* @author Benjamin Renaud46* @author Andreas Sterbenz47*48*/49class DSAKeyPairGenerator extends KeyPairGenerator {5051/* Length for prime P and subPrime Q in bits */52private int plen;53private int qlen;5455/* whether to force new parameters to be generated for each KeyPair */56boolean forceNewParameters;5758/* preset algorithm parameters. */59private DSAParameterSpec params;6061/* The source of random bits to use */62private SecureRandom random;6364DSAKeyPairGenerator(int defaultKeySize) {65super("DSA");66initialize(defaultKeySize, null);67}6869private static void checkStrength(int sizeP, int sizeQ) {70if ((sizeP >= 512) && (sizeP <= 1024) && (sizeP % 64 == 0)71&& sizeQ == 160) {72// traditional - allow for backward compatibility73// L=multiples of 64 and between 512 and 1024 (inclusive)74// N=16075} else if (sizeP == 2048 && (sizeQ == 224 || sizeQ == 256)) {76// L=2048, N=224 or 25677} else if (sizeP == 3072 && sizeQ == 256) {78// L=3072, N=25679} else {80throw new InvalidParameterException81("Unsupported prime and subprime size combination: " +82sizeP + ", " + sizeQ);83}84}8586public void initialize(int modlen, SecureRandom random) {87init(modlen, random, false);88}8990/**91* Initializes the DSA object using a parameter object.92*93* @param params the parameter set to be used to generate94* the keys.95* @param random the source of randomness for this generator.96*97* @exception InvalidAlgorithmParameterException if the given parameters98* are inappropriate for this key pair generator99*/100public void initialize(AlgorithmParameterSpec params, SecureRandom random)101throws InvalidAlgorithmParameterException {102if (!(params instanceof DSAParameterSpec)) {103throw new InvalidAlgorithmParameterException104("Inappropriate parameter");105}106init((DSAParameterSpec)params, random, false);107}108109void init(int modlen, SecureRandom random, boolean forceNew) {110int subPrimeLen = getDefDSASubprimeSize(modlen);111checkStrength(modlen, subPrimeLen);112this.plen = modlen;113this.qlen = subPrimeLen;114this.params = null;115this.random = random;116this.forceNewParameters = forceNew;117}118119void init(DSAParameterSpec params, SecureRandom random,120boolean forceNew) {121int sizeP = params.getP().bitLength();122int sizeQ = params.getQ().bitLength();123checkStrength(sizeP, sizeQ);124this.plen = sizeP;125this.qlen = sizeQ;126this.params = params;127this.random = random;128this.forceNewParameters = forceNew;129}130131/**132* Generates a pair of keys usable by any JavaSecurity compliant133* DSA implementation.134*/135public KeyPair generateKeyPair() {136if (random == null) {137random = JCAUtil.getSecureRandom();138}139DSAParameterSpec spec;140try {141if (forceNewParameters) {142// generate new parameters each time143spec = ParameterCache.getNewDSAParameterSpec(plen, qlen, random);144} else {145if (params == null) {146params =147ParameterCache.getDSAParameterSpec(plen, qlen, random);148}149spec = params;150}151} catch (GeneralSecurityException e) {152throw new ProviderException(e);153}154return generateKeyPair(spec.getP(), spec.getQ(), spec.getG(), random);155}156157private KeyPair generateKeyPair(BigInteger p, BigInteger q, BigInteger g,158SecureRandom random) {159160BigInteger x = generateX(random, q);161BigInteger y = generateY(x, p, g);162163try {164165// See the comments in DSAKeyFactory, 4532506, and 6232513.166167DSAPublicKey pub;168pub = new DSAPublicKeyImpl(y, p, q, g);169DSAPrivateKey priv = new DSAPrivateKey(x, p, q, g);170171KeyPair pair = new KeyPair(pub, priv);172return pair;173} catch (InvalidKeyException e) {174throw new ProviderException(e);175}176}177178/**179* Generate the private key component of the key pair using the180* provided source of random bits. This method uses the random but181* source passed to generate a seed and then calls the seed-based182* generateX method.183*/184private BigInteger generateX(SecureRandom random, BigInteger q) {185BigInteger x = null;186byte[] temp = new byte[qlen];187while (true) {188random.nextBytes(temp);189x = new BigInteger(1, temp).mod(q);190if (x.signum() > 0 && (x.compareTo(q) < 0)) {191return x;192}193}194}195196/**197* Generate the public key component y of the key pair.198*199* @param x the private key component.200*201* @param p the base parameter.202*/203BigInteger generateY(BigInteger x, BigInteger p, BigInteger g) {204BigInteger y = g.modPow(x, p);205return y;206}207208public static final class Current extends DSAKeyPairGenerator {209public Current() {210super(DEF_DSA_KEY_SIZE);211}212}213214public static final class Legacy extends DSAKeyPairGenerator215implements java.security.interfaces.DSAKeyPairGenerator {216217public Legacy() {218super(1024);219}220221/**222* Initializes the DSA key pair generator. If <code>genParams</code>223* is false, a set of pre-computed parameters is used.224*/225@Override226public void initialize(int modlen, boolean genParams,227SecureRandom random) throws InvalidParameterException {228if (genParams) {229super.init(modlen, random, true);230} else {231DSAParameterSpec cachedParams =232ParameterCache.getCachedDSAParameterSpec(modlen,233getDefDSASubprimeSize(modlen));234if (cachedParams == null) {235throw new InvalidParameterException236("No precomputed parameters for requested modulus" +237" size available");238}239super.init(cachedParams, random, false);240}241}242243/**244* Initializes the DSA object using a DSA parameter object.245*246* @param params a fully initialized DSA parameter object.247*/248@Override249public void initialize(DSAParams params, SecureRandom random)250throws InvalidParameterException {251if (params == null) {252throw new InvalidParameterException("Params must not be null");253}254DSAParameterSpec spec = new DSAParameterSpec255(params.getP(), params.getQ(), params.getG());256super.init(spec, random, false);257}258}259}260261262