From 9145a2add77625790526f4ecc70b03fb06587722 Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Wed, 10 Jul 2024 11:52:16 -0500 Subject: [PATCH] migrate poly1305, x448/25519, ed448/25519 to declarative --- src/rust/src/backend/ed25519.rs | 19 ++++++------------- src/rust/src/backend/ed448.rs | 19 ++++++------------- src/rust/src/backend/mod.rs | 10 ---------- src/rust/src/backend/poly1305.rs | 14 +++++--------- src/rust/src/backend/x25519.rs | 19 ++++++------------- src/rust/src/backend/x448.rs | 19 ++++++------------- src/rust/src/lib.rs | 12 ++++++++++++ 7 files changed, 41 insertions(+), 71 deletions(-) diff --git a/src/rust/src/backend/ed25519.rs b/src/rust/src/backend/ed25519.rs index ab800d637af8..3460640a1a53 100644 --- a/src/rust/src/backend/ed25519.rs +++ b/src/rust/src/backend/ed25519.rs @@ -6,7 +6,6 @@ use crate::backend::utils; use crate::buf::CffiBuf; use crate::error::{CryptographyError, CryptographyResult}; use crate::exceptions; -use pyo3::types::PyModuleMethods; #[pyo3::pyclass(frozen, module = "cryptography.hazmat.bindings._rust.openssl.ed25519")] pub(crate) struct Ed25519PrivateKey { @@ -160,16 +159,10 @@ impl Ed25519PublicKey { } } -pub(crate) fn create_module( - py: pyo3::Python<'_>, -) -> pyo3::PyResult> { - let m = pyo3::types::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::()?; - - Ok(m) +#[pyo3::pymodule] +pub(crate) mod ed25519 { + #[pymodule_export] + use super::{ + from_private_bytes, from_public_bytes, generate_key, Ed25519PrivateKey, Ed25519PublicKey, + }; } diff --git a/src/rust/src/backend/ed448.rs b/src/rust/src/backend/ed448.rs index 27b716ee5f2e..d27f6b361df3 100644 --- a/src/rust/src/backend/ed448.rs +++ b/src/rust/src/backend/ed448.rs @@ -6,7 +6,6 @@ use crate::backend::utils; use crate::buf::CffiBuf; use crate::error::{CryptographyError, CryptographyResult}; use crate::exceptions; -use pyo3::types::PyModuleMethods; #[pyo3::pyclass(frozen, module = "cryptography.hazmat.bindings._rust.openssl.ed448")] pub(crate) struct Ed448PrivateKey { @@ -157,16 +156,10 @@ impl Ed448PublicKey { } } -pub(crate) fn create_module( - py: pyo3::Python<'_>, -) -> pyo3::PyResult> { - let m = pyo3::types::PyModule::new_bound(py, "ed448")?; - 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::()?; - - Ok(m) +#[pyo3::pymodule] +pub(crate) mod ed448 { + #[pymodule_export] + use super::{ + from_private_bytes, from_public_bytes, generate_key, Ed448PrivateKey, Ed448PublicKey, + }; } diff --git a/src/rust/src/backend/mod.rs b/src/rust/src/backend/mod.rs index ea1e5f07f72b..4ee5f6bee124 100644 --- a/src/rust/src/backend/mod.rs +++ b/src/rust/src/backend/mod.rs @@ -33,16 +33,6 @@ pub(crate) fn add_to_module(module: &pyo3::Bound<'_, pyo3::types::PyModule>) -> module.add_submodule(&dsa::create_module(module.py())?)?; module.add_submodule(&ec::create_module(module.py())?)?; - module.add_submodule(&ed25519::create_module(module.py())?)?; - #[cfg(all(not(CRYPTOGRAPHY_IS_LIBRESSL), not(CRYPTOGRAPHY_IS_BORINGSSL)))] - module.add_submodule(&ed448::create_module(module.py())?)?; - - 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(&poly1305::create_module(module.py())?)?; - module.add_submodule(&rsa::create_module(module.py())?)?; Ok(()) diff --git a/src/rust/src/backend/poly1305.rs b/src/rust/src/backend/poly1305.rs index 4d07985407af..e998a43aaff6 100644 --- a/src/rust/src/backend/poly1305.rs +++ b/src/rust/src/backend/poly1305.rs @@ -6,7 +6,7 @@ use crate::backend::hashes::already_finalized_error; use crate::buf::CffiBuf; use crate::error::{CryptographyError, CryptographyResult}; use crate::exceptions; -use pyo3::types::{PyBytesMethods, PyModuleMethods}; +use pyo3::types::PyBytesMethods; #[cfg(any(CRYPTOGRAPHY_IS_BORINGSSL, CRYPTOGRAPHY_IS_LIBRESSL))] struct Poly1305Boring { @@ -165,12 +165,8 @@ impl Poly1305 { } } -pub(crate) fn create_module( - py: pyo3::Python<'_>, -) -> pyo3::PyResult> { - let m = pyo3::types::PyModule::new_bound(py, "poly1305")?; - - m.add_class::()?; - - Ok(m) +#[pyo3::pymodule] +pub(crate) mod poly1305 { + #[pymodule_export] + use super::Poly1305; } diff --git a/src/rust/src/backend/x25519.rs b/src/rust/src/backend/x25519.rs index 9e22c0ab998d..84f355f49787 100644 --- a/src/rust/src/backend/x25519.rs +++ b/src/rust/src/backend/x25519.rs @@ -5,7 +5,6 @@ use crate::backend::utils; use crate::buf::CffiBuf; use crate::error::CryptographyResult; -use pyo3::types::PyModuleMethods; #[pyo3::pyclass(frozen, module = "cryptography.hazmat.bindings._rust.openssl.x25519")] pub(crate) struct X25519PrivateKey { @@ -150,16 +149,10 @@ impl X25519PublicKey { } } -pub(crate) fn create_module( - py: pyo3::Python<'_>, -) -> pyo3::PyResult> { - let m = pyo3::types::PyModule::new_bound(py, "x25519")?; - 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::()?; - - Ok(m) +#[pyo3::pymodule] +pub(crate) mod x25519 { + #[pymodule_export] + use super::{ + from_private_bytes, from_public_bytes, generate_key, X25519PrivateKey, X25519PublicKey, + }; } diff --git a/src/rust/src/backend/x448.rs b/src/rust/src/backend/x448.rs index 4b88035d3226..0e9aa1c99194 100644 --- a/src/rust/src/backend/x448.rs +++ b/src/rust/src/backend/x448.rs @@ -5,7 +5,6 @@ use crate::backend::utils; use crate::buf::CffiBuf; use crate::error::CryptographyResult; -use pyo3::types::PyModuleMethods; #[pyo3::pyclass(frozen, module = "cryptography.hazmat.bindings._rust.openssl.x448")] pub(crate) struct X448PrivateKey { @@ -149,16 +148,10 @@ impl X448PublicKey { } } -pub(crate) fn create_module( - py: pyo3::Python<'_>, -) -> pyo3::PyResult> { - let m = pyo3::types::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::()?; - - Ok(m) +#[pyo3::pymodule] +pub(crate) mod x448 { + #[pymodule_export] + use super::{ + from_private_bytes, from_public_bytes, generate_key, X448PrivateKey, X448PublicKey, + }; } diff --git a/src/rust/src/lib.rs b/src/rust/src/lib.rs index e40331887ef2..738c27f79c58 100644 --- a/src/rust/src/lib.rs +++ b/src/rust/src/lib.rs @@ -148,6 +148,11 @@ mod _rust { #[pymodule_export] use super::super::{is_fips_enabled, openssl_version, openssl_version_text}; #[pymodule_export] + use crate::backend::ed25519::ed25519; + #[cfg(all(not(CRYPTOGRAPHY_IS_LIBRESSL), not(CRYPTOGRAPHY_IS_BORINGSSL)))] + #[pymodule_export] + use crate::backend::ed448::ed448; + #[pymodule_export] use crate::backend::hashes::hashes; #[pymodule_export] use crate::backend::hmac::hmac; @@ -156,6 +161,13 @@ mod _rust { #[pymodule_export] use crate::backend::keys::keys; #[pymodule_export] + use crate::backend::poly1305::poly1305; + #[pymodule_export] + use crate::backend::x25519::x25519; + #[cfg(all(not(CRYPTOGRAPHY_IS_LIBRESSL), not(CRYPTOGRAPHY_IS_BORINGSSL)))] + #[pymodule_export] + use crate::backend::x448::x448; + #[pymodule_export] use crate::error::{capture_error_stack, raise_openssl_error, OpenSSLError}; #[pymodule_init]