dsa.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. import typing
  7. from cryptography.hazmat.bindings._rust import openssl as rust_openssl
  8. from cryptography.hazmat.primitives import _serialization, hashes
  9. from cryptography.hazmat.primitives.asymmetric import utils as asym_utils
  10. from cryptography.utils import Buffer
  11. class DSAParameters(metaclass=abc.ABCMeta):
  12. @abc.abstractmethod
  13. def generate_private_key(self) -> DSAPrivateKey:
  14. """
  15. Generates and returns a DSAPrivateKey.
  16. """
  17. @abc.abstractmethod
  18. def parameter_numbers(self) -> DSAParameterNumbers:
  19. """
  20. Returns a DSAParameterNumbers.
  21. """
  22. DSAParametersWithNumbers = DSAParameters
  23. DSAParameters.register(rust_openssl.dsa.DSAParameters)
  24. class DSAPrivateKey(metaclass=abc.ABCMeta):
  25. @property
  26. @abc.abstractmethod
  27. def key_size(self) -> int:
  28. """
  29. The bit length of the prime modulus.
  30. """
  31. @abc.abstractmethod
  32. def public_key(self) -> DSAPublicKey:
  33. """
  34. The DSAPublicKey associated with this private key.
  35. """
  36. @abc.abstractmethod
  37. def parameters(self) -> DSAParameters:
  38. """
  39. The DSAParameters object associated with this private key.
  40. """
  41. @abc.abstractmethod
  42. def sign(
  43. self,
  44. data: Buffer,
  45. algorithm: asym_utils.Prehashed | hashes.HashAlgorithm,
  46. ) -> bytes:
  47. """
  48. Signs the data
  49. """
  50. @abc.abstractmethod
  51. def private_numbers(self) -> DSAPrivateNumbers:
  52. """
  53. Returns a DSAPrivateNumbers.
  54. """
  55. @abc.abstractmethod
  56. def private_bytes(
  57. self,
  58. encoding: _serialization.Encoding,
  59. format: _serialization.PrivateFormat,
  60. encryption_algorithm: _serialization.KeySerializationEncryption,
  61. ) -> bytes:
  62. """
  63. Returns the key serialized as bytes.
  64. """
  65. @abc.abstractmethod
  66. def __copy__(self) -> DSAPrivateKey:
  67. """
  68. Returns a copy.
  69. """
  70. @abc.abstractmethod
  71. def __deepcopy__(self, memo: dict) -> DSAPrivateKey:
  72. """
  73. Returns a deep copy.
  74. """
  75. DSAPrivateKeyWithSerialization = DSAPrivateKey
  76. DSAPrivateKey.register(rust_openssl.dsa.DSAPrivateKey)
  77. class DSAPublicKey(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 parameters(self) -> DSAParameters:
  86. """
  87. The DSAParameters object associated with this public key.
  88. """
  89. @abc.abstractmethod
  90. def public_numbers(self) -> DSAPublicNumbers:
  91. """
  92. Returns a DSAPublicNumbers.
  93. """
  94. @abc.abstractmethod
  95. def public_bytes(
  96. self,
  97. encoding: _serialization.Encoding,
  98. format: _serialization.PublicFormat,
  99. ) -> bytes:
  100. """
  101. Returns the key serialized as bytes.
  102. """
  103. @abc.abstractmethod
  104. def verify(
  105. self,
  106. signature: Buffer,
  107. data: Buffer,
  108. algorithm: asym_utils.Prehashed | hashes.HashAlgorithm,
  109. ) -> None:
  110. """
  111. Verifies the signature of the data.
  112. """
  113. @abc.abstractmethod
  114. def __eq__(self, other: object) -> bool:
  115. """
  116. Checks equality.
  117. """
  118. @abc.abstractmethod
  119. def __copy__(self) -> DSAPublicKey:
  120. """
  121. Returns a copy.
  122. """
  123. @abc.abstractmethod
  124. def __deepcopy__(self, memo: dict) -> DSAPublicKey:
  125. """
  126. Returns a deep copy.
  127. """
  128. DSAPublicKeyWithSerialization = DSAPublicKey
  129. DSAPublicKey.register(rust_openssl.dsa.DSAPublicKey)
  130. DSAPrivateNumbers = rust_openssl.dsa.DSAPrivateNumbers
  131. DSAPublicNumbers = rust_openssl.dsa.DSAPublicNumbers
  132. DSAParameterNumbers = rust_openssl.dsa.DSAParameterNumbers
  133. def generate_parameters(
  134. key_size: int, backend: typing.Any = None
  135. ) -> DSAParameters:
  136. if key_size not in (1024, 2048, 3072, 4096):
  137. raise ValueError("Key size must be 1024, 2048, 3072, or 4096 bits.")
  138. return rust_openssl.dsa.generate_parameters(key_size)
  139. def generate_private_key(
  140. key_size: int, backend: typing.Any = None
  141. ) -> DSAPrivateKey:
  142. parameters = generate_parameters(key_size)
  143. return parameters.generate_private_key()