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 
    12from cryptography.utils import Buffer 
    13 
    14 
    15class X448PublicKey(metaclass=abc.ABCMeta): 
    16    @classmethod 
    17    def from_public_bytes(cls, data: bytes) -> X448PublicKey: 
    18        from cryptography.hazmat.backends.openssl.backend import backend 
    19 
    20        if not backend.x448_supported(): 
    21            raise UnsupportedAlgorithm( 
    22                "X448 is not supported by this version of OpenSSL.", 
    23                _Reasons.UNSUPPORTED_EXCHANGE_ALGORITHM, 
    24            ) 
    25 
    26        return rust_openssl.x448.from_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    @abc.abstractmethod 
    39    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.abstractmethod 
    46    def __eq__(self, other: object) -> bool: 
    47        """ 
    48        Checks equality. 
    49        """ 
    50 
    51    @abc.abstractmethod 
    52    def __copy__(self) -> X448PublicKey: 
    53        """ 
    54        Returns a copy. 
    55        """ 
    56 
    57 
    58if hasattr(rust_openssl, "x448"): 
    59    X448PublicKey.register(rust_openssl.x448.X448PublicKey) 
    60 
    61 
    62class X448PrivateKey(metaclass=abc.ABCMeta): 
    63    @classmethod 
    64    def generate(cls) -> X448PrivateKey: 
    65        from cryptography.hazmat.backends.openssl.backend import backend 
    66 
    67        if not backend.x448_supported(): 
    68            raise UnsupportedAlgorithm( 
    69                "X448 is not supported by this version of OpenSSL.", 
    70                _Reasons.UNSUPPORTED_EXCHANGE_ALGORITHM, 
    71            ) 
    72 
    73        return rust_openssl.x448.generate_key() 
    74 
    75    @classmethod 
    76    def from_private_bytes(cls, data: Buffer) -> X448PrivateKey: 
    77        from cryptography.hazmat.backends.openssl.backend import backend 
    78 
    79        if not backend.x448_supported(): 
    80            raise UnsupportedAlgorithm( 
    81                "X448 is not supported by this version of OpenSSL.", 
    82                _Reasons.UNSUPPORTED_EXCHANGE_ALGORITHM, 
    83            ) 
    84 
    85        return rust_openssl.x448.from_private_bytes(data) 
    86 
    87    @abc.abstractmethod 
    88    def public_key(self) -> X448PublicKey: 
    89        """ 
    90        Returns the public key associated with this private key 
    91        """ 
    92 
    93    @abc.abstractmethod 
    94    def private_bytes( 
    95        self, 
    96        encoding: _serialization.Encoding, 
    97        format: _serialization.PrivateFormat, 
    98        encryption_algorithm: _serialization.KeySerializationEncryption, 
    99    ) -> bytes: 
    100        """ 
    101        The serialized bytes of the private key. 
    102        """ 
    103 
    104    @abc.abstractmethod 
    105    def private_bytes_raw(self) -> bytes: 
    106        """ 
    107        The raw bytes of the private key. 
    108        Equivalent to private_bytes(Raw, Raw, NoEncryption()). 
    109        """ 
    110 
    111    @abc.abstractmethod 
    112    def exchange(self, peer_public_key: X448PublicKey) -> bytes: 
    113        """ 
    114        Performs a key exchange operation using the provided peer's public key. 
    115        """ 
    116 
    117    @abc.abstractmethod 
    118    def __copy__(self) -> X448PrivateKey: 
    119        """ 
    120        Returns a copy. 
    121        """ 
    122 
    123 
    124if hasattr(rust_openssl, "x448"): 
    125    X448PrivateKey.register(rust_openssl.x448.X448PrivateKey)