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 RANDOM = 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 */ KeyGenerator(KeyGeneratorSpi keyGenSpi, Provider provider, String algorithm)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 */ getAlgorithm()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 */ getProvider()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 */ getInstance(String algorithm)98 public static final KeyGenerator getInstance(String algorithm) 99 throws NoSuchAlgorithmException { 100 if (algorithm == null) { 101 throw new NullPointerException("algorithm == null"); 102 } 103 Engine.SpiAndProvider sap = ENGINE.getInstance(algorithm, null); 104 return new KeyGenerator((KeyGeneratorSpi) sap.spi, sap.provider, algorithm); 105 } 106 107 /** 108 * Creates a new {@code KeyGenerator} instance that provides the specified 109 * key algorithm from the specified provider. 110 * 111 * @param algorithm 112 * the name of the requested key algorithm. 113 * @param provider 114 * the name of the provider that is providing the algorithm. 115 * @return the new {@code KeyGenerator} instance. 116 * @throws NoSuchAlgorithmException 117 * if the specified algorithm is not provided by the specified 118 * provider. 119 * @throws NoSuchProviderException 120 * if the specified provider is not available. 121 * @throws IllegalArgumentException 122 * if the specified provider is name is {@code null} or empty. 123 * @throws NullPointerException 124 * if the specified algorithm name is {@code null}. 125 */ getInstance(String algorithm, String provider)126 public static final KeyGenerator getInstance(String algorithm, 127 String provider) throws NoSuchAlgorithmException, NoSuchProviderException { 128 if (provider == null || provider.isEmpty()) { 129 throw new IllegalArgumentException("Provider is null or empty"); 130 } 131 Provider impProvider = Security.getProvider(provider); 132 if (impProvider == null) { 133 throw new NoSuchProviderException(provider); 134 } 135 return getInstance(algorithm, impProvider); 136 } 137 138 /** 139 * Creates a new {@code KeyGenerator} instance that provides the specified 140 * key algorithm from the specified provider. 141 * 142 * @param algorithm 143 * the name of the requested key algorithm. 144 * @param provider 145 * the provider that is providing the algorithm 146 * @return the new {@code KeyGenerator} instance. 147 * @throws NoSuchAlgorithmException 148 * if the specified algorithm is not provided by the specified 149 * provider. 150 * @throws IllegalArgumentException 151 * if the specified provider is {@code null}. 152 * @throws NullPointerException 153 * if the specified algorithm name is {@code null}. 154 */ getInstance(String algorithm, Provider provider)155 public static final KeyGenerator getInstance(String algorithm, 156 Provider provider) throws NoSuchAlgorithmException { 157 if (provider == null) { 158 throw new IllegalArgumentException("provider == null"); 159 } 160 if (algorithm == null) { 161 throw new NullPointerException("algorithm == null"); 162 } 163 Object spi = ENGINE.getInstance(algorithm, provider, null); 164 return new KeyGenerator((KeyGeneratorSpi) spi, provider, algorithm); 165 } 166 167 /** 168 * Generates a secret key. 169 * 170 * @return the generated secret key. 171 */ generateKey()172 public final SecretKey generateKey() { 173 return spiImpl.engineGenerateKey(); 174 } 175 176 /** 177 * Initializes this {@code KeyGenerator} instance with the specified 178 * algorithm parameters. 179 * 180 * @param params 181 * the parameters for the key generation algorithm. 182 * @throws InvalidAlgorithmParameterException 183 * if the parameters cannot be used to initialize this key 184 * generator algorithm. 185 */ init(AlgorithmParameterSpec params)186 public final void init(AlgorithmParameterSpec params) 187 throws InvalidAlgorithmParameterException { 188 spiImpl.engineInit(params, RANDOM);//new SecureRandom()); 189 } 190 191 /** 192 * Initializes this {@code KeyGenerator} instance with the specified 193 * algorithm parameters and randomness source. 194 * 195 * @param params 196 * the parameters for the key generation algorithm. 197 * @param random 198 * the randomness source for any random bytes. 199 * @throws InvalidAlgorithmParameterException 200 * if the parameters cannot be uses to initialize this key 201 * generator algorithm. 202 */ init(AlgorithmParameterSpec params, SecureRandom random)203 public final void init(AlgorithmParameterSpec params, SecureRandom random) 204 throws InvalidAlgorithmParameterException { 205 spiImpl.engineInit(params, random); 206 } 207 208 /** 209 * Initializes this {@code KeyGenerator} instance for the specified key size 210 * (in bits). 211 * 212 * @param keysize 213 * the size of the key (in bits). 214 */ init(int keysize)215 public final void init(int keysize) { 216 spiImpl.engineInit(keysize, RANDOM);//new SecureRandom()); 217 } 218 219 /** 220 * Initializes this {@code KeyGenerator} instance for the specified key size 221 * (in bits) using the specified randomness source. 222 * 223 * @param keysize 224 * the size of the key (in bits). 225 * @param random 226 * the randomness source for any random bytes. 227 */ init(int keysize, SecureRandom random)228 public final void init(int keysize, SecureRandom random) { 229 spiImpl.engineInit(keysize, random); 230 } 231 232 /** 233 * Initializes this {@code KeyGenerator} with the specified randomness 234 * source. 235 * 236 * @param random 237 * the randomness source for any random bytes. 238 */ init(SecureRandom random)239 public final void init(SecureRandom random) { 240 spiImpl.engineInit(random); 241 } 242 } 243