Source code: cryptix/jce/provider/elgamal/ElGamalKeyPairGenerator.java
1 /* $Id: ElGamalKeyPairGenerator.java,v 1.3 2001/02/26 16:26:48 gelderen Exp $
2 *
3 * Copyright (C) 2000 The Cryptix Foundation Limited.
4 * All rights reserved.
5 *
6 * Use, modification, copying and distribution of this software is subject
7 * the terms and conditions of the Cryptix General Licence. You should have
8 * received a copy of the Cryptix General Licence along with this library;
9 * if not, you can download a copy from http://www.cryptix.org/ .
10 */
11 package cryptix.jce.provider.elgamal;
12
13
14 import cryptix.jce.ElGamalParams;
15
16 import java.math.BigInteger;
17
18 import java.security.InvalidAlgorithmParameterException;
19 import java.security.KeyPair;
20 import java.security.KeyPairGeneratorSpi;
21 import java.security.SecureRandom;
22
23 import java.security.spec.AlgorithmParameterSpec;
24
25
26 /**
27 * @version $Revision: 1.3 $
28 * @author Jeroen C. van Gelderen (gelderen@cryptix.org)
29 */
30 public final class ElGamalKeyPairGenerator
31 extends KeyPairGeneratorSpi
32 {
33
34 // Constants
35 //...........................................................................
36
37 private static final int
38 KEYSIZE_MIN = 384,
39 KEYSIZE_MAX = 16384,
40 KEYSIZE_DEFAULT = 1536;
41
42 private static final BigInteger
43 TWO = BigInteger.valueOf(2);
44
45
46 // Instance variables
47 //...........................................................................
48
49 private SecureRandom random;
50
51
52 private int keysize;
53
54
55 /** Initialized already? */
56 private boolean initialized = false;
57
58
59 // Constructor
60 //...........................................................................
61
62 public ElGamalKeyPairGenerator()
63 {
64 super();
65 }
66
67
68 // KeyPairGeneratorSpi methods
69 //...........................................................................
70
71 public void initialize(int keysize, SecureRandom random)
72 {
73 //ASSERT(random != null);
74
75 if( (keysize < KEYSIZE_MIN) || (keysize > KEYSIZE_MAX) )
76 throw new IllegalArgumentException(
77 "keysize: invalid size (" + keysize + ")" );
78
79 this.keysize = keysize;
80 this.random = random;
81
82 this.initialized = true;
83 }
84
85
86 public void initialize(AlgorithmParameterSpec params, SecureRandom random)
87 throws InvalidAlgorithmParameterException
88 {
89 throw new RuntimeException("NYI");
90 // don't forget:
91 //this.initialized = true;
92 }
93
94
95 public KeyPair generateKeyPair()
96 {
97 if( !this.initialized )
98 initialize();
99
100 // try and obtain precomputed parameters
101 ElGamalParams params = PrecomputedParams.get(this.keysize);
102 if(params == null)
103 {
104 // generate 'em
105 // FIXME: throw for now
106 throw new RuntimeException("NYI");
107 }
108
109 BigInteger p = params.getP();
110 BigInteger g = params.getG();
111 BigInteger xMin = TWO;
112 BigInteger xMax = p.subtract(TWO);
113 int xLen = p.bitLength();
114
115 BigInteger x, y;
116 do
117 {
118 x = new BigInteger(xLen, this.random);
119 }
120 while( (x.compareTo(xMin) == -1) || (x.compareTo(xMax) == 1) );
121
122 y = g.modPow(x, p);
123
124 ElGamalPublicKeyCryptix pub = new ElGamalPublicKeyCryptix(y, params);
125 ElGamalPrivateKeyCryptix priv = new ElGamalPrivateKeyCryptix(x, params);
126 return new KeyPair(pub, priv);
127 }
128
129
130 private void initialize()
131 {
132 initialize( KEYSIZE_DEFAULT, new SecureRandom() );
133 }
134 }