1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
* Copyright (C) 2012 Webdoc SA
*
* This file is part of Open-Sankoré.
*
* Open-Sankoré is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation, version 2,
* with a specific linking exception for the OpenSSL project's
* "OpenSSL" library (or with modified versions of it that use the
* same license as the "OpenSSL" library).
*
* Open-Sankoré 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with Open-Sankoré; if not, see
* <http://www.gnu.org/licenses/>.
*/
#include "UBCryptoUtils.h"
#include <openssl/aes.h>
#include "core/memcheck.h"
UBCryptoUtils* UBCryptoUtils::sInstance(0);
QString UBCryptoUtils::sAESKey("9ecHaspud9uD9ste5erAchehefrup3echej-caje6&thestawacuk=h#F3jet3aF");
QString UBCryptoUtils::sAESSalt("6f0083e0-a90c-11de-ac21-0002a5d5c51b");
UBCryptoUtils* UBCryptoUtils::instance()
{
if(!sInstance)
sInstance = new UBCryptoUtils(UBApplication::staticMemoryCleaner);
return sInstance;
}
void UBCryptoUtils::destroy()
{
if (sInstance)
delete sInstance;
sInstance = NULL;
}
UBCryptoUtils::UBCryptoUtils(QObject * pParent)
: QObject(pParent)
{
aesInit();
}
UBCryptoUtils::~UBCryptoUtils()
{
// TODO UB 4.x aes destroy
}
QString UBCryptoUtils::symetricEncrypt(const QString& clear)
{
QByteArray clearData = clear.toUtf8();
int cipheredLength = clearData.length() + AES_BLOCK_SIZE;
int paddingLength = 0;
unsigned char *ciphertext = (unsigned char *)malloc(cipheredLength);
if(!EVP_EncryptInit_ex(&mAesEncryptContext, NULL, NULL, NULL, NULL))
return QString();
if(!EVP_EncryptUpdate(&mAesEncryptContext, ciphertext, &cipheredLength, (unsigned char *)clearData.data(), clearData.length()))
return QString();
/* update ciphertext with the final remaining bytes */
if(!EVP_EncryptFinal_ex(&mAesEncryptContext, ciphertext + cipheredLength, &paddingLength))
return QString();
QByteArray cipheredData((const char *)ciphertext, cipheredLength + paddingLength);
free(ciphertext);
return QString::fromAscii(cipheredData.toBase64());
}
QString UBCryptoUtils::symetricDecrypt(const QString& encrypted)
{
QByteArray encryptedData = QByteArray::fromBase64(encrypted.toAscii());
int encryptedLength = encryptedData.length();
int paddingLength = 0;
unsigned char *plaintext = (unsigned char *)malloc(encryptedLength);
if(!EVP_DecryptInit_ex(&mAesDecryptContext, NULL, NULL, NULL, NULL))
return QString();
if(!EVP_DecryptUpdate(&mAesDecryptContext, plaintext, &encryptedLength, (const unsigned char *)encryptedData.data(), encryptedData.length()))
return QString();
if(!EVP_DecryptFinal_ex(&mAesDecryptContext, plaintext + encryptedLength, &paddingLength))
return QString();
int len = encryptedLength + paddingLength;
QByteArray clearData((const char *)plaintext, len);
free(plaintext);
return QString::fromUtf8(clearData);;
}
void UBCryptoUtils::aesInit()
{
int i, nrounds = 5;
unsigned char key[32], iv[32];
unsigned char *key_data = (unsigned char *)sAESKey.toAscii().data();
int key_data_len = sAESKey.length();
i = EVP_BytesToKey(EVP_aes_256_cbc(), EVP_sha1(), (unsigned char *)sAESSalt.toAscii().data(), key_data,
key_data_len, nrounds, key, iv);
if (i != 32)
{
qWarning() << QString("Key size is %1 bits - should be 256 bits").arg(i);
return;
}
EVP_CIPHER_CTX_init(&mAesEncryptContext);
EVP_EncryptInit_ex(&mAesEncryptContext, EVP_aes_256_cbc(), NULL, key, iv);
EVP_CIPHER_CTX_init(&mAesDecryptContext);
EVP_DecryptInit_ex(&mAesDecryptContext, EVP_aes_256_cbc(), NULL, key, iv);
}