Refactored to feistel class

This commit is contained in:
Leonetienne 2021-12-05 22:40:36 +01:00
parent a6a7cb5c35
commit 2eb93c064c
11 changed files with 722 additions and 319 deletions

5
Block.h Normal file
View File

@ -0,0 +1,5 @@
#pragma once
#include <bitset>
#include "Config.h"
typedef std::bitset<BLOCK_SIZE> Block;

4
Config.h Normal file
View File

@ -0,0 +1,4 @@
#pragma once
#define BLOCK_SIZE 128
#define N_ROUNDS 64

201
Feistel.cpp Normal file
View File

@ -0,0 +1,201 @@
#include "Feistel.h"
#include "Util.h"
#include "Config.h"
Feistel::Feistel(const Block& key)
{
SetKey(key);
return;
}
Feistel::~Feistel()
{
ZeroKeyMemory();
return;
}
void Feistel::SetKey(const Block& key)
{
GenerateRoundKeys(key);
return;
}
Block Feistel::Encipher(const Block& data)
{
return Run(data, false);
}
Block Feistel::Decipher(const Block& data)
{
return Run(data, true);
}
Block Feistel::Run(const Block& data, bool reverseKeys)
{
const auto splitData = FeistelSplit(data);
Halfblock l = splitData.first;
Halfblock r = splitData.second;
Halfblock tmp;
for (std::size_t i = 0; i < N_ROUNDS; i++)
{
// Calculate key index
std::size_t keyIndex;
if (reverseKeys)
keyIndex = N_ROUNDS - i - 1;
else
keyIndex = i;
// Do a feistel round
tmp = r;
r = l ^ F(r, roundKeys[keyIndex]);
l = tmp;
}
return FeistelCombine(r, l);
}
Halfblock Feistel::F(Halfblock m, const Block& key)
{
// Made-up F function
// Expand to full bitwidth
Block m_expanded = ExpansionFunction(m);
// Shift to left by 1
m_expanded = Shiftl(m_expanded, 1);
// Xor with key
m_expanded ^= key;
// Non-linearly apply subsitution boxes
std::stringstream ss;
const std::string m_str = m_expanded.to_string();
for (std::size_t i = 0; i < m_str.size(); i += 4)
{
ss << SBox(m_str.substr(i, 4));
}
m_expanded = Block(ss.str());
// Return the compressed version
return CompressionFunction(m_expanded);
}
std::pair<Halfblock, Halfblock> Feistel::FeistelSplit(const Block& block)
{
const std::string bits = block.to_string();
Halfblock l(bits.substr(0, bits.size() / 2));
Halfblock r(bits.substr(bits.size() / 2));
return std::make_pair(l, r);
}
Block Feistel::FeistelCombine(const Halfblock& l, const Halfblock& r)
{
return Block(l.to_string() + r.to_string());
}
Block Feistel::ExpansionFunction(const Halfblock& block)
{
std::stringstream ss;
const std::string bits = block.to_string();
// We have to double the bits!
for (std::size_t i = 0; i < bits.size(); i += 2)
{
const std::string sub = bits.substr(i, 2);
if (sub == "00") ss << "1101";
else if (sub == "01") ss << "1000";
else if (sub == "10") ss << "0010";
else if (sub == "11") ss << "0111";
}
return Block(ss.str());
}
Halfblock Feistel::CompressionFunction(const Block& block)
{
std::stringstream ss;
const std::string bits = block.to_string();
// We have to double the bits!
for (std::size_t i = 0; i < bits.size(); i += 4)
{
const std::string sub = bits.substr(i, 4);
if (sub == "0000") ss << "10";
else if (sub == "0001") ss << "01";
else if (sub == "0010") ss << "10";
else if (sub == "0011") ss << "10";
else if (sub == "0100") ss << "11";
else if (sub == "0101") ss << "01";
else if (sub == "0110") ss << "00";
else if (sub == "0111") ss << "11";
else if (sub == "1000") ss << "01";
else if (sub == "1001") ss << "00";
else if (sub == "1010") ss << "11";
else if (sub == "1011") ss << "00";
else if (sub == "1100") ss << "11";
else if (sub == "1101") ss << "10";
else if (sub == "1110") ss << "00";
else if (sub == "1111") ss << "01";
}
return Halfblock(ss.str());
}
std::string Feistel::SBox(const std::string& in)
{
if (in == "0000") return "1100";
else if (in == "0001") return "1000";
else if (in == "0010") return "0001";
else if (in == "0011") return "0111";
else if (in == "0100") return "1011";
else if (in == "0101") return "0011";
else if (in == "0110") return "1101";
else if (in == "0111") return "1111";
else if (in == "1000") return "0000";
else if (in == "1001") return "1010";
else if (in == "1010") return "0100";
else if (in == "1011") return "1001";
else if (in == "1100") return "0010";
else if (in == "1101") return "1110";
else if (in == "1110") return "0101";
else /*if (in == "1111")*/ return "0110";
}
void Feistel::GenerateRoundKeys(const Block& seedKey)
{
// Clear initial key memory
ZeroKeyMemory();
roundKeys = Keyset();
// Generate new keys from the seed key
roundKeys[0] = seedKey;
roundKeys[1] = (Shiftl(seedKey, 32) ^ roundKeys[0]);
for (std::size_t i = 2; i < roundKeys.size(); i++)
{
roundKeys[i] = Shiftl(roundKeys[i - 1], i + 32) ^ roundKeys[i - 2];
}
return;
}
// These pragmas only work for MSVC, as far as i know. Beware!!!
#pragma optimize( "", off )
void Feistel::ZeroKeyMemory()
{
for (Block& key : roundKeys)
key.reset();
return;
}
#pragma optimize( "", on )

51
Feistel.h Normal file
View File

@ -0,0 +1,51 @@
#pragma once
#include "Keyset.h"
#include "Block.h"
#include "Halfblock.h"
class Feistel
{
public:
Feistel(const Block& key);
~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;
};

View File

@ -139,8 +139,23 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="all.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="Feistel.cpp" />
<ClCompile Include="main.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="Block.h" />
<ClInclude Include="Config.h" />
<ClInclude Include="Feistel.h" />
<ClInclude Include="Halfblock.h" />
<ClInclude Include="Keyset.h" />
<ClInclude Include="Util.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>

View File

@ -15,8 +15,34 @@
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="Feistel.cpp">
<Filter>Quelldateien</Filter>
</ClCompile>
<ClCompile Include="all.cpp">
<Filter>Quelldateien</Filter>
</ClCompile>
<ClCompile Include="main.cpp">
<Filter>Quelldateien</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Feistel.h">
<Filter>Headerdateien</Filter>
</ClInclude>
<ClInclude Include="Keyset.h">
<Filter>Headerdateien</Filter>
</ClInclude>
<ClInclude Include="Block.h">
<Filter>Headerdateien</Filter>
</ClInclude>
<ClInclude Include="Config.h">
<Filter>Headerdateien</Filter>
</ClInclude>
<ClInclude Include="Halfblock.h">
<Filter>Headerdateien</Filter>
</ClInclude>
<ClInclude Include="Util.h">
<Filter>Headerdateien</Filter>
</ClInclude>
</ItemGroup>
</Project>

7
Halfblock.h Normal file
View File

@ -0,0 +1,7 @@
#pragma once
#include <bitset>
#include "Config.h"
#define HALFBLOCK_SIZE (BLOCK_SIZE / 2)
typedef std::bitset<HALFBLOCK_SIZE> Halfblock;

6
Keyset.h Normal file
View File

@ -0,0 +1,6 @@
#pragma once
#include <array>
#include "Block.h"
#include "Config.h"
typedef std::array<Block, N_ROUNDS> Keyset;

78
Util.h Normal file
View File

@ -0,0 +1,78 @@
#pragma once
#include <bitset>
#include <sstream>
#include "Block.h"
//! Mod-operator that works with negative values
inline int Mod(int numerator, int denominator)
{
return (denominator + (numerator % denominator)) % denominator;
}
//! Will perform a wrapping left-bitshift on a bitset
template <std::size_t T>
inline std::bitset<T> Shiftl(const std::bitset<T>& bits, 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 std::bitset<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, 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 std::bitset<T>(ss.str());
}
//! Will convert a string to a data block
inline Block StringToBits(const std::string& s)
{
std::stringstream ss;
for (std::size_t i = 0; i < s.size(); i++)
ss << std::bitset<8>(s[i]);
// Pad rest with zeores
for (std::size_t i = s.size() * 8; i < BLOCK_SIZE; i++)
ss << '0';
return Block(ss.str());
}
//! Will convert a data block to a string
inline std::string BitsToString(const Block& bits)
{
std::stringstream ss;
const std::string bitstring = bits.to_string();
for (std::size_t i = 0; i < BLOCK_SIZE; i += 8)
{
ss << (char)std::bitset<8>(bitstring.substr(i, 8)).to_ulong();
}
return ss.str();
}
//! Creates a key of size key-size from a password of arbitrary length.
inline Block PasswordToKey(const std::string& in)
{
Block b;
// Segment the password in segments of key-size, and xor them together.
for (std::size_t i = 0; i < in.size(); i += BLOCK_SIZE / 8)
b ^= StringToBits(in.substr(i, BLOCK_SIZE / 8));
return b;
}

325
all.cpp Normal file
View File

@ -0,0 +1,325 @@
#include <iostream>
#include <sstream>
#include <array>
#include <bitset>
#define BLOCK_SIZE 128
#define HALFBLOCK_SIZE (BLOCK_SIZE / 2)
#define N_ROUNDS 64
typedef std::bitset<BLOCK_SIZE> Block;
typedef std::bitset<HALFBLOCK_SIZE> Halfblock;
typedef std::array<Block, N_ROUNDS> Keyset;
// Will convert a string to a data block
Block StringToBits(const std::string& s);
// Will convert a data block to a string
std::string BitsToString(const Block& bits);
// Split a data block into two half blocks (into L and R)
std::pair<Halfblock, Halfblock> FeistelSplit(const Block& block);
// Combine two half blocks (L and R) into a regular data block
Block FeistelCombine(const Halfblock& l, const Halfblock& r);
// Creates a key of size key-size from a password of arbitrary length.
Block PasswordToKey(const std::string& in);
// Will generate a keyset from a seed-key
Keyset GenerateRoundkeys(const Block& seedKey);
// Feistel-cipher
Block Feistel(const Block& data, const Keyset& keys, bool reverseKeyOrder = false);
// Will expand a halfblock to a fullblock
Block ExpansionFunction(const Halfblock& block);
// Will compress a fullblock to a halfblock
Halfblock CompressionFunction(const Block& block);
// Substitutes four bits by static random others
std::string SBox(const std::string& in);
// Arbitrary cipher function
Halfblock F(Halfblock m, const Block& key);
template<std::size_t T>
std::bitset<T> Shiftl(const std::bitset<T>& bits, std::size_t amount);
template<std::size_t T>
std::bitset<T> Shiftr(const std::bitset<T>& bits, std::size_t amount);
std::string DiffusionCheck(const std::string& asciiMessage);
int Mod(int numerator, int denominator)
{
return (denominator + (numerator % denominator)) % denominator;
}
int main()
{
const std::string cipher1 = DiffusionCheck("Hello, World :3");
std::cout << std::endl;
const std::string cipher2 = DiffusionCheck("Hello, world :3");
std::size_t numDiff = 0;
for (std::size_t i = 0; i < cipher1.size(); i++)
if (cipher1[i] != cipher2[i])
numDiff++;
std::cout << std::endl;
std::cout << "Total difference between C1 and C2: " << numDiff << std::endl;
return 0;
}
std::string DiffusionCheck(const std::string& asciiMessage)
{
Block message = StringToBits(asciiMessage);
const Block seedKey = PasswordToKey("Ich bin ein PASSWORT-SCHLÜSSEL!");
Keyset roundkeys = GenerateRoundkeys(seedKey);
//std::cout << "Keys: " << std::endl;
//for (std::size_t i = 0; i < roundkeys.size(); i++)
// std::cout << roundkeys[i] << std::endl;
//std::cout << "---" << std::endl;
//exit(0);
std::cout << "Message ascii: " << asciiMessage << std::endl;
std::cout << "Message: " << message << std::endl;
Block ciphertext = Feistel(message, roundkeys);
std::cout << "Ciphertext: " << ciphertext << std::endl;
Block decrypted = Feistel(ciphertext, roundkeys, true);
std::cout << "Decrypted: " << decrypted << std::endl;
const std::string asciiDecrypted = BitsToString(decrypted);
std::cout << "Decrypted ascii: " << asciiDecrypted << std::endl;
return ciphertext.to_string();
}
Block Feistel(const Block& data, const Keyset& keys, bool reverseKeyOrder)
{
const auto splitData = FeistelSplit(data);
Halfblock l = splitData.first;
Halfblock r = splitData.second;
Halfblock tmp;
for (std::size_t i = 0; i < N_ROUNDS; i++)
{
// Calculate key index
std::size_t keyIndex;
if (reverseKeyOrder)
keyIndex = N_ROUNDS - i - 1;
else
keyIndex = i;
// Do a feistel round
tmp = r;
r = l ^ F(r, keys[keyIndex]);
l = tmp;
}
return FeistelCombine(r, l);
}
Halfblock F(Halfblock m, const Block& key)
{
// Made-up F function
// Expand to full bitwidht
Block m_expanded = ExpansionFunction(m);
// Shift to left by 1
m_expanded = Shiftl(m_expanded, 1);
// Xor with key
m_expanded ^= key;
// Non-linearly apply subsitution boxes
std::stringstream ss;
const std::string m_str = m_expanded.to_string();
for (std::size_t i = 0; i < m_str.size(); i += 4)
{
ss << SBox(m_str.substr(i, 4));
}
m_expanded = Block(ss.str());
// Return the compressed version
return CompressionFunction(m_expanded);
}
Block ExpansionFunction(const Halfblock& block)
{
std::stringstream ss;
const std::string bits = block.to_string();
// We have to double the bits!
for (std::size_t i = 0; i < bits.size(); i += 2)
{
const std::string sub = bits.substr(i, 2);
if (sub == "00") ss << "1101";
else if (sub == "01") ss << "1000";
else if (sub == "10") ss << "0010";
else /*if (sub == "11")*/ ss << "0111";
}
return Block(ss.str());
}
Halfblock CompressionFunction(const Block& block)
{
std::stringstream ss;
const std::string bits = block.to_string();
// We have to double the bits!
for (std::size_t i = 0; i < bits.size(); i += 4)
{
const std::string sub = bits.substr(i, 4);
if (sub == "0000") ss << "10";
else if (sub == "0001") ss << "01";
else if (sub == "0010") ss << "10";
else if (sub == "0011") ss << "10";
else if (sub == "0100") ss << "11";
else if (sub == "0101") ss << "01";
else if (sub == "0110") ss << "00";
else if (sub == "0111") ss << "11";
else if (sub == "1000") ss << "01";
else if (sub == "1001") ss << "00";
else if (sub == "1010") ss << "11";
else if (sub == "1011") ss << "00";
else if (sub == "1100") ss << "11";
else if (sub == "1101") ss << "10";
else if (sub == "1110") ss << "00";
else /*if (sub == "1111")*/ ss << "01";
}
return Halfblock(ss.str());
}
std::string SBox(const std::string& in)
{
if (in == "0000") return "1100";
else if (in == "0001") return "1000";
else if (in == "0010") return "0001";
else if (in == "0011") return "0111";
else if (in == "0100") return "1011";
else if (in == "0101") return "0011";
else if (in == "0110") return "1101";
else if (in == "0111") return "1111";
else if (in == "1000") return "0000";
else if (in == "1001") return "1010";
else if (in == "1010") return "0100";
else if (in == "1011") return "1001";
else if (in == "1100") return "0010";
else if (in == "1101") return "1110";
else if (in == "1110") return "0101";
else /*if (in == "1111")*/ return "0110";
}
Block StringToBits(const std::string& s)
{
std::stringstream ss;
for (std::size_t i = 0; i < s.size(); i++)
ss << std::bitset<8>(s[i]);
// Pad rest with zeores
for (std::size_t i = s.size() * 8; i < BLOCK_SIZE; i++)
ss << '0';
return Block(ss.str());
}
std::string BitsToString(const Block& bits)
{
std::stringstream ss;
const std::string bitstring = bits.to_string();
for (std::size_t i = 0; i < BLOCK_SIZE; i += 8)
{
ss << (char)std::bitset<8>(bitstring.substr(i, 8)).to_ulong();
}
return ss.str();
}
std::pair<Halfblock, Halfblock> FeistelSplit(const Block& block)
{
const std::string bits = block.to_string();
Halfblock l(bits.substr(0, bits.size() / 2));
Halfblock r(bits.substr(bits.size() / 2));
return std::make_pair(l, r);
}
Block FeistelCombine(const Halfblock& l, const Halfblock& r)
{
return Block(l.to_string() + r.to_string());
}
Keyset GenerateRoundkeys(const Block& seedKey)
{
Keyset keys;
keys[0] = seedKey;
keys[1] = (Shiftl(seedKey, 32) ^ keys[0]);
for (std::size_t i = 2; i < keys.size(); i++)
{
keys[i] = Shiftl(keys[i-1], i + 32) ^ keys[i-2];
}
return keys;
}
template <std::size_t T>
std::bitset<T> Shiftl(const std::bitset<T>& bits, 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 std::bitset<T>(ss.str());
}
template <std::size_t T>
std::bitset<T> Shiftr(const std::bitset<T>& bits, 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 std::bitset<T>(ss.str());
}
Block PasswordToKey(const std::string& in)
{
Block b;
// Segment the password in segments of key-size, and xor them together.
for (std::size_t i = 0; i < in.size(); i += BLOCK_SIZE / 8)
b ^= StringToBits(in.substr(i, BLOCK_SIZE / 8));
return b;
}

321
main.cpp
View File

@ -1,325 +1,10 @@
#pragma once
#include <iostream>
#include <sstream>
#include <array>
#include <bitset>
#define BLOCK_SIZE 128
#define HALFBLOCK_SIZE (BLOCK_SIZE / 2)
#define N_ROUNDS 128
typedef std::bitset<BLOCK_SIZE> Block;
typedef std::bitset<HALFBLOCK_SIZE> Halfblock;
typedef std::array<Block, N_ROUNDS> Keyset;
// Will convert a string to a data block
Block StringToBits(const std::string& s);
// Will convert a data block to a string
std::string BitsToString(const Block& bits);
// Split a data block into two half blocks (into L and R)
std::pair<Halfblock, Halfblock> FeistelSplit(const Block& block);
// Combine two half blocks (L and R) into a regular data block
Block FeistelCombine(const Halfblock& l, const Halfblock& r);
// Creates a key of size key-size from a password of arbitrary length.
Block PasswordToKey(const std::string& in);
// Will generate a keyset from a seed-key
Keyset GenerateRoundkeys(const Block& seedKey);
// Feistel-cipher
Block Feistel(const Block& data, const Keyset& keys, bool reverseKeyOrder = false);
// Will expand a halfblock to a fullblock
Block ExpansionFunction(const Halfblock& block);
// Will compress a fullblock to a halfblock
Halfblock CompressionFunction(const Block& block);
// Substitutes four bits by static random others
std::string SBox(const std::string& in);
// Arbitrary cipher function
Halfblock F(Halfblock m, const Block& key);
template<std::size_t T>
std::bitset<T> Shiftl(const std::bitset<T>& bits, std::size_t amount);
template<std::size_t T>
std::bitset<T> Shiftr(const std::bitset<T>& bits, std::size_t amount);
std::string DiffusionCheck(const std::string& asciiMessage);
int Mod(int numerator, int denominator)
{
return (denominator + (numerator % denominator)) % denominator;
}
#include "Util.h"
#include "Feistel.h"
int main()
{
const std::string cipher1 = DiffusionCheck("Hello, World :3");
std::cout << std::endl;
const std::string cipher2 = DiffusionCheck("Hello, world :3");
std::size_t numDiff = 0;
for (std::size_t i = 0; i < cipher1.size(); i++)
if (cipher1[i] != cipher2[i])
numDiff++;
std::cout << std::endl;
std::cout << "Total difference between C1 and C2: " << numDiff << std::endl;
return 0;
}
std::string DiffusionCheck(const std::string& asciiMessage)
{
Block message = StringToBits(asciiMessage);
const Block seedKey = PasswordToKey("Ich bin ein PASSWORT-SCHLÜSSEL!");
Keyset roundkeys = GenerateRoundkeys(seedKey);
//std::cout << "Keys: " << std::endl;
//for (std::size_t i = 0; i < roundkeys.size(); i++)
// std::cout << roundkeys[i] << std::endl;
//std::cout << "---" << std::endl;
//exit(0);
std::cout << "Message ascii: " << asciiMessage << std::endl;
std::cout << "Message: " << message << std::endl;
Block ciphertext = Feistel(message, roundkeys);
std::cout << "Ciphertext: " << ciphertext << std::endl;
Block decrypted = Feistel(ciphertext, roundkeys, true);
std::cout << "Decrypted: " << decrypted << std::endl;
const std::string asciiDecrypted = BitsToString(decrypted);
std::cout << "Decrypted ascii: " << asciiDecrypted << std::endl;
return ciphertext.to_string();
}
Block Feistel(const Block& data, const Keyset& keys, bool reverseKeyOrder)
{
const auto splitData = FeistelSplit(data);
Halfblock l = splitData.first;
Halfblock r = splitData.second;
Halfblock tmp;
for (std::size_t i = 0; i < N_ROUNDS; i++)
{
// Calculate key index
std::size_t keyIndex;
if (reverseKeyOrder)
keyIndex = N_ROUNDS - i - 1;
else
keyIndex = i;
// Do a feistel round
tmp = r;
r = l ^ F(r, keys[keyIndex]);
l = tmp;
}
return FeistelCombine(r, l);
}
Halfblock F(Halfblock m, const Block& key)
{
// Made-up F function
// Expand to full bitwidht
Block m_expanded = ExpansionFunction(m);
// Shift to left by 1
m_expanded = Shiftl(m_expanded, 1);
// Xor with key
m_expanded ^= key;
// Non-linearly apply subsitution boxes
std::stringstream ss;
const std::string m_str = m_expanded.to_string();
for (std::size_t i = 0; i < m_str.size(); i += 4)
{
ss << SBox(m_str.substr(i, 4));
}
m_expanded = Block(ss.str());
// Return the compressed version
return CompressionFunction(m_expanded);
}
Block ExpansionFunction(const Halfblock& block)
{
std::stringstream ss;
const std::string bits = block.to_string();
// We have to double the bits!
for (std::size_t i = 0; i < bits.size(); i += 2)
{
const std::string sub = bits.substr(i, 2);
if (sub == "00") ss << "1101";
else if (sub == "01") ss << "1000";
else if (sub == "10") ss << "0010";
else /*if (sub == "11")*/ ss << "0111";
}
return Block(ss.str());
}
Halfblock CompressionFunction(const Block& block)
{
std::stringstream ss;
const std::string bits = block.to_string();
// We have to double the bits!
for (std::size_t i = 0; i < bits.size(); i += 4)
{
const std::string sub = bits.substr(i, 4);
if (sub == "0000") ss << "10";
else if (sub == "0001") ss << "01";
else if (sub == "0010") ss << "10";
else if (sub == "0011") ss << "10";
else if (sub == "0100") ss << "11";
else if (sub == "0101") ss << "01";
else if (sub == "0110") ss << "00";
else if (sub == "0111") ss << "11";
else if (sub == "1000") ss << "01";
else if (sub == "1001") ss << "00";
else if (sub == "1010") ss << "11";
else if (sub == "1011") ss << "00";
else if (sub == "1100") ss << "11";
else if (sub == "1101") ss << "10";
else if (sub == "1110") ss << "00";
else /*if (sub == "1111")*/ ss << "01";
}
return Halfblock(ss.str());
}
std::string SBox(const std::string& in)
{
if (in == "0000") return "1100";
else if (in == "0001") return "1000";
else if (in == "0010") return "0001";
else if (in == "0011") return "0111";
else if (in == "0100") return "1011";
else if (in == "0101") return "0011";
else if (in == "0110") return "1101";
else if (in == "0111") return "1111";
else if (in == "1000") return "0000";
else if (in == "1001") return "1010";
else if (in == "1010") return "0100";
else if (in == "1011") return "1001";
else if (in == "1100") return "0010";
else if (in == "1101") return "1110";
else if (in == "1110") return "0101";
else /*if (in == "1111")*/ return "0110";
}
Block StringToBits(const std::string& s)
{
std::stringstream ss;
for (std::size_t i = 0; i < s.size(); i++)
ss << std::bitset<8>(s[i]);
// Pad rest with zeores
for (std::size_t i = s.size() * 8; i < BLOCK_SIZE; i++)
ss << '0';
return Block(ss.str());
}
std::string BitsToString(const Block& bits)
{
std::stringstream ss;
const std::string bitstring = bits.to_string();
for (std::size_t i = 0; i < BLOCK_SIZE; i += 8)
{
ss << (char)std::bitset<8>(bitstring.substr(i, 8)).to_ulong();
}
return ss.str();
}
std::pair<Halfblock, Halfblock> FeistelSplit(const Block& block)
{
const std::string bits = block.to_string();
Halfblock l(bits.substr(0, bits.size() / 2));
Halfblock r(bits.substr(bits.size() / 2));
return std::make_pair(l, r);
}
Block FeistelCombine(const Halfblock& l, const Halfblock& r)
{
return Block(l.to_string() + r.to_string());
}
Keyset GenerateRoundkeys(const Block& seedKey)
{
Keyset keys;
keys[0] = seedKey;
keys[1] = (Shiftl(seedKey, 32) ^ keys[0]);
for (std::size_t i = 2; i < keys.size(); i++)
{
keys[i] = Shiftl(keys[i-1], i + 32) ^ keys[i-2];
}
return keys;
}
template <std::size_t T>
std::bitset<T> Shiftl(const std::bitset<T>& bits, 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 std::bitset<T>(ss.str());
}
template <std::size_t T>
std::bitset<T> Shiftr(const std::bitset<T>& bits, 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 std::bitset<T>(ss.str());
}
Block PasswordToKey(const std::string& in)
{
Block b;
// Segment the password in segments of key-size, and xor them together.
for (std::size_t i = 0; i < in.size(); i += BLOCK_SIZE / 8)
b ^= StringToBits(in.substr(i, BLOCK_SIZE / 8));
return b;
}