blob: 606998a817ca73e34a2977e08f42b9935c6ab308 [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
140 * key algorithm from the specified provider.
Jesse Wilsonce9ec012009-08-31 15:37:14 -0700141 *
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800142 * @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 Projectadc854b2009-03-03 19:28:47 -0800154 */
155 public static final KeyGenerator getInstance(String algorithm,
156 Provider provider) throws NoSuchAlgorithmException {
157 if (provider == null) {
Elliott Hughes80a7fba2010-05-21 16:58:35 -0700158 throw new IllegalArgumentException("provider == null");
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800159 }
160 if (algorithm == null) {
Kenny Root86acc042012-09-12 10:32:58 -0700161 throw new NullPointerException("algorithm == null");
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800162 }
Brian Carlstrom6cdb6b72010-10-19 11:27:15 -0700163 Object spi = ENGINE.getInstance(algorithm, provider, null);
164 return new KeyGenerator((KeyGeneratorSpi) spi, provider, algorithm);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800165 }
166
167 /**
168 * Generates a secret key.
Jesse Wilsonce9ec012009-08-31 15:37:14 -0700169 *
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800170 * @return the generated secret key.
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800171 */
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 Wilsonce9ec012009-08-31 15:37:14 -0700179 *
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800180 * @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 Projectadc854b2009-03-03 19:28:47 -0800185 */
186 public final void init(AlgorithmParameterSpec params)
187 throws InvalidAlgorithmParameterException {
Brian Carlstrom0a480842010-10-18 23:00:51 -0700188 spiImpl.engineInit(params, RANDOM);//new SecureRandom());
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800189 }
190
191 /**
192 * Initializes this {@code KeyGenerator} instance with the specified
193 * algorithm parameters and randomness source.
Jesse Wilsonce9ec012009-08-31 15:37:14 -0700194 *
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800195 * @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 Projectadc854b2009-03-03 19:28:47 -0800202 */
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 Wilsonce9ec012009-08-31 15:37:14 -0700211 *
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800212 * @param keysize
213 * the size of the key (in bits).
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800214 */
215 public final void init(int keysize) {
Brian Carlstrom0a480842010-10-18 23:00:51 -0700216 spiImpl.engineInit(keysize, RANDOM);//new SecureRandom());
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800217 }
218
219 /**
220 * Initializes this {@code KeyGenerator} instance for the specified key size
221 * (in bits) using the specified randomness source.
Jesse Wilsonce9ec012009-08-31 15:37:14 -0700222 *
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800223 * @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 Projectadc854b2009-03-03 19:28:47 -0800227 */
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 Wilsonce9ec012009-08-31 15:37:14 -0700235 *
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800236 * @param random
237 * the randomness source for any random bytes.
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800238 */
239 public final void init(SecureRandom random) {
240 spiImpl.engineInit(random);
241 }
Elliott Hughes80a7fba2010-05-21 16:58:35 -0700242}