Debian Codegraph @

Doc/src/hash/kmac256.rst

raw
1 KMAC256
2 =======
3
4 KMAC256 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 KMAC256 provides a security strength of 256 bits.
8 It must be keyed with a secret of 32 bytes or more.
9
10 This is an example showing how to generate a KMAC256 tag::
11
12 >>> from Crypto.Hash import KMAC256
13 >>>
14 >>> secret = b'Protect this thirty-two byte key'
15 >>> mac = KMAC256.new(key=secret, mac_len=16)
16 >>> mac.update(b'Hello')
17 >>> print(mac.hexdigest())
18 4ba8c9808f10b3bf5621f393363f4e1a
19
20 And this is an example showing how to validate the KMAC256 tag::
21
22 >>> from Crypto.Hash import KMAC256
23 >>>
24 >>> # We have received a message 'msg' together
25 >>> # with its MAC 'mac_tag'
26 >>>
27 >>> secret = b'Protect this thirty-two byte key'
28 >>> mac = KMAC256.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 KMAC256 you can produce 16-byte tags (see the examples above) but also a 33-byte tag::
39
40 >>> from Crypto.Hash import KMAC256
41 >>>
42 >>> secret = b'Protect this thirty-two byte key'
43 >>> mac = KMAC256.new(key=secret, mac_len=33)
44 >>> mac.update(b'Hello')
45 >>> print(mac.hexdigest())
46 518938a66f4ce8f50a35cf77d16f002d5734da495eb6dea1e41191e657890ba4ad
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.KMAC256
51 :members: