67 |
/** The private key being used for this agreement. */ |
/** The private key being used for this agreement. */ |
68 |
private DHPrivateKey key; |
private DHPrivateKey key; |
69 |
|
|
|
/** The random-number generator used to generate secrets. */ |
|
|
private SecureRandom random; |
|
|
|
|
70 |
/** The current result. */ |
/** The current result. */ |
71 |
private BigInteger result; |
private BigInteger result; |
72 |
|
|
77 |
public DiffieHellmanImpl () |
public DiffieHellmanImpl () |
78 |
{ |
{ |
79 |
key = null; |
key = null; |
|
random = null; |
|
80 |
result = null; |
result = null; |
81 |
last_phase_done = false; |
last_phase_done = false; |
82 |
} |
} |
95 |
throw new InvalidKeyException ("expecting javax.crypto.interfaces.DHPublicKey"); |
throw new InvalidKeyException ("expecting javax.crypto.interfaces.DHPublicKey"); |
96 |
DHPublicKey pub = (DHPublicKey) incoming; |
DHPublicKey pub = (DHPublicKey) incoming; |
97 |
DHParameterSpec s1 = key.getParams(); |
DHParameterSpec s1 = key.getParams(); |
98 |
DHParameterSpec s2 = key.getParams(); |
DHParameterSpec s2 = pub.getParams(); |
99 |
if (!s1.getG().equals (s2.getG()) |
if (!s1.getG().equals (s2.getG()) |
100 |
|| !s1.getP().equals (s2.getP()) |
|| !s1.getP().equals (s2.getP()) |
101 |
|| s1.getL() != s2.getL()) |
|| s1.getL() != s2.getL()) |
102 |
throw new InvalidKeyException ("supplied key is not compatible"); |
throw new InvalidKeyException ("supplied key is not compatible"); |
103 |
|
|
104 |
BigInteger randval = new BigInteger (s1.getL(), random); |
result = pub.getY().modPow (key.getX(), s1.getP()); |
|
BigInteger out = s1.getG().modPow (key.getX(), s1.getP()); |
|
|
if (result == null) |
|
|
result = s1.getG(); |
|
|
result = result.modPow (pub.getY(), s1.getP()); |
|
105 |
if (lastPhase) |
if (lastPhase) |
106 |
{ |
{ |
107 |
last_phase_done = true; |
last_phase_done = true; |
108 |
return null; |
return null; |
109 |
} |
} |
110 |
return new GnuDHPublicKey (s1, out, null); |
|
111 |
|
throw new IllegalArgumentException ("only supports two-party Diffie Hellman"); |
112 |
} |
} |
113 |
|
|
114 |
protected byte[] engineGenerateSecret () |
protected byte[] engineGenerateSecret () |
146 |
if (!(key instanceof DHPrivateKey)) |
if (!(key instanceof DHPrivateKey)) |
147 |
throw new InvalidKeyException ("not a javax.crypto.interfaces.DHPrivateKey"); |
throw new InvalidKeyException ("not a javax.crypto.interfaces.DHPrivateKey"); |
148 |
this.key = (DHPrivateKey) key; |
this.key = (DHPrivateKey) key; |
|
if (random != null) |
|
|
this.random = random; |
|
|
else if (this.random == null) |
|
|
this.random = new SecureRandom(); |
|
149 |
result = null; |
result = null; |
150 |
last_phase_done = false; |
last_phase_done = false; |
151 |
} |
} |