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

@@ -65,7 +65,7 @@ GhettoCipher::Flexblock GhettoCipher::Cipher::Encipher(const Flexblock& data, bo
std::cout << "Encrypting... (Block " << i << " / " << blocks.size() << " - " << ((float)i*100 / blocks.size()) << "%)" << std::endl;
const Block& lastBlock = (i>0) ? blocks[i-1] : initializationVector;
blocks[i] = feistel.Encipher(blocks[i] ^ lastBlock);
blocks[i] = feistel.Encipher(blocks[i] ^ lastBlock); // Xor last cipher block with new clear text block before E()
}
// Concatenate ciphertext blocks back into a flexblock
@@ -101,7 +101,7 @@ GhettoCipher::Flexblock GhettoCipher::Cipher::Decipher(const Flexblock& data, bo
Block tmpCopy = blocks[i];
blocks[i] = feistel.Decipher(blocks[i]) ^ lastBlock;
blocks[i] = feistel.Decipher(blocks[i]) ^ lastBlock; // Decipher cipher block [i] and then xor it with the last cipher block [i-1] we've had
lastBlock = std::move(tmpCopy);
}

View File

@@ -36,6 +36,6 @@ namespace GhettoCipher
void ZeroKeyMemory();
// Initial value for cipher block chaining
const Block initializationVector;
Block initializationVector;
};
}

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;

View File

@@ -22,14 +22,14 @@ namespace GhettoCipher
void SetKey(const Block& key);
//! Will encipher a data block via the set seed-key
Block Encipher(const Block& data) const;
Block Encipher(const Block& data);
//! Will decipher a data block via the set seed-key
Block Decipher(const Block& data) const;
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) const;
Block Run(const Block& data, bool reverseKeys);
//! Arbitrary cipher function
static Halfblock F(Halfblock m, const Block& key);

View File

@@ -1,11 +1,13 @@
#include "GhettoCryptWrapper.h"
#include "Cipher.h"
#include "Util.h"
#include <iostream>
std::string GhettoCipher::GhettoCryptWrapper::EncryptString(const std::string& cleartext, const std::string& password)
{
// Instanciate our cipher and supply a key
Cipher cipher(password);
const Block key = PasswordToKey(password);
Cipher cipher(key);
// Recode the ascii-string to bits
const Flexblock cleartext_bits = StringToBits(cleartext);
@@ -23,7 +25,8 @@ std::string GhettoCipher::GhettoCryptWrapper::EncryptString(const std::string& c
std::string GhettoCipher::GhettoCryptWrapper::DecryptString(const std::string& ciphertext, const std::string& password)
{
// Instanciate our cipher and supply a key
Cipher cipher(password);
const Block key = PasswordToKey(password);
Cipher cipher(key);
// Recode the hex-string to bits
const Flexblock ciphertext_bits = HexstringToBits(ciphertext);
@@ -46,7 +49,8 @@ bool GhettoCipher::GhettoCryptWrapper::EncryptFile(const std::string& filename_i
const Flexblock cleartext_bits = ReadFileToBits(filename_in);
// Instanciate our cipher and supply a key
Cipher cipher(password);
const Block key = PasswordToKey(password);
Cipher cipher(key);
// Encrypt our cleartext bits
const Flexblock ciphertext_bits = cipher.Encipher(cleartext_bits, printProgressReport);
@@ -70,7 +74,8 @@ bool GhettoCipher::GhettoCryptWrapper::DecryptFile(const std::string& filename_i
const Flexblock ciphertext_bits = ReadFileToBits(filename_in);
// Instanciate our cipher and supply a key
Cipher cipher(password);
const Block key = PasswordToKey(password);
Cipher cipher(key);
// Decrypt the ciphertext bits
const Flexblock cleartext_bits = cipher.Decipher(ciphertext_bits, printProgressReport);

View File

@@ -2,9 +2,18 @@
#include <random>
#include <sstream>
using namespace GhettoCipher;
// It would be REALLY BAD if another compiler/*version would use
// a mersenne twister with different attrbitutes. It would basically mean
// that E_machine1(M,K) != E_machine2(M,K), which would make them incompatible.
// We do NOT want this to happen, so let's be VERY specific about what mersenne twister setup we want.
// This is std::mt19937, as of msvc stl.
using Prng_MT = std::mersenne_twister_engine<
unsigned int,
32, 624, 397, 31, 0x9908b0df, 11, 0xffffffff,
7, 0x9d2c5680, 15,0xefc60000, 18, 1812433253
>;
InitializationVector::InitializationVector(const Block& seed)
GhettoCipher::InitializationVector::InitializationVector(const Block& seed)
{
// Since an initialization vector does not have to be a secret,
// we should be fine just using a mersenne twister seeded with
@@ -12,8 +21,7 @@ InitializationVector::InitializationVector(const Block& seed)
// Loosely seed mersenne twister with seed
// Here is nothing copied. Both Block::Get, and Hash<>::operator() take refs.
std::mt19937 mt = std::mt19937(std::hash<std::bitset<BLOCK_SIZE>>()(seed.Get()));
Prng_MT mt = Prng_MT(std::hash<std::bitset<BLOCK_SIZE>>()(seed.Get()));
// Now generate BLOCK_SIZE urandom bits
std::stringstream ss;
for (std::size_t i = 0; i < BLOCK_SIZE; i++)
@@ -23,7 +31,7 @@ InitializationVector::InitializationVector(const Block& seed)
iv = Block(ss.str());
}
InitializationVector::operator GhettoCipher::Block() const
GhettoCipher::InitializationVector::operator GhettoCipher::Block() const
{
return iv;
}

View File

@@ -2,15 +2,18 @@
#include "Config.h"
#include "Block.h"
/** Will create a sudo-random Block based on a seed
*/
class InitializationVector
namespace GhettoCipher
{
public:
InitializationVector(const GhettoCipher::Block& seed);
/** Will create a sudo-random Block based on a seed
*/
class InitializationVector
{
public:
InitializationVector(const GhettoCipher::Block& seed);
operator GhettoCipher::Block() const;
operator GhettoCipher::Block() const;
private:
GhettoCipher::Block iv;
};
private:
GhettoCipher::Block iv;
};
}

View File

@@ -5,6 +5,7 @@
#include "SecureBitset.h"
#include "Block.h"
#include "Flexblock.h"
#include "InitializationVector.h"
namespace GhettoCipher
{
@@ -90,8 +91,8 @@ namespace GhettoCipher
return Flexblock(ss.str());
}
//! Will convert a fixed-size data block to a string
inline std::string BitblockToString(const Block& bits)
//! Will convert a fixed-size data block to a bytestring
inline std::string BitblockToBytes(const Block& bits)
{
std::stringstream ss;
@@ -102,7 +103,15 @@ namespace GhettoCipher
ss << (char)std::bitset<8>(bitstring.substr(i, 8)).to_ulong();
}
std::string text = ss.str();
return ss.str();
}
//! Will convert a fixed-size data block to a string
//! The difference to BitblockToBytes() is, that it strips excess nullbytes
inline std::string BitblockToString(const Block& bits)
{
// Decode to bytes
std::string text = BitblockToBytes(bits);
// D<>mp excess nullbytes
text.resize(strlen(text.data()));
@@ -110,8 +119,8 @@ namespace GhettoCipher
return text;
}
//! Will convert a flexible data block to a string
inline std::string BitsToString(const Flexblock& bits)
//! Will convert a flexible data block to a bytestring
inline std::string BitsToBytes(const Flexblock& bits)
{
std::stringstream ss;
@@ -122,7 +131,15 @@ namespace GhettoCipher
ss << (char)std::bitset<8>(bitstring.substr(i, 8)).to_ulong();
}
std::string text = ss.str();
return ss.str();
}
//! Will convert a flexible data block to a string
//! //! The difference to BitsToBytes() is, that it strips excess nullbytes
inline std::string BitsToString(const Flexblock& bits)
{
// Decode to bytes
std::string text = BitsToBytes(bits);
// D<>mp excess nullbytes
text.resize(strlen(text.data()));
@@ -212,8 +229,10 @@ namespace GhettoCipher
}
//! Creates a key of size BLOCK_SIZE from a password of arbitrary length.
//! Using passwords larger (in bits) than BLOCK_SIZE is not generally recommended.
//! Note that if your password is shorter (in bits) than BLOCK_SIZE, the rest of the key will be padded with 0x0. Further round-keys will be extrapolated though.
//! Using passwords larger (in bits) than BLOCK_SIZE is generally not recommended.
//! Note that if your password is shorter (in bits) than BLOCK_SIZE, the rest of the key will be padded with 0 (see next line!).
//! To provide a better initial key, (and to get rid of padding zeroes), the raw result (b) will be xor'd with an initialization vector based on b.
//! : return b ^ iv(b)
inline Block PasswordToKey(const std::string& in)
{
Block b;
@@ -224,7 +243,7 @@ namespace GhettoCipher
PadStringToLength(in.substr(i, BLOCK_SIZE / 8), BLOCK_SIZE / 8, 0, false)
);
return b;
return b ^ InitializationVector(b);
}
//! Will read a file into a flexblock
@@ -255,7 +274,7 @@ namespace GhettoCipher
inline void WriteBitsToFile(const std::string& filepath, const Flexblock& bits)
{
// Convert bits to bytes
const std::string bytes = BitsToString(bits);
const std::string bytes = BitsToBytes(bits);
// Write bits to file
std::ofstream ofs(filepath, std::ios::binary);

View File

@@ -1,2 +1,2 @@
#pragma once
#define GHETTOCRYPT_VERSION 0.13
#define GHETTOCRYPT_VERSION 0.2