Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/cryptography/hazmat/primitives/asymmetric/x448.py: 72%
43 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:50 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:50 +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.
5from __future__ import annotations
7import abc
9from cryptography.exceptions import UnsupportedAlgorithm, _Reasons
10from cryptography.hazmat.bindings._rust import openssl as rust_openssl
11from cryptography.hazmat.primitives import _serialization
14class X448PublicKey(metaclass=abc.ABCMeta):
15 @classmethod
16 def from_public_bytes(cls, data: bytes) -> X448PublicKey:
17 from cryptography.hazmat.backends.openssl.backend import backend
19 if not backend.x448_supported():
20 raise UnsupportedAlgorithm(
21 "X448 is not supported by this version of OpenSSL.",
22 _Reasons.UNSUPPORTED_EXCHANGE_ALGORITHM,
23 )
25 return backend.x448_load_public_bytes(data)
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 """
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 """
44 @abc.abstractmethod
45 def __eq__(self, other: object) -> bool:
46 """
47 Checks equality.
48 """
51if hasattr(rust_openssl, "x448"):
52 X448PublicKey.register(rust_openssl.x448.X448PublicKey)
55class X448PrivateKey(metaclass=abc.ABCMeta):
56 @classmethod
57 def generate(cls) -> X448PrivateKey:
58 from cryptography.hazmat.backends.openssl.backend import backend
60 if not backend.x448_supported():
61 raise UnsupportedAlgorithm(
62 "X448 is not supported by this version of OpenSSL.",
63 _Reasons.UNSUPPORTED_EXCHANGE_ALGORITHM,
64 )
65 return backend.x448_generate_key()
67 @classmethod
68 def from_private_bytes(cls, data: bytes) -> X448PrivateKey:
69 from cryptography.hazmat.backends.openssl.backend import backend
71 if not backend.x448_supported():
72 raise UnsupportedAlgorithm(
73 "X448 is not supported by this version of OpenSSL.",
74 _Reasons.UNSUPPORTED_EXCHANGE_ALGORITHM,
75 )
77 return backend.x448_load_private_bytes(data)
79 @abc.abstractmethod
80 def public_key(self) -> X448PublicKey:
81 """
82 Returns the public key associated with this private key
83 """
85 @abc.abstractmethod
86 def private_bytes(
87 self,
88 encoding: _serialization.Encoding,
89 format: _serialization.PrivateFormat,
90 encryption_algorithm: _serialization.KeySerializationEncryption,
91 ) -> bytes:
92 """
93 The serialized bytes of the private key.
94 """
96 @abc.abstractmethod
97 def private_bytes_raw(self) -> bytes:
98 """
99 The raw bytes of the private key.
100 Equivalent to private_bytes(Raw, Raw, NoEncryption()).
101 """
103 @abc.abstractmethod
104 def exchange(self, peer_public_key: X448PublicKey) -> bytes:
105 """
106 Performs a key exchange operation using the provided peer's public key.
107 """
110if hasattr(rust_openssl, "x448"):
111 X448PrivateKey.register(rust_openssl.x448.X448PrivateKey)