AES CMAC generating wrong signature

Viewed 50

I'm trying to generate AES-CMAC using cryptography library. And with message and key it is generating different output then online calculator : AES-CMAC ONLINE CALCULATOR

Here is my code

aeskey_for_CMAC = binascii.hexlify(key[-16:])

c = cmac.CMAC(algorithms.AES(aeskey_for_CMAC))
c.update(message)
Cmac = c.finalize()

print("CMAC : ",binascii.hexlify( Cmac))

Here the key = b'cce9cdc049d9b46140a850dc8c7b146c9efa60caa228c49e1eec9303bed205ba' and message = b'6ffad88b23783dc86974c75f87370dc0'

Output from above code : b'dc629809794c584107f4d81cb7133982' Output from online calculator/Expected : dfea87c2 b4c2678a b63241e5 52b84acd

Can anybody tell me whats wrong in my code?

1 Answers

To derive the key aeskey_for_CMAC, key must not be hex encoded, but hex decoded and from the result the last 16 bytes are to be used as CMAC key:

aeskey_for_CMAC = binascii.unhexlify(key)[-16:]

The message must also be hex decoded:

c.update(binascii.unhexlify(message))

With these changes, the code returns the result you are looking for: dfea87c2b4c2678ab63241e552b84acd.

Related