Debian Codegraph @

Doc/src/hash/kmac128.rst

raw
1 KMAC128
2 =======
3
4 KMAC128 is a variable-length Message Authenticated Code (MAC) derived from SHA-3
5 and standardized in `NIST SP 800-185 <https://nvlpubs.nist.gov/nistpubs/specialpublications/nist.sp.800-185.pdf>`_.
6
7 KMAC128 provides a security strength of 128 bits.
8 It must be keyed with a secret of 16 bytes or more.
9
10 This is an example showing how to generate a KMAC128 tag::
11
12 >>> from Crypto.Hash import KMAC128
13 >>>
14 >>> secret = b'Sixteen byte key'
15 >>> mac = KMAC128.new(key=secret, mac_len=16)
16 >>> mac.update(b'Hello')
17 >>> print(mac.hexdigest())
18 e6cb0fb015898ebd019d4eb5fad444bf
19
20 And this is an example showing how to validate the KMAC128 tag::
21
22 >>> from Crypto.Hash import KMAC128
23 >>>
24 >>> # We have received a message 'msg' together
25 >>> # with its MAC 'mac_tag'
26 >>>
27 >>> secret = b'Sixteen byte key'
28 >>> mac = KMAC128.new(key=secret, mac_len=16)
29 >>> mac.update(msg)
30 >>> try:
31 >>> mac.verify(mac_tag)
32 >>> print("The message '%s' is authentic" % msg)
33 >>> except ValueError:
34 >>> print("The message or the key is wrong")
35
36 An application can select the length of the MAC tag by means of the initialization parameter ``mac_len``.
37 For instance, while the traditional HMAC-SHA256 can only produce 32-byte tags,
38 with KMAC128 you can produce 16-byte tags (see the examples above) but also a 33-byte tag::
39
40 >>> from Crypto.Hash import KMAC128
41 >>>
42 >>> secret = b'Sixteen byte key'
43 >>> mac = KMAC128.new(key=secret, mac_len=33)
44 >>> mac.update(b'Hello')
45 >>> print(mac.hexdigest())
46 eed4b3157bd5d98002ad0ca990c192125416c7a72705fea22cf5d896361243bc5a
47
48 Note how the 16-byte tag is NOT just the truncated version of the 33-byte tag: they are cryptographically uncorrelated.
49
50 .. automodule:: Crypto.Hash.KMAC128
51 :members: