java.lang.Object
org.bouncycastle.crypto.digests.KeccakDigest
org.bouncycastle.crypto.digests.SHAKEDigest
All Implemented Interfaces:
Digest, ExtendedDigest, Xof

public class SHAKEDigestextends KeccakDigest implements Xof
Implementation of SHAKE based on following KeccakNISTInterface.c from keccak.team

Following the naming conventions used in the C source code to enable easy review of the implementation.

  • Field Summary

    Fields inherited from class KeccakDigest

    bitsInQueue, dataQueue, fixedOutputLength, purpose, rate, squeezing, state
    Modifier and Type
    Field
    Description
    protected int
    Number of message bits currently held in dataQueue.
    protected byte[]
    Buffer holding message bits not yet absorbed into the state.
    protected int
    Digest capacity in bits, twice the output length.
    protected final CryptoServicePurpose
    Crypto service purpose this digest instance was created for.
    protected int
    Sponge rate in bits, the number of message bits absorbed per permutation.
    protected boolean
    True once padding has been applied and output is being squeezed.
    protected long[]
    The sponge state, 25 lanes of 64 bits each.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a SHAKE digest with 128 bits of security strength.
    SHAKEDigest(int bitStrength)
    Base constructor.
    SHAKEDigest(int bitStrength, CryptoServicePurpose purpose)
    Base constructor.
    Creates a 128-bit SHAKE digest bound to a declared purpose.
    Clone constructor
  • Method Summary

    Modifier and Type
    Method
    Description
    private static int
    checkBitLength(int bitStrength)
    Rejects a bit strength that is not a multiple of 8.
    Returns null, as SHAKE declares no crypto service properties.
    int
    doFinal(byte[] out, int outOff)
    Squeezes the default digest size and resets the sponge.
    protected int
    doFinal(byte[] out, int outOff, byte partialByte, int partialBits)
    Squeezes the default digest size after a partial trailing byte.
    int
    doFinal(byte[] out, int outOff, int outLen)
    Squeezes outLen bytes, then resets the sponge.
    protected int
    doFinal(byte[] out, int outOff, int outLen, byte partialByte, int partialBits)
    Squeezes outLen bytes after a partial trailing byte.
    int
    doOutput(byte[] out, int outOff, int outLen)
    Squeezes outLen bytes without resetting, so output can be streamed.
    Names the algorithm as SHAKE followed by its bit strength.
    int
    Gets the default output size in bytes, a quarter of the bit strength.

    Methods inherited from class KeccakDigest

    absorb, absorb, absorbBits, getByteLength, reset, squeeze, update, update
    Modifier and Type
    Method
    Description
    protected void
    absorb(byte data)
    Absorb one byte into the sponge, permuting once the rate is reached.
    protected void
    absorb(byte[] data, int off, int len)
    Absorb a byte range into the sponge, permuting once per rate-sized block.
    protected void
    absorbBits(int data, int bits)
    Absorb the trailing 1-7 bits of a message, leaving the queue not a multiple of 8 so that no further absorbing is possible.
    int
    Return the size of block that the compression function is applied to in bytes.
    void
    Clear all absorbed message and output state, making the instance ready for a fresh message at its existing capacity.
    protected void
    squeeze(byte[] output, int offset, long outputLength)
    Squeeze outputLength bits out of the sponge, padding first if the message has not been finished.
    void
    update(byte in)
    Absorb one more byte of message.
    void
    update(byte[] in, int inOff, int len)
    Absorb more message bytes.

    Methods inherited from class Object

    clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • SHAKEDigest

      public SHAKEDigest()
      Creates a SHAKE digest with 128 bits of security strength.
    • SHAKEDigest

      public SHAKEDigest(CryptoServicePurpose purpose)
      Creates a 128-bit SHAKE digest bound to a declared purpose.
      Parameters:
      purpose - the use the digest is declared to serve, which callers must match
    • SHAKEDigest

      public SHAKEDigest(int bitStrength)
      Base constructor.
      Parameters:
      bitStrength - the security strength in bits of the XOF.
    • SHAKEDigest

      public SHAKEDigest(int bitStrength, CryptoServicePurpose purpose)
      Base constructor.
      Parameters:
      bitStrength - the security strength in bits of the XOF.
      purpose - the purpose of the digest will be used for.
    • SHAKEDigest

      public SHAKEDigest(SHAKEDigest source)
      Clone constructor
      Parameters:
      source - the other digest to be copied.
  • Method Details

    • checkBitLength

      private static int checkBitLength(int bitStrength)
      Rejects a bit strength that is not a multiple of 8.
      Parameters:
      bitStrength - the strength to check
      Returns:
      the strength, if valid
    • getAlgorithmName

      public String getAlgorithmName()
      Names the algorithm as SHAKE followed by its bit strength.
      Specified by:
      getAlgorithmName in interface Digest
      Overrides:
      getAlgorithmName in class KeccakDigest
      Returns:
      the digest length in bits, prefixed with "Keccak-"
    • getDigestSize

      public int getDigestSize()
      Gets the default output size in bytes, a quarter of the bit strength.
      Specified by:
      getDigestSize in interface Digest
      Overrides:
      getDigestSize in class KeccakDigest
      Returns:
      the digest length in bytes
    • doFinal

      public int doFinal(byte[] out, int outOff)
      Squeezes the default digest size and resets the sponge.
      Specified by:
      doFinal in interface Digest
      Overrides:
      doFinal in class KeccakDigest
      Parameters:
      out - the buffer to write the digest into
      outOff - the offset into out at which to write the digest
      Returns:
      the number of bytes written to out
    • doFinal

      public int doFinal(byte[] out, int outOff, int outLen)
      Squeezes outLen bytes, then resets the sponge.
      Specified by:
      doFinal in interface Xof
      Parameters:
      out - output array to write the output bytes to.
      outOff - offset to start writing the bytes at.
      outLen - the number of output bytes requested.
      Returns:
      the number of bytes written
    • doOutput

      public int doOutput(byte[] out, int outOff, int outLen)
      Squeezes outLen bytes without resetting, so output can be streamed.
      Specified by:
      doOutput in interface Xof
      Parameters:
      out - output array to write the output bytes to.
      outOff - offset to start writing the bytes at.
      outLen - the number of output bytes requested.
      Returns:
      the number of bytes written
    • doFinal

      protected int doFinal(byte[] out, int outOff, byte partialByte, int partialBits)
      Squeezes the default digest size after a partial trailing byte.
      Overrides:
      doFinal in class KeccakDigest
      Parameters:
      out - the buffer to write the digest into
      outOff - the offset into out at which to write the digest
      partialByte - a byte holding the final 1-7 bits of the message
      partialBits - how many bits of partialByte are message, 1 to 7, or 0 if none
      Returns:
      the number of bytes written to out
    • doFinal

      protected int doFinal(byte[] out, int outOff, int outLen, byte partialByte, int partialBits)
      Squeezes outLen bytes after a partial trailing byte.
      Parameters:
      out - the buffer the squeezed output is written to
      outOff - the index in out at which output starts
      outLen - the number of output bytes asked for
      partialByte - the byte holding any bits not yet absorbed
      partialBits - how many low bits of partialByte are data, 0 to 7
      Returns:
      the number of bytes written to out, always outLen
    • cryptoServiceProperties

      protected CryptoServiceProperties cryptoServiceProperties()
      Returns null, as SHAKE declares no crypto service properties.
      Overrides:
      cryptoServiceProperties in class KeccakDigest
      Returns:
      the digest's service properties, or null if it declares none