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

35 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.primitives import _serialization 

10 

11 

12class X448PublicKey(metaclass=abc.ABCMeta): 

13 @classmethod 

14 def from_public_bytes(cls, data: bytes) -> "X448PublicKey": 

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

16 

17 if not backend.x448_supported(): 

18 raise UnsupportedAlgorithm( 

19 "X448 is not supported by this version of OpenSSL.", 

20 _Reasons.UNSUPPORTED_EXCHANGE_ALGORITHM, 

21 ) 

22 

23 return backend.x448_load_public_bytes(data) 

24 

25 @abc.abstractmethod 

26 def public_bytes( 

27 self, 

28 encoding: _serialization.Encoding, 

29 format: _serialization.PublicFormat, 

30 ) -> bytes: 

31 """ 

32 The serialized bytes of the public key. 

33 """ 

34 

35 def public_bytes_raw(self) -> bytes: 

36 """ 

37 The raw bytes of the public key. 

38 Equivalent to public_bytes(Raw, Raw). 

39 """ 

40 return self.public_bytes( 

41 _serialization.Encoding.Raw, _serialization.PublicFormat.Raw 

42 ) 

43 

44 

45class X448PrivateKey(metaclass=abc.ABCMeta): 

46 @classmethod 

47 def generate(cls) -> "X448PrivateKey": 

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

49 

50 if not backend.x448_supported(): 

51 raise UnsupportedAlgorithm( 

52 "X448 is not supported by this version of OpenSSL.", 

53 _Reasons.UNSUPPORTED_EXCHANGE_ALGORITHM, 

54 ) 

55 return backend.x448_generate_key() 

56 

57 @classmethod 

58 def from_private_bytes(cls, data: bytes) -> "X448PrivateKey": 

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

60 

61 if not backend.x448_supported(): 

62 raise UnsupportedAlgorithm( 

63 "X448 is not supported by this version of OpenSSL.", 

64 _Reasons.UNSUPPORTED_EXCHANGE_ALGORITHM, 

65 ) 

66 

67 return backend.x448_load_private_bytes(data) 

68 

69 @abc.abstractmethod 

70 def public_key(self) -> X448PublicKey: 

71 """ 

72 Returns the public key associated with this private key 

73 """ 

74 

75 @abc.abstractmethod 

76 def private_bytes( 

77 self, 

78 encoding: _serialization.Encoding, 

79 format: _serialization.PrivateFormat, 

80 encryption_algorithm: _serialization.KeySerializationEncryption, 

81 ) -> bytes: 

82 """ 

83 The serialized bytes of the private key. 

84 """ 

85 

86 def private_bytes_raw(self) -> bytes: 

87 """ 

88 The raw bytes of the private key. 

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

90 """ 

91 return self.private_bytes( 

92 _serialization.Encoding.Raw, 

93 _serialization.PrivateFormat.Raw, 

94 _serialization.NoEncryption(), 

95 ) 

96 

97 @abc.abstractmethod 

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

99 """ 

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

101 """