summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStefan Suhren <suhren.stefan@fh-swf.de>2015-05-27 15:07:29 +0200
committerStefan Suhren <suhren.stefan@fh-swf.de>2015-05-27 15:07:29 +0200
commit672ebc3868997c44b83f7afe59e9b4d876135247 (patch)
treef2a7a48dc1ff6c85e417767bfd237be331897423
parent220702c05e5c67817e5ba45765fd75ead8e92bb3 (diff)
downloadIT-Sicherheit-672ebc3868997c44b83f7afe59e9b4d876135247.tar.gz
IT-Sicherheit-672ebc3868997c44b83f7afe59e9b4d876135247.zip
Add an AES implementation with openssl evp
-rw-r--r--IT-Sicherheit_SS2015_BaseProject.pro6
-rw-r--r--src/crypt/cryptaes.cpp111
-rw-r--r--src/crypt/cryptaes.h21
-rw-r--r--src/crypt/cryptclassbase.cpp1
-rw-r--r--src/crypt/cryptclassevp.cpp7
-rw-r--r--src/crypt/cryptengine.cpp4
-rw-r--r--src/crypt/cryptengine.h2
7 files changed, 144 insertions, 8 deletions
diff --git a/IT-Sicherheit_SS2015_BaseProject.pro b/IT-Sicherheit_SS2015_BaseProject.pro
index efbfe13..2fbf22f 100644
--- a/IT-Sicherheit_SS2015_BaseProject.pro
+++ b/IT-Sicherheit_SS2015_BaseProject.pro
@@ -29,7 +29,8 @@ SOURCES += src/main.cpp \
src/crypt/cryptclasscaesar.cpp \
src/crypt/cryptclassrc4.cpp \
src/crypt/cryptrc4.cpp \
- src/crypt/cryptclassevp.cpp
+ src/crypt/cryptclassevp.cpp \
+ src/crypt/cryptaes.cpp
HEADERS += src/mainwindow.h \
src/tabwidgetselectalgorithm.h \
@@ -46,7 +47,8 @@ HEADERS += src/mainwindow.h \
src/crypt/cryptclasscaesar.h \
src/crypt/cryptclassrc4.h \
src/crypt/cryptrc4.h \
- src/crypt/cryptclassevp.h
+ src/crypt/cryptclassevp.h \
+ src/crypt/cryptaes.h
FORMS += src/forms/mainwindow.ui \
src/forms/tabsymmetric.ui \
diff --git a/src/crypt/cryptaes.cpp b/src/crypt/cryptaes.cpp
new file mode 100644
index 0000000..b74ae03
--- /dev/null
+++ b/src/crypt/cryptaes.cpp
@@ -0,0 +1,111 @@
+#include "cryptaes.h"
+
+CryptAes::CryptAes()
+ : CryptClassEvp()
+{
+ m_iv = QByteArray(16, 0);
+}
+
+const EVP_CIPHER *CryptAes::algorithm()
+{
+ switch (m_keyLength)
+ {
+ case 128:
+
+ return EVP_aes_128_cbc();
+ break;
+
+ case 192:
+
+ return EVP_aes_192_cbc();
+ break;
+
+ case 256:
+
+ return EVP_aes_256_cbc();
+ break;
+ }
+
+ return EVP_enc_null();
+}
+
+
+void CryptAes::encrypt()
+{
+ EVP_MD_CTX *ctx;
+
+ /* Create and initialise the context */
+ if (!(ctx = EVP_MD_CTX_create()))
+ {
+ handleOpenSslError();
+ }
+
+ /* Initialise the encryption operation. IMPORTANT - ensure you use a key
+ * and IV size appropriate for your cipher */
+ if (1 != EVP_DigestInit_ex(ctx, EVP_sha256(), NULL))
+ {
+ handleOpenSslError();
+ }
+
+ /* Provide the message to be encrypted, and obtain the encrypted output.
+ * EVP_EncryptUpdate can be called multiple times if necessary
+ */
+ if (1 != EVP_DigestUpdate(ctx, m_key.data(), m_key.size()))
+ {
+ handleOpenSslError();
+ }
+
+ m_key = QByteArray(256, 0);
+
+ /* Finalise the encryption. Further ciphertext bytes may be written at
+ * this stage.
+ */
+ if (1 != EVP_DigestFinal_ex(ctx, (unsigned char *) m_key.data(), NULL))
+ {
+ handleOpenSslError();
+ }
+
+ EVP_MD_CTX_destroy(ctx);
+
+ CryptClassEvp::encrypt();
+}
+
+void CryptAes::decrypt()
+{
+ EVP_MD_CTX *ctx;
+
+ /* Create and initialise the context */
+ if (!(ctx = EVP_MD_CTX_create()))
+ {
+ handleOpenSslError();
+ }
+
+ /* Initialise the encryption operation. IMPORTANT - ensure you use a key
+ * and IV size appropriate for your cipher */
+ if (1 != EVP_DigestInit_ex(ctx, EVP_sha256(), NULL))
+ {
+ handleOpenSslError();
+ }
+
+ /* Provide the message to be encrypted, and obtain the encrypted output.
+ * EVP_EncryptUpdate can be called multiple times if necessary
+ */
+ if (1 != EVP_DigestUpdate(ctx, m_key.data(), m_key.size()))
+ {
+ handleOpenSslError();
+ }
+
+ m_key = QByteArray(256, 0);
+
+ /* Finalise the encryption. Further ciphertext bytes may be written at
+ * this stage.
+ */
+ if (1 != EVP_DigestFinal_ex(ctx, (unsigned char *) m_key.data(), NULL))
+ {
+ handleOpenSslError();
+ }
+
+ EVP_MD_CTX_destroy(ctx);
+
+ CryptClassEvp::decrypt();
+}
diff --git a/src/crypt/cryptaes.h b/src/crypt/cryptaes.h
new file mode 100644
index 0000000..58ddba1
--- /dev/null
+++ b/src/crypt/cryptaes.h
@@ -0,0 +1,21 @@
+#ifndef CRYPTAES_H
+#define CRYPTAES_H
+
+#include "cryptclassevp.h"
+
+#include <QDebug>
+
+class CryptAes : public CryptClassEvp
+{
+public:
+ CryptAes();
+
+ // CryptClassEvp interface
+public:
+ virtual const EVP_CIPHER *algorithm();
+
+ virtual void encrypt();
+ virtual void decrypt();
+};
+
+#endif // CRYPTAES_H
diff --git a/src/crypt/cryptclassbase.cpp b/src/crypt/cryptclassbase.cpp
index 88f99a1..ac4a812 100644
--- a/src/crypt/cryptclassbase.cpp
+++ b/src/crypt/cryptclassbase.cpp
@@ -10,6 +10,7 @@ CryptClassBase::~CryptClassBase()
m_cryptText.fill(0);
m_clearText.fill(0);
m_binaryData = false;
+ m_keyLength = 0;
}
void CryptClassBase::generateRandomKey()
diff --git a/src/crypt/cryptclassevp.cpp b/src/crypt/cryptclassevp.cpp
index 1ee09a0..7aec7b6 100644
--- a/src/crypt/cryptclassevp.cpp
+++ b/src/crypt/cryptclassevp.cpp
@@ -19,9 +19,9 @@ void CryptClassEvp::encrypt()
EVP_CIPHER_CTX *ctx;
- int len;
+ int len = 0;
- int ciphertext_len;
+ int ciphertext_len = 0;
/* Create and initialise the context */
if (!(ctx = EVP_CIPHER_CTX_new()))
@@ -58,6 +58,7 @@ void CryptClassEvp::encrypt()
ciphertext_len += len;
+ m_cryptText.resize(ciphertext_len);
/* Clean up */
EVP_CIPHER_CTX_free(ctx);
}
@@ -104,7 +105,7 @@ void CryptClassEvp::decrypt()
}
plaintext_len += len;
-
+ m_clearText.resize(plaintext_len);
/* Clean up */
EVP_CIPHER_CTX_free(ctx);
}
diff --git a/src/crypt/cryptengine.cpp b/src/crypt/cryptengine.cpp
index 3e620a6..fcee6f2 100644
--- a/src/crypt/cryptengine.cpp
+++ b/src/crypt/cryptengine.cpp
@@ -114,8 +114,8 @@ void CryptEngine::oncipherSelected_triggered(int cipher)
}
else if( algorithm == "AES" )
{
-// delete m_cryptClass;
-// m_cryptClass = new CryptClassAES;
+ delete m_cryptClass;
+ m_cryptClass = new CryptAes;
qDebug("CryptEngine::oncipherSelected_triggered - AES");
}
else if( algorithm == "RSA" )
diff --git a/src/crypt/cryptengine.h b/src/crypt/cryptengine.h
index df91599..50f91a1 100644
--- a/src/crypt/cryptengine.h
+++ b/src/crypt/cryptengine.h
@@ -9,7 +9,7 @@
#include "cryptclasscaesar.h"
#include "cryptclassrc4.h"
//#include "cryptclasscube.h"
-//#include "cryptclassaes.h"
+#include "cryptaes.h"
//#include "cryptclassrsa.h"
//#include "cryptclassecc.h"