diff --git a/src/rust/src/backend/aead.rs b/src/rust/src/backend/aead.rs index 34be02f5efce..d67bae78b9ba 100644 --- a/src/rust/src/backend/aead.rs +++ b/src/rust/src/backend/aead.rs @@ -5,7 +5,7 @@ use crate::buf::CffiBuf; use crate::error::{CryptographyError, CryptographyResult}; use crate::{exceptions, types}; -use pyo3::types::{PyAnyMethods, PyListMethods, PyModuleMethods}; +use pyo3::types::{PyAnyMethods, PyListMethods}; fn check_length(data: &[u8]) -> CryptographyResult<()> { if data.len() > (i32::MAX as usize) { @@ -1153,17 +1153,8 @@ impl AesGcmSiv { } } -pub(crate) fn create_module( - py: pyo3::Python<'_>, -) -> pyo3::PyResult> { - let m = pyo3::types::PyModule::new_bound(py, "aead")?; - - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - - Ok(m) +#[pyo3::pymodule] +pub(crate) mod aead { + #[pymodule_export] + use super::{AesCcm, AesGcm, AesGcmSiv, AesOcb3, AesSiv, ChaCha20Poly1305}; } diff --git a/src/rust/src/backend/ciphers.rs b/src/rust/src/backend/ciphers.rs index 14ac3d13c758..b1a2c2474a0b 100644 --- a/src/rust/src/backend/ciphers.rs +++ b/src/rust/src/backend/ciphers.rs @@ -7,7 +7,7 @@ use crate::buf::{CffiBuf, CffiMutBuf}; use crate::error::{CryptographyError, CryptographyResult}; use crate::exceptions; use crate::types; -use pyo3::types::{PyAnyMethods, PyModuleMethods}; +use pyo3::types::PyAnyMethods; use pyo3::IntoPy; pub(crate) struct CipherContext { @@ -604,20 +604,11 @@ fn _advance_aad(ctx: pyo3::Bound<'_, pyo3::PyAny>, n: u64) { } } -pub(crate) fn create_module( - py: pyo3::Python<'_>, -) -> pyo3::PyResult> { - let m = pyo3::types::PyModule::new_bound(py, "ciphers")?; - m.add_function(pyo3::wrap_pyfunction_bound!(create_encryption_ctx, &m)?)?; - m.add_function(pyo3::wrap_pyfunction_bound!(create_decryption_ctx, &m)?)?; - m.add_function(pyo3::wrap_pyfunction_bound!(cipher_supported, &m)?)?; - - m.add_function(pyo3::wrap_pyfunction_bound!(_advance, &m)?)?; - m.add_function(pyo3::wrap_pyfunction_bound!(_advance_aad, &m)?)?; - - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - - Ok(m) +#[pyo3::pymodule] +pub(crate) mod ciphers { + #[pymodule_export] + use super::{ + _advance, _advance_aad, cipher_supported, create_decryption_ctx, create_encryption_ctx, + PyAEADDecryptionContext, PyAEADEncryptionContext, PyCipherContext, + }; } diff --git a/src/rust/src/backend/cmac.rs b/src/rust/src/backend/cmac.rs index dd30be2bec68..6a8737964643 100644 --- a/src/rust/src/backend/cmac.rs +++ b/src/rust/src/backend/cmac.rs @@ -7,7 +7,7 @@ use crate::backend::hashes::already_finalized_error; use crate::buf::CffiBuf; use crate::error::{CryptographyError, CryptographyResult}; use crate::{exceptions, types}; -use pyo3::types::{PyAnyMethods, PyBytesMethods, PyModuleMethods}; +use pyo3::types::{PyAnyMethods, PyBytesMethods}; #[pyo3::pyclass( module = "cryptography.hazmat.bindings._rust.openssl.cmac", @@ -100,12 +100,8 @@ impl Cmac { } } -pub(crate) fn create_module( - py: pyo3::Python<'_>, -) -> pyo3::PyResult> { - let m = pyo3::types::PyModule::new_bound(py, "cmac")?; - - m.add_class::()?; - - Ok(m) +#[pyo3::pymodule] +pub(crate) mod cmac { + #[pymodule_export] + use super::Cmac; } diff --git a/src/rust/src/backend/mod.rs b/src/rust/src/backend/mod.rs index 0a36c4c70192..3cbf2e3b99b6 100644 --- a/src/rust/src/backend/mod.rs +++ b/src/rust/src/backend/mod.rs @@ -26,10 +26,6 @@ pub(crate) mod x25519; pub(crate) mod x448; pub(crate) fn add_to_module(module: &pyo3::Bound<'_, pyo3::types::PyModule>) -> pyo3::PyResult<()> { - module.add_submodule(&aead::create_module(module.py())?)?; - module.add_submodule(&ciphers::create_module(module.py())?)?; - module.add_submodule(&cmac::create_module(module.py())?)?; - module.add_submodule(&rsa::create_module(module.py())?)?; Ok(()) diff --git a/src/rust/src/lib.rs b/src/rust/src/lib.rs index dbf497aa73a0..766c9ed0d8e9 100644 --- a/src/rust/src/lib.rs +++ b/src/rust/src/lib.rs @@ -156,6 +156,12 @@ mod _rust { #[pymodule_export] use super::super::{is_fips_enabled, openssl_version, openssl_version_text}; #[pymodule_export] + use crate::backend::aead::aead; + #[pymodule_export] + use crate::backend::ciphers::ciphers; + #[pymodule_export] + use crate::backend::cmac::cmac; + #[pymodule_export] use crate::backend::dh::dh; #[pymodule_export] use crate::backend::dsa::dsa;