forked from pyca/cryptography
-
Notifications
You must be signed in to change notification settings - Fork 9
/
x25519.rs
158 lines (138 loc) · 4.83 KB
/
x25519.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// This file is dual licensed under the terms of the Apache License, Version
// 2.0, and the BSD License. See the LICENSE file in the root of this repository
// for complete details.
use crate::backend::utils;
use crate::buf::CffiBuf;
use crate::error::CryptographyResult;
#[pyo3::prelude::pyclass(frozen, module = "cryptography.hazmat.bindings._rust.openssl.x25519")]
pub(crate) struct X25519PrivateKey {
pkey: openssl::pkey::PKey<openssl::pkey::Private>,
}
#[pyo3::prelude::pyclass(frozen, module = "cryptography.hazmat.bindings._rust.openssl.x25519")]
pub(crate) struct X25519PublicKey {
pkey: openssl::pkey::PKey<openssl::pkey::Public>,
}
#[pyo3::prelude::pyfunction]
fn generate_key() -> CryptographyResult<X25519PrivateKey> {
Ok(X25519PrivateKey {
pkey: openssl::pkey::PKey::generate_x25519()?,
})
}
pub(crate) fn private_key_from_pkey(
pkey: &openssl::pkey::PKeyRef<openssl::pkey::Private>,
) -> X25519PrivateKey {
X25519PrivateKey {
pkey: pkey.to_owned(),
}
}
pub(crate) fn public_key_from_pkey(
pkey: &openssl::pkey::PKeyRef<openssl::pkey::Public>,
) -> X25519PublicKey {
X25519PublicKey {
pkey: pkey.to_owned(),
}
}
#[pyo3::prelude::pyfunction]
fn from_private_bytes(data: CffiBuf<'_>) -> pyo3::PyResult<X25519PrivateKey> {
let pkey =
openssl::pkey::PKey::private_key_from_raw_bytes(data.as_bytes(), openssl::pkey::Id::X25519)
.map_err(|e| {
pyo3::exceptions::PyValueError::new_err(format!(
"An X25519 private key is 32 bytes long: {e}"
))
})?;
Ok(X25519PrivateKey { pkey })
}
#[pyo3::prelude::pyfunction]
fn from_public_bytes(data: &[u8]) -> pyo3::PyResult<X25519PublicKey> {
let pkey = openssl::pkey::PKey::public_key_from_raw_bytes(data, openssl::pkey::Id::X25519)
.map_err(|_| {
pyo3::exceptions::PyValueError::new_err("An X25519 public key is 32 bytes long")
})?;
Ok(X25519PublicKey { pkey })
}
#[pyo3::prelude::pymethods]
impl X25519PrivateKey {
fn exchange<'p>(
&self,
py: pyo3::Python<'p>,
peer_public_key: &X25519PublicKey,
) -> CryptographyResult<&'p pyo3::types::PyBytes> {
let mut deriver = openssl::derive::Deriver::new(&self.pkey)?;
deriver.set_peer(&peer_public_key.pkey)?;
Ok(pyo3::types::PyBytes::new_with(py, deriver.len()?, |b| {
let n = deriver.derive(b).map_err(|_| {
pyo3::exceptions::PyValueError::new_err("Error computing shared key.")
})?;
assert_eq!(n, b.len());
Ok(())
})?)
}
fn public_key(&self) -> CryptographyResult<X25519PublicKey> {
let raw_bytes = self.pkey.raw_public_key()?;
Ok(X25519PublicKey {
pkey: openssl::pkey::PKey::public_key_from_raw_bytes(
&raw_bytes,
openssl::pkey::Id::X25519,
)?,
})
}
fn private_bytes_raw<'p>(
&self,
py: pyo3::Python<'p>,
) -> CryptographyResult<&'p pyo3::types::PyBytes> {
let raw_bytes = self.pkey.raw_private_key()?;
Ok(pyo3::types::PyBytes::new(py, &raw_bytes))
}
fn private_bytes<'p>(
slf: &pyo3::PyCell<Self>,
py: pyo3::Python<'p>,
encoding: &pyo3::PyAny,
format: &pyo3::PyAny,
encryption_algorithm: &pyo3::PyAny,
) -> CryptographyResult<&'p pyo3::types::PyBytes> {
utils::pkey_private_bytes(
py,
slf,
&slf.borrow().pkey,
encoding,
format,
encryption_algorithm,
false,
true,
)
}
}
#[pyo3::prelude::pymethods]
impl X25519PublicKey {
fn public_bytes_raw<'p>(
&self,
py: pyo3::Python<'p>,
) -> CryptographyResult<&'p pyo3::types::PyBytes> {
let raw_bytes = self.pkey.raw_public_key()?;
Ok(pyo3::types::PyBytes::new(py, &raw_bytes))
}
fn public_bytes<'p>(
slf: &pyo3::PyCell<Self>,
py: pyo3::Python<'p>,
encoding: &pyo3::PyAny,
format: &pyo3::PyAny,
) -> CryptographyResult<&'p pyo3::types::PyBytes> {
utils::pkey_public_bytes(py, slf, &slf.borrow().pkey, encoding, format, false, true)
}
fn __eq__(&self, other: pyo3::PyRef<'_, Self>) -> bool {
self.pkey.public_eq(&other.pkey)
}
fn __copy__(slf: pyo3::PyRef<'_, Self>) -> pyo3::PyRef<'_, Self> {
slf
}
}
pub(crate) fn create_module(py: pyo3::Python<'_>) -> pyo3::PyResult<&pyo3::prelude::PyModule> {
let m = pyo3::prelude::PyModule::new(py, "x25519")?;
m.add_function(pyo3::wrap_pyfunction!(generate_key, m)?)?;
m.add_function(pyo3::wrap_pyfunction!(from_private_bytes, m)?)?;
m.add_function(pyo3::wrap_pyfunction!(from_public_bytes, m)?)?;
m.add_class::<X25519PrivateKey>()?;
m.add_class::<X25519PublicKey>()?;
Ok(m)
}