ed25519.py 2.9 KB

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