@@ -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+
2961pub ( 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 }
0 commit comments