blob: 11b107358a1c0011d2089bf37d12923c75ffcc2e [file] [log] [blame]
Alex Gaynoraf82d5e2013-10-29 17:07:24 -07001.. hazmat::
Paul Kehrer0317b042013-10-28 17:34:27 -05002
Alex Stapletonc5fffd32014-03-18 15:29:00 +00003Hash-based message authentication codes
Paul Kehrer0317b042013-10-28 17:34:27 -05004=======================================
5
Alex Gaynor4658ce12013-10-29 15:26:50 -07006.. currentmodule:: cryptography.hazmat.primitives.hmac
7
Paul Kehrer0317b042013-10-28 17:34:27 -05008.. testsetup::
9
10 import binascii
11 key = binascii.unhexlify(b"0" * 32)
12
13Hash-based message authentication codes (or HMACs) are a tool for calculating
14message authentication codes using a cryptographic hash function coupled with a
Alex Stapleton79043462014-03-09 16:46:26 +000015secret key. You can use an HMAC to verify both the integrity and authenticity
16of a message.
Paul Kehrer0317b042013-10-28 17:34:27 -050017
David Reidef0fcf22013-11-06 11:12:45 -080018.. class:: HMAC(key, algorithm, backend)
Paul Kehrer0317b042013-10-28 17:34:27 -050019
Alex Stapleton79043462014-03-09 16:46:26 +000020 HMAC objects take a ``key`` and a
21 :class:`~cryptography.hazmat.primitives.interfaces.HashAlgorithm` provider.
Paul Kehrer50a88152013-10-29 10:46:05 -050022 The ``key`` should be randomly generated bytes and is recommended to be
23 equal in length to the ``digest_size`` of the hash function chosen.
24 You must keep the ``key`` secret.
Paul Kehrer0317b042013-10-28 17:34:27 -050025
Alex Gaynor58ecc8d2013-11-03 21:21:00 -080026 This is an implementation of :rfc:`2104`.
27
Paul Kehrer0317b042013-10-28 17:34:27 -050028 .. doctest::
29
Alex Gaynorf8796b12013-12-13 20:28:55 -080030 >>> from cryptography.hazmat.backends import default_backend
Paul Kehrerbf8962a2013-10-28 17:44:42 -050031 >>> from cryptography.hazmat.primitives import hashes, hmac
David Reid63fa19a2013-11-20 10:49:13 -080032 >>> h = hmac.HMAC(key, hashes.SHA256(), backend=default_backend())
Paul Kehrer0317b042013-10-28 17:34:27 -050033 >>> h.update(b"message to hash")
David Reid6753e392013-11-01 15:32:03 -070034 >>> h.finalize()
35 '#F\xdaI\x8b"e\xc4\xf1\xbb\x9a\x8fc\xff\xf5\xdex.\xbc\xcd/+\x8a\x86\x1d\x84\'\xc3\xa6\x1d\xd8J'
Paul Kehrer0317b042013-10-28 17:34:27 -050036
Alex Stapleton447d64f2013-12-21 21:26:55 +000037 If the backend doesn't support the requested ``algorithm`` an
Alex Gaynor7a489db2014-03-22 15:09:34 -070038 :class:`~cryptography.exceptions.UnsupportedAlgorithm` exception will be
39 raised.
David Reid663295d2013-11-20 13:55:08 -080040
Paul Kehrer288bae72014-01-08 21:46:18 -060041 To check that a given signature is correct use the :meth:`verify` method.
42 You will receive an exception if the signature is wrong:
Alex Gaynor5bbcf762014-01-08 18:36:57 -080043
44 .. code-block:: pycon
45
46 >>> h.verify(b"an incorrect signature")
47 Traceback (most recent call last):
48 ...
49 cryptography.exceptions.InvalidSignature: Signature did not match digest.
50
Alex Stapleton79043462014-03-09 16:46:26 +000051 :param bytes key: Secret key as ``bytes``.
52 :param algorithm: An
David Reid663295d2013-11-20 13:55:08 -080053 :class:`~cryptography.hazmat.primitives.interfaces.HashAlgorithm`
54 provider such as those described in
55 :ref:`Cryptographic Hashes <cryptographic-hash-algorithms>`.
Alex Stapleton79043462014-03-09 16:46:26 +000056 :param backend: An
Alex Gaynorf8796b12013-12-13 20:28:55 -080057 :class:`~cryptography.hazmat.backends.interfaces.HMACBackend`
David Reid663295d2013-11-20 13:55:08 -080058 provider.
59
Alex Gaynor7a489db2014-03-22 15:09:34 -070060 :raises cryptography.exceptions.UnsupportedAlgorithm: This is raised if the
Ayrxecac0292014-03-16 14:14:17 +080061 provided ``backend`` does not implement
62 :class:`~cryptography.hazmat.backends.interfaces.HMACBackend`
63
Paul Kehrer2824ab72013-10-28 11:06:55 -050064 .. method:: update(msg)
Paul Kehrer0317b042013-10-28 17:34:27 -050065
Paul Kehrer50a88152013-10-29 10:46:05 -050066 :param bytes msg: The bytes to hash and authenticate.
David Reid2cce6182013-11-13 13:49:41 -080067 :raises cryptography.exceptions.AlreadyFinalized: See :meth:`finalize`
Paul Kehrer0317b042013-10-28 17:34:27 -050068
69 .. method:: copy()
70
David Reid2cce6182013-11-13 13:49:41 -080071 Copy this :class:`HMAC` instance, usually so that we may call
Alex Stapleton79043462014-03-09 16:46:26 +000072 :meth:`finalize` to get an intermediate digest value while we continue
73 to call :meth:`update` on the original instance.
David Reid2cce6182013-11-13 13:49:41 -080074
Alex Stapleton63b3de22014-02-08 09:43:16 +000075 :return: A new instance of :class:`HMAC` that can be updated
David Reid2cce6182013-11-13 13:49:41 -080076 and finalized independently of the original instance.
77 :raises cryptography.exceptions.AlreadyFinalized: See :meth:`finalize`
Paul Kehrer0317b042013-10-28 17:34:27 -050078
Julian Krausec91fe6a2013-12-25 11:00:49 -080079 .. method:: verify(signature)
Julian Krause2288e302013-12-17 21:26:23 -080080
Alex Gaynor12252702014-01-02 11:10:35 -080081 Finalize the current context and securely compare digest to
82 ``signature``.
Julian Krause2288e302013-12-17 21:26:23 -080083
Alex Gaynor12252702014-01-02 11:10:35 -080084 :param bytes signature: The bytes to compare the current digest
85 against.
Julian Krause2288e302013-12-17 21:26:23 -080086 :raises cryptography.exceptions.AlreadyFinalized: See :meth:`finalize`
Alex Gaynor12252702014-01-02 11:10:35 -080087 :raises cryptography.exceptions.InvalidSignature: If signature does not
88 match digest
Alex Gaynor5bbcf762014-01-08 18:36:57 -080089
90 .. method:: finalize()
91
92 Finalize the current context and return the message digest as bytes.
93
Alex Stapleton79043462014-03-09 16:46:26 +000094 After ``finalize`` has been called this object can no longer be used
95 and :meth:`update`, :meth:`copy`, :meth:`verify` and :meth:`finalize`
96 will raise an :class:`~cryptography.exceptions.AlreadyFinalized`
97 exception.
Alex Gaynor5bbcf762014-01-08 18:36:57 -080098
99 :return bytes: The message digest as bytes.
100 :raises cryptography.exceptions.AlreadyFinalized: