aboutsummaryrefslogtreecommitdiffstats
path: root/src/com/trilead/ssh2/crypto/dh/GenericDhExchange.java
blob: 039ff75a3e4e21ffbd7043f5c897898007e62f19 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package com.trilead.ssh2.crypto.dh;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;

import com.trilead.ssh2.crypto.digest.HashForSSH2Types;
import com.trilead.ssh2.log.Logger;


/**
 * DhExchange.
 *
 * @author Christian Plattner, plattner@trilead.com
 * @version $Id: DhExchange.java,v 1.2 2008/04/01 12:38:09 cplattne Exp $
 */
public abstract class GenericDhExchange
{
	private static final Logger log = Logger.getLogger(GenericDhExchange.class);

	/* Shared secret */

	BigInteger sharedSecret;

	protected GenericDhExchange()
	{
	}

	public static GenericDhExchange getInstance(String algo) {
		if (algo.startsWith("ecdh-sha2-")) {
			return new EcDhExchange();
		} else {
			return new DhExchange();
		}
	}

	public abstract void init(String name) throws IOException;

	/**
	 * @return Returns the e (public value)
	 * @throws IllegalStateException
	 */
	public abstract byte[] getE();

	/**
	 * @return Returns the server's e (public value)
	 * @throws IllegalStateException
	 */
	protected abstract byte[] getServerE();

	/**
	 * @return Returns the shared secret k.
	 * @throws IllegalStateException
	 */
	public BigInteger getK()
	{
		if (sharedSecret == null)
			throw new IllegalStateException("Shared secret not yet known, need f first!");

		return sharedSecret;
	}

	/**
	 * @param f
	 */
	public abstract void setF(byte[] f) throws IOException;

	public byte[] calculateH(byte[] clientversion, byte[] serverversion, byte[] clientKexPayload,
			byte[] serverKexPayload, byte[] hostKey) throws UnsupportedEncodingException
	{
		HashForSSH2Types hash = new HashForSSH2Types(getHashAlgo());

		if (log.isEnabled())
		{
			log.log(90, "Client: '" + new String(clientversion, "ISO-8859-1") + "'");
			log.log(90, "Server: '" + new String(serverversion, "ISO-8859-1") + "'");
		}

		hash.updateByteString(clientversion);
		hash.updateByteString(serverversion);
		hash.updateByteString(clientKexPayload);
		hash.updateByteString(serverKexPayload);
		hash.updateByteString(hostKey);
		hash.updateByteString(getE());
		hash.updateByteString(getServerE());
		hash.updateBigInt(sharedSecret);

		return hash.getDigest();
	}

	public abstract String getHashAlgo();
}