Class AbstractDH

  • Direct Known Subclasses:
    DHG, ECDH

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

      Fields 
      Modifier and Type Field Description
      private byte[] e_array  
      private byte[] k_array  
      protected javax.crypto.KeyAgreement myKeyAgree  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected AbstractDH()  
    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      protected abstract byte[] calculateE()
      Lazy-called by getE() if the public key data has not been generated yet.
      protected abstract byte[] calculateK()
      Lazy-called by getK() if the shared secret data has not been calculated yet
      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.
      byte[] getE()  
      abstract Digest getHash()  
      byte[] getK()  
      boolean isPublicDataAvailable()  
      boolean isSharedSecretAvailable()  
      abstract void setF​(byte[] f)  
      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).
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • 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
      • 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