Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 37 additions & 11 deletions src/Functions/FunctionsAES.h
Original file line number Diff line number Diff line change
Expand Up @@ -586,16 +586,29 @@ class FunctionEncrypt final : public IFunction
if constexpr (mode == CipherMode::RFC5116_AEAD_AES_GCM)
{
// 1.a.1: Init CTX with custom IV length and optionally with AAD
if (EVP_EncryptInit_ex(evp_ctx, evp_cipher, nullptr, nullptr, nullptr) != 1)
if (!ctx_key_state.hasContext() && EVP_EncryptInit_ex(evp_ctx, evp_cipher, nullptr, nullptr, nullptr) != 1)
onError("EVP_EncryptInit_ex");

if (EVP_CIPHER_CTX_ctrl(evp_ctx, EVP_CTRL_AEAD_SET_IVLEN, safe_cast<int>(iv_value.size()), nullptr) != 1)
onError("EVP_CIPHER_CTX_ctrl");

if (EVP_EncryptInit_ex(evp_ctx, nullptr, nullptr,
reinterpret_cast<const unsigned char*>(key_value.data()),
reinterpret_cast<const unsigned char*>(iv_value.data())) != 1)
onError("EVP_EncryptInit_ex");
if (ctx_key_state.matches(key_value))
{
/// The context already holds the expanded key schedule for this
/// key: reset only the IV, skipping the cipher setup and key
/// schedule expansion that dominate the per-row cost in OpenSSL 3.x.
if (EVP_EncryptInit_ex(evp_ctx, nullptr, nullptr, nullptr,
reinterpret_cast<const unsigned char*>(iv_value.data())) != 1)
onError("EVP_EncryptInit_ex");
}
else
{
if (EVP_EncryptInit_ex(evp_ctx, nullptr, nullptr,
reinterpret_cast<const unsigned char*>(key_value.data()),
reinterpret_cast<const unsigned char*>(iv_value.data())) != 1)
onError("EVP_EncryptInit_ex");
ctx_key_state.remember(key_value);
}

// 1.a.2 Set AAD
if (aad_column)
Expand Down Expand Up @@ -1025,7 +1038,7 @@ class FunctionDecrypt final : public IFunction
auto * decrypted = decrypted_result_column_data.data();

KeyHolder<mode> key_holder{};
[[maybe_unused]] CachedKeyState ctx_key_state;
CachedKeyState ctx_key_state;

for (size_t row_idx = 0; row_idx < input_rows_count; ++row_idx)
{
Expand Down Expand Up @@ -1078,18 +1091,31 @@ class FunctionDecrypt final : public IFunction
// 1: Init CTX
if constexpr (mode == CipherMode::RFC5116_AEAD_AES_GCM)
{
if (EVP_DecryptInit_ex(evp_ctx, evp_cipher, nullptr, nullptr, nullptr) != 1)
if (!ctx_key_state.hasContext() && EVP_DecryptInit_ex(evp_ctx, evp_cipher, nullptr, nullptr, nullptr) != 1)
onError("EVP_DecryptInit_ex");

// 1.a.1 : Set custom IV length
if (EVP_CIPHER_CTX_ctrl(evp_ctx, EVP_CTRL_AEAD_SET_IVLEN, safe_cast<int>(iv_value.size()), nullptr) != 1)
onError("EVP_CIPHER_CTX_ctrl");

// 1.a.1 : Init CTX with key and IV
if (EVP_DecryptInit_ex(evp_ctx, nullptr, nullptr,
reinterpret_cast<const unsigned char*>(key_value.data()),
reinterpret_cast<const unsigned char*>(iv_value.data())) != 1)
onError("EVP_DecryptInit_ex");
if (ctx_key_state.matches(key_value))
{
/// The context already holds the expanded key schedule for this
/// key: reset only the IV, skipping the cipher setup and key
/// schedule expansion that dominate the per-row cost in OpenSSL 3.x.
if (EVP_DecryptInit_ex(evp_ctx, nullptr, nullptr, nullptr,
reinterpret_cast<const unsigned char*>(iv_value.data())) != 1)
onError("EVP_DecryptInit_ex");
}
else
{
if (EVP_DecryptInit_ex(evp_ctx, nullptr, nullptr,
reinterpret_cast<const unsigned char*>(key_value.data()),
reinterpret_cast<const unsigned char*>(iv_value.data())) != 1)
onError("EVP_DecryptInit_ex");
ctx_key_state.remember(key_value);
}

// 1.a.2: Set AAD if present
if (aad_column)
Expand Down
8 changes: 8 additions & 0 deletions tests/performance/encrypt_decrypt.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,29 @@
<value>decrypt('aes-128-cbc', encrypt('aes-128-cbc', materialize(plaintext), key16, iv16), key16, iv16)</value>
<value>decrypt('aes-128-gcm', encrypt('aes-128-gcm', materialize(plaintext), key16, iv12), key16, iv12)</value>

<!-- Constant key + constant IV + AAD: GCM encrypt -->
<value>encrypt('aes-128-gcm', materialize(plaintext), key16, iv12, 'some aad')</value>

<!-- Variable key, constant IV: encrypt (key changes per row, full init per row) -->
<value>encrypt('aes-128-cbc', materialize(plaintext), randomString(16), iv16)</value>
<value>encrypt('aes-128-ecb', materialize(plaintext), randomString(16))</value>
<value>encrypt('aes-128-gcm', materialize(plaintext), randomString(16), iv12)</value>

<!-- Alternating keys: every row misses the key schedule cache -->
<value>encrypt('aes-128-cbc', materialize(plaintext), concat(repeat('k', 15), toString(number % 2)), iv16)</value>
<value>encrypt('aes-128-gcm', materialize(plaintext), concat(repeat('k', 15), toString(number % 2)), iv12)</value>

<!-- Constant key, variable IV: encrypt (IV changes per row, only the IV is reset) -->
<value>encrypt('aes-128-cbc', materialize(plaintext), key16, randomString(16))</value>
<value>encrypt('aes-128-gcm', materialize(plaintext), key16, randomString(12))</value>

<!-- Variable key / variable IV round trips (deterministic per-row values,
so decrypt sees the same key/IV as encrypt) -->
<value>decrypt('aes-128-cbc', encrypt('aes-128-cbc', materialize(plaintext), vkey16, iv16), vkey16, iv16)</value>
<value>decrypt('aes-128-ecb', encrypt('aes-128-ecb', materialize(plaintext), vkey16), vkey16)</value>
<value>decrypt('aes-128-cbc', encrypt('aes-128-cbc', materialize(plaintext), key16, viv16), key16, viv16)</value>
<value>decrypt('aes-128-gcm', encrypt('aes-128-gcm', materialize(plaintext), vkey16, iv12), vkey16, iv12)</value>
<value>decrypt('aes-128-gcm', encrypt('aes-128-gcm', materialize(plaintext), key16, viv16), key16, viv16)</value>
</values>
</substitution>
<substitution>
Expand Down
Loading
Loading