Gcryptlib: new brace style, and moved to Leonetienne namespace

This commit is contained in:
Leonetienne 2022-05-16 22:15:34 +02:00
parent c551f5fa64
commit acf9dea387
No known key found for this signature in database
GPG Key ID: C33879CD92E9708C
17 changed files with 917 additions and 901 deletions

View File

@ -4,7 +4,7 @@
#include <Util.h>
#include <InitializationVector.h>
using namespace GhettoCipher;
using namespace Leonetienne::GCrypt;
void ExampleString() {
std::cout << "Example on how to encrypt & decrypt a string:" << std::endl;

View File

@ -2,7 +2,7 @@
#include "SecureBitset.h"
#include "Config.h"
namespace GhettoCipher {
namespace Leonetienne::GCrypt {
typedef SecureBitset<BLOCK_SIZE> Block;
}

View File

@ -2,7 +2,7 @@
#include "Feistel.h"
#include "Flexblock.h"
namespace GhettoCipher {
namespace Leonetienne::GCrypt {
/** Class to apply a block cipher to messages of arbitrary length in a distributed manner
*/
class Cipher {

View File

@ -1,7 +1,7 @@
#pragma once
#include <cstddef>
namespace GhettoCipher {
namespace Leonetienne::GCrypt {
// MUST BE A POWER OF 2 > 4
constexpr std::size_t BLOCK_SIZE = 512;

View File

@ -3,7 +3,7 @@
#include "Block.h"
#include "Halfblock.h"
namespace GhettoCipher {
namespace Leonetienne::GCrypt {
/** Class to perform a feistel block chipher
*/
class Feistel {

View File

@ -1,7 +1,7 @@
#pragma once
#include <string>
namespace GhettoCipher {
namespace Leonetienne::GCrypt {
//! A "bitset" of variable length
typedef std::string Flexblock;
}

View File

@ -1,7 +1,7 @@
#pragma once
#include <string>
namespace GhettoCipher {
namespace Leonetienne::GCrypt {
/** This class is a wrapper to make working with the GhettoCipher
* super easy with a python-like syntax
*/

View File

@ -3,7 +3,7 @@
#include <cstdint>
#include "Config.h"
namespace GhettoCipher {
namespace Leonetienne::GCrypt {
constexpr std::size_t HALFBLOCK_SIZE = (BLOCK_SIZE / 2);
typedef SecureBitset<HALFBLOCK_SIZE> Halfblock;
}

View File

@ -2,16 +2,16 @@
#include "Config.h"
#include "Block.h"
namespace GhettoCipher {
namespace Leonetienne::GCrypt {
/** Will create a sudo-random Block based on a seed
*/
class InitializationVector {
public:
InitializationVector(const GhettoCipher::Block& seed);
InitializationVector(const Block& seed);
operator GhettoCipher::Block() const;
operator Block() const;
private:
GhettoCipher::Block iv;
Block iv;
};
}

View File

@ -3,6 +3,6 @@
#include "Block.h"
#include "Config.h"
namespace GhettoCipher {
namespace Leonetienne::GCrypt {
typedef std::array<Block, N_ROUNDS> Keyset;
}

View File

@ -3,7 +3,7 @@
#include <ostream>
#include <istream>
namespace GhettoCipher {
namespace Leonetienne::GCrypt {
/** Wrapper for std::bitset<T> that zeroes memory upon deletion.
* This does not include ALL methods, but the ones needed.
*

View File

@ -10,7 +10,7 @@
#include "Cipher.h"
#include "InitializationVector.h"
namespace GhettoCipher {
namespace Leonetienne::GCrypt {
//! Mod-operator that works with negative values
inline int Mod(const int numerator, const int denominator) {
return (denominator + (numerator % denominator)) % denominator;

View File

@ -4,7 +4,9 @@
#include "Util.h"
#include "InitializationVector.h"
GhettoCipher::Cipher::Cipher(const Block& key)
namespace Leonetienne::GCrypt {
Cipher::Cipher(const Block& key)
:
key { key },
initializationVector(InitializationVector(key)) {
@ -12,35 +14,35 @@ GhettoCipher::Cipher::Cipher(const Block& key)
return;
}
GhettoCipher::Cipher::Cipher(const std::string& password)
Cipher::Cipher(const std::string& password)
:
key { PasswordToKey(password) },
initializationVector(InitializationVector(key)) {
return;
}
GhettoCipher::Cipher::~Cipher() {
Cipher::~Cipher() {
// Clear key memory
ZeroKeyMemory();
return;
}
void GhettoCipher::Cipher::SetKey(const Block& key) {
void Cipher::SetKey(const Block& key) {
ZeroKeyMemory();
this->key = key;
return;
}
void GhettoCipher::Cipher::SetPassword(const std::string& password) {
void Cipher::SetPassword(const std::string& password) {
ZeroKeyMemory();
key = PasswordToKey(password);
return;
}
GhettoCipher::Flexblock GhettoCipher::Cipher::Encipher(const Flexblock& data, bool printProgress) const {
Flexblock Cipher::Encipher(const Flexblock& data, bool printProgress) const {
// Split cleartext into blocks
std::vector<Block> blocks;
@ -73,7 +75,7 @@ GhettoCipher::Flexblock GhettoCipher::Cipher::Encipher(const Flexblock& data, bo
return ss.str();
}
GhettoCipher::Flexblock GhettoCipher::Cipher::Decipher(const Flexblock& data, bool printProgress) const {
Flexblock Cipher::Decipher(const Flexblock& data, bool printProgress) const {
// Split ciphertext into blocks
std::vector<Block> blocks;
@ -119,7 +121,7 @@ GhettoCipher::Flexblock GhettoCipher::Cipher::Decipher(const Flexblock& data, bo
#pragma GCC push_options
#pragma GCC optimize ("O0")
#endif
void GhettoCipher::Cipher::ZeroKeyMemory() {
void Cipher::ZeroKeyMemory() {
key.reset();
return;
}
@ -129,3 +131,5 @@ void GhettoCipher::Cipher::ZeroKeyMemory() {
#pragma GCC pop_options
#endif
}

View File

@ -3,34 +3,36 @@
#include "Util.h"
#include "Config.h"
GhettoCipher::Feistel::Feistel(const Block& key) {
namespace Leonetienne::GCrypt {
Feistel::Feistel(const Block& key) {
SetKey(key);
return;
}
GhettoCipher::Feistel::~Feistel() {
Feistel::~Feistel() {
ZeroKeyMemory();
return;
}
void GhettoCipher::Feistel::SetKey(const Block& key) {
void Feistel::SetKey(const Block& key) {
GenerateRoundKeys(key);
return;
}
GhettoCipher::Block GhettoCipher::Feistel::Encipher(const Block& data) {
Block Feistel::Encipher(const Block& data) {
return Run(data, false);
}
GhettoCipher::Block GhettoCipher::Feistel::Decipher(const Block& data) {
Block Feistel::Decipher(const Block& data) {
return Run(data, true);
}
GhettoCipher::Block GhettoCipher::Feistel::Run(const Block& data, bool reverseKeys) {
Block Feistel::Run(const Block& data, bool reverseKeys) {
const auto splitData = FeistelSplit(data);
GhettoCipher::Halfblock l = splitData.first;
GhettoCipher::Halfblock r = splitData.second;
Halfblock l = splitData.first;
Halfblock r = splitData.second;
Halfblock tmp;
@ -57,7 +59,7 @@ GhettoCipher::Block GhettoCipher::Feistel::Run(const Block& data, bool reverseKe
return FeistelCombine(r, l);
}
GhettoCipher::Halfblock GhettoCipher::Feistel::F(Halfblock m, const Block& key) {
Halfblock Feistel::F(Halfblock m, const Block& key) {
// Made-up F function
// Expand to full bitwidth
@ -83,7 +85,7 @@ GhettoCipher::Halfblock GhettoCipher::Feistel::F(Halfblock m, const Block& key)
return CompressionFunction(m_expanded);
}
std::pair<GhettoCipher::Halfblock, GhettoCipher::Halfblock> GhettoCipher::Feistel::FeistelSplit(const Block& block) {
std::pair<Halfblock, Halfblock> Feistel::FeistelSplit(const Block& block) {
const std::string bits = block.to_string();
Halfblock l(bits.substr(0, bits.size() / 2));
@ -92,11 +94,11 @@ std::pair<GhettoCipher::Halfblock, GhettoCipher::Halfblock> GhettoCipher::Feiste
return std::make_pair(l, r);
}
GhettoCipher::Block GhettoCipher::Feistel::FeistelCombine(const Halfblock& l, const Halfblock& r) {
Block Feistel::FeistelCombine(const Halfblock& l, const Halfblock& r) {
return Block(l.to_string() + r.to_string());
}
GhettoCipher::Block GhettoCipher::Feistel::ExpansionFunction(const Halfblock& block) {
Block Feistel::ExpansionFunction(const Halfblock& block) {
std::stringstream ss;
const std::string bits = block.to_string();
@ -115,7 +117,7 @@ GhettoCipher::Block GhettoCipher::Feistel::ExpansionFunction(const Halfblock& bl
return Block(ss.str());
}
GhettoCipher::Halfblock GhettoCipher::Feistel::CompressionFunction(const Block& block) {
Halfblock Feistel::CompressionFunction(const Block& block) {
std::stringstream ss;
const std::string bits = block.to_string();
@ -146,7 +148,7 @@ GhettoCipher::Halfblock GhettoCipher::Feistel::CompressionFunction(const Block&
return Halfblock(ss.str());
}
std::string GhettoCipher::Feistel::SBox(const std::string& in) {
std::string Feistel::SBox(const std::string& in) {
static std::unordered_map<std::string, std::string> subMap;
static bool mapInitialized = false;
if (!mapInitialized) {
@ -172,7 +174,7 @@ std::string GhettoCipher::Feistel::SBox(const std::string& in) {
return subMap[in];
}
void GhettoCipher::Feistel::GenerateRoundKeys(const Block& seedKey) {
void Feistel::GenerateRoundKeys(const Block& seedKey) {
// Clear initial key memory
ZeroKeyMemory();
roundKeys = Keyset();
@ -245,7 +247,7 @@ void GhettoCipher::Feistel::GenerateRoundKeys(const Block& seedKey) {
#pragma GCC push_options
#pragma GCC optimize ("O0")
#endif
void GhettoCipher::Feistel::ZeroKeyMemory() {
void Feistel::ZeroKeyMemory() {
for (Block& key : roundKeys) {
key.reset();
}
@ -258,3 +260,5 @@ void GhettoCipher::Feistel::ZeroKeyMemory() {
#pragma GCC pop_options
#endif
}

View File

@ -2,7 +2,9 @@
#include "Cipher.h"
#include "Util.h"
std::string GhettoCipher::GhettoCryptWrapper::EncryptString(const std::string& cleartext, const std::string& password) {
namespace Leonetienne::GCrypt {
std::string GhettoCryptWrapper::EncryptString(const std::string& cleartext, const std::string& password) {
// Instanciate our cipher and supply a key
const Block key = PasswordToKey(password);
Cipher cipher(key);
@ -20,7 +22,7 @@ std::string GhettoCipher::GhettoCryptWrapper::EncryptString(const std::string& c
return ciphertext;
}
std::string GhettoCipher::GhettoCryptWrapper::DecryptString(const std::string& ciphertext, const std::string& password) {
std::string GhettoCryptWrapper::DecryptString(const std::string& ciphertext, const std::string& password) {
// Instanciate our cipher and supply a key
const Block key = PasswordToKey(password);
Cipher cipher(key);
@ -38,7 +40,7 @@ std::string GhettoCipher::GhettoCryptWrapper::DecryptString(const std::string& c
return cleartext;
}
bool GhettoCipher::GhettoCryptWrapper::EncryptFile(const std::string& filename_in, const std::string& filename_out, const std::string& password, bool printProgressReport) {
bool GhettoCryptWrapper::EncryptFile(const std::string& filename_in, const std::string& filename_out, const std::string& password, bool printProgressReport) {
try {
// Read the file to bits
const Flexblock cleartext_bits = ReadFileToBits(filename_in);
@ -60,7 +62,7 @@ bool GhettoCipher::GhettoCryptWrapper::EncryptFile(const std::string& filename_i
}
}
bool GhettoCipher::GhettoCryptWrapper::DecryptFile(const std::string& filename_in, const std::string& filename_out, const std::string& password, bool printProgressReport) {
bool GhettoCryptWrapper::DecryptFile(const std::string& filename_in, const std::string& filename_out, const std::string& password, bool printProgressReport) {
try {
// Read the file to bits
const Flexblock ciphertext_bits = ReadFileToBits(filename_in);
@ -82,3 +84,5 @@ bool GhettoCipher::GhettoCryptWrapper::DecryptFile(const std::string& filename_i
}
}
}

View File

@ -1,13 +1,17 @@
#include "InitializationVector.h"
#include "Feistel.h"
GhettoCipher::InitializationVector::InitializationVector(const Block& seed) {
namespace Leonetienne::GCrypt {
InitializationVector::InitializationVector(const Block& seed) {
// We'll generate our initialization vector by encrypting our seed with itself as a key
// iv = E(M=seed, K=seed)
iv = Feistel(seed).Encipher(seed);
}
GhettoCipher::InitializationVector::operator GhettoCipher::Block() const {
InitializationVector::operator Block() const {
return iv;
}
}