forked from apache/arrow
-
Notifications
You must be signed in to change notification settings - Fork 26
DX-108149: Add support for CBC encryption mode #104
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
timhurskidremio
merged 3 commits into
dremio:dremio_26.1_18.1.0
from
timhurskidremio:add-support-for-cbc-encryption-mode
Nov 26, 2025
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,81 @@ | ||
| // 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 "gandiva/encrypt_mode_dispatcher.h" | ||
| #include "gandiva/encrypt_utils_ecb.h" | ||
| #include "gandiva/encrypt_utils_cbc.h" | ||
| #include "arrow/util/string.h" | ||
| #include <string> | ||
| #include <sstream> | ||
| #include <stdexcept> | ||
|
|
||
| namespace gandiva { | ||
|
|
||
| int32_t EncryptModeDispatcher::encrypt( | ||
| const char* plaintext, int32_t plaintext_len, const char* key, | ||
| int32_t key_len, const char* mode, int32_t mode_len, const char* iv, | ||
| int32_t iv_len, const char* fifth_argument, int32_t fifth_argument_len, | ||
| unsigned char* cipher) { | ||
| std::string mode_str = | ||
| arrow::internal::AsciiToUpper(std::string_view(mode, mode_len)); | ||
|
|
||
| if (mode_str == "AES-ECB") { | ||
| return aes_encrypt_ecb(plaintext, plaintext_len, key, key_len, cipher); | ||
| } else if (mode_str == "AES-CBC-PKCS7") { | ||
| return aes_encrypt_cbc(plaintext, plaintext_len, key, key_len, | ||
| iv, iv_len, true, cipher); | ||
| } else if (mode_str == "AES-CBC-NONE") { | ||
| return aes_encrypt_cbc(plaintext, plaintext_len, key, key_len, | ||
| iv, iv_len, false, cipher); | ||
| } else if (mode_str == "AES-GCM") { | ||
| throw std::runtime_error("AES-GCM encryption mode is not yet implemented"); | ||
| } else { | ||
| std::ostringstream oss; | ||
| oss << "Unsupported encryption mode: " << mode_str | ||
| << ". Supported modes: AES-ECB, AES-CBC-PKCS7, AES-CBC-NONE"; | ||
| throw std::runtime_error(oss.str()); | ||
| } | ||
| } | ||
|
|
||
| int32_t EncryptModeDispatcher::decrypt( | ||
| const char* ciphertext, int32_t ciphertext_len, const char* key, | ||
| int32_t key_len, const char* mode, int32_t mode_len, const char* iv, | ||
| int32_t iv_len, const char* fifth_argument, int32_t fifth_argument_len, | ||
| unsigned char* plaintext) { | ||
| std::string mode_str = | ||
| arrow::internal::AsciiToUpper(std::string_view(mode, mode_len)); | ||
|
|
||
| if (mode_str == "AES-ECB") { | ||
| return aes_decrypt_ecb(ciphertext, ciphertext_len, key, key_len, plaintext); | ||
| } else if (mode_str == "AES-CBC-PKCS7") { | ||
| return aes_decrypt_cbc(ciphertext, ciphertext_len, key, key_len, | ||
| iv, iv_len, true, plaintext); | ||
| } else if (mode_str == "AES-CBC-NONE") { | ||
| return aes_decrypt_cbc(ciphertext, ciphertext_len, key, key_len, | ||
| iv, iv_len, false, plaintext); | ||
| } else if (mode_str == "AES-GCM") { | ||
| throw std::runtime_error("AES-GCM decryption mode is not yet implemented"); | ||
| } else { | ||
| std::ostringstream oss; | ||
| oss << "Unsupported decryption mode: " << mode_str | ||
| << ". Supported modes: AES-ECB, AES-CBC-PKCS7, AES-CBC-NONE"; | ||
| throw std::runtime_error(oss.str()); | ||
| } | ||
| } | ||
|
|
||
| } // namespace gandiva | ||
|
|
||
This file contains hidden or 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,83 @@ | ||
| // 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. | ||
|
|
||
| #ifndef GANDIVA_ENCRYPT_MODE_DISPATCHER_H | ||
| #define GANDIVA_ENCRYPT_MODE_DISPATCHER_H | ||
|
|
||
| #include <cstdint> | ||
|
|
||
| namespace gandiva { | ||
|
|
||
| /** | ||
| * Dispatcher for AES encryption/decryption based on mode string. | ||
| * Routes calls to appropriate implementation. | ||
| */ | ||
| class EncryptModeDispatcher { | ||
| public: | ||
| /** | ||
| * Encrypt data using the specified mode | ||
| * | ||
| * @param plaintext The data to encrypt | ||
| * @param plaintext_len Length of plaintext in bytes | ||
| * @param key The encryption key | ||
| * @param key_len Length of key in bytes | ||
| * @param mode Mode string | ||
| * @param mode_len Length of mode string in bytes | ||
| * @param iv The initialization vector (optional, only for modes that support it) | ||
| * @param iv_len Length of the IV in bytes | ||
| * @param fifth_argument Additional parameter (optional, only for modes that support it) | ||
| * @param fifth_argument_len Length of fifth_argument in bytes | ||
| * @param cipher Output buffer for encrypted data | ||
| * @return Length of encrypted data in bytes | ||
| * @throws std::runtime_error on encryption failure or unsupported mode | ||
| */ | ||
| static int32_t encrypt(const char* plaintext, int32_t plaintext_len, | ||
| const char* key, int32_t key_len, | ||
| const char* mode, int32_t mode_len, | ||
| const char* iv, int32_t iv_len, | ||
| const char* fifth_argument, int32_t fifth_argument_len, | ||
| unsigned char* cipher); | ||
|
|
||
| /** | ||
| * Decrypt data using the specified mode | ||
| * | ||
| * @param ciphertext The data to decrypt | ||
| * @param ciphertext_len Length of ciphertext in bytes | ||
| * @param key The decryption key | ||
| * @param key_len Length of key in bytes | ||
| * @param mode Mode string | ||
| * @param mode_len Length of mode string in bytes | ||
| * @param iv The initialization vector (optional, only for modes that support it) | ||
| * @param iv_len Length of the IV in bytes | ||
| * @param fifth_argument Additional parameter (optional, only for modes that support it) | ||
| * @param fifth_argument_len Length of fifth_argument in bytes | ||
| * @param plaintext Output buffer for decrypted data | ||
| * @return Length of decrypted data in bytes | ||
| * @throws std::runtime_error on decryption failure or unsupported mode | ||
| */ | ||
| static int32_t decrypt(const char* ciphertext, int32_t ciphertext_len, | ||
| const char* key, int32_t key_len, | ||
| const char* mode, int32_t mode_len, | ||
| const char* iv, int32_t iv_len, | ||
| const char* fifth_argument, int32_t fifth_argument_len, | ||
| unsigned char* plaintext); | ||
| }; | ||
|
|
||
| } // namespace gandiva | ||
|
|
||
| #endif // GANDIVA_ENCRYPT_MODE_DISPATCHER_H | ||
|
|
This file contains hidden or 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,169 @@ | ||
| // 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 "gandiva/encrypt_utils_cbc.h" | ||
| #include "gandiva/encrypt_utils_common.h" | ||
| #include <openssl/aes.h> | ||
| #include <openssl/err.h> | ||
| #include <stdexcept> | ||
| #include <cstring> | ||
| #include <sstream> | ||
| #include <cctype> | ||
|
|
||
| namespace gandiva { | ||
|
|
||
| namespace { | ||
|
|
||
| const EVP_CIPHER* get_cbc_cipher_algo(int32_t key_length) { | ||
| switch (key_length) { | ||
| case 16: | ||
| return EVP_aes_128_cbc(); | ||
| case 24: | ||
| return EVP_aes_192_cbc(); | ||
| case 32: | ||
| return EVP_aes_256_cbc(); | ||
| default: { | ||
| std::ostringstream oss; | ||
| oss << "Unsupported key length for AES-CBC: " << key_length | ||
| << " bytes. Supported lengths: 16, 24, 32 bytes"; | ||
| throw std::runtime_error(oss.str()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| GANDIVA_EXPORT | ||
| int32_t aes_encrypt_cbc(const char* plaintext, int32_t plaintext_len, const char* key, | ||
| int32_t key_len, const char* iv, int32_t iv_len, | ||
| bool use_padding, unsigned char* cipher) { | ||
| // Validate IV length | ||
| if (iv_len != 16) { | ||
| std::ostringstream oss; | ||
| oss << "Invalid IV length for AES-CBC: " << iv_len | ||
| << " bytes. IV must be exactly 16 bytes"; | ||
| throw std::runtime_error(oss.str()); | ||
| } | ||
|
|
||
| int32_t cipher_len = 0; | ||
| int32_t len = 0; | ||
| EVP_CIPHER_CTX* en_ctx = EVP_CIPHER_CTX_new(); | ||
| const EVP_CIPHER* cipher_algo = get_cbc_cipher_algo(key_len); | ||
|
|
||
| if (!en_ctx) { | ||
| throw std::runtime_error("Could not create EVP cipher context for encryption: " + | ||
| get_openssl_error_string()); | ||
| } | ||
|
|
||
| if (!EVP_EncryptInit_ex(en_ctx, cipher_algo, nullptr, | ||
| reinterpret_cast<const unsigned char*>(key), | ||
| reinterpret_cast<const unsigned char*>(iv))) { | ||
| EVP_CIPHER_CTX_free(en_ctx); | ||
| throw std::runtime_error("Could not initialize EVP cipher context for encryption: " + | ||
| get_openssl_error_string()); | ||
| } | ||
|
|
||
| int padding_flag = use_padding ? 1 : 0; | ||
| if (!EVP_CIPHER_CTX_set_padding(en_ctx, padding_flag)) { | ||
| EVP_CIPHER_CTX_free(en_ctx); | ||
| throw std::runtime_error("Could not set padding mode for encryption: " + | ||
| get_openssl_error_string()); | ||
| } | ||
|
|
||
| if (!EVP_EncryptUpdate(en_ctx, cipher, &len, | ||
| reinterpret_cast<const unsigned char*>(plaintext), | ||
| plaintext_len)) { | ||
| EVP_CIPHER_CTX_free(en_ctx); | ||
| throw std::runtime_error("Could not update EVP cipher context for encryption: " + | ||
| get_openssl_error_string()); | ||
| } | ||
|
|
||
| cipher_len += len; | ||
|
|
||
| if (!EVP_EncryptFinal_ex(en_ctx, cipher + len, &len)) { | ||
| EVP_CIPHER_CTX_free(en_ctx); | ||
| throw std::runtime_error("Could not finalize EVP cipher context for encryption: " + | ||
| get_openssl_error_string()); | ||
| } | ||
|
|
||
| cipher_len += len; | ||
|
|
||
| EVP_CIPHER_CTX_free(en_ctx); | ||
| return cipher_len; | ||
| } | ||
|
|
||
| GANDIVA_EXPORT | ||
| int32_t aes_decrypt_cbc(const char* ciphertext, int32_t ciphertext_len, const char* key, | ||
| int32_t key_len, const char* iv, int32_t iv_len, | ||
| bool use_padding, unsigned char* plaintext) { | ||
| // Validate IV length | ||
| if (iv_len != 16) { | ||
| std::ostringstream oss; | ||
| oss << "Invalid IV length for AES-CBC: " << iv_len | ||
| << " bytes. IV must be exactly 16 bytes"; | ||
| throw std::runtime_error(oss.str()); | ||
| } | ||
|
|
||
| int32_t plaintext_len = 0; | ||
| int32_t len = 0; | ||
| EVP_CIPHER_CTX* de_ctx = EVP_CIPHER_CTX_new(); | ||
| const EVP_CIPHER* cipher_algo = get_cbc_cipher_algo(key_len); | ||
|
|
||
| if (!de_ctx) { | ||
| throw std::runtime_error("Could not create EVP cipher context for decryption: " + | ||
| get_openssl_error_string()); | ||
| } | ||
|
|
||
| if (!EVP_DecryptInit_ex(de_ctx, cipher_algo, nullptr, | ||
| reinterpret_cast<const unsigned char*>(key), | ||
| reinterpret_cast<const unsigned char*>(iv))) { | ||
| EVP_CIPHER_CTX_free(de_ctx); | ||
| throw std::runtime_error("Could not initialize EVP cipher context for decryption: " + | ||
| get_openssl_error_string()); | ||
| } | ||
|
|
||
| int padding_flag = use_padding ? 1 : 0; | ||
| if (!EVP_CIPHER_CTX_set_padding(de_ctx, padding_flag)) { | ||
| EVP_CIPHER_CTX_free(de_ctx); | ||
| throw std::runtime_error("Could not set padding mode for decryption: " + | ||
| get_openssl_error_string()); | ||
| } | ||
|
|
||
| if (!EVP_DecryptUpdate(de_ctx, plaintext, &len, | ||
| reinterpret_cast<const unsigned char*>(ciphertext), | ||
| ciphertext_len)) { | ||
| EVP_CIPHER_CTX_free(de_ctx); | ||
| throw std::runtime_error("Could not update EVP cipher context for decryption: " + | ||
| get_openssl_error_string()); | ||
| } | ||
|
|
||
| plaintext_len += len; | ||
|
|
||
| if (!EVP_DecryptFinal_ex(de_ctx, plaintext + len, &len)) { | ||
| EVP_CIPHER_CTX_free(de_ctx); | ||
| throw std::runtime_error("Could not finalize EVP cipher context for decryption: " + | ||
| get_openssl_error_string()); | ||
| } | ||
|
|
||
| plaintext_len += len; | ||
|
|
||
| EVP_CIPHER_CTX_free(de_ctx); | ||
| return plaintext_len; | ||
| } | ||
|
|
||
| } // namespace gandiva | ||
|
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If any of these comments are used more than once it would be good to make them constants. That could be done in a follow up pr though.