SlunkCrypt/libslunkcrypt/include/slunkcrypt.hpp

170 lines
4.3 KiB
C++
Raw Permalink Normal View History

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
/*
* 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>
/*
* Namespace
*/
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:
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;
protected:
::slunkcrypt_t m_instance;
};
/*
* Class for encryption
*/
2021-04-02 16:37:19 +02:00
class Encryptor : public SlunkBase
2020-10-24 15:51:13 +02:00
{
public:
2021-04-02 16:37:19 +02:00
Encryptor(const std::string &passwd)
2020-10-24 15:51:13 +02:00
{
2021-04-02 16:37:19 +02:00
if (::slunkcrypt_generate_nonce(&m_nonce) != SLUNKCRYPT_SUCCESS)
{
throw std::runtime_error("SlunkCryptEncr: Failed to generate the seed value!");
}
2021-04-02 16:37:19 +02:00
if ((m_instance = ::slunkcrypt_alloc(m_nonce, (const uint8_t*)passwd.c_str(), passwd.length(), SLUNKCRYPT_ENCRYPT)) == SLUNKCRYPT_NULL)
{
throw std::runtime_error("SlunkCryptEncr: Failed to create encoder instance!");
}
2020-10-24 15:51:13 +02:00
}
2021-04-02 16:37:19 +02:00
Encryptor(Encryptor &&other) noexcept
2020-10-24 15:51:13 +02:00
{
this->m_instance = other.m_instance;
this->m_nonce = other.m_nonce;
other.m_instance = SLUNKCRYPT_NULL;
other.m_nonce = (uint64_t)(-1);
2020-10-24 15:51:13 +02:00
}
~Encryptor(void)
2020-10-24 15:51:13 +02:00
{
if (m_instance != SLUNKCRYPT_NULL)
{
2021-04-02 16:37:19 +02:00
::slunkcrypt_free(m_instance);
}
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)
{
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);
}
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-04-02 16:37:19 +02:00
return (::slunkcrypt_inplace(m_instance, buffer, length) == SLUNKCRYPT_SUCCESS);
}
2020-10-24 15:51:13 +02:00
2021-04-02 16:37:19 +02:00
bool inplace(std::vector<uint8_t> &buffer)
{
2021-04-02 16:37:19 +02:00
return (::slunkcrypt_inplace(m_instance, buffer.data(), buffer.size()) == SLUNKCRYPT_SUCCESS);
}
2020-10-24 15:51:13 +02:00
uint64_t get_nonce(void) const
2020-10-24 15:51:13 +02:00
{
return m_nonce;
2020-10-24 15:51:13 +02: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
{
public:
2021-04-02 16:37:19 +02:00
Decryptor(const uint64_t nonce, const std::string &passwd)
{
2021-04-02 16:37:19 +02:00
if ((m_instance = ::slunkcrypt_alloc(nonce, (const uint8_t*)passwd.c_str(), passwd.length(), SLUNKCRYPT_DECRYPT)) == SLUNKCRYPT_NULL)
{
throw std::runtime_error("SlunkCryptDecr: Failed to create decoder instance!");
}
}
2020-10-24 15:51:13 +02:00
2021-04-02 16:37:19 +02:00
Decryptor(Decryptor &&other) noexcept
2020-10-24 15:51:13 +02:00
{
this->m_instance = other.m_instance;
other.m_instance = SLUNKCRYPT_NULL;
2020-10-24 15:51:13 +02:00
}
~Decryptor(void)
{
if (m_instance != SLUNKCRYPT_NULL)
{
2021-04-02 16:37:19 +02:00
::slunkcrypt_free(m_instance);
}
}
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)
{
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);
}
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-04-02 16:37:19 +02:00
return (::slunkcrypt_inplace(m_instance, buffer, length) == SLUNKCRYPT_SUCCESS);
}
2020-10-24 15:51:13 +02:00
2021-04-02 16:37:19 +02:00
bool inplace(std::vector<uint8_t> &buffer)
{
2021-04-02 16:37:19 +02:00
return (::slunkcrypt_inplace(m_instance, buffer.data(), buffer.size()) == SLUNKCRYPT_SUCCESS);
}
2020-10-24 15:51:13 +02:00
private:
Decryptor(const Decryptor&) = delete;
Decryptor& operator=(const Decryptor&) = delete;
};
}
2020-10-24 15:51:13 +02:00
#endif