Replaced some more byte-order-specific code with a byte-order-agnostic implementation.

This commit is contained in:
LoRd_MuldeR 2021-03-20 17:36:24 +01:00
parent 1b049d5291
commit d8f446832b
Signed by: mulder
GPG Key ID: 2B5913365F57E03F
5 changed files with 59 additions and 30 deletions

View File

@ -1,4 +1,5 @@
/* /*
This is a stripped-down and simplified "64-Bit only" version of BLAKE2s
BLAKE2 reference source code package - reference C implementations BLAKE2 reference source code package - reference C implementations
Copyright 2012, Samuel Neves <sneves@dei.uc.pt>. You may use this under the Copyright 2012, Samuel Neves <sneves@dei.uc.pt>. You may use this under the
terms of the CC0, the OpenSSL Licence, or the Apache Public License 2.0, at terms of the CC0, the OpenSSL Licence, or the Apache Public License 2.0, at

View File

@ -1,4 +1,5 @@
/* /*
This is a stripped-down and simplified "64-Bit only" version of BLAKE2s
BLAKE2 reference source code package - reference C implementations BLAKE2 reference source code package - reference C implementations
Copyright 2012, Samuel Neves <sneves@dei.uc.pt>. You may use this under the Copyright 2012, Samuel Neves <sneves@dei.uc.pt>. You may use this under the
terms of the CC0, the OpenSSL Licence, or the Apache Public License 2.0, at terms of the CC0, the OpenSSL Licence, or the Apache Public License 2.0, at
@ -24,10 +25,12 @@ typedef struct _blake2s_t
} }
blake2s_t; blake2s_t;
/*BLAKE2s stream API*/
void blake2s_init(blake2s_t*const S); void blake2s_init(blake2s_t*const S);
void blake2s_update(blake2s_t*const S, const void* const pin, size_t inlen); void blake2s_update(blake2s_t*const S, const void* const pin, size_t inlen);
uint64_t blake2s_final(blake2s_t*const S); uint64_t blake2s_final(blake2s_t*const S);
/*BLAKE2s at once*/
uint64_t blake2s_compute(const void* const pin, const size_t inlen); uint64_t blake2s_compute(const void* const pin, const size_t inlen);
#endif #endif

View File

@ -275,8 +275,7 @@ static int encrypt(const char* const passphrase, const CHR* const input_path, co
goto clean_up; goto clean_up;
} }
nonce = swap_bytes_u64(nonce ^ MAGIC_NUMBER); if (fwrite_ui64(nonce ^ MAGIC_NUMBER, file_out) < 1U)
if (fwrite(&nonce, sizeof(uint64_t), 1U, file_out) < 1U)
{ {
FPUTS(T("\n\nI/O error: Failed to write nonce value!\n\n"), stderr); FPUTS(T("\n\nI/O error: Failed to write nonce value!\n\n"), stderr);
goto clean_up; goto clean_up;
@ -357,15 +356,16 @@ static int encrypt(const char* const passphrase, const CHR* const input_path, co
goto clean_up; goto clean_up;
} }
const uint64_t checksum_actual = swap_bytes_u64(blake2s_final(&blake2s_state)); uint8_t checksum_buffer[sizeof(uint64_t)];
store_ui64(checksum_buffer, blake2s_final(&blake2s_state));
if ((status = slunkcrypt_encrypt_inplace(ctx, (uint8_t*)&checksum_actual, sizeof(uint64_t))) != SLUNKCRYPT_SUCCESS) if ((status = slunkcrypt_encrypt_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); FPUTS((status == SLUNKCRYPT_ABORTED) ? T("\n\nProcess interrupted!\n\n") : T("\n\nSlunkCrypt error: Failed to encrypt checksum!\n\n"), stderr);
goto clean_up; goto clean_up;
} }
if (fwrite(&checksum_actual, sizeof(uint64_t), 1U, file_out) < 1U) if (fwrite(checksum_buffer, sizeof(uint8_t), sizeof(uint64_t), file_out) < sizeof(uint64_t))
{ {
FPUTS(T("\n\nI/O error: Failed to write the checksum!\n\n"), stderr); FPUTS(T("\n\nI/O error: Failed to write the checksum!\n\n"), stderr);
goto clean_up; goto clean_up;
@ -434,13 +434,13 @@ static int decrypt(const char* const passphrase, const CHR* const input_path, co
fflush(stderr); fflush(stderr);
uint64_t nonce; uint64_t nonce;
if (fread(&nonce, sizeof(uint64_t), 1U, file_in) < 1U) if (fread_ui64(&nonce, file_in) < 1U)
{ {
FPUTS(T("\n\nI/O error: Failed to read nonce value!\n\n"), stderr); FPUTS(T("\n\nI/O error: Failed to read nonce value!\n\n"), stderr);
goto clean_up; goto clean_up;
} }
ctx = slunkcrypt_alloc(swap_bytes_u64(nonce) ^ MAGIC_NUMBER, (const uint8_t*)passphrase, strlen(passphrase)); ctx = slunkcrypt_alloc(nonce ^ MAGIC_NUMBER, (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);
@ -529,24 +529,23 @@ static int decrypt(const char* const passphrase, const CHR* const input_path, co
const uint64_t checksum_actual = blake2s_final(&blake2s_state); const uint64_t checksum_actual = blake2s_final(&blake2s_state);
uint64_t checksum_expected; uint8_t checksum_buffer[sizeof(uint64_t)];
if (fread(&checksum_expected, sizeof(uint64_t), 1U, file_in) < 1U) if (fread(checksum_buffer, sizeof(uint8_t), sizeof(uint64_t), file_in) < sizeof(uint64_t))
{ {
FPUTS(T("\n\nI/O error: Failed to read the checksum!\n\n"), stderr); FPUTS(T("\n\nI/O error: Failed to read the checksum!\n\n"), stderr);
goto clean_up; goto clean_up;
} }
if ((status = slunkcrypt_decrypt_inplace(ctx, (uint8_t*)&checksum_expected, sizeof(uint64_t))) != SLUNKCRYPT_SUCCESS) if ((status = slunkcrypt_decrypt_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); FPUTS((status == SLUNKCRYPT_ABORTED) ? T("\n\nProcess interrupted!\n\n") : T("\n\nSlunkCrypt error: Failed to decrypt checksum!\n\n"), stderr);
goto clean_up; goto clean_up;
} }
checksum_expected = swap_bytes_u64(checksum_expected);
FPRINTF(stderr, T("\b\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);
const uint64_t checksum_expected = load_ui64(checksum_buffer);
if (checksum_actual != checksum_expected) if (checksum_actual != checksum_expected)
{ {
FPRINTF(stderr, T("Error: Checksum mismatch detected! [expected: 0x%016") T(PRIX64) T(", actual: 0x%016") T(PRIX64) T("]\n\n"), checksum_expected, checksum_actual); FPRINTF(stderr, T("Error: Checksum mismatch detected! [expected: 0x%016") T(PRIX64) T(", actual: 0x%016") T(PRIX64) T("]\n\n"), checksum_expected, checksum_actual);

View File

@ -143,25 +143,45 @@ char* CHR_to_utf8(const CHR*const input)
// Byte-order support // Byte-order support
// ========================================================================== // ==========================================================================
#if defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && (__BYTE_ORDER == __BIG_ENDIAN) void store_ui64(uint8_t *const dst, const uint64_t value)
# define BIG_ENDIAN_BYTE_ORDER 1 {
#elif defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) dst[0U] = (uint8_t)(value >> 0);
# define BIG_ENDIAN_BYTE_ORDER 1 dst[1U] = (uint8_t)(value >> 8);
#else dst[2U] = (uint8_t)(value >> 16);
# define BIG_ENDIAN_BYTE_ORDER 0 dst[3U] = (uint8_t)(value >> 24);
#endif dst[4U] = (uint8_t)(value >> 32);
dst[5U] = (uint8_t)(value >> 40);
uint64_t swap_bytes_u64(const uint64_t value) dst[6U] = (uint8_t)(value >> 48);
dst[7U] = (uint8_t)(value >> 56);
}
uint64_t load_ui64(const uint8_t *const src)
{ {
#if BIG_ENDIAN_BYTE_ORDER
return return
(((value) >> 56) & 0x00000000000000FF) | (((value) >> 40) & 0x000000000000FF00) | ((uint64_t)(src[0U]) << 0) |
(((value) >> 24) & 0x0000000000FF0000) | (((value) >> 8) & 0x00000000FF000000) | ((uint64_t)(src[1U]) << 8) |
(((value) << 8) & 0x000000FF00000000) | (((value) << 24) & 0x0000FF0000000000) | ((uint64_t)(src[2U]) << 16) |
(((value) << 40) & 0x00FF000000000000) | (((value) << 56) & 0xFF00000000000000); ((uint64_t)(src[3U]) << 24) |
#else ((uint64_t)(src[4U]) << 32) |
return value; /*nothing to do*/ ((uint64_t)(src[5U]) << 40) |
#endif ((uint64_t)(src[6U]) << 48) |
((uint64_t)(src[7U]) << 56);
}
size_t fwrite_ui64(const uint64_t value, FILE *const stream)
{
uint8_t buffer[sizeof(uint64_t)];
store_ui64(buffer, value);
const size_t result = fwrite(buffer, sizeof(uint8_t), sizeof(uint64_t), stream);
return result / sizeof(uint64_t);
}
size_t fread_ui64(uint64_t *const value, FILE *const stream)
{
uint8_t buffer[sizeof(uint64_t)];
const size_t result = fread(buffer, sizeof(uint8_t), sizeof(uint64_t), stream);
*value = (result >= sizeof(uint64_t)) ? load_ui64(buffer) : ((uint64_t)(-1LL));
return result / sizeof(uint64_t);
} }
// ========================================================================== // ==========================================================================

View File

@ -13,10 +13,16 @@ typedef void (signal_handler_t)(int);
void init_terminal(void); void init_terminal(void);
void setup_signal_handler(const int signo, signal_handler_t* const handler); void setup_signal_handler(const int signo, signal_handler_t* const handler);
uint64_t swap_bytes_u64(const uint64_t value);
void store_ui64(uint8_t* const dst, const uint64_t value);
uint64_t load_ui64(const uint8_t* const src);
size_t fwrite_ui64(const uint64_t value, FILE *const stream);
size_t fread_ui64(uint64_t *const value, FILE *const stream);
char* CHR_to_utf8(const CHR *const input); char* CHR_to_utf8(const CHR *const input);
uint64_t get_file_size(FILE* const file); uint64_t get_file_size(FILE* const file);
const CHR *get_file_name(const CHR *path); const CHR *get_file_name(const CHR *path);
uint64_t round_down(const uint64_t value, const uint64_t base); uint64_t round_down(const uint64_t value, const uint64_t base);
#define ARRAY_SIZE(X) (sizeof((X)) / sizeof(*(X))) #define ARRAY_SIZE(X) (sizeof((X)) / sizeof(*(X)))