2022-03-23 22:33:45 +01:00
|
|
|
|
/******************************************************************************/
|
2020-10-24 15:51:13 +02:00
|
|
|
|
/* SlunkCrypt, by LoRd_MuldeR <MuldeR2@GMX.de> */
|
|
|
|
|
/* This work has been released under the CC0 1.0 Universal license! */
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
|
|
#ifndef INC_SLUNKCRYPT_PLUSPLUS
|
|
|
|
|
#define INC_SLUNKCRYPT_PLUSPLUS
|
|
|
|
|
|
2021-03-24 20:21:59 +01:00
|
|
|
|
/*
|
|
|
|
|
* Compiler check
|
|
|
|
|
*/
|
|
|
|
|
#if (!defined(__cplusplus) || (__cplusplus < 201103L)) && (!defined(_MSVC_LANG) || (_MSVC_LANG < 201103L))
|
|
|
|
|
#error This file requires compiler and library support for the ISO C++11 standard.
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Dependencies
|
|
|
|
|
*/
|
2020-10-24 15:51:13 +02:00
|
|
|
|
#include "slunkcrypt.h"
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <vector>
|
|
|
|
|
#include <stdexcept>
|
2022-03-23 22:33:45 +01:00
|
|
|
|
#include <cstring>
|
2020-10-24 15:51:13 +02:00
|
|
|
|
|
2021-03-24 20:21:59 +01:00
|
|
|
|
/*
|
2021-03-24 21:34:46 +01:00
|
|
|
|
* Namespace
|
2021-03-24 20:21:59 +01:00
|
|
|
|
*/
|
2021-03-24 21:34:46 +01:00
|
|
|
|
namespace slunkcrypt
|
2020-10-24 15:51:13 +02:00
|
|
|
|
{
|
2021-04-02 16:37:19 +02:00
|
|
|
|
/*
|
|
|
|
|
* Base class for SlunkCrypt
|
|
|
|
|
*/
|
|
|
|
|
class SlunkBase
|
|
|
|
|
{
|
|
|
|
|
public:
|
2022-10-15 20:40:39 +02:00
|
|
|
|
SlunkBase(const size_t thread_count, const bool legacy_compat, const bool debug_logging)
|
2022-03-23 22:33:45 +01:00
|
|
|
|
{
|
|
|
|
|
std::memset(&m_param, 0, sizeof(m_param));
|
|
|
|
|
m_param.version = ::SLUNKCRYPT_PARAM_VERSION;
|
|
|
|
|
m_param.thread_count = thread_count;
|
2022-10-15 20:40:39 +02:00
|
|
|
|
m_param.legacy_compat = legacy_compat ? SLUNKCRYPT_TRUE : SLUNKCRYPT_FALSE;
|
|
|
|
|
m_param.debug_logging = debug_logging ? SLUNKCRYPT_TRUE : SLUNKCRYPT_FALSE;
|
2022-03-23 22:33:45 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-02 16:37:19 +02:00
|
|
|
|
virtual bool process(const uint8_t *const input, uint8_t *const output, size_t length) = 0;
|
|
|
|
|
virtual bool process(const std::vector<uint8_t> &input, std::vector<uint8_t> &output) = 0;
|
|
|
|
|
virtual bool inplace(uint8_t *const buffer, size_t length) = 0;
|
|
|
|
|
virtual bool inplace(std::vector<uint8_t> &buffer) = 0;
|
2022-03-23 22:33:45 +01:00
|
|
|
|
|
2021-04-02 16:37:19 +02:00
|
|
|
|
protected:
|
2022-03-23 22:33:45 +01:00
|
|
|
|
::slunkparam_t m_param;
|
2021-04-02 16:37:19 +02:00
|
|
|
|
::slunkcrypt_t m_instance;
|
|
|
|
|
};
|
|
|
|
|
|
2021-03-24 21:34:46 +01:00
|
|
|
|
/*
|
|
|
|
|
* Class for encryption
|
|
|
|
|
*/
|
2021-04-02 16:37:19 +02:00
|
|
|
|
class Encryptor : public SlunkBase
|
2020-10-24 15:51:13 +02:00
|
|
|
|
{
|
2021-03-24 21:34:46 +01:00
|
|
|
|
public:
|
2022-10-15 20:40:39 +02:00
|
|
|
|
Encryptor(const std::string &passwd, const size_t thread_count = 0U, const bool legacy_compat = false, const bool debug_logging = false)
|
|
|
|
|
: SlunkBase(thread_count, legacy_compat, debug_logging)
|
2020-10-24 15:51:13 +02:00
|
|
|
|
{
|
2021-04-02 16:37:19 +02:00
|
|
|
|
if (::slunkcrypt_generate_nonce(&m_nonce) != SLUNKCRYPT_SUCCESS)
|
2021-03-24 21:34:46 +01:00
|
|
|
|
{
|
|
|
|
|
throw std::runtime_error("SlunkCryptEncr: Failed to generate the seed value!");
|
|
|
|
|
}
|
2022-03-23 22:33:45 +01:00
|
|
|
|
if ((m_instance = ::slunkcrypt_alloc_ext(m_nonce, (const uint8_t*)passwd.c_str(), passwd.length(), SLUNKCRYPT_ENCRYPT, &m_param)) == SLUNKCRYPT_NULL)
|
2021-03-24 21:34:46 +01:00
|
|
|
|
{
|
|
|
|
|
throw std::runtime_error("SlunkCryptEncr: Failed to create encoder instance!");
|
|
|
|
|
}
|
2020-10-24 15:51:13 +02:00
|
|
|
|
}
|
2021-03-24 21:34:46 +01:00
|
|
|
|
|
|
|
|
|
~Encryptor(void)
|
2020-10-24 15:51:13 +02:00
|
|
|
|
{
|
2021-03-24 21:34:46 +01:00
|
|
|
|
if (m_instance != SLUNKCRYPT_NULL)
|
|
|
|
|
{
|
2021-04-02 16:37:19 +02:00
|
|
|
|
::slunkcrypt_free(m_instance);
|
2021-03-24 21:34:46 +01:00
|
|
|
|
}
|
2020-10-24 15:51:13 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-02 16:37:19 +02:00
|
|
|
|
bool process(const uint8_t *const input, uint8_t *const output, size_t length)
|
2020-10-24 15:51:13 +02:00
|
|
|
|
{
|
2021-04-02 16:37:19 +02:00
|
|
|
|
return (::slunkcrypt_process(m_instance, input, output, length) == SLUNKCRYPT_SUCCESS);
|
2020-10-24 15:51:13 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-02 16:37:19 +02:00
|
|
|
|
bool process(const std::vector<uint8_t> &input, std::vector<uint8_t> &output)
|
2021-03-24 21:34:46 +01:00
|
|
|
|
{
|
|
|
|
|
if (output.size() >= input.size())
|
|
|
|
|
{
|
2021-04-02 16:37:19 +02:00
|
|
|
|
return (::slunkcrypt_process(m_instance, input.data(), output.data(), input.size()) == SLUNKCRYPT_SUCCESS);
|
2021-03-24 21:34:46 +01:00
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2020-10-24 15:51:13 +02:00
|
|
|
|
|
2021-04-02 16:37:19 +02:00
|
|
|
|
bool inplace(uint8_t *const buffer, size_t length)
|
2021-03-24 21:34:46 +01:00
|
|
|
|
{
|
2021-04-02 16:37:19 +02:00
|
|
|
|
return (::slunkcrypt_inplace(m_instance, buffer, length) == SLUNKCRYPT_SUCCESS);
|
2021-03-24 21:34:46 +01:00
|
|
|
|
}
|
2020-10-24 15:51:13 +02:00
|
|
|
|
|
2021-04-02 16:37:19 +02:00
|
|
|
|
bool inplace(std::vector<uint8_t> &buffer)
|
2021-03-24 21:34:46 +01:00
|
|
|
|
{
|
2021-04-02 16:37:19 +02:00
|
|
|
|
return (::slunkcrypt_inplace(m_instance, buffer.data(), buffer.size()) == SLUNKCRYPT_SUCCESS);
|
2021-03-24 21:34:46 +01:00
|
|
|
|
}
|
2020-10-24 15:51:13 +02:00
|
|
|
|
|
2021-03-24 21:34:46 +01:00
|
|
|
|
uint64_t get_nonce(void) const
|
2020-10-24 15:51:13 +02:00
|
|
|
|
{
|
2021-03-24 21:34:46 +01:00
|
|
|
|
return m_nonce;
|
2020-10-24 15:51:13 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-24 21:34:46 +01:00
|
|
|
|
private:
|
|
|
|
|
Encryptor(const Encryptor&) = delete;
|
|
|
|
|
Encryptor& operator=(const Encryptor&) = delete;
|
|
|
|
|
uint64_t m_nonce;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Class for decryption
|
|
|
|
|
*/
|
2021-04-02 16:37:19 +02:00
|
|
|
|
class Decryptor : public SlunkBase
|
2020-10-24 15:51:13 +02:00
|
|
|
|
{
|
2021-03-24 21:34:46 +01:00
|
|
|
|
public:
|
2022-10-15 20:40:39 +02:00
|
|
|
|
Decryptor(const std::string &passwd, const uint64_t nonce, const size_t thread_count = 0U, const bool legacy_compat = false, const bool debug_logging = false)
|
|
|
|
|
: SlunkBase(thread_count, legacy_compat, debug_logging)
|
2021-03-24 21:34:46 +01:00
|
|
|
|
{
|
2022-03-23 22:33:45 +01:00
|
|
|
|
if ((m_instance = ::slunkcrypt_alloc_ext(nonce, (const uint8_t*)passwd.c_str(), passwd.length(), SLUNKCRYPT_DECRYPT, &m_param)) == SLUNKCRYPT_NULL)
|
2021-03-24 21:34:46 +01:00
|
|
|
|
{
|
|
|
|
|
throw std::runtime_error("SlunkCryptDecr: Failed to create decoder instance!");
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-10-24 15:51:13 +02:00
|
|
|
|
|
2021-03-24 21:34:46 +01:00
|
|
|
|
~Decryptor(void)
|
|
|
|
|
{
|
|
|
|
|
if (m_instance != SLUNKCRYPT_NULL)
|
|
|
|
|
{
|
2021-04-02 16:37:19 +02:00
|
|
|
|
::slunkcrypt_free(m_instance);
|
2021-03-24 21:34:46 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-10-24 15:51:13 +02:00
|
|
|
|
|
2021-04-02 16:37:19 +02:00
|
|
|
|
bool process(const uint8_t *const input, uint8_t *const output, size_t length)
|
2020-10-24 15:51:13 +02:00
|
|
|
|
{
|
2021-04-02 16:37:19 +02:00
|
|
|
|
return (::slunkcrypt_process(m_instance, input, output, length) == SLUNKCRYPT_SUCCESS);
|
2020-10-24 15:51:13 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-02 16:37:19 +02:00
|
|
|
|
bool process(const std::vector<uint8_t> &input, std::vector<uint8_t> &output)
|
2021-03-24 21:34:46 +01:00
|
|
|
|
{
|
|
|
|
|
if (output.size() >= input.size())
|
|
|
|
|
{
|
2021-04-02 16:37:19 +02:00
|
|
|
|
return (::slunkcrypt_process(m_instance, input.data(), output.data(), input.size()) == SLUNKCRYPT_SUCCESS);
|
2021-03-24 21:34:46 +01:00
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2020-10-24 15:51:13 +02:00
|
|
|
|
|
2021-04-02 16:37:19 +02:00
|
|
|
|
bool inplace(uint8_t *const buffer, size_t length)
|
2021-03-24 21:34:46 +01:00
|
|
|
|
{
|
2021-04-02 16:37:19 +02:00
|
|
|
|
return (::slunkcrypt_inplace(m_instance, buffer, length) == SLUNKCRYPT_SUCCESS);
|
2021-03-24 21:34:46 +01:00
|
|
|
|
}
|
2020-10-24 15:51:13 +02:00
|
|
|
|
|
2021-04-02 16:37:19 +02:00
|
|
|
|
bool inplace(std::vector<uint8_t> &buffer)
|
2021-03-24 21:34:46 +01:00
|
|
|
|
{
|
2021-04-02 16:37:19 +02:00
|
|
|
|
return (::slunkcrypt_inplace(m_instance, buffer.data(), buffer.size()) == SLUNKCRYPT_SUCCESS);
|
2021-03-24 21:34:46 +01:00
|
|
|
|
}
|
2020-10-24 15:51:13 +02:00
|
|
|
|
|
2021-03-24 21:34:46 +01:00
|
|
|
|
private:
|
|
|
|
|
Decryptor(const Decryptor&) = delete;
|
|
|
|
|
Decryptor& operator=(const Decryptor&) = delete;
|
|
|
|
|
};
|
|
|
|
|
}
|
2020-10-24 15:51:13 +02:00
|
|
|
|
#endif
|