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
13 changed files with 25 additions and 365 deletions

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();
}