Skip to content

Commit 23375d1

Browse files
committed
Move cyphertext reading into decryptor
1 parent c4860da commit 23375d1

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

parquet/src/encryption/decryption.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,16 @@
1818
use crate::errors::{ParquetError, Result};
1919
use ring::aead::{Aad, LessSafeKey, UnboundKey, AES_128_GCM};
2020
use std::collections::HashMap;
21+
use std::io::Read;
2122

2223
const NONCE_LEN: usize = 12;
2324
const TAG_LEN: usize = 16;
2425
const SIZE_LEN: usize = 4;
2526

2627
pub trait BlockDecryptor {
2728
fn decrypt(&self, length_and_ciphertext: &[u8], aad: &[u8]) -> Result<Vec<u8>>;
29+
30+
fn read_and_decrypt<T: Read>(&self, input: &mut T, aad: &[u8]) -> Result<Vec<u8>>;
2831
}
2932

3033
#[derive(Debug, Clone)]
@@ -59,6 +62,16 @@ impl BlockDecryptor for RingGcmBlockDecryptor {
5962
result.resize(result.len() - TAG_LEN, 0u8);
6063
Ok(result)
6164
}
65+
66+
fn read_and_decrypt<T: Read>(&self, input: &mut T, aad: &[u8]) -> Result<Vec<u8>> {
67+
let mut len_bytes = [0; 4];
68+
input.read_exact(&mut len_bytes)?;
69+
let ciphertext_len = u32::from_le_bytes(len_bytes) as usize;
70+
let mut ciphertext = vec![0; 4 + ciphertext_len];
71+
input.read_exact(&mut ciphertext[4..])?;
72+
73+
self.decrypt(&ciphertext, aad.as_ref())
74+
}
6275
}
6376

6477
#[derive(Debug, Clone, PartialEq)]
@@ -102,7 +115,9 @@ impl DecryptionPropertiesBuilder {
102115

103116
pub fn build(self) -> Result<FileDecryptionProperties> {
104117
if self.footer_key.is_none() && self.column_keys.is_none() {
105-
return Err(ParquetError::General("Footer or at least one column key is required".to_string()))
118+
return Err(ParquetError::General(
119+
"Footer or at least one column key is required".to_string(),
120+
));
106121
}
107122

108123
Ok(FileDecryptionProperties {

parquet/src/file/serialized_reader.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -377,16 +377,10 @@ pub(crate) fn read_page_header<T: Read>(
377377
crypto_context.page_ordinal,
378378
)?;
379379

380-
let mut len_bytes = [0; 4];
381-
input.read_exact(&mut len_bytes)?;
382-
let ciphertext_len = u32::from_le_bytes(len_bytes) as usize;
383-
let mut ciphertext = vec![0; 4 + ciphertext_len];
384-
input.read_exact(&mut ciphertext[4..])?;
385-
386380
let buf = data_decryptor
387381
.footer_decryptor()
388382
.unwrap()
389-
.decrypt(&ciphertext, aad.as_ref())?;
383+
.read_and_decrypt(input, aad.as_ref())?;
390384

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

0 commit comments

Comments
 (0)