jdk/src/share/classes/com/sun/crypto/provider/DHParameterGenerator.java
author jjg
Mon, 15 Aug 2011 11:48:20 -0700
changeset 10336 0bb1999251f8
parent 5506 202f599c92aa
child 13672 604588823b5a
permissions -rw-r--r--
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror Reviewed-by: xuelei, mullan Contributed-by: alexandre.boulgakov@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
     2
 * Copyright (c) 1997, 2011, 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 com.sun.crypto.provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.spec.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import javax.crypto.spec.DHParameterSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.crypto.spec.DHGenParameterSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * This class generates parameters for the Diffie-Hellman algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * The parameters are a prime, a base, and optionally the length in bits of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * the private value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <p>The Diffie-Hellman parameter generation accepts the size in bits of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * prime modulus and the size in bits of the random exponent as input.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * The size of the prime modulus defaults to 1024 bits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * @author Jan Luehe
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @see java.security.AlgorithmParameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @see java.security.spec.AlgorithmParameterSpec
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * @see DHParameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
public final class DHParameterGenerator
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
extends AlgorithmParameterGeneratorSpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    // The size in bits of the prime modulus
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private int primeSize = 1024;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    // The size in bits of the random exponent (private value)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private int exponentSize = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    // The source of randomness
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private SecureRandom random = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * Initializes this parameter generator for a certain keysize
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * and source of randomness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * The keysize is specified as the size in bits of the prime modulus.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * @param keysize the keysize (size of prime modulus) in bits
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * @param random the source of randomness
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    protected void engineInit(int keysize, SecureRandom random) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        if ((keysize < 512) || (keysize > 1024) || (keysize % 64 != 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            throw new InvalidParameterException("Keysize must be multiple "
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                                                + "of 64, and can only range "
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                                                + "from 512 to 1024 "
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                                                + "(inclusive)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        this.primeSize = keysize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        this.random = random;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * Initializes this parameter generator with a set of parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * generation values, which specify the size of the prime modulus and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * the size of the random exponent, both in bits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * @param params the set of parameter generation values
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * @param random the source of randomness
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * @exception InvalidAlgorithmParameterException if the given parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * generation values are inappropriate for this parameter generator
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    protected void engineInit(AlgorithmParameterSpec genParamSpec,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                              SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        throws InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            if (!(genParamSpec instanceof DHGenParameterSpec)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                throw new InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                    ("Inappropriate parameter type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            DHGenParameterSpec dhParamSpec = (DHGenParameterSpec)genParamSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            primeSize = dhParamSpec.getPrimeSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            if ((primeSize<512) || (primeSize>1024) || (primeSize%64 != 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                throw new InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                    ("Modulus size must be multiple of 64, and can only range "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                     + "from 512 to 1024 (inclusive)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            exponentSize = dhParamSpec.getExponentSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            if (exponentSize <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                throw new InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                    ("Exponent size must be greater than zero");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            // Require exponentSize < primeSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            if (exponentSize >= primeSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                throw new InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                    ("Exponent size must be less than modulus size");
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * Generates the parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * @return the new AlgorithmParameters object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     */
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        if (this.exponentSize == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            this.exponentSize = this.primeSize - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        if (this.random == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            this.random = SunJCE.RANDOM;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            AlgorithmParameterGenerator paramGen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            DSAParameterSpec dsaParamSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            paramGen = AlgorithmParameterGenerator.getInstance("DSA");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            paramGen.init(this.primeSize, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            algParams = paramGen.generateParameters();
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   143
            dsaParamSpec = algParams.getParameterSpec(DSAParameterSpec.class);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            DHParameterSpec dhParamSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            if (this.exponentSize > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                dhParamSpec = new DHParameterSpec(dsaParamSpec.getP(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                                                  dsaParamSpec.getG(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                                                  this.exponentSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                dhParamSpec = new DHParameterSpec(dsaParamSpec.getP(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                                                  dsaParamSpec.getG());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            algParams = AlgorithmParameters.getInstance("DH", "SunJCE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            algParams.init(dhParamSpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        } catch (InvalidParameterSpecException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            // this should never happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            throw new RuntimeException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        } catch (NoSuchAlgorithmException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            // this should never happen, because we provide it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            throw new RuntimeException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        } catch (NoSuchProviderException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            // this should never happen, because we provide it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            throw new RuntimeException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        return algParams;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
}