src/java.base/share/classes/sun/security/provider/DSAParameterGenerator.java
author weijun
Wed, 01 Aug 2018 13:35:08 +0800
changeset 51272 9d92ff04a29c
parent 47421 f9e03aef3a49
permissions -rw-r--r--
8208602: Cannot read PEM X.509 cert if there is whitespace after the header or footer Reviewed-by: xuelei
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
47421
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
     2
 * Copyright (c) 1997, 2017, 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
import java.security.AlgorithmParameterGeneratorSpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.AlgorithmParameters;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.InvalidAlgorithmParameterException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.NoSuchAlgorithmException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.NoSuchProviderException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.InvalidParameterException;
13672
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
    35
import java.security.MessageDigest;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.SecureRandom;
47421
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
    37
import java.security.ProviderException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.security.spec.AlgorithmParameterSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.security.spec.InvalidParameterSpecException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.security.spec.DSAParameterSpec;
13672
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
    41
import java.security.spec.DSAGenParameterSpec;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
47421
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
    43
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
    44
import static sun.security.util.SecurityProviderConstants.getDefDSASubprimeSize;
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
    45
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
    46
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
/**
47421
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
    48
 * This class generates parameters for the DSA algorithm.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * @author Jan Luehe
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * @see java.security.AlgorithmParameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * @see java.security.spec.AlgorithmParameterSpec
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * @see DSAParameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
public class DSAParameterGenerator extends AlgorithmParameterGeneratorSpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
13672
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
    62
    // the length of prime P, subPrime Q, and seed in bits
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
    63
    private int valueL = -1;
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
    64
    private int valueN = -1;
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
    65
    private int seedLen = -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    // the source of randomness
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private SecureRandom random;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    public DSAParameterGenerator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * Initializes this parameter generator for a certain strength
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * and source of randomness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * @param strength the strength (size of prime) in bits
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * @param random the source of randomness
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     */
37361
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 31538
diff changeset
    80
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    protected void engineInit(int strength, 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
    82
        if ((strength != 2048) && (strength != 3072) &&
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
    83
            ((strength < 512) || (strength > 1024) || (strength % 64 != 0))) {
37361
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 31538
diff changeset
    84
            throw new InvalidParameterException(
47421
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
    85
                "Unexpected strength (size of prime): " + strength +
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
    86
                ". Prime size should be 512-1024, 2048, or 3072");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        }
13672
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
    88
        this.valueL = strength;
47421
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
    89
        this.valueN = getDefDSASubprimeSize(strength);
13672
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
    90
        this.seedLen = valueN;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        this.random = random;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * Initializes this parameter generator with a set of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * algorithm-specific parameter generation values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     *
37361
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 31538
diff changeset
    98
     * @param genParamSpec the set of algorithm-specific parameter
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 31538
diff changeset
    99
     *        generation values
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * @param random the source of randomness
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * @exception InvalidAlgorithmParameterException if the given parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * generation values are inappropriate for this parameter generator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     */
37361
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 31538
diff changeset
   105
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    protected void engineInit(AlgorithmParameterSpec genParamSpec,
37361
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 31538
diff changeset
   107
            SecureRandom random) throws InvalidAlgorithmParameterException {
13672
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   108
        if (!(genParamSpec instanceof DSAGenParameterSpec)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            throw new InvalidAlgorithmParameterException("Invalid parameter");
13672
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   110
        }
37361
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 31538
diff changeset
   111
        DSAGenParameterSpec dsaGenParams = (DSAGenParameterSpec)genParamSpec;
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 31538
diff changeset
   112
13672
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   113
        // directly initialize using the already validated values
37361
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 31538
diff changeset
   114
        this.valueL = dsaGenParams.getPrimePLength();
13672
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   115
        this.valueN = dsaGenParams.getSubprimeQLength();
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   116
        this.seedLen = dsaGenParams.getSeedLength();
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   117
        this.random = random;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * Generates the parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * @return the new AlgorithmParameters object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     */
37361
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 31538
diff changeset
   125
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    protected AlgorithmParameters engineGenerateParameters() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        AlgorithmParameters algParams = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            if (this.random == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                this.random = new SecureRandom();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            }
13672
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   132
            if (valueL == -1) {
47421
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
   133
                engineInit(DEF_DSA_KEY_SIZE, this.random);
13672
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   134
            }
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   135
            BigInteger[] pAndQ = generatePandQ(this.random, valueL,
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   136
                                               valueN, seedLen);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            BigInteger paramP = pAndQ[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            BigInteger paramQ = pAndQ[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            BigInteger paramG = generateG(paramP, paramQ);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
13672
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   141
            DSAParameterSpec dsaParamSpec =
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   142
                new DSAParameterSpec(paramP, paramQ, paramG);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            algParams = AlgorithmParameters.getInstance("DSA", "SUN");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            algParams.init(dsaParamSpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        } catch (InvalidParameterSpecException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            // this should never happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            throw new RuntimeException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        } catch (NoSuchAlgorithmException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            // this should never happen, because we provide it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            throw new RuntimeException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        } catch (NoSuchProviderException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            // this should never happen, because we provide it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            throw new RuntimeException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        return algParams;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * Generates the prime and subprime parameters for DSA,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * using the provided source of randomness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * This method will generate new seeds until a suitable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * seed has been found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * @param random the source of randomness to generate the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * seed
13672
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   167
     * @param valueL the size of <code>p</code>, in bits.
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   168
     * @param valueN the size of <code>q</code>, in bits.
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   169
     * @param seedLen the length of <code>seed</code>, in bits.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * @return an array of BigInteger, with <code>p</code> at index 0 and
13672
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   172
     * <code>q</code> at index 1, the seed at index 2, and the counter value
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   173
     * at index 3.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     */
13672
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   175
    private static BigInteger[] generatePandQ(SecureRandom random, int valueL,
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   176
                                              int valueN, int seedLen) {
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   177
        String hashAlg = null;
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   178
        if (valueN == 160) {
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   179
            hashAlg = "SHA";
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   180
        } else if (valueN == 224) {
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   181
            hashAlg = "SHA-224";
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   182
        } else if (valueN == 256) {
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   183
            hashAlg = "SHA-256";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        }
13672
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   185
        MessageDigest hashObj = null;
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   186
        try {
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   187
            hashObj = MessageDigest.getInstance(hashAlg);
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   188
        } catch (NoSuchAlgorithmException nsae) {
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   189
            // should never happen
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   190
            nsae.printStackTrace();
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   191
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
13672
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   193
        /* Step 3, 4: Useful variables */
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   194
        int outLen = hashObj.getDigestLength()*8;
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   195
        int n = (valueL - 1) / outLen;
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   196
        int b = (valueL - 1) % outLen;
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   197
        byte[] seedBytes = new byte[seedLen/8];
37361
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 31538
diff changeset
   198
        BigInteger twoSl = BigInteger.TWO.pow(seedLen);
47421
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
   199
        int primeCertainty = -1;
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
   200
        if (valueL <= 1024) {
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
   201
            primeCertainty = 80;
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
   202
        } else if (valueL == 2048) {
13672
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   203
            primeCertainty = 112;
37361
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 31538
diff changeset
   204
        } else if (valueL == 3072) {
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 31538
diff changeset
   205
            primeCertainty = 128;
13672
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   206
        }
47421
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
   207
        if (primeCertainty < 0) {
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
   208
            throw new ProviderException("Invalid valueL: " + valueL);
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
   209
        }
13672
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   210
        BigInteger resultP, resultQ, seed = null;
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   211
        int counter;
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   212
        while (true) {
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   213
            do {
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   214
                /* Step 5 */
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   215
                random.nextBytes(seedBytes);
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   216
                seed = new BigInteger(1, seedBytes);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
13672
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   218
                /* Step 6 */
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   219
                BigInteger U = new BigInteger(1, hashObj.digest(seedBytes)).
37361
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 31538
diff changeset
   220
                    mod(BigInteger.TWO.pow(valueN - 1));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
13672
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   222
                /* Step 7 */
37361
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 31538
diff changeset
   223
                resultQ = BigInteger.TWO.pow(valueN - 1)
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 31538
diff changeset
   224
                            .add(U)
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 31538
diff changeset
   225
                            .add(BigInteger.ONE)
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 31538
diff changeset
   226
                            .subtract(U.mod(BigInteger.TWO));
13672
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   227
            } while (!resultQ.isProbablePrime(primeCertainty));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
13672
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   229
            /* Step 10 */
37361
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 31538
diff changeset
   230
            BigInteger offset = BigInteger.ONE;
13672
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   231
            /* Step 11 */
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   232
            for (counter = 0; counter < 4*valueL; counter++) {
31538
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 25859
diff changeset
   233
                BigInteger[] V = new BigInteger[n + 1];
13672
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   234
                /* Step 11.1 */
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   235
                for (int j = 0; j <= n; j++) {
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   236
                    BigInteger J = BigInteger.valueOf(j);
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   237
                    BigInteger tmp = (seed.add(offset).add(J)).mod(twoSl);
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   238
                    byte[] vjBytes = hashObj.digest(toByteArray(tmp));
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   239
                    V[j] = new BigInteger(1, vjBytes);
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   240
                }
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   241
                /* Step 11.2 */
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   242
                BigInteger W = V[0];
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   243
                for (int i = 1; i < n; i++) {
37361
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 31538
diff changeset
   244
                    W = W.add(V[i].multiply(BigInteger.TWO.pow(i * outLen)));
13672
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   245
                }
37361
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 31538
diff changeset
   246
                W = W.add((V[n].mod(BigInteger.TWO.pow(b)))
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 31538
diff changeset
   247
                               .multiply(BigInteger.TWO.pow(n * outLen)));
13672
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   248
                /* Step 11.3 */
37361
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 31538
diff changeset
   249
                BigInteger twoLm1 = BigInteger.TWO.pow(valueL - 1);
13672
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   250
                BigInteger X = W.add(twoLm1);
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   251
                /* Step 11.4, 11.5 */
37361
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 31538
diff changeset
   252
                BigInteger c = X.mod(resultQ.multiply(BigInteger.TWO));
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 31538
diff changeset
   253
                resultP = X.subtract(c.subtract(BigInteger.ONE));
13672
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   254
                /* Step 11.6, 11.7 */
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   255
                if (resultP.compareTo(twoLm1) > -1
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   256
                    && resultP.isProbablePrime(primeCertainty)) {
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   257
                    /* Step 11.8 */
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   258
                    BigInteger[] result = {resultP, resultQ, seed,
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   259
                                           BigInteger.valueOf(counter)};
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   260
                    return result;
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   261
                }
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   262
                /* Step 11.9 */
37361
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 31538
diff changeset
   263
                offset = offset.add(BigInteger.valueOf(n)).add(BigInteger.ONE);
13672
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   264
             }
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   265
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * Generates the <code>g</code> parameter for DSA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * @param p the prime, <code>p</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * @param q the subprime, <code>q</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * @param the <code>g</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     */
13672
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   277
    private static BigInteger generateG(BigInteger p, BigInteger q) {
37361
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 31538
diff changeset
   278
        BigInteger h = BigInteger.ONE;
13672
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   279
        /* Step 1 */
37361
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 31538
diff changeset
   280
        BigInteger pMinusOneOverQ = (p.subtract(BigInteger.ONE)).divide(q);
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 31538
diff changeset
   281
        BigInteger resultG = BigInteger.ONE;
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 31538
diff changeset
   282
        while (resultG.compareTo(BigInteger.TWO) < 0) {
13672
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   283
            /* Step 3 */
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   284
            resultG = h.modPow(pMinusOneOverQ, p);
37361
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 31538
diff changeset
   285
            h = h.add(BigInteger.ONE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        }
13672
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   287
        return resultG;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * Converts the result of a BigInteger.toByteArray call to an exact
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * signed magnitude representation for any positive number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     */
13672
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   294
    private static byte[] toByteArray(BigInteger bigInt) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        byte[] result = bigInt.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        if (result[0] == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            byte[] tmp = new byte[result.length - 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            System.arraycopy(result, 1, tmp, 0, tmp.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            result = tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
}