You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

44 line
1.4 KiB

  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. AlreadyFinalized, UnsupportedAlgorithm, _Reasons
  8. )
  9. class Poly1305(object):
  10. def __init__(self, key):
  11. from cryptography.hazmat.backends.openssl.backend import backend
  12. if not backend.poly1305_supported():
  13. raise UnsupportedAlgorithm(
  14. "poly1305 is not supported by this version of OpenSSL.",
  15. _Reasons.UNSUPPORTED_MAC
  16. )
  17. self._ctx = backend.create_poly1305_ctx(key)
  18. def update(self, data):
  19. if self._ctx is None:
  20. raise AlreadyFinalized("Context was already finalized.")
  21. utils._check_byteslike("data", data)
  22. self._ctx.update(data)
  23. def finalize(self):
  24. if self._ctx is None:
  25. raise AlreadyFinalized("Context was already finalized.")
  26. mac = self._ctx.finalize()
  27. self._ctx = None
  28. return mac
  29. def verify(self, tag):
  30. utils._check_bytes("tag", tag)
  31. if self._ctx is None:
  32. raise AlreadyFinalized("Context was already finalized.")
  33. ctx, self._ctx = self._ctx, None
  34. ctx.verify(tag)