EnricoMi commented on code in PR #44990:
URL: https://github.com/apache/arrow/pull/44990#discussion_r1880694658


##########
cpp/src/parquet/encryption/encryption_internal.cc:
##########
@@ -52,25 +52,85 @@ constexpr int32_t kBufferSizeLength = 4;
     throw ParquetException("Couldn't init ALG decryption");           \
   }
 
-class AesEncryptor::AesEncryptorImpl {
+class AesEncryptionContext {
+ public:
+  AesEncryptionContext(ParquetCipher::type alg_id, int32_t key_len, bool 
metadata,
+                       bool write_length) {
+    openssl::EnsureInitialized();
+
+    length_buffer_length_ = write_length ? kBufferSizeLength : 0;
+    ciphertext_size_delta_ = length_buffer_length_ + kNonceLength;
+    if (metadata || (ParquetCipher::AES_GCM_V1 == alg_id)) {
+      aes_mode_ = kGcmMode;
+      ciphertext_size_delta_ += kGcmTagLength;
+    } else {
+      aes_mode_ = kCtrMode;
+    }
+
+    if (16 != key_len && 24 != key_len && 32 != key_len) {
+      std::stringstream ss;
+      ss << "Wrong key length: " << key_len;
+      throw ParquetException(ss.str());
+    }
+
+    key_length_ = key_len;
+  };
+
+  virtual ~AesEncryptionContext() = default;
+
+ protected:
+  void InitCipherContext() {
+    if (ctx_) return;
+
+    ctx_ = std::unique_ptr<EVP_CIPHER_CTX, 
decltype(ctxDeleter)>(EVP_CIPHER_CTX_new(),
+                                                                 ctxDeleter);
+    if (!ctx_) throw ParquetException("Couldn't init cipher context");
+    InitCipherContext(ctx_.get());
+  }
+
+  virtual void InitCipherContext(EVP_CIPHER_CTX* ctx) = 0;
+
+  std::function<void(EVP_CIPHER_CTX*)> ctxDeleter = [](EVP_CIPHER_CTX* ctx) {
+    EVP_CIPHER_CTX_free(ctx);
+  };
+
+  /// Create a new cipher context that auto-frees
+  /// This duplicates un unused but initialized private context to avoid going 
through
+  /// initialization

Review Comment:
   Took some measurements:
   - creating, initializing and freeing a contexts takes 1.7µs
   - duplicating and freeing a context takes 1.2µs
   
   This was measured over 10,000,000 instances (17s and 12s).
   
   I'll remove complexity given creating a context takes µs.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to