Message Authentication Codes (MACs)

Overview

>>> from tomcrypt import mac

This module contains a MAC class, and a convenience function for every MAC provided:

>>> mymac = mac.MAC('hmac', 'sha256', b'secret')
>>> # OR
>>> mymac = mac.hmac('sha256', b'secret')

The module also contains a list of the names of all MACS provided, and lists of those which use ciphers or hashes:

>>> sorted(mac.names)
['hmac', 'omac', 'pmac', 'xcbc']
>>> sorted(mac.hash_macs)
['hmac']
>>> sorted(mac.cipher_macs)
['omac', 'pmac', 'xcbc']

The MAC will accept either a name or a Descriptor to specify which hash/cipher algorithm to use:

>>> mac.hmac('md5', b'secret', b'content').hexdigest()
'97e5f3684213a40aaaa9ef31f9f4b1a7'
>>> mac.hmac(hash.md5, b'secret', b'content').hexdigest()
'97e5f3684213a40aaaa9ef31f9f4b1a7'

>>> mac.pmac('aes', b'0123456789abcdef', b'content').hexdigest()
'530566cd1c33e874f503b3c3272d0fd4'
>>> mac.pmac(cipher.aes, b'0123456789abcdef', b'content').hexdigest()
'530566cd1c33e874f503b3c3272d0fd4'

The rest of the API is similar to the stdlib hmac (or hashlib or tomcrypt.hash, etc.), offering update, digest, hexdigest, and copy methods.

MAC API

class tomcrypt.mac.MAC

Calculator of keyed hashes.

Parameters:
str mode – What type of mac. One of ‘hmac’, ‘omac’, ‘pmac’, or ‘xcbc’. idx – What cipher/hash to use. Either a name or descriptor. bytes key – The key. bytes input – Initial data.
>>> mac = MAC('hmac', 'md5', b'secret') # or hmac('md5', b'secret')
>>> mac = MAC('omac', 'aes', b'0123456789abcdef') # or omac(...)
>>> mac = MAC('pmac', 'aes', b'0123456789abcdef') # or pmac(...)
>>> mac = MAC('xcbc', 'aes', b'0123456789abcdef') # or xcbc(...)
copy()

Get a copy of the mac state.

>>> a = hmac('md5', b'secret', b'message')
>>> b = a.copy()
>>> b.update(b'some more')
>>> b.hexdigest()
'e0cdc5e1d7af04f800b0e0f0ceee588a'
>>> a.hexdigest()
'7e0d0767775312154ba16fd3af9771a2'
digest()

Return binary digest.

>>> mac = hmac('md5', b'secret', b'message')
>>> mac.digest()
b'~\r\x07gwS\x12\x15K\xa1o\xd3\xaf\x97q\xa2'
hexdigest()

Return hex-encoded string of digest.

>>> mac = hmac('md5', b'secret', b'message')
>>> mac.hexdigest()
'7e0d0767775312154ba16fd3af9771a2'
update()

Add more data to the mac.

>>> mac = hmac('md5', b'secret')
>>> mac.update(b'message')
>>> mac.hexdigest()
'7e0d0767775312154ba16fd3af9771a2'
tomcrypt.mac.test_library()

Run internal libtomcrypt mac tests.

>>> test_library()
True

Table Of Contents

Previous topic

Hash Functions

Next topic

Pseudo-Random Number Generators (PRNGs)

This Page