rev |
line source |
shann@25634
|
1 --- a/src/lib/tools/aesinterface.cpp
|
shann@25634
|
2 +++ b/src/lib/tools/aesinterface.cpp
|
shann@25634
|
3 @@ -39,14 +39,18 @@
|
shann@25634
|
4 : QObject(parent)
|
shann@25634
|
5 , m_ok(false)
|
shann@25634
|
6 {
|
shann@25634
|
7 - EVP_CIPHER_CTX_init(&m_encodeCTX);
|
shann@25634
|
8 - EVP_CIPHER_CTX_init(&m_decodeCTX);
|
shann@25634
|
9 + m_encodeCTX = EVP_CIPHER_CTX_new();
|
shann@25634
|
10 + m_decodeCTX = EVP_CIPHER_CTX_new();
|
shann@25634
|
11 + EVP_CIPHER_CTX_init(m_encodeCTX);
|
shann@25634
|
12 + EVP_CIPHER_CTX_init(m_decodeCTX);
|
shann@25634
|
13 }
|
shann@25634
|
14
|
shann@25634
|
15 AesInterface::~AesInterface()
|
shann@25634
|
16 {
|
shann@25634
|
17 - EVP_CIPHER_CTX_cleanup(&m_encodeCTX);
|
shann@25634
|
18 - EVP_CIPHER_CTX_cleanup(&m_decodeCTX);
|
shann@25634
|
19 + EVP_CIPHER_CTX_cleanup(m_encodeCTX);
|
shann@25634
|
20 + EVP_CIPHER_CTX_cleanup(m_decodeCTX);
|
shann@25634
|
21 + EVP_CIPHER_CTX_free(m_encodeCTX);
|
shann@25634
|
22 + EVP_CIPHER_CTX_free(m_decodeCTX);
|
shann@25634
|
23 }
|
shann@25634
|
24
|
shann@25634
|
25 bool AesInterface::isOk()
|
shann@25634
|
26 @@ -78,10 +82,10 @@
|
shann@25634
|
27 int result = 0;
|
shann@25634
|
28 if (evpMode == EVP_PKEY_MO_ENCRYPT) {
|
shann@25634
|
29 m_iVector = createRandomData(EVP_MAX_IV_LENGTH);
|
shann@25634
|
30 - result = EVP_EncryptInit_ex(&m_encodeCTX, EVP_aes_256_cbc(), NULL, key, (uchar*)m_iVector.constData());
|
shann@25634
|
31 + result = EVP_EncryptInit_ex(m_encodeCTX, EVP_aes_256_cbc(), NULL, key, (uchar*)m_iVector.constData());
|
shann@25634
|
32 }
|
shann@25634
|
33 else if (evpMode == EVP_PKEY_MO_DECRYPT) {
|
shann@25634
|
34 - result = EVP_DecryptInit_ex(&m_decodeCTX, EVP_aes_256_cbc(), NULL, key, (uchar*)iVector.constData());
|
shann@25634
|
35 + result = EVP_DecryptInit_ex(m_decodeCTX, EVP_aes_256_cbc(), NULL, key, (uchar*)iVector.constData());
|
shann@25634
|
36 }
|
shann@25634
|
37
|
shann@25634
|
38 if (result == 0) {
|
shann@25634
|
39 @@ -106,14 +110,14 @@
|
shann@25634
|
40 uchar* ciphertext = (uchar*)malloc(cipherlength);
|
shann@25634
|
41
|
shann@25634
|
42 // allows reusing of 'm_encodeCTX' for multiple encryption cycles
|
shann@25634
|
43 - EVP_EncryptInit_ex(&m_encodeCTX, NULL, NULL, NULL, NULL);
|
shann@25634
|
44 + EVP_EncryptInit_ex(m_encodeCTX, NULL, NULL, NULL, NULL);
|
shann@25634
|
45
|
shann@25634
|
46 // update ciphertext, c_len is filled with the length of ciphertext generated,
|
shann@25634
|
47 // dataLength is the size of plaintext in bytes
|
shann@25634
|
48 - EVP_EncryptUpdate(&m_encodeCTX, ciphertext, &cipherlength, (uchar*)plainData.data(), dataLength);
|
shann@25634
|
49 + EVP_EncryptUpdate(m_encodeCTX, ciphertext, &cipherlength, (uchar*)plainData.data(), dataLength);
|
shann@25634
|
50
|
shann@25634
|
51 // update ciphertext with the final remaining bytes
|
shann@25634
|
52 - EVP_EncryptFinal_ex(&m_encodeCTX, ciphertext + cipherlength, &finalLength);
|
shann@25634
|
53 + EVP_EncryptFinal_ex(m_encodeCTX, ciphertext + cipherlength, &finalLength);
|
shann@25634
|
54
|
shann@25634
|
55 dataLength = cipherlength + finalLength;
|
shann@25634
|
56 QByteArray out((char*)ciphertext, dataLength);
|
shann@25634
|
57 @@ -163,9 +167,9 @@
|
shann@25634
|
58 // because we have padding ON, we must allocate an extra cipher block size of memory
|
shann@25634
|
59 uchar* plainText = (uchar*)malloc(plainTextLength + AES_BLOCK_SIZE);
|
shann@25634
|
60
|
shann@25634
|
61 - EVP_DecryptInit_ex(&m_decodeCTX, NULL, NULL, NULL, NULL);
|
shann@25634
|
62 - EVP_DecryptUpdate(&m_decodeCTX, plainText, &plainTextLength, cipherText, cipherLength);
|
shann@25634
|
63 - int success = EVP_DecryptFinal_ex(&m_decodeCTX, plainText + plainTextLength, &finalLength);
|
shann@25634
|
64 + EVP_DecryptInit_ex(m_decodeCTX, NULL, NULL, NULL, NULL);
|
shann@25634
|
65 + EVP_DecryptUpdate(m_decodeCTX, plainText, &plainTextLength, cipherText, cipherLength);
|
shann@25634
|
66 + int success = EVP_DecryptFinal_ex(m_decodeCTX, plainText + plainTextLength, &finalLength);
|
shann@25634
|
67
|
shann@25634
|
68 cipherLength = plainTextLength + finalLength;
|
shann@25634
|
69
|
shann@25634
|
70 --- a/src/lib/tools/aesinterface.h
|
shann@25634
|
71 +++ b/src/lib/tools/aesinterface.h
|
shann@25634
|
72 @@ -50,8 +50,8 @@
|
shann@25634
|
73 private:
|
shann@25634
|
74 bool init(int evpMode, const QByteArray &password, const QByteArray &iVector = QByteArray());
|
shann@25634
|
75
|
shann@25634
|
76 - EVP_CIPHER_CTX m_encodeCTX;
|
shann@25634
|
77 - EVP_CIPHER_CTX m_decodeCTX;
|
shann@25634
|
78 + EVP_CIPHER_CTX *m_encodeCTX;
|
shann@25634
|
79 + EVP_CIPHER_CTX *m_decodeCTX;
|
shann@25634
|
80
|
shann@25634
|
81 bool m_ok;
|
shann@25634
|
82 QByteArray m_iVector;
|