jdk/src/share/classes/com/sun/crypto/provider/KeyGeneratorCore.java
author valeriep
Tue, 08 Jan 2013 11:55:21 -0800
changeset 15010 ec6b49ce42b1
parent 12685 8a448b5b9006
permissions -rw-r--r--
8004044: Lazily instantiate SunJCE.RANDOM Summary: Replace the static initialization of SunJCE.RANDOM object w/ lazy initialization Reviewed-by: mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
15010
ec6b49ce42b1 8004044: Lazily instantiate SunJCE.RANDOM
valeriep
parents: 12685
diff changeset
     2
 * Copyright (c) 2003, 2013, 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: 3353
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: 3353
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: 3353
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3353
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3353
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.AlgorithmParameterSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.crypto.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.crypto.spec.SecretKeySpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * KeyGeneratore core implementation and individual key generator
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * implementations. Because of US export regulations, we cannot use
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * subclassing to achieve code sharing between the key generator
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * implementations for our various algorithms. Instead, we have the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * core implementation in this KeyGeneratorCore class, which is used
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * by the individual implementations. See those further down in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * @since   1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * @author  Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
final class KeyGeneratorCore {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    // algorithm name to use for the generator keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private final String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    // default key size in bits
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private final int defaultKeySize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    // current key size in bits
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private int keySize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    // PRNG to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private SecureRandom random;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * Construct a new KeyGeneratorCore object with the specified name
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * and defaultKeySize. Initialize to default key size in case the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * application does not call any of the init() methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    KeyGeneratorCore(String name, int defaultKeySize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        this.defaultKeySize = defaultKeySize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        implInit(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    // implementation for engineInit(), see JCE doc
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    // reset keySize to default
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    void implInit(SecureRandom random) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        this.keySize = defaultKeySize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        this.random = random;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    // implementation for engineInit(), see JCE doc
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    // we do not support any parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    void implInit(AlgorithmParameterSpec params, SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            throws InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        throw new InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            (name + " key generation does not take any parameters");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    // implementation for engineInit(), see JCE doc
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    // we enforce a general 40 bit minimum key size for security
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    void implInit(int keysize, SecureRandom random) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        if (keysize < 40) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            throw new InvalidParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                ("Key length must be at least 40 bits");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        this.keySize = keysize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        this.random = random;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    // implementation for engineInit(), see JCE doc
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    // generate the key
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    SecretKey implGenerateKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        if (random == null) {
15010
ec6b49ce42b1 8004044: Lazily instantiate SunJCE.RANDOM
valeriep
parents: 12685
diff changeset
   101
            random = SunJCE.getRandom();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        byte[] b = new byte[(keySize + 7) >> 3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        random.nextBytes(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        return new SecretKeySpec(b, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
12685
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
   108
    // nested static classes for the HmacSHA-2 family of key generator
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
   109
    abstract static class HmacSHA2KG extends KeyGeneratorSpi {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        private final KeyGeneratorCore core;
12685
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
   111
        protected HmacSHA2KG(String algoName, int len) {
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
   112
            core = new KeyGeneratorCore(algoName, len);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        protected void engineInit(SecureRandom random) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            core.implInit(random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        protected void engineInit(AlgorithmParameterSpec params,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                SecureRandom random) throws InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            core.implInit(params, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        protected void engineInit(int keySize, SecureRandom random) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            core.implInit(keySize, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        protected SecretKey engineGenerateKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            return core.implGenerateKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
12685
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
   128
        public static final class SHA224 extends HmacSHA2KG {
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
   129
            public SHA224() {
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
   130
                super("HmacSHA224", 224);
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
   131
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        }
12685
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
   133
        public static final class SHA256 extends HmacSHA2KG {
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
   134
            public SHA256() {
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
   135
                super("HmacSHA256", 256);
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
   136
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
12685
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
   138
        public static final class SHA384 extends HmacSHA2KG {
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
   139
            public SHA384() {
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
   140
                super("HmacSHA384", 384);
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
   141
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        }
12685
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
   143
        public static final class SHA512 extends HmacSHA2KG {
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
   144
            public SHA512() {
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
   145
                super("HmacSHA512", 512);
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
   146
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    // nested static class for the RC2 key generator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    public static final class RC2KeyGenerator extends KeyGeneratorSpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        private final KeyGeneratorCore core;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        public RC2KeyGenerator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            core = new KeyGeneratorCore("RC2", 128);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        protected void engineInit(SecureRandom random) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            core.implInit(random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        protected void engineInit(AlgorithmParameterSpec params,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                SecureRandom random) throws InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            core.implInit(params, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        protected void engineInit(int keySize, SecureRandom random) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            if ((keySize < 40) || (keySize > 1024)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                throw new InvalidParameterException("Key length for RC2"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                    + " must be between 40 and 1024 bits");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            core.implInit(keySize, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        protected SecretKey engineGenerateKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            return core.implGenerateKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    // nested static class for the ARCFOUR (RC4) key generator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    public static final class ARCFOURKeyGenerator extends KeyGeneratorSpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        private final KeyGeneratorCore core;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        public ARCFOURKeyGenerator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            core = new KeyGeneratorCore("ARCFOUR", 128);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        protected void engineInit(SecureRandom random) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            core.implInit(random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        protected void engineInit(AlgorithmParameterSpec params,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                SecureRandom random) throws InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            core.implInit(params, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        protected void engineInit(int keySize, SecureRandom random) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            if ((keySize < 40) || (keySize > 1024)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                throw new InvalidParameterException("Key length for ARCFOUR"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                    + " must be between 40 and 1024 bits");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            core.implInit(keySize, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        protected SecretKey engineGenerateKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            return core.implGenerateKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
}