|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License") you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +#include "gandiva/encrypt_mode_dispatcher.h" |
| 19 | +#include "gandiva/encrypt_utils_ecb.h" |
| 20 | +#include "gandiva/encrypt_utils_cbc.h" |
| 21 | +#include "gandiva/encrypt_utils_gcm.h" |
| 22 | +#include "arrow/util/string.h" |
| 23 | +#include <string> |
| 24 | +#include <sstream> |
| 25 | +#include <stdexcept> |
| 26 | +#include <vector> |
| 27 | + |
| 28 | +namespace gandiva { |
| 29 | + |
| 30 | +// Supported encryption modes |
| 31 | +static const std::vector<std::string_view> SUPPORTED_MODES = { |
| 32 | + AES_ECB_MODE, AES_ECB_PKCS7_MODE, AES_ECB_NONE_MODE, |
| 33 | + AES_CBC_MODE, AES_CBC_PKCS7_MODE, AES_CBC_NONE_MODE, |
| 34 | + AES_GCM_MODE |
| 35 | +}; |
| 36 | + |
| 37 | +enum class EncryptionMode { |
| 38 | + ECB, |
| 39 | + ECB_PKCS7, |
| 40 | + ECB_NONE, |
| 41 | + CBC, |
| 42 | + CBC_PKCS7, |
| 43 | + CBC_NONE, |
| 44 | + GCM, |
| 45 | + UNKNOWN |
| 46 | +}; |
| 47 | + |
| 48 | +EncryptionMode ParseEncryptionMode(std::string_view mode_str) { |
| 49 | + if (mode_str == AES_ECB_MODE) return EncryptionMode::ECB; |
| 50 | + if (mode_str == AES_ECB_PKCS7_MODE) return EncryptionMode::ECB_PKCS7; |
| 51 | + if (mode_str == AES_ECB_NONE_MODE) return EncryptionMode::ECB_NONE; |
| 52 | + if (mode_str == AES_CBC_MODE) return EncryptionMode::CBC; |
| 53 | + if (mode_str == AES_CBC_PKCS7_MODE) return EncryptionMode::CBC_PKCS7; |
| 54 | + if (mode_str == AES_CBC_NONE_MODE) return EncryptionMode::CBC_NONE; |
| 55 | + if (mode_str == AES_GCM_MODE) return EncryptionMode::GCM; |
| 56 | + return EncryptionMode::UNKNOWN; |
| 57 | +} |
| 58 | + |
| 59 | +int32_t EncryptModeDispatcher::encrypt( |
| 60 | + const char* plaintext, int32_t plaintext_len, const char* key, |
| 61 | + int32_t key_len, const char* mode, int32_t mode_len, const char* iv, |
| 62 | + int32_t iv_len, const char* fifth_argument, int32_t fifth_argument_len, |
| 63 | + unsigned char* cipher) { |
| 64 | + std::string mode_str = |
| 65 | + arrow::internal::AsciiToUpper(std::string_view(mode, mode_len)); |
| 66 | + |
| 67 | + switch (ParseEncryptionMode(mode_str)) { |
| 68 | + case EncryptionMode::ECB: |
| 69 | + case EncryptionMode::ECB_PKCS7: |
| 70 | + // Shorthand AES-ECB and explicit AES-ECB-PKCS7 both use ECB with PKCS7 padding |
| 71 | + return aes_encrypt_ecb(plaintext, plaintext_len, key, key_len, true, cipher); |
| 72 | + case EncryptionMode::ECB_NONE: |
| 73 | + // ECB without padding |
| 74 | + return aes_encrypt_ecb(plaintext, plaintext_len, key, key_len, false, cipher); |
| 75 | + case EncryptionMode::CBC: |
| 76 | + case EncryptionMode::CBC_PKCS7: |
| 77 | + // Shorthand AES-CBC and explicit AES-CBC-PKCS7 both use CBC with PKCS7 |
| 78 | + return aes_encrypt_cbc(plaintext, plaintext_len, key, key_len, |
| 79 | + iv, iv_len, true, cipher); |
| 80 | + case EncryptionMode::CBC_NONE: |
| 81 | + // CBC without padding |
| 82 | + return aes_encrypt_cbc(plaintext, plaintext_len, key, key_len, |
| 83 | + iv, iv_len, false, cipher); |
| 84 | + case EncryptionMode::GCM: |
| 85 | + return aes_encrypt_gcm(plaintext, plaintext_len, key, key_len, |
| 86 | + iv, iv_len, fifth_argument, fifth_argument_len, cipher); |
| 87 | + case EncryptionMode::UNKNOWN: |
| 88 | + default: { |
| 89 | + std::string modes_str = arrow::internal::JoinStrings(SUPPORTED_MODES, ", "); |
| 90 | + std::ostringstream oss; |
| 91 | + oss << "Unsupported encryption mode: " << mode_str |
| 92 | + << ". Supported modes: " << modes_str; |
| 93 | + throw std::runtime_error(oss.str()); |
| 94 | + } |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +int32_t EncryptModeDispatcher::decrypt( |
| 99 | + const char* ciphertext, int32_t ciphertext_len, const char* key, |
| 100 | + int32_t key_len, const char* mode, int32_t mode_len, const char* iv, |
| 101 | + int32_t iv_len, const char* fifth_argument, int32_t fifth_argument_len, |
| 102 | + unsigned char* plaintext) { |
| 103 | + std::string mode_str = |
| 104 | + arrow::internal::AsciiToUpper(std::string_view(mode, mode_len)); |
| 105 | + |
| 106 | + switch (ParseEncryptionMode(mode_str)) { |
| 107 | + case EncryptionMode::ECB: |
| 108 | + case EncryptionMode::ECB_PKCS7: |
| 109 | + // Shorthand AES-ECB and explicit AES-ECB-PKCS7 both use ECB with PKCS7 padding |
| 110 | + return aes_decrypt_ecb(ciphertext, ciphertext_len, key, key_len, true, plaintext); |
| 111 | + case EncryptionMode::ECB_NONE: |
| 112 | + // ECB without padding |
| 113 | + return aes_decrypt_ecb(ciphertext, ciphertext_len, key, key_len, false, plaintext); |
| 114 | + case EncryptionMode::CBC: |
| 115 | + case EncryptionMode::CBC_PKCS7: |
| 116 | + // Shorthand AES-CBC and explicit AES-CBC-PKCS7 both use CBC with PKCS7 |
| 117 | + return aes_decrypt_cbc(ciphertext, ciphertext_len, key, key_len, |
| 118 | + iv, iv_len, true, plaintext); |
| 119 | + case EncryptionMode::CBC_NONE: |
| 120 | + // CBC without padding |
| 121 | + return aes_decrypt_cbc(ciphertext, ciphertext_len, key, key_len, |
| 122 | + iv, iv_len, false, plaintext); |
| 123 | + case EncryptionMode::GCM: |
| 124 | + return aes_decrypt_gcm(ciphertext, ciphertext_len, key, key_len, |
| 125 | + iv, iv_len, fifth_argument, fifth_argument_len, plaintext); |
| 126 | + case EncryptionMode::UNKNOWN: |
| 127 | + default: { |
| 128 | + std::string modes_str = arrow::internal::JoinStrings(SUPPORTED_MODES, ", "); |
| 129 | + std::ostringstream oss; |
| 130 | + oss << "Unsupported decryption mode: " << mode_str |
| 131 | + << ". Supported modes: " << modes_str; |
| 132 | + throw std::runtime_error(oss.str()); |
| 133 | + } |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +} // namespace gandiva |
| 138 | + |
0 commit comments