view src/ch/ethz/ssh2/crypto/dh/GenericDhExchange.java @ 337:0d0d2fc9918c ganymed

merge
author Carl Byington <carl@five-ten-sg.com>
date Thu, 31 Jul 2014 15:00:28 -0700
parents 6740870cf268
children
line wrap: on
line source


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();
}