Clean up function names.

This commit is contained in:
LoRd_MuldeR 2021-04-02 16:37:19 +02:00
parent e6511aec5e
commit 06f57ca483
Signed by: mulder
GPG Key ID: 2B5913365F57E03F
6 changed files with 58 additions and 50 deletions

View File

@ -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;

View File

@ -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;

View File

@ -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);

View File

@ -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

View File

@ -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<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
*/
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<uint8_t>& input, std::vector<uint8_t>& output)
bool process(const std::vector<uint8_t> &input, std::vector<uint8_t> &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<uint8_t>& buffer)
bool inplace(std::vector<uint8_t> &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<uint8_t>& input, std::vector<uint8_t>& output)
bool process(const std::vector<uint8_t> &input, std::vector<uint8_t> &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<uint8_t>& buffer)
bool inplace(std::vector<uint8_t> &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

View File

@ -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)