Skip to content

Commit

Permalink
Adding HMAC calculation for column keys
Browse files Browse the repository at this point in the history
  • Loading branch information
ccfelius committed Nov 19, 2024
1 parent 0defac3 commit 8384c8c
Show file tree
Hide file tree
Showing 9 changed files with 185 additions and 83 deletions.
2 changes: 1 addition & 1 deletion src/core/crypto/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ set(EXTENSION_SOURCES
${EXTENSION_SOURCES}
${CMAKE_CURRENT_SOURCE_DIR}/crypto_primitives.cpp
PARENT_SCOPE
)
)
35 changes: 35 additions & 0 deletions src/core/crypto/crypto_primitives.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@
#include "duckdb/common/common.hpp"
#include <stdio.h>

// todo; use httplib for windows compatibility
//#define CPPHTTPLIB_OPENSSL_SUPPORT
//#include "duckdb/third_party/httplib/httplib.hpp"

// OpenSSL functions
#include <openssl/err.h>
#include <openssl/evp.h>
#include <openssl/rand.h>
#include <openssl/hmac.h>

namespace duckdb {

Expand Down Expand Up @@ -190,4 +195,34 @@ extern "C" {
DUCKDB_EXTENSION_API AESStateSSLFactory *CreateSSLFactory() {
return new AESStateSSLFactory();
};
}

namespace simple_encryption {

namespace core {

std::string CalculateHMAC(const std::string &secret, const std::string &message, const uint32_t length) {
const EVP_MD *algorithm = EVP_sha256(); // Replace with EVP_sha1(), EVP_md5(), etc., if needed.
unsigned char key_buffer[32];

// Output buffer and length
unsigned char hmacResult[EVP_MAX_MD_SIZE];
unsigned int hmacLength = 0;

// Compute the HMAC
HMAC(algorithm,
secret.data(), secret.size(), // Key
reinterpret_cast<const unsigned char*>(message.data()), message.size(), // Message
hmacResult, &hmacLength);

// Copy the desired number of bytes
memcpy(key_buffer, hmacResult, length);

// convert to string
std::string result_key(reinterpret_cast<const char*>(key_buffer), length);

return result_key;
}

}
}
1 change: 0 additions & 1 deletion src/core/functions/function_data/encrypt_function_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ EncryptFunctionData::EncryptBind(ClientContext &context,
vector<unique_ptr<Expression>> &arguments) {
// here, implement bound statements?

// do something
return make_uniq<EncryptFunctionData>(context);
}
} // namespace core
Expand Down
Loading

0 comments on commit 8384c8c

Please sign in to comment.