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 */ 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("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 */ 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. The {@code provider} 141 * supplied does not have to be registered. 142 * 143 * @param algorithm 144 * the name of the requested key algorithm. 145 * @param provider 146 * the provider that is providing the algorithm 147 * @return the new {@code KeyGenerator} instance. 148 * @throws NoSuchAlgorithmException 149 * if the specified algorithm is not provided by the specified 150 * provider. 151 * @throws IllegalArgumentException 152 * if the specified provider is {@code null}. 153 * @throws NullPointerException 154 * if the specified algorithm name is {@code null}. 155 */ 156 public static final KeyGenerator getInstance(String algorithm, 157 Provider provider) throws NoSuchAlgorithmException { 158 if (provider == null) { 159 throw new IllegalArgumentException("provider == null"); 160 } 161 if (algorithm == null) { 162 throw new NullPointerException("algorithm == null"); 163 } 164 Object spi = ENGINE.getInstance(algorithm, provider, null); 165 return new KeyGenerator((KeyGeneratorSpi) spi, provider, algorithm); 166 } 167 168 /** 169 * Generates a secret key. 170 * 171 * @return the generated secret key. 172 */ 173 public final SecretKey generateKey() { 174 return spiImpl.engineGenerateKey(); 175 } 176 177 /** 178 * Initializes this {@code KeyGenerator} instance with the specified 179 * algorithm parameters. 180 * 181 * @param params 182 * the parameters for the key generation algorithm. 183 * @throws InvalidAlgorithmParameterException 184 * if the parameters cannot be used to initialize this key 185 * generator algorithm. 186 */ 187 public final void init(AlgorithmParameterSpec params) 188 throws InvalidAlgorithmParameterException { 189 spiImpl.engineInit(params, RANDOM);//new SecureRandom()); 190 } 191 192 /** 193 * Initializes this {@code KeyGenerator} instance with the specified 194 * algorithm parameters and randomness source. 195 * 196 * @param params 197 * the parameters for the key generation algorithm. 198 * @param random 199 * the randomness source for any random bytes. 200 * @throws InvalidAlgorithmParameterException 201 * if the parameters cannot be uses to initialize this key 202 * generator algorithm. 203 */ 204 public final void init(AlgorithmParameterSpec params, SecureRandom random) 205 throws InvalidAlgorithmParameterException { 206 spiImpl.engineInit(params, random); 207 } 208 209 /** 210 * Initializes this {@code KeyGenerator} instance for the specified key size 211 * (in bits). 212 * 213 * @param keysize 214 * the size of the key (in bits). 215 */ 216 public final void init(int keysize) { 217 spiImpl.engineInit(keysize, RANDOM);//new SecureRandom()); 218 } 219 220 /** 221 * Initializes this {@code KeyGenerator} instance for the specified key size 222 * (in bits) using the specified randomness source. 223 * 224 * @param keysize 225 * the size of the key (in bits). 226 * @param random 227 * the randomness source for any random bytes. 228 */ 229 public final void init(int keysize, SecureRandom random) { 230 spiImpl.engineInit(keysize, random); 231 } 232 233 /** 234 * Initializes this {@code KeyGenerator} with the specified randomness 235 * source. 236 * 237 * @param random 238 * the randomness source for any random bytes. 239 */ 240 public final void init(SecureRandom random) { 241 spiImpl.engineInit(random); 242 } 243 } 244