Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/cryptography/hazmat/primitives/asymmetric/dsa.py: 91%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

55 statements  

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 

5from __future__ import annotations 

6 

7import abc 

8import typing 

9 

10from cryptography.hazmat.bindings._rust import openssl as rust_openssl 

11from cryptography.hazmat.primitives import _serialization, hashes 

12from cryptography.hazmat.primitives.asymmetric import utils as asym_utils 

13 

14 

15class DSAParameters(metaclass=abc.ABCMeta): 

16 @abc.abstractmethod 

17 def generate_private_key(self) -> DSAPrivateKey: 

18 """ 

19 Generates and returns a DSAPrivateKey. 

20 """ 

21 

22 @abc.abstractmethod 

23 def parameter_numbers(self) -> DSAParameterNumbers: 

24 """ 

25 Returns a DSAParameterNumbers. 

26 """ 

27 

28 

29DSAParametersWithNumbers = DSAParameters 

30DSAParameters.register(rust_openssl.dsa.DSAParameters) 

31 

32 

33class DSAPrivateKey(metaclass=abc.ABCMeta): 

34 @property 

35 @abc.abstractmethod 

36 def key_size(self) -> int: 

37 """ 

38 The bit length of the prime modulus. 

39 """ 

40 

41 @abc.abstractmethod 

42 def public_key(self) -> DSAPublicKey: 

43 """ 

44 The DSAPublicKey associated with this private key. 

45 """ 

46 

47 @abc.abstractmethod 

48 def parameters(self) -> DSAParameters: 

49 """ 

50 The DSAParameters object associated with this private key. 

51 """ 

52 

53 @abc.abstractmethod 

54 def sign( 

55 self, 

56 data: bytes, 

57 algorithm: asym_utils.Prehashed | hashes.HashAlgorithm, 

58 ) -> bytes: 

59 """ 

60 Signs the data 

61 """ 

62 

63 @abc.abstractmethod 

64 def private_numbers(self) -> DSAPrivateNumbers: 

65 """ 

66 Returns a DSAPrivateNumbers. 

67 """ 

68 

69 @abc.abstractmethod 

70 def private_bytes( 

71 self, 

72 encoding: _serialization.Encoding, 

73 format: _serialization.PrivateFormat, 

74 encryption_algorithm: _serialization.KeySerializationEncryption, 

75 ) -> bytes: 

76 """ 

77 Returns the key serialized as bytes. 

78 """ 

79 

80 

81DSAPrivateKeyWithSerialization = DSAPrivateKey 

82DSAPrivateKey.register(rust_openssl.dsa.DSAPrivateKey) 

83 

84 

85class DSAPublicKey(metaclass=abc.ABCMeta): 

86 @property 

87 @abc.abstractmethod 

88 def key_size(self) -> int: 

89 """ 

90 The bit length of the prime modulus. 

91 """ 

92 

93 @abc.abstractmethod 

94 def parameters(self) -> DSAParameters: 

95 """ 

96 The DSAParameters object associated with this public key. 

97 """ 

98 

99 @abc.abstractmethod 

100 def public_numbers(self) -> DSAPublicNumbers: 

101 """ 

102 Returns a DSAPublicNumbers. 

103 """ 

104 

105 @abc.abstractmethod 

106 def public_bytes( 

107 self, 

108 encoding: _serialization.Encoding, 

109 format: _serialization.PublicFormat, 

110 ) -> bytes: 

111 """ 

112 Returns the key serialized as bytes. 

113 """ 

114 

115 @abc.abstractmethod 

116 def verify( 

117 self, 

118 signature: bytes, 

119 data: bytes, 

120 algorithm: asym_utils.Prehashed | hashes.HashAlgorithm, 

121 ) -> None: 

122 """ 

123 Verifies the signature of the data. 

124 """ 

125 

126 @abc.abstractmethod 

127 def __eq__(self, other: object) -> bool: 

128 """ 

129 Checks equality. 

130 """ 

131 

132 

133DSAPublicKeyWithSerialization = DSAPublicKey 

134DSAPublicKey.register(rust_openssl.dsa.DSAPublicKey) 

135 

136DSAPrivateNumbers = rust_openssl.dsa.DSAPrivateNumbers 

137DSAPublicNumbers = rust_openssl.dsa.DSAPublicNumbers 

138DSAParameterNumbers = rust_openssl.dsa.DSAParameterNumbers 

139 

140 

141def generate_parameters( 

142 key_size: int, backend: typing.Any = None 

143) -> DSAParameters: 

144 if key_size not in (1024, 2048, 3072, 4096): 

145 raise ValueError("Key size must be 1024, 2048, 3072, or 4096 bits.") 

146 

147 return rust_openssl.dsa.generate_parameters(key_size) 

148 

149 

150def generate_private_key( 

151 key_size: int, backend: typing.Any = None 

152) -> DSAPrivateKey: 

153 parameters = generate_parameters(key_size) 

154 return parameters.generate_private_key()