• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 absolute_import, division, print_function
6
7import abc
8
9import six
10
11from cryptography.exceptions import UnsupportedAlgorithm, _Reasons
12
13
14@six.add_metaclass(abc.ABCMeta)
15class X25519PublicKey(object):
16    @classmethod
17    def from_public_bytes(cls, data):
18        from cryptography.hazmat.backends.openssl.backend import backend
19        if not backend.x25519_supported():
20            raise UnsupportedAlgorithm(
21                "X25519 is not supported by this version of OpenSSL.",
22                _Reasons.UNSUPPORTED_EXCHANGE_ALGORITHM
23            )
24
25        return backend.x25519_load_public_bytes(data)
26
27    @abc.abstractmethod
28    def public_bytes(self, encoding=None, format=None):
29        """
30        The serialized bytes of the public key.
31        """
32
33
34@six.add_metaclass(abc.ABCMeta)
35class X25519PrivateKey(object):
36    @classmethod
37    def generate(cls):
38        from cryptography.hazmat.backends.openssl.backend import backend
39        if not backend.x25519_supported():
40            raise UnsupportedAlgorithm(
41                "X25519 is not supported by this version of OpenSSL.",
42                _Reasons.UNSUPPORTED_EXCHANGE_ALGORITHM
43            )
44        return backend.x25519_generate_key()
45
46    @classmethod
47    def from_private_bytes(cls, data):
48        from cryptography.hazmat.backends.openssl.backend import backend
49        if not backend.x25519_supported():
50            raise UnsupportedAlgorithm(
51                "X25519 is not supported by this version of OpenSSL.",
52                _Reasons.UNSUPPORTED_EXCHANGE_ALGORITHM
53            )
54
55        return backend.x25519_load_private_bytes(data)
56
57    @abc.abstractmethod
58    def public_key(self):
59        """
60        The serialized bytes of the public key.
61        """
62
63    @abc.abstractmethod
64    def private_bytes(self, encoding, format, encryption_algorithm):
65        """
66        The serialized bytes of the private key.
67        """
68
69    @abc.abstractmethod
70    def exchange(self, peer_public_key):
71        """
72        Performs a key exchange operation using the provided peer's public key.
73        """
74