jdk/src/share/classes/com/sun/crypto/provider/KeyGeneratorCore.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 3353 ddbd63234844
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2003-2007 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            random = SunJCE.RANDOM;
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    // nested static class for the HmacSHA256 key generator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    public static final class HmacSHA256KG extends KeyGeneratorSpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        private final KeyGeneratorCore core;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        public HmacSHA256KG() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            SunJCE.ensureIntegrity(getClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            core = new KeyGeneratorCore("HmacSHA256", 256);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        protected void engineInit(SecureRandom random) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            core.implInit(random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        protected void engineInit(AlgorithmParameterSpec params,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                SecureRandom random) throws InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            core.implInit(params, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        protected void engineInit(int keySize, SecureRandom random) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            core.implInit(keySize, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        protected SecretKey engineGenerateKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            return core.implGenerateKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    // nested static class for the HmacSHA384 key generator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    public static final class HmacSHA384KG extends KeyGeneratorSpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        private final KeyGeneratorCore core;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        public HmacSHA384KG() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            SunJCE.ensureIntegrity(getClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            core = new KeyGeneratorCore("HmacSHA384", 384);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        protected void engineInit(SecureRandom random) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            core.implInit(random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        protected void engineInit(AlgorithmParameterSpec params,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                SecureRandom random) throws InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            core.implInit(params, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        protected void engineInit(int keySize, SecureRandom random) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            core.implInit(keySize, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        protected SecretKey engineGenerateKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            return core.implGenerateKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    // nested static class for the HmacSHA384 key generator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    public static final class HmacSHA512KG extends KeyGeneratorSpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        private final KeyGeneratorCore core;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        public HmacSHA512KG() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            SunJCE.ensureIntegrity(getClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            core = new KeyGeneratorCore("HmacSHA512", 512);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        protected void engineInit(SecureRandom random) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            core.implInit(random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        protected void engineInit(AlgorithmParameterSpec params,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                SecureRandom random) throws InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            core.implInit(params, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        protected void engineInit(int keySize, SecureRandom random) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            core.implInit(keySize, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        protected SecretKey engineGenerateKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            return core.implGenerateKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    // nested static class for the RC2 key generator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    public static final class RC2KeyGenerator extends KeyGeneratorSpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        private final KeyGeneratorCore core;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        public RC2KeyGenerator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            SunJCE.ensureIntegrity(getClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            core = new KeyGeneratorCore("RC2", 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 RC2"
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
    // nested static class for the ARCFOUR (RC4) key generator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    public static final class ARCFOURKeyGenerator extends KeyGeneratorSpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        private final KeyGeneratorCore core;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        public ARCFOURKeyGenerator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            SunJCE.ensureIntegrity(getClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            core = new KeyGeneratorCore("ARCFOUR", 128);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        protected void engineInit(SecureRandom random) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            core.implInit(random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        protected void engineInit(AlgorithmParameterSpec params,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                SecureRandom random) throws InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            core.implInit(params, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        protected void engineInit(int keySize, SecureRandom random) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            if ((keySize < 40) || (keySize > 1024)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                throw new InvalidParameterException("Key length for ARCFOUR"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                    + " must be between 40 and 1024 bits");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            core.implInit(keySize, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        protected SecretKey engineGenerateKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            return core.implGenerateKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
}