perf(crypto): encrypt and decrypt stream_io fragments in place (#9733)

This commit is contained in:
Ramakrishna Chilaka
2026-11-29 07:19:47 +05:30
committed by GitHub
parent 39065bb10c
commit 312e1bc6f6
+18 -14
View File
@@ -65,7 +65,7 @@ where
A: AeadInPlace,
{
let ciphertext_len = SIO_BUF_SIZE + overhead;
let ad = build_associated_data(&aead, nonce_prefix)?;
let mut ad = build_associated_data(&aead, nonce_prefix)?;
let mut plain = Vec::with_capacity(body.len());
let mut seq_num: u32 = 1;
@@ -84,21 +84,25 @@ where
nonce[0..SIO_NONCE_PREFIX_LEN].copy_from_slice(nonce_prefix);
nonce[8..12].copy_from_slice(&seq_num.to_le_bytes());
let mut ad_mut = ad.clone();
ad_mut[0] = if is_last { 0x80 } else { 0x00 };
ad[0] = if is_last { 0x80 } else { 0x00 };
let fragment = &body[pos..pos + frag_len];
let tag_len = overhead;
let (ct, tag) = fragment.split_at(frag_len - tag_len);
let mut buffer = ct.to_vec();
let nonce_arr = Array::<u8, <A as AeadCore>::NonceSize>::try_from(&nonce[..])
.map_err(|_| Error::ErrDecryptFailed(aes_gcm::aead::Error))?;
let tag_arr =
Array::<u8, <A as AeadCore>::TagSize>::try_from(tag).map_err(|_| Error::ErrDecryptFailed(aes_gcm::aead::Error))?;
aead.decrypt_in_place_detached(&nonce_arr, &ad_mut, &mut buffer, &tag_arr)
.map_err(|_| Error::ErrDecryptFailed(aes_gcm::aead::Error))?;
plain.extend_from_slice(&buffer);
let start = plain.len();
plain.extend_from_slice(ct);
if aead
.decrypt_in_place_detached(&nonce_arr, &ad, &mut plain[start..], &tag_arr)
.is_err()
{
plain.clear();
return Err(Error::ErrDecryptFailed(aes_gcm::aead::Error));
}
pos += frag_len;
seq_num += 1;
@@ -149,7 +153,8 @@ pub fn encrypt_stream_io(password: &[u8], data: &[u8]) -> Result<Vec<u8>, Error>
let key = id.get_key(password, &salt)?;
let nonce_prefix: [u8; SIO_NONCE_PREFIX_LEN] = rand::random();
let mut out = Vec::with_capacity(STREAM_IO_HEADER_LEN + data.len() + 32);
let fragments = data.len().div_ceil(SIO_BUF_SIZE).max(1);
let mut out = Vec::with_capacity(STREAM_IO_HEADER_LEN + data.len() + fragments * AES_GCM_OVERHEAD);
out.extend_from_slice(&salt);
out.push(id as u8);
out.extend_from_slice(&nonce_prefix);
@@ -184,7 +189,7 @@ fn encrypt_stream<A>(
where
A: AeadInPlace,
{
let ad = build_associated_data(&aead, nonce_prefix)?;
let mut ad = build_associated_data(&aead, nonce_prefix)?;
let mut seq_num: u32 = 1;
let mut pos = 0;
@@ -199,16 +204,15 @@ where
nonce[0..SIO_NONCE_PREFIX_LEN].copy_from_slice(nonce_prefix);
nonce[8..12].copy_from_slice(&seq_num.to_le_bytes());
let mut ad_mut = ad.clone();
ad_mut[0] = if is_last { 0x80 } else { 0x00 };
ad[0] = if is_last { 0x80 } else { 0x00 };
let mut buffer = chunk.to_vec();
let nonce_arr = Array::<u8, <A as AeadCore>::NonceSize>::try_from(&nonce[..])
.map_err(|_| Error::ErrEncryptFailed(aes_gcm::aead::Error))?;
let start = out.len();
out.extend_from_slice(chunk);
let tag = aead
.encrypt_in_place_detached(&nonce_arr, &ad_mut, &mut buffer)
.encrypt_in_place_detached(&nonce_arr, &ad, &mut out[start..])
.map_err(Error::ErrEncryptFailed)?;
out.extend_from_slice(&buffer);
out.extend_from_slice(tag.as_slice());
pos += chunk_len;