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

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

43 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 

12 

13 

14class Ed25519PublicKey(metaclass=abc.ABCMeta): 

15 @classmethod 

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

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

18 

19 if not backend.ed25519_supported(): 

20 raise UnsupportedAlgorithm( 

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

22 _Reasons.UNSUPPORTED_PUBLIC_KEY_ALGORITHM, 

23 ) 

24 

25 return rust_openssl.ed25519.from_public_bytes(data) 

26 

27 @abc.abstractmethod 

28 def public_bytes( 

29 self, 

30 encoding: _serialization.Encoding, 

31 format: _serialization.PublicFormat, 

32 ) -> bytes: 

33 """ 

34 The serialized bytes of the public key. 

35 """ 

36 

37 @abc.abstractmethod 

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 

44 @abc.abstractmethod 

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

46 """ 

47 Verify the signature. 

48 """ 

49 

50 @abc.abstractmethod 

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

52 """ 

53 Checks equality. 

54 """ 

55 

56 

57Ed25519PublicKey.register(rust_openssl.ed25519.Ed25519PublicKey) 

58 

59 

60class Ed25519PrivateKey(metaclass=abc.ABCMeta): 

61 @classmethod 

62 def generate(cls) -> Ed25519PrivateKey: 

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

64 

65 if not backend.ed25519_supported(): 

66 raise UnsupportedAlgorithm( 

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

68 _Reasons.UNSUPPORTED_PUBLIC_KEY_ALGORITHM, 

69 ) 

70 

71 return rust_openssl.ed25519.generate_key() 

72 

73 @classmethod 

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

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

76 

77 if not backend.ed25519_supported(): 

78 raise UnsupportedAlgorithm( 

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

80 _Reasons.UNSUPPORTED_PUBLIC_KEY_ALGORITHM, 

81 ) 

82 

83 return rust_openssl.ed25519.from_private_bytes(data) 

84 

85 @abc.abstractmethod 

86 def public_key(self) -> Ed25519PublicKey: 

87 """ 

88 The Ed25519PublicKey derived from the private key. 

89 """ 

90 

91 @abc.abstractmethod 

92 def private_bytes( 

93 self, 

94 encoding: _serialization.Encoding, 

95 format: _serialization.PrivateFormat, 

96 encryption_algorithm: _serialization.KeySerializationEncryption, 

97 ) -> bytes: 

98 """ 

99 The serialized bytes of the private key. 

100 """ 

101 

102 @abc.abstractmethod 

103 def private_bytes_raw(self) -> bytes: 

104 """ 

105 The raw bytes of the private key. 

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

107 """ 

108 

109 @abc.abstractmethod 

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

111 """ 

112 Signs the data. 

113 """ 

114 

115 

116Ed25519PrivateKey.register(rust_openssl.ed25519.Ed25519PrivateKey)