generated from duckdb/extension-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
change AESGCMstate to AESState for expansion
- Loading branch information
Showing
9 changed files
with
275 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,9 @@ | ||
# This file is included by DuckDB's build system. It specifies which extension to load | ||
|
||
# Extension from this repo | ||
duckdb_extension_load(simple_encryption | ||
LOAD_TEST | ||
SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR} | ||
) | ||
|
||
# Load httpfs extension | ||
duckdb_extension_load(httpfs) | ||
|
||
# Any extra extensions that should be built | ||
# e.g.: duckdb_extension_load(json) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
set(EXTENSION_SOURCES | ||
${EXTENSION_SOURCES} | ||
${CMAKE_CURRENT_SOURCE_DIR}/crypto_primitives.cpp | ||
PARENT_SCOPE | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
#include "simple_encryption/core/crypto/crypto_primitives.hpp" | ||
#include "mbedtls_wrapper.hpp" | ||
#include <iostream> | ||
#include "duckdb/common/common.hpp" | ||
#include <stdio.h> | ||
|
||
// OpenSSL functions | ||
#include <openssl/err.h> | ||
#include <openssl/evp.h> | ||
#include <openssl/rand.h> | ||
|
||
namespace duckdb { | ||
|
||
void sha256(const char *in, size_t in_len, hash_bytes &out) { | ||
duckdb_mbedtls::MbedTlsWrapper::ComputeSha256Hash(in, in_len, (char *)out); | ||
} | ||
|
||
void hmac256(const std::string &message, const char *secret, size_t secret_len, hash_bytes &out) { | ||
duckdb_mbedtls::MbedTlsWrapper::Hmac256(secret, secret_len, message.data(), message.size(), (char *)out); | ||
} | ||
|
||
void hmac256(std::string message, hash_bytes secret, hash_bytes &out) { | ||
hmac256(message, (char *)secret, sizeof(hash_bytes), out); | ||
} | ||
|
||
void hex256(hash_bytes &in, hash_str &out) { | ||
const char *hex = "0123456789abcdef"; | ||
unsigned char *pin = in; | ||
unsigned char *pout = out; | ||
for (; pin < in + sizeof(in); pout += 2, pin++) { | ||
pout[0] = hex[(*pin >> 4) & 0xF]; | ||
pout[1] = hex[*pin & 0xF]; | ||
} | ||
} | ||
|
||
const EVP_CIPHER *GetCipher(const string &key) { | ||
// For now, we only support GCM ciphers | ||
switch (key.size()) { | ||
case 16: | ||
return EVP_aes_128_gcm(); | ||
case 24: | ||
return EVP_aes_192_gcm(); | ||
case 32: | ||
return EVP_aes_256_gcm(); | ||
default: | ||
throw InternalException("Invalid AES key length"); | ||
} | ||
} | ||
|
||
AESStateSSL::AESStateSSL() : gcm_context(EVP_CIPHER_CTX_new()) { | ||
if (!(gcm_context)) { | ||
throw InternalException("AES GCM failed with initializing context"); | ||
} | ||
} | ||
|
||
AESStateSSL::~AESStateSSL() { | ||
// Clean up | ||
EVP_CIPHER_CTX_free(gcm_context); | ||
} | ||
|
||
bool AESStateSSL::IsOpenSSL() { | ||
return ssl; | ||
} | ||
|
||
void AESStateSSL::GenerateRandomData(data_ptr_t data, idx_t len) { | ||
// generate random bytes for nonce | ||
RAND_bytes(data, len); | ||
} | ||
|
||
void AESStateSSL::InitializeEncryption(const_data_ptr_t iv, idx_t iv_len, const string *key) { | ||
mode = ENCRYPT; | ||
|
||
if (1 != EVP_EncryptInit_ex(gcm_context, GetCipher(*key), NULL, const_data_ptr_cast(key->data()), iv)) { | ||
throw InternalException("EncryptInit failed"); | ||
} | ||
} | ||
|
||
void AESStateSSL::InitializeDecryption(const_data_ptr_t iv, idx_t iv_len, const string *key) { | ||
mode = DECRYPT; | ||
|
||
if (1 != EVP_DecryptInit_ex(gcm_context, GetCipher(*key), NULL, const_data_ptr_cast(key->data()), iv)) { | ||
throw InternalException("DecryptInit failed"); | ||
} | ||
} | ||
|
||
size_t AESStateSSL::Process(const_data_ptr_t in, idx_t in_len, data_ptr_t out, idx_t out_len) { | ||
|
||
switch (mode) { | ||
case ENCRYPT: | ||
if (1 != EVP_EncryptUpdate(gcm_context, data_ptr_cast(out), reinterpret_cast<int *>(&out_len), | ||
const_data_ptr_cast(in), (int)in_len)) { | ||
throw InternalException("Encryption failed at OpenSSL EVP_EncryptUpdate"); | ||
} | ||
break; | ||
|
||
case DECRYPT: | ||
if (1 != EVP_DecryptUpdate(gcm_context, data_ptr_cast(out), reinterpret_cast<int *>(&out_len), | ||
const_data_ptr_cast(in), (int)in_len)) { | ||
|
||
throw InternalException("Decryption failed at OpenSSL EVP_DecryptUpdate"); | ||
} | ||
break; | ||
} | ||
|
||
if (out_len != in_len) { | ||
throw InternalException("AES GCM failed, in- and output lengths differ"); | ||
} | ||
|
||
return out_len; | ||
} | ||
|
||
size_t AESStateSSL::Finalize(data_ptr_t out, idx_t out_len, data_ptr_t tag, idx_t tag_len) { | ||
auto text_len = out_len; | ||
|
||
switch (mode) { | ||
case ENCRYPT: | ||
if (1 != EVP_EncryptFinal_ex(gcm_context, data_ptr_cast(out) + out_len, reinterpret_cast<int *>(&out_len))) { | ||
throw InternalException("EncryptFinal failed"); | ||
} | ||
text_len += out_len; | ||
// The computed tag is written at the end of a chunk | ||
if (1 != EVP_CIPHER_CTX_ctrl(gcm_context, EVP_CTRL_GCM_GET_TAG, tag_len, tag)) { | ||
throw InternalException("Calculating the tag failed"); | ||
} | ||
return text_len; | ||
case DECRYPT: | ||
// Set expected tag value | ||
if (!EVP_CIPHER_CTX_ctrl(gcm_context, EVP_CTRL_GCM_SET_TAG, tag_len, tag)) { | ||
throw InternalException("Finalizing tag failed"); | ||
} | ||
// EVP_DecryptFinal() will return an error code if final block is not correctly formatted. | ||
int ret = EVP_DecryptFinal_ex(gcm_context, data_ptr_cast(out) + out_len, reinterpret_cast<int *>(&out_len)); | ||
text_len += out_len; | ||
|
||
if (ret > 0) { | ||
// success | ||
return text_len; | ||
} | ||
throw InvalidInputException("Computed AES tag differs from read AES tag, are you using the right key?"); | ||
} | ||
} | ||
Check warning on line 141 in src/core/crypto/crypto_primitives.cpp GitHub Actions / Build extension binaries / Windows (windows_amd64, x64-windows-static-md)
Check warning on line 141 in src/core/crypto/crypto_primitives.cpp GitHub Actions / Build extension binaries / Windows (windows_amd64, x64-windows-static-md)
Check warning on line 141 in src/core/crypto/crypto_primitives.cpp GitHub Actions / Build extension binaries / Windows (windows_amd64, x64-windows-static-md)
Check warning on line 141 in src/core/crypto/crypto_primitives.cpp GitHub Actions / Build extension binaries / Windows (windows_amd64, x64-windows-static-md)
Check warning on line 141 in src/core/crypto/crypto_primitives.cpp GitHub Actions / Build extension binaries / Windows (windows_amd64_rtools, x64-mingw-static)
|
||
|
||
} // namespace duckdb | ||
|
||
extern "C" { | ||
|
||
// Call the member function through the factory object | ||
DUCKDB_EXTENSION_API AESStateSSLFactory *CreateSSLFactory() { | ||
return new AESStateSSLFactory(); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.