2017-06-27 01:52:06 +02:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
2017-07-16 01:03:42 +02:00
|
|
|
// This source code is licensed under both the GPLv2 (found in the
|
|
|
|
// COPYING file in the root directory) and Apache 2.0 License
|
|
|
|
// (found in the LICENSE.Apache file in the root directory).
|
2017-06-27 01:52:06 +02:00
|
|
|
|
|
|
|
#ifndef ROCKSDB_LITE
|
|
|
|
|
2020-04-02 01:57:03 +02:00
|
|
|
#include "rocksdb/env_encryption.h"
|
|
|
|
|
2017-06-27 01:52:06 +02:00
|
|
|
#include <algorithm>
|
2019-04-11 19:22:07 +02:00
|
|
|
#include <cassert>
|
2017-06-27 01:52:06 +02:00
|
|
|
#include <cctype>
|
|
|
|
#include <iostream>
|
|
|
|
|
2021-03-16 03:48:36 +01:00
|
|
|
#include "env/composite_env_wrapper.h"
|
2020-09-16 00:12:58 +02:00
|
|
|
#include "env/env_encryption_ctr.h"
|
2020-04-02 01:57:03 +02:00
|
|
|
#include "monitoring/perf_context_imp.h"
|
2020-09-16 00:12:58 +02:00
|
|
|
#include "rocksdb/convenience.h"
|
2021-03-16 03:48:36 +01:00
|
|
|
#include "rocksdb/io_status.h"
|
2021-03-15 12:32:24 +01:00
|
|
|
#include "rocksdb/system_clock.h"
|
2017-06-27 01:52:06 +02:00
|
|
|
#include "util/aligned_buffer.h"
|
|
|
|
#include "util/coding.h"
|
|
|
|
#include "util/random.h"
|
2020-09-16 00:12:58 +02:00
|
|
|
#include "util/string_util.h"
|
2017-06-27 01:52:06 +02:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2020-02-20 21:07:53 +01:00
|
|
|
namespace ROCKSDB_NAMESPACE {
|
2017-06-27 01:52:06 +02:00
|
|
|
|
|
|
|
#ifndef ROCKSDB_LITE
|
2020-09-16 00:12:58 +02:00
|
|
|
static constexpr char kROT13CipherName[] = "ROT13";
|
|
|
|
static constexpr char kCTRProviderName[] = "CTR";
|
|
|
|
|
|
|
|
Status BlockCipher::CreateFromString(const ConfigOptions& /*config_options*/,
|
|
|
|
const std::string& value,
|
|
|
|
std::shared_ptr<BlockCipher>* result) {
|
|
|
|
std::string id = value;
|
|
|
|
size_t colon = value.find(':');
|
|
|
|
if (colon != std::string::npos) {
|
|
|
|
id = value.substr(0, colon);
|
|
|
|
}
|
|
|
|
if (id == kROT13CipherName) {
|
|
|
|
if (colon != std::string::npos) {
|
|
|
|
size_t block_size = ParseSizeT(value.substr(colon + 1));
|
|
|
|
result->reset(new ROT13BlockCipher(block_size));
|
|
|
|
} else {
|
|
|
|
result->reset(new ROT13BlockCipher(32));
|
|
|
|
}
|
|
|
|
return Status::OK();
|
|
|
|
} else {
|
|
|
|
return Status::NotSupported("Could not find cipher ", value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Status EncryptionProvider::CreateFromString(
|
|
|
|
const ConfigOptions& /*config_options*/, const std::string& value,
|
|
|
|
std::shared_ptr<EncryptionProvider>* result) {
|
|
|
|
std::string id = value;
|
|
|
|
bool is_test = StartsWith(value, "test://");
|
|
|
|
Status status = Status::OK();
|
|
|
|
if (is_test) {
|
|
|
|
id = value.substr(strlen("test://"));
|
|
|
|
}
|
|
|
|
if (id == kCTRProviderName) {
|
|
|
|
result->reset(new CTREncryptionProvider());
|
|
|
|
} else if (is_test) {
|
|
|
|
result->reset(new CTREncryptionProvider());
|
|
|
|
} else {
|
|
|
|
return Status::NotSupported("Could not find provider ", value);
|
|
|
|
}
|
|
|
|
if (status.ok() && is_test) {
|
|
|
|
status = result->get()->TEST_Initialize();
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
2017-06-27 01:52:06 +02:00
|
|
|
|
2020-09-16 00:12:58 +02:00
|
|
|
std::shared_ptr<EncryptionProvider> EncryptionProvider::NewCTRProvider(
|
|
|
|
const std::shared_ptr<BlockCipher>& cipher) {
|
|
|
|
return std::make_shared<CTREncryptionProvider>(cipher);
|
|
|
|
}
|
2017-06-27 01:52:06 +02:00
|
|
|
|
|
|
|
// Read up to "n" bytes from the file. "scratch[0..n-1]" may be
|
|
|
|
// written by this routine. Sets "*result" to the data that was
|
|
|
|
// read (including if fewer than "n" bytes were successfully read).
|
|
|
|
// May set "*result" to point at data in "scratch[0..n-1]", so
|
|
|
|
// "scratch[0..n-1]" must be live when "*result" is used.
|
|
|
|
// If an error was encountered, returns a non-OK status.
|
|
|
|
//
|
|
|
|
// REQUIRES: External synchronization
|
2021-03-16 03:48:36 +01:00
|
|
|
IOStatus EncryptedSequentialFile::Read(size_t n, const IOOptions& options,
|
|
|
|
Slice* result, char* scratch,
|
|
|
|
IODebugContext* dbg) {
|
2020-06-22 22:25:41 +02:00
|
|
|
assert(scratch);
|
2021-03-16 03:48:36 +01:00
|
|
|
IOStatus io_s = file_->Read(n, options, result, scratch, dbg);
|
|
|
|
if (!io_s.ok()) {
|
|
|
|
return io_s;
|
2017-06-27 01:52:06 +02:00
|
|
|
}
|
2020-06-22 22:25:41 +02:00
|
|
|
{
|
|
|
|
PERF_TIMER_GUARD(decrypt_data_nanos);
|
2021-03-16 03:48:36 +01:00
|
|
|
io_s = status_to_io_status(
|
|
|
|
stream_->Decrypt(offset_, (char*)result->data(), result->size()));
|
2020-06-22 22:25:41 +02:00
|
|
|
}
|
2021-03-16 03:48:36 +01:00
|
|
|
if (io_s.ok()) {
|
|
|
|
offset_ += result->size(); // We've already ready data from disk, so update
|
|
|
|
// offset_ even if decryption fails.
|
|
|
|
}
|
|
|
|
return io_s;
|
2020-06-22 22:25:41 +02:00
|
|
|
}
|
2017-06-27 01:52:06 +02:00
|
|
|
|
2020-06-22 22:25:41 +02:00
|
|
|
// Skip "n" bytes from the file. This is guaranteed to be no
|
|
|
|
// slower that reading the same data, but may be faster.
|
|
|
|
//
|
|
|
|
// If end of file is reached, skipping will stop at the end of the
|
|
|
|
// file, and Skip will return OK.
|
|
|
|
//
|
|
|
|
// REQUIRES: External synchronization
|
2021-03-16 03:48:36 +01:00
|
|
|
IOStatus EncryptedSequentialFile::Skip(uint64_t n) {
|
2020-06-22 22:25:41 +02:00
|
|
|
auto status = file_->Skip(n);
|
|
|
|
if (!status.ok()) {
|
2017-06-27 01:52:06 +02:00
|
|
|
return status;
|
|
|
|
}
|
2020-06-22 22:25:41 +02:00
|
|
|
offset_ += n;
|
|
|
|
return status;
|
|
|
|
}
|
2017-06-27 01:52:06 +02:00
|
|
|
|
2020-06-22 22:25:41 +02:00
|
|
|
// Indicates the upper layers if the current SequentialFile implementation
|
|
|
|
// uses direct IO.
|
|
|
|
bool EncryptedSequentialFile::use_direct_io() const {
|
|
|
|
return file_->use_direct_io();
|
|
|
|
}
|
2017-06-27 01:52:06 +02:00
|
|
|
|
2020-06-22 22:25:41 +02:00
|
|
|
// Use the returned alignment value to allocate
|
|
|
|
// aligned buffer for Direct I/O
|
|
|
|
size_t EncryptedSequentialFile::GetRequiredBufferAlignment() const {
|
|
|
|
return file_->GetRequiredBufferAlignment();
|
|
|
|
}
|
2017-06-27 01:52:06 +02:00
|
|
|
|
|
|
|
// Remove any kind of caching of data from the offset to offset+length
|
|
|
|
// of this file. If the length is 0, then it refers to the end of file.
|
|
|
|
// If the system is not caching the file contents, then this is a noop.
|
2021-03-16 03:48:36 +01:00
|
|
|
IOStatus EncryptedSequentialFile::InvalidateCache(size_t offset,
|
|
|
|
size_t length) {
|
2020-06-22 22:25:41 +02:00
|
|
|
return file_->InvalidateCache(offset + prefixLength_, length);
|
|
|
|
}
|
2017-06-27 01:52:06 +02:00
|
|
|
|
|
|
|
// Positioned Read for direct I/O
|
|
|
|
// If Direct I/O enabled, offset, n, and scratch should be properly aligned
|
2021-03-16 03:48:36 +01:00
|
|
|
IOStatus EncryptedSequentialFile::PositionedRead(uint64_t offset, size_t n,
|
|
|
|
const IOOptions& options,
|
|
|
|
Slice* result, char* scratch,
|
|
|
|
IODebugContext* dbg) {
|
2020-06-22 22:25:41 +02:00
|
|
|
assert(scratch);
|
|
|
|
offset += prefixLength_; // Skip prefix
|
2021-03-16 03:48:36 +01:00
|
|
|
auto io_s = file_->PositionedRead(offset, n, options, result, scratch, dbg);
|
|
|
|
if (!io_s.ok()) {
|
|
|
|
return io_s;
|
2017-06-27 01:52:06 +02:00
|
|
|
}
|
2020-06-22 22:25:41 +02:00
|
|
|
offset_ = offset + result->size();
|
|
|
|
{
|
|
|
|
PERF_TIMER_GUARD(decrypt_data_nanos);
|
2021-03-16 03:48:36 +01:00
|
|
|
io_s = status_to_io_status(
|
|
|
|
stream_->Decrypt(offset, (char*)result->data(), result->size()));
|
2020-06-22 22:25:41 +02:00
|
|
|
}
|
2021-03-16 03:48:36 +01:00
|
|
|
return io_s;
|
2020-06-22 22:25:41 +02:00
|
|
|
}
|
2017-06-27 01:52:06 +02:00
|
|
|
|
|
|
|
// Read up to "n" bytes from the file starting at "offset".
|
|
|
|
// "scratch[0..n-1]" may be written by this routine. Sets "*result"
|
|
|
|
// to the data that was read (including if fewer than "n" bytes were
|
|
|
|
// successfully read). May set "*result" to point at data in
|
|
|
|
// "scratch[0..n-1]", so "scratch[0..n-1]" must be live when
|
|
|
|
// "*result" is used. If an error was encountered, returns a non-OK
|
|
|
|
// status.
|
|
|
|
//
|
|
|
|
// Safe for concurrent use by multiple threads.
|
|
|
|
// If Direct I/O enabled, offset, n, and scratch should be aligned properly.
|
2021-03-16 03:48:36 +01:00
|
|
|
IOStatus EncryptedRandomAccessFile::Read(uint64_t offset, size_t n,
|
|
|
|
const IOOptions& options,
|
|
|
|
Slice* result, char* scratch,
|
|
|
|
IODebugContext* dbg) const {
|
2020-06-22 22:25:41 +02:00
|
|
|
assert(scratch);
|
|
|
|
offset += prefixLength_;
|
2021-03-16 03:48:36 +01:00
|
|
|
auto io_s = file_->Read(offset, n, options, result, scratch, dbg);
|
|
|
|
if (!io_s.ok()) {
|
|
|
|
return io_s;
|
2017-06-27 01:52:06 +02:00
|
|
|
}
|
2020-06-22 22:25:41 +02:00
|
|
|
{
|
|
|
|
PERF_TIMER_GUARD(decrypt_data_nanos);
|
2021-03-16 03:48:36 +01:00
|
|
|
io_s = status_to_io_status(
|
|
|
|
stream_->Decrypt(offset, (char*)result->data(), result->size()));
|
2020-06-22 22:25:41 +02:00
|
|
|
}
|
2021-03-16 03:48:36 +01:00
|
|
|
return io_s;
|
2020-06-22 22:25:41 +02:00
|
|
|
}
|
2017-06-27 01:52:06 +02:00
|
|
|
|
|
|
|
// Readahead the file starting from offset by n bytes for caching.
|
2021-03-16 03:48:36 +01:00
|
|
|
IOStatus EncryptedRandomAccessFile::Prefetch(uint64_t offset, size_t n,
|
|
|
|
const IOOptions& options,
|
|
|
|
IODebugContext* dbg) {
|
2020-06-22 22:25:41 +02:00
|
|
|
// return Status::OK();
|
2021-03-16 03:48:36 +01:00
|
|
|
return file_->Prefetch(offset + prefixLength_, n, options, dbg);
|
2020-06-22 22:25:41 +02:00
|
|
|
}
|
2017-06-27 01:52:06 +02:00
|
|
|
|
|
|
|
// Tries to get an unique ID for this file that will be the same each time
|
|
|
|
// the file is opened (and will stay the same while the file is open).
|
|
|
|
// Furthermore, it tries to make this ID at most "max_size" bytes. If such an
|
|
|
|
// ID can be created this function returns the length of the ID and places it
|
|
|
|
// in "id"; otherwise, this function returns 0, in which case "id"
|
|
|
|
// may not have been modified.
|
|
|
|
//
|
|
|
|
// This function guarantees, for IDs from a given environment, two unique ids
|
2018-03-08 19:18:34 +01:00
|
|
|
// cannot be made equal to each other by adding arbitrary bytes to one of
|
2017-06-27 01:52:06 +02:00
|
|
|
// them. That is, no unique ID is the prefix of another.
|
|
|
|
//
|
|
|
|
// This function guarantees that the returned ID will not be interpretable as
|
|
|
|
// a single varint.
|
|
|
|
//
|
|
|
|
// Note: these IDs are only valid for the duration of the process.
|
2020-06-22 22:25:41 +02:00
|
|
|
size_t EncryptedRandomAccessFile::GetUniqueId(char* id, size_t max_size) const {
|
|
|
|
return file_->GetUniqueId(id, max_size);
|
|
|
|
};
|
2017-06-27 01:52:06 +02:00
|
|
|
|
2020-06-22 22:25:41 +02:00
|
|
|
void EncryptedRandomAccessFile::Hint(AccessPattern pattern) {
|
|
|
|
file_->Hint(pattern);
|
|
|
|
}
|
2017-06-27 01:52:06 +02:00
|
|
|
|
|
|
|
// Indicates the upper layers if the current RandomAccessFile implementation
|
|
|
|
// uses direct IO.
|
2020-06-22 22:25:41 +02:00
|
|
|
bool EncryptedRandomAccessFile::use_direct_io() const {
|
|
|
|
return file_->use_direct_io();
|
|
|
|
}
|
2017-06-27 01:52:06 +02:00
|
|
|
|
|
|
|
// Use the returned alignment value to allocate
|
|
|
|
// aligned buffer for Direct I/O
|
2020-06-22 22:25:41 +02:00
|
|
|
size_t EncryptedRandomAccessFile::GetRequiredBufferAlignment() const {
|
|
|
|
return file_->GetRequiredBufferAlignment();
|
|
|
|
}
|
2017-06-27 01:52:06 +02:00
|
|
|
|
|
|
|
// Remove any kind of caching of data from the offset to offset+length
|
|
|
|
// of this file. If the length is 0, then it refers to the end of file.
|
|
|
|
// If the system is not caching the file contents, then this is a noop.
|
2021-03-16 03:48:36 +01:00
|
|
|
IOStatus EncryptedRandomAccessFile::InvalidateCache(size_t offset,
|
|
|
|
size_t length) {
|
2020-06-22 22:25:41 +02:00
|
|
|
return file_->InvalidateCache(offset + prefixLength_, length);
|
|
|
|
}
|
2017-06-27 01:52:06 +02:00
|
|
|
|
|
|
|
// A file abstraction for sequential writing. The implementation
|
|
|
|
// must provide buffering since callers may append small fragments
|
|
|
|
// at a time to the file.
|
2021-03-16 03:48:36 +01:00
|
|
|
IOStatus EncryptedWritableFile::Append(const Slice& data,
|
|
|
|
const IOOptions& options,
|
|
|
|
IODebugContext* dbg) {
|
2020-06-22 22:25:41 +02:00
|
|
|
AlignedBuffer buf;
|
|
|
|
Slice dataToAppend(data);
|
|
|
|
if (data.size() > 0) {
|
2021-03-16 03:48:36 +01:00
|
|
|
auto offset = file_->GetFileSize(options, dbg); // size including prefix
|
2020-06-22 22:25:41 +02:00
|
|
|
// Encrypt in cloned buffer
|
|
|
|
buf.Alignment(GetRequiredBufferAlignment());
|
|
|
|
buf.AllocateNewBuffer(data.size());
|
|
|
|
// TODO (sagar0): Modify AlignedBuffer.Append to allow doing a memmove
|
|
|
|
// so that the next two lines can be replaced with buf.Append().
|
|
|
|
memmove(buf.BufferStart(), data.data(), data.size());
|
|
|
|
buf.Size(data.size());
|
2021-03-16 03:48:36 +01:00
|
|
|
IOStatus io_s;
|
2020-06-22 22:25:41 +02:00
|
|
|
{
|
|
|
|
PERF_TIMER_GUARD(encrypt_data_nanos);
|
2021-03-16 03:48:36 +01:00
|
|
|
io_s = status_to_io_status(
|
|
|
|
stream_->Encrypt(offset, buf.BufferStart(), buf.CurrentSize()));
|
2017-06-27 01:52:06 +02:00
|
|
|
}
|
2021-03-16 03:48:36 +01:00
|
|
|
if (!io_s.ok()) {
|
|
|
|
return io_s;
|
2017-06-27 01:52:06 +02:00
|
|
|
}
|
2020-06-22 22:25:41 +02:00
|
|
|
dataToAppend = Slice(buf.BufferStart(), buf.CurrentSize());
|
|
|
|
}
|
2021-03-16 03:48:36 +01:00
|
|
|
return file_->Append(dataToAppend, options, dbg);
|
2020-06-22 22:25:41 +02:00
|
|
|
}
|
2017-06-27 01:52:06 +02:00
|
|
|
|
2021-03-16 03:48:36 +01:00
|
|
|
IOStatus EncryptedWritableFile::PositionedAppend(const Slice& data,
|
|
|
|
uint64_t offset,
|
|
|
|
const IOOptions& options,
|
|
|
|
IODebugContext* dbg) {
|
2020-06-22 22:25:41 +02:00
|
|
|
AlignedBuffer buf;
|
|
|
|
Slice dataToAppend(data);
|
|
|
|
offset += prefixLength_;
|
|
|
|
if (data.size() > 0) {
|
|
|
|
// Encrypt in cloned buffer
|
|
|
|
buf.Alignment(GetRequiredBufferAlignment());
|
|
|
|
buf.AllocateNewBuffer(data.size());
|
|
|
|
memmove(buf.BufferStart(), data.data(), data.size());
|
|
|
|
buf.Size(data.size());
|
2021-03-16 03:48:36 +01:00
|
|
|
IOStatus io_s;
|
2020-06-22 22:25:41 +02:00
|
|
|
{
|
|
|
|
PERF_TIMER_GUARD(encrypt_data_nanos);
|
2021-03-16 03:48:36 +01:00
|
|
|
io_s = status_to_io_status(
|
|
|
|
stream_->Encrypt(offset, buf.BufferStart(), buf.CurrentSize()));
|
2017-06-27 01:52:06 +02:00
|
|
|
}
|
2021-03-16 03:48:36 +01:00
|
|
|
if (!io_s.ok()) {
|
|
|
|
return io_s;
|
2017-06-27 01:52:06 +02:00
|
|
|
}
|
2020-06-22 22:25:41 +02:00
|
|
|
dataToAppend = Slice(buf.BufferStart(), buf.CurrentSize());
|
|
|
|
}
|
2021-03-16 03:48:36 +01:00
|
|
|
return file_->PositionedAppend(dataToAppend, offset, options, dbg);
|
2020-06-22 22:25:41 +02:00
|
|
|
}
|
2017-06-27 01:52:06 +02:00
|
|
|
|
|
|
|
// Indicates the upper layers if the current WritableFile implementation
|
|
|
|
// uses direct IO.
|
2020-06-22 22:25:41 +02:00
|
|
|
bool EncryptedWritableFile::use_direct_io() const {
|
|
|
|
return file_->use_direct_io();
|
|
|
|
}
|
2017-06-27 01:52:06 +02:00
|
|
|
|
|
|
|
// Use the returned alignment value to allocate
|
|
|
|
// aligned buffer for Direct I/O
|
2020-06-22 22:25:41 +02:00
|
|
|
size_t EncryptedWritableFile::GetRequiredBufferAlignment() const {
|
|
|
|
return file_->GetRequiredBufferAlignment();
|
|
|
|
}
|
2017-06-27 01:52:06 +02:00
|
|
|
|
2020-06-22 22:25:41 +02:00
|
|
|
/*
|
|
|
|
* Get the size of valid data in the file.
|
|
|
|
*/
|
2021-03-16 03:48:36 +01:00
|
|
|
uint64_t EncryptedWritableFile::GetFileSize(const IOOptions& options,
|
|
|
|
IODebugContext* dbg) {
|
|
|
|
return file_->GetFileSize(options, dbg) - prefixLength_;
|
2020-06-22 22:25:41 +02:00
|
|
|
}
|
2017-06-27 01:52:06 +02:00
|
|
|
|
2021-03-16 03:48:36 +01:00
|
|
|
// Truncate is necessary to trim the file to the correct size
|
|
|
|
// before closing. It is not always possible to keep track of the file
|
|
|
|
// size due to whole pages writes. The behavior is undefined if called
|
|
|
|
// with other writes to follow.
|
|
|
|
IOStatus EncryptedWritableFile::Truncate(uint64_t size,
|
|
|
|
const IOOptions& options,
|
|
|
|
IODebugContext* dbg) {
|
|
|
|
return file_->Truncate(size + prefixLength_, options, dbg);
|
2020-06-22 22:25:41 +02:00
|
|
|
}
|
2017-06-27 01:52:06 +02:00
|
|
|
|
2021-03-16 03:48:36 +01:00
|
|
|
// Remove any kind of caching of data from the offset to offset+length
|
|
|
|
// of this file. If the length is 0, then it refers to the end of file.
|
|
|
|
// If the system is not caching the file contents, then this is a noop.
|
|
|
|
// This call has no effect on dirty pages in the cache.
|
|
|
|
IOStatus EncryptedWritableFile::InvalidateCache(size_t offset, size_t length) {
|
2020-06-22 22:25:41 +02:00
|
|
|
return file_->InvalidateCache(offset + prefixLength_, length);
|
|
|
|
}
|
2017-06-27 01:52:06 +02:00
|
|
|
|
2021-03-16 03:48:36 +01:00
|
|
|
// Sync a file range with disk.
|
|
|
|
// offset is the starting byte of the file range to be synchronized.
|
|
|
|
// nbytes specifies the length of the range to be synchronized.
|
|
|
|
// This asks the OS to initiate flushing the cached data to disk,
|
|
|
|
// without waiting for completion.
|
|
|
|
// Default implementation does nothing.
|
|
|
|
IOStatus EncryptedWritableFile::RangeSync(uint64_t offset, uint64_t nbytes,
|
|
|
|
const IOOptions& options,
|
|
|
|
IODebugContext* dbg) {
|
|
|
|
return file_->RangeSync(offset + prefixLength_, nbytes, options, dbg);
|
|
|
|
}
|
|
|
|
|
|
|
|
// PrepareWrite performs any necessary preparation for a write
|
|
|
|
// before the write actually occurs. This allows for pre-allocation
|
|
|
|
// of space on devices where it can result in less file
|
|
|
|
// fragmentation and/or less waste from over-zealous filesystem
|
|
|
|
// pre-allocation.
|
|
|
|
void EncryptedWritableFile::PrepareWrite(size_t offset, size_t len,
|
|
|
|
const IOOptions& options,
|
|
|
|
IODebugContext* dbg) {
|
|
|
|
file_->PrepareWrite(offset + prefixLength_, len, options, dbg);
|
|
|
|
}
|
|
|
|
|
2021-04-20 17:41:32 +02:00
|
|
|
void EncryptedWritableFile::SetPreallocationBlockSize(size_t size) {
|
|
|
|
// the size here doesn't need to include prefixLength_, as it's a
|
|
|
|
// configuration will be use for `PrepareWrite()`.
|
|
|
|
file_->SetPreallocationBlockSize(size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void EncryptedWritableFile::GetPreallocationStatus(
|
|
|
|
size_t* block_size, size_t* last_allocated_block) {
|
|
|
|
file_->GetPreallocationStatus(block_size, last_allocated_block);
|
|
|
|
}
|
|
|
|
|
2021-03-16 03:48:36 +01:00
|
|
|
// Pre-allocates space for a file.
|
|
|
|
IOStatus EncryptedWritableFile::Allocate(uint64_t offset, uint64_t len,
|
|
|
|
const IOOptions& options,
|
|
|
|
IODebugContext* dbg) {
|
|
|
|
return file_->Allocate(offset + prefixLength_, len, options, dbg);
|
|
|
|
}
|
|
|
|
|
|
|
|
IOStatus EncryptedWritableFile::Flush(const IOOptions& options,
|
|
|
|
IODebugContext* dbg) {
|
|
|
|
return file_->Flush(options, dbg);
|
2020-06-22 22:25:41 +02:00
|
|
|
}
|
2017-06-27 01:52:06 +02:00
|
|
|
|
2021-03-16 03:48:36 +01:00
|
|
|
IOStatus EncryptedWritableFile::Sync(const IOOptions& options,
|
|
|
|
IODebugContext* dbg) {
|
|
|
|
return file_->Sync(options, dbg);
|
2020-06-22 22:25:41 +02:00
|
|
|
}
|
2017-06-27 01:52:06 +02:00
|
|
|
|
2021-03-16 03:48:36 +01:00
|
|
|
IOStatus EncryptedWritableFile::Close(const IOOptions& options,
|
|
|
|
IODebugContext* dbg) {
|
|
|
|
return file_->Close(options, dbg);
|
2020-06-22 22:25:41 +02:00
|
|
|
}
|
2017-06-27 01:52:06 +02:00
|
|
|
|
|
|
|
// A file abstraction for random reading and writing.
|
|
|
|
|
2020-06-22 22:25:41 +02:00
|
|
|
// Indicates if the class makes use of direct I/O
|
|
|
|
// If false you must pass aligned buffer to Write()
|
|
|
|
bool EncryptedRandomRWFile::use_direct_io() const {
|
|
|
|
return file_->use_direct_io();
|
|
|
|
}
|
2017-06-27 01:52:06 +02:00
|
|
|
|
|
|
|
// Use the returned alignment value to allocate
|
|
|
|
// aligned buffer for Direct I/O
|
2020-06-22 22:25:41 +02:00
|
|
|
size_t EncryptedRandomRWFile::GetRequiredBufferAlignment() const {
|
|
|
|
return file_->GetRequiredBufferAlignment();
|
|
|
|
}
|
2017-06-27 01:52:06 +02:00
|
|
|
|
|
|
|
// Write bytes in `data` at offset `offset`, Returns Status::OK() on success.
|
|
|
|
// Pass aligned buffer when use_direct_io() returns true.
|
2021-03-16 03:48:36 +01:00
|
|
|
IOStatus EncryptedRandomRWFile::Write(uint64_t offset, const Slice& data,
|
|
|
|
const IOOptions& options,
|
|
|
|
IODebugContext* dbg) {
|
2020-06-22 22:25:41 +02:00
|
|
|
AlignedBuffer buf;
|
|
|
|
Slice dataToWrite(data);
|
|
|
|
offset += prefixLength_;
|
|
|
|
if (data.size() > 0) {
|
|
|
|
// Encrypt in cloned buffer
|
|
|
|
buf.Alignment(GetRequiredBufferAlignment());
|
|
|
|
buf.AllocateNewBuffer(data.size());
|
|
|
|
memmove(buf.BufferStart(), data.data(), data.size());
|
|
|
|
buf.Size(data.size());
|
2021-03-16 03:48:36 +01:00
|
|
|
IOStatus io_s;
|
2020-06-22 22:25:41 +02:00
|
|
|
{
|
|
|
|
PERF_TIMER_GUARD(encrypt_data_nanos);
|
2021-03-16 03:48:36 +01:00
|
|
|
io_s = status_to_io_status(
|
|
|
|
stream_->Encrypt(offset, buf.BufferStart(), buf.CurrentSize()));
|
2017-06-27 01:52:06 +02:00
|
|
|
}
|
2021-03-16 03:48:36 +01:00
|
|
|
if (!io_s.ok()) {
|
|
|
|
return io_s;
|
2020-06-22 22:25:41 +02:00
|
|
|
}
|
|
|
|
dataToWrite = Slice(buf.BufferStart(), buf.CurrentSize());
|
2017-06-27 01:52:06 +02:00
|
|
|
}
|
2021-03-16 03:48:36 +01:00
|
|
|
return file_->Write(offset, dataToWrite, options, dbg);
|
2020-06-22 22:25:41 +02:00
|
|
|
}
|
2017-06-27 01:52:06 +02:00
|
|
|
|
|
|
|
// Read up to `n` bytes starting from offset `offset` and store them in
|
|
|
|
// result, provided `scratch` size should be at least `n`.
|
|
|
|
// Returns Status::OK() on success.
|
2021-03-16 03:48:36 +01:00
|
|
|
IOStatus EncryptedRandomRWFile::Read(uint64_t offset, size_t n,
|
|
|
|
const IOOptions& options, Slice* result,
|
|
|
|
char* scratch, IODebugContext* dbg) const {
|
2020-06-22 22:25:41 +02:00
|
|
|
assert(scratch);
|
|
|
|
offset += prefixLength_;
|
2021-03-16 03:48:36 +01:00
|
|
|
auto status = file_->Read(offset, n, options, result, scratch, dbg);
|
2020-06-22 22:25:41 +02:00
|
|
|
if (!status.ok()) {
|
2017-06-27 01:52:06 +02:00
|
|
|
return status;
|
|
|
|
}
|
2020-06-22 22:25:41 +02:00
|
|
|
{
|
|
|
|
PERF_TIMER_GUARD(decrypt_data_nanos);
|
2021-03-16 03:48:36 +01:00
|
|
|
status = status_to_io_status(
|
|
|
|
stream_->Decrypt(offset, (char*)result->data(), result->size()));
|
2020-06-22 22:25:41 +02:00
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
2017-06-27 01:52:06 +02:00
|
|
|
|
2021-03-16 03:48:36 +01:00
|
|
|
IOStatus EncryptedRandomRWFile::Flush(const IOOptions& options,
|
|
|
|
IODebugContext* dbg) {
|
|
|
|
return file_->Flush(options, dbg);
|
|
|
|
}
|
2017-06-27 01:52:06 +02:00
|
|
|
|
2021-03-16 03:48:36 +01:00
|
|
|
IOStatus EncryptedRandomRWFile::Sync(const IOOptions& options,
|
|
|
|
IODebugContext* dbg) {
|
|
|
|
return file_->Sync(options, dbg);
|
|
|
|
}
|
2017-06-27 01:52:06 +02:00
|
|
|
|
2021-03-16 03:48:36 +01:00
|
|
|
IOStatus EncryptedRandomRWFile::Fsync(const IOOptions& options,
|
|
|
|
IODebugContext* dbg) {
|
|
|
|
return file_->Fsync(options, dbg);
|
|
|
|
}
|
2017-06-27 01:52:06 +02:00
|
|
|
|
2021-03-16 03:48:36 +01:00
|
|
|
IOStatus EncryptedRandomRWFile::Close(const IOOptions& options,
|
|
|
|
IODebugContext* dbg) {
|
|
|
|
return file_->Close(options, dbg);
|
|
|
|
}
|
2017-06-27 01:52:06 +02:00
|
|
|
|
2021-03-16 03:48:36 +01:00
|
|
|
namespace {
|
|
|
|
// EncryptedFileSystemImpl implements an FileSystemWrapper that adds encryption
|
|
|
|
// to files stored on disk.
|
|
|
|
class EncryptedFileSystemImpl : public EncryptedFileSystem {
|
|
|
|
public:
|
|
|
|
const char* Name() const override { return "EncryptedFS"; }
|
2020-09-16 00:12:58 +02:00
|
|
|
// Returns the raw encryption provider that should be used to write the input
|
|
|
|
// encrypted file. If there is no such provider, NotFound is returned.
|
2021-03-16 03:48:36 +01:00
|
|
|
IOStatus GetWritableProvider(const std::string& /*fname*/,
|
|
|
|
EncryptionProvider** result) {
|
2020-09-16 00:12:58 +02:00
|
|
|
if (provider_) {
|
|
|
|
*result = provider_.get();
|
2021-03-16 03:48:36 +01:00
|
|
|
return IOStatus::OK();
|
2020-09-16 00:12:58 +02:00
|
|
|
} else {
|
|
|
|
*result = nullptr;
|
2021-03-16 03:48:36 +01:00
|
|
|
return IOStatus::NotFound("No WriteProvider specified");
|
2020-09-16 00:12:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the raw encryption provider that should be used to read the input
|
|
|
|
// encrypted file. If there is no such provider, NotFound is returned.
|
2021-03-16 03:48:36 +01:00
|
|
|
IOStatus GetReadableProvider(const std::string& /*fname*/,
|
|
|
|
EncryptionProvider** result) {
|
2020-09-16 00:12:58 +02:00
|
|
|
if (provider_) {
|
|
|
|
*result = provider_.get();
|
2021-03-16 03:48:36 +01:00
|
|
|
return IOStatus::OK();
|
2020-09-16 00:12:58 +02:00
|
|
|
} else {
|
|
|
|
*result = nullptr;
|
2021-03-16 03:48:36 +01:00
|
|
|
return IOStatus::NotFound("No Provider specified");
|
2020-09-16 00:12:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Creates a CipherStream for the underlying file/name using the options
|
|
|
|
// If a writable provider is found and encryption is enabled, uses
|
|
|
|
// this provider to create a cipher stream.
|
|
|
|
// @param fname Name of the writable file
|
|
|
|
// @param underlying The underlying "raw" file
|
|
|
|
// @param options Options for creating the file/cipher
|
|
|
|
// @param prefix_length Returns the length of the encryption prefix used for
|
|
|
|
// this file
|
|
|
|
// @param stream Returns the cipher stream to use for this file if it
|
|
|
|
// should be encrypted
|
|
|
|
// @return OK on success, non-OK on failure.
|
|
|
|
template <class TypeFile>
|
2021-03-16 03:48:36 +01:00
|
|
|
IOStatus CreateWritableCipherStream(
|
2020-09-16 00:12:58 +02:00
|
|
|
const std::string& fname, const std::unique_ptr<TypeFile>& underlying,
|
2021-03-16 03:48:36 +01:00
|
|
|
const FileOptions& options, size_t* prefix_length,
|
|
|
|
std::unique_ptr<BlockAccessCipherStream>* stream, IODebugContext* dbg) {
|
2020-09-16 00:12:58 +02:00
|
|
|
EncryptionProvider* provider = nullptr;
|
|
|
|
*prefix_length = 0;
|
2021-03-16 03:48:36 +01:00
|
|
|
IOStatus status = GetWritableProvider(fname, &provider);
|
2020-09-16 00:12:58 +02:00
|
|
|
if (!status.ok()) {
|
|
|
|
return status;
|
|
|
|
} else if (provider != nullptr) {
|
|
|
|
// Initialize & write prefix (if needed)
|
|
|
|
AlignedBuffer buffer;
|
|
|
|
Slice prefix;
|
|
|
|
*prefix_length = provider->GetPrefixLength();
|
|
|
|
if (*prefix_length > 0) {
|
|
|
|
// Initialize prefix
|
|
|
|
buffer.Alignment(underlying->GetRequiredBufferAlignment());
|
|
|
|
buffer.AllocateNewBuffer(*prefix_length);
|
2021-03-16 03:48:36 +01:00
|
|
|
status = status_to_io_status(provider->CreateNewPrefix(
|
|
|
|
fname, buffer.BufferStart(), *prefix_length));
|
2020-09-16 00:12:58 +02:00
|
|
|
if (status.ok()) {
|
|
|
|
buffer.Size(*prefix_length);
|
|
|
|
prefix = Slice(buffer.BufferStart(), buffer.CurrentSize());
|
|
|
|
// Write prefix
|
2021-03-16 03:48:36 +01:00
|
|
|
status = underlying->Append(prefix, options.io_options, dbg);
|
2020-09-16 00:12:58 +02:00
|
|
|
}
|
|
|
|
if (!status.ok()) {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Create cipher stream
|
2021-03-16 03:48:36 +01:00
|
|
|
status = status_to_io_status(
|
|
|
|
provider->CreateCipherStream(fname, options, prefix, stream));
|
2020-09-16 00:12:58 +02:00
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class TypeFile>
|
2021-03-16 03:48:36 +01:00
|
|
|
IOStatus CreateWritableEncryptedFile(const std::string& fname,
|
|
|
|
std::unique_ptr<TypeFile>& underlying,
|
|
|
|
const FileOptions& options,
|
|
|
|
std::unique_ptr<TypeFile>* result,
|
|
|
|
IODebugContext* dbg) {
|
2020-09-16 00:12:58 +02:00
|
|
|
// Create cipher stream
|
|
|
|
std::unique_ptr<BlockAccessCipherStream> stream;
|
|
|
|
size_t prefix_length;
|
2021-03-16 03:48:36 +01:00
|
|
|
IOStatus status = CreateWritableCipherStream(fname, underlying, options,
|
|
|
|
&prefix_length, &stream, dbg);
|
2020-09-16 00:12:58 +02:00
|
|
|
if (status.ok()) {
|
|
|
|
if (stream) {
|
|
|
|
result->reset(new EncryptedWritableFile(
|
|
|
|
std::move(underlying), std::move(stream), prefix_length));
|
|
|
|
} else {
|
|
|
|
result->reset(underlying.release());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Creates a CipherStream for the underlying file/name using the options
|
|
|
|
// If a writable provider is found and encryption is enabled, uses
|
|
|
|
// this provider to create a cipher stream.
|
|
|
|
// @param fname Name of the writable file
|
|
|
|
// @param underlying The underlying "raw" file
|
|
|
|
// @param options Options for creating the file/cipher
|
|
|
|
// @param prefix_length Returns the length of the encryption prefix used for
|
|
|
|
// this file
|
|
|
|
// @param stream Returns the cipher stream to use for this file if it
|
|
|
|
// should be encrypted
|
|
|
|
// @return OK on success, non-OK on failure.
|
|
|
|
template <class TypeFile>
|
2021-03-16 03:48:36 +01:00
|
|
|
IOStatus CreateRandomWriteCipherStream(
|
2020-09-16 00:12:58 +02:00
|
|
|
const std::string& fname, const std::unique_ptr<TypeFile>& underlying,
|
2021-03-16 03:48:36 +01:00
|
|
|
const FileOptions& options, size_t* prefix_length,
|
|
|
|
std::unique_ptr<BlockAccessCipherStream>* stream, IODebugContext* dbg) {
|
2020-09-16 00:12:58 +02:00
|
|
|
EncryptionProvider* provider = nullptr;
|
|
|
|
*prefix_length = 0;
|
2021-03-16 03:48:36 +01:00
|
|
|
IOStatus io_s = GetWritableProvider(fname, &provider);
|
|
|
|
if (!io_s.ok()) {
|
|
|
|
return io_s;
|
2020-09-16 00:12:58 +02:00
|
|
|
} else if (provider != nullptr) {
|
|
|
|
// Initialize & write prefix (if needed)
|
|
|
|
AlignedBuffer buffer;
|
|
|
|
Slice prefix;
|
|
|
|
*prefix_length = provider->GetPrefixLength();
|
|
|
|
if (*prefix_length > 0) {
|
|
|
|
// Initialize prefix
|
|
|
|
buffer.Alignment(underlying->GetRequiredBufferAlignment());
|
|
|
|
buffer.AllocateNewBuffer(*prefix_length);
|
2021-03-16 03:48:36 +01:00
|
|
|
io_s = status_to_io_status(provider->CreateNewPrefix(
|
|
|
|
fname, buffer.BufferStart(), *prefix_length));
|
|
|
|
if (io_s.ok()) {
|
2020-09-16 00:12:58 +02:00
|
|
|
buffer.Size(*prefix_length);
|
|
|
|
prefix = Slice(buffer.BufferStart(), buffer.CurrentSize());
|
|
|
|
// Write prefix
|
2021-03-16 03:48:36 +01:00
|
|
|
io_s = underlying->Write(0, prefix, options.io_options, dbg);
|
2020-09-16 00:12:58 +02:00
|
|
|
}
|
2021-03-16 03:48:36 +01:00
|
|
|
if (!io_s.ok()) {
|
|
|
|
return io_s;
|
2020-09-16 00:12:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Create cipher stream
|
2021-03-16 03:48:36 +01:00
|
|
|
io_s = status_to_io_status(
|
|
|
|
provider->CreateCipherStream(fname, options, prefix, stream));
|
2020-09-16 00:12:58 +02:00
|
|
|
}
|
2021-03-16 03:48:36 +01:00
|
|
|
return io_s;
|
2020-09-16 00:12:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Creates a CipherStream for the underlying file/name using the options
|
|
|
|
// If a readable provider is found and the file is encrypted, uses
|
|
|
|
// this provider to create a cipher stream.
|
|
|
|
// @param fname Name of the writable file
|
|
|
|
// @param underlying The underlying "raw" file
|
|
|
|
// @param options Options for creating the file/cipher
|
|
|
|
// @param prefix_length Returns the length of the encryption prefix used for
|
|
|
|
// this file
|
|
|
|
// @param stream Returns the cipher stream to use for this file if it
|
|
|
|
// is encrypted
|
|
|
|
// @return OK on success, non-OK on failure.
|
|
|
|
template <class TypeFile>
|
2021-03-16 03:48:36 +01:00
|
|
|
IOStatus CreateSequentialCipherStream(
|
2020-09-16 00:12:58 +02:00
|
|
|
const std::string& fname, const std::unique_ptr<TypeFile>& underlying,
|
2021-03-16 03:48:36 +01:00
|
|
|
const FileOptions& options, size_t* prefix_length,
|
|
|
|
std::unique_ptr<BlockAccessCipherStream>* stream, IODebugContext* dbg) {
|
2020-09-16 00:12:58 +02:00
|
|
|
// Read prefix (if needed)
|
|
|
|
AlignedBuffer buffer;
|
|
|
|
Slice prefix;
|
|
|
|
*prefix_length = provider_->GetPrefixLength();
|
|
|
|
if (*prefix_length > 0) {
|
|
|
|
// Read prefix
|
|
|
|
buffer.Alignment(underlying->GetRequiredBufferAlignment());
|
|
|
|
buffer.AllocateNewBuffer(*prefix_length);
|
2021-03-16 03:48:36 +01:00
|
|
|
IOStatus status = underlying->Read(*prefix_length, options.io_options,
|
|
|
|
&prefix, buffer.BufferStart(), dbg);
|
2020-09-16 00:12:58 +02:00
|
|
|
if (!status.ok()) {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
buffer.Size(*prefix_length);
|
|
|
|
}
|
2021-03-16 03:48:36 +01:00
|
|
|
return status_to_io_status(
|
|
|
|
provider_->CreateCipherStream(fname, options, prefix, stream));
|
2020-09-16 00:12:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Creates a CipherStream for the underlying file/name using the options
|
|
|
|
// If a readable provider is found and the file is encrypted, uses
|
|
|
|
// this provider to create a cipher stream.
|
|
|
|
// @param fname Name of the writable file
|
|
|
|
// @param underlying The underlying "raw" file
|
|
|
|
// @param options Options for creating the file/cipher
|
|
|
|
// @param prefix_length Returns the length of the encryption prefix used for
|
|
|
|
// this file
|
|
|
|
// @param stream Returns the cipher stream to use for this file if it
|
|
|
|
// is encrypted
|
|
|
|
// @return OK on success, non-OK on failure.
|
|
|
|
template <class TypeFile>
|
2021-03-16 03:48:36 +01:00
|
|
|
IOStatus CreateRandomReadCipherStream(
|
2020-09-16 00:12:58 +02:00
|
|
|
const std::string& fname, const std::unique_ptr<TypeFile>& underlying,
|
2021-03-16 03:48:36 +01:00
|
|
|
const FileOptions& options, size_t* prefix_length,
|
|
|
|
std::unique_ptr<BlockAccessCipherStream>* stream, IODebugContext* dbg) {
|
2020-09-16 00:12:58 +02:00
|
|
|
// Read prefix (if needed)
|
|
|
|
AlignedBuffer buffer;
|
|
|
|
Slice prefix;
|
|
|
|
*prefix_length = provider_->GetPrefixLength();
|
|
|
|
if (*prefix_length > 0) {
|
|
|
|
// Read prefix
|
|
|
|
buffer.Alignment(underlying->GetRequiredBufferAlignment());
|
|
|
|
buffer.AllocateNewBuffer(*prefix_length);
|
2021-03-16 03:48:36 +01:00
|
|
|
IOStatus status = underlying->Read(0, *prefix_length, options.io_options,
|
|
|
|
&prefix, buffer.BufferStart(), dbg);
|
2020-09-16 00:12:58 +02:00
|
|
|
if (!status.ok()) {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
buffer.Size(*prefix_length);
|
|
|
|
}
|
2021-03-16 03:48:36 +01:00
|
|
|
return status_to_io_status(
|
|
|
|
provider_->CreateCipherStream(fname, options, prefix, stream));
|
2020-09-16 00:12:58 +02:00
|
|
|
}
|
|
|
|
|
2017-06-27 01:52:06 +02:00
|
|
|
public:
|
2021-03-16 03:48:36 +01:00
|
|
|
EncryptedFileSystemImpl(const std::shared_ptr<FileSystem>& base,
|
|
|
|
const std::shared_ptr<EncryptionProvider>& provider)
|
|
|
|
: EncryptedFileSystem(base) {
|
2017-06-27 01:52:06 +02:00
|
|
|
provider_ = provider;
|
|
|
|
}
|
|
|
|
|
2021-03-16 03:48:36 +01:00
|
|
|
Status AddCipher(const std::string& descriptor, const char* cipher,
|
|
|
|
size_t len, bool for_write) override {
|
|
|
|
return provider_->AddCipher(descriptor, cipher, len, for_write);
|
|
|
|
}
|
|
|
|
|
2017-06-27 01:52:06 +02:00
|
|
|
// NewSequentialFile opens a file for sequential reading.
|
2021-03-16 03:48:36 +01:00
|
|
|
IOStatus NewSequentialFile(const std::string& fname,
|
|
|
|
const FileOptions& options,
|
|
|
|
std::unique_ptr<FSSequentialFile>* result,
|
|
|
|
IODebugContext* dbg) override {
|
2017-06-27 01:52:06 +02:00
|
|
|
result->reset();
|
|
|
|
if (options.use_mmap_reads) {
|
2021-03-16 03:48:36 +01:00
|
|
|
return IOStatus::InvalidArgument();
|
2017-06-27 01:52:06 +02:00
|
|
|
}
|
|
|
|
// Open file using underlying Env implementation
|
2021-03-16 03:48:36 +01:00
|
|
|
std::unique_ptr<FSSequentialFile> underlying;
|
|
|
|
auto status =
|
|
|
|
FileSystemWrapper::NewSequentialFile(fname, options, &underlying, dbg);
|
2017-06-27 01:52:06 +02:00
|
|
|
if (!status.ok()) {
|
|
|
|
return status;
|
|
|
|
}
|
2021-02-05 21:39:05 +01:00
|
|
|
uint64_t file_size;
|
2021-03-16 03:48:36 +01:00
|
|
|
status = FileSystemWrapper::GetFileSize(fname, options.io_options,
|
|
|
|
&file_size, dbg);
|
2021-02-05 21:39:05 +01:00
|
|
|
if (!status.ok()) {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
if (!file_size) {
|
|
|
|
*result = std::move(underlying);
|
|
|
|
return status;
|
|
|
|
}
|
2017-06-27 01:52:06 +02:00
|
|
|
// Create cipher stream
|
|
|
|
std::unique_ptr<BlockAccessCipherStream> stream;
|
2020-09-16 00:12:58 +02:00
|
|
|
size_t prefix_length;
|
|
|
|
status = CreateSequentialCipherStream(fname, underlying, options,
|
2021-03-16 03:48:36 +01:00
|
|
|
&prefix_length, &stream, dbg);
|
2020-09-16 00:12:58 +02:00
|
|
|
if (status.ok()) {
|
|
|
|
result->reset(new EncryptedSequentialFile(
|
|
|
|
std::move(underlying), std::move(stream), prefix_length));
|
2017-06-27 01:52:06 +02:00
|
|
|
}
|
2020-09-16 00:12:58 +02:00
|
|
|
return status;
|
2017-06-27 01:52:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewRandomAccessFile opens a file for random read access.
|
2021-03-16 03:48:36 +01:00
|
|
|
IOStatus NewRandomAccessFile(const std::string& fname,
|
|
|
|
const FileOptions& options,
|
|
|
|
std::unique_ptr<FSRandomAccessFile>* result,
|
|
|
|
IODebugContext* dbg) override {
|
2017-06-27 01:52:06 +02:00
|
|
|
result->reset();
|
|
|
|
if (options.use_mmap_reads) {
|
2021-03-16 03:48:36 +01:00
|
|
|
return IOStatus::InvalidArgument();
|
2017-06-27 01:52:06 +02:00
|
|
|
}
|
|
|
|
// Open file using underlying Env implementation
|
2021-03-16 03:48:36 +01:00
|
|
|
std::unique_ptr<FSRandomAccessFile> underlying;
|
|
|
|
auto status = FileSystemWrapper::NewRandomAccessFile(fname, options,
|
|
|
|
&underlying, dbg);
|
2017-06-27 01:52:06 +02:00
|
|
|
if (!status.ok()) {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
std::unique_ptr<BlockAccessCipherStream> stream;
|
2020-09-16 00:12:58 +02:00
|
|
|
size_t prefix_length;
|
|
|
|
status = CreateRandomReadCipherStream(fname, underlying, options,
|
2021-03-16 03:48:36 +01:00
|
|
|
&prefix_length, &stream, dbg);
|
2020-09-16 00:12:58 +02:00
|
|
|
if (status.ok()) {
|
|
|
|
if (stream) {
|
|
|
|
result->reset(new EncryptedRandomAccessFile(
|
|
|
|
std::move(underlying), std::move(stream), prefix_length));
|
|
|
|
} else {
|
|
|
|
result->reset(underlying.release());
|
|
|
|
}
|
2017-06-27 01:52:06 +02:00
|
|
|
}
|
2020-09-16 00:12:58 +02:00
|
|
|
return status;
|
2017-06-27 01:52:06 +02:00
|
|
|
}
|
2018-11-09 20:17:34 +01:00
|
|
|
|
2017-06-27 01:52:06 +02:00
|
|
|
// NewWritableFile opens a file for sequential writing.
|
2021-03-16 03:48:36 +01:00
|
|
|
IOStatus NewWritableFile(const std::string& fname, const FileOptions& options,
|
|
|
|
std::unique_ptr<FSWritableFile>* result,
|
|
|
|
IODebugContext* dbg) override {
|
2017-06-27 01:52:06 +02:00
|
|
|
result->reset();
|
|
|
|
if (options.use_mmap_writes) {
|
2021-03-16 03:48:36 +01:00
|
|
|
return IOStatus::InvalidArgument();
|
2017-06-27 01:52:06 +02:00
|
|
|
}
|
|
|
|
// Open file using underlying Env implementation
|
2021-03-16 03:48:36 +01:00
|
|
|
std::unique_ptr<FSWritableFile> underlying;
|
|
|
|
IOStatus status =
|
|
|
|
FileSystemWrapper::NewWritableFile(fname, options, &underlying, dbg);
|
2017-06-27 01:52:06 +02:00
|
|
|
if (!status.ok()) {
|
|
|
|
return status;
|
|
|
|
}
|
2021-03-16 03:48:36 +01:00
|
|
|
return CreateWritableEncryptedFile(fname, underlying, options, result, dbg);
|
2017-06-27 01:52:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create an object that writes to a new file with the specified
|
|
|
|
// name. Deletes any existing file with the same name and creates a
|
|
|
|
// new file. On success, stores a pointer to the new file in
|
|
|
|
// *result and returns OK. On failure stores nullptr in *result and
|
|
|
|
// returns non-OK.
|
|
|
|
//
|
|
|
|
// The returned file will only be accessed by one thread at a time.
|
2021-03-16 03:48:36 +01:00
|
|
|
IOStatus ReopenWritableFile(const std::string& fname,
|
|
|
|
const FileOptions& options,
|
|
|
|
std::unique_ptr<FSWritableFile>* result,
|
|
|
|
IODebugContext* dbg) override {
|
2017-06-27 01:52:06 +02:00
|
|
|
result->reset();
|
|
|
|
if (options.use_mmap_writes) {
|
2021-03-16 03:48:36 +01:00
|
|
|
return IOStatus::InvalidArgument();
|
2017-06-27 01:52:06 +02:00
|
|
|
}
|
|
|
|
// Open file using underlying Env implementation
|
2021-03-16 03:48:36 +01:00
|
|
|
std::unique_ptr<FSWritableFile> underlying;
|
|
|
|
IOStatus status =
|
|
|
|
FileSystemWrapper::ReopenWritableFile(fname, options, &underlying, dbg);
|
2017-06-27 01:52:06 +02:00
|
|
|
if (!status.ok()) {
|
|
|
|
return status;
|
|
|
|
}
|
2021-03-16 03:48:36 +01:00
|
|
|
return CreateWritableEncryptedFile(fname, underlying, options, result, dbg);
|
2017-06-27 01:52:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Reuse an existing file by renaming it and opening it as writable.
|
2021-03-16 03:48:36 +01:00
|
|
|
IOStatus ReuseWritableFile(const std::string& fname,
|
|
|
|
const std::string& old_fname,
|
|
|
|
const FileOptions& options,
|
|
|
|
std::unique_ptr<FSWritableFile>* result,
|
|
|
|
IODebugContext* dbg) override {
|
2017-06-27 01:52:06 +02:00
|
|
|
result->reset();
|
|
|
|
if (options.use_mmap_writes) {
|
2021-03-16 03:48:36 +01:00
|
|
|
return IOStatus::InvalidArgument();
|
2017-06-27 01:52:06 +02:00
|
|
|
}
|
|
|
|
// Open file using underlying Env implementation
|
2021-03-16 03:48:36 +01:00
|
|
|
std::unique_ptr<FSWritableFile> underlying;
|
|
|
|
auto status = FileSystemWrapper::ReuseWritableFile(
|
|
|
|
fname, old_fname, options, &underlying, dbg);
|
2017-06-27 01:52:06 +02:00
|
|
|
if (!status.ok()) {
|
|
|
|
return status;
|
|
|
|
}
|
2021-03-16 03:48:36 +01:00
|
|
|
return CreateWritableEncryptedFile(fname, underlying, options, result, dbg);
|
2017-06-27 01:52:06 +02:00
|
|
|
}
|
|
|
|
|
2018-03-08 19:18:34 +01:00
|
|
|
// Open `fname` for random read and write, if file doesn't exist the file
|
2017-06-27 01:52:06 +02:00
|
|
|
// will be created. On success, stores a pointer to the new file in
|
|
|
|
// *result and returns OK. On failure returns non-OK.
|
|
|
|
//
|
|
|
|
// The returned file will only be accessed by one thread at a time.
|
2021-03-16 03:48:36 +01:00
|
|
|
IOStatus NewRandomRWFile(const std::string& fname, const FileOptions& options,
|
|
|
|
std::unique_ptr<FSRandomRWFile>* result,
|
|
|
|
IODebugContext* dbg) override {
|
2017-06-27 01:52:06 +02:00
|
|
|
result->reset();
|
|
|
|
if (options.use_mmap_reads || options.use_mmap_writes) {
|
2021-03-16 03:48:36 +01:00
|
|
|
return IOStatus::InvalidArgument();
|
2017-06-27 01:52:06 +02:00
|
|
|
}
|
|
|
|
// Check file exists
|
2021-03-16 03:48:36 +01:00
|
|
|
bool isNewFile = !FileExists(fname, options.io_options, dbg).ok();
|
2017-06-27 01:52:06 +02:00
|
|
|
|
|
|
|
// Open file using underlying Env implementation
|
2021-03-16 03:48:36 +01:00
|
|
|
std::unique_ptr<FSRandomRWFile> underlying;
|
|
|
|
auto status =
|
|
|
|
FileSystemWrapper::NewRandomRWFile(fname, options, &underlying, dbg);
|
2017-06-27 01:52:06 +02:00
|
|
|
if (!status.ok()) {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
// Create cipher stream
|
|
|
|
std::unique_ptr<BlockAccessCipherStream> stream;
|
2020-09-16 00:12:58 +02:00
|
|
|
size_t prefix_length = 0;
|
|
|
|
if (!isNewFile) {
|
|
|
|
// File already exists, read prefix
|
|
|
|
status = CreateRandomReadCipherStream(fname, underlying, options,
|
2021-03-16 03:48:36 +01:00
|
|
|
&prefix_length, &stream, dbg);
|
2020-09-16 00:12:58 +02:00
|
|
|
} else {
|
|
|
|
status = CreateRandomWriteCipherStream(fname, underlying, options,
|
2021-03-16 03:48:36 +01:00
|
|
|
&prefix_length, &stream, dbg);
|
2017-06-27 01:52:06 +02:00
|
|
|
}
|
2020-09-16 00:12:58 +02:00
|
|
|
if (status.ok()) {
|
|
|
|
if (stream) {
|
|
|
|
result->reset(new EncryptedRandomRWFile(
|
|
|
|
std::move(underlying), std::move(stream), prefix_length));
|
|
|
|
} else {
|
|
|
|
result->reset(underlying.release());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return status;
|
2017-06-27 01:52:06 +02:00
|
|
|
}
|
|
|
|
|
2020-06-22 22:25:41 +02:00
|
|
|
// Store in *result the attributes of the children of the specified
|
|
|
|
// directory.
|
|
|
|
// In case the implementation lists the directory prior to iterating the
|
|
|
|
// files
|
|
|
|
// and files are concurrently deleted, the deleted files will be omitted
|
|
|
|
// from
|
2017-06-27 01:52:06 +02:00
|
|
|
// result.
|
|
|
|
// The name attributes are relative to "dir".
|
|
|
|
// Original contents of *results are dropped.
|
|
|
|
// Returns OK if "dir" exists and "*result" contains its children.
|
2020-06-22 22:25:41 +02:00
|
|
|
// NotFound if "dir" does not exist, the calling process does not
|
|
|
|
// have
|
2017-06-27 01:52:06 +02:00
|
|
|
// permission to access "dir", or if "dir" is invalid.
|
|
|
|
// IOError if an IO Error was encountered
|
2021-03-16 03:48:36 +01:00
|
|
|
IOStatus GetChildrenFileAttributes(const std::string& dir,
|
|
|
|
const IOOptions& options,
|
|
|
|
std::vector<FileAttributes>* result,
|
|
|
|
IODebugContext* dbg) override {
|
|
|
|
auto status =
|
|
|
|
FileSystemWrapper::GetChildrenFileAttributes(dir, options, result, dbg);
|
2017-06-27 01:52:06 +02:00
|
|
|
if (!status.ok()) {
|
|
|
|
return status;
|
|
|
|
}
|
2020-06-22 22:25:41 +02:00
|
|
|
for (auto it = std::begin(*result); it != std::end(*result); ++it) {
|
|
|
|
// assert(it->size_bytes >= prefixLength);
|
|
|
|
// breaks env_basic_test when called on directory containing
|
|
|
|
// directories
|
|
|
|
// which makes subtraction of prefixLength worrisome since
|
|
|
|
// FileAttributes does not identify directories
|
2020-09-16 00:12:58 +02:00
|
|
|
EncryptionProvider* provider;
|
|
|
|
status = GetReadableProvider(it->name, &provider);
|
|
|
|
if (!status.ok()) {
|
|
|
|
return status;
|
|
|
|
} else if (provider != nullptr) {
|
|
|
|
it->size_bytes -= provider->GetPrefixLength();
|
|
|
|
}
|
2017-06-27 01:52:06 +02:00
|
|
|
}
|
2021-03-16 03:48:36 +01:00
|
|
|
return IOStatus::OK();
|
2019-02-19 22:36:04 +01:00
|
|
|
}
|
2017-06-27 01:52:06 +02:00
|
|
|
|
|
|
|
// Store the size of fname in *file_size.
|
2021-03-16 03:48:36 +01:00
|
|
|
IOStatus GetFileSize(const std::string& fname, const IOOptions& options,
|
|
|
|
uint64_t* file_size, IODebugContext* dbg) override {
|
|
|
|
auto status =
|
|
|
|
FileSystemWrapper::GetFileSize(fname, options, file_size, dbg);
|
2021-02-05 21:39:05 +01:00
|
|
|
if (!status.ok() || !(*file_size)) {
|
2017-06-27 01:52:06 +02:00
|
|
|
return status;
|
|
|
|
}
|
2020-09-16 00:12:58 +02:00
|
|
|
EncryptionProvider* provider;
|
|
|
|
status = GetReadableProvider(fname, &provider);
|
|
|
|
if (provider != nullptr && status.ok()) {
|
|
|
|
size_t prefixLength = provider->GetPrefixLength();
|
|
|
|
assert(*file_size >= prefixLength);
|
|
|
|
*file_size -= prefixLength;
|
|
|
|
}
|
|
|
|
return status;
|
2017-06-27 01:52:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-09-16 00:12:58 +02:00
|
|
|
std::shared_ptr<EncryptionProvider> provider_;
|
2017-06-27 01:52:06 +02:00
|
|
|
};
|
2021-03-16 03:48:36 +01:00
|
|
|
} // namespace
|
2017-06-27 01:52:06 +02:00
|
|
|
|
2021-03-16 03:48:36 +01:00
|
|
|
std::shared_ptr<FileSystem> NewEncryptedFS(
|
|
|
|
const std::shared_ptr<FileSystem>& base,
|
|
|
|
const std::shared_ptr<EncryptionProvider>& provider) {
|
|
|
|
return std::make_shared<EncryptedFileSystemImpl>(base, provider);
|
|
|
|
}
|
2019-06-20 01:42:59 +02:00
|
|
|
// Returns an Env that encrypts data when stored on disk and decrypts data when
|
2017-06-27 01:52:06 +02:00
|
|
|
// read from disk.
|
2020-09-16 00:12:58 +02:00
|
|
|
Env* NewEncryptedEnv(Env* base_env,
|
|
|
|
const std::shared_ptr<EncryptionProvider>& provider) {
|
2021-03-16 03:48:36 +01:00
|
|
|
return new CompositeEnvWrapper(
|
|
|
|
base_env, NewEncryptedFS(base_env->GetFileSystem(), provider));
|
2017-06-27 01:52:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Encrypt one or more (partial) blocks of data at the file offset.
|
|
|
|
// Length of data is given in dataSize.
|
|
|
|
Status BlockAccessCipherStream::Encrypt(uint64_t fileOffset, char *data, size_t dataSize) {
|
|
|
|
// Calculate block index
|
|
|
|
auto blockSize = BlockSize();
|
|
|
|
uint64_t blockIndex = fileOffset / blockSize;
|
|
|
|
size_t blockOffset = fileOffset % blockSize;
|
2018-11-09 20:17:34 +01:00
|
|
|
std::unique_ptr<char[]> blockBuffer;
|
2017-06-27 01:52:06 +02:00
|
|
|
|
|
|
|
std::string scratch;
|
|
|
|
AllocateScratch(scratch);
|
|
|
|
|
|
|
|
// Encrypt individual blocks.
|
|
|
|
while (1) {
|
|
|
|
char *block = data;
|
|
|
|
size_t n = std::min(dataSize, blockSize - blockOffset);
|
|
|
|
if (n != blockSize) {
|
2019-06-20 01:42:59 +02:00
|
|
|
// We're not encrypting a full block.
|
2017-06-27 01:52:06 +02:00
|
|
|
// Copy data to blockBuffer
|
|
|
|
if (!blockBuffer.get()) {
|
2018-11-09 20:17:34 +01:00
|
|
|
// Allocate buffer
|
|
|
|
blockBuffer = std::unique_ptr<char[]>(new char[blockSize]);
|
2017-06-27 01:52:06 +02:00
|
|
|
}
|
|
|
|
block = blockBuffer.get();
|
2019-06-20 01:42:59 +02:00
|
|
|
// Copy plain data to block buffer
|
2017-06-27 01:52:06 +02:00
|
|
|
memmove(block + blockOffset, data, n);
|
|
|
|
}
|
|
|
|
auto status = EncryptBlock(blockIndex, block, (char*)scratch.data());
|
|
|
|
if (!status.ok()) {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
if (block != data) {
|
|
|
|
// Copy encrypted data back to `data`.
|
|
|
|
memmove(data, block + blockOffset, n);
|
|
|
|
}
|
|
|
|
dataSize -= n;
|
|
|
|
if (dataSize == 0) {
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
data += n;
|
|
|
|
blockOffset = 0;
|
|
|
|
blockIndex++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decrypt one or more (partial) blocks of data at the file offset.
|
|
|
|
// Length of data is given in dataSize.
|
|
|
|
Status BlockAccessCipherStream::Decrypt(uint64_t fileOffset, char *data, size_t dataSize) {
|
|
|
|
// Calculate block index
|
|
|
|
auto blockSize = BlockSize();
|
|
|
|
uint64_t blockIndex = fileOffset / blockSize;
|
|
|
|
size_t blockOffset = fileOffset % blockSize;
|
2018-11-09 20:17:34 +01:00
|
|
|
std::unique_ptr<char[]> blockBuffer;
|
2017-06-27 01:52:06 +02:00
|
|
|
|
|
|
|
std::string scratch;
|
|
|
|
AllocateScratch(scratch);
|
|
|
|
|
|
|
|
// Decrypt individual blocks.
|
|
|
|
while (1) {
|
|
|
|
char *block = data;
|
|
|
|
size_t n = std::min(dataSize, blockSize - blockOffset);
|
|
|
|
if (n != blockSize) {
|
2019-06-20 01:42:59 +02:00
|
|
|
// We're not decrypting a full block.
|
2017-06-27 01:52:06 +02:00
|
|
|
// Copy data to blockBuffer
|
|
|
|
if (!blockBuffer.get()) {
|
2018-11-09 20:17:34 +01:00
|
|
|
// Allocate buffer
|
|
|
|
blockBuffer = std::unique_ptr<char[]>(new char[blockSize]);
|
2017-06-27 01:52:06 +02:00
|
|
|
}
|
|
|
|
block = blockBuffer.get();
|
2019-06-20 01:42:59 +02:00
|
|
|
// Copy encrypted data to block buffer
|
2017-06-27 01:52:06 +02:00
|
|
|
memmove(block + blockOffset, data, n);
|
|
|
|
}
|
|
|
|
auto status = DecryptBlock(blockIndex, block, (char*)scratch.data());
|
|
|
|
if (!status.ok()) {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
if (block != data) {
|
|
|
|
// Copy decrypted data back to `data`.
|
|
|
|
memmove(data, block + blockOffset, n);
|
|
|
|
}
|
2019-04-08 23:54:36 +02:00
|
|
|
|
|
|
|
// Simply decrementing dataSize by n could cause it to underflow,
|
|
|
|
// which will very likely make it read over the original bounds later
|
|
|
|
assert(dataSize >= n);
|
|
|
|
if (dataSize < n) {
|
|
|
|
return Status::Corruption("Cannot decrypt data at given offset");
|
|
|
|
}
|
|
|
|
|
2017-06-27 01:52:06 +02:00
|
|
|
dataSize -= n;
|
|
|
|
if (dataSize == 0) {
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
data += n;
|
|
|
|
blockOffset = 0;
|
|
|
|
blockIndex++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-16 00:12:58 +02:00
|
|
|
const char* ROT13BlockCipher::Name() const { return kROT13CipherName; }
|
|
|
|
|
2017-06-27 01:52:06 +02:00
|
|
|
// Encrypt a block of data.
|
|
|
|
// Length of data is equal to BlockSize().
|
2020-09-16 00:12:58 +02:00
|
|
|
Status ROT13BlockCipher::Encrypt(char* data) {
|
2017-06-27 01:52:06 +02:00
|
|
|
for (size_t i = 0; i < blockSize_; ++i) {
|
2020-09-16 00:12:58 +02:00
|
|
|
data[i] += 13;
|
2017-06-27 01:52:06 +02:00
|
|
|
}
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decrypt a block of data.
|
|
|
|
// Length of data is equal to BlockSize().
|
2020-09-16 00:12:58 +02:00
|
|
|
Status ROT13BlockCipher::Decrypt(char* data) { return Encrypt(data); }
|
2017-06-27 01:52:06 +02:00
|
|
|
|
|
|
|
// Allocate scratch space which is passed to EncryptBlock/DecryptBlock.
|
|
|
|
void CTRCipherStream::AllocateScratch(std::string& scratch) {
|
2020-09-16 00:12:58 +02:00
|
|
|
auto blockSize = cipher_->BlockSize();
|
2017-06-27 01:52:06 +02:00
|
|
|
scratch.reserve(blockSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Encrypt a block of data at the given block index.
|
|
|
|
// Length of data is equal to BlockSize();
|
2020-09-16 00:12:58 +02:00
|
|
|
Status CTRCipherStream::EncryptBlock(uint64_t blockIndex, char* data,
|
|
|
|
char* scratch) {
|
2017-06-27 01:52:06 +02:00
|
|
|
// Create nonce + counter
|
2020-09-16 00:12:58 +02:00
|
|
|
auto blockSize = cipher_->BlockSize();
|
2017-06-27 01:52:06 +02:00
|
|
|
memmove(scratch, iv_.data(), blockSize);
|
|
|
|
EncodeFixed64(scratch, blockIndex + initialCounter_);
|
|
|
|
|
2019-06-20 01:42:59 +02:00
|
|
|
// Encrypt nonce+counter
|
2020-09-16 00:12:58 +02:00
|
|
|
auto status = cipher_->Encrypt(scratch);
|
2017-06-27 01:52:06 +02:00
|
|
|
if (!status.ok()) {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
// XOR data with ciphertext.
|
|
|
|
for (size_t i = 0; i < blockSize; i++) {
|
|
|
|
data[i] = data[i] ^ scratch[i];
|
|
|
|
}
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decrypt a block of data at the given block index.
|
|
|
|
// Length of data is equal to BlockSize();
|
2020-09-16 00:12:58 +02:00
|
|
|
Status CTRCipherStream::DecryptBlock(uint64_t blockIndex, char* data,
|
|
|
|
char* scratch) {
|
2019-06-20 01:42:59 +02:00
|
|
|
// For CTR decryption & encryption are the same
|
2017-06-27 01:52:06 +02:00
|
|
|
return EncryptBlock(blockIndex, data, scratch);
|
|
|
|
}
|
|
|
|
|
2020-09-16 00:12:58 +02:00
|
|
|
const char* CTREncryptionProvider::Name() const { return kCTRProviderName; }
|
|
|
|
|
2017-06-27 01:52:06 +02:00
|
|
|
// GetPrefixLength returns the length of the prefix that is added to every file
|
|
|
|
// and used for storing encryption options.
|
2019-06-20 01:42:59 +02:00
|
|
|
// For optimal performance, the prefix length should be a multiple of
|
2018-03-08 19:18:34 +01:00
|
|
|
// the page size.
|
2020-09-16 00:12:58 +02:00
|
|
|
size_t CTREncryptionProvider::GetPrefixLength() const {
|
2017-06-27 01:52:06 +02:00
|
|
|
return defaultPrefixLength;
|
|
|
|
}
|
|
|
|
|
2020-09-16 00:12:58 +02:00
|
|
|
Status CTREncryptionProvider::TEST_Initialize() {
|
|
|
|
if (!cipher_) {
|
|
|
|
return BlockCipher::CreateFromString(
|
|
|
|
ConfigOptions(), std::string(kROT13CipherName) + ":32", &cipher_);
|
|
|
|
}
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
Status CTREncryptionProvider::AddCipher(const std::string& /*descriptor*/,
|
|
|
|
const char* cipher, size_t len,
|
|
|
|
bool /*for_write*/) {
|
|
|
|
if (cipher_) {
|
|
|
|
return Status::NotSupported("Cannot add keys to CTREncryptionProvider");
|
|
|
|
} else if (strcmp(kROT13CipherName, cipher) == 0) {
|
|
|
|
cipher_.reset(new ROT13BlockCipher(len));
|
|
|
|
return Status::OK();
|
|
|
|
} else {
|
|
|
|
return BlockCipher::CreateFromString(ConfigOptions(), std::string(cipher),
|
|
|
|
&cipher_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-27 01:52:06 +02:00
|
|
|
// decodeCTRParameters decodes the initial counter & IV from the given
|
|
|
|
// (plain text) prefix.
|
2020-09-16 00:12:58 +02:00
|
|
|
static void decodeCTRParameters(const char* prefix, size_t blockSize,
|
|
|
|
uint64_t& initialCounter, Slice& iv) {
|
2017-06-27 01:52:06 +02:00
|
|
|
// First block contains 64-bit initial counter
|
|
|
|
initialCounter = DecodeFixed64(prefix);
|
|
|
|
// Second block contains IV
|
|
|
|
iv = Slice(prefix + blockSize, blockSize);
|
|
|
|
}
|
|
|
|
|
2019-06-20 01:42:59 +02:00
|
|
|
// CreateNewPrefix initialized an allocated block of prefix memory
|
2017-06-27 01:52:06 +02:00
|
|
|
// for a new file.
|
2018-03-05 22:08:17 +01:00
|
|
|
Status CTREncryptionProvider::CreateNewPrefix(const std::string& /*fname*/,
|
|
|
|
char* prefix,
|
2020-09-16 00:12:58 +02:00
|
|
|
size_t prefixLength) const {
|
|
|
|
if (!cipher_) {
|
|
|
|
return Status::InvalidArgument("Encryption Cipher is missing");
|
|
|
|
}
|
2017-06-27 01:52:06 +02:00
|
|
|
// Create & seed rnd.
|
2021-03-15 12:32:24 +01:00
|
|
|
Random rnd((uint32_t)SystemClock::Default()->NowMicros());
|
2017-06-27 01:52:06 +02:00
|
|
|
// Fill entire prefix block with random values.
|
|
|
|
for (size_t i = 0; i < prefixLength; i++) {
|
|
|
|
prefix[i] = rnd.Uniform(256) & 0xFF;
|
|
|
|
}
|
|
|
|
// Take random data to extract initial counter & IV
|
2020-09-16 00:12:58 +02:00
|
|
|
auto blockSize = cipher_->BlockSize();
|
2017-06-27 01:52:06 +02:00
|
|
|
uint64_t initialCounter;
|
|
|
|
Slice prefixIV;
|
|
|
|
decodeCTRParameters(prefix, blockSize, initialCounter, prefixIV);
|
|
|
|
|
|
|
|
// Now populate the rest of the prefix, starting from the third block.
|
2020-09-16 00:12:58 +02:00
|
|
|
PopulateSecretPrefixPart(prefix + (2 * blockSize),
|
|
|
|
prefixLength - (2 * blockSize), blockSize);
|
2017-06-27 01:52:06 +02:00
|
|
|
|
2020-04-02 01:57:03 +02:00
|
|
|
// Encrypt the prefix, starting from block 2 (leave block 0, 1 with initial
|
|
|
|
// counter & IV unencrypted)
|
2017-06-27 01:52:06 +02:00
|
|
|
CTRCipherStream cipherStream(cipher_, prefixIV.data(), initialCounter);
|
2020-04-02 01:57:03 +02:00
|
|
|
Status status;
|
|
|
|
{
|
|
|
|
PERF_TIMER_GUARD(encrypt_data_nanos);
|
|
|
|
status = cipherStream.Encrypt(0, prefix + (2 * blockSize),
|
|
|
|
prefixLength - (2 * blockSize));
|
|
|
|
}
|
2017-06-27 01:52:06 +02:00
|
|
|
if (!status.ok()) {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2019-06-20 01:42:59 +02:00
|
|
|
// PopulateSecretPrefixPart initializes the data into a new prefix block
|
2017-06-27 01:52:06 +02:00
|
|
|
// in plain text.
|
|
|
|
// Returns the amount of space (starting from the start of the prefix)
|
|
|
|
// that has been initialized.
|
2020-09-16 00:12:58 +02:00
|
|
|
size_t CTREncryptionProvider::PopulateSecretPrefixPart(
|
|
|
|
char* /*prefix*/, size_t /*prefixLength*/, size_t /*blockSize*/) const {
|
2017-06-27 01:52:06 +02:00
|
|
|
// Nothing to do here, put in custom data in override when needed.
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-11-09 20:17:34 +01:00
|
|
|
Status CTREncryptionProvider::CreateCipherStream(
|
|
|
|
const std::string& fname, const EnvOptions& options, Slice& prefix,
|
|
|
|
std::unique_ptr<BlockAccessCipherStream>* result) {
|
2020-09-16 00:12:58 +02:00
|
|
|
if (!cipher_) {
|
|
|
|
return Status::InvalidArgument("Encryption Cipher is missing");
|
|
|
|
}
|
2017-06-27 01:52:06 +02:00
|
|
|
// Read plain text part of prefix.
|
2020-09-16 00:12:58 +02:00
|
|
|
auto blockSize = cipher_->BlockSize();
|
2017-06-27 01:52:06 +02:00
|
|
|
uint64_t initialCounter;
|
|
|
|
Slice iv;
|
|
|
|
decodeCTRParameters(prefix.data(), blockSize, initialCounter, iv);
|
|
|
|
|
2019-04-08 23:54:36 +02:00
|
|
|
// If the prefix is smaller than twice the block size, we would below read a
|
|
|
|
// very large chunk of the file (and very likely read over the bounds)
|
|
|
|
assert(prefix.size() >= 2 * blockSize);
|
|
|
|
if (prefix.size() < 2 * blockSize) {
|
2019-04-11 19:22:07 +02:00
|
|
|
return Status::Corruption("Unable to read from file " + fname +
|
|
|
|
": read attempt would read beyond file bounds");
|
2019-04-08 23:54:36 +02:00
|
|
|
}
|
|
|
|
|
2020-04-02 01:57:03 +02:00
|
|
|
// Decrypt the encrypted part of the prefix, starting from block 2 (block 0, 1
|
|
|
|
// with initial counter & IV are unencrypted)
|
2017-06-27 01:52:06 +02:00
|
|
|
CTRCipherStream cipherStream(cipher_, iv.data(), initialCounter);
|
2020-04-02 01:57:03 +02:00
|
|
|
Status status;
|
|
|
|
{
|
|
|
|
PERF_TIMER_GUARD(decrypt_data_nanos);
|
|
|
|
status = cipherStream.Decrypt(0, (char*)prefix.data() + (2 * blockSize),
|
|
|
|
prefix.size() - (2 * blockSize));
|
|
|
|
}
|
2017-06-27 01:52:06 +02:00
|
|
|
if (!status.ok()) {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2019-06-20 01:42:59 +02:00
|
|
|
// Create cipher stream
|
2020-09-16 00:12:58 +02:00
|
|
|
return CreateCipherStreamFromPrefix(fname, options, initialCounter, iv,
|
|
|
|
prefix, result);
|
2017-06-27 01:52:06 +02:00
|
|
|
}
|
|
|
|
|
2020-09-16 00:12:58 +02:00
|
|
|
// CreateCipherStreamFromPrefix creates a block access cipher stream for a file
|
|
|
|
// given given name and options. The given prefix is already decrypted.
|
2018-03-05 22:08:17 +01:00
|
|
|
Status CTREncryptionProvider::CreateCipherStreamFromPrefix(
|
|
|
|
const std::string& /*fname*/, const EnvOptions& /*options*/,
|
|
|
|
uint64_t initialCounter, const Slice& iv, const Slice& /*prefix*/,
|
2018-11-09 20:17:34 +01:00
|
|
|
std::unique_ptr<BlockAccessCipherStream>* result) {
|
|
|
|
(*result) = std::unique_ptr<BlockAccessCipherStream>(
|
|
|
|
new CTRCipherStream(cipher_, iv.data(), initialCounter));
|
2017-06-27 01:52:06 +02:00
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // ROCKSDB_LITE
|
|
|
|
|
2020-02-20 21:07:53 +01:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|