From 672ebc3868997c44b83f7afe59e9b4d876135247 Mon Sep 17 00:00:00 2001 From: Stefan Suhren Date: Wed, 27 May 2015 15:07:29 +0200 Subject: Add an AES implementation with openssl evp --- src/crypt/cryptaes.cpp | 111 +++++++++++++++++++++++++++++++++++++++++++ src/crypt/cryptaes.h | 21 ++++++++ src/crypt/cryptclassbase.cpp | 1 + src/crypt/cryptclassevp.cpp | 7 +-- src/crypt/cryptengine.cpp | 4 +- src/crypt/cryptengine.h | 2 +- 6 files changed, 140 insertions(+), 6 deletions(-) create mode 100644 src/crypt/cryptaes.cpp create mode 100644 src/crypt/cryptaes.h (limited to 'src') 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 + +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" -- cgit v1.2.3-70-g09d2