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

View File

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

View File

@ -1,39 +1,39 @@
#pragma once #pragma once
#include "Feistel.h" #include "Feistel.h"
#include "Flexblock.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 to apply a block cipher to messages of arbitrary length in a distributed manner
*/ */
class Cipher { class Cipher {
public: public:
explicit Cipher(const Block& key); explicit Cipher(const Block& key);
explicit Cipher(const std::string& password); explicit Cipher(const std::string& password);
Cipher(const Cipher& other) = delete; Cipher(const Cipher& other) = delete;
Cipher(Cipher&& other) noexcept = delete; Cipher(Cipher&& other) noexcept = delete;
~Cipher(); ~Cipher();
//! Will set the key //! Will set the key
void SetKey(const Block& key); void SetKey(const Block& key);
//! Will set the key from a password //! Will set the key from a password
void SetPassword(const std::string& password); void SetPassword(const std::string& password);
//! Will encipher a flexblock of data //! Will encipher a flexblock of data
Flexblock Encipher(const Flexblock& data, bool printProgress = false) const; Flexblock Encipher(const Flexblock& data, bool printProgress = false) const;
//! Will decipher a flexblock of data //! Will decipher a flexblock of data
Flexblock Decipher(const Flexblock& data, bool printProgress = false) const; Flexblock Decipher(const Flexblock& data, bool printProgress = false) const;
private: private:
Block key; Block key;
//! Will zero the memory used by the key //! Will zero the memory used by the key
void ZeroKeyMemory(); void ZeroKeyMemory();
// Initial value for cipher block chaining // Initial value for cipher block chaining
Block initializationVector; Block initializationVector;
}; };
} }

View File

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

View File

@ -1,59 +1,59 @@
#pragma once #pragma once
#include "Keyset.h" #include "Keyset.h"
#include "Block.h" #include "Block.h"
#include "Halfblock.h" #include "Halfblock.h"
namespace GhettoCipher { namespace Leonetienne::GCrypt {
/** Class to perform a feistel block chipher /** Class to perform a feistel block chipher
*/ */
class Feistel { class Feistel {
public: public:
explicit Feistel(const Block& key); explicit Feistel(const Block& key);
Feistel(const Feistel& other) = delete; Feistel(const Feistel& other) = delete;
Feistel(Feistel&& other) noexcept = delete; Feistel(Feistel&& other) noexcept = delete;
~Feistel(); ~Feistel();
//! Will set the seed-key for this feistel network. //! Will set the seed-key for this feistel network.
//! Roundkeys will be derived from this. //! Roundkeys will be derived from this.
void SetKey(const Block& key); void SetKey(const Block& key);
//! Will encipher a data block via the set seed-key //! Will encipher a data block via the set seed-key
Block Encipher(const Block& data); Block Encipher(const Block& data);
//! Will decipher a data block via the set seed-key //! Will decipher a data block via the set seed-key
Block Decipher(const Block& data); Block Decipher(const Block& data);
private: private:
//! Will run the feistel rounds, with either regular key //! Will run the feistel rounds, with either regular key
//! order or reversed key order //! order or reversed key order
Block Run(const Block& data, bool reverseKeys); Block Run(const Block& data, bool reverseKeys);
//! Arbitrary cipher function //! Arbitrary cipher function
static Halfblock F(Halfblock m, const Block& key); static Halfblock F(Halfblock m, const Block& key);
//! Split a data block into two half blocks (into L and R) //! Split a data block into two half blocks (into L and R)
static std::pair<Halfblock, Halfblock> FeistelSplit(const Block& block); static std::pair<Halfblock, Halfblock> FeistelSplit(const Block& block);
//! Combine two half blocks (L and R) into a regular data block //! Combine two half blocks (L and R) into a regular data block
static Block FeistelCombine(const Halfblock& l, const Halfblock& r); static Block FeistelCombine(const Halfblock& l, const Halfblock& r);
//! Will expand a halfblock to a fullblock //! Will expand a halfblock to a fullblock
static Block ExpansionFunction(const Halfblock& block); static Block ExpansionFunction(const Halfblock& block);
//! Will compress a fullblock to a halfblock //! Will compress a fullblock to a halfblock
static Halfblock CompressionFunction(const Block& block); static Halfblock CompressionFunction(const Block& block);
//! Substitutes four bits by static random others //! Substitutes four bits by static random others
static std::string SBox(const std::string& in); static std::string SBox(const std::string& in);
//! Will generate a the round keys //! Will generate a the round keys
void GenerateRoundKeys(const Block& seedKey); void GenerateRoundKeys(const Block& seedKey);
//! Will zero the memory used by the keyset //! Will zero the memory used by the keyset
void ZeroKeyMemory(); void ZeroKeyMemory();
Keyset roundKeys; Keyset roundKeys;
}; };
} }

View File

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

View File

@ -1,32 +1,32 @@
#pragma once #pragma once
#include <string> #include <string>
namespace GhettoCipher { namespace Leonetienne::GCrypt {
/** This class is a wrapper to make working with the GhettoCipher /** This class is a wrapper to make working with the GhettoCipher
* super easy with a python-like syntax * super easy with a python-like syntax
*/ */
class GhettoCryptWrapper { class GhettoCryptWrapper {
public: public:
//! Will encrypt a string and return it hexadecimally encoded. //! Will encrypt a string and return it hexadecimally encoded.
static std::string EncryptString(const std::string& cleartext, const std::string& password); static std::string EncryptString(const std::string& cleartext, const std::string& password);
//! Will decrypt a hexadecimally encoded string. //! Will decrypt a hexadecimally encoded string.
static std::string DecryptString(const std::string& ciphertext, const std::string& password); static std::string DecryptString(const std::string& ciphertext, const std::string& password);
//! Will encrypt a file. //! Will encrypt a file.
//! Returns false if anything goes wrong (like, file-access). //! Returns false if anything goes wrong (like, file-access).
//! @filename_in The file to be read. //! @filename_in The file to be read.
//! @filename_out The file the encrypted version should be saved in. //! @filename_out The file the encrypted version should be saved in.
static bool EncryptFile(const std::string& filename_in, const std::string& filename_out, const std::string& password, bool printProgressReport = false); static bool EncryptFile(const std::string& filename_in, const std::string& filename_out, const std::string& password, bool printProgressReport = false);
//! Will decrypt a file. //! Will decrypt a file.
//! Returns false if anything goes wrong (like, file-access). //! Returns false if anything goes wrong (like, file-access).
//! @filename_in The file to be read. //! @filename_in The file to be read.
//! @filename_out The file the decrypted version should be saved in. //! @filename_out The file the decrypted version should be saved in.
static bool DecryptFile(const std::string& filename_in, const std::string& filename_out, const std::string& password, bool printProgressReport = false); static bool DecryptFile(const std::string& filename_in, const std::string& filename_out, const std::string& password, bool printProgressReport = false);
private: private:
// No instanciation! >:( // No instanciation! >:(
GhettoCryptWrapper(); GhettoCryptWrapper();
}; };
} }

View File

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

View File

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

View File

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

View File

@ -1,286 +1,286 @@
#pragma once #pragma once
#include <bitset> #include <bitset>
#include <ostream> #include <ostream>
#include <istream> #include <istream>
namespace GhettoCipher { namespace Leonetienne::GCrypt {
/** Wrapper for std::bitset<T> that zeroes memory upon deletion. /** Wrapper for std::bitset<T> that zeroes memory upon deletion.
* This does not include ALL methods, but the ones needed. * This does not include ALL methods, but the ones needed.
* *
* Just creating a specialization of std::bitset<T> does not work. * Just creating a specialization of std::bitset<T> does not work.
*/ */
template <std::size_t T> template <std::size_t T>
class SecureBitset { class SecureBitset {
public: public:
explicit SecureBitset(); explicit SecureBitset();
explicit SecureBitset(const std::string& str); explicit SecureBitset(const std::string& str);
explicit SecureBitset(const long long int i); explicit SecureBitset(const long long int i);
~SecureBitset(); ~SecureBitset();
bool operator==(const SecureBitset<T>& other) const; bool operator==(const SecureBitset<T>& other) const;
bool operator!=(const SecureBitset<T>& other) const; bool operator!=(const SecureBitset<T>& other) const;
bool operator[](const std::size_t) const; bool operator[](const std::size_t) const;
bool test(const std::size_t index) const; bool test(const std::size_t index) const;
bool all() const; bool all() const;
bool any() const; bool any() const;
bool none() const; bool none() const;
std::size_t count() const; std::size_t count() const;
std::size_t size() const; std::size_t size() const;
SecureBitset<T>& operator&=(const SecureBitset<T>& other); SecureBitset<T>& operator&=(const SecureBitset<T>& other);
SecureBitset<T>& operator|=(const SecureBitset<T>& other); SecureBitset<T>& operator|=(const SecureBitset<T>& other);
SecureBitset<T>& operator^=(const SecureBitset<T>& other); SecureBitset<T>& operator^=(const SecureBitset<T>& other);
SecureBitset<T> operator&(const SecureBitset<T>& other); SecureBitset<T> operator&(const SecureBitset<T>& other);
SecureBitset<T> operator|(const SecureBitset<T>& other); SecureBitset<T> operator|(const SecureBitset<T>& other);
SecureBitset<T> operator^(const SecureBitset<T>& other); SecureBitset<T> operator^(const SecureBitset<T>& other);
SecureBitset<T> operator~() const; SecureBitset<T> operator~() const;
SecureBitset<T>& operator<<=(const std::size_t offset); SecureBitset<T>& operator<<=(const std::size_t offset);
SecureBitset<T>& operator>>=(const std::size_t offset); SecureBitset<T>& operator>>=(const std::size_t offset);
SecureBitset<T> operator<<(const std::size_t offset) const; SecureBitset<T> operator<<(const std::size_t offset) const;
SecureBitset<T> operator>>(const std::size_t offset) const; SecureBitset<T> operator>>(const std::size_t offset) const;
SecureBitset<T>& set(); SecureBitset<T>& set();
SecureBitset<T>& set(const std::size_t index, bool value = true); SecureBitset<T>& set(const std::size_t index, bool value = true);
SecureBitset<T>& reset(); SecureBitset<T>& reset();
SecureBitset<T>& reset(const std::size_t index); SecureBitset<T>& reset(const std::size_t index);
SecureBitset<T>& flip(); SecureBitset<T>& flip();
SecureBitset<T>& flip(const std::size_t index); SecureBitset<T>& flip(const std::size_t index);
std::string to_string() const; std::string to_string() const;
unsigned long to_ulong() const; unsigned long to_ulong() const;
unsigned long long to_ullong() const; unsigned long long to_ullong() const;
std::bitset<T>& Get(); std::bitset<T>& Get();
const std::bitset<T>& Get() const; const std::bitset<T>& Get() const;
private: private:
std::bitset<T> bitset; std::bitset<T> bitset;
}; };
template<std::size_t T> template<std::size_t T>
inline SecureBitset<T>::SecureBitset() inline SecureBitset<T>::SecureBitset()
: :
bitset() { bitset() {
return; return;
} }
template<std::size_t T> template<std::size_t T>
inline SecureBitset<T>::SecureBitset(const std::string& str) inline SecureBitset<T>::SecureBitset(const std::string& str)
: :
bitset(str) { bitset(str) {
return; return;
} }
template<std::size_t T> template<std::size_t T>
inline SecureBitset<T>::SecureBitset(const long long int i) inline SecureBitset<T>::SecureBitset(const long long int i)
: :
bitset(i) { bitset(i) {
return; return;
} }
// Don't optimize the destructor out!!! // Don't optimize the destructor out!!!
// 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 #if defined _WIN32 || defined _WIN64
#pragma optimize("", off ) #pragma optimize("", off )
#elif defined __GNUG__ #elif defined __GNUG__
#pragma GCC push_options #pragma GCC push_options
#pragma GCC optimize ("O0") #pragma GCC optimize ("O0")
#endif #endif
template<std::size_t T> template<std::size_t T>
inline SecureBitset<T>::~SecureBitset() { inline SecureBitset<T>::~SecureBitset() {
bitset.reset(); bitset.reset();
return; return;
} }
#if defined _WIN32 || defined _WIN64 #if defined _WIN32 || defined _WIN64
#pragma optimize("", on ) #pragma optimize("", on )
#elif defined __GNUG__ #elif defined __GNUG__
#pragma GCC pop_options #pragma GCC pop_options
#endif #endif
template<std::size_t T> template<std::size_t T>
inline bool SecureBitset<T>::operator==(const SecureBitset<T>& other) const { inline bool SecureBitset<T>::operator==(const SecureBitset<T>& other) const {
return bitset == other.bitset; return bitset == other.bitset;
} }
template<std::size_t T> template<std::size_t T>
inline bool SecureBitset<T>::operator!=(const SecureBitset<T>& other) const { inline bool SecureBitset<T>::operator!=(const SecureBitset<T>& other) const {
return bitset != other.bitset; return bitset != other.bitset;
} }
template<std::size_t T> template<std::size_t T>
inline bool SecureBitset<T>::operator[](const std::size_t index) const { inline bool SecureBitset<T>::operator[](const std::size_t index) const {
return bitset[index]; return bitset[index];
} }
template<std::size_t T> template<std::size_t T>
inline bool SecureBitset<T>::test(const std::size_t index) const { inline bool SecureBitset<T>::test(const std::size_t index) const {
return bitset.test(index); return bitset.test(index);
} }
template<std::size_t T> template<std::size_t T>
inline bool SecureBitset<T>::all() const { inline bool SecureBitset<T>::all() const {
return bitset.all(); return bitset.all();
} }
template<std::size_t T> template<std::size_t T>
inline bool SecureBitset<T>::any() const { inline bool SecureBitset<T>::any() const {
return bitset.any(); return bitset.any();
} }
template<std::size_t T> template<std::size_t T>
inline bool SecureBitset<T>::none() const { inline bool SecureBitset<T>::none() const {
return bitset.none(); return bitset.none();
} }
template<std::size_t T> template<std::size_t T>
inline std::size_t SecureBitset<T>::count() const { inline std::size_t SecureBitset<T>::count() const {
return bitset.count(); return bitset.count();
} }
template<std::size_t T> template<std::size_t T>
inline std::size_t SecureBitset<T>::size() const { inline std::size_t SecureBitset<T>::size() const {
return bitset.count(); return bitset.count();
} }
template<std::size_t T> template<std::size_t T>
inline SecureBitset<T>& SecureBitset<T>::operator&=(const SecureBitset<T>& other) { inline SecureBitset<T>& SecureBitset<T>::operator&=(const SecureBitset<T>& other) {
bitset &= other.bitset; bitset &= other.bitset;
return *this; return *this;
} }
template<std::size_t T> template<std::size_t T>
inline SecureBitset<T>& SecureBitset<T>::operator|=(const SecureBitset<T>& other) { inline SecureBitset<T>& SecureBitset<T>::operator|=(const SecureBitset<T>& other) {
bitset |= other.bitset; bitset |= other.bitset;
return *this; return *this;
} }
template<std::size_t T> template<std::size_t T>
inline SecureBitset<T>& SecureBitset<T>::operator^=(const SecureBitset<T>& other) { inline SecureBitset<T>& SecureBitset<T>::operator^=(const SecureBitset<T>& other) {
bitset ^= other.bitset; bitset ^= other.bitset;
return *this; return *this;
} }
template<std::size_t T> template<std::size_t T>
inline SecureBitset<T> SecureBitset<T>::operator&(const SecureBitset<T>& other) { inline SecureBitset<T> SecureBitset<T>::operator&(const SecureBitset<T>& other) {
SecureBitset bs; SecureBitset bs;
bs.bitset = bitset & other.bitset; bs.bitset = bitset & other.bitset;
return bs; return bs;
} }
template<std::size_t T> template<std::size_t T>
inline SecureBitset<T> SecureBitset<T>::operator|(const SecureBitset<T>& other) { inline SecureBitset<T> SecureBitset<T>::operator|(const SecureBitset<T>& other) {
SecureBitset bs; SecureBitset bs;
bs.bitset = bitset | other.bitset; bs.bitset = bitset | other.bitset;
return bs; return bs;
} }
template<std::size_t T> template<std::size_t T>
inline SecureBitset<T> SecureBitset<T>::operator^(const SecureBitset<T>& other) { inline SecureBitset<T> SecureBitset<T>::operator^(const SecureBitset<T>& other) {
SecureBitset bs; SecureBitset bs;
bs.bitset = bitset ^ other.bitset; bs.bitset = bitset ^ other.bitset;
return bs; return bs;
} }
template<std::size_t T> template<std::size_t T>
inline SecureBitset<T> SecureBitset<T>::operator~() const { inline SecureBitset<T> SecureBitset<T>::operator~() const {
SecureBitset bs; SecureBitset bs;
bs.bitset = ~bitset; bs.bitset = ~bitset;
return bs; return bs;
} }
template<std::size_t T> template<std::size_t T>
inline SecureBitset<T>& SecureBitset<T>::operator<<=(const std::size_t offset) { inline SecureBitset<T>& SecureBitset<T>::operator<<=(const std::size_t offset) {
bitset <<= offset; bitset <<= offset;
return *this; return *this;
} }
template<std::size_t T> template<std::size_t T>
inline SecureBitset<T>& SecureBitset<T>::operator>>=(const std::size_t offset) { inline SecureBitset<T>& SecureBitset<T>::operator>>=(const std::size_t offset) {
bitset >>= offset; bitset >>= offset;
return *this; return *this;
} }
template<std::size_t T> template<std::size_t T>
inline SecureBitset<T> SecureBitset<T>::operator<<(const std::size_t offset) const { inline SecureBitset<T> SecureBitset<T>::operator<<(const std::size_t offset) const {
SecureBitset bs; SecureBitset bs;
bs.bitset = bitset << offset; bs.bitset = bitset << offset;
return bs; return bs;
} }
template<std::size_t T> template<std::size_t T>
inline SecureBitset<T> SecureBitset<T>::operator>>(const std::size_t offset) const { inline SecureBitset<T> SecureBitset<T>::operator>>(const std::size_t offset) const {
SecureBitset bs; SecureBitset bs;
bs.bitset = bitset >> offset; bs.bitset = bitset >> offset;
return bs; return bs;
} }
template<std::size_t T> template<std::size_t T>
inline SecureBitset<T>& SecureBitset<T>::set() { inline SecureBitset<T>& SecureBitset<T>::set() {
bitset.set(); bitset.set();
return *this; return *this;
} }
template<std::size_t T> template<std::size_t T>
inline SecureBitset<T>& SecureBitset<T>::set(const std::size_t index, bool value) { inline SecureBitset<T>& SecureBitset<T>::set(const std::size_t index, bool value) {
bitset.set(index, value); bitset.set(index, value);
return *this; return *this;
} }
template<std::size_t T> template<std::size_t T>
inline SecureBitset<T>& SecureBitset<T>::reset() { inline SecureBitset<T>& SecureBitset<T>::reset() {
bitset.reset(); bitset.reset();
return *this; return *this;
} }
template<std::size_t T> template<std::size_t T>
inline SecureBitset<T>& SecureBitset<T>::reset(const std::size_t index) { inline SecureBitset<T>& SecureBitset<T>::reset(const std::size_t index) {
bitset.reset(index); bitset.reset(index);
return *this; return *this;
} }
template<std::size_t T> template<std::size_t T>
inline SecureBitset<T>& SecureBitset<T>::flip() { inline SecureBitset<T>& SecureBitset<T>::flip() {
bitset.flip(); bitset.flip();
return *this; return *this;
} }
template<std::size_t T> template<std::size_t T>
inline SecureBitset<T>& SecureBitset<T>::flip(const std::size_t index) { inline SecureBitset<T>& SecureBitset<T>::flip(const std::size_t index) {
bitset.flip(index); bitset.flip(index);
return *this; return *this;
} }
template<std::size_t T> template<std::size_t T>
inline std::string SecureBitset<T>::to_string() const { inline std::string SecureBitset<T>::to_string() const {
return bitset.to_string(); return bitset.to_string();
} }
template<std::size_t T> template<std::size_t T>
inline unsigned long SecureBitset<T>::to_ulong() const { inline unsigned long SecureBitset<T>::to_ulong() const {
return bitset.to_ulong(); return bitset.to_ulong();
} }
template<std::size_t T> template<std::size_t T>
inline unsigned long long SecureBitset<T>::to_ullong() const { inline unsigned long long SecureBitset<T>::to_ullong() const {
return bitset.to_ullong(); return bitset.to_ullong();
} }
template<std::size_t T> template<std::size_t T>
inline std::bitset<T>& SecureBitset<T>::Get() { inline std::bitset<T>& SecureBitset<T>::Get() {
return bitset; return bitset;
} }
template<std::size_t T> template<std::size_t T>
inline const std::bitset<T>& SecureBitset<T>::Get() const { inline const std::bitset<T>& SecureBitset<T>::Get() const {
return bitset; return bitset;
} }
template <std::size_t T> template <std::size_t T>
inline std::ostream& operator<<(std::ostream& ofs, const SecureBitset<T>& bs) { inline std::ostream& operator<<(std::ostream& ofs, const SecureBitset<T>& bs) {
return ofs << bs.Get(); return ofs << bs.Get();
} }
template <std::size_t T> template <std::size_t T>
inline std::istream& operator>>(std::istream& ifs, const SecureBitset<T>& bs) { inline std::istream& operator>>(std::istream& ifs, const SecureBitset<T>& bs) {
return ifs >> bs.Get(); return ifs >> bs.Get();
} }
} }

View File

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

View File

@ -1,3 +1,3 @@
#pragma once #pragma once
#define GHETTOCRYPT_VERSION 0.21 #define GHETTOCRYPT_VERSION 0.21

View File

@ -1,131 +1,135 @@
#include <iostream> #include <iostream>
#include <vector> #include <vector>
#include "Cipher.h" #include "Cipher.h"
#include "Util.h" #include "Util.h"
#include "InitializationVector.h" #include "InitializationVector.h"
GhettoCipher::Cipher::Cipher(const Block& key) namespace Leonetienne::GCrypt {
:
key { key }, Cipher::Cipher(const Block& key)
initializationVector(InitializationVector(key)) { :
key { key },
return; initializationVector(InitializationVector(key)) {
}
return;
GhettoCipher::Cipher::Cipher(const std::string& password) }
:
key { PasswordToKey(password) }, Cipher::Cipher(const std::string& password)
initializationVector(InitializationVector(key)) { :
return; key { PasswordToKey(password) },
} initializationVector(InitializationVector(key)) {
return;
GhettoCipher::Cipher::~Cipher() { }
// Clear key memory
ZeroKeyMemory(); Cipher::~Cipher() {
// Clear key memory
return; ZeroKeyMemory();
}
return;
void GhettoCipher::Cipher::SetKey(const Block& key) { }
ZeroKeyMemory();
void Cipher::SetKey(const Block& key) {
this->key = key; ZeroKeyMemory();
return;
} this->key = key;
return;
void GhettoCipher::Cipher::SetPassword(const std::string& password) { }
ZeroKeyMemory();
void Cipher::SetPassword(const std::string& password) {
key = PasswordToKey(password); ZeroKeyMemory();
return;
} key = PasswordToKey(password);
return;
GhettoCipher::Flexblock GhettoCipher::Cipher::Encipher(const Flexblock& data, bool printProgress) const { }
// Split cleartext into blocks
std::vector<Block> blocks; Flexblock Cipher::Encipher(const Flexblock& data, bool printProgress) const {
// Split cleartext into blocks
for (std::size_t i = 0; i < data.size(); i += BLOCK_SIZE) { std::vector<Block> blocks;
blocks.push_back(Block(
PadStringToLength(data.substr(i, BLOCK_SIZE), BLOCK_SIZE, '0', false)) for (std::size_t i = 0; i < data.size(); i += BLOCK_SIZE) {
); blocks.push_back(Block(
} PadStringToLength(data.substr(i, BLOCK_SIZE), BLOCK_SIZE, '0', false))
);
// Encrypt individual blocks using cipher block chaining }
Feistel feistel(key);
// Encrypt individual blocks using cipher block chaining
for (std::size_t i = 0; i < blocks.size(); i++) { Feistel feistel(key);
// Print reports if desired. If we have > 1000 blocks, print one report every 100 blocks. Otherwise for every 10th block.
if ((i % ((blocks.size() > 1000)? 100 : 10) == 0) && (printProgress)) { for (std::size_t i = 0; i < blocks.size(); i++) {
std::cout << "Encrypting... (Block " << i << " / " << blocks.size() << " - " << ((float)i*100 / blocks.size()) << "%)" << std::endl; // Print reports if desired. If we have > 1000 blocks, print one report every 100 blocks. Otherwise for every 10th block.
} if ((i % ((blocks.size() > 1000)? 100 : 10) == 0) && (printProgress)) {
std::cout << "Encrypting... (Block " << i << " / " << blocks.size() << " - " << ((float)i*100 / blocks.size()) << "%)" << std::endl;
const Block& lastBlock = (i>0) ? blocks[i-1] : initializationVector; }
blocks[i] = feistel.Encipher(blocks[i] ^ lastBlock); // Xor last cipher block with new clear text block before E()
} const Block& lastBlock = (i>0) ? blocks[i-1] : initializationVector;
blocks[i] = feistel.Encipher(blocks[i] ^ lastBlock); // Xor last cipher block with new clear text block before E()
// Concatenate ciphertext blocks back into a flexblock }
std::stringstream ss;
for (Block& b : blocks) { // Concatenate ciphertext blocks back into a flexblock
ss << b; std::stringstream ss;
} for (Block& b : blocks) {
ss << b;
// Return it }
return ss.str();
} // Return it
return ss.str();
GhettoCipher::Flexblock GhettoCipher::Cipher::Decipher(const Flexblock& data, bool printProgress) const { }
// Split ciphertext into blocks
std::vector<Block> blocks; Flexblock Cipher::Decipher(const Flexblock& data, bool printProgress) const {
// Split ciphertext into blocks
for (std::size_t i = 0; i < data.size(); i += BLOCK_SIZE) { std::vector<Block> blocks;
blocks.push_back(Block(
PadStringToLength(data.substr(i, BLOCK_SIZE), BLOCK_SIZE, '0', false)) for (std::size_t i = 0; i < data.size(); i += BLOCK_SIZE) {
); blocks.push_back(Block(
} PadStringToLength(data.substr(i, BLOCK_SIZE), BLOCK_SIZE, '0', false))
);
// Decrypt individual blocks }
Feistel feistel(key);
// Decrypt individual blocks
// We can't do this in-loop for decryption, because we are decrypting the blocks in-place. Feistel feistel(key);
Block lastBlock = initializationVector;
// We can't do this in-loop for decryption, because we are decrypting the blocks in-place.
for (std::size_t i = 0; i < blocks.size(); i++) { Block lastBlock = initializationVector;
// Print reports if desired. If we have > 1000 blocks, print one report every 100 blocks. Otherwise for every 10th block.
if ((i % ((blocks.size() > 1000) ? 100 : 10) == 0) && (printProgress)) { for (std::size_t i = 0; i < blocks.size(); i++) {
std::cout << "Decrypting... (Block " << i << " / " << blocks.size() << " - " << ((float)i*100/ blocks.size()) << "%)" << std::endl; // Print reports if desired. If we have > 1000 blocks, print one report every 100 blocks. Otherwise for every 10th block.
} if ((i % ((blocks.size() > 1000) ? 100 : 10) == 0) && (printProgress)) {
std::cout << "Decrypting... (Block " << i << " / " << blocks.size() << " - " << ((float)i*100/ blocks.size()) << "%)" << std::endl;
Block tmpCopy = blocks[i]; }
blocks[i] = feistel.Decipher(blocks[i]) ^ lastBlock; // Decipher cipher block [i] and then xor it with the last cipher block [i-1] we've had Block tmpCopy = blocks[i];
lastBlock = std::move(tmpCopy); blocks[i] = feistel.Decipher(blocks[i]) ^ lastBlock; // Decipher cipher block [i] and then xor it with the last cipher block [i-1] we've had
}
lastBlock = std::move(tmpCopy);
// Concatenate ciphertext blocks back into a flexblock }
std::stringstream ss;
for (Block& b : blocks) { // Concatenate ciphertext blocks back into a flexblock
ss << b; std::stringstream ss;
} for (Block& b : blocks) {
ss << b;
// Return it }
return ss.str();
} // Return it
return ss.str();
// These pragmas only work for MSVC and g++, as far as i know. Beware!!! }
#if defined _WIN32 || defined _WIN64
#pragma optimize("", off ) // These pragmas only work for MSVC and g++, as far as i know. Beware!!!
#elif defined __GNUG__ #if defined _WIN32 || defined _WIN64
#pragma GCC push_options #pragma optimize("", off )
#pragma GCC optimize ("O0") #elif defined __GNUG__
#endif #pragma GCC push_options
void GhettoCipher::Cipher::ZeroKeyMemory() { #pragma GCC optimize ("O0")
key.reset(); #endif
return; void Cipher::ZeroKeyMemory() {
} key.reset();
#if defined _WIN32 || defined _WIN64 return;
#pragma optimize("", on ) }
#elif defined __GNUG__ #if defined _WIN32 || defined _WIN64
#pragma GCC pop_options #pragma optimize("", on )
#endif #elif defined __GNUG__
#pragma GCC pop_options
#endif
}

View File

@ -3,258 +3,262 @@
#include "Util.h" #include "Util.h"
#include "Config.h" #include "Config.h"
GhettoCipher::Feistel::Feistel(const Block& key) { namespace Leonetienne::GCrypt {
SetKey(key);
return;
}
GhettoCipher::Feistel::~Feistel() { Feistel::Feistel(const Block& key) {
ZeroKeyMemory(); SetKey(key);
return;
}
return; Feistel::~Feistel() {
} ZeroKeyMemory();
void GhettoCipher::Feistel::SetKey(const Block& key) { return;
GenerateRoundKeys(key); }
return;
}
GhettoCipher::Block GhettoCipher::Feistel::Encipher(const Block& data) { void Feistel::SetKey(const Block& key) {
return Run(data, false); GenerateRoundKeys(key);
} return;
}
GhettoCipher::Block GhettoCipher::Feistel::Decipher(const Block& data) { Block Feistel::Encipher(const Block& data) {
return Run(data, true); return Run(data, false);
} }
GhettoCipher::Block GhettoCipher::Feistel::Run(const Block& data, bool reverseKeys) { Block Feistel::Decipher(const Block& data) {
const auto splitData = FeistelSplit(data); return Run(data, true);
GhettoCipher::Halfblock l = splitData.first; }
GhettoCipher::Halfblock r = splitData.second;
Halfblock tmp; Block Feistel::Run(const Block& data, bool reverseKeys) {
const auto splitData = FeistelSplit(data);
Halfblock l = splitData.first;
Halfblock r = splitData.second;
for (std::size_t i = 0; i < N_ROUNDS; i++) { Halfblock tmp;
// Calculate key index
std::size_t keyIndex;
if (reverseKeys) {
keyIndex = N_ROUNDS - i - 1;
}
else {
keyIndex = i;
}
// Do a feistel round for (std::size_t i = 0; i < N_ROUNDS; i++) {
tmp = r; // Calculate key index
r = l ^ F(r, roundKeys[keyIndex]); std::size_t keyIndex;
l = tmp; if (reverseKeys) {
} keyIndex = N_ROUNDS - i - 1;
}
else {
keyIndex = i;
}
// Block has finished de*ciphering. // Do a feistel round
// Let's generate a new set of round keys. tmp = r;
GenerateRoundKeys((Block)roundKeys.back()); r = l ^ F(r, roundKeys[keyIndex]);
l = tmp;
}
return FeistelCombine(r, l); // Block has finished de*ciphering.
} // Let's generate a new set of round keys.
GenerateRoundKeys((Block)roundKeys.back());
GhettoCipher::Halfblock GhettoCipher::Feistel::F(Halfblock m, const Block& key) { return FeistelCombine(r, l);
// Made-up F function }
// Expand to full bitwidth Halfblock Feistel::F(Halfblock m, const Block& key) {
Block m_expanded = ExpansionFunction(m); // Made-up F function
// Shift to left by 1 // Expand to full bitwidth
m_expanded = Shiftl(m_expanded, 1); Block m_expanded = ExpansionFunction(m);
// Xor with key // Shift to left by 1
m_expanded ^= key; m_expanded = Shiftl(m_expanded, 1);
// Non-linearly apply subsitution boxes // Xor with key
std::stringstream ss; m_expanded ^= key;
const std::string m_str = m_expanded.to_string();
for (std::size_t i = 0; i < BLOCK_SIZE; i += 4) { // Non-linearly apply subsitution boxes
ss << SBox(m_str.substr(i, 4)); std::stringstream ss;
} const std::string m_str = m_expanded.to_string();
m_expanded = Block(ss.str()); for (std::size_t i = 0; i < BLOCK_SIZE; i += 4) {
ss << SBox(m_str.substr(i, 4));
}
// Return the compressed version m_expanded = Block(ss.str());
return CompressionFunction(m_expanded);
}
std::pair<GhettoCipher::Halfblock, GhettoCipher::Halfblock> GhettoCipher::Feistel::FeistelSplit(const Block& block) { // Return the compressed version
const std::string bits = block.to_string(); return CompressionFunction(m_expanded);
}
Halfblock l(bits.substr(0, bits.size() / 2)); std::pair<Halfblock, Halfblock> Feistel::FeistelSplit(const Block& block) {
Halfblock r(bits.substr(bits.size() / 2)); const std::string bits = block.to_string();
return std::make_pair(l, r); Halfblock l(bits.substr(0, bits.size() / 2));
} Halfblock r(bits.substr(bits.size() / 2));
GhettoCipher::Block GhettoCipher::Feistel::FeistelCombine(const Halfblock& l, const Halfblock& r) { return std::make_pair(l, r);
return Block(l.to_string() + r.to_string()); }
}
GhettoCipher::Block GhettoCipher::Feistel::ExpansionFunction(const Halfblock& block) { Block Feistel::FeistelCombine(const Halfblock& l, const Halfblock& r) {
std::stringstream ss; return Block(l.to_string() + r.to_string());
const std::string bits = block.to_string(); }
std::unordered_map<std::string, std::string> expansionMap; Block Feistel::ExpansionFunction(const Halfblock& block) {
expansionMap["00"] = "1101"; std::stringstream ss;
expansionMap["01"] = "1000"; const std::string bits = block.to_string();
expansionMap["10"] = "0010";
expansionMap["11"] = "0111";
// We have to double the bits! std::unordered_map<std::string, std::string> expansionMap;
for (std::size_t i = 0; i < HALFBLOCK_SIZE; i += 2) { expansionMap["00"] = "1101";
const std::string sub = bits.substr(i, 2); expansionMap["01"] = "1000";
ss << expansionMap[sub]; expansionMap["10"] = "0010";
} expansionMap["11"] = "0111";
return Block(ss.str()); // We have to double the bits!
} for (std::size_t i = 0; i < HALFBLOCK_SIZE; i += 2) {
const std::string sub = bits.substr(i, 2);
ss << expansionMap[sub];
}
GhettoCipher::Halfblock GhettoCipher::Feistel::CompressionFunction(const Block& block) { return Block(ss.str());
std::stringstream ss; }
const std::string bits = block.to_string();
std::unordered_map<std::string, std::string> compressionMap; Halfblock Feistel::CompressionFunction(const Block& block) {
compressionMap["0000"] = "10"; std::stringstream ss;
compressionMap["0001"] = "01"; const std::string bits = block.to_string();
compressionMap["0010"] = "10";
compressionMap["0011"] = "10";
compressionMap["0100"] = "11";
compressionMap["0101"] = "01";
compressionMap["0110"] = "00";
compressionMap["0111"] = "11";
compressionMap["1000"] = "01";
compressionMap["1001"] = "00";
compressionMap["1010"] = "11";
compressionMap["1011"] = "00";
compressionMap["1100"] = "11";
compressionMap["1101"] = "10";
compressionMap["1110"] = "00";
compressionMap["1111"] = "01";
// We have to half the bits! std::unordered_map<std::string, std::string> compressionMap;
for (std::size_t i = 0; i < BLOCK_SIZE; i += 4) { compressionMap["0000"] = "10";
const std::string sub = bits.substr(i, 4); compressionMap["0001"] = "01";
ss << compressionMap[sub]; compressionMap["0010"] = "10";
} compressionMap["0011"] = "10";
compressionMap["0100"] = "11";
compressionMap["0101"] = "01";
compressionMap["0110"] = "00";
compressionMap["0111"] = "11";
compressionMap["1000"] = "01";
compressionMap["1001"] = "00";
compressionMap["1010"] = "11";
compressionMap["1011"] = "00";
compressionMap["1100"] = "11";
compressionMap["1101"] = "10";
compressionMap["1110"] = "00";
compressionMap["1111"] = "01";
return Halfblock(ss.str()); // We have to half the bits!
} for (std::size_t i = 0; i < BLOCK_SIZE; i += 4) {
const std::string sub = bits.substr(i, 4);
ss << compressionMap[sub];
}
std::string GhettoCipher::Feistel::SBox(const std::string& in) { return Halfblock(ss.str());
static std::unordered_map<std::string, std::string> subMap; }
static bool mapInitialized = false;
if (!mapInitialized) {
subMap["0000"] = "1100";
subMap["0001"] = "1000";
subMap["0010"] = "0001";
subMap["0011"] = "0111";
subMap["0100"] = "1011";
subMap["0101"] = "0011";
subMap["0110"] = "1101";
subMap["0111"] = "1111";
subMap["1000"] = "0000";
subMap["1001"] = "1010";
subMap["1010"] = "0100";
subMap["1011"] = "1001";
subMap["1100"] = "0010";
subMap["1101"] = "1110";
subMap["1110"] = "0101";
subMap["1111"] = "0110";
mapInitialized = true;
}
return subMap[in]; std::string Feistel::SBox(const std::string& in) {
} static std::unordered_map<std::string, std::string> subMap;
static bool mapInitialized = false;
if (!mapInitialized) {
subMap["0000"] = "1100";
subMap["0001"] = "1000";
subMap["0010"] = "0001";
subMap["0011"] = "0111";
subMap["0100"] = "1011";
subMap["0101"] = "0011";
subMap["0110"] = "1101";
subMap["0111"] = "1111";
subMap["1000"] = "0000";
subMap["1001"] = "1010";
subMap["1010"] = "0100";
subMap["1011"] = "1001";
subMap["1100"] = "0010";
subMap["1101"] = "1110";
subMap["1110"] = "0101";
subMap["1111"] = "0110";
mapInitialized = true;
}
void GhettoCipher::Feistel::GenerateRoundKeys(const Block& seedKey) { return subMap[in];
// Clear initial key memory }
ZeroKeyMemory();
roundKeys = Keyset();
// Derive the initial two round keys void Feistel::GenerateRoundKeys(const Block& seedKey) {
// Clear initial key memory
ZeroKeyMemory();
roundKeys = Keyset();
// Compress- substitute, and expand the seed key to form the initial and the second-initial round key // Derive the initial two round keys
// This action is non-linear and irreversible, and thus strenghtens security.
Halfblock compressedSeed1 = CompressionFunction(seedKey);
Halfblock compressedSeed2 = CompressionFunction(Shiftl(seedKey, 1)); // Shifting one key by 1 will result in a completely different compression
// To add further confusion, let's shift seed1 by 1 aswell (after compression, but before substitution) // Compress- substitute, and expand the seed key to form the initial and the second-initial round key
// but only if the total number of bits set are a multiple of 3 // This action is non-linear and irreversible, and thus strenghtens security.
// if it is a multiple of 4, we'll shift it by 1 into the opposite direction Halfblock compressedSeed1 = CompressionFunction(seedKey);
const std::size_t setBits1 = compressedSeed1.count(); Halfblock compressedSeed2 = CompressionFunction(Shiftl(seedKey, 1)); // Shifting one key by 1 will result in a completely different compression
if (setBits1 % 4 == 0) { // To add further confusion, let's shift seed1 by 1 aswell (after compression, but before substitution)
compressedSeed1 = Shiftr(compressedSeed1, 1); // but only if the total number of bits set are a multiple of 3
} // if it is a multiple of 4, we'll shift it by 1 into the opposite direction
else if (setBits1 % 3 == 0) { const std::size_t setBits1 = compressedSeed1.count();
compressedSeed1 = Shiftl(compressedSeed1, 1);
}
// Now apply substitution if (setBits1 % 4 == 0) {
std::stringstream ssKey1; compressedSeed1 = Shiftr(compressedSeed1, 1);
std::stringstream ssKey2; }
const std::string bitsKey1 = compressedSeed1.to_string(); else if (setBits1 % 3 == 0) {
const std::string bitsKey2 = compressedSeed2.to_string(); compressedSeed1 = Shiftl(compressedSeed1, 1);
}
for (std::size_t i = 0; i < HALFBLOCK_SIZE; i += 4) { // Now apply substitution
ssKey1 << SBox(bitsKey1.substr(i, 4)); std::stringstream ssKey1;
ssKey2 << SBox(bitsKey2.substr(i, 4)); std::stringstream ssKey2;
} const std::string bitsKey1 = compressedSeed1.to_string();
const std::string bitsKey2 = compressedSeed2.to_string();
compressedSeed1 = Halfblock(ssKey1.str()); for (std::size_t i = 0; i < HALFBLOCK_SIZE; i += 4) {
compressedSeed2 = Halfblock(ssKey2.str()); ssKey1 << SBox(bitsKey1.substr(i, 4));
ssKey2 << SBox(bitsKey2.substr(i, 4));
}
// Now extrapolate them to BLOCK_SIZE (key size) again compressedSeed1 = Halfblock(ssKey1.str());
// Xor with the original seed key to get rid of the repititions caused by the expansion compressedSeed2 = Halfblock(ssKey2.str());
roundKeys[0] = ExpansionFunction(compressedSeed1) ^ seedKey;
roundKeys[1] = ExpansionFunction(compressedSeed2) ^ seedKey;
// Now derive all other round keys // Now extrapolate them to BLOCK_SIZE (key size) again
// Xor with the original seed key to get rid of the repititions caused by the expansion
roundKeys[0] = ExpansionFunction(compressedSeed1) ^ seedKey;
roundKeys[1] = ExpansionFunction(compressedSeed2) ^ seedKey;
for (std::size_t i = 2; i < roundKeys.size(); i++) { // Now derive all other round keys
// Initialize new round key with last round key
Block newKey = roundKeys[i - 1];
// Shift to left by how many bits are set, modulo 8 for (std::size_t i = 2; i < roundKeys.size(); i++) {
newKey = Shiftl(newKey, newKey.count() % 8); // This action is irreversible // Initialize new round key with last round key
Block newKey = roundKeys[i - 1];
// Split into two halfblocks, // Shift to left by how many bits are set, modulo 8
// apply F() to one halfblock with rk[i-2], newKey = Shiftl(newKey, newKey.count() % 8); // This action is irreversible
// xor the other one with it
// and put them back together
auto halfkeys = FeistelSplit(newKey);
Halfblock halfkey1 = F(halfkeys.first, roundKeys[i - 2]);
Halfblock halfkey2 = halfkeys.second ^ halfkey1; // I know this is reversible, but it helps to diffuse future round keys.
roundKeys[i] = FeistelCombine(halfkey1, halfkey2); // Split into two halfblocks,
} // apply F() to one halfblock with rk[i-2],
// xor the other one with it
// and put them back together
auto halfkeys = FeistelSplit(newKey);
Halfblock halfkey1 = F(halfkeys.first, roundKeys[i - 2]);
Halfblock halfkey2 = halfkeys.second ^ halfkey1; // I know this is reversible, but it helps to diffuse future round keys.
return; roundKeys[i] = FeistelCombine(halfkey1, halfkey2);
} }
// These pragmas only work for MSVC and g++, as far as i know. Beware!!! return;
}
// These pragmas only work for MSVC and g++, as far as i know. Beware!!!
#if defined _WIN32 || defined _WIN64 #if defined _WIN32 || defined _WIN64
#pragma optimize("", off ) #pragma optimize("", off )
#elif defined __GNUG__ #elif defined __GNUG__
#pragma GCC push_options #pragma GCC push_options
#pragma GCC optimize ("O0") #pragma GCC optimize ("O0")
#endif #endif
void GhettoCipher::Feistel::ZeroKeyMemory() { void Feistel::ZeroKeyMemory() {
for (Block& key : roundKeys) { for (Block& key : roundKeys) {
key.reset(); key.reset();
} }
return; return;
} }
#if defined _WIN32 || defined _WIN64 #if defined _WIN32 || defined _WIN64
#pragma optimize("", on ) #pragma optimize("", on )
#elif defined __GNUG__ #elif defined __GNUG__
#pragma GCC pop_options #pragma GCC pop_options
#endif #endif
}

View File

@ -1,84 +1,88 @@
#include "GhettoCryptWrapper.h" #include "GhettoCryptWrapper.h"
#include "Cipher.h" #include "Cipher.h"
#include "Util.h" #include "Util.h"
std::string GhettoCipher::GhettoCryptWrapper::EncryptString(const std::string& cleartext, const std::string& password) { namespace Leonetienne::GCrypt {
// Instanciate our cipher and supply a key
const Block key = PasswordToKey(password); std::string GhettoCryptWrapper::EncryptString(const std::string& cleartext, const std::string& password) {
Cipher cipher(key); // Instanciate our cipher and supply a key
const Block key = PasswordToKey(password);
// Recode the ascii-string to bits Cipher cipher(key);
const Flexblock cleartext_bits = StringToBits(cleartext);
// Recode the ascii-string to bits
// Encrypt our cleartext bits const Flexblock cleartext_bits = StringToBits(cleartext);
const Flexblock ciphertext_bits = cipher.Encipher(cleartext_bits);
// Encrypt our cleartext bits
// Recode the ciphertext bits to a hex-string const Flexblock ciphertext_bits = cipher.Encipher(cleartext_bits);
const std::string ciphertext = BitsToHexstring(ciphertext_bits);
// Recode the ciphertext bits to a hex-string
// Return it const std::string ciphertext = BitsToHexstring(ciphertext_bits);
return ciphertext;
} // Return it
return ciphertext;
std::string GhettoCipher::GhettoCryptWrapper::DecryptString(const std::string& ciphertext, const std::string& password) { }
// Instanciate our cipher and supply a key
const Block key = PasswordToKey(password); std::string GhettoCryptWrapper::DecryptString(const std::string& ciphertext, const std::string& password) {
Cipher cipher(key); // Instanciate our cipher and supply a key
const Block key = PasswordToKey(password);
// Recode the hex-string to bits Cipher cipher(key);
const Flexblock ciphertext_bits = HexstringToBits(ciphertext);
// Recode the hex-string to bits
// Decrypt the ciphertext bits const Flexblock ciphertext_bits = HexstringToBits(ciphertext);
const std::string cleartext_bits = cipher.Decipher(ciphertext_bits);
// Decrypt the ciphertext bits
// Recode the cleartext bits to an ascii-string const std::string cleartext_bits = cipher.Decipher(ciphertext_bits);
const std::string cleartext = BitsToString(cleartext_bits);
// Recode the cleartext bits to an ascii-string
// Return it const std::string cleartext = BitsToString(cleartext_bits);
return cleartext;
} // Return it
return cleartext;
bool GhettoCipher::GhettoCryptWrapper::EncryptFile(const std::string& filename_in, const std::string& filename_out, const std::string& password, bool printProgressReport) { }
try {
// Read the file to bits bool GhettoCryptWrapper::EncryptFile(const std::string& filename_in, const std::string& filename_out, const std::string& password, bool printProgressReport) {
const Flexblock cleartext_bits = ReadFileToBits(filename_in); try {
// Read the file to bits
// Instanciate our cipher and supply a key const Flexblock cleartext_bits = ReadFileToBits(filename_in);
const Block key = PasswordToKey(password);
Cipher cipher(key); // Instanciate our cipher and supply a key
const Block key = PasswordToKey(password);
// Encrypt our cleartext bits Cipher cipher(key);
const Flexblock ciphertext_bits = cipher.Encipher(cleartext_bits, printProgressReport);
// Encrypt our cleartext bits
// Write our ciphertext bits to file const Flexblock ciphertext_bits = cipher.Encipher(cleartext_bits, printProgressReport);
WriteBitsToFile(filename_out, ciphertext_bits);
// Write our ciphertext bits to file
return true; WriteBitsToFile(filename_out, ciphertext_bits);
}
catch (std::runtime_error&) { return true;
return false; }
} 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) { }
try {
// Read the file to bits bool GhettoCryptWrapper::DecryptFile(const std::string& filename_in, const std::string& filename_out, const std::string& password, bool printProgressReport) {
const Flexblock ciphertext_bits = ReadFileToBits(filename_in); try {
// Read the file to bits
// Instanciate our cipher and supply a key const Flexblock ciphertext_bits = ReadFileToBits(filename_in);
const Block key = PasswordToKey(password);
Cipher cipher(key); // Instanciate our cipher and supply a key
const Block key = PasswordToKey(password);
// Decrypt the ciphertext bits Cipher cipher(key);
const Flexblock cleartext_bits = cipher.Decipher(ciphertext_bits, printProgressReport);
// Decrypt the ciphertext bits
// Write our cleartext bits to file const Flexblock cleartext_bits = cipher.Decipher(ciphertext_bits, printProgressReport);
WriteBitsToFile(filename_out, cleartext_bits);
// Write our cleartext bits to file
return true; WriteBitsToFile(filename_out, cleartext_bits);
}
catch (std::runtime_error&) { return true;
return false; }
} catch (std::runtime_error&) {
} return false;
}
}
}

View File

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