Alex Gaynor | af82d5e | 2013-10-29 17:07:24 -0700 | [diff] [blame] | 1 | .. hazmat:: |
Paul Kehrer | 0317b04 | 2013-10-28 17:34:27 -0500 | [diff] [blame] | 2 | |
Alex Stapleton | c5fffd3 | 2014-03-18 15:29:00 +0000 | [diff] [blame] | 3 | Hash-based message authentication codes |
Paul Kehrer | 0317b04 | 2013-10-28 17:34:27 -0500 | [diff] [blame] | 4 | ======================================= |
| 5 | |
Alex Gaynor | 4658ce1 | 2013-10-29 15:26:50 -0700 | [diff] [blame] | 6 | .. currentmodule:: cryptography.hazmat.primitives.hmac |
| 7 | |
Paul Kehrer | 0317b04 | 2013-10-28 17:34:27 -0500 | [diff] [blame] | 8 | .. testsetup:: |
| 9 | |
| 10 | import binascii |
| 11 | key = binascii.unhexlify(b"0" * 32) |
| 12 | |
| 13 | Hash-based message authentication codes (or HMACs) are a tool for calculating |
| 14 | message authentication codes using a cryptographic hash function coupled with a |
Alex Stapleton | 7904346 | 2014-03-09 16:46:26 +0000 | [diff] [blame] | 15 | secret key. You can use an HMAC to verify both the integrity and authenticity |
| 16 | of a message. |
Paul Kehrer | 0317b04 | 2013-10-28 17:34:27 -0500 | [diff] [blame] | 17 | |
David Reid | ef0fcf2 | 2013-11-06 11:12:45 -0800 | [diff] [blame] | 18 | .. class:: HMAC(key, algorithm, backend) |
Paul Kehrer | 0317b04 | 2013-10-28 17:34:27 -0500 | [diff] [blame] | 19 | |
Alex Stapleton | 7904346 | 2014-03-09 16:46:26 +0000 | [diff] [blame] | 20 | HMAC objects take a ``key`` and a |
| 21 | :class:`~cryptography.hazmat.primitives.interfaces.HashAlgorithm` provider. |
Paul Kehrer | 50a8815 | 2013-10-29 10:46:05 -0500 | [diff] [blame] | 22 | 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 Kehrer | 0317b04 | 2013-10-28 17:34:27 -0500 | [diff] [blame] | 25 | |
Alex Gaynor | 58ecc8d | 2013-11-03 21:21:00 -0800 | [diff] [blame] | 26 | This is an implementation of :rfc:`2104`. |
| 27 | |
Paul Kehrer | 0317b04 | 2013-10-28 17:34:27 -0500 | [diff] [blame] | 28 | .. doctest:: |
| 29 | |
Alex Gaynor | f8796b1 | 2013-12-13 20:28:55 -0800 | [diff] [blame] | 30 | >>> from cryptography.hazmat.backends import default_backend |
Paul Kehrer | bf8962a | 2013-10-28 17:44:42 -0500 | [diff] [blame] | 31 | >>> from cryptography.hazmat.primitives import hashes, hmac |
David Reid | 63fa19a | 2013-11-20 10:49:13 -0800 | [diff] [blame] | 32 | >>> h = hmac.HMAC(key, hashes.SHA256(), backend=default_backend()) |
Paul Kehrer | 0317b04 | 2013-10-28 17:34:27 -0500 | [diff] [blame] | 33 | >>> h.update(b"message to hash") |
David Reid | 6753e39 | 2013-11-01 15:32:03 -0700 | [diff] [blame] | 34 | >>> 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 Kehrer | 0317b04 | 2013-10-28 17:34:27 -0500 | [diff] [blame] | 36 | |
Alex Stapleton | 447d64f | 2013-12-21 21:26:55 +0000 | [diff] [blame] | 37 | If the backend doesn't support the requested ``algorithm`` an |
Alex Gaynor | 7a489db | 2014-03-22 15:09:34 -0700 | [diff] [blame] | 38 | :class:`~cryptography.exceptions.UnsupportedAlgorithm` exception will be |
| 39 | raised. |
David Reid | 663295d | 2013-11-20 13:55:08 -0800 | [diff] [blame] | 40 | |
Paul Kehrer | 288bae7 | 2014-01-08 21:46:18 -0600 | [diff] [blame] | 41 | 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 Gaynor | 5bbcf76 | 2014-01-08 18:36:57 -0800 | [diff] [blame] | 43 | |
| 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 Stapleton | 7904346 | 2014-03-09 16:46:26 +0000 | [diff] [blame] | 51 | :param bytes key: Secret key as ``bytes``. |
| 52 | :param algorithm: An |
David Reid | 663295d | 2013-11-20 13:55:08 -0800 | [diff] [blame] | 53 | :class:`~cryptography.hazmat.primitives.interfaces.HashAlgorithm` |
| 54 | provider such as those described in |
| 55 | :ref:`Cryptographic Hashes <cryptographic-hash-algorithms>`. |
Alex Stapleton | 7904346 | 2014-03-09 16:46:26 +0000 | [diff] [blame] | 56 | :param backend: An |
Alex Gaynor | f8796b1 | 2013-12-13 20:28:55 -0800 | [diff] [blame] | 57 | :class:`~cryptography.hazmat.backends.interfaces.HMACBackend` |
David Reid | 663295d | 2013-11-20 13:55:08 -0800 | [diff] [blame] | 58 | provider. |
| 59 | |
Alex Gaynor | 7a489db | 2014-03-22 15:09:34 -0700 | [diff] [blame] | 60 | :raises cryptography.exceptions.UnsupportedAlgorithm: This is raised if the |
Ayrx | ecac029 | 2014-03-16 14:14:17 +0800 | [diff] [blame] | 61 | provided ``backend`` does not implement |
| 62 | :class:`~cryptography.hazmat.backends.interfaces.HMACBackend` |
| 63 | |
Paul Kehrer | 2824ab7 | 2013-10-28 11:06:55 -0500 | [diff] [blame] | 64 | .. method:: update(msg) |
Paul Kehrer | 0317b04 | 2013-10-28 17:34:27 -0500 | [diff] [blame] | 65 | |
Paul Kehrer | 50a8815 | 2013-10-29 10:46:05 -0500 | [diff] [blame] | 66 | :param bytes msg: The bytes to hash and authenticate. |
David Reid | 2cce618 | 2013-11-13 13:49:41 -0800 | [diff] [blame] | 67 | :raises cryptography.exceptions.AlreadyFinalized: See :meth:`finalize` |
Paul Kehrer | 0317b04 | 2013-10-28 17:34:27 -0500 | [diff] [blame] | 68 | |
| 69 | .. method:: copy() |
| 70 | |
David Reid | 2cce618 | 2013-11-13 13:49:41 -0800 | [diff] [blame] | 71 | Copy this :class:`HMAC` instance, usually so that we may call |
Alex Stapleton | 7904346 | 2014-03-09 16:46:26 +0000 | [diff] [blame] | 72 | :meth:`finalize` to get an intermediate digest value while we continue |
| 73 | to call :meth:`update` on the original instance. |
David Reid | 2cce618 | 2013-11-13 13:49:41 -0800 | [diff] [blame] | 74 | |
Alex Stapleton | 63b3de2 | 2014-02-08 09:43:16 +0000 | [diff] [blame] | 75 | :return: A new instance of :class:`HMAC` that can be updated |
David Reid | 2cce618 | 2013-11-13 13:49:41 -0800 | [diff] [blame] | 76 | and finalized independently of the original instance. |
| 77 | :raises cryptography.exceptions.AlreadyFinalized: See :meth:`finalize` |
Paul Kehrer | 0317b04 | 2013-10-28 17:34:27 -0500 | [diff] [blame] | 78 | |
Julian Krause | c91fe6a | 2013-12-25 11:00:49 -0800 | [diff] [blame] | 79 | .. method:: verify(signature) |
Julian Krause | 2288e30 | 2013-12-17 21:26:23 -0800 | [diff] [blame] | 80 | |
Alex Gaynor | 1225270 | 2014-01-02 11:10:35 -0800 | [diff] [blame] | 81 | Finalize the current context and securely compare digest to |
| 82 | ``signature``. |
Julian Krause | 2288e30 | 2013-12-17 21:26:23 -0800 | [diff] [blame] | 83 | |
Alex Gaynor | 1225270 | 2014-01-02 11:10:35 -0800 | [diff] [blame] | 84 | :param bytes signature: The bytes to compare the current digest |
| 85 | against. |
Julian Krause | 2288e30 | 2013-12-17 21:26:23 -0800 | [diff] [blame] | 86 | :raises cryptography.exceptions.AlreadyFinalized: See :meth:`finalize` |
Alex Gaynor | 1225270 | 2014-01-02 11:10:35 -0800 | [diff] [blame] | 87 | :raises cryptography.exceptions.InvalidSignature: If signature does not |
| 88 | match digest |
Alex Gaynor | 5bbcf76 | 2014-01-08 18:36:57 -0800 | [diff] [blame] | 89 | |
| 90 | .. method:: finalize() |
| 91 | |
| 92 | Finalize the current context and return the message digest as bytes. |
| 93 | |
Alex Stapleton | 7904346 | 2014-03-09 16:46:26 +0000 | [diff] [blame] | 94 | 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 Gaynor | 5bbcf76 | 2014-01-08 18:36:57 -0800 | [diff] [blame] | 98 | |
| 99 | :return bytes: The message digest as bytes. |
| 100 | :raises cryptography.exceptions.AlreadyFinalized: |