#define SLUNK_MODE_VERS 1
#define SLUNK_MODE_ENCR 2
#define SLUNK_MODE_DECR 3
-#define SLUNK_MODE_TEST 4
+#define SLUNK_MODE_PASS 4
+#define SLUNK_MODE_TEST 5
static const CHR *const ENVV_PASSWD_NAME = T("SLUNK_PASSPHRASE");
+static const char PASSWD_SYMBOLS[] =
+{
+ '!', '#', '$', '%', '&', '(', ')', '*', '+', ',', '-', '.', '/', '0', '1',
+ '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '<', '=', '>', '?', '@',
+ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
+ 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '[', ']', '^', '_',
+ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
+ 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '}', '~'
+};
// ==========================================================================
// Auxiliary functions
{
return SLUNK_MODE_DECR;
}
+ else if ((!STRICMP(command, T("-p"))) || (!STRICMP(command, T("--make-pw"))))
+ {
+ return SLUNK_MODE_PASS;
+ }
else if ((!STRICMP(command, T("-t"))) || (!STRICMP(command, T("--self-test"))))
{
return SLUNK_MODE_TEST;
FPUTS(T("====================================================================\n\n"), stderr);
FPUTS(T("Usage:\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);
+ FPRINTF(stderr, T(" %") T(PRISTR) T(" --decrypt [[@][:]<passphrase>] <input.enc> <output.txt>\n"), program);
+ FPRINTF(stderr, T(" %") T(PRISTR) T(" --make-pw [<length>]\n\n"), program);
}
static char *read_passphrase(const CHR* const file_name)
return !strong;
}
+static int generate_passphrase(const size_t length)
+{
+ int result = EXIT_FAILURE;
+ const size_t passwd_len = BOUND(SLUNKCRYPT_PWDLEN_MIN, length, SLUNKCRYPT_PWDLEN_MAX);
+
+ char *const buffer = (char*) malloc((passwd_len + 1U) * sizeof(char));
+ if (!buffer)
+ {
+ FPUTS(T("\n\nError: Failed to allocate memory buffer!\n\n"), stderr);
+ return EXIT_FAILURE;
+ }
+
+ do
+ {
+ for (size_t i = 0U; i < passwd_len; ++i)
+ {
+ uint64_t value;
+ if (slunkcrypt_generate_nonce(&value) != SLUNKCRYPT_SUCCESS)
+ {
+ FPUTS(T("\n\nError: Failed to generate next random number!\n\n"), stderr);
+ goto clean_up;
+ }
+ buffer[i] = PASSWD_SYMBOLS[value % ARRAY_SIZE(PASSWD_SYMBOLS)];
+ }
+ buffer[passwd_len] = '\0';
+ }
+ while (weak_passphrase(buffer));
+
+ FPRINTF(stdout, T("%") T(PRIstr) T("\n\n"), buffer);
+ fflush(stdout);
+ result = EXIT_SUCCESS;
+
+clean_up:
+
+ if (buffer)
+ {
+ slunkcrypt_bzero(buffer, passwd_len * sizeof(char));
+ free(buffer);
+ }
+
+ return result;
+}
+
static int open_files(FILE **const file_in, FILE **const file_out, const CHR* const input_path, const CHR* const output_path)
{
*file_in = FOPEN(input_path, T("rb"));
if (open_files(&file_in, &file_out, input_path, output_path) != EXIT_SUCCESS)
{
- goto clean_up;;
+ goto clean_up;
}
const uint64_t file_size = get_file_size(file_in);
}
else if ((file_size % sizeof(uint64_t)) != 0)
{
- FPRINTF(stderr, T("Warning: File size is *not* an integer multiple of %u, ignoring excess bytes!\n\n"), sizeof(uint64_t));
+ FPRINTF(stderr, T("Warning: File size is *not* an integer multiple of %u, ignoring excess bytes!\n\n"), (unsigned int)sizeof(uint64_t));
}
FPUTS(T("Decrypting file contents, please be patient... "), stderr);
case SLUNK_MODE_VERS:
result = EXIT_SUCCESS;
goto clean_up;
+ case SLUNK_MODE_PASS:
+ result = generate_passphrase((argc > 2) ? STRTOUL(argv[2U]) : 24U);
+ goto clean_up;
case SLUNK_MODE_TEST:
result = run_self_test();
goto clean_up;
# define STRLEN(X) wcslen((X))
# define STRICMP(X,Y) _wcsicmp((X),(Y))
# define STRRCHR(X,Y) wcsrchr((X),(Y))
+# define STRTOUL(X) wcstoul((X), NULL, 0)
# define FPUTS(X,Y) fputws((X),(Y))
# define FPRINTF(X,Y,...) fwprintf((X),(Y),__VA_ARGS__)
# define FOPEN(X,Y) _wfsopen((X),(Y),_SH_SECURE)
# define STRLEN(X) strlen((X))
# define STRICMP(X,Y) strcasecmp((X),(Y))
# define STRRCHR(X,Y) strrchr((X),(Y))
+# define STRTOUL(X) strtoul((X), NULL, 0)
# define FPUTS(X,Y) fputs((X),(Y))
# define FPRINTF(X,Y,...) fprintf((X),(Y),__VA_ARGS__)
# define FOPEN(X,Y) fopen((X),(Y))