Add Configuration class to intelligently decide default settings
This commit is contained in:
parent
f28e510dba
commit
8b9221e2e0
@ -42,3 +42,8 @@ target_include_directories(${PROJECT_NAME} PRIVATE
|
||||
${gcrypt_include}
|
||||
)
|
||||
|
||||
target_compile_options(${PROJECT_NAME} PRIVATE
|
||||
-Werror
|
||||
-fdiagnostics-color=always
|
||||
)
|
||||
|
||||
|
52
GCryptCLI/include/Configuration.h
Normal file
52
GCryptCLI/include/Configuration.h
Normal file
@ -0,0 +1,52 @@
|
||||
#ifndef GCRYPTCLI_CONFIGURATION_H
|
||||
#define GCRYPTCLI_CONFIGURATION_H
|
||||
|
||||
#include <string>
|
||||
|
||||
class Configuration {
|
||||
public:
|
||||
static enum class INPUT_FROM {
|
||||
STDIN,
|
||||
FILE,
|
||||
PARAMETER
|
||||
} inputFrom;
|
||||
|
||||
static enum class OUTPUT_TO {
|
||||
STDOUT,
|
||||
FILE
|
||||
} outputTo;
|
||||
|
||||
static enum class IOBASE_FORMAT {
|
||||
BASE_BYTES,
|
||||
BASE_2,
|
||||
BASE_8,
|
||||
BASE_10,
|
||||
BASE_16,
|
||||
BASE_64,
|
||||
BASE_UWU,
|
||||
BASE_UGH
|
||||
} iobaseFormat;
|
||||
|
||||
static std::string inputFilename;
|
||||
static std::string outputFilename;
|
||||
|
||||
static enum class MODULE {
|
||||
ENCRYPT,
|
||||
DECRYPT,
|
||||
HASH,
|
||||
GENERATE_KEYFILE
|
||||
} activeModule;
|
||||
|
||||
//! Will analyze the supplied cli parameters,
|
||||
//! and decide what the configuration will be.
|
||||
static void Parse();
|
||||
|
||||
private:
|
||||
static void DecideInputFrom();
|
||||
static void DecideOutputTo();
|
||||
static void DecideIOBaseFormat();
|
||||
static void DecideModule();
|
||||
};
|
||||
|
||||
#endif
|
||||
|
0
GCryptCLI/include/DataIngestionLayer.h
Normal file
0
GCryptCLI/include/DataIngestionLayer.h
Normal file
@ -2,14 +2,13 @@
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <GCrypt/Version.h>
|
||||
#include <GCrypt/Config.h>
|
||||
#include <GCrypt/Block.h>
|
||||
#include "Version.h"
|
||||
|
||||
using namespace Hazelnp;
|
||||
using namespace Leonetienne;
|
||||
using namespace Leonetienne::GCrypt;
|
||||
|
||||
void CommandlineInterface::Init(int argc, const char* const* argv)
|
||||
{
|
||||
void CommandlineInterface::Init(int argc, const char* const* argv) {
|
||||
/* General information */
|
||||
std::stringstream ss;
|
||||
ss << "CLI for the GCrypt cipher/obfuscator" << std::endl
|
||||
@ -23,20 +22,20 @@ void CommandlineInterface::Init(int argc, const char* const* argv)
|
||||
nupp.SetCrashOnFail("true");
|
||||
|
||||
/* Builtin documentation */
|
||||
nupp.RegisterDescription("--encrypt", "Use the encryption routine.");
|
||||
nupp.RegisterDescription("--encrypt", "Use the encryption module.");
|
||||
nupp.RegisterConstraint("--encrypt", ParamConstraint(true, DATA_TYPE::VOID, {}, false, {"--decrypt", "--hash" }));
|
||||
nupp.RegisterAbbreviation("-e", "--encrypt");
|
||||
|
||||
nupp.RegisterDescription("--decrypt", "Use decryption routine.");
|
||||
nupp.RegisterDescription("--decrypt", "Use decryption module.");
|
||||
nupp.RegisterConstraint("--decrypt", ParamConstraint(true, DATA_TYPE::VOID, {}, false, { "--encrypt", "--hash", "--generate-keyfile" }));
|
||||
nupp.RegisterAbbreviation("-d", "--decrypt");
|
||||
|
||||
nupp.RegisterDescription("--hash", "Use the GHash hash function to calculate a hashsum.");
|
||||
nupp.RegisterDescription("--hash", "Use the GHash hash module to calculate a hashsum.");
|
||||
nupp.RegisterConstraint("--hash", ParamConstraint(true, DATA_TYPE::VOID, {}, false, { "--encrypt", "--decrypt", "--generate-keyfile" }));
|
||||
nupp.RegisterAbbreviation("-h", "--hash");
|
||||
|
||||
nupp.RegisterDescription("--generate-keyfile", "Will generate a random keyfile, and exit.");
|
||||
nupp.RegisterConstraint("--generate-keyfile", ParamConstraint(true, DATA_TYPE::STRING, { "--encrypt", "--decrypt", "--hash" }, false, {}));
|
||||
nupp.RegisterDescription("--generate-keyfile", "Use the Keyfile module. Will generate a random keyfile, and exit.");
|
||||
nupp.RegisterConstraint("--generate-keyfile", ParamConstraint(true, DATA_TYPE::STRING, {}, false, { "--encrypt", "--decrypt", "--hash" }));
|
||||
|
||||
nupp.RegisterDescription("--intext", "Encrypt this string.");
|
||||
nupp.RegisterConstraint("--intext", ParamConstraint(true, DATA_TYPE::STRING, {}, false, { "--infile" }));
|
||||
@ -55,7 +54,7 @@ void CommandlineInterface::Init(int argc, const char* const* argv)
|
||||
nupp.RegisterConstraint("--key", ParamConstraint(true, DATA_TYPE::STRING, {}, false, { "--keyfile", "--keyask", "--hash" }));
|
||||
nupp.RegisterAbbreviation("-k", "--key");
|
||||
|
||||
ss << "Read in the first {KEYSIZE}(=" << GCrypt::BLOCK_SIZE << ") bits of this file and use that as an encryption key. WARNING: Arguments may be logged by the system!";
|
||||
ss << "Read in the first {KEYSIZE}(=" << Block::BLOCK_SIZE_BITS << ") bits of this file and use that as an encryption key. WARNING: Arguments may be logged by the system!";
|
||||
nupp.RegisterDescription("--keyfile", ss.str());
|
||||
ss.str("");
|
||||
nupp.RegisterConstraint("--keyfile", ParamConstraint(true, DATA_TYPE::STRING, {}, false, { "--key", "--keyask", "--hash" }));
|
||||
@ -73,25 +72,25 @@ void CommandlineInterface::Init(int argc, const char* const* argv)
|
||||
nupp.RegisterConstraint("--iobase-bytes", ParamConstraint(true, DATA_TYPE::VOID, {}, false, { "--iobase-2", "--iobase-8", "--iobase-10", "--iobase-16", "--iobase-64", "--iobase-uwu", "--iobase-ugh" }));
|
||||
|
||||
nupp.RegisterDescription("--iobase-2", "Interpret and format ciphertexts in base2");
|
||||
nupp.RegisterConstraint("--iobase-2", ParamConstraint(true, DATA_TYPE::VOID, {}, false, { "--iobase-bytes", "--iobase-8", "--iobase-10", "--iobase-16",, "--iobase-64", "--iobase-uwu", "--iobase-ugh" }));
|
||||
nupp.RegisterConstraint("--iobase-2", ParamConstraint(true, DATA_TYPE::VOID, {}, false, { "--iobase-bytes", "--iobase-8", "--iobase-10", "--iobase-16", "--iobase-64", "--iobase-uwu", "--iobase-ugh" }));
|
||||
|
||||
nupp.RegisterDescription("--iobase-8", "Interpret and format ciphertexts in base8");
|
||||
nupp.RegisterConstraint("--iobase-8", ParamConstraint(true, DATA_TYPE::VOID, {}, false, { "--iobase-bytes", "--iobase-2", "--iobase-10", "--iobase-16",, "--iobase-64", "--iobase-uwu", "--iobase-ugh" }));
|
||||
nupp.RegisterConstraint("--iobase-8", ParamConstraint(true, DATA_TYPE::VOID, {}, false, { "--iobase-bytes", "--iobase-2", "--iobase-10", "--iobase-16", "--iobase-64", "--iobase-uwu", "--iobase-ugh" }));
|
||||
|
||||
nupp.RegisterDescription("--iobase-10", "Interpret and format ciphertexts in base10");
|
||||
nupp.RegisterConstraint("--iobase-10", ParamConstraint(true, DATA_TYPE::VOID, {}, false, { "--iobase-bytes", "--iobase-2", "--iobase-8", "--iobase-16",, "--iobase-64", "--iobase-uwu", "--iobase-ugh" }));
|
||||
nupp.RegisterConstraint("--iobase-10", ParamConstraint(true, DATA_TYPE::VOID, {}, false, { "--iobase-bytes", "--iobase-2", "--iobase-8", "--iobase-16", "--iobase-64", "--iobase-uwu", "--iobase-ugh" }));
|
||||
|
||||
nupp.RegisterDescription("--iobase-16", "Interpret and format ciphertexts in base16 (hex)");
|
||||
nupp.RegisterConstraint("--iobase-16", ParamConstraint(true, DATA_TYPE::VOID, {}, false, { "--iobase-bytes", "--iobase-2", "--iobase-8", "--iobase-64", "--iobase-uwu", "--iobase-ugh" }));
|
||||
|
||||
nupp.RegisterDescription("--iobase-64", "Interpret and format ciphertexts in base64");
|
||||
nupp.RegisterConstraint("--iobase-64", ParamConstraint(true, DATA_TYPE::VOID, {}, false, { "--iobase-bytes", "--iobase-2", "--iobase-8", "--iobase-10", "--iobase-16",, "--iobase-uwu", "--iobase-ugh" }));
|
||||
nupp.RegisterConstraint("--iobase-64", ParamConstraint(true, DATA_TYPE::VOID, {}, false, { "--iobase-bytes", "--iobase-2", "--iobase-8", "--iobase-10", "--iobase-16", "--iobase-uwu", "--iobase-ugh" }));
|
||||
|
||||
nupp.RegisterDescription("--iobase-uwu", "Interpret and format ciphertexts in base uwu");
|
||||
nupp.RegisterConstraint("--iobase-uwu", ParamConstraint(true, DATA_TYPE::VOID, {}, false, { "--iobase-bytes", "--iobase-2", "--iobase-8", "--iobase-10", "--iobase-16",, "--iobase-64", "--iobase-ugh" }));
|
||||
nupp.RegisterConstraint("--iobase-uwu", ParamConstraint(true, DATA_TYPE::VOID, {}, false, { "--iobase-bytes", "--iobase-2", "--iobase-8", "--iobase-10", "--iobase-16", "--iobase-64", "--iobase-ugh" }));
|
||||
|
||||
nupp.RegisterDescription("--iobase-ugh", "Interpret and format ciphertexts in base ugh");
|
||||
nupp.RegisterConstraint("--iobase-ugh", ParamConstraint(true, DATA_TYPE::VOID, {}, false, { "--iobase-bytes", "--iobase-2", "--iobase-8", "--iobase-10", "--iobase-16",, "--iobase-64", "--iobase-uwu" }));
|
||||
nupp.RegisterConstraint("--iobase-ugh", ParamConstraint(true, DATA_TYPE::VOID, {}, false, { "--iobase-bytes", "--iobase-2", "--iobase-8", "--iobase-10", "--iobase-16", "--iobase-64", "--iobase-uwu" }));
|
||||
|
||||
nupp.RegisterDescription("--lib-version", "Will supply the version of GCryptLib used.");
|
||||
nupp.RegisterConstraint("--lib-version", ParamConstraint(true, DATA_TYPE::VOID, {}, false, {}));
|
||||
@ -115,13 +114,23 @@ void CommandlineInterface::Init(int argc, const char* const* argv)
|
||||
return;
|
||||
}
|
||||
|
||||
Hazelnp::CmdArgsInterface& CommandlineInterface::Get()
|
||||
{
|
||||
Hazelnp::CmdArgsInterface& CommandlineInterface::Get() {
|
||||
return nupp;
|
||||
}
|
||||
|
||||
void CommandlineInterface::SpecialCompatibilityChecking()
|
||||
{
|
||||
void CommandlineInterface::SpecialCompatibilityChecking() {
|
||||
|
||||
// Active module
|
||||
// Do we have EITHER --encrypt or --decrypt or --hash?
|
||||
if (
|
||||
(!nupp.HasParam("--generate-keyfile")) &&
|
||||
(!nupp.HasParam("--hash")) &&
|
||||
(!nupp.HasParam("--encrypt")) &&
|
||||
(!nupp.HasParam("--decrypt"))
|
||||
) {
|
||||
CrashWithMsg("No module supplied! Please supply either --encrypt, --decrypt, --hash, or --generate-keyfile!");
|
||||
}
|
||||
|
||||
// Encryption key
|
||||
// Do we have EITHER --hash (no key required), --generate-keyfile (no key required), --key, --keyask or --keyfile given?
|
||||
if (
|
||||
@ -130,23 +139,43 @@ void CommandlineInterface::SpecialCompatibilityChecking()
|
||||
(!nupp.HasParam("--key")) &&
|
||||
(!nupp.HasParam("--keyfile")) &&
|
||||
(!nupp.HasParam("--keyask"))
|
||||
)
|
||||
) {
|
||||
CrashWithMsg("No encryption key supplied! Please supply either --key, --keyfile, or --keyask!");
|
||||
}
|
||||
|
||||
// Digestion mode
|
||||
// Do we have EITHER --encrypt or --decrypt or --hash?
|
||||
// Check that, if supplied, filename strings are not empty.
|
||||
if (
|
||||
(!nupp.HasParam("--hash")) &&
|
||||
(!nupp.HasParam("--encrypt")) &&
|
||||
(!nupp.HasParam("--decrypt"))
|
||||
)
|
||||
CrashWithMsg("No digestion mode supplied! Please supply either --encrypt, --decrypt, or --hash!");
|
||||
(nupp.HasParam("--ofile")) &&
|
||||
(nupp["--ofile"].GetString().length() == 0)
|
||||
) {
|
||||
CrashWithMsg("Length of --ofile is zero! That can't be a valid path!");
|
||||
}
|
||||
|
||||
if (
|
||||
(nupp.HasParam("--ifile")) &&
|
||||
(nupp["--ifile"].GetString().length() == 0)
|
||||
) {
|
||||
CrashWithMsg("Length of --ifile is zero! That can't be a valid path!");
|
||||
}
|
||||
|
||||
if (
|
||||
(nupp.HasParam("--keyfile")) &&
|
||||
(nupp["--keyfile"].GetString().length() == 0)
|
||||
) {
|
||||
CrashWithMsg("Length of --keyfile is zero! That can't be a valid path!");
|
||||
}
|
||||
|
||||
if (
|
||||
(nupp.HasParam("--generate-keyfile")) &&
|
||||
(nupp["--generate-keyfile"].GetString().length() == 0)
|
||||
) {
|
||||
CrashWithMsg("Length of --generate-keyfile is zero! That can't be a valid path!");
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void CommandlineInterface::CrashWithMsg(const std::string& msg)
|
||||
{
|
||||
void CommandlineInterface::CrashWithMsg(const std::string& msg) {
|
||||
std::cerr
|
||||
<< nupp.GetBriefDescription()
|
||||
<< std::endl
|
||||
@ -156,11 +185,10 @@ void CommandlineInterface::CrashWithMsg(const std::string& msg)
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
void CommandlineInterface::CatchVersionQueries()
|
||||
{
|
||||
void CommandlineInterface::CatchVersionQueries() {
|
||||
if (
|
||||
(nupp.HasParam("--version")) ||
|
||||
(nupp.HasParam("--cli-version")
|
||||
(nupp.HasParam("--cli-version"))
|
||||
) {
|
||||
std::cout << GCRYPTCLI_VERSION << std::endl;
|
||||
exit(0);
|
||||
|
156
GCryptCLI/src/Configuration.cpp
Normal file
156
GCryptCLI/src/Configuration.cpp
Normal file
@ -0,0 +1,156 @@
|
||||
#include "Configuration.h"
|
||||
#include "CommandlineInterface.h"
|
||||
|
||||
void Configuration::Parse() {
|
||||
DecideModule();
|
||||
DecideInputFrom();
|
||||
DecideOutputTo();
|
||||
DecideIOBaseFormat();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void Configuration::DecideInputFrom() {
|
||||
|
||||
if (CommandlineInterface::Get().HasParam("--intext")) {
|
||||
inputFrom = INPUT_FROM::PARAMETER;
|
||||
}
|
||||
else if (CommandlineInterface::Get().HasParam("--infile")) {
|
||||
inputFrom = INPUT_FROM::FILE;
|
||||
inputFilename = CommandlineInterface::Get()["--infile"].GetString();
|
||||
}
|
||||
else {
|
||||
inputFrom = INPUT_FROM::STDIN;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void Configuration::DecideOutputTo() {
|
||||
|
||||
// If module is "generate keyfile", we'll always write to a file.
|
||||
if (activeModule == MODULE::GENERATE_KEYFILE) {
|
||||
outputTo = OUTPUT_TO::FILE;
|
||||
outputFilename = CommandlineInterface::Get()["--generate-keyfile"].GetString();
|
||||
}
|
||||
|
||||
// Else, check if we have an --ofile defined.
|
||||
if (CommandlineInterface::Get().HasParam("--ofile")) {
|
||||
outputTo = OUTPUT_TO::FILE;
|
||||
outputFilename = CommandlineInterface::Get()["--ofile"].GetString();
|
||||
}
|
||||
else {
|
||||
outputTo = OUTPUT_TO::STDOUT;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void Configuration::DecideModule() {
|
||||
if (CommandlineInterface::Get().HasParam("--encrypt")) {
|
||||
activeModule = MODULE::ENCRYPT;
|
||||
}
|
||||
else if (CommandlineInterface::Get().HasParam("--decrypt")) {
|
||||
activeModule == MODULE::DECRYPT;
|
||||
}
|
||||
else if (CommandlineInterface::Get().HasParam("--hash")) {
|
||||
activeModule == MODULE::HASH;
|
||||
}
|
||||
else if (CommandlineInterface::Get().HasParam("--generate-keyfile")) {
|
||||
activeModule == MODULE::GENERATE_KEYFILE;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void Configuration::DecideIOBaseFormat() {
|
||||
|
||||
// Do we have any iobase specified?
|
||||
if (CommandlineInterface::Get().HasParam("--iobase-bytes")) {
|
||||
iobaseFormat = IOBASE_FORMAT::BASE_BYTES;
|
||||
return;
|
||||
}
|
||||
else if (CommandlineInterface::Get().HasParam("--iobase-2")) {
|
||||
iobaseFormat = IOBASE_FORMAT::BASE_2;
|
||||
return;
|
||||
}
|
||||
else if (CommandlineInterface::Get().HasParam("--iobase-8")) {
|
||||
iobaseFormat = IOBASE_FORMAT::BASE_8;
|
||||
return;
|
||||
}
|
||||
else if (CommandlineInterface::Get().HasParam("--iobase-10")) {
|
||||
iobaseFormat = IOBASE_FORMAT::BASE_10;
|
||||
return;
|
||||
}
|
||||
else if (CommandlineInterface::Get().HasParam("--iobase-16")) {
|
||||
iobaseFormat = IOBASE_FORMAT::BASE_16;
|
||||
return;
|
||||
}
|
||||
else if (CommandlineInterface::Get().HasParam("--iobase-64")) {
|
||||
iobaseFormat = IOBASE_FORMAT::BASE_64;
|
||||
return;
|
||||
}
|
||||
else if (CommandlineInterface::Get().HasParam("--iobase-uwu")) {
|
||||
iobaseFormat = IOBASE_FORMAT::BASE_UWU;
|
||||
return;
|
||||
}
|
||||
else if (CommandlineInterface::Get().HasParam("--iobase-ugh")) {
|
||||
iobaseFormat = IOBASE_FORMAT::BASE_UGH;
|
||||
return;
|
||||
}
|
||||
|
||||
// So we have no iobase explicitly specified.. Let's default..
|
||||
|
||||
// If we are encrypting or hashing,
|
||||
if (
|
||||
(activeModule == MODULE::ENCRYPT) ||
|
||||
(activeModule == MODULE::HASH)
|
||||
) {
|
||||
// and input comes from a parameter,
|
||||
// and output goes to stdout, let's assume base-16.
|
||||
if (
|
||||
(inputFrom == INPUT_FROM::PARAMETER) &&
|
||||
(outputTo == OUTPUT_TO::STDOUT)
|
||||
) {
|
||||
iobaseFormat = IOBASE_FORMAT::BASE_16;
|
||||
return;
|
||||
}
|
||||
|
||||
// Any other case whilst encrypting, we'll assume base-bytes.
|
||||
else {
|
||||
iobaseFormat = IOBASE_FORMAT::BASE_BYTES;
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Else, if we are decrypting,
|
||||
else if (activeModule == MODULE::DECRYPT) {
|
||||
// and input comes from a parameter, we'll assume base-16.
|
||||
if (inputFrom == INPUT_FROM::PARAMETER) {
|
||||
iobaseFormat = IOBASE_FORMAT::BASE_16;
|
||||
return;
|
||||
}
|
||||
// Any other case whilst decrypting, we'll assume base-bytes.
|
||||
else {
|
||||
iobaseFormat = IOBASE_FORMAT::BASE_BYTES;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Else, if we are generating a keyfile, iobase format is bytes.
|
||||
else if (activeModule == MODULE::GENERATE_KEYFILE) {
|
||||
iobaseFormat = IOBASE_FORMAT::BASE_BYTES;
|
||||
return;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
std::string Configuration::inputFilename;
|
||||
std::string Configuration::outputFilename;
|
||||
Configuration::MODULE Configuration::activeModule;
|
||||
Configuration::IOBASE_FORMAT Configuration::iobaseFormat;
|
||||
Configuration::INPUT_FROM Configuration::inputFrom;
|
||||
Configuration::OUTPUT_TO Configuration::outputTo;
|
||||
|
0
GCryptCLI/src/DataIngestionLayer.cpp
Normal file
0
GCryptCLI/src/DataIngestionLayer.cpp
Normal file
@ -1,420 +1,14 @@
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <cstring>
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
#include <GCrypt/Util.h>
|
||||
#include <GCrypt/Config.h>
|
||||
#include <GCrypt/Cipher.h>
|
||||
#include <GCrypt/Flexblock.h>
|
||||
#include <GCrypt/Block.h>
|
||||
#include "CommandlineInterface.h"
|
||||
#include "Configuration.h"
|
||||
|
||||
#include "Bases.h"
|
||||
#include <GeneralUtility/BaseConversion.h>
|
||||
#include <StringTools/StringTools.h>
|
||||
int main(int argc, char* const* argv) {
|
||||
|
||||
// Required for hidden password input
|
||||
#if defined _WIN32 || defined _WIN64
|
||||
#include <Windows.h>
|
||||
#elif defined __GNUG__
|
||||
#include <termios.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
// Init cli args
|
||||
CommandlineInterface::Init(argc, argv);
|
||||
|
||||
using namespace ::Leonetienne::GCrypt;
|
||||
using namespace ::Leonetienne::StringTools;
|
||||
using namespace ::Leonetienne::GeneralUtility;
|
||||
// Parse configuration
|
||||
Configuration::Parse();
|
||||
|
||||
enum class OPERATION_MODE {
|
||||
ENCRYPT,
|
||||
DECRYPT,
|
||||
HASH
|
||||
};
|
||||
|
||||
namespace {
|
||||
inline std::string Bin2CustomBase(const std::string& bin, const std::vector<std::string>& customSet, const std::string& seperator = "")
|
||||
{
|
||||
std::stringstream ss;
|
||||
|
||||
// Translate to custom set
|
||||
const std::vector<std::string> vec_base_custom =
|
||||
Leonetienne::GeneralUtility::BaseConversion::BaseX_2_Y<std::string, std::vector<std::string>>(bin, "01", customSet);
|
||||
|
||||
// Convert to string
|
||||
for (std::size_t i = 0; i < vec_base_custom.size(); i++)
|
||||
{
|
||||
ss << vec_base_custom[i];
|
||||
|
||||
if (i != vec_base_custom.size() - 1)
|
||||
ss << seperator;
|
||||
}
|
||||
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
inline std::string CustomBase2Bin(const std::string& in, const std::vector<std::string>& customSet, const std::string& seperator = "")
|
||||
{
|
||||
// Split input into symbols
|
||||
const std::vector<std::string> in_symbols = StringTools::Split(in, seperator);
|
||||
|
||||
// Translate to binary
|
||||
std::string binary =
|
||||
Leonetienne::GeneralUtility::BaseConversion::BaseX_2_Y<std::vector<std::string>, std::string>(in_symbols, customSet, std::string("01"));
|
||||
|
||||
// Apply padding to be a multiple of BLOCK_SIZE
|
||||
// Count how many bits we need
|
||||
std::size_t required_size = 0;
|
||||
while (required_size < binary.length())
|
||||
required_size += BLOCK_SIZE;
|
||||
|
||||
// Create a string of that many zeroes
|
||||
std::string padding = "";
|
||||
for (std::size_t i = binary.length(); i < required_size; i++)
|
||||
padding += "0";
|
||||
|
||||
// Prepend it to our bitstring
|
||||
binary = padding + binary;
|
||||
|
||||
return binary;
|
||||
}
|
||||
}
|
||||
|
||||
//! Will prompt a user password from stdin, hiding the input
|
||||
std::string PasswordPrompt()
|
||||
{
|
||||
// Disable stdin-echo
|
||||
#if defined _WIN32 || defined _WIN64
|
||||
HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
|
||||
DWORD mode = 0;
|
||||
GetConsoleMode(hStdin, &mode);
|
||||
SetConsoleMode(hStdin, mode & (~ENABLE_ECHO_INPUT));
|
||||
|
||||
#elif defined __GNUG__
|
||||
termios oldt;
|
||||
tcgetattr(STDIN_FILENO, &oldt);
|
||||
termios newt = oldt;
|
||||
newt.c_lflag &= ~ECHO;
|
||||
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
|
||||
|
||||
#endif
|
||||
|
||||
// Prompt stdin
|
||||
std::string key;
|
||||
std::cin >> key;
|
||||
|
||||
// Restore previous config
|
||||
#if defined _WIN32 || defined _WIN64
|
||||
SetConsoleMode(hStdin, mode);
|
||||
|
||||
#elif defined __GNUG__
|
||||
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
|
||||
|
||||
#endif
|
||||
|
||||
// Return input
|
||||
return key;
|
||||
}
|
||||
|
||||
Block GetEncryptionKey()
|
||||
{
|
||||
// Easy-case: key supplied as param
|
||||
if (CommandlineInterface::Get().HasParam("--key"))
|
||||
return PasswordToKey(CommandlineInterface::Get()["--key"].GetString());
|
||||
|
||||
// Case: Ask for key
|
||||
else if (CommandlineInterface::Get().HasParam("--keyask"))
|
||||
return PasswordToKey(PasswordPrompt());
|
||||
|
||||
// Case: Read key from file
|
||||
else if (CommandlineInterface::Get().HasParam("--keyfile"))
|
||||
{
|
||||
const std::string keyfilepath = CommandlineInterface::Get()["--keyfile"].GetString();
|
||||
|
||||
// Read this many chars
|
||||
const std::size_t maxChars = BLOCK_SIZE / 8;
|
||||
|
||||
// Open ifilestream for keyfile
|
||||
std::ifstream ifs(keyfilepath, std::ios::in | std::ios::binary);
|
||||
|
||||
// Is the file open now? Or were there any issues...
|
||||
if (!ifs.good())
|
||||
{
|
||||
std::cerr << "Unable to open ifilestream for keyfile \"" << keyfilepath << "\"! Aborting..." << std::endl;
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
// Read these chars to buffer
|
||||
char* ckeyfileContent = new char[maxChars];
|
||||
memset(ckeyfileContent, 0, maxChars * sizeof(char));
|
||||
ifs.read(ckeyfileContent, maxChars);
|
||||
ifs.close();
|
||||
|
||||
// Convert the buffer to a bit block of key size
|
||||
std::stringstream ss;
|
||||
for (std::size_t i = 0; i < maxChars; i++)
|
||||
ss << std::bitset<8>(ckeyfileContent[i]);
|
||||
|
||||
Block key(ss.str());
|
||||
|
||||
// And delete the buffer
|
||||
delete[] ckeyfileContent;
|
||||
ckeyfileContent = nullptr;
|
||||
|
||||
// Return it
|
||||
return key;
|
||||
}
|
||||
|
||||
// Unreachable
|
||||
throw std::runtime_error("This code should not have been reached. Most likely, the cli argument parser failed making sure at least one key method was supplied.");
|
||||
}
|
||||
|
||||
const Flexblock GetInputText(const OPERATION_MODE operationMode)
|
||||
{
|
||||
// Easy-case: input text supplied as param
|
||||
if (CommandlineInterface::Get().HasParam("--intext")) {
|
||||
switch (operationMode) {
|
||||
// Encryption, or hashing mode: We want to return the text as-is, as bits
|
||||
case OPERATION_MODE::ENCRYPT:
|
||||
case OPERATION_MODE::HASH:
|
||||
return StringToBits(CommandlineInterface::Get()["--intext"].GetString());
|
||||
|
||||
// Decryption mode: We need to first convert our input to a bitstring
|
||||
case OPERATION_MODE::DECRYPT:
|
||||
const std::string userInput = CommandlineInterface::Get()["--intext"].GetString();
|
||||
|
||||
// Are we using iobase 2?
|
||||
if (CommandlineInterface::Get().HasParam("--iobase-2"))
|
||||
// Yes: convert to binary from base 2
|
||||
return CustomBase2Bin(userInput, BASE_2);
|
||||
|
||||
// Are we using iobase 8?
|
||||
else if (CommandlineInterface::Get().HasParam("--iobase-8"))
|
||||
// Yes: convert to binary from base 8
|
||||
return CustomBase2Bin(userInput, BASE_8);
|
||||
|
||||
// Are we using iobase 10?
|
||||
else if (CommandlineInterface::Get().HasParam("--iobase-10"))
|
||||
// Yes: convert to binary from base 10
|
||||
return CustomBase2Bin(userInput, BASE_10);
|
||||
|
||||
// Are we using iobase 64?
|
||||
else if (CommandlineInterface::Get().HasParam("--iobase-64"))
|
||||
// Yes: convert to binary from base 64
|
||||
return CustomBase2Bin(userInput, BASE_64);
|
||||
|
||||
// Are we using iobase uwu?
|
||||
else if (CommandlineInterface::Get().HasParam("--iobase-uwu"))
|
||||
// Yes: convert to binary from base uwu
|
||||
return CustomBase2Bin(userInput, BASE_UWU, " ");
|
||||
|
||||
// Are we using iobase emoji?
|
||||
else if (CommandlineInterface::Get().HasParam("--iobase-ugh"))
|
||||
// Yes: convert to binary from base ugh
|
||||
return CustomBase2Bin(userInput, BASE_UGH, " ");
|
||||
|
||||
// Else, no special output format specified, use hex
|
||||
else
|
||||
return HexstringToBits(userInput);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Case: Read from file
|
||||
else if (CommandlineInterface::Get().HasParam("--infile"))
|
||||
return ReadFileToBits(CommandlineInterface::Get()["--infile"].GetString());
|
||||
|
||||
// Unreachable
|
||||
throw std::runtime_error("This code should not have been reached. Most likely, the cli argument parser failed making sure at least one input method was supplied.");
|
||||
}
|
||||
|
||||
const std::string GetOutfileName(const OPERATION_MODE operationMode)
|
||||
{
|
||||
// Do we have an output file name specified?
|
||||
// Use it.
|
||||
if (CommandlineInterface::Get().HasParam("--ofile"))
|
||||
return CommandlineInterface::Get()["--ofile"].GetString();
|
||||
|
||||
// Else: append a custom postfix to the inputs filename
|
||||
else
|
||||
return CommandlineInterface::Get()["--infile"].GetString() +
|
||||
(operationMode == OPERATION_MODE::ENCRYPT ? ".crypt" : ".plain");
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
// Init cmdargs
|
||||
CommandlineInterface::Init(argc, argv);
|
||||
|
||||
// Get operation modes
|
||||
OPERATION_MODE opMode;
|
||||
if (CommandlineInterface::Get().HasParam("--encrypt"))
|
||||
opMode = OPERATION_MODE::ENCRYPT;
|
||||
else if (CommandlineInterface::Get().HasParam("--hash"))
|
||||
opMode = OPERATION_MODE::HASH;
|
||||
else if (CommandlineInterface::Get().HasParam("--decrypt"))
|
||||
opMode = OPERATION_MODE::DECRYPT;
|
||||
else
|
||||
// Unreachable
|
||||
throw std::runtime_error("This code should not have been reached. Most likely, the cli argument parser failed making sure at least one mode of operation (-e, -d, -h) was supplied.");
|
||||
|
||||
const bool isFileMode = CommandlineInterface::Get().HasParam("--infile");
|
||||
const bool printDigestionProgress = CommandlineInterface::Get().HasParam("--progress");
|
||||
|
||||
|
||||
// Digest
|
||||
Flexblock output;
|
||||
|
||||
switch (opMode) {
|
||||
case OPERATION_MODE::ENCRYPT: {
|
||||
// Gather input and key
|
||||
const Block encryptionKey = GetEncryptionKey();
|
||||
const Flexblock input = GetInputText(opMode);
|
||||
|
||||
// Create cipher
|
||||
Cipher cipher(encryptionKey);
|
||||
|
||||
// Run it
|
||||
output = cipher.Encipher(input, printDigestionProgress);
|
||||
break;
|
||||
}
|
||||
|
||||
case OPERATION_MODE::DECRYPT: {
|
||||
// Gather input and key
|
||||
const Block encryptionKey = GetEncryptionKey();
|
||||
const Flexblock input = GetInputText(opMode);
|
||||
|
||||
// Create cipher
|
||||
Cipher cipher(encryptionKey);
|
||||
|
||||
// Run it
|
||||
output = cipher.Decipher(input, printDigestionProgress);
|
||||
break;
|
||||
}
|
||||
|
||||
case OPERATION_MODE::HASH: {
|
||||
// Gather input
|
||||
Flexblock input = GetInputText(opMode);
|
||||
|
||||
// Also add an extra left-padded block with the length of the input,
|
||||
// to make sure, H(n) != H(n + 0x0)
|
||||
input += PadStringToLength(Block(input.size()).to_string(), BLOCK_SIZE, '0');
|
||||
|
||||
// Derive an encryption key
|
||||
// This is quiet sensitive, because it absolutely MUST be impossible
|
||||
// to acquire K without already knowing M.
|
||||
// This alone is making this cipher a one-way function.
|
||||
// Just reducing the output is not a solution, because: If len(input) <= BLOCK_SIZE: H(m) = E(m)_k ^ k, with k being 0 <= k <= BLOCK_SIZE. That's not good.
|
||||
//
|
||||
// So here's what we're doing:
|
||||
// - We're going to take the first BLOCK_SIZE bits of the input (it's length is at least that because of the size embedding)
|
||||
// - We're creating a sudo-random initialization vector, seeded with this block.
|
||||
// - That's our encryption key.
|
||||
// This way, the encryption key can only be obtained by already knowing the first BLOCK_SIZE bits of the cleartext.
|
||||
// By not taking in ALL the bits for deriving a key, we're saving a LOT on performance, as otherwise we would basically be encrypting the input THREE times.
|
||||
// Why create an initialization vector at all? To make sure, even an input of only zeroes would provide a strong key
|
||||
const Block encryptionKey = InitializationVector(
|
||||
Block(input.substr(0, BLOCK_SIZE))
|
||||
);
|
||||
|
||||
// Create cipher
|
||||
Cipher cipher(encryptionKey);
|
||||
|
||||
// Run it
|
||||
output = cipher.Encipher(input, printDigestionProgress);
|
||||
|
||||
if (printDigestionProgress) {
|
||||
std::cout << "Done encrypting... Applying reduction function on ciphertext..." << std::endl;
|
||||
std::cout << "This will take about just as long..." << std::endl;
|
||||
}
|
||||
|
||||
// Reduce output to a single block
|
||||
output = ReductionFunction_Flexblock2Block(output).to_string();
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Now output the darn thing.
|
||||
// Outputting to a file (only if not hashing)
|
||||
if ((isFileMode) && (opMode != OPERATION_MODE::HASH))
|
||||
{
|
||||
// File mode is a bit different.
|
||||
|
||||
// Dump to stdout?
|
||||
if (CommandlineInterface::Get().HasParam("--ostdout"))
|
||||
{
|
||||
const std::string outstr = BitsToBytes(output);
|
||||
|
||||
// We have to print char-by-char to prevent a nullbyte terminating output.
|
||||
for (std::size_t i = 0; i < outstr.size(); i++)
|
||||
std::cout << outstr[i];
|
||||
}
|
||||
// Else: Dump to file
|
||||
else
|
||||
{
|
||||
const std::string outfileName = GetOutfileName(opMode);
|
||||
WriteBitsToFile(outfileName, output);
|
||||
}
|
||||
}
|
||||
// Else: we are just dealing with a string, or want to print a hashsum
|
||||
else
|
||||
{
|
||||
switch (opMode) {
|
||||
case OPERATION_MODE::ENCRYPT:
|
||||
case OPERATION_MODE::HASH: {
|
||||
|
||||
// We are encrypting or hashing:
|
||||
// Output to stdout as a formatted bytestring
|
||||
std::string formattedCiphertext;
|
||||
|
||||
// Are we using iobase 2?
|
||||
if (CommandlineInterface::Get().HasParam("--iobase-2"))
|
||||
// Yes: convert hex to base 2
|
||||
formattedCiphertext = Bin2CustomBase(output, BASE_2);
|
||||
|
||||
// Are we using iobase 8?
|
||||
else if (CommandlineInterface::Get().HasParam("--iobase-8"))
|
||||
// Yes: convert binary to base 8
|
||||
formattedCiphertext = Bin2CustomBase(output, BASE_8);
|
||||
|
||||
// Are we using iobase 10?
|
||||
else if (CommandlineInterface::Get().HasParam("--iobase-10"))
|
||||
// Yes: convert binary to base 10
|
||||
formattedCiphertext = Bin2CustomBase(output, BASE_10);
|
||||
|
||||
// Are we using iobase 64?
|
||||
else if (CommandlineInterface::Get().HasParam("--iobase-64"))
|
||||
// Yes: convert binary to base 64
|
||||
formattedCiphertext = Bin2CustomBase(output, BASE_64);
|
||||
|
||||
// Are we using iobase uwu?
|
||||
else if (CommandlineInterface::Get().HasParam("--iobase-uwu"))
|
||||
// Yes: convert binary to base 64
|
||||
formattedCiphertext = Bin2CustomBase(output, BASE_UWU, " ");
|
||||
|
||||
// Are we using iobase ugh?
|
||||
else if (CommandlineInterface::Get().HasParam("--iobase-ugh"))
|
||||
// Yes: convert binary to base 64
|
||||
formattedCiphertext = Bin2CustomBase(output, BASE_UGH, " ");
|
||||
|
||||
// Else, no special output format specified, use hex
|
||||
else
|
||||
formattedCiphertext = BitsToHexstring(output);
|
||||
|
||||
std::cout << formattedCiphertext << std::endl;
|
||||
break;
|
||||
}
|
||||
|
||||
case OPERATION_MODE::DECRYPT:
|
||||
// We are just decrypting:
|
||||
// Just print it string-formatted
|
||||
std::cout << BitsToString(output) << std::endl;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user