From 8fe7ed3336ce29f72fdaf3ea8439f063e9be7f12 Mon Sep 17 00:00:00 2001 From: "J. Hamatoma" Date: Wed, 27 Nov 2013 13:37:01 +0100 Subject: [PATCH] dayly work --- rplmath/rplenigma.cpp | 96 ++++++++++++++++++++++++++++++++++ rplmath/rplenigma.hpp | 34 ++++++++++++ rplmath/rplmath.hpp | 14 +++++ rplmath/rplrandom.cpp | 111 ++++++++++++++++++++++++++++++++++++++++ rplmath/rplrandom.hpp | 22 ++++++++ rplnet/rpltcpserver.cpp | 48 +++++++++++++++++ rplnet/rpltcpserver.hpp | 13 ++++- 7 files changed, 337 insertions(+), 1 deletion(-) create mode 100644 rplmath/rplenigma.cpp create mode 100644 rplmath/rplenigma.hpp create mode 100644 rplmath/rplmath.hpp create mode 100644 rplmath/rplrandom.cpp create mode 100644 rplmath/rplrandom.hpp diff --git a/rplmath/rplenigma.cpp b/rplmath/rplenigma.cpp new file mode 100644 index 0000000..1f03068 --- /dev/null +++ b/rplmath/rplenigma.cpp @@ -0,0 +1,96 @@ +/* + * Licence: + * You can use and modify this file without any restriction. + * There is no warranty. + * You also can use the licence from http://www.wtfpl.net/. + * The original sources can be found on https://github.com/republib. + */ +#include "rplmath.hpp" +/** @class RplEnigma rplenigma.hpp "rplmath/rplenigma.hpp" + * + * @brief Implements a portable en/decryption engine. + * + * It is used a symetric encryption. + * Therefore the encrypt methods can be used for decryption too. + * + * The encryption can be unique by using certificates. + * A certificate is a series of bytes affecting the pseudo random sequence. + * + * More than one certificate can be used. + * + * The encryption is done with an pseudo random generator. + * The portability (over the hardware architecture) is guaranteed if the + * pseudo random generator is portable. + * + */ +/** + * @brief Constructor. + * + * @param random pseudo random generator + */ +RplEnigma::RplEnigma(RplRandom* random) : + m_random(random), + m_ownsRandom(false), + m_byteSeeds(), + m_ixSeed(0) +{ + if (random == NULL){ + m_random = new RplRandom(); + m_ownsRandom = true; + } +} +/** + * @brief Destructor. + */ +RplEnigma::~RplEnigma(); + +/** + * @brief Reads a certificate. + * + * @param filename the name of the certificate + * + * @return empty string: error while reading
+ * otherwise: the certificate as byte array + */ +QByteArray RplEnigma::readCertificate(const char* filename){ + QByteArray rc; + + return rc; +} + +/** + * @brief Encodes one character. + * + * @param byte value to encode + * @param mode affects the possible values of the result + * @return the encoded character + */ +char RplEnigma::encodeChar(char byte, mode_t mode = MODE_STANDARD){ + char rc = 0; + + switch(mode){ + case MODE_DECIMAL: + + break; + case MODE_HEXADECIMAL: + case MODE_ALPHA_NUMERIC: + case MODE_ASCII: + case MODE_CHAR255: + case MODE_FILENAME: + break; + case MODE_STANDARD: + default: + rc = + ; + break; + } + return rc; +} +void RplEnigma::encode(QByteArray& value, mode_t mode = MODE_STANDARD); +void RplEnigma::addByteSeed(const QByteArray& byteSeed); +void RplEnigma::setSeed(u_int64_t seed); + protected: + QList m_byteSeeds; + ///> each call of setSeed sets this value to 0. + int m_ixSeed; + }; diff --git a/rplmath/rplenigma.hpp b/rplmath/rplenigma.hpp new file mode 100644 index 0000000..842c05e --- /dev/null +++ b/rplmath/rplenigma.hpp @@ -0,0 +1,34 @@ +#ifndef RPLENIGMA_HPP +#define RPLENIGMA_HPP + +class RplEnigma { + public: + typedef enum { + MODE_STANDARD = 1, ///< no limitation + MODE_DECIMAL, ///< a decimal will be encrypted by an decimal + MODE_HEXADECIMAL, ///< a hexadecimal will be encrypted by an hexadecimal + MODE_ALPHA_NUMERIC, ///< [a-zA-Z0-9_] remains in this class + MODE_FILENAME, ///< [a-zA-Z0-9_!^$%=+-#~.] remains in this class + MODE_ASCII, ///< chr(32)..chr(127) remains in this class + MODE_CHAR255 ///< chr(32)..chr(255) remains in this class + } mode_t; + public: + RplEnigma(RplRandom* random = NULL); + virtual ~RplEnigma(); + public: + QByteArray readCertificate(); + char encodeChar(char byte, mode_t mode = MODE_STANDARD); + void encode(QByteArray& value, mode_t mode = MODE_STANDARD); + void addByteSeed(const QByteArray& byteSeed); + void setSeed(u_int64_t seed); + protected: + ///> a pseudo random generator + RplRandom* m_random; + ///> true: m_random must be destroyed (in the destructor). + bool m_ownsRandom; + QList m_byteSeeds; + ///> each call of setSeed sets this value to 0. + int m_ixSeed; + }; + +#endif // RPLENIGMA_HPP diff --git a/rplmath/rplmath.hpp b/rplmath/rplmath.hpp new file mode 100644 index 0000000..14f2e86 --- /dev/null +++ b/rplmath/rplmath.hpp @@ -0,0 +1,14 @@ +/* + * Licence: + * You can use and modify this file without any restriction. + * There is no warranty. + * You also can use the licence from http://www.wtfpl.net/. + * The original sources can be found on https://github.com/republib. + */ +#ifndef RPLMATH_HPP +#define RPLMATH_HPP + +#include "rplrandom.hpp" +#include "rplenigma.hpp" + +#endif // RPLMATH_HPP diff --git a/rplmath/rplrandom.cpp b/rplmath/rplrandom.cpp new file mode 100644 index 0000000..a0a38e2 --- /dev/null +++ b/rplmath/rplrandom.cpp @@ -0,0 +1,111 @@ +/* + * Licence: + * You can use and modify this file without any restriction. + * There is no warranty. + * You also can use the licence from http://www.wtfpl.net/. + * The original sources can be found on https://github.com/republib. + */ +#include "rplmath.hpp" + +/** @class RplRandom rplrandom.hpp "rplmath/rplrandom.hpp" + * + * @brief Implements a portable pseudo random generator. + * + */ + +/** + * @brief Constructor. + */ +RplRandom::RplRandom() : + m_seed(0) +{ +} + +/** + * @brief Destructor. + */ +RplRandom::~RplRandom(){ +} + +/** + * @brief Returns the next random number as 64 bit unsigned integer. + * + * @return the next random number. + */ +u_int64_t RplRandom::nextInt64(){ + // Knuth recommands (64-Bit): + m_seed = m_seed * 6364136223846793005L + 1442695040888963407L; + return m_seed; +} +/** + * @brief Sets the random seed. + * + * @param seed the new seed. + */ +void RplRandom::setSeed(u_int64_t seed){ + m_seed = seed; +} + +/** + * @brief nextByte returns the next random byte. + * + * @return a pseudo random value 0..255 + */ +u_int8_t RplRandom::nextByte(){ + u_int64_t value = nextInt64(); + // forget the last 3 bits: + u_int8_t rc = (u_int8_t) (value >> 3) % 256; + return rc; +} + +/** + * @brief Returns the next pseudo random integer. + * + * @param minValue the minimal result (including) + * @param maxValue the maximal result (includeing) + * @return the next integer + */ +int RplRandom::nextInt(int minValue, int maxValue){ + u_int64_t value = nextInt64(); + u_int64_t diff = maxValue - minValue; + int rc; + if (diff <= 0) + rc = minValue; + else + rc = (int) (minValue + value % diff); + return rc; +} + +/** + * @brief Returns a random string. + * + * @param length the length of the result + * @param minChar all characters of the result are greater or equal than this value + * @param maxChar all characters of the result are lower or equal than this value + * @return a random string + */ +QByteArray RplRandom::nextString(int length = 8, char minChar = ' ', char maxChar = 127){ + QByteArray rc; + rc.resize(length); + for (int ii = 0; ii < length; ii++){ + rc[ii] = minChar + nextInt(maxChar + 1); + } + return rc; +} + +/** + * @brief Returns a random string. + * + * @param length the length of the result + * @param charSet a string with all allowed characters + * @return a random string with characters from the given set + */ +QByteArray RplRandom::nextString(int length = 8, char* charSet){ + QByteArray rc; + rc.resize(length); + int ubound = strlen(charSet) - 1; + for (int ii = 0; ii < length; ii++){ + rc[ii] = charSet[nextInt(0, ubound)]; + } + return rc; +} diff --git a/rplmath/rplrandom.hpp b/rplmath/rplrandom.hpp new file mode 100644 index 0000000..db6c983 --- /dev/null +++ b/rplmath/rplrandom.hpp @@ -0,0 +1,22 @@ +#ifndef RPLRANDOM_HPP +#define RPLRANDOM_HPP + +#include +class RplRandom +{ +public: + RplRandom(); + ~RplRandom(); +public: + virtual u_int64_t nextInt64(); + virtual void setSeed(u_int64_t seed); + u_int8_t nextByte(); + int nextInt(int minValue, int maxValue); + QByteArray nextString(int length = 8, char minChar = ' ', char maxChar = 127); + QByteArray nextString(int length, char* charSet); +protected: + u_int64_t m_seed; +}; + + +#endif // RPLRANDOM_HPP diff --git a/rplnet/rpltcpserver.cpp b/rplnet/rpltcpserver.cpp index 6fa0743..59a388d 100644 --- a/rplnet/rpltcpserver.cpp +++ b/rplnet/rpltcpserver.cpp @@ -149,3 +149,51 @@ int RplTaskHandler::getThreadId() const{ return m_threadId; } +/** @class RplCryptoTaskHandler rpltcpserver.hpp "rplcore/rpltcpserver.hpp" + * + * @brief An abstract base class for an handler processing an encrypted data unit. + * + * The handler knows the stucture of the data unit and can interpret this. + * The data unit contains some authentification and encryption information. + * The embedded data are encrypted. + * + * Structure of the exchanged data units: + * + * The data unit (further named "envelope") is a RplContainer. + * It contains 1 bag with the following items: + *
    + *
  • string user: the login will be done for this user. + *
  • integer current_salt: the random generator must be initialized by this value. + * Then the embedded data can be decrypted. + * Note: other user specific data (certificate) affects the encryption as well.
  • + *
  • integer salt": this is the encrypted version of the current salt: + * Only if the sender knows the user's credentials this value can be calculated correctly. + * This is the authentification. + * The encryption is done in the 'hexadecimal mode': input of hex digits produses output of hex digits.
  • + *
  • string application_command. 2 characters for application, 2 characters for the command. + * Encrpyted in 'ASCII mode': input of chr(32)-chr(127) produces chr(32)-chr(127).
  • + *
  • container embeddedInfo: not encrypted: contains 1 bag with one item: the encrypted data.
    + * The encryption is done by the "byte stream mode". Each info-byte is "xor-ed" by an byte value of the random generator.
  • + *
+ */ +/** + * @brief Constructor. + */ +RplCryptoTaskHandler::RplCryptoTaskHandler(){ +} +/** + * @brief Destructor. + */ +RplCryptoTaskHandler::~RplCryptoTaskHandler(){ +} +/** + * @brief Processes an (partitionally) encrypted data unit. + * + * Reads one data unit, decrypts it and calls another task handler to process the unencrypted data. + * + * @param socket the socket for reading and writing + * @return true: the process must be finished. + */ +bool RplCryptoTaskHandler::handle(QAbstractSocket* socket){ + return false; +} diff --git a/rplnet/rpltcpserver.hpp b/rplnet/rpltcpserver.hpp index ba076a9..a4146fe 100644 --- a/rplnet/rpltcpserver.hpp +++ b/rplnet/rpltcpserver.hpp @@ -39,7 +39,7 @@ public: virtual ~RplTaskHandler(); public: - bool handle(QAbstractSocket* socket); + virtual bool handle(QAbstractSocket* socket); /** * @brief Processes one data unit from the socket. * @@ -61,6 +61,17 @@ private: int m_threadId; }; +class RplCryptoTaskHandler +{ +public: + RplCryptoTaskHandler(); + virtual ~RplCryptoTaskHandler(); +public: + + virtual bool handle(QAbstractSocket* socket); +}; + + class RplTcpThread : public QThread { Q_OBJECT public: -- 2.39.5