Skip to content

Commit

Permalink
Release GIL during cipher update
Browse files Browse the repository at this point in the history
  • Loading branch information
kcking committed Nov 5, 2024
1 parent 26b293c commit f2c3d19
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions src/rust/src/backend/ciphers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,22 +179,26 @@ impl CipherContext {
}

let mut total_written = 0;
for chunk in data.chunks(1 << 29) {
// SAFETY: We ensure that outbuf is sufficiently large above.
unsafe {
let n = if self.py_mode.bind(py).is_instance(&types::XTS.get(py)?)? {
self.ctx.cipher_update_unchecked(chunk, Some(&mut buf[total_written..])).map_err(|_| {
pyo3::exceptions::PyValueError::new_err(
"In XTS mode you must supply at least a full block in the first update call. For AES this is 16 bytes."
)
})?
} else {
self.ctx
.cipher_update_unchecked(chunk, Some(&mut buf[total_written..]))?
};
total_written += n;
let is_xts = self.py_mode.bind(py).is_instance(&types::XTS.get(py)?)?;
py.allow_threads::<CryptographyResult<()>, _>(|| {
for chunk in data.chunks(1 << 29) {
// SAFETY: We ensure that outbuf is sufficiently large above.
unsafe {
let n = if is_xts {
self.ctx.cipher_update_unchecked(chunk, Some(&mut buf[total_written..])).map_err(|_| {
pyo3::exceptions::PyValueError::new_err(
"In XTS mode you must supply at least a full block in the first update call. For AES this is 16 bytes."
)
})?
} else {
self.ctx
.cipher_update_unchecked(chunk, Some(&mut buf[total_written..]))?
};
total_written += n;
}
}
}
Ok(())
})?;

Ok(total_written)
}
Expand Down

0 comments on commit f2c3d19

Please sign in to comment.