Skip to content

Commit 9759c8a

Browse files
committed
fix
1 parent 4ae0007 commit 9759c8a

File tree

2 files changed

+29
-33
lines changed

2 files changed

+29
-33
lines changed

parquet/src/encryption/encrypt.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
//! Configuration and utilities for Parquet Modular Encryption
1919
20-
use crate::encryption::ciphers::{BlockEncryptor, RingGcmBlockEncryptor};
20+
use crate::encryption::ciphers::{BlockEncryptor, RingGcmBlockEncryptor, NONCE_LEN, SIZE_LEN, TAG_LEN};
2121
use crate::errors::{ParquetError, Result};
2222
use crate::file::column_crypto_metadata::{ColumnCryptoMetaData, EncryptionWithColumnKey};
2323
use crate::schema::types::{ColumnDescPtr, SchemaDescriptor};
@@ -374,6 +374,30 @@ pub(crate) fn encrypt_object<T: TSerializable, W: Write>(
374374
Ok(())
375375
}
376376

377+
pub(crate) fn sign_and_write_object<T: TSerializable, W: Write>(
378+
object: &T,
379+
encryptor: &mut Box<dyn BlockEncryptor>,
380+
sink: &mut W,
381+
module_aad: &[u8],
382+
) -> Result<()> {
383+
let mut buffer: Vec<u8> = vec![];
384+
{
385+
let mut protocol = TCompactOutputProtocol::new(&mut buffer);
386+
object.write_to_out_protocol(&mut protocol)?;
387+
}
388+
let plaintext_len = buffer.len();
389+
sink.write_all(&buffer)?;
390+
391+
let encrypted_buffer = encryptor.encrypt(buffer.as_ref(), module_aad)?;
392+
393+
// Format is: [ciphertext size, nonce, ciphertext, authentication tag]
394+
let nonce = encrypted_buffer[SIZE_LEN..SIZE_LEN + NONCE_LEN].to_vec();
395+
let tag = encrypted_buffer[SIZE_LEN + NONCE_LEN + plaintext_len..SIZE_LEN + NONCE_LEN + plaintext_len + TAG_LEN].to_vec();
396+
sink.write_all(&nonce)?;
397+
sink.write_all(&tag)?;
398+
Ok(())
399+
}
400+
377401
/// Encrypt a Thrift serializable object to a byte vector
378402
pub(crate) fn encrypt_object_to_vec<T: TSerializable>(
379403
object: &T,

parquet/src/file/metadata/writer.rs

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

1818
#[cfg(feature = "encryption")]
19-
use crate::encryption::ciphers::{NONCE_LEN, SIZE_LEN, TAG_LEN};
20-
#[cfg(feature = "encryption")]
21-
use crate::encryption::encrypt::{encrypt_object, encrypt_object_to_vec, FileEncryptor};
19+
use crate::encryption::encrypt::{encrypt_object, encrypt_object_to_vec, sign_and_write_object, FileEncryptor};
2220
#[cfg(feature = "encryption")]
2321
use crate::encryption::modules::{create_footer_aad, create_module_aad, ModuleType};
2422
#[cfg(feature = "encryption")]
@@ -507,35 +505,9 @@ impl MetadataObjectWriter {
507505
}
508506
Some(file_encryptor) if !file_encryptor.properties().encrypt_footer() => {
509507
// todo: should we also check for file_metadata.encryption_algorithm.is_some() ?
510-
// Write unencrypted footer
511-
let data_len: usize;
512-
{
513-
let mut buffer: Vec<u8> = vec![];
514-
let mut unencrypted_protocol = TCompactOutputProtocol::new(&mut buffer);
515-
file_metadata.write_to_out_protocol(&mut unencrypted_protocol)?;
516-
data_len = buffer.len();
517-
sink.write_all(&buffer)?;
518-
}
519-
520-
// Write nonce and tag
521-
{
522-
let mut encrypted_buffer: Vec<u8> = vec![];
523-
let aad = create_footer_aad(file_encryptor.file_aad())?;
524-
let mut encryptor = file_encryptor.get_footer_encryptor()?;
525-
526-
let mut protocol = TCompactOutputProtocol::new(&mut encrypted_buffer);
527-
file_metadata.write_to_out_protocol(&mut protocol)?;
528-
encryptor.encrypt(encrypted_buffer.as_ref(), &aad)?;
529-
530-
// todo: check for overflow when calculating lengths
531-
let nonce =
532-
&encrypted_buffer[SIZE_LEN + data_len..SIZE_LEN + data_len + NONCE_LEN];
533-
sink.write_all(nonce)?;
534-
let tag = &encrypted_buffer[SIZE_LEN + data_len + NONCE_LEN
535-
..SIZE_LEN + data_len + NONCE_LEN + TAG_LEN];
536-
sink.write_all(tag)?;
537-
}
538-
Ok(())
508+
let aad = create_footer_aad(file_encryptor.file_aad())?;
509+
let mut encryptor = file_encryptor.get_footer_encryptor()?;
510+
sign_and_write_object(file_metadata, &mut encryptor, &mut sink, &aad)
539511
}
540512
_ => Self::write_object(file_metadata, &mut sink),
541513
}

0 commit comments

Comments
 (0)