blob: fc409daf7328da28b0d2d153f61faaae984b929c [file] [log] [blame]
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08001/*
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
18package javax.crypto;
19
20import java.security.InvalidAlgorithmParameterException;
21import java.security.NoSuchAlgorithmException;
22import java.security.NoSuchProviderException;
23import java.security.Provider;
24import java.security.SecureRandom;
25import java.security.Security;
26import java.security.spec.AlgorithmParameterSpec;
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080027import 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 Projectadc854b2009-03-03 19:28:47 -080033 */
34public class KeyGenerator {
35
36 // Used to access common engine functionality
Brian Carlstrom0a480842010-10-18 23:00:51 -070037 private static final Engine ENGINE = new Engine("KeyGenerator");
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080038
39 // Store SecureRandom
Brian Carlstrom0a480842010-10-18 23:00:51 -070040 private static final SecureRandom RANDOM = new SecureRandom();
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080041
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 Wilsonce9ec012009-08-31 15:37:14 -070053 *
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080054 * @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 Projectadc854b2009-03-03 19:28:47 -080060 */
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 Wilsonce9ec012009-08-31 15:37:14 -070070 *
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080071 * @return the name of the key generation algorithm.
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080072 */
73 public final String getAlgorithm() {
74 return algorithm;
75 }
76
77 /**
78 * Returns the provider of this {@code KeyGenerator} instance.
Jesse Wilsonce9ec012009-08-31 15:37:14 -070079 *
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080080 * @return the provider of this {@code KeyGenerator} instance.
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080081 */
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 Wilsonce9ec012009-08-31 15:37:14 -070089 *
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080090 * @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 Projectadc854b2009-03-03 19:28:47 -080097 */
98 public static final KeyGenerator getInstance(String algorithm)
99 throws NoSuchAlgorithmException {
100 if (algorithm == null) {
Kenny Root86acc042012-09-12 10:32:58 -0700101 throw new NullPointerException("algorithm == null");
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800102 }
Brian Carlstrom6cdb6b72010-10-19 11:27:15 -0700103 Engine.SpiAndProvider sap = ENGINE.getInstance(algorithm, null);
104 return new KeyGenerator((KeyGeneratorSpi) sap.spi, sap.provider, algorithm);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800105 }
106
107 /**
108 * Creates a new {@code KeyGenerator} instance that provides the specified
109 * key algorithm from the specified provider.
Jesse Wilsonce9ec012009-08-31 15:37:14 -0700110 *
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800111 * @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 Projectadc854b2009-03-03 19:28:47 -0800125 */
126 public static final KeyGenerator getInstance(String algorithm,
Elliott Hughes80a7fba2010-05-21 16:58:35 -0700127 String provider) throws NoSuchAlgorithmException, NoSuchProviderException {
128 if (provider == null || provider.isEmpty()) {
129 throw new IllegalArgumentException("Provider is null or empty");
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800130 }
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
Kenny Root0a648872014-02-05 10:09:13 -0800140 * key algorithm from the specified provider. The {@code provider}
141 * supplied does not have to be registered.
Jesse Wilsonce9ec012009-08-31 15:37:14 -0700142 *
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800143 * @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}.
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800155 */
156 public static final KeyGenerator getInstance(String algorithm,
157 Provider provider) throws NoSuchAlgorithmException {
158 if (provider == null) {
Elliott Hughes80a7fba2010-05-21 16:58:35 -0700159 throw new IllegalArgumentException("provider == null");
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800160 }
161 if (algorithm == null) {
Kenny Root86acc042012-09-12 10:32:58 -0700162 throw new NullPointerException("algorithm == null");
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800163 }
Brian Carlstrom6cdb6b72010-10-19 11:27:15 -0700164 Object spi = ENGINE.getInstance(algorithm, provider, null);
165 return new KeyGenerator((KeyGeneratorSpi) spi, provider, algorithm);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800166 }
167
168 /**
169 * Generates a secret key.
Jesse Wilsonce9ec012009-08-31 15:37:14 -0700170 *
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800171 * @return the generated secret key.
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800172 */
173 public final SecretKey generateKey() {
174 return spiImpl.engineGenerateKey();
175 }
176
177 /**
178 * Initializes this {@code KeyGenerator} instance with the specified
179 * algorithm parameters.
Jesse Wilsonce9ec012009-08-31 15:37:14 -0700180 *
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800181 * @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.
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800186 */
187 public final void init(AlgorithmParameterSpec params)
188 throws InvalidAlgorithmParameterException {
Brian Carlstrom0a480842010-10-18 23:00:51 -0700189 spiImpl.engineInit(params, RANDOM);//new SecureRandom());
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800190 }
191
192 /**
193 * Initializes this {@code KeyGenerator} instance with the specified
194 * algorithm parameters and randomness source.
Jesse Wilsonce9ec012009-08-31 15:37:14 -0700195 *
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800196 * @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.
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800203 */
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).
Jesse Wilsonce9ec012009-08-31 15:37:14 -0700212 *
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800213 * @param keysize
214 * the size of the key (in bits).
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800215 */
216 public final void init(int keysize) {
Brian Carlstrom0a480842010-10-18 23:00:51 -0700217 spiImpl.engineInit(keysize, RANDOM);//new SecureRandom());
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800218 }
219
220 /**
221 * Initializes this {@code KeyGenerator} instance for the specified key size
222 * (in bits) using the specified randomness source.
Jesse Wilsonce9ec012009-08-31 15:37:14 -0700223 *
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800224 * @param keysize
225 * the size of the key (in bits).
226 * @param random
227 * the randomness source for any random bytes.
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800228 */
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.
Jesse Wilsonce9ec012009-08-31 15:37:14 -0700236 *
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800237 * @param random
238 * the randomness source for any random bytes.
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800239 */
240 public final void init(SecureRandom random) {
241 spiImpl.engineInit(random);
242 }
Elliott Hughes80a7fba2010-05-21 16:58:35 -0700243}