diff --git a/frontend/src/main.c b/frontend/src/main.c index ec2268d..8b29f14 100644 --- a/frontend/src/main.c +++ b/frontend/src/main.c @@ -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); fflush(stderr); - uint64_t seed; - if (slunkcrypt_generate_seed(&seed) != SLUNKCRYPT_SUCCESS) + uint64_t salt; + 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; } - 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; } - ctx = slunkcrypt_alloc(seed, (const uint8_t*)passphrase, strlen(passphrase)); + ctx = slunkcrypt_alloc(salt, (const uint8_t*)passphrase, strlen(passphrase)); if (!ctx) { 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); fflush(stderr); - uint64_t seed; - if (fread(&seed, sizeof(uint64_t), 1U, file_in) < 1U) + uint64_t salt; + 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; } - ctx = slunkcrypt_alloc(seed, (const uint8_t*)passphrase, strlen(passphrase)); + ctx = slunkcrypt_alloc(salt, (const uint8_t*)passphrase, strlen(passphrase)); if (!ctx) { 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; slunkcrypt_t ctx = SLUNKCRYPT_NULL; - uint64_t seed; - if (slunkcrypt_generate_seed(&seed) != SLUNKCRYPT_SUCCESS) + uint64_t salt; + 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; } @@ -485,7 +485,7 @@ static int run_test_case(const char *const message, const uint64_t checksum) 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) { 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; } - 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) { FPUTS((status == SLUNKCRYPT_ABORTED) ? T("\n\nProcess interrupted!\n\n") : T("\n\nWhoops: Failed to initialize decoder!\n\n"), stderr); diff --git a/libslunkcrypt/include/slunkcrypt.h b/libslunkcrypt/include/slunkcrypt.h index dfeef17..338d1b6 100644 --- a/libslunkcrypt/include/slunkcrypt.h +++ b/libslunkcrypt/include/slunkcrypt.h @@ -16,9 +16,9 @@ #define SLUNKCRYPT_EXPORT 0 #endif -/* - * Win32 DLL support - */ + /* + * DLL support + */ #if defined(_MSC_VER) && SLUNKCRYPT_SHARED #if SLUNKCRYPT_EXPORT #define SLUNKCRYPT_API __declspec(dllexport) @@ -28,27 +28,36 @@ #else #define SLUNKCRYPT_API #endif - + +/* + * C++ support + */ +#ifndef __cplusplus #include #include +#else +#include +#include +extern "C" { +#endif - /* - * Opaque handle to internal state - */ +/* + * Opaque handle to internal state + */ typedef uintptr_t slunkcrypt_t; #define SLUNKCRYPT_NULL ((slunkcrypt_t)NULL) /* * Error codes */ -static const int SLUNKCRYPT_SUCCESS = 0; +static const int SLUNKCRYPT_SUCCESS = 0; static const int SLUNKCRYPT_FAILURE = -1; static const int SLUNKCRYPT_ABORTED = -2; /* * Limits */ -static const size_t SLUNKCRYPT_PWDLEN_MIN = 5U; +static const size_t SLUNKCRYPT_PWDLEN_MIN = 5U; static const size_t SLUNKCRYPT_PWDLEN_MAX = 512U; /* @@ -77,7 +86,7 @@ SLUNKCRYPT_API void slunkcrypt_cleanup(void); /* * 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 @@ -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 void slunkcrypt_bzero(void* const ptr, const size_t length); +#ifdef __cplusplus +} +#endif #endif diff --git a/libslunkcrypt/include/slunkcrypt.hpp b/libslunkcrypt/include/slunkcrypt.hpp new file mode 100644 index 0000000..614f8e2 --- /dev/null +++ b/libslunkcrypt/include/slunkcrypt.hpp @@ -0,0 +1,135 @@ +/******************************************************************************/ +/* SlunkCrypt, by LoRd_MuldeR */ +/* This work has been released under the CC0 1.0 Universal license! */ +/******************************************************************************/ + +#ifndef INC_SLUNKCRYPT_PLUSPLUS +#define INC_SLUNKCRYPT_PLUSPLUS + +#include "slunkcrypt.h" +#include +#include +#include + +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 &input, std::vector &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 &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& input, std::vector& 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& 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 diff --git a/libslunkcrypt/libSlunkCrypt.vcxproj b/libslunkcrypt/libSlunkCrypt.vcxproj index 0cebba1..0f8089e 100644 --- a/libslunkcrypt/libSlunkCrypt.vcxproj +++ b/libslunkcrypt/libSlunkCrypt.vcxproj @@ -32,6 +32,7 @@ + diff --git a/libslunkcrypt/libSlunkCrypt.vcxproj.filters b/libslunkcrypt/libSlunkCrypt.vcxproj.filters index 025ea26..78e3bd9 100644 --- a/libslunkcrypt/libSlunkCrypt.vcxproj.filters +++ b/libslunkcrypt/libSlunkCrypt.vcxproj.filters @@ -29,5 +29,8 @@ Header Files + + Header Files + \ No newline at end of file diff --git a/libslunkcrypt/src/slunkcrypt.c b/libslunkcrypt/src/slunkcrypt.c index eb7ede2..7c76050 100644 --- a/libslunkcrypt/src/slunkcrypt.c +++ b/libslunkcrypt/src/slunkcrypt.c @@ -247,7 +247,7 @@ static FORCE_INLINE uint8_t process_dec(crypt_state_t* const crypt_state, uint8_ // Public API // ========================================================================== -int slunkcrypt_generate_seed(uint64_t* const seed) +int slunkcrypt_generate_salt(uint64_t* const seed) { if (!seed) {