Top Description Inners Fields Constructors Methods
org.bouncycastle.crypto.modes

public Class CCMBlockCipher

extends Object
implements CCMModeCipher
Class Inheritance
All Implemented Interfaces
org.bouncycastle.crypto.modes.CCMModeCipher, org.bouncycastle.crypto.modes.AEADBlockCipher, org.bouncycastle.crypto.modes.AEADCipher
Imports
java.io.ByteArrayOutputStream, org.bouncycastle.crypto.BlockCipher, .CipherParameters, .DataLengthException, .InvalidCipherTextException, .Mac, .OutputLengthException, org.bouncycastle.crypto.macs.CBCBlockCipherMac, org.bouncycastle.crypto.params.AEADParameters, .ParametersWithIV, org.bouncycastle.util.Arrays

Implements the Counter with Cipher Block Chaining mode (CCM) detailed in NIST Special Publication 800-38C.

Note

this mode is a packet mode - it needs all the data up front.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class

Field Summary

Modifier and TypeField and Description
private CCMBlockCipher.ExposedByteArrayOutputStream
private int
private BlockCipher
private CCMBlockCipher.ExposedByteArrayOutputStream
private boolean
private byte[]
private CipherParameters
private byte[]
private int
private byte[]

Constructor Summary

AccessConstructor and Description
public
CCMBlockCipher(BlockCipher
the block cipher to be used.
c
)

Deprecated use the CCMBlockCipher.newInstance() static method.
Basic constructor.

Method Summary

Modifier and TypeMethod and Description
private int
calculateMac(byte[] data, int dataOff, int dataLen, byte[] macBlock)

public int
doFinal(byte[]
space for any resulting output data.
out
,
int
offset into out to start copying the data at.
outOff
)

Implements org.bouncycastle.crypto.modes.AEADCipher.doFinal.

Finish the operation either appending or verifying the MAC at the end of the data.
public String
getAlgorithmName()

Implements org.bouncycastle.crypto.modes.AEADCipher.getAlgorithmName.

Return the name of the algorithm.
private int
public byte[]

Returns:

the last mac calculated.
getMac
()

Implements org.bouncycastle.crypto.modes.AEADCipher.getMac.

Returns a byte array containing the mac calculated as part of the last encrypt or decrypt operation.
private int
getMacSize(boolean forEncryption, int requestedMacBits)

public int
getOutputSize(int
the length of the input.
len
)

Implements org.bouncycastle.crypto.modes.AEADCipher.getOutputSize.

return the size of the output buffer required for a processBytes plus a doFinal with an input of len bytes.
public BlockCipher

Returns:

the underlying block cipher that we are wrapping.
getUnderlyingCipher
()

Implements org.bouncycastle.crypto.modes.AEADBlockCipher.getUnderlyingCipher.

return the underlying block cipher that we are wrapping.
public int
getUpdateOutputSize(int
the length of the input.
len
)

Implements org.bouncycastle.crypto.modes.AEADCipher.getUpdateOutputSize.

return the size of the output buffer required for a processBytes an input of len bytes.
private boolean
public void
init(boolean
true if we are setting up for encryption, false otherwise.
forEncryption
,
CipherParameters
the necessary parameters for the underlying cipher to be initialised.
params
)

Implements org.bouncycastle.crypto.modes.AEADCipher.init.

initialise the underlying cipher.
public static CCMModeCipher
newInstance(BlockCipher
the base cipher for the CCM mode.
cipher
)

Return a new CCM mode cipher based on the passed in base cipher

public void
processAADByte(byte
the byte to be processed.
in
)

Implements org.bouncycastle.crypto.modes.AEADCipher.processAADByte.

Add a single byte to the associated data check.
public void
processAADBytes(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
)

Implements org.bouncycastle.crypto.modes.AEADCipher.processAADBytes.

Add a sequence of bytes to the associated data check.
public int
processByte(byte
the byte to be processed.
in
,
byte[]
the output buffer the processed byte goes into.
out
,
int
the offset into the output byte array the processed data starts at.
outOff
)

Implements org.bouncycastle.crypto.modes.AEADCipher.processByte.

encrypt/decrypt a single byte.
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.
inLen
,
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.modes.AEADCipher.processBytes.

process a block of bytes from in putting the result into out.
public byte[]

Returns:

a byte array containing the processed input..
processPacket
(byte[]
data for processing.
in
,
int
offset at which data starts in the input array.
inOff
,
int
length of the data in the input array.
inLen
)

Process a packet of data for either CCM decryption or encryption.

public int

Returns:

the number of bytes added to output.
processPacket
(byte[]
data for processing.
in
,
int
offset at which data starts in the input array.
inOff
,
int
length of the data in the input array.
inLen
,
byte[]
output array.
output
,
int
offset into output array to start putting processed bytes.
outOff
)

Process a packet of data for either CCM decryption or encryption.

public void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

associatedTextback to summary
private CCMBlockCipher.ExposedByteArrayOutputStream associatedText
blockSizeback to summary
private int blockSize
cipherback to summary
private BlockCipher cipher
databack to summary
private CCMBlockCipher.ExposedByteArrayOutputStream data
forEncryptionback to summary
private boolean forEncryption
initialAssociatedTextback to summary
private byte[] initialAssociatedText
keyParamback to summary
private CipherParameters keyParam
macBlockback to summary
private byte[] macBlock
macSizeback to summary
private int macSize
nonceback to summary
private byte[] nonce

Constructor Detail

CCMBlockCipherback to summary
public CCMBlockCipher(BlockCipher c)

Deprecated

use the CCMBlockCipher.newInstance() static method.

Basic constructor.

Parameters
c:BlockCipher

the block cipher to be used.

Method Detail

calculateMacback to summary
private int calculateMac(byte[] data, int dataOff, int dataLen, byte[] macBlock)
doFinalback to summary
public int doFinal(byte[] out, int outOff) throws IllegalStateException, InvalidCipherTextException

Implements org.bouncycastle.crypto.modes.AEADCipher.doFinal.

Doc from org.bouncycastle.crypto.modes.AEADCipher.doFinal.

Finish the operation either appending or verifying the MAC at the end of the data.

Parameters
out:byte[]

space for any resulting output data.

outOff:int

offset into out to start copying the data at.

Returns:int

number of bytes written into out.

Exceptions
IllegalStateException:
if the cipher is in an inappropriate state.
InvalidCipherTextException:
if the MAC fails to match.
getAlgorithmNameback to summary
public String getAlgorithmName()

Implements org.bouncycastle.crypto.modes.AEADCipher.getAlgorithmName.

Doc from org.bouncycastle.crypto.modes.AEADCipher.getAlgorithmName.

Return the name of the algorithm.

Returns:String

the algorithm name.

getAssociatedTextLengthback to summary
private int getAssociatedTextLength()
getMacback to summary
public byte[] getMac()

Implements org.bouncycastle.crypto.modes.AEADCipher.getMac.

Returns a byte array containing the mac calculated as part of the last encrypt or decrypt operation.

Returns:byte[]

the last mac calculated.

getMacSizeback to summary
private int getMacSize(boolean forEncryption, int requestedMacBits)
getOutputSizeback to summary
public int getOutputSize(int len)

Implements org.bouncycastle.crypto.modes.AEADCipher.getOutputSize.

Doc from org.bouncycastle.crypto.modes.AEADCipher.getOutputSize.

return the size of the output buffer required for a processBytes plus a doFinal with an input of len bytes.

The returned size may be dependent on the initialisation of this cipher and may not be accurate once subsequent input data is processed - this method should be invoked immediately prior to a call to final processing of input data and a call to doFinal(byte[], int).

Parameters
len:int

the length of the input.

Returns:int

the space required to accommodate a call to processBytes and doFinal with len bytes of input.

getUnderlyingCipherback to summary
public BlockCipher getUnderlyingCipher()

Implements org.bouncycastle.crypto.modes.AEADBlockCipher.getUnderlyingCipher.

return the underlying block cipher that we are wrapping.

Returns:BlockCipher

the underlying block cipher that we are wrapping.

getUpdateOutputSizeback to summary
public int getUpdateOutputSize(int len)

Implements org.bouncycastle.crypto.modes.AEADCipher.getUpdateOutputSize.

Doc from org.bouncycastle.crypto.modes.AEADCipher.getUpdateOutputSize.

return the size of the output buffer required for a processBytes an input of len bytes.

The returned size may be dependent on the initialisation of this cipher and may not be accurate once subsequent input data is processed - this method should be invoked immediately prior to input data being processed.

Parameters
len:int

the length of the input.

Returns:int

the space required to accommodate a call to processBytes with len bytes of input.

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

Implements org.bouncycastle.crypto.modes.AEADCipher.init.

Doc from org.bouncycastle.crypto.modes.AEADCipher.init.

initialise the underlying cipher. Parameter can either be an AEADParameters or a ParametersWithIV object.

Parameters
forEncryption:boolean

true if we are setting up for encryption, false otherwise.

params:CipherParameters

the necessary parameters for the underlying cipher to be initialised.

Exceptions
IllegalArgumentException:
if the params argument is inappropriate.
newInstanceback to summary
public static CCMModeCipher newInstance(BlockCipher cipher)

Return a new CCM mode cipher based on the passed in base cipher

Parameters
cipher:BlockCipher

the base cipher for the CCM mode.

processAADByteback to summary
public void processAADByte(byte in)

Implements org.bouncycastle.crypto.modes.AEADCipher.processAADByte.

Doc from org.bouncycastle.crypto.modes.AEADCipher.processAADByte.

Add a single byte to the associated data check.
If the implementation supports it, this will be an online operation and will not retain the associated data.

Parameters
in:byte

the byte to be processed.

processAADBytesback to summary
public void processAADBytes(byte[] in, int inOff, int len)

Implements org.bouncycastle.crypto.modes.AEADCipher.processAADBytes.

Doc from org.bouncycastle.crypto.modes.AEADCipher.processAADBytes.

Add a sequence of bytes to the associated data check.
If the implementation supports it, this will be an online operation and will not retain the associated data.

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.

processByteback to summary
public int processByte(byte in, byte[] out, int outOff) throws DataLengthException, IllegalStateException

Implements org.bouncycastle.crypto.modes.AEADCipher.processByte.

Doc from org.bouncycastle.crypto.modes.AEADCipher.processByte.

encrypt/decrypt a single byte.

Parameters
in:byte

the byte to be processed.

out:byte[]

the output buffer the processed byte goes into.

outOff:int

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

Returns:int

the number of bytes written to out.

Exceptions
DataLengthException:
if the output buffer is too small.
processBytesback to summary
public int processBytes(byte[] in, int inOff, int inLen, byte[] out, int outOff) throws DataLengthException, IllegalStateException

Implements org.bouncycastle.crypto.modes.AEADCipher.processBytes.

Doc from org.bouncycastle.crypto.modes.AEADCipher.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.

inLen: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 written to out.

Exceptions
DataLengthException:
if the output buffer is too small.
processPacketback to summary
public byte[] processPacket(byte[] in, int inOff, int inLen) throws IllegalStateException, InvalidCipherTextException

Process a packet of data for either CCM decryption or encryption.

Parameters
in:byte[]

data for processing.

inOff:int

offset at which data starts in the input array.

inLen:int

length of the data in the input array.

Returns:byte[]

a byte array containing the processed input..

Exceptions
IllegalStateException:
if the cipher is not appropriately set up.
InvalidCipherTextException:
if the input data is truncated or the mac check fails.
processPacketback to summary
public int processPacket(byte[] in, int inOff, int inLen, byte[] output, int outOff) throws IllegalStateException, InvalidCipherTextException, DataLengthException

Process a packet of data for either CCM decryption or encryption.

Parameters
in:byte[]

data for processing.

inOff:int

offset at which data starts in the input array.

inLen:int

length of the data in the input array.

output:byte[]

output array.

outOff:int

offset into output array to start putting processed bytes.

Returns:int

the number of bytes added to output.

Exceptions
IllegalStateException:
if the cipher is not appropriately set up.
InvalidCipherTextException:
if the input data is truncated or the mac check fails.
DataLengthException:
if output buffer too short.
resetback to summary
public void reset()

Implements org.bouncycastle.crypto.modes.AEADCipher.reset.

Doc from org.bouncycastle.crypto.modes.AEADCipher.reset.

Reset the cipher. After resetting the cipher is in the same state as it was after the last init (if there was one).

org.bouncycastle.crypto.modes back to summary

private Class CCMBlockCipher.ExposedByteArrayOutputStream

extends ByteArrayOutputStream
Class Inheritance

Field Summary

Inherited from java.io.ByteArrayOutputStream:
bufcount

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public byte[]
Inherited from java.io.ByteArrayOutputStream:
closeresetsizetoByteArraytoStringtoStringtoStringtoStringwritewritewriteByteswriteTo

Constructor Detail

ExposedByteArrayOutputStreamback to summary
public ExposedByteArrayOutputStream()

Method Detail

getBufferback to summary
public byte[] getBuffer()