Skip to content

Commit

Permalink
Move cyphertext reading into decryptor
Browse files Browse the repository at this point in the history
  • Loading branch information
rok committed Jan 23, 2025
1 parent c4860da commit f6b9e88
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
13 changes: 13 additions & 0 deletions parquet/src/encryption/decryption.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@
use crate::errors::{ParquetError, Result};
use ring::aead::{Aad, LessSafeKey, UnboundKey, AES_128_GCM};
use std::collections::HashMap;
use std::io::Read;

const NONCE_LEN: usize = 12;
const TAG_LEN: usize = 16;
const SIZE_LEN: usize = 4;

pub trait BlockDecryptor {
fn decrypt(&self, length_and_ciphertext: &[u8], aad: &[u8]) -> Result<Vec<u8>>;

fn read_and_decrypt<T: Read>(&self, input: &mut T, aad: &[u8]) -> Result<Vec<u8>>;
}

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -59,6 +62,16 @@ impl BlockDecryptor for RingGcmBlockDecryptor {
result.resize(result.len() - TAG_LEN, 0u8);
Ok(result)
}

fn read_and_decrypt<T: Read>(&self, input: &mut T, aad: &[u8]) -> Result<Vec<u8>> {
let mut len_bytes = [0; 4];
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];
input.read_exact(&mut ciphertext[4..])?;

self.decrypt(&ciphertext, aad.as_ref())
}
}

#[derive(Debug, Clone, PartialEq)]
Expand Down
8 changes: 1 addition & 7 deletions parquet/src/file/serialized_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,16 +377,10 @@ pub(crate) fn read_page_header<T: Read>(
crypto_context.page_ordinal,
)?;

let mut len_bytes = [0; 4];
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];
input.read_exact(&mut ciphertext[4..])?;

let buf = data_decryptor
.footer_decryptor()
.unwrap()
.decrypt(&ciphertext, aad.as_ref())?;
.read_and_decrypt(input, aad.as_ref())?;

let mut prot = TCompactSliceInputProtocol::new(buf.as_slice());
let page_header = PageHeader::read_from_in_protocol(&mut prot)?;
Expand Down

0 comments on commit f6b9e88

Please sign in to comment.