Skip to content
Merged
1 change: 1 addition & 0 deletions dev/release/rat_exclude_files.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ requirements.txt
test/resources/**
*.json
src/iceberg/util/murmurhash3_internal.*
src/iceberg/test/resources/encryption/**
12 changes: 11 additions & 1 deletion src/iceberg/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ set(ICEBERG_SOURCES
deletes/position_delete_index.cc
deletes/position_delete_range_consumer.cc
deletes/roaring_position_bitmap.cc
encryption/encrypted_files.cc
encryption/encryption_manager.cc

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new public headers are missing from the explicit install lists: encryption_manager.h, encryption_register.h and standard_encryption_manager.h. Please add them to the core, bundle and encryption groups respectively and regenerate the public-header coverage. Installed consumers currently cannot include these APIs; the registration header should remain available when the bundle is built with encryption disabled.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added the three missing headers to the install lists — encryption_manager.h as core, encryption_register.h tagged ICEBERG_PUBLIC_HEADERS_BUNDLE so it stays available with encryption disabled, and standard_encryption_manager.h in the existing encryption group — and regenerated cpp20_compatibility_internal.h.

encryption/key_management_client.cc
encryption/standard_key_metadata.cc
expression/aggregate.cc
Expand Down Expand Up @@ -337,6 +339,7 @@ if(ICEBERG_BUILD_BUNDLE)
avro/avro_register.cc
avro/avro_schema_util.cc
avro/avro_stream_internal.cc
encryption/encryption_register.cc
parquet/parquet_bloom_row_group_filter.cc
parquet/parquet_data_util.cc
parquet/parquet_dictionary_row_group_filter.cc
Expand All @@ -354,7 +357,9 @@ if(ICEBERG_BUILD_BUNDLE)
ICEBERG_BUNDLE_SOURCES
encryption/aes_gcm.cc
encryption/aes_gcm_stream.cc
encryption/in_memory_kms.cc)
encryption/encryption_util.cc
encryption/in_memory_kms.cc
encryption/standard_encryption_manager.cc)
endif()

# Libraries to link with exported libiceberg_bundle.{so,a}.
Expand Down Expand Up @@ -429,6 +434,11 @@ if(ICEBERG_BUILD_BUNDLE)

foreach(target iceberg_bundle_static iceberg_bundle_shared)
if(TARGET ${target})
if(ICEBERG_ENCRYPTION)
target_compile_definitions(${target} PRIVATE ICEBERG_ENCRYPTION_ENABLED=1)
else()
target_compile_definitions(${target} PRIVATE ICEBERG_ENCRYPTION_ENABLED=0)
endif()
if(ICEBERG_S3)
target_compile_definitions(${target}
PUBLIC "$<BUILD_INTERFACE:ICEBERG_S3_ENABLED=1>")
Expand Down
8 changes: 8 additions & 0 deletions src/iceberg/cpp20_compatibility_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,14 @@
#include "iceberg/deletes/position_delete_index.h"
#include "iceberg/deletes/position_delete_range_consumer.h"
#include "iceberg/deletes/roaring_position_bitmap.h"
#include "iceberg/encryption/encrypted_file.h"
#include "iceberg/encryption/encrypted_files.h"
#include "iceberg/encryption/encrypted_key.h"
#include "iceberg/encryption/encryption_key_metadata.h"
#include "iceberg/encryption/encryption_manager.h"
#include "iceberg/encryption/key_management_client.h"
#include "iceberg/encryption/standard_key_metadata.h"
#include "iceberg/encryption/type_fwd.h"
#include "iceberg/exception.h"
#include "iceberg/expression/aggregate.h"
#include "iceberg/expression/binder.h"
Expand Down Expand Up @@ -222,14 +227,17 @@
# include "iceberg/avro/avro_reader.h"
# include "iceberg/avro/avro_register.h"
# include "iceberg/avro/avro_writer.h"
# include "iceberg/encryption/encryption_register.h"
# include "iceberg/parquet/parquet_reader.h"
# include "iceberg/parquet/parquet_register.h"
# include "iceberg/parquet/parquet_writer.h"
#endif // ICEBERG_PUBLIC_HEADERS_BUNDLE

#ifdef ICEBERG_PUBLIC_HEADERS_ENCRYPTION
# include "iceberg/encryption/aes_gcm_stream.h"
# include "iceberg/encryption/encryption_util.h"
# include "iceberg/encryption/in_memory_kms.h"
# include "iceberg/encryption/standard_encryption_manager.h"
#endif // ICEBERG_PUBLIC_HEADERS_ENCRYPTION

#ifdef ICEBERG_PUBLIC_HEADERS_HIVE
Expand Down
24 changes: 19 additions & 5 deletions src/iceberg/encryption/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,28 @@

iceberg_install_all_headers(iceberg/encryption
PATTERN
encrypted_file.h
encrypted_files.h
encrypted_key.h
encryption_key_metadata.h
encryption_manager.h
key_management_client.h
standard_key_metadata.h)
if(ICEBERG_BUILD_BUNDLE AND ICEBERG_ENCRYPTION)
standard_key_metadata.h
type_fwd.h)
if(ICEBERG_BUILD_BUNDLE)
iceberg_install_all_headers(iceberg/encryption
PATTERN
aes_gcm_stream.h
in_memory_kms.h
encryption_register.h
HEADER_GROUP
ICEBERG_PUBLIC_HEADERS_ENCRYPTION)
ICEBERG_PUBLIC_HEADERS_BUNDLE)
if(ICEBERG_ENCRYPTION)
iceberg_install_all_headers(iceberg/encryption
PATTERN
aes_gcm_stream.h
encryption_util.h
in_memory_kms.h
standard_encryption_manager.h
HEADER_GROUP
ICEBERG_PUBLIC_HEADERS_ENCRYPTION)
endif()
endif()
14 changes: 14 additions & 0 deletions src/iceberg/encryption/aes_gcm_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,32 @@
/// \file iceberg/encryption/aes_gcm_internal.h
/// AES GCM cipher compatible with Java's org.apache.iceberg.encryption.Ciphers.

#include <cstddef>
#include <cstdint>
#include <memory>
#include <span>
#include <vector>

#include "iceberg/result.h"
#include "iceberg/type_fwd.h"

namespace iceberg::encryption {

inline constexpr int32_t kNonceLength = 12;
inline constexpr int32_t kGcmTagLength = 16;

inline constexpr bool IsValidAesKeyLength(std::size_t length) {
return length == 16 || length == 24 || length == 32;
}

Result<int64_t> AesGcmPlaintextLength(std::span<const uint8_t> key,
int64_t encrypted_length);

/// \brief Open an AGS1 stream that owns its underlying stream.
Result<std::unique_ptr<SeekableInputStream>> OpenAesGcmInputStream(
InputFile& source, std::span<const uint8_t> key, std::span<const uint8_t> aad_prefix,
int64_t encrypted_length);

/// \brief Fill `out` with cryptographically secure random bytes.
Status SecureRandomBytes(std::span<uint8_t> out);

Expand Down
71 changes: 57 additions & 14 deletions src/iceberg/encryption/aes_gcm_stream.cc
Original file line number Diff line number Diff line change
Expand Up @@ -320,29 +320,72 @@ Result<int64_t> AesGcmStream::PlaintextLength(int64_t encrypted_length) {
return num_full_blocks * kPlainBlockSize + last_plain;
}

namespace encryption {

Result<int64_t> AesGcmPlaintextLength(std::span<const uint8_t> key,
int64_t encrypted_length) {
ICEBERG_PRECHECK(IsValidAesKeyLength(key.size()),
"Invalid encryption key length: {} (must be 16, 24 or 32 bytes)",
key.size());
return AesGcmStream::PlaintextLength(encrypted_length);
}

Result<std::unique_ptr<SeekableInputStream>> OpenAesGcmInputStream(
InputFile& source, std::span<const uint8_t> key, std::span<const uint8_t> aad_prefix,
int64_t encrypted_length) {
ICEBERG_ASSIGN_OR_RAISE(auto plaintext_length,
AesGcmPlaintextLength(key, encrypted_length));
ICEBERG_ASSIGN_OR_RAISE(auto cipher, AesGcmCipher::Make(key));
ICEBERG_ASSIGN_OR_RAISE(auto stream, source.Open());
auto decrypted = std::make_unique<AesGcmInputStream>(
std::move(stream), std::move(cipher),
std::vector<uint8_t>(aad_prefix.begin(), aad_prefix.end()), encrypted_length,
plaintext_length);
ICEBERG_RETURN_UNEXPECTED(decrypted->ValidateHeader());
ICEBERG_RETURN_UNEXPECTED(decrypted->ValidateEmptyStream());
return decrypted;
}

} // namespace encryption

Result<std::unique_ptr<AesGcmInputFile>> AesGcmInputFile::Make(
std::unique_ptr<InputFile> source, std::span<const uint8_t> key,
std::span<const uint8_t> aad_prefix, int64_t encrypted_length) {
ICEBERG_PRECHECK(source != nullptr, "Encrypted input file cannot be null");
ICEBERG_ASSIGN_OR_RAISE(auto plaintext_length,
encryption::AesGcmPlaintextLength(key, encrypted_length));
return std::unique_ptr<AesGcmInputFile>(new AesGcmInputFile(
std::move(source), {key.begin(), key.end()}, {aad_prefix.begin(), aad_prefix.end()},
encrypted_length, plaintext_length));
}

AesGcmInputFile::AesGcmInputFile(std::unique_ptr<InputFile> source,
std::vector<uint8_t> key,
std::vector<uint8_t> aad_prefix,
int64_t encrypted_length)
int64_t encrypted_length, int64_t plaintext_length)
: source_(std::move(source)),
key_(std::move(key)),
aad_prefix_(std::move(aad_prefix)),
encrypted_length_(encrypted_length) {}
encrypted_length_(encrypted_length),
plaintext_length_(plaintext_length) {}

Result<int64_t> AesGcmInputFile::Size() const {
return AesGcmStream::PlaintextLength(encrypted_length_);
}
Result<int64_t> AesGcmInputFile::Size() const { return plaintext_length_; }

Result<std::unique_ptr<SeekableInputStream>> AesGcmInputFile::Open() {
ICEBERG_ASSIGN_OR_RAISE(auto plain_size,
AesGcmStream::PlaintextLength(encrypted_length_));
ICEBERG_ASSIGN_OR_RAISE(auto cipher, AesGcmCipher::Make(key_));
ICEBERG_ASSIGN_OR_RAISE(auto source, source_->Open());
auto stream = std::make_unique<AesGcmInputStream>(
std::move(source), std::move(cipher), aad_prefix_, encrypted_length_, plain_size);
ICEBERG_RETURN_UNEXPECTED(stream->ValidateHeader());
ICEBERG_RETURN_UNEXPECTED(stream->ValidateEmptyStream());
return stream;
return encryption::OpenAesGcmInputStream(*source_, key_, aad_prefix_,
encrypted_length_);
}

Result<std::unique_ptr<AesGcmOutputFile>> AesGcmOutputFile::Make(
std::unique_ptr<OutputFile> target, std::span<const uint8_t> key,
std::span<const uint8_t> aad_prefix) {
ICEBERG_PRECHECK(target != nullptr, "Output file cannot be null");
ICEBERG_PRECHECK(encryption::IsValidAesKeyLength(key.size()),
"Invalid encryption key length: {} (must be 16, 24 or 32 bytes)",
key.size());
return std::unique_ptr<AesGcmOutputFile>(
new AesGcmOutputFile(std::move(target), {key.begin(), key.end()},
{aad_prefix.begin(), aad_prefix.end()}));
}

AesGcmOutputFile::AesGcmOutputFile(std::unique_ptr<OutputFile> target,
Expand Down
18 changes: 14 additions & 4 deletions src/iceberg/encryption/aes_gcm_stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,31 +66,41 @@ struct AesGcmStream {
/// truncation of the file is detected.
class ICEBERG_BUNDLE_EXPORT AesGcmInputFile : public InputFile {
public:
AesGcmInputFile(std::unique_ptr<InputFile> source, std::vector<uint8_t> key,
std::vector<uint8_t> aad_prefix, int64_t encrypted_length);
static Result<std::unique_ptr<AesGcmInputFile>> Make(
std::unique_ptr<InputFile> source, std::span<const uint8_t> key,
std::span<const uint8_t> aad_prefix, int64_t encrypted_length);

std::string_view location() const override { return source_->location(); }
Result<int64_t> Size() const override;
Result<std::unique_ptr<SeekableInputStream>> Open() override;

private:
AesGcmInputFile(std::unique_ptr<InputFile> source, std::vector<uint8_t> key,
std::vector<uint8_t> aad_prefix, int64_t encrypted_length,
int64_t plaintext_length);

std::unique_ptr<InputFile> source_;
std::vector<uint8_t> key_;
std::vector<uint8_t> aad_prefix_;
int64_t encrypted_length_;
int64_t plaintext_length_;
};

/// \brief An OutputFile that writes an AGS1 file.
class ICEBERG_BUNDLE_EXPORT AesGcmOutputFile : public OutputFile {
public:
AesGcmOutputFile(std::unique_ptr<OutputFile> target, std::vector<uint8_t> key,
std::vector<uint8_t> aad_prefix);
static Result<std::unique_ptr<AesGcmOutputFile>> Make(
std::unique_ptr<OutputFile> target, std::span<const uint8_t> key,
std::span<const uint8_t> aad_prefix);

std::string_view location() const override { return target_->location(); }
Result<std::unique_ptr<PositionOutputStream>> Create() override;
Result<std::unique_ptr<PositionOutputStream>> CreateOrOverwrite() override;

private:
AesGcmOutputFile(std::unique_ptr<OutputFile> target, std::vector<uint8_t> key,
std::vector<uint8_t> aad_prefix);

std::unique_ptr<OutputFile> target_;
std::vector<uint8_t> key_;
std::vector<uint8_t> aad_prefix_;
Expand Down
121 changes: 121 additions & 0 deletions src/iceberg/encryption/encrypted_file.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
* 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.
*/

#pragma once

/// \file iceberg/encryption/encrypted_file.h
/// Pairs a file handle with its key metadata, mirroring Java's
/// EncryptedInputFile / EncryptedOutputFile.

#include <memory>

#include "iceberg/encryption/encryption_key_metadata.h"
#include "iceberg/encryption/type_fwd.h"
#include "iceberg/file_io.h"
#include "iceberg/iceberg_export.h"

namespace iceberg {

/// \brief An encrypted input file together with the key metadata needed to decrypt it.
class ICEBERG_EXPORT EncryptedInputFile {
public:
virtual ~EncryptedInputFile() = default;

/// \brief Take ownership of a native wrapper, or leave encrypted intact and return
/// null.
static std::unique_ptr<NativeEncryptionInputFile> AsNative(
std::unique_ptr<EncryptedInputFile>& encrypted);

/// \brief Transfer ownership of the still-encrypted raw input file.
///
/// Returns nullptr if the wrapper has no file. Use the returned handle for file access
/// after detaching.
virtual std::unique_ptr<InputFile> DetachInputFile() && = 0;

/// \brief The key metadata needed to decrypt this file.
virtual const EncryptionKeyMetadata& key_metadata() const = 0;

private:
virtual NativeEncryptionInputFile* native_input_file() noexcept { return nullptr; }
};

/// \brief An encrypted input file that supports both read paths of Java's
/// NativeEncryptionInputFile.
///
/// - Native path, for formats such as Parquet that perform their own modular decryption:
/// key_metadata() gives the key parameters (and AAD prefix) the native reader needs.
/// DetachInputFile() then transfers the still-encrypted raw file.
/// - Stream path: the InputFile methods read the file as an AES GCM stream (AGS1),
/// decrypting it, and require the encrypted file length in the key metadata.
///
/// Implementations may defer building the stream reader until the InputFile methods are
/// first used, so native-only callers are not subject to the stream path's requirements.
class ICEBERG_EXPORT NativeEncryptionInputFile : public EncryptedInputFile,
public InputFile {
public:
const NativeEncryptionKeyMetadata& key_metadata() const override = 0;

private:
NativeEncryptionInputFile* native_input_file() noexcept final { return this; }
};

/// \brief An encrypting output file together with the key metadata that was used (or
/// will be used) to encrypt it.
class ICEBERG_EXPORT EncryptedOutputFile {
public:
virtual ~EncryptedOutputFile() = default;

/// \brief Borrow the encrypting output file from this wrapper.
virtual OutputFile& encrypting_output_file() const = 0;

/// \brief The key metadata that was used to encrypt this file.
virtual const EncryptionKeyMetadata& key_metadata() const = 0;
};

/// \brief An encrypting output file that supports both write paths of Java's
/// NativeEncryptionOutputFile.
///
/// - Native path, for formats such as Parquet that perform their own modular encryption:
/// plain_output_file() gives the file to write the (already encrypted) bytes to and
/// key_metadata() gives the key parameters (and AAD prefix) the native writer needs.
/// - Stream path: encrypting_output_file(), and the OutputFile methods that delegate to
/// it, write the file as an AES GCM stream (AGS1).
class ICEBERG_EXPORT NativeEncryptionOutputFile : public EncryptedOutputFile,
public OutputFile {
public:
/// \brief The underlying plain output file, for native writers.
virtual OutputFile& plain_output_file() const = 0;

const NativeEncryptionKeyMetadata& key_metadata() const override = 0;

// OutputFile, delegating to the stream path.
std::string_view location() const override {
return encrypting_output_file().location();
}

Result<std::unique_ptr<PositionOutputStream>> Create() override {
return encrypting_output_file().Create();
}

Result<std::unique_ptr<PositionOutputStream>> CreateOrOverwrite() override {
return encrypting_output_file().CreateOrOverwrite();
}
};

} // namespace iceberg
Loading
Loading