Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move hashes, hmac, and kdf to declarative #11244

Merged
merged 3 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions src/rust/src/backend/hashes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// 2.0, and the BSD License. See the LICENSE file in the root of this repository
// for complete details.

use pyo3::types::{PyAnyMethods, PyModuleMethods};
use pyo3::types::PyAnyMethods;
use pyo3::IntoPy;
use std::borrow::Cow;

Expand Down Expand Up @@ -138,11 +138,8 @@ impl Hash {
}
}

pub(crate) fn create_module(
py: pyo3::Python<'_>,
) -> pyo3::PyResult<pyo3::Bound<'_, pyo3::types::PyModule>> {
let m = pyo3::types::PyModule::new_bound(py, "hashes")?;
m.add_class::<Hash>()?;

Ok(m)
#[pyo3::pymodule]
pub(crate) mod hashes {
#[pymodule_export]
use super::Hash;
}
13 changes: 5 additions & 8 deletions src/rust/src/backend/hmac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::backend::hashes::{already_finalized_error, message_digest_from_algori
use crate::buf::CffiBuf;
use crate::error::{CryptographyError, CryptographyResult};
use crate::exceptions;
use pyo3::types::{PyBytesMethods, PyModuleMethods};
use pyo3::types::PyBytesMethods;

#[pyo3::pyclass(
module = "cryptography.hazmat.bindings._rust.openssl.hmac",
Expand Down Expand Up @@ -106,11 +106,8 @@ impl Hmac {
}
}

pub(crate) fn create_module(
py: pyo3::Python<'_>,
) -> pyo3::PyResult<pyo3::Bound<'_, pyo3::types::PyModule>> {
let m = pyo3::types::PyModule::new_bound(py, "hmac")?;
m.add_class::<Hmac>()?;

Ok(m)
#[pyo3::pymodule]
pub(crate) mod hmac {
#[pymodule_export]
use super::Hmac;
}
16 changes: 6 additions & 10 deletions src/rust/src/backend/kdf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use crate::backend::hashes;
use crate::buf::CffiBuf;
use crate::error::CryptographyResult;
use pyo3::types::PyModuleMethods;

#[pyo3::pyfunction]
pub(crate) fn derive_pbkdf2_hmac<'p>(
Expand Down Expand Up @@ -49,14 +48,11 @@ fn derive_scrypt<'p>(
})?)
}

pub(crate) fn create_module(
py: pyo3::Python<'_>,
) -> pyo3::PyResult<pyo3::Bound<'_, pyo3::types::PyModule>> {
let m = pyo3::types::PyModule::new_bound(py, "kdf")?;

m.add_function(pyo3::wrap_pyfunction_bound!(derive_pbkdf2_hmac, &m)?)?;
#[pyo3::pymodule]
pub(crate) mod kdf {
#[pymodule_export]
use super::derive_pbkdf2_hmac;
#[cfg(not(CRYPTOGRAPHY_IS_LIBRESSL))]
m.add_function(pyo3::wrap_pyfunction_bound!(derive_scrypt, &m)?)?;

Ok(m)
#[pymodule_export]
use super::derive_scrypt;
}
3 changes: 0 additions & 3 deletions src/rust/src/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@ pub(crate) fn add_to_module(module: &pyo3::Bound<'_, pyo3::types::PyModule>) ->

module.add_submodule(&poly1305::create_module(module.py())?)?;

module.add_submodule(&hashes::create_module(module.py())?)?;
module.add_submodule(&hmac::create_module(module.py())?)?;
module.add_submodule(&kdf::create_module(module.py())?)?;
module.add_submodule(&rsa::create_module(module.py())?)?;

Ok(())
Expand Down
6 changes: 6 additions & 0 deletions src/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,12 @@ mod _rust {
#[pymodule_export]
use super::super::{is_fips_enabled, openssl_version, openssl_version_text};
#[pymodule_export]
use crate::backend::hashes::hashes;
#[pymodule_export]
use crate::backend::hmac::hmac;
#[pymodule_export]
use crate::backend::kdf::kdf;
#[pymodule_export]
use crate::backend::keys::keys;
#[pymodule_export]
use crate::error::{capture_error_stack, raise_openssl_error, OpenSSLError};
Expand Down