summaryrefslogtreecommitdiffstats
path: root/src/librbd/crypto/luks/LUKSEncryptionFormat.h
blob: 353bd8933c24c3f0bffe385be5ece77e61fba869 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab

#ifndef CEPH_LIBRBD_CRYPTO_LUKS_ENCRYPTION_FORMAT_H
#define CEPH_LIBRBD_CRYPTO_LUKS_ENCRYPTION_FORMAT_H

#include <string_view>
#include "include/rbd/librbd.hpp"
#include "librbd/crypto/CryptoInterface.h"
#include "librbd/crypto/EncryptionFormat.h"

namespace librbd {

struct ImageCtx;

namespace crypto {
namespace luks {

template <typename ImageCtxT>
class EncryptionFormat : public crypto::EncryptionFormat<ImageCtxT> {
public:
  void flatten(ImageCtxT* ictx, Context* on_finish) override;

  CryptoInterface* get_crypto() override {
    ceph_assert(m_crypto);
    return m_crypto.get();
  }

protected:
  std::unique_ptr<CryptoInterface> m_crypto;
};

template <typename ImageCtxT>
class LUKSEncryptionFormat : public EncryptionFormat<ImageCtxT> {
public:
  LUKSEncryptionFormat(std::string_view passphrase)
      : m_passphrase(passphrase) {}

  std::unique_ptr<crypto::EncryptionFormat<ImageCtxT>> clone() const override {
    return std::make_unique<LUKSEncryptionFormat>(m_passphrase);
  }

  void format(ImageCtxT* ictx, Context* on_finish) override;
  void load(ImageCtxT* ictx, std::string* detected_format_name,
            Context* on_finish) override;

private:
  std::string_view m_passphrase;
};

template <typename ImageCtxT>
class LUKS1EncryptionFormat : public EncryptionFormat<ImageCtxT> {
public:
  LUKS1EncryptionFormat(encryption_algorithm_t alg, std::string_view passphrase)
      : m_alg(alg), m_passphrase(passphrase) {}

  std::unique_ptr<crypto::EncryptionFormat<ImageCtxT>> clone() const override {
    return std::make_unique<LUKS1EncryptionFormat>(m_alg, m_passphrase);
  }

  void format(ImageCtxT* ictx, Context* on_finish) override;
  void load(ImageCtxT* ictx, std::string* detected_format_name,
            Context* on_finish) override;

private:
  encryption_algorithm_t m_alg;
  std::string_view m_passphrase;
};

template <typename ImageCtxT>
class LUKS2EncryptionFormat : public EncryptionFormat<ImageCtxT> {
public:
  LUKS2EncryptionFormat(encryption_algorithm_t alg, std::string_view passphrase)
      : m_alg(alg), m_passphrase(passphrase) {}

  std::unique_ptr<crypto::EncryptionFormat<ImageCtxT>> clone() const override {
    return std::make_unique<LUKS2EncryptionFormat>(m_alg, m_passphrase);
  }

  void format(ImageCtxT* ictx, Context* on_finish) override;
  void load(ImageCtxT* ictx, std::string* detected_format_name,
            Context* on_finish) override;

private:
  encryption_algorithm_t m_alg;
  std::string_view m_passphrase;
};

} // namespace luks
} // namespace crypto
} // namespace librbd

extern template class librbd::crypto::luks::LUKSEncryptionFormat<
        librbd::ImageCtx>;
extern template class librbd::crypto::luks::LUKS1EncryptionFormat<
        librbd::ImageCtx>;
extern template class librbd::crypto::luks::LUKS2EncryptionFormat<
        librbd::ImageCtx>;

#endif // CEPH_LIBRBD_CRYPTO_LUKS_ENCRYPTION_FORMAT_H