retabbed gcryptlib

This commit is contained in:
Leonetienne
2022-05-16 22:01:52 +02:00
parent ae276e49af
commit c551f5fa64
17 changed files with 576 additions and 662 deletions

View File

@@ -5,114 +5,111 @@
#include "InitializationVector.h"
GhettoCipher::Cipher::Cipher(const Block& key)
:
key { key },
initializationVector(InitializationVector(key))
{
:
key { key },
initializationVector(InitializationVector(key)) {
return;
return;
}
GhettoCipher::Cipher::Cipher(const std::string& password)
:
key { PasswordToKey(password) },
initializationVector(InitializationVector(key))
{
return;
:
key { PasswordToKey(password) },
initializationVector(InitializationVector(key)) {
return;
}
GhettoCipher::Cipher::~Cipher()
{
// Clear key memory
ZeroKeyMemory();
GhettoCipher::Cipher::~Cipher() {
// Clear key memory
ZeroKeyMemory();
return;
return;
}
void GhettoCipher::Cipher::SetKey(const Block& key)
{
ZeroKeyMemory();
void GhettoCipher::Cipher::SetKey(const Block& key) {
ZeroKeyMemory();
this->key = key;
return;
this->key = key;
return;
}
void GhettoCipher::Cipher::SetPassword(const std::string& password)
{
ZeroKeyMemory();
void GhettoCipher::Cipher::SetPassword(const std::string& password) {
ZeroKeyMemory();
key = PasswordToKey(password);
return;
key = PasswordToKey(password);
return;
}
GhettoCipher::Flexblock GhettoCipher::Cipher::Encipher(const Flexblock& data, bool printProgress) const
{
// Split cleartext into blocks
std::vector<Block> blocks;
GhettoCipher::Flexblock GhettoCipher::Cipher::Encipher(const Flexblock& data, bool printProgress) const {
// Split cleartext into blocks
std::vector<Block> blocks;
for (std::size_t i = 0; i < data.size(); i += BLOCK_SIZE)
blocks.push_back(Block(
PadStringToLength(data.substr(i, BLOCK_SIZE), BLOCK_SIZE, '0', false))
);
for (std::size_t i = 0; i < data.size(); i += BLOCK_SIZE) {
blocks.push_back(Block(
PadStringToLength(data.substr(i, BLOCK_SIZE), BLOCK_SIZE, '0', false))
);
}
// Encrypt individual blocks using cipher block chaining
Feistel feistel(key);
// Encrypt individual blocks using cipher block chaining
Feistel feistel(key);
for (std::size_t i = 0; i < blocks.size(); i++)
{
// Print reports if desired. If we have > 1000 blocks, print one report every 100 blocks. Otherwise for every 10th block.
if ((i % ((blocks.size() > 1000)? 100 : 10) == 0) && (printProgress))
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); // Xor last cipher block with new clear text block before E()
}
for (std::size_t i = 0; i < blocks.size(); i++) {
// Print reports if desired. If we have > 1000 blocks, print one report every 100 blocks. Otherwise for every 10th block.
if ((i % ((blocks.size() > 1000)? 100 : 10) == 0) && (printProgress)) {
std::cout << "Encrypting... (Block " << i << " / " << blocks.size() << " - " << ((float)i*100 / blocks.size()) << "%)" << std::endl;
}
// Concatenate ciphertext blocks back into a flexblock
std::stringstream ss;
for (Block& b : blocks)
ss << b;
const Block& lastBlock = (i>0) ? blocks[i-1] : initializationVector;
blocks[i] = feistel.Encipher(blocks[i] ^ lastBlock); // Xor last cipher block with new clear text block before E()
}
// Return it
return ss.str();
// Concatenate ciphertext blocks back into a flexblock
std::stringstream ss;
for (Block& b : blocks) {
ss << b;
}
// Return it
return ss.str();
}
GhettoCipher::Flexblock GhettoCipher::Cipher::Decipher(const Flexblock& data, bool printProgress) const
{
// Split ciphertext into blocks
std::vector<Block> blocks;
GhettoCipher::Flexblock GhettoCipher::Cipher::Decipher(const Flexblock& data, bool printProgress) const {
// Split ciphertext into blocks
std::vector<Block> blocks;
for (std::size_t i = 0; i < data.size(); i += BLOCK_SIZE)
blocks.push_back(Block(
PadStringToLength(data.substr(i, BLOCK_SIZE), BLOCK_SIZE, '0', false))
);
for (std::size_t i = 0; i < data.size(); i += BLOCK_SIZE) {
blocks.push_back(Block(
PadStringToLength(data.substr(i, BLOCK_SIZE), BLOCK_SIZE, '0', false))
);
}
// Decrypt individual blocks
Feistel feistel(key);
// Decrypt individual blocks
Feistel feistel(key);
// We can't do this in-loop for decryption, because we are decrypting the blocks in-place.
Block lastBlock = initializationVector;
for (std::size_t i = 0; i < blocks.size(); i++)
{
// Print reports if desired. If we have > 1000 blocks, print one report every 100 blocks. Otherwise for every 10th block.
if ((i % ((blocks.size() > 1000) ? 100 : 10) == 0) && (printProgress))
std::cout << "Decrypting... (Block " << i << " / " << blocks.size() << " - " << ((float)i*100/ blocks.size()) << "%)" << std::endl;
// We can't do this in-loop for decryption, because we are decrypting the blocks in-place.
Block lastBlock = initializationVector;
Block tmpCopy = blocks[i];
for (std::size_t i = 0; i < blocks.size(); i++) {
// Print reports if desired. If we have > 1000 blocks, print one report every 100 blocks. Otherwise for every 10th block.
if ((i % ((blocks.size() > 1000) ? 100 : 10) == 0) && (printProgress)) {
std::cout << "Decrypting... (Block " << i << " / " << blocks.size() << " - " << ((float)i*100/ blocks.size()) << "%)" << std::endl;
}
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
Block tmpCopy = blocks[i];
lastBlock = std::move(tmpCopy);
}
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
// Concatenate ciphertext blocks back into a flexblock
std::stringstream ss;
for (Block& b : blocks)
ss << b;
lastBlock = std::move(tmpCopy);
}
// Return it
return ss.str();
// Concatenate ciphertext blocks back into a flexblock
std::stringstream ss;
for (Block& b : blocks) {
ss << b;
}
// Return it
return ss.str();
}
// These pragmas only work for MSVC and g++, as far as i know. Beware!!!
@@ -122,13 +119,13 @@ GhettoCipher::Flexblock GhettoCipher::Cipher::Decipher(const Flexblock& data, bo
#pragma GCC push_options
#pragma GCC optimize ("O0")
#endif
void GhettoCipher::Cipher::ZeroKeyMemory()
{
key.reset();
return;
void GhettoCipher::Cipher::ZeroKeyMemory() {
key.reset();
return;
}
#if defined _WIN32 || defined _WIN64
#pragma optimize("", on )
#elif defined __GNUG__
#pragma GCC pop_options
#endif

View File

@@ -3,51 +3,46 @@
#include "Util.h"
#include "Config.h"
GhettoCipher::Feistel::Feistel(const Block& key)
{
GhettoCipher::Feistel::Feistel(const Block& key) {
SetKey(key);
return;
}
GhettoCipher::Feistel::~Feistel()
{
GhettoCipher::Feistel::~Feistel() {
ZeroKeyMemory();
return;
}
void GhettoCipher::Feistel::SetKey(const Block& key)
{
void GhettoCipher::Feistel::SetKey(const Block& key) {
GenerateRoundKeys(key);
return;
}
GhettoCipher::Block GhettoCipher::Feistel::Encipher(const Block& data)
{
GhettoCipher::Block GhettoCipher::Feistel::Encipher(const Block& data) {
return Run(data, false);
}
GhettoCipher::Block GhettoCipher::Feistel::Decipher(const Block& data)
{
GhettoCipher::Block GhettoCipher::Feistel::Decipher(const Block& data) {
return Run(data, true);
}
GhettoCipher::Block GhettoCipher::Feistel::Run(const Block& data, bool reverseKeys)
{
GhettoCipher::Block GhettoCipher::Feistel::Run(const Block& data, bool reverseKeys) {
const auto splitData = FeistelSplit(data);
GhettoCipher::Halfblock l = splitData.first;
GhettoCipher::Halfblock r = splitData.second;
Halfblock tmp;
for (std::size_t i = 0; i < N_ROUNDS; i++)
{
for (std::size_t i = 0; i < N_ROUNDS; i++) {
// Calculate key index
std::size_t keyIndex;
if (reverseKeys)
if (reverseKeys) {
keyIndex = N_ROUNDS - i - 1;
else
}
else {
keyIndex = i;
}
// Do a feistel round
tmp = r;
@@ -62,8 +57,7 @@ GhettoCipher::Block GhettoCipher::Feistel::Run(const Block& data, bool reverseKe
return FeistelCombine(r, l);
}
GhettoCipher::Halfblock GhettoCipher::Feistel::F(Halfblock m, const Block& key)
{
GhettoCipher::Halfblock GhettoCipher::Feistel::F(Halfblock m, const Block& key) {
// Made-up F function
// Expand to full bitwidth
@@ -79,8 +73,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 < BLOCK_SIZE; i += 4)
{
for (std::size_t i = 0; i < BLOCK_SIZE; i += 4) {
ss << SBox(m_str.substr(i, 4));
}
@@ -90,8 +83,7 @@ GhettoCipher::Halfblock GhettoCipher::Feistel::F(Halfblock m, const Block& key)
return CompressionFunction(m_expanded);
}
std::pair<GhettoCipher::Halfblock, GhettoCipher::Halfblock> GhettoCipher::Feistel::FeistelSplit(const Block& block)
{
std::pair<GhettoCipher::Halfblock, GhettoCipher::Halfblock> GhettoCipher::Feistel::FeistelSplit(const Block& block) {
const std::string bits = block.to_string();
Halfblock l(bits.substr(0, bits.size() / 2));
@@ -100,13 +92,11 @@ std::pair<GhettoCipher::Halfblock, GhettoCipher::Halfblock> GhettoCipher::Feiste
return std::make_pair(l, r);
}
GhettoCipher::Block GhettoCipher::Feistel::FeistelCombine(const Halfblock& l, const Halfblock& r)
{
GhettoCipher::Block GhettoCipher::Feistel::FeistelCombine(const Halfblock& l, const Halfblock& r) {
return Block(l.to_string() + r.to_string());
}
GhettoCipher::Block GhettoCipher::Feistel::ExpansionFunction(const Halfblock& block)
{
GhettoCipher::Block GhettoCipher::Feistel::ExpansionFunction(const Halfblock& block) {
std::stringstream ss;
const std::string bits = block.to_string();
@@ -117,8 +107,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 < HALFBLOCK_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];
}
@@ -126,8 +115,7 @@ GhettoCipher::Block GhettoCipher::Feistel::ExpansionFunction(const Halfblock& bl
return Block(ss.str());
}
GhettoCipher::Halfblock GhettoCipher::Feistel::CompressionFunction(const Block& block)
{
GhettoCipher::Halfblock GhettoCipher::Feistel::CompressionFunction(const Block& block) {
std::stringstream ss;
const std::string bits = block.to_string();
@@ -150,8 +138,7 @@ GhettoCipher::Halfblock GhettoCipher::Feistel::CompressionFunction(const Block&
compressionMap["1111"] = "01";
// We have to half the bits!
for (std::size_t i = 0; i < BLOCK_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];
}
@@ -159,12 +146,10 @@ GhettoCipher::Halfblock GhettoCipher::Feistel::CompressionFunction(const Block&
return Halfblock(ss.str());
}
std::string GhettoCipher::Feistel::SBox(const std::string& in)
{
std::string GhettoCipher::Feistel::SBox(const std::string& in) {
static std::unordered_map<std::string, std::string> subMap;
static bool mapInitialized = false;
if (!mapInitialized)
{
if (!mapInitialized) {
subMap["0000"] = "1100";
subMap["0001"] = "1000";
subMap["0010"] = "0001";
@@ -187,8 +172,7 @@ std::string GhettoCipher::Feistel::SBox(const std::string& in)
return subMap[in];
}
void GhettoCipher::Feistel::GenerateRoundKeys(const Block& seedKey)
{
void GhettoCipher::Feistel::GenerateRoundKeys(const Block& seedKey) {
// Clear initial key memory
ZeroKeyMemory();
roundKeys = Keyset();
@@ -205,10 +189,12 @@ void GhettoCipher::Feistel::GenerateRoundKeys(const Block& seedKey)
// 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)
if (setBits1 % 4 == 0) {
compressedSeed1 = Shiftr(compressedSeed1, 1);
else if (setBits1 % 3 == 0)
}
else if (setBits1 % 3 == 0) {
compressedSeed1 = Shiftl(compressedSeed1, 1);
}
// Now apply substitution
std::stringstream ssKey1;
@@ -216,8 +202,7 @@ void GhettoCipher::Feistel::GenerateRoundKeys(const Block& seedKey)
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)
{
for (std::size_t i = 0; i < HALFBLOCK_SIZE; i += 4) {
ssKey1 << SBox(bitsKey1.substr(i, 4));
ssKey2 << SBox(bitsKey2.substr(i, 4));
}
@@ -230,11 +215,9 @@ void GhettoCipher::Feistel::GenerateRoundKeys(const Block& seedKey)
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++)
{
for (std::size_t i = 2; i < roundKeys.size(); i++) {
// Initialize new round key with last round key
Block newKey = roundKeys[i - 1];
@@ -262,10 +245,10 @@ void GhettoCipher::Feistel::GenerateRoundKeys(const Block& seedKey)
#pragma GCC push_options
#pragma GCC optimize ("O0")
#endif
void GhettoCipher::Feistel::ZeroKeyMemory()
{
for (Block& key : roundKeys)
void GhettoCipher::Feistel::ZeroKeyMemory() {
for (Block& key : roundKeys) {
key.reset();
}
return;
}
@@ -274,3 +257,4 @@ void GhettoCipher::Feistel::ZeroKeyMemory()
#elif defined __GNUG__
#pragma GCC pop_options
#endif

View File

@@ -2,90 +2,83 @@
#include "Cipher.h"
#include "Util.h"
std::string GhettoCipher::GhettoCryptWrapper::EncryptString(const std::string& cleartext, const std::string& password)
{
// Instanciate our cipher and supply a key
const Block key = PasswordToKey(password);
Cipher cipher(key);
std::string GhettoCipher::GhettoCryptWrapper::EncryptString(const std::string& cleartext, const std::string& password) {
// Instanciate our cipher and supply a key
const Block key = PasswordToKey(password);
Cipher cipher(key);
// Recode the ascii-string to bits
const Flexblock cleartext_bits = StringToBits(cleartext);
// Recode the ascii-string to bits
const Flexblock cleartext_bits = StringToBits(cleartext);
// Encrypt our cleartext bits
const Flexblock ciphertext_bits = cipher.Encipher(cleartext_bits);
// Encrypt our cleartext bits
const Flexblock ciphertext_bits = cipher.Encipher(cleartext_bits);
// Recode the ciphertext bits to a hex-string
const std::string ciphertext = BitsToHexstring(ciphertext_bits);
// Recode the ciphertext bits to a hex-string
const std::string ciphertext = BitsToHexstring(ciphertext_bits);
// Return it
return ciphertext;
// Return it
return ciphertext;
}
std::string GhettoCipher::GhettoCryptWrapper::DecryptString(const std::string& ciphertext, const std::string& password)
{
// Instanciate our cipher and supply a key
const Block key = PasswordToKey(password);
Cipher cipher(key);
std::string GhettoCipher::GhettoCryptWrapper::DecryptString(const std::string& ciphertext, const std::string& password) {
// Instanciate our cipher and supply a key
const Block key = PasswordToKey(password);
Cipher cipher(key);
// Recode the hex-string to bits
const Flexblock ciphertext_bits = HexstringToBits(ciphertext);
// Recode the hex-string to bits
const Flexblock ciphertext_bits = HexstringToBits(ciphertext);
// Decrypt the ciphertext bits
const std::string cleartext_bits = cipher.Decipher(ciphertext_bits);
// Decrypt the ciphertext bits
const std::string cleartext_bits = cipher.Decipher(ciphertext_bits);
// Recode the cleartext bits to an ascii-string
const std::string cleartext = BitsToString(cleartext_bits);
// Recode the cleartext bits to an ascii-string
const std::string cleartext = BitsToString(cleartext_bits);
// Return it
return cleartext;
// Return it
return cleartext;
}
bool GhettoCipher::GhettoCryptWrapper::EncryptFile(const std::string& filename_in, const std::string& filename_out, const std::string& password, bool printProgressReport)
{
try
{
// Read the file to bits
const Flexblock cleartext_bits = ReadFileToBits(filename_in);
bool GhettoCipher::GhettoCryptWrapper::EncryptFile(const std::string& filename_in, const std::string& filename_out, const std::string& password, bool printProgressReport) {
try {
// Read the file to bits
const Flexblock cleartext_bits = ReadFileToBits(filename_in);
// Instanciate our cipher and supply a key
const Block key = PasswordToKey(password);
Cipher cipher(key);
// Instanciate our cipher and supply a key
const Block key = PasswordToKey(password);
Cipher cipher(key);
// Encrypt our cleartext bits
const Flexblock ciphertext_bits = cipher.Encipher(cleartext_bits, printProgressReport);
// Encrypt our cleartext bits
const Flexblock ciphertext_bits = cipher.Encipher(cleartext_bits, printProgressReport);
// Write our ciphertext bits to file
WriteBitsToFile(filename_out, ciphertext_bits);
// Write our ciphertext bits to file
WriteBitsToFile(filename_out, ciphertext_bits);
return true;
}
catch (std::runtime_error&)
{
return false;
}
return true;
}
catch (std::runtime_error&) {
return false;
}
}
bool GhettoCipher::GhettoCryptWrapper::DecryptFile(const std::string& filename_in, const std::string& filename_out, const std::string& password, bool printProgressReport)
{
try
{
// Read the file to bits
const Flexblock ciphertext_bits = ReadFileToBits(filename_in);
bool GhettoCipher::GhettoCryptWrapper::DecryptFile(const std::string& filename_in, const std::string& filename_out, const std::string& password, bool printProgressReport) {
try {
// Read the file to bits
const Flexblock ciphertext_bits = ReadFileToBits(filename_in);
// Instanciate our cipher and supply a key
const Block key = PasswordToKey(password);
Cipher cipher(key);
// Instanciate our cipher and supply a key
const Block key = PasswordToKey(password);
Cipher cipher(key);
// Decrypt the ciphertext bits
const Flexblock cleartext_bits = cipher.Decipher(ciphertext_bits, printProgressReport);
// Decrypt the ciphertext bits
const Flexblock cleartext_bits = cipher.Decipher(ciphertext_bits, printProgressReport);
// Write our cleartext bits to file
WriteBitsToFile(filename_out, cleartext_bits);
// Write our cleartext bits to file
WriteBitsToFile(filename_out, cleartext_bits);
return true;
}
catch (std::runtime_error&)
{
return false;
}
return true;
}
catch (std::runtime_error&) {
return false;
}
}

View File

@@ -1,14 +1,13 @@
#include "InitializationVector.h"
#include "Feistel.h"
GhettoCipher::InitializationVector::InitializationVector(const Block& seed)
{
// We'll generate our initialization vector by encrypting our seed with itself as a key
// iv = E(M=seed, K=seed)
iv = Feistel(seed).Encipher(seed);
GhettoCipher::InitializationVector::InitializationVector(const Block& seed) {
// We'll generate our initialization vector by encrypting our seed with itself as a key
// iv = E(M=seed, K=seed)
iv = Feistel(seed).Encipher(seed);
}
GhettoCipher::InitializationVector::operator GhettoCipher::Block() const
{
return iv;
GhettoCipher::InitializationVector::operator GhettoCipher::Block() const {
return iv;
}