001: package org.bouncycastle.jce.provider;
002:
003: import org.bouncycastle.crypto.CipherKeyGenerator;
004: import org.bouncycastle.crypto.KeyGenerationParameters;
005: import org.bouncycastle.crypto.generators.DESKeyGenerator;
006: import org.bouncycastle.crypto.generators.DESedeKeyGenerator;
007:
008: import javax.crypto.KeyGeneratorSpi;
009: import javax.crypto.SecretKey;
010: import javax.crypto.spec.SecretKeySpec;
011: import java.security.InvalidAlgorithmParameterException;
012: import java.security.InvalidParameterException;
013: import java.security.SecureRandom;
014: import java.security.spec.AlgorithmParameterSpec;
015:
016: public class JCEKeyGenerator extends KeyGeneratorSpi {
017: protected String algName;
018: protected int keySize;
019: protected int defaultKeySize;
020: protected CipherKeyGenerator engine;
021:
022: protected boolean uninitialised = true;
023:
024: protected JCEKeyGenerator(String algName, int defaultKeySize,
025: CipherKeyGenerator engine) {
026: this .algName = algName;
027: this .keySize = this .defaultKeySize = defaultKeySize;
028: this .engine = engine;
029: }
030:
031: protected void engineInit(AlgorithmParameterSpec params,
032: SecureRandom random)
033: throws InvalidAlgorithmParameterException {
034: throw new InvalidAlgorithmParameterException("Not Implemented");
035: }
036:
037: protected void engineInit(SecureRandom random) {
038: if (random != null) {
039: engine.init(new KeyGenerationParameters(random,
040: defaultKeySize));
041: uninitialised = false;
042: }
043: }
044:
045: protected void engineInit(int keySize, SecureRandom random) {
046: try {
047: engine.init(new KeyGenerationParameters(random, keySize));
048: uninitialised = false;
049: } catch (IllegalArgumentException e) {
050: throw new InvalidParameterException(e.getMessage());
051: }
052: }
053:
054: protected SecretKey engineGenerateKey() {
055: if (uninitialised) {
056: engine.init(new KeyGenerationParameters(new SecureRandom(),
057: defaultKeySize));
058: uninitialised = false;
059: }
060:
061: return new SecretKeySpec(engine.generateKey(), algName);
062: }
063:
064: /**
065: * the generators that are defined directly off us.
066: */
067:
068: /**
069: * DES
070: */
071: public static class DES extends JCEKeyGenerator {
072: public DES() {
073: super ("DES", 64, new DESKeyGenerator());
074: }
075: }
076:
077: /**
078: * DESede - the default for this is to generate a key in
079: * a-b-a format that's 24 bytes long but has 16 bytes of
080: * key material (the first 8 bytes is repeated as the last
081: * 8 bytes). If you give it a size, you'll get just what you
082: * asked for.
083: */
084: public static class DESede extends JCEKeyGenerator {
085: private boolean keySizeSet = false;
086:
087: public DESede() {
088: super ("DESede", 192, new DESedeKeyGenerator());
089: }
090:
091: protected void engineInit(int keySize, SecureRandom random) {
092: super .engineInit(keySize, random);
093: keySizeSet = true;
094: }
095:
096: protected SecretKey engineGenerateKey() {
097: if (uninitialised) {
098: engine.init(new KeyGenerationParameters(
099: new SecureRandom(), defaultKeySize));
100: uninitialised = false;
101: }
102:
103: //
104: // if no key size has been defined generate a 24 byte key in
105: // the a-b-a format
106: //
107: if (!keySizeSet) {
108: byte[] k = engine.generateKey();
109:
110: System.arraycopy(k, 0, k, 16, 8);
111:
112: return (SecretKey) (new SecretKeySpec(k, algName));
113: } else {
114: return (SecretKey) (new SecretKeySpec(engine
115: .generateKey(), algName));
116: }
117: }
118: }
119:
120: /**
121: * generate a desEDE key in the a-b-c format.
122: */
123: public static class DESede3 extends JCEKeyGenerator {
124: public DESede3() {
125: super ("DESede3", 192, new DESedeKeyGenerator());
126: }
127: }
128:
129: /**
130: * SKIPJACK
131: */
132: public static class Skipjack extends JCEKeyGenerator {
133: public Skipjack() {
134: super ("SKIPJACK", 80, new CipherKeyGenerator());
135: }
136: }
137:
138: /**
139: * Blowfish
140: */
141: public static class Blowfish extends JCEKeyGenerator {
142: public Blowfish() {
143: super ("Blowfish", 448, new CipherKeyGenerator());
144: }
145: }
146:
147: /**
148: * Twofish
149: */
150: public static class Twofish extends JCEKeyGenerator {
151: public Twofish() {
152: super ("Twofish", 256, new CipherKeyGenerator());
153: }
154: }
155:
156: /**
157: * RC2
158: */
159: public static class RC2 extends JCEKeyGenerator {
160: public RC2() {
161: super ("RC2", 128, new CipherKeyGenerator());
162: }
163: }
164:
165: /**
166: * RC4
167: */
168: public static class RC4 extends JCEKeyGenerator {
169: public RC4() {
170: super ("RC4", 128, new CipherKeyGenerator());
171: }
172: }
173:
174: /**
175: * RC5
176: */
177: public static class RC5 extends JCEKeyGenerator {
178: public RC5() {
179: super ("RC5", 128, new CipherKeyGenerator());
180: }
181: }
182:
183: /**
184: * RC5
185: */
186: public static class RC564 extends JCEKeyGenerator {
187: public RC564() {
188: super ("RC5-64", 256, new CipherKeyGenerator());
189: }
190: }
191:
192: /**
193: * RC6
194: */
195: public static class RC6 extends JCEKeyGenerator {
196: public RC6() {
197: super ("RC6", 256, new CipherKeyGenerator());
198: }
199: }
200:
201: /**
202: * GOST28147
203: */
204: public static class GOST28147 extends JCEKeyGenerator {
205: public GOST28147() {
206: super ("GOST28147", 256, new CipherKeyGenerator());
207: }
208: }
209:
210: /**
211: * Rijndael
212: */
213: public static class Rijndael extends JCEKeyGenerator {
214: public Rijndael() {
215: super ("Rijndael", 192, new CipherKeyGenerator());
216: }
217: }
218:
219: /**
220: * Serpent
221: */
222: public static class Serpent extends JCEKeyGenerator {
223: public Serpent() {
224: super ("Serpent", 192, new CipherKeyGenerator());
225: }
226: }
227:
228: /**
229: * CAST6
230: */
231: public static class CAST6 extends JCEKeyGenerator {
232: public CAST6() {
233: super ("CAST6", 256, new CipherKeyGenerator());
234: }
235: }
236:
237: /**
238: * IDEA
239: */
240: public static class IDEA extends JCEKeyGenerator {
241: public IDEA() {
242: super ("IDEA", 128, new CipherKeyGenerator());
243: }
244: }
245:
246: /**
247: * TEA
248: */
249: public static class TEA extends JCEKeyGenerator {
250: public TEA() {
251: super ("TEA", 128, new CipherKeyGenerator());
252: }
253: }
254:
255: /**
256: * XTEA
257: */
258: public static class XTEA extends JCEKeyGenerator {
259: public XTEA() {
260: super ("XTEA", 128, new CipherKeyGenerator());
261: }
262: }
263:
264: /**
265: * Salsa20
266: */
267: public static class Salsa20 extends JCEKeyGenerator {
268: public Salsa20() {
269: super ("Salsa20", 128, new CipherKeyGenerator());
270: }
271: }
272:
273: /**
274: * HC128
275: */
276: public static class HC128 extends JCEKeyGenerator {
277: public HC128() {
278: super ("HC128", 128, new CipherKeyGenerator());
279: }
280: }
281:
282: /**
283: * HC256
284: */
285: public static class HC256 extends JCEKeyGenerator {
286: public HC256() {
287: super ("HC256", 256, new CipherKeyGenerator());
288: }
289: }
290:
291: /**
292: * VMPC
293: */
294: public static class VMPC extends JCEKeyGenerator {
295: public VMPC() {
296: super ("VMPC", 128, new CipherKeyGenerator());
297: }
298: }
299:
300: /**
301: * VMPC-KSA3
302: */
303: public static class VMPCKSA3 extends JCEKeyGenerator {
304: public VMPCKSA3() {
305: super ("VMPC-KSA3", 128, new CipherKeyGenerator());
306: }
307: }
308:
309: // HMAC Related secret keys..
310:
311: /**
312: * MD2HMAC
313: */
314: public static class MD2HMAC extends JCEKeyGenerator {
315: public MD2HMAC() {
316: super ("HMACMD2", 128, new CipherKeyGenerator());
317: }
318: }
319:
320: /**
321: * MD4HMAC
322: */
323: public static class MD4HMAC extends JCEKeyGenerator {
324: public MD4HMAC() {
325: super ("HMACMD4", 128, new CipherKeyGenerator());
326: }
327: }
328:
329: /**
330: * MD5HMAC
331: */
332: public static class MD5HMAC extends JCEKeyGenerator {
333: public MD5HMAC() {
334: super ("HMACMD5", 128, new CipherKeyGenerator());
335: }
336: }
337:
338: /**
339: * RIPE128HMAC
340: */
341: public static class RIPEMD128HMAC extends JCEKeyGenerator {
342: public RIPEMD128HMAC() {
343: super ("HMACRIPEMD128", 128, new CipherKeyGenerator());
344: }
345: }
346:
347: /**
348: * RIPE160HMAC
349: */
350: public static class RIPEMD160HMAC extends JCEKeyGenerator {
351: public RIPEMD160HMAC() {
352: super ("HMACRIPEMD160", 160, new CipherKeyGenerator());
353: }
354: }
355:
356: /**
357: * HMACSHA1
358: */
359: public static class HMACSHA1 extends JCEKeyGenerator {
360: public HMACSHA1() {
361: super ("HMACSHA1", 160, new CipherKeyGenerator());
362: }
363: }
364:
365: /**
366: * HMACSHA224
367: */
368: public static class HMACSHA224 extends JCEKeyGenerator {
369: public HMACSHA224() {
370: super ("HMACSHA224", 224, new CipherKeyGenerator());
371: }
372: }
373:
374: /**
375: * HMACSHA256
376: */
377: public static class HMACSHA256 extends JCEKeyGenerator {
378: public HMACSHA256() {
379: super ("HMACSHA256", 256, new CipherKeyGenerator());
380: }
381: }
382:
383: /**
384: * HMACSHA384
385: */
386: public static class HMACSHA384 extends JCEKeyGenerator {
387: public HMACSHA384() {
388: super ("HMACSHA384", 384, new CipherKeyGenerator());
389: }
390: }
391:
392: /**
393: * HMACSHA512
394: */
395: public static class HMACSHA512 extends JCEKeyGenerator {
396: public HMACSHA512() {
397: super ("HMACSHA512", 512, new CipherKeyGenerator());
398: }
399: }
400:
401: /**
402: * HMACTIGER
403: */
404: public static class HMACTIGER extends JCEKeyGenerator {
405: public HMACTIGER() {
406: super ("HMACTIGER", 192, new CipherKeyGenerator());
407: }
408: }
409: }
|