Now using a SecureBitset class that zeroes memory in dtor.

This commit is contained in:
Leonetienne 2021-12-13 14:40:29 +01:00
parent 1931602a38
commit 81bf6c7aee
11 changed files with 680 additions and 49 deletions

View File

@ -1,6 +1,7 @@
#pragma once
#include <iostream>
#include <GhettoCryptWrapper.h>
#include <SecureBitset.h>
using namespace GhettoCipher;

View File

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

View File

@ -125,7 +125,7 @@ GhettoCipher::Halfblock GhettoCipher::Feistel::CompressionFunction(const Block&
std::stringstream ss;
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)
{
const std::string sub = bits.substr(i, 4);
@ -191,13 +191,6 @@ void GhettoCipher::Feistel::GenerateRoundKeys(const Block& seedKey)
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()
{
for (Block& key : roundKeys)
@ -205,8 +198,3 @@ void GhettoCipher::Feistel::ZeroKeyMemory()
return;
}
#if defined _WIN32 || defined _WIN64
#pragma optimize("", on )
#elif defined __GNUG__
#pragma GCC pop_options
#endif

View File

@ -141,8 +141,8 @@
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="Feistel.cpp" />
<ClCompile Include="GhettoCryptWrapper.cpp" />
<ClCompile Include="Cipher.cpp" />
<ClCompile Include="GhettoCryptWrapper.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="Block.h" />
@ -153,6 +153,7 @@
<ClInclude Include="Flexblock.h" />
<ClInclude Include="Halfblock.h" />
<ClInclude Include="Keyset.h" />
<ClInclude Include="SecureBitset.h" />
<ClInclude Include="Util.h" />
<ClInclude Include="Version.h" />
</ItemGroup>

View File

@ -56,5 +56,8 @@
<ClInclude Include="GhettoCryptWrapper.h">
<Filter>Headerdateien</Filter>
</ClInclude>
<ClInclude Include="SecureBitset.h">
<Filter>Headerdateien</Filter>
</ClInclude>
</ItemGroup>
</Project>

View File

@ -1,10 +1,10 @@
#pragma once
#include <bitset>
#include "SecureBitset.h"
#include <cstdint>
#include "Config.h"
namespace GhettoCipher
{
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
View 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();
}
}

View File

@ -2,6 +2,7 @@
#include <bitset>
#include <sstream>
#include <fstream>
#include "SecureBitset.h"
#include "Block.h"
#include "Flexblock.h"
@ -15,7 +16,7 @@ namespace GhettoCipher
//! Will perform a wrapping left-bitshift on a bitset
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;
const std::string bitss = bits.to_string();
@ -23,12 +24,12 @@ namespace GhettoCipher
for (std::size_t i = 0; i < bitss.size(); i++)
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
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;
const std::string bitss = bits.to_string();
@ -36,7 +37,7 @@ namespace GhettoCipher
for (std::size_t i = 0; i < bitss.size(); i++)
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

View File

@ -1,2 +1,2 @@
#pragma once
#define GHETTOCRYPT_VERSION 0.1
#define GHETTOCRYPT_VERSION 0.12

View File

@ -289,7 +289,7 @@ GhettoCipher::Halfblock GhettoCipher::Feistel::CompressionFunction(const Block&
std::stringstream ss;
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)
{
const std::string sub = bits.substr(i, 4);
@ -355,13 +355,6 @@ void GhettoCipher::Feistel::GenerateRoundKeys(const Block& seedKey)
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()
{
for (Block& key : roundKeys)
@ -369,11 +362,6 @@ void GhettoCipher::Feistel::ZeroKeyMemory()
return;
}
#if defined _WIN32 || defined _WIN64
#pragma optimize("", on )
#elif defined __GNUG__
#pragma GCC pop_options
#endif
/*** ./../GhettoCrypt/GhettoCryptWrapper.cpp ***/

View File

@ -28,11 +28,6 @@
#pragma once
/*** ./../GhettoCrypt/Version.h ***/
#pragma once
#define GHETTOCRYPT_VERSION 0.1
/*** ./../GhettoCrypt/GhettoCryptWrapper.h ***/
#pragma once
@ -91,14 +86,340 @@ namespace GhettoCipher
constexpr std::size_t N_ROUNDS = 64;
}
/*** ./../GhettoCrypt/Block.h ***/
/*** ./../GhettoCrypt/SecureBitset.h ***/
#pragma once
#include <bitset>
#include <ostream>
#include <istream>
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 ***/
@ -118,7 +439,7 @@ namespace GhettoCipher
//! Will perform a wrapping left-bitshift on a bitset
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;
const std::string bitss = bits.to_string();
@ -126,12 +447,12 @@ namespace GhettoCipher
for (std::size_t i = 0; i < bitss.size(); i++)
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
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;
const std::string bitss = bits.to_string();
@ -139,7 +460,7 @@ namespace GhettoCipher
for (std::size_t i = 0; i < bitss.size(); i++)
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
@ -362,6 +683,11 @@ namespace GhettoCipher
}
}
/*** ./../GhettoCrypt/Version.h ***/
#pragma once
#define GHETTOCRYPT_VERSION 0.12
/*** ./../GhettoCrypt/Keyset.h ***/
#pragma once
@ -375,13 +701,12 @@ namespace GhettoCipher
/*** ./../GhettoCrypt/Halfblock.h ***/
#pragma once
#include <bitset>
#include <cstdint>
namespace GhettoCipher
{
constexpr std::size_t HALFBLOCK_SIZE = (BLOCK_SIZE / 2);
typedef std::bitset<HALFBLOCK_SIZE> Halfblock;
typedef SecureBitset<HALFBLOCK_SIZE> Halfblock;
}
/*** ./../GhettoCrypt/Feistel.h ***/