comparison app/src/main/java/ch/ethz/ssh2/crypto/dh/GenericDhExchange.java @ 438:d29cce60f393

migrate from Eclipse to Android Studio
author Carl Byington <carl@five-ten-sg.com>
date Thu, 03 Dec 2015 11:23:55 -0800
parents src/ch/ethz/ssh2/crypto/dh/GenericDhExchange.java@6740870cf268
children
comparison
equal deleted inserted replaced
437:208b31032318 438:d29cce60f393
1
2 package ch.ethz.ssh2.crypto.dh;
3
4 import java.io.IOException;
5 import java.io.UnsupportedEncodingException;
6 import java.math.BigInteger;
7
8 import ch.ethz.ssh2.crypto.digest.HashForSSH2Types;
9 import ch.ethz.ssh2.log.Logger;
10
11
12 /**
13 * DhExchange.
14 *
15 * @author Christian Plattner, plattner@trilead.com
16 * @version $Id: DhExchange.java,v 1.2 2008/04/01 12:38:09 cplattne Exp $
17 */
18 public abstract class GenericDhExchange {
19 private static final Logger log = Logger.getLogger(GenericDhExchange.class);
20
21 /* Shared secret */
22
23 BigInteger sharedSecret;
24
25 protected GenericDhExchange() {
26 }
27
28 public static GenericDhExchange getInstance(String algo) {
29 if (algo.startsWith("ecdh-sha2-")) {
30 return new EcDhExchange();
31 }
32 else {
33 return new DhExchange();
34 }
35 }
36
37 public abstract void init(String name) throws IOException;
38
39 /**
40 * @return Returns the e (public value)
41 * @throws IllegalStateException
42 */
43 public abstract byte[] getE();
44
45 public void setE(BigInteger e) throws IOException {
46 throw new IOException();
47 }
48
49 /**
50 * @return Returns the server's e (public value)
51 * @throws IllegalStateException
52 */
53 protected abstract byte[] getServerE();
54
55 /**
56 * @return Returns the shared secret k.
57 * @throws IllegalStateException
58 */
59 public BigInteger getK() {
60 if (sharedSecret == null)
61 throw new IllegalStateException("Shared secret not yet known, need f first!");
62
63 return sharedSecret;
64 }
65
66 /**
67 * @param f
68 */
69 public void setF(BigInteger f) throws IOException {
70 setF(f.toByteArray());
71 }
72
73 public abstract byte[] getF();
74
75 public abstract void setF(byte[] f) throws IOException;
76
77 public byte[] calculateH(byte[] clientversion, byte[] serverversion, byte[] clientKexPayload,
78 byte[] serverKexPayload, byte[] hostKey) throws UnsupportedEncodingException, IOException {
79 HashForSSH2Types hash = new HashForSSH2Types(getHashAlgo());
80 log.debug("Client: '" + new String(clientversion, "ISO-8859-1") + "'");
81 log.debug("Server: '" + new String(serverversion, "ISO-8859-1") + "'");
82 hash.updateByteString(clientversion);
83 hash.updateByteString(serverversion);
84 hash.updateByteString(clientKexPayload);
85 hash.updateByteString(serverKexPayload);
86 hash.updateByteString(hostKey);
87 hash.updateByteString(getE());
88 hash.updateByteString(getServerE());
89 hash.updateBigInt(sharedSecret);
90 return hash.getDigest();
91 }
92
93 public abstract String getHashAlgo();
94 }