view src/ch/ethz/ssh2/crypto/dh/GenericDhExchange.java @ 310:9e42082d1f93 ganymed

add ecdsa key support everywhere
author Carl Byington <carl@five-ten-sg.com>
date Wed, 30 Jul 2014 14:42:55 -0700
parents cb179051f0f2
children fde9d6568bea
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();

    /**
     * @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 abstract void setF(byte[] f) throws IOException;

    public byte[] calculateH(byte[] clientversion, byte[] serverversion, byte[] clientKexPayload,
                             byte[] serverKexPayload, byte[] hostKey) throws UnsupportedEncodingException {
        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();
}