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

35 statements  

« prev     ^ index     » next       coverage.py v7.0.1, created at 2022-12-25 06:11 +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 

12_ED25519_KEY_SIZE = 32 

13_ED25519_SIG_SIZE = 64 

14 

15 

16class Ed25519PublicKey(metaclass=abc.ABCMeta): 

17 @classmethod 

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

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

20 

21 if not backend.ed25519_supported(): 

22 raise UnsupportedAlgorithm( 

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

24 _Reasons.UNSUPPORTED_PUBLIC_KEY_ALGORITHM, 

25 ) 

26 

27 return backend.ed25519_load_public_bytes(data) 

28 

29 @abc.abstractmethod 

30 def public_bytes( 

31 self, 

32 encoding: _serialization.Encoding, 

33 format: _serialization.PublicFormat, 

34 ) -> bytes: 

35 """ 

36 The serialized bytes of the public key. 

37 """ 

38 

39 @abc.abstractmethod 

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

41 """ 

42 Verify the signature. 

43 """ 

44 

45 

46class Ed25519PrivateKey(metaclass=abc.ABCMeta): 

47 @classmethod 

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

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

50 

51 if not backend.ed25519_supported(): 

52 raise UnsupportedAlgorithm( 

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

54 _Reasons.UNSUPPORTED_PUBLIC_KEY_ALGORITHM, 

55 ) 

56 

57 return backend.ed25519_generate_key() 

58 

59 @classmethod 

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

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

62 

63 if not backend.ed25519_supported(): 

64 raise UnsupportedAlgorithm( 

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

66 _Reasons.UNSUPPORTED_PUBLIC_KEY_ALGORITHM, 

67 ) 

68 

69 return backend.ed25519_load_private_bytes(data) 

70 

71 @abc.abstractmethod 

72 def public_key(self) -> Ed25519PublicKey: 

73 """ 

74 The Ed25519PublicKey derived from the private key. 

75 """ 

76 

77 @abc.abstractmethod 

78 def private_bytes( 

79 self, 

80 encoding: _serialization.Encoding, 

81 format: _serialization.PrivateFormat, 

82 encryption_algorithm: _serialization.KeySerializationEncryption, 

83 ) -> bytes: 

84 """ 

85 The serialized bytes of the private key. 

86 """ 

87 

88 @abc.abstractmethod 

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

90 """ 

91 Signs the data. 

92 """