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

Convert src/backend/hmac.rs to new pyo3 APIs #10726

Merged
merged 1 commit into from
Apr 5, 2024
Merged
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
21 changes: 12 additions & 9 deletions src/rust/src/backend/hmac.rs
Original file line number Diff line number Diff line change
@@ -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::PyNativeType;
use pyo3::prelude::{PyBytesMethods, PyModuleMethods};

#[pyo3::prelude::pyclass(
module = "cryptography.hazmat.bindings._rust.openssl.hmac",
@@ -22,7 +22,7 @@ impl Hmac {
pub(crate) fn new_bytes(
py: pyo3::Python<'_>,
key: &[u8],
algorithm: &pyo3::PyAny,
algorithm: &pyo3::Bound<'_, pyo3::PyAny>,
) -> CryptographyResult<Hmac> {
let md = message_digest_from_algorithm(py, &algorithm.as_borrowed())?;
let ctx = cryptography_openssl::hmac::Hmac::new(key, md).map_err(|_| {
@@ -34,7 +34,7 @@ impl Hmac {

Ok(Hmac {
ctx: Some(ctx),
algorithm: algorithm.into(),
algorithm: algorithm.clone().unbind(),
})
}

@@ -65,7 +65,7 @@ impl Hmac {
fn new(
py: pyo3::Python<'_>,
key: CffiBuf<'_>,
algorithm: &pyo3::PyAny,
algorithm: &pyo3::Bound<'_, pyo3::PyAny>,
backend: Option<pyo3::Bound<'_, pyo3::PyAny>>,
) -> CryptographyResult<Hmac> {
let _ = backend;
@@ -80,14 +80,15 @@ impl Hmac {
pub(crate) fn finalize<'p>(
&mut self,
py: pyo3::Python<'p>,
) -> CryptographyResult<&'p pyo3::types::PyBytes> {
) -> CryptographyResult<pyo3::Bound<'p, pyo3::types::PyBytes>> {
let data = self.get_mut_ctx()?.finish()?;
self.ctx = None;
Ok(pyo3::types::PyBytes::new(py, &data))
Ok(pyo3::types::PyBytes::new_bound(py, &data))
}

fn verify(&mut self, py: pyo3::Python<'_>, signature: &[u8]) -> CryptographyResult<()> {
let actual = self.finalize(py)?.as_bytes();
let actual_bound = self.finalize(py)?;
let actual = actual_bound.as_bytes();
if actual.len() != signature.len() || !openssl::memcmp::eq(actual, signature) {
return Err(CryptographyError::from(
exceptions::InvalidSignature::new_err("Signature did not match digest."),
@@ -105,8 +106,10 @@ impl Hmac {
}
}

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

Ok(m)
2 changes: 1 addition & 1 deletion src/rust/src/backend/mod.rs
Original file line number Diff line number Diff line change
@@ -43,7 +43,7 @@ pub(crate) fn add_to_module(module: &pyo3::prelude::PyModule) -> pyo3::PyResult<
module.add_submodule(poly1305::create_module(module.py())?)?;

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