Package org.bouncycastle.crypto.engines
Class Salsa20Engine
java.lang.Object
org.bouncycastle.crypto.engines.Salsa20Engine
- All Implemented Interfaces:
SkippingCipher,SkippingStreamCipher,StreamCipher
- Direct Known Subclasses:
ChaCha7539Engine,ChaChaEngine,XSalsa20Engine
Implementation of Daniel J. Bernstein's Salsa20 stream cipher, Snuffle 2005
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intprotected int[]protected intprotected static final byte[]Deprecated.protected static final byte[]Deprecated.protected int[] -
Constructor Summary
ConstructorsConstructorDescriptionCreates a 20 round Salsa20 engine.Salsa20Engine(int rounds) Creates a Salsa20 engine with a specific number of rounds. -
Method Summary
Modifier and TypeMethodDescriptionprotected voidprotected voidadvanceCounter(long diff) protected voidgenerateKeyStream(byte[] output) Return the name of the algorithm the cipher implements.protected longprotected intlongReturn the current "position" of the ciphervoidinit(boolean forEncryption, CipherParameters params) initialise a Salsa20 cipher.protected voidpackTauOrSigma(int keyLength, int[] state, int stateOffset) intprocessBytes(byte[] in, int inOff, int len, byte[] out, int outOff) process a block of bytes from in putting the result into out.voidreset()reset the cipher.protected voidprotected voidprotected voidretreatCounter(long diff) bytereturnByte(byte in) encrypt/decrypt a single byte returning the result.static voidsalsaCore(int rounds, int[] input, int[] x) Salsa20 functionlongseekTo(long position) Reset the cipher and then skip forward to a given position.protected voidsetKey(byte[] keyBytes, byte[] ivBytes) longskip(long numberOfBytes) Skip numberOfBytes forwards, or backwards.
-
Field Details
-
DEFAULT_ROUNDS
public static final int DEFAULT_ROUNDS- See Also:
-
sigma
protected static final byte[] sigmaDeprecated. -
tau
protected static final byte[] tauDeprecated. -
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
initialise a Salsa20 cipher.- Specified by:
initin interfaceStreamCipher- 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
Description copied from interface:StreamCipherReturn the name of the algorithm the cipher implements.- Specified by:
getAlgorithmNamein interfaceStreamCipher- Returns:
- the name of the algorithm the cipher implements.
-
returnByte
public byte returnByte(byte in) Description copied from interface:StreamCipherencrypt/decrypt a single byte returning the result.- Specified by:
returnBytein interfaceStreamCipher- 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:StreamCipherprocess a block of bytes from in putting the result into out.- Specified by:
processBytesin interfaceStreamCipher- 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:SkippingCipherSkip numberOfBytes forwards, or backwards.- Specified by:
skipin interfaceSkippingCipher- 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:SkippingCipherReset the cipher and then skip forward to a given position.- Specified by:
seekToin interfaceSkippingCipher- 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:SkippingCipherReturn the current "position" of the cipher- Specified by:
getPositionin interfaceSkippingCipher- Returns:
- the current byte position.
-
reset
public void reset()Description copied from interface:StreamCipherreset the cipher. This leaves it in the same state it was at after the last init (if there was one).- Specified by:
resetin interfaceStreamCipher
-
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
-