Class AbstractDH

  • Direct Known Subclasses:
    DHG, ECDH, XDH

    public abstract class AbstractDH
    extends java.lang.Object
    Base class for the Diffie-Hellman key agreement.
    • Field Detail

      • myKeyAgree

        protected javax.crypto.KeyAgreement myKeyAgree
      • k_array

        private byte[] k_array
      • e_array

        private byte[] e_array
    • Constructor Detail

      • AbstractDH

        protected AbstractDH()
    • Method Detail

      • setF

        public abstract void setF​(byte[] f)
      • isPublicDataAvailable

        public boolean isPublicDataAvailable()
      • calculateE

        protected abstract byte[] calculateE()
                                      throws java.lang.Exception
        Lazy-called by getE() if the public key data has not been generated yet.
        Returns:
        The calculated public key data
        Throws:
        java.lang.Exception - If failed to generate the relevant data
      • getE

        public byte[] getE()
                    throws java.lang.Exception
        Returns:
        The local public key data
        Throws:
        java.lang.Exception - If failed to calculate it
      • putE

        public void putE​(Buffer buffer,
                         byte[] e)
      • putF

        public void putF​(Buffer buffer,
                         byte[] f)
      • isSharedSecretAvailable

        public boolean isSharedSecretAvailable()
      • calculateK

        protected abstract byte[] calculateK()
                                      throws java.lang.Exception
        Lazy-called by getK() if the shared secret data has not been calculated yet
        Returns:
        The shared secret data
        Throws:
        java.lang.Exception - If failed to calculate it
      • getK

        public byte[] getK()
                    throws java.lang.Exception
        Returns:
        The shared secret key
        Throws:
        java.lang.Exception - If failed to calculate it
      • checkKeyAgreementNecessity

        protected void checkKeyAgreementNecessity()
        Called after either public or private parts have been calculated in order to check if the key-agreement mediator is still required. By default, if both public and private parts have been calculated then key-agreement mediator is null-ified to enable GC for it.
        See Also:
        getE(), getK()
      • getHash

        public abstract Digest getHash()
                                throws java.lang.Exception
        Throws:
        java.lang.Exception
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • stripLeadingZeroes

        public static byte[] stripLeadingZeroes​(byte[] x)
        The shared secret returned by KeyAgreement.generateSecret() is a byte array, which can (by chance, roughly 1 out of 256 times) begin with zero byte (some JCE providers might strip this, though). In SSH, the shared secret is an integer, so we need to strip the leading zero(es).
        Parameters:
        x - The original array
        Returns:
        An (possibly) sub-array guaranteed to start with a non-zero byte
        Throws:
        java.lang.IllegalArgumentException - If all zeroes array
        See Also:
        SSHD-330