ed448.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 annotations
  5. import abc
  6. from cryptography.exceptions import UnsupportedAlgorithm, _Reasons
  7. from cryptography.hazmat.bindings._rust import openssl as rust_openssl
  8. from cryptography.hazmat.primitives import _serialization
  9. class Ed448PublicKey(metaclass=abc.ABCMeta):
  10. @classmethod
  11. def from_public_bytes(cls, data: bytes) -> Ed448PublicKey:
  12. from cryptography.hazmat.backends.openssl.backend import backend
  13. if not backend.ed448_supported():
  14. raise UnsupportedAlgorithm(
  15. "ed448 is not supported by this version of OpenSSL.",
  16. _Reasons.UNSUPPORTED_PUBLIC_KEY_ALGORITHM,
  17. )
  18. return rust_openssl.ed448.from_public_bytes(data)
  19. @abc.abstractmethod
  20. def public_bytes(
  21. self,
  22. encoding: _serialization.Encoding,
  23. format: _serialization.PublicFormat,
  24. ) -> bytes:
  25. """
  26. The serialized bytes of the public key.
  27. """
  28. @abc.abstractmethod
  29. def public_bytes_raw(self) -> bytes:
  30. """
  31. The raw bytes of the public key.
  32. Equivalent to public_bytes(Raw, Raw).
  33. """
  34. @abc.abstractmethod
  35. def verify(self, signature: bytes, data: bytes) -> None:
  36. """
  37. Verify the signature.
  38. """
  39. @abc.abstractmethod
  40. def __eq__(self, other: object) -> bool:
  41. """
  42. Checks equality.
  43. """
  44. if hasattr(rust_openssl, "ed448"):
  45. Ed448PublicKey.register(rust_openssl.ed448.Ed448PublicKey)
  46. class Ed448PrivateKey(metaclass=abc.ABCMeta):
  47. @classmethod
  48. def generate(cls) -> Ed448PrivateKey:
  49. from cryptography.hazmat.backends.openssl.backend import backend
  50. if not backend.ed448_supported():
  51. raise UnsupportedAlgorithm(
  52. "ed448 is not supported by this version of OpenSSL.",
  53. _Reasons.UNSUPPORTED_PUBLIC_KEY_ALGORITHM,
  54. )
  55. return rust_openssl.ed448.generate_key()
  56. @classmethod
  57. def from_private_bytes(cls, data: bytes) -> Ed448PrivateKey:
  58. from cryptography.hazmat.backends.openssl.backend import backend
  59. if not backend.ed448_supported():
  60. raise UnsupportedAlgorithm(
  61. "ed448 is not supported by this version of OpenSSL.",
  62. _Reasons.UNSUPPORTED_PUBLIC_KEY_ALGORITHM,
  63. )
  64. return rust_openssl.ed448.from_private_bytes(data)
  65. @abc.abstractmethod
  66. def public_key(self) -> Ed448PublicKey:
  67. """
  68. The Ed448PublicKey derived from the private key.
  69. """
  70. @abc.abstractmethod
  71. def sign(self, data: bytes) -> bytes:
  72. """
  73. Signs the data.
  74. """
  75. @abc.abstractmethod
  76. def private_bytes(
  77. self,
  78. encoding: _serialization.Encoding,
  79. format: _serialization.PrivateFormat,
  80. encryption_algorithm: _serialization.KeySerializationEncryption,
  81. ) -> bytes:
  82. """
  83. The serialized bytes of the private key.
  84. """
  85. @abc.abstractmethod
  86. def private_bytes_raw(self) -> bytes:
  87. """
  88. The raw bytes of the private key.
  89. Equivalent to private_bytes(Raw, Raw, NoEncryption()).
  90. """
  91. if hasattr(rust_openssl, "x448"):
  92. Ed448PrivateKey.register(rust_openssl.ed448.Ed448PrivateKey)