2021-12-15 20:13:40 +01:00
|
|
|
#include <unordered_map>
|
2021-12-05 22:40:36 +01:00
|
|
|
#include "Feistel.h"
|
|
|
|
#include "Util.h"
|
|
|
|
#include "Config.h"
|
|
|
|
|
2021-12-06 02:20:47 +01:00
|
|
|
GhettoCipher::Feistel::Feistel(const Block& key)
|
2021-12-05 22:40:36 +01:00
|
|
|
{
|
|
|
|
SetKey(key);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-12-06 02:20:47 +01:00
|
|
|
GhettoCipher::Feistel::~Feistel()
|
2021-12-05 22:40:36 +01:00
|
|
|
{
|
|
|
|
ZeroKeyMemory();
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-12-06 02:20:47 +01:00
|
|
|
void GhettoCipher::Feistel::SetKey(const Block& key)
|
2021-12-05 22:40:36 +01:00
|
|
|
{
|
|
|
|
GenerateRoundKeys(key);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-02-06 21:54:43 +01:00
|
|
|
GhettoCipher::Block GhettoCipher::Feistel::Encipher(const Block& data)
|
2021-12-05 22:40:36 +01:00
|
|
|
{
|
|
|
|
return Run(data, false);
|
|
|
|
}
|
|
|
|
|
2022-02-06 21:54:43 +01:00
|
|
|
GhettoCipher::Block GhettoCipher::Feistel::Decipher(const Block& data)
|
2021-12-05 22:40:36 +01:00
|
|
|
{
|
|
|
|
return Run(data, true);
|
|
|
|
}
|
|
|
|
|
2022-02-06 21:54:43 +01:00
|
|
|
GhettoCipher::Block GhettoCipher::Feistel::Run(const Block& data, bool reverseKeys)
|
2021-12-05 22:40:36 +01:00
|
|
|
{
|
|
|
|
const auto splitData = FeistelSplit(data);
|
2021-12-06 02:20:47 +01:00
|
|
|
GhettoCipher::Halfblock l = splitData.first;
|
|
|
|
GhettoCipher::Halfblock r = splitData.second;
|
2021-12-05 22:40:36 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-02-06 21:54:43 +01:00
|
|
|
// Block has finished de*ciphering.
|
|
|
|
// Let's generate a new set of round keys.
|
|
|
|
GenerateRoundKeys((Block)roundKeys.back());
|
|
|
|
|
2021-12-05 22:40:36 +01:00
|
|
|
return FeistelCombine(r, l);
|
|
|
|
}
|
|
|
|
|
2021-12-06 02:20:47 +01:00
|
|
|
GhettoCipher::Halfblock GhettoCipher::Feistel::F(Halfblock m, const Block& key)
|
2021-12-05 22:40:36 +01:00
|
|
|
{
|
|
|
|
// 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();
|
|
|
|
|
2022-02-06 21:54:43 +01:00
|
|
|
for (std::size_t i = 0; i < BLOCK_SIZE; i += 4)
|
2021-12-05 22:40:36 +01:00
|
|
|
{
|
|
|
|
ss << SBox(m_str.substr(i, 4));
|
|
|
|
}
|
|
|
|
|
|
|
|
m_expanded = Block(ss.str());
|
|
|
|
|
|
|
|
// Return the compressed version
|
|
|
|
return CompressionFunction(m_expanded);
|
|
|
|
}
|
|
|
|
|
2021-12-06 02:20:47 +01:00
|
|
|
std::pair<GhettoCipher::Halfblock, GhettoCipher::Halfblock> GhettoCipher::Feistel::FeistelSplit(const Block& block)
|
2021-12-05 22:40:36 +01:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2021-12-06 02:20:47 +01:00
|
|
|
GhettoCipher::Block GhettoCipher::Feistel::FeistelCombine(const Halfblock& l, const Halfblock& r)
|
2021-12-05 22:40:36 +01:00
|
|
|
{
|
|
|
|
return Block(l.to_string() + r.to_string());
|
|
|
|
}
|
|
|
|
|
2021-12-06 02:20:47 +01:00
|
|
|
GhettoCipher::Block GhettoCipher::Feistel::ExpansionFunction(const Halfblock& block)
|
2021-12-05 22:40:36 +01:00
|
|
|
{
|
|
|
|
std::stringstream ss;
|
|
|
|
const std::string bits = block.to_string();
|
|
|
|
|
2021-12-15 20:13:40 +01:00
|
|
|
std::unordered_map<std::string, std::string> expansionMap;
|
|
|
|
expansionMap["00"] = "1101";
|
|
|
|
expansionMap["01"] = "1000";
|
|
|
|
expansionMap["10"] = "0010";
|
|
|
|
expansionMap["11"] = "0111";
|
|
|
|
|
2021-12-05 22:40:36 +01:00
|
|
|
// We have to double the bits!
|
2022-02-06 21:54:43 +01:00
|
|
|
for (std::size_t i = 0; i < HALFBLOCK_SIZE; i += 2)
|
2021-12-05 22:40:36 +01:00
|
|
|
{
|
|
|
|
const std::string sub = bits.substr(i, 2);
|
2021-12-15 20:13:40 +01:00
|
|
|
ss << expansionMap[sub];
|
2021-12-05 22:40:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return Block(ss.str());
|
|
|
|
}
|
|
|
|
|
2021-12-06 02:20:47 +01:00
|
|
|
GhettoCipher::Halfblock GhettoCipher::Feistel::CompressionFunction(const Block& block)
|
2021-12-05 22:40:36 +01:00
|
|
|
{
|
|
|
|
std::stringstream ss;
|
|
|
|
const std::string bits = block.to_string();
|
|
|
|
|
2021-12-15 20:13:40 +01:00
|
|
|
std::unordered_map<std::string, std::string> compressionMap;
|
|
|
|
compressionMap["0000"] = "10";
|
|
|
|
compressionMap["0001"] = "01";
|
|
|
|
compressionMap["0010"] = "10";
|
|
|
|
compressionMap["0011"] = "10";
|
|
|
|
compressionMap["0100"] = "11";
|
|
|
|
compressionMap["0101"] = "01";
|
|
|
|
compressionMap["0110"] = "00";
|
|
|
|
compressionMap["0111"] = "11";
|
|
|
|
compressionMap["1000"] = "01";
|
|
|
|
compressionMap["1001"] = "00";
|
|
|
|
compressionMap["1010"] = "11";
|
|
|
|
compressionMap["1011"] = "00";
|
|
|
|
compressionMap["1100"] = "11";
|
|
|
|
compressionMap["1101"] = "10";
|
|
|
|
compressionMap["1110"] = "00";
|
|
|
|
compressionMap["1111"] = "01";
|
|
|
|
|
2021-12-13 14:40:29 +01:00
|
|
|
// We have to half the bits!
|
2022-02-06 21:54:43 +01:00
|
|
|
for (std::size_t i = 0; i < BLOCK_SIZE; i += 4)
|
2021-12-05 22:40:36 +01:00
|
|
|
{
|
|
|
|
const std::string sub = bits.substr(i, 4);
|
2021-12-15 20:13:40 +01:00
|
|
|
ss << compressionMap[sub];
|
2021-12-05 22:40:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return Halfblock(ss.str());
|
|
|
|
}
|
|
|
|
|
2021-12-06 02:20:47 +01:00
|
|
|
std::string GhettoCipher::Feistel::SBox(const std::string& in)
|
2021-12-05 22:40:36 +01:00
|
|
|
{
|
2021-12-15 20:13:40 +01:00
|
|
|
static std::unordered_map<std::string, std::string> subMap;
|
|
|
|
static bool mapInitialized = false;
|
|
|
|
if (!mapInitialized)
|
|
|
|
{
|
|
|
|
subMap["0000"] = "1100";
|
|
|
|
subMap["0001"] = "1000";
|
|
|
|
subMap["0010"] = "0001";
|
|
|
|
subMap["0011"] = "0111";
|
|
|
|
subMap["0100"] = "1011";
|
|
|
|
subMap["0101"] = "0011";
|
|
|
|
subMap["0110"] = "1101";
|
|
|
|
subMap["0111"] = "1111";
|
|
|
|
subMap["1000"] = "0000";
|
|
|
|
subMap["1001"] = "1010";
|
|
|
|
subMap["1010"] = "0100";
|
|
|
|
subMap["1011"] = "1001";
|
|
|
|
subMap["1100"] = "0010";
|
|
|
|
subMap["1101"] = "1110";
|
|
|
|
subMap["1110"] = "0101";
|
|
|
|
subMap["1111"] = "0110";
|
|
|
|
mapInitialized = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return subMap[in];
|
2021-12-05 22:40:36 +01:00
|
|
|
}
|
|
|
|
|
2021-12-06 02:20:47 +01:00
|
|
|
void GhettoCipher::Feistel::GenerateRoundKeys(const Block& seedKey)
|
2021-12-05 22:40:36 +01:00
|
|
|
{
|
|
|
|
// Clear initial key memory
|
|
|
|
ZeroKeyMemory();
|
|
|
|
roundKeys = Keyset();
|
|
|
|
|
2022-02-06 21:54:43 +01:00
|
|
|
// Derive the initial two round keys
|
|
|
|
|
|
|
|
// Compress- substitute, and expand the seed key to form the initial and the second-initial round key
|
|
|
|
// This action is non-linear and irreversible, and thus strenghtens security.
|
|
|
|
Halfblock compressedSeed1 = CompressionFunction(seedKey);
|
|
|
|
Halfblock compressedSeed2 = CompressionFunction(Shiftl(seedKey, 1)); // Shifting one key by 1 will result in a completely different compression
|
|
|
|
|
|
|
|
// To add further confusion, let's shift seed1 by 1 aswell (after compression, but before substitution)
|
|
|
|
// but only if the total number of bits set are a multiple of 3
|
|
|
|
// if it is a multiple of 4, we'll shift it by 1 into the opposite direction
|
|
|
|
const std::size_t setBits1 = compressedSeed1.count();
|
|
|
|
|
|
|
|
if (setBits1 % 4 == 0)
|
|
|
|
compressedSeed1 = Shiftr(compressedSeed1, 1);
|
|
|
|
else if (setBits1 % 3 == 0)
|
|
|
|
compressedSeed1 = Shiftl(compressedSeed1, 1);
|
|
|
|
|
|
|
|
// Now apply substitution
|
|
|
|
std::stringstream ssKey1;
|
|
|
|
std::stringstream ssKey2;
|
|
|
|
const std::string bitsKey1 = compressedSeed1.to_string();
|
|
|
|
const std::string bitsKey2 = compressedSeed2.to_string();
|
|
|
|
|
|
|
|
for (std::size_t i = 0; i < HALFBLOCK_SIZE; i += 4)
|
|
|
|
{
|
|
|
|
ssKey1 << SBox(bitsKey1.substr(i, 4));
|
|
|
|
ssKey2 << SBox(bitsKey2.substr(i, 4));
|
|
|
|
}
|
|
|
|
|
|
|
|
compressedSeed1 = Halfblock(ssKey1.str());
|
|
|
|
compressedSeed2 = Halfblock(ssKey2.str());
|
|
|
|
|
|
|
|
// Now extrapolate them to BLOCK_SIZE (key size) again
|
|
|
|
// Xor with the original seed key to get rid of the repititions caused by the expansion
|
|
|
|
roundKeys[0] = ExpansionFunction(compressedSeed1) ^ seedKey;
|
|
|
|
roundKeys[1] = ExpansionFunction(compressedSeed2) ^ seedKey;
|
|
|
|
|
|
|
|
|
|
|
|
// Now derive all other round keys
|
2021-12-05 22:40:36 +01:00
|
|
|
|
|
|
|
for (std::size_t i = 2; i < roundKeys.size(); i++)
|
|
|
|
{
|
2022-02-06 21:54:43 +01:00
|
|
|
// Initialize new round key with last round key
|
|
|
|
Block newKey = roundKeys[i - 1];
|
|
|
|
|
|
|
|
// Shift to left by how many bits are set, modulo 8
|
|
|
|
newKey = Shiftl(newKey, newKey.count() % 8); // This action is irreversible
|
|
|
|
|
|
|
|
// Split into two halfblocks,
|
|
|
|
// apply F() to one halfblock with rk[i-2],
|
|
|
|
// xor the other one with it
|
|
|
|
// and put them back together
|
|
|
|
auto halfkeys = FeistelSplit(newKey);
|
|
|
|
Halfblock halfkey1 = F(halfkeys.first, roundKeys[i - 2]);
|
|
|
|
Halfblock halfkey2 = halfkeys.second ^ halfkey1;
|
|
|
|
|
|
|
|
roundKeys[i] = FeistelCombine(halfkey1, halfkey2);
|
2021-12-05 22:40:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-12-13 15:56:24 +01:00
|
|
|
// 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
|
2021-12-06 02:20:47 +01:00
|
|
|
void GhettoCipher::Feistel::ZeroKeyMemory()
|
2021-12-05 22:40:36 +01:00
|
|
|
{
|
|
|
|
for (Block& key : roundKeys)
|
|
|
|
key.reset();
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2021-12-13 15:56:24 +01:00
|
|
|
#if defined _WIN32 || defined _WIN64
|
|
|
|
#pragma optimize("", on )
|
|
|
|
#elif defined __GNUG__
|
|
|
|
#pragma GCC pop_options
|
|
|
|
#endif
|