diff --git a/src/rust/src/backend/ciphers.rs b/src/rust/src/backend/ciphers.rs index 142175eb2471..8c90fe32e3d8 100644 --- a/src/rust/src/backend/ciphers.rs +++ b/src/rust/src/backend/ciphers.rs @@ -156,41 +156,41 @@ impl CipherContext { fn update<'p>( &mut self, py: pyo3::Python<'p>, - buf: &[u8], + data: &[u8], ) -> CryptographyResult> { - let mut out_buf = vec![0; buf.len() + self.ctx.block_size()]; - let n = self.update_into(py, buf, &mut out_buf)?; - Ok(pyo3::types::PyBytes::new_bound(py, &out_buf[..n])) + let mut buf = vec![0; data.len() + self.ctx.block_size()]; + let n = self.update_into(py, data, &mut buf)?; + Ok(pyo3::types::PyBytes::new_bound(py, &buf[..n])) } pub(crate) fn update_into( &mut self, py: pyo3::Python<'_>, - buf: &[u8], - out_buf: &mut [u8], + data: &[u8], + buf: &mut [u8], ) -> CryptographyResult { - if out_buf.len() < (buf.len() + self.ctx.block_size() - 1) { + if buf.len() < (data.len() + self.ctx.block_size() - 1) { return Err(CryptographyError::from( pyo3::exceptions::PyValueError::new_err(format!( "buffer must be at least {} bytes for this payload", - buf.len() + self.ctx.block_size() - 1 + data.len() + self.ctx.block_size() - 1 )), )); } let mut total_written = 0; - for chunk in buf.chunks(1 << 29) { + 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 out_buf[total_written..])).map_err(|_| { + 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 out_buf[total_written..]))? + .cipher_update_unchecked(chunk, Some(&mut buf[total_written..]))? }; total_written += n; } @@ -199,8 +199,8 @@ impl CipherContext { Ok(total_written) } - fn authenticate_additional_data(&mut self, buf: &[u8]) -> CryptographyResult<()> { - self.ctx.cipher_update(buf, None)?; + fn authenticate_additional_data(&mut self, data: &[u8]) -> CryptographyResult<()> { + self.ctx.cipher_update(data, None)?; Ok(()) } @@ -268,9 +268,9 @@ impl PyCipherContext { fn update<'p>( &mut self, py: pyo3::Python<'p>, - buf: CffiBuf<'_>, + data: CffiBuf<'_>, ) -> CryptographyResult> { - get_mut_ctx(self.ctx.as_mut())?.update(py, buf.as_bytes()) + get_mut_ctx(self.ctx.as_mut())?.update(py, data.as_bytes()) } fn reset_nonce(&mut self, py: pyo3::Python<'_>, nonce: CffiBuf<'_>) -> CryptographyResult<()> { @@ -280,10 +280,10 @@ impl PyCipherContext { fn update_into( &mut self, py: pyo3::Python<'_>, - buf: CffiBuf<'_>, - mut out_buf: CffiMutBuf<'_>, + data: CffiBuf<'_>, + mut buf: CffiMutBuf<'_>, ) -> CryptographyResult { - get_mut_ctx(self.ctx.as_mut())?.update_into(py, buf.as_bytes(), out_buf.as_mut_bytes()) + get_mut_ctx(self.ctx.as_mut())?.update_into(py, data.as_bytes(), buf.as_mut_bytes()) } fn finalize<'p>( @@ -301,9 +301,9 @@ impl PyAEADEncryptionContext { fn update<'p>( &mut self, py: pyo3::Python<'p>, - buf: CffiBuf<'_>, + data: CffiBuf<'_>, ) -> CryptographyResult> { - let data = buf.as_bytes(); + let data = data.as_bytes(); self.updated = true; self.bytes_remaining = self @@ -318,10 +318,10 @@ impl PyAEADEncryptionContext { fn update_into( &mut self, py: pyo3::Python<'_>, - buf: CffiBuf<'_>, - mut out_buf: CffiMutBuf<'_>, + data: CffiBuf<'_>, + mut buf: CffiMutBuf<'_>, ) -> CryptographyResult { - let data = buf.as_bytes(); + let data = data.as_bytes(); self.updated = true; self.bytes_remaining = self @@ -330,10 +330,10 @@ impl PyAEADEncryptionContext { .ok_or_else(|| { pyo3::exceptions::PyValueError::new_err("Exceeded maximum encrypted byte limit") })?; - get_mut_ctx(self.ctx.as_mut())?.update_into(py, data, out_buf.as_mut_bytes()) + get_mut_ctx(self.ctx.as_mut())?.update_into(py, data, buf.as_mut_bytes()) } - fn authenticate_additional_data(&mut self, buf: CffiBuf<'_>) -> CryptographyResult<()> { + fn authenticate_additional_data(&mut self, data: CffiBuf<'_>) -> CryptographyResult<()> { let ctx = get_mut_ctx(self.ctx.as_mut())?; if self.updated { return Err(CryptographyError::from( @@ -341,7 +341,7 @@ impl PyAEADEncryptionContext { )); } - let data = buf.as_bytes(); + let data = data.as_bytes(); self.aad_bytes_remaining = self .aad_bytes_remaining .checked_sub(data.len().try_into().unwrap()) @@ -392,9 +392,9 @@ impl PyAEADDecryptionContext { fn update<'p>( &mut self, py: pyo3::Python<'p>, - buf: CffiBuf<'_>, + data: CffiBuf<'_>, ) -> CryptographyResult> { - let data = buf.as_bytes(); + let data = data.as_bytes(); self.updated = true; self.bytes_remaining = self @@ -409,10 +409,10 @@ impl PyAEADDecryptionContext { fn update_into( &mut self, py: pyo3::Python<'_>, - buf: CffiBuf<'_>, - mut out_buf: CffiMutBuf<'_>, + data: CffiBuf<'_>, + mut buf: CffiMutBuf<'_>, ) -> CryptographyResult { - let data = buf.as_bytes(); + let data = data.as_bytes(); self.updated = true; self.bytes_remaining = self @@ -421,10 +421,10 @@ impl PyAEADDecryptionContext { .ok_or_else(|| { pyo3::exceptions::PyValueError::new_err("Exceeded maximum encrypted byte limit") })?; - get_mut_ctx(self.ctx.as_mut())?.update_into(py, data, out_buf.as_mut_bytes()) + get_mut_ctx(self.ctx.as_mut())?.update_into(py, data, buf.as_mut_bytes()) } - fn authenticate_additional_data(&mut self, buf: CffiBuf<'_>) -> CryptographyResult<()> { + fn authenticate_additional_data(&mut self, data: CffiBuf<'_>) -> CryptographyResult<()> { let ctx = get_mut_ctx(self.ctx.as_mut())?; if self.updated { return Err(CryptographyError::from( @@ -432,7 +432,7 @@ impl PyAEADDecryptionContext { )); } - let data = buf.as_bytes(); + let data = data.as_bytes(); self.aad_bytes_remaining = self .aad_bytes_remaining .checked_sub(data.len().try_into().unwrap())