wgtmac commented on code in PR #1030: URL: https://github.com/apache/iceberg-cpp/pull/1030#discussion_r4303397586
########## src/iceberg/encryption/aes_gcm_stream.cc: ########## @@ -0,0 +1,344 @@ +/* + * 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 "iceberg/encryption/aes_gcm_stream.h" + +#include <algorithm> +#include <array> +#include <cstring> +#include <limits> +#include <utility> + +#include "iceberg/encryption/aes_gcm_internal.h" +#include "iceberg/util/macros.h" + +namespace iceberg { + +namespace { + +using encryption::AesGcmCipher; + +constexpr int64_t kPlainBlock = AesGcmStream::kPlainBlockSize; +constexpr int64_t kCipherBlock = AesGcmStream::kCipherBlockSize; +constexpr int64_t kBlockOverhead = + AesGcmStream::kNonceLength + AesGcmStream::kGcmTagLength; + +/// \brief Block AAD: file AAD prefix followed by the block index as a 4-byte LE int. +std::vector<uint8_t> BlockAad(const std::vector<uint8_t>& aad_prefix, + int32_t block_index) { + std::vector<uint8_t> aad(aad_prefix); + for (int i = 0; i < 4; ++i) { + aad.push_back( + static_cast<uint8_t>((static_cast<uint32_t>(block_index) >> (8 * i)) & 0xFF)); + } + return aad; +} + +std::array<std::byte, AesGcmStream::kHeaderLength> HeaderBytes() { + std::array<std::byte, AesGcmStream::kHeaderLength> header{}; + std::memcpy(header.data(), AesGcmStream::kMagic.data(), AesGcmStream::kMagic.size()); + for (int i = 0; i < 4; ++i) { + header[4 + i] = static_cast<std::byte>( + (static_cast<uint32_t>(AesGcmStream::kPlainBlockSize) >> (8 * i)) & 0xFF); + } + return header; +} + +class AesGcmInputStream : public SeekableInputStream { + public: + AesGcmInputStream(std::unique_ptr<SeekableInputStream> source, + std::unique_ptr<AesGcmCipher> cipher, std::vector<uint8_t> aad_prefix, + int64_t encrypted_length) + : source_(std::move(source)), + cipher_(std::move(cipher)), + aad_prefix_(std::move(aad_prefix)), + cipher_block_(kCipherBlock), + plain_block_(kPlainBlock) { + int64_t stream_length = encrypted_length - AesGcmStream::kHeaderLength; Review Comment: Please validate the encrypted length with checked arithmetic before deriving `plain_size`. A very large metadata length can overflow `num_full_blocks * kPlainBlockSize`; the current block-index check happens only when a block is decrypted, after `Size()` may already have returned an invalid length. Java bounds the block count with `Math.toIntExact`, so this path should reject the value before constructing the stream. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
