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 Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
     
    protected int[]
     
    protected int
     
    protected static final byte[]
    Deprecated. 
    protected static final byte[]
    Deprecated. 
    protected int[]
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a 20 round Salsa20 engine.
    Salsa20Engine(int rounds)
    Creates a Salsa20 engine with a specific number of rounds.
  • Method Summary

    Modifier and Type
    Method
    Description
    protected void
     
    protected void
    advanceCounter(long diff)
     
    protected void
    generateKeyStream(byte[] output)
     
    Return the name of the algorithm the cipher implements.
    protected long
     
    protected int
     
    long
    Return the current "position" of the cipher
    void
    init(boolean forEncryption, CipherParameters params)
    initialise a Salsa20 cipher.
    protected void
    packTauOrSigma(int keyLength, int[] state, int stateOffset)
     
    int
    processBytes(byte[] in, int inOff, int len, byte[] out, int outOff)
    process a block of bytes from in putting the result into out.
    void
    reset the cipher.
    protected void
     
    protected void
     
    protected void
    retreatCounter(long diff)
     
    byte
    returnByte(byte in)
    encrypt/decrypt a single byte returning the result.
    static void
    salsaCore(int rounds, int[] input, int[] x)
    Salsa20 function
    long
    seekTo(long position)
    Reset the cipher and then skip forward to a given position.
    protected void
    setKey(byte[] keyBytes, byte[] ivBytes)
     
    long
    skip(long numberOfBytes)
    Skip numberOfBytes forwards, or backwards.

    Methods inherited from class java.lang.Object

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

    • DEFAULT_ROUNDS

      public static final int DEFAULT_ROUNDS
      See Also:
    • sigma

      protected static final byte[] sigma
      Deprecated.
    • tau

      protected static final byte[] tau
      Deprecated.
    • 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