From c551f5fa64f301f9347209f3eab327fa2842eca7 Mon Sep 17 00:00:00 2001 From: Leonetienne Date: Mon, 16 May 2022 22:01:52 +0200 Subject: [PATCH] retabbed gcryptlib --- GhettoCrypt/exec/main.cpp | 53 ++- GhettoCrypt/include/Block.h | 6 +- GhettoCrypt/include/Cipher.h | 1 - GhettoCrypt/include/Config.h | 1 - GhettoCrypt/include/Feistel.h | 77 ++-- GhettoCrypt/include/Flexblock.h | 7 +- GhettoCrypt/include/GhettoCryptWrapper.h | 49 +- GhettoCrypt/include/Halfblock.h | 7 +- GhettoCrypt/include/InitializationVector.h | 22 +- GhettoCrypt/include/Keyset.h | 5 +- GhettoCrypt/include/SecureBitset.h | 492 ++++++++++----------- GhettoCrypt/include/Util.h | 140 +++--- GhettoCrypt/include/Version.h | 1 + GhettoCrypt/src/Cipher.cpp | 161 ++++--- GhettoCrypt/src/Feistel.cpp | 80 ++-- GhettoCrypt/src/GhettoCryptWrapper.cpp | 121 +++-- GhettoCrypt/src/InitializationVector.cpp | 15 +- 17 files changed, 576 insertions(+), 662 deletions(-) diff --git a/GhettoCrypt/exec/main.cpp b/GhettoCrypt/exec/main.cpp index 6c735fa..0ec5d82 100644 --- a/GhettoCrypt/exec/main.cpp +++ b/GhettoCrypt/exec/main.cpp @@ -1,4 +1,3 @@ -#pragma once #include #include #include @@ -7,42 +6,40 @@ using namespace GhettoCipher; -void ExampleString() -{ - std::cout << "Example on how to encrypt & decrypt a string:" << std::endl; +void ExampleString() { + std::cout << "Example on how to encrypt & decrypt a string:" << std::endl; - // Get some string - const std::string input = "I am a super secret message!"; - std::cout << input << std::endl; + // Get some string + const std::string input = "I am a super secret message!"; + std::cout << input << std::endl; - // Encrypt - const std::string encrypted = GhettoCryptWrapper::EncryptString(input, "password1"); - std::cout << encrypted << std::endl; - - // Decrypt - const std::string decrypted = GhettoCryptWrapper::DecryptString(encrypted, "password1"); - std::cout << decrypted << std::endl; + // Encrypt + const std::string encrypted = GhettoCryptWrapper::EncryptString(input, "password1"); + std::cout << encrypted << std::endl; - return; + // Decrypt + const std::string decrypted = GhettoCryptWrapper::DecryptString(encrypted, "password1"); + std::cout << decrypted << std::endl; + + return; } -void ExampleFiles() -{ - std::cout << "Example on how to encrypt & decrypt any file:" << std::endl; +void ExampleFiles() { + std::cout << "Example on how to encrypt & decrypt any file:" << std::endl; - // Encrypt - GhettoCryptWrapper::EncryptFile("main.cpp", "main.cpp.crypt", "password1"); + // Encrypt + GhettoCryptWrapper::EncryptFile("main.cpp", "main.cpp.crypt", "password1"); - // Decrypt - GhettoCryptWrapper::DecryptFile("main.cpp.crypt", "main.cpp.clear", "password1"); + // Decrypt + GhettoCryptWrapper::DecryptFile("main.cpp.crypt", "main.cpp.clear", "password1"); - return; + return; } -int main() -{ - ExampleString(); - //ExampleFiles(); +int main() { + ExampleString(); + //ExampleFiles(); - return 0; + return 0; } + diff --git a/GhettoCrypt/include/Block.h b/GhettoCrypt/include/Block.h index 8b39cd1..921a190 100644 --- a/GhettoCrypt/include/Block.h +++ b/GhettoCrypt/include/Block.h @@ -2,7 +2,7 @@ #include "SecureBitset.h" #include "Config.h" -namespace GhettoCipher -{ - typedef SecureBitset Block; +namespace GhettoCipher { + typedef SecureBitset Block; } + diff --git a/GhettoCrypt/include/Cipher.h b/GhettoCrypt/include/Cipher.h index b949e5b..a7fb231 100644 --- a/GhettoCrypt/include/Cipher.h +++ b/GhettoCrypt/include/Cipher.h @@ -37,4 +37,3 @@ namespace GhettoCipher { Block initializationVector; }; } - diff --git a/GhettoCrypt/include/Config.h b/GhettoCrypt/include/Config.h index 9372314..1f47f3b 100644 --- a/GhettoCrypt/include/Config.h +++ b/GhettoCrypt/include/Config.h @@ -8,4 +8,3 @@ namespace GhettoCipher { // MUST BE > 2 constexpr std::size_t N_ROUNDS = 64; } - diff --git a/GhettoCrypt/include/Feistel.h b/GhettoCrypt/include/Feistel.h index cae2a51..063a890 100644 --- a/GhettoCrypt/include/Feistel.h +++ b/GhettoCrypt/include/Feistel.h @@ -3,58 +3,57 @@ #include "Block.h" #include "Halfblock.h" -namespace GhettoCipher -{ - /** Class to perform a feistel block chipher - */ - class Feistel - { - public: - explicit Feistel(const Block& key); +namespace GhettoCipher { + /** Class to perform a feistel block chipher + */ + class Feistel { + public: + explicit Feistel(const Block& key); - Feistel(const Feistel& other) = delete; - Feistel(Feistel&& other) noexcept = delete; + Feistel(const Feistel& other) = delete; + Feistel(Feistel&& other) noexcept = delete; - ~Feistel(); + ~Feistel(); - //! Will set the seed-key for this feistel network. - //! Roundkeys will be derived from this. - void SetKey(const Block& key); + //! Will set the seed-key for this feistel network. + //! Roundkeys will be derived from this. + void SetKey(const Block& key); - //! Will encipher a data block via the set seed-key - Block Encipher(const Block& data); + //! Will encipher a data block via the set seed-key + Block Encipher(const Block& data); - //! Will decipher a data block via the set seed-key - Block Decipher(const Block& data); + //! Will decipher a data block via the set seed-key + Block Decipher(const Block& data); - private: - //! Will run the feistel rounds, with either regular key order or reversed key order - Block Run(const Block& data, bool reverseKeys); + private: + //! Will run the feistel rounds, with either regular key + //! order or reversed key order + Block Run(const Block& data, bool reverseKeys); - //! Arbitrary cipher function - static Halfblock F(Halfblock m, const Block& key); + //! Arbitrary cipher function + static Halfblock F(Halfblock m, const Block& key); - //! Split a data block into two half blocks (into L and R) - static std::pair FeistelSplit(const Block& block); + //! Split a data block into two half blocks (into L and R) + static std::pair FeistelSplit(const Block& block); - //! Combine two half blocks (L and R) into a regular data block - static Block FeistelCombine(const Halfblock& l, const Halfblock& r); + //! Combine two half blocks (L and R) into a regular data block + static Block FeistelCombine(const Halfblock& l, const Halfblock& r); - //! Will expand a halfblock to a fullblock - static Block ExpansionFunction(const Halfblock& block); + //! Will expand a halfblock to a fullblock + static Block ExpansionFunction(const Halfblock& block); - //! Will compress a fullblock to a halfblock - static Halfblock CompressionFunction(const Block& block); + //! Will compress a fullblock to a halfblock + static Halfblock CompressionFunction(const Block& block); - //! Substitutes four bits by static random others - static std::string SBox(const std::string& in); + //! Substitutes four bits by static random others + static std::string SBox(const std::string& in); - //! Will generate a the round keys - void GenerateRoundKeys(const Block& seedKey); + //! Will generate a the round keys + void GenerateRoundKeys(const Block& seedKey); - //! Will zero the memory used by the keyset - void ZeroKeyMemory(); + //! Will zero the memory used by the keyset + void ZeroKeyMemory(); - Keyset roundKeys; - }; + Keyset roundKeys; + }; } diff --git a/GhettoCrypt/include/Flexblock.h b/GhettoCrypt/include/Flexblock.h index bc1e415..aa7d733 100644 --- a/GhettoCrypt/include/Flexblock.h +++ b/GhettoCrypt/include/Flexblock.h @@ -1,8 +1,7 @@ #pragma once #include -namespace GhettoCipher -{ - //! A "bitset" of variable length - typedef std::string Flexblock; +namespace GhettoCipher { + //! A "bitset" of variable length + typedef std::string Flexblock; } diff --git a/GhettoCrypt/include/GhettoCryptWrapper.h b/GhettoCrypt/include/GhettoCryptWrapper.h index 6cae822..363dae8 100644 --- a/GhettoCrypt/include/GhettoCryptWrapper.h +++ b/GhettoCrypt/include/GhettoCryptWrapper.h @@ -1,33 +1,32 @@ #pragma once #include -namespace GhettoCipher -{ - /** This class is a wrapper to make working with the GhettoCipher super easy with a python-like syntax - */ - class GhettoCryptWrapper - { - public: - //! Will encrypt a string and return it hexadecimally encoded. - static std::string EncryptString(const std::string& cleartext, const std::string& password); +namespace GhettoCipher { + /** This class is a wrapper to make working with the GhettoCipher + * super easy with a python-like syntax + */ + class GhettoCryptWrapper { + public: + //! Will encrypt a string and return it hexadecimally encoded. + static std::string EncryptString(const std::string& cleartext, const std::string& password); - //! Will decrypt a hexadecimally encoded string. - static std::string DecryptString(const std::string& ciphertext, const std::string& password); + //! Will decrypt a hexadecimally encoded string. + static std::string DecryptString(const std::string& ciphertext, const std::string& password); - //! Will encrypt a file. - //! Returns false if anything goes wrong (like, file-access). - //! @filename_in The file to be read. - //! @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); + //! Will encrypt a file. + //! Returns false if anything goes wrong (like, file-access). + //! @filename_in The file to be read. + //! @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); - //! Will decrypt a file. - //! Returns false if anything goes wrong (like, file-access). - //! @filename_in The file to be read. - //! @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); + //! Will decrypt a file. + //! Returns false if anything goes wrong (like, file-access). + //! @filename_in The file to be read. + //! @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); - private: - // No instanciation! >:( - GhettoCryptWrapper(); - }; + private: + // No instanciation! >:( + GhettoCryptWrapper(); + }; } diff --git a/GhettoCrypt/include/Halfblock.h b/GhettoCrypt/include/Halfblock.h index e619035..65f689f 100644 --- a/GhettoCrypt/include/Halfblock.h +++ b/GhettoCrypt/include/Halfblock.h @@ -3,8 +3,7 @@ #include #include "Config.h" -namespace GhettoCipher -{ - constexpr std::size_t HALFBLOCK_SIZE = (BLOCK_SIZE / 2); - typedef SecureBitset Halfblock; +namespace GhettoCipher { + constexpr std::size_t HALFBLOCK_SIZE = (BLOCK_SIZE / 2); + typedef SecureBitset Halfblock; } diff --git a/GhettoCrypt/include/InitializationVector.h b/GhettoCrypt/include/InitializationVector.h index 0ae2728..a54f36c 100644 --- a/GhettoCrypt/include/InitializationVector.h +++ b/GhettoCrypt/include/InitializationVector.h @@ -2,18 +2,16 @@ #include "Config.h" #include "Block.h" -namespace GhettoCipher -{ - /** Will create a sudo-random Block based on a seed - */ - class InitializationVector - { - public: - InitializationVector(const GhettoCipher::Block& seed); +namespace GhettoCipher { + /** Will create a sudo-random Block based on a seed + */ + class InitializationVector { + public: + InitializationVector(const GhettoCipher::Block& seed); - operator GhettoCipher::Block() const; + operator GhettoCipher::Block() const; - private: - GhettoCipher::Block iv; - }; + private: + GhettoCipher::Block iv; + }; } diff --git a/GhettoCrypt/include/Keyset.h b/GhettoCrypt/include/Keyset.h index 0cd1f43..25f4b7a 100644 --- a/GhettoCrypt/include/Keyset.h +++ b/GhettoCrypt/include/Keyset.h @@ -3,7 +3,6 @@ #include "Block.h" #include "Config.h" -namespace GhettoCipher -{ - typedef std::array Keyset; +namespace GhettoCipher { + typedef std::array Keyset; } diff --git a/GhettoCrypt/include/SecureBitset.h b/GhettoCrypt/include/SecureBitset.h index fa0f2d1..5802634 100644 --- a/GhettoCrypt/include/SecureBitset.h +++ b/GhettoCrypt/include/SecureBitset.h @@ -3,322 +3,284 @@ #include #include -namespace GhettoCipher -{ -/** Wrapper for std::bitset that zeroes memory upon deletion. -* This does not include ALL methods, but the ones needed. -* -* Just creating a specialization of std::bitset does not work. -*/ -template -class SecureBitset -{ -public: - explicit SecureBitset(); - explicit SecureBitset(const std::string& str); - explicit SecureBitset(const long long int i); +namespace GhettoCipher { + /** Wrapper for std::bitset that zeroes memory upon deletion. + * This does not include ALL methods, but the ones needed. + * + * Just creating a specialization of std::bitset does not work. + */ + template + class SecureBitset { + public: + explicit SecureBitset(); + explicit SecureBitset(const std::string& str); + explicit SecureBitset(const long long int i); - ~SecureBitset(); + ~SecureBitset(); - bool operator==(const SecureBitset& other) const; - bool operator!=(const SecureBitset& other) const; - bool operator[](const std::size_t) const; - bool test(const std::size_t index) const; - bool all() const; - bool any() const; - bool none() const; - std::size_t count() const; - std::size_t size() const; - SecureBitset& operator&=(const SecureBitset& other); - SecureBitset& operator|=(const SecureBitset& other); - SecureBitset& operator^=(const SecureBitset& other); - SecureBitset operator&(const SecureBitset& other); - SecureBitset operator|(const SecureBitset& other); - SecureBitset operator^(const SecureBitset& other); - SecureBitset operator~() const; - SecureBitset& operator<<=(const std::size_t offset); - SecureBitset& operator>>=(const std::size_t offset); - SecureBitset operator<<(const std::size_t offset) const; - SecureBitset operator>>(const std::size_t offset) const; - SecureBitset& set(); - SecureBitset& set(const std::size_t index, bool value = true); - SecureBitset& reset(); - SecureBitset& reset(const std::size_t index); - SecureBitset& flip(); - SecureBitset& flip(const std::size_t index); - std::string to_string() const; - unsigned long to_ulong() const; - unsigned long long to_ullong() const; + bool operator==(const SecureBitset& other) const; + bool operator!=(const SecureBitset& other) const; + bool operator[](const std::size_t) const; + bool test(const std::size_t index) const; + bool all() const; + bool any() const; + bool none() const; + std::size_t count() const; + std::size_t size() const; + SecureBitset& operator&=(const SecureBitset& other); + SecureBitset& operator|=(const SecureBitset& other); + SecureBitset& operator^=(const SecureBitset& other); + SecureBitset operator&(const SecureBitset& other); + SecureBitset operator|(const SecureBitset& other); + SecureBitset operator^(const SecureBitset& other); + SecureBitset operator~() const; + SecureBitset& operator<<=(const std::size_t offset); + SecureBitset& operator>>=(const std::size_t offset); + SecureBitset operator<<(const std::size_t offset) const; + SecureBitset operator>>(const std::size_t offset) const; + SecureBitset& set(); + SecureBitset& set(const std::size_t index, bool value = true); + SecureBitset& reset(); + SecureBitset& reset(const std::size_t index); + SecureBitset& flip(); + SecureBitset& flip(const std::size_t index); + std::string to_string() const; + unsigned long to_ulong() const; + unsigned long long to_ullong() const; - std::bitset& Get(); - const std::bitset& Get() const; + std::bitset& Get(); + const std::bitset& Get() const; -private: - std::bitset bitset; + private: + std::bitset bitset; }; -template -inline SecureBitset::SecureBitset() - : - bitset() -{ - return; -} + template + inline SecureBitset::SecureBitset() + : + bitset() { + return; + } -template -inline SecureBitset::SecureBitset(const std::string& str) - : - bitset(str) -{ - return; -} + template + inline SecureBitset::SecureBitset(const std::string& str) + : + bitset(str) { + return; + } -template -inline SecureBitset::SecureBitset(const long long int i) - : - bitset(i) -{ - return; -} + template + inline SecureBitset::SecureBitset(const long long int i) + : + bitset(i) { + return; + } -// Don't optimize the destructor out!!! -// These pragmas only work for MSVC and g++, as far as i know. Beware!!! + // Don't optimize the destructor out!!! + // 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 -template -inline SecureBitset::~SecureBitset() -{ - bitset.reset(); - return; -} + template + inline SecureBitset::~SecureBitset() { + bitset.reset(); + return; + } #if defined _WIN32 || defined _WIN64 #pragma optimize("", on ) #elif defined __GNUG__ #pragma GCC pop_options #endif -template -inline bool SecureBitset::operator==(const SecureBitset& other) const -{ - return bitset == other.bitset; -} + template + inline bool SecureBitset::operator==(const SecureBitset& other) const { + return bitset == other.bitset; + } -template -inline bool SecureBitset::operator!=(const SecureBitset& other) const -{ - return bitset != other.bitset; -} + template + inline bool SecureBitset::operator!=(const SecureBitset& other) const { + return bitset != other.bitset; + } -template -inline bool SecureBitset::operator[](const std::size_t index) const -{ - return bitset[index]; -} + template + inline bool SecureBitset::operator[](const std::size_t index) const { + return bitset[index]; + } -template -inline bool SecureBitset::test(const std::size_t index) const -{ - return bitset.test(index); -} + template + inline bool SecureBitset::test(const std::size_t index) const { + return bitset.test(index); + } -template -inline bool SecureBitset::all() const -{ - return bitset.all(); -} + template + inline bool SecureBitset::all() const { + return bitset.all(); + } -template -inline bool SecureBitset::any() const -{ - return bitset.any(); -} + template + inline bool SecureBitset::any() const { + return bitset.any(); + } -template -inline bool SecureBitset::none() const -{ - return bitset.none(); -} + template + inline bool SecureBitset::none() const { + return bitset.none(); + } -template -inline std::size_t SecureBitset::count() const -{ - return bitset.count(); -} + template + inline std::size_t SecureBitset::count() const { + return bitset.count(); + } -template -inline std::size_t SecureBitset::size() const -{ - return bitset.count(); -} + template + inline std::size_t SecureBitset::size() const { + return bitset.count(); + } -template -inline SecureBitset& SecureBitset::operator&=(const SecureBitset& other) -{ - bitset &= other.bitset; - return *this; -} + template + inline SecureBitset& SecureBitset::operator&=(const SecureBitset& other) { + bitset &= other.bitset; + return *this; + } -template -inline SecureBitset& SecureBitset::operator|=(const SecureBitset& other) -{ - bitset |= other.bitset; - return *this; -} + template + inline SecureBitset& SecureBitset::operator|=(const SecureBitset& other) { + bitset |= other.bitset; + return *this; + } -template -inline SecureBitset& SecureBitset::operator^=(const SecureBitset& other) -{ - bitset ^= other.bitset; - return *this; -} + template + inline SecureBitset& SecureBitset::operator^=(const SecureBitset& other) { + bitset ^= other.bitset; + return *this; + } -template -inline SecureBitset SecureBitset::operator&(const SecureBitset& other) -{ - SecureBitset bs; - bs.bitset = bitset & other.bitset; - return bs; -} + template + inline SecureBitset SecureBitset::operator&(const SecureBitset& other) { + SecureBitset bs; + bs.bitset = bitset & other.bitset; + return bs; + } -template -inline SecureBitset SecureBitset::operator|(const SecureBitset& other) -{ - SecureBitset bs; - bs.bitset = bitset | other.bitset; - return bs; -} + template + inline SecureBitset SecureBitset::operator|(const SecureBitset& other) { + SecureBitset bs; + bs.bitset = bitset | other.bitset; + return bs; + } -template -inline SecureBitset SecureBitset::operator^(const SecureBitset& other) -{ - SecureBitset bs; - bs.bitset = bitset ^ other.bitset; - return bs; -} + template + inline SecureBitset SecureBitset::operator^(const SecureBitset& other) { + SecureBitset bs; + bs.bitset = bitset ^ other.bitset; + return bs; + } -template -inline SecureBitset SecureBitset::operator~() const -{ - SecureBitset bs; - bs.bitset = ~bitset; - return bs; -} + template + inline SecureBitset SecureBitset::operator~() const { + SecureBitset bs; + bs.bitset = ~bitset; + return bs; + } -template -inline SecureBitset& SecureBitset::operator<<=(const std::size_t offset) -{ - bitset <<= offset; - return *this; -} + template + inline SecureBitset& SecureBitset::operator<<=(const std::size_t offset) { + bitset <<= offset; + return *this; + } -template -inline SecureBitset& SecureBitset::operator>>=(const std::size_t offset) -{ - bitset >>= offset; - return *this; -} + template + inline SecureBitset& SecureBitset::operator>>=(const std::size_t offset) { + bitset >>= offset; + return *this; + } -template -inline SecureBitset SecureBitset::operator<<(const std::size_t offset) const -{ - SecureBitset bs; - bs.bitset = bitset << offset; - return bs; -} + template + inline SecureBitset SecureBitset::operator<<(const std::size_t offset) const { + SecureBitset bs; + bs.bitset = bitset << offset; + return bs; + } -template -inline SecureBitset SecureBitset::operator>>(const std::size_t offset) const -{ - SecureBitset bs; - bs.bitset = bitset >> offset; - return bs; -} + template + inline SecureBitset SecureBitset::operator>>(const std::size_t offset) const { + SecureBitset bs; + bs.bitset = bitset >> offset; + return bs; + } -template -inline SecureBitset& SecureBitset::set() -{ - bitset.set(); - return *this; -} + template + inline SecureBitset& SecureBitset::set() { + bitset.set(); + return *this; + } -template -inline SecureBitset& SecureBitset::set(const std::size_t index, bool value) -{ - bitset.set(index, value); - return *this; -} + template + inline SecureBitset& SecureBitset::set(const std::size_t index, bool value) { + bitset.set(index, value); + return *this; + } -template -inline SecureBitset& SecureBitset::reset() -{ - bitset.reset(); - return *this; -} + template + inline SecureBitset& SecureBitset::reset() { + bitset.reset(); + return *this; + } -template -inline SecureBitset& SecureBitset::reset(const std::size_t index) -{ - bitset.reset(index); - return *this; -} + template + inline SecureBitset& SecureBitset::reset(const std::size_t index) { + bitset.reset(index); + return *this; + } -template -inline SecureBitset& SecureBitset::flip() -{ - bitset.flip(); - return *this; -} + template + inline SecureBitset& SecureBitset::flip() { + bitset.flip(); + return *this; + } -template -inline SecureBitset& SecureBitset::flip(const std::size_t index) -{ - bitset.flip(index); - return *this; -} + template + inline SecureBitset& SecureBitset::flip(const std::size_t index) { + bitset.flip(index); + return *this; + } -template -inline std::string SecureBitset::to_string() const -{ - return bitset.to_string(); -} + template + inline std::string SecureBitset::to_string() const { + return bitset.to_string(); + } -template -inline unsigned long SecureBitset::to_ulong() const -{ - return bitset.to_ulong(); -} -template -inline unsigned long long SecureBitset::to_ullong() const -{ - return bitset.to_ullong(); -} + template + inline unsigned long SecureBitset::to_ulong() const { + return bitset.to_ulong(); + } -template -inline std::bitset& SecureBitset::Get() -{ - return bitset; -} + template + inline unsigned long long SecureBitset::to_ullong() const { + return bitset.to_ullong(); + } -template -inline const std::bitset& SecureBitset::Get() const -{ - return bitset; -} + template + inline std::bitset& SecureBitset::Get() { + return bitset; + } -template -inline std::ostream& operator<<(std::ostream& ofs, const SecureBitset& bs) -{ - return ofs << bs.Get(); -} + template + inline const std::bitset& SecureBitset::Get() const { + return bitset; + } -template -inline std::istream& operator>>(std::istream& ifs, const SecureBitset& bs) -{ - return ifs >> bs.Get(); -} + template + inline std::ostream& operator<<(std::ostream& ofs, const SecureBitset& bs) { + return ofs << bs.Get(); + } + + template + inline std::istream& operator>>(std::istream& ifs, const SecureBitset& bs) { + return ifs >> bs.Get(); + } } diff --git a/GhettoCrypt/include/Util.h b/GhettoCrypt/include/Util.h index 4150438..788bd3b 100644 --- a/GhettoCrypt/include/Util.h +++ b/GhettoCrypt/include/Util.h @@ -10,109 +10,104 @@ #include "Cipher.h" #include "InitializationVector.h" -namespace GhettoCipher -{ +namespace GhettoCipher { //! 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; } //! Will perform a wrapping left-bitshift on a bitset template - inline SecureBitset Shiftl(const SecureBitset& bits, const std::size_t amount) - { + inline SecureBitset Shiftl(const SecureBitset& bits, const std::size_t amount) { std::stringstream ss; const std::string bitss = bits.to_string(); - for (std::size_t i = 0; i < bitss.size(); i++) + for (std::size_t i = 0; i < bitss.size(); i++) { ss << bitss[Mod((int)(i + amount), (int)bitss.size())]; + } return SecureBitset(ss.str()); } //! Will perform a wrapping right-bitshift on a bitset template - inline SecureBitset Shiftr(const SecureBitset& bits, const std::size_t amount) - { + inline SecureBitset Shiftr(const SecureBitset& bits, const std::size_t amount) { std::stringstream ss; const std::string bitss = bits.to_string(); - for (std::size_t i = 0; i < bitss.size(); i++) + for (std::size_t i = 0; i < bitss.size(); i++) { ss << bitss[Mod((i - amount), bitss.size())]; + } return SecureBitset(ss.str()); } //! Will pad a string to a set length with a certain character - inline std::string PadStringToLength(const std::string& str, const std::size_t len, const char pad, const bool padLeft = true) - { + inline std::string PadStringToLength(const std::string& str, const std::size_t len, const char pad, const bool padLeft = true) { // Fast-reject: Already above padded length - if (str.length() >= len) + if (str.length() >= len) { return str; + } std::stringstream ss; // Pad left: - if (padLeft) - { - for (std::size_t i = 0; i < len - str.size(); i++) + if (padLeft) { + for (std::size_t i = 0; i < len - str.size(); i++) { ss << pad; + } ss << str; } // Pad right: - else - { + else { ss << str; - for (std::size_t i = 0; i < len - str.size(); i++) + for (std::size_t i = 0; i < len - str.size(); i++) { ss << pad; + } } return ss.str(); } //! Will convert a string to a fixed-size data block - inline Block StringToBitblock(const std::string& s) - { + inline Block StringToBitblock(const std::string& s) { std::stringstream ss; - for (std::size_t i = 0; i < s.size(); i++) + for (std::size_t i = 0; i < s.size(); i++) { ss << std::bitset<8>(s[i]); + } // Pad rest with zeores return Block(PadStringToLength(ss.str(), 128, '0', false)); } //! Will convert a string to a flexible data block - inline Flexblock StringToBits(const std::string& s) - { + inline Flexblock StringToBits(const std::string& s) { std::stringstream ss; - for (std::size_t i = 0; i < s.size(); i++) + for (std::size_t i = 0; i < s.size(); i++) { ss << std::bitset<8>(s[i]); + } return Flexblock(ss.str()); } //! Will convert a fixed-size data block to a bytestring - inline std::string BitblockToBytes(const Block& bits) - { + inline std::string BitblockToBytes(const Block& bits) { std::stringstream ss; const std::string bitstring = bits.to_string(); - for (std::size_t i = 0; i < BLOCK_SIZE; i += 8) - { + for (std::size_t i = 0; i < BLOCK_SIZE; i += 8) { ss << (char)std::bitset<8>(bitstring.substr(i, 8)).to_ulong(); } return ss.str(); } - + //! Will convert a fixed-size data block to a string //! The difference to BitblockToBytes() is, that it strips excess nullbytes - inline std::string BitblockToString(const Block& bits) - { + inline std::string BitblockToString(const Block& bits) { // Decode to bytes std::string text = BitblockToBytes(bits); @@ -123,14 +118,12 @@ namespace GhettoCipher } //! Will convert a flexible data block to a bytestring - inline std::string BitsToBytes(const Flexblock& bits) - { + inline std::string BitsToBytes(const Flexblock& bits) { std::stringstream ss; const std::string bitstring = bits; - for (std::size_t i = 0; i < bits.size(); i += 8) - { + for (std::size_t i = 0; i < bits.size(); i += 8) { ss << (char)std::bitset<8>(bitstring.substr(i, 8)).to_ulong(); } @@ -138,9 +131,8 @@ namespace GhettoCipher } //! Will convert a flexible data block to a string - //! //! The difference to BitsToBytes() is, that it strips excess nullbytes - inline std::string BitsToString(const Flexblock& bits) - { + //! The difference to BitsToBytes() is, that it strips excess nullbytes + inline std::string BitsToString(const Flexblock& bits) { // Decode to bytes std::string text = BitsToBytes(bits); @@ -151,51 +143,52 @@ namespace GhettoCipher } //! Turns a fixed-size data block into a hex-string - inline std::string BitblockToHexstring(const Block& b) - { + inline std::string BitblockToHexstring(const Block& b) { std::stringstream ss; const std::string charset = "0123456789abcdef"; const std::string bstr = b.to_string(); - for (std::size_t i = 0; i < bstr.size(); i += 4) + for (std::size_t i = 0; i < bstr.size(); i += 4) { ss << charset[std::bitset<4>(bstr.substr(i, 4)).to_ulong()]; + } return ss.str(); } //! Turns a flexible data block into a hex-string - inline std::string BitsToHexstring(const Flexblock& b) - { + inline std::string BitsToHexstring(const Flexblock& b) { std::stringstream ss; const std::string charset = "0123456789abcdef"; const std::string bstr = b; - for (std::size_t i = 0; i < bstr.size(); i += 4) + for (std::size_t i = 0; i < bstr.size(); i += 4) { ss << charset[std::bitset<4>(bstr.substr(i, 4)).to_ulong()]; + } return ss.str(); } //! Turns a hex string into a fixed-size data block - inline Block HexstringToBitblock(const std::string& hexstring) - { + inline Block HexstringToBitblock(const std::string& hexstring) { std::stringstream ss; - for (std::size_t i = 0; i < hexstring.size(); i++) - { + for (std::size_t i = 0; i < hexstring.size(); i++) { const char c = hexstring[i]; // Get value std::size_t value; - if ((c >= '0') && (c <= '9')) + if ((c >= '0') && (c <= '9')) { // Is it a number? value = ((std::size_t)c - '0') + 0; - else if ((c >= 'a') && (c <= 'f')) + } + else if ((c >= 'a') && (c <= 'f')) { // Else, it is a lowercase letter value = ((std::size_t)c - 'a') + 10; - else + } + else { throw std::logic_error("non-hex string detected in HexstringToBits()"); + } // Append to our bits ss << std::bitset<4>(value); @@ -205,24 +198,25 @@ namespace GhettoCipher } //! Turns a hex string into a flexible data block - inline Flexblock HexstringToBits(const std::string& hexstring) - { + inline Flexblock HexstringToBits(const std::string& hexstring) { std::stringstream ss; - for (std::size_t i = 0; i < hexstring.size(); i++) - { + for (std::size_t i = 0; i < hexstring.size(); i++) { const char c = hexstring[i]; // Get value std::size_t value; - if ((c >= '0') && (c <= '9')) + if ((c >= '0') && (c <= '9')) { // Is it a number? value = ((std::size_t)c - '0') + 0; - else if ((c >= 'a') && (c <= 'f')) + } + else if ((c >= 'a') && (c <= 'f')) { // Else, it is a lowercase letter value = ((std::size_t)c - 'a') + 10; - else + } + else { throw std::logic_error("non-hex string detected in HexstringToBits()"); + } // Append to our bits ss << std::bitset<4>(value); @@ -235,14 +229,12 @@ namespace GhettoCipher //! Note that if your password is shorter (in bits) than BLOCK_SIZE, the rest of the key will be padded with 0 (see next line!). //! To provide a better initial key, (and to get rid of padding zeroes), the raw result (b) will be xor'd with an initialization vector based on b. //! : return b ^ iv(b) - inline Block PasswordToKey(const std::string& in) - { + inline Block PasswordToKey(const std::string& in) { // Let's provide a nice initial value to be sure even a password of length 0 results in a proper key Block b = InitializationVector(StringToBitblock("3J7IipfQTDJbO8jtasz9PgWui6faPaEMOuVuAqyhB1S2CRcLw5caawewgDUEG1WN")); // Segment the password in segments of key-size, and xor them together. - for (std::size_t i = 0; i < in.size(); i += BLOCK_SIZE / 8) - { + for (std::size_t i = 0; i < in.size(); i += BLOCK_SIZE / 8) { const Block fragment = StringToBitblock( PadStringToLength(in.substr(i, BLOCK_SIZE / 8), BLOCK_SIZE / 8, 0, false) ); @@ -251,22 +243,19 @@ namespace GhettoCipher // To provide diffusion, hash fragment to fragment' first b ^= Block(Cipher(fragment).Encipher(fragment.to_string())); } - return b; } //! Will reduce a flexblock (they are of arbitrary length) to a single block. //! This single block should change completely, if a single bit in the input flexblock changes anywhere. - inline Block ReductionFunction_Flexblock2Block(const Flexblock& in) - { + inline Block ReductionFunction_Flexblock2Block(const Flexblock& in) { Block b; // No initialization vector needed here // Segment the input in segments of BLOCK_SIZE, and xor them together. - for (std::size_t i = 0; i < in.size(); i += BLOCK_SIZE) - { + for (std::size_t i = 0; i < in.size(); i += BLOCK_SIZE) { const Block fragment = Block(PadStringToLength(in.substr(i, BLOCK_SIZE), BLOCK_SIZE, 0, false)); - + // To provide confusion, xor the blocks together // To provide diffusion, hash fragment to fragment' first b ^= Block(Cipher(fragment).Encipher(fragment.to_string())); @@ -276,13 +265,13 @@ namespace GhettoCipher } //! Will read a file into a flexblock - inline Flexblock ReadFileToBits(const std::string& filepath) - { + inline Flexblock ReadFileToBits(const std::string& filepath) { // Read file std::ifstream ifs(filepath, std::ios::binary); - if (!ifs.good()) + if (!ifs.good()) { throw std::runtime_error("Unable to open ifilestream!"); + } std::stringstream ss; std::copy( @@ -300,16 +289,16 @@ namespace GhettoCipher } //! Will save bits to a binary file - inline void WriteBitsToFile(const std::string& filepath, const Flexblock& bits) - { + inline void WriteBitsToFile(const std::string& filepath, const Flexblock& bits) { // Convert bits to bytes const std::string bytes = BitsToBytes(bits); // Write bits to file std::ofstream ofs(filepath, std::ios::binary); - if (!ofs.good()) + if (!ofs.good()) { throw std::runtime_error("Unable to open ofilestream!"); + } ofs.write(bytes.data(), bytes.length()); ofs.close(); @@ -317,3 +306,4 @@ namespace GhettoCipher return; } } + diff --git a/GhettoCrypt/include/Version.h b/GhettoCrypt/include/Version.h index c2e7dc2..48e51ef 100644 --- a/GhettoCrypt/include/Version.h +++ b/GhettoCrypt/include/Version.h @@ -1,2 +1,3 @@ #pragma once #define GHETTOCRYPT_VERSION 0.21 + diff --git a/GhettoCrypt/src/Cipher.cpp b/GhettoCrypt/src/Cipher.cpp index ebd93a0..632c421 100644 --- a/GhettoCrypt/src/Cipher.cpp +++ b/GhettoCrypt/src/Cipher.cpp @@ -5,114 +5,111 @@ #include "InitializationVector.h" GhettoCipher::Cipher::Cipher(const Block& key) - : - key { key }, - initializationVector(InitializationVector(key)) -{ + : + key { key }, + initializationVector(InitializationVector(key)) { - return; + return; } GhettoCipher::Cipher::Cipher(const std::string& password) - : - key { PasswordToKey(password) }, - initializationVector(InitializationVector(key)) -{ - return; + : + key { PasswordToKey(password) }, + initializationVector(InitializationVector(key)) { + return; } -GhettoCipher::Cipher::~Cipher() -{ - // Clear key memory - ZeroKeyMemory(); +GhettoCipher::Cipher::~Cipher() { + // Clear key memory + ZeroKeyMemory(); - return; + return; } -void GhettoCipher::Cipher::SetKey(const Block& key) -{ - ZeroKeyMemory(); +void GhettoCipher::Cipher::SetKey(const Block& key) { + ZeroKeyMemory(); - this->key = key; - return; + this->key = key; + return; } -void GhettoCipher::Cipher::SetPassword(const std::string& password) -{ - ZeroKeyMemory(); +void GhettoCipher::Cipher::SetPassword(const std::string& password) { + ZeroKeyMemory(); - key = PasswordToKey(password); - return; + key = PasswordToKey(password); + return; } -GhettoCipher::Flexblock GhettoCipher::Cipher::Encipher(const Flexblock& data, bool printProgress) const -{ - // Split cleartext into blocks - std::vector blocks; +GhettoCipher::Flexblock GhettoCipher::Cipher::Encipher(const Flexblock& data, bool printProgress) const { + // Split cleartext into blocks + std::vector blocks; - 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)) - ); + 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 + Feistel feistel(key); - for (std::size_t i = 0; i < blocks.size(); i++) - { - // 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() - } + for (std::size_t i = 0; i < blocks.size(); i++) { + // 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; + } - // Concatenate ciphertext blocks back into a flexblock - std::stringstream ss; - for (Block& b : blocks) - ss << b; + 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() + } - // Return it - return ss.str(); + // Concatenate ciphertext blocks back into a flexblock + std::stringstream ss; + for (Block& b : blocks) { + ss << b; + } + + // Return it + return ss.str(); } -GhettoCipher::Flexblock GhettoCipher::Cipher::Decipher(const Flexblock& data, bool printProgress) const -{ - // Split ciphertext into blocks - std::vector blocks; +GhettoCipher::Flexblock GhettoCipher::Cipher::Decipher(const Flexblock& data, bool printProgress) const { + // Split ciphertext into blocks + std::vector blocks; - 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)) - ); + 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 + Feistel feistel(key); - // We can't do this in-loop for decryption, because we are decrypting the blocks in-place. - Block lastBlock = initializationVector; - - for (std::size_t i = 0; i < blocks.size(); i++) - { - // 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; + // We can't do this in-loop for decryption, because we are decrypting the blocks in-place. + Block lastBlock = initializationVector; - Block tmpCopy = blocks[i]; + for (std::size_t i = 0; i < blocks.size(); i++) { + // 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; + } - 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 - // Concatenate ciphertext blocks back into a flexblock - std::stringstream ss; - for (Block& b : blocks) - ss << b; + lastBlock = std::move(tmpCopy); + } - // Return it - return ss.str(); + // Concatenate ciphertext blocks back into a flexblock + std::stringstream ss; + for (Block& b : blocks) { + ss << b; + } + + // Return it + return ss.str(); } // These pragmas only work for MSVC and g++, as far as i know. Beware!!! @@ -122,13 +119,13 @@ GhettoCipher::Flexblock GhettoCipher::Cipher::Decipher(const Flexblock& data, bo #pragma GCC push_options #pragma GCC optimize ("O0") #endif -void GhettoCipher::Cipher::ZeroKeyMemory() -{ - key.reset(); - return; +void GhettoCipher::Cipher::ZeroKeyMemory() { + key.reset(); + return; } #if defined _WIN32 || defined _WIN64 #pragma optimize("", on ) #elif defined __GNUG__ #pragma GCC pop_options #endif + diff --git a/GhettoCrypt/src/Feistel.cpp b/GhettoCrypt/src/Feistel.cpp index 121f6ac..8c6bfa6 100644 --- a/GhettoCrypt/src/Feistel.cpp +++ b/GhettoCrypt/src/Feistel.cpp @@ -3,51 +3,46 @@ #include "Util.h" #include "Config.h" -GhettoCipher::Feistel::Feistel(const Block& key) -{ +GhettoCipher::Feistel::Feistel(const Block& key) { SetKey(key); return; } -GhettoCipher::Feistel::~Feistel() -{ +GhettoCipher::Feistel::~Feistel() { ZeroKeyMemory(); return; } -void GhettoCipher::Feistel::SetKey(const Block& key) -{ +void GhettoCipher::Feistel::SetKey(const Block& key) { GenerateRoundKeys(key); return; } -GhettoCipher::Block GhettoCipher::Feistel::Encipher(const Block& data) -{ +GhettoCipher::Block GhettoCipher::Feistel::Encipher(const Block& data) { return Run(data, false); } -GhettoCipher::Block GhettoCipher::Feistel::Decipher(const Block& data) -{ +GhettoCipher::Block GhettoCipher::Feistel::Decipher(const Block& data) { return Run(data, true); } -GhettoCipher::Block GhettoCipher::Feistel::Run(const Block& data, bool reverseKeys) -{ +GhettoCipher::Block GhettoCipher::Feistel::Run(const Block& data, bool reverseKeys) { const auto splitData = FeistelSplit(data); GhettoCipher::Halfblock l = splitData.first; GhettoCipher::Halfblock r = splitData.second; Halfblock tmp; - for (std::size_t i = 0; i < N_ROUNDS; i++) - { + for (std::size_t i = 0; i < N_ROUNDS; i++) { // Calculate key index std::size_t keyIndex; - if (reverseKeys) + if (reverseKeys) { keyIndex = N_ROUNDS - i - 1; - else + } + else { keyIndex = i; + } // Do a feistel round tmp = r; @@ -62,8 +57,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) -{ +GhettoCipher::Halfblock GhettoCipher::Feistel::F(Halfblock m, const Block& key) { // Made-up F function // Expand to full bitwidth @@ -79,8 +73,7 @@ GhettoCipher::Halfblock GhettoCipher::Feistel::F(Halfblock m, const Block& key) std::stringstream ss; const std::string m_str = m_expanded.to_string(); - for (std::size_t i = 0; i < BLOCK_SIZE; i += 4) - { + for (std::size_t i = 0; i < BLOCK_SIZE; i += 4) { ss << SBox(m_str.substr(i, 4)); } @@ -90,8 +83,7 @@ GhettoCipher::Halfblock GhettoCipher::Feistel::F(Halfblock m, const Block& key) return CompressionFunction(m_expanded); } -std::pair GhettoCipher::Feistel::FeistelSplit(const Block& block) -{ +std::pair GhettoCipher::Feistel::FeistelSplit(const Block& block) { const std::string bits = block.to_string(); Halfblock l(bits.substr(0, bits.size() / 2)); @@ -100,13 +92,11 @@ std::pair GhettoCipher::Feiste return std::make_pair(l, r); } -GhettoCipher::Block GhettoCipher::Feistel::FeistelCombine(const Halfblock& l, const Halfblock& r) -{ +GhettoCipher::Block GhettoCipher::Feistel::FeistelCombine(const Halfblock& l, const Halfblock& r) { return Block(l.to_string() + r.to_string()); } -GhettoCipher::Block GhettoCipher::Feistel::ExpansionFunction(const Halfblock& block) -{ +GhettoCipher::Block GhettoCipher::Feistel::ExpansionFunction(const Halfblock& block) { std::stringstream ss; const std::string bits = block.to_string(); @@ -117,8 +107,7 @@ GhettoCipher::Block GhettoCipher::Feistel::ExpansionFunction(const Halfblock& bl expansionMap["11"] = "0111"; // We have to double the bits! - for (std::size_t i = 0; i < HALFBLOCK_SIZE; i += 2) - { + for (std::size_t i = 0; i < HALFBLOCK_SIZE; i += 2) { const std::string sub = bits.substr(i, 2); ss << expansionMap[sub]; } @@ -126,8 +115,7 @@ GhettoCipher::Block GhettoCipher::Feistel::ExpansionFunction(const Halfblock& bl return Block(ss.str()); } -GhettoCipher::Halfblock GhettoCipher::Feistel::CompressionFunction(const Block& block) -{ +GhettoCipher::Halfblock GhettoCipher::Feistel::CompressionFunction(const Block& block) { std::stringstream ss; const std::string bits = block.to_string(); @@ -150,8 +138,7 @@ GhettoCipher::Halfblock GhettoCipher::Feistel::CompressionFunction(const Block& compressionMap["1111"] = "01"; // We have to half the bits! - for (std::size_t i = 0; i < BLOCK_SIZE; i += 4) - { + for (std::size_t i = 0; i < BLOCK_SIZE; i += 4) { const std::string sub = bits.substr(i, 4); ss << compressionMap[sub]; } @@ -159,12 +146,10 @@ GhettoCipher::Halfblock GhettoCipher::Feistel::CompressionFunction(const Block& return Halfblock(ss.str()); } -std::string GhettoCipher::Feistel::SBox(const std::string& in) -{ +std::string GhettoCipher::Feistel::SBox(const std::string& in) { static std::unordered_map subMap; static bool mapInitialized = false; - if (!mapInitialized) - { + if (!mapInitialized) { subMap["0000"] = "1100"; subMap["0001"] = "1000"; subMap["0010"] = "0001"; @@ -187,8 +172,7 @@ std::string GhettoCipher::Feistel::SBox(const std::string& in) return subMap[in]; } -void GhettoCipher::Feistel::GenerateRoundKeys(const Block& seedKey) -{ +void GhettoCipher::Feistel::GenerateRoundKeys(const Block& seedKey) { // Clear initial key memory ZeroKeyMemory(); roundKeys = Keyset(); @@ -205,10 +189,12 @@ void GhettoCipher::Feistel::GenerateRoundKeys(const Block& seedKey) // if it is a multiple of 4, we'll shift it by 1 into the opposite direction const std::size_t setBits1 = compressedSeed1.count(); - if (setBits1 % 4 == 0) + if (setBits1 % 4 == 0) { compressedSeed1 = Shiftr(compressedSeed1, 1); - else if (setBits1 % 3 == 0) + } + else if (setBits1 % 3 == 0) { compressedSeed1 = Shiftl(compressedSeed1, 1); + } // Now apply substitution std::stringstream ssKey1; @@ -216,8 +202,7 @@ void GhettoCipher::Feistel::GenerateRoundKeys(const Block& seedKey) const std::string bitsKey1 = compressedSeed1.to_string(); const std::string bitsKey2 = compressedSeed2.to_string(); - for (std::size_t i = 0; i < HALFBLOCK_SIZE; i += 4) - { + for (std::size_t i = 0; i < HALFBLOCK_SIZE; i += 4) { ssKey1 << SBox(bitsKey1.substr(i, 4)); ssKey2 << SBox(bitsKey2.substr(i, 4)); } @@ -230,11 +215,9 @@ void GhettoCipher::Feistel::GenerateRoundKeys(const Block& seedKey) roundKeys[0] = ExpansionFunction(compressedSeed1) ^ seedKey; roundKeys[1] = ExpansionFunction(compressedSeed2) ^ seedKey; - // Now derive all other round keys - for (std::size_t i = 2; i < roundKeys.size(); i++) - { + for (std::size_t i = 2; i < roundKeys.size(); i++) { // Initialize new round key with last round key Block newKey = roundKeys[i - 1]; @@ -262,10 +245,10 @@ void GhettoCipher::Feistel::GenerateRoundKeys(const Block& seedKey) #pragma GCC push_options #pragma GCC optimize ("O0") #endif -void GhettoCipher::Feistel::ZeroKeyMemory() -{ - for (Block& key : roundKeys) +void GhettoCipher::Feistel::ZeroKeyMemory() { + for (Block& key : roundKeys) { key.reset(); + } return; } @@ -274,3 +257,4 @@ void GhettoCipher::Feistel::ZeroKeyMemory() #elif defined __GNUG__ #pragma GCC pop_options #endif + diff --git a/GhettoCrypt/src/GhettoCryptWrapper.cpp b/GhettoCrypt/src/GhettoCryptWrapper.cpp index 2e19a13..6860b1b 100644 --- a/GhettoCrypt/src/GhettoCryptWrapper.cpp +++ b/GhettoCrypt/src/GhettoCryptWrapper.cpp @@ -2,90 +2,83 @@ #include "Cipher.h" #include "Util.h" -std::string GhettoCipher::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); +std::string GhettoCipher::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); - // Recode the ascii-string to bits - const Flexblock cleartext_bits = StringToBits(cleartext); + // Recode the ascii-string to bits + const Flexblock cleartext_bits = StringToBits(cleartext); - // Encrypt our cleartext bits - const Flexblock ciphertext_bits = cipher.Encipher(cleartext_bits); + // Encrypt our cleartext bits + const Flexblock ciphertext_bits = cipher.Encipher(cleartext_bits); - // Recode the ciphertext bits to a hex-string - const std::string ciphertext = BitsToHexstring(ciphertext_bits); + // Recode the ciphertext bits to a hex-string + const std::string ciphertext = BitsToHexstring(ciphertext_bits); - // Return it - 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); - Cipher cipher(key); +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); + Cipher cipher(key); - // Recode the hex-string to bits - const Flexblock ciphertext_bits = HexstringToBits(ciphertext); + // Recode the hex-string to bits + const Flexblock ciphertext_bits = HexstringToBits(ciphertext); - // Decrypt the ciphertext bits - const std::string cleartext_bits = cipher.Decipher(ciphertext_bits); + // Decrypt the ciphertext bits + const std::string cleartext_bits = cipher.Decipher(ciphertext_bits); - // Recode the cleartext bits to an ascii-string - const std::string cleartext = BitsToString(cleartext_bits); + // Recode the cleartext bits to an ascii-string + const std::string cleartext = BitsToString(cleartext_bits); - // Return it - 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 - const Flexblock cleartext_bits = ReadFileToBits(filename_in); +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 + const Flexblock cleartext_bits = ReadFileToBits(filename_in); - // Instanciate our cipher and supply a key - const Block key = PasswordToKey(password); - Cipher cipher(key); + // Instanciate our cipher and supply a key + const Block key = PasswordToKey(password); + Cipher cipher(key); - // Encrypt our cleartext bits - const Flexblock ciphertext_bits = cipher.Encipher(cleartext_bits, printProgressReport); + // Encrypt our cleartext bits + const Flexblock ciphertext_bits = cipher.Encipher(cleartext_bits, printProgressReport); - // Write our ciphertext bits to file - WriteBitsToFile(filename_out, ciphertext_bits); + // Write our ciphertext bits to file + WriteBitsToFile(filename_out, ciphertext_bits); - return true; - } - catch (std::runtime_error&) - { - return false; - } + return true; + } + 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 - const Flexblock ciphertext_bits = ReadFileToBits(filename_in); +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 + const Flexblock ciphertext_bits = ReadFileToBits(filename_in); - // Instanciate our cipher and supply a key - const Block key = PasswordToKey(password); - Cipher cipher(key); + // Instanciate our cipher and supply a key + const Block key = PasswordToKey(password); + Cipher cipher(key); - // Decrypt the ciphertext bits - const Flexblock cleartext_bits = cipher.Decipher(ciphertext_bits, printProgressReport); + // Decrypt the ciphertext bits + const Flexblock cleartext_bits = cipher.Decipher(ciphertext_bits, printProgressReport); - // Write our cleartext bits to file - WriteBitsToFile(filename_out, cleartext_bits); + // Write our cleartext bits to file + WriteBitsToFile(filename_out, cleartext_bits); - return true; - } - catch (std::runtime_error&) - { - return false; - } + return true; + } + catch (std::runtime_error&) { + return false; + } } + diff --git a/GhettoCrypt/src/InitializationVector.cpp b/GhettoCrypt/src/InitializationVector.cpp index 36ca575..9ad7f3b 100644 --- a/GhettoCrypt/src/InitializationVector.cpp +++ b/GhettoCrypt/src/InitializationVector.cpp @@ -1,14 +1,13 @@ #include "InitializationVector.h" #include "Feistel.h" -GhettoCipher::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::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 -{ - return iv; +GhettoCipher::InitializationVector::operator GhettoCipher::Block() const { + return iv; } +