hmac.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. import typing
  5. from cryptography.exceptions import (
  6. InvalidSignature,
  7. UnsupportedAlgorithm,
  8. _Reasons,
  9. )
  10. from cryptography.hazmat.primitives import constant_time, hashes
  11. if typing.TYPE_CHECKING:
  12. from cryptography.hazmat.backends.openssl.backend import Backend
  13. class _HMACContext(hashes.HashContext):
  14. def __init__(
  15. self,
  16. backend: "Backend",
  17. key: bytes,
  18. algorithm: hashes.HashAlgorithm,
  19. ctx=None,
  20. ):
  21. self._algorithm = algorithm
  22. self._backend = backend
  23. if ctx is None:
  24. ctx = self._backend._lib.HMAC_CTX_new()
  25. self._backend.openssl_assert(ctx != self._backend._ffi.NULL)
  26. ctx = self._backend._ffi.gc(ctx, self._backend._lib.HMAC_CTX_free)
  27. evp_md = self._backend._evp_md_from_algorithm(algorithm)
  28. if evp_md == self._backend._ffi.NULL:
  29. raise UnsupportedAlgorithm(
  30. "{} is not a supported hash on this backend".format(
  31. algorithm.name
  32. ),
  33. _Reasons.UNSUPPORTED_HASH,
  34. )
  35. key_ptr = self._backend._ffi.from_buffer(key)
  36. res = self._backend._lib.HMAC_Init_ex(
  37. ctx, key_ptr, len(key), evp_md, self._backend._ffi.NULL
  38. )
  39. self._backend.openssl_assert(res != 0)
  40. self._ctx = ctx
  41. self._key = key
  42. @property
  43. def algorithm(self) -> hashes.HashAlgorithm:
  44. return self._algorithm
  45. def copy(self) -> "_HMACContext":
  46. copied_ctx = self._backend._lib.HMAC_CTX_new()
  47. self._backend.openssl_assert(copied_ctx != self._backend._ffi.NULL)
  48. copied_ctx = self._backend._ffi.gc(
  49. copied_ctx, self._backend._lib.HMAC_CTX_free
  50. )
  51. res = self._backend._lib.HMAC_CTX_copy(copied_ctx, self._ctx)
  52. self._backend.openssl_assert(res != 0)
  53. return _HMACContext(
  54. self._backend, self._key, self.algorithm, ctx=copied_ctx
  55. )
  56. def update(self, data: bytes) -> None:
  57. data_ptr = self._backend._ffi.from_buffer(data)
  58. res = self._backend._lib.HMAC_Update(self._ctx, data_ptr, len(data))
  59. self._backend.openssl_assert(res != 0)
  60. def finalize(self) -> bytes:
  61. buf = self._backend._ffi.new(
  62. "unsigned char[]", self._backend._lib.EVP_MAX_MD_SIZE
  63. )
  64. outlen = self._backend._ffi.new("unsigned int *")
  65. res = self._backend._lib.HMAC_Final(self._ctx, buf, outlen)
  66. self._backend.openssl_assert(res != 0)
  67. self._backend.openssl_assert(outlen[0] == self.algorithm.digest_size)
  68. return self._backend._ffi.buffer(buf)[: outlen[0]]
  69. def verify(self, signature: bytes) -> None:
  70. digest = self.finalize()
  71. if not constant_time.bytes_eq(digest, signature):
  72. raise InvalidSignature("Signature did not match digest.")