Salsa20Engine (Bouncy Castle Library 1.52 API Specification)

org.bouncycastle.crypto.engines

## Class Salsa20Engine

- java.lang.Object
- - org.bouncycastle.crypto.engines.Salsa20Engine

- All Implemented Interfaces:
    SkippingCipher, SkippingStreamCipher, StreamCipher
  Direct Known Subclasses:
    ChaChaEngine, XSalsa20Engine

      public class Salsa20Engine
      extends java.lang.Object
      implements SkippingStreamCipher

  Implementation of Daniel J. Bernstein's Salsa20 stream cipher, Snuffle 2005

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `static int` | `DEFAULT_ROUNDS`
    `protected int[]` | `engineState`
    `protected int` | `rounds`
    `protected static byte[]` | `sigma`
    `protected static byte[]` | `tau`
    `protected int[]` | `x`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `Salsa20Engine()` Creates a 20 round Salsa20 engine.
    `Salsa20Engine(int rounds)` Creates a Salsa20 engine with a specific number of rounds.

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `protected void` | `advanceCounter()`
    `protected void` | `advanceCounter(long diff)`
    `protected void` | `generateKeyStream(byte[] output)`
    `java.lang.String` | `getAlgorithmName()` Return the name of the algorithm the cipher implements.
    `protected long` | `getCounter()`
    `protected int` | `getNonceSize()`
    `long` | `getPosition()` Return the current "position" of the cipher
    `void` | `init(boolean forEncryption, CipherParameters params)` initialise a Salsa20 cipher.
    `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()` reset the cipher.
    `protected void` | `resetCounter()`
    `protected void` | `retreatCounter()`
    `protected void` | `retreatCounter(long diff)`
    `byte` | `returnByte(byte in)` encrypt/decrypt a single byte returning the result.
    `protected static int` | `rotl(int x, int y)` Rotate left
    `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 Detail

    - #### DEFAULT_ROUNDS

          public static final int DEFAULT_ROUNDS

      See Also:
        Constant Field Values

    - #### sigma

          protected static final byte[] sigma

    - #### tau

          protected static final byte[] tau

    - #### rounds

          protected int rounds

    - #### engineState

          protected int[] engineState

    - #### x

          protected int[] x

  - ### Constructor Detail

    - #### 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 Detail

    - #### 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:
        `java.lang.IllegalArgumentException` - if the params argument is inappropriate.

    - #### getNonceSize

          protected int getNonceSize()

    - #### getAlgorithmName

          public java.lang.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

    - #### rotl

          protected static int rotl(int x,
                 int y)

      Rotate left
      Parameters:
        `x` - value to rotate
        `y` - amount to rotate x
      Returns:
        rotated x