/* * AsyncFileEncrypted.h * * This source file is part of the FoundationDB open source project * * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors * * Licensed 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 #include "fdbrpc/IAsyncFile.h" #include "flow/FastRef.h" #include "flow/flow.h" #include "flow/IRandom.h" #include "flow/StreamCipher.h" #include /* * Append-only file encrypted using AES-128-GCM. * */ class AsyncFileEncrypted : public IAsyncFile, public ReferenceCounted { Reference file; StreamCipher::IV firstBlockIV; StreamCipher::IV getIV(uint16_t block) const; bool canWrite; Future writeLastBlockToFile(); friend class AsyncFileEncryptedImpl; // Reading: class RandomCache { size_t maxSize; std::vector vec; std::unordered_map> hashMap; size_t evict(); public: RandomCache(size_t maxSize); void insert(uint16_t block, const Standalone& value); Optional> get(uint16_t block) const; } readBuffers; // Writing (append only): std::unique_ptr encryptor; uint16_t currentBlock{ 0 }; int offsetInBlock{ 0 }; std::vector writeBuffer; Future initialize(); public: // TODO: Remove boolean parameter here: AsyncFileEncrypted(Reference, bool canWrite); void addref() override; void delref() override; Future read(void* data, int length, int64_t offset) override; Future write(void const* data, int length, int64_t offset) override; Future zeroRange(int64_t offset, int64_t length) override; Future truncate(int64_t size) override; Future sync() override; Future flush() override; Future size() const override; std::string getFilename() const override; Future readZeroCopy(void** data, int* length, int64_t offset) override; void releaseZeroCopy(void* data, int length, int64_t offset) override; int64_t debugFD() const override; };