diff options
| author | Stefan Suhren <suhren.stefan@fh-swf.de> | 2015-05-27 15:07:29 +0200 |
|---|---|---|
| committer | Stefan Suhren <suhren.stefan@fh-swf.de> | 2015-05-27 15:07:29 +0200 |
| commit | 672ebc3868997c44b83f7afe59e9b4d876135247 (patch) | |
| tree | f2a7a48dc1ff6c85e417767bfd237be331897423 /src/crypt/cryptaes.cpp | |
| parent | 220702c05e5c67817e5ba45765fd75ead8e92bb3 (diff) | |
| download | IT-Sicherheit-672ebc3868997c44b83f7afe59e9b4d876135247.tar.gz IT-Sicherheit-672ebc3868997c44b83f7afe59e9b4d876135247.zip | |
Add an AES implementation with openssl evp
Diffstat (limited to 'src/crypt/cryptaes.cpp')
| -rw-r--r-- | src/crypt/cryptaes.cpp | 111 |
1 files changed, 111 insertions, 0 deletions
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(); +} |
