Class Salsa20Engine

java.lang.Object
org.bouncycastle.crypto.engines.Salsa20Engine
All Implemented Interfaces:
SkippingCipher, SkippingStreamCipher, StreamCipher
Direct Known Subclasses:
ChaCha7539Engine, ChaChaEngine, XSalsa20Engine

public class Salsa20Engine extends Object implements SkippingStreamCipher
Implementation of Daniel J. Bernstein's Salsa20 stream cipher, Snuffle 2005
  • Field Details

    • DEFAULT_ROUNDS

      public static final int DEFAULT_ROUNDS
      See Also:
    • rounds

      protected int rounds
    • engineState

      protected int[] engineState
    • x

      protected int[] x
  • Constructor Details

    • Salsa20Engine

      public Salsa20Engine()
      Creates a 20 round Salsa20 engine.
    • Salsa20Engine

      public Salsa20Engine(int rounds)
      Creates a Salsa20 engine with a specific number of rounds.
      Parameters:
      rounds - the number of rounds (must be an even number).
  • Method Details

    • packTauOrSigma

      protected void packTauOrSigma(int keyLength, int[] state, int stateOffset)
    • init

      public void init(boolean forEncryption, CipherParameters params)
      initialise a Salsa20 cipher.
      Specified by:
      init in interface StreamCipher
      Parameters:
      forEncryption - whether or not we are for encryption.
      params - the parameters required to set up the cipher.
      Throws:
      IllegalArgumentException - if the params argument is inappropriate.
    • getNonceSize

      protected int getNonceSize()
    • getAlgorithmName

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

      public byte returnByte(byte in)
      Description copied from interface: StreamCipher
      encrypt/decrypt a single byte returning the result.
      Specified by:
      returnByte in interface StreamCipher
      Parameters:
      in - the byte to be processed.
      Returns:
      the result of processing the input byte.
    • advanceCounter

      protected void advanceCounter(long diff)
    • advanceCounter

      protected void advanceCounter()
    • retreatCounter

      protected void retreatCounter(long diff)
    • retreatCounter

      protected void retreatCounter()
    • processBytes

      public int processBytes(byte[] in, int inOff, int len, byte[] out, int outOff)
      Description copied from interface: StreamCipher
      process a block of bytes from in putting the result into out.
      Specified by:
      processBytes in interface StreamCipher
      Parameters:
      in - the input byte array.
      inOff - the offset into the in array where the data to be processed starts.
      len - the number of bytes to be processed.
      out - the output buffer the processed bytes go into.
      outOff - the offset into the output byte array the processed data starts at.
      Returns:
      the number of bytes produced - should always be len.
    • skip

      public long skip(long numberOfBytes)
      Description copied from interface: SkippingCipher
      Skip numberOfBytes forwards, or backwards.
      Specified by:
      skip in interface SkippingCipher
      Parameters:
      numberOfBytes - the number of bytes to skip (positive forward, negative backwards).
      Returns:
      the number of bytes actually skipped.
    • seekTo

      public long seekTo(long position)
      Description copied from interface: SkippingCipher
      Reset the cipher and then skip forward to a given position.
      Specified by:
      seekTo in interface SkippingCipher
      Parameters:
      position - the number of bytes in to set the cipher state to.
      Returns:
      the byte position moved to.
    • getPosition

      public long getPosition()
      Description copied from interface: SkippingCipher
      Return the current "position" of the cipher
      Specified by:
      getPosition in interface SkippingCipher
      Returns:
      the current byte position.
    • reset

      public void reset()
      Description copied from interface: StreamCipher
      reset the cipher. This leaves it in the same state it was at after the last init (if there was one).
      Specified by:
      reset in interface StreamCipher
    • getCounter

      protected long getCounter()
    • resetCounter

      protected void resetCounter()
    • setKey

      protected void setKey(byte[] keyBytes, byte[] ivBytes)
    • generateKeyStream

      protected void generateKeyStream(byte[] output)
    • salsaCore

      public static void salsaCore(int rounds, int[] input, int[] x)
      Salsa20 function
      Parameters:
      input - input data