Doxygen adjustment, and better directory name for GCryptLib
This commit is contained in:
46
GCryptLib/CMakeLists.txt
Normal file
46
GCryptLib/CMakeLists.txt
Normal file
@@ -0,0 +1,46 @@
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
project(GCrypt)
|
||||
|
||||
###################
|
||||
# Library project #
|
||||
###################
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
|
||||
FILE(GLOB main_src src/*.cpp)
|
||||
add_library(${PROJECT_NAME}
|
||||
${main_src}
|
||||
)
|
||||
|
||||
target_include_directories(${PROJECT_NAME} PRIVATE
|
||||
include
|
||||
)
|
||||
|
||||
#########
|
||||
# Tests #
|
||||
#########
|
||||
FILE(GLOB test_src test/*.cpp)
|
||||
add_executable(test
|
||||
test/Catch2.h
|
||||
${test_src}
|
||||
)
|
||||
target_link_libraries(test ${PROJECT_NAME})
|
||||
|
||||
target_include_directories(test PRIVATE
|
||||
include
|
||||
)
|
||||
|
||||
|
||||
##############
|
||||
# Executable #
|
||||
##############
|
||||
FILE(GLOB exec_src exec/*.cpp)
|
||||
add_executable(exec
|
||||
${exec_src}
|
||||
)
|
||||
target_link_libraries(exec ${PROJECT_NAME})
|
||||
|
||||
target_include_directories(exec PRIVATE
|
||||
include
|
||||
${eule_include}
|
||||
)
|
||||
|
1
GCryptLib/doxygen/.gitignore
vendored
Normal file
1
GCryptLib/doxygen/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
build/
|
2579
GCryptLib/doxygen/doxyfig
Normal file
2579
GCryptLib/doxygen/doxyfig
Normal file
File diff suppressed because it is too large
Load Diff
11
GCryptLib/doxygen/doxyrun.sh
Normal file
11
GCryptLib/doxygen/doxyrun.sh
Normal file
@@ -0,0 +1,11 @@
|
||||
#!zsh
|
||||
|
||||
# Copy repository readme here to be used as a cover page
|
||||
tail ../../readme.md -n +2 > index.md
|
||||
|
||||
# Run doxygen
|
||||
doxygen doxyfig
|
||||
|
||||
# Cleanup index.md
|
||||
rm -f index.md
|
||||
|
45
GCryptLib/exec/main.cpp
Normal file
45
GCryptLib/exec/main.cpp
Normal file
@@ -0,0 +1,45 @@
|
||||
#include <iostream>
|
||||
#include <GhettoCryptWrapper.h>
|
||||
#include <SecureBitset.h>
|
||||
#include <Util.h>
|
||||
#include <InitializationVector.h>
|
||||
|
||||
using namespace Leonetienne::GCrypt;
|
||||
|
||||
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;
|
||||
|
||||
// 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;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void ExampleFiles() {
|
||||
std::cout << "Example on how to encrypt & decrypt any file:" << std::endl;
|
||||
|
||||
// Encrypt
|
||||
GhettoCryptWrapper::EncryptFile("main.cpp", "main.cpp.crypt", "password1");
|
||||
|
||||
// Decrypt
|
||||
GhettoCryptWrapper::DecryptFile("main.cpp.crypt", "main.cpp.clear", "password1");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
int main() {
|
||||
ExampleString();
|
||||
//ExampleFiles();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
8
GCryptLib/include/Block.h
Normal file
8
GCryptLib/include/Block.h
Normal file
@@ -0,0 +1,8 @@
|
||||
#pragma once
|
||||
#include "SecureBitset.h"
|
||||
#include "Config.h"
|
||||
|
||||
namespace Leonetienne::GCrypt {
|
||||
typedef SecureBitset<BLOCK_SIZE> Block;
|
||||
}
|
||||
|
39
GCryptLib/include/Cipher.h
Normal file
39
GCryptLib/include/Cipher.h
Normal file
@@ -0,0 +1,39 @@
|
||||
#pragma once
|
||||
#include "Feistel.h"
|
||||
#include "Flexblock.h"
|
||||
|
||||
namespace Leonetienne::GCrypt {
|
||||
/** Class to apply a block cipher to messages of arbitrary length in a distributed manner
|
||||
*/
|
||||
class Cipher {
|
||||
public:
|
||||
explicit Cipher(const Block& key);
|
||||
explicit Cipher(const std::string& password);
|
||||
|
||||
Cipher(const Cipher& other) = delete;
|
||||
Cipher(Cipher&& other) noexcept = delete;
|
||||
|
||||
~Cipher();
|
||||
|
||||
//! Will set the key
|
||||
void SetKey(const Block& key);
|
||||
|
||||
//! Will set the key from a password
|
||||
void SetPassword(const std::string& password);
|
||||
|
||||
//! Will encipher a flexblock of data
|
||||
Flexblock Encipher(const Flexblock& data, bool printProgress = false) const;
|
||||
|
||||
//! Will decipher a flexblock of data
|
||||
Flexblock Decipher(const Flexblock& data, bool printProgress = false) const;
|
||||
|
||||
private:
|
||||
Block key;
|
||||
|
||||
//! Will zero the memory used by the key
|
||||
void ZeroKeyMemory();
|
||||
|
||||
// Initial value for cipher block chaining
|
||||
Block initializationVector;
|
||||
};
|
||||
}
|
10
GCryptLib/include/Config.h
Normal file
10
GCryptLib/include/Config.h
Normal file
@@ -0,0 +1,10 @@
|
||||
#pragma once
|
||||
#include <cstddef>
|
||||
|
||||
namespace Leonetienne::GCrypt {
|
||||
// MUST BE A POWER OF 2 > 4
|
||||
constexpr std::size_t BLOCK_SIZE = 512;
|
||||
|
||||
// MUST BE > 2
|
||||
constexpr std::size_t N_ROUNDS = 64;
|
||||
}
|
59
GCryptLib/include/Feistel.h
Normal file
59
GCryptLib/include/Feistel.h
Normal file
@@ -0,0 +1,59 @@
|
||||
#pragma once
|
||||
#include "Keyset.h"
|
||||
#include "Block.h"
|
||||
#include "Halfblock.h"
|
||||
|
||||
namespace Leonetienne::GCrypt {
|
||||
/** 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();
|
||||
|
||||
//! 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 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);
|
||||
|
||||
//! 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<Halfblock, Halfblock> 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);
|
||||
|
||||
//! 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);
|
||||
|
||||
//! 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 zero the memory used by the keyset
|
||||
void ZeroKeyMemory();
|
||||
|
||||
Keyset roundKeys;
|
||||
};
|
||||
}
|
7
GCryptLib/include/Flexblock.h
Normal file
7
GCryptLib/include/Flexblock.h
Normal file
@@ -0,0 +1,7 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
|
||||
namespace Leonetienne::GCrypt {
|
||||
//! A "bitset" of variable length
|
||||
typedef std::string Flexblock;
|
||||
}
|
32
GCryptLib/include/GhettoCryptWrapper.h
Normal file
32
GCryptLib/include/GhettoCryptWrapper.h
Normal file
@@ -0,0 +1,32 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
|
||||
namespace Leonetienne::GCrypt {
|
||||
/** 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 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);
|
||||
|
||||
private:
|
||||
// No instanciation! >:(
|
||||
GhettoCryptWrapper();
|
||||
};
|
||||
}
|
9
GCryptLib/include/Halfblock.h
Normal file
9
GCryptLib/include/Halfblock.h
Normal file
@@ -0,0 +1,9 @@
|
||||
#pragma once
|
||||
#include "SecureBitset.h"
|
||||
#include <cstdint>
|
||||
#include "Config.h"
|
||||
|
||||
namespace Leonetienne::GCrypt {
|
||||
constexpr std::size_t HALFBLOCK_SIZE = (BLOCK_SIZE / 2);
|
||||
typedef SecureBitset<HALFBLOCK_SIZE> Halfblock;
|
||||
}
|
17
GCryptLib/include/InitializationVector.h
Normal file
17
GCryptLib/include/InitializationVector.h
Normal file
@@ -0,0 +1,17 @@
|
||||
#pragma once
|
||||
#include "Config.h"
|
||||
#include "Block.h"
|
||||
|
||||
namespace Leonetienne::GCrypt {
|
||||
/** Will create a sudo-random Block based on a seed
|
||||
*/
|
||||
class InitializationVector {
|
||||
public:
|
||||
InitializationVector(const Block& seed);
|
||||
|
||||
operator Block() const;
|
||||
|
||||
private:
|
||||
Block iv;
|
||||
};
|
||||
}
|
8
GCryptLib/include/Keyset.h
Normal file
8
GCryptLib/include/Keyset.h
Normal file
@@ -0,0 +1,8 @@
|
||||
#pragma once
|
||||
#include <array>
|
||||
#include "Block.h"
|
||||
#include "Config.h"
|
||||
|
||||
namespace Leonetienne::GCrypt {
|
||||
typedef std::array<Block, N_ROUNDS> Keyset;
|
||||
}
|
286
GCryptLib/include/SecureBitset.h
Normal file
286
GCryptLib/include/SecureBitset.h
Normal file
@@ -0,0 +1,286 @@
|
||||
#pragma once
|
||||
#include <bitset>
|
||||
#include <ostream>
|
||||
#include <istream>
|
||||
|
||||
namespace Leonetienne::GCrypt {
|
||||
/** Wrapper for std::bitset<T> that zeroes memory upon deletion.
|
||||
* This does not include ALL methods, but the ones needed.
|
||||
*
|
||||
* Just creating a specialization of std::bitset<T> does not work.
|
||||
*/
|
||||
template <std::size_t T>
|
||||
class SecureBitset {
|
||||
public:
|
||||
explicit SecureBitset();
|
||||
explicit SecureBitset(const std::string& str);
|
||||
explicit SecureBitset(const long long int i);
|
||||
|
||||
~SecureBitset();
|
||||
|
||||
bool operator==(const SecureBitset<T>& other) const;
|
||||
bool operator!=(const SecureBitset<T>& 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<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 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>& set();
|
||||
SecureBitset<T>& set(const std::size_t index, bool value = true);
|
||||
SecureBitset<T>& reset();
|
||||
SecureBitset<T>& reset(const std::size_t index);
|
||||
SecureBitset<T>& flip();
|
||||
SecureBitset<T>& flip(const std::size_t index);
|
||||
std::string to_string() const;
|
||||
unsigned long to_ulong() const;
|
||||
unsigned long long to_ullong() const;
|
||||
|
||||
std::bitset<T>& Get();
|
||||
const std::bitset<T>& Get() const;
|
||||
|
||||
private:
|
||||
std::bitset<T> bitset;
|
||||
};
|
||||
|
||||
template<std::size_t T>
|
||||
inline SecureBitset<T>::SecureBitset()
|
||||
:
|
||||
bitset() {
|
||||
return;
|
||||
}
|
||||
|
||||
template<std::size_t T>
|
||||
inline SecureBitset<T>::SecureBitset(const std::string& str)
|
||||
:
|
||||
bitset(str) {
|
||||
return;
|
||||
}
|
||||
|
||||
template<std::size_t T>
|
||||
inline SecureBitset<T>::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!!!
|
||||
#if defined _WIN32 || defined _WIN64
|
||||
#pragma optimize("", off )
|
||||
#elif defined __GNUG__
|
||||
#pragma GCC push_options
|
||||
#pragma GCC optimize ("O0")
|
||||
#endif
|
||||
template<std::size_t T>
|
||||
inline SecureBitset<T>::~SecureBitset() {
|
||||
bitset.reset();
|
||||
return;
|
||||
}
|
||||
#if defined _WIN32 || defined _WIN64
|
||||
#pragma optimize("", on )
|
||||
#elif defined __GNUG__
|
||||
#pragma GCC pop_options
|
||||
#endif
|
||||
|
||||
template<std::size_t T>
|
||||
inline bool SecureBitset<T>::operator==(const SecureBitset<T>& other) const {
|
||||
return bitset == other.bitset;
|
||||
}
|
||||
|
||||
template<std::size_t T>
|
||||
inline bool SecureBitset<T>::operator!=(const SecureBitset<T>& other) const {
|
||||
return bitset != other.bitset;
|
||||
}
|
||||
|
||||
template<std::size_t T>
|
||||
inline bool SecureBitset<T>::operator[](const std::size_t index) const {
|
||||
return bitset[index];
|
||||
}
|
||||
|
||||
template<std::size_t T>
|
||||
inline bool SecureBitset<T>::test(const std::size_t index) const {
|
||||
return bitset.test(index);
|
||||
}
|
||||
|
||||
template<std::size_t T>
|
||||
inline bool SecureBitset<T>::all() const {
|
||||
return bitset.all();
|
||||
}
|
||||
|
||||
template<std::size_t T>
|
||||
inline bool SecureBitset<T>::any() const {
|
||||
return bitset.any();
|
||||
}
|
||||
|
||||
template<std::size_t T>
|
||||
inline bool SecureBitset<T>::none() const {
|
||||
return bitset.none();
|
||||
}
|
||||
|
||||
template<std::size_t T>
|
||||
inline std::size_t SecureBitset<T>::count() const {
|
||||
return bitset.count();
|
||||
}
|
||||
|
||||
template<std::size_t T>
|
||||
inline std::size_t SecureBitset<T>::size() const {
|
||||
return bitset.count();
|
||||
}
|
||||
|
||||
template<std::size_t T>
|
||||
inline SecureBitset<T>& SecureBitset<T>::operator&=(const SecureBitset<T>& other) {
|
||||
bitset &= other.bitset;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<std::size_t T>
|
||||
inline SecureBitset<T>& SecureBitset<T>::operator|=(const SecureBitset<T>& other) {
|
||||
bitset |= other.bitset;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<std::size_t T>
|
||||
inline SecureBitset<T>& SecureBitset<T>::operator^=(const SecureBitset<T>& other) {
|
||||
bitset ^= other.bitset;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<std::size_t T>
|
||||
inline SecureBitset<T> SecureBitset<T>::operator&(const SecureBitset<T>& other) {
|
||||
SecureBitset bs;
|
||||
bs.bitset = bitset & other.bitset;
|
||||
return bs;
|
||||
}
|
||||
|
||||
template<std::size_t T>
|
||||
inline SecureBitset<T> SecureBitset<T>::operator|(const SecureBitset<T>& other) {
|
||||
SecureBitset bs;
|
||||
bs.bitset = bitset | other.bitset;
|
||||
return bs;
|
||||
}
|
||||
|
||||
template<std::size_t T>
|
||||
inline SecureBitset<T> SecureBitset<T>::operator^(const SecureBitset<T>& other) {
|
||||
SecureBitset bs;
|
||||
bs.bitset = bitset ^ other.bitset;
|
||||
return bs;
|
||||
}
|
||||
|
||||
template<std::size_t T>
|
||||
inline SecureBitset<T> SecureBitset<T>::operator~() const {
|
||||
SecureBitset bs;
|
||||
bs.bitset = ~bitset;
|
||||
return bs;
|
||||
}
|
||||
|
||||
template<std::size_t T>
|
||||
inline SecureBitset<T>& SecureBitset<T>::operator<<=(const std::size_t offset) {
|
||||
bitset <<= offset;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<std::size_t T>
|
||||
inline SecureBitset<T>& SecureBitset<T>::operator>>=(const std::size_t offset) {
|
||||
bitset >>= offset;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<std::size_t T>
|
||||
inline SecureBitset<T> SecureBitset<T>::operator<<(const std::size_t offset) const {
|
||||
SecureBitset bs;
|
||||
bs.bitset = bitset << offset;
|
||||
return bs;
|
||||
}
|
||||
|
||||
template<std::size_t T>
|
||||
inline SecureBitset<T> SecureBitset<T>::operator>>(const std::size_t offset) const {
|
||||
SecureBitset bs;
|
||||
bs.bitset = bitset >> offset;
|
||||
return bs;
|
||||
}
|
||||
|
||||
template<std::size_t T>
|
||||
inline SecureBitset<T>& SecureBitset<T>::set() {
|
||||
bitset.set();
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<std::size_t T>
|
||||
inline SecureBitset<T>& SecureBitset<T>::set(const std::size_t index, bool value) {
|
||||
bitset.set(index, value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<std::size_t T>
|
||||
inline SecureBitset<T>& SecureBitset<T>::reset() {
|
||||
bitset.reset();
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<std::size_t T>
|
||||
inline SecureBitset<T>& SecureBitset<T>::reset(const std::size_t index) {
|
||||
bitset.reset(index);
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<std::size_t T>
|
||||
inline SecureBitset<T>& SecureBitset<T>::flip() {
|
||||
bitset.flip();
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<std::size_t T>
|
||||
inline SecureBitset<T>& SecureBitset<T>::flip(const std::size_t index) {
|
||||
bitset.flip(index);
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<std::size_t T>
|
||||
inline std::string SecureBitset<T>::to_string() const {
|
||||
return bitset.to_string();
|
||||
}
|
||||
|
||||
template<std::size_t T>
|
||||
inline unsigned long SecureBitset<T>::to_ulong() const {
|
||||
return bitset.to_ulong();
|
||||
}
|
||||
|
||||
template<std::size_t T>
|
||||
inline unsigned long long SecureBitset<T>::to_ullong() const {
|
||||
return bitset.to_ullong();
|
||||
}
|
||||
|
||||
template<std::size_t T>
|
||||
inline std::bitset<T>& SecureBitset<T>::Get() {
|
||||
return bitset;
|
||||
}
|
||||
|
||||
template<std::size_t T>
|
||||
inline const std::bitset<T>& SecureBitset<T>::Get() const {
|
||||
return bitset;
|
||||
}
|
||||
|
||||
template <std::size_t T>
|
||||
inline std::ostream& operator<<(std::ostream& ofs, const SecureBitset<T>& bs) {
|
||||
return ofs << bs.Get();
|
||||
}
|
||||
|
||||
template <std::size_t T>
|
||||
inline std::istream& operator>>(std::istream& ifs, const SecureBitset<T>& bs) {
|
||||
return ifs >> bs.Get();
|
||||
}
|
||||
}
|
309
GCryptLib/include/Util.h
Normal file
309
GCryptLib/include/Util.h
Normal file
@@ -0,0 +1,309 @@
|
||||
#pragma once
|
||||
#include <bitset>
|
||||
#include <sstream>
|
||||
#include <fstream>
|
||||
#include <cstring>
|
||||
#include "SecureBitset.h"
|
||||
#include "Block.h"
|
||||
#include "Flexblock.h"
|
||||
#include "Config.h"
|
||||
#include "Cipher.h"
|
||||
#include "InitializationVector.h"
|
||||
|
||||
namespace Leonetienne::GCrypt {
|
||||
//! Mod-operator that works with negative values
|
||||
inline int Mod(const int numerator, const int denominator) {
|
||||
return (denominator + (numerator % denominator)) % denominator;
|
||||
}
|
||||
|
||||
//! Will perform a wrapping left-bitshift on a bitset
|
||||
template <std::size_t T>
|
||||
inline SecureBitset<T> Shiftl(const SecureBitset<T>& 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++) {
|
||||
ss << bitss[Mod((int)(i + amount), (int)bitss.size())];
|
||||
}
|
||||
|
||||
return SecureBitset<T>(ss.str());
|
||||
}
|
||||
|
||||
//! Will perform a wrapping right-bitshift on a bitset
|
||||
template <std::size_t T>
|
||||
inline SecureBitset<T> Shiftr(const SecureBitset<T>& 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++) {
|
||||
ss << bitss[Mod((i - amount), bitss.size())];
|
||||
}
|
||||
|
||||
return SecureBitset<T>(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) {
|
||||
// Fast-reject: Already above padded length
|
||||
if (str.length() >= len) {
|
||||
return str;
|
||||
}
|
||||
|
||||
std::stringstream ss;
|
||||
|
||||
// Pad left:
|
||||
if (padLeft) {
|
||||
for (std::size_t i = 0; i < len - str.size(); i++) {
|
||||
ss << pad;
|
||||
}
|
||||
ss << str;
|
||||
}
|
||||
// Pad right:
|
||||
else {
|
||||
ss << str;
|
||||
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) {
|
||||
std::stringstream ss;
|
||||
|
||||
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) {
|
||||
std::stringstream ss;
|
||||
|
||||
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) {
|
||||
std::stringstream ss;
|
||||
|
||||
const std::string bitstring = bits.to_string();
|
||||
|
||||
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) {
|
||||
// Decode to bytes
|
||||
std::string text = BitblockToBytes(bits);
|
||||
|
||||
// Dümp excess nullbytes
|
||||
text.resize(strlen(text.data()));
|
||||
|
||||
return text;
|
||||
}
|
||||
|
||||
//! Will convert a flexible data block to a bytestring
|
||||
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) {
|
||||
ss << (char)std::bitset<8>(bitstring.substr(i, 8)).to_ulong();
|
||||
}
|
||||
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
//! 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) {
|
||||
// Decode to bytes
|
||||
std::string text = BitsToBytes(bits);
|
||||
|
||||
// Dümp excess nullbytes
|
||||
text.resize(strlen(text.data()));
|
||||
|
||||
return text;
|
||||
}
|
||||
|
||||
//! Turns a fixed-size data block into a hex-string
|
||||
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) {
|
||||
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) {
|
||||
std::stringstream ss;
|
||||
const std::string charset = "0123456789abcdef";
|
||||
const std::string bstr = b;
|
||||
|
||||
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) {
|
||||
std::stringstream ss;
|
||||
|
||||
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')) {
|
||||
// Is it a number?
|
||||
value = ((std::size_t)c - '0') + 0;
|
||||
}
|
||||
else if ((c >= 'a') && (c <= 'f')) {
|
||||
// Else, it is a lowercase letter
|
||||
value = ((std::size_t)c - 'a') + 10;
|
||||
}
|
||||
else {
|
||||
throw std::logic_error("non-hex string detected in HexstringToBits()");
|
||||
}
|
||||
|
||||
// Append to our bits
|
||||
ss << std::bitset<4>(value);
|
||||
}
|
||||
|
||||
return Block(ss.str());
|
||||
}
|
||||
|
||||
//! Turns a hex string into a flexible data block
|
||||
inline Flexblock HexstringToBits(const std::string& hexstring) {
|
||||
std::stringstream ss;
|
||||
|
||||
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')) {
|
||||
// Is it a number?
|
||||
value = ((std::size_t)c - '0') + 0;
|
||||
}
|
||||
else if ((c >= 'a') && (c <= 'f')) {
|
||||
// Else, it is a lowercase letter
|
||||
value = ((std::size_t)c - 'a') + 10;
|
||||
}
|
||||
else {
|
||||
throw std::logic_error("non-hex string detected in HexstringToBits()");
|
||||
}
|
||||
|
||||
// Append to our bits
|
||||
ss << std::bitset<4>(value);
|
||||
}
|
||||
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
//! Creates a key of size BLOCK_SIZE from a password of arbitrary length.
|
||||
//! 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) {
|
||||
// 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) {
|
||||
const Block fragment = StringToBitblock(
|
||||
PadStringToLength(in.substr(i, BLOCK_SIZE / 8), BLOCK_SIZE / 8, 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()));
|
||||
}
|
||||
|
||||
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) {
|
||||
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) {
|
||||
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()));
|
||||
}
|
||||
|
||||
return b;
|
||||
}
|
||||
|
||||
//! Will read a file into a flexblock
|
||||
inline Flexblock ReadFileToBits(const std::string& filepath) {
|
||||
// Read file
|
||||
std::ifstream ifs(filepath, std::ios::binary);
|
||||
|
||||
if (!ifs.good()) {
|
||||
throw std::runtime_error("Unable to open ifilestream!");
|
||||
}
|
||||
|
||||
std::stringstream ss;
|
||||
std::copy(
|
||||
std::istreambuf_iterator<char>(ifs),
|
||||
std::istreambuf_iterator<char>(),
|
||||
std::ostreambuf_iterator<char>(ss)
|
||||
);
|
||||
|
||||
ifs.close();
|
||||
|
||||
const std::string bytes = ss.str();
|
||||
|
||||
// Convert bytes to bits
|
||||
return StringToBits(bytes);
|
||||
}
|
||||
|
||||
//! Will save bits to a binary file
|
||||
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()) {
|
||||
throw std::runtime_error("Unable to open ofilestream!");
|
||||
}
|
||||
|
||||
ofs.write(bytes.data(), bytes.length());
|
||||
ofs.close();
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
3
GCryptLib/include/Version.h
Normal file
3
GCryptLib/include/Version.h
Normal file
@@ -0,0 +1,3 @@
|
||||
#pragma once
|
||||
#define GHETTOCRYPT_VERSION 0.21
|
||||
|
135
GCryptLib/src/Cipher.cpp
Normal file
135
GCryptLib/src/Cipher.cpp
Normal file
@@ -0,0 +1,135 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include "Cipher.h"
|
||||
#include "Util.h"
|
||||
#include "InitializationVector.h"
|
||||
|
||||
namespace Leonetienne::GCrypt {
|
||||
|
||||
Cipher::Cipher(const Block& key)
|
||||
:
|
||||
key { key },
|
||||
initializationVector(InitializationVector(key)) {
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
Cipher::Cipher(const std::string& password)
|
||||
:
|
||||
key { PasswordToKey(password) },
|
||||
initializationVector(InitializationVector(key)) {
|
||||
return;
|
||||
}
|
||||
|
||||
Cipher::~Cipher() {
|
||||
// Clear key memory
|
||||
ZeroKeyMemory();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void Cipher::SetKey(const Block& key) {
|
||||
ZeroKeyMemory();
|
||||
|
||||
this->key = key;
|
||||
return;
|
||||
}
|
||||
|
||||
void Cipher::SetPassword(const std::string& password) {
|
||||
ZeroKeyMemory();
|
||||
|
||||
key = PasswordToKey(password);
|
||||
return;
|
||||
}
|
||||
|
||||
Flexblock Cipher::Encipher(const Flexblock& data, bool printProgress) const {
|
||||
// Split cleartext into blocks
|
||||
std::vector<Block> 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))
|
||||
);
|
||||
}
|
||||
|
||||
// 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()
|
||||
}
|
||||
|
||||
// Concatenate ciphertext blocks back into a flexblock
|
||||
std::stringstream ss;
|
||||
for (Block& b : blocks) {
|
||||
ss << b;
|
||||
}
|
||||
|
||||
// Return it
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
Flexblock Cipher::Decipher(const Flexblock& data, bool printProgress) const {
|
||||
// Split ciphertext into blocks
|
||||
std::vector<Block> 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))
|
||||
);
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
lastBlock = std::move(tmpCopy);
|
||||
}
|
||||
|
||||
// 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!!!
|
||||
#if defined _WIN32 || defined _WIN64
|
||||
#pragma optimize("", off )
|
||||
#elif defined __GNUG__
|
||||
#pragma GCC push_options
|
||||
#pragma GCC optimize ("O0")
|
||||
#endif
|
||||
void Cipher::ZeroKeyMemory() {
|
||||
key.reset();
|
||||
return;
|
||||
}
|
||||
#if defined _WIN32 || defined _WIN64
|
||||
#pragma optimize("", on )
|
||||
#elif defined __GNUG__
|
||||
#pragma GCC pop_options
|
||||
#endif
|
||||
|
||||
}
|
||||
|
264
GCryptLib/src/Feistel.cpp
Normal file
264
GCryptLib/src/Feistel.cpp
Normal file
@@ -0,0 +1,264 @@
|
||||
#include <unordered_map>
|
||||
#include "Feistel.h"
|
||||
#include "Util.h"
|
||||
#include "Config.h"
|
||||
|
||||
namespace Leonetienne::GCrypt {
|
||||
|
||||
Feistel::Feistel(const Block& key) {
|
||||
SetKey(key);
|
||||
return;
|
||||
}
|
||||
|
||||
Feistel::~Feistel() {
|
||||
ZeroKeyMemory();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void Feistel::SetKey(const Block& key) {
|
||||
GenerateRoundKeys(key);
|
||||
return;
|
||||
}
|
||||
|
||||
Block Feistel::Encipher(const Block& data) {
|
||||
return Run(data, false);
|
||||
}
|
||||
|
||||
Block Feistel::Decipher(const Block& data) {
|
||||
return Run(data, true);
|
||||
}
|
||||
|
||||
Block Feistel::Run(const Block& data, bool reverseKeys) {
|
||||
const auto splitData = FeistelSplit(data);
|
||||
Halfblock l = splitData.first;
|
||||
Halfblock r = splitData.second;
|
||||
|
||||
Halfblock tmp;
|
||||
|
||||
for (std::size_t i = 0; i < N_ROUNDS; i++) {
|
||||
// Calculate key index
|
||||
std::size_t keyIndex;
|
||||
if (reverseKeys) {
|
||||
keyIndex = N_ROUNDS - i - 1;
|
||||
}
|
||||
else {
|
||||
keyIndex = i;
|
||||
}
|
||||
|
||||
// Do a feistel round
|
||||
tmp = r;
|
||||
r = l ^ F(r, roundKeys[keyIndex]);
|
||||
l = tmp;
|
||||
}
|
||||
|
||||
// Block has finished de*ciphering.
|
||||
// Let's generate a new set of round keys.
|
||||
GenerateRoundKeys((Block)roundKeys.back());
|
||||
|
||||
return FeistelCombine(r, l);
|
||||
}
|
||||
|
||||
Halfblock Feistel::F(Halfblock m, const Block& key) {
|
||||
// Made-up F function
|
||||
|
||||
// Expand to full bitwidth
|
||||
Block m_expanded = ExpansionFunction(m);
|
||||
|
||||
// Shift to left by 1
|
||||
m_expanded = Shiftl(m_expanded, 1);
|
||||
|
||||
// Xor with key
|
||||
m_expanded ^= key;
|
||||
|
||||
// Non-linearly apply subsitution boxes
|
||||
std::stringstream ss;
|
||||
const std::string m_str = m_expanded.to_string();
|
||||
|
||||
for (std::size_t i = 0; i < BLOCK_SIZE; i += 4) {
|
||||
ss << SBox(m_str.substr(i, 4));
|
||||
}
|
||||
|
||||
m_expanded = Block(ss.str());
|
||||
|
||||
// Return the compressed version
|
||||
return CompressionFunction(m_expanded);
|
||||
}
|
||||
|
||||
std::pair<Halfblock, Halfblock> Feistel::FeistelSplit(const Block& block) {
|
||||
const std::string bits = block.to_string();
|
||||
|
||||
Halfblock l(bits.substr(0, bits.size() / 2));
|
||||
Halfblock r(bits.substr(bits.size() / 2));
|
||||
|
||||
return std::make_pair(l, r);
|
||||
}
|
||||
|
||||
Block Feistel::FeistelCombine(const Halfblock& l, const Halfblock& r) {
|
||||
return Block(l.to_string() + r.to_string());
|
||||
}
|
||||
|
||||
Block Feistel::ExpansionFunction(const Halfblock& block) {
|
||||
std::stringstream ss;
|
||||
const std::string bits = block.to_string();
|
||||
|
||||
std::unordered_map<std::string, std::string> expansionMap;
|
||||
expansionMap["00"] = "1101";
|
||||
expansionMap["01"] = "1000";
|
||||
expansionMap["10"] = "0010";
|
||||
expansionMap["11"] = "0111";
|
||||
|
||||
// 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];
|
||||
}
|
||||
|
||||
return Block(ss.str());
|
||||
}
|
||||
|
||||
Halfblock Feistel::CompressionFunction(const Block& block) {
|
||||
std::stringstream ss;
|
||||
const std::string bits = block.to_string();
|
||||
|
||||
std::unordered_map<std::string, std::string> compressionMap;
|
||||
compressionMap["0000"] = "10";
|
||||
compressionMap["0001"] = "01";
|
||||
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!
|
||||
for (std::size_t i = 0; i < BLOCK_SIZE; i += 4) {
|
||||
const std::string sub = bits.substr(i, 4);
|
||||
ss << compressionMap[sub];
|
||||
}
|
||||
|
||||
return Halfblock(ss.str());
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
return subMap[in];
|
||||
}
|
||||
|
||||
void Feistel::GenerateRoundKeys(const Block& seedKey) {
|
||||
// Clear initial key memory
|
||||
ZeroKeyMemory();
|
||||
roundKeys = Keyset();
|
||||
|
||||
// Derive the initial two round keys
|
||||
|
||||
// Compress- substitute, and expand the seed key to form the initial and the second-initial round key
|
||||
// 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)
|
||||
// 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
|
||||
const std::size_t setBits1 = compressedSeed1.count();
|
||||
|
||||
if (setBits1 % 4 == 0) {
|
||||
compressedSeed1 = Shiftr(compressedSeed1, 1);
|
||||
}
|
||||
else if (setBits1 % 3 == 0) {
|
||||
compressedSeed1 = Shiftl(compressedSeed1, 1);
|
||||
}
|
||||
|
||||
// Now apply substitution
|
||||
std::stringstream ssKey1;
|
||||
std::stringstream ssKey2;
|
||||
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) {
|
||||
ssKey1 << SBox(bitsKey1.substr(i, 4));
|
||||
ssKey2 << SBox(bitsKey2.substr(i, 4));
|
||||
}
|
||||
|
||||
compressedSeed1 = Halfblock(ssKey1.str());
|
||||
compressedSeed2 = Halfblock(ssKey2.str());
|
||||
|
||||
// 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;
|
||||
|
||||
// Now derive all other round keys
|
||||
|
||||
for (std::size_t i = 2; i < roundKeys.size(); i++) {
|
||||
// Initialize new round key with last round key
|
||||
Block newKey = roundKeys[i - 1];
|
||||
|
||||
// Shift to left by how many bits are set, modulo 8
|
||||
newKey = Shiftl(newKey, newKey.count() % 8); // This action is irreversible
|
||||
|
||||
// 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.
|
||||
|
||||
roundKeys[i] = FeistelCombine(halfkey1, halfkey2);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// These pragmas only work for MSVC and g++, as far as i know. Beware!!!
|
||||
#if defined _WIN32 || defined _WIN64
|
||||
#pragma optimize("", off )
|
||||
#elif defined __GNUG__
|
||||
#pragma GCC push_options
|
||||
#pragma GCC optimize ("O0")
|
||||
#endif
|
||||
void Feistel::ZeroKeyMemory() {
|
||||
for (Block& key : roundKeys) {
|
||||
key.reset();
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
#if defined _WIN32 || defined _WIN64
|
||||
#pragma optimize("", on )
|
||||
#elif defined __GNUG__
|
||||
#pragma GCC pop_options
|
||||
#endif
|
||||
|
||||
}
|
||||
|
88
GCryptLib/src/GhettoCryptWrapper.cpp
Normal file
88
GCryptLib/src/GhettoCryptWrapper.cpp
Normal file
@@ -0,0 +1,88 @@
|
||||
#include "GhettoCryptWrapper.h"
|
||||
#include "Cipher.h"
|
||||
#include "Util.h"
|
||||
|
||||
namespace Leonetienne::GCrypt {
|
||||
|
||||
std::string GhettoCryptWrapper::EncryptString(const std::string& cleartext, const std::string& password) {
|
||||
// Instanciate our cipher and supply a key
|
||||
const Block key = PasswordToKey(password);
|
||||
Cipher cipher(key);
|
||||
|
||||
// Recode the ascii-string to bits
|
||||
const Flexblock cleartext_bits = StringToBits(cleartext);
|
||||
|
||||
// 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);
|
||||
|
||||
// Return it
|
||||
return ciphertext;
|
||||
}
|
||||
|
||||
std::string GhettoCryptWrapper::DecryptString(const std::string& ciphertext, const std::string& password) {
|
||||
// Instanciate our cipher and supply a key
|
||||
const Block key = PasswordToKey(password);
|
||||
Cipher cipher(key);
|
||||
|
||||
// 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);
|
||||
|
||||
// Recode the cleartext bits to an ascii-string
|
||||
const std::string cleartext = BitsToString(cleartext_bits);
|
||||
|
||||
// Return it
|
||||
return cleartext;
|
||||
}
|
||||
|
||||
bool GhettoCryptWrapper::EncryptFile(const std::string& filename_in, const std::string& filename_out, const std::string& password, bool printProgressReport) {
|
||||
try {
|
||||
// Read the file to bits
|
||||
const Flexblock cleartext_bits = ReadFileToBits(filename_in);
|
||||
|
||||
// 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);
|
||||
|
||||
// Write our ciphertext bits to file
|
||||
WriteBitsToFile(filename_out, ciphertext_bits);
|
||||
|
||||
return true;
|
||||
}
|
||||
catch (std::runtime_error&) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool GhettoCryptWrapper::DecryptFile(const std::string& filename_in, const std::string& filename_out, const std::string& password, bool printProgressReport) {
|
||||
try {
|
||||
// Read the file to bits
|
||||
const Flexblock ciphertext_bits = ReadFileToBits(filename_in);
|
||||
|
||||
// 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);
|
||||
|
||||
// Write our cleartext bits to file
|
||||
WriteBitsToFile(filename_out, cleartext_bits);
|
||||
|
||||
return true;
|
||||
}
|
||||
catch (std::runtime_error&) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
17
GCryptLib/src/InitializationVector.cpp
Normal file
17
GCryptLib/src/InitializationVector.cpp
Normal file
@@ -0,0 +1,17 @@
|
||||
#include "InitializationVector.h"
|
||||
#include "Feistel.h"
|
||||
|
||||
namespace Leonetienne::GCrypt {
|
||||
|
||||
InitializationVector::InitializationVector(const Block& seed) {
|
||||
// We'll generate our initialization vector by encrypting our seed with itself as a key
|
||||
// iv = E(M=seed, K=seed)
|
||||
iv = Feistel(seed).Encipher(seed);
|
||||
}
|
||||
|
||||
InitializationVector::operator Block() const {
|
||||
return iv;
|
||||
}
|
||||
|
||||
}
|
||||
|
17965
GCryptLib/test/Catch2.h
Normal file
17965
GCryptLib/test/Catch2.h
Normal file
File diff suppressed because it is too large
Load Diff
230
GCryptLib/test/EncryptEqualsDecrypt.cpp
Normal file
230
GCryptLib/test/EncryptEqualsDecrypt.cpp
Normal file
@@ -0,0 +1,230 @@
|
||||
#include "CppUnitTest.h"
|
||||
#include "../GhettoCrypt/Cipher.h"
|
||||
#include "../GhettoCrypt/Util.h"
|
||||
|
||||
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
|
||||
using namespace GhettoCipher;
|
||||
|
||||
// THESE TESTS ASSUME BLOCK_SIZE == 512
|
||||
|
||||
namespace SimpleTests
|
||||
{
|
||||
TEST_CLASS(EncryptEqualsDecrypt)
|
||||
{
|
||||
public:
|
||||
|
||||
// Tests that encrypting a message of exactly BLOCK_SIZE yields the exact message back
|
||||
TEST_METHOD(SingleBlock_NoPadding)
|
||||
{
|
||||
// Instanciate our cipher and supply a key
|
||||
const Block key = PasswordToKey("1234");
|
||||
const Cipher cipher(key);
|
||||
|
||||
// Recode the ascii-string to bits
|
||||
const Flexblock cleartext_bits =
|
||||
"1011110011010110000010110001111000111010111101001010100100011101"
|
||||
"0101110101010010100000110100001000011000111010001001110101111111"
|
||||
"1110110101100101110001010101011110001010000010111110011011010111"
|
||||
"1100110100111000000011100101010100110010001110010011000010111001"
|
||||
"0000010000010000011001111010011110111001000000000110101000110001"
|
||||
"0110111110110110100000010100000011010001000011100100111001001011"
|
||||
"1101100100000100010000001011100010010001101111100100101100010001"
|
||||
"0000011110010110111010110110111110011110011010001100100111110101";
|
||||
|
||||
// Encrypt our cleartext bits
|
||||
const Flexblock ciphertext_bits = cipher.Encipher(cleartext_bits);
|
||||
|
||||
// Decipher it again
|
||||
const Flexblock decryptedBits = cipher.Decipher(ciphertext_bits);
|
||||
|
||||
// Assert that the decrypted text equals the plaintext
|
||||
Assert::AreEqual(
|
||||
cleartext_bits,
|
||||
decryptedBits
|
||||
);
|
||||
}
|
||||
|
||||
// Tests that encrypting a message of less than BLOCK_SIZE yields the exact message plus zero-padding back
|
||||
TEST_METHOD(SingleBlock_Padding)
|
||||
{
|
||||
// Instanciate our cipher and supply a key
|
||||
const Block key = PasswordToKey("1234");
|
||||
const Cipher cipher(key);
|
||||
|
||||
// Recode the ascii-string to bits
|
||||
const Flexblock cleartext_bits =
|
||||
"1011110011010110000010110001111000111010111101001010100100011101"
|
||||
"0101110101010010100000110100001000011000111010001001110101111111"
|
||||
"1110110101100101110001010101011110001010000010111110011011010111"
|
||||
"1100110100111000000011100101010100110010001110010011000010111001"
|
||||
"0000010000010000011001111010011110111001000000000110101000110001"
|
||||
"0110111110110110100000010100000011010001000011100100111001001011"
|
||||
"1101100100000100";
|
||||
|
||||
const Flexblock cleartext_bits_EXPECTED_RESULT =
|
||||
"1011110011010110000010110001111000111010111101001010100100011101"
|
||||
"0101110101010010100000110100001000011000111010001001110101111111"
|
||||
"1110110101100101110001010101011110001010000010111110011011010111"
|
||||
"1100110100111000000011100101010100110010001110010011000010111001"
|
||||
"0000010000010000011001111010011110111001000000000110101000110001"
|
||||
"0110111110110110100000010100000011010001000011100100111001001011"
|
||||
"1101100100000100000000000000000000000000000000000000000000000000"
|
||||
"0000000000000000000000000000000000000000000000000000000000000000";
|
||||
|
||||
// Encrypt our cleartext bits
|
||||
const Flexblock ciphertext_bits = cipher.Encipher(cleartext_bits);
|
||||
|
||||
// Decipher it again
|
||||
const Flexblock decryptedBits = cipher.Decipher(ciphertext_bits);
|
||||
|
||||
// Assert that the decrypted text equals the plaintext
|
||||
Assert::AreEqual(
|
||||
cleartext_bits_EXPECTED_RESULT,
|
||||
decryptedBits
|
||||
);
|
||||
}
|
||||
|
||||
// Tests that a decrypted ciphertext equals its plaintrext version, using a cleartext that requires A LOT of blocks
|
||||
TEST_METHOD(MultiBlock_NoPadding)
|
||||
{
|
||||
// Instanciate our cipher and supply a key
|
||||
const Block key = PasswordToKey("1234");
|
||||
const Cipher cipher(key);
|
||||
|
||||
// Recode the ascii-string to bits
|
||||
const Flexblock cleartext_bits =
|
||||
"1011110011010110000010110001111000111010111101001010100100011101"
|
||||
"0101110101010010100000110100001000011000111010001001110101111111"
|
||||
"1110110101100101110001010101011110001010000010111110011011010111"
|
||||
"1100110100111000000011100101010100110010001110010011000010111001"
|
||||
"0000010000010000011001111010011110111001000000000110101000110001"
|
||||
"0110111110110110100000010100000011010001000011100100111001001011"
|
||||
"1101100100000100010000001011100010010001101111100100101100010001"
|
||||
"0000011110010110111010110110111110011110011010001100100111110101"
|
||||
"1000010010000000000100101011110001000101101101100000010011111011"
|
||||
"1011111010110100100111100111110011100001111101111110000110001100"
|
||||
"0001000111000111101110000111011011101010100010100101100111010100"
|
||||
"0101111110110010110000111111011001101110101101100100100011000100"
|
||||
"1000110010101001000100001001101000011111101011111100100000100101"
|
||||
"1100001100111001011111001101000111011101011101000110010110110110"
|
||||
"0111001010011010010000010110000110010101101100101110111100100011"
|
||||
"0010111110011100010100000101100101110101101011110100100000110110"
|
||||
"1001101110101001001111111000010100011100100000101000111101101111"
|
||||
"0101111011110001101010111010001000111010101111001101100100100100"
|
||||
"1110110111001100011010110000101000011001011100101100111101110000"
|
||||
"1010101111011110000111011011011110000111010110110111111010101010"
|
||||
"0111100101111001010111101000001010100000111010111100111011111001"
|
||||
"0110111000000110100011011100101101010101101000010010011111100100"
|
||||
"0010111000001011101110000110010011101001111010100111110111110101"
|
||||
"1110111000000000101011000100101010000110110111101010011001111010"
|
||||
"1101011110001110000011010111001100001100101000000101000101000010"
|
||||
"0101000011011111010010110010000010101100001110011000110111110111"
|
||||
"1110010101011110111001100010110101101011100111100011101010001011"
|
||||
"0101110010100110101100111100010000111101111100000111000110110110"
|
||||
"1001100111000000011010100000011101011000010010011010001011110000"
|
||||
"1100100111111001001000011100110000011110001100000000010000001001"
|
||||
"1110000000110010000010011010100011011011000000000111110000110111"
|
||||
"0101110011001101010110010100011001110110000110010001100110011111";
|
||||
|
||||
// Encrypt our cleartext bits
|
||||
const Flexblock ciphertext_bits = cipher.Encipher(cleartext_bits);
|
||||
|
||||
// Decipher it again
|
||||
const Flexblock decryptedBits = cipher.Decipher(ciphertext_bits);
|
||||
|
||||
// Assert that the decrypted text equals the plaintext
|
||||
Assert::AreEqual(
|
||||
cleartext_bits,
|
||||
decryptedBits
|
||||
);
|
||||
}
|
||||
|
||||
// Tests that a decrypted ciphertext equals its plaintrext version, using a cleartext that requires A LOT of blocks
|
||||
TEST_METHOD(MultiBlock_Padding)
|
||||
{
|
||||
// Instanciate our cipher and supply a key
|
||||
const Block key = PasswordToKey("1234");
|
||||
const Cipher cipher(key);
|
||||
|
||||
// Recode the ascii-string to bits
|
||||
const Flexblock cleartext_bits =
|
||||
"1011110011010110000010110001111000111010111101001010100100011101"
|
||||
"0101110101010010100000110100001000011000111010001001110101111111"
|
||||
"1110110101100101110001010101011110001010000010111110011011010111"
|
||||
"1100110100111000000011100101010100110010001110010011000010111001"
|
||||
"0000010000010000011001111010011110111001000000000110101000110001"
|
||||
"0110111110110110100000010100000011010001000011100100111001001011"
|
||||
"1101100100000100010000001011100010010001101111100100101100010001"
|
||||
"0000011110010110111010110110111110011110011010001100100111110101"
|
||||
"1000010010000000000100101011110001000101101101100000010011111011"
|
||||
"1011111010110100100111100111110011100001111101111110000110001100"
|
||||
"0001000111000111101110000111011011101010100010100101100111010100"
|
||||
"0101111110110010110000111111011001101110101101100100100011000100"
|
||||
"1000110010101001000100001001101000011111101011111100100000100101"
|
||||
"1100001100111001011111001101000111011101011101000110010110110110"
|
||||
"0111001010011010010000010110000110010101101100101110111100100011"
|
||||
"0010111110011100010100000101100101110101101011110100100000110110"
|
||||
"1001101110101001001111111000010100011100100000101000111101101111"
|
||||
"0101111011110001101010111010001000111010101111001101100100100100"
|
||||
"1110110111001100011010110000101000011001011100101100111101110000"
|
||||
"1010101111011110000111011011011110000111010110110111111010101010"
|
||||
"0111100101111001010111101000001010100000111010111100111011111001"
|
||||
"0110111000000110100011011100101101010101101000010010011111100100"
|
||||
"0010111000001011101110000110010011101001111010100111110111110101"
|
||||
"1110111000000000101011000100101010000110110111101010011001111010"
|
||||
"1101011110001110000011010111001100001100101000000101000101000010"
|
||||
"0101000011011111010010110010000010101100001110011000110111110111"
|
||||
"1110010101011110111001100010110101101011100111100011101010001011"
|
||||
"0101110010100110101100111100010000111101111100000111000110110110"
|
||||
"1001100111000000011010100000011101011000010010011010001011110000"
|
||||
"1100100111111001001000011100110000011110001100000000010000001001"
|
||||
"11100000001100100000100110101000110110110000000001111100001";
|
||||
|
||||
const Flexblock cleartext_bits_EXPECTED_RESULT =
|
||||
"1011110011010110000010110001111000111010111101001010100100011101"
|
||||
"0101110101010010100000110100001000011000111010001001110101111111"
|
||||
"1110110101100101110001010101011110001010000010111110011011010111"
|
||||
"1100110100111000000011100101010100110010001110010011000010111001"
|
||||
"0000010000010000011001111010011110111001000000000110101000110001"
|
||||
"0110111110110110100000010100000011010001000011100100111001001011"
|
||||
"1101100100000100010000001011100010010001101111100100101100010001"
|
||||
"0000011110010110111010110110111110011110011010001100100111110101"
|
||||
"1000010010000000000100101011110001000101101101100000010011111011"
|
||||
"1011111010110100100111100111110011100001111101111110000110001100"
|
||||
"0001000111000111101110000111011011101010100010100101100111010100"
|
||||
"0101111110110010110000111111011001101110101101100100100011000100"
|
||||
"1000110010101001000100001001101000011111101011111100100000100101"
|
||||
"1100001100111001011111001101000111011101011101000110010110110110"
|
||||
"0111001010011010010000010110000110010101101100101110111100100011"
|
||||
"0010111110011100010100000101100101110101101011110100100000110110"
|
||||
"1001101110101001001111111000010100011100100000101000111101101111"
|
||||
"0101111011110001101010111010001000111010101111001101100100100100"
|
||||
"1110110111001100011010110000101000011001011100101100111101110000"
|
||||
"1010101111011110000111011011011110000111010110110111111010101010"
|
||||
"0111100101111001010111101000001010100000111010111100111011111001"
|
||||
"0110111000000110100011011100101101010101101000010010011111100100"
|
||||
"0010111000001011101110000110010011101001111010100111110111110101"
|
||||
"1110111000000000101011000100101010000110110111101010011001111010"
|
||||
"1101011110001110000011010111001100001100101000000101000101000010"
|
||||
"0101000011011111010010110010000010101100001110011000110111110111"
|
||||
"1110010101011110111001100010110101101011100111100011101010001011"
|
||||
"0101110010100110101100111100010000111101111100000111000110110110"
|
||||
"1001100111000000011010100000011101011000010010011010001011110000"
|
||||
"1100100111111001001000011100110000011110001100000000010000001001"
|
||||
"1110000000110010000010011010100011011011000000000111110000100000"
|
||||
"0000000000000000000000000000000000000000000000000000000000000000";
|
||||
|
||||
// Encrypt our cleartext bits
|
||||
const Flexblock ciphertext_bits = cipher.Encipher(cleartext_bits);
|
||||
|
||||
// Decipher it again
|
||||
const Flexblock decryptedBits = cipher.Decipher(ciphertext_bits);
|
||||
|
||||
// Assert that the decrypted text equals the plaintext
|
||||
Assert::AreEqual(
|
||||
cleartext_bits_EXPECTED_RESULT,
|
||||
decryptedBits
|
||||
);
|
||||
}
|
||||
};
|
||||
}
|
81
GCryptLib/test/GCWrapper.cpp
Normal file
81
GCryptLib/test/GCWrapper.cpp
Normal file
@@ -0,0 +1,81 @@
|
||||
#include "CppUnitTest.h"
|
||||
#include "../GhettoCrypt/GhettoCryptWrapper.h"
|
||||
#include "../GhettoCrypt/Flexblock.h"
|
||||
#include "../GhettoCrypt/Util.h"
|
||||
|
||||
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
|
||||
using namespace GhettoCipher;
|
||||
|
||||
namespace SimpleTests
|
||||
{
|
||||
TEST_CLASS(GCWrapper)
|
||||
{
|
||||
public:
|
||||
|
||||
// Tests that encrypting and decrypting strings using the wrapper works.
|
||||
// This test will start from scratch after encryption, to ensure EVERYTHING has to be re-calculated.
|
||||
TEST_METHOD(String)
|
||||
{
|
||||
// Setup
|
||||
const std::string plaintext = "Hello, World!";
|
||||
const std::string password = "Der Affe will Zucker";
|
||||
|
||||
std::string ciphertext;
|
||||
std::string decrypted;
|
||||
|
||||
// Encryption
|
||||
{
|
||||
ciphertext = GhettoCryptWrapper::EncryptString(plaintext, password);
|
||||
}
|
||||
|
||||
// Decryption
|
||||
{
|
||||
decrypted = GhettoCryptWrapper::DecryptString(ciphertext, password);
|
||||
}
|
||||
|
||||
// Assertion
|
||||
Assert::AreEqual(
|
||||
plaintext,
|
||||
decrypted
|
||||
);
|
||||
}
|
||||
|
||||
// Tests that encrypting and decrypting files using the wrapper works.
|
||||
// This test will start from scratch after encryption, to ensure EVERYTHING has to be re-calculated.
|
||||
TEST_METHOD(File)
|
||||
{
|
||||
// Setup
|
||||
#if defined _WIN64
|
||||
const std::string testfile_dir = "../../SimpleTests/";
|
||||
#elif defined _WIN32
|
||||
const std::string testfile_dir = "../SimpleTests/";
|
||||
#endif
|
||||
|
||||
const std::string filename_plain = testfile_dir + "testfile.png";
|
||||
const std::string filename_encrypted = testfile_dir + "testfile.png.crypt";
|
||||
const std::string filename_decrypted = testfile_dir + "testfile.png.clear.png";
|
||||
const std::string password = "Der Affe will Zucker";
|
||||
|
||||
|
||||
// Encryption
|
||||
{
|
||||
GhettoCryptWrapper::EncryptFile(filename_plain, filename_encrypted, password);
|
||||
}
|
||||
|
||||
// Decryption
|
||||
{
|
||||
GhettoCryptWrapper::DecryptFile(filename_encrypted, filename_decrypted, password);
|
||||
}
|
||||
|
||||
// Read in both the base, and the decrypted file
|
||||
const Flexblock plainfile = ReadFileToBits(filename_plain);
|
||||
const Flexblock decryptfile = ReadFileToBits(filename_decrypted);
|
||||
|
||||
// Assertion (If this fails, maybe check if the image is even readable by an image viewer)
|
||||
Assert::AreEqual(
|
||||
PadStringToLength(plainfile, decryptfile.length(), '0', false),
|
||||
decryptfile
|
||||
);
|
||||
}
|
||||
};
|
||||
}
|
110
GCryptLib/test/Password2Key_CollisionResistance.cpp
Normal file
110
GCryptLib/test/Password2Key_CollisionResistance.cpp
Normal file
@@ -0,0 +1,110 @@
|
||||
#include "CppUnitTest.h"
|
||||
#include "../GhettoCrypt/Util.h"
|
||||
#include "../GhettoCrypt/Config.h"
|
||||
#include <unordered_map>
|
||||
#include <codecvt>
|
||||
#include <sstream>
|
||||
|
||||
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
|
||||
using namespace GhettoCipher;
|
||||
|
||||
// We can generate passwords by just translating a decimal number to base "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
inline std::string Base10_2_X(const unsigned long long int i, const std::string set, unsigned int padding)
|
||||
{
|
||||
if (set.length() == 0)
|
||||
return ""; // Return empty string, if set is empty. Play stupid games, win stupid prizes.
|
||||
|
||||
std::stringstream ss;
|
||||
|
||||
if (i != 0)
|
||||
{
|
||||
{
|
||||
unsigned long long int buf = i;
|
||||
while (buf != 0)
|
||||
{
|
||||
const unsigned long long int mod = buf % set.length();
|
||||
buf /= set.length();
|
||||
ss << set[(std::size_t)mod];
|
||||
}
|
||||
}
|
||||
{
|
||||
const std::string buf = ss.str();
|
||||
ss.str("");
|
||||
for (long long int i = buf.length() - 1; i >= 0; i--)
|
||||
ss << buf[(std::size_t)i];
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ss << set[0]; // If i is 0, just pass a null-value. The algorithm would hang otherwise.
|
||||
}
|
||||
|
||||
// Add as much null-values to the left as requested.
|
||||
if (ss.str().length() < padding)
|
||||
{
|
||||
const std::size_t cachedLen = ss.str().length();
|
||||
const std::string cachedStr = ss.str();
|
||||
ss.str("");
|
||||
for (std::size_t i = 0; i < padding - cachedLen; i++)
|
||||
ss << set[0];
|
||||
ss << cachedStr;
|
||||
}
|
||||
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
using convert_t = std::codecvt_utf8<wchar_t>;
|
||||
|
||||
namespace SimpleTests
|
||||
{
|
||||
TEST_CLASS(Password2Key)
|
||||
{
|
||||
public:
|
||||
|
||||
// Run a few thousand random passwords through the keygen and see if we'll find a collision.
|
||||
// This test passing does NOT mean that it's resistant! Maybe good, maybe shit! But if it fails, it's definitely shit.
|
||||
// Already validated range: Password 0 - 1.000.000
|
||||
TEST_METHOD(CollisionResistance)
|
||||
{
|
||||
// To test resistence set this to a high number around a million.
|
||||
// This will take a LONG while to execute though (about 2.5hrs on my machine), hence why it's set so low.
|
||||
constexpr std::size_t NUM_RUN_TESTS = 1000;
|
||||
|
||||
std::unordered_map<std::bitset<BLOCK_SIZE>, std::string> keys; // <key, password>
|
||||
|
||||
// Try NUM_RUN_TESTS passwords
|
||||
const std::string charset = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
|
||||
std::wstring_convert<convert_t, wchar_t> strconverter;
|
||||
|
||||
for (std::size_t i = 0; i < NUM_RUN_TESTS; i++)
|
||||
{
|
||||
// Get password
|
||||
const std::string password = Base10_2_X(i, charset, 0);
|
||||
|
||||
// Generate key
|
||||
const std::bitset<BLOCK_SIZE> newKey = PasswordToKey(password).Get();
|
||||
|
||||
// Check if this block is already in our map
|
||||
if (keys.find(newKey) != keys.cend())
|
||||
{
|
||||
std::wstringstream wss;
|
||||
wss << "Collision found between password \""
|
||||
<< strconverter.from_bytes(password)
|
||||
<< "\" and \""
|
||||
<< strconverter.from_bytes(keys[newKey])
|
||||
<< "\". The key is \""
|
||||
<< newKey
|
||||
<< "\".";
|
||||
|
||||
Assert::Fail(wss.str().c_str());
|
||||
}
|
||||
|
||||
// All good? Insert it into our map
|
||||
keys[newKey] = password;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
};
|
||||
}
|
BIN
GCryptLib/test/testfile.png
Normal file
BIN
GCryptLib/test/testfile.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 9.5 KiB |
Reference in New Issue
Block a user