Skip to content

Commit

Permalink
move hashes, hmac, and kdf to declarative (#11244)
Browse files Browse the repository at this point in the history
* move hashes, hmac, and kdf to declarative

* libre fix

* unneeded pub
  • Loading branch information
reaperhulk committed Jul 10, 2024
1 parent ccae9ef commit 1ed43fd
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 29 deletions.
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

0 comments on commit 1ed43fd

Please sign in to comment.