|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +#include <limits> |
| 21 | + |
| 22 | +#include <openssl/evp.h> |
| 23 | +#include <openssl/rand.h> |
| 24 | + |
| 25 | +#include "iceberg/encryption/aes_gcm_internal.h" |
| 26 | +#include "iceberg/util/macros.h" |
| 27 | + |
| 28 | +namespace iceberg::encryption { |
| 29 | + |
| 30 | +namespace { |
| 31 | + |
| 32 | +const EVP_CIPHER* CipherForKey(size_t key_length) { |
| 33 | + switch (key_length) { |
| 34 | + case 16: |
| 35 | + return EVP_aes_128_gcm(); |
| 36 | + case 24: |
| 37 | + return EVP_aes_192_gcm(); |
| 38 | + case 32: |
| 39 | + return EVP_aes_256_gcm(); |
| 40 | + default: |
| 41 | + return nullptr; |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +EVP_CIPHER_CTX* Ctx(void* ctx) { return static_cast<EVP_CIPHER_CTX*>(ctx); } |
| 46 | + |
| 47 | +} // namespace |
| 48 | + |
| 49 | +Status SecureRandomBytes(std::span<uint8_t> out) { |
| 50 | + if (out.size() > static_cast<size_t>(std::numeric_limits<int>::max()) || |
| 51 | + RAND_bytes(out.data(), static_cast<int>(out.size())) != 1) { |
| 52 | + return IOError("Failed to generate secure random bytes"); |
| 53 | + } |
| 54 | + return {}; |
| 55 | +} |
| 56 | + |
| 57 | +Result<std::unique_ptr<AesGcmCipher>> AesGcmCipher::Make(std::span<const uint8_t> key) { |
| 58 | + if (CipherForKey(key.size()) == nullptr) { |
| 59 | + return InvalidArgument("Invalid key length: {} (must be 16, 24, or 32 bytes)", |
| 60 | + key.size()); |
| 61 | + } |
| 62 | + EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new(); |
| 63 | + if (ctx == nullptr) { |
| 64 | + return IOError("Failed to create cipher context"); |
| 65 | + } |
| 66 | + return std::unique_ptr<AesGcmCipher>( |
| 67 | + new AesGcmCipher(std::vector<uint8_t>(key.begin(), key.end()), ctx)); |
| 68 | +} |
| 69 | + |
| 70 | +AesGcmCipher::AesGcmCipher(std::vector<uint8_t> key, void* ctx) |
| 71 | + : key_(std::move(key)), ctx_(ctx) {} |
| 72 | + |
| 73 | +AesGcmCipher::~AesGcmCipher() { EVP_CIPHER_CTX_free(Ctx(ctx_)); } |
| 74 | + |
| 75 | +Status AesGcmCipher::Encrypt(std::span<const uint8_t> plaintext, |
| 76 | + std::span<const uint8_t> aad, std::span<uint8_t> out) { |
| 77 | + if (out.size() != plaintext.size() + kNonceLength + kGcmTagLength) { |
| 78 | + return InvalidArgument("Invalid ciphertext buffer size: {}", out.size()); |
| 79 | + } |
| 80 | + if (plaintext.size() > static_cast<size_t>(std::numeric_limits<int>::max())) { |
| 81 | + return InvalidArgument("Plaintext too large: {}", plaintext.size()); |
| 82 | + } |
| 83 | + |
| 84 | + auto nonce = out.first(kNonceLength); |
| 85 | + auto ciphertext = out.subspan(kNonceLength, plaintext.size()); |
| 86 | + auto tag = out.last(kGcmTagLength); |
| 87 | + ICEBERG_RETURN_UNEXPECTED(SecureRandomBytes(nonce)); |
| 88 | + |
| 89 | + auto* ctx = Ctx(ctx_); |
| 90 | + int len = 0; |
| 91 | + if (EVP_EncryptInit_ex(ctx, CipherForKey(key_.size()), /*impl=*/nullptr, |
| 92 | + /*key=*/nullptr, /*iv=*/nullptr) != 1 || |
| 93 | + EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, kNonceLength, /*ptr=*/nullptr) != |
| 94 | + 1 || |
| 95 | + EVP_EncryptInit_ex(ctx, /*cipher=*/nullptr, /*impl=*/nullptr, key_.data(), |
| 96 | + nonce.data()) != 1) { |
| 97 | + return IOError("Failed to initialize AES GCM encryption"); |
| 98 | + } |
| 99 | + if (!aad.empty() && EVP_EncryptUpdate(ctx, /*out=*/nullptr, &len, aad.data(), |
| 100 | + static_cast<int>(aad.size())) != 1) { |
| 101 | + return IOError("Failed to set AES GCM AAD"); |
| 102 | + } |
| 103 | + if (!plaintext.empty() && |
| 104 | + EVP_EncryptUpdate(ctx, ciphertext.data(), &len, plaintext.data(), |
| 105 | + static_cast<int>(plaintext.size())) != 1) { |
| 106 | + return IOError("Failed to encrypt"); |
| 107 | + } |
| 108 | + if (EVP_EncryptFinal_ex(ctx, ciphertext.data() + plaintext.size(), &len) != 1 || |
| 109 | + EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG, kGcmTagLength, tag.data()) != 1) { |
| 110 | + return IOError("Failed to finalize AES GCM encryption"); |
| 111 | + } |
| 112 | + return {}; |
| 113 | +} |
| 114 | + |
| 115 | +Status AesGcmCipher::Decrypt(std::span<const uint8_t> ciphertext, |
| 116 | + std::span<const uint8_t> aad, std::span<uint8_t> out) { |
| 117 | + if (ciphertext.size() < static_cast<size_t>(kNonceLength + kGcmTagLength)) { |
| 118 | + return Invalid( |
| 119 | + "Cannot decrypt cipher text of length {}: shorter than nonce and tag, text may " |
| 120 | + "not be encrypted with AES GCM", |
| 121 | + ciphertext.size()); |
| 122 | + } |
| 123 | + size_t plaintext_length = ciphertext.size() - kNonceLength - kGcmTagLength; |
| 124 | + if (out.size() != plaintext_length) { |
| 125 | + return InvalidArgument("Invalid plaintext buffer size: {}", out.size()); |
| 126 | + } |
| 127 | + |
| 128 | + auto nonce = ciphertext.first(kNonceLength); |
| 129 | + auto data = ciphertext.subspan(kNonceLength, plaintext_length); |
| 130 | + auto tag = ciphertext.last(kGcmTagLength); |
| 131 | + |
| 132 | + auto* ctx = Ctx(ctx_); |
| 133 | + int len = 0; |
| 134 | + if (EVP_DecryptInit_ex(ctx, CipherForKey(key_.size()), /*impl=*/nullptr, |
| 135 | + /*key=*/nullptr, /*iv=*/nullptr) != 1 || |
| 136 | + EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, kNonceLength, /*ptr=*/nullptr) != |
| 137 | + 1 || |
| 138 | + EVP_DecryptInit_ex(ctx, /*cipher=*/nullptr, /*impl=*/nullptr, key_.data(), |
| 139 | + nonce.data()) != 1) { |
| 140 | + return IOError("Failed to initialize AES GCM decryption"); |
| 141 | + } |
| 142 | + if (!aad.empty() && EVP_DecryptUpdate(ctx, /*out=*/nullptr, &len, aad.data(), |
| 143 | + static_cast<int>(aad.size())) != 1) { |
| 144 | + return IOError("Failed to set AES GCM AAD"); |
| 145 | + } |
| 146 | + if (!data.empty() && EVP_DecryptUpdate(ctx, out.data(), &len, data.data(), |
| 147 | + static_cast<int>(data.size())) != 1) { |
| 148 | + return IOError("Failed to decrypt"); |
| 149 | + } |
| 150 | + if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, kGcmTagLength, |
| 151 | + const_cast<uint8_t*>(tag.data())) != 1 || |
| 152 | + EVP_DecryptFinal_ex(ctx, out.data() + plaintext_length, &len) != 1) { |
| 153 | + return Invalid( |
| 154 | + "GCM tag check failed. Possible reasons: wrong decryption key; or " |
| 155 | + "corrupt/tampered data. AES GCM doesn't differentiate between these two."); |
| 156 | + } |
| 157 | + return {}; |
| 158 | +} |
| 159 | + |
| 160 | +Result<std::vector<uint8_t>> AesGcmCipher::Encrypt(std::span<const uint8_t> plaintext, |
| 161 | + std::span<const uint8_t> aad) { |
| 162 | + std::vector<uint8_t> out(plaintext.size() + kNonceLength + kGcmTagLength); |
| 163 | + ICEBERG_RETURN_UNEXPECTED(Encrypt(plaintext, aad, out)); |
| 164 | + return out; |
| 165 | +} |
| 166 | + |
| 167 | +Result<std::vector<uint8_t>> AesGcmCipher::Decrypt(std::span<const uint8_t> ciphertext, |
| 168 | + std::span<const uint8_t> aad) { |
| 169 | + if (ciphertext.size() < static_cast<size_t>(kNonceLength + kGcmTagLength)) { |
| 170 | + return Invalid("Cannot decrypt cipher text of length {}", ciphertext.size()); |
| 171 | + } |
| 172 | + std::vector<uint8_t> out(ciphertext.size() - kNonceLength - kGcmTagLength); |
| 173 | + ICEBERG_RETURN_UNEXPECTED(Decrypt(ciphertext, aad, out)); |
| 174 | + return out; |
| 175 | +} |
| 176 | + |
| 177 | +} // namespace iceberg::encryption |
0 commit comments