Coverage Report

Created: 2026-11-12 08:06

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/rocksdb/env/env_encryption_ctr.h
Line
Count
Source
1
//  Copyright (c) 2016-present, Facebook, Inc.  All rights reserved.
2
//  This source code is licensed under both the GPLv2 (found in the
3
//  COPYING file in the root directory) and Apache 2.0 License
4
//  (found in the LICENSE.Apache file in the root directory).
5
6
#pragma once
7
8
#include "rocksdb/env_encryption.h"
9
10
namespace ROCKSDB_NAMESPACE {
11
// CTRCipherStream implements BlockAccessCipherStream using an
12
// Counter operations mode.
13
// See https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation
14
//
15
// Note: This is a possible implementation of BlockAccessCipherStream,
16
// it is considered suitable for use.
17
class CTRCipherStream final : public BlockAccessCipherStream {
18
 private:
19
  std::shared_ptr<BlockCipher> cipher_;
20
  std::string iv_;
21
  uint64_t initialCounter_;
22
23
 public:
24
  CTRCipherStream(const std::shared_ptr<BlockCipher>& c, const char* iv,
25
                  uint64_t initialCounter)
26
0
      : cipher_(c), iv_(iv, c->BlockSize()), initialCounter_(initialCounter) {}
27
0
  ~CTRCipherStream() override = default;
28
29
0
  size_t BlockSize() override { return cipher_->BlockSize(); }
30
31
 protected:
32
  void AllocateScratch(std::string&) override;
33
34
  Status EncryptBlock(uint64_t blockIndex, char* data, char* scratch) override;
35
36
  Status DecryptBlock(uint64_t blockIndex, char* data, char* scratch) override;
37
};
38
39
// This encryption provider uses a CTR cipher stream, with a given block cipher
40
// and IV.
41
//
42
// Note: This is a possible implementation of EncryptionProvider,
43
// it is considered suitable for use, provided a safe BlockCipher is used.
44
class CTREncryptionProvider : public EncryptionProvider {
45
 private:
46
  std::shared_ptr<BlockCipher> cipher_;
47
48
 protected:
49
  // For optimal performance when using direct IO, the prefix length should be a
50
  // multiple of the page size. This size is to ensure the first real data byte
51
  // is placed at largest known alignment point for direct io.
52
  const static size_t defaultPrefixLength = 4096;
53
54
 public:
55
  explicit CTREncryptionProvider(
56
      const std::shared_ptr<BlockCipher>& c = nullptr);
57
0
  ~CTREncryptionProvider() override = default;
58
59
0
  static const char* kClassName() { return "CTR"; }
60
0
  const char* Name() const override { return kClassName(); }
61
  bool IsInstanceOf(const std::string& name) const override;
62
  size_t GetPrefixLength() const override;
63
  Status CreateNewPrefix(const std::string& fname, char* prefix,
64
                         size_t prefixLength) const override;
65
  Status CreateCipherStream(
66
      const std::string& fname, const EnvOptions& options, Slice& prefix,
67
      std::unique_ptr<BlockAccessCipherStream>* result) override;
68
69
  Status AddCipher(const std::string& descriptor, const char* /*cipher*/,
70
                   size_t /*len*/, bool /*for_write*/) override;
71
72
 protected:
73
  // PopulateSecretPrefixPart initializes the data into a new prefix block
74
  // that will be encrypted. This function will store the data in plain text.
75
  // It will be encrypted later (before written to disk).
76
  // Returns the amount of space (starting from the start of the prefix)
77
  // that has been initialized.
78
  virtual size_t PopulateSecretPrefixPart(char* prefix, size_t prefixLength,
79
                                          size_t blockSize) const;
80
81
  // CreateCipherStreamFromPrefix creates a block access cipher stream for a
82
  // file given
83
  // given name and options. The given prefix is already decrypted.
84
  virtual Status CreateCipherStreamFromPrefix(
85
      const std::string& fname, const EnvOptions& options,
86
      uint64_t initialCounter, const Slice& iv, const Slice& prefix,
87
      std::unique_ptr<BlockAccessCipherStream>* result);
88
};
89
90
Status NewEncryptedFileSystemImpl(
91
    const std::shared_ptr<FileSystem>& base_fs,
92
    const std::shared_ptr<EncryptionProvider>& provider,
93
    std::unique_ptr<FileSystem>* fs);
94
95
}  // namespace ROCKSDB_NAMESPACE