diff options
Diffstat (limited to 'lib/src/main/java/com/trilead/ssh2/crypto/dh')
-rw-r--r-- | lib/src/main/java/com/trilead/ssh2/crypto/dh/DhExchange.java | 56 | ||||
-rw-r--r-- | lib/src/main/java/com/trilead/ssh2/crypto/dh/DhGroupExchange.java | 226 |
2 files changed, 141 insertions, 141 deletions
diff --git a/lib/src/main/java/com/trilead/ssh2/crypto/dh/DhExchange.java b/lib/src/main/java/com/trilead/ssh2/crypto/dh/DhExchange.java index d5bb112..3acde25 100644 --- a/lib/src/main/java/com/trilead/ssh2/crypto/dh/DhExchange.java +++ b/lib/src/main/java/com/trilead/ssh2/crypto/dh/DhExchange.java @@ -1,10 +1,10 @@ /** * */ -package com.trilead.ssh2.crypto.dh;
-
+package com.trilead.ssh2.crypto.dh; + import java.io.IOException; -import java.math.BigInteger;
+import java.math.BigInteger; import java.security.InvalidAlgorithmParameterException; import java.security.InvalidKeyException; import java.security.KeyFactory; @@ -18,15 +18,15 @@ import javax.crypto.interfaces.DHPrivateKey; import javax.crypto.interfaces.DHPublicKey; import javax.crypto.spec.DHParameterSpec; import javax.crypto.spec.DHPublicKeySpec; -
-/**
+ +/** * @author kenny * - */
+ */ public class DhExchange extends GenericDhExchange { -
- /* Given by the standard */
-
+ + /* Given by the standard */ + private static final BigInteger P1 = new BigInteger( "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" + "29024E088A67CC74020BBEA63B139B22514A08798E3404DD" @@ -49,14 +49,14 @@ public class DhExchange extends GenericDhExchange { + "15728E5A8AACAA68FFFFFFFFFFFFFFFF", 16); private static final BigInteger G = BigInteger.valueOf(2); -
- /* Client public and private */
-
+ + /* Client public and private */ + private DHPrivateKey clientPrivate; private DHPublicKey clientPublic; -
- /* Server public */
-
+ + /* Server public */ + private DHPublicKey serverPublic; @Override @@ -81,24 +81,24 @@ public class DhExchange extends GenericDhExchange { } catch (InvalidAlgorithmParameterException e) { throw (IOException) new IOException("Invalid DH parameters").initCause(e); } - }
-
+ } + @Override public byte[] getE() { if (clientPublic == null) throw new IllegalStateException("DhExchange not initialized!"); -
+ return clientPublic.getY().toByteArray(); - }
-
+ } + @Override protected byte[] getServerE() { if (serverPublic == null) throw new IllegalStateException("DhExchange not initialized!"); -
+ return serverPublic.getY().toByteArray(); - }
-
+ } + @Override public void setF(byte[] f) throws IOException { if (clientPublic == null) @@ -120,13 +120,13 @@ public class DhExchange extends GenericDhExchange { throw (IOException) new IOException("Invalid DH key").initCause(e); } catch (InvalidKeySpecException e) { throw (IOException) new IOException("Invalid DH key").initCause(e); - }
-
+ } + sharedSecret = new BigInteger(ka.generateSecret()); } -
+ @Override public String getHashAlgo() { return "SHA1"; - }
-}
+ } +} diff --git a/lib/src/main/java/com/trilead/ssh2/crypto/dh/DhGroupExchange.java b/lib/src/main/java/com/trilead/ssh2/crypto/dh/DhGroupExchange.java index 8e798ee..2922284 100644 --- a/lib/src/main/java/com/trilead/ssh2/crypto/dh/DhGroupExchange.java +++ b/lib/src/main/java/com/trilead/ssh2/crypto/dh/DhGroupExchange.java @@ -1,113 +1,113 @@ -
-package com.trilead.ssh2.crypto.dh;
-
-import java.math.BigInteger;
-import java.security.SecureRandom;
-
-import com.trilead.ssh2.DHGexParameters;
-import com.trilead.ssh2.crypto.digest.HashForSSH2Types;
-
-
-/**
- * DhGroupExchange.
- *
- * @author Christian Plattner, plattner@trilead.com
- * @version $Id: DhGroupExchange.java,v 1.1 2007/10/15 12:49:57 cplattne Exp $
- */
-public class DhGroupExchange
-{
- /* Given by the standard */
-
- private BigInteger p;
- private BigInteger g;
-
- /* Client public and private */
-
- private BigInteger e;
- private BigInteger x;
-
- /* Server public */
-
- private BigInteger f;
-
- /* Shared secret */
-
- private BigInteger k;
-
- public DhGroupExchange(BigInteger p, BigInteger g)
- {
- this.p = p;
- this.g = g;
- }
-
- public void init(SecureRandom rnd)
- {
- k = null;
-
- x = new BigInteger(p.bitLength() - 1, rnd);
- e = g.modPow(x, p);
- }
-
- /**
- * @return Returns the e.
- */
- public BigInteger getE()
- {
- if (e == null)
- throw new IllegalStateException("Not initialized!");
-
- return e;
- }
-
- /**
- * @return Returns the shared secret k.
- */
- public BigInteger getK()
- {
- if (k == null)
- throw new IllegalStateException("Shared secret not yet known, need f first!");
-
- return k;
- }
-
- /**
- * Sets f and calculates the shared secret.
- */
- public void setF(BigInteger f)
- {
- if (e == null)
- throw new IllegalStateException("Not initialized!");
-
- BigInteger zero = BigInteger.valueOf(0);
-
- if (zero.compareTo(f) >= 0 || p.compareTo(f) <= 0)
- throw new IllegalArgumentException("Invalid f specified!");
-
- this.f = f;
- this.k = f.modPow(x, p);
- }
-
- public byte[] calculateH(byte[] clientversion, byte[] serverversion, byte[] clientKexPayload,
- byte[] serverKexPayload, byte[] hostKey, DHGexParameters para)
- {
- HashForSSH2Types hash = new HashForSSH2Types("SHA1");
-
- hash.updateByteString(clientversion);
- hash.updateByteString(serverversion);
- hash.updateByteString(clientKexPayload);
- hash.updateByteString(serverKexPayload);
- hash.updateByteString(hostKey);
- if (para.getMin_group_len() > 0)
- hash.updateUINT32(para.getMin_group_len());
- hash.updateUINT32(para.getPref_group_len());
- if (para.getMax_group_len() > 0)
- hash.updateUINT32(para.getMax_group_len());
- hash.updateBigInt(p);
- hash.updateBigInt(g);
- hash.updateBigInt(e);
- hash.updateBigInt(f);
- hash.updateBigInt(k);
-
- return hash.getDigest();
- }
-}
+ +package com.trilead.ssh2.crypto.dh; + +import java.math.BigInteger; +import java.security.SecureRandom; + +import com.trilead.ssh2.DHGexParameters; +import com.trilead.ssh2.crypto.digest.HashForSSH2Types; + + +/** + * DhGroupExchange. + * + * @author Christian Plattner, plattner@trilead.com + * @version $Id: DhGroupExchange.java,v 1.1 2007/10/15 12:49:57 cplattne Exp $ + */ +public class DhGroupExchange +{ + /* Given by the standard */ + + private BigInteger p; + private BigInteger g; + + /* Client public and private */ + + private BigInteger e; + private BigInteger x; + + /* Server public */ + + private BigInteger f; + + /* Shared secret */ + + private BigInteger k; + + public DhGroupExchange(BigInteger p, BigInteger g) + { + this.p = p; + this.g = g; + } + + public void init(SecureRandom rnd) + { + k = null; + + x = new BigInteger(p.bitLength() - 1, rnd); + e = g.modPow(x, p); + } + + /** + * @return Returns the e. + */ + public BigInteger getE() + { + if (e == null) + throw new IllegalStateException("Not initialized!"); + + return e; + } + + /** + * @return Returns the shared secret k. + */ + public BigInteger getK() + { + if (k == null) + throw new IllegalStateException("Shared secret not yet known, need f first!"); + + return k; + } + + /** + * Sets f and calculates the shared secret. + */ + public void setF(BigInteger f) + { + if (e == null) + throw new IllegalStateException("Not initialized!"); + + BigInteger zero = BigInteger.valueOf(0); + + if (zero.compareTo(f) >= 0 || p.compareTo(f) <= 0) + throw new IllegalArgumentException("Invalid f specified!"); + + this.f = f; + this.k = f.modPow(x, p); + } + + public byte[] calculateH(byte[] clientversion, byte[] serverversion, byte[] clientKexPayload, + byte[] serverKexPayload, byte[] hostKey, DHGexParameters para) + { + HashForSSH2Types hash = new HashForSSH2Types("SHA1"); + + hash.updateByteString(clientversion); + hash.updateByteString(serverversion); + hash.updateByteString(clientKexPayload); + hash.updateByteString(serverKexPayload); + hash.updateByteString(hostKey); + if (para.getMin_group_len() > 0) + hash.updateUINT32(para.getMin_group_len()); + hash.updateUINT32(para.getPref_group_len()); + if (para.getMax_group_len() > 0) + hash.updateUINT32(para.getMax_group_len()); + hash.updateBigInt(p); + hash.updateBigInt(g); + hash.updateBigInt(e); + hash.updateBigInt(f); + hash.updateBigInt(k); + + return hash.getDigest(); + } +} |