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
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 Description static intDEFAULT_ROUNDSprotected int[]engineStateprotected introundsprotected static byte[]sigmaDeprecated.protected static byte[]tauDeprecated.protected int[]x
-
Constructor Summary
Constructors Constructor Description Salsa20Engine()Creates a 20 round Salsa20 engine.Salsa20Engine(int rounds)Creates a Salsa20 engine with a specific number of rounds.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidadvanceCounter()protected voidadvanceCounter(long diff)protected voidgenerateKeyStream(byte[] output)java.lang.StringgetAlgorithmName()Return the name of the algorithm the cipher implements.protected longgetCounter()protected intgetNonceSize()longgetPosition()Return 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 voidresetCounter()protected voidretreatCounter()protected 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 Detail
-
DEFAULT_ROUNDS
public static final int DEFAULT_ROUNDS
- See Also:
- Constant Field Values
-
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
-
-
Method Detail
-
packTauOrSigma
protected void packTauOrSigma(int keyLength, int[] state, int stateOffset)
-
init
public void init(boolean forEncryption, CipherParameters params)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:
java.lang.IllegalArgumentException- if the params argument is inappropriate.
-
getNonceSize
protected int getNonceSize()
-
getAlgorithmName
public java.lang.String 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
-
-