Now using a SecureBitset class that zeroes memory in dtor.
This commit is contained in:
parent
1931602a38
commit
81bf6c7aee
@ -1,6 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <GhettoCryptWrapper.h>
|
#include <GhettoCryptWrapper.h>
|
||||||
|
#include <SecureBitset.h>
|
||||||
|
|
||||||
using namespace GhettoCipher;
|
using namespace GhettoCipher;
|
||||||
|
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <bitset>
|
#include "SecureBitset.h"
|
||||||
#include "Config.h"
|
#include "Config.h"
|
||||||
|
|
||||||
namespace GhettoCipher
|
namespace GhettoCipher
|
||||||
{
|
{
|
||||||
typedef std::bitset<BLOCK_SIZE> Block;
|
typedef SecureBitset<BLOCK_SIZE> Block;
|
||||||
}
|
}
|
||||||
|
@ -125,7 +125,7 @@ GhettoCipher::Halfblock GhettoCipher::Feistel::CompressionFunction(const Block&
|
|||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
const std::string bits = block.to_string();
|
const std::string bits = block.to_string();
|
||||||
|
|
||||||
// We have to double the bits!
|
// We have to half the bits!
|
||||||
for (std::size_t i = 0; i < bits.size(); i += 4)
|
for (std::size_t i = 0; i < bits.size(); i += 4)
|
||||||
{
|
{
|
||||||
const std::string sub = bits.substr(i, 4);
|
const std::string sub = bits.substr(i, 4);
|
||||||
@ -191,13 +191,6 @@ void GhettoCipher::Feistel::GenerateRoundKeys(const Block& seedKey)
|
|||||||
return;
|
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 GhettoCipher::Feistel::ZeroKeyMemory()
|
void GhettoCipher::Feistel::ZeroKeyMemory()
|
||||||
{
|
{
|
||||||
for (Block& key : roundKeys)
|
for (Block& key : roundKeys)
|
||||||
@ -205,8 +198,3 @@ void GhettoCipher::Feistel::ZeroKeyMemory()
|
|||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
#if defined _WIN32 || defined _WIN64
|
|
||||||
#pragma optimize("", on )
|
|
||||||
#elif defined __GNUG__
|
|
||||||
#pragma GCC pop_options
|
|
||||||
#endif
|
|
||||||
|
@ -141,8 +141,8 @@
|
|||||||
</ItemDefinitionGroup>
|
</ItemDefinitionGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClCompile Include="Feistel.cpp" />
|
<ClCompile Include="Feistel.cpp" />
|
||||||
<ClCompile Include="GhettoCryptWrapper.cpp" />
|
|
||||||
<ClCompile Include="Cipher.cpp" />
|
<ClCompile Include="Cipher.cpp" />
|
||||||
|
<ClCompile Include="GhettoCryptWrapper.cpp" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="Block.h" />
|
<ClInclude Include="Block.h" />
|
||||||
@ -153,6 +153,7 @@
|
|||||||
<ClInclude Include="Flexblock.h" />
|
<ClInclude Include="Flexblock.h" />
|
||||||
<ClInclude Include="Halfblock.h" />
|
<ClInclude Include="Halfblock.h" />
|
||||||
<ClInclude Include="Keyset.h" />
|
<ClInclude Include="Keyset.h" />
|
||||||
|
<ClInclude Include="SecureBitset.h" />
|
||||||
<ClInclude Include="Util.h" />
|
<ClInclude Include="Util.h" />
|
||||||
<ClInclude Include="Version.h" />
|
<ClInclude Include="Version.h" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
@ -56,5 +56,8 @@
|
|||||||
<ClInclude Include="GhettoCryptWrapper.h">
|
<ClInclude Include="GhettoCryptWrapper.h">
|
||||||
<Filter>Headerdateien</Filter>
|
<Filter>Headerdateien</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
<ClInclude Include="SecureBitset.h">
|
||||||
|
<Filter>Headerdateien</Filter>
|
||||||
|
</ClInclude>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
@ -1,10 +1,10 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <bitset>
|
#include "SecureBitset.h"
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include "Config.h"
|
#include "Config.h"
|
||||||
|
|
||||||
namespace GhettoCipher
|
namespace GhettoCipher
|
||||||
{
|
{
|
||||||
constexpr std::size_t HALFBLOCK_SIZE = (BLOCK_SIZE / 2);
|
constexpr std::size_t HALFBLOCK_SIZE = (BLOCK_SIZE / 2);
|
||||||
typedef std::bitset<HALFBLOCK_SIZE> Halfblock;
|
typedef SecureBitset<HALFBLOCK_SIZE> Halfblock;
|
||||||
}
|
}
|
||||||
|
324
GhettoCrypt/SecureBitset.h
Normal file
324
GhettoCrypt/SecureBitset.h
Normal file
@ -0,0 +1,324 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <bitset>
|
||||||
|
#include <ostream>
|
||||||
|
#include <istream>
|
||||||
|
|
||||||
|
namespace GhettoCipher
|
||||||
|
{
|
||||||
|
/** 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();
|
||||||
|
}
|
||||||
|
}
|
@ -2,6 +2,7 @@
|
|||||||
#include <bitset>
|
#include <bitset>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
#include "SecureBitset.h"
|
||||||
#include "Block.h"
|
#include "Block.h"
|
||||||
#include "Flexblock.h"
|
#include "Flexblock.h"
|
||||||
|
|
||||||
@ -15,7 +16,7 @@ namespace GhettoCipher
|
|||||||
|
|
||||||
//! Will perform a wrapping left-bitshift on a bitset
|
//! Will perform a wrapping left-bitshift on a bitset
|
||||||
template <std::size_t T>
|
template <std::size_t T>
|
||||||
inline std::bitset<T> Shiftl(const std::bitset<T>& bits, const std::size_t amount)
|
inline SecureBitset<T> Shiftl(const SecureBitset<T>& bits, const std::size_t amount)
|
||||||
{
|
{
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
const std::string bitss = bits.to_string();
|
const std::string bitss = bits.to_string();
|
||||||
@ -23,12 +24,12 @@ namespace GhettoCipher
|
|||||||
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())];
|
ss << bitss[Mod((int)(i + amount), (int)bitss.size())];
|
||||||
|
|
||||||
return std::bitset<T>(ss.str());
|
return SecureBitset<T>(ss.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
//! Will perform a wrapping right-bitshift on a bitset
|
//! Will perform a wrapping right-bitshift on a bitset
|
||||||
template <std::size_t T>
|
template <std::size_t T>
|
||||||
inline std::bitset<T> Shiftr(const std::bitset<T>& bits, const std::size_t amount)
|
inline SecureBitset<T> Shiftr(const SecureBitset<T>& bits, const std::size_t amount)
|
||||||
{
|
{
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
const std::string bitss = bits.to_string();
|
const std::string bitss = bits.to_string();
|
||||||
@ -36,7 +37,7 @@ namespace GhettoCipher
|
|||||||
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())];
|
ss << bitss[Mod((i - amount), bitss.size())];
|
||||||
|
|
||||||
return std::bitset<T>(ss.str());
|
return SecureBitset<T>(ss.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
//! Will pad a string to a set length with a certain character
|
//! Will pad a string to a set length with a certain character
|
||||||
|
@ -1,2 +1,2 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#define GHETTOCRYPT_VERSION 0.1
|
#define GHETTOCRYPT_VERSION 0.12
|
||||||
|
@ -289,7 +289,7 @@ GhettoCipher::Halfblock GhettoCipher::Feistel::CompressionFunction(const Block&
|
|||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
const std::string bits = block.to_string();
|
const std::string bits = block.to_string();
|
||||||
|
|
||||||
// We have to double the bits!
|
// We have to half the bits!
|
||||||
for (std::size_t i = 0; i < bits.size(); i += 4)
|
for (std::size_t i = 0; i < bits.size(); i += 4)
|
||||||
{
|
{
|
||||||
const std::string sub = bits.substr(i, 4);
|
const std::string sub = bits.substr(i, 4);
|
||||||
@ -355,13 +355,6 @@ void GhettoCipher::Feistel::GenerateRoundKeys(const Block& seedKey)
|
|||||||
return;
|
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 GhettoCipher::Feistel::ZeroKeyMemory()
|
void GhettoCipher::Feistel::ZeroKeyMemory()
|
||||||
{
|
{
|
||||||
for (Block& key : roundKeys)
|
for (Block& key : roundKeys)
|
||||||
@ -369,11 +362,6 @@ void GhettoCipher::Feistel::ZeroKeyMemory()
|
|||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
#if defined _WIN32 || defined _WIN64
|
|
||||||
#pragma optimize("", on )
|
|
||||||
#elif defined __GNUG__
|
|
||||||
#pragma GCC pop_options
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
/*** ./../GhettoCrypt/GhettoCryptWrapper.cpp ***/
|
/*** ./../GhettoCrypt/GhettoCryptWrapper.cpp ***/
|
||||||
|
@ -28,11 +28,6 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
/*** ./../GhettoCrypt/Version.h ***/
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
#define GHETTOCRYPT_VERSION 0.1
|
|
||||||
|
|
||||||
/*** ./../GhettoCrypt/GhettoCryptWrapper.h ***/
|
/*** ./../GhettoCrypt/GhettoCryptWrapper.h ***/
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
@ -91,14 +86,340 @@ namespace GhettoCipher
|
|||||||
constexpr std::size_t N_ROUNDS = 64;
|
constexpr std::size_t N_ROUNDS = 64;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*** ./../GhettoCrypt/Block.h ***/
|
/*** ./../GhettoCrypt/SecureBitset.h ***/
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
#include <bitset>
|
#include <bitset>
|
||||||
|
#include <ostream>
|
||||||
|
#include <istream>
|
||||||
|
|
||||||
namespace GhettoCipher
|
namespace GhettoCipher
|
||||||
{
|
{
|
||||||
typedef std::bitset<BLOCK_SIZE> Block;
|
/** 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*** ./../GhettoCrypt/Block.h ***/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
namespace GhettoCipher
|
||||||
|
{
|
||||||
|
typedef SecureBitset<BLOCK_SIZE> Block;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*** ./../GhettoCrypt/Util.h ***/
|
/*** ./../GhettoCrypt/Util.h ***/
|
||||||
@ -118,7 +439,7 @@ namespace GhettoCipher
|
|||||||
|
|
||||||
//! Will perform a wrapping left-bitshift on a bitset
|
//! Will perform a wrapping left-bitshift on a bitset
|
||||||
template <std::size_t T>
|
template <std::size_t T>
|
||||||
inline std::bitset<T> Shiftl(const std::bitset<T>& bits, const std::size_t amount)
|
inline SecureBitset<T> Shiftl(const SecureBitset<T>& bits, const std::size_t amount)
|
||||||
{
|
{
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
const std::string bitss = bits.to_string();
|
const std::string bitss = bits.to_string();
|
||||||
@ -126,12 +447,12 @@ namespace GhettoCipher
|
|||||||
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())];
|
ss << bitss[Mod((int)(i + amount), (int)bitss.size())];
|
||||||
|
|
||||||
return std::bitset<T>(ss.str());
|
return SecureBitset<T>(ss.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
//! Will perform a wrapping right-bitshift on a bitset
|
//! Will perform a wrapping right-bitshift on a bitset
|
||||||
template <std::size_t T>
|
template <std::size_t T>
|
||||||
inline std::bitset<T> Shiftr(const std::bitset<T>& bits, const std::size_t amount)
|
inline SecureBitset<T> Shiftr(const SecureBitset<T>& bits, const std::size_t amount)
|
||||||
{
|
{
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
const std::string bitss = bits.to_string();
|
const std::string bitss = bits.to_string();
|
||||||
@ -139,7 +460,7 @@ namespace GhettoCipher
|
|||||||
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())];
|
ss << bitss[Mod((i - amount), bitss.size())];
|
||||||
|
|
||||||
return std::bitset<T>(ss.str());
|
return SecureBitset<T>(ss.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
//! Will pad a string to a set length with a certain character
|
//! Will pad a string to a set length with a certain character
|
||||||
@ -362,6 +683,11 @@ namespace GhettoCipher
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*** ./../GhettoCrypt/Version.h ***/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#define GHETTOCRYPT_VERSION 0.12
|
||||||
|
|
||||||
/*** ./../GhettoCrypt/Keyset.h ***/
|
/*** ./../GhettoCrypt/Keyset.h ***/
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
@ -375,13 +701,12 @@ namespace GhettoCipher
|
|||||||
/*** ./../GhettoCrypt/Halfblock.h ***/
|
/*** ./../GhettoCrypt/Halfblock.h ***/
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
#include <bitset>
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
|
||||||
namespace GhettoCipher
|
namespace GhettoCipher
|
||||||
{
|
{
|
||||||
constexpr std::size_t HALFBLOCK_SIZE = (BLOCK_SIZE / 2);
|
constexpr std::size_t HALFBLOCK_SIZE = (BLOCK_SIZE / 2);
|
||||||
typedef std::bitset<HALFBLOCK_SIZE> Halfblock;
|
typedef SecureBitset<HALFBLOCK_SIZE> Halfblock;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*** ./../GhettoCrypt/Feistel.h ***/
|
/*** ./../GhettoCrypt/Feistel.h ***/
|
||||||
|
Loading…
x
Reference in New Issue
Block a user