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 checking if a hash is supported to Rust #11317

Merged
merged 1 commit into from
Jul 20, 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
14 changes: 1 addition & 13 deletions src/cryptography/hazmat/backends/openssl/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,23 +101,11 @@ def openssl_version_text(self) -> str:
def openssl_version_number(self) -> int:
return rust_openssl.openssl_version()

def _evp_md_from_algorithm(self, algorithm: hashes.HashAlgorithm):
if algorithm.name in ("blake2b", "blake2s"):
alg = f"{algorithm.name}{algorithm.digest_size * 8}".encode(
"ascii"
)
else:
alg = algorithm.name.encode("ascii")

evp_md = self._lib.EVP_get_digestbyname(alg)
return evp_md

def hash_supported(self, algorithm: hashes.HashAlgorithm) -> bool:
if self._fips_enabled and not isinstance(algorithm, self._fips_hashes):
return False

evp_md = self._evp_md_from_algorithm(algorithm)
return evp_md != self._ffi.NULL
return rust_openssl.hashes.hash_supported(algorithm)

def signature_hash_supported(
self, algorithm: hashes.HashAlgorithm
Expand Down
2 changes: 2 additions & 0 deletions src/cryptography/hazmat/bindings/_rust/openssl/hashes.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ class Hash(hashes.HashContext):
def update(self, data: bytes) -> None: ...
def finalize(self) -> bytes: ...
def copy(self) -> Hash: ...

def hash_supported(algorithm: hashes.HashAlgorithm) -> bool: ...
7 changes: 6 additions & 1 deletion src/rust/src/backend/hashes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ pub(crate) fn message_digest_from_algorithm(
}
}

#[pyo3::pyfunction]
fn hash_supported(py: pyo3::Python<'_>, algorithm: pyo3::Bound<'_, pyo3::PyAny>) -> bool {
message_digest_from_algorithm(py, &algorithm).is_ok()
}

impl Hash {
pub(crate) fn update_bytes(&mut self, data: &[u8]) -> CryptographyResult<()> {
self.get_mut_ctx()?.update(data)?;
Expand Down Expand Up @@ -141,5 +146,5 @@ impl Hash {
#[pyo3::pymodule]
pub(crate) mod hashes {
#[pymodule_export]
use super::Hash;
use super::{hash_supported, Hash};
}