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

39 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_ED25519_KEY_SIZE = 32 

12_ED25519_SIG_SIZE = 64 

13 

14 

15class Ed25519PublicKey(metaclass=abc.ABCMeta): 

16 @classmethod 

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

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

19 

20 if not backend.ed25519_supported(): 

21 raise UnsupportedAlgorithm( 

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

23 _Reasons.UNSUPPORTED_PUBLIC_KEY_ALGORITHM, 

24 ) 

25 

26 return backend.ed25519_load_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 def public_bytes_raw(self) -> bytes: 

39 """ 

40 The raw bytes of the public key. 

41 Equivalent to public_bytes(Raw, Raw). 

42 """ 

43 return self.public_bytes( 

44 _serialization.Encoding.Raw, _serialization.PublicFormat.Raw 

45 ) 

46 

47 @abc.abstractmethod 

48 def verify(self, signature: bytes, data: bytes) -> None: 

49 """ 

50 Verify the signature. 

51 """ 

52 

53 

54class Ed25519PrivateKey(metaclass=abc.ABCMeta): 

55 @classmethod 

56 def generate(cls) -> "Ed25519PrivateKey": 

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

58 

59 if not backend.ed25519_supported(): 

60 raise UnsupportedAlgorithm( 

61 "ed25519 is not supported by this version of OpenSSL.", 

62 _Reasons.UNSUPPORTED_PUBLIC_KEY_ALGORITHM, 

63 ) 

64 

65 return backend.ed25519_generate_key() 

66 

67 @classmethod 

68 def from_private_bytes(cls, data: bytes) -> "Ed25519PrivateKey": 

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

70 

71 if not backend.ed25519_supported(): 

72 raise UnsupportedAlgorithm( 

73 "ed25519 is not supported by this version of OpenSSL.", 

74 _Reasons.UNSUPPORTED_PUBLIC_KEY_ALGORITHM, 

75 ) 

76 

77 return backend.ed25519_load_private_bytes(data) 

78 

79 @abc.abstractmethod 

80 def public_key(self) -> Ed25519PublicKey: 

81 """ 

82 The Ed25519PublicKey derived from the private key. 

83 """ 

84 

85 @abc.abstractmethod 

86 def private_bytes( 

87 self, 

88 encoding: _serialization.Encoding, 

89 format: _serialization.PrivateFormat, 

90 encryption_algorithm: _serialization.KeySerializationEncryption, 

91 ) -> bytes: 

92 """ 

93 The serialized bytes of the private key. 

94 """ 

95 

96 def private_bytes_raw(self) -> bytes: 

97 """ 

98 The raw bytes of the private key. 

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

100 """ 

101 return self.private_bytes( 

102 _serialization.Encoding.Raw, 

103 _serialization.PrivateFormat.Raw, 

104 _serialization.NoEncryption(), 

105 ) 

106 

107 @abc.abstractmethod 

108 def sign(self, data: bytes) -> bytes: 

109 """ 

110 Signs the data. 

111 """