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/poly1305.rs to new pyo3 APIs #10728

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
2 changes: 1 addition & 1 deletion src/rust/src/backend/mod.rs
Original file line number Diff line number Diff line change
@@ -40,7 +40,7 @@ pub(crate) fn add_to_module(module: &pyo3::prelude::PyModule) -> pyo3::PyResult<
#[cfg(all(not(CRYPTOGRAPHY_IS_LIBRESSL), not(CRYPTOGRAPHY_IS_BORINGSSL)))]
module.add_submodule(x448::create_module(module.py())?.into_gil_ref())?;

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

module.add_submodule(hashes::create_module(module.py())?.into_gil_ref())?;
module.add_submodule(hmac::create_module(module.py())?)?;
22 changes: 13 additions & 9 deletions src/rust/src/backend/poly1305.rs
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@ use crate::backend::hashes::already_finalized_error;
use crate::buf::CffiBuf;
use crate::error::{CryptographyError, CryptographyResult};
use crate::exceptions;
use pyo3::prelude::{PyBytesMethods, PyModuleMethods};

#[cfg(any(CRYPTOGRAPHY_IS_BORINGSSL, CRYPTOGRAPHY_IS_LIBRESSL))]
struct Poly1305Boring {
@@ -31,8 +32,8 @@ impl Poly1305Boring {
fn finalize<'p>(
&mut self,
py: pyo3::Python<'p>,
) -> CryptographyResult<&'p pyo3::types::PyBytes> {
let result = pyo3::types::PyBytes::new_with(py, 16usize, |b| {
) -> CryptographyResult<pyo3::Bound<'p, pyo3::types::PyBytes>> {
let result = pyo3::types::PyBytes::new_bound_with(py, 16usize, |b| {
self.context.finalize(b.as_mut());
Ok(())
})?;
@@ -77,8 +78,8 @@ impl Poly1305Open {
fn finalize<'p>(
&mut self,
py: pyo3::Python<'p>,
) -> CryptographyResult<&'p pyo3::types::PyBytes> {
let result = pyo3::types::PyBytes::new_with(py, self.signer.len()?, |b| {
) -> CryptographyResult<pyo3::Bound<'p, pyo3::types::PyBytes>> {
let result = pyo3::types::PyBytes::new_bound_with(py, self.signer.len()?, |b| {
let n = self.signer.sign(b).unwrap();
assert_eq!(n, b.len());
Ok(())
@@ -114,7 +115,7 @@ impl Poly1305 {
py: pyo3::Python<'p>,
key: CffiBuf<'_>,
data: CffiBuf<'_>,
) -> CryptographyResult<&'p pyo3::types::PyBytes> {
) -> CryptographyResult<pyo3::Bound<'p, pyo3::types::PyBytes>> {
let mut p = Poly1305::new(key)?;
p.update(data)?;
p.finalize(py)
@@ -141,7 +142,7 @@ impl Poly1305 {
fn finalize<'p>(
&mut self,
py: pyo3::Python<'p>,
) -> CryptographyResult<&'p pyo3::types::PyBytes> {
) -> CryptographyResult<pyo3::Bound<'p, pyo3::types::PyBytes>> {
let res = self
.inner
.as_mut()
@@ -152,7 +153,8 @@ impl Poly1305 {
}

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("Value did not match computed tag."),
@@ -163,8 +165,10 @@ impl Poly1305 {
}
}

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

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