Skip to content

Commit

Permalink
migrate aead, ciphers, and cmac to declarative (#11248)
Browse files Browse the repository at this point in the history
  • Loading branch information
reaperhulk authored Jul 10, 2024
1 parent 6f09c97 commit 56bab5e
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 44 deletions.
19 changes: 5 additions & 14 deletions src/rust/src/backend/aead.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -1153,17 +1153,8 @@ impl AesGcmSiv {
}
}

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

m.add_class::<AesGcm>()?;
m.add_class::<ChaCha20Poly1305>()?;
m.add_class::<AesCcm>()?;
m.add_class::<AesSiv>()?;
m.add_class::<AesOcb3>()?;
m.add_class::<AesGcmSiv>()?;

Ok(m)
#[pyo3::pymodule]
pub(crate) mod aead {
#[pymodule_export]
use super::{AesCcm, AesGcm, AesGcmSiv, AesOcb3, AesSiv, ChaCha20Poly1305};
}
25 changes: 8 additions & 17 deletions src/rust/src/backend/ciphers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -604,20 +604,11 @@ fn _advance_aad(ctx: pyo3::Bound<'_, pyo3::PyAny>, n: u64) {
}
}

pub(crate) fn create_module(
py: pyo3::Python<'_>,
) -> pyo3::PyResult<pyo3::Bound<'_, pyo3::types::PyModule>> {
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::<PyCipherContext>()?;
m.add_class::<PyAEADEncryptionContext>()?;
m.add_class::<PyAEADDecryptionContext>()?;

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,
};
}
14 changes: 5 additions & 9 deletions src/rust/src/backend/cmac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -100,12 +100,8 @@ impl Cmac {
}
}

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

m.add_class::<Cmac>()?;

Ok(m)
#[pyo3::pymodule]
pub(crate) mod cmac {
#[pymodule_export]
use super::Cmac;
}
4 changes: 0 additions & 4 deletions src/rust/src/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
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 @@ -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;
Expand Down

0 comments on commit 56bab5e

Please sign in to comment.