From 584e387a72042c456526d3b330290a7869dacec0 Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Thu, 4 Apr 2024 22:13:36 -0400 Subject: [PATCH] Convert `src/backend/x448.rs` to new pyo3 APIs --- src/rust/src/backend/mod.rs | 2 +- src/rust/src/backend/x448.rs | 41 +++++++++++++++++++++--------------- 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/src/rust/src/backend/mod.rs b/src/rust/src/backend/mod.rs index 666c15b47d48..25142bab2622 100644 --- a/src/rust/src/backend/mod.rs +++ b/src/rust/src/backend/mod.rs @@ -38,7 +38,7 @@ pub(crate) fn add_to_module(module: &pyo3::prelude::PyModule) -> pyo3::PyResult< module.add_submodule(x25519::create_module(module.py())?)?; #[cfg(all(not(CRYPTOGRAPHY_IS_LIBRESSL), not(CRYPTOGRAPHY_IS_BORINGSSL)))] - module.add_submodule(x448::create_module(module.py())?)?; + module.add_submodule(x448::create_module(module.py())?.into_gil_ref())?; module.add_submodule(poly1305::create_module(module.py())?)?; diff --git a/src/rust/src/backend/x448.rs b/src/rust/src/backend/x448.rs index 517fc48c0493..1d8d9e5837cc 100644 --- a/src/rust/src/backend/x448.rs +++ b/src/rust/src/backend/x448.rs @@ -5,6 +5,7 @@ use crate::backend::utils; use crate::buf::CffiBuf; use crate::error::CryptographyResult; +use pyo3::prelude::PyModuleMethods; #[pyo3::prelude::pyclass(frozen, module = "cryptography.hazmat.bindings._rust.openssl.x448")] pub(crate) struct X448PrivateKey { @@ -65,17 +66,21 @@ impl X448PrivateKey { &self, py: pyo3::Python<'p>, peer_public_key: &X448PublicKey, - ) -> CryptographyResult<&'p pyo3::types::PyBytes> { + ) -> CryptographyResult> { 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(()) - })?) + Ok(pyo3::types::PyBytes::new_bound_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 { @@ -91,9 +96,9 @@ impl X448PrivateKey { fn private_bytes_raw<'p>( &self, py: pyo3::Python<'p>, - ) -> CryptographyResult<&'p pyo3::types::PyBytes> { + ) -> CryptographyResult> { let raw_bytes = self.pkey.raw_private_key()?; - Ok(pyo3::types::PyBytes::new(py, &raw_bytes)) + Ok(pyo3::types::PyBytes::new_bound(py, &raw_bytes)) } fn private_bytes<'p>( @@ -121,9 +126,9 @@ impl X448PublicKey { fn public_bytes_raw<'p>( &self, py: pyo3::Python<'p>, - ) -> CryptographyResult<&'p pyo3::types::PyBytes> { + ) -> CryptographyResult> { let raw_bytes = self.pkey.raw_public_key()?; - Ok(pyo3::types::PyBytes::new(py, &raw_bytes)) + Ok(pyo3::types::PyBytes::new_bound(py, &raw_bytes)) } fn public_bytes<'p>( @@ -144,11 +149,13 @@ impl X448PublicKey { } } -pub(crate) fn create_module(py: pyo3::Python<'_>) -> pyo3::PyResult<&pyo3::prelude::PyModule> { - let m = pyo3::prelude::PyModule::new(py, "x448")?; - 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)?)?; +pub(crate) fn create_module( + py: pyo3::Python<'_>, +) -> pyo3::PyResult> { + let m = pyo3::prelude::PyModule::new_bound(py, "x448")?; + m.add_function(pyo3::wrap_pyfunction_bound!(generate_key, &m)?)?; + m.add_function(pyo3::wrap_pyfunction_bound!(from_private_bytes, &m)?)?; + m.add_function(pyo3::wrap_pyfunction_bound!(from_public_bytes, &m)?)?; m.add_class::()?; m.add_class::()?;