|
| 1 | +// Copyright 2017 The Cockroach Authors. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 12 | +// implied. See the License for the specific language governing |
| 13 | +// permissions and limitations under the License. |
| 14 | + |
| 15 | +#ifndef ROACHLIB_PREAMBLE_H |
| 16 | +#define ROACHLIB_PREAMBLE_H |
| 17 | + |
| 18 | +#include <rocksdb/env.h> |
| 19 | +#include <rocksdb/env_encryption.h> |
| 20 | + |
| 21 | +// Preamble length. |
| 22 | +// WARNING: changing this will result in incompatible on-disk format. |
| 23 | +// The preamble length must fit in a uint16_t. |
| 24 | +const static size_t defaultPreambleLength = 4096; |
| 25 | + |
| 26 | +class PreambleHandler : rocksdb::EncryptionProvider { |
| 27 | + public: |
| 28 | + |
| 29 | + PreambleHandler() {} |
| 30 | + virtual ~PreambleHandler() {} |
| 31 | + |
| 32 | + // GetEnv returns an EncryptionEnv wrapped around base_env. |
| 33 | + rocksdb::Env* GetEnv(rocksdb::Env* base_env); |
| 34 | + |
| 35 | + // GetPrefixLength returns the preamble length. |
| 36 | + virtual size_t GetPrefixLength() override; |
| 37 | + |
| 38 | + // CreateNewPrefix initializes an allocated block of prefix memory for a new file. |
| 39 | + virtual rocksdb::Status CreateNewPrefix(const std::string& fname, char *prefix, size_t prefixLength) override; |
| 40 | + |
| 41 | + // CreateCipherStream creates a block access cipher stream for a file given name and options. |
| 42 | + virtual rocksdb::Status CreateCipherStream(const std::string& fname, const rocksdb::EnvOptions& options, |
| 43 | + rocksdb::Slice& prefix, std::unique_ptr<rocksdb::BlockAccessCipherStream>* result) override; |
| 44 | +}; |
| 45 | + |
| 46 | +// Blocksize for the plaintext cipher stream. |
| 47 | +// TODO(mberhault): we can pick anything we like. What's good? |
| 48 | +const static size_t plaintextBlockSize = 4096; |
| 49 | + |
| 50 | +// PlaintextCipherStream implements BlockAccessCipherStream with |
| 51 | +// no-op encrypt/decrypt operations. |
| 52 | +class PlaintextCipherStream final : public rocksdb::BlockAccessCipherStream { |
| 53 | + public: |
| 54 | + PlaintextCipherStream() {} |
| 55 | + virtual ~PlaintextCipherStream() {} |
| 56 | + |
| 57 | + // BlockSize returns the size of each block supported by this cipher stream. |
| 58 | + virtual size_t BlockSize() override { return plaintextBlockSize; } |
| 59 | + |
| 60 | + // Encrypt blocks of data. This is a noop. |
| 61 | + virtual rocksdb::Status Encrypt(uint64_t fileOffset, char *data, size_t dataSize) override { |
| 62 | + return rocksdb::Status::OK(); |
| 63 | + } |
| 64 | + |
| 65 | + // Decrypt blocks of data. This is a noop. |
| 66 | + virtual rocksdb::Status Decrypt(uint64_t fileOffset, char *data, size_t dataSize) override { |
| 67 | + return rocksdb::Status::OK(); |
| 68 | + } |
| 69 | + protected: |
| 70 | + // Allocate scratch space which is passed to EncryptBlock/DecryptBlock. |
| 71 | + virtual void AllocateScratch(std::string&) override {} |
| 72 | + |
| 73 | + // Encrypt a block of data at the given block index. |
| 74 | + // Length of data is equal to BlockSize(); |
| 75 | + virtual rocksdb::Status EncryptBlock(uint64_t blockIndex, char *data, char* scratch) override { |
| 76 | + return rocksdb::Status::OK(); |
| 77 | + } |
| 78 | + |
| 79 | + // Decrypt a block of data at the given block index. |
| 80 | + // Length of data is equal to BlockSize(); |
| 81 | + virtual rocksdb::Status DecryptBlock(uint64_t blockIndex, char *data, char* scratch) override { |
| 82 | + return rocksdb::Status::OK(); |
| 83 | + } |
| 84 | +}; |
| 85 | + |
| 86 | +#endif // ROACHLIB_PREAMBLE_H |
0 commit comments