Gcryptlib: new brace style, and moved to Leonetienne namespace
This commit is contained in:
parent
c551f5fa64
commit
acf9dea387
@ -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;
|
||||
|
@ -2,7 +2,7 @@
|
||||
#include "SecureBitset.h"
|
||||
#include "Config.h"
|
||||
|
||||
namespace GhettoCipher {
|
||||
namespace Leonetienne::GCrypt {
|
||||
typedef SecureBitset<BLOCK_SIZE> Block;
|
||||
}
|
||||
|
||||
|
@ -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 {
|
||||
|
@ -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;
|
||||
|
||||
|
@ -3,7 +3,7 @@
|
||||
#include "Block.h"
|
||||
#include "Halfblock.h"
|
||||
|
||||
namespace GhettoCipher {
|
||||
namespace Leonetienne::GCrypt {
|
||||
/** Class to perform a feistel block chipher
|
||||
*/
|
||||
class Feistel {
|
||||
|
@ -1,7 +1,7 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
|
||||
namespace GhettoCipher {
|
||||
namespace Leonetienne::GCrypt {
|
||||
//! A "bitset" of variable length
|
||||
typedef std::string Flexblock;
|
||||
}
|
||||
|
@ -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
|
||||
*/
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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;
|
||||
};
|
||||
}
|
||||
|
@ -3,6 +3,6 @@
|
||||
#include "Block.h"
|
||||
#include "Config.h"
|
||||
|
||||
namespace GhettoCipher {
|
||||
namespace Leonetienne::GCrypt {
|
||||
typedef std::array<Block, N_ROUNDS> Keyset;
|
||||
}
|
||||
|
@ -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.
|
||||
*
|
||||
|
@ -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;
|
||||
|
@ -4,43 +4,45 @@
|
||||
#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)) {
|
||||
|
||||
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;
|
||||
|
||||
@ -71,9 +73,9 @@ GhettoCipher::Flexblock GhettoCipher::Cipher::Encipher(const Flexblock& data, bo
|
||||
|
||||
// Return it
|
||||
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;
|
||||
|
||||
@ -110,22 +112,24 @@ GhettoCipher::Flexblock GhettoCipher::Cipher::Decipher(const Flexblock& data, bo
|
||||
|
||||
// Return it
|
||||
return ss.str();
|
||||
}
|
||||
}
|
||||
|
||||
// These pragmas only work for MSVC and g++, as far as i know. Beware!!!
|
||||
// These pragmas only work for MSVC and g++, as far as i know. Beware!!!
|
||||
#if defined _WIN32 || defined _WIN64
|
||||
#pragma optimize("", off )
|
||||
#elif defined __GNUG__
|
||||
#pragma GCC push_options
|
||||
#pragma GCC optimize ("O0")
|
||||
#endif
|
||||
void GhettoCipher::Cipher::ZeroKeyMemory() {
|
||||
void Cipher::ZeroKeyMemory() {
|
||||
key.reset();
|
||||
return;
|
||||
}
|
||||
}
|
||||
#if defined _WIN32 || defined _WIN64
|
||||
#pragma optimize("", on )
|
||||
#elif defined __GNUG__
|
||||
#pragma GCC pop_options
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
|
||||
@ -55,9 +57,9 @@ GhettoCipher::Block GhettoCipher::Feistel::Run(const Block& data, bool reverseKe
|
||||
GenerateRoundKeys((Block)roundKeys.back());
|
||||
|
||||
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
|
||||
@ -81,22 +83,22 @@ GhettoCipher::Halfblock GhettoCipher::Feistel::F(Halfblock m, const Block& key)
|
||||
|
||||
// Return the compressed version
|
||||
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));
|
||||
Halfblock r(bits.substr(bits.size() / 2));
|
||||
|
||||
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();
|
||||
|
||||
@ -113,9 +115,9 @@ 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();
|
||||
|
||||
@ -144,9 +146,9 @@ 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) {
|
||||
@ -170,9 +172,9 @@ 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();
|
||||
@ -236,25 +238,27 @@ void GhettoCipher::Feistel::GenerateRoundKeys(const Block& seedKey) {
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// These pragmas only work for MSVC and g++, as far as i know. Beware!!!
|
||||
// These pragmas only work for MSVC and g++, as far as i know. Beware!!!
|
||||
#if defined _WIN32 || defined _WIN64
|
||||
#pragma optimize("", off )
|
||||
#elif defined __GNUG__
|
||||
#pragma GCC push_options
|
||||
#pragma GCC optimize ("O0")
|
||||
#endif
|
||||
void GhettoCipher::Feistel::ZeroKeyMemory() {
|
||||
void Feistel::ZeroKeyMemory() {
|
||||
for (Block& key : roundKeys) {
|
||||
key.reset();
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
#if defined _WIN32 || defined _WIN64
|
||||
#pragma optimize("", on )
|
||||
#elif defined __GNUG__
|
||||
#pragma GCC pop_options
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
@ -18,9 +20,9 @@ std::string GhettoCipher::GhettoCryptWrapper::EncryptString(const std::string& c
|
||||
|
||||
// Return it
|
||||
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);
|
||||
@ -36,9 +38,9 @@ std::string GhettoCipher::GhettoCryptWrapper::DecryptString(const std::string& c
|
||||
|
||||
// Return it
|
||||
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);
|
||||
@ -58,9 +60,9 @@ bool GhettoCipher::GhettoCryptWrapper::EncryptFile(const std::string& filename_i
|
||||
catch (std::runtime_error&) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
@ -80,5 +82,7 @@ bool GhettoCipher::GhettoCryptWrapper::DecryptFile(const std::string& filename_i
|
||||
catch (std::runtime_error&) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user