diff --git a/GCryptCLI/include/Configuration.h b/GCryptCLI/include/Configuration.h index 979cae6..73e4d95 100644 --- a/GCryptCLI/include/Configuration.h +++ b/GCryptCLI/include/Configuration.h @@ -34,7 +34,7 @@ class Configuration { ENCRYPT, DECRYPT, HASH, - GENERATE_KEYFILE + GENERATE_KEY } activeModule; //! Will analyze the supplied cli parameters, diff --git a/GCryptCLI/include/ModuleGenerateKey.h b/GCryptCLI/include/ModuleGenerateKey.h new file mode 100644 index 0000000..b9121d8 --- /dev/null +++ b/GCryptCLI/include/ModuleGenerateKey.h @@ -0,0 +1,15 @@ +#ifndef GCRYPTCLI_MODULE_GENERATEKEY_H +#define GCRYPTCLI_MODULE_GENERATEKEY_H + +namespace Module { + // This module just generates a key, and outputs it. + // Can be used to create a keyfiles. + class GenerateKey { + public: + //! Will write the key to a file + static void Run(); + }; +} + +#endif + diff --git a/GCryptCLI/include/ModuleGenerateKeyfile.h b/GCryptCLI/include/ModuleGenerateKeyfile.h deleted file mode 100644 index eaf5cef..0000000 --- a/GCryptCLI/include/ModuleGenerateKeyfile.h +++ /dev/null @@ -1,14 +0,0 @@ -#ifndef GCRYPTCLI_MODULE_GENERATEKEYFILE_H -#define GCRYPTCLI_MODULE_GENERATEKEYFILE_H - -namespace Module { - // This class has the task to prepare and supply the encryption key. - class GenerateKeyfile { - public: - //! Will write the key to a file - static void Run(); - }; -} - -#endif - diff --git a/GCryptCLI/src/CommandlineInterface.cpp b/GCryptCLI/src/CommandlineInterface.cpp index 0a17b39..2b1d6af 100644 --- a/GCryptCLI/src/CommandlineInterface.cpp +++ b/GCryptCLI/src/CommandlineInterface.cpp @@ -27,15 +27,15 @@ void CommandlineInterface::Init(int argc, const char* const* argv) { nupp.RegisterAbbreviation("-e", "--encrypt"); nupp.RegisterDescription("--decrypt", "Use decryption module."); - nupp.RegisterConstraint("--decrypt", ParamConstraint(true, DATA_TYPE::VOID, {}, false, { "--encrypt", "--hash", "--generate-keyfile" })); + nupp.RegisterConstraint("--decrypt", ParamConstraint(true, DATA_TYPE::VOID, {}, false, { "--encrypt", "--hash", "--generate-key" })); nupp.RegisterAbbreviation("-d", "--decrypt"); 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.RegisterConstraint("--hash", ParamConstraint(true, DATA_TYPE::VOID, {}, false, { "--encrypt", "--decrypt", "--generate-key" })); nupp.RegisterAbbreviation("-h", "--hash"); - 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("--generate-key", "Use the key generation module. Will generate a random key based on hardware events, output it, and exit."); + nupp.RegisterConstraint("--generate-key", ParamConstraint(true, DATA_TYPE::VOID, {}, false, { "--encrypt", "--decrypt", "--hash" })); nupp.RegisterDescription("--intext", "Encrypt this string."); nupp.RegisterConstraint("--intext", ParamConstraint(true, DATA_TYPE::STRING, {}, false, { "--infile" })); @@ -123,19 +123,19 @@ void CommandlineInterface::SpecialCompatibilityChecking() { // Active module // Do we have EITHER --encrypt or --decrypt or --hash? if ( - (!nupp.HasParam("--generate-keyfile")) && + (!nupp.HasParam("--generate-key")) && (!nupp.HasParam("--hash")) && (!nupp.HasParam("--encrypt")) && (!nupp.HasParam("--decrypt")) ) { - CrashWithMsg("No module supplied! Please supply either --encrypt, --decrypt, --hash, or --generate-keyfile!"); + CrashWithMsg("No module supplied! Please supply either --encrypt, --decrypt, --hash, or --generate-key!"); } // Encryption key - // Do we have EITHER --hash (no key required), --generate-keyfile (no key required), --key, --keyask or --keyfile given? + // Do we have EITHER --hash (no key required), --generate-key (no key required), --key, --keyask or --keyfile given? if ( (!nupp.HasParam("--hash")) && - (!nupp.HasParam("--generate-keyfile")) && + (!nupp.HasParam("--generate-key")) && (!nupp.HasParam("--key")) && (!nupp.HasParam("--keyfile")) && (!nupp.HasParam("--keyask")) @@ -165,13 +165,6 @@ void CommandlineInterface::SpecialCompatibilityChecking() { 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; } diff --git a/GCryptCLI/src/Configuration.cpp b/GCryptCLI/src/Configuration.cpp index e069503..c91c2f6 100644 --- a/GCryptCLI/src/Configuration.cpp +++ b/GCryptCLI/src/Configuration.cpp @@ -23,8 +23,8 @@ void Configuration::DecideModule() { activeModule = MODULE::HASH; return; } - else if (CommandlineInterface::Get().HasParam("--generate-keyfile")) { - activeModule = MODULE::GENERATE_KEYFILE; + else if (CommandlineInterface::Get().HasParam("--generate-key")) { + activeModule = MODULE::GENERATE_KEY; return; } @@ -51,13 +51,6 @@ void Configuration::DecideInputFrom() { 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(); @@ -71,7 +64,7 @@ void Configuration::DecideOutputTo() { void Configuration::DecideIOBaseFormat() { - // Do we have any iobase specified? + // Do we have any iobase explicitly specified? if (CommandlineInterface::Get().HasParam("--iobase-bytes")) { iobaseFormat = IOBASE_FORMAT::BASE_BYTES; return; @@ -113,7 +106,8 @@ void Configuration::DecideIOBaseFormat() { (activeModule == MODULE::HASH) ) { // and input comes from a parameter, - // and output goes to stdout, let's assume base-16. + // and output goes to stdout, + // let's assume base-16. if ( (inputFrom == INPUT_FROM::PARAMETER) && (outputTo == OUTPUT_TO::STDOUT) @@ -144,8 +138,13 @@ void Configuration::DecideIOBaseFormat() { } } - // Else, if we are generating a keyfile, iobase format is bytes. - else if (activeModule == MODULE::GENERATE_KEYFILE) { + // Else, if we are generating a key, + else if (activeModule == MODULE::GENERATE_KEY) { + // and we're outputting to stdout, we'll use base-16. + if (outputTo == OUTPUT_TO::STDOUT) { + iobaseFormat = IOBASE_FORMAT::BASE_16; + } + // else, we're outputting to a file, use base-bytes. iobaseFormat = IOBASE_FORMAT::BASE_BYTES; return; } diff --git a/GCryptCLI/src/KeyManager.cpp b/GCryptCLI/src/KeyManager.cpp index a904692..94b758a 100644 --- a/GCryptCLI/src/KeyManager.cpp +++ b/GCryptCLI/src/KeyManager.cpp @@ -21,8 +21,8 @@ void KeyManager::PrepareKey() { } // Special-case: We are generating a keyfile: - // just take a random one - else if (Configuration::activeModule == Configuration::MODULE::GENERATE_KEYFILE) { + // generate a random key from hardware events. + else if (Configuration::activeModule == Configuration::MODULE::GENERATE_KEY) { key = Key::Random(); return; } diff --git a/GCryptCLI/src/ModuleGenerateKey.cpp b/GCryptCLI/src/ModuleGenerateKey.cpp new file mode 100644 index 0000000..8d84d61 --- /dev/null +++ b/GCryptCLI/src/ModuleGenerateKey.cpp @@ -0,0 +1,15 @@ +#include "ModuleGenerateKey.h" + +#include "KeyManager.h" +#include "CommandlineInterface.h" + +using namespace Leonetienne::GCrypt; +using namespace Module; + +void GenerateKey::Run() { + + // Pass KeyManager::GetKey() to data output layer + + return; +} + diff --git a/GCryptCLI/src/ModuleGenerateKeyfile.cpp b/GCryptCLI/src/ModuleGenerateKeyfile.cpp deleted file mode 100644 index 2b4cd6d..0000000 --- a/GCryptCLI/src/ModuleGenerateKeyfile.cpp +++ /dev/null @@ -1,17 +0,0 @@ -#include "ModuleGenerateKeyfile.h" - -#include "KeyManager.h" -#include "CommandlineInterface.h" - -using namespace Leonetienne::GCrypt; -using namespace Module; - -void GenerateKeyfile::Run() { - - KeyManager::GetKey().WriteToFile( - CommandlineInterface::Get()["--generate-keyfile"].GetString() - ); - - return; -} - diff --git a/GCryptCLI/src/main.cpp b/GCryptCLI/src/main.cpp index 2d851b4..a3dd30d 100644 --- a/GCryptCLI/src/main.cpp +++ b/GCryptCLI/src/main.cpp @@ -1,7 +1,7 @@ #include "CommandlineInterface.h" #include "Configuration.h" #include "KeyManager.h" -#include "ModuleGenerateKeyfile.h" +#include "ModuleGenerateKey.h" int main(int argc, char* const* argv) { @@ -16,8 +16,8 @@ int main(int argc, char* const* argv) { // Launch our module switch (Configuration::activeModule) { - case Configuration::MODULE::GENERATE_KEYFILE: - Module::GenerateKeyfile::Run(); + case Configuration::MODULE::GENERATE_KEY: + Module::GenerateKey::Run(); } return 0;