Enigma  3.2.0
A Simple, Reliable and Efficient Encryption Tool
Algorithm.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <Core/Core.hpp>
4 #include <Logger/Logger.hpp>
5 
6 // Crypto++
7 #pragma warning(push, 0) // This ignores all warnings raised inside External headers
9 #include <cryptlib.h> // CryptoPP
10 #include <filters.h> // StringSink, StringSource, StreamTransformationFilter
11 #include <hex.h> // HexEncoder, HexDecoder
12 #include <hkdf.h> // HKDF
13 #include <osrng.h> // AutoSeededRandomPool
14 #include <sha.h> // SHA256
15 //#include <modes.h> // Paddings CBC, EBC...
16 #include <aes.h> // AES
17 #include <eax.h> // EAX Authentication Mode
18 #include <gcm.h> // GCM Authentication Mode
19 //#include <chacha.h> // ChaCha/Salsa20
20 #include <blowfish.h> // Blowfish
21 #include <chachapoly.h> // ChaCha20Poly1305 Authenticated
22 #include <des.h> // TripleDES
23 #include <idea.h> // IDEA
24 #include <pssr.h> // RSA Signer & Verifier
25 #include <rsa.h> // RSA
26 #include <twofish.h> // Twofish
27 #include <camellia.h> // Camellia
28 #include <serpent.h> // Serpent
29 #pragma warning(pop)
30 static_assert(sizeof(Enigma::byte) == sizeof(CryptoPP::byte), "Enigma byte size must be the same size with Crypto++'s byte");
31 
32 
34 
38 class AES;
39 class Twofish;
40 class TripleDES;
41 class Blowfish;
42 class IDEA;
43 class ChaCha20Poly1305;
44 class RSA;
45 class Camellia;
46 class Serpent;
47 
53 class Algorithm {
54  public:
62  enum class Intent : Enigma::byte {
63  None = 0 << 0,
64  Encrypt = 1 << 1,
65  Decrypt = 1 << 2,
66  All = Encrypt | Decrypt
67  };
69 
70 
74  enum class Type : byte {
75  AES = 0x01, // AES-GCM
76  Twofish, // Twofish-GCM
77  TripleDES, // TripleDES-EAX
78  Blowfish, // Blowfish-EAX
79  IDEA, // IDEA-EAX
80  ChaCha20Poly1305, // ChaCha20Poly1305
81  RSA, // RSA-OAEP-SHA256
82  Camellia, // Camellia-GCM
83  Serpent, // Serpent-GCM
84 
86  };
87 
88  inline static const std::unordered_map<Type, std::string_view> ALGORITHM_DESCRIPTIONS = {
89  {Type::AES, "Symmetric, extremely high security, efficient, widely supported"},
90  {Type::Twofish, "Symmetric, high security, flexible and efficient"},
91  {Type::TripleDES, "Symmetric, moderate security, suitable for legacy systems"},
92  {Type::Blowfish, "Symmetric, moderate security, fast and simple"},
93  {Type::IDEA, "Symmetric, moderate to high security, simple and efficient"},
94  {Type::ChaCha20Poly1305, "Symmetric, very high security, high performance in software"},
95  {Type::RSA, "Asymmetric, very high security, excellent for key exchange"},
96  {Type::Camellia, "Symmetric, very high security, comparable to AES"},
97  {Type::Serpent, "Symmetric, high security, conservative design"},
98  };
99 
100  public:
101  explicit Algorithm(const Type type, const Intent intent = Intent::Encrypt | Intent::Decrypt) noexcept;
102  virtual ~Algorithm() noexcept;
103 
104  public:
112  virtual std::vector<byte> Encrypt(const std::string& password, const byte *buffer, const std::size_t buffSize) = 0;
113  virtual std::vector<byte> Encrypt(const std::string& password, const std::vector<byte>& buffer) = 0;
121  virtual std::vector<byte> Decrypt(const std::string& password, const byte *cipher, const std::size_t cipherSize) = 0;
122  virtual std::vector<byte> Decrypt(const std::string& password, const std::vector<byte>& cipher) = 0;
123 
131  virtual void Encrypt(const std::string& password, const fs::path& in_filename, const fs::path& out_filename) = 0;
139  virtual void Decrypt(const std::string& password, const fs::path& in_filename, const fs::path& out_filename) = 0;
140 
141  public: /* Create polymorphic algorithm by either mode name or type*/
146  static std::unique_ptr<Algorithm> CreateFromName(const std::string& name, const Intent intent);
150  static std::unique_ptr<Algorithm> CreateFromType(const Type type, const Intent intent);
151 
152  public:
153  Type GetType() const noexcept { return m_type; }
154  void SetType(const Type type) noexcept { this->m_type = type; }
155  std::string GetTypeString() const noexcept { return AlgoTypeEnumToStr(m_type); }
156 
157  protected:
161  static std::vector<byte> GenerateRandomIV(const std::size_t size);
162 
163 
164  public:
165 #if 0
169  static Type DetectFromCipher(const std::string& cipher);
170 
174  static Type DetectFromCipherBase64(const std::string& cipher_base64);
175 
179  static Type DetectFromFile(const std::string& filename);
180 #endif
181 
185  static std::string AlgoTypeEnumToStr(const Algorithm::Type e) noexcept;
186 
190  static std::string GetSupportedAlgorithmsStr() noexcept;
191 
195  static std::vector<std::pair<std::string, Algorithm::Type>> GetSupportedAlgorithms() noexcept;
196 
197  protected:
200  inline static std::unique_ptr<CryptoPP::AutoSeededRandomPool> m_auto_seeded_random_pool{nullptr};
201 };
202 
#define NS_ENIGMA_BEGIN
Enable/Disable Assertions.
Definition: Macros.hpp:13
#define NS_ENIGMA_END
Definition: Macros.hpp:14
#define ENIGMA_ENUM_DECLARE_BEGIN_END(begin)
Allows looping over an enum by providing a BEGIN and END enum values NOTE: should be placed at the en...
Definition: Macros.hpp:148
Definition: AES.hpp:10
Algorithm abstract class.
Definition: Algorithm.hpp:53
Type GetType() const noexcept
Definition: Algorithm.hpp:153
virtual std::vector< byte > Decrypt(const std::string &password, const byte *cipher, const std::size_t cipherSize)=0
std::string GetTypeString() const noexcept
Definition: Algorithm.hpp:155
static std::string AlgoTypeEnumToStr(const Algorithm::Type e) noexcept
Type m_type
Definition: Algorithm.hpp:198
Algorithm(const Type type, const Intent intent=Intent::Encrypt|Intent::Decrypt) noexcept
virtual ~Algorithm() noexcept
static const std::unordered_map< Type, std::string_view > ALGORITHM_DESCRIPTIONS
Definition: Algorithm.hpp:88
static std::vector< byte > GenerateRandomIV(const std::size_t size)
static std::unique_ptr< CryptoPP::AutoSeededRandomPool > m_auto_seeded_random_pool
Definition: Algorithm.hpp:200
static std::vector< std::pair< std::string, Algorithm::Type > > GetSupportedAlgorithms() noexcept
void SetType(const Type type) noexcept
Definition: Algorithm.hpp:154
static std::unique_ptr< Algorithm > CreateFromName(const std::string &name, const Intent intent)
ENIGMA_ENUM_CLASS_BITWISE_OPERATORS(Intent, Enigma::byte)
static std::unique_ptr< Algorithm > CreateFromType(const Type type, const Intent intent)
static std::string GetSupportedAlgorithmsStr() noexcept
Intent m_intent
Definition: Algorithm.hpp:199
virtual std::vector< byte > Encrypt(const std::string &password, const byte *buffer, const std::size_t buffSize)=0
Definition: IDEA.hpp:11
Definition: RSA.hpp:16
std::uint8_t byte
Definition: Types.hpp:12