-
Notifications
You must be signed in to change notification settings - Fork 0
/
Crypto.cpp
383 lines (291 loc) · 9.97 KB
/
Crypto.cpp
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#include "Crypto.h"
using namespace std;
EVP_PKEY* Crypto::localKeypair;
Crypto::Crypto() {
localKeypair = NULL;
remotePublicKey = NULL;
#ifdef PSEUDO_CLIENT
generateRsaKeypair(&remotePublicKey);
#endif
init();
}
Crypto::Crypto(unsigned char *remotePublicKey, size_t remotePublicKeyLength) {
localKeypair = NULL;
this->remotePublicKey = NULL;
setRemotePublicKey(remotePublicKey, remotePublicKeyLength);
init();
}
Crypto::~Crypto() {
EVP_PKEY_free(remotePublicKey);
EVP_CIPHER_CTX_free(rsaEncryptContext);
EVP_CIPHER_CTX_free(aesEncryptContext);
EVP_CIPHER_CTX_free(rsaDecryptContext);
EVP_CIPHER_CTX_free(aesDecryptContext);
free(aesKey);
free(aesIv);
}
int Crypto::init() {
// Initalize contexts
rsaEncryptContext = EVP_CIPHER_CTX_new();
aesEncryptContext = EVP_CIPHER_CTX_new();
rsaDecryptContext = EVP_CIPHER_CTX_new();
aesDecryptContext = EVP_CIPHER_CTX_new();
// Check if any of the contexts initializations failed
if(rsaEncryptContext == NULL || aesEncryptContext == NULL || rsaDecryptContext == NULL || aesDecryptContext == NULL) {
return FAILURE;
}
/* Don't set key or IV right away; we want to set lengths */
EVP_CIPHER_CTX_init(aesEncryptContext);
EVP_CIPHER_CTX_init(aesDecryptContext);
EVP_CipherInit_ex(aesEncryptContext, EVP_aes_256_cbc(), NULL, NULL, NULL, 1);
/* Now we can set key and IV lengths */
aesKeyLength = EVP_CIPHER_CTX_key_length(aesEncryptContext);
aesIvLength = EVP_CIPHER_CTX_iv_length(aesEncryptContext);
// Generate RSA and AES keys
generateRsaKeypair(&localKeypair);
generateAesKey(&aesKey, &aesIv);
return SUCCESS;
}
//生成密钥对
int Crypto::generateRsaKeypair(EVP_PKEY **keypair) {
EVP_PKEY_CTX *context = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL);
if(EVP_PKEY_keygen_init(context) <= 0) {
return FAILURE;
}
if(EVP_PKEY_CTX_set_rsa_keygen_bits(context, RSA_KEYLEN) <= 0) {
return FAILURE;
}
if(EVP_PKEY_keygen(context, keypair) <= 0) {
return FAILURE;
}
EVP_PKEY_CTX_free(context);
return SUCCESS;
}
int Crypto::generateAesKey(unsigned char **aesKey, unsigned char **aesIv) {
*aesKey = (unsigned char*)malloc(aesKeyLength);
*aesIv = (unsigned char*)malloc(aesIvLength);
if(aesKey == NULL || aesIv == NULL) {
return FAILURE;
}
// For the AES key we have the option of using a PBKDF or just using straight random
// data for the key and IV. Depending on your use case, you will want to pick one or another.
#ifdef USE_PBKDF
unsigned char *aesPass = (unsigned char*)malloc(aesKeyLength);
unsigned char *aesSalt = (unsigned char*)malloc(8);
if(aesPass == NULL || aesSalt == NULL) {
return FAILURE;
}
// Get some random data to use as the AES pass and salt
if(RAND_bytes(aesPass, aesKeyLength) == 0) {
return FAILURE;
}
if(RAND_bytes(aesSalt, 8) == 0) {
return FAILURE;
}
if(EVP_BytesToKey(EVP_aes_256_cbc(), EVP_sha256(), aesSalt, aesPass, aesKeyLength, AES_ROUNDS, aesKey, aesIv) == 0) {
return FAILURE;
}
free(aesPass);
free(aesSalt);
#else
if(RAND_bytes(*aesKey, aesKeyLength) == 0) {
return FAILURE;
}
if(RAND_bytes(*aesIv, aesIvLength) == 0) {
return FAILURE;
}
#endif
return SUCCESS;
}
int Crypto::rsaEncrypt(const unsigned char *message, size_t messageLength, unsigned char **encryptedMessage, unsigned char **encryptedKey,
size_t *encryptedKeyLength, unsigned char **iv, size_t *ivLength) {
// Allocate memory for everything
size_t encryptedMessageLength = 0;
size_t blockLength = 0;
*encryptedKey = (unsigned char*)malloc(EVP_PKEY_size(remotePublicKey));
*iv = (unsigned char*)malloc(EVP_MAX_IV_LENGTH);
*ivLength = EVP_MAX_IV_LENGTH;
if(*encryptedKey == NULL || *iv == NULL) {
return FAILURE;
}
*encryptedMessage = (unsigned char*)malloc(messageLength + EVP_MAX_IV_LENGTH);
if(encryptedMessage == NULL) {
return FAILURE;
}
// Encrypt it!
if(!EVP_SealInit(rsaEncryptContext, EVP_aes_256_cbc(), encryptedKey, (int*)encryptedKeyLength, *iv, &remotePublicKey, 1)) {
return FAILURE;
}
if(!EVP_SealUpdate(rsaEncryptContext, *encryptedMessage + encryptedMessageLength, (int*)&blockLength, (const unsigned char*)message, (int)messageLength)) {
return FAILURE;
}
encryptedMessageLength += blockLength;
if(!EVP_SealFinal(rsaEncryptContext, *encryptedMessage + encryptedMessageLength, (int*)&blockLength)) {
return FAILURE;
}
encryptedMessageLength += blockLength;
return (int)encryptedMessageLength;
}
int Crypto::rsaDecrypt(unsigned char *encryptedMessage, size_t encryptedMessageLength, unsigned char *encryptedKey,
size_t encryptedKeyLength, unsigned char *iv, size_t ivLength, unsigned char **decryptedMessage) {
// Allocate memory for everything
size_t decryptedMessageLength = 0;
size_t blockLength = 0;
*decryptedMessage = (unsigned char*)malloc(encryptedMessageLength + ivLength);
if(*decryptedMessage == NULL) {
return FAILURE;
}
#ifdef PSEUDO_CLIENT
EVP_PKEY *key = remotePublicKey;
#else
EVP_PKEY *key = localKeypair;
#endif
// Decrypt it!
if(!EVP_OpenInit(rsaDecryptContext, EVP_aes_256_cbc(), encryptedKey, encryptedKeyLength, iv, key)) {
return FAILURE;
}
if(!EVP_OpenUpdate(rsaDecryptContext, (unsigned char*)*decryptedMessage + decryptedMessageLength, (int*)&blockLength, encryptedMessage, (int)encryptedMessageLength)) {
return FAILURE;
}
decryptedMessageLength += blockLength;
if(!EVP_OpenFinal(rsaDecryptContext, (unsigned char*)*decryptedMessage + decryptedMessageLength, (int*)&blockLength)) {
return FAILURE;
}
decryptedMessageLength += blockLength;
return (int)decryptedMessageLength;
}
int Crypto::aesEncrypt(const unsigned char *message, size_t messageLength, unsigned char **encryptedMessage) {
// Allocate memory for everything
size_t blockLength = 0;
size_t encryptedMessageLength = 0;
*encryptedMessage = (unsigned char*)malloc(messageLength + AES_BLOCK_SIZE);
if(encryptedMessage == NULL) {
return FAILURE;
}
// Encrypt it!
if(!EVP_EncryptInit_ex(aesEncryptContext, EVP_aes_256_cbc(), NULL, aesKey, aesIv)) {
return FAILURE;
}
if(!EVP_EncryptUpdate(aesEncryptContext, *encryptedMessage, (int*)&blockLength, (unsigned char*)message, messageLength)) {
return FAILURE;
}
encryptedMessageLength += blockLength;
if(!EVP_EncryptFinal_ex(aesEncryptContext, *encryptedMessage + encryptedMessageLength, (int*)&blockLength)) {
return FAILURE;
}
return encryptedMessageLength + blockLength;
}
int Crypto::aesDecrypt(unsigned char *encryptedMessage, size_t encryptedMessageLength, unsigned char **decryptedMessage) {
// Allocate memory for everything
size_t decryptedMessageLength = 0;
size_t blockLength = 0;
*decryptedMessage = (unsigned char*)malloc(encryptedMessageLength);
if(*decryptedMessage == NULL) {
return FAILURE;
}
// Decrypt it!
if(!EVP_DecryptInit_ex(aesDecryptContext, EVP_aes_256_cbc(), NULL, aesKey, aesIv)) {
return FAILURE;
}
if(!EVP_DecryptUpdate(aesDecryptContext, (unsigned char*)*decryptedMessage, (int*)&blockLength, encryptedMessage, (int)encryptedMessageLength)) {
return FAILURE;
}
decryptedMessageLength += blockLength;
if(!EVP_DecryptFinal_ex(aesDecryptContext, (unsigned char*)*decryptedMessage + decryptedMessageLength, (int*)&blockLength)) {
return FAILURE;
}
decryptedMessageLength += blockLength;
return (int)decryptedMessageLength;
}
int Crypto::getRemotePublicKey(unsigned char **publicKey) {
BIO *bio = BIO_new(BIO_s_mem());
PEM_write_bio_PUBKEY(bio, remotePublicKey);
return bioToString(bio, publicKey);
}
int Crypto::setRemotePublicKey(unsigned char *publicKey, size_t publicKeyLength) {
BIO *bio = BIO_new(BIO_s_mem());
if(BIO_write(bio, publicKey, publicKeyLength) != (int)publicKeyLength) {
return FAILURE;
}
PEM_read_bio_PUBKEY(bio, &remotePublicKey, NULL, NULL);
BIO_free_all(bio);
return SUCCESS;
}
int Crypto::getLocalPublicKey(unsigned char **publicKey) {
BIO *bio = BIO_new(BIO_s_mem());
PEM_write_bio_PUBKEY(bio, localKeypair);
return bioToString(bio, publicKey);
}
int Crypto::getLocalPrivateKey(unsigned char **privateKey) {
BIO *bio = BIO_new(BIO_s_mem());
PEM_write_bio_PrivateKey(bio, localKeypair, NULL, NULL, 0, 0, NULL);
return bioToString(bio, privateKey);
}
int Crypto::getAesKey(unsigned char **aesKey) {
*aesKey = this->aesKey;
return aesKeyLength;
}
unsigned char * Crypto::getAesKey()
{
return this->aesKey;
}
int Crypto::setAesKey(unsigned char *aesKey, size_t aesKeyLengthgth) {
// Ensure the new key is the proper size
if(aesKeyLengthgth != aesKeyLength) {
return FAILURE;
}
memcpy(this->aesKey, aesKey, aesKeyLength);
return SUCCESS;
}
int Crypto::getAesIv(unsigned char **aesIv) {
*aesIv = this->aesIv;
return aesIvLength;
}
int Crypto::setAesIv(unsigned char *aesIv, size_t aesIvLengthgth) {
// Ensure the new IV is the proper size
if(aesIvLengthgth != aesIvLength) {
return FAILURE;
}
memcpy(this->aesIv, aesIv, aesIvLength);
return SUCCESS;
}
int Crypto::writeKeyToFile(FILE *file, int key) {
switch(key) {
case KEY_SERVER_PRI:
if(!PEM_write_PrivateKey(file, localKeypair, NULL, NULL, 0, 0, NULL)) {
return FAILURE;
}
break;
case KEY_SERVER_PUB:
if(!PEM_write_PUBKEY(file, localKeypair)) {
return FAILURE;
}
break;
case KEY_CLIENT_PUB:
if(!PEM_write_PUBKEY(file, remotePublicKey)) {
return FAILURE;
}
break;
case KEY_AES:
fwrite(aesKey, 1, aesKeyLength * 8, file);
break;
case KEY_AES_IV:
fwrite(aesIv, 1, aesIvLength * 8, file);
break;
default:
return FAILURE;
}
return SUCCESS;
}
int Crypto::bioToString(BIO *bio, unsigned char **string) {
size_t bioLength = BIO_pending(bio);
*string = (unsigned char*)malloc(bioLength + 1);
if(string == NULL) {
return FAILURE;
}
BIO_read(bio, *string, bioLength);
// Insert the NUL terminator
(*string)[bioLength] = '\0';
BIO_free_all(bio);
return (int)bioLength;
}