Convert src/backend/ed25519.rs to new pyo3 APIs (#10722)

This commit is contained in:
Alex Gaynor 2024-04-04 22:32:56 -04:00 committed by GitHub
parent 0ac63a4bb9
commit c56ff9679c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 16 additions and 12 deletions

View file

@ -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<pyo3::Bound<'p, pyo3::types::PyBytes>> {
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<pyo3::Bound<'p, pyo3::types::PyBytes>> {
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<pyo3::Bound<'p, pyo3::types::PyBytes>> {
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<pyo3::Bound<'_, pyo3::prelude::PyModule>> {
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::<Ed25519PrivateKey>()?;
m.add_class::<Ed25519PublicKey>()?;

View file

@ -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())?)?;