Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/cryptography/hazmat/primitives/asymmetric/x25519.py: 75%

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

51 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 

8 

9from cryptography.exceptions import UnsupportedAlgorithm, _Reasons 

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

11from cryptography.hazmat.primitives import _serialization 

12from cryptography.utils import Buffer 

13 

14 

15class X25519PublicKey(metaclass=abc.ABCMeta): 

16 @classmethod 

17 def from_public_bytes(cls, data: bytes) -> X25519PublicKey: 

18 from cryptography.hazmat.backends.openssl.backend import backend 

19 

20 if not backend.x25519_supported(): 

21 raise UnsupportedAlgorithm( 

22 "X25519 is not supported by this version of OpenSSL.", 

23 _Reasons.UNSUPPORTED_EXCHANGE_ALGORITHM, 

24 ) 

25 

26 return rust_openssl.x25519.from_public_bytes(data) 

27 

28 @abc.abstractmethod 

29 def public_bytes( 

30 self, 

31 encoding: _serialization.Encoding, 

32 format: _serialization.PublicFormat, 

33 ) -> bytes: 

34 """ 

35 The serialized bytes of the public key. 

36 """ 

37 

38 @abc.abstractmethod 

39 def public_bytes_raw(self) -> bytes: 

40 """ 

41 The raw bytes of the public key. 

42 Equivalent to public_bytes(Raw, Raw). 

43 """ 

44 

45 @abc.abstractmethod 

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

47 """ 

48 Checks equality. 

49 """ 

50 

51 @abc.abstractmethod 

52 def __copy__(self) -> X25519PublicKey: 

53 """ 

54 Returns a copy. 

55 """ 

56 

57 @abc.abstractmethod 

58 def __deepcopy__(self, memo: dict) -> X25519PublicKey: 

59 """ 

60 Returns a deep copy. 

61 """ 

62 

63 

64X25519PublicKey.register(rust_openssl.x25519.X25519PublicKey) 

65 

66 

67class X25519PrivateKey(metaclass=abc.ABCMeta): 

68 @classmethod 

69 def generate(cls) -> X25519PrivateKey: 

70 from cryptography.hazmat.backends.openssl.backend import backend 

71 

72 if not backend.x25519_supported(): 

73 raise UnsupportedAlgorithm( 

74 "X25519 is not supported by this version of OpenSSL.", 

75 _Reasons.UNSUPPORTED_EXCHANGE_ALGORITHM, 

76 ) 

77 return rust_openssl.x25519.generate_key() 

78 

79 @classmethod 

80 def from_private_bytes(cls, data: Buffer) -> X25519PrivateKey: 

81 from cryptography.hazmat.backends.openssl.backend import backend 

82 

83 if not backend.x25519_supported(): 

84 raise UnsupportedAlgorithm( 

85 "X25519 is not supported by this version of OpenSSL.", 

86 _Reasons.UNSUPPORTED_EXCHANGE_ALGORITHM, 

87 ) 

88 

89 return rust_openssl.x25519.from_private_bytes(data) 

90 

91 @abc.abstractmethod 

92 def public_key(self) -> X25519PublicKey: 

93 """ 

94 Returns the public key associated with this private key 

95 """ 

96 

97 @abc.abstractmethod 

98 def private_bytes( 

99 self, 

100 encoding: _serialization.Encoding, 

101 format: _serialization.PrivateFormat, 

102 encryption_algorithm: _serialization.KeySerializationEncryption, 

103 ) -> bytes: 

104 """ 

105 The serialized bytes of the private key. 

106 """ 

107 

108 @abc.abstractmethod 

109 def private_bytes_raw(self) -> bytes: 

110 """ 

111 The raw bytes of the private key. 

112 Equivalent to private_bytes(Raw, Raw, NoEncryption()). 

113 """ 

114 

115 @abc.abstractmethod 

116 def exchange(self, peer_public_key: X25519PublicKey) -> bytes: 

117 """ 

118 Performs a key exchange operation using the provided peer's public key. 

119 """ 

120 

121 @abc.abstractmethod 

122 def __copy__(self) -> X25519PrivateKey: 

123 """ 

124 Returns a copy. 

125 """ 

126 

127 @abc.abstractmethod 

128 def __deepcopy__(self, memo: dict) -> X25519PrivateKey: 

129 """ 

130 Returns a deep copy. 

131 """ 

132 

133 

134X25519PrivateKey.register(rust_openssl.x25519.X25519PrivateKey)