The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 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; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 27 | import org.apache.harmony.security.fortress.Engine; |
| 28 | |
| 29 | |
| 30 | /** |
| 31 | * This class provides the public API for generating symmetric cryptographic |
| 32 | * keys. |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 33 | */ |
| 34 | public class KeyGenerator { |
| 35 | |
| 36 | // Used to access common engine functionality |
Brian Carlstrom | 0a48084 | 2010-10-18 23:00:51 -0700 | [diff] [blame] | 37 | private static final Engine ENGINE = new Engine("KeyGenerator"); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 38 | |
| 39 | // Store SecureRandom |
Brian Carlstrom | 0a48084 | 2010-10-18 23:00:51 -0700 | [diff] [blame] | 40 | private static final SecureRandom RANDOM = new SecureRandom(); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 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. |
Jesse Wilson | ce9ec01 | 2009-08-31 15:37:14 -0700 | [diff] [blame] | 53 | * |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 54 | * @param keyGenSpi |
| 55 | * the implementation delegate. |
| 56 | * @param provider |
| 57 | * the implementation provider. |
| 58 | * @param algorithm |
| 59 | * the name of the algorithm. |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 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. |
Jesse Wilson | ce9ec01 | 2009-08-31 15:37:14 -0700 | [diff] [blame] | 70 | * |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 71 | * @return the name of the key generation algorithm. |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 72 | */ |
| 73 | public final String getAlgorithm() { |
| 74 | return algorithm; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Returns the provider of this {@code KeyGenerator} instance. |
Jesse Wilson | ce9ec01 | 2009-08-31 15:37:14 -0700 | [diff] [blame] | 79 | * |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 80 | * @return the provider of this {@code KeyGenerator} instance. |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 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, |
Jesse Wilson | ce9ec01 | 2009-08-31 15:37:14 -0700 | [diff] [blame] | 89 | * |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 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}. |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 97 | */ |
| 98 | public static final KeyGenerator getInstance(String algorithm) |
| 99 | throws NoSuchAlgorithmException { |
| 100 | if (algorithm == null) { |
Kenny Root | 86acc04 | 2012-09-12 10:32:58 -0700 | [diff] [blame] | 101 | throw new NullPointerException("algorithm == null"); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 102 | } |
Brian Carlstrom | 6cdb6b7 | 2010-10-19 11:27:15 -0700 | [diff] [blame] | 103 | Engine.SpiAndProvider sap = ENGINE.getInstance(algorithm, null); |
| 104 | return new KeyGenerator((KeyGeneratorSpi) sap.spi, sap.provider, algorithm); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Creates a new {@code KeyGenerator} instance that provides the specified |
| 109 | * key algorithm from the specified provider. |
Jesse Wilson | ce9ec01 | 2009-08-31 15:37:14 -0700 | [diff] [blame] | 110 | * |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 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}. |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 125 | */ |
| 126 | public static final KeyGenerator getInstance(String algorithm, |
Elliott Hughes | 80a7fba | 2010-05-21 16:58:35 -0700 | [diff] [blame] | 127 | String provider) throws NoSuchAlgorithmException, NoSuchProviderException { |
| 128 | if (provider == null || provider.isEmpty()) { |
| 129 | throw new IllegalArgumentException("Provider is null or empty"); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 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. |
Jesse Wilson | ce9ec01 | 2009-08-31 15:37:14 -0700 | [diff] [blame] | 141 | * |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 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}. |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 154 | */ |
| 155 | public static final KeyGenerator getInstance(String algorithm, |
| 156 | Provider provider) throws NoSuchAlgorithmException { |
| 157 | if (provider == null) { |
Elliott Hughes | 80a7fba | 2010-05-21 16:58:35 -0700 | [diff] [blame] | 158 | throw new IllegalArgumentException("provider == null"); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 159 | } |
| 160 | if (algorithm == null) { |
Kenny Root | 86acc04 | 2012-09-12 10:32:58 -0700 | [diff] [blame] | 161 | throw new NullPointerException("algorithm == null"); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 162 | } |
Brian Carlstrom | 6cdb6b7 | 2010-10-19 11:27:15 -0700 | [diff] [blame] | 163 | Object spi = ENGINE.getInstance(algorithm, provider, null); |
| 164 | return new KeyGenerator((KeyGeneratorSpi) spi, provider, algorithm); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Generates a secret key. |
Jesse Wilson | ce9ec01 | 2009-08-31 15:37:14 -0700 | [diff] [blame] | 169 | * |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 170 | * @return the generated secret key. |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 171 | */ |
| 172 | public final SecretKey generateKey() { |
| 173 | return spiImpl.engineGenerateKey(); |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Initializes this {@code KeyGenerator} instance with the specified |
| 178 | * algorithm parameters. |
Jesse Wilson | ce9ec01 | 2009-08-31 15:37:14 -0700 | [diff] [blame] | 179 | * |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 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. |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 185 | */ |
| 186 | public final void init(AlgorithmParameterSpec params) |
| 187 | throws InvalidAlgorithmParameterException { |
Brian Carlstrom | 0a48084 | 2010-10-18 23:00:51 -0700 | [diff] [blame] | 188 | spiImpl.engineInit(params, RANDOM);//new SecureRandom()); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Initializes this {@code KeyGenerator} instance with the specified |
| 193 | * algorithm parameters and randomness source. |
Jesse Wilson | ce9ec01 | 2009-08-31 15:37:14 -0700 | [diff] [blame] | 194 | * |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 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. |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 202 | */ |
| 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). |
Jesse Wilson | ce9ec01 | 2009-08-31 15:37:14 -0700 | [diff] [blame] | 211 | * |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 212 | * @param keysize |
| 213 | * the size of the key (in bits). |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 214 | */ |
| 215 | public final void init(int keysize) { |
Brian Carlstrom | 0a48084 | 2010-10-18 23:00:51 -0700 | [diff] [blame] | 216 | spiImpl.engineInit(keysize, RANDOM);//new SecureRandom()); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Initializes this {@code KeyGenerator} instance for the specified key size |
| 221 | * (in bits) using the specified randomness source. |
Jesse Wilson | ce9ec01 | 2009-08-31 15:37:14 -0700 | [diff] [blame] | 222 | * |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 223 | * @param keysize |
| 224 | * the size of the key (in bits). |
| 225 | * @param random |
| 226 | * the randomness source for any random bytes. |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 227 | */ |
| 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. |
Jesse Wilson | ce9ec01 | 2009-08-31 15:37:14 -0700 | [diff] [blame] | 235 | * |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 236 | * @param random |
| 237 | * the randomness source for any random bytes. |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 238 | */ |
| 239 | public final void init(SecureRandom random) { |
| 240 | spiImpl.engineInit(random); |
| 241 | } |
Elliott Hughes | 80a7fba | 2010-05-21 16:58:35 -0700 | [diff] [blame] | 242 | } |