|
| 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_utils_gcm.h" |
| 19 | +#include "gandiva/encrypt_utils_common.h" |
| 20 | +#include <openssl/aes.h> |
| 21 | +#include <openssl/err.h> |
| 22 | +#include <stdexcept> |
| 23 | +#include <cstring> |
| 24 | +#include <sstream> |
| 25 | + |
| 26 | +namespace gandiva { |
| 27 | + |
| 28 | +namespace { |
| 29 | + |
| 30 | +const EVP_CIPHER* get_gcm_cipher_algo(int32_t key_length) { |
| 31 | + switch (key_length) { |
| 32 | + case 16: |
| 33 | + return EVP_aes_128_gcm(); |
| 34 | + case 24: |
| 35 | + return EVP_aes_192_gcm(); |
| 36 | + case 32: |
| 37 | + return EVP_aes_256_gcm(); |
| 38 | + default: { |
| 39 | + std::ostringstream oss; |
| 40 | + oss << "Unsupported key length for AES-GCM: " << key_length |
| 41 | + << " bytes. Supported lengths: 16, 24, 32 bytes"; |
| 42 | + throw std::runtime_error(oss.str()); |
| 43 | + } |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +} // namespace |
| 48 | + |
| 49 | +GANDIVA_EXPORT |
| 50 | +int32_t aes_encrypt_gcm(const char* plaintext, int32_t plaintext_len, |
| 51 | + const char* key, int32_t key_len, const char* iv, |
| 52 | + int32_t iv_len, const char* aad, int32_t aad_len, |
| 53 | + unsigned char* cipher) { |
| 54 | + if (iv_len <= 0) { |
| 55 | + throw std::runtime_error( |
| 56 | + "Invalid IV length for AES-GCM: IV length must be greater than 0"); |
| 57 | + } |
| 58 | + |
| 59 | + int32_t cipher_len = 0; |
| 60 | + int32_t len = 0; |
| 61 | + EVP_CIPHER_CTX* en_ctx = EVP_CIPHER_CTX_new(); |
| 62 | + const EVP_CIPHER* cipher_algo = get_gcm_cipher_algo(key_len); |
| 63 | + |
| 64 | + if (!en_ctx) { |
| 65 | + throw std::runtime_error("Could not create EVP cipher context for encryption: " + |
| 66 | + get_openssl_error_string()); |
| 67 | + } |
| 68 | + |
| 69 | + try { |
| 70 | + if (!EVP_EncryptInit_ex(en_ctx, cipher_algo, nullptr, |
| 71 | + reinterpret_cast<const unsigned char*>(key), |
| 72 | + reinterpret_cast<const unsigned char*>(iv))) { |
| 73 | + throw std::runtime_error( |
| 74 | + "Could not initialize EVP cipher context for encryption: " + |
| 75 | + get_openssl_error_string()); |
| 76 | + } |
| 77 | + |
| 78 | + // Set IV length for GCM mode |
| 79 | + if (!EVP_CIPHER_CTX_ctrl(en_ctx, EVP_CTRL_GCM_SET_IVLEN, iv_len, nullptr)) { |
| 80 | + throw std::runtime_error("Could not set GCM IV length: " + |
| 81 | + get_openssl_error_string()); |
| 82 | + } |
| 83 | + |
| 84 | + // Process AAD if provided |
| 85 | + if (aad != nullptr && aad_len > 0) { |
| 86 | + if (!EVP_EncryptUpdate(en_ctx, nullptr, &len, |
| 87 | + reinterpret_cast<const unsigned char*>(aad), aad_len)) { |
| 88 | + throw std::runtime_error("Could not process AAD for encryption: " + |
| 89 | + get_openssl_error_string()); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + // Encrypt plaintext |
| 94 | + if (!EVP_EncryptUpdate(en_ctx, cipher, &len, |
| 95 | + reinterpret_cast<const unsigned char*>(plaintext), |
| 96 | + plaintext_len)) { |
| 97 | + throw std::runtime_error("Could not update EVP cipher context for encryption: " + |
| 98 | + get_openssl_error_string()); |
| 99 | + } |
| 100 | + |
| 101 | + cipher_len += len; |
| 102 | + |
| 103 | + // Finalize encryption |
| 104 | + if (!EVP_EncryptFinal_ex(en_ctx, cipher + len, &len)) { |
| 105 | + throw std::runtime_error("Could not finalize EVP cipher context for encryption: " + |
| 106 | + get_openssl_error_string()); |
| 107 | + } |
| 108 | + |
| 109 | + cipher_len += len; |
| 110 | + |
| 111 | + // Get the authentication tag and append it to ciphertext |
| 112 | + if (!EVP_CIPHER_CTX_ctrl(en_ctx, EVP_CTRL_GCM_GET_TAG, GCM_TAG_LENGTH, |
| 113 | + cipher + cipher_len)) { |
| 114 | + throw std::runtime_error("Could not get GCM authentication tag: " + |
| 115 | + get_openssl_error_string()); |
| 116 | + } |
| 117 | + cipher_len += GCM_TAG_LENGTH; |
| 118 | + } catch (...) { |
| 119 | + EVP_CIPHER_CTX_free(en_ctx); |
| 120 | + throw; |
| 121 | + } |
| 122 | + |
| 123 | + EVP_CIPHER_CTX_free(en_ctx); |
| 124 | + return cipher_len; |
| 125 | +} |
| 126 | + |
| 127 | +GANDIVA_EXPORT |
| 128 | +int32_t aes_decrypt_gcm(const char* ciphertext, int32_t ciphertext_len, |
| 129 | + const char* key, int32_t key_len, const char* iv, |
| 130 | + int32_t iv_len, const char* aad, int32_t aad_len, |
| 131 | + unsigned char* plaintext) { |
| 132 | + if (iv_len <= 0) { |
| 133 | + throw std::runtime_error( |
| 134 | + "Invalid IV length for AES-GCM: IV length must be greater than 0"); |
| 135 | + } |
| 136 | + |
| 137 | + if (ciphertext_len < GCM_TAG_LENGTH) { |
| 138 | + throw std::runtime_error( |
| 139 | + "Ciphertext too short for AES-GCM: must be at least 16 bytes for tag"); |
| 140 | + } |
| 141 | + |
| 142 | + int32_t plaintext_len = 0; |
| 143 | + int32_t len = 0; |
| 144 | + EVP_CIPHER_CTX* de_ctx = EVP_CIPHER_CTX_new(); |
| 145 | + const EVP_CIPHER* cipher_algo = get_gcm_cipher_algo(key_len); |
| 146 | + |
| 147 | + if (!de_ctx) { |
| 148 | + throw std::runtime_error("Could not create EVP cipher context for decryption: " + |
| 149 | + get_openssl_error_string()); |
| 150 | + } |
| 151 | + |
| 152 | + try { |
| 153 | + if (!EVP_DecryptInit_ex(de_ctx, cipher_algo, nullptr, |
| 154 | + reinterpret_cast<const unsigned char*>(key), |
| 155 | + reinterpret_cast<const unsigned char*>(iv))) { |
| 156 | + throw std::runtime_error( |
| 157 | + "Could not initialize EVP cipher context for decryption: " + |
| 158 | + get_openssl_error_string()); |
| 159 | + } |
| 160 | + |
| 161 | + // Set IV length for GCM mode |
| 162 | + if (!EVP_CIPHER_CTX_ctrl(de_ctx, EVP_CTRL_GCM_SET_IVLEN, iv_len, nullptr)) { |
| 163 | + throw std::runtime_error("Could not set GCM IV length: " + |
| 164 | + get_openssl_error_string()); |
| 165 | + } |
| 166 | + |
| 167 | + // Process AAD if provided |
| 168 | + if (aad != nullptr && aad_len > 0) { |
| 169 | + if (!EVP_DecryptUpdate(de_ctx, nullptr, &len, |
| 170 | + reinterpret_cast<const unsigned char*>(aad), aad_len)) { |
| 171 | + throw std::runtime_error("Could not process AAD for decryption: " + |
| 172 | + get_openssl_error_string()); |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + // Extract tag from end of ciphertext |
| 177 | + int32_t actual_ciphertext_len = ciphertext_len - GCM_TAG_LENGTH; |
| 178 | + const unsigned char* tag = |
| 179 | + reinterpret_cast<const unsigned char*>(ciphertext + actual_ciphertext_len); |
| 180 | + |
| 181 | + // Set the authentication tag |
| 182 | + if (!EVP_CIPHER_CTX_ctrl(de_ctx, EVP_CTRL_GCM_SET_TAG, GCM_TAG_LENGTH, |
| 183 | + const_cast<unsigned char*>(tag))) { |
| 184 | + throw std::runtime_error("Could not set GCM authentication tag: " + |
| 185 | + get_openssl_error_string()); |
| 186 | + } |
| 187 | + |
| 188 | + // Decrypt ciphertext |
| 189 | + if (!EVP_DecryptUpdate(de_ctx, plaintext, &len, |
| 190 | + reinterpret_cast<const unsigned char*>(ciphertext), |
| 191 | + actual_ciphertext_len)) { |
| 192 | + throw std::runtime_error("Could not update EVP cipher context for decryption: " + |
| 193 | + get_openssl_error_string()); |
| 194 | + } |
| 195 | + |
| 196 | + plaintext_len += len; |
| 197 | + |
| 198 | + // Finalize decryption (this verifies the tag) |
| 199 | + if (!EVP_DecryptFinal_ex(de_ctx, plaintext + len, &len)) { |
| 200 | + throw std::runtime_error("GCM tag verification failed or decryption error: " + |
| 201 | + get_openssl_error_string()); |
| 202 | + } |
| 203 | + plaintext_len += len; |
| 204 | + } catch (...) { |
| 205 | + EVP_CIPHER_CTX_free(de_ctx); |
| 206 | + throw; |
| 207 | + } |
| 208 | + |
| 209 | + EVP_CIPHER_CTX_free(de_ctx); |
| 210 | + return plaintext_len; |
| 211 | +} |
| 212 | + |
| 213 | +} // namespace gandiva |
| 214 | + |
0 commit comments