Replaced halfblock with instanciation of Basic_Block

This commit is contained in:
Leonetienne 2022-05-26 00:55:24 +02:00
parent 1f913b3a54
commit 8ddd9d6bfb
No known key found for this signature in database
GPG Key ID: C33879CD92E9708C
13 changed files with 25 additions and 365 deletions

View File

@ -165,6 +165,7 @@ namespace Leonetienne::GCrypt {
static constexpr std::size_t CHUNK_SIZE = sizeof(T);
static constexpr std::size_t CHUNK_SIZE_BITS = CHUNK_SIZE * 8;
static constexpr std::size_t BLOCK_SIZE_BITS = CHUNK_SIZE_BITS * 16;
friend std::ostream& operator<<(std::ostream& os, const Basic_Block<T>& b) {
for (std::size_t i = 0; i < b.data.size(); i++) {
@ -180,13 +181,13 @@ namespace Leonetienne::GCrypt {
// Instantiate templates
template class Basic_Block<std::uint32_t>;
//template class Basic_Block<std::uint16_t>;
template class Basic_Block<std::uint16_t>;
//! This a full-sized 512-bit block
typedef Basic_Block<std::uint32_t> Block;
//! This is a half-block used within the feistel class
//typedef Basic_Block<std::uint16_t> Halfblock;
typedef Basic_Block<std::uint16_t> Halfblock;
}
#endif

View File

@ -4,9 +4,6 @@
#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 = 6;
}

View File

@ -4,7 +4,6 @@
#include "GCrypt/Keyset.h"
#include "GCrypt/Block.h"
#include "GCrypt/Key.h"
#include "GCrypt/Halfblock.h"
namespace Leonetienne::GCrypt {
/** Class to perform a feistel block chipher

View File

@ -1,9 +0,0 @@
#pragma once
#include <cstdint>
#include "GCrypt/SecureBitset.h"
#include "GCrypt/Config.h"
namespace Leonetienne::GCrypt {
constexpr std::size_t HALFBLOCK_SIZE = (BLOCK_SIZE / 2);
typedef SecureBitset<HALFBLOCK_SIZE> Halfblock;
}

View File

@ -1,292 +0,0 @@
#ifndef GCRYPT_SECUREBITSET_H
#define GCRYPT_SECUREBITSET_H
#include <bitset>
#include <ostream>
#include <istream>
#include <vector>
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) const;
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) const {
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();
}
}
#endif

View File

@ -5,7 +5,6 @@
#include <sstream>
#include <fstream>
#include <cstring>
#include "GCrypt/SecureBitset.h"
#include "GCrypt/Block.h"
#include "GCrypt/Flexblock.h"
#include "GCrypt/Config.h"
@ -18,43 +17,6 @@ namespace Leonetienne::GCrypt {
return (denominator + (numerator % denominator)) % denominator;
}
inline Block Shiftl(const Block& bits, const std::size_t amount) {
std::stringstream ss;
const std::string bitss = bits.ToString();
for (std::size_t i = 0; i < bitss.size(); i++) {
ss << bitss[Mod((int)(i + amount), (int)bitss.size())];
}
return Block(ss.str());
}
//! 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
std::string PadStringToLength(const std::string& str, const std::size_t len, const char pad, const bool padLeft = true);

View File

@ -28,7 +28,7 @@ namespace Leonetienne::GCrypt {
template <typename T>
void Basic_Block<T>::FromString(const std::string& str) {
assert(str.length() == BLOCK_SIZE);
assert(str.length() == BLOCK_SIZE_BITS);
for (std::size_t i = 0; i < data.size(); i++) {
data[i] = std::bitset<CHUNK_SIZE_BITS>(

View File

@ -78,7 +78,7 @@ namespace Leonetienne::GCrypt {
// Non-linearly apply subsitution boxes
std::stringstream ss;
const std::string m_str = m_expanded.ToString();
for (std::size_t i = 0; i < BLOCK_SIZE; i += 4) {
for (std::size_t i = 0; i < Block::BLOCK_SIZE_BITS; i += 4) {
ss << SBox(m_str.substr(i, 4));
}
m_expanded = Block(ss.str());
@ -98,12 +98,12 @@ namespace Leonetienne::GCrypt {
}
Block Feistel::FeistelCombine(const Halfblock& l, const Halfblock& r) {
return Block(l.to_string() + r.to_string());
return Block(l.ToString() + r.ToString());
}
Block Feistel::ExpansionFunction(const Halfblock& block) {
std::stringstream ss;
const std::string bits = block.to_string();
const std::string bits = block.ToString();
std::unordered_map<std::string, std::string> expansionMap;
expansionMap["00"] = "1101";
@ -112,7 +112,7 @@ namespace Leonetienne::GCrypt {
expansionMap["11"] = "0111";
// We have to double the bits!
for (std::size_t i = 0; i < HALFBLOCK_SIZE; i += 2) {
for (std::size_t i = 0; i < Halfblock::BLOCK_SIZE_BITS; i += 2) {
const std::string sub = bits.substr(i, 2);
ss << expansionMap[sub];
}
@ -143,7 +143,7 @@ namespace Leonetienne::GCrypt {
compressionMap["1111"] = "01";
// We have to half the bits!
for (std::size_t i = 0; i < BLOCK_SIZE; i += 4) {
for (std::size_t i = 0; i < Block::BLOCK_SIZE_BITS; i += 4) {
const std::string sub = bits.substr(i, 4);
ss << compressionMap[sub];
}

View File

@ -1,6 +1,7 @@
#include "GCrypt/GHash.h"
#include "GCrypt/Util.h"
#include "GCrypt/InitializationVector.h"
#include <vector>
namespace Leonetienne::GCrypt {
@ -34,9 +35,9 @@ namespace Leonetienne::GCrypt {
// Split input into blocks
std::vector<Block> blocks;
for (std::size_t i = 0; i < data.size(); i += BLOCK_SIZE) {
for (std::size_t i = 0; i < data.size(); i += Block::BLOCK_SIZE_BITS) {
blocks.push_back(Block(
PadStringToLength(data.substr(i, BLOCK_SIZE), BLOCK_SIZE, '0', false))
PadStringToLength(data.substr(i, Block::BLOCK_SIZE_BITS), Block::BLOCK_SIZE_BITS, '0', false))
);
}

View File

@ -21,7 +21,7 @@ namespace Leonetienne::GCrypt {
bool GPrng::GetBit() {
// If we have no more bits to go, create new ones
if (nextBit >= BLOCK_SIZE) {
if (nextBit >= Block::BLOCK_SIZE_BITS) {
AdvanceBlock();
}
@ -51,18 +51,18 @@ namespace Leonetienne::GCrypt {
// Slurp up the rest of the current block
std::stringstream ss;
const std::size_t bitsLeft = BLOCK_SIZE - nextBit;
const std::size_t bitsLeft = Block::BLOCK_SIZE_BITS - nextBit;
ss << hasher.GetHashsum().ToString().substr(nextBit, bitsLeft);
// Now we have to advance to the next block
AdvanceBlock();
// Now, grab the remaining bits
const std::size_t remainingBits = BLOCK_SIZE - bitsLeft;
const std::size_t remainingBits = Block::BLOCK_SIZE_BITS - bitsLeft;
ss << hasher.GetHashsum().ToString().substr(0, remainingBits);
// Assert that we have the correct number of bits
assert(ss.str().length() == BLOCK_SIZE);
assert(ss.str().length() == Block::BLOCK_SIZE_BITS);
// Set out bitpointer
nextBit = remainingBits;

View File

@ -1,6 +1,7 @@
#include "GCrypt/GWrapper.h"
#include "GCrypt/GCipher.h"
#include "GCrypt/Util.h"
#include <vector>
namespace Leonetienne::GCrypt {
@ -92,9 +93,9 @@ namespace Leonetienne::GCrypt {
// Split input into blocks
std::vector<Block> blocks;
for (std::size_t i = 0; i < data.size(); i += BLOCK_SIZE) {
for (std::size_t i = 0; i < data.size(); i += Block::BLOCK_SIZE_BITS) {
blocks.push_back(Block(
PadStringToLength(data.substr(i, BLOCK_SIZE), BLOCK_SIZE, '0', false))
PadStringToLength(data.substr(i, Block::BLOCK_SIZE_BITS), Block::BLOCK_SIZE_BITS, '0', false))
);
}

View File

@ -21,12 +21,12 @@ namespace Leonetienne::GCrypt {
// Fetch BLOCK_SIZE bits
std::stringstream ss;
for (std::size_t i = 0; i < BLOCK_SIZE / bitsPerCall; i++) {
for (std::size_t i = 0; i < Key::BLOCK_SIZE_BITS / bitsPerCall; i++) {
ss << std::bitset<bitsPerCall>(rng());
}
// Verify that we actually have the correct size
assert(ss.str().length() == BLOCK_SIZE);
assert(ss.str().length() == Key::BLOCK_SIZE_BITS);
// Return them as a key
return Key(Block(ss.str()));
@ -34,7 +34,7 @@ namespace Leonetienne::GCrypt {
Key Key::LoadFromFile(const std::string& path) {
// Read this many chars
const std::size_t maxChars = BLOCK_SIZE / 8;
const std::size_t maxChars = Key::BLOCK_SIZE_BITS / 8;
// Open ifilestream for keyfile
std::ifstream ifs(path, std::ios::in | std::ios::binary);
@ -73,7 +73,7 @@ namespace Leonetienne::GCrypt {
std::ofstream ofs(path, std::ios::out | std::ios::binary);
// Write the key
ofs.write(keybytes.data(), BLOCK_SIZE/8);
ofs.write(keybytes.data(), Key::BLOCK_SIZE_BITS / 8);
// Close the file handle
ofs.close();

View File

@ -37,7 +37,7 @@ namespace Leonetienne::GCrypt {
}
// Pad rest with zeores
return Block(PadStringToLength(ss.str(), BLOCK_SIZE, '0', padLeft));
return Block(PadStringToLength(ss.str(), Block::BLOCK_SIZE_BITS, '0', padLeft));
}
Flexblock StringToBits(const std::string& s) {
@ -55,7 +55,7 @@ namespace Leonetienne::GCrypt {
const std::string bitstring = bits.ToString();
for (std::size_t i = 0; i < BLOCK_SIZE; i += 8) {
for (std::size_t i = 0; i < Block::BLOCK_SIZE_BITS; i += 8) {
ss << (char)std::bitset<8>(bitstring.substr(i, 8)).to_ulong();
}