java.lang.Object
org.bouncycastle.crypto.digests.KeccakDigest
All Implemented Interfaces:
Digest, ExtendedDigest
Direct Known Subclasses:
SHA3Digest, SHAKEDigest

public class KeccakDigestextends Object implements ExtendedDigest
Implementation of Keccak 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
    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.
    private static long[]
    The round constants of the Keccak permutation.
    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
    Create a Keccak-288 digest for any crypto service purpose.
    KeccakDigest(int bitLength)
    Create a digest of the given capacity, for any crypto service purpose.
    KeccakDigest(int bitLength, CryptoServicePurpose purpose)
    Create a digest of the given capacity and crypto service purpose.
    Create a Keccak-288 digest for the given crypto service purpose.
    Copy the state of an existing digest, including any partially absorbed data.
  • Method Summary

    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.
    Describe this digest's properties for the crypto services registrar.
    int
    doFinal(byte[] out, int outOff)
    Finish the digest, write it out and reset this instance.
    protected int
    doFinal(byte[] out, int outOff, byte partialByte, int partialBits)
    Finish the digest, first absorbing the trailing bits of a partial byte.
    Get the JCA name of this algorithm.
    int
    Return the size of block that the compression function is applied to in bytes.
    int
    Get the size of the digest this class produces.
    private void
    init(int bitLength)
    Sets up the digest for a given output size.
    private void
    initSponge(int rate)
    Sets up the sponge for a given rate.
    private void
    KeccakAbsorb(byte[] data, int off)
    Absorbs one block of input.
    private void
    Copies the current state into the output buffer.
    private void
    Runs the permutation over the state.
    private void
    Applies the padding and moves to the squeezing phase.
    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
  • Field Details

    • KeccakRoundConstants

      private static long[] KeccakRoundConstants
      The round constants of the Keccak permutation.
    • purpose

      protected final CryptoServicePurpose purpose
      Crypto service purpose this digest instance was created for.
    • state

      protected long[] state
      The sponge state, 25 lanes of 64 bits each.
    • dataQueue

      protected byte[] dataQueue
      Buffer holding message bits not yet absorbed into the state.
    • rate

      protected int rate
      Sponge rate in bits, the number of message bits absorbed per permutation.
    • bitsInQueue

      protected int bitsInQueue
      Number of message bits currently held in dataQueue.
    • fixedOutputLength

      protected int fixedOutputLength
      Digest capacity in bits, twice the output length.
    • squeezing

      protected boolean squeezing
      True once padding has been applied and output is being squeezed.
  • Constructor Details

    • KeccakDigest

      public KeccakDigest()
      Create a Keccak-288 digest for any crypto service purpose.
    • KeccakDigest

      public KeccakDigest(CryptoServicePurpose purpose)
      Create a Keccak-288 digest for the given crypto service purpose.
      Parameters:
      purpose - the crypto service purpose to register the digest under
    • KeccakDigest

      public KeccakDigest(int bitLength)
      Create a digest of the given capacity, for any crypto service purpose.
      Parameters:
      bitLength - the capacity in bits, i.e. twice the digest length
    • KeccakDigest

      public KeccakDigest(int bitLength, CryptoServicePurpose purpose)
      Create a digest of the given capacity and crypto service purpose.
      Parameters:
      bitLength - the capacity in bits, i.e. twice the digest length
      purpose - the crypto service purpose to register the digest under
    • KeccakDigest

      public KeccakDigest(KeccakDigest source)
      Copy the state of an existing digest, including any partially absorbed data.
      Parameters:
      source - the digest to copy the state and rate from
  • Method Details

    • getAlgorithmName

      public String getAlgorithmName()
      Get the JCA name of this algorithm.
      Specified by:
      getAlgorithmName in interface Digest
      Returns:
      the digest length in bits, prefixed with "Keccak-"
    • getDigestSize

      public int getDigestSize()
      Get the size of the digest this class produces.
      Specified by:
      getDigestSize in interface Digest
      Returns:
      the digest length in bytes
    • update

      public void update(byte in)
      Absorb one more byte of message.
      Specified by:
      update in interface Digest
      Parameters:
      in - the byte to add to the message
    • update

      public void update(byte[] in, int inOff, int len)
      Absorb more message bytes.
      Specified by:
      update in interface Digest
      Parameters:
      in - the buffer holding the message
      inOff - the offset into in of the first byte to absorb
      len - the number of bytes to absorb
    • doFinal

      public int doFinal(byte[] out, int outOff)
      Finish the digest, write it out and reset this instance.
      Specified by:
      doFinal in interface Digest
      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

      protected int doFinal(byte[] out, int outOff, byte partialByte, int partialBits)
      Finish the digest, first absorbing the trailing bits of a partial byte.
      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
    • reset

      public void reset()
      Clear all absorbed message and output state, making the instance ready for a fresh message at its existing capacity.
      Specified by:
      reset in interface Digest
    • getByteLength

      public int getByteLength()
      Return the size of block that the compression function is applied to in bytes.
      Specified by:
      getByteLength in interface ExtendedDigest
      Returns:
      internal byte length of a block.
    • init

      private void init(int bitLength)
      Sets up the digest for a given output size.
      Parameters:
      bitLength - the output size, in bits
    • initSponge

      private void initSponge(int rate)
      Sets up the sponge for a given rate.
      Parameters:
      rate - the sponge rate, in bits
    • absorb

      protected void absorb(byte data)
      Absorb one byte into the sponge, permuting once the rate is reached.
      Parameters:
      data - the byte to absorb
    • absorb

      protected void absorb(byte[] data, int off, int len)
      Absorb a byte range into the sponge, permuting once per rate-sized block.
      Parameters:
      data - the buffer holding the message
      off - the offset into data of the first byte to absorb
      len - the number of bytes to absorb
    • absorbBits

      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.
      Parameters:
      data - the value holding the final bits, of which only the low ones count
      bits - how many low bits of data are message, 1 to 7
    • padAndSwitchToSqueezingPhase

      private void padAndSwitchToSqueezingPhase()
      Applies the padding and moves to the squeezing phase.
    • squeeze

      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.
      Parameters:
      output - the buffer to write the output into
      offset - the offset into output at which to write
      outputLength - the number of bits to squeeze, a multiple of 8
    • KeccakAbsorb

      private void KeccakAbsorb(byte[] data, int off)
      Absorbs one block of input.
      Parameters:
      data - the input bytes
      off - where the block starts
    • KeccakExtract

      private void KeccakExtract()
      Copies the current state into the output buffer.
    • KeccakPermutation

      private void KeccakPermutation()
      Runs the permutation over the state.
    • cryptoServiceProperties

      protected CryptoServiceProperties cryptoServiceProperties()
      Describe this digest's properties for the crypto services registrar.
      Returns:
      the digest's service properties, or null if it declares none