Change module GenerateKeyfile to more versatile GenerateKey, which doesnt have to output to a file

This commit is contained in:
Leonetienne 2022-05-27 17:17:43 +02:00
parent 93be4d9cdc
commit b9d81d1425
No known key found for this signature in database
GPG Key ID: C33879CD92E9708C
9 changed files with 56 additions and 65 deletions

View File

@ -34,7 +34,7 @@ class Configuration {
ENCRYPT, ENCRYPT,
DECRYPT, DECRYPT,
HASH, HASH,
GENERATE_KEYFILE GENERATE_KEY
} activeModule; } activeModule;
//! Will analyze the supplied cli parameters, //! Will analyze the supplied cli parameters,

View File

@ -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

View File

@ -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

View File

@ -27,15 +27,15 @@ void CommandlineInterface::Init(int argc, const char* const* argv) {
nupp.RegisterAbbreviation("-e", "--encrypt"); nupp.RegisterAbbreviation("-e", "--encrypt");
nupp.RegisterDescription("--decrypt", "Use decryption module."); 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.RegisterAbbreviation("-d", "--decrypt");
nupp.RegisterDescription("--hash", "Use the GHash hash module 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.RegisterConstraint("--hash", ParamConstraint(true, DATA_TYPE::VOID, {}, false, { "--encrypt", "--decrypt", "--generate-key" }));
nupp.RegisterAbbreviation("-h", "--hash"); nupp.RegisterAbbreviation("-h", "--hash");
nupp.RegisterDescription("--generate-keyfile", "Use the Keyfile module. Will generate a random keyfile, and exit."); 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-keyfile", ParamConstraint(true, DATA_TYPE::STRING, {}, false, { "--encrypt", "--decrypt", "--hash" })); nupp.RegisterConstraint("--generate-key", ParamConstraint(true, DATA_TYPE::VOID, {}, false, { "--encrypt", "--decrypt", "--hash" }));
nupp.RegisterDescription("--intext", "Encrypt this string."); nupp.RegisterDescription("--intext", "Encrypt this string.");
nupp.RegisterConstraint("--intext", ParamConstraint(true, DATA_TYPE::STRING, {}, false, { "--infile" })); nupp.RegisterConstraint("--intext", ParamConstraint(true, DATA_TYPE::STRING, {}, false, { "--infile" }));
@ -123,19 +123,19 @@ void CommandlineInterface::SpecialCompatibilityChecking() {
// Active module // Active module
// Do we have EITHER --encrypt or --decrypt or --hash? // Do we have EITHER --encrypt or --decrypt or --hash?
if ( if (
(!nupp.HasParam("--generate-keyfile")) && (!nupp.HasParam("--generate-key")) &&
(!nupp.HasParam("--hash")) && (!nupp.HasParam("--hash")) &&
(!nupp.HasParam("--encrypt")) && (!nupp.HasParam("--encrypt")) &&
(!nupp.HasParam("--decrypt")) (!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 // 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 ( if (
(!nupp.HasParam("--hash")) && (!nupp.HasParam("--hash")) &&
(!nupp.HasParam("--generate-keyfile")) && (!nupp.HasParam("--generate-key")) &&
(!nupp.HasParam("--key")) && (!nupp.HasParam("--key")) &&
(!nupp.HasParam("--keyfile")) && (!nupp.HasParam("--keyfile")) &&
(!nupp.HasParam("--keyask")) (!nupp.HasParam("--keyask"))
@ -165,13 +165,6 @@ void CommandlineInterface::SpecialCompatibilityChecking() {
CrashWithMsg("Length of --keyfile is zero! That can't be a valid path!"); 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; return;
} }

View File

@ -23,8 +23,8 @@ void Configuration::DecideModule() {
activeModule = MODULE::HASH; activeModule = MODULE::HASH;
return; return;
} }
else if (CommandlineInterface::Get().HasParam("--generate-keyfile")) { else if (CommandlineInterface::Get().HasParam("--generate-key")) {
activeModule = MODULE::GENERATE_KEYFILE; activeModule = MODULE::GENERATE_KEY;
return; return;
} }
@ -51,13 +51,6 @@ void Configuration::DecideInputFrom() {
void Configuration::DecideOutputTo() { 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")) { if (CommandlineInterface::Get().HasParam("--ofile")) {
outputTo = OUTPUT_TO::FILE; outputTo = OUTPUT_TO::FILE;
outputFilename = CommandlineInterface::Get()["--ofile"].GetString(); outputFilename = CommandlineInterface::Get()["--ofile"].GetString();
@ -71,7 +64,7 @@ void Configuration::DecideOutputTo() {
void Configuration::DecideIOBaseFormat() { void Configuration::DecideIOBaseFormat() {
// Do we have any iobase specified? // Do we have any iobase explicitly specified?
if (CommandlineInterface::Get().HasParam("--iobase-bytes")) { if (CommandlineInterface::Get().HasParam("--iobase-bytes")) {
iobaseFormat = IOBASE_FORMAT::BASE_BYTES; iobaseFormat = IOBASE_FORMAT::BASE_BYTES;
return; return;
@ -113,7 +106,8 @@ void Configuration::DecideIOBaseFormat() {
(activeModule == MODULE::HASH) (activeModule == MODULE::HASH)
) { ) {
// and input comes from a parameter, // 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 ( if (
(inputFrom == INPUT_FROM::PARAMETER) && (inputFrom == INPUT_FROM::PARAMETER) &&
(outputTo == OUTPUT_TO::STDOUT) (outputTo == OUTPUT_TO::STDOUT)
@ -144,8 +138,13 @@ void Configuration::DecideIOBaseFormat() {
} }
} }
// Else, if we are generating a keyfile, iobase format is bytes. // Else, if we are generating a key,
else if (activeModule == MODULE::GENERATE_KEYFILE) { 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; iobaseFormat = IOBASE_FORMAT::BASE_BYTES;
return; return;
} }

View File

@ -21,8 +21,8 @@ void KeyManager::PrepareKey() {
} }
// Special-case: We are generating a keyfile: // Special-case: We are generating a keyfile:
// just take a random one // generate a random key from hardware events.
else if (Configuration::activeModule == Configuration::MODULE::GENERATE_KEYFILE) { else if (Configuration::activeModule == Configuration::MODULE::GENERATE_KEY) {
key = Key::Random(); key = Key::Random();
return; return;
} }

View File

@ -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;
}

View File

@ -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;
}

View File

@ -1,7 +1,7 @@
#include "CommandlineInterface.h" #include "CommandlineInterface.h"
#include "Configuration.h" #include "Configuration.h"
#include "KeyManager.h" #include "KeyManager.h"
#include "ModuleGenerateKeyfile.h" #include "ModuleGenerateKey.h"
int main(int argc, char* const* argv) { int main(int argc, char* const* argv) {
@ -16,8 +16,8 @@ int main(int argc, char* const* argv) {
// Launch our module // Launch our module
switch (Configuration::activeModule) { switch (Configuration::activeModule) {
case Configuration::MODULE::GENERATE_KEYFILE: case Configuration::MODULE::GENERATE_KEY:
Module::GenerateKeyfile::Run(); Module::GenerateKey::Run();
} }
return 0; return 0;