dh.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. generate_parameters = rust_openssl.dh.generate_parameters
  9. DHPrivateNumbers = rust_openssl.dh.DHPrivateNumbers
  10. DHPublicNumbers = rust_openssl.dh.DHPublicNumbers
  11. DHParameterNumbers = rust_openssl.dh.DHParameterNumbers
  12. class DHParameters(metaclass=abc.ABCMeta):
  13. @abc.abstractmethod
  14. def generate_private_key(self) -> DHPrivateKey:
  15. """
  16. Generates and returns a DHPrivateKey.
  17. """
  18. @abc.abstractmethod
  19. def parameter_bytes(
  20. self,
  21. encoding: _serialization.Encoding,
  22. format: _serialization.ParameterFormat,
  23. ) -> bytes:
  24. """
  25. Returns the parameters serialized as bytes.
  26. """
  27. @abc.abstractmethod
  28. def parameter_numbers(self) -> DHParameterNumbers:
  29. """
  30. Returns a DHParameterNumbers.
  31. """
  32. DHParametersWithSerialization = DHParameters
  33. DHParameters.register(rust_openssl.dh.DHParameters)
  34. class DHPublicKey(metaclass=abc.ABCMeta):
  35. @property
  36. @abc.abstractmethod
  37. def key_size(self) -> int:
  38. """
  39. The bit length of the prime modulus.
  40. """
  41. @abc.abstractmethod
  42. def parameters(self) -> DHParameters:
  43. """
  44. The DHParameters object associated with this public key.
  45. """
  46. @abc.abstractmethod
  47. def public_numbers(self) -> DHPublicNumbers:
  48. """
  49. Returns a DHPublicNumbers.
  50. """
  51. @abc.abstractmethod
  52. def public_bytes(
  53. self,
  54. encoding: _serialization.Encoding,
  55. format: _serialization.PublicFormat,
  56. ) -> bytes:
  57. """
  58. Returns the key serialized as bytes.
  59. """
  60. @abc.abstractmethod
  61. def __eq__(self, other: object) -> bool:
  62. """
  63. Checks equality.
  64. """
  65. @abc.abstractmethod
  66. def __copy__(self) -> DHPublicKey:
  67. """
  68. Returns a copy.
  69. """
  70. @abc.abstractmethod
  71. def __deepcopy__(self, memo: dict) -> DHPublicKey:
  72. """
  73. Returns a deep copy.
  74. """
  75. DHPublicKeyWithSerialization = DHPublicKey
  76. DHPublicKey.register(rust_openssl.dh.DHPublicKey)
  77. class DHPrivateKey(metaclass=abc.ABCMeta):
  78. @property
  79. @abc.abstractmethod
  80. def key_size(self) -> int:
  81. """
  82. The bit length of the prime modulus.
  83. """
  84. @abc.abstractmethod
  85. def public_key(self) -> DHPublicKey:
  86. """
  87. The DHPublicKey associated with this private key.
  88. """
  89. @abc.abstractmethod
  90. def parameters(self) -> DHParameters:
  91. """
  92. The DHParameters object associated with this private key.
  93. """
  94. @abc.abstractmethod
  95. def exchange(self, peer_public_key: DHPublicKey) -> bytes:
  96. """
  97. Given peer's DHPublicKey, carry out the key exchange and
  98. return shared key as bytes.
  99. """
  100. @abc.abstractmethod
  101. def private_numbers(self) -> DHPrivateNumbers:
  102. """
  103. Returns a DHPrivateNumbers.
  104. """
  105. @abc.abstractmethod
  106. def private_bytes(
  107. self,
  108. encoding: _serialization.Encoding,
  109. format: _serialization.PrivateFormat,
  110. encryption_algorithm: _serialization.KeySerializationEncryption,
  111. ) -> bytes:
  112. """
  113. Returns the key serialized as bytes.
  114. """
  115. @abc.abstractmethod
  116. def __copy__(self) -> DHPrivateKey:
  117. """
  118. Returns a copy.
  119. """
  120. @abc.abstractmethod
  121. def __deepcopy__(self, memo: dict) -> DHPrivateKey:
  122. """
  123. Returns a deep copy.
  124. """
  125. DHPrivateKeyWithSerialization = DHPrivateKey
  126. DHPrivateKey.register(rust_openssl.dh.DHPrivateKey)