Skip to content

Commit 95018ff

Browse files
reaperhulkclaude
andauthored
Release the GIL in one-shot AEAD encrypt/decrypt (#15361)
* Release the GIL in one-shot AEAD encrypt/decrypt Follow-up to the hash/HMAC/cipher GIL-release work: EvpCipherAead encrypt/decrypt (the AESGCM, ChaCha20Poly1305, AESGCMSIV, AESOCB3, AESSIV, and AESCCM one-shot APIs) now run the process_aad + process_data + tag region via run_with_gil_detached when plaintext + AAD is at least the 2KiB threshold. Because the detached region must not touch Python objects, the AAD buffers are now extracted and length-checked up front (extract_aad), keeping the CffiBufs alive on the calling frame while only borrowed slices cross into the detached closure. The BoringSSL/AWS-LC EvpAead path is unchanged. Benchmark (Linux x86-64, 4 cores, CPython 3.11, release build): 4 threads each AESGCM-encrypting 16MiB: 9.3 ms -> 5.1 ms wall (1.8x; memory-bandwidth bound) AESGCM encrypt 64B: 617 -> 630 ns (noise) Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01C5bsKENedjVD9gcLApf1x4 * Address review feedback Fold the AAD slice harvesting and total-length computation into extract_aad itself: the returned ExtractedAad carries the raw slices (with the full GIL lifetime) alongside the CffiBufs that keep the buffers alive, so call sites just pass extracted.slices() through and the single-AAD path allocates nothing. Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01C5bsKENedjVD9gcLApf1x4 * Return a tuple from extract_aad Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01C5bsKENedjVD9gcLApf1x4 * Address review feedback: pass ExtractedAad through; inline the closures Split the AAD extraction result into an AadOwner (the Python buffer keepalives, which stay on the calling frame) and an ExtractedAad (raw slices + total length, no Python references) so process_aad takes the ExtractedAad directly and the whole thing can cross into the detached closure, which is now written inline at each call site. Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01C5bsKENedjVD9gcLApf1x4 * Make CffiBuf GIL-independent and pass ExtractedAad directly Per review: store Py rather than Bound inside CffiBuf (PyBuffer is already Send + Sync), which makes CffiBuf itself usable from a detached region as long as it's kept alive. ExtractedAad is then just the CffiBufs, process_aad takes it directly, and the separate owner/slices split and the wide-lifetime as_bytes_full accessor are gone. into_pyobj now takes py to rebind. Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01C5bsKENedjVD9gcLApf1x4 * Return the AAD length from extract_aad Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01C5bsKENedjVD9gcLApf1x4 * Remove comment per review Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01C5bsKENedjVD9gcLApf1x4 * Retain the soundness note on CffiBuf Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01C5bsKENedjVD9gcLApf1x4 --------- Co-authored-by: Claude <[email protected]>
1 parent 6954733 commit 95018ff

4 files changed

Lines changed: 78 additions & 25 deletions

File tree

‎src/rust/src/backend/aead.rs‎

Lines changed: 61 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,38 @@ pub(crate) enum Aad<'a> {
2626
List(pyo3::Bound<'a, pyo3::types::PyList>),
2727
}
2828

29+
/// AAD that has been extracted and length-checked while attached to the
30+
/// interpreter. `CffiBuf` holds no GIL-bound references, so this may be
31+
/// used from a detached region.
32+
enum ExtractedAad<'a> {
33+
None,
34+
Single(CffiBuf<'a>),
35+
List(Vec<CffiBuf<'a>>),
36+
}
37+
38+
/// Returns the extracted AAD and its total length in bytes.
39+
fn extract_aad(aad: Option<Aad<'_>>) -> CryptographyResult<(ExtractedAad<'_>, usize)> {
40+
match aad {
41+
None => Ok((ExtractedAad::None, 0)),
42+
Some(Aad::Single(ad)) => {
43+
check_length(ad.as_bytes())?;
44+
let len = ad.as_bytes().len();
45+
Ok((ExtractedAad::Single(ad), len))
46+
}
47+
Some(Aad::List(ads)) => {
48+
let mut bufs = Vec::with_capacity(ads.len());
49+
let mut len = 0;
50+
for ad in ads.iter() {
51+
let buf = ad.extract::<CffiBuf<'_>>()?;
52+
check_length(buf.as_bytes())?;
53+
len += buf.as_bytes().len();
54+
bufs.push(buf);
55+
}
56+
Ok((ExtractedAad::List(bufs), len))
57+
}
58+
}
59+
}
60+
2961
pub(crate) enum AeadCipher {
3062
Static(&'static openssl::cipher::CipherRef),
3163
#[cfg(not(any(
@@ -96,21 +128,18 @@ impl EvpCipherAead {
96128

97129
fn process_aad(
98130
ctx: &mut openssl::cipher_ctx::CipherCtx,
99-
aad: Option<Aad<'_>>,
131+
aad: &ExtractedAad<'_>,
100132
) -> CryptographyResult<()> {
101133
match aad {
102-
Some(Aad::Single(ad)) => {
103-
check_length(ad.as_bytes())?;
134+
ExtractedAad::None => {}
135+
ExtractedAad::Single(ad) => {
104136
ctx.cipher_update(ad.as_bytes(), None)?;
105137
}
106-
Some(Aad::List(ads)) => {
107-
for ad in ads.iter() {
108-
let ad = ad.extract::<CffiBuf<'_>>()?;
109-
check_length(ad.as_bytes())?;
138+
ExtractedAad::List(ads) => {
139+
for ad in ads {
110140
ctx.cipher_update(ad.as_bytes(), None)?;
111141
}
112142
}
113-
None => {}
114143
}
115144

116145
Ok(())
@@ -205,8 +234,6 @@ impl EvpCipherAead {
205234
ctx.encrypt_init(None, None, nonce)?;
206235
}
207236

208-
Self::process_aad(&mut ctx, aad)?;
209-
210237
let ciphertext;
211238
let tag;
212239
if self.tag_first {
@@ -215,9 +242,19 @@ impl EvpCipherAead {
215242
(ciphertext, tag) = buf.split_at_mut(plaintext.len());
216243
}
217244

218-
Self::process_data(&mut ctx, plaintext, ciphertext, self.is_ccm)?;
245+
let (aad, aad_len) = extract_aad(aad)?;
219246

220-
ctx.tag(tag).map_err(CryptographyError::from)?;
247+
let is_ccm = self.is_ccm;
248+
crate::backend::run_with_gil_detached(
249+
py,
250+
plaintext.len() + aad_len,
251+
|| -> CryptographyResult<()> {
252+
Self::process_aad(&mut ctx, &aad)?;
253+
Self::process_data(&mut ctx, plaintext, ciphertext, is_ccm)?;
254+
ctx.tag(tag).map_err(CryptographyError::from)?;
255+
Ok(())
256+
},
257+
)?;
221258

222259
Ok(())
223260
}
@@ -270,10 +307,19 @@ impl EvpCipherAead {
270307
ctx.set_tag(tag)?;
271308
}
272309

273-
Self::process_aad(&mut ctx, aad)?;
310+
let (aad, aad_len) = extract_aad(aad)?;
274311

275-
Self::process_data(&mut ctx, ciphertext_data, buf, self.is_ccm)
276-
.map_err(|_| exceptions::InvalidTag::new_err(()))?;
312+
let is_ccm = self.is_ccm;
313+
crate::backend::run_with_gil_detached(
314+
py,
315+
ciphertext_data.len() + aad_len,
316+
|| -> CryptographyResult<()> {
317+
Self::process_aad(&mut ctx, &aad)?;
318+
Self::process_data(&mut ctx, ciphertext_data, buf, is_ccm)
319+
.map_err(|_| exceptions::InvalidTag::new_err(()))?;
320+
Ok(())
321+
},
322+
)?;
277323

278324
Ok(())
279325
}

‎src/rust/src/buf.rs‎

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,12 @@ fn _extract_buffer_length<'p>(
7878
Ok((bufobj, ptrval, len))
7979
}
8080

81+
// Contains no GIL-bound references, so it is sound to use while the GIL
82+
// is released, as long as it's kept alive.
8183
pub(crate) struct CffiBuf<'p> {
82-
pyobj: pyo3::Bound<'p, pyo3::PyAny>,
84+
pyobj: pyo3::Py<pyo3::PyAny>,
8385
#[cfg(not(Py_3_11))]
84-
_bufobj: pyo3::Bound<'p, pyo3::PyAny>,
86+
_bufobj: pyo3::Py<pyo3::PyAny>,
8587
#[cfg(Py_3_11)]
8688
_bufobj: Option<pyo3::buffer::PyBuffer<u8>>,
8789
buf: &'p [u8],
@@ -90,11 +92,11 @@ pub(crate) struct CffiBuf<'p> {
9092
impl<'a> CffiBuf<'a> {
9193
pub(crate) fn from_bytes(py: pyo3::Python<'a>, buf: &'a [u8]) -> Self {
9294
CffiBuf {
93-
pyobj: py.None().into_bound(py),
95+
pyobj: py.None(),
9496
#[cfg(Py_3_11)]
9597
_bufobj: None,
9698
#[cfg(not(Py_3_11))]
97-
_bufobj: py.None().into_bound(py),
99+
_bufobj: py.None(),
98100
buf,
99101
}
100102
}
@@ -103,8 +105,8 @@ impl<'a> CffiBuf<'a> {
103105
self.buf
104106
}
105107

106-
pub(crate) fn into_pyobj(self) -> pyo3::Bound<'a, pyo3::PyAny> {
107-
self.pyobj
108+
pub(crate) fn into_pyobj(self, py: pyo3::Python<'a>) -> pyo3::Bound<'a, pyo3::PyAny> {
109+
self.pyobj.into_bound(py)
108110
}
109111
}
110112

@@ -127,8 +129,11 @@ impl<'p> pyo3::conversion::FromPyObject<'_, 'p> for CffiBuf<'p> {
127129
unsafe { slice::from_raw_parts(ptrval as *const u8, len) }
128130
};
129131
Ok(CffiBuf {
130-
pyobj: pyobj.to_owned(),
132+
pyobj: pyobj.to_owned().unbind(),
133+
#[cfg(Py_3_11)]
131134
_bufobj: bufobj,
135+
#[cfg(not(Py_3_11))]
136+
_bufobj: bufobj.unbind(),
132137
buf,
133138
})
134139
}

‎src/rust/src/padding.rs‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,13 @@ impl PKCS7PaddingContext {
8383

8484
pub(crate) fn update<'a>(
8585
&mut self,
86+
py: pyo3::Python<'a>,
8687
buf: CffiBuf<'a>,
8788
) -> CryptographyResult<pyo3::Bound<'a, pyo3::PyAny>> {
8889
match self.length_seen.as_mut() {
8990
Some(v) => {
9091
*v += buf.as_bytes().len();
91-
Ok(buf.into_pyobj())
92+
Ok(buf.into_pyobj(py))
9293
}
9394
None => Err(exceptions::already_finalized_error()),
9495
}
@@ -127,12 +128,13 @@ impl ANSIX923PaddingContext {
127128

128129
pub(crate) fn update<'a>(
129130
&mut self,
131+
py: pyo3::Python<'a>,
130132
buf: CffiBuf<'a>,
131133
) -> CryptographyResult<pyo3::Bound<'a, pyo3::PyAny>> {
132134
match self.length_seen.as_mut() {
133135
Some(v) => {
134136
*v += buf.as_bytes().len();
135-
Ok(buf.into_pyobj())
137+
Ok(buf.into_pyobj(py))
136138
}
137139
None => Err(exceptions::already_finalized_error()),
138140
}

‎src/rust/src/pkcs12.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ pub(crate) fn symmetric_encrypt(
106106
let n = cipher.update_into(py, data, &mut ciphertext)?;
107107

108108
let mut padder = PKCS7PaddingContext::new(block_size);
109-
assert!(padder.update(CffiBuf::from_bytes(py, data))?.is_none());
109+
assert!(padder.update(py, CffiBuf::from_bytes(py, data))?.is_none());
110110
let padding = padder.finalize(py)?;
111111

112112
let pad_n = cipher.update_into(py, padding.as_bytes(), &mut ciphertext[n..])?;

0 commit comments

Comments
 (0)