codekingpro/portable-devtools
114k
1# This file is dual licensed under the terms of the Apache License, Version2# 2.0, and the BSD License. See the LICENSE file in the root of this repository3# for complete details.4 5from __future__ import annotations6 7import abc8 9from cryptography.exceptions import UnsupportedAlgorithm, _Reasons10from cryptography.hazmat.bindings._rust import openssl as rust_openssl11from cryptography.hazmat.primitives import _serialization12from cryptography.utils import Buffer13 14 15class Ed448PublicKey(metaclass=abc.ABCMeta):16 @classmethod17 def from_public_bytes(cls, data: bytes) -> Ed448PublicKey:18 from cryptography.hazmat.backends.openssl.backend import backend19 20 if not backend.ed448_supported():21 raise UnsupportedAlgorithm(22 "ed448 is not supported by this version of OpenSSL.",23 _Reasons.UNSUPPORTED_PUBLIC_KEY_ALGORITHM,24 )25 26 return rust_openssl.ed448.from_public_bytes(data)27 28 @abc.abstractmethod29 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 @abc.abstractmethod39 def public_bytes_raw(self) -> bytes:40 """41 The raw bytes of the public key.42 Equivalent to public_bytes(Raw, Raw).43 """44 45 @abc.abstractmethod46 def verify(self, signature: Buffer, data: Buffer) -> None:47 """48 Verify the signature.49 """50 51 @abc.abstractmethod52 def __eq__(self, other: object) -> bool:53 """54 Checks equality.55 """56 57 @abc.abstractmethod58 def __copy__(self) -> Ed448PublicKey:59 """60 Returns a copy.61 """62 63 @abc.abstractmethod64 def __deepcopy__(self, memo: dict) -> Ed448PublicKey:65 """66 Returns a deep copy.67 """68 69 70if hasattr(rust_openssl, "ed448"):71 Ed448PublicKey.register(rust_openssl.ed448.Ed448PublicKey)72 73 74class Ed448PrivateKey(metaclass=abc.ABCMeta):75 @classmethod76 def generate(cls) -> Ed448PrivateKey:77 from cryptography.hazmat.backends.openssl.backend import backend78 79 if not backend.ed448_supported():80 raise UnsupportedAlgorithm(81 "ed448 is not supported by this version of OpenSSL.",82 _Reasons.UNSUPPORTED_PUBLIC_KEY_ALGORITHM,83 )84 85 return rust_openssl.ed448.generate_key()86 87 @classmethod88 def from_private_bytes(cls, data: Buffer) -> Ed448PrivateKey:89 from cryptography.hazmat.backends.openssl.backend import backend90 91 if not backend.ed448_supported():92 raise UnsupportedAlgorithm(93 "ed448 is not supported by this version of OpenSSL.",94 _Reasons.UNSUPPORTED_PUBLIC_KEY_ALGORITHM,95 )96 97 return rust_openssl.ed448.from_private_bytes(data)98 99 @abc.abstractmethod100 def public_key(self) -> Ed448PublicKey:101 """102 The Ed448PublicKey derived from the private key.103 """104 105 @abc.abstractmethod106 def sign(self, data: Buffer) -> bytes:107 """108 Signs the data.109 """110 111 @abc.abstractmethod112 def private_bytes(113 self,114 encoding: _serialization.Encoding,115 format: _serialization.PrivateFormat,116 encryption_algorithm: _serialization.KeySerializationEncryption,117 ) -> bytes:118 """119 The serialized bytes of the private key.120 """121 122 @abc.abstractmethod123 def private_bytes_raw(self) -> bytes:124 """125 The raw bytes of the private key.126 Equivalent to private_bytes(Raw, Raw, NoEncryption()).127 """128 129 @abc.abstractmethod130 def __copy__(self) -> Ed448PrivateKey:131 """132 Returns a copy.133 """134 135 @abc.abstractmethod136 def __deepcopy__(self, memo: dict) -> Ed448PrivateKey:137 """138 Returns a deep copy.139 """140 141 142if hasattr(rust_openssl, "x448"):143 Ed448PrivateKey.register(rust_openssl.ed448.Ed448PrivateKey)144 