Enigma 3.2.2
A Simple, Reliable and Efficient Encryption Tool
Loading...
Searching...
No Matches
RSA.hpp
Go to the documentation of this file.
1#pragma once
3#include <memory>
4#include <optional>
5#include <rsa.h>
6#include <base64.h>
8#include <pssr.h>
9
16class RSA : public Algorithm {
17 inline static const std::string BEGIN_RSA_PRIVATE_KEY_HEADER = "-----BEGIN RSA PRIVATE KEY-----\n";
18 inline static const std::string END_RSA_PRIVATE_KEY_FOOTER = "-----END RSA PRIVATE KEY-----\n";
19 inline static const std::string BEGIN_RSA_PUBLIC_KEY_HEADER = "-----BEGIN RSA PUBLIC KEY-----\n";
20 inline static const std::string END_RSA_PUBLIC_KEY_FOOTER = "-----END RSA PUBLIC KEY-----\n";
21
22 public:
26 explicit RSA(const Algorithm::Intent intent) noexcept;
27 ~RSA() noexcept override;
28
29 struct RSASettings {
30 std::size_t keySize{};
31 std::optional<std::string> privateKey{std::nullopt};
32 //std::optional<std::string> publicKey{std::nullopt};
33 std::optional<fs::path> privateKeyFilename{std::nullopt};
34 //std::optional<fs::path> publicKeyFilename{std::nullopt};
35 };
36
37 void setSettings(RSASettings&& settings);
38
39
40 public:
41 std::vector<byte> Encrypt(const std::string& password, const byte *buffer, const std::size_t buffSize) override;
42 std::vector<byte> Encrypt(const std::string& password, const std::vector<byte>& buffer) override;
43 std::vector<byte> Decrypt(const std::string& password, const byte *cipher, const std::size_t cipherSize) override;
44 std::vector<byte> Decrypt(const std::string& password, const std::vector<byte>& cipher) override;
45 void Encrypt(const std::string& password, const fs::path& in_filename, const fs::path& out_filename) override;
46 void Decrypt(const std::string& password, const fs::path& in_filename, const fs::path& out_filename) override;
47
52 [[deprecated("Not implemented yet")]]
53 bool SignAndVerify(const std::vector<byte>& message) {
54 /*
55 * TO SIGN: YOU NEED PRIV KEY
56 * TO VERIFY: YOU NEED PUB KEY
57 *
58 */
59 // Verifier object
60 // Signer object
61 using namespace CryptoPP;
62 RSASS<PSS, SHA256>::Signer signer(*m_private_key);
63
64 // Create signature space
65 size_t length = signer.MaxSignatureLength();
66 SecByteBlock signature(length);
67
68 // Sign message
69 length = signer.SignMessage(*m_auto_seeded_random_pool, message.data(),
70 message.size(),
71 signature);
72
73 // Resize now we know the true size of the signature
74 signature.resize(length);
75
76 // Verifier object
77 RSASS<PSS, SHA256>::Verifier verifier(*m_public_key);
78
79 // Verify
80 bool result = verifier.VerifyMessage(message.data(),
81 message.size(), signature, signature.size());
82 return result;
83 }
84
85 private:
86 void initialize();
87 void setPrivateKey(const std::string& privateKey);
88 void setPublicKey(const std::string& publicKey);
89
90 public:
91 std::string getPrivateKey() const;
92 std::string getPublicKey() const;
93
94 public:
95 std::size_t getMaximumBufferSize() const;
96 static std::size_t getMaximumBufferSizeFromKeySize(const std::size_t keySize);
97
98 private:
99 std::unique_ptr<CryptoPP::RSAES<CryptoPP::OAEP<CryptoPP::SHA256>>::Encryptor> m_rsa_encryptor;
100 std::unique_ptr<CryptoPP::RSAES<CryptoPP::OAEP<CryptoPP::SHA256>>::Decryptor> m_rsa_decryptor;
101 std::unique_ptr<CryptoPP::RSA::PrivateKey> m_private_key;
102 std::unique_ptr<CryptoPP::RSA::PublicKey> m_public_key;
103 std::unique_ptr<CryptoPP::InvertibleRSAFunction> m_params;
104
105 std::unique_ptr<RSASettings> m_settings{};
106
107 public:
109 inline static const std::map<std::size_t, std::string_view> RECOMMENDED_KEY_SIZES = {
110 {2048, "Secure for use at least until 2030."},
111 {3072, "Secure for use beyond 2030."},
112 {4096, "Secure for longer-term security needs."},
113 {8192, "Very high security but very slow. Suitable for cases requiring extremely high security,\nthough it's rarely used in practice due to performance concerns."},
114 {16384, "Almost never used due to extreme computational cost.\nThis key size will take about ~5 minutes to complete"},
115 {32768, "Theoretical and impractical for most applications\ndue to excessive computational and storage requirements.\nThis key size will take a lot of time to complete"},
116 };
117};
118
#define NS_ENIGMA_BEGIN
Enable/Disable Assertions.
Definition Macros.hpp:13
#define NS_ENIGMA_END
Definition Macros.hpp:14
Algorithm abstract class.
Definition Algorithm.hpp:53
static std::unique_ptr< CryptoPP::AutoSeededRandomPool > m_auto_seeded_random_pool
Definition RSA.hpp:16
std::size_t getMaximumBufferSize() const
~RSA() noexcept override
std::vector< byte > Decrypt(const std::string &password, const std::vector< byte > &cipher) override
void Encrypt(const std::string &password, const fs::path &in_filename, const fs::path &out_filename) override
RSA(const Algorithm::Intent intent) noexcept
std::vector< byte > Encrypt(const std::string &password, const byte *buffer, const std::size_t buffSize) override
void Decrypt(const std::string &password, const fs::path &in_filename, const fs::path &out_filename) override
std::vector< byte > Decrypt(const std::string &password, const byte *cipher, const std::size_t cipherSize) override
std::string getPublicKey() const
bool SignAndVerify(const std::vector< byte > &message)
Definition RSA.hpp:53
void setSettings(RSASettings &&settings)
static const std::map< std::size_t, std::string_view > RECOMMENDED_KEY_SIZES
Key, description.
Definition RSA.hpp:109
std::vector< byte > Encrypt(const std::string &password, const std::vector< byte > &buffer) override
static std::size_t getMaximumBufferSizeFromKeySize(const std::size_t keySize)
std::string getPrivateKey() const