szaszm commented on a change in pull request #1090:
URL: https://github.com/apache/nifi-minifi-cpp/pull/1090#discussion_r664729323



##########
File path: extensions/rocksdb-repos/encryption/RocksDbEncryptionProvider.cpp
##########
@@ -0,0 +1,119 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "RocksDbEncryptionProvider.h"
+#include "utils/crypto/ciphers/Aes256Ecb.h"
+#include "logging/LoggerConfiguration.h"
+
+namespace org {
+namespace apache {
+namespace nifi {
+namespace minifi {
+namespace core {
+namespace repository {
+
+using utils::crypto::Bytes;
+using utils::crypto::Aes256EcbCipher;
+
+class AES256BlockCipher final : public rocksdb::BlockCipher {
+  static std::shared_ptr<logging::Logger> logger_;
+ public:
+  AES256BlockCipher(std::string database, Aes256EcbCipher cipher_impl)
+      : database_(std::move(database)),
+        cipher_impl_(std::move(cipher_impl)) {}
+
+  const char *Name() const override {
+    return "AES256BlockCipher";
+  }
+
+  size_t BlockSize() override {
+    return Aes256EcbCipher::BLOCK_SIZE;
+  }
+
+  bool hasEqualKey(const AES256BlockCipher& other) const {
+    return cipher_impl_.hasEqualKey(other.cipher_impl_);
+  }
+
+  rocksdb::Status Encrypt(char *data) override;
+
+  rocksdb::Status Decrypt(char *data) override;
+
+ private:
+  const std::string database_;
+  const Aes256EcbCipher cipher_impl_;
+};
+
+class EncryptingEnv : public rocksdb::EnvWrapper {
+ public:
+  EncryptingEnv(Env* target, std::shared_ptr<AES256BlockCipher> cipher) : 
EnvWrapper(target), env_(target), cipher_(std::move(cipher)) {}
+
+  bool hasEqualKey(const EncryptingEnv& other) const {
+    return cipher_->hasEqualKey(*other.cipher_);
+  }
+
+ private:
+  std::unique_ptr<Env> env_;
+  std::shared_ptr<AES256BlockCipher> cipher_;
+};
+
+std::shared_ptr<logging::Logger> AES256BlockCipher::logger_ = 
logging::LoggerFactory<AES256BlockCipher>::getLogger();
+
+std::shared_ptr<rocksdb::Env> createEncryptingEnv(const 
utils::crypto::EncryptionManager& manager, const DbEncryptionOptions& options) {
+  auto cipher_impl = 
manager.createAes256EcbCipher(options.encryption_key_name);

Review comment:
       Back to the first question, you mentioned CTR (a.k.a counter mode 
encryption) in a different answer. This makes me think that we are just using 
ECB functions to encrypt the nonce + counter as a block, but not really using 
ECB mode encryption. Am I correct with this assumption?
   
   Here's the rocksdb CTR function: 
https://github.com/facebook/rocksdb/commit/51778612c9cf4cb842eed10f270ec0ed29ff22f1#diff-31e5f5f565771d096323f879c644c2792db9bf42018b12ca0441f09b2c876b39R803
   
   Second (authentication): I agree, I can't see anything related to message 
authentication codes in the above commit or the latest rocksdb 
env/env_encryption* source files. This is a pity, because an attacker can 
easily flip bits in the plaintext by just flipping the corresponding bits in 
the ciphertext.
   
   disclaimer: I'm by no means an crypto expert, just researched the topic 
while reviewing this PR.

##########
File path: extensions/rocksdb-repos/encryption/RocksDbEncryptionProvider.cpp
##########
@@ -0,0 +1,119 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "RocksDbEncryptionProvider.h"
+#include "utils/crypto/ciphers/Aes256Ecb.h"
+#include "logging/LoggerConfiguration.h"
+
+namespace org {
+namespace apache {
+namespace nifi {
+namespace minifi {
+namespace core {
+namespace repository {
+
+using utils::crypto::Bytes;
+using utils::crypto::Aes256EcbCipher;
+
+class AES256BlockCipher final : public rocksdb::BlockCipher {
+  static std::shared_ptr<logging::Logger> logger_;
+ public:
+  AES256BlockCipher(std::string database, Aes256EcbCipher cipher_impl)
+      : database_(std::move(database)),
+        cipher_impl_(std::move(cipher_impl)) {}
+
+  const char *Name() const override {
+    return "AES256BlockCipher";
+  }
+
+  size_t BlockSize() override {
+    return Aes256EcbCipher::BLOCK_SIZE;
+  }
+
+  bool hasEqualKey(const AES256BlockCipher& other) const {
+    return cipher_impl_.hasEqualKey(other.cipher_impl_);
+  }
+
+  rocksdb::Status Encrypt(char *data) override;
+
+  rocksdb::Status Decrypt(char *data) override;
+
+ private:
+  const std::string database_;
+  const Aes256EcbCipher cipher_impl_;
+};
+
+class EncryptingEnv : public rocksdb::EnvWrapper {
+ public:
+  EncryptingEnv(Env* target, std::shared_ptr<AES256BlockCipher> cipher) : 
EnvWrapper(target), env_(target), cipher_(std::move(cipher)) {}
+
+  bool hasEqualKey(const EncryptingEnv& other) const {
+    return cipher_->hasEqualKey(*other.cipher_);
+  }
+
+ private:
+  std::unique_ptr<Env> env_;
+  std::shared_ptr<AES256BlockCipher> cipher_;
+};
+
+std::shared_ptr<logging::Logger> AES256BlockCipher::logger_ = 
logging::LoggerFactory<AES256BlockCipher>::getLogger();
+
+std::shared_ptr<rocksdb::Env> createEncryptingEnv(const 
utils::crypto::EncryptionManager& manager, const DbEncryptionOptions& options) {
+  auto cipher_impl = 
manager.createAes256EcbCipher(options.encryption_key_name);

Review comment:
       Back to the first question, you mentioned CTR (a.k.a counter mode 
encryption) in a different answer. This makes me think that we are just using 
ECB functions to encrypt the nonce + counter as a block, but not really using 
ECB mode encryption. Am I correct with this assumption?
   
   Here's the rocksdb CTR function: 
https://github.com/facebook/rocksdb/commit/51778612c9cf4cb842eed10f270ec0ed29ff22f1#diff-31e5f5f565771d096323f879c644c2792db9bf42018b12ca0441f09b2c876b39R803
   
   Second (authentication): I agree, I can't see anything related to message 
authentication codes in the above commit or the latest rocksdb 
env/env_encryption* source files. This is a pity, because an attacker can 
easily flip bits in the plaintext by just flipping the corresponding bits in 
the ciphertext.
   
   disclaimer: I'm by no means a crypto expert, just researched the topic while 
reviewing this PR.




-- 
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: issues-unsubscr...@nifi.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to