src/java.base/share/classes/sun/security/provider/DSAKeyPairGenerator.java
author coffeys
Fri, 17 Aug 2018 22:20:47 +0100
changeset 51438 6ca468ea3564
parent 47421 f9e03aef3a49
permissions -rw-r--r--
8208675: Remove legacy sun.security.key.serial.interop property Reviewed-by: mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
51438
6ca468ea3564 8208675: Remove legacy sun.security.key.serial.interop property
coffeys
parents: 47421
diff changeset
     2
 * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.security.provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.math.BigInteger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.SecureRandom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.interfaces.DSAParams;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.spec.AlgorithmParameterSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.spec.InvalidParameterSpecException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.spec.DSAParameterSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.security.jca.JCAUtil;
47421
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
    38
import static sun.security.util.SecurityProviderConstants.DEF_DSA_KEY_SIZE;
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
    39
import static sun.security.util.SecurityProviderConstants.getDefDSASubprimeSize;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * This class generates DSA key parameters and public/private key
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * pairs according to the DSS standard NIST FIPS 186. It uses the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * updated version of SHA, SHA-1 as described in FIPS 180-1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @author Benjamin Renaud
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * @author Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 */
47421
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
    50
class DSAKeyPairGenerator extends KeyPairGenerator {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
13672
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
    52
    /* Length for prime P and subPrime Q in bits */
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
    53
    private int plen;
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
    54
    private int qlen;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /* whether to force new parameters to be generated for each KeyPair */
47421
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
    57
    boolean forceNewParameters;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    /* preset algorithm parameters. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private DSAParameterSpec params;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    /* The source of random bits to use */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private SecureRandom random;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
47421
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
    65
    DSAKeyPairGenerator(int defaultKeySize) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        super("DSA");
47421
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
    67
        initialize(defaultKeySize, null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
13672
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
    70
    private static void checkStrength(int sizeP, int sizeQ) {
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
    71
        if ((sizeP >= 512) && (sizeP <= 1024) && (sizeP % 64 == 0)
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
    72
            && sizeQ == 160) {
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
    73
            // traditional - allow for backward compatibility
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
    74
            // L=multiples of 64 and between 512 and 1024 (inclusive)
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
    75
            // N=160
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
    76
        } else if (sizeP == 2048 && (sizeQ == 224 || sizeQ == 256)) {
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
    77
            // L=2048, N=224 or 256
37361
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 25859
diff changeset
    78
        } else if (sizeP == 3072 && sizeQ == 256) {
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 25859
diff changeset
    79
            // L=3072, N=256
13672
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
    80
        } else {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            throw new InvalidParameterException
13672
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
    82
                ("Unsupported prime and subprime size combination: " +
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
    83
                 sizeP + ", " + sizeQ);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    public void initialize(int modlen, SecureRandom random) {
47421
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
    88
        init(modlen, random, false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * Initializes the DSA object using a parameter object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * @param params the parameter set to be used to generate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * the keys.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * @param random the source of randomness for this generator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * @exception InvalidAlgorithmParameterException if the given parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * are inappropriate for this key pair generator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    public void initialize(AlgorithmParameterSpec params, SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            throws InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        if (!(params instanceof DSAParameterSpec)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            throw new InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                ("Inappropriate parameter");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
47421
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
   107
        init((DSAParameterSpec)params, random, false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
47421
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
   110
    void init(int modlen, SecureRandom random, boolean forceNew) {
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
   111
        int subPrimeLen = getDefDSASubprimeSize(modlen);
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
   112
        checkStrength(modlen, subPrimeLen);
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
   113
        this.plen = modlen;
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
   114
        this.qlen = subPrimeLen;
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
   115
        this.params = null;
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
   116
        this.random = random;
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
   117
        this.forceNewParameters = forceNew;
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
   118
    }
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
   119
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
   120
    void init(DSAParameterSpec params, SecureRandom random,
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
   121
        boolean forceNew) {
13672
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   122
        int sizeP = params.getP().bitLength();
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   123
        int sizeQ = params.getQ().bitLength();
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   124
        checkStrength(sizeP, sizeQ);
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   125
        this.plen = sizeP;
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   126
        this.qlen = sizeQ;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        this.params = params;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        this.random = random;
47421
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
   129
        this.forceNewParameters = forceNew;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * Generates a pair of keys usable by any JavaSecurity compliant
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * DSA implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    public KeyPair generateKeyPair() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        if (random == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            random = JCAUtil.getSecureRandom();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        DSAParameterSpec spec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            if (forceNewParameters) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                // generate new parameters each time
13672
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   144
                spec = ParameterCache.getNewDSAParameterSpec(plen, qlen, random);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                if (params == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                    params =
13672
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   148
                        ParameterCache.getDSAParameterSpec(plen, qlen, random);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                spec = params;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            throw new ProviderException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        return generateKeyPair(spec.getP(), spec.getQ(), spec.getG(), random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
47421
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
   158
    private KeyPair generateKeyPair(BigInteger p, BigInteger q, BigInteger g,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                                   SecureRandom random) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        BigInteger x = generateX(random, q);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        BigInteger y = generateY(x, p, g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            // See the comments in DSAKeyFactory, 4532506, and 6232513.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            DSAPublicKey pub;
51438
6ca468ea3564 8208675: Remove legacy sun.security.key.serial.interop property
coffeys
parents: 47421
diff changeset
   169
            pub = new DSAPublicKeyImpl(y, p, q, g);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            DSAPrivateKey priv = new DSAPrivateKey(x, p, q, g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            KeyPair pair = new KeyPair(pub, priv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            return pair;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        } catch (InvalidKeyException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            throw new ProviderException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * Generate the private key component of the key pair using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * provided source of random bits. This method uses the random but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * source passed to generate a seed and then calls the seed-based
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * generateX method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    private BigInteger generateX(SecureRandom random, BigInteger q) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        BigInteger x = null;
13672
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   187
        byte[] temp = new byte[qlen];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        while (true) {
13672
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   189
            random.nextBytes(temp);
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   190
            x = new BigInteger(1, temp).mod(q);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            if (x.signum() > 0 && (x.compareTo(q) < 0)) {
13672
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   192
                return x;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * Generate the public key component y of the key pair.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * @param x the private key component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * @param p the base parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    BigInteger generateY(BigInteger x, BigInteger p, BigInteger g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        BigInteger y = g.modPow(x, p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        return y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
47421
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
   209
    public static final class Current extends DSAKeyPairGenerator {
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
   210
        public Current() {
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
   211
            super(DEF_DSA_KEY_SIZE);
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
   212
        }
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
   213
    }
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
   214
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
   215
    public static final class Legacy extends DSAKeyPairGenerator
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
   216
        implements java.security.interfaces.DSAKeyPairGenerator {
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
   217
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
   218
        public Legacy() {
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
   219
            super(1024);
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
   220
        }
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
   221
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
   222
        /**
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
   223
         * Initializes the DSA key pair generator. If <code>genParams</code>
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
   224
         * is false, a set of pre-computed parameters is used.
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
   225
         */
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
   226
        @Override
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
   227
        public void initialize(int modlen, boolean genParams,
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
   228
            SecureRandom random) throws InvalidParameterException {
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
   229
            if (genParams) {
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
   230
                super.init(modlen, random, true);
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
   231
            } else {
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
   232
                DSAParameterSpec cachedParams =
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
   233
                    ParameterCache.getCachedDSAParameterSpec(modlen,
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
   234
                        getDefDSASubprimeSize(modlen));
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
   235
                if (cachedParams == null) {
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
   236
                    throw new InvalidParameterException
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
   237
                        ("No precomputed parameters for requested modulus" +
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
   238
                         " size available");
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
   239
                }
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
   240
                super.init(cachedParams, random, false);
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
   241
            }
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
   242
        }
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
   243
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
   244
        /**
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
   245
         * Initializes the DSA object using a DSA parameter object.
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
   246
         *
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
   247
         * @param params a fully initialized DSA parameter object.
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
   248
         */
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
   249
        @Override
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
   250
        public void initialize(DSAParams params, SecureRandom random)
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
   251
            throws InvalidParameterException {
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
   252
            if (params == null) {
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
   253
                throw new InvalidParameterException("Params must not be null");
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
   254
             }
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
   255
             DSAParameterSpec spec = new DSAParameterSpec
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
   256
                 (params.getP(), params.getQ(), params.getG());
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
   257
             super.init(spec, random, false);
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
   258
        }
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
   259
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
}