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

migrate poly1305, x448/25519, ed448/25519 to declarative #11245

Merged
merged 1 commit 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
19 changes: 6 additions & 13 deletions src/rust/src/backend/ed25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -160,16 +159,10 @@ impl Ed25519PublicKey {
}
}

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

Ok(m)
#[pyo3::pymodule]
pub(crate) mod ed25519 {
#[pymodule_export]
use super::{
from_private_bytes, from_public_bytes, generate_key, Ed25519PrivateKey, Ed25519PublicKey,
};
}
19 changes: 6 additions & 13 deletions src/rust/src/backend/ed448.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -157,16 +156,10 @@ impl Ed448PublicKey {
}
}

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

Ok(m)
#[pyo3::pymodule]
pub(crate) mod ed448 {
#[pymodule_export]
use super::{
from_private_bytes, from_public_bytes, generate_key, Ed448PrivateKey, Ed448PublicKey,
};
}
10 changes: 0 additions & 10 deletions src/rust/src/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
Expand Down
14 changes: 5 additions & 9 deletions src/rust/src/backend/poly1305.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -165,12 +165,8 @@ impl Poly1305 {
}
}

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

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

Ok(m)
#[pyo3::pymodule]
pub(crate) mod poly1305 {
#[pymodule_export]
use super::Poly1305;
}
19 changes: 6 additions & 13 deletions src/rust/src/backend/x25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -150,16 +149,10 @@ impl X25519PublicKey {
}
}

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

Ok(m)
#[pyo3::pymodule]
pub(crate) mod x25519 {
#[pymodule_export]
use super::{
from_private_bytes, from_public_bytes, generate_key, X25519PrivateKey, X25519PublicKey,
};
}
19 changes: 6 additions & 13 deletions src/rust/src/backend/x448.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -149,16 +148,10 @@ impl X448PublicKey {
}
}

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

Ok(m)
#[pyo3::pymodule]
pub(crate) mod x448 {
#[pymodule_export]
use super::{
from_private_bytes, from_public_bytes, generate_key, X448PrivateKey, X448PublicKey,
};
}
12 changes: 12 additions & 0 deletions src/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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]
Expand Down