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.hazmat.bindings._rust import openssl as rust_openssl
10from cryptography.hazmat.primitives import _serialization
11from cryptography.utils import Buffer
12
13
14class Ed25519PublicKey(metaclass=abc.ABCMeta):
15 @classmethod
16 def from_public_bytes(cls, data: bytes) -> Ed25519PublicKey:
17 return rust_openssl.ed25519.from_public_bytes(data)
18
19 @abc.abstractmethod
20 def public_bytes(
21 self,
22 encoding: _serialization.Encoding,
23 format: _serialization.PublicFormat,
24 ) -> bytes:
25 """
26 The serialized bytes of the public key.
27 """
28
29 @abc.abstractmethod
30 def public_bytes_raw(self) -> bytes:
31 """
32 The raw bytes of the public key.
33 Equivalent to public_bytes(Raw, Raw).
34 """
35
36 @abc.abstractmethod
37 def verify(self, signature: Buffer, data: Buffer) -> None:
38 """
39 Verify the signature.
40 """
41
42 @abc.abstractmethod
43 def __eq__(self, other: object) -> bool:
44 """
45 Checks equality.
46 """
47
48 @abc.abstractmethod
49 def __copy__(self) -> Ed25519PublicKey:
50 """
51 Returns a copy.
52 """
53
54 @abc.abstractmethod
55 def __deepcopy__(self, memo: dict) -> Ed25519PublicKey:
56 """
57 Returns a deep copy.
58 """
59
60
61Ed25519PublicKey.register(rust_openssl.ed25519.Ed25519PublicKey)
62
63
64class Ed25519PrivateKey(metaclass=abc.ABCMeta):
65 @classmethod
66 def generate(cls) -> Ed25519PrivateKey:
67 return rust_openssl.ed25519.generate_key()
68
69 @classmethod
70 def from_private_bytes(cls, data: Buffer) -> Ed25519PrivateKey:
71 return rust_openssl.ed25519.from_private_bytes(data)
72
73 @abc.abstractmethod
74 def public_key(self) -> Ed25519PublicKey:
75 """
76 The Ed25519PublicKey derived from the private key.
77 """
78
79 @abc.abstractmethod
80 def private_bytes(
81 self,
82 encoding: _serialization.Encoding,
83 format: _serialization.PrivateFormat,
84 encryption_algorithm: _serialization.KeySerializationEncryption,
85 ) -> bytes:
86 """
87 The serialized bytes of the private key.
88 """
89
90 @abc.abstractmethod
91 def private_bytes_raw(self) -> bytes:
92 """
93 The raw bytes of the private key.
94 Equivalent to private_bytes(Raw, Raw, NoEncryption()).
95 """
96
97 @abc.abstractmethod
98 def sign(self, data: Buffer) -> bytes:
99 """
100 Signs the data.
101 """
102
103 @abc.abstractmethod
104 def __copy__(self) -> Ed25519PrivateKey:
105 """
106 Returns a copy.
107 """
108
109 @abc.abstractmethod
110 def __deepcopy__(self, memo: dict) -> Ed25519PrivateKey:
111 """
112 Returns a deep copy.
113 """
114
115
116Ed25519PrivateKey.register(rust_openssl.ed25519.Ed25519PrivateKey)