org.bouncycastle.crypto.engines
Class Salsa20Engine

java.lang.Object
  extended by org.bouncycastle.crypto.engines.Salsa20Engine
All Implemented Interfaces:
StreamCipher

public class Salsa20Engine
extends java.lang.Object
implements StreamCipher

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


Field Summary
private  int cW0
           
private  int cW1
           
private  int cW2
           
private  int[] engineState
           
private  int index
           
private  boolean initialised
           
private  byte[] keyStream
           
private static byte[] sigma
           
private static int stateSize
          Constants
private static byte[] tau
           
private  byte[] workingIV
           
private  byte[] workingKey
           
private  int[] x
           
 
Constructor Summary
Salsa20Engine()
           
 
Method Summary
private  int byteToIntLittle(byte[] x, int offset)
          Pack byte[] array into an int in little endian order
 java.lang.String getAlgorithmName()
          Return the name of the algorithm the cipher implements.
 void init(boolean forEncryption, CipherParameters params)
          initialise a Salsa20 cipher.
private  byte[] intToByteLittle(int x, byte[] out, int off)
          32 bit word to 4 byte array in little endian order
private  boolean limitExceeded()
           
private  boolean limitExceeded(int len)
           
 void 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.
private  void resetCounter()
           
 byte returnByte(byte in)
          encrypt/decrypt a single byte returning the result.
private  int rotl(int x, int y)
          Rotate left
private  void salsa20WordToByte(int[] input, byte[] output)
          Salsa20 function
private  void setKey(byte[] keyBytes, byte[] ivBytes)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

stateSize

private static final int stateSize
Constants

See Also:
Constant Field Values

sigma

private static final byte[] sigma

tau

private static final byte[] tau

index

private int index

engineState

private int[] engineState

x

private int[] x

keyStream

private byte[] keyStream

workingKey

private byte[] workingKey

workingIV

private byte[] workingIV

initialised

private boolean initialised

cW0

private int cW0

cW1

private int cW1

cW2

private int cW2
Constructor Detail

Salsa20Engine

public Salsa20Engine()
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.

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.

processBytes

public void 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.

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

setKey

private void setKey(byte[] keyBytes,
                    byte[] ivBytes)

salsa20WordToByte

private void salsa20WordToByte(int[] input,
                               byte[] output)
Salsa20 function

Parameters:
input - input data

intToByteLittle

private byte[] intToByteLittle(int x,
                               byte[] out,
                               int off)
32 bit word to 4 byte array in little endian order

Parameters:
x - value to 'unpack'
Returns:
value of x expressed as a byte[] array in little endian order

rotl

private int rotl(int x,
                 int y)
Rotate left

Parameters:
x - value to rotate
y - amount to rotate x
Returns:
rotated x

byteToIntLittle

private int byteToIntLittle(byte[] x,
                            int offset)
Pack byte[] array into an int in little endian order

Parameters:
x - byte array to 'pack'
offset - only x[offset]..x[offset+3] will be packed
Returns:
x[offset]..x[offset+3] 'packed' into an int in little-endian order

resetCounter

private void resetCounter()

limitExceeded

private boolean limitExceeded()

limitExceeded

private boolean limitExceeded(int len)