Skip to content

Commit

Permalink
symbols renaming to match cryptography.hazmat.primitives.ciphers.base…
Browse files Browse the repository at this point in the history
….CipherContext interface (#11692)
  • Loading branch information
xpinked authored Oct 3, 2024
1 parent 56e5c23 commit a1c012b
Showing 1 changed file with 34 additions and 34 deletions.
68 changes: 34 additions & 34 deletions src/rust/src/backend/ciphers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,41 +156,41 @@ impl CipherContext {
fn update<'p>(
&mut self,
py: pyo3::Python<'p>,
buf: &[u8],
data: &[u8],
) -> CryptographyResult<pyo3::Bound<'p, pyo3::types::PyBytes>> {
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<usize> {
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;
}
Expand All @@ -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(())
}

Expand Down Expand Up @@ -268,9 +268,9 @@ impl PyCipherContext {
fn update<'p>(
&mut self,
py: pyo3::Python<'p>,
buf: CffiBuf<'_>,
data: CffiBuf<'_>,
) -> CryptographyResult<pyo3::Bound<'p, pyo3::types::PyBytes>> {
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<()> {
Expand All @@ -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<usize> {
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>(
Expand All @@ -301,9 +301,9 @@ impl PyAEADEncryptionContext {
fn update<'p>(
&mut self,
py: pyo3::Python<'p>,
buf: CffiBuf<'_>,
data: CffiBuf<'_>,
) -> CryptographyResult<pyo3::Bound<'p, pyo3::types::PyBytes>> {
let data = buf.as_bytes();
let data = data.as_bytes();

self.updated = true;
self.bytes_remaining = self
Expand All @@ -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<usize> {
let data = buf.as_bytes();
let data = data.as_bytes();

self.updated = true;
self.bytes_remaining = self
Expand All @@ -330,18 +330,18 @@ 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(
exceptions::AlreadyUpdated::new_err("Update has been called on this context."),
));
}

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())
Expand Down Expand Up @@ -392,9 +392,9 @@ impl PyAEADDecryptionContext {
fn update<'p>(
&mut self,
py: pyo3::Python<'p>,
buf: CffiBuf<'_>,
data: CffiBuf<'_>,
) -> CryptographyResult<pyo3::Bound<'p, pyo3::types::PyBytes>> {
let data = buf.as_bytes();
let data = data.as_bytes();

self.updated = true;
self.bytes_remaining = self
Expand All @@ -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<usize> {
let data = buf.as_bytes();
let data = data.as_bytes();

self.updated = true;
self.bytes_remaining = self
Expand All @@ -421,18 +421,18 @@ 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(
exceptions::AlreadyUpdated::new_err("Update has been called on this context."),
));
}

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())
Expand Down

0 comments on commit a1c012b

Please sign in to comment.