Top Description Fields Constructors Methods
org.bouncycastle.crypto.engines

public Class Grain128Engine

extends Object
implements StreamCipher
Class Inheritance
All Implemented Interfaces
org.bouncycastle.crypto.StreamCipher
Imports
org.bouncycastle.crypto.CipherParameters, .CryptoServicesRegistrar, .DataLengthException, .OutputLengthException, .StreamCipher, org.bouncycastle.crypto.constraints.DefaultServiceProperties, org.bouncycastle.crypto.params.KeyParameter, .ParametersWithIV

Implementation of Martin Hell's, Thomas Johansson's and Willi Meier's stream cipher, Grain-128.

Field Summary

Modifier and TypeField and Description
private int
private boolean
private int[]
private int[]
private byte[]
private int
private static final int
STATE_SIZE

Constants

private byte[]
private byte[]
workingKey

Variables to hold the state of the engine during encryption and decryption

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public String
getAlgorithmName()

Implements org.bouncycastle.crypto.StreamCipher.getAlgorithmName.

Return the name of the algorithm the cipher implements.

private byte
private int

Returns:

Output from h(x).
getOutput
()

Get output from output function h(x).

private int

Returns:

Output from LFSR.
getOutputLFSR
()

Get output from linear function f(x).

private int

Returns:

Output from NFSR.
getOutputNFSR
()

Get output from non-linear function g(x).

public void
init(boolean
Whether or not we are for encryption.
forEncryption
,
CipherParameters
The parameters required to set up the cipher.
params
)

Implements org.bouncycastle.crypto.StreamCipher.init.

Initialize a Grain-128 cipher.

private void
initGrain()

256 clocks initialization phase.

private void
oneRound()

Run Grain one round(i.e. 32 bits).

public int
processBytes(byte[]
the input byte array.
in
,
int
the offset into the in array where the data to be processed starts.
inOff
,
int
the number of bytes to be processed.
len
,
byte[]
the output buffer the processed bytes go into.
out
,
int
the offset into the output byte array the processed data starts at.
outOff
)

Implements org.bouncycastle.crypto.StreamCipher.processBytes.

process a block of bytes from in putting the result into out.

public void
reset()

Implements org.bouncycastle.crypto.StreamCipher.reset.

reset the cipher.

public byte
returnByte(byte
the byte to be processed.
in
)

Implements org.bouncycastle.crypto.StreamCipher.returnByte.

encrypt/decrypt a single byte returning the result.

private void
setKey(byte[]
The key.
keyBytes
,
byte[]
The IV.
ivBytes
)

Set keys, reset cipher.

private int[]

Returns:

The shifted array with val added to index.length - 1.
shift
(int[]
The array to shift.
array
,
int
The value to shift in.
val
)

Shift array 32 bits and add val to index.length - 1.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

indexback to summary
private int index
initialisedback to summary
private boolean initialised
lfsrback to summary
private int[] lfsr
nfsrback to summary
private int[] nfsr
outback to summary
private byte[] out
outputback to summary
private int output
STATE_SIZEback to summary
private static final int STATE_SIZE

Constants

workingIVback to summary
private byte[] workingIV
workingKeyback to summary
private byte[] workingKey

Variables to hold the state of the engine during encryption and decryption

Constructor Detail

Grain128Engineback to summary
public Grain128Engine()

Method Detail

getAlgorithmNameback to summary
public String getAlgorithmName()

Implements org.bouncycastle.crypto.StreamCipher.getAlgorithmName.

Doc from org.bouncycastle.crypto.StreamCipher.getAlgorithmName.

Return the name of the algorithm the cipher implements.

Returns:String

the name of the algorithm the cipher implements.

getKeyStreamback to summary
private byte getKeyStream()
getOutputback to summary
private int getOutput()

Get output from output function h(x).

Returns:int

Output from h(x).

getOutputLFSRback to summary
private int getOutputLFSR()

Get output from linear function f(x).

Returns:int

Output from LFSR.

getOutputNFSRback to summary
private int getOutputNFSR()

Get output from non-linear function g(x).

Returns:int

Output from NFSR.

initback to summary
public void init(boolean forEncryption, CipherParameters params) throws IllegalArgumentException

Implements org.bouncycastle.crypto.StreamCipher.init.

Initialize a Grain-128 cipher.

Parameters
forEncryption:boolean

Whether or not we are for encryption.

params:CipherParameters

The parameters required to set up the cipher.

Exceptions
IllegalArgumentException:
If the params argument is inappropriate.
initGrainback to summary
private void initGrain()

256 clocks initialization phase.

oneRoundback to summary
private void oneRound()

Run Grain one round(i.e. 32 bits).

processBytesback to summary
public int processBytes(byte[] in, int inOff, int len, byte[] out, int outOff) throws DataLengthException

Implements org.bouncycastle.crypto.StreamCipher.processBytes.

Doc from org.bouncycastle.crypto.StreamCipher.processBytes.

process a block of bytes from in putting the result into out.

Parameters
in:byte[]

the input byte array.

inOff:int

the offset into the in array where the data to be processed starts.

len:int

the number of bytes to be processed.

out:byte[]

the output buffer the processed bytes go into.

outOff:int

the offset into the output byte array the processed data starts at.

Returns:int

the number of bytes produced - should always be len.

Exceptions
DataLengthException:
if the output buffer is too small.
resetback to summary
public void reset()

Implements org.bouncycastle.crypto.StreamCipher.reset.

Doc from org.bouncycastle.crypto.StreamCipher.reset.

reset the cipher. This leaves it in the same state it was at after the last init (if there was one).

returnByteback to summary
public byte returnByte(byte in)

Implements org.bouncycastle.crypto.StreamCipher.returnByte.

Doc from org.bouncycastle.crypto.StreamCipher.returnByte.

encrypt/decrypt a single byte returning the result.

Parameters
in:byte

the byte to be processed.

Returns:byte

the result of processing the input byte.

setKeyback to summary
private void setKey(byte[] keyBytes, byte[] ivBytes)

Set keys, reset cipher.

Parameters
keyBytes:byte[]

The key.

ivBytes:byte[]

The IV.

shiftback to summary
private int[] shift(int[] array, int val)

Shift array 32 bits and add val to index.length - 1.

Parameters
array:int[]

The array to shift.

val:int

The value to shift in.

Returns:int[]

The shifted array with val added to index.length - 1.