diff options
Diffstat (limited to 'src/crypt')
| -rw-r--r-- | src/crypt/cryptclassbase.cpp | 48 | ||||
| -rw-r--r-- | src/crypt/cryptclassbase.h | 250 | ||||
| -rw-r--r-- | src/crypt/cryptclassnullcipher.cpp | 21 | ||||
| -rw-r--r-- | src/crypt/cryptclassnullcipher.h | 49 | ||||
| -rw-r--r-- | src/crypt/cryptengine.cpp | 137 | ||||
| -rw-r--r-- | src/crypt/cryptengine.h | 261 |
6 files changed, 766 insertions, 0 deletions
diff --git a/src/crypt/cryptclassbase.cpp b/src/crypt/cryptclassbase.cpp new file mode 100644 index 0000000..88f99a1 --- /dev/null +++ b/src/crypt/cryptclassbase.cpp @@ -0,0 +1,48 @@ +#include "src/crypt/cryptclassbase.h" + +CryptClassBase::CryptClassBase() +{ +} + +CryptClassBase::~CryptClassBase() +{ + m_key.fill(0); + m_cryptText.fill(0); + m_clearText.fill(0); + m_binaryData = false; +} + +void CryptClassBase::generateRandomKey() +{ + qDebug("CryptClassBase::generateRandomKey - This Method does nothing so far!"); +} + +void CryptClassBase::setKey(QByteArray value) +{ + m_key = value; +} + +void CryptClassBase::setKey(const uchar* value, int keyLength ) +{ + m_key.setRawData( (const char*)value, keyLength ); +} + +void CryptClassBase::setCryptText( QByteArray value ) +{ + m_cryptText = value; +} + +void CryptClassBase::setCryptText( const uchar* value, int dataLength ) +{ + m_cryptText.setRawData( (const char*)value, dataLength ); +} + +void CryptClassBase::setClearText( QByteArray value ) +{ + m_clearText = value; +} + +void CryptClassBase::setClearText( const uchar* value, int dataLength ) +{ + m_clearText.setRawData( (const char*)value, dataLength ); +} diff --git a/src/crypt/cryptclassbase.h b/src/crypt/cryptclassbase.h new file mode 100644 index 0000000..064264e --- /dev/null +++ b/src/crypt/cryptclassbase.h @@ -0,0 +1,250 @@ +#ifndef CRYPTCLASSBASE_H +#define CRYPTCLASSBASE_H + +#include <QString> +#include <QByteArray> + + + + +/** +* \class CryptClassBase +* \brief Purely virtual Base-Class for all Ciphers. +* +* This class serves as Base or Parent Class for all Ciphers, containing Methods to set or get key, encrypted (CryptText) and unencrypted (ClearText) text. +* The purely virtual Methods encrypt and decrypt have to be overloaded in each subclass. +* \author Uwe Gogolin [Gogolin.Uwe@FH-SWF.de] +* \version 0.1 +* \date 28.02.2013 +*/ +class CryptClassBase +{ +public: //Methods + /** + * \brief Class Constructor. + * + * Class Constructor. Does nothing. + */ + CryptClassBase(); + + /** + * \brief Class Destructor. + * + * Class Destructor. Overwrites Key, Crypt- and ClearText with 0 when called. + */ + ~CryptClassBase(); + + + + /** + * \brief Method to generate a random Key. + * + * Overload this if your Cipher supports/ needs random Key generation. + * \see CryptEngine::generateRandomKey + */ + virtual void generateRandomKey(); + + + /** + * \brief Method to set the Key for en/decryption. + * + * Sets the Attribute m_key to contents of value. + * \param QByteArray value Contains the Key to be used for en/decryption + * \see CryptEngine::setKey + */ + virtual void setKey( QByteArray value ); + + + /** + * \brief Method to set the Key for en/decryption. + * + * Sets the Attribute m_key to contents of value of length keyLength. + * \param const uchar* value Contains the Key to be used for en/decryption + * \param int keyLength Length of keyData contained in value + * \see CryptEngine::setKey + */ + virtual void setKey( const uchar* value, int keyLength ); + + + /** + * \brief Method to get the Key used for en/decryption. + * + * Returns a reference to the Attribute m_key, containing the current key for en/decryption. + * \returns QBteArray& key The Key used for en/decryption. + * \see Cryptengine::getKey + */ + virtual QByteArray& getKey() { return m_key; } + + + /** + * \brief Method to set the length of the Key used for en/decryption. + * + * Sets the length of the Key used for en/decryption to value. + * \see Cryptengine::setKeyLength + */ + virtual void setKeyLength( int value ) { m_keyLength = value; } + + + /** + * \brief Method to set the Initialization Vector for Ciphers needing it. + * + * Sets the Initialization Vector to contents of iv. + * \param const QBteArray& iv The Initialization Vector to contents of iv. + * \see Cryptengine::setIV + */ + virtual void setIV( const QByteArray& iv ) { m_iv = iv; } + + + /** + * \brief Method to get the Initialization Vector for Ciphers needing it. + * + * Returns a reference to the the Initialization Vector. + * \returns QBteArray& The Initialization Vector for Ciphers needing it. + * \see Cryptengine::getIV + */ + virtual QByteArray& getIV() { return m_iv; } + + /** + * \brief True if algorithm uses binary data instead of plain text. + * + * Binar Data cannot be displayed as Plain Text and vice versa. + * This property holds information on whether to handle the Crypted + * text prodiced by this class. + * \returns bool: True if algorithm uses binary data instead of plain text. + * \see CryptEngine::oncipherSelected_triggered() + */ + bool getBinaryData() { return m_binaryData; } + + + /** + * \brief Method to set the encrypted text. + * + * Sets the Attribute m_cryptText to contents of value. + * \param QByteArray value Contains the encrypted data + * \see CryptEngine::setCryptText + */ + void setCryptText( QByteArray value ); + + + /** + * \brief Method to set the encrypted text. + * + * Sets the Attribute m_cryptText to contents of value of length dataLength. + * \param const uchar* value Contains the encrypted data + * \param int dataLength Length of encrypted data contained in value + * \see CryptEngine::setCryptText + */ + void setCryptText( const uchar* value, int dataLength ); + + + /** + * \brief Method to set the unencrypted text. + * + * Sets the Attribute m_clearText to contents of value. + * \param QByteArray value Contains the unencrypted data + * \see CryptEngine::setClearText + */ + void setClearText( QByteArray value ); + + + /** + * \brief Method to set the unencrypted text. + * + * Sets the Attribute m_clearText to contents of value of length dataLength. + * \param const uchar* value Contains the unencrypted data + * \param int dataLength Length of unencrypted data contained in value + * \see CryptEngine::setClearText + */ + void setClearText( const uchar* value, int keyLength ); + + + /** + * \brief Method to get the encrypted text. + * + * Returns the encrypted data as a QByteArray + * \see CryptEngine::getCryptText + * \returns QByteArray with encrypted data + */ + QByteArray getCryptText() { return m_cryptText; } + + + /** + * \brief Method to get the unencrypted text. + * + * Returns the unencrypted data as a QByteArray + * \see CryptEngine::getClearText + * \returns QByteArray with unencrypted data + */ + QByteArray getClearText() { return m_clearText; } + + + /** + * \brief Purely virtual Method for encryption. + * + * SubClasses need to overload this purely virtual method so that it contains or calls all necessary steps to encrypt the data in + * m_clearText using the algorithm the specific SubClass is designed for. The encrypted Text will be stored in m_cryptText. + */ + virtual void encrypt() = 0; + + + /** + * \brief Purely virtual Method for decryption. + * + * SubClasses need to overload this purely virtual method so that it contains or calls all necessary steps to decrypt the data in + * m_cryptText using the algorithm the specific SubClass is designed for. The decrypted Text will be stored in m_clearText. + */ + virtual void decrypt() = 0; + + + +protected: //Attributes + /** + * \brief ByteArray to hold the Key for en/decryption. + * + * \attention Sensitive data. Overwrite when no longer needed! + */ + QByteArray m_key; + + + /** + * \brief Length of Key to use for the Cipher. + */ + int m_keyLength; + + + /** + * \brief ByteArray to hold the Initialization Vector for Ciphers needing it. + * + * \attention Sensitive data. Overwrite when no longer needed! + */ + QByteArray m_iv; + + + /** + * \brief ByteArray to hold the encrypted text. + * + * \attention Sensitive data. Overwrite when no longer needed! + */ + QByteArray m_cryptText; + + + /** + * \brief ByteArray to hold the unencrypted text. + * + * \attention Sensitive data. Overwrite when no longer needed! + */ + QByteArray m_clearText; + + + /** + * \brief True if algorithm uses binary data instead of plain text. + * + * Binar Data cannot be displayed as Plain Text and vice versa. + * This property holds information on whether to handle the Crypted + * text prodiced by this class. + * \see CryptEngine::oncipherSelected_triggered() + */ + bool m_binaryData; +}; + +#endif // CRYPTCLASSBASE_H diff --git a/src/crypt/cryptclassnullcipher.cpp b/src/crypt/cryptclassnullcipher.cpp new file mode 100644 index 0000000..29e9dd7 --- /dev/null +++ b/src/crypt/cryptclassnullcipher.cpp @@ -0,0 +1,21 @@ +#include "cryptclassnullcipher.h" + +CryptClassNullCipher::CryptClassNullCipher() + : CryptClassBase() +{ + +} + +void CryptClassNullCipher::encrypt() +{ + m_cryptText = m_clearText; +} + + + + + +void CryptClassNullCipher::decrypt() +{ + m_clearText = m_cryptText; +} diff --git a/src/crypt/cryptclassnullcipher.h b/src/crypt/cryptclassnullcipher.h new file mode 100644 index 0000000..6b3c284 --- /dev/null +++ b/src/crypt/cryptclassnullcipher.h @@ -0,0 +1,49 @@ +#ifndef CRYPTCLASSNULLCIPHER_H +#define CRYPTCLASSNULLCIPHER_H + +#include "cryptclassbase.h" + +#include <openssl/evp.h> + + +/** +* \class CryptClassNullCipher +* \brief For testing purposes, NullCipher does nothing to the Text. +* +* The NullCipher class is, like all Ciphers/Algorithm-classes, derived from CryptclassBase. +* It's intended to be used for testing purposes of the overall Programm-Funcionality only and does +* no encryption whatsoever. Data is merely copied from m_cryptText to m_clearText and vice versa. +* \author Uwe Gogolin [Gogolin.Uwe@FH-SWF.de] +* \version 0.1 +* \date 28.02.2013 +*/ +class CryptClassNullCipher : public CryptClassBase +{ +public: //Methods + /** + * \brief Class Constructor. + * + * Class Constructor. Does nothing. + */ + CryptClassNullCipher(); + + + /** + * \brief Overloaded Method to encrypt present unencrypted data using the current key. + * + * NullCipher is used for testing purposes and simulation. No actual encryption is done. + * Data is copied from m_clearText to m_cryptText. + */ + virtual void encrypt(); + + + /** + * \brief Overloaded Method to decrypt present encrypted data using the current key. + * + * NullCipher is used for testing purposes and simulation. No actual decryption is done. + * Data is copied from m_cryptText to m_clearText. + */ + virtual void decrypt(); +}; + +#endif // CRYPTCLASSNULLCIPHER_H diff --git a/src/crypt/cryptengine.cpp b/src/crypt/cryptengine.cpp new file mode 100644 index 0000000..32e33bb --- /dev/null +++ b/src/crypt/cryptengine.cpp @@ -0,0 +1,137 @@ +#include "cryptengine.h" + +CryptEngine::CryptEngine(QObject *parent) + : QObject(parent) +{ + m_algorithm = 0; //Initialize NullCipher + m_cryptClass = new CryptClassNullCipher; +} + + +CryptEngine::~CryptEngine() +{ + delete m_cryptClass; +} + +void CryptEngine::generateRandomKey() +{ + m_cryptClass->generateRandomKey(); +} + +void CryptEngine::setKey(QString value) +{ + m_cryptClass->setKey( value.toLatin1() ); +} + +void CryptEngine::setKey(QByteArray value) +{ + m_cryptClass->setKey( value ); +} + +void CryptEngine::setKey(const uchar *value, int keyLength ) +{ + m_cryptClass->setKey( value, keyLength ); +} + +QByteArray &CryptEngine::getKey() +{ + return m_cryptClass->getKey(); +} + +void CryptEngine::setCryptText(QByteArray value) +{ + m_cryptClass->setCryptText(value); +} + +void CryptEngine::setCryptText(const uchar *value, int dataLength ) +{ + m_cryptClass->setCryptText(value, dataLength); +} + +void CryptEngine::setClearText(QByteArray value) +{ + m_cryptClass->setClearText(value); +} + +void CryptEngine::setClearText(const uchar *value, int dataLength ) +{ + m_cryptClass->setClearText(value, dataLength); +} + +void CryptEngine::encrypt() +{ + //If cleartext present, set in CryptClass, encrypt, and fetch encrypted data + if( m_cryptClass->getClearText().length() > 0 ) + { + m_cryptClass->encrypt(); + } +} + +void CryptEngine::decrypt() +{ + //If crypttext present, set in CryptClass, decrypt, and fetch decrypted data + if( m_cryptClass->getCryptText().length() > 0 ) + { + m_cryptClass->decrypt(); + } +} + +void CryptEngine::oncipherSelected_triggered(int cipher) +{ + m_algorithm = cipher; + + //Get Instance of Singleton mapping Integers to String for Cipher description + CiphersSingleton ciphers = CiphersSingleton::getInstance(); + + //Algorithm contains cipher as text. + QString algorithm = ciphers.textFromValue(m_algorithm); + + + //delete old CryptClass and create new one according to selected algorithm + if( algorithm == "NullCipher" ) + { + delete m_cryptClass; + m_cryptClass = new CryptClassNullCipher; + qDebug("CryptEngine::oncipherSelected_triggered - NullCipher"); + } + else if( algorithm == "Caesar" ) + { +// delete m_cryptClass; +// m_cryptClass = new CryptClassCaesar; + qDebug("CryptEngine::oncipherSelected_triggered - Caesar"); + } + else if( algorithm == "Cube" ) + { +// delete m_cryptClass; +// m_cryptClass = new CryptClassCube; + qDebug("CryptEngine::oncipherSelected_triggered - Cube"); + } + else if( algorithm == "AES" ) + { +// delete m_cryptClass; +// m_cryptClass = new CryptClassAES; + qDebug("CryptEngine::oncipherSelected_triggered - AES"); + } + else if( algorithm == "RSA" ) + { +// delete m_cryptClass; +// m_cryptClass = new CryptClassRSA; + qDebug("CryptEngine::oncipherSelected_triggered - RSA"); + } + else if( algorithm == "ECC" ) + { +// delete m_cryptClass; +// m_cryptClass = new CryptClassECC; + qDebug("CryptEngine::oncipherSelected_triggered - ECC"); + } + + //Send Signal confirming if data is binary or not + emit binaryData( m_cryptClass->getBinaryData() ); +} + +void CryptEngine::onkeylengthChanged(int value, bool symmetric) +{ + qDebug("CrptEngine::onkeylengthChanged - new Keylength: %d", value); + + m_cryptClass->setKeyLength(value); +} diff --git a/src/crypt/cryptengine.h b/src/crypt/cryptengine.h new file mode 100644 index 0000000..9272183 --- /dev/null +++ b/src/crypt/cryptengine.h @@ -0,0 +1,261 @@ +#ifndef CRYPTENGINE_H +#define CRYPTENGINE_H + +#include <QObject> + +#include "../cipherssingleton.h" +#include "cryptclassbase.h" +#include "cryptclassnullcipher.h" +//#include "cryptclasscaesar.h" +//#include "cryptclasscube.h" +//#include "cryptclassaes.h" +//#include "cryptclassrsa.h" +//#include "cryptclassecc.h" + + +/** +* \class CryptEngine +* \brief Interface for all algorithms. +* +* This class provides an easy to use interface for all Ciphers/algorithms (used as synonyms) and encapsulates all functionality +* related to de/encryption. +* Ciphers are referenced by NameSpaceCryptEngine::CryptAlgorithm (see enums.h), the currently active one stored in m_algorithm. +* The Functionality for every Cipher is encapsulated in a SubClass of CryptClassBase, m_cryptClass points to an instance of the +* one for the currently active algorithm. +* \author Uwe Gogolin [Gogolin.Uwe@FH-SWF.de] +* \version 0.7 +* \date 13.05.2013 +*/ +class CryptEngine : public QObject +{ + Q_OBJECT + +public: //Methods + /** + * \brief Class Constructor. + * + * Class Constructor. Initializes NullCipher. + * \param QObject* parent The QObject the used instance of this class is subordinated to. + */ + CryptEngine( QObject* parent = 0 ); + + + /** + * \brief Class Destructor. + * + * Class Destructor. Overwrites Key, Crypt- and ClearText with 0 when called. + */ + ~CryptEngine(); + + + + /** + * \brief Method to generate a random Key. + * + * Calls Method generateKey of the CryptClass in use. + * \attention This Method may do nothing depending on the Algorithm in use. + * \see CryptClassBase::generateRandomKey + */ + void generateRandomKey(); + + + /** + * \brief Method to set the Key for en/decryption. + * + * Sets the Attribute m_key to contents of value. + * \param QString value Contains the Key to be used for en/decryption + * \see CryptClassBase::setKey + */ + void setKey( QString value ); + + + /** + * \brief Method to set the Key for en/decryption. + * + * Sets the Attribute m_key to contents of value. + * \param QByteArray value Contains the Key to be used for en/decryption + * \see CryptClassBase::setKey + */ + void setKey( QByteArray value ); + + + /** + * \brief Method to set the Key for en/decryption. + * + * Sets the Attribute m_key to contents of value of length keyLength. + * \param const uchar* value Contains the Key to be used for en/decryption + * \param int keyLength Length of keyData contained in value + * \see CryptClassBase::setKey + */ + void setKey( const uchar* value, int keyLength ); + + + /** + * \brief Method to get the Key used for en/decryption. + * + * Returns a reference to the Attribute m_key, containing the current key for en/decryption. + * \returns QBteArray& key The Key used for en/decryption. + * \see CryptClassBase::getKey + */ + QByteArray& getKey(); + + + /** + * \brief Method to set the length of the Key used for en/decryption. + * + * Sets the length of the Key used for en/decryption to value. + * \see CryptClassBase::setKeyLength + */ + virtual void setKeyLength( int value ) { m_cryptClass->setKeyLength(value); } + + + /** + * \brief Method to set the Initialization Vector for Ciphers needing it. + * + * Sets the Initialization Vector to contents of iv. + * \param const QBteArray& iv The Initialization Vector to contents of iv. + * \see CryptClassBase::setIV + */ + virtual void setIV( const QByteArray& iv ) { m_cryptClass->setIV(iv); } + + + /** + * \brief Method to get the Initialization Vector for Ciphers needing it. + * + * Returns a reference to the the Initialization Vector. + * \returns QBteArray& The Initialization Vector for Ciphers needing it. + * \see CryptClassBase::getIV + */ + virtual QByteArray& getIV() { return m_cryptClass->getIV(); } + + + /** + * \brief Method to set the encrypted text. + * + * Sets the Attribute m_cryptText to contents of value. + * \param QByteArray value Contains the encrypted data + * \see CryptClassBase::setCryptText + */ + void setCryptText( QByteArray value ); + + + /** + * \brief Method to set the encrypted text. + * + * Sets the Attribute m_cryptText to contents of value of length dataLength. + * \param const uchar* value Contains the encrypted data + * \param int dataLength Length of encrypted data contained in value + * \see CryptClassBase::setCryptText + */ + void setCryptText( const uchar* value, int dataLength ); + + + /** + * \brief Method to set the unencrypted text. + * + * Sets the Attribute m_clearText to contents of value. + * \param QByteArray value Contains the unencrypted data + * \see CryptClassBase::setClearText + */ + void setClearText( QByteArray value ); + + + /** + * \brief Method to set the unencrypted text. + * + * Sets the Attribute m_clearText to contents of value of length dataLength. + * \param const uchar* value Contains the unencrypted data + * \param int dataLength Length of unencrypted data contained in value + * \see CryptClassBase::setClearText + */ + void setClearText( const uchar* value, int keyLength ); + + + /** + * \brief Method to get the encrypted text. + * + * Returns the encrypted data as a QByteArray + * \see CryptClassBase::getCryptText + * \returns QByteArray with encrypted data + */ + QByteArray getCryptText() { return m_cryptClass->getCryptText(); } + + + /** + * \brief Method to get the unencrypted text. + * + * Returns the unencrypted data as a QByteArray + * \see CryptClassBase::getClearText + * \returns QByteArray with unencrypted data + */ + QByteArray getClearText() { return m_cryptClass->getClearText(); } + + + /** + * \brief Method to encrypt present unencrypted data using the currently selected algorithm and key. + * + * Calls the encrypt method of the currently selected algorithm + */ + void encrypt(); + + + /** + * \brief Method to decrypt present encrypted data using the currently selected algorithm and key. + * + * Calls the decrypt method of the currently selected algorithm + */ + void decrypt(); + + + +public slots: + /** + * \brief Slot called when an active algorithm gets selected. + * + * Deletes intance of former Cipher and creates a new object for the selected one. + * \param int cipher Integer referencing the selected Cipher according to assignment in Class CiphersSingleton. + * \see Class CiphersSingleton + */ + void oncipherSelected_triggered( int cipher ); + + + /** + * \brief SLOT to set the new Keylength. + * + * Called when a new Keylength is selected. Passes the value on in connection with the bool + * signifying that it is for a symmetric Cipher (true) or an asymmetric one (false). + * \param int value The new Keylength. + * \param bool symmetric true for symmetric Cipher, false for asymmetric. + */ + void onkeylengthChanged(int value, bool symmetric); + + +signals: + /** + * \brief SIGNAL to inform other Classes that the selected Cipher uses binary data. + * + * This Signal is used to inform other Classes that the selected Cipher uses binary data. + * \param bool dataIsbinary is true when data should be displayes as Hex. + */ + void binaryData( bool dataIsBinary ); + + +private: //Attributes + /** + * \brief Enum for currently used algorithm (NullCipher/ Caesar). + * + * \see File enums.h for list of available algorithms + */ + int m_algorithm; + + + /** + * \brief Pointer to an instance of a SubClass of CryptclassBase for the currently used algorithm. + * + * Should either be NULL or point to an instance of the SubClass for the algorithm specified in m_algorithm + * \see CryptClassBase + */ + CryptClassBase* m_cryptClass; +}; + +#endif // CRYPTENGINE_H |
