001: package org.bouncycastle.jce.provider;
002:
003: import org.bouncycastle.asn1.cryptopro.CryptoProObjectIdentifiers;
004: import org.bouncycastle.asn1.cryptopro.ECGOST3410NamedCurves;
005: import org.bouncycastle.asn1.nist.NISTNamedCurves;
006: import org.bouncycastle.asn1.sec.SECNamedCurves;
007: import org.bouncycastle.asn1.teletrust.TeleTrusTNamedCurves;
008: import org.bouncycastle.asn1.x9.X962NamedCurves;
009: import org.bouncycastle.asn1.x9.X9ECParameters;
010: import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
011: import org.bouncycastle.crypto.generators.DHBasicKeyPairGenerator;
012: import org.bouncycastle.crypto.generators.DHParametersGenerator;
013: import org.bouncycastle.crypto.generators.DSAKeyPairGenerator;
014: import org.bouncycastle.crypto.generators.DSAParametersGenerator;
015: import org.bouncycastle.crypto.generators.ECKeyPairGenerator;
016: import org.bouncycastle.crypto.generators.ElGamalKeyPairGenerator;
017: import org.bouncycastle.crypto.generators.ElGamalParametersGenerator;
018: import org.bouncycastle.crypto.generators.GOST3410KeyPairGenerator;
019: import org.bouncycastle.crypto.generators.RSAKeyPairGenerator;
020: import org.bouncycastle.crypto.params.DHKeyGenerationParameters;
021: import org.bouncycastle.crypto.params.DHParameters;
022: import org.bouncycastle.crypto.params.DHPrivateKeyParameters;
023: import org.bouncycastle.crypto.params.DHPublicKeyParameters;
024: import org.bouncycastle.crypto.params.DSAKeyGenerationParameters;
025: import org.bouncycastle.crypto.params.DSAParameters;
026: import org.bouncycastle.crypto.params.DSAPrivateKeyParameters;
027: import org.bouncycastle.crypto.params.DSAPublicKeyParameters;
028: import org.bouncycastle.crypto.params.ECDomainParameters;
029: import org.bouncycastle.crypto.params.ECKeyGenerationParameters;
030: import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
031: import org.bouncycastle.crypto.params.ECPublicKeyParameters;
032: import org.bouncycastle.crypto.params.ElGamalKeyGenerationParameters;
033: import org.bouncycastle.crypto.params.ElGamalParameters;
034: import org.bouncycastle.crypto.params.ElGamalPrivateKeyParameters;
035: import org.bouncycastle.crypto.params.ElGamalPublicKeyParameters;
036: import org.bouncycastle.crypto.params.GOST3410KeyGenerationParameters;
037: import org.bouncycastle.crypto.params.GOST3410Parameters;
038: import org.bouncycastle.crypto.params.GOST3410PrivateKeyParameters;
039: import org.bouncycastle.crypto.params.GOST3410PublicKeyParameters;
040: import org.bouncycastle.crypto.params.RSAKeyGenerationParameters;
041: import org.bouncycastle.crypto.params.RSAKeyParameters;
042: import org.bouncycastle.crypto.params.RSAPrivateCrtKeyParameters;
043: import org.bouncycastle.jce.spec.ECNamedCurveSpec;
044: import org.bouncycastle.jce.spec.ECParameterSpec;
045: import org.bouncycastle.jce.spec.ElGamalParameterSpec;
046: import org.bouncycastle.jce.spec.GOST3410ParameterSpec;
047: import org.bouncycastle.jce.spec.GOST3410PublicKeyParameterSetSpec;
048: import org.bouncycastle.math.ec.ECCurve;
049: import org.bouncycastle.math.ec.ECPoint;
050:
051: import java.math.BigInteger;
052: import java.security.InvalidAlgorithmParameterException;
053: import java.security.InvalidParameterException;
054: import java.security.KeyPair;
055: import java.security.KeyPairGenerator;
056: import java.security.SecureRandom;
057: import java.security.spec.AlgorithmParameterSpec;
058: import java.security.spec.DSAParameterSpec;
059: import java.security.spec.ECGenParameterSpec;
060: import java.security.spec.RSAKeyGenParameterSpec;
061: import java.util.Hashtable;
062:
063: import javax.crypto.spec.DHParameterSpec;
064:
065: public abstract class JDKKeyPairGenerator extends KeyPairGenerator {
066: public JDKKeyPairGenerator(String algorithmName) {
067: super (algorithmName);
068: }
069:
070: public abstract void initialize(int strength, SecureRandom random);
071:
072: public abstract KeyPair generateKeyPair();
073:
074: public static class RSA extends JDKKeyPairGenerator {
075: final static BigInteger defaultPublicExponent = BigInteger
076: .valueOf(0x10001);
077: final static int defaultTests = 12;
078:
079: RSAKeyGenerationParameters param;
080: RSAKeyPairGenerator engine;
081:
082: public RSA() {
083: super ("RSA");
084:
085: engine = new RSAKeyPairGenerator();
086: param = new RSAKeyGenerationParameters(
087: defaultPublicExponent, new SecureRandom(), 2048,
088: defaultTests);
089: engine.init(param);
090: }
091:
092: public void initialize(int strength, SecureRandom random) {
093: param = new RSAKeyGenerationParameters(
094: defaultPublicExponent, random, strength,
095: defaultTests);
096:
097: engine.init(param);
098: }
099:
100: public void initialize(AlgorithmParameterSpec params,
101: SecureRandom random)
102: throws InvalidAlgorithmParameterException {
103: if (!(params instanceof RSAKeyGenParameterSpec)) {
104: throw new InvalidAlgorithmParameterException(
105: "parameter object not a RSAKeyGenParameterSpec");
106: }
107: RSAKeyGenParameterSpec rsaParams = (RSAKeyGenParameterSpec) params;
108:
109: param = new RSAKeyGenerationParameters(rsaParams
110: .getPublicExponent(), random, rsaParams
111: .getKeysize(), defaultTests);
112:
113: engine.init(param);
114: }
115:
116: public KeyPair generateKeyPair() {
117: AsymmetricCipherKeyPair pair = engine.generateKeyPair();
118: RSAKeyParameters pub = (RSAKeyParameters) pair.getPublic();
119: RSAPrivateCrtKeyParameters priv = (RSAPrivateCrtKeyParameters) pair
120: .getPrivate();
121:
122: return new KeyPair(new JCERSAPublicKey(pub),
123: new JCERSAPrivateCrtKey(priv));
124: }
125: }
126:
127: public static class DH extends JDKKeyPairGenerator {
128: DHKeyGenerationParameters param;
129: DHBasicKeyPairGenerator engine = new DHBasicKeyPairGenerator();
130: int strength = 1024;
131: int certainty = 20;
132: SecureRandom random = new SecureRandom();
133: boolean initialised = false;
134:
135: public DH() {
136: super ("DH");
137: }
138:
139: public void initialize(int strength, SecureRandom random) {
140: this .strength = strength;
141: this .random = random;
142: }
143:
144: public void initialize(AlgorithmParameterSpec params,
145: SecureRandom random)
146: throws InvalidAlgorithmParameterException {
147: if (!(params instanceof DHParameterSpec)) {
148: throw new InvalidAlgorithmParameterException(
149: "parameter object not a DHParameterSpec");
150: }
151: DHParameterSpec dhParams = (DHParameterSpec) params;
152:
153: param = new DHKeyGenerationParameters(random,
154: new DHParameters(dhParams.getP(), dhParams.getG(),
155: null, dhParams.getL()));
156:
157: engine.init(param);
158: initialised = true;
159: }
160:
161: public KeyPair generateKeyPair() {
162: if (!initialised) {
163: DHParametersGenerator pGen = new DHParametersGenerator();
164:
165: pGen.init(strength, certainty, random);
166: param = new DHKeyGenerationParameters(random, pGen
167: .generateParameters());
168: engine.init(param);
169: initialised = true;
170: }
171:
172: AsymmetricCipherKeyPair pair = engine.generateKeyPair();
173: DHPublicKeyParameters pub = (DHPublicKeyParameters) pair
174: .getPublic();
175: DHPrivateKeyParameters priv = (DHPrivateKeyParameters) pair
176: .getPrivate();
177:
178: return new KeyPair(new JCEDHPublicKey(pub),
179: new JCEDHPrivateKey(priv));
180: }
181: }
182:
183: public static class DSA extends JDKKeyPairGenerator {
184: DSAKeyGenerationParameters param;
185: DSAKeyPairGenerator engine = new DSAKeyPairGenerator();
186: int strength = 1024;
187: int certainty = 20;
188: SecureRandom random = new SecureRandom();
189: boolean initialised = false;
190:
191: public DSA() {
192: super ("DSA");
193: }
194:
195: public void initialize(int strength, SecureRandom random) {
196: if (strength < 512 || strength > 1024 || strength % 64 != 0) {
197: throw new InvalidParameterException(
198: "strength must be from 512 - 1024 and a multiple of 64");
199: }
200:
201: this .strength = strength;
202: this .random = random;
203: }
204:
205: public void initialize(AlgorithmParameterSpec params,
206: SecureRandom random)
207: throws InvalidAlgorithmParameterException {
208: if (!(params instanceof DSAParameterSpec)) {
209: throw new InvalidAlgorithmParameterException(
210: "parameter object not a DSAParameterSpec");
211: }
212: DSAParameterSpec dsaParams = (DSAParameterSpec) params;
213:
214: param = new DSAKeyGenerationParameters(random,
215: new DSAParameters(dsaParams.getP(), dsaParams
216: .getQ(), dsaParams.getG()));
217:
218: engine.init(param);
219: initialised = true;
220: }
221:
222: public KeyPair generateKeyPair() {
223: if (!initialised) {
224: DSAParametersGenerator pGen = new DSAParametersGenerator();
225:
226: pGen.init(strength, certainty, random);
227: param = new DSAKeyGenerationParameters(random, pGen
228: .generateParameters());
229: engine.init(param);
230: initialised = true;
231: }
232:
233: AsymmetricCipherKeyPair pair = engine.generateKeyPair();
234: DSAPublicKeyParameters pub = (DSAPublicKeyParameters) pair
235: .getPublic();
236: DSAPrivateKeyParameters priv = (DSAPrivateKeyParameters) pair
237: .getPrivate();
238:
239: return new KeyPair(new JDKDSAPublicKey(pub),
240: new JDKDSAPrivateKey(priv));
241: }
242: }
243:
244: public static class ElGamal extends JDKKeyPairGenerator {
245: ElGamalKeyGenerationParameters param;
246: ElGamalKeyPairGenerator engine = new ElGamalKeyPairGenerator();
247: int strength = 1024;
248: int certainty = 20;
249: SecureRandom random = new SecureRandom();
250: boolean initialised = false;
251:
252: public ElGamal() {
253: super ("ElGamal");
254: }
255:
256: public void initialize(int strength, SecureRandom random) {
257: this .strength = strength;
258: this .random = random;
259: }
260:
261: public void initialize(AlgorithmParameterSpec params,
262: SecureRandom random)
263: throws InvalidAlgorithmParameterException {
264: if (!(params instanceof ElGamalParameterSpec)
265: && !(params instanceof DHParameterSpec)) {
266: throw new InvalidAlgorithmParameterException(
267: "parameter object not a DHParameterSpec or an ElGamalParameterSpec");
268: }
269:
270: if (params instanceof ElGamalParameterSpec) {
271: ElGamalParameterSpec elParams = (ElGamalParameterSpec) params;
272:
273: param = new ElGamalKeyGenerationParameters(random,
274: new ElGamalParameters(elParams.getP(), elParams
275: .getG()));
276: } else {
277: DHParameterSpec dhParams = (DHParameterSpec) params;
278:
279: param = new ElGamalKeyGenerationParameters(random,
280: new ElGamalParameters(dhParams.getP(), dhParams
281: .getG(), dhParams.getL()));
282: }
283:
284: engine.init(param);
285: initialised = true;
286: }
287:
288: public KeyPair generateKeyPair() {
289: if (!initialised) {
290: ElGamalParametersGenerator pGen = new ElGamalParametersGenerator();
291:
292: pGen.init(strength, certainty, random);
293: param = new ElGamalKeyGenerationParameters(random, pGen
294: .generateParameters());
295: engine.init(param);
296: initialised = true;
297: }
298:
299: AsymmetricCipherKeyPair pair = engine.generateKeyPair();
300: ElGamalPublicKeyParameters pub = (ElGamalPublicKeyParameters) pair
301: .getPublic();
302: ElGamalPrivateKeyParameters priv = (ElGamalPrivateKeyParameters) pair
303: .getPrivate();
304:
305: return new KeyPair(new JCEElGamalPublicKey(pub),
306: new JCEElGamalPrivateKey(priv));
307: }
308: }
309:
310: public static class GOST3410 extends JDKKeyPairGenerator {
311: GOST3410KeyGenerationParameters param;
312: GOST3410KeyPairGenerator engine = new GOST3410KeyPairGenerator();
313: GOST3410ParameterSpec gost3410Params;
314: int strength = 1024;
315: SecureRandom random = null;
316: boolean initialised = false;
317:
318: public GOST3410() {
319: super ("GOST3410");
320: }
321:
322: public void initialize(int strength, SecureRandom random) {
323: this .strength = strength;
324: this .random = random;
325: }
326:
327: private void init(GOST3410ParameterSpec gParams,
328: SecureRandom random) {
329: GOST3410PublicKeyParameterSetSpec spec = gParams
330: .getPublicKeyParameters();
331:
332: param = new GOST3410KeyGenerationParameters(random,
333: new GOST3410Parameters(spec.getP(), spec.getQ(),
334: spec.getA()));
335:
336: engine.init(param);
337:
338: initialised = true;
339: gost3410Params = gParams;
340: }
341:
342: public void initialize(AlgorithmParameterSpec params,
343: SecureRandom random)
344: throws InvalidAlgorithmParameterException {
345: if (!(params instanceof GOST3410ParameterSpec)) {
346: throw new InvalidAlgorithmParameterException(
347: "parameter object not a GOST3410ParameterSpec");
348: }
349:
350: init((GOST3410ParameterSpec) params, random);
351: }
352:
353: public KeyPair generateKeyPair() {
354: if (!initialised) {
355: init(
356: new GOST3410ParameterSpec(
357: CryptoProObjectIdentifiers.gostR3410_94_CryptoPro_A
358: .getId()), new SecureRandom());
359: }
360:
361: AsymmetricCipherKeyPair pair = engine.generateKeyPair();
362: GOST3410PublicKeyParameters pub = (GOST3410PublicKeyParameters) pair
363: .getPublic();
364: GOST3410PrivateKeyParameters priv = (GOST3410PrivateKeyParameters) pair
365: .getPrivate();
366:
367: return new KeyPair(new JDKGOST3410PublicKey(pub,
368: gost3410Params), new JDKGOST3410PrivateKey(priv,
369: gost3410Params));
370: }
371: }
372:
373: public static class EC extends JDKKeyPairGenerator {
374: ECKeyGenerationParameters param;
375: ECKeyPairGenerator engine = new ECKeyPairGenerator();
376: Object ecParams = null;
377: int strength = 239;
378: int certainty = 50;
379: SecureRandom random = new SecureRandom();
380: boolean initialised = false;
381: String algorithm;
382:
383: static private Hashtable ecParameters;
384:
385: static {
386: ecParameters = new Hashtable();
387:
388: ecParameters.put(new Integer(192), new ECGenParameterSpec(
389: "prime192v1"));
390: ecParameters.put(new Integer(239), new ECGenParameterSpec(
391: "prime239v1"));
392: ecParameters.put(new Integer(256), new ECGenParameterSpec(
393: "prime256v1"));
394: }
395:
396: public EC() {
397: super ("EC");
398: this .algorithm = "EC";
399: }
400:
401: public EC(String algorithm) {
402: super (algorithm);
403: this .algorithm = algorithm;
404: }
405:
406: public void initialize(int strength, SecureRandom random) {
407: this .strength = strength;
408: this .random = random;
409: this .ecParams = ecParameters.get(new Integer(strength));
410:
411: if (ecParams != null) {
412: try {
413: initialize((ECGenParameterSpec) ecParams, random);
414: } catch (InvalidAlgorithmParameterException e) {
415: throw new InvalidParameterException(
416: "key size not configurable.");
417: }
418: } else {
419: throw new InvalidParameterException("unknown key size.");
420: }
421: }
422:
423: public void initialize(AlgorithmParameterSpec params,
424: SecureRandom random)
425: throws InvalidAlgorithmParameterException {
426: if (params instanceof ECParameterSpec) {
427: ECParameterSpec p = (ECParameterSpec) params;
428: this .ecParams = params;
429:
430: param = new ECKeyGenerationParameters(
431: new ECDomainParameters(p.getCurve(), p.getG(),
432: p.getN()), random);
433:
434: engine.init(param);
435: initialised = true;
436: } else if (params instanceof java.security.spec.ECParameterSpec) {
437: java.security.spec.ECParameterSpec p = (java.security.spec.ECParameterSpec) params;
438: this .ecParams = params;
439:
440: ECCurve curve = EC5Util.convertCurve(p.getCurve());
441: ECPoint g = EC5Util.convertPoint(curve, p
442: .getGenerator(), false);
443:
444: param = new ECKeyGenerationParameters(
445: new ECDomainParameters(curve, g, p.getOrder(),
446: BigInteger.valueOf(p.getCofactor())),
447: random);
448:
449: engine.init(param);
450: initialised = true;
451: } else if (params instanceof ECGenParameterSpec) {
452: if (this .algorithm.equals("ECGOST3410")) {
453: ECDomainParameters ecP = ECGOST3410NamedCurves
454: .getByName(((ECGenParameterSpec) params)
455: .getName());
456: if (ecP == null) {
457: throw new InvalidAlgorithmParameterException(
458: "unknown curve name: "
459: + ((ECGenParameterSpec) params)
460: .getName());
461: }
462:
463: this .ecParams = new ECNamedCurveSpec(
464: ((ECGenParameterSpec) params).getName(),
465: ecP.getCurve(), ecP.getG(), ecP.getN(), ecP
466: .getH(), ecP.getSeed());
467: } else {
468: X9ECParameters ecP = X962NamedCurves
469: .getByName(((ECGenParameterSpec) params)
470: .getName());
471: if (ecP == null) {
472: ecP = SECNamedCurves
473: .getByName(((ECGenParameterSpec) params)
474: .getName());
475: if (ecP == null) {
476: ecP = NISTNamedCurves
477: .getByName(((ECGenParameterSpec) params)
478: .getName());
479: }
480: if (ecP == null) {
481: ecP = TeleTrusTNamedCurves
482: .getByName(((ECGenParameterSpec) params)
483: .getName());
484: }
485: if (ecP == null) {
486: throw new InvalidAlgorithmParameterException(
487: "unknown curve name: "
488: + ((ECGenParameterSpec) params)
489: .getName());
490: }
491: }
492:
493: this .ecParams = new ECNamedCurveSpec(
494: ((ECGenParameterSpec) params).getName(),
495: ecP.getCurve(), ecP.getG(), ecP.getN(), ecP
496: .getH(), ecP.getSeed());
497: }
498:
499: java.security.spec.ECParameterSpec p = (java.security.spec.ECParameterSpec) ecParams;
500:
501: ECCurve curve = EC5Util.convertCurve(p.getCurve());
502: ECPoint g = EC5Util.convertPoint(curve, p
503: .getGenerator(), false);
504:
505: param = new ECKeyGenerationParameters(
506: new ECDomainParameters(curve, g, p.getOrder(),
507: BigInteger.valueOf(p.getCofactor())),
508: random);
509:
510: engine.init(param);
511: initialised = true;
512: } else if (params == null
513: && ProviderUtil.getEcImplicitlyCa() != null) {
514: ECParameterSpec p = ProviderUtil.getEcImplicitlyCa();
515: this .ecParams = params;
516:
517: param = new ECKeyGenerationParameters(
518: new ECDomainParameters(p.getCurve(), p.getG(),
519: p.getN()), random);
520:
521: engine.init(param);
522: initialised = true;
523: } else if (params == null
524: && ProviderUtil.getEcImplicitlyCa() == null) {
525: throw new InvalidAlgorithmParameterException(
526: "null parameter passed by no implicitCA set");
527: } else {
528: throw new InvalidAlgorithmParameterException(
529: "parameter object not a ECParameterSpec");
530: }
531: }
532:
533: public KeyPair generateKeyPair() {
534: if (!initialised) {
535: throw new IllegalStateException(
536: "EC Key Pair Generator not initialised");
537: }
538:
539: AsymmetricCipherKeyPair pair = engine.generateKeyPair();
540: ECPublicKeyParameters pub = (ECPublicKeyParameters) pair
541: .getPublic();
542: ECPrivateKeyParameters priv = (ECPrivateKeyParameters) pair
543: .getPrivate();
544:
545: if (ecParams instanceof ECParameterSpec) {
546: ECParameterSpec p = (ECParameterSpec) ecParams;
547:
548: return new KeyPair(
549: new JCEECPublicKey(algorithm, pub, p),
550: new JCEECPrivateKey(algorithm, priv, p));
551: } else if (ecParams == null) {
552: return new KeyPair(new JCEECPublicKey(algorithm, pub),
553: new JCEECPrivateKey(algorithm, priv));
554: } else {
555: java.security.spec.ECParameterSpec p = (java.security.spec.ECParameterSpec) ecParams;
556:
557: return new KeyPair(
558: new JCEECPublicKey(algorithm, pub, p),
559: new JCEECPrivateKey(algorithm, priv, p));
560: }
561: }
562: }
563:
564: public static class ECDSA extends EC {
565: public ECDSA() {
566: super ("ECDSA");
567: }
568: }
569:
570: public static class ECGOST3410 extends EC {
571: public ECGOST3410() {
572: super ("ECGOST3410");
573: }
574: }
575:
576: public static class ECDH extends EC {
577: public ECDH() {
578: super ("ECDH");
579: }
580: }
581:
582: public static class ECDHC extends EC {
583: public ECDHC() {
584: super ("ECDHC");
585: }
586: }
587: }
|