2011-07-02 14:52:16 +00:00
|
|
|
/*****************************************************************************
|
|
|
|
|
* PokerTH - The open source texas holdem engine *
|
2012-12-25 15:06:23 +01:00
|
|
|
* Copyright (C) 2006-2012 Felix Hammer, Florian Thauer, Lothar May *
|
2011-07-02 14:52:16 +00:00
|
|
|
* *
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify *
|
|
|
|
|
* it under the terms of the GNU Affero General Public License as *
|
|
|
|
|
* published by the Free Software Foundation, either version 3 of the *
|
|
|
|
|
* License, or (at your option) any later version. *
|
|
|
|
|
* *
|
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
|
* GNU Affero General Public License for more details. *
|
|
|
|
|
* *
|
|
|
|
|
* You should have received a copy of the GNU Affero General Public License *
|
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
|
2012-12-25 15:06:23 +01:00
|
|
|
* *
|
|
|
|
|
* *
|
|
|
|
|
* Additional permission under GNU AGPL version 3 section 7 *
|
|
|
|
|
* *
|
|
|
|
|
* If you modify this program, or any covered work, by linking or *
|
|
|
|
|
* combining it with the OpenSSL project's OpenSSL library (or a *
|
|
|
|
|
* modified version of that library), containing parts covered by the *
|
|
|
|
|
* terms of the OpenSSL or SSLeay licenses, the authors of PokerTH *
|
|
|
|
|
* (Felix Hammer, Florian Thauer, Lothar May) grant you additional *
|
|
|
|
|
* permission to convey the resulting work. *
|
|
|
|
|
* Corresponding Source for a non-source form of such a combination *
|
|
|
|
|
* shall include the source code for the parts of OpenSSL used as well *
|
|
|
|
|
* as that of the covered work. *
|
2011-07-02 14:52:16 +00:00
|
|
|
*****************************************************************************/
|
2007-11-16 15:24:25 +00:00
|
|
|
|
2010-09-04 11:56:40 +00:00
|
|
|
#include <core/crypthelper.h>
|
2007-11-16 15:24:25 +00:00
|
|
|
|
2008-04-21 22:27:15 +00:00
|
|
|
#include <core/openssl_wrapper.h>
|
2007-11-16 15:24:25 +00:00
|
|
|
#include <cstring>
|
|
|
|
|
#include <cstdio>
|
|
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
|
|
// Helper function.
|
|
|
|
|
static int
|
|
|
|
|
fromHex(int ch)
|
|
|
|
|
{
|
|
|
|
|
int retVal = -1;
|
|
|
|
|
|
|
|
|
|
if (ch >= '0' && ch <= '9')
|
|
|
|
|
retVal = ch - '0';
|
|
|
|
|
else if (ch >= 'a' && ch <= 'f')
|
|
|
|
|
retVal = ch - 'a' + 10;
|
|
|
|
|
else if (ch >= 'A' && ch <= 'F')
|
|
|
|
|
retVal = ch - 'A' + 10;
|
|
|
|
|
|
|
|
|
|
return retVal;
|
|
|
|
|
}
|
|
|
|
|
|
2009-12-28 12:16:52 +00:00
|
|
|
HashBuf::~HashBuf()
|
2007-11-16 15:24:25 +00:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string
|
2009-12-28 12:16:52 +00:00
|
|
|
HashBuf::ToString() const
|
2007-11-16 15:24:25 +00:00
|
|
|
{
|
|
|
|
|
// Create a hex-based string from the MD5 data.
|
|
|
|
|
string retValue;
|
|
|
|
|
char tmpBuf[2 + 1];
|
|
|
|
|
tmpBuf[sizeof(tmpBuf) - 1] = 0;
|
2009-12-28 12:16:52 +00:00
|
|
|
const unsigned char *tmpData = GetData();
|
2011-02-11 20:02:08 +00:00
|
|
|
for (int i = 0; i < GetDataSize(); i++) {
|
2009-12-28 12:16:52 +00:00
|
|
|
sprintf(tmpBuf, "%02x", tmpData[i]);
|
2007-11-16 15:24:25 +00:00
|
|
|
retValue += tmpBuf;
|
|
|
|
|
}
|
|
|
|
|
return retValue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool
|
2009-12-28 12:16:52 +00:00
|
|
|
HashBuf::FromString(const std::string &text)
|
2007-11-16 15:24:25 +00:00
|
|
|
{
|
|
|
|
|
// Convert hex-based string to MD5 data.
|
|
|
|
|
bool retVal = false;
|
2009-12-28 12:16:52 +00:00
|
|
|
int tmpSize = GetDataSize();
|
2011-02-11 20:02:08 +00:00
|
|
|
if (text.size() == 2 * (unsigned)tmpSize) {
|
2009-12-28 12:16:52 +00:00
|
|
|
unsigned char *tmpData = GetData();
|
2007-11-16 15:24:25 +00:00
|
|
|
const char *t = text.c_str();
|
|
|
|
|
int i = 0;
|
2009-12-28 12:16:52 +00:00
|
|
|
for (; i < tmpSize; i++) {
|
2007-11-16 15:24:25 +00:00
|
|
|
int part1 = fromHex(*t++);
|
|
|
|
|
if (part1 == -1)
|
|
|
|
|
break;
|
|
|
|
|
int part2 = fromHex(*t++);
|
|
|
|
|
if (part2 == -1)
|
|
|
|
|
break;
|
|
|
|
|
*tmpData++ = (part1<<4) + part2;
|
|
|
|
|
}
|
2009-12-28 12:16:52 +00:00
|
|
|
retVal = i == tmpSize;
|
2007-11-16 15:24:25 +00:00
|
|
|
}
|
|
|
|
|
return retVal;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool
|
2009-12-28 12:16:52 +00:00
|
|
|
HashBuf::IsZero() const
|
2007-11-16 15:24:25 +00:00
|
|
|
{
|
2009-12-28 12:16:52 +00:00
|
|
|
int dataSize = GetDataSize();
|
|
|
|
|
const unsigned char *tmpData = GetData();
|
|
|
|
|
int i;
|
2011-02-11 20:02:08 +00:00
|
|
|
for (i = 0; i < dataSize; i++) {
|
2009-12-28 12:16:52 +00:00
|
|
|
if (tmpData[i] != 0)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return i == dataSize;
|
2007-11-16 15:24:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool
|
2009-12-28 12:16:52 +00:00
|
|
|
HashBuf::operator==(const HashBuf &other) const
|
2007-11-16 15:24:25 +00:00
|
|
|
{
|
2009-12-28 12:16:52 +00:00
|
|
|
return GetDataSize() == other.GetDataSize() && memcmp(GetData(), other.GetData(), GetDataSize()) == 0;
|
2007-11-16 15:24:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool
|
2009-12-28 12:16:52 +00:00
|
|
|
HashBuf::operator<(const HashBuf &other) const
|
2007-11-16 15:24:25 +00:00
|
|
|
{
|
2009-12-28 12:16:52 +00:00
|
|
|
int smallestDataSize = GetDataSize() < other.GetDataSize() ? GetDataSize() : other.GetDataSize();
|
|
|
|
|
return memcmp(GetData(), other.GetData(), smallestDataSize) < 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
MD5Buf::MD5Buf()
|
|
|
|
|
{
|
|
|
|
|
memset(m_data, 0, sizeof(m_data));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unsigned char *
|
|
|
|
|
MD5Buf::GetData()
|
|
|
|
|
{
|
|
|
|
|
return m_data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const unsigned char *
|
|
|
|
|
MD5Buf::GetData() const
|
|
|
|
|
{
|
|
|
|
|
return m_data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
MD5Buf::GetDataSize() const
|
|
|
|
|
{
|
|
|
|
|
return sizeof(m_data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SHA1Buf::SHA1Buf()
|
|
|
|
|
{
|
|
|
|
|
memset(m_data, 0, sizeof(m_data));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unsigned char *
|
|
|
|
|
SHA1Buf::GetData()
|
|
|
|
|
{
|
|
|
|
|
return m_data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const unsigned char *
|
|
|
|
|
SHA1Buf::GetData() const
|
|
|
|
|
{
|
|
|
|
|
return m_data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
SHA1Buf::GetDataSize() const
|
|
|
|
|
{
|
|
|
|
|
return sizeof(m_data);
|
2007-11-16 15:24:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
|
CryptHelper::MD5Sum(const std::string &fileName, MD5Buf &buf)
|
|
|
|
|
{
|
|
|
|
|
bool retVal = false;
|
|
|
|
|
FILE *file = fopen(fileName.c_str(), "rb");
|
|
|
|
|
|
2011-02-11 20:02:08 +00:00
|
|
|
if (file) {
|
2007-11-16 15:24:25 +00:00
|
|
|
// Calculate MD5 sum of file.
|
2011-03-08 18:42:57 +00:00
|
|
|
unsigned char *readBuf = new unsigned char[8192];
|
2011-04-10 10:04:42 +00:00
|
|
|
size_t numBytes;
|
2007-11-16 15:24:25 +00:00
|
|
|
|
2011-03-08 18:42:57 +00:00
|
|
|
#ifdef HAVE_OPENSSL
|
|
|
|
|
MD5_CTX context;
|
2007-11-16 15:24:25 +00:00
|
|
|
MD5_Init(&context);
|
2011-03-08 18:42:57 +00:00
|
|
|
while ((numBytes = fread(readBuf, 1, sizeof(readBuf), file)) > 0) {
|
2007-11-16 15:24:25 +00:00
|
|
|
MD5_Update(&context, readBuf, numBytes);
|
2011-03-08 18:42:57 +00:00
|
|
|
}
|
2009-12-28 12:16:52 +00:00
|
|
|
MD5_Final(buf.GetData(), &context);
|
2011-03-08 18:42:57 +00:00
|
|
|
#else
|
|
|
|
|
gcry_md_hd_t hash;
|
|
|
|
|
gcry_md_open(&hash, GCRY_MD_MD5, 0);
|
|
|
|
|
while ((numBytes = fread(readBuf, 1, sizeof(readBuf), file)) > 0) {
|
|
|
|
|
gcry_md_write(hash, readBuf, numBytes);
|
|
|
|
|
}
|
|
|
|
|
memcpy(buf.GetData(), gcry_md_read(hash, GCRY_MD_MD5), MD5_DATA_SIZE);
|
|
|
|
|
gcry_md_close(hash);
|
|
|
|
|
#endif
|
|
|
|
|
|
2007-11-16 15:24:25 +00:00
|
|
|
retVal = ferror(file) == 0;
|
|
|
|
|
|
2011-03-08 18:42:57 +00:00
|
|
|
delete[] readBuf;
|
2007-11-16 15:24:25 +00:00
|
|
|
fclose(file);
|
|
|
|
|
}
|
|
|
|
|
return retVal;
|
|
|
|
|
}
|
|
|
|
|
|
2009-12-28 12:16:52 +00:00
|
|
|
bool
|
2010-07-14 07:26:21 +00:00
|
|
|
CryptHelper::SHA1Hash(const unsigned char *data, unsigned dataSize, SHA1Buf &buf)
|
2009-12-28 12:16:52 +00:00
|
|
|
{
|
2009-12-28 17:29:21 +00:00
|
|
|
bool retVal;
|
2009-12-28 12:16:52 +00:00
|
|
|
#ifdef HAVE_OPENSSL
|
2009-12-28 17:29:21 +00:00
|
|
|
retVal = SHA1(data, dataSize, buf.GetData()) != NULL;
|
2009-12-28 12:16:52 +00:00
|
|
|
#else
|
2009-12-28 17:29:21 +00:00
|
|
|
// We use the shortcut since we assume that the system supports SHA1.
|
|
|
|
|
// This call has no error return value.
|
|
|
|
|
gcry_md_hash_buffer(GCRY_MD_SHA1, buf.GetData(), data, dataSize);
|
|
|
|
|
retVal = true;
|
|
|
|
|
#endif
|
|
|
|
|
return retVal;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool
|
2010-07-14 07:26:21 +00:00
|
|
|
CryptHelper::HMACSha1(const unsigned char *keyData, unsigned keySize, const unsigned char *plainData, unsigned plainSize, SHA1Buf &buf)
|
2009-12-28 17:29:21 +00:00
|
|
|
{
|
|
|
|
|
bool retVal;
|
|
|
|
|
#ifdef HAVE_OPENSSL
|
|
|
|
|
unsigned hashLen = 0;
|
|
|
|
|
HMAC(EVP_sha1(), keyData, keySize, plainData, plainSize, buf.GetData(), &hashLen);
|
|
|
|
|
retVal = hashLen == (unsigned)buf.GetDataSize();
|
|
|
|
|
#else
|
|
|
|
|
retVal = false;
|
|
|
|
|
gcry_md_hd_t hd;
|
|
|
|
|
gcry_error_t err = gcry_md_open(&hd, GCRY_MD_SHA1, GCRY_MD_FLAG_HMAC);
|
2011-02-11 20:02:08 +00:00
|
|
|
if (!err) {
|
2009-12-28 17:29:21 +00:00
|
|
|
err = gcry_md_setkey(hd, keyData, keySize);
|
2011-02-11 20:02:08 +00:00
|
|
|
if (!err) {
|
2009-12-28 17:29:21 +00:00
|
|
|
gcry_md_write(hd, plainData, plainSize);
|
|
|
|
|
unsigned char *hash = gcry_md_read(hd, 0);
|
2011-02-11 20:02:08 +00:00
|
|
|
if (hash) {
|
2009-12-28 17:29:21 +00:00
|
|
|
memcpy(buf.GetData(), hash, buf.GetDataSize());
|
|
|
|
|
retVal = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
gcry_md_close(hd);
|
|
|
|
|
}
|
2009-12-28 12:16:52 +00:00
|
|
|
#endif
|
|
|
|
|
return retVal;
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-29 15:39:46 +00:00
|
|
|
void
|
|
|
|
|
CryptHelper::BytesToKey(const unsigned char *keyData, unsigned keySize, unsigned char *key, unsigned char *iv)
|
|
|
|
|
{
|
|
|
|
|
// The key/iv derivation is kind of like EVP_BytesToKey of OpenSSL with count 2 and no salt.
|
|
|
|
|
// EVP_BytesToKey is not used because there is nothing like it in GnuTLS or gcrypt.
|
|
|
|
|
SHA1Buf tmpBuf1, tmpBuf2, keyBuf1, keyBuf2;
|
|
|
|
|
// First 20 bytes
|
|
|
|
|
CryptHelper::SHA1Hash(keyData, keySize, tmpBuf1);
|
|
|
|
|
CryptHelper::SHA1Hash(tmpBuf1.GetData(), tmpBuf1.GetDataSize(), keyBuf1);
|
|
|
|
|
// Second 20 bytes (we only need a total of 32 bytes, but anyway).
|
|
|
|
|
unsigned tmpKeySize = keySize + keyBuf1.GetDataSize();
|
|
|
|
|
unsigned char *tmpKeyData = new unsigned char[tmpKeySize];
|
|
|
|
|
// Concatenate our first hash and the key data.
|
|
|
|
|
memcpy(tmpKeyData, keyBuf1.GetData(), keyBuf1.GetDataSize());
|
|
|
|
|
memcpy(tmpKeyData + keyBuf1.GetDataSize(), keyData, keySize);
|
|
|
|
|
CryptHelper::SHA1Hash(tmpKeyData, tmpKeySize, tmpBuf2);
|
|
|
|
|
CryptHelper::SHA1Hash(tmpBuf2.GetData(), tmpBuf2.GetDataSize(), keyBuf2);
|
|
|
|
|
delete[] tmpKeyData;
|
|
|
|
|
// Copy the hashes to key/iv.
|
|
|
|
|
memcpy(key, keyBuf1.GetData(), AES_BLOCK_SIZE);
|
|
|
|
|
unsigned tmpivBytes = keyBuf1.GetDataSize() - AES_BLOCK_SIZE;
|
|
|
|
|
memcpy(iv, keyBuf1.GetData() + AES_BLOCK_SIZE, tmpivBytes);
|
|
|
|
|
memcpy(iv + tmpivBytes, keyBuf2.GetData(), AES_BLOCK_SIZE - tmpivBytes);
|
|
|
|
|
}
|
|
|
|
|
|
2009-12-28 12:16:52 +00:00
|
|
|
bool
|
2010-08-29 15:39:46 +00:00
|
|
|
CryptHelper::AES128Encrypt(const unsigned char *keyData, unsigned keySize, const string &plainStr, std::vector<unsigned char> &outCipher)
|
2009-12-28 12:16:52 +00:00
|
|
|
{
|
|
|
|
|
bool retVal = false;
|
2010-08-29 15:39:46 +00:00
|
|
|
unsigned plainSize = static_cast<unsigned>(plainStr.size());
|
2011-02-11 20:02:08 +00:00
|
|
|
if (keySize && plainSize) {
|
2010-08-29 15:39:46 +00:00
|
|
|
unsigned char key[AES_BLOCK_SIZE];
|
|
|
|
|
unsigned char iv[AES_BLOCK_SIZE];
|
|
|
|
|
BytesToKey(keyData, keySize, key, iv);
|
|
|
|
|
// Add padding to plain data.
|
|
|
|
|
unsigned paddedPlainSize = ADD_PADDING(plainSize);
|
2012-11-26 22:47:40 +01:00
|
|
|
unsigned char *paddedPlainStr = (unsigned char *)calloc(paddedPlainSize, 1);
|
2010-08-29 15:39:46 +00:00
|
|
|
memcpy(paddedPlainStr, plainStr.c_str(), plainSize);
|
2009-12-29 18:23:45 +00:00
|
|
|
// Perform the encryption.
|
2010-08-29 15:39:46 +00:00
|
|
|
int cipherSize = paddedPlainSize;
|
2010-07-14 07:26:21 +00:00
|
|
|
outCipher.resize(cipherSize);
|
|
|
|
|
|
2009-12-28 17:29:21 +00:00
|
|
|
#ifdef HAVE_OPENSSL
|
2019-03-21 13:16:18 +01:00
|
|
|
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
|
|
|
|
|
EVP_CIPHER_CTX *encryptCtx = EVP_CIPHER_CTX_new();
|
|
|
|
|
#else
|
|
|
|
|
EVP_CIPHER_CTX _encryptCtx;
|
|
|
|
|
EVP_CIPHER_CTX *encryptCtx;
|
|
|
|
|
encryptCtx = &_encryptCtx;
|
|
|
|
|
#endif // OPENSSL_VERSION_NUMBER >= 0x10100000L
|
|
|
|
|
|
|
|
|
|
EVP_CIPHER_CTX_init(encryptCtx);
|
2010-08-29 15:39:46 +00:00
|
|
|
int outCipherSize = cipherSize;
|
|
|
|
|
|
2019-03-21 13:16:18 +01:00
|
|
|
int success = EVP_EncryptInit(encryptCtx, EVP_aes_128_cbc(), key, iv);
|
|
|
|
|
EVP_CIPHER_CTX_set_padding(encryptCtx, 0);
|
2011-02-11 20:02:08 +00:00
|
|
|
if (success) {
|
2019-03-21 13:16:18 +01:00
|
|
|
success = EVP_EncryptUpdate(encryptCtx, &outCipher[0], &outCipherSize, paddedPlainStr, paddedPlainSize);
|
2010-08-29 15:39:46 +00:00
|
|
|
|
2011-02-11 20:02:08 +00:00
|
|
|
if (success && outCipherSize) {
|
2010-08-29 15:39:46 +00:00
|
|
|
// Since padding is off, this will not modify the cipher. However, parameters need to be set.
|
2019-03-21 13:16:18 +01:00
|
|
|
EVP_EncryptFinal(encryptCtx, &outCipher[0], &outCipherSize);
|
2010-08-29 15:39:46 +00:00
|
|
|
retVal = true;
|
|
|
|
|
}
|
2011-02-11 20:02:08 +00:00
|
|
|
} else
|
2009-12-29 18:23:45 +00:00
|
|
|
outCipher.clear();
|
2019-03-21 13:16:18 +01:00
|
|
|
|
|
|
|
|
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
|
|
|
|
|
EVP_CIPHER_CTX_free(encryptCtx);
|
|
|
|
|
#endif // OPENSSL_VERSION_NUMBER >= 0x10100000L
|
2009-12-28 17:29:21 +00:00
|
|
|
#else
|
2010-07-14 07:26:21 +00:00
|
|
|
gcry_cipher_hd_t hd;
|
2010-08-29 15:39:46 +00:00
|
|
|
gcry_error_t err = gcry_cipher_open(&hd, GCRY_CIPHER_AES128, GCRY_CIPHER_MODE_CBC, 0);
|
2011-02-11 20:02:08 +00:00
|
|
|
if (!err) {
|
2010-07-14 07:26:21 +00:00
|
|
|
gcry_cipher_setkey(hd, key, sizeof(key));
|
|
|
|
|
gcry_cipher_setiv(hd, iv, sizeof(iv));
|
2010-08-29 15:39:46 +00:00
|
|
|
err = gcry_cipher_encrypt(hd, &outCipher[0], cipherSize, paddedPlainStr, paddedPlainSize);
|
2010-07-14 07:26:21 +00:00
|
|
|
if (!err)
|
|
|
|
|
retVal = true;
|
2010-08-29 15:39:46 +00:00
|
|
|
else
|
|
|
|
|
outCipher.clear();
|
2011-02-11 20:02:08 +00:00
|
|
|
} else
|
2010-08-29 15:39:46 +00:00
|
|
|
outCipher.clear();
|
|
|
|
|
|
|
|
|
|
gcry_cipher_close(hd);
|
2009-12-28 17:29:21 +00:00
|
|
|
#endif
|
2012-11-26 22:47:40 +01:00
|
|
|
free(paddedPlainStr);
|
2009-12-28 17:29:21 +00:00
|
|
|
}
|
2009-12-28 12:16:52 +00:00
|
|
|
return retVal;
|
|
|
|
|
}
|
2010-08-29 15:39:46 +00:00
|
|
|
|
|
|
|
|
bool
|
|
|
|
|
CryptHelper::AES128Decrypt(const unsigned char *keyData, unsigned keySize, const unsigned char *cipher, unsigned cipherSize, string &outPlain)
|
|
|
|
|
{
|
|
|
|
|
bool retVal = false;
|
2011-02-11 20:02:08 +00:00
|
|
|
if (keySize && cipherSize) {
|
2010-08-29 15:39:46 +00:00
|
|
|
unsigned char key[AES_BLOCK_SIZE];
|
|
|
|
|
unsigned char iv[AES_BLOCK_SIZE];
|
|
|
|
|
BytesToKey(keyData, keySize, key, iv);
|
|
|
|
|
outPlain.resize(cipherSize);
|
|
|
|
|
#ifdef HAVE_OPENSSL
|
2019-03-21 13:16:18 +01:00
|
|
|
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
|
|
|
|
|
EVP_CIPHER_CTX *decryptCtx = EVP_CIPHER_CTX_new();
|
|
|
|
|
#else
|
|
|
|
|
EVP_CIPHER_CTX _decryptCtx;
|
|
|
|
|
EVP_CIPHER_CTX *decryptCtx;
|
|
|
|
|
decryptCtx = &_decryptCtx;
|
|
|
|
|
#endif // OPENSSL_VERSION_NUMBER >= 0x10100000L
|
|
|
|
|
|
|
|
|
|
EVP_CIPHER_CTX_init(decryptCtx);
|
2010-08-29 15:39:46 +00:00
|
|
|
int outPlainSize = cipherSize;
|
|
|
|
|
|
2019-03-21 13:16:18 +01:00
|
|
|
int success = EVP_DecryptInit(decryptCtx, EVP_aes_128_cbc(), key, iv);
|
|
|
|
|
EVP_CIPHER_CTX_set_padding(decryptCtx, 0);
|
2011-02-11 20:02:08 +00:00
|
|
|
if (success) {
|
2019-03-21 13:16:18 +01:00
|
|
|
success = EVP_DecryptUpdate(decryptCtx, (unsigned char *)&outPlain[0], &outPlainSize, cipher, cipherSize);
|
2010-08-29 15:39:46 +00:00
|
|
|
|
2011-02-11 20:02:08 +00:00
|
|
|
if (success && outPlainSize) {
|
2010-08-29 15:39:46 +00:00
|
|
|
// Since padding is off, this will not modify the plain text. However, parameters need to be set.
|
2019-03-21 13:16:18 +01:00
|
|
|
EVP_DecryptFinal(decryptCtx, (unsigned char *)outPlain.c_str(), &outPlainSize);
|
2010-08-29 15:39:46 +00:00
|
|
|
retVal = true;
|
|
|
|
|
}
|
2011-02-11 20:02:08 +00:00
|
|
|
} else
|
2010-08-29 15:39:46 +00:00
|
|
|
outPlain.clear();
|
2019-03-21 13:16:18 +01:00
|
|
|
|
|
|
|
|
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
|
|
|
|
|
EVP_CIPHER_CTX_free(decryptCtx);
|
|
|
|
|
#endif // OPENSSL_VERSION_NUMBER >= 0x10100000L
|
2010-08-29 15:39:46 +00:00
|
|
|
#else
|
|
|
|
|
gcry_cipher_hd_t hd;
|
|
|
|
|
gcry_error_t err = gcry_cipher_open(&hd, GCRY_CIPHER_AES128, GCRY_CIPHER_MODE_CBC, 0);
|
2011-02-11 20:02:08 +00:00
|
|
|
if (!err) {
|
2010-08-29 15:39:46 +00:00
|
|
|
gcry_cipher_setkey(hd, key, sizeof(key));
|
|
|
|
|
gcry_cipher_setiv(hd, iv, sizeof(iv));
|
|
|
|
|
err = gcry_cipher_decrypt(hd, &outPlain[0], outPlain.size(), cipher, cipherSize);
|
|
|
|
|
if (!err)
|
|
|
|
|
retVal = true;
|
|
|
|
|
else
|
|
|
|
|
outPlain.clear();
|
2011-02-11 20:02:08 +00:00
|
|
|
} else
|
2010-08-29 15:39:46 +00:00
|
|
|
outPlain.clear();
|
|
|
|
|
|
|
|
|
|
gcry_cipher_close(hd);
|
|
|
|
|
#endif
|
|
|
|
|
// Remove trailing zeroes (padding).
|
2011-02-11 20:02:08 +00:00
|
|
|
if (!outPlain.empty()) {
|
2010-08-29 15:39:46 +00:00
|
|
|
size_t pos = outPlain.find_first_of('\0');
|
|
|
|
|
if (pos != string::npos)
|
|
|
|
|
outPlain = outPlain.substr(0, pos);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return retVal;
|
|
|
|
|
}
|
|
|
|
|
|