Search code examples
javaandroidcryptographyaes

CMAC-AES (RFC 4493) calculation in Java


I have key and data:

Now how do I calculate CMAC based on these parameters?

Some more info :

MACs are calculated using AES as the underlying block cipher, according to the CMAC standard described in NIST Special Publication 800-38B. but I'm having a hard time finding proper examples of MAC calculation in java.

EDIT this link works perfectly for this. I need Java samples of this.


Solution

  • If anyone Looking for CMAC AES calculation here it is using Bouncy Castle:

     public void getCMAC()
                throws Exception {
            byte[] msg = new byte[]{(byte) 0xA3, (byte) 0x00, (byte) 0x00, (byte) 0x35, (byte) 0xE7, (byte) 0x58,
                    (byte) 0xC6, (byte) 0x09, (byte) 0x00, (byte) 0x4F, (byte) 0x44, (byte) 0x36, (byte) 0xF0,
                    (byte) 0xEA, (byte) 0x31, (byte) 0x9A, (byte) 0xF4, (byte) 0x31, (byte) 0xE1,
                    (byte) 0x98, (byte) 0xBC, (byte) 0x41, (byte) 0xA0, (byte) 0x67, (byte) 0xD1};
    
            byte[] keydata = new byte[]{(byte) 0x67, (byte) 0x1B, (byte) 0x9D, (byte) 0x1D, (byte) 0xC1, (byte) 0x54,
                    (byte) 0x74, (byte) 0xA2, (byte) 0x5C, (byte) 0xB1, (byte) 0x77, (byte) 0xCA, (byte) 0x1A,
                    (byte) 0x19, (byte) 0x9F, (byte) 0x0E};
    
            CipherParameters params = new KeyParameter(keydata);
            BlockCipher aes = new AESEngine();
            CMac mac = new CMac(aes);
            mac.init(params);
            mac.update(msg, 0, msg.length);
            byte[] out = new byte[mac.getMacSize()];
            mac.doFinal(out, 0);
    
            StringBuilder s19 = new StringBuilder();
            for (byte b : out) {
                s19.append(String.format("%02X ", b));
            }
            Log.e("ecrypted Kmac :", s19.toString());
        }
    

    Make sure you add lib like this:

     implementation 'org.bouncycastle:bcpkix-jdk15on:1.56'