annotate src/ch/ethz/ssh2/crypto/dh/GenericDhExchange.java @ 312:1442be38051b ganymed

add ecdsa key support everywhere
author Carl Byington <carl@five-ten-sg.com>
date Wed, 30 Jul 2014 16:03:21 -0700
parents fde9d6568bea
children 1d400fd78e4a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
309
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
2 package ch.ethz.ssh2.crypto.dh;
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
3
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
4 import java.io.IOException;
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
5 import java.io.UnsupportedEncodingException;
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
6 import java.math.BigInteger;
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
7
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
8 import ch.ethz.ssh2.crypto.digest.HashForSSH2Types;
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
9 import ch.ethz.ssh2.log.Logger;
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
10
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
11
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
12 /**
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
13 * DhExchange.
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
14 *
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
15 * @author Christian Plattner, plattner@trilead.com
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
16 * @version $Id: DhExchange.java,v 1.2 2008/04/01 12:38:09 cplattne Exp $
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
17 */
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
18 public abstract class GenericDhExchange {
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
19 private static final Logger log = Logger.getLogger(GenericDhExchange.class);
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
20
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
21 /* Shared secret */
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
22
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
23 BigInteger sharedSecret;
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
24
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
25 protected GenericDhExchange() {
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
26 }
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
27
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
28 public static GenericDhExchange getInstance(String algo) {
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
29 if (algo.startsWith("ecdh-sha2-")) {
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
30 return new EcDhExchange();
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
31 }
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
32 else {
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
33 return new DhExchange();
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
34 }
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
35 }
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
36
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
37 public abstract void init(String name) throws IOException;
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
38
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
39 /**
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
40 * @return Returns the e (public value)
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
41 * @throws IllegalStateException
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
42 */
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
43 public abstract byte[] getE();
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
44
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
45 /**
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
46 * @return Returns the server's e (public value)
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
47 * @throws IllegalStateException
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
48 */
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
49 protected abstract byte[] getServerE();
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
50
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
51 /**
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
52 * @return Returns the shared secret k.
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
53 * @throws IllegalStateException
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
54 */
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
55 public BigInteger getK() {
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
56 if (sharedSecret == null)
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
57 throw new IllegalStateException("Shared secret not yet known, need f first!");
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
58
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
59 return sharedSecret;
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
60 }
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
61
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
62 /**
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
63 * @param f
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
64 */
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
65 public abstract void setF(byte[] f) throws IOException;
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
66
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
67 public byte[] calculateH(byte[] clientversion, byte[] serverversion, byte[] clientKexPayload,
311
fde9d6568bea add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents: 310
diff changeset
68 byte[] serverKexPayload, byte[] hostKey) throws UnsupportedEncodingException, IOException {
309
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
69 HashForSSH2Types hash = new HashForSSH2Types(getHashAlgo());
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
70
310
9e42082d1f93 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents: 309
diff changeset
71 log.debug("Client: '" + new String(clientversion, "ISO-8859-1") + "'");
9e42082d1f93 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents: 309
diff changeset
72 log.debug("Server: '" + new String(serverversion, "ISO-8859-1") + "'");
309
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
73
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
74 hash.updateByteString(clientversion);
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
75 hash.updateByteString(serverversion);
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
76 hash.updateByteString(clientKexPayload);
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
77 hash.updateByteString(serverKexPayload);
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
78 hash.updateByteString(hostKey);
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
79 hash.updateByteString(getE());
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
80 hash.updateByteString(getServerE());
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
81 hash.updateBigInt(sharedSecret);
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
82 return hash.getDigest();
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
83 }
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
84
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
85 public abstract String getHashAlgo();
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
86 }