diff options
author | Kenny Root <kenny@the-b.org> | 2015-07-18 11:30:18 -0700 |
---|---|---|
committer | Kenny Root <kenny@the-b.org> | 2015-07-18 11:30:18 -0700 |
commit | 2f5f3754dce85212a71138fd80c2300b73461908 (patch) | |
tree | ac51a1c9f143bc77a0cfec2e7da1b7c184394a98 /sshlib/src/main/java/com/trilead/ssh2/crypto/dh/DhGroupExchange.java | |
parent | 0cf7ac30faecc82e04b080f418b08758624b07f5 (diff) | |
download | sshlib-2f5f3754dce85212a71138fd80c2300b73461908.tar.gz sshlib-2f5f3754dce85212a71138fd80c2300b73461908.tar.bz2 sshlib-2f5f3754dce85212a71138fd80c2300b73461908.zip |
Rename project to sshlib
Diffstat (limited to 'sshlib/src/main/java/com/trilead/ssh2/crypto/dh/DhGroupExchange.java')
-rw-r--r-- | sshlib/src/main/java/com/trilead/ssh2/crypto/dh/DhGroupExchange.java | 113 |
1 files changed, 113 insertions, 0 deletions
diff --git a/sshlib/src/main/java/com/trilead/ssh2/crypto/dh/DhGroupExchange.java b/sshlib/src/main/java/com/trilead/ssh2/crypto/dh/DhGroupExchange.java new file mode 100644 index 0000000..a888950 --- /dev/null +++ b/sshlib/src/main/java/com/trilead/ssh2/crypto/dh/DhGroupExchange.java @@ -0,0 +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(String hashAlgo, byte[] clientversion, byte[] serverversion, + byte[] clientKexPayload, byte[] serverKexPayload, byte[] hostKey, DHGexParameters para) + { + HashForSSH2Types hash = new HashForSSH2Types(hashAlgo); + + 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(); + } +} |