-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcryptoengine.cpp
More file actions
171 lines (141 loc) · 5.97 KB
/
Copy pathcryptoengine.cpp
File metadata and controls
171 lines (141 loc) · 5.97 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include "cryptoengine.h"
#include <openssl/evp.h>
#include <openssl/rand.h>
QByteArray CryptoEngine::randomBytes(int size) {
QByteArray buf(size, '\0');
RAND_bytes(reinterpret_cast<unsigned char *>(buf.data()), size);
return buf;
}
QByteArray CryptoEngine::deriveKey(const QString &passphrase, const QByteArray &salt, int keyLen) {
QByteArray key(keyLen, '\0');
QByteArray pass = passphrase.toUtf8();
PKCS5_PBKDF2_HMAC(
pass.constData(), pass.size(),
reinterpret_cast<const unsigned char *>(salt.constData()), salt.size(),
PBKDF2_ITERATIONS, EVP_sha256(),
keyLen, reinterpret_cast<unsigned char *>(key.data()));
return key;
}
static const EVP_CIPHER *cipherFor(CryptoEngine::Algorithm algo, int &ivSize, int &keyLen, bool &aead) {
switch (algo) {
case CryptoEngine::AES_256_GCM:
ivSize = 12; keyLen = 32; aead = true;
return EVP_aes_256_gcm();
case CryptoEngine::AES_128_GCM:
ivSize = 12; keyLen = 16; aead = true;
return EVP_aes_128_gcm();
case CryptoEngine::AES_256_CBC:
ivSize = 16; keyLen = 32; aead = false;
return EVP_aes_256_cbc();
case CryptoEngine::CHACHA20_POLY1305:
ivSize = 12; keyLen = 32; aead = true;
return EVP_chacha20_poly1305();
}
ivSize = 12; keyLen = 32; aead = true;
return EVP_aes_256_gcm();
}
CryptoEngine::Result CryptoEngine::encrypt(const QByteArray &plaintext, const QString &passphrase, Algorithm algo) {
if (passphrase.isEmpty())
return {false, {}, "Passphrase cannot be empty"};
int ivSize = 0, keyLen = 0;
bool aead = false;
const EVP_CIPHER *cipher = cipherFor(algo, ivSize, keyLen, aead);
QByteArray salt = randomBytes(SALT_SIZE);
QByteArray iv = randomBytes(ivSize);
QByteArray key = deriveKey(passphrase, salt, keyLen);
EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
if (!ctx)
return {false, {}, "Failed to create cipher context"};
int rc = EVP_EncryptInit_ex(ctx, cipher, nullptr,
reinterpret_cast<const unsigned char *>(key.constData()),
reinterpret_cast<const unsigned char *>(iv.constData()));
if (rc != 1) { EVP_CIPHER_CTX_free(ctx); return {false, {}, "Encrypt init failed"}; }
QByteArray out(plaintext.size() + EVP_CIPHER_block_size(cipher), '\0');
int len = 0, total = 0;
rc = EVP_EncryptUpdate(ctx,
reinterpret_cast<unsigned char *>(out.data()), &len,
reinterpret_cast<const unsigned char *>(plaintext.constData()), plaintext.size());
if (rc != 1) { EVP_CIPHER_CTX_free(ctx); return {false, {}, "Encrypt update failed"}; }
total = len;
rc = EVP_EncryptFinal_ex(ctx, reinterpret_cast<unsigned char *>(out.data()) + total, &len);
if (rc != 1) { EVP_CIPHER_CTX_free(ctx); return {false, {}, "Encrypt final failed"}; }
total += len;
out.resize(total);
QByteArray tag;
if (aead) {
tag.resize(TAG_SIZE);
EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, TAG_SIZE, tag.data());
}
EVP_CIPHER_CTX_free(ctx);
// Format: salt || iv || [tag] || ciphertext
QByteArray packed;
packed.reserve(SALT_SIZE + ivSize + (aead ? TAG_SIZE : 0) + out.size());
packed.append(salt);
packed.append(iv);
if (aead) packed.append(tag);
packed.append(out);
return {true, packed.toBase64(), {}};
}
CryptoEngine::Result CryptoEngine::decrypt(const QByteArray &ciphertext_b64, const QString &passphrase, Algorithm algo) {
if (passphrase.isEmpty())
return {false, {}, "Passphrase cannot be empty"};
QByteArray packed = QByteArray::fromBase64(ciphertext_b64);
int ivSize = 0, keyLen = 0;
bool aead = false;
const EVP_CIPHER *cipher = cipherFor(algo, ivSize, keyLen, aead);
int hdr = SALT_SIZE + ivSize + (aead ? TAG_SIZE : 0);
if (packed.size() < hdr)
return {false, {}, "Invalid ciphertext: data too short"};
int off = 0;
QByteArray salt = packed.mid(off, SALT_SIZE); off += SALT_SIZE;
QByteArray iv = packed.mid(off, ivSize); off += ivSize;
QByteArray tag;
if (aead) { tag = packed.mid(off, TAG_SIZE); off += TAG_SIZE; }
QByteArray ct = packed.mid(off);
QByteArray key = deriveKey(passphrase, salt, keyLen);
EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
if (!ctx)
return {false, {}, "Failed to create cipher context"};
int rc = EVP_DecryptInit_ex(ctx, cipher, nullptr,
reinterpret_cast<const unsigned char *>(key.constData()),
reinterpret_cast<const unsigned char *>(iv.constData()));
if (rc != 1) { EVP_CIPHER_CTX_free(ctx); return {false, {}, "Decrypt init failed"}; }
QByteArray out(ct.size() + EVP_CIPHER_block_size(cipher), '\0');
int len = 0, total = 0;
rc = EVP_DecryptUpdate(ctx,
reinterpret_cast<unsigned char *>(out.data()), &len,
reinterpret_cast<const unsigned char *>(ct.constData()), ct.size());
if (rc != 1) { EVP_CIPHER_CTX_free(ctx); return {false, {}, "Decrypt update failed"}; }
total = len;
if (aead) {
EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, TAG_SIZE,
const_cast<char *>(tag.constData()));
}
rc = EVP_DecryptFinal_ex(ctx, reinterpret_cast<unsigned char *>(out.data()) + total, &len);
if (rc != 1) {
EVP_CIPHER_CTX_free(ctx);
return {false, {}, "Decryption failed — wrong passphrase or corrupted data"};
}
total += len;
out.resize(total);
EVP_CIPHER_CTX_free(ctx);
return {true, out, {}};
}
QString CryptoEngine::algorithmName(Algorithm algo) {
switch (algo) {
case AES_256_GCM: return "AES-256-GCM";
case AES_128_GCM: return "AES-128-GCM";
case AES_256_CBC: return "AES-256-CBC";
case CHACHA20_POLY1305: return "ChaCha20-Poly1305";
}
return "Unknown";
}
CryptoEngine::Algorithm CryptoEngine::algorithmFromIndex(int index) {
switch (index) {
case 0: return AES_256_GCM;
case 1: return AES_128_GCM;
case 2: return AES_256_CBC;
case 3: return CHACHA20_POLY1305;
default: return AES_256_GCM;
}
}