Gcryptlib: new brace style, and moved to Leonetienne namespace
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
#pragma once
|
||||
#include "SecureBitset.h"
|
||||
#include "Config.h"
|
||||
|
||||
namespace GhettoCipher {
|
||||
typedef SecureBitset<BLOCK_SIZE> Block;
|
||||
}
|
||||
|
||||
#pragma once
|
||||
#include "SecureBitset.h"
|
||||
#include "Config.h"
|
||||
|
||||
namespace Leonetienne::GCrypt {
|
||||
typedef SecureBitset<BLOCK_SIZE> Block;
|
||||
}
|
||||
|
||||
|
@@ -1,39 +1,39 @@
|
||||
#pragma once
|
||||
#include "Feistel.h"
|
||||
#include "Flexblock.h"
|
||||
|
||||
namespace GhettoCipher {
|
||||
/** 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;
|
||||
};
|
||||
}
|
||||
#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;
|
||||
};
|
||||
}
|
||||
|
@@ -1,10 +1,10 @@
|
||||
#pragma once
|
||||
#include <cstddef>
|
||||
|
||||
namespace GhettoCipher {
|
||||
// MUST BE A POWER OF 2 > 4
|
||||
constexpr std::size_t BLOCK_SIZE = 512;
|
||||
|
||||
// MUST BE > 2
|
||||
constexpr std::size_t N_ROUNDS = 64;
|
||||
}
|
||||
#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;
|
||||
}
|
||||
|
@@ -1,59 +1,59 @@
|
||||
#pragma once
|
||||
#include "Keyset.h"
|
||||
#include "Block.h"
|
||||
#include "Halfblock.h"
|
||||
|
||||
namespace GhettoCipher {
|
||||
/** Class to perform a feistel block chipher
|
||||
*/
|
||||
class Feistel {
|
||||
public:
|
||||
explicit Feistel(const Block& key);
|
||||
|
||||
Feistel(const Feistel& other) = delete;
|
||||
Feistel(Feistel&& other) noexcept = delete;
|
||||
|
||||
~Feistel();
|
||||
|
||||
//! 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;
|
||||
};
|
||||
}
|
||||
#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;
|
||||
};
|
||||
}
|
||||
|
@@ -1,7 +1,7 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
|
||||
namespace GhettoCipher {
|
||||
//! A "bitset" of variable length
|
||||
typedef std::string Flexblock;
|
||||
}
|
||||
#pragma once
|
||||
#include <string>
|
||||
|
||||
namespace Leonetienne::GCrypt {
|
||||
//! A "bitset" of variable length
|
||||
typedef std::string Flexblock;
|
||||
}
|
||||
|
@@ -1,32 +1,32 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
|
||||
namespace GhettoCipher {
|
||||
/** This class is a wrapper to make working with the GhettoCipher
|
||||
* super easy with a python-like syntax
|
||||
*/
|
||||
class GhettoCryptWrapper {
|
||||
public:
|
||||
//! Will encrypt a string and return it hexadecimally encoded.
|
||||
static std::string EncryptString(const std::string& cleartext, const std::string& password);
|
||||
|
||||
//! Will decrypt a hexadecimally encoded string.
|
||||
static std::string DecryptString(const std::string& ciphertext, const std::string& password);
|
||||
|
||||
//! Will 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();
|
||||
};
|
||||
}
|
||||
#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();
|
||||
};
|
||||
}
|
||||
|
@@ -1,9 +1,9 @@
|
||||
#pragma once
|
||||
#include "SecureBitset.h"
|
||||
#include <cstdint>
|
||||
#include "Config.h"
|
||||
|
||||
namespace GhettoCipher {
|
||||
constexpr std::size_t HALFBLOCK_SIZE = (BLOCK_SIZE / 2);
|
||||
typedef SecureBitset<HALFBLOCK_SIZE> Halfblock;
|
||||
}
|
||||
#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;
|
||||
}
|
||||
|
@@ -1,17 +1,17 @@
|
||||
#pragma once
|
||||
#include "Config.h"
|
||||
#include "Block.h"
|
||||
|
||||
namespace GhettoCipher {
|
||||
/** Will create a sudo-random Block based on a seed
|
||||
*/
|
||||
class InitializationVector {
|
||||
public:
|
||||
InitializationVector(const GhettoCipher::Block& seed);
|
||||
|
||||
operator GhettoCipher::Block() const;
|
||||
|
||||
private:
|
||||
GhettoCipher::Block iv;
|
||||
};
|
||||
}
|
||||
#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;
|
||||
};
|
||||
}
|
||||
|
@@ -1,8 +1,8 @@
|
||||
#pragma once
|
||||
#include <array>
|
||||
#include "Block.h"
|
||||
#include "Config.h"
|
||||
|
||||
namespace GhettoCipher {
|
||||
typedef std::array<Block, N_ROUNDS> Keyset;
|
||||
}
|
||||
#pragma once
|
||||
#include <array>
|
||||
#include "Block.h"
|
||||
#include "Config.h"
|
||||
|
||||
namespace Leonetienne::GCrypt {
|
||||
typedef std::array<Block, N_ROUNDS> Keyset;
|
||||
}
|
||||
|
@@ -1,286 +1,286 @@
|
||||
#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();
|
||||
}
|
||||
}
|
||||
#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();
|
||||
}
|
||||
}
|
||||
|
@@ -10,7 +10,7 @@
|
||||
#include "Cipher.h"
|
||||
#include "InitializationVector.h"
|
||||
|
||||
namespace GhettoCipher {
|
||||
namespace Leonetienne::GCrypt {
|
||||
//! Mod-operator that works with negative values
|
||||
inline int Mod(const int numerator, const int denominator) {
|
||||
return (denominator + (numerator % denominator)) % denominator;
|
||||
|
@@ -1,3 +1,3 @@
|
||||
#pragma once
|
||||
#define GHETTOCRYPT_VERSION 0.21
|
||||
|
||||
#pragma once
|
||||
#define GHETTOCRYPT_VERSION 0.21
|
||||
|
||||
|
Reference in New Issue
Block a user