Skip to content

Commit 2028cf8

Browse files
authored
refactor aead encrypt_with_context to take a mutable buffer (#13698)
1 parent 9f88b1f commit 2028cf8

File tree

1 file changed

+46
-37
lines changed

1 file changed

+46
-37
lines changed

src/rust/src/backend/aead.rs

Lines changed: 46 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -136,29 +136,37 @@ impl EvpCipherAead {
136136
) -> CryptographyResult<pyo3::Bound<'p, pyo3::types::PyBytes>> {
137137
let mut ctx = openssl::cipher_ctx::CipherCtx::new()?;
138138
ctx.copy(&self.base_encryption_ctx)?;
139-
Self::encrypt_with_context(
139+
140+
Ok(pyo3::types::PyBytes::new_with(
140141
py,
141-
ctx,
142-
plaintext,
143-
aad,
144-
nonce,
145-
self.tag_len,
146-
self.tag_first,
147-
false,
148-
)
142+
plaintext.len() + self.tag_len,
143+
|b| {
144+
Self::encrypt_with_context(
145+
ctx,
146+
plaintext,
147+
aad,
148+
nonce,
149+
self.tag_len,
150+
self.tag_first,
151+
false,
152+
b,
153+
)?;
154+
Ok(())
155+
},
156+
)?)
149157
}
150158

151159
#[allow(clippy::too_many_arguments)]
152-
fn encrypt_with_context<'p>(
153-
py: pyo3::Python<'p>,
160+
fn encrypt_with_context(
154161
mut ctx: openssl::cipher_ctx::CipherCtx,
155162
plaintext: &[u8],
156163
aad: Option<Aad<'_>>,
157164
nonce: Option<&[u8]>,
158165
tag_len: usize,
159166
tag_first: bool,
160167
is_ccm: bool,
161-
) -> CryptographyResult<pyo3::Bound<'p, pyo3::types::PyBytes>> {
168+
buf: &mut [u8],
169+
) -> CryptographyResult<()> {
162170
check_length(plaintext)?;
163171

164172
if !is_ccm {
@@ -173,25 +181,19 @@ impl EvpCipherAead {
173181

174182
Self::process_aad(&mut ctx, aad)?;
175183

176-
Ok(pyo3::types::PyBytes::new_with(
177-
py,
178-
plaintext.len() + tag_len,
179-
|b| {
180-
let ciphertext;
181-
let tag;
182-
if tag_first {
183-
(tag, ciphertext) = b.split_at_mut(tag_len);
184-
} else {
185-
(ciphertext, tag) = b.split_at_mut(plaintext.len());
186-
}
184+
let ciphertext;
185+
let tag;
186+
if tag_first {
187+
(tag, ciphertext) = buf.split_at_mut(tag_len);
188+
} else {
189+
(ciphertext, tag) = buf.split_at_mut(plaintext.len());
190+
}
187191

188-
Self::process_data(&mut ctx, plaintext, ciphertext, is_ccm)?;
192+
Self::process_data(&mut ctx, plaintext, ciphertext, is_ccm)?;
189193

190-
ctx.tag(tag).map_err(CryptographyError::from)?;
194+
ctx.tag(tag).map_err(CryptographyError::from)?;
191195

192-
Ok(())
193-
},
194-
)?)
196+
Ok(())
195197
}
196198

197199
fn decrypt<'p>(
@@ -314,16 +316,23 @@ impl LazyEvpCipherAead {
314316
encryption_ctx.encrypt_init(Some(self.cipher), Some(key_buf.as_bytes()), None)?;
315317
}
316318

317-
EvpCipherAead::encrypt_with_context(
319+
Ok(pyo3::types::PyBytes::new_with(
318320
py,
319-
encryption_ctx,
320-
plaintext,
321-
aad,
322-
nonce,
323-
self.tag_len,
324-
self.tag_first,
325-
self.is_ccm,
326-
)
321+
plaintext.len() + self.tag_len,
322+
|b| {
323+
EvpCipherAead::encrypt_with_context(
324+
encryption_ctx,
325+
plaintext,
326+
aad,
327+
nonce,
328+
self.tag_len,
329+
self.tag_first,
330+
self.is_ccm,
331+
b,
332+
)?;
333+
Ok(())
334+
},
335+
)?)
327336
}
328337

329338
fn decrypt<'p>(

0 commit comments

Comments
 (0)