shangxinli commented on code in PR #1030: URL: https://github.com/apache/iceberg-cpp/pull/1030#discussion_r4303959571
########## 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: Done - PlaintextLength now returns a Result and rejects the length before the stream is constructed, so Size() errors too; the int64 product itself can't actually overflow since num_full_blocks times the plain block size is always smaller than the stream length, but the Math.toIntExact bound on the block count was genuinely missing and is now enforced up front. -- 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]
