Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/security/provider/DSAKeyPairGenerator.java
38830 views
/*1* Copyright (c) 1997, 2017, 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;168if (DSAKeyFactory.SERIAL_INTEROP) {169pub = new DSAPublicKey(y, p, q, g);170} else {171pub = new DSAPublicKeyImpl(y, p, q, g);172}173DSAPrivateKey priv = new DSAPrivateKey(x, p, q, g);174175KeyPair pair = new KeyPair(pub, priv);176return pair;177} catch (InvalidKeyException e) {178throw new ProviderException(e);179}180}181182/**183* Generate the private key component of the key pair using the184* provided source of random bits. This method uses the random but185* source passed to generate a seed and then calls the seed-based186* generateX method.187*/188private BigInteger generateX(SecureRandom random, BigInteger q) {189BigInteger x = null;190byte[] temp = new byte[qlen];191while (true) {192random.nextBytes(temp);193x = new BigInteger(1, temp).mod(q);194if (x.signum() > 0 && (x.compareTo(q) < 0)) {195return x;196}197}198}199200/**201* Generate the public key component y of the key pair.202*203* @param x the private key component.204*205* @param p the base parameter.206*/207BigInteger generateY(BigInteger x, BigInteger p, BigInteger g) {208BigInteger y = g.modPow(x, p);209return y;210}211212public static final class Current extends DSAKeyPairGenerator {213public Current() {214super(DEF_DSA_KEY_SIZE);215}216}217218public static final class Legacy extends DSAKeyPairGenerator219implements java.security.interfaces.DSAKeyPairGenerator {220221public Legacy() {222super(1024);223}224225/**226* Initializes the DSA key pair generator. If <code>genParams</code>227* is false, a set of pre-computed parameters is used.228*/229@Override230public void initialize(int modlen, boolean genParams,231SecureRandom random) throws InvalidParameterException {232if (genParams) {233super.init(modlen, random, true);234} else {235DSAParameterSpec cachedParams =236ParameterCache.getCachedDSAParameterSpec(modlen,237getDefDSASubprimeSize(modlen));238if (cachedParams == null) {239throw new InvalidParameterException240("No precomputed parameters for requested modulus" +241" size available");242}243super.init(cachedParams, random, false);244}245}246247/**248* Initializes the DSA object using a DSA parameter object.249*250* @param params a fully initialized DSA parameter object.251*/252@Override253public void initialize(DSAParams params, SecureRandom random)254throws InvalidParameterException {255if (params == null) {256throw new InvalidParameterException("Params must not be null");257}258DSAParameterSpec spec = new DSAParameterSpec259(params.getP(), params.getQ(), params.getG());260super.init(spec, random, false);261}262}263}264265266