C++ wrapper has been implemented.

This commit is contained in:
LoRd_MuldeR 2020-10-24 15:51:13 +02:00
parent 49cc6ec674
commit ec7176feba
Signed by: mulder
GPG Key ID: 2B5913365F57E03F
6 changed files with 177 additions and 26 deletions

View File

@ -189,20 +189,20 @@ static int encrypt(const char* const passphrase, const CHR* const input_path, co
FPUTS(T("Encrypting file contents, please be patient... "), stderr); FPUTS(T("Encrypting file contents, please be patient... "), stderr);
fflush(stderr); fflush(stderr);
uint64_t seed; uint64_t salt;
if (slunkcrypt_generate_seed(&seed) != SLUNKCRYPT_SUCCESS) if (slunkcrypt_generate_salt(&salt) != SLUNKCRYPT_SUCCESS)
{ {
FPUTS(T("\n\nSlunkCrypt error: Failed to generate seed!\n\n"), stderr); FPUTS(T("\n\nSlunkCrypt error: Failed to generate salt!\n\n"), stderr);
goto clean_up; goto clean_up;
} }
if (fwrite(&seed, sizeof(uint64_t), 1U, file_out) < 1U) if (fwrite(&salt, sizeof(uint64_t), 1U, file_out) < 1U)
{ {
FPUTS(T("\n\nI/O error: Failed to write seed value!\n\n"), stderr); FPUTS(T("\n\nI/O error: Failed to write salt value!\n\n"), stderr);
goto clean_up; goto clean_up;
} }
ctx = slunkcrypt_alloc(seed, (const uint8_t*)passphrase, strlen(passphrase)); ctx = slunkcrypt_alloc(salt, (const uint8_t*)passphrase, strlen(passphrase));
if (!ctx) if (!ctx)
{ {
FPUTS(g_slunkcrypt_abort_flag ? T("\n\nProcess interrupted!\n\n") : T("\n\nSlunkCrypt error: Failed to initialize encryption!\n\n"), stderr); FPUTS(g_slunkcrypt_abort_flag ? T("\n\nProcess interrupted!\n\n") : T("\n\nSlunkCrypt error: Failed to initialize encryption!\n\n"), stderr);
@ -334,14 +334,14 @@ static int decrypt(const char* const passphrase, const CHR* const input_path, co
FPUTS(T("Decrypting file contents, please be patient... "), stderr); FPUTS(T("Decrypting file contents, please be patient... "), stderr);
fflush(stderr); fflush(stderr);
uint64_t seed; uint64_t salt;
if (fread(&seed, sizeof(uint64_t), 1U, file_in) < 1U) if (fread(&salt, sizeof(uint64_t), 1U, file_in) < 1U)
{ {
FPUTS(T("\n\nI/O error: Failed to read seed value!\n\n"), stderr); FPUTS(T("\n\nI/O error: Failed to read salt value!\n\n"), stderr);
goto clean_up; goto clean_up;
} }
ctx = slunkcrypt_alloc(seed, (const uint8_t*)passphrase, strlen(passphrase)); ctx = slunkcrypt_alloc(salt, (const uint8_t*)passphrase, strlen(passphrase));
if (!ctx) if (!ctx)
{ {
FPUTS(g_slunkcrypt_abort_flag ? T("\n\nProcess interrupted!\n\n") : T("\n\nSlunkCrypt error: Failed to initialize decryption!\n\n"), stderr); FPUTS(g_slunkcrypt_abort_flag ? T("\n\nProcess interrupted!\n\n") : T("\n\nSlunkCrypt error: Failed to initialize decryption!\n\n"), stderr);
@ -464,10 +464,10 @@ static int run_test_case(const char *const message, const uint64_t checksum)
const size_t length = strlen(message) + 1U; const size_t length = strlen(message) + 1U;
slunkcrypt_t ctx = SLUNKCRYPT_NULL; slunkcrypt_t ctx = SLUNKCRYPT_NULL;
uint64_t seed; uint64_t salt;
if (slunkcrypt_generate_seed(&seed) != SLUNKCRYPT_SUCCESS) if (slunkcrypt_generate_salt(&salt) != SLUNKCRYPT_SUCCESS)
{ {
FPUTS(T("\n\nWhoops: Failed to generate seed!\n\n"), stderr); FPUTS(T("\n\nWhoops: Failed to generate salt!\n\n"), stderr);
return EXIT_FAILURE; return EXIT_FAILURE;
} }
@ -485,7 +485,7 @@ static int run_test_case(const char *const message, const uint64_t checksum)
goto clean_up; goto clean_up;
} }
ctx = slunkcrypt_alloc(seed, (const uint8_t*)passphrase, strlen(passphrase)); ctx = slunkcrypt_alloc(salt, (const uint8_t*)passphrase, strlen(passphrase));
if (!ctx) if (!ctx)
{ {
FPUTS(g_slunkcrypt_abort_flag ? T("\n\nProcess interrupted!\n\n") : T("\n\nWhoops: Failed to initialize encoder!\n\n"), stderr); FPUTS(g_slunkcrypt_abort_flag ? T("\n\nProcess interrupted!\n\n") : T("\n\nWhoops: Failed to initialize encoder!\n\n"), stderr);
@ -505,7 +505,7 @@ static int run_test_case(const char *const message, const uint64_t checksum)
goto clean_up; goto clean_up;
} }
status = slunkcrypt_reset(ctx, seed, (const uint8_t*)passphrase, strlen(passphrase)); status = slunkcrypt_reset(ctx, salt, (const uint8_t*)passphrase, strlen(passphrase));
if (status != SLUNKCRYPT_SUCCESS) if (status != SLUNKCRYPT_SUCCESS)
{ {
FPUTS((status == SLUNKCRYPT_ABORTED) ? T("\n\nProcess interrupted!\n\n") : T("\n\nWhoops: Failed to initialize decoder!\n\n"), stderr); FPUTS((status == SLUNKCRYPT_ABORTED) ? T("\n\nProcess interrupted!\n\n") : T("\n\nWhoops: Failed to initialize decoder!\n\n"), stderr);

View File

@ -17,7 +17,7 @@
#endif #endif
/* /*
* Win32 DLL support * DLL support
*/ */
#if defined(_MSC_VER) && SLUNKCRYPT_SHARED #if defined(_MSC_VER) && SLUNKCRYPT_SHARED
#if SLUNKCRYPT_EXPORT #if SLUNKCRYPT_EXPORT
@ -29,8 +29,17 @@
#define SLUNKCRYPT_API #define SLUNKCRYPT_API
#endif #endif
/*
* C++ support
*/
#ifndef __cplusplus
#include <stdlib.h> #include <stdlib.h>
#include <stdint.h> #include <stdint.h>
#else
#include <cstdlib>
#include <cstdint>
extern "C" {
#endif
/* /*
* Opaque handle to internal state * Opaque handle to internal state
@ -77,7 +86,7 @@ SLUNKCRYPT_API void slunkcrypt_cleanup(void);
/* /*
* Seed generator * Seed generator
*/ */
SLUNKCRYPT_API int slunkcrypt_generate_seed(uint64_t* const seed); SLUNKCRYPT_API int slunkcrypt_generate_salt(uint64_t* const seed);
/* /*
* Allocate, reset or free state * Allocate, reset or free state
@ -104,4 +113,7 @@ SLUNKCRYPT_API int slunkcrypt_decrypt_inplace(const slunkcrypt_t context, uint8_
SLUNKCRYPT_API int slunkcrypt_random_bytes(uint8_t* const buffer, const size_t length); SLUNKCRYPT_API int slunkcrypt_random_bytes(uint8_t* const buffer, const size_t length);
SLUNKCRYPT_API void slunkcrypt_bzero(void* const ptr, const size_t length); SLUNKCRYPT_API void slunkcrypt_bzero(void* const ptr, const size_t length);
#ifdef __cplusplus
}
#endif
#endif #endif

View File

@ -0,0 +1,135 @@
/******************************************************************************/
/* 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
#include "slunkcrypt.h"
#include <string>
#include <vector>
#include <stdexcept>
class SlunkCryptEncr
{
public:
SlunkCryptEncr(const std::string &passwd)
{
if (slunkcrypt_generate_salt(&m_salt) != SLUNKCRYPT_SUCCESS)
{
throw std::runtime_error("Failed to generate the seed value!");
}
if ((m_instance = slunkcrypt_alloc(m_salt, (const uint8_t*)passwd.c_str(), passwd.length())) == SLUNKCRYPT_NULL)
{
throw std::runtime_error("Failed to create encoder instance!");
}
}
SlunkCryptEncr(SlunkCryptEncr &&other) noexcept
{
this->m_instance = other.m_instance;
this->m_salt = other.m_salt;
other.m_instance = SLUNKCRYPT_NULL;
}
~SlunkCryptEncr(void)
{
if (m_instance != SLUNKCRYPT_NULL)
{
slunkcrypt_free(m_instance);
}
}
bool encrypt(const uint8_t* const input, uint8_t* const output, size_t length)
{
return (slunkcrypt_encrypt(m_instance, input, output, length) == SLUNKCRYPT_SUCCESS);
}
bool encrypt(const std::vector<uint8_t> &input, std::vector<uint8_t> &output)
{
if (output.size() >= input.size())
{
return (slunkcrypt_encrypt(m_instance, input.data(), output.data(), input.size()) == SLUNKCRYPT_SUCCESS);
}
return false;
}
bool encrypt_inplace(uint8_t* const buffer, size_t length)
{
return (slunkcrypt_encrypt_inplace(m_instance, buffer, length) == SLUNKCRYPT_SUCCESS);
}
bool encrypt_inplace(std::vector<uint8_t> &buffer)
{
return (slunkcrypt_encrypt_inplace(m_instance, buffer.data(), buffer.size()) == SLUNKCRYPT_SUCCESS);
}
uint64_t salt_value(void) const
{
return m_salt;
}
private:
SlunkCryptEncr(const SlunkCryptEncr&);
SlunkCryptEncr& operator=(const SlunkCryptEncr&);
uint64_t m_salt;
slunkcrypt_t m_instance;
};
class SlunkCryptDecr
{
public:
SlunkCryptDecr(const uint64_t salt, const std::string& passwd)
{
if ((m_instance = slunkcrypt_alloc(salt, (const uint8_t*)passwd.c_str(), passwd.length())) == SLUNKCRYPT_NULL)
{
throw std::runtime_error("Failed to create encoder instance!");
}
}
SlunkCryptDecr(SlunkCryptDecr &&other) noexcept
{
this->m_instance = other.m_instance;
other.m_instance = SLUNKCRYPT_NULL;
}
~SlunkCryptDecr(void)
{
if (m_instance != SLUNKCRYPT_NULL)
{
slunkcrypt_free(m_instance);
}
}
bool decrypt(const uint8_t* const input, uint8_t* const output, size_t length)
{
return (slunkcrypt_decrypt(m_instance, input, output, length) == SLUNKCRYPT_SUCCESS);
}
bool decrypt(const std::vector<uint8_t>& input, std::vector<uint8_t>& output)
{
if (output.size() >= input.size())
{
return (slunkcrypt_decrypt(m_instance, input.data(), output.data(), input.size()) == SLUNKCRYPT_SUCCESS);
}
return false;
}
bool decrypt_inplace(uint8_t* const buffer, size_t length)
{
return (slunkcrypt_decrypt_inplace(m_instance, buffer, length) == SLUNKCRYPT_SUCCESS);
}
bool decrypt_inplace(std::vector<uint8_t>& buffer)
{
return (slunkcrypt_decrypt_inplace(m_instance, buffer.data(), buffer.size()) == SLUNKCRYPT_SUCCESS);
}
private:
SlunkCryptDecr(const SlunkCryptDecr&);
SlunkCryptDecr& operator=(const SlunkCryptDecr&);
slunkcrypt_t m_instance;
};
#endif

View File

@ -32,6 +32,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="include\slunkcrypt.h" /> <ClInclude Include="include\slunkcrypt.h" />
<ClInclude Include="include\slunkcrypt.hpp" />
<ClInclude Include="src\version.h" /> <ClInclude Include="src\version.h" />
</ItemGroup> </ItemGroup>
<PropertyGroup Label="Globals"> <PropertyGroup Label="Globals">

View File

@ -29,5 +29,8 @@
<ClInclude Include="src\version.h"> <ClInclude Include="src\version.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="include\slunkcrypt.hpp">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -247,7 +247,7 @@ static FORCE_INLINE uint8_t process_dec(crypt_state_t* const crypt_state, uint8_
// Public API // Public API
// ========================================================================== // ==========================================================================
int slunkcrypt_generate_seed(uint64_t* const seed) int slunkcrypt_generate_salt(uint64_t* const seed)
{ {
if (!seed) if (!seed)
{ {