24 */
25
26 package com.sun.crypto.provider;
27
28 import java.util.*;
29 import java.lang.*;
30 import java.math.BigInteger;
31 import java.security.InvalidAlgorithmParameterException;
32 import java.security.InvalidKeyException;
33 import java.security.Key;
34 import java.security.NoSuchAlgorithmException;
35 import java.security.SecureRandom;
36 import java.security.ProviderException;
37 import java.security.spec.AlgorithmParameterSpec;
38 import java.security.spec.InvalidKeySpecException;
39 import javax.crypto.KeyAgreementSpi;
40 import javax.crypto.ShortBufferException;
41 import javax.crypto.SecretKey;
42 import javax.crypto.spec.*;
43
44 /**
45 * This class implements the Diffie-Hellman key agreement protocol between
46 * any number of parties.
47 *
48 * @author Jan Luehe
49 *
50 */
51
52 public final class DHKeyAgreement
53 extends KeyAgreementSpi {
54
55 private boolean generateSecret = false;
56 private BigInteger init_p = null;
57 private BigInteger init_g = null;
58 private BigInteger x = BigInteger.ZERO; // the private value
59 private BigInteger y = BigInteger.ZERO;
60
61 /**
62 * Empty constructor
63 */
182 throw new InvalidKeyException("Diffie-Hellman public key "
183 + "expected");
184 }
185 javax.crypto.interfaces.DHPublicKey dhPubKey;
186 dhPubKey = (javax.crypto.interfaces.DHPublicKey)key;
187
188 if (init_p == null || init_g == null) {
189 throw new IllegalStateException("Not initialized");
190 }
191
192 // check if public key parameters are compatible with
193 // initialized ones
194 BigInteger pub_p = dhPubKey.getParams().getP();
195 BigInteger pub_g = dhPubKey.getParams().getG();
196 if (pub_p != null && !(init_p.equals(pub_p))) {
197 throw new InvalidKeyException("Incompatible parameters");
198 }
199 if (pub_g != null && !(init_g.equals(pub_g))) {
200 throw new InvalidKeyException("Incompatible parameters");
201 }
202
203 // store the y value
204 this.y = dhPubKey.getY();
205
206 // we've received a public key (from one of the other parties),
207 // so we are ready to create the secret, which may be an
208 // intermediate secret, in which case we wrap it into a
209 // Diffie-Hellman public key object and return it.
210 generateSecret = true;
211 if (lastPhase == false) {
212 byte[] intermediate = engineGenerateSecret();
213 return new DHPublicKey(new BigInteger(1, intermediate),
214 init_p, init_g);
215 } else {
216 return null;
217 }
218 }
219
220 /**
221 * Generates the shared secret and returns it in a new buffer.
|
24 */
25
26 package com.sun.crypto.provider;
27
28 import java.util.*;
29 import java.lang.*;
30 import java.math.BigInteger;
31 import java.security.InvalidAlgorithmParameterException;
32 import java.security.InvalidKeyException;
33 import java.security.Key;
34 import java.security.NoSuchAlgorithmException;
35 import java.security.SecureRandom;
36 import java.security.ProviderException;
37 import java.security.spec.AlgorithmParameterSpec;
38 import java.security.spec.InvalidKeySpecException;
39 import javax.crypto.KeyAgreementSpi;
40 import javax.crypto.ShortBufferException;
41 import javax.crypto.SecretKey;
42 import javax.crypto.spec.*;
43
44 import sun.security.util.KeyUtil;
45
46 /**
47 * This class implements the Diffie-Hellman key agreement protocol between
48 * any number of parties.
49 *
50 * @author Jan Luehe
51 *
52 */
53
54 public final class DHKeyAgreement
55 extends KeyAgreementSpi {
56
57 private boolean generateSecret = false;
58 private BigInteger init_p = null;
59 private BigInteger init_g = null;
60 private BigInteger x = BigInteger.ZERO; // the private value
61 private BigInteger y = BigInteger.ZERO;
62
63 /**
64 * Empty constructor
65 */
184 throw new InvalidKeyException("Diffie-Hellman public key "
185 + "expected");
186 }
187 javax.crypto.interfaces.DHPublicKey dhPubKey;
188 dhPubKey = (javax.crypto.interfaces.DHPublicKey)key;
189
190 if (init_p == null || init_g == null) {
191 throw new IllegalStateException("Not initialized");
192 }
193
194 // check if public key parameters are compatible with
195 // initialized ones
196 BigInteger pub_p = dhPubKey.getParams().getP();
197 BigInteger pub_g = dhPubKey.getParams().getG();
198 if (pub_p != null && !(init_p.equals(pub_p))) {
199 throw new InvalidKeyException("Incompatible parameters");
200 }
201 if (pub_g != null && !(init_g.equals(pub_g))) {
202 throw new InvalidKeyException("Incompatible parameters");
203 }
204
205 // validate the Diffie-Hellman public key
206 KeyUtil.validate(dhPubKey);
207
208 // store the y value
209 this.y = dhPubKey.getY();
210
211 // we've received a public key (from one of the other parties),
212 // so we are ready to create the secret, which may be an
213 // intermediate secret, in which case we wrap it into a
214 // Diffie-Hellman public key object and return it.
215 generateSecret = true;
216 if (lastPhase == false) {
217 byte[] intermediate = engineGenerateSecret();
218 return new DHPublicKey(new BigInteger(1, intermediate),
219 init_p, init_g);
220 } else {
221 return null;
222 }
223 }
224
225 /**
226 * Generates the shared secret and returns it in a new buffer.
|