Class ThreefishEngine

java.lang.Object
org.bouncycastle.crypto.engines.ThreefishEngine
All Implemented Interfaces:
BlockCipher

public class ThreefishEngine extends Object implements BlockCipher
Implementation of the Threefish tweakable large block cipher in 256, 512 and 1024 bit block sizes.

This is the 1.3 version of Threefish defined in the Skein hash function submission to the NIST SHA-3 competition in October 2010.

Threefish was designed by Niels Ferguson - Stefan Lucks - Bruce Schneier - Doug Whiting - Mihir Bellare - Tadayoshi Kohno - Jon Callas - Jesse Walker.

This implementation inlines all round functions, unrolls 8 rounds, and uses 1.2k of static tables to speed up key schedule injection.
2 x block size state is retained by each cipher instance.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    1024 bit block size - Threefish-1024
    static final int
    256 bit block size - Threefish-256
    static final int
    512 bit block size - Threefish-512
  • Constructor Summary

    Constructors
    Constructor
    Description
    ThreefishEngine(int blocksizeBits)
    Constructs a new Threefish cipher, with a specified block size.
  • Method Summary

    Modifier and Type
    Method
    Description
    static long
    bytesToWord(byte[] bytes, int off)
    Deprecated.
    Will be removed
    Return the name of the algorithm the cipher implements.
    int
    Return the block size for this cipher (in bytes).
    void
    init(boolean forEncryption, long[] key, long[] tweak)
    Initialise the engine, specifying the key and tweak directly.
    void
    init(boolean forEncryption, CipherParameters params)
    Initialise the engine.
    int
    processBlock(byte[] in, int inOff, byte[] out, int outOff)
    Process one block of input from the array in and write it to the out array.
    int
    processBlock(long[] in, long[] out)
    Process a block of data represented as 64 bit words.
    void
    Reset the cipher.
    static void
    wordToBytes(long word, byte[] bytes, int off)
    Deprecated.
    Will be removed

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • BLOCKSIZE_256

      public static final int BLOCKSIZE_256
      256 bit block size - Threefish-256
      See Also:
    • BLOCKSIZE_512

      public static final int BLOCKSIZE_512
      512 bit block size - Threefish-512
      See Also:
    • BLOCKSIZE_1024

      public static final int BLOCKSIZE_1024
      1024 bit block size - Threefish-1024
      See Also:
  • Constructor Details

    • ThreefishEngine

      public ThreefishEngine(int blocksizeBits)
      Constructs a new Threefish cipher, with a specified block size.
      Parameters:
      blocksizeBits - the block size in bits, one of BLOCKSIZE_256, BLOCKSIZE_512, BLOCKSIZE_1024.
  • Method Details

    • init

      public void init(boolean forEncryption, CipherParameters params) throws IllegalArgumentException
      Initialise the engine.
      Specified by:
      init in interface BlockCipher
      Parameters:
      forEncryption - if true the cipher is initialised for encryption, if false for decryption.
      params - an instance of TweakableBlockCipherParameters, or KeyParameter (to use a 0 tweak)
      Throws:
      IllegalArgumentException - if the params argument is inappropriate.
    • init

      public void init(boolean forEncryption, long[] key, long[] tweak)
      Initialise the engine, specifying the key and tweak directly.
      Parameters:
      forEncryption - the cipher mode.
      key - the words of the key, or null to use the current key.
      tweak - the 2 word (128 bit) tweak, or null to use the current tweak.
    • getAlgorithmName

      public String getAlgorithmName()
      Description copied from interface: BlockCipher
      Return the name of the algorithm the cipher implements.
      Specified by:
      getAlgorithmName in interface BlockCipher
      Returns:
      the name of the algorithm the cipher implements.
    • getBlockSize

      public int getBlockSize()
      Description copied from interface: BlockCipher
      Return the block size for this cipher (in bytes).
      Specified by:
      getBlockSize in interface BlockCipher
      Returns:
      the block size for this cipher in bytes.
    • reset

      public void reset()
      Description copied from interface: BlockCipher
      Reset the cipher. After resetting the cipher is in the same state as it was after the last init (if there was one).
      Specified by:
      reset in interface BlockCipher
    • processBlock

      public int processBlock(byte[] in, int inOff, byte[] out, int outOff) throws DataLengthException, IllegalStateException
      Description copied from interface: BlockCipher
      Process one block of input from the array in and write it to the out array.
      Specified by:
      processBlock in interface BlockCipher
      Parameters:
      in - the array containing the input data.
      inOff - offset into the in array the data starts at.
      out - the array the output data will be copied into.
      outOff - the offset into the out array the output will start at.
      Returns:
      the number of bytes processed and produced.
      Throws:
      DataLengthException - if there isn't enough data in input , or space in out.
      IllegalStateException - if the cipher isn't initialised.
    • processBlock

      public int processBlock(long[] in, long[] out) throws DataLengthException, IllegalStateException
      Process a block of data represented as 64 bit words.
      Parameters:
      in - a block sized buffer of words to process.
      out - a block sized buffer of words to receive the output of the operation.
      Returns:
      the number of 8 byte words processed (which will be the same as the block size).
      Throws:
      DataLengthException - if either the input or output is not block sized.
      IllegalStateException - if this engine is not initialised.
    • bytesToWord

      public static long bytesToWord(byte[] bytes, int off)
      Deprecated.
      Will be removed
      Read a single 64 bit word from input in LSB first order.
    • wordToBytes

      public static void wordToBytes(long word, byte[] bytes, int off)
      Deprecated.
      Will be removed
      Write a 64 bit word to output in LSB first order.