--- /dev/null
+/*
+ * 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<br>
+ * 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<QByteArray> m_byteSeeds;
+ ///> each call of setSeed sets this value to 0.
+ int m_ixSeed;
+ };
--- /dev/null
+#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<QByteArray> m_byteSeeds;
+ ///> each call of setSeed sets this value to 0.
+ int m_ixSeed;
+ };
+
+#endif // RPLENIGMA_HPP
--- /dev/null
+/*
+ * 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
--- /dev/null
+/*
+ * 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;
+}
--- /dev/null
+#ifndef RPLRANDOM_HPP
+#define RPLRANDOM_HPP
+
+#include <QtCore>
+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
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 <code>RplContainer</code>.
+ * It contains 1 bag with the following items:
+ * <ul>
+ * <li>string user: the login will be done for this user.
+ * <li>integer current_salt: the random generator must be initialized by this value.
+ * Then the embedded data can be decrypted.
+ * <b>Note: other user specific data (certificate) affects the encryption as well.</li>
+ * <li>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.</li>
+ * <li>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).</li>
+ * <li>container embeddedInfo: not encrypted: contains 1 bag with one item: the encrypted data.<br>
+ * The encryption is done by the "byte stream mode". Each info-byte is "xor-ed" by an byte value of the random generator. </li>
+ * </ul>
+ */
+/**
+ * @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;
+}
virtual ~RplTaskHandler();
public:
- bool handle(QAbstractSocket* socket);
+ virtual bool handle(QAbstractSocket* socket);
/**
* @brief Processes one data unit from the socket.
*
int m_threadId;
};
+class RplCryptoTaskHandler
+{
+public:
+ RplCryptoTaskHandler();
+ virtual ~RplCryptoTaskHandler();
+public:
+
+ virtual bool handle(QAbstractSocket* socket);
+};
+
+
class RplTcpThread : public QThread {
Q_OBJECT
public: