annotate app/src/main/java/ch/ethz/ssh2/crypto/dh/GenericDhExchange.java @ 493:0a17c6e7cb0f stable-1.9.4-3

improve Makefile
author Carl Byington <carl@five-ten-sg.com>
date Tue, 15 Feb 2022 14:07:00 -0800
parents d29cce60f393
children
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
315
eeb7ae13833b add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents: 314
diff changeset
45 public void setE(BigInteger e) throws IOException {
316
6b424bb783a2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents: 315
diff changeset
46 throw new IOException();
314
d85bc45139f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents: 313
diff changeset
47 }
d85bc45139f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents: 313
diff changeset
48
309
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
49 /**
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
50 * @return Returns the server's e (public value)
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
51 * @throws IllegalStateException
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
52 */
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
53 protected abstract byte[] getServerE();
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 /**
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
56 * @return Returns the shared secret k.
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
57 * @throws IllegalStateException
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 public BigInteger getK() {
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
60 if (sharedSecret == null)
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
61 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
62
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
63 return sharedSecret;
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
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 * @param f
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
68 */
313
1d400fd78e4a add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents: 311
diff changeset
69 public void setF(BigInteger f) throws IOException {
1d400fd78e4a add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents: 311
diff changeset
70 setF(f.toByteArray());
1d400fd78e4a add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents: 311
diff changeset
71 }
1d400fd78e4a add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents: 311
diff changeset
72
1d400fd78e4a add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents: 311
diff changeset
73 public abstract byte[] getF();
1d400fd78e4a add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents: 311
diff changeset
74
309
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
75 public abstract void setF(byte[] f) throws IOException;
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
76
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
77 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
78 byte[] serverKexPayload, byte[] hostKey) throws UnsupportedEncodingException, IOException {
309
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
79 HashForSSH2Types hash = new HashForSSH2Types(getHashAlgo());
310
9e42082d1f93 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents: 309
diff changeset
80 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
81 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
82 hash.updateByteString(clientversion);
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
83 hash.updateByteString(serverversion);
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
84 hash.updateByteString(clientKexPayload);
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
85 hash.updateByteString(serverKexPayload);
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
86 hash.updateByteString(hostKey);
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
87 hash.updateByteString(getE());
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
88 hash.updateByteString(getServerE());
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
89 hash.updateBigInt(sharedSecret);
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
90 return hash.getDigest();
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
91 }
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
92
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
93 public abstract String getHashAlgo();
cb179051f0f2 add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
94 }