Implemented a key class

This commit is contained in:
Leonetienne
2022-05-22 13:43:23 +02:00
parent bb76cbb2d7
commit 9fdc642bd6
13 changed files with 100 additions and 95 deletions

View File

@@ -1,6 +1,7 @@
#pragma once
#include "GCrypt/Keyset.h"
#include "GCrypt/Block.h"
#include "GCrypt/Key.h"
#include "GCrypt/Halfblock.h"
namespace Leonetienne::GCrypt {
@@ -8,7 +9,7 @@ namespace Leonetienne::GCrypt {
*/
class Feistel {
public:
explicit Feistel(const Block& key);
explicit Feistel(const Key& key);
Feistel(const Feistel& other) = delete;
Feistel(Feistel&& other) noexcept = delete;
@@ -17,7 +18,7 @@ namespace Leonetienne::GCrypt {
//! Will set the seed-key for this feistel network.
//! Roundkeys will be derived from this.
void SetKey(const Block& key);
void SetKey(const Key& key);
//! Will encipher a data block via the set seed-key
Block Encipher(const Block& data);
@@ -31,7 +32,7 @@ namespace Leonetienne::GCrypt {
Block Run(const Block& data, bool reverseKeys);
//! Arbitrary cipher function
static Halfblock F(Halfblock m, const Block& key);
static Halfblock F(Halfblock m, const Key& key);
//! Split a data block into two half blocks (into L and R)
static std::pair<Halfblock, Halfblock> FeistelSplit(const Block& block);
@@ -49,7 +50,7 @@ namespace Leonetienne::GCrypt {
static std::string SBox(const std::string& in);
//! Will generate a the round keys
void GenerateRoundKeys(const Block& seedKey);
void GenerateRoundKeys(const Key& seedKey);
//! Will zero the memory used by the keyset
void ZeroKeyMemory();

View File

@@ -14,22 +14,16 @@ namespace Leonetienne::GCrypt {
};
//! Will initialize this cipher with a key
explicit GCipher(const Block& key, const DIRECTION direction);
//! Will initialize this cipher with a key
explicit GCipher(const std::string& password, const DIRECTION direction);
explicit GCipher(const Key& key, const DIRECTION direction);
// Disable copying
GCipher(const GCipher& other) = delete;
GCipher(GCipher&& other) noexcept = delete;
~GCipher();
//! Will digest a data block, and return it
Block Digest(const Block& input);
private:
Block key;
const DIRECTION direction;
//! The feistel instance to be used
@@ -37,8 +31,5 @@ namespace Leonetienne::GCrypt {
//! The last block, required for CBC.
Block lastBlock;
//! Will zero the memory used by the key
void ZeroKeyMemory();
};
}

View File

@@ -3,6 +3,8 @@
#include "GCrypt/Flexblock.h"
#include "GCrypt/Block.h"
#include "GCrypt/GCipher.h"
#include "GCrypt/Key.h"
namespace Leonetienne::GCrypt {
/** This class is a wrapper to make working with the GhettoCipher
@@ -11,25 +13,25 @@ namespace Leonetienne::GCrypt {
class GWrapper {
public:
//! Will encrypt a string and return it hexadecimally encoded.
static std::string EncryptString(const std::string& cleartext, const std::string& password);
static std::string EncryptString(const std::string& cleartext, const Key& key);
//! Will decrypt a hexadecimally encoded string.
static std::string DecryptString(const std::string& ciphertext, const std::string& password);
static std::string DecryptString(const std::string& ciphertext, const Key& key);
//! Will encrypt a file.
//! Returns false if anything goes wrong (like, file-access).
//! @filename_in The file to be read.
//! @filename_out The file the encrypted version should be saved in.
static bool EncryptFile(const std::string& filename_in, const std::string& filename_out, const std::string& password, bool printProgressReport = false);
static bool EncryptFile(const std::string& filename_in, const std::string& filename_out, const Key& key, bool printProgressReport = false);
//! Will decrypt a file.
//! Returns false if anything goes wrong (like, file-access).
//! @filename_in The file to be read.
//! @filename_out The file the decrypted version should be saved in.
static bool DecryptFile(const std::string& filename_in, const std::string& filename_out, const std::string& password, bool printProgressReport = false);
static bool DecryptFile(const std::string& filename_in, const std::string& filename_out, const Key& key, bool printProgressReport = false);
//! Will enncrypt or decrypt an entire flexblock of binary data, given a key.
static Flexblock CipherFlexblock(const Flexblock& data, const Block& key, const GCipher::DIRECTION direction);
static Flexblock CipherFlexblock(const Flexblock& data, const Key& key, const GCipher::DIRECTION direction);
private:

View File

@@ -0,0 +1,26 @@
#ifndef GCRYPT_KEY_H
#define GCRYPT_KEY_H
#include "GCrypt/Block.h"
#include <string>
namespace Leonetienne::GCrypt {
/* This class represents encryption keys.
You can copy them, create them from data blocks,
or even read from files (to be implemented).
*/
class Key : public Block {
public:
static Key FromPassword(const std::string& password);
Key();
Key(const Key& k);
Key(const Block& b);
private:
};
}
#endif

View File

@@ -1,8 +1,9 @@
#pragma once
#include <array>
#include "GCrypt/Block.h"
#include "GCrypt/Key.h"
#include "GCrypt/Config.h"
namespace Leonetienne::GCrypt {
typedef std::array<Block, N_ROUNDS> Keyset;
typedef std::array<Key, N_ROUNDS> Keyset;
}

View File

@@ -227,9 +227,6 @@ namespace Leonetienne::GCrypt {
return ss.str();
}
//! Creates a key of size BLOCK_SIZE from a password of arbitrary length.
Block PasswordToKey(const std::string& in);
//! Will read a file into a flexblock
inline Flexblock ReadFileToBits(const std::string& filepath) {
// Read file