hmac.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. # This file is dual licensed under the terms of the Apache License, Version
  2. # 2.0, and the BSD License. See the LICENSE file in the root of this repository
  3. # for complete details.
  4. from __future__ import absolute_import, division, print_function
  5. from cryptography import utils
  6. from cryptography.exceptions import (
  7. InvalidSignature,
  8. UnsupportedAlgorithm,
  9. _Reasons,
  10. )
  11. from cryptography.hazmat.primitives import constant_time, hashes
  12. @utils.register_interface(hashes.HashContext)
  13. class _HMACContext(object):
  14. def __init__(self, backend, key, algorithm, ctx=None):
  15. self._algorithm = algorithm
  16. self._backend = backend
  17. if ctx is None:
  18. ctx = self._backend._lib.Cryptography_HMAC_CTX_new()
  19. self._backend.openssl_assert(ctx != self._backend._ffi.NULL)
  20. ctx = self._backend._ffi.gc(
  21. ctx, self._backend._lib.Cryptography_HMAC_CTX_free
  22. )
  23. evp_md = self._backend._evp_md_from_algorithm(algorithm)
  24. if evp_md == self._backend._ffi.NULL:
  25. raise UnsupportedAlgorithm(
  26. "{} is not a supported hash on this backend".format(
  27. algorithm.name
  28. ),
  29. _Reasons.UNSUPPORTED_HASH,
  30. )
  31. key_ptr = self._backend._ffi.from_buffer(key)
  32. res = self._backend._lib.HMAC_Init_ex(
  33. ctx, key_ptr, len(key), evp_md, self._backend._ffi.NULL
  34. )
  35. self._backend.openssl_assert(res != 0)
  36. self._ctx = ctx
  37. self._key = key
  38. algorithm = utils.read_only_property("_algorithm")
  39. def copy(self):
  40. copied_ctx = self._backend._lib.Cryptography_HMAC_CTX_new()
  41. self._backend.openssl_assert(copied_ctx != self._backend._ffi.NULL)
  42. copied_ctx = self._backend._ffi.gc(
  43. copied_ctx, self._backend._lib.Cryptography_HMAC_CTX_free
  44. )
  45. res = self._backend._lib.HMAC_CTX_copy(copied_ctx, self._ctx)
  46. self._backend.openssl_assert(res != 0)
  47. return _HMACContext(
  48. self._backend, self._key, self.algorithm, ctx=copied_ctx
  49. )
  50. def update(self, data):
  51. data_ptr = self._backend._ffi.from_buffer(data)
  52. res = self._backend._lib.HMAC_Update(self._ctx, data_ptr, len(data))
  53. self._backend.openssl_assert(res != 0)
  54. def finalize(self):
  55. buf = self._backend._ffi.new(
  56. "unsigned char[]", self._backend._lib.EVP_MAX_MD_SIZE
  57. )
  58. outlen = self._backend._ffi.new("unsigned int *")
  59. res = self._backend._lib.HMAC_Final(self._ctx, buf, outlen)
  60. self._backend.openssl_assert(res != 0)
  61. self._backend.openssl_assert(outlen[0] == self.algorithm.digest_size)
  62. return self._backend._ffi.buffer(buf)[: outlen[0]]
  63. def verify(self, signature):
  64. digest = self.finalize()
  65. if not constant_time.bytes_eq(digest, signature):
  66. raise InvalidSignature("Signature did not match digest.")