Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.bouncycastle.crypto.macs;
  
CMAC - as specified at www.nuee.nagoya-u.ac.jp/labs/tiwata/omac/omac.html

CMAC is analogous to OMAC1 - see also en.wikipedia.org/wiki/CMAC

CMAC is a NIST recomendation - see csrc.nist.gov/CryptoToolkit/modes/800-38_Series_Publications/SP800-38B.pdf

CMAC/OMAC1 is a blockcipher-based message authentication code designed and analyzed by Tetsu Iwata and Kaoru Kurosawa.

CMAC/OMAC1 is a simple variant of the CBC MAC (Cipher Block Chaining Message Authentication Code). OMAC stands for One-Key CBC MAC.

It supports 128- or 64-bits block ciphers, with any key size, and returns a MAC with dimension less or equal to the block size of the underlying cipher.

 
 public class CMac implements Mac
 {
     private static final byte CONSTANT_128 = (byte)0x87;
     private static final byte CONSTANT_64 = (byte)0x1b;
 
     private byte[] ZEROES;
 
     private byte[] mac;
 
     private byte[] buf;
     private int bufOff;
     private BlockCipher cipher;
 
     private int macSize;
 
     private byte[] LLuLu2;

    
create a standard MAC based on a CBC block cipher (64 or 128 bit block). This will produce an authentication code the length of the block size of the cipher.

Parameters:
cipher the cipher to be used as the basis of the MAC generation.
 
     public CMac(BlockCipher cipher)
     {
         this(ciphercipher.getBlockSize() * 8);
     }

    
create a standard MAC based on a block cipher with the size of the MAC been given in bits.

Note: the size of the MAC must be at least 24 bits (FIPS Publication 81), or 16 bits if being used as a data authenticator (FIPS Publication 113), and in general should be less than the size of the block cipher as it reduces the chance of an exhaustive attack (see Handbook of Applied Cryptography).

Parameters:
cipher the cipher to be used as the basis of the MAC generation.
macSizeInBits the size of the MAC in bits, must be a multiple of 8 and <= 128.
 
     public CMac(BlockCipher cipherint macSizeInBits)
     {
         if ((macSizeInBits % 8) != 0)
         {
             throw new IllegalArgumentException("MAC size must be multiple of 8");
         }
 
         if (macSizeInBits > (cipher.getBlockSize() * 8))
         {
             throw new IllegalArgumentException(
                 "MAC size must be less or equal to "
                     + (cipher.getBlockSize() * 8));
         }
 
         if (cipher.getBlockSize() != 8 && cipher.getBlockSize() != 16)
         {
             throw new IllegalArgumentException(
                 "Block size must be either 64 or 128 bits");
         }
 
         this. = new CBCBlockCipher(cipher);
         this. = macSizeInBits / 8;
 
          = new byte[cipher.getBlockSize()];
 
          = new byte[cipher.getBlockSize()];
 
          = new byte[cipher.getBlockSize()];
 
          = 0;
    }
    public String getAlgorithmName()
    {
        return .getAlgorithmName();
    }
    private static int shiftLeft(byte[] blockbyte[] output)
    {
        int i = block.length;
        int bit = 0;
        while (--i >= 0)
        {
            int b = block[i] & 0xff;
            output[i] = (byte)((b << 1) | bit);
            bit = (b >>> 7) & 1;
        }
        return bit;
    }
    private static byte[] doubleLu(byte[] in)
    {
        byte[] ret = new byte[in.length];
        int carry = shiftLeft(inret);
        int xor = 0xff & (in.length == 16 ?  : );
        /*
         * NOTE: This construction is an attempt at a constant-time implementation.
         */
        ret[in.length - 1] ^= (xor >>> ((1 - carry) << 3));
        return ret;
    }
    public void init(CipherParameters params)
    {
        validate(params);
        .init(trueparams);
        //initializes the L, Lu, Lu2 numbers
         = new byte[.];
        .processBlock(, 0, , 0);
         = doubleLu();
         = doubleLu();
        reset();
    }
    void validate(CipherParameters params)
    {
        if (params != null)
        {
            if (!(params instanceof KeyParameter))
            {
                // CMAC mode does not permit IV to underlying CBC mode
                throw new IllegalArgumentException("CMac mode only permits key to be set.");
            }
        }
    }
    public int getMacSize()
    {
        return ;
    }
    public void update(byte in)
    {
        if ( == .)
        {
            .processBlock(, 0, , 0);
             = 0;
        }
        [++] = in;
    }
    public void update(byte[] inint inOffint len)
    {
        if (len < 0)
        {
            throw new IllegalArgumentException(
                "Can't have a negative input length!");
        }
        int blockSize = .getBlockSize();
        int gapLen = blockSize - ;
        if (len > gapLen)
        {
            System.arraycopy(ininOffgapLen);
            .processBlock(, 0, , 0);
             = 0;
            len -= gapLen;
            inOff += gapLen;
            while (len > blockSize)
            {
                .processBlock(ininOff, 0);
                len -= blockSize;
                inOff += blockSize;
            }
        }
        System.arraycopy(ininOfflen);
         += len;
    }
    public int doFinal(byte[] outint outOff)
    {
        int blockSize = .getBlockSize();
        byte[] lu;
        if ( == blockSize)
        {
            lu = ;
        }
        else
        {
            new ISO7816d4Padding().addPadding();
            lu = ;
        }
        for (int i = 0; i < .i++)
        {
            [i] ^= lu[i];
        }
        .processBlock(, 0, , 0);
        System.arraycopy(, 0, outoutOff);
        reset();
        return ;
    }

    
Reset the mac generator.
    public void reset()
    {
        /*
         * clean the buffer.
         */
        for (int i = 0; i < .i++)
        {
            [i] = 0;
        }
         = 0;
        /*
         * reset the underlying cipher.
         */
        .reset();
    }
New to GrepCode? Check out our FAQ X