Some code refactoring.

This commit is contained in:
LoRd_MuldeR 2020-10-20 15:21:00 +02:00
parent f671aef3d7
commit 20d7632fcb
Signed by: mulder
GPG Key ID: 2B5913365F57E03F
6 changed files with 223 additions and 110 deletions

View File

@ -85,5 +85,5 @@ $(SUBDIR_LIB)/obj/%$(CONFIG).o: $(SUBDIR_LIB)/src/%.c
$(CC) $(CFLAGS) -c $< -o $@ $(CC) $(CFLAGS) -c $< -o $@
clean: clean:
$(RM) $(addsuffix *crypt*,$(dir $(TARGET_APP)) $(dir $(TARGET_LIB))) $(RM) $(SUBDIR_APP)/obj/*.o $(SUBDIR_APP)/lib/*.a $(SUBDIR_APP)/bin/*$(SUFFIX)
$(RM) $(addsuffix *.o,$(dir $(OBJECTS_APP)) $(dir $(OBJECTS_LIB))) $(RM) $(SUBDIR_LIB)/obj/*.o $(SUBDIR_LIB)/lib/*.a $(SUBDIR_LIB)/bin/*$(SUFFIX)

View File

@ -8,6 +8,10 @@
#include "../../libslunkcrypt/src/version.h" #include "../../libslunkcrypt/src/version.h"
#define VERSION_HELPER1(X,Y,Z) #X "." #Y "." #Z
#define VERSION_HELPER2(X,Y,Z) VERSION_HELPER1(X,Y,Z)
#define VERSION_STRING VERSION_HELPER2(MY_VERSION_MAJOR,MY_VERSION_MINOR,MY_VERSION_PATCH)
///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////
// //
// Neutral resources // Neutral resources
@ -22,8 +26,8 @@ LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL
// Version // Version
// //
VS_VERSION_INFO VERSIONINFO VS_VERSION_INFO VERSIONINFO
FILEVERSION LIB_VERSION_MAJOR, LIB_VERSION_MINOR, LIB_VERSION_PATCH, 0 FILEVERSION MY_VERSION_MAJOR,MY_VERSION_MINOR,0,MY_VERSION_PATCH
PRODUCTVERSION LIB_VERSION_MAJOR, LIB_VERSION_MINOR, LIB_VERSION_PATCH, 0 PRODUCTVERSION MY_VERSION_MAJOR,MY_VERSION_MINOR,0,MY_VERSION_PATCH
FILEFLAGSMASK 0x17L FILEFLAGSMASK 0x17L
#ifdef _DEBUG #ifdef _DEBUG
FILEFLAGS 0x3L FILEFLAGS 0x3L
@ -40,8 +44,8 @@ BEGIN
BEGIN BEGIN
VALUE "ProductName", "SlunkCrypt" VALUE "ProductName", "SlunkCrypt"
VALUE "FileDescription", "SlunkCrypt" VALUE "FileDescription", "SlunkCrypt"
VALUE "ProductVersion", LIB_VERSION_STRING VALUE "ProductVersion", VERSION_STRING
VALUE "FileVersion", LIB_VERSION_STRING VALUE "FileVersion", VERSION_STRING
VALUE "InternalName", "slunkcrypt" VALUE "InternalName", "slunkcrypt"
VALUE "OriginalFilename", "slunkcrypt.exe" VALUE "OriginalFilename", "slunkcrypt.exe"
VALUE "LegalCopyright", "Created by LoRd_MuldeR <MuldeR2@GMX.de>" VALUE "LegalCopyright", "Created by LoRd_MuldeR <MuldeR2@GMX.de>"

View File

@ -15,14 +15,73 @@
#include <ctype.h> #include <ctype.h>
#include <signal.h> #include <signal.h>
#define BUFF_SIZE 4096U #define BUFFER_SIZE 4096U
#define OP_MODE_NONE 0
#define OP_MODE_HELP 1
#define OP_MODE_VERS 2
#define OP_MODE_ENCR 3
#define OP_MODE_DECR 4
#define OP_MODE_TEST 5
static const CHR *const ENVV_PASSWD_NAME = T("SLUNK_PASSPHRASE"); static const CHR *const ENVV_PASSWD_NAME = T("SLUNK_PASSPHRASE");
// ==========================================================================
// Auxiliary functions
// ==========================================================================
static int parse_mode(const CHR* const command)
{
if ((!STRICMP(command, T("-h"))) || (!STRICMP(command, T("/?"))) || (!STRICMP(command, T("--help"))))
{
return OP_MODE_HELP;
}
else if ((!STRICMP(command, T("-v"))) || (!STRICMP(command, T("--version"))))
{
return OP_MODE_VERS;
}
else if ((!STRICMP(command, T("-e"))) || (!STRICMP(command, T("--encrypt"))))
{
return OP_MODE_ENCR;
}
else if ((!STRICMP(command, T("-d"))) || (!STRICMP(command, T("--decrypt"))))
{
return OP_MODE_DECR;
}
else if ((!STRICMP(command, T("-t"))) || (!STRICMP(command, T("--self-test"))))
{
return OP_MODE_TEST;
}
else
{
FPRINTF(stderr, T("Error: The specified command \"%") T(PRISTR) T("\" is unknown!\n\n"), command);
return -1;
}
}
static void print_manpage(const CHR *const program)
{
FPUTS(T("====================================================================\n"), stderr);
FPUTS(T("This software has been released under the CC0 1.0 Universal license:\n"), stderr);
FPUTS(T("https://creativecommons.org/publicdomain/zero/1.0/legalcode\n"), stderr);
FPUTS(T("====================================================================\n\n"), stderr);
FPUTS(T("Synopsis:\n"), stderr);
FPRINTF(stderr, T(" %") T(PRISTR) T(" --encrypt [[@][:]<passphrase>] <input.txt> <output.enc>\n"), program);
FPRINTF(stderr, T(" %") T(PRISTR) T(" --decrypt [[@][:]<passphrase>] <input.enc> <output.txt>\n\n"), program);
FPUTS(T("Options:\n"), stderr);
FPUTS(T("- If <passphrase> is prefixed with a '@' character, then it specifies the file\n"), stderr);
FPUTS(T(" to read the passphrase from; only the first line in that file is used!\n"), stderr);
FPUTS(T("- If <passphrase> is prefixed with a ':' character, then the leading character\n"), stderr);
FPUTS(T(" is skipped and the remainder of the argument is used as passphrase.\n"), stderr);
FPUTS(T("- If argument <passphrase> is omitted, then the passphrase is read from the\n"), stderr);
FPRINTF(stderr, T(" environment variable \"%") T(PRISTR) T("\" instead.\n"), ENVV_PASSWD_NAME);
FPUTS(T("- Specify \"@-\" in order to read the passphrase from the standard input stream!\n\n"), stderr);
}
static char* read_passphrase(const CHR* const file_name) static char* read_passphrase(const CHR* const file_name)
{ {
static const size_t buff_size = 512U; const size_t max_len = SLUNKCRYPT_PWDLEN_MAX + 3U;
char *buffer = (char*) malloc(buff_size * sizeof(char)); char *buffer = (char*) malloc(max_len * sizeof(char));
if (!buffer) if (!buffer)
{ {
return NULL; return NULL;
@ -38,7 +97,7 @@ static char* read_passphrase(const CHR* const file_name)
do do
{ {
if (!fgets(buffer, (int)buff_size, file)) if (!fgets(buffer, (int)max_len, file))
{ {
free(buffer); free(buffer);
buffer = NULL; buffer = NULL;
@ -101,6 +160,19 @@ static int open_files(FILE **const file_in, FILE **const file_out, const CHR* co
return 0; return 0;
} }
static void sigint_handler(const int sig)
{
if (sig == SIGINT)
{
g_slunkcrypt_abort_flag = 1;
signal(SIGINT, sigint_handler);
}
}
// ==========================================================================
// Encrypt
// ==========================================================================
static int encrypt(const char* const passphrase, const CHR* const input_path, const CHR* const output_path) static int encrypt(const char* const passphrase, const CHR* const input_path, const CHR* const output_path)
{ {
slunkcrypt_t ctx = SLUNKCRYPT_NULL; slunkcrypt_t ctx = SLUNKCRYPT_NULL;
@ -149,7 +221,7 @@ static int encrypt(const char* const passphrase, const CHR* const input_path, co
clock_t clk_now, clk_update = clock(); clock_t clk_now, clk_update = clock();
uint64_t crc_actual = CRC_INITIALIZER, bytes_read = 0U; uint64_t crc_actual = CRC_INITIALIZER, bytes_read = 0U;
uint8_t buffer[BUFF_SIZE]; uint8_t buffer[BUFFER_SIZE];
FPRINTF(stderr, T("%5.1f%% "), 0.0); FPRINTF(stderr, T("%5.1f%% "), 0.0);
fflush(stderr); fflush(stderr);
@ -157,7 +229,7 @@ static int encrypt(const char* const passphrase, const CHR* const input_path, co
while (bytes_read < file_size) while (bytes_read < file_size)
{ {
const uint64_t bytes_remaining = file_size - bytes_read; const uint64_t bytes_remaining = file_size - bytes_read;
const size_t request_len = (bytes_remaining < BUFF_SIZE) ? ((size_t)bytes_remaining) : BUFF_SIZE; const size_t request_len = (bytes_remaining < BUFFER_SIZE) ? ((size_t)bytes_remaining) : BUFFER_SIZE;
const size_t count = fread(buffer, sizeof(uint8_t), request_len, file_in); const size_t count = fread(buffer, sizeof(uint8_t), request_len, file_in);
if (count > 0U) if (count > 0U)
{ {
@ -200,7 +272,7 @@ static int encrypt(const char* const passphrase, const CHR* const input_path, co
} }
crc_actual = crc64_finish(crc_actual); crc_actual = crc64_finish(crc_actual);
FPRINTF(stderr, T("\b\b\b\b\b\b%5.1f%%\n\n"), 100.0); FPRINTF(stderr, T("\b\b\b\b\b\b\b%5.1f%%\n\n"), 100.0);
fflush(stderr); fflush(stderr);
if (fwrite(&crc_actual, sizeof(uint64_t), 1U, file_out) < 1U) if (fwrite(&crc_actual, sizeof(uint64_t), 1U, file_out) < 1U)
@ -234,6 +306,10 @@ clean_up:
return result; return result;
} }
// ==========================================================================
// Decrypt
// ==========================================================================
static int decrypt(const char* const passphrase, const CHR* const input_path, const CHR* const output_path) static int decrypt(const char* const passphrase, const CHR* const input_path, const CHR* const output_path)
{ {
slunkcrypt_t ctx = SLUNKCRYPT_NULL; slunkcrypt_t ctx = SLUNKCRYPT_NULL;
@ -276,7 +352,7 @@ static int decrypt(const char* const passphrase, const CHR* const input_path, co
clock_t clk_now, clk_update = clock(); clock_t clk_now, clk_update = clock();
uint64_t crc_actual = CRC_INITIALIZER, bytes_read = sizeof(uint64_t); uint64_t crc_actual = CRC_INITIALIZER, bytes_read = sizeof(uint64_t);
uint8_t buffer[BUFF_SIZE]; uint8_t buffer[BUFFER_SIZE];
const uint64_t read_limit = file_size - sizeof(uint64_t); const uint64_t read_limit = file_size - sizeof(uint64_t);
FPRINTF(stderr, T("%5.1f%% "), 0.0); FPRINTF(stderr, T("%5.1f%% "), 0.0);
@ -285,7 +361,7 @@ static int decrypt(const char* const passphrase, const CHR* const input_path, co
while (bytes_read < read_limit) while (bytes_read < read_limit)
{ {
const uint64_t bytes_remaining = read_limit - bytes_read; const uint64_t bytes_remaining = read_limit - bytes_read;
const size_t request_len = (bytes_remaining < BUFF_SIZE) ? ((size_t)bytes_remaining) : BUFF_SIZE; const size_t request_len = (bytes_remaining < BUFFER_SIZE) ? ((size_t)bytes_remaining) : BUFFER_SIZE;
const size_t count = fread(buffer, sizeof(uint8_t), request_len, file_in); const size_t count = fread(buffer, sizeof(uint8_t), request_len, file_in);
if (count > 0U) if (count > 0U)
{ {
@ -328,7 +404,7 @@ static int decrypt(const char* const passphrase, const CHR* const input_path, co
} }
crc_actual = crc64_finish(crc_actual); crc_actual = crc64_finish(crc_actual);
FPRINTF(stderr, T("\b\b\b\b\b\b%5.1f%%\n\n"), 100.0); FPRINTF(stderr, T("\b\b\b\b\b\b\b%5.1f%%\n\n"), 100.0);
fflush(stderr); fflush(stderr);
uint64_t crc_expected; uint64_t crc_expected;
@ -370,7 +446,11 @@ clean_up:
return result; return result;
} }
static int run_test(const char *const message) // ==========================================================================
// Self-test
// ==========================================================================
static int run_test_case(const char *const message)
{ {
static const char* const passphrase = "OrpheanBeh0lderScry!Doubt"; static const char* const passphrase = "OrpheanBeh0lderScry!Doubt";
@ -450,7 +530,7 @@ clean_up:
return result; return result;
} }
static int self_test(void) static int run_self_test(void)
{ {
static const size_t total = 32U; static const size_t total = 32U;
const char* const test_data[] = { TEST_DATA_0, TEST_DATA_1, TEST_DATA_2, TEST_DATA_3, NULL }; const char* const test_data[] = { TEST_DATA_0, TEST_DATA_1, TEST_DATA_2, TEST_DATA_3, NULL };
@ -465,7 +545,7 @@ static int self_test(void)
{ {
FPRINTF(stderr, T("\b\b\b\b\b\b%2u/%2u "), (unsigned int)++completed, (unsigned int)total); FPRINTF(stderr, T("\b\b\b\b\b\b%2u/%2u "), (unsigned int)++completed, (unsigned int)total);
fflush(stderr); fflush(stderr);
if (run_test(test_data[j])) if (run_test_case(test_data[j]))
{ {
return 1; return 1;
} }
@ -478,72 +558,53 @@ static int self_test(void)
return 0; return 0;
} }
static void sigint_handler(const int sig) // ==========================================================================
{ // Main function
if (sig == SIGINT) // ==========================================================================
{
g_slunkcrypt_abort_flag = 1;
signal(SIGINT, sigint_handler);
}
}
int MAIN(int argc, CHR* argv[]) int MAIN(const int argc, CHR *const argv[])
{ {
init_terminal(); init_terminal();
signal(SIGINT, sigint_handler); signal(SIGINT, sigint_handler);
int result = -1;
FPRINTF(stderr, T("SlunkCrypt Utility (%") T(PRIstr) T("-%") T(PRIstr) T("), by LoRd_MuldeR <MuldeR2@GMX.de>\n"), OS_TYPE, CPU_ARCH); FPRINTF(stderr, T("SlunkCrypt Utility (%") T(PRIstr) T("-%") T(PRIstr) T("), by LoRd_MuldeR <MuldeR2@GMX.de>\n"), OS_TYPE, CPU_ARCH);
FPRINTF(stderr, T("Using libSlunkCrypt v%") T(PRIstr) T(" [%") T(PRIstr) T("]\n\n"), SLUNKCRYPT_VERSION, SLUNKCRYPT_BUILD); FPRINTF(stderr, T("Using libSlunkCrypt v%u.%u.%u [%") T(PRIstr) T("]\n\n"), SLUNKCRYPT_VERSION_MAJOR, SLUNKCRYPT_VERSION_MINOR, SLUNKCRYPT_VERSION_PATCH, SLUNKCRYPT_BUILD);
if (argc > 1) /* ----------------------------------------------------- */
/* Parse arguments */
/* ----------------------------------------------------- */
const int mode = (argc > 1) ? parse_mode(argv[1U]) : OP_MODE_NONE;
switch (mode)
{ {
if ((!STRICMP(argv[1U], T("-v"))) || (!STRICMP(argv[1U], T("--version")))) case OP_MODE_NONE:
{ FPRINTF(stderr, T("Error: Nothing to do. Please type '%") T(PRISTR) T(" --help' for details!\n\n"), get_file_name(argv[0U]));
return 0; /*exit right now*/ return 1;
} case OP_MODE_HELP:
if ((!STRICMP(argv[1U], T("-h"))) || (!STRICMP(argv[1U], T("/?"))) || (!STRICMP(argv[1U], T("--help")))) print_manpage(get_file_name(argv[0U]));
{ return 0;
const CHR* const program = get_file_name(argv[0U]); case OP_MODE_ENCR:
FPUTS(T("====================================================================\n"), stderr); case OP_MODE_DECR:
FPUTS(T("This software has been released under the CC0 1.0 Universal license:\n"), stderr); break;
FPUTS(T("https://creativecommons.org/publicdomain/zero/1.0/legalcode\n"), stderr); case OP_MODE_TEST:
FPUTS(T("====================================================================\n\n"), stderr); return run_self_test();
FPUTS(T("Synopsis:\n"), stderr); default:
FPRINTF(stderr, T(" %") T(PRISTR) T(" --encrypt [[@][:]<passphrase>] <input.txt> <output.enc>\n"), program); return -1;
FPRINTF(stderr, T(" %") T(PRISTR) T(" --decrypt [[@][:]<passphrase>] <input.enc> <output.txt>\n\n"), program);
FPUTS(T("Options:\n"), stderr);
FPUTS(T("- If <passphrase> is prefixed with a '@' character, then it specifies the file\n"), stderr);
FPUTS(T(" to read the passphrase from; only the first line in that file is used!\n"), stderr);
FPUTS(T("- If <passphrase> is prefixed with a ':' character, then the leading character\n"), stderr);
FPUTS(T(" is skipped and the remainder of the argument is used as passphrase.\n"), stderr);
FPUTS(T("- If argument <passphrase> is omitted, then the passphrase is read from the\n"), stderr);
FPRINTF(stderr, T(" environment variable \"%") T(PRISTR) T("\" instead.\n"), ENVV_PASSWD_NAME);
FPUTS(T("- Specify \"@-\" in order to read the passphrase from the standard input stream!\n\n"), stderr);
return 0;
}
if ((!STRICMP(argv[1U], T("-t"))) || (!STRICMP(argv[1U], T("--self-test"))))
{
return self_test(); /*only self-test!*/
}
} }
if (argc < 4) if (argc < 4)
{ {
const CHR* const program = get_file_name(argv[0U]); FPRINTF(stderr, T("Error: Required argument is missing. Please type '%") T(PRISTR) T(" --help' for details!\n\n"), get_file_name(argv[0U]));
FPRINTF(stderr, T("Error: Nothing to do. Please type '%") T(PRISTR) T(" --help' for details!\n\n"), program);
return 1; return 1;
} }
else if (argc > 5)
{
FPUTS(T("Warning: Excess command-line argument is ignored!\n\n"), stderr);
}
const CHR *const command = argv[1U], *const passphrase = (argc > 4) ? argv[2U] : GETENV(ENVV_PASSWD_NAME); const CHR* const passphrase = (argc > 4) ? argv[2U] : GETENV(ENVV_PASSWD_NAME);
const CHR *const input_file = argv[(argc > 4) ? 3U : 2U], *const output_file = argv[(argc > 4) ? 4U : 3U]; const CHR* const input_file = argv[(argc > 4) ? 3U : 2U], * const output_file = argv[(argc > 4) ? 4U : 3U];
if ((!passphrase) || (!passphrase[0U]) || (((passphrase[0U] == T('@')) || (passphrase[0U] == T(':'))) && (!passphrase[1U]))) if ((!passphrase) || (!passphrase[0U]) || (((passphrase[0U] == T('@')) || (passphrase[0U] == T(':'))) && (!passphrase[1U])))
{ {
FPUTS(T("Error: The passphrase (file name) must not be empty!\n\n"), stderr); FPUTS(T("Error: The specified passphrase must not be empty!\n\n"), stderr);
return 1; return 1;
} }
@ -553,16 +614,32 @@ int MAIN(int argc, CHR* argv[])
return 1; return 1;
} }
/* ----------------------------------------------------- */
/* Initialize passphrase */
/* ----------------------------------------------------- */
char *const passphrase_buffer = (passphrase[0U] == T('@')) ? read_passphrase(passphrase + 1U) : CHR_to_utf8((passphrase[0U] == T(':')) ? (passphrase + 1U) : passphrase); char *const passphrase_buffer = (passphrase[0U] == T('@')) ? read_passphrase(passphrase + 1U) : CHR_to_utf8((passphrase[0U] == T(':')) ? (passphrase + 1U) : passphrase);
if (!passphrase_buffer) if (!passphrase_buffer)
{ {
FPUTS(T("Error: Failed to read the passphrase file!\n\n"), stderr); FPUTS(T("Error: Failed to read the passphrase!\n\n"), stderr);
return 1; return 1;
} }
slunkcrypt_bzero((CHR*)passphrase, STRLEN(passphrase) * sizeof(CHR)); slunkcrypt_bzero((CHR*)passphrase, STRLEN(passphrase) * sizeof(CHR));
if (strlen(passphrase_buffer) < 12U) const size_t passphrase_len = strlen(passphrase_buffer);
if (passphrase_len < SLUNKCRYPT_PWDLEN_MIN)
{
FPRINTF(stderr, T("Error: Passphrase must be at least %") T(PRIu64) T(" characters in length!\n\n"), (uint64_t)SLUNKCRYPT_PWDLEN_MIN);
goto exiting;
}
else if (passphrase_len > SLUNKCRYPT_PWDLEN_MAX)
{
FPRINTF(stderr, T("Error: Passphrase must be at most %") T(PRIu64) T(" characters in length!\n\n"), (uint64_t)SLUNKCRYPT_PWDLEN_MAX);
goto exiting;
}
if (passphrase_len < 12U)
{ {
FPUTS(T("Warning: Using a *short* passphrase; a length of 12 characters or more is recommended!\n\n"), stderr); FPUTS(T("Warning: Using a *short* passphrase; a length of 12 characters or more is recommended!\n\n"), stderr);
} }
@ -571,21 +648,22 @@ int MAIN(int argc, CHR* argv[])
FPUTS(T("Warning: Using a *weak* passphrase; a mix of upper-case letters, lower-case letters, digits and 'special' characters is recommended!\n\n"), stderr); FPUTS(T("Warning: Using a *weak* passphrase; a mix of upper-case letters, lower-case letters, digits and 'special' characters is recommended!\n\n"), stderr);
} }
const clock_t clk_start = clock(); /* ----------------------------------------------------- */
int result = -1; /* Encrypt or decrypt */
/* ----------------------------------------------------- */
if ((!STRICMP(command, T("-e"))) || (!STRICMP(command, T("--encrypt")))) const clock_t clk_start = clock();
switch (mode)
{ {
case OP_MODE_ENCR:
result = encrypt(passphrase_buffer, input_file, output_file); result = encrypt(passphrase_buffer, input_file, output_file);
} break;
else if ((!STRICMP(command, T("-d"))) || (!STRICMP(command, T("--decrypt")))) case OP_MODE_DECR:
{
result = decrypt(passphrase_buffer, input_file, output_file); result = decrypt(passphrase_buffer, input_file, output_file);
} break;
else default:
{ abort(); /*not supposed to happen!*/
FPRINTF(stderr, T("Error: Command \"%") T(PRISTR) T("\" is unknown!\n\n"), command);
goto exiting;
} }
if (!g_slunkcrypt_abort_flag) if (!g_slunkcrypt_abort_flag)
@ -596,6 +674,10 @@ int MAIN(int argc, CHR* argv[])
FPRINTF(stderr, T("Operation completed after %.1f seconds.\n\n"), (clk_end - clk_start) / ((double)CLOCKS_PER_SEC)); FPRINTF(stderr, T("Operation completed after %.1f seconds.\n\n"), (clk_end - clk_start) / ((double)CLOCKS_PER_SEC));
} }
/* ----------------------------------------------------- */
/* Final clean-up */
/* ----------------------------------------------------- */
exiting: exiting:
if (passphrase_buffer) if (passphrase_buffer)

View File

@ -12,8 +12,14 @@
/* /*
* Version info * Version info
*/ */
extern const char *const SLUNKCRYPT_VERSION; extern const uint16_t SLUNKCRYPT_VERSION_MAJOR;
extern const char* const SLUNKCRYPT_BUILD; extern const uint16_t SLUNKCRYPT_VERSION_MINOR;
extern const uint16_t SLUNKCRYPT_VERSION_PATCH;
/*
* Build date and time
*/
extern const char *const SLUNKCRYPT_BUILD;
/* /*
* Abort flag * Abort flag
@ -24,14 +30,20 @@ extern volatile int g_slunkcrypt_abort_flag;
* Opaque handle to internal state * Opaque handle to internal state
*/ */
typedef uintptr_t slunkcrypt_t; typedef uintptr_t slunkcrypt_t;
#define SLUNKCRYPT_NULL ((slunkcrypt_t)NULL)
/* /*
* Constants * Error codes
*/ */
#define SLUNKCRYPT_NULL ((slunkcrypt_t)NULL) static const int SLUNKCRYPT_SUCCESS = 0;
#define SLUNKCRYPT_SUCCESS 0 static const int SLUNKCRYPT_FAILURE = -1;
#define SLUNKCRYPT_FAILURE (-1) static const int SLUNKCRYPT_ABORTED = -2;
#define SLUNKCRYPT_ABORTED (-2)
/*
* Limits
*/
static const size_t SLUNKCRYPT_PWDLEN_MIN = 5U;
static const size_t SLUNKCRYPT_PWDLEN_MAX = 512U;
/* /*
* Seed generator * Seed generator

View File

@ -24,8 +24,15 @@
#endif #endif
#endif #endif
const char* const SLUNKCRYPT_VERSION = LIB_VERSION_STRING; /* Version info */
const char* const SLUNKCRYPT_BUILD = __DATE__", "__TIME__; const uint16_t SLUNKCRYPT_VERSION_MAJOR = MY_VERSION_MAJOR;
const uint16_t SLUNKCRYPT_VERSION_MINOR = MY_VERSION_MINOR;
const uint16_t SLUNKCRYPT_VERSION_PATCH = MY_VERSION_PATCH;
const char* const SLUNKCRYPT_BUILD = __DATE__ " " __TIME__;
// ==========================================================================
// Data structures
// ==========================================================================
typedef struct typedef struct
{ {
@ -259,7 +266,7 @@ int slunkcrypt_generate_seed(uint64_t* const seed)
slunkcrypt_t slunkcrypt_alloc(const uint64_t salt, const uint8_t *const passwd, const size_t passwd_len) slunkcrypt_t slunkcrypt_alloc(const uint64_t salt, const uint8_t *const passwd, const size_t passwd_len)
{ {
if ((!passwd) || (passwd_len < 1U)) if ((!passwd) || (passwd_len < SLUNKCRYPT_PWDLEN_MIN) || (passwd_len > SLUNKCRYPT_PWDLEN_MAX))
{ {
return SLUNKCRYPT_NULL; return SLUNKCRYPT_NULL;
} }
@ -282,7 +289,7 @@ slunkcrypt_t slunkcrypt_alloc(const uint64_t salt, const uint8_t *const passwd,
int slunkcrypt_reset(const slunkcrypt_t context, const uint64_t salt, const uint8_t *const passwd, const size_t passwd_len) int slunkcrypt_reset(const slunkcrypt_t context, const uint64_t salt, const uint8_t *const passwd, const size_t passwd_len)
{ {
crypt_state_t* const state = (crypt_state_t*)context; crypt_state_t* const state = (crypt_state_t*)context;
if ((!state) || (!passwd) || (passwd_len < 1U)) if ((!state) || (!passwd) || (passwd_len < SLUNKCRYPT_PWDLEN_MIN) || (passwd_len > SLUNKCRYPT_PWDLEN_MAX))
{ {
return SLUNKCRYPT_FAILURE; return SLUNKCRYPT_FAILURE;
} }
@ -301,10 +308,13 @@ int slunkcrypt_encrypt(const slunkcrypt_t context, const uint8_t* const input, u
{ {
return SLUNKCRYPT_FAILURE; return SLUNKCRYPT_FAILURE;
} }
for (size_t i = 0; i < length; ++i) if (length > 0U)
{ {
output[i] = process_enc(state, input[i]); for (size_t i = 0; i < length; ++i)
CHECK_ABORTED(); {
output[i] = process_enc(state, input[i]);
CHECK_ABORTED();
}
} }
return SLUNKCRYPT_SUCCESS; return SLUNKCRYPT_SUCCESS;
} }
@ -316,10 +326,13 @@ int slunkcrypt_encrypt_inplace(const slunkcrypt_t context, uint8_t* const buffer
{ {
return SLUNKCRYPT_FAILURE; return SLUNKCRYPT_FAILURE;
} }
for (size_t i = 0; i < length; ++i) if (length > 0U)
{ {
buffer[i] = process_enc(state, buffer[i]); for (size_t i = 0; i < length; ++i)
CHECK_ABORTED(); {
buffer[i] = process_enc(state, buffer[i]);
CHECK_ABORTED();
}
} }
return SLUNKCRYPT_SUCCESS; return SLUNKCRYPT_SUCCESS;
} }
@ -332,10 +345,13 @@ int slunkcrypt_decrypt(const slunkcrypt_t context, const uint8_t* const input, u
{ {
return SLUNKCRYPT_FAILURE; return SLUNKCRYPT_FAILURE;
} }
for (size_t i = 0; i < length; ++i) if (length > 0U)
{ {
output[i] = process_dec(state, input[i]); for (size_t i = 0; i < length; ++i)
CHECK_ABORTED(); {
output[i] = process_dec(state, input[i]);
CHECK_ABORTED();
}
} }
return SLUNKCRYPT_SUCCESS; return SLUNKCRYPT_SUCCESS;
} }
@ -347,10 +363,13 @@ int slunkcrypt_decrypt_inplace(const slunkcrypt_t context, uint8_t* const buffer
{ {
return SLUNKCRYPT_FAILURE; return SLUNKCRYPT_FAILURE;
} }
for (size_t i = 0; i < length; ++i) if (length > 0U)
{ {
buffer[i] = process_dec(state, buffer[i]); for (size_t i = 0; i < length; ++i)
CHECK_ABORTED(); {
buffer[i] = process_dec(state, buffer[i]);
CHECK_ABORTED();
}
} }
return SLUNKCRYPT_SUCCESS; return SLUNKCRYPT_SUCCESS;
} }

View File

@ -6,12 +6,8 @@
#ifndef INC_SLUNKCRYPT_VERSION_H #ifndef INC_SLUNKCRYPT_VERSION_H
#define INC_SLUNKCRYPT_VERSION_H #define INC_SLUNKCRYPT_VERSION_H
#define LIB_VERSION_MAJOR 1 #define MY_VERSION_MAJOR 1
#define LIB_VERSION_MINOR 0 #define MY_VERSION_MINOR 0
#define LIB_VERSION_PATCH 0 #define MY_VERSION_PATCH 0
#define VERSION_HELPER1(X,Y,Z) #X "." #Y "." #Z
#define VERSION_HELPER2(X,Y,Z) VERSION_HELPER1(X,Y,Z)
#define LIB_VERSION_STRING VERSION_HELPER2(LIB_VERSION_MAJOR,LIB_VERSION_MINOR,LIB_VERSION_PATCH)
#endif #endif