jdk/src/java.base/share/classes/javax/crypto/KeyGenerator.java
author vinnie
Thu, 25 Sep 2014 12:24:19 +0100
changeset 26736 5a93000b26cd
parent 25859 3317bb8137f4
child 26861 47dde7f5cf36
permissions -rw-r--r--
8056026: Debug security logging should print Provider used for each crypto operation Reviewed-by: mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
26736
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
     2
 * Copyright (c) 1997, 2014, 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 javax.crypto;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.*;
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.Provider.Service;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.spec.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.security.jca.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.security.jca.GetInstance.Instance;
26736
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
    36
import sun.security.util.Debug;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * This class provides the functionality of a secret (symmetric) key generator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <p>Key generators are constructed using one of the <code>getInstance</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * class methods of this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <p>KeyGenerator objects are reusable, i.e., after a key has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * generated, the same KeyGenerator object can be re-used to generate further
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * keys.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * <p>There are two ways to generate a key: in an algorithm-independent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * manner, and in an algorithm-specific manner.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * The only difference between the two is the initialization of the object:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * <li><b>Algorithm-Independent Initialization</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * <p>All key generators share the concepts of a <i>keysize</i> and a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * <i>source of randomness</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * There is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * {@link #init(int, java.security.SecureRandom) init}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * method in this KeyGenerator class that takes these two universally
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * shared types of arguments. There is also one that takes just a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * <code>keysize</code> argument, and uses the SecureRandom implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * of the highest-priority installed provider as the source of randomness
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * (or a system-provided source of randomness if none of the installed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * providers supply a SecureRandom implementation), and one that takes just a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * source of randomness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * <p>Since no other parameters are specified when you call the above
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * algorithm-independent <code>init</code> methods, it is up to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * provider what to do about the algorithm-specific parameters (if any) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * associated with each of the keys.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * <li><b>Algorithm-Specific Initialization</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * <p>For situations where a set of algorithm-specific parameters already
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * exists, there are two
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * {@link #init(java.security.spec.AlgorithmParameterSpec) init}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * methods that have an <code>AlgorithmParameterSpec</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * argument. One also has a <code>SecureRandom</code> argument, while the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * other uses the SecureRandom implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * of the highest-priority installed provider as the source of randomness
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * (or a system-provided source of randomness if none of the installed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * providers supply a SecureRandom implementation).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * <p>In case the client does not explicitly initialize the KeyGenerator
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * (via a call to an <code>init</code> method), each provider must
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * supply (and document) a default initialization.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 *
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    87
 * <p> Every implementation of the Java platform is required to support the
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    88
 * following standard <code>KeyGenerator</code> algorithms with the keysizes in
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    89
 * parentheses:
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    90
 * <ul>
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    91
 * <li><tt>AES</tt> (128)</li>
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    92
 * <li><tt>DES</tt> (56)</li>
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    93
 * <li><tt>DESede</tt> (168)</li>
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    94
 * <li><tt>HmacSHA1</tt></li>
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    95
 * <li><tt>HmacSHA256</tt></li>
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    96
 * </ul>
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    97
 * These algorithms are described in the <a href=
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    98
 * "{@docRoot}/../technotes/guides/security/StandardNames.html#KeyGenerator">
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    99
 * KeyGenerator section</a> of the
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   100
 * Java Cryptography Architecture Standard Algorithm Name Documentation.
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   101
 * Consult the release documentation for your implementation to see if any
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   102
 * other algorithms are supported.
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   103
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * @author Jan Luehe
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * @see SecretKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
public class KeyGenerator {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
26736
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   112
    private static final Debug pdebug =
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   113
                        Debug.getInstance("provider", "Provider");
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   114
    private static final boolean skipDebug =
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   115
        Debug.isOn("engine=") && !Debug.isOn("keygenerator");
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   116
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    // see java.security.KeyPairGenerator for failover notes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    private final static int I_NONE   = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    private final static int I_RANDOM = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    private final static int I_PARAMS = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    private final static int I_SIZE   = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    // The provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    private Provider provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    // The provider implementation (delegate)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    private volatile KeyGeneratorSpi spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    // The algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    private final String algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    private final Object lock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   135
    private Iterator<Service> serviceIterator;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    private int initType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    private int initKeySize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    private AlgorithmParameterSpec initParams;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    private SecureRandom initRandom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * Creates a KeyGenerator object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @param keyGenSpi the delegate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * @param provider the provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * @param algorithm the algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    protected KeyGenerator(KeyGeneratorSpi keyGenSpi, Provider provider,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                           String algorithm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        this.spi = keyGenSpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        this.provider = provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        this.algorithm = algorithm;
26736
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   154
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   155
        if (!skipDebug && pdebug != null) {
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   156
            pdebug.println("KeyGenerator." + algorithm + " algorithm from: " +
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   157
                this.provider.getName());
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   158
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    private KeyGenerator(String algorithm) throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        this.algorithm = algorithm;
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   163
        List<Service> list =
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   164
                GetInstance.getServices("KeyGenerator", algorithm);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        serviceIterator = list.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        initType = I_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        // fetch and instantiate initial spi
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        if (nextSpi(null, false) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            throw new NoSuchAlgorithmException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                (algorithm + " KeyGenerator not available");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        }
26736
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   172
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   173
        if (!skipDebug && pdebug != null) {
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   174
            pdebug.println("KeyGenerator." + algorithm + " algorithm from: " +
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   175
                this.provider.getName());
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   176
        }
2
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
     * Returns the algorithm name of this <code>KeyGenerator</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * <p>This is the same name that was specified in one of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * <code>getInstance</code> calls that created this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * <code>KeyGenerator</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * @return the algorithm name of this <code>KeyGenerator</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    public final String getAlgorithm() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        return this.algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * Returns a <code>KeyGenerator</code> object that generates secret keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * for the specified algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * <p> This method traverses the list of registered security Providers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * starting with the most preferred Provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * A new KeyGenerator object encapsulating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * KeyGeneratorSpi implementation from the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * Provider that supports the specified algorithm is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * <p> Note that the list of registered providers may be retrieved via
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * the {@link Security#getProviders() Security.getProviders()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * @param algorithm the standard name of the requested key algorithm.
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   206
     * See the KeyGenerator section in the <a href=
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   207
     * "{@docRoot}/../technotes/guides/security/StandardNames.html#KeyGenerator">
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   208
     * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * for information about standard algorithm names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * @return the new <code>KeyGenerator</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * @exception NullPointerException if the specified algorithm is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * @exception NoSuchAlgorithmException if no Provider supports a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     *          KeyGeneratorSpi implementation for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     *          specified algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * @see java.security.Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    public static final KeyGenerator getInstance(String algorithm)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        return new KeyGenerator(algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * Returns a <code>KeyGenerator</code> object that generates secret keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * for the specified algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * <p> A new KeyGenerator object encapsulating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * KeyGeneratorSpi implementation from the specified provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * is returned.  The specified provider must be registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * in the security provider list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * <p> Note that the list of registered providers may be retrieved via
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * the {@link Security#getProviders() Security.getProviders()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * @param algorithm the standard name of the requested key algorithm.
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   239
     * See the KeyGenerator section in the <a href=
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   240
     * "{@docRoot}/../technotes/guides/security/StandardNames.html#KeyGenerator">
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   241
     * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * for information about standard algorithm names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * @param provider the name of the provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * @return the new <code>KeyGenerator</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * @exception NullPointerException if the specified algorithm is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * @exception NoSuchAlgorithmException if a KeyGeneratorSpi
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     *          implementation for the specified algorithm is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     *          available from the specified provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * @exception NoSuchProviderException if the specified provider is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     *          registered in the security provider list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * @exception IllegalArgumentException if the <code>provider</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     *          is null or empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * @see java.security.Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    public static final KeyGenerator getInstance(String algorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            String provider) throws NoSuchAlgorithmException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            NoSuchProviderException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        Instance instance = JceSecurity.getInstance("KeyGenerator",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                KeyGeneratorSpi.class, algorithm, provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        return new KeyGenerator((KeyGeneratorSpi)instance.impl,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                instance.provider, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * Returns a <code>KeyGenerator</code> object that generates secret keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * for the specified algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * <p> A new KeyGenerator object encapsulating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * KeyGeneratorSpi implementation from the specified Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * object is returned.  Note that the specified Provider object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * does not have to be registered in the provider list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * @param algorithm the standard name of the requested key algorithm.
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   281
     * See the KeyGenerator section in the <a href=
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   282
     * "{@docRoot}/../technotes/guides/security/StandardNames.html#KeyGenerator">
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   283
     * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * for information about standard algorithm names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * @param provider the provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * @return the new <code>KeyGenerator</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * @exception NullPointerException if the specified algorithm is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * @exception NoSuchAlgorithmException if a KeyGeneratorSpi
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     *          implementation for the specified algorithm is not available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     *          from the specified Provider object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * @exception IllegalArgumentException if the <code>provider</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     *          is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * @see java.security.Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    public static final KeyGenerator getInstance(String algorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            Provider provider) throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        Instance instance = JceSecurity.getInstance("KeyGenerator",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                KeyGeneratorSpi.class, algorithm, provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        return new KeyGenerator((KeyGeneratorSpi)instance.impl,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                instance.provider, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * Returns the provider of this <code>KeyGenerator</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * @return the provider of this <code>KeyGenerator</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    public final Provider getProvider() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            disableFailover();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            return provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * Update the active spi of this class and return the next
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * implementation for failover. If no more implemenations are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * available, this method returns null. However, the active spi of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * this class is never set to null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    private KeyGeneratorSpi nextSpi(KeyGeneratorSpi oldSpi,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            boolean reinit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            // somebody else did a failover concurrently
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            // try that spi now
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            if ((oldSpi != null) && (oldSpi != spi)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                return spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            if (serviceIterator == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            while (serviceIterator.hasNext()) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   339
                Service s = serviceIterator.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                if (JceSecurity.canUseProvider(s.getProvider()) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                    Object inst = s.newInstance(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                    // ignore non-spis
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                    if (inst instanceof KeyGeneratorSpi == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                    KeyGeneratorSpi spi = (KeyGeneratorSpi)inst;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                    if (reinit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                        if (initType == I_SIZE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                            spi.engineInit(initKeySize, initRandom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                        } else if (initType == I_PARAMS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                            spi.engineInit(initParams, initRandom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                        } else if (initType == I_RANDOM) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                            spi.engineInit(initRandom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                        } else if (initType != I_NONE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                            throw new AssertionError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                                ("KeyGenerator initType: " + initType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                    provider = s.getProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                    this.spi = spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                    return spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                    // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            disableFailover();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    void disableFailover() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        serviceIterator = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        initType = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        initParams = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        initRandom = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * Initializes this key generator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * @param random the source of randomness for this generator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    public final void init(SecureRandom random) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        if (serviceIterator == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            spi.engineInit(random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        RuntimeException failure = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        KeyGeneratorSpi mySpi = spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                mySpi.engineInit(random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                initType = I_RANDOM;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                initKeySize = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                initParams = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                initRandom = random;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            } catch (RuntimeException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                if (failure == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                    failure = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                mySpi = nextSpi(mySpi, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        } while (mySpi != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        throw failure;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * Initializes this key generator with the specified parameter set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * <p> If this key generator requires any random bytes, it will get them
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * using the
18771
9dadb0719cea 8019772: Fix doclint issues in javax.crypto and javax.security subpackages
juh
parents: 10336
diff changeset
   416
     * {@link java.security.SecureRandom}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * implementation of the highest-priority installed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * provider as the source of randomness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * (If none of the installed providers supply an implementation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * SecureRandom, a system-provided source of randomness will be used.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * @param params the key generation parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * @exception InvalidAlgorithmParameterException if the given parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * are inappropriate for this key generator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    public final void init(AlgorithmParameterSpec params)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        throws InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        init(params, JceSecurity.RANDOM);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * Initializes this key generator with the specified parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * set and a user-provided source of randomness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * @param params the key generation parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * @param random the source of randomness for this key generator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * @exception InvalidAlgorithmParameterException if <code>params</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * inappropriate for this key generator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    public final void init(AlgorithmParameterSpec params, SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        throws InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        if (serviceIterator == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            spi.engineInit(params, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        Exception failure = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        KeyGeneratorSpi mySpi = spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                mySpi.engineInit(params, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                initType = I_PARAMS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                initKeySize = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                initParams = params;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                initRandom = random;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                if (failure == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                    failure = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                mySpi = nextSpi(mySpi, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        } while (mySpi != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        if (failure instanceof InvalidAlgorithmParameterException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            throw (InvalidAlgorithmParameterException)failure;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        if (failure instanceof RuntimeException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            throw (RuntimeException)failure;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        throw new InvalidAlgorithmParameterException("init() failed", failure);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * Initializes this key generator for a certain keysize.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * <p> If this key generator requires any random bytes, it will get them
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * using the
18771
9dadb0719cea 8019772: Fix doclint issues in javax.crypto and javax.security subpackages
juh
parents: 10336
diff changeset
   481
     * {@link java.security.SecureRandom}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * implementation of the highest-priority installed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * provider as the source of randomness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * (If none of the installed providers supply an implementation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * SecureRandom, a system-provided source of randomness will be used.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * @param keysize the keysize. This is an algorithm-specific metric,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * specified in number of bits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * @exception InvalidParameterException if the keysize is wrong or not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * supported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    public final void init(int keysize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        init(keysize, JceSecurity.RANDOM);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * Initializes this key generator for a certain keysize, using a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * user-provided source of randomness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * @param keysize the keysize. This is an algorithm-specific metric,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     * specified in number of bits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     * @param random the source of randomness for this key generator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * @exception InvalidParameterException if the keysize is wrong or not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * supported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    public final void init(int keysize, SecureRandom random) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        if (serviceIterator == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            spi.engineInit(keysize, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        RuntimeException failure = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        KeyGeneratorSpi mySpi = spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                mySpi.engineInit(keysize, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                initType = I_SIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                initKeySize = keysize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                initParams = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                initRandom = random;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
            } catch (RuntimeException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                if (failure == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                    failure = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                mySpi = nextSpi(mySpi, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        } while (mySpi != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        throw failure;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * Generates a secret key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * @return the new key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    public final SecretKey generateKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        if (serviceIterator == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            return spi.engineGenerateKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        RuntimeException failure = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        KeyGeneratorSpi mySpi = spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                return mySpi.engineGenerateKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            } catch (RuntimeException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                if (failure == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                    failure = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                mySpi = nextSpi(mySpi, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        } while (mySpi != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        throw failure;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
}