FPUTS(T("Encrypting file contents, please be patient... "), stderr);
fflush(stderr);
- uint64_t salt;
- if (slunkcrypt_generate_salt(&salt) != SLUNKCRYPT_SUCCESS)
+ uint64_t nonce;
+ if (slunkcrypt_generate_nonce(&nonce) != SLUNKCRYPT_SUCCESS)
{
FPUTS(T("\n\nSlunkCrypt error: Failed to generate salt!\n\n"), stderr);
goto clean_up;
}
- if (fwrite(&salt, sizeof(uint64_t), 1U, file_out) < 1U)
+ ctx = slunkcrypt_alloc(nonce, (const uint8_t*)passphrase, strlen(passphrase));
+ if (!ctx)
{
- FPUTS(T("\n\nI/O error: Failed to write salt value!\n\n"), stderr);
+ FPUTS(g_slunkcrypt_abort_flag ? T("\n\nProcess interrupted!\n\n") : T("\n\nSlunkCrypt error: Failed to initialize encryption!\n\n"), stderr);
goto clean_up;
}
- ctx = slunkcrypt_alloc(salt, (const uint8_t*)passphrase, strlen(passphrase));
- if (!ctx)
+ nonce = swap_bytes_u64(nonce);
+ if (fwrite(&nonce, sizeof(uint64_t), 1U, file_out) < 1U)
{
- FPUTS(g_slunkcrypt_abort_flag ? T("\n\nProcess interrupted!\n\n") : T("\n\nSlunkCrypt error: Failed to initialize encryption!\n\n"), stderr);
+ FPUTS(T("\n\nI/O error: Failed to write salt value!\n\n"), stderr);
goto clean_up;
}
goto clean_up;
}
- crc_actual = crc64_finish(crc_actual);
+ crc_actual = swap_bytes_u64(crc64_finish(crc_actual));
const int status = slunkcrypt_encrypt_inplace(ctx, (uint8_t*)&crc_actual, sizeof(uint64_t));
if (status != SLUNKCRYPT_SUCCESS)
FPUTS(T("Decrypting file contents, please be patient... "), stderr);
fflush(stderr);
- uint64_t salt;
- if (fread(&salt, sizeof(uint64_t), 1U, file_in) < 1U)
+ uint64_t nonce;
+ if (fread(&nonce, sizeof(uint64_t), 1U, file_in) < 1U)
{
FPUTS(T("\n\nI/O error: Failed to read salt value!\n\n"), stderr);
goto clean_up;
}
- ctx = slunkcrypt_alloc(salt, (const uint8_t*)passphrase, strlen(passphrase));
+ ctx = slunkcrypt_alloc(swap_bytes_u64(nonce), (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);
goto clean_up;
}
+ crc_expected = swap_bytes_u64(crc_expected);
+
FPRINTF(stderr, T("\b\b\b\b\b\b\b%5.1f%%\n\n"), 100.0);
fflush(stderr);
const size_t length = strlen(message) + 1U;
slunkcrypt_t ctx = SLUNKCRYPT_NULL;
- uint64_t salt;
- if (slunkcrypt_generate_salt(&salt) != SLUNKCRYPT_SUCCESS)
+ uint64_t nonce;
+ if (slunkcrypt_generate_nonce(&nonce) != SLUNKCRYPT_SUCCESS)
{
FPUTS(T("\n\nWhoops: Failed to generate salt!\n\n"), stderr);
return EXIT_FAILURE;
goto clean_up;
}
- ctx = slunkcrypt_alloc(salt, (const uint8_t*)passphrase, strlen(passphrase));
+ ctx = slunkcrypt_alloc(nonce, (const uint8_t*)passphrase, strlen(passphrase));
if (!ctx)
{
FPUTS(g_slunkcrypt_abort_flag ? T("\n\nProcess interrupted!\n\n") : T("\n\nWhoops: Failed to initialize encoder!\n\n"), stderr);
goto clean_up;
}
- status = slunkcrypt_reset(ctx, salt, (const uint8_t*)passphrase, strlen(passphrase));
+ status = slunkcrypt_reset(ctx, nonce, (const uint8_t*)passphrase, strlen(passphrase));
if (status != SLUNKCRYPT_SUCCESS)
{
FPUTS((status == SLUNKCRYPT_ABORTED) ? T("\n\nProcess interrupted!\n\n") : T("\n\nWhoops: Failed to initialize decoder!\n\n"), stderr);
/******************************************************************************/
#ifdef _WIN32
-#define WIN32_LEAN_AND_MEAN 1
-#define _CRT_SECURE_NO_WARNINGS 1
+# define WIN32_LEAN_AND_MEAN 1
+# define _CRT_SECURE_NO_WARNINGS 1
#else
-#define _GNU_SOURCE 1
+# define _GNU_SOURCE 1
#endif
#include "utils.h"
#include <sys/types.h>
#include <signal.h>
+/* Platform support */
#ifdef _WIN32
-#include <Windows.h>
-#include <io.h>
-#include <fcntl.h>
-#define STAT_T struct _stati64
-#define FSTAT(X,Y) _fstati64((X),(Y))
-#define FILENO(X) _fileno((X))
-#define S_IFMT _S_IFMT
-#define S_IFDIR _S_IFDIR
-#define S_IFIFO _S_IFIFO
-#ifndef _O_U8TEXT
-#define _O_U8TEXT 0x40000
+# include <Windows.h>
+# include <io.h>
+# include <fcntl.h>
+# define STAT_T struct _stati64
+# define FSTAT(X,Y) _fstati64((X),(Y))
+# define FILENO(X) _fileno((X))
+# define S_IFMT _S_IFMT
+# define S_IFDIR _S_IFDIR
+# define S_IFIFO _S_IFIFO
+# ifndef _O_U8TEXT
+# define _O_U8TEXT 0x40000
+# endif
+#else
+# if defined(__USE_LARGEFILE64) && (__USE_LARGEFILE64)
+# define STAT_T struct stat64
+# define FSTAT(X,Y) fstat64((X),(Y))
+# else
+# define STAT_T struct stat
+# define FSTAT(X,Y) fstat((X),(Y))
+# endif
+# define FILENO(X) fileno((X))
#endif
-#ifndef _DLL
+
+// ==========================================================================
+// Terminal initialization
+// ==========================================================================
+
+/* CRT imports */
+#if defined(_WIN32) && !defined(_DLL)
extern char *const _acmdln;
extern wchar_t *const _wcmdln;
#endif
-#else
-#if defined(__USE_LARGEFILE64) && (__USE_LARGEFILE64)
-#define STAT_T struct stat64
-#define FSTAT(X,Y) fstat64((X),(Y))
-#else
-#define STAT_T struct stat
-#define FSTAT(X,Y) fstat((X),(Y))
-#endif
-#define FILENO(X) fileno((X))
-#endif
void init_terminal(void)
{
#endif
}
+// ==========================================================================
+// Signal handling
+// ==========================================================================
+
void setup_signal_handler(const int signo, signal_handler_t* const handler)
{
#ifdef _WIN32
#endif
}
+// ==========================================================================
+// Character set conversion
+// ==========================================================================
+
char* CHR_to_utf8(const CHR*const input)
{
#ifdef _WIN32
#endif
}
+// ==========================================================================
+// 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)
+{
+#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
+}
+
+// ==========================================================================
+// File functions
+// ==========================================================================
+
uint64_t get_file_size(FILE* const file)
{
STAT_T stat;
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);
char* CHR_to_utf8(const CHR *const input);
uint64_t get_file_size(FILE* const file);
const CHR *get_file_name(const CHR *path);