| 199 | } |
| 200 | |
| 201 | int32_t AesEncryptor::AesEncryptorImpl::Encrypt(std::span<const uint8_t> plaintext, |
| 202 | std::span<const uint8_t> key, |
| 203 | std::span<const uint8_t> aad, |
| 204 | std::span<uint8_t> ciphertext) { |
| 205 | if (static_cast<size_t>(key_length_) != key.size()) { |
| 206 | std::stringstream ss; |
| 207 | ss << "Wrong key length " << key.size() << ". Should be " << key_length_; |
| 208 | throw ParquetException(ss.str()); |
| 209 | } |
| 210 | |
| 211 | if (ciphertext.size() != plaintext.size() + ciphertext_size_delta_) { |
| 212 | std::stringstream ss; |
| 213 | ss << "Ciphertext buffer length " << ciphertext.size() |
| 214 | << " does not match expected length " |
| 215 | << (plaintext.size() + ciphertext_size_delta_); |
| 216 | throw ParquetException(ss.str()); |
| 217 | } |
| 218 | |
| 219 | std::array<uint8_t, kNonceLength> nonce{}; |
| 220 | // Random nonce |
| 221 | RAND_bytes(nonce.data(), kNonceLength); |
| 222 | |
| 223 | if (kGcmMode == aes_mode_) { |
| 224 | return GcmEncrypt(plaintext, key, nonce, aad, ciphertext); |
| 225 | } |
| 226 | |
| 227 | return CtrEncrypt(plaintext, key, nonce, ciphertext); |
| 228 | } |
| 229 | |
| 230 | int32_t AesEncryptor::AesEncryptorImpl::GcmEncrypt(std::span<const uint8_t> plaintext, |
| 231 | std::span<const uint8_t> key, |