Home | History | Annotate | Download | only in crypto
      1 /*
      2  *  Licensed to the Apache Software Foundation (ASF) under one or more
      3  *  contributor license agreements.  See the NOTICE file distributed with
      4  *  this work for additional information regarding copyright ownership.
      5  *  The ASF licenses this file to You under the Apache License, Version 2.0
      6  *  (the "License"); you may not use this file except in compliance with
      7  *  the License.  You may obtain a copy of the License at
      8  *
      9  *     http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  *  Unless required by applicable law or agreed to in writing, software
     12  *  distributed under the License is distributed on an "AS IS" BASIS,
     13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  *  See the License for the specific language governing permissions and
     15  *  limitations under the License.
     16  */
     17 
     18 package javax.crypto;
     19 
     20 import java.security.InvalidAlgorithmParameterException;
     21 import java.security.NoSuchAlgorithmException;
     22 import java.security.NoSuchProviderException;
     23 import java.security.Provider;
     24 import java.security.SecureRandom;
     25 import java.security.Security;
     26 import java.security.spec.AlgorithmParameterSpec;
     27 import org.apache.harmony.security.fortress.Engine;
     28 
     29 
     30 /**
     31  * This class provides the public API for generating symmetric cryptographic
     32  * keys.
     33  */
     34 public class KeyGenerator {
     35 
     36     // Used to access common engine functionality
     37     private static final Engine engine = new Engine("KeyGenerator");
     38 
     39     // Store SecureRandom
     40     private static final SecureRandom rndm = new SecureRandom();
     41 
     42     // Store used provider
     43     private final Provider provider;
     44 
     45     // Store used spi implementation
     46     private final KeyGeneratorSpi spiImpl;
     47 
     48     // Store used algorithm name
     49     private final String algorithm;
     50 
     51     /**
     52      * Creates a new {@code KeyGenerator} instance.
     53      *
     54      * @param keyGenSpi
     55      *            the implementation delegate.
     56      * @param provider
     57      *            the implementation provider.
     58      * @param algorithm
     59      *            the name of the algorithm.
     60      */
     61     protected KeyGenerator(KeyGeneratorSpi keyGenSpi, Provider provider,
     62             String algorithm) {
     63         this.provider = provider;
     64         this.algorithm = algorithm;
     65         this.spiImpl = keyGenSpi;
     66     }
     67 
     68     /**
     69      * Returns the name of the key generation algorithm.
     70      *
     71      * @return the name of the key generation algorithm.
     72      */
     73     public final String getAlgorithm() {
     74         return algorithm;
     75     }
     76 
     77     /**
     78      * Returns the provider of this {@code KeyGenerator} instance.
     79      *
     80      * @return the provider of this {@code KeyGenerator} instance.
     81      */
     82     public final Provider getProvider() {
     83         return provider;
     84     }
     85 
     86     /**
     87      * Creates a new {@code KeyGenerator} instance that provides the specified
     88      * key algorithm,
     89      *
     90      * @param algorithm
     91      *            the name of the requested key algorithm
     92      * @return the new {@code KeyGenerator} instance.
     93      * @throws NoSuchAlgorithmException
     94      *             if the specified algorithm is not available by any provider.
     95      * @throws NullPointerException
     96      *             if {@code algorithm} is {@code null}.
     97      */
     98     public static final KeyGenerator getInstance(String algorithm)
     99             throws NoSuchAlgorithmException {
    100         if (algorithm == null) {
    101             throw new NullPointerException();
    102         }
    103         synchronized (engine) {
    104             engine.getInstance(algorithm, null);
    105             return new KeyGenerator((KeyGeneratorSpi) engine.spi, engine.provider,
    106                     algorithm);
    107         }
    108     }
    109 
    110     /**
    111      * Creates a new {@code KeyGenerator} instance that provides the specified
    112      * key algorithm from the specified provider.
    113      *
    114      * @param algorithm
    115      *            the name of the requested key algorithm.
    116      * @param provider
    117      *            the name of the provider that is providing the algorithm.
    118      * @return the new {@code KeyGenerator} instance.
    119      * @throws NoSuchAlgorithmException
    120      *             if the specified algorithm is not provided by the specified
    121      *             provider.
    122      * @throws NoSuchProviderException
    123      *             if the specified provider is not available.
    124      * @throws IllegalArgumentException
    125      *             if the specified provider is name is {@code null} or empty.
    126      * @throws NullPointerException
    127      *             if the specified algorithm name is {@code null}.
    128      */
    129     public static final KeyGenerator getInstance(String algorithm,
    130             String provider) throws NoSuchAlgorithmException, NoSuchProviderException {
    131         if (provider == null || provider.isEmpty()) {
    132             throw new IllegalArgumentException("Provider is null or empty");
    133         }
    134         Provider impProvider = Security.getProvider(provider);
    135         if (impProvider == null) {
    136             throw new NoSuchProviderException(provider);
    137         }
    138         return getInstance(algorithm, impProvider);
    139     }
    140 
    141     /**
    142      * Creates a new {@code KeyGenerator} instance that provides the specified
    143      * key algorithm from the specified provider.
    144      *
    145      * @param algorithm
    146      *            the name of the requested key algorithm.
    147      * @param provider
    148      *            the provider that is providing the algorithm
    149      * @return the new {@code KeyGenerator} instance.
    150      * @throws NoSuchAlgorithmException
    151      *             if the specified algorithm is not provided by the specified
    152      *             provider.
    153      * @throws IllegalArgumentException
    154      *             if the specified provider is {@code null}.
    155      * @throws NullPointerException
    156      *             if the specified algorithm name is {@code null}.
    157      */
    158     public static final KeyGenerator getInstance(String algorithm,
    159             Provider provider) throws NoSuchAlgorithmException {
    160         if (provider == null) {
    161             throw new IllegalArgumentException("provider == null");
    162         }
    163         if (algorithm == null) {
    164             throw new NullPointerException();
    165         }
    166         synchronized (engine) {
    167             engine.getInstance(algorithm, provider, null);
    168             return new KeyGenerator((KeyGeneratorSpi) engine.spi, provider,
    169                     algorithm);
    170         }
    171     }
    172 
    173     /**
    174      * Generates a secret key.
    175      *
    176      * @return the generated secret key.
    177      */
    178     public final SecretKey generateKey() {
    179         return spiImpl.engineGenerateKey();
    180     }
    181 
    182     /**
    183      * Initializes this {@code KeyGenerator} instance with the specified
    184      * algorithm parameters.
    185      *
    186      * @param params
    187      *            the parameters for the key generation algorithm.
    188      * @throws InvalidAlgorithmParameterException
    189      *             if the parameters cannot be used to initialize this key
    190      *             generator algorithm.
    191      */
    192     public final void init(AlgorithmParameterSpec params)
    193             throws InvalidAlgorithmParameterException {
    194         spiImpl.engineInit(params, rndm);//new SecureRandom());
    195     }
    196 
    197     /**
    198      * Initializes this {@code KeyGenerator} instance with the specified
    199      * algorithm parameters and randomness source.
    200      *
    201      * @param params
    202      *            the parameters for the key generation algorithm.
    203      * @param random
    204      *            the randomness source for any random bytes.
    205      * @throws InvalidAlgorithmParameterException
    206      *             if the parameters cannot be uses to initialize this key
    207      *             generator algorithm.
    208      */
    209     public final void init(AlgorithmParameterSpec params, SecureRandom random)
    210             throws InvalidAlgorithmParameterException {
    211         spiImpl.engineInit(params, random);
    212     }
    213 
    214     /**
    215      * Initializes this {@code KeyGenerator} instance for the specified key size
    216      * (in bits).
    217      *
    218      * @param keysize
    219      *            the size of the key (in bits).
    220      */
    221     public final void init(int keysize) {
    222         spiImpl.engineInit(keysize, rndm);//new SecureRandom());
    223     }
    224 
    225     /**
    226      * Initializes this {@code KeyGenerator} instance for the specified key size
    227      * (in bits) using the specified randomness source.
    228      *
    229      * @param keysize
    230      *            the size of the key (in bits).
    231      * @param random
    232      *            the randomness source for any random bytes.
    233      */
    234     public final void init(int keysize, SecureRandom random) {
    235         spiImpl.engineInit(keysize, random);
    236     }
    237 
    238     /**
    239      * Initializes this {@code KeyGenerator} with the specified randomness
    240      * source.
    241      *
    242      * @param random
    243      *            the randomness source for any random bytes.
    244      */
    245     public final void init(SecureRandom random) {
    246         spiImpl.engineInit(random);
    247     }
    248 }
    249