summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorStefan Suhren <suhren.stefan@fh-swf.de>2015-05-27 13:02:31 +0200
committerStefan Suhren <suhren.stefan@fh-swf.de>2015-05-27 13:02:31 +0200
commit220702c05e5c67817e5ba45765fd75ead8e92bb3 (patch)
tree5daf0156e022c79d9fc4142995195435fe5b3f5d /src
parent5db765c2ee63f0ee6774817cf85fcb4b5078ca4a (diff)
downloadIT-Sicherheit-220702c05e5c67817e5ba45765fd75ead8e92bb3.tar.gz
IT-Sicherheit-220702c05e5c67817e5ba45765fd75ead8e92bb3.zip
Use an EVP base class to reduce repetition
Diffstat (limited to 'src')
-rw-r--r--src/crypt/cryptclassbase.h2
-rw-r--r--src/crypt/cryptclassevp.cpp115
-rw-r--r--src/crypt/cryptclassevp.h48
-rw-r--r--src/crypt/cryptclassnullcipher.cpp110
-rw-r--r--src/crypt/cryptclassnullcipher.h30
5 files changed, 170 insertions, 135 deletions
diff --git a/src/crypt/cryptclassbase.h b/src/crypt/cryptclassbase.h
index 064264e..ed02d85 100644
--- a/src/crypt/cryptclassbase.h
+++ b/src/crypt/cryptclassbase.h
@@ -32,7 +32,7 @@ public: //Methods
*
* Class Destructor. Overwrites Key, Crypt- and ClearText with 0 when called.
*/
- ~CryptClassBase();
+ virtual ~CryptClassBase();
diff --git a/src/crypt/cryptclassevp.cpp b/src/crypt/cryptclassevp.cpp
new file mode 100644
index 0000000..1ee09a0
--- /dev/null
+++ b/src/crypt/cryptclassevp.cpp
@@ -0,0 +1,115 @@
+#include "cryptclassevp.h"
+
+CryptClassEvp::CryptClassEvp()
+ : CryptClassBase()
+{
+ ERR_load_crypto_strings();
+ OpenSSL_add_all_algorithms();
+}
+
+CryptClassEvp::~CryptClassEvp()
+{
+ EVP_cleanup();
+ ERR_free_strings();
+}
+
+void CryptClassEvp::encrypt()
+{
+ m_cryptText = QByteArray(m_clearText.size(), 0);
+
+ EVP_CIPHER_CTX *ctx;
+
+ int len;
+
+ int ciphertext_len;
+
+ /* Create and initialise the context */
+ if (!(ctx = EVP_CIPHER_CTX_new()))
+ {
+ handleOpenSslError();
+ }
+
+ /* Initialise the encryption operation. IMPORTANT - ensure you use a key
+ * and IV size appropriate for your cipher */
+ if (1 != EVP_EncryptInit_ex(ctx, algorithm(), NULL, (unsigned char *) getKey().data(), (unsigned char *) getIV().data()))
+ {
+ handleOpenSslError();
+ }
+
+ /* Provide the message to be encrypted, and obtain the encrypted output.
+ * EVP_EncryptUpdate can be called multiple times if necessary
+ */
+ if (1 != EVP_EncryptUpdate(ctx, (unsigned char *) m_cryptText.data(), &len,
+ (unsigned char *) m_clearText.data(), m_clearText.length()))
+ {
+ handleOpenSslError();
+ }
+
+ ciphertext_len = len;
+
+ /* Finalise the encryption. Further ciphertext bytes may be written at
+ * this stage.
+ */
+ if (1 != EVP_EncryptFinal_ex(ctx, (unsigned char *)(m_cryptText.data() + len),
+ &len))
+ {
+ handleOpenSslError();
+ }
+
+ ciphertext_len += len;
+
+ /* Clean up */
+ EVP_CIPHER_CTX_free(ctx);
+}
+
+void CryptClassEvp::decrypt()
+{
+ m_clearText = QByteArray(m_cryptText.size(), 0);
+
+ EVP_CIPHER_CTX *ctx;
+
+ int len;
+
+ int plaintext_len;
+
+ /* Create and initialise the context */
+ if (!(ctx = EVP_CIPHER_CTX_new()))
+ {
+ handleOpenSslError();
+ }
+
+ /* Initialise the decryption operation. IMPORTANT - ensure you use a key
+ * and IV size appropriate for your cipher */
+ if (1 != EVP_DecryptInit_ex(ctx, algorithm(), NULL, (unsigned char *) getKey().data(), (unsigned char *) getIV().data()))
+ {
+ handleOpenSslError();
+ }
+
+ /* Provide the message to be decrypted, and obtain the plaintext output.
+ * EVP_DecryptUpdate can be called multiple times if necessary
+ */
+ if (1 != EVP_DecryptUpdate(ctx, (unsigned char*) m_clearText.data(), &len, (unsigned char*) m_cryptText.data(), m_cryptText.length()))
+ {
+ handleOpenSslError();
+ }
+
+ plaintext_len = len;
+
+ /* Finalise the decryption. Further plaintext bytes may be written at
+ * this stage.
+ */
+ if (1 != EVP_DecryptFinal_ex(ctx, (unsigned char*) m_cryptText.data() + len, &len))
+ {
+ handleOpenSslError();
+ }
+
+ plaintext_len += len;
+
+ /* Clean up */
+ EVP_CIPHER_CTX_free(ctx);
+}
+
+void CryptClassEvp::handleOpenSslError()
+{
+ throw std::runtime_error(ERR_reason_error_string(ERR_get_error()));
+}
diff --git a/src/crypt/cryptclassevp.h b/src/crypt/cryptclassevp.h
new file mode 100644
index 0000000..8e9c66c
--- /dev/null
+++ b/src/crypt/cryptclassevp.h
@@ -0,0 +1,48 @@
+#ifndef CRYPTCLASSEVP_H
+#define CRYPTCLASSEVP_H
+
+#include "cryptclassbase.h"
+
+#include <openssl/evp.h>
+#include <openssl/err.h>
+#include <stdexcept>
+
+/**
+* @author Walter Roth, 2015
+* @brief The CryptClassEvp is a class for symmetric cryptography using OpenSSL's EVP API.
+* Subclasses must overwrite the algorithm function to specify the algorithm to be used.
+* The implementation of algorithm in this class returns a NULL-Cipher object (no encryption).
+* This is quite useful for debugging.
+*
+*/
+class CryptClassEvp : public CryptClassBase
+{
+public:
+ CryptClassEvp();
+ virtual ~CryptClassEvp();
+
+ /**
+ * @brief setAlgorithm Overwrite this function to specify the algorithm to be used.
+ * @param cipher The cipher object e.g. EVP_bf_cbc() for Blowfish in CBC mode.
+ */
+ virtual const EVP_CIPHER *algorithm() = 0;
+
+ /**
+ * @brief encrypt Setup context and key and encrypt m_clearText into m_cryptText.
+ */
+ void encrypt();
+
+ /**
+ * @brief encrypt Setup context and key and decrypt m_cryptText into m_clearText.
+ */
+ void decrypt();
+
+ /**
+ * @brief handleOpenSslError Calls ERR_get_error and sends debug output to stderr.
+ * @param file The __FILE__ makro
+ * @param line The __LINE__ makro
+ * @return
+ */
+ void handleOpenSslError();
+};
+#endif // CRYPTCLASSEVP_H
diff --git a/src/crypt/cryptclassnullcipher.cpp b/src/crypt/cryptclassnullcipher.cpp
index 1dbc349..a60bab4 100644
--- a/src/crypt/cryptclassnullcipher.cpp
+++ b/src/crypt/cryptclassnullcipher.cpp
@@ -1,119 +1,15 @@
#include "cryptclassnullcipher.h"
CryptClassNullCipher::CryptClassNullCipher()
- : CryptClassBase()
+ : CryptClassEvp()
{
- ERR_load_crypto_strings();
- OpenSSL_add_all_algorithms();
}
CryptClassNullCipher::~CryptClassNullCipher()
{
- EVP_cleanup();
- ERR_free_strings();
}
-void CryptClassNullCipher::encrypt()
+const EVP_CIPHER *CryptClassNullCipher::algorithm()
{
- // int encrypt(unsigned char *plaintext, int plaintext_len, unsigned char *key,
- // unsigned char *iv, unsigned char *ciphertext)
-
- m_cryptText = QByteArray(m_clearText.size() + 50, 0);
-
- EVP_CIPHER_CTX *ctx;
-
- int len;
-
- int ciphertext_len;
-
- /* Create and initialise the context */
- if (!(ctx = EVP_CIPHER_CTX_new()))
- {
- handleErrors();
- }
-
- /* Initialise the encryption operation. IMPORTANT - ensure you use a key
- * and IV size appropriate for your cipher */
- if (1 != EVP_EncryptInit_ex(ctx, EVP_enc_null(), NULL, NULL, NULL))
- {
- handleErrors();
- }
-
- /* Provide the message to be encrypted, and obtain the encrypted output.
- * EVP_EncryptUpdate can be called multiple times if necessary
- */
- if (1 != EVP_EncryptUpdate(ctx, (unsigned char *) m_cryptText.data(), &len,
- (unsigned char *) m_clearText.data(), m_clearText.length()))
- {
- handleErrors();
- }
-
- ciphertext_len = len;
-
- /* Finalise the encryption. Further ciphertext bytes may be written at
- * this stage.
- */
- if (1 != EVP_EncryptFinal_ex(ctx, (unsigned char *)(m_cryptText.data() + len),
- &len))
- {
- handleErrors();
- }
-
- ciphertext_len += len;
-
- /* Clean up */
- EVP_CIPHER_CTX_free(ctx);
-}
-
-void CryptClassNullCipher::decrypt()
-{
- m_clearText = QByteArray(m_cryptText.size() + 50, 0);
-
- EVP_CIPHER_CTX *ctx;
-
- int len;
-
- int plaintext_len;
-
- /* Create and initialise the context */
- if (!(ctx = EVP_CIPHER_CTX_new()))
- {
- handleErrors();
- }
-
- /* Initialise the decryption operation. IMPORTANT - ensure you use a key
- * and IV size appropriate for your cipher */
- if (1 != EVP_DecryptInit_ex(ctx, EVP_enc_null(), NULL, NULL, NULL))
- {
- handleErrors();
- }
-
- /* Provide the message to be decrypted, and obtain the plaintext output.
- * EVP_DecryptUpdate can be called multiple times if necessary
- */
- if (1 != EVP_DecryptUpdate(ctx, (unsigned char*) m_clearText.data(), &len, (unsigned char*) m_cryptText.data(), m_cryptText.length()))
- {
- handleErrors();
- }
-
- plaintext_len = len;
-
- /* Finalise the decryption. Further plaintext bytes may be written at
- * this stage.
- */
- if (1 != EVP_DecryptFinal_ex(ctx, (unsigned char*) m_cryptText.data() + len, &len))
- {
- handleErrors();
- }
-
- plaintext_len += len;
-
- /* Clean up */
- EVP_CIPHER_CTX_free(ctx);
-}
-
-void CryptClassNullCipher::handleErrors()
-{
- qDebug() << ERR_reason_error_string(ERR_get_error());
- abort();
+ return EVP_enc_null();
}
diff --git a/src/crypt/cryptclassnullcipher.h b/src/crypt/cryptclassnullcipher.h
index ae05ef6..b25c8e9 100644
--- a/src/crypt/cryptclassnullcipher.h
+++ b/src/crypt/cryptclassnullcipher.h
@@ -1,13 +1,7 @@
#ifndef CRYPTCLASSNULLCIPHER_H
#define CRYPTCLASSNULLCIPHER_H
-#include "cryptclassbase.h"
-
-#include <QDebug>
-
-#include <openssl/evp.h>
-#include <openssl/err.h>
-#include <openssl/conf.h>
+#include "cryptclassevp.h"
/**
@@ -21,7 +15,7 @@
* \version 0.1
* \date 28.02.2013
*/
-class CryptClassNullCipher : public CryptClassBase
+class CryptClassNullCipher : public CryptClassEvp
{
public: //Methods
/**
@@ -38,25 +32,7 @@ public: //Methods
*/
~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();
-
-private:
- void handleErrors();
+ virtual const EVP_CIPHER *algorithm();
};
#endif // CRYPTCLASSNULLCIPHER_H