Skip to content

Commit 073550b

Browse files
committed
Return encryptors as a Result<Box<dyn BlockEncryptor>>
1 parent 642350f commit 073550b

File tree

4 files changed

+29
-24
lines changed

4 files changed

+29
-24
lines changed

parquet/src/encryption/ciphers.rs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ impl BlockDecryptor for RingGcmBlockDecryptor {
8181
}
8282
}
8383

84-
pub trait BlockEncryptor: Debug + Send + Sync + Clone {
84+
pub trait BlockEncryptor: Debug + Send + Sync {
8585
fn encrypt(&mut self, plaintext: &[u8], aad: &[u8]) -> Vec<u8>;
8686
}
8787

@@ -92,19 +92,19 @@ struct CounterNonce {
9292
}
9393

9494
impl CounterNonce {
95-
pub fn new(rng: &SystemRandom) -> Self {
95+
pub fn new(rng: &SystemRandom) -> Result<Self> {
9696
let mut buf = [0; 16];
97-
rng.fill(&mut buf).unwrap();
97+
rng.fill(&mut buf)?;
9898

99-
// Since this is a random seed value, endianess doesn't matter at all,
99+
// Since this is a random seed value, endianness doesn't matter at all,
100100
// and we can use whatever is platform-native.
101101
let start = u128::from_ne_bytes(buf) & RIGHT_TWELVE;
102102
let counter = start.wrapping_add(1);
103103

104-
Self { start, counter }
104+
Ok(Self { start, counter })
105105
}
106106

107-
/// One accessor for the nonce bytes to avoid potentially flipping endianess
107+
/// One accessor for the nonce bytes to avoid potentially flipping endianness
108108
#[inline]
109109
pub fn get_bytes(&self) -> [u8; NONCE_LEN] {
110110
self.counter.to_le_bytes()[0..NONCE_LEN].try_into().unwrap()
@@ -132,22 +132,21 @@ pub(crate) struct RingGcmBlockEncryptor {
132132
}
133133

134134
impl RingGcmBlockEncryptor {
135-
// todo TBD: some KMS systems produce data keys, need to be able to pass them to Encryptor.
136-
// todo TBD: for other KMSs, we will create data keys inside arrow-rs, making sure to use SystemRandom
137135
/// Create a new `RingGcmBlockEncryptor` with a given key and random nonce.
138136
/// The nonce will advance appropriately with each block encryption and
139137
/// return an error if it wraps around.
140-
pub(crate) fn new(key_bytes: &[u8]) -> Self {
138+
pub(crate) fn new(key_bytes: &[u8]) -> Result<Self> {
141139
let rng = SystemRandom::new();
142140

143141
// todo support other key sizes
144-
let key = UnboundKey::new(&AES_128_GCM, key_bytes).unwrap();
145-
let nonce = CounterNonce::new(&rng);
142+
let key = UnboundKey::new(&AES_128_GCM, key_bytes)
143+
.map_err(|e| general_err!("Error creating AES key: {}", e))?;
144+
let nonce = CounterNonce::new(&rng)?;
146145

147-
Self {
146+
Ok(Self {
148147
key: LessSafeKey::new(key),
149148
nonce_sequence: nonce,
150-
}
149+
})
151150
}
152151
}
153152

@@ -176,7 +175,7 @@ mod tests {
176175
#[test]
177176
fn test_round_trip() {
178177
let key = [0u8; 16];
179-
let mut encryptor = RingGcmBlockEncryptor::new(&key);
178+
let mut encryptor = RingGcmBlockEncryptor::new(&key).unwrap();
180179
let decryptor = RingGcmBlockDecryptor::new(&key);
181180

182181
let plaintext = b"hello, world!";

parquet/src/encryption/encrypt.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -166,19 +166,24 @@ impl FileEncryptor {
166166
&self.aad_file_unique
167167
}
168168

169-
pub(crate) fn get_footer_encryptor(&self) -> RingGcmBlockEncryptor {
170-
RingGcmBlockEncryptor::new(&self.properties.footer_key.key)
169+
pub(crate) fn get_footer_encryptor(&self) -> Result<Box<dyn BlockEncryptor>> {
170+
Ok(Box::new(RingGcmBlockEncryptor::new(
171+
&self.properties.footer_key.key,
172+
)?))
171173
}
172174

173-
pub(crate) fn get_column_encryptor(&self, column_path: &str) -> RingGcmBlockEncryptor {
175+
pub(crate) fn get_column_encryptor(
176+
&self,
177+
column_path: &str,
178+
) -> Result<Box<dyn BlockEncryptor>> {
174179
if self.properties.column_keys.is_empty() {
175-
return RingGcmBlockEncryptor::new(self.properties.footer_key.key());
180+
return self.get_footer_encryptor();
176181
}
177182
// TODO: Column paths should be stored as String
178183
let column_path = column_path.as_bytes();
179184
match self.properties.column_keys.get(column_path) {
180185
None => todo!("Handle unencrypted columns"),
181-
Some(column_key) => RingGcmBlockEncryptor::new(column_key.key()),
186+
Some(column_key) => Ok(Box::new(RingGcmBlockEncryptor::new(column_key.key())?)),
182187
}
183188
}
184189
}
@@ -197,7 +202,7 @@ pub(crate) fn encrypt_object<T: TSerializable, W: Write>(
197202

198203
// TODO: Get correct encryptor (footer vs column, data vs metadata)
199204
let encrypted_buffer = encryptor
200-
.get_footer_encryptor()
205+
.get_footer_encryptor()?
201206
.encrypt(buffer.as_ref(), module_aad);
202207

203208
sink.write_all(&encrypted_buffer)?;

parquet/src/encryption/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
//! Encryption implementation specific to Parquet, as described
1919
//! in the [spec](https://github.com/apache/parquet-format/blob/master/Encryption.md).
2020
21-
pub mod ciphers;
21+
pub(crate) mod ciphers;
2222
pub mod decryption;
2323
pub mod encrypt;
24-
pub mod modules;
24+
pub(crate) mod modules;
2525
pub(crate) mod page_encryptor;

parquet/src/encryption/page_encryptor.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
// under the License.
1717

1818
use crate::column::page::CompressedPage;
19-
use crate::encryption::ciphers::BlockEncryptor;
2019
use crate::encryption::encrypt::{encrypt_object, FileEncryptor};
2120
use crate::encryption::modules::{create_module_aad, ModuleType};
2221
use crate::errors::ParquetError;
@@ -66,7 +65,9 @@ impl PageEncryptor {
6665
self.column_index,
6766
Some(self.page_index),
6867
)?;
69-
let mut encryptor = self.file_encryptor.get_column_encryptor(&self.column_path);
68+
let mut encryptor = self
69+
.file_encryptor
70+
.get_column_encryptor(&self.column_path)?;
7071
// todo: use column encryptor when needed
7172
// self.file_encryptor.get_column_encryptor(self.column_path.as_ref())
7273
let encrypted_buffer = encryptor.encrypt(page.data(), &aad);

0 commit comments

Comments
 (0)