dh.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 import utils
  7. from cryptography.hazmat.bindings._rust import openssl as rust_openssl
  8. from cryptography.hazmat.primitives import _serialization
  9. _FFDH_DEPRECATION_MSG = (
  10. "Diffie-Hellman over finite fields (FFDH) is deprecated and support "
  11. "will be removed in a future release. Use a more modern key exchange "
  12. "algorithm."
  13. )
  14. generate_parameters = rust_openssl.dh.generate_parameters
  15. DHPrivateNumbers = rust_openssl.dh.DHPrivateNumbers
  16. DHPublicNumbers = rust_openssl.dh.DHPublicNumbers
  17. DHParameterNumbers = rust_openssl.dh.DHParameterNumbers
  18. class DHParameters(metaclass=abc.ABCMeta):
  19. @abc.abstractmethod
  20. def generate_private_key(self) -> DHPrivateKey:
  21. """
  22. Generates and returns a DHPrivateKey.
  23. """
  24. @abc.abstractmethod
  25. def parameter_bytes(
  26. self,
  27. encoding: _serialization.Encoding,
  28. format: _serialization.ParameterFormat,
  29. ) -> bytes:
  30. """
  31. Returns the parameters serialized as bytes.
  32. """
  33. @abc.abstractmethod
  34. def parameter_numbers(self) -> DHParameterNumbers:
  35. """
  36. Returns a DHParameterNumbers.
  37. """
  38. DHParametersWithSerialization = DHParameters
  39. DHParameters.register(rust_openssl.dh.DHParameters)
  40. class DHPublicKey(metaclass=abc.ABCMeta):
  41. @property
  42. @abc.abstractmethod
  43. def key_size(self) -> int:
  44. """
  45. The bit length of the prime modulus.
  46. """
  47. @abc.abstractmethod
  48. def parameters(self) -> DHParameters:
  49. """
  50. The DHParameters object associated with this public key.
  51. """
  52. @abc.abstractmethod
  53. def public_numbers(self) -> DHPublicNumbers:
  54. """
  55. Returns a DHPublicNumbers.
  56. """
  57. @abc.abstractmethod
  58. def public_bytes(
  59. self,
  60. encoding: _serialization.Encoding,
  61. format: _serialization.PublicFormat,
  62. ) -> bytes:
  63. """
  64. Returns the key serialized as bytes.
  65. """
  66. @abc.abstractmethod
  67. def __eq__(self, other: object) -> bool:
  68. """
  69. Checks equality.
  70. """
  71. @abc.abstractmethod
  72. def __copy__(self) -> DHPublicKey:
  73. """
  74. Returns a copy.
  75. """
  76. @abc.abstractmethod
  77. def __deepcopy__(self, memo: dict) -> DHPublicKey:
  78. """
  79. Returns a deep copy.
  80. """
  81. DHPublicKeyWithSerialization = DHPublicKey
  82. DHPublicKey.register(rust_openssl.dh.DHPublicKey)
  83. class DHPrivateKey(metaclass=abc.ABCMeta):
  84. @property
  85. @abc.abstractmethod
  86. def key_size(self) -> int:
  87. """
  88. The bit length of the prime modulus.
  89. """
  90. @abc.abstractmethod
  91. def public_key(self) -> DHPublicKey:
  92. """
  93. The DHPublicKey associated with this private key.
  94. """
  95. @abc.abstractmethod
  96. def parameters(self) -> DHParameters:
  97. """
  98. The DHParameters object associated with this private key.
  99. """
  100. @abc.abstractmethod
  101. def exchange(self, peer_public_key: DHPublicKey) -> bytes:
  102. """
  103. Given peer's DHPublicKey, carry out the key exchange and
  104. return shared key as bytes.
  105. """
  106. @abc.abstractmethod
  107. def private_numbers(self) -> DHPrivateNumbers:
  108. """
  109. Returns a DHPrivateNumbers.
  110. """
  111. @abc.abstractmethod
  112. def private_bytes(
  113. self,
  114. encoding: _serialization.Encoding,
  115. format: _serialization.PrivateFormat,
  116. encryption_algorithm: _serialization.KeySerializationEncryption,
  117. ) -> bytes:
  118. """
  119. Returns the key serialized as bytes.
  120. """
  121. @abc.abstractmethod
  122. def __copy__(self) -> DHPrivateKey:
  123. """
  124. Returns a copy.
  125. """
  126. @abc.abstractmethod
  127. def __deepcopy__(self, memo: dict) -> DHPrivateKey:
  128. """
  129. Returns a deep copy.
  130. """
  131. DHPrivateKeyWithSerialization = DHPrivateKey
  132. DHPrivateKey.register(rust_openssl.dh.DHPrivateKey)
  133. # Aliases that do not emit the deprecation warning on attribute access, for
  134. # internal use (e.g. the unions in
  135. # cryptography.hazmat.primitives.asymmetric.types, which are evaluated at
  136. # import time).
  137. _DHPublicKey = DHPublicKey
  138. _DHPrivateKey = DHPrivateKey
  139. utils.deprecated(
  140. generate_parameters,
  141. __name__,
  142. _FFDH_DEPRECATION_MSG,
  143. utils.DeprecatedIn50,
  144. name="generate_parameters",
  145. )
  146. utils.deprecated(
  147. DHPrivateNumbers,
  148. __name__,
  149. _FFDH_DEPRECATION_MSG,
  150. utils.DeprecatedIn50,
  151. name="DHPrivateNumbers",
  152. )
  153. utils.deprecated(
  154. DHPublicNumbers,
  155. __name__,
  156. _FFDH_DEPRECATION_MSG,
  157. utils.DeprecatedIn50,
  158. name="DHPublicNumbers",
  159. )
  160. utils.deprecated(
  161. DHParameterNumbers,
  162. __name__,
  163. _FFDH_DEPRECATION_MSG,
  164. utils.DeprecatedIn50,
  165. name="DHParameterNumbers",
  166. )
  167. utils.deprecated(
  168. DHParameters,
  169. __name__,
  170. _FFDH_DEPRECATION_MSG,
  171. utils.DeprecatedIn50,
  172. name="DHParameters",
  173. )
  174. utils.deprecated(
  175. DHParameters,
  176. __name__,
  177. _FFDH_DEPRECATION_MSG,
  178. utils.DeprecatedIn50,
  179. name="DHParametersWithSerialization",
  180. )
  181. utils.deprecated(
  182. DHPublicKey,
  183. __name__,
  184. _FFDH_DEPRECATION_MSG,
  185. utils.DeprecatedIn50,
  186. name="DHPublicKey",
  187. )
  188. utils.deprecated(
  189. DHPublicKey,
  190. __name__,
  191. _FFDH_DEPRECATION_MSG,
  192. utils.DeprecatedIn50,
  193. name="DHPublicKeyWithSerialization",
  194. )
  195. utils.deprecated(
  196. DHPrivateKey,
  197. __name__,
  198. _FFDH_DEPRECATION_MSG,
  199. utils.DeprecatedIn50,
  200. name="DHPrivateKey",
  201. )
  202. utils.deprecated(
  203. DHPrivateKey,
  204. __name__,
  205. _FFDH_DEPRECATION_MSG,
  206. utils.DeprecatedIn50,
  207. name="DHPrivateKeyWithSerialization",
  208. )