public class Grainv1Engine extends java.lang.Object implements StreamCipher
Modifier and Type | Field and Description |
---|---|
private int |
index |
private boolean |
initialised |
private int[] |
lfsr |
private int[] |
nfsr |
private byte[] |
out |
private int |
output |
private static int |
STATE_SIZE
Constants
|
private byte[] |
workingIV |
private byte[] |
workingKey
Variables to hold the state of the engine during encryption and
decryption
|
Constructor and Description |
---|
Grainv1Engine() |
Modifier and Type | Method and Description |
---|---|
java.lang.String |
getAlgorithmName()
Return the name of the algorithm the cipher implements.
|
private byte |
getKeyStream() |
private int |
getOutput()
Get output from output function h(x).
|
private int |
getOutputLFSR()
Get output from linear function f(x).
|
private int |
getOutputNFSR()
Get output from non-linear function g(x).
|
void |
init(boolean forEncryption,
CipherParameters params)
Initialize a Grain v1 cipher.
|
private void |
initGrain()
160 clocks initialization phase.
|
private void |
oneRound()
Run Grain one round(i.e.
|
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.
|
byte |
returnByte(byte in)
encrypt/decrypt a single byte returning the result.
|
private void |
setKey(byte[] keyBytes,
byte[] ivBytes)
Set keys, reset cipher.
|
private int[] |
shift(int[] array,
int val)
Shift array 16 bits and add val to index.length - 1.
|
private static final int STATE_SIZE
private byte[] workingKey
private byte[] workingIV
private byte[] out
private int[] lfsr
private int[] nfsr
private int output
private int index
private boolean initialised
public java.lang.String getAlgorithmName()
StreamCipher
getAlgorithmName
in interface StreamCipher
public void init(boolean forEncryption, CipherParameters params) throws java.lang.IllegalArgumentException
init
in interface StreamCipher
forEncryption
- Whether or not we are for encryption.params
- The parameters required to set up the cipher.java.lang.IllegalArgumentException
- If the params argument is inappropriate.private void initGrain()
private int getOutputNFSR()
private int getOutputLFSR()
private int getOutput()
private int[] shift(int[] array, int val)
array
- The array to shift.val
- The value to shift in.private void setKey(byte[] keyBytes, byte[] ivBytes)
keyBytes
- The key.ivBytes
- The IV.public void processBytes(byte[] in, int inOff, int len, byte[] out, int outOff) throws DataLengthException
StreamCipher
processBytes
in interface StreamCipher
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.DataLengthException
- if the output buffer is too small.public void reset()
StreamCipher
reset
in interface StreamCipher
private void oneRound()
public byte returnByte(byte in)
StreamCipher
returnByte
in interface StreamCipher
in
- the byte to be processed.private byte getKeyStream()