GCrypt/main.cpp

326 lines
9.0 KiB
C++
Raw Normal View History

2021-12-05 15:47:31 +01:00
#include <iostream>
#include <sstream>
#include <array>
2021-12-05 15:58:56 +01:00
#include <bitset>
2021-12-05 15:47:31 +01:00
2021-12-05 16:38:42 +01:00
#define BLOCK_SIZE 128
2021-12-05 18:48:54 +01:00
#define HALFBLOCK_SIZE (BLOCK_SIZE / 2)
2021-12-05 18:27:29 +01:00
#define N_ROUNDS 128
2021-12-05 15:47:31 +01:00
2021-12-05 15:58:56 +01:00
typedef std::bitset<BLOCK_SIZE> Block;
2021-12-05 18:48:54 +01:00
typedef std::bitset<HALFBLOCK_SIZE> Halfblock;
2021-12-05 18:47:42 +01:00
typedef std::array<Block, N_ROUNDS> Keyset;
2021-12-05 15:58:56 +01:00
2021-12-05 16:38:42 +01:00
// Will convert a string to a data block
Block StringToBits(const std::string& s);
2021-12-05 15:47:31 +01:00
2021-12-05 16:38:42 +01:00
// Will convert a data block to a string
std::string BitsToString(const Block& bits);
2021-12-05 16:15:11 +01:00
2021-12-05 18:48:54 +01:00
// Split a data block into two half blocks (into L and R)
std::pair<Halfblock, Halfblock> FeistelSplit(const Block& block);
2021-12-05 16:15:11 +01:00
2021-12-05 18:48:54 +01:00
// Combine two half blocks (L and R) into a regular data block
Block FeistelCombine(const Halfblock& l, const Halfblock& r);
2021-12-05 16:15:11 +01:00
// Creates a key of size key-size from a password of arbitrary length.
Block PasswordToKey(const std::string& in);
2021-12-05 17:09:01 +01:00
// Will generate a keyset from a seed-key
2021-12-05 18:47:42 +01:00
Keyset GenerateRoundkeys(const Block& seedKey);
2021-12-05 17:09:01 +01:00
2021-12-05 16:15:11 +01:00
// Feistel-cipher
2021-12-05 17:09:01 +01:00
Block Feistel(const Block& data, const Keyset& keys, bool reverseKeyOrder = false);
2021-12-05 16:15:11 +01:00
2021-12-05 18:47:42 +01:00
// Will expand a halfblock to a fullblock
2021-12-05 18:48:54 +01:00
Block ExpansionFunction(const Halfblock& block);
2021-12-05 18:47:42 +01:00
// Will compress a fullblock to a halfblock
2021-12-05 18:48:54 +01:00
Halfblock CompressionFunction(const Block& block);
2021-12-05 18:47:42 +01:00
2021-12-05 18:27:29 +01:00
// Substitutes four bits by static random others
std::string SBox(const std::string& in);
2021-12-05 16:15:11 +01:00
// Arbitrary cipher function
2021-12-05 18:48:54 +01:00
Halfblock F(Halfblock m, const Block& key);
2021-12-05 15:47:31 +01:00
2021-12-05 17:50:17 +01:00
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);
2021-12-05 19:31:33 +01:00
std::string DiffusionCheck(const std::string& asciiMessage);
2021-12-05 18:27:29 +01:00
2021-12-05 17:50:17 +01:00
int Mod(int numerator, int denominator)
{
return (denominator + (numerator % denominator)) % denominator;
}
2021-12-05 15:47:31 +01:00
int main()
{
2021-12-05 19:31:33 +01:00
const std::string cipher1 = DiffusionCheck("Hello, World :3");
2021-12-05 18:27:29 +01:00
std::cout << std::endl;
2021-12-05 19:31:33 +01:00
const std::string cipher2 = DiffusionCheck("Hello, world :3");
2021-12-05 18:27:29 +01:00
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;
2021-12-05 18:48:54 +01:00
std::cout << "Total difference between C1 and C2: " << numDiff << std::endl;
2021-12-05 18:27:29 +01:00
return 0;
}
2021-12-05 16:38:42 +01:00
2021-12-05 19:31:33 +01:00
std::string DiffusionCheck(const std::string& asciiMessage)
2021-12-05 18:27:29 +01:00
{
2021-12-05 16:38:42 +01:00
Block message = StringToBits(asciiMessage);
2021-12-05 16:15:11 +01:00
const Block seedKey = PasswordToKey("Ich bin ein PASSWORT-SCHLÜSSEL!");
2021-12-05 18:27:29 +01:00
Keyset roundkeys = GenerateRoundkeys(seedKey);
2021-12-05 17:09:01 +01:00
//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;
2021-12-05 18:47:42 +01:00
//exit(0);
2021-12-05 17:50:17 +01:00
2021-12-05 15:47:31 +01:00
2021-12-05 16:38:42 +01:00
std::cout << "Message ascii: " << asciiMessage << std::endl;
std::cout << "Message: " << message << std::endl;
2021-12-05 15:47:31 +01:00
2021-12-05 17:09:01 +01:00
Block ciphertext = Feistel(message, roundkeys);
2021-12-05 16:38:42 +01:00
std::cout << "Ciphertext: " << ciphertext << std::endl;
2021-12-05 15:47:31 +01:00
2021-12-05 17:09:01 +01:00
Block decrypted = Feistel(ciphertext, roundkeys, true);
2021-12-05 16:38:42 +01:00
std::cout << "Decrypted: " << decrypted << std::endl;
const std::string asciiDecrypted = BitsToString(decrypted);
std::cout << "Decrypted ascii: " << asciiDecrypted << std::endl;
2021-12-05 15:47:31 +01:00
2021-12-05 18:27:29 +01:00
return ciphertext.to_string();
2021-12-05 15:47:31 +01:00
}
2021-12-05 17:09:01 +01:00
Block Feistel(const Block& data, const Keyset& keys, bool reverseKeyOrder)
2021-12-05 15:47:31 +01:00
{
2021-12-05 16:15:11 +01:00
const auto splitData = FeistelSplit(data);
2021-12-05 18:48:54 +01:00
Halfblock l = splitData.first;
Halfblock r = splitData.second;
2021-12-05 16:15:11 +01:00
2021-12-05 18:48:54 +01:00
Halfblock tmp;
2021-12-05 15:47:31 +01:00
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;
2021-12-05 15:58:56 +01:00
r = l ^ F(r, keys[keyIndex]);
2021-12-05 15:47:31 +01:00
l = tmp;
}
2021-12-05 16:15:11 +01:00
return FeistelCombine(r, l);
2021-12-05 15:47:31 +01:00
}
2021-12-05 18:48:54 +01:00
Halfblock F(Halfblock m, const Block& key)
2021-12-05 15:47:31 +01:00
{
// Made-up F function
2021-12-05 18:47:42 +01:00
// Expand to full bitwidht
Block m_expanded = ExpansionFunction(m);
2021-12-05 18:27:29 +01:00
// Shift to left by 1
2021-12-05 18:47:42 +01:00
m_expanded = Shiftl(m_expanded, 1);
2021-12-05 15:47:31 +01:00
// Xor with key
2021-12-05 18:47:42 +01:00
m_expanded ^= key;
2021-12-05 18:27:29 +01:00
// Non-linearly apply subsitution boxes
std::stringstream ss;
2021-12-05 18:47:42 +01:00
const std::string m_str = m_expanded.to_string();
2021-12-05 18:27:29 +01:00
for (std::size_t i = 0; i < m_str.size(); i += 4)
{
ss << SBox(m_str.substr(i, 4));
}
2021-12-05 18:47:42 +01:00
m_expanded = Block(ss.str());
// Return the compressed version
return CompressionFunction(m_expanded);
}
2021-12-05 18:48:54 +01:00
Block ExpansionFunction(const Halfblock& block)
2021-12-05 18:47:42 +01:00
{
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());
}
2021-12-05 18:48:54 +01:00
Halfblock CompressionFunction(const Block& block)
2021-12-05 18:47:42 +01:00
{
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";
}
2021-12-05 18:27:29 +01:00
2021-12-05 18:48:54 +01:00
return Halfblock(ss.str());
2021-12-05 18:27:29 +01:00
}
std::string SBox(const std::string& in)
{
2021-12-05 18:47:42 +01:00
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";
2021-12-05 18:27:29 +01:00
else /*if (in == "1111")*/ return "0110";
2021-12-05 15:47:31 +01:00
}
2021-12-05 16:15:11 +01:00
2021-12-05 16:38:42 +01:00
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)
{
2021-12-05 18:27:29 +01:00
ss << (char)std::bitset<8>(bitstring.substr(i, 8)).to_ulong();
2021-12-05 16:38:42 +01:00
}
return ss.str();
}
2021-12-05 18:48:54 +01:00
std::pair<Halfblock, Halfblock> FeistelSplit(const Block& block)
2021-12-05 16:15:11 +01:00
{
const std::string bits = block.to_string();
2021-12-05 18:48:54 +01:00
Halfblock l(bits.substr(0, bits.size() / 2));
Halfblock r(bits.substr(bits.size() / 2));
2021-12-05 16:15:11 +01:00
return std::make_pair(l, r);
}
2021-12-05 18:48:54 +01:00
Block FeistelCombine(const Halfblock& l, const Halfblock& r)
2021-12-05 16:15:11 +01:00
{
return Block(l.to_string() + r.to_string());
}
2021-12-05 17:09:01 +01:00
2021-12-05 18:47:42 +01:00
Keyset GenerateRoundkeys(const Block& seedKey)
2021-12-05 17:09:01 +01:00
{
Keyset keys;
2021-12-05 17:50:17 +01:00
2021-12-05 17:09:01 +01:00
keys[0] = seedKey;
2021-12-05 17:50:17 +01:00
keys[1] = (Shiftl(seedKey, 32) ^ keys[0]);
for (std::size_t i = 2; i < keys.size(); i++)
2021-12-05 17:09:01 +01:00
{
2021-12-05 17:50:17 +01:00
keys[i] = Shiftl(keys[i-1], i + 32) ^ keys[i-2];
2021-12-05 17:09:01 +01:00
}
return keys;
}
2021-12-05 17:50:17 +01:00
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;
}