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 absolute_import, division, print_function 6 7import abc 8 9import six 10 11from cryptography.exceptions import UnsupportedAlgorithm, _Reasons 12 13 14_ED25519_KEY_SIZE = 32 15_ED25519_SIG_SIZE = 64 16 17 18@six.add_metaclass(abc.ABCMeta) 19class Ed25519PublicKey(object): 20 @classmethod 21 def from_public_bytes(cls, data): 22 from cryptography.hazmat.backends.openssl.backend import backend 23 24 if not backend.ed25519_supported(): 25 raise UnsupportedAlgorithm( 26 "ed25519 is not supported by this version of OpenSSL.", 27 _Reasons.UNSUPPORTED_PUBLIC_KEY_ALGORITHM, 28 ) 29 30 return backend.ed25519_load_public_bytes(data) 31 32 @abc.abstractmethod 33 def public_bytes(self, encoding, format): 34 """ 35 The serialized bytes of the public key. 36 """ 37 38 @abc.abstractmethod 39 def verify(self, signature, data): 40 """ 41 Verify the signature. 42 """ 43 44 45@six.add_metaclass(abc.ABCMeta) 46class Ed25519PrivateKey(object): 47 @classmethod 48 def generate(cls): 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): 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): 73 """ 74 The Ed25519PublicKey derived from the private key. 75 """ 76 77 @abc.abstractmethod 78 def private_bytes(self, encoding, format, encryption_algorithm): 79 """ 80 The serialized bytes of the private key. 81 """ 82 83 @abc.abstractmethod 84 def sign(self, data): 85 """ 86 Signs the data. 87 """ 88