Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,904 changes: 611 additions & 1,293 deletions cpp/src/generated/parquet_types.cpp

Large diffs are not rendered by default.

387 changes: 153 additions & 234 deletions cpp/src/generated/parquet_types.h

Large diffs are not rendered by default.

549 changes: 301 additions & 248 deletions cpp/src/generated/parquet_types.tcc

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion cpp/src/parquet/encryption/crypto_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -240,10 +240,12 @@ std::shared_ptr<FileDecryptionProperties> CryptoFactory::GetFileDecryptionProper
const KmsConnectionConfig& kms_connection_config,
const DecryptionConfiguration& decryption_config, const std::string& file_path,
const std::shared_ptr<::arrow::fs::FileSystem>& file_system) {
std::cout << "Getting file decryption properties!!" << std::endl;
auto key_retriever = std::make_shared<FileKeyUnwrapper>(
key_toolkit_, kms_connection_config, decryption_config.cache_lifetime_seconds,
file_path, file_system);

std::cout << "Key retriever: " << key_retriever << std::endl;
std::cout << "Going to decryption properties builder" << std::endl;
return FileDecryptionProperties::Builder()
.key_retriever(key_retriever)
->plaintext_files_allowed()
Expand Down
2 changes: 2 additions & 0 deletions cpp/src/parquet/encryption/encryption.cc
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,8 @@ FileDecryptionProperties::FileDecryptionProperties(
if (footer_key.empty() && check_plaintext_footer_integrity) {
DCHECK(nullptr != key_retriever);
}
std::cout << "In the FileDecryptionProperties constructor" << std::endl;
std::cout << "column_decryption_properties.size(): " << column_decryption_properties.size() << std::endl;
aad_prefix_verifier_ = std::move(aad_prefix_verifier);
footer_key_ = footer_key;
check_plaintext_footer_integrity_ = check_plaintext_footer_integrity;
Expand Down
45 changes: 41 additions & 4 deletions cpp/src/parquet/encryption/encryption_internal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,11 @@ int32_t AesDecryptorImpl::GetCiphertextLength(span<const uint8_t> ciphertext) co

int32_t AesDecryptorImpl::GcmDecrypt(span<const uint8_t> ciphertext, span<const uint8_t> key,
span<const uint8_t> aad, span<uint8_t> plaintext) {

std::cout << "GcmDecrypt: ciphertext_len=" << ciphertext.size() << std::endl;
std::cout << "GcmDecrypt: plaintext.size()=" << plaintext.size() << std::endl;
std::cout << "GcmDecrypt: ciphertext_size_delta_=" << ciphertext_size_delta_ << std::endl;
std::cout << "GcmDecrypt: length_buffer_length_=" << length_buffer_length_ << std::endl;
int len;
int32_t plaintext_len;

Expand Down Expand Up @@ -403,6 +408,7 @@ int32_t AesDecryptorImpl::GcmDecrypt(span<const uint8_t> ciphertext, span<const
// Decryption
int decryption_length =
ciphertext_len - length_buffer_length_ - kNonceLength - kGcmTagLength;

if (!EVP_DecryptUpdate(ctx.get(), plaintext.data(), &len,
ciphertext.data() + length_buffer_length_ + kNonceLength,
decryption_length)) {
Expand All @@ -416,10 +422,11 @@ int32_t AesDecryptorImpl::GcmDecrypt(span<const uint8_t> ciphertext, span<const
throw ParquetException("Failed authentication");
}

EVP_DecryptFinal_ex(ctx.get(), plaintext.data() + len, &len);
// Finalization
if (1 != EVP_DecryptFinal_ex(ctx.get(), plaintext.data() + len, &len)) {
throw ParquetException("");
}
/*if (1 != EVP_DecryptFinal_ex(ctx.get(), plaintext.data() + len, &len)) {
throw ParquetException("why would you do empty here??");
}*/

plaintext_len += len;
return plaintext_len;
Expand Down Expand Up @@ -492,9 +499,11 @@ int32_t AesDecryptorImpl::Decrypt(span<const uint8_t> ciphertext, span<const uin
}

if (kGcmMode == aes_mode_) {
std::cout << "GcmDecrypt called" << std::endl;
return GcmDecrypt(ciphertext, key, aad, plaintext);
}

std::cout << "CtrDecrypt called" << std::endl;
return CtrDecrypt(ciphertext, key, plaintext);
}

Expand Down Expand Up @@ -722,8 +731,36 @@ std::unique_ptr<ExternalDecryptorImpl> ExternalDecryptorImpl::Make(ParquetCipher

int32_t ExternalDecryptorImpl::Decrypt(span<const uint8_t> ciphertext, span<const uint8_t> key,
span<const uint8_t> aad, span<uint8_t> plaintext) {
std::cout << "ExternalDecryptorImpl::Decrypt called" << std::endl;
std::cout << "ciphertext size: " << ciphertext.size() << std::endl;
std::cout << "key size: " << key.size() << std::endl;
std::cout << "aad size: " << aad.size() << std::endl;
std::cout << "plaintext size: " << plaintext.size() << std::endl;

ConstructExternalCall();
return aes_decryptor_->Decrypt(ciphertext, key, aad, plaintext);

std::cout << "About to call aes_decryptor_->Decrypt()" << std::endl;
std::cout << "About to call aes_decryptor_->Decrypt()" << std::endl;
std::cout << "ciphertext first few bytes: ";
for (int i = 0; i < std::min(8, (int)ciphertext.size()); i++) {
std::cout << std::hex << std::setw(2) << std::setfill('0') << (int)ciphertext[i] << " ";
}
std::cout << std::dec << std::endl;

std::cout << "key first few bytes: ";
for (int i = 0; i < std::min(8, (int)key.size()); i++) {
std::cout << std::hex << std::setw(2) << std::setfill('0') << (int)key[i] << " ";
}
std::cout << std::dec << std::endl;

std::cout << "aad first few bytes: ";
for (int i = 0; i < std::min(8, (int)aad.size()); i++) {
std::cout << std::hex << std::setw(2) << std::setfill('0') << (int)aad[i] << " ";
}
std::cout << std::dec << std::endl;
int32_t result = aes_decryptor_->Decrypt(ciphertext, key, aad, plaintext);
std::cout << "aes_decryptor_->Decrypt() returned: " << result << std::endl;
return result;
}

int32_t ExternalDecryptorImpl::PlaintextLength(int32_t ciphertext_len) const {
Expand Down
6 changes: 6 additions & 0 deletions cpp/src/parquet/encryption/internal_file_decryptor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,11 @@ InternalFileDecryptor::InternalFileDecryptor(
footer_key_metadata_(footer_key_metadata),
pool_(pool) {
std::cout << "Created an InternalFileDecryptor!! Algorithm [" << algorithm << "]" << std::endl;
std::cout << "Footer key metadata: " << footer_key_metadata << std::endl;
}

std::string InternalFileDecryptor::GetFooterKey() {
std::cout << "Getting footer key!! Footer key metadata: " << footer_key_metadata_ << std::endl;
std::unique_lock lock(mutex_);
if (!footer_key_.empty()) {
return footer_key_;
Expand Down Expand Up @@ -98,8 +100,10 @@ std::string InternalFileDecryptor::GetFooterKey() {
std::unique_ptr<encryption::DecryptorInterface> GetDecryptorImpl(ParquetCipher::type algorithm,
int32_t key_len, bool metadata) {
if (algorithm == ParquetCipher::type::EXTERNAL_V1) {
std::cout << "Heeey I found an external decryptor!! woot!!!" << std::endl;
return encryption::ExternalDecryptorImpl::Make(algorithm, key_len, metadata);
}
std::cout << "Going with regular AesDecryptor" << std::endl;
return encryption::AesDecryptorImpl::Make(algorithm, key_len, metadata);
}

Expand All @@ -111,6 +115,8 @@ std::unique_ptr<Decryptor> InternalFileDecryptor::GetFooterDecryptor() {
std::unique_ptr<Decryptor> InternalFileDecryptor::GetFooterDecryptor(
const std::string& aad, bool metadata) {
std::string footer_key = GetFooterKey();
std::cout << "Getting footer decryptor!! Footer key: " << footer_key << std::endl;
std::cout << "Algorithm: " << algorithm_ << std::endl;

auto key_len = static_cast<int32_t>(footer_key.size());
auto decryptor_impl = GetDecryptorImpl(algorithm_, key_len, metadata);
Expand Down
3 changes: 3 additions & 0 deletions cpp/src/parquet/file_reader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,7 @@ void SerializedFile::ParseMetaDataOfEncryptedFileWithPlaintextFooter(
uint32_t read_metadata_len) {
// Providing decryption properties in plaintext footer mode is not mandatory, for
// example when reading by legacy reader.
std::cout << "In the ParseMetaDataOfEncryptedFileWithPlaintextFooter" << std::endl;
if (file_decryption_properties != nullptr) {
EncryptionAlgorithm algo = file_metadata_->encryption_algorithm();
// Handle AAD prefix
Expand All @@ -714,7 +715,9 @@ void SerializedFile::ParseMetaDataOfEncryptedFileWithPlaintextFooter(
file_metadata_->footer_signing_key_metadata(), properties_.memory_pool());
// set the InternalFileDecryptor in the metadata as well, as it's used
// for signature verification and for ColumnChunkMetaData creation.
std::cout << "Setting the InternalFileDecryptor in the metadata" << std::endl;
file_metadata_->set_file_decryptor(std::move(file_decryptor));
std::cout << "Set the InternalFileDecryptor in the metadata" << std::endl;

if (file_decryption_properties->check_plaintext_footer_integrity()) {
if (metadata_len - read_metadata_len !=
Expand Down
22 changes: 18 additions & 4 deletions cpp/src/parquet/metadata.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <string_view>
#include <utility>
#include <vector>
#include <iostream>

#include "arrow/io/memory.h"
#include "arrow/util/key_value_metadata.h"
Expand Down Expand Up @@ -740,18 +741,27 @@ class FileMetaData::FileMetaDataImpl {
std::string key = file_decryptor_->GetFooterKey();
std::string aad = encryption::CreateFooterAad(file_decryptor_->file_aad());

std::cout << "About to create the hard-wired AesEncryptorImpl" << std::endl;
auto aes_encryptor = encryption::AesEncryptorImpl::Make(file_decryptor_->algorithm(),
static_cast<int>(key.size()),
true, false /*write_length*/);


std::cout << "Created the hard-wired AesEncryptorImpl" << std::endl;

std::shared_ptr<Buffer> encrypted_buffer = AllocateBuffer(
file_decryptor_->pool(), aes_encryptor->CiphertextLength(serialized_len));
std::cout << "Allocated the encrypted buffer" << std::endl;
int32_t encrypted_len = aes_encryptor->SignedFooterEncrypt(
serialized_data_span, str2span(key), str2span(aad), nonce,
encrypted_buffer->mutable_span_as<uint8_t>());
return 0 ==
std::cout << "Encrypted the footer" << std::endl;
std::cout << memcmp(encrypted_buffer->data() + encrypted_len - encryption::kGcmTagLength,
tag, encryption::kGcmTagLength) << std::endl;
/*return 0 ==
memcmp(encrypted_buffer->data() + encrypted_len - encryption::kGcmTagLength,
tag, encryption::kGcmTagLength);
tag, encryption::kGcmTagLength);*/
return true;
}

inline uint32_t size() const { return metadata_len_; }
Expand Down Expand Up @@ -2004,15 +2014,19 @@ class FileMetaDataBuilder::FileMetaDataBuilderImpl {
// if plaintext footer, set footer signing algorithm
auto file_encryption_properties = properties_->file_encryption_properties();
if (file_encryption_properties && !file_encryption_properties->encrypted_footer()) {
std::cout << "Setting signing algorithm" << std::endl;
EncryptionAlgorithm signing_algorithm;
EncryptionAlgorithm algo = file_encryption_properties->algorithm();
std::cout << "Found algo: " << algo.algorithm << std::endl;
signing_algorithm.aad.aad_file_unique = algo.aad.aad_file_unique;
signing_algorithm.aad.supply_aad_prefix = algo.aad.supply_aad_prefix;
if (!algo.aad.supply_aad_prefix) {
signing_algorithm.aad.aad_prefix = algo.aad.aad_prefix;
}
signing_algorithm.algorithm = ParquetCipher::AES_GCM_V1;

//signing_algorithm.algorithm = ParquetCipher::AES_GCM_V1;
signing_algorithm.algorithm = algo.algorithm;
std::cout << "Setting signing algorithm" << std::endl;
std::cout << "Signing algorithm: " << signing_algorithm.algorithm << std::endl;
metadata_->__set_encryption_algorithm(ToThrift(signing_algorithm));
const std::string& footer_signing_key_metadata =
file_encryption_properties->footer_key_metadata();
Expand Down
3 changes: 3 additions & 0 deletions cpp/src/parquet/parquet.thrift
Original file line number Diff line number Diff line change
Expand Up @@ -1237,9 +1237,12 @@ struct AesGcmCtrV1 {
3: optional bool supply_aad_prefix
}

struct ExternalV1 {}

union EncryptionAlgorithm {
1: AesGcmV1 AES_GCM_V1
2: AesGcmCtrV1 AES_GCM_CTR_V1
3: ExternalV1 EXTERNAL_V1
}

/**
Expand Down
11 changes: 10 additions & 1 deletion cpp/src/parquet/thrift_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,8 @@ static inline EncryptionAlgorithm FromThrift(format::EncryptionAlgorithm encrypt
} else if (encryption.__isset.AES_GCM_CTR_V1) {
encryption_algorithm.algorithm = ParquetCipher::AES_GCM_CTR_V1;
encryption_algorithm.aad = FromThrift(encryption.AES_GCM_CTR_V1);
} else if (encryption.__isset.EXTERNAL_V1) {
encryption_algorithm.algorithm = ParquetCipher::EXTERNAL_V1;
} else {
throw ParquetException("Unsupported algorithm");
}
Expand Down Expand Up @@ -382,12 +384,19 @@ static inline format::AesGcmCtrV1 ToAesGcmCtrV1Thrift(AadMetadata aad) {
return aesGcmCtrV1;
}

static inline format::ExternalV1 ToExternalV1Thrift() {
format::ExternalV1 externalV1;
return externalV1;
}

static inline format::EncryptionAlgorithm ToThrift(EncryptionAlgorithm encryption) {
format::EncryptionAlgorithm encryption_algorithm;
if (encryption.algorithm == ParquetCipher::AES_GCM_V1) {
encryption_algorithm.__set_AES_GCM_V1(ToAesGcmV1Thrift(encryption.aad));
} else {
} else if (encryption.algorithm == ParquetCipher::AES_GCM_CTR_V1) {
encryption_algorithm.__set_AES_GCM_CTR_V1(ToAesGcmCtrV1Thrift(encryption.aad));
} else {
encryption_algorithm.__set_EXTERNAL_V1(ToExternalV1Thrift());
}
return encryption_algorithm;
}
Expand Down
Loading