| 1 |
CMAC |
| 2 |
==== |
| 3 |
|
| 4 |
CMAC (Cipher-based Message Authentication Code) is a MAC defined |
| 5 |
in `NIST SP 800-38B`_ and in RFC4493_ (for AES only) and |
| 6 |
constructed using a block cipher. It was originally known as `OMAC1`_. |
| 7 |
|
| 8 |
The algorithm is sometimes named *X-CMAC* where *X* is the name |
| 9 |
of the cipher (e.g. AES-CMAC). |
| 10 |
|
| 11 |
This is an example showing how to generate an AES-CMAC tag:: |
| 12 |
|
| 13 |
>>> from Crypto.Hash import CMAC |
| 14 |
>>> from Crypto.Cipher import AES |
| 15 |
>>> |
| 16 |
>>> secret = b'Sixteen byte key' |
| 17 |
>>> cobj = CMAC.new(secret, ciphermod=AES) |
| 18 |
>>> cobj.update(b'Hello') |
| 19 |
>>> print cobj.hexdigest() |
| 20 |
|
| 21 |
And this is an example showing how to validate the AES-CMAC:: |
| 22 |
|
| 23 |
>>> from Crypto.Hash import CMAC |
| 24 |
>>> from Crypto.Cipher import AES |
| 25 |
>>> |
| 26 |
>>> # We have received a message 'msg' together |
| 27 |
>>> # with its MAC 'mac' |
| 28 |
>>> |
| 29 |
>>> secret = b'Sixteen byte key' |
| 30 |
>>> cobj = CMAC.new(secret, ciphermod=AES) |
| 31 |
>>> cobj.update(msg) |
| 32 |
>>> try: |
| 33 |
>>> cobj.verify(mac) |
| 34 |
>>> print "The message '%s' is authentic" % msg |
| 35 |
>>> except ValueError: |
| 36 |
>>> print "The message or the key is wrong" |
| 37 |
|
| 38 |
A cipher block size of 128 bits (like for AES) guarantees that the risk |
| 39 |
of MAC collisions remains negligible even when the same CMAC key is |
| 40 |
used to authenticate a large amount of data. |
| 41 |
|
| 42 |
This implementation allows also usage of ciphers with a 64 bits block size |
| 43 |
(like TDES) for legacy purposes only. |
| 44 |
However, the risk is much higher and one CMAC key should be rotated |
| 45 |
after as little as 16 MB (in total) have been authenticated. |
| 46 |
|
| 47 |
.. _`NIST SP 800-38B`: http://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-38b.pdf |
| 48 |
.. _RFC4493: http://www.ietf.org/rfc/rfc4493.txt |
| 49 |
.. _OMAC1: http://www.nuee.nagoya-u.ac.jp/labs/tiwata/omac/omac.html |
| 50 |
|
| 51 |
.. automodule:: Crypto.Hash.CMAC |
| 52 |
:members: |