Skip to content

Commit

Permalink
Add check of ciphertext length
Browse files Browse the repository at this point in the history
  • Loading branch information
adamreeve committed Mar 4, 2025
1 parent f819a4a commit cc24157
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
20 changes: 19 additions & 1 deletion parquet/src/encryption/ciphers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// specific language governing permissions and limitations
// under the License.

use crate::errors::Result;
use crate::errors::{ParquetError, Result};
use ring::aead::{Aad, LessSafeKey, NonceSequence, UnboundKey, AES_128_GCM};
use ring::rand::{SecureRandom, SystemRandom};
use std::fmt::Debug;
Expand Down Expand Up @@ -47,6 +47,24 @@ impl RingGcmBlockDecryptor {

impl BlockDecryptor for RingGcmBlockDecryptor {
fn decrypt(&self, length_and_ciphertext: &[u8], aad: &[u8]) -> Result<Vec<u8>> {
if length_and_ciphertext.len() < SIZE_LEN {
return Err(general_err!(
"Ciphertext buffer size {} must be at least {}",
length_and_ciphertext.len(),
SIZE_LEN
));
}
let mut len_bytes = [0; 4];
len_bytes.copy_from_slice(&length_and_ciphertext[0..SIZE_LEN]);
let ciphertext_len = u32::from_le_bytes(len_bytes) as usize;
if length_and_ciphertext.len() != SIZE_LEN + ciphertext_len {
return Err(general_err!(
"Ciphertext buffer size {} does not match expected size {}",
length_and_ciphertext.len(),
SIZE_LEN + ciphertext_len
));
}

let mut result =
Vec::with_capacity(length_and_ciphertext.len() - SIZE_LEN - NONCE_LEN - TAG_LEN);
result.extend_from_slice(&length_and_ciphertext[SIZE_LEN + NONCE_LEN..]);
Expand Down
1 change: 1 addition & 0 deletions parquet/src/encryption/decryption.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub fn read_and_decrypt<T: Read>(
input.read_exact(&mut len_bytes)?;
let ciphertext_len = u32::from_le_bytes(len_bytes) as usize;
let mut ciphertext = vec![0; 4 + ciphertext_len];
ciphertext[0..4].copy_from_slice(&len_bytes);
input.read_exact(&mut ciphertext[4..])?;

decryptor.decrypt(&ciphertext, aad.as_ref())
Expand Down

0 comments on commit cc24157

Please sign in to comment.