Got rid of flexblocks

This commit is contained in:
Leonetienne
2022-05-26 15:47:24 +02:00
parent 143ec19bf3
commit e7c1e17e2c
14 changed files with 160 additions and 454 deletions

View File

@@ -10,6 +10,10 @@ namespace Leonetienne::GCrypt {
*/
class Feistel {
public:
//! Empty initializer. If you use this, you must call SetKey()!
Feistel();
//! Will initialize the feistel cipher with a key
explicit Feistel(const Key& key);
Feistel(const Feistel& other) = delete;
@@ -59,6 +63,8 @@ namespace Leonetienne::GCrypt {
void ZeroKeyMemory();
Keyset roundKeys;
bool isInitialized = false;
};
}

View File

@@ -1,16 +0,0 @@
#ifndef GCRYPT_FLEXBLOCK_H
#define GCRYPT_FLEXBLOCK_H
#include <string>
namespace Leonetienne::GCrypt {
//! A type used for conveying "bitstrings". e.g. "10101001001"
//! These should generally not be used, as they are really really slow.
//! The only valid usecase I can think of is when using GHash for example, because for hashing
//! an absolute input length is required.
//! If you need to, you can use the StringToBits() and BitsToString() functions defined in Util.h.
typedef std::string Flexblock;
}
#endif

View File

@@ -2,7 +2,6 @@
#define GCRYPT_GCIPHER_H
#include "GCrypt/Feistel.h"
#include "GCrypt/Flexblock.h"
namespace Leonetienne::GCrypt {
/** Class to apply a block/-stream cipher to messages of arbitrary length in a distributed manner
@@ -15,6 +14,9 @@ namespace Leonetienne::GCrypt {
DECIPHER
};
//! Empty initializer. If you use this, you must call Initialize()!
GCipher();
//! Will initialize this cipher with a key
explicit GCipher(const Key& key, const DIRECTION direction);
@@ -30,6 +32,11 @@ namespace Leonetienne::GCrypt {
void operator=(const GCipher& other);
//! Will initialize the cipher with a key, and a mode.
//! If called on an existing object, it will reset its state.
void Initialize(const Key& key, const DIRECTION direction);
private:
DIRECTION direction;
@@ -38,6 +45,8 @@ namespace Leonetienne::GCrypt {
//! The last block, required for CBC.
Block lastBlock;
bool isInitialized = false;
};
}

View File

@@ -1,7 +1,6 @@
#ifndef GCRYPT_GHASH_H
#define GCRYPT_GHASH_H
#include "GCrypt/Flexblock.h"
#include "GCrypt/Block.h"
#include "GCrypt/GCipher.h"
#include <vector>

View File

@@ -36,7 +36,12 @@ namespace Leonetienne::GCrypt {
}
// Transform to bytes
const std::string bytes = BitsToBytes(ss.str());
const std::string bits = ss.str();
ss.str("");
for (std::size_t i = 0; i < bits.size(); i += 8) {
ss << (char)std::bitset<8>(bits.substr(i, 8)).to_ulong();
}
const std::string bytes = ss.str();
// Cram bytes into type
T t;

View File

@@ -1,10 +1,11 @@
#pragma once
#include <string>
#include "GCrypt/Flexblock.h"
#ifndef GCRYPT_GWRAPPER_H
#define GCRYPT_GWRAPPER_H
#include "GCrypt/Block.h"
#include "GCrypt/GCipher.h"
#include "GCrypt/Key.h"
#include <string>
#include <vector>
namespace Leonetienne::GCrypt {
/** This class is a wrapper to make working with the GhettoCipher
@@ -31,7 +32,7 @@ namespace Leonetienne::GCrypt {
static bool DecryptFile(const std::string& filename_in, const std::string& filename_out, const Key& key, bool printProgressReport = false);
//! Will enncrypt or decrypt an entire flexblock of binary data, given a key.
static Flexblock CipherFlexblock(const Flexblock& data, const Key& key, const GCipher::DIRECTION direction);
static std::vector<Block> CipherBlocks(const std::vector<Block>& data, const Key& key, const GCipher::DIRECTION direction);
private:
@@ -40,3 +41,5 @@ namespace Leonetienne::GCrypt {
};
}
#endif

View File

@@ -7,7 +7,6 @@
#include <cstring>
#include <vector>
#include "GCrypt/Block.h"
#include "GCrypt/Flexblock.h"
#include "GCrypt/Config.h"
#include "GCrypt/GCipher.h"
#include "GCrypt/InitializationVector.h"
@@ -21,56 +20,16 @@ namespace Leonetienne::GCrypt {
//! 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);
//! Will convert a string to a fixed-size data block
//! @s: The string to pad
//! padLeft: should padding be added to the left? If not, to the right.
Block StringToBitblock(const std::string& s, bool padLeft = true);
//! Will convert a string to a flexible data block
Flexblock StringToBits(const std::string& s);
//! Will convert a string to a vector of blocks
std::vector<Block> StringToBitblocks(const std::string& str);
//! Will convert a fixed-size data block to a bytestring
std::string BitblockToBytes(const Block& block);
//! Will convert an array of data blocks to a bytestring
std::string BitblocksToBytes(const std::vector<Block>& bits);
//! Will convert a fixed-size data blocks to a textstring
//! The difference to BitblockToBytes() is, that it strips excess nullbytes
std::string BitblockToString(const Block& block);
//! Will convert an array of blocks to a character-string
//! The difference to BitblocksToBytes() is, that it strips excess nullbytes
std::string BitblocksToString(const std::vector<Block>& blocks);
//! Will convert a flexible data block to a bytestring
std::string BitsToBytes(const Flexblock& bits);
//! Will convert a flexible data block to a string
//! The difference to BitsToBytes() is, that it strips excess nullbytes
std::string BitsToString(const Flexblock& bits);
//! Turns a fixed-size data block into a hex-string
std::string BitblockToHexstring(const Block& b);
//! Turns a flexible data block into a hex-string
std::string BitsToHexstring(const Flexblock& b);
//! Turns a hex string into a fixed-size data block
Block HexstringToBitblock(const std::string& hexstring);
//! Turns a hex string into a flexible data block
Flexblock HexstringToBits(const std::string& hexstring);
//! Will read a file into a flexblock
Flexblock ReadFileToBits(const std::string& filepath);
//! Will save bits to a binary file
void WriteBitsToFile(const std::string& filepath, const Flexblock& bits);
//! Will read a file directly to data blocks, and yield the amount of bytes read
std::vector<Block> ReadFileToBlocks(const std::string& filepath, std::size_t& bytes_read);