Version 3 means that the WAL is encrypted For encryption, the length field remains plaintext After the length field, we store a 12-byte nonce (for GCM) After the nonce, we store the checksum, followed by the actual entry After the stored entry, we store a 16-byte nonce (for GCM).
443878 {
443879 if (state_p.wal_version == 1) {
443880
443881 return WriteAheadLogDeserializer(state_p, stream, deserialize_only);
443882 }
443883
443884 if (state_p.wal_version == 2) {
443885
443887 auto stored_checksum = stream.Read<
uint64_t>();
443888 auto offset = stream.CurrentOffset();
443889 auto file_size = stream.FileSize();
443890
443891 if (offset + size > file_size) {
443892 throw SerializationException(
443893 "Corrupt WAL file: entry size exceeded remaining data in file at byte position %llu "
443894 "(found entry with size %llu bytes, file size %llu bytes)",
443895 offset, size, file_size);
443896 }
443897
443898
443899 auto buffer = unique_ptr<data_t[]>(
new data_t[size]);
443900 stream.ReadData(buffer.get(), size);
443901
443902
443903 auto computed_checksum =
Checksum(buffer.get(), size);
443904 if (stored_checksum != computed_checksum) {
443905 throw IOException("Corrupt WAL file: entry at byte position %llu computed checksum %llu does not match "
443906 "stored checksum %llu",
443907 offset, computed_checksum, stored_checksum);
443908 }
443909
443910 return WriteAheadLogDeserializer(state_p, std::move(buffer), size, deserialize_only);
443911 }
443912
443913 if (state_p.wal_version == 3) {
443914 auto &database = state_p.db.GetDatabase();
443920
443921
443923
443924 const auto ciphertext_size =
size +
sizeof(
uint64_t);
443925
443926 auto offset = stream.CurrentOffset();
443927 auto file_size = stream.FileSize();
443928
443929 EncryptionNonce nonce(state_p.db.GetStorageManager().GetCipher(),
443930 state_p.db.GetStorageManager().GetEncryptionVersion());
443931 EncryptionTag tag;
443932
443933 stream.ReadData(nonce.data(), nonce.size());
443934
443935 auto &keys = EncryptionKeyManager::Get(state_p.db.GetDatabase());
443936 auto &catalog = state_p.db.GetCatalog().Cast<DuckCatalog>();
443937 auto derived_key = keys.GetKey(catalog.GetEncryptionKeyId());
443938 auto metadata = make_uniq<EncryptionStateMetadata>(state_p.db.GetStorageManager().GetCipher(),
443940 state_p.db.GetStorageManager().GetEncryptionVersion());
443942 auto encryption_state =
443943 database.GetEncryptionUtil(state_p.db.IsReadOnly())->CreateEncryptionState(std::move(metadata));
443944 encryption_state->InitializeDecryption(nonce, derived_key);
443945
443946 if (encryption_state->GetCipher() == EncryptionTypes::CipherType::CTR) {
443947 tag.SetSize(0);
443948 }
443949
443950 if (offset + nonce.size() + ciphertext_size + tag.size() > file_size) {
443951 throw SerializationException(
443952 "Corrupt Encrypted WAL file: entry size exceeded remaining data in file at byte position %llu "
443953 "(found entry with size %llu bytes, file size %llu bytes)",
443954 offset, size, file_size);
443955 }
443956
443958 auto buffer = unique_ptr<data_t[]>(
new data_t[ciphertext_size]);
443959 auto out_buffer = unique_ptr<data_t[]>(
new data_t[size]);
443960
443961 stream.ReadData(buffer.get(), ciphertext_size);
443962 encryption_state->Process(buffer.get(), ciphertext_size, buffer.get(), ciphertext_size);
443963
443964 if (encryption_state->GetCipher() == EncryptionTypes::CipherType::GCM) {
443966 stream.ReadData(tag.data(), tag.size());
443967 D_ASSERT(!tag.IsAllZeros());
443968 encryption_state->Finalize(buffer.get(), ciphertext_size, tag.data(), tag.size());
443969 } else {
443970 encryption_state->Finalize(buffer.get(), ciphertext_size, nullptr, 0);
443971 }
443972
443974 auto stored_checksum = Load<uint64_t>(buffer.get());
443975
443977 memcpy(out_buffer.get(), buffer.get() + sizeof(stored_checksum), size);
443978
443979
443980 auto computed_checksum =
Checksum(out_buffer.get(), size);
443981 if (stored_checksum != computed_checksum) {
443982 throw IOException("Corrupt WAL file: entry at byte position %llu computed checksum %llu does not match "
443983 "stored checksum %llu",
443984 offset, computed_checksum, stored_checksum);
443985 }
443986
443987 return WriteAheadLogDeserializer(state_p, std::move(out_buffer), size, deserialize_only);
443988 }
443989
443990 throw IOException("Failed to read WAL of version %llu - can only read version 1, 2 and 3 (encrypted)",
443991 state_p.wal_version);
443992 }
static constexpr uint64_t DEFAULT_ENCRYPTION_KEY_LENGTH
The encryption key length.
Definition duckdb.hpp:9749
GOpaque< Size > size(const GMat &src)
uint8_t data_t
data pointers
Definition duckdb.hpp:246
uint64_t Checksum(uint8_t *buffer, size_t size)
Compute a checksum over a buffer of size size.
Definition duckdb.cpp:49970