dsa.py 3.9 KB

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