001: package org.bouncycastle.jce.provider;
002:
003: import org.bouncycastle.crypto.generators.DHParametersGenerator;
004: import org.bouncycastle.crypto.generators.DSAParametersGenerator;
005: import org.bouncycastle.crypto.generators.ElGamalParametersGenerator;
006: import org.bouncycastle.crypto.generators.GOST3410ParametersGenerator;
007: import org.bouncycastle.crypto.params.DHParameters;
008: import org.bouncycastle.crypto.params.DSAParameters;
009: import org.bouncycastle.crypto.params.ElGamalParameters;
010: import org.bouncycastle.crypto.params.GOST3410Parameters;
011: import org.bouncycastle.jce.spec.GOST3410ParameterSpec;
012: import org.bouncycastle.jce.spec.GOST3410PublicKeyParameterSetSpec;
013:
014: import javax.crypto.spec.DHGenParameterSpec;
015: import javax.crypto.spec.DHParameterSpec;
016: import javax.crypto.spec.IvParameterSpec;
017: import javax.crypto.spec.RC2ParameterSpec;
018: import java.security.AlgorithmParameterGeneratorSpi;
019: import java.security.AlgorithmParameters;
020: import java.security.InvalidAlgorithmParameterException;
021: import java.security.InvalidParameterException;
022: import java.security.SecureRandom;
023: import java.security.spec.AlgorithmParameterSpec;
024: import java.security.spec.DSAParameterSpec;
025:
026: public abstract class JDKAlgorithmParameterGenerator extends
027: AlgorithmParameterGeneratorSpi {
028: protected SecureRandom random;
029: protected int strength = 1024;
030:
031: protected void engineInit(int strength, SecureRandom random) {
032: this .strength = strength;
033: this .random = random;
034: }
035:
036: public static class DH extends JDKAlgorithmParameterGenerator {
037: private int l = 0;
038:
039: protected void engineInit(AlgorithmParameterSpec genParamSpec,
040: SecureRandom random)
041: throws InvalidAlgorithmParameterException {
042: if (!(genParamSpec instanceof DHGenParameterSpec)) {
043: throw new InvalidAlgorithmParameterException(
044: "DH parameter generator requires a DHGenParameterSpec for initialisation");
045: }
046: DHGenParameterSpec spec = (DHGenParameterSpec) genParamSpec;
047:
048: this .strength = spec.getPrimeSize();
049: this .l = spec.getExponentSize();
050: this .random = random;
051: }
052:
053: protected AlgorithmParameters engineGenerateParameters() {
054: DHParametersGenerator pGen = new DHParametersGenerator();
055:
056: if (random != null) {
057: pGen.init(strength, 20, random);
058: } else {
059: pGen.init(strength, 20, new SecureRandom());
060: }
061:
062: DHParameters p = pGen.generateParameters();
063:
064: AlgorithmParameters params;
065:
066: try {
067: params = AlgorithmParameters.getInstance("DH", "BC");
068: params.init(new DHParameterSpec(p.getP(), p.getG(), l));
069: } catch (Exception e) {
070: throw new RuntimeException(e.getMessage());
071: }
072:
073: return params;
074: }
075: }
076:
077: public static class DSA extends JDKAlgorithmParameterGenerator {
078: protected void engineInit(int strength, SecureRandom random) {
079: if (strength < 512 || strength > 1024 || strength % 64 != 0) {
080: throw new InvalidParameterException(
081: "strength must be from 512 - 1024 and a multiple of 64");
082: }
083:
084: this .strength = strength;
085: this .random = random;
086: }
087:
088: protected void engineInit(AlgorithmParameterSpec genParamSpec,
089: SecureRandom random)
090: throws InvalidAlgorithmParameterException {
091: throw new InvalidAlgorithmParameterException(
092: "No supported AlgorithmParameterSpec for DSA parameter generation.");
093: }
094:
095: protected AlgorithmParameters engineGenerateParameters() {
096: DSAParametersGenerator pGen = new DSAParametersGenerator();
097:
098: if (random != null) {
099: pGen.init(strength, 20, random);
100: } else {
101: pGen.init(strength, 20, new SecureRandom());
102: }
103:
104: DSAParameters p = pGen.generateParameters();
105:
106: AlgorithmParameters params;
107:
108: try {
109: params = AlgorithmParameters.getInstance("DSA", "BC");
110: params.init(new DSAParameterSpec(p.getP(), p.getQ(), p
111: .getG()));
112: } catch (Exception e) {
113: throw new RuntimeException(e.getMessage());
114: }
115:
116: return params;
117: }
118: }
119:
120: public static class GOST3410 extends JDKAlgorithmParameterGenerator {
121: protected void engineInit(AlgorithmParameterSpec genParamSpec,
122: SecureRandom random)
123: throws InvalidAlgorithmParameterException {
124: throw new InvalidAlgorithmParameterException(
125: "No supported AlgorithmParameterSpec for GOST3410 parameter generation.");
126: }
127:
128: protected AlgorithmParameters engineGenerateParameters() {
129: GOST3410ParametersGenerator pGen = new GOST3410ParametersGenerator();
130:
131: if (random != null) {
132: pGen.init(strength, 2, random);
133: } else {
134: pGen.init(strength, 2, new SecureRandom());
135: }
136:
137: GOST3410Parameters p = pGen.generateParameters();
138:
139: AlgorithmParameters params;
140:
141: try {
142: params = AlgorithmParameters.getInstance("GOST3410",
143: "BC");
144: params.init(new GOST3410ParameterSpec(
145: new GOST3410PublicKeyParameterSetSpec(p.getP(),
146: p.getQ(), p.getA())));
147: } catch (Exception e) {
148: throw new RuntimeException(e.getMessage());
149: }
150:
151: return params;
152: }
153: }
154:
155: public static class ElGamal extends JDKAlgorithmParameterGenerator {
156: private int l = 0;
157:
158: protected void engineInit(AlgorithmParameterSpec genParamSpec,
159: SecureRandom random)
160: throws InvalidAlgorithmParameterException {
161: if (!(genParamSpec instanceof DHGenParameterSpec)) {
162: throw new InvalidAlgorithmParameterException(
163: "DH parameter generator requires a DHGenParameterSpec for initialisation");
164: }
165: DHGenParameterSpec spec = (DHGenParameterSpec) genParamSpec;
166:
167: this .strength = spec.getPrimeSize();
168: this .l = spec.getExponentSize();
169: this .random = random;
170: }
171:
172: protected AlgorithmParameters engineGenerateParameters() {
173: ElGamalParametersGenerator pGen = new ElGamalParametersGenerator();
174:
175: if (random != null) {
176: pGen.init(strength, 20, random);
177: } else {
178: pGen.init(strength, 20, new SecureRandom());
179: }
180:
181: ElGamalParameters p = pGen.generateParameters();
182:
183: AlgorithmParameters params;
184:
185: try {
186: params = AlgorithmParameters.getInstance("ElGamal",
187: "BC");
188: params.init(new DHParameterSpec(p.getP(), p.getG(), l));
189: } catch (Exception e) {
190: throw new RuntimeException(e.getMessage());
191: }
192:
193: return params;
194: }
195: }
196:
197: public static class DES extends JDKAlgorithmParameterGenerator {
198: protected void engineInit(AlgorithmParameterSpec genParamSpec,
199: SecureRandom random)
200: throws InvalidAlgorithmParameterException {
201: throw new InvalidAlgorithmParameterException(
202: "No supported AlgorithmParameterSpec for DES parameter generation.");
203: }
204:
205: protected AlgorithmParameters engineGenerateParameters() {
206: byte[] iv = new byte[8];
207:
208: if (random == null) {
209: random = new SecureRandom();
210: }
211:
212: random.nextBytes(iv);
213:
214: AlgorithmParameters params;
215:
216: try {
217: params = AlgorithmParameters.getInstance("DES", "BC");
218: params.init(new IvParameterSpec(iv));
219: } catch (Exception e) {
220: throw new RuntimeException(e.getMessage());
221: }
222:
223: return params;
224: }
225: }
226:
227: public static class RC2 extends JDKAlgorithmParameterGenerator {
228: RC2ParameterSpec spec = null;
229:
230: protected void engineInit(AlgorithmParameterSpec genParamSpec,
231: SecureRandom random)
232: throws InvalidAlgorithmParameterException {
233: if (genParamSpec instanceof RC2ParameterSpec) {
234: spec = (RC2ParameterSpec) genParamSpec;
235: return;
236: }
237:
238: throw new InvalidAlgorithmParameterException(
239: "No supported AlgorithmParameterSpec for RC2 parameter generation.");
240: }
241:
242: protected AlgorithmParameters engineGenerateParameters() {
243: AlgorithmParameters params;
244:
245: if (spec == null) {
246: byte[] iv = new byte[8];
247:
248: if (random == null) {
249: random = new SecureRandom();
250: }
251:
252: random.nextBytes(iv);
253:
254: try {
255: params = AlgorithmParameters.getInstance("RC2",
256: "BC");
257: params.init(new IvParameterSpec(iv));
258: } catch (Exception e) {
259: throw new RuntimeException(e.getMessage());
260: }
261: } else {
262: try {
263: params = AlgorithmParameters.getInstance("RC2",
264: "BC");
265: params.init(spec);
266: } catch (Exception e) {
267: throw new RuntimeException(e.getMessage());
268: }
269: }
270:
271: return params;
272: }
273: }
274:
275: public static class IDEA extends JDKAlgorithmParameterGenerator {
276: protected void engineInit(AlgorithmParameterSpec genParamSpec,
277: SecureRandom random)
278: throws InvalidAlgorithmParameterException {
279: throw new InvalidAlgorithmParameterException(
280: "No supported AlgorithmParameterSpec for IDEA parameter generation.");
281: }
282:
283: protected AlgorithmParameters engineGenerateParameters() {
284: byte[] iv = new byte[8];
285:
286: if (random == null) {
287: random = new SecureRandom();
288: }
289:
290: random.nextBytes(iv);
291:
292: AlgorithmParameters params;
293:
294: try {
295: params = AlgorithmParameters.getInstance("IDEA", "BC");
296: params.init(new IvParameterSpec(iv));
297: } catch (Exception e) {
298: throw new RuntimeException(e.getMessage());
299: }
300:
301: return params;
302: }
303: }
304: }
|