Virgil Security Crypto library  2.4.6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
VirgilOperationCipher.h
1 
37 #ifndef VIRGIL_CRYPTO_VIRGIL_SYMMETRIC_CIPHER_H
38 #define VIRGIL_CRYPTO_VIRGIL_SYMMETRIC_CIPHER_H
39 
40 #include "../VirgilByteArray.h"
41 
42 #include <memory>
43 
44 namespace virgil { namespace crypto { inline namespace primitive {
45 
52 private:
53  template<class Impl>
54  struct Model;
55 public:
61  template<class Impl>
62  VirgilOperationCipher(Impl impl) : self_(std::make_shared<Model<Impl>>(std::move(impl))) {}
63 
68  size_t getKeySize() const {
69  return self_->doGetKeySize();
70  }
71 
76  size_t getNonceSize() const {
77  return self_->doGetNonceSize();
78  }
79 
90  const VirgilByteArray& plainText, const VirgilByteArray& key, const VirgilByteArray& nonce,
91  const VirgilByteArray& authData = VirgilByteArray()) const {
92 
93  return self_->doEncrypt(plainText, key, nonce, authData);
94  }
95 
106  const VirgilByteArray& cipherText, const VirgilByteArray& key, const VirgilByteArray& nonce,
107  const VirgilByteArray& authData = VirgilByteArray()) const {
108 
109  return self_->doDecrypt(cipherText, key, nonce, authData);
110  }
111 
116 
117 private:
118  struct Concept {
119  virtual size_t doGetKeySize() const = 0;
120 
121  virtual size_t doGetNonceSize() const = 0;
122 
123  virtual VirgilByteArray doEncrypt(
124  const VirgilByteArray& plainText, const VirgilByteArray& key, const VirgilByteArray& nonce,
125  const VirgilByteArray& authData) const = 0;
126 
127  virtual VirgilByteArray doDecrypt(
128  const VirgilByteArray& plainText, const VirgilByteArray& key, const VirgilByteArray& nonce,
129  const VirgilByteArray& authData) const = 0;
130 
131  virtual ~Concept() noexcept = default;
132  };
133 
134  template<class Impl>
135  struct Model : Concept {
136 
137  explicit Model(Impl impl) : impl_(std::move(impl)) {}
138 
139  size_t doGetKeySize() const override {
140  return impl_.getKeySize();
141  }
142 
143  size_t doGetNonceSize() const override {
144  return impl_.getNonceSize();
145  }
146 
147  VirgilByteArray doEncrypt(
148  const VirgilByteArray& plainText, const VirgilByteArray& key, const VirgilByteArray& nonce,
149  const VirgilByteArray& authData) const override {
150  return impl_.encrypt(plainText, key, nonce, authData);
151  }
152 
153  VirgilByteArray doDecrypt(
154  const VirgilByteArray& plainText, const VirgilByteArray& key, const VirgilByteArray& nonce,
155  const VirgilByteArray& authData) const override {
156  return impl_.decrypt(plainText, key, nonce, authData);
157  }
158  private:
159  Impl impl_;
160  };
161 
162 private:
163  std::shared_ptr<const Concept> self_;
164 };
165 
166 }}}
167 
168 #endif //VIRGIL_CRYPTO_VIRGIL_SYMMETRIC_CIPHER_H
size_t getNonceSize() const
Return size of the nonce.
Definition: VirgilOperationCipher.h:76
static VirgilOperationCipher getDefault()
Return default implementation.
Define proxy interface for the Symmetric Cipher functionality.
Definition: VirgilOperationCipher.h:51
std::vector< unsigned char > VirgilByteArray
This type represents a sequence of bytes.
Definition: VirgilByteArray.h:50
VirgilByteArray encrypt(const VirgilByteArray &plainText, const VirgilByteArray &key, const VirgilByteArray &nonce, const VirgilByteArray &authData=VirgilByteArray()) const
Encrypt given plain text.
Definition: VirgilOperationCipher.h:89
size_t getKeySize() const
Return size of the encryption/decryption key.
Definition: VirgilOperationCipher.h:68
VirgilByteArray decrypt(const VirgilByteArray &cipherText, const VirgilByteArray &key, const VirgilByteArray &nonce, const VirgilByteArray &authData=VirgilByteArray()) const
Decrypt given cipher text.
Definition: VirgilOperationCipher.h:105
VirgilOperationCipher(Impl impl)
Captures implementation object.
Definition: VirgilOperationCipher.h:62