From c56ff9679c68c3dae1f683d098dce88ce9858474 Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Thu, 4 Apr 2024 22:32:56 -0400 Subject: [PATCH] Convert `src/backend/ed25519.rs` to new pyo3 APIs (#10722) --- src/rust/src/backend/ed25519.rs | 26 +++++++++++++++----------- src/rust/src/backend/mod.rs | 2 +- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/rust/src/backend/ed25519.rs b/src/rust/src/backend/ed25519.rs index 383fa3a5f..565f839f7 100644 --- a/src/rust/src/backend/ed25519.rs +++ b/src/rust/src/backend/ed25519.rs @@ -6,6 +6,7 @@ use crate::backend::utils; use crate::buf::CffiBuf; use crate::error::{CryptographyError, CryptographyResult}; use crate::exceptions; +use pyo3::prelude::PyModuleMethods; #[pyo3::prelude::pyclass(frozen, module = "cryptography.hazmat.bindings._rust.openssl.ed25519")] pub(crate) struct Ed25519PrivateKey { @@ -67,9 +68,10 @@ impl Ed25519PrivateKey { &self, py: pyo3::Python<'p>, data: CffiBuf<'_>, - ) -> CryptographyResult<&'p pyo3::types::PyBytes> { + ) -> CryptographyResult> { let mut signer = openssl::sign::Signer::new_without_digest(&self.pkey)?; - Ok(pyo3::types::PyBytes::new_with(py, signer.len()?, |b| { + let len = signer.len()?; + Ok(pyo3::types::PyBytes::new_bound_with(py, len, |b| { let n = signer .sign_oneshot(b, data.as_bytes()) .map_err(CryptographyError::from)?; @@ -91,9 +93,9 @@ impl Ed25519PrivateKey { 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>( @@ -135,9 +137,9 @@ impl Ed25519PublicKey { 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>( @@ -158,11 +160,13 @@ impl Ed25519PublicKey { } } -pub(crate) fn create_module(py: pyo3::Python<'_>) -> pyo3::PyResult<&pyo3::prelude::PyModule> { - let m = pyo3::prelude::PyModule::new(py, "ed25519")?; - 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, "ed25519")?; + 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::()?; diff --git a/src/rust/src/backend/mod.rs b/src/rust/src/backend/mod.rs index 25142bab2..050963f9c 100644 --- a/src/rust/src/backend/mod.rs +++ b/src/rust/src/backend/mod.rs @@ -32,7 +32,7 @@ pub(crate) fn add_to_module(module: &pyo3::prelude::PyModule) -> pyo3::PyResult< module.add_submodule(ec::create_module(module.py())?.into_gil_ref())?; module.add_submodule(keys::create_module(module.py())?.into_gil_ref())?; - module.add_submodule(ed25519::create_module(module.py())?)?; + module.add_submodule(ed25519::create_module(module.py())?.into_gil_ref())?; #[cfg(all(not(CRYPTOGRAPHY_IS_LIBRESSL), not(CRYPTOGRAPHY_IS_BORINGSSL)))] module.add_submodule(ed448::create_module(module.py())?)?;