jdk/src/share/classes/com/sun/crypto/provider/DHKeyPairGenerator.java
author valeriep
Tue, 08 May 2012 17:57:48 -0700
changeset 12685 8a448b5b9006
parent 5506 202f599c92aa
child 13672 604588823b5a
permissions -rw-r--r--
4963723: Implement SHA-224 Summary: Add support for SHA-224, SHA224withRSA, SHA224withECDSA, HmacSHA224 and OAEPwithSHA-224AndMGF1Padding. Reviewed-by: vinnie
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1997, 2007, 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.math.BigInteger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.spec.AlgorithmParameterSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.spec.InvalidParameterSpecException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.crypto.spec.DHParameterSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.crypto.spec.DHGenParameterSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.security.provider.ParameterCache;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * This class represents the key pair generator for Diffie-Hellman key pairs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <p>This key pair generator may be initialized in two different ways:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <li>By providing the size in bits of the prime modulus -
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * This will be used to create a prime modulus and base generator, which will
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * then be used to create the Diffie-Hellman key pair. The default size of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * prime modulus is 1024 bits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <li>By providing a prime modulus and base generator
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * </ul>
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.KeyPairGenerator
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
public final class DHKeyPairGenerator extends KeyPairGeneratorSpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    // parameters to use or null if not specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private DHParameterSpec params;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    // The size in bits of the prime modulus
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private int pSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    // The size in bits of the random exponent (private value)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private int lSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    // The source of randomness
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private SecureRandom random;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    public DHKeyPairGenerator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        initialize(1024, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * Initializes this key pair generator for a certain keysize and source of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * randomness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * The keysize is specified as the size in bits of the prime modulus.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * @param keysize the keysize (size of prime modulus) in bits
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * @param random the source of randomness
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    public void initialize(int keysize, SecureRandom random) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        if ((keysize < 512) || (keysize > 1024) || (keysize % 64 != 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            throw new InvalidParameterException("Keysize must be multiple "
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                                                + "of 64, and can only range "
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                                                + "from 512 to 1024 "
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                                                + "(inclusive)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        this.pSize = keysize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        this.lSize = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        this.random = random;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        this.params = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * Initializes this key pair generator for the specified parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * set and source of randomness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * <p>The given parameter set contains the prime modulus, the base
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * generator, and optionally the requested size in bits of the random
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * exponent (private value).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * @param params the parameter set used to generate the key pair
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * @param random the source of randomness
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * @exception InvalidAlgorithmParameterException if the given parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * are inappropriate for this key pair generator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    public void initialize(AlgorithmParameterSpec algParams,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            SecureRandom random) throws InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        if (!(algParams instanceof DHParameterSpec)){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            throw new InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                ("Inappropriate parameter type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        params = (DHParameterSpec)algParams;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        pSize = params.getP().bitLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        if ((pSize < 512) || (pSize > 1024) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            (pSize % 64 != 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            throw new InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                ("Prime size must be multiple of 64, and can only range "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                 + "from 512 to 1024 (inclusive)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        // exponent size is optional, could be 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        lSize = params.getL();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        // Require exponentSize < primeSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        if ((lSize != 0) && (lSize > pSize)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            throw new InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                ("Exponent size must not be larger than modulus size");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        this.random = random;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * Generates a key pair.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * @return the new key pair
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    public KeyPair generateKeyPair() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        if (random == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            random = SunJCE.RANDOM;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        if (params == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                params = ParameterCache.getDHParameterSpec(pSize, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                // should never happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                throw new ProviderException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        BigInteger p = params.getP();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        BigInteger g = params.getG();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        if (lSize <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            // use an exponent size of (pSize / 2) but at least 384 bits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            lSize = Math.max(384, pSize >> 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            // if lSize is larger than pSize, limit by pSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            lSize = Math.min(lSize, pSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        BigInteger x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        BigInteger pMinus2 = p.subtract(BigInteger.valueOf(2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        // Handbook of Applied Cryptography:  Menezes, et.al.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        // Repeat if the following does not hold:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        //     1 <= x <= p-2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            // generate random x up to 2^lSize bits long
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            x = new BigInteger(lSize, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        } while ((x.compareTo(BigInteger.ONE) < 0) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            ((x.compareTo(pMinus2) > 0)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        // calculate public value y
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        BigInteger y = g.modPow(x, p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        DHPublicKey pubKey = new DHPublicKey(y, p, g, lSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        DHPrivateKey privKey = new DHPrivateKey(x, p, g, lSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        return new KeyPair(pubKey, privKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
}