From 06f57ca483e3e54c7e93da77ce94e13d4f9697ee Mon Sep 17 00:00:00 2001 From: LoRd_MuldeR Date: Fri, 2 Apr 2021 16:37:19 +0200 Subject: [PATCH] Clean up function names. --- examples/example_decrypt.cpp | 2 +- examples/example_encrypt.cpp | 2 +- frontend/src/main.c | 16 +++---- libslunkcrypt/include/slunkcrypt.h | 2 +- libslunkcrypt/include/slunkcrypt.hpp | 72 ++++++++++++++++------------ libslunkcrypt/src/slunkcrypt.c | 14 ++---- 6 files changed, 58 insertions(+), 50 deletions(-) diff --git a/examples/example_decrypt.cpp b/examples/example_decrypt.cpp index 2acb964..712489e 100644 --- a/examples/example_decrypt.cpp +++ b/examples/example_decrypt.cpp @@ -74,7 +74,7 @@ static int decrypt_main(int argc, char *argv[]) const std::streamsize count = file_src.gcount(); if (count > 0) { - if (!slunk_decrypt.process_inplace(buffer, (size_t)count)) + if (!slunk_decrypt.inplace(buffer, (size_t)count)) { std::cerr << "failed!\n\nError: SlunkCrypt decryption has failed!" << std::endl; return -1; diff --git a/examples/example_encrypt.cpp b/examples/example_encrypt.cpp index aa867ca..483dbbd 100644 --- a/examples/example_encrypt.cpp +++ b/examples/example_encrypt.cpp @@ -72,7 +72,7 @@ static int encrypt_main(int argc, char *argv[]) const std::streamsize count = file_src.gcount(); if (count > 0) { - if (!slunk_encrypt.process_inplace(buffer, (size_t)count)) + if (!slunk_encrypt.inplace(buffer, (size_t)count)) { std::cerr << "failed!\n\nError: SlunkCrypt encryption has failed!" << std::endl; return -1; diff --git a/frontend/src/main.c b/frontend/src/main.c index eafd534..c691116 100644 --- a/frontend/src/main.c +++ b/frontend/src/main.c @@ -337,7 +337,7 @@ static int encrypt(const char* const passphrase, const CHR* const input_path, co { blake2s_update(&blake2s_state, buffer, count); bytes_read += count; - if ((status = slunkcrypt_process_inplace(ctx, buffer, count)) != SLUNKCRYPT_SUCCESS) + if ((status = slunkcrypt_inplace(ctx, buffer, count)) != SLUNKCRYPT_SUCCESS) { FPUTS((status == SLUNKCRYPT_ABORTED) ? T("\n\nProcess interrupted!\n\n") : T("\n\nSlunkCrypt error: Failed to encrypt data!\n\n"), stderr); goto clean_up; @@ -385,7 +385,7 @@ static int encrypt(const char* const passphrase, const CHR* const input_path, co } SET_LOWBITS(buffer[padding - 1U], padding - 1U); - if ((status = slunkcrypt_process_inplace(ctx, buffer, padding)) != SLUNKCRYPT_SUCCESS) + if ((status = slunkcrypt_inplace(ctx, buffer, padding)) != SLUNKCRYPT_SUCCESS) { FPUTS((status == SLUNKCRYPT_ABORTED) ? T("\n\nProcess interrupted!\n\n") : T("\n\nSlunkCrypt error: Failed to encrypt data!\n\n"), stderr); goto clean_up; @@ -400,7 +400,7 @@ static int encrypt(const char* const passphrase, const CHR* const input_path, co uint8_t checksum_buffer[sizeof(uint64_t)]; store_ui64(checksum_buffer, blake2s_final(&blake2s_state)); - if ((status = slunkcrypt_process_inplace(ctx, checksum_buffer, sizeof(uint64_t))) != SLUNKCRYPT_SUCCESS) + if ((status = slunkcrypt_inplace(ctx, checksum_buffer, sizeof(uint64_t))) != SLUNKCRYPT_SUCCESS) { FPUTS((status == SLUNKCRYPT_ABORTED) ? T("\n\nProcess interrupted!\n\n") : T("\n\nSlunkCrypt error: Failed to encrypt checksum!\n\n"), stderr); goto clean_up; @@ -514,7 +514,7 @@ static int decrypt(const char* const passphrase, const CHR* const input_path, co if (count > 0U) { bytes_read += count; - if ((status = slunkcrypt_process_inplace(ctx, buffer, count)) != SLUNKCRYPT_SUCCESS) + if ((status = slunkcrypt_inplace(ctx, buffer, count)) != SLUNKCRYPT_SUCCESS) { FPUTS((status == SLUNKCRYPT_ABORTED) ? T("\n\nProcess interrupted!\n\n") : T("\n\nSlunkCrypt error: Failed to decrypt data!\n\n"), stderr); goto clean_up; @@ -560,7 +560,7 @@ static int decrypt(const char* const passphrase, const CHR* const input_path, co goto clean_up; } - if ((status = slunkcrypt_process_inplace(ctx, buffer, sizeof(uint64_t))) != SLUNKCRYPT_SUCCESS) + if ((status = slunkcrypt_inplace(ctx, buffer, sizeof(uint64_t))) != SLUNKCRYPT_SUCCESS) { FPUTS((status == SLUNKCRYPT_ABORTED) ? T("\n\nProcess interrupted!\n\n") : T("\n\nSlunkCrypt error: Failed to decrypt data!\n\n"), stderr); goto clean_up; @@ -588,7 +588,7 @@ static int decrypt(const char* const passphrase, const CHR* const input_path, co goto clean_up; } - if ((status = slunkcrypt_process_inplace(ctx, checksum_buffer, sizeof(uint64_t))) != SLUNKCRYPT_SUCCESS) + if ((status = slunkcrypt_inplace(ctx, checksum_buffer, sizeof(uint64_t))) != SLUNKCRYPT_SUCCESS) { FPUTS((status == SLUNKCRYPT_ABORTED) ? T("\n\nProcess interrupted!\n\n") : T("\n\nSlunkCrypt error: Failed to decrypt checksum!\n\n"), stderr); goto clean_up; @@ -677,7 +677,7 @@ static int run_test_case(const char *const message, const uint64_t checksum_mess goto clean_up; } - status = slunkcrypt_process_inplace(ctx, (uint8_t*)text_temp, length); + status = slunkcrypt_inplace(ctx, (uint8_t*)text_temp, length); if (status != SLUNKCRYPT_SUCCESS) { FPUTS((status == SLUNKCRYPT_ABORTED) ? T("\n\nProcess interrupted!\n\n") : T("\n\nWhoops: Failed to encrypt the message!\n\n"), stderr); @@ -697,7 +697,7 @@ static int run_test_case(const char *const message, const uint64_t checksum_mess goto clean_up; } - status = slunkcrypt_process_inplace(ctx, (uint8_t*)text_temp, length); + status = slunkcrypt_inplace(ctx, (uint8_t*)text_temp, length); if (status != SLUNKCRYPT_SUCCESS) { FPUTS((status == SLUNKCRYPT_ABORTED) ? T("\n\nProcess interrupted!\n\n") : T("\n\nWhoops: Failed to decrypt the message!\n\n"), stderr); diff --git a/libslunkcrypt/include/slunkcrypt.h b/libslunkcrypt/include/slunkcrypt.h index 51d5367..1831f91 100644 --- a/libslunkcrypt/include/slunkcrypt.h +++ b/libslunkcrypt/include/slunkcrypt.h @@ -108,7 +108,7 @@ SLUNKCRYPT_API void slunkcrypt_free(const slunkcrypt_t context); * Encryption routines */ SLUNKCRYPT_API int slunkcrypt_process(const slunkcrypt_t context, const uint8_t *const input, uint8_t *const output, size_t length); -SLUNKCRYPT_API int slunkcrypt_process_inplace(const slunkcrypt_t context, uint8_t *const buffer, size_t length); +SLUNKCRYPT_API int slunkcrypt_inplace(const slunkcrypt_t context, uint8_t *const buffer, size_t length); /* * Auxiliary functions diff --git a/libslunkcrypt/include/slunkcrypt.hpp b/libslunkcrypt/include/slunkcrypt.hpp index 78b6424..da115e7 100644 --- a/libslunkcrypt/include/slunkcrypt.hpp +++ b/libslunkcrypt/include/slunkcrypt.hpp @@ -27,24 +27,38 @@ namespace slunkcrypt { /* - * Class for encryption + * Base class for SlunkCrypt */ - class Encryptor + class SlunkBase { public: - Encryptor(const std::string& passwd) + virtual bool process(const uint8_t *const input, uint8_t *const output, size_t length) = 0; + virtual bool process(const std::vector &input, std::vector &output) = 0; + virtual bool inplace(uint8_t *const buffer, size_t length) = 0; + virtual bool inplace(std::vector &buffer) = 0; + protected: + ::slunkcrypt_t m_instance; + }; + + /* + * Class for encryption + */ + class Encryptor : public SlunkBase + { + public: + Encryptor(const std::string &passwd) { - if (slunkcrypt_generate_nonce(&m_nonce) != SLUNKCRYPT_SUCCESS) + if (::slunkcrypt_generate_nonce(&m_nonce) != SLUNKCRYPT_SUCCESS) { throw std::runtime_error("SlunkCryptEncr: Failed to generate the seed value!"); } - if ((m_instance = slunkcrypt_alloc(m_nonce, (const uint8_t*)passwd.c_str(), passwd.length(), SLUNKCRYPT_ENCRYPT)) == SLUNKCRYPT_NULL) + 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!"); } } - Encryptor(Encryptor&& other) noexcept + Encryptor(Encryptor &&other) noexcept { this->m_instance = other.m_instance; this->m_nonce = other.m_nonce; @@ -56,32 +70,32 @@ namespace slunkcrypt { if (m_instance != SLUNKCRYPT_NULL) { - slunkcrypt_free(m_instance); + ::slunkcrypt_free(m_instance); } } - bool process(const uint8_t* const input, uint8_t* const output, size_t length) + bool process(const uint8_t *const input, uint8_t *const output, size_t length) { - return (slunkcrypt_process(m_instance, input, output, length) == SLUNKCRYPT_SUCCESS); + return (::slunkcrypt_process(m_instance, input, output, length) == SLUNKCRYPT_SUCCESS); } - bool process(const std::vector& input, std::vector& output) + bool process(const std::vector &input, std::vector &output) { if (output.size() >= input.size()) { - return (slunkcrypt_process(m_instance, input.data(), output.data(), input.size()) == SLUNKCRYPT_SUCCESS); + return (::slunkcrypt_process(m_instance, input.data(), output.data(), input.size()) == SLUNKCRYPT_SUCCESS); } return false; } - bool process_inplace(uint8_t* const buffer, size_t length) + bool inplace(uint8_t *const buffer, size_t length) { - return (slunkcrypt_process_inplace(m_instance, buffer, length) == SLUNKCRYPT_SUCCESS); + return (::slunkcrypt_inplace(m_instance, buffer, length) == SLUNKCRYPT_SUCCESS); } - bool process_inplace(std::vector& buffer) + bool inplace(std::vector &buffer) { - return (slunkcrypt_process_inplace(m_instance, buffer.data(), buffer.size()) == SLUNKCRYPT_SUCCESS); + return (::slunkcrypt_inplace(m_instance, buffer.data(), buffer.size()) == SLUNKCRYPT_SUCCESS); } uint64_t get_nonce(void) const @@ -93,24 +107,23 @@ namespace slunkcrypt Encryptor(const Encryptor&) = delete; Encryptor& operator=(const Encryptor&) = delete; uint64_t m_nonce; - slunkcrypt_t m_instance; }; /* * Class for decryption */ - class Decryptor + class Decryptor : public SlunkBase { public: - Decryptor(const uint64_t nonce, const std::string& passwd) + Decryptor(const uint64_t nonce, const std::string &passwd) { - if ((m_instance = slunkcrypt_alloc(nonce, (const uint8_t*)passwd.c_str(), passwd.length(), SLUNKCRYPT_DECRYPT)) == SLUNKCRYPT_NULL) + 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!"); } } - Decryptor(Decryptor&& other) noexcept + Decryptor(Decryptor &&other) noexcept { this->m_instance = other.m_instance; other.m_instance = SLUNKCRYPT_NULL; @@ -120,38 +133,37 @@ namespace slunkcrypt { if (m_instance != SLUNKCRYPT_NULL) { - slunkcrypt_free(m_instance); + ::slunkcrypt_free(m_instance); } } - bool process(const uint8_t* const input, uint8_t* const output, size_t length) + bool process(const uint8_t *const input, uint8_t *const output, size_t length) { - return (slunkcrypt_process(m_instance, input, output, length) == SLUNKCRYPT_SUCCESS); + return (::slunkcrypt_process(m_instance, input, output, length) == SLUNKCRYPT_SUCCESS); } - bool process(const std::vector& input, std::vector& output) + bool process(const std::vector &input, std::vector &output) { if (output.size() >= input.size()) { - return (slunkcrypt_process(m_instance, input.data(), output.data(), input.size()) == SLUNKCRYPT_SUCCESS); + return (::slunkcrypt_process(m_instance, input.data(), output.data(), input.size()) == SLUNKCRYPT_SUCCESS); } return false; } - bool process_inplace(uint8_t* const buffer, size_t length) + bool inplace(uint8_t *const buffer, size_t length) { - return (slunkcrypt_process_inplace(m_instance, buffer, length) == SLUNKCRYPT_SUCCESS); + return (::slunkcrypt_inplace(m_instance, buffer, length) == SLUNKCRYPT_SUCCESS); } - bool process_inplace(std::vector& buffer) + bool inplace(std::vector &buffer) { - return (slunkcrypt_process_inplace(m_instance, buffer.data(), buffer.size()) == SLUNKCRYPT_SUCCESS); + return (::slunkcrypt_inplace(m_instance, buffer.data(), buffer.size()) == SLUNKCRYPT_SUCCESS); } private: Decryptor(const Decryptor&) = delete; Decryptor& operator=(const Decryptor&) = delete; - slunkcrypt_t m_instance; }; } #endif diff --git a/libslunkcrypt/src/slunkcrypt.c b/libslunkcrypt/src/slunkcrypt.c index df5fd0b..d51eb58 100644 --- a/libslunkcrypt/src/slunkcrypt.c +++ b/libslunkcrypt/src/slunkcrypt.c @@ -34,12 +34,8 @@ const char *const SLUNKCRYPT_BUILD = __DATE__ " " __TIME__; #define HASH_MAGIC_PRIME 0x00000100000001B3ull #define HASH_OFFSET_BASE 0xCBF29CE484222325ull -/* Types */ -typedef _Bool boolean; - /* Utilities */ -#define INT_TO_BOOL(X) ((boolean)(!(!(X)))) -#define LOGICAL_XOR(X,Y) ((Y) ? (!(X)) : (X)) +#define BOOLIFY(X) (!!(X)) // ========================================================================== // Data structures @@ -59,7 +55,7 @@ rand_state_t; typedef struct { - boolean reverse_mode; + int reverse_mode; uint8_t wheel[256U][256U]; rand_state_t random; } @@ -230,7 +226,7 @@ static int initialize_state(crypt_state_t *const state, const uint64_t nonce, co /* initialize state */ slunkcrypt_bzero(state, sizeof(crypt_state_t)); - const boolean reverse = state->reverse_mode = INT_TO_BOOL(mode); + const int reverse = state->reverse_mode = BOOLIFY(mode); /* set up the wheel permutations */ for (r = 0U; r < 256U; ++r) @@ -279,7 +275,7 @@ aborted: // Encrypt / Decrypt // ========================================================================== -static FORCE_INLINE void calculate_offsets(uint8_t *const offset, rand_state_t *const state, const boolean reverse) +static FORCE_INLINE void calculate_offsets(uint8_t *const offset, rand_state_t *const state, const int reverse) { uint32_t temp = 0U; size_t i; @@ -388,7 +384,7 @@ aborted: return SLUNKCRYPT_ABORTED; } -int slunkcrypt_process_inplace(const slunkcrypt_t context, uint8_t *const buffer, size_t length) +int slunkcrypt_inplace(const slunkcrypt_t context, uint8_t *const buffer, size_t length) { crypt_state_t *const state = (crypt_state_t*)context; if (!state)