org.clazzes.optional.sec
Class GCMCipher

java.lang.Object
  extended by org.clazzes.optional.sec.GCMCipher
All Implemented Interfaces:
AEADBlockCipher, BlockCipher

public class GCMCipher
extends java.lang.Object
implements AEADBlockCipher

galois counter mode for symmetric ciphers. Copied from bouncycastle.


Constructor Summary
GCMCipher(BlockCipher c)
           
GCMCipher(BlockCipher c, GCMMultiplier m)
           
 
Method Summary
 int doFinal(byte[] out, int outOff)
          Finish the operation either appending or verifying the MAC at the end of the data.
 java.lang.String getAlgorithmName()
          Return the name of the algorithm the cipher implements.
 int getBlockSize()
           
 byte[] getMac()
          Return the value of the MAC associated with the last stream processed.
 int getOutputSize(int len)
           
 BlockCipher getUnderlyingCipher()
          return the underlying block cipher that we are wrapping or null, if we do wrap another cipher.
 int getUpdateOutputSize(int len)
           
 void init(byte[] iv, boolean useForEncryption)
          Initialize the block cipher.
 void init(byte[] aad, int macSizeBits, byte[] iv, boolean useForEncryption)
          Initialize the block cipher.
 int processBlock(byte[] in, int inOff, byte[] out, int outOff)
          Proceed the next block of BlockCipher.getBlockSize() bytes from the input.
 int processBytes(byte[] in, int inOff, int inLen, byte[] out, int outOff)
          Proceed the next block of in_len bytes from the input.
 void reset()
          Reset this cipher to state of the last call to #init(byte[], byte[], boolean).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

GCMCipher

public GCMCipher(BlockCipher c)
Parameters:
c - The underlying block cipher, which should have been initialized in encrypting ("forward") mode regardless of whether we want to encrypt or decrypt.

GCMCipher

public GCMCipher(BlockCipher c,
                 GCMMultiplier m)
Parameters:
c - The underlying block cipher, which should have been initialized in encrypting ("forward") mode regardless of whether we want to encrypt or decrypt.
m - A GCM multiplier or null, if you want to use the default multiplier implementation.
Method Detail

getUnderlyingCipher

public BlockCipher getUnderlyingCipher()
Description copied from interface: BlockCipher
return the underlying block cipher that we are wrapping or null, if we do wrap another cipher.

Specified by:
getUnderlyingCipher in interface BlockCipher
Returns:
the underlying block cipher that we are wrapping.

getAlgorithmName

public java.lang.String getAlgorithmName()
Description copied from interface: BlockCipher
Return the name of the algorithm the cipher implements.

Specified by:
getAlgorithmName in interface BlockCipher
Returns:
the name of the algorithm the cipher implements.

init

public void init(byte[] iv,
                 boolean useForEncryption)
          throws java.lang.IllegalArgumentException
Description copied from interface: BlockCipher
Initialize the block cipher.

Specified by:
init in interface BlockCipher
Parameters:
iv - The key for basic ciphers or initialization vector for cipher modes or null, if no key or initialization vector is needed.
useForEncryption - Whether we want to encrypt. false means to set up this cipher for decryption.
Throws:
java.lang.IllegalArgumentException

init

public void init(byte[] aad,
                 int macSizeBits,
                 byte[] iv,
                 boolean useForEncryption)
          throws java.lang.IllegalArgumentException
Description copied from interface: AEADBlockCipher
Initialize the block cipher.

Specified by:
init in interface AEADBlockCipher
Parameters:
aad - The additional authenticated data or null, if no additional authenticated data is needed.
macSizeBits - The length of the calculated myac in bits.
iv - The initialization (nonce) vector or null, if no initialization vector is needed.
useForEncryption - Whether we want to encrypt. false means to set up this cipher for decryption.
Throws:
java.lang.IllegalArgumentException

getMac

public byte[] getMac()
Description copied from interface: AEADBlockCipher
Return the value of the MAC associated with the last stream processed.

Specified by:
getMac in interface AEADBlockCipher
Returns:
MAC for plaintext data.

getOutputSize

public int getOutputSize(int len)
Specified by:
getOutputSize in interface AEADBlockCipher
Parameters:
len - The length of the input data.
Returns:
The length of the generated output data.

getUpdateOutputSize

public int getUpdateOutputSize(int len)

processBlock

public int processBlock(byte[] in,
                        int inOff,
                        byte[] out,
                        int outOff)
Description copied from interface: BlockCipher
Proceed the next block of BlockCipher.getBlockSize() bytes from the input.

Specified by:
processBlock in interface BlockCipher
Parameters:
in - The buffer where the input is stored. BlockCipher.getBlockSize() bytes starting with in_off will be consumed.
inOff - The offset at which the input data starts.
out - The buffer where the output is stored. BlockCipher.getBlockSize() bytes starting with out_off will be written.
outOff - The offset at which the output will be stored.
Returns:
The block size aka the number of bytes written.

processBytes

public int processBytes(byte[] in,
                        int inOff,
                        int inLen,
                        byte[] out,
                        int outOff)
Description copied from interface: AEADBlockCipher
Proceed the next block of in_len bytes from the input. This complements the functionality of BlockCipher.processBlock(byte[], int, byte[], int), because AEAD ciphers may process arbitrary data length.

Specified by:
processBytes in interface AEADBlockCipher
Parameters:
in - The buffer where the input is stored. BlockCipher.getBlockSize() bytes starting with in_off will be consumed.
inOff - The offset at which the input data starts.
inLen - The length of the input data to process.
out - The buffer where the output is stored. BlockCipher.getBlockSize() bytes starting with out_off will be written.
outOff - The offset at which the output will be stored.
Returns:
The block size aka the number of bytes written.

doFinal

public int doFinal(byte[] out,
                   int outOff)
            throws InvalidCipherTextException
Description copied from interface: AEADBlockCipher
Finish the operation either appending or verifying the MAC at the end of the data.

Specified by:
doFinal in interface AEADBlockCipher
Parameters:
out - space for any resulting output data.
outOff - offset into out to start copying the data at.
Returns:
number of bytes written into out.
Throws:
InvalidCipherTextException - if the MAC fails to match.

reset

public void reset()
Description copied from interface: BlockCipher
Reset this cipher to state of the last call to #init(byte[], byte[], boolean).

Specified by:
reset in interface BlockCipher

getBlockSize

public int getBlockSize()
Specified by:
getBlockSize in interface BlockCipher
Returns:
The block size used for the BlockCipher.processBlock(byte[], int, byte[], int) operation.


Copyright © 2010. All Rights Reserved.