Made the whole thing MUCH more secure, by adding an IV (initialization vector), implemeted RRKM (rolling round key mode) and redone key extrapolation

This commit is contained in:
Leonetienne
2022-02-06 21:54:43 +01:00
parent e57456e9ae
commit 8678d3cb1b
18 changed files with 427 additions and 65 deletions

View File

@@ -22,17 +22,17 @@ void GhettoCipher::Feistel::SetKey(const Block& key)
return;
}
GhettoCipher::Block GhettoCipher::Feistel::Encipher(const Block& data) const
GhettoCipher::Block GhettoCipher::Feistel::Encipher(const Block& data)
{
return Run(data, false);
}
GhettoCipher::Block GhettoCipher::Feistel::Decipher(const Block& data) const
GhettoCipher::Block GhettoCipher::Feistel::Decipher(const Block& data)
{
return Run(data, true);
}
GhettoCipher::Block GhettoCipher::Feistel::Run(const Block& data, bool reverseKeys) const
GhettoCipher::Block GhettoCipher::Feistel::Run(const Block& data, bool reverseKeys)
{
const auto splitData = FeistelSplit(data);
GhettoCipher::Halfblock l = splitData.first;
@@ -55,6 +55,10 @@ GhettoCipher::Block GhettoCipher::Feistel::Run(const Block& data, bool reverseKe
l = tmp;
}
// Block has finished de*ciphering.
// Let's generate a new set of round keys.
GenerateRoundKeys((Block)roundKeys.back());
return FeistelCombine(r, l);
}
@@ -75,7 +79,7 @@ GhettoCipher::Halfblock GhettoCipher::Feistel::F(Halfblock m, const Block& key)
std::stringstream ss;
const std::string m_str = m_expanded.to_string();
for (std::size_t i = 0; i < m_str.size(); i += 4)
for (std::size_t i = 0; i < BLOCK_SIZE; i += 4)
{
ss << SBox(m_str.substr(i, 4));
}
@@ -113,7 +117,7 @@ GhettoCipher::Block GhettoCipher::Feistel::ExpansionFunction(const Halfblock& bl
expansionMap["11"] = "0111";
// We have to double the bits!
for (std::size_t i = 0; i < bits.size(); i += 2)
for (std::size_t i = 0; i < HALFBLOCK_SIZE; i += 2)
{
const std::string sub = bits.substr(i, 2);
ss << expansionMap[sub];
@@ -146,7 +150,7 @@ GhettoCipher::Halfblock GhettoCipher::Feistel::CompressionFunction(const Block&
compressionMap["1111"] = "01";
// We have to half the bits!
for (std::size_t i = 0; i < bits.size(); i += 4)
for (std::size_t i = 0; i < BLOCK_SIZE; i += 4)
{
const std::string sub = bits.substr(i, 4);
ss << compressionMap[sub];
@@ -185,19 +189,67 @@ std::string GhettoCipher::Feistel::SBox(const std::string& in)
void GhettoCipher::Feistel::GenerateRoundKeys(const Block& seedKey)
{
// Generate round keys via output feedback modus (OFM) method
// Clear initial key memory
ZeroKeyMemory();
roundKeys = Keyset();
// Generate new keys from the seed key
roundKeys[0] = seedKey;
roundKeys[1] = (Shiftl(seedKey, 32) ^ roundKeys[0]);
// 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
for (std::size_t i = 2; i < roundKeys.size(); i++)
{
roundKeys[i] = Shiftl(roundKeys[i - 1], i + 32) ^ roundKeys[i - 2];
// 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);
}
return;