Replaced some more byte-order-specific code with a byte-order-agnostic implementation.
This commit is contained in:
parent
1b049d5291
commit
d8f446832b
@ -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
|
||||
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
|
||||
|
@ -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
|
||||
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
|
||||
@ -24,10 +25,12 @@ typedef struct _blake2s_t
|
||||
}
|
||||
blake2s_t;
|
||||
|
||||
/*BLAKE2s stream API*/
|
||||
void blake2s_init(blake2s_t*const S);
|
||||
void blake2s_update(blake2s_t*const S, const void* const pin, size_t inlen);
|
||||
uint64_t blake2s_final(blake2s_t*const S);
|
||||
|
||||
/*BLAKE2s at once*/
|
||||
uint64_t blake2s_compute(const void* const pin, const size_t inlen);
|
||||
|
||||
#endif
|
||||
|
@ -275,8 +275,7 @@ static int encrypt(const char* const passphrase, const CHR* const input_path, co
|
||||
goto clean_up;
|
||||
}
|
||||
|
||||
nonce = swap_bytes_u64(nonce ^ MAGIC_NUMBER);
|
||||
if (fwrite(&nonce, sizeof(uint64_t), 1U, file_out) < 1U)
|
||||
if (fwrite_ui64(nonce ^ MAGIC_NUMBER, file_out) < 1U)
|
||||
{
|
||||
FPUTS(T("\n\nI/O error: Failed to write nonce value!\n\n"), stderr);
|
||||
goto clean_up;
|
||||
@ -357,15 +356,16 @@ static int encrypt(const char* const passphrase, const CHR* const input_path, co
|
||||
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);
|
||||
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);
|
||||
goto clean_up;
|
||||
@ -434,13 +434,13 @@ static int decrypt(const char* const passphrase, const CHR* const input_path, co
|
||||
fflush(stderr);
|
||||
|
||||
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);
|
||||
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)
|
||||
{
|
||||
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);
|
||||
|
||||
uint64_t checksum_expected;
|
||||
if (fread(&checksum_expected, sizeof(uint64_t), 1U, file_in) < 1U)
|
||||
uint8_t checksum_buffer[sizeof(uint64_t)];
|
||||
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);
|
||||
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);
|
||||
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);
|
||||
fflush(stderr);
|
||||
|
||||
const uint64_t checksum_expected = load_ui64(checksum_buffer);
|
||||
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);
|
||||
|
@ -143,25 +143,45 @@ char* CHR_to_utf8(const CHR*const input)
|
||||
// Byte-order support
|
||||
// ==========================================================================
|
||||
|
||||
#if defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && (__BYTE_ORDER == __BIG_ENDIAN)
|
||||
# define BIG_ENDIAN_BYTE_ORDER 1
|
||||
#elif defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
|
||||
# define BIG_ENDIAN_BYTE_ORDER 1
|
||||
#else
|
||||
# define BIG_ENDIAN_BYTE_ORDER 0
|
||||
#endif
|
||||
|
||||
uint64_t swap_bytes_u64(const uint64_t value)
|
||||
void store_ui64(uint8_t *const dst, const uint64_t value)
|
||||
{
|
||||
dst[0U] = (uint8_t)(value >> 0);
|
||||
dst[1U] = (uint8_t)(value >> 8);
|
||||
dst[2U] = (uint8_t)(value >> 16);
|
||||
dst[3U] = (uint8_t)(value >> 24);
|
||||
dst[4U] = (uint8_t)(value >> 32);
|
||||
dst[5U] = (uint8_t)(value >> 40);
|
||||
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
|
||||
(((value) >> 56) & 0x00000000000000FF) | (((value) >> 40) & 0x000000000000FF00) |
|
||||
(((value) >> 24) & 0x0000000000FF0000) | (((value) >> 8) & 0x00000000FF000000) |
|
||||
(((value) << 8) & 0x000000FF00000000) | (((value) << 24) & 0x0000FF0000000000) |
|
||||
(((value) << 40) & 0x00FF000000000000) | (((value) << 56) & 0xFF00000000000000);
|
||||
#else
|
||||
return value; /*nothing to do*/
|
||||
#endif
|
||||
((uint64_t)(src[0U]) << 0) |
|
||||
((uint64_t)(src[1U]) << 8) |
|
||||
((uint64_t)(src[2U]) << 16) |
|
||||
((uint64_t)(src[3U]) << 24) |
|
||||
((uint64_t)(src[4U]) << 32) |
|
||||
((uint64_t)(src[5U]) << 40) |
|
||||
((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);
|
||||
}
|
||||
|
||||
// ==========================================================================
|
||||
|
@ -13,10 +13,16 @@ typedef void (signal_handler_t)(int);
|
||||
|
||||
void init_terminal(void);
|
||||
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);
|
||||
uint64_t get_file_size(FILE* const file);
|
||||
const CHR *get_file_name(const CHR *path);
|
||||
|
||||
uint64_t round_down(const uint64_t value, const uint64_t base);
|
||||
|
||||
#define ARRAY_SIZE(X) (sizeof((X)) / sizeof(*(X)))
|
||||
|
Loading…
Reference in New Issue
Block a user