jdk/src/share/classes/sun/security/rsa/RSAKeyPairGenerator.java
author malenkov
Tue, 29 Oct 2013 17:01:06 +0400
changeset 21278 ef8a3a2a72f2
parent 5506 202f599c92aa
child 23010 6dadb192ad81
permissions -rw-r--r--
8022746: List of spelling errors in API doc Reviewed-by: alexsch, smarks
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: 2596
diff changeset
     2
 * Copyright (c) 2003, 2008, 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: 2596
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: 2596
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: 2596
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2596
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2596
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.rsa;
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.spec.AlgorithmParameterSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.spec.RSAKeyGenParameterSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.security.jca.JCAUtil;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * RSA keypair generation. Standard algorithm, minimum key length 512 bit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * We generate two random primes until we find two where phi is relative
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * prime to the public exponent. Default exponent is 65537. It has only bit 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * and bit 4 set, which makes it particularly efficient.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * @since   1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * @author  Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
public final class RSAKeyPairGenerator extends KeyPairGeneratorSpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    // public exponent to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private BigInteger publicExponent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
    50
    // size of the key to generate, >= RSAKeyFactory.MIN_MODLEN
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private int keySize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    // PRNG to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private SecureRandom random;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    public RSAKeyPairGenerator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        // initialize to default in case the app does not call initialize()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        initialize(1024, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    // initialize the generator. See JCA doc
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    public void initialize(int keySize, SecureRandom random) {
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
    63
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
    64
        // do not allow unreasonably small or large key sizes,
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
    65
        // probably user error
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
    66
        try {
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
    67
            RSAKeyFactory.checkKeyLengths(keySize, RSAKeyGenParameterSpec.F4,
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
    68
                512, 64 * 1024);
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
    69
        } catch (InvalidKeyException e) {
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
    70
            throw new InvalidParameterException(e.getMessage());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        }
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
    72
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        this.keySize = keySize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        this.random = random;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        this.publicExponent = RSAKeyGenParameterSpec.F4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    // second initialize method. See JCA doc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    public void initialize(AlgorithmParameterSpec params, SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            throws InvalidAlgorithmParameterException {
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
    81
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        if (params instanceof RSAKeyGenParameterSpec == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            throw new InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                ("Params must be instance of RSAKeyGenParameterSpec");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        }
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
    86
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        RSAKeyGenParameterSpec rsaSpec = (RSAKeyGenParameterSpec)params;
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
    88
        int tmpKeySize = rsaSpec.getKeysize();
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
    89
        BigInteger tmpPublicExponent = rsaSpec.getPublicExponent();
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
    90
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
    91
        if (tmpPublicExponent == null) {
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
    92
            tmpPublicExponent = RSAKeyGenParameterSpec.F4;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        } else {
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
    94
            if (tmpPublicExponent.compareTo(RSAKeyGenParameterSpec.F0) < 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                throw new InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                        ("Public exponent must be 3 or larger");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            }
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
    98
            if (tmpPublicExponent.bitLength() > tmpKeySize) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                throw new InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                        ("Public exponent must be smaller than key size");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        }
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   103
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   104
        // do not allow unreasonably large key sizes, probably user error
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   105
        try {
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   106
            RSAKeyFactory.checkKeyLengths(tmpKeySize, tmpPublicExponent,
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   107
                512, 64 * 1024);
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   108
        } catch (InvalidKeyException e) {
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   109
            throw new InvalidAlgorithmParameterException(
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   110
                "Invalid key sizes", e);
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   111
        }
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   112
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   113
        this.keySize = tmpKeySize;
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   114
        this.publicExponent = tmpPublicExponent;
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   115
        this.random = random;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    // generate the keypair. See JCA doc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    public KeyPair generateKeyPair() {
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 5506
diff changeset
   120
        // accommodate odd key sizes in case anybody wants to use them
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        int lp = (keySize + 1) >> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        int lq = keySize - lp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        if (random == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            random = JCAUtil.getSecureRandom();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        BigInteger e = publicExponent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            // generate two random primes of size lp/lq
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            BigInteger p = BigInteger.probablePrime(lp, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            BigInteger q, n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                q = BigInteger.probablePrime(lq, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                // convention is for p > q
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                if (p.compareTo(q) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                    BigInteger tmp = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                    p = q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                    q = tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                // modulus n = p * q
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                n = p.multiply(q);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                // even with correctly sized p and q, there is a chance that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                // n will be one bit short. re-generate the smaller prime if so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            } while (n.bitLength() < keySize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            // phi = (p - 1) * (q - 1) must be relative prime to e
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            // otherwise RSA just won't work ;-)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            BigInteger p1 = p.subtract(BigInteger.ONE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            BigInteger q1 = q.subtract(BigInteger.ONE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            BigInteger phi = p1.multiply(q1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            // generate new p and q until they work. typically
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            // the first try will succeed when using F4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            if (e.gcd(phi).equals(BigInteger.ONE) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            // private exponent d is the inverse of e mod phi
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            BigInteger d = e.modInverse(phi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            // 1st prime exponent pe = d mod (p - 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            BigInteger pe = d.mod(p1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            // 2nd prime exponent qe = d mod (q - 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            BigInteger qe = d.mod(q1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            // crt coefficient coeff is the inverse of q mod p
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            BigInteger coeff = q.modInverse(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                PublicKey publicKey = new RSAPublicKeyImpl(n, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                PrivateKey privateKey =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                        new RSAPrivateCrtKeyImpl(n, e, d, p, q, pe, qe, coeff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                return new KeyPair(publicKey, privateKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            } catch (InvalidKeyException exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                // invalid key exception only thrown for keys < 512 bit,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                // will not happen here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                throw new RuntimeException(exc);
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
}