dsa.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. DSAPrivateKeyWithSerialization = DSAPrivateKey
  71. DSAPrivateKey.register(rust_openssl.dsa.DSAPrivateKey)
  72. class DSAPublicKey(metaclass=abc.ABCMeta):
  73. @property
  74. @abc.abstractmethod
  75. def key_size(self) -> int:
  76. """
  77. The bit length of the prime modulus.
  78. """
  79. @abc.abstractmethod
  80. def parameters(self) -> DSAParameters:
  81. """
  82. The DSAParameters object associated with this public key.
  83. """
  84. @abc.abstractmethod
  85. def public_numbers(self) -> DSAPublicNumbers:
  86. """
  87. Returns a DSAPublicNumbers.
  88. """
  89. @abc.abstractmethod
  90. def public_bytes(
  91. self,
  92. encoding: _serialization.Encoding,
  93. format: _serialization.PublicFormat,
  94. ) -> bytes:
  95. """
  96. Returns the key serialized as bytes.
  97. """
  98. @abc.abstractmethod
  99. def verify(
  100. self,
  101. signature: Buffer,
  102. data: Buffer,
  103. algorithm: asym_utils.Prehashed | hashes.HashAlgorithm,
  104. ) -> None:
  105. """
  106. Verifies the signature of the data.
  107. """
  108. @abc.abstractmethod
  109. def __eq__(self, other: object) -> bool:
  110. """
  111. Checks equality.
  112. """
  113. @abc.abstractmethod
  114. def __copy__(self) -> DSAPublicKey:
  115. """
  116. Returns a copy.
  117. """
  118. DSAPublicKeyWithSerialization = DSAPublicKey
  119. DSAPublicKey.register(rust_openssl.dsa.DSAPublicKey)
  120. DSAPrivateNumbers = rust_openssl.dsa.DSAPrivateNumbers
  121. DSAPublicNumbers = rust_openssl.dsa.DSAPublicNumbers
  122. DSAParameterNumbers = rust_openssl.dsa.DSAParameterNumbers
  123. def generate_parameters(
  124. key_size: int, backend: typing.Any = None
  125. ) -> DSAParameters:
  126. if key_size not in (1024, 2048, 3072, 4096):
  127. raise ValueError("Key size must be 1024, 2048, 3072, or 4096 bits.")
  128. return rust_openssl.dsa.generate_parameters(key_size)
  129. def generate_private_key(
  130. key_size: int, backend: typing.Any = None
  131. ) -> DSAPrivateKey:
  132. parameters = generate_parameters(key_size)
  133. return parameters.generate_private_key()