Skip to content

Commit

Permalink
Merge pull request #5 from revital76/Gidon_Fix_key_wipeout
Browse files Browse the repository at this point in the history
Port key erasure mechanism
  • Loading branch information
ggershinsky authored May 20, 2019
2 parents f708bf4 + 9ba0fb5 commit 4d51930
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
14 changes: 10 additions & 4 deletions cpp/src/parquet/util/crypto.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ constexpr int BufferSizeLength = 4;
throw ParquetException("Couldn't init ALG decryption"); \
}

AesEncryptor::AesEncryptor(ParquetCipher::type alg_id, int key_len, bool metadata) {
AesEncryptor::AesEncryptor(ParquetCipher::type alg_id, int key_len, bool metadata,
std::shared_ptr<std::list<AesEncryptor*>> all_encryptors) {
if (all_encryptors != NULLPTR) {
all_encryptors->push_back(this);
}
ctx_ = nullptr;

if (ParquetCipher::AES_GCM_V1 != alg_id && ParquetCipher::AES_GCM_CTR_V1 != alg_id) {
Expand Down Expand Up @@ -233,7 +237,11 @@ int AesEncryptor::Encrypt(const uint8_t* plaintext, int plaintext_len, uint8_t*
return ctr_encrypt(plaintext, plaintext_len, key, key_len, nonce, ciphertext);
}

AesDecryptor::AesDecryptor(ParquetCipher::type alg_id, int key_len, bool metadata) {
AesDecryptor::AesDecryptor(ParquetCipher::type alg_id, int key_len, bool metadata,
std::shared_ptr<std::list<AesDecryptor*>> all_decryptors) {
if (all_decryptors != NULLPTR) {
all_decryptors->push_back(this);
}
ctx_ = nullptr;

if (ParquetCipher::AES_GCM_V1 != alg_id && ParquetCipher::AES_GCM_CTR_V1 != alg_id) {
Expand Down Expand Up @@ -411,7 +419,6 @@ int AesDecryptor::Decrypt(const uint8_t* ciphertext, int ciphertext_len, uint8_t
return ctr_decrypt(ciphertext, ciphertext_len, key, key_len, plaintext);
}


static std::string shortToBytesLE(int16_t input) {
int8_t output[2];
memset(output, 0, 2);
Expand Down Expand Up @@ -458,5 +465,4 @@ void quickUpdatePageAAD(const std::string& AAD, int16_t new_page_ordinal) {
std::memcpy((int16_t*)(const_cast<char*>(AAD.c_str() + length - 2)),
(const int16_t*)(page_ordinal_bytes.c_str()), 2);
}

} // namespace parquet_encryption
23 changes: 15 additions & 8 deletions cpp/src/parquet/util/crypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#ifndef PARQUET_UTIL_CRYPTO_H
#define PARQUET_UTIL_CRYPTO_H

#include <list>
#include <memory>
#include <string>

Expand Down Expand Up @@ -45,7 +46,8 @@ const int8_t OffsetIndex = 7;
class AesEncryptor {
public:
// Can serve one key length only. Possible values: 16, 24, 32 bytes.
AesEncryptor(ParquetCipher::type alg_id, int key_len, bool metadata);
AesEncryptor(ParquetCipher::type alg_id, int key_len, bool metadata,
std::shared_ptr<std::list<AesEncryptor*>> all_encryptors);

// Size different between plaintext and ciphertext, for this cipher.
int CiphertextSizeDelta();
Expand All @@ -62,12 +64,14 @@ class AesEncryptor {
void WipeOut() {
if (NULLPTR != ctx_) {
EVP_CIPHER_CTX_free(ctx_);
ctx_ = NULLPTR;
}
}

~AesEncryptor() {
if (NULLPTR != ctx_) {
EVP_CIPHER_CTX_free(ctx_);
ctx_ = NULLPTR;
}
}

Expand All @@ -87,7 +91,15 @@ class AesEncryptor {
class AesDecryptor {
public:
// Can serve one key length only. Possible values: 16, 24, 32 bytes.
AesDecryptor(ParquetCipher::type alg_id, int key_len, bool metadata);
AesDecryptor(ParquetCipher::type alg_id, int key_len, bool metadata,
std::shared_ptr<std::list<AesDecryptor*>> all_decryptors);

void WipeOut() {
if (NULLPTR != ctx_) {
EVP_CIPHER_CTX_free(ctx_);
ctx_ = NULLPTR;
}
}

// Size different between plaintext and ciphertext, for this cipher.
int CiphertextSizeDelta();
Expand All @@ -97,15 +109,10 @@ class AesDecryptor {
int Decrypt(const uint8_t* ciphertext, int ciphertext_len, uint8_t* key, int key_len,
uint8_t* aad, int aad_len, uint8_t* plaintext);

void WipeOut() {
if (NULLPTR != ctx_) {
EVP_CIPHER_CTX_free(ctx_);
}
}

~AesDecryptor() {
if (NULLPTR != ctx_) {
EVP_CIPHER_CTX_free(ctx_);
ctx_ = NULLPTR;
}
}

Expand Down

0 comments on commit 4d51930

Please sign in to comment.