diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/app/src/main/java/ch/ethz/ssh2/crypto/dh/GenericDhExchange.java	Thu Dec 03 11:23:55 2015 -0800
@@ -0,0 +1,94 @@
+
+package ch.ethz.ssh2.crypto.dh;
+
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+import java.math.BigInteger;
+
+import ch.ethz.ssh2.crypto.digest.HashForSSH2Types;
+import ch.ethz.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();
+
+    public void setE(BigInteger e)  throws IOException {
+        throw new IOException();
+    }
+
+    /**
+     * @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 void setF(BigInteger f) throws IOException {
+        setF(f.toByteArray());
+    }
+
+    public abstract byte[] getF();
+
+    public abstract void setF(byte[] f) throws IOException;
+
+    public byte[] calculateH(byte[] clientversion, byte[] serverversion, byte[] clientKexPayload,
+                             byte[] serverKexPayload, byte[] hostKey) throws UnsupportedEncodingException, IOException {
+        HashForSSH2Types hash = new HashForSSH2Types(getHashAlgo());
+        log.debug("Client: '" + new String(clientversion, "ISO-8859-1") + "'");
+        log.debug("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();
+}