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

40 statements  

« prev     ^ index     » next       coverage.py v7.2.2, created at 2023-03-26 06:36 +0000

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 

5 

6import abc 

7 

8from cryptography.exceptions import UnsupportedAlgorithm, _Reasons 

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

10from cryptography.hazmat.primitives import _serialization 

11 

12 

13class X25519PublicKey(metaclass=abc.ABCMeta): 

14 @classmethod 

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

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

17 

18 if not backend.x25519_supported(): 

19 raise UnsupportedAlgorithm( 

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

21 _Reasons.UNSUPPORTED_EXCHANGE_ALGORITHM, 

22 ) 

23 

24 return backend.x25519_load_public_bytes(data) 

25 

26 @abc.abstractmethod 

27 def public_bytes( 

28 self, 

29 encoding: _serialization.Encoding, 

30 format: _serialization.PublicFormat, 

31 ) -> bytes: 

32 """ 

33 The serialized bytes of the public key. 

34 """ 

35 

36 @abc.abstractmethod 

37 def public_bytes_raw(self) -> bytes: 

38 """ 

39 The raw bytes of the public key. 

40 Equivalent to public_bytes(Raw, Raw). 

41 """ 

42 

43 

44# For LibreSSL 

45if hasattr(rust_openssl, "x25519"): 

46 X25519PublicKey.register(rust_openssl.x25519.X25519PublicKey) 

47 

48 

49class X25519PrivateKey(metaclass=abc.ABCMeta): 

50 @classmethod 

51 def generate(cls) -> "X25519PrivateKey": 

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

53 

54 if not backend.x25519_supported(): 

55 raise UnsupportedAlgorithm( 

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

57 _Reasons.UNSUPPORTED_EXCHANGE_ALGORITHM, 

58 ) 

59 return backend.x25519_generate_key() 

60 

61 @classmethod 

62 def from_private_bytes(cls, data: bytes) -> "X25519PrivateKey": 

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

64 

65 if not backend.x25519_supported(): 

66 raise UnsupportedAlgorithm( 

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

68 _Reasons.UNSUPPORTED_EXCHANGE_ALGORITHM, 

69 ) 

70 

71 return backend.x25519_load_private_bytes(data) 

72 

73 @abc.abstractmethod 

74 def public_key(self) -> X25519PublicKey: 

75 """ 

76 Returns the public key assosciated with this private key 

77 """ 

78 

79 @abc.abstractmethod 

80 def private_bytes( 

81 self, 

82 encoding: _serialization.Encoding, 

83 format: _serialization.PrivateFormat, 

84 encryption_algorithm: _serialization.KeySerializationEncryption, 

85 ) -> bytes: 

86 """ 

87 The serialized bytes of the private key. 

88 """ 

89 

90 @abc.abstractmethod 

91 def private_bytes_raw(self) -> bytes: 

92 """ 

93 The raw bytes of the private key. 

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

95 """ 

96 

97 @abc.abstractmethod 

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

99 """ 

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

101 """ 

102 

103 

104# For LibreSSL 

105if hasattr(rust_openssl, "x25519"): 

106 X25519PrivateKey.register(rust_openssl.x25519.X25519PrivateKey)