Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/cryptography/hazmat/primitives/asymmetric/ed25519.py: 73%
45 statements
« prev ^ index » next coverage.py v7.3.2, created at 2023-12-08 06:05 +0000
« prev ^ index » next coverage.py v7.3.2, created at 2023-12-08 06:05 +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 Ed25519PublicKey(metaclass=abc.ABCMeta):
15 @classmethod
16 def from_public_bytes(cls, data: bytes) -> Ed25519PublicKey:
17 from cryptography.hazmat.backends.openssl.backend import backend
19 if not backend.ed25519_supported():
20 raise UnsupportedAlgorithm(
21 "ed25519 is not supported by this version of OpenSSL.",
22 _Reasons.UNSUPPORTED_PUBLIC_KEY_ALGORITHM,
23 )
25 return backend.ed25519_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 verify(self, signature: bytes, data: bytes) -> None:
46 """
47 Verify the signature.
48 """
50 @abc.abstractmethod
51 def __eq__(self, other: object) -> bool:
52 """
53 Checks equality.
54 """
57if hasattr(rust_openssl, "ed25519"):
58 Ed25519PublicKey.register(rust_openssl.ed25519.Ed25519PublicKey)
61class Ed25519PrivateKey(metaclass=abc.ABCMeta):
62 @classmethod
63 def generate(cls) -> Ed25519PrivateKey:
64 from cryptography.hazmat.backends.openssl.backend import backend
66 if not backend.ed25519_supported():
67 raise UnsupportedAlgorithm(
68 "ed25519 is not supported by this version of OpenSSL.",
69 _Reasons.UNSUPPORTED_PUBLIC_KEY_ALGORITHM,
70 )
72 return backend.ed25519_generate_key()
74 @classmethod
75 def from_private_bytes(cls, data: bytes) -> Ed25519PrivateKey:
76 from cryptography.hazmat.backends.openssl.backend import backend
78 if not backend.ed25519_supported():
79 raise UnsupportedAlgorithm(
80 "ed25519 is not supported by this version of OpenSSL.",
81 _Reasons.UNSUPPORTED_PUBLIC_KEY_ALGORITHM,
82 )
84 return backend.ed25519_load_private_bytes(data)
86 @abc.abstractmethod
87 def public_key(self) -> Ed25519PublicKey:
88 """
89 The Ed25519PublicKey derived from the private key.
90 """
92 @abc.abstractmethod
93 def private_bytes(
94 self,
95 encoding: _serialization.Encoding,
96 format: _serialization.PrivateFormat,
97 encryption_algorithm: _serialization.KeySerializationEncryption,
98 ) -> bytes:
99 """
100 The serialized bytes of the private key.
101 """
103 @abc.abstractmethod
104 def private_bytes_raw(self) -> bytes:
105 """
106 The raw bytes of the private key.
107 Equivalent to private_bytes(Raw, Raw, NoEncryption()).
108 """
110 @abc.abstractmethod
111 def sign(self, data: bytes) -> bytes:
112 """
113 Signs the data.
114 """
117if hasattr(rust_openssl, "x25519"):
118 Ed25519PrivateKey.register(rust_openssl.ed25519.Ed25519PrivateKey)